Clean up EmergeManager, do initial work on Mapgen configuration
[oweals/minetest.git] / src / mapgen.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MAPGEN_HEADER
21 #define MAPGEN_HEADER
22
23 #include "irrlichttypes_extrabloated.h"
24 #include "util/container.h" // UniqueQueue
25 #include "gamedef.h"
26 #include "mapnode.h"
27 #include "noise.h"
28
29 class BiomeDefManager;
30 class Biome;
31
32 //struct BlockMakeData;
33 class MapBlock;
34 class ManualMapVoxelManipulator;
35 class VoxelManipulator;
36 class INodeDefManager;
37
38
39 enum BiomeType
40 {
41         BT_NORMAL,
42         BT_DESERT
43 };
44
45
46 struct BlockMakeData {
47         bool no_op;
48         ManualMapVoxelManipulator *vmanip;
49         u64 seed;
50         v3s16 blockpos_min;
51         v3s16 blockpos_max;
52         v3s16 blockpos_requested;
53         UniqueQueue<v3s16> transforming_liquid;
54         INodeDefManager *nodedef;
55
56         BlockMakeData();
57         ~BlockMakeData();
58 };
59
60
61 /////////////////// Mapgen flags
62 #define MG_TREES         0x01
63 #define MG_CAVES         0x02
64 #define MG_DUNGEONS      0x04
65 #define MGV6_FORESTS     0x08
66 #define MGV6_BIOME_BLEND 0x10
67
68 #define AVERAGE_MUD_AMOUNT 4
69
70 /////////////////// Mapgen V6 perlin noise default values
71 NoiseParams nparams_v6_def_terrain_base =
72         {-AVERAGE_MUD_AMOUNT, 20.0, v3f(250.0, 250.0, 250.0), 82341, 5, 0.6};
73 NoiseParams nparams_v6_def_terrain_higher =
74         {20.0, 16.0, v3f(500.0, 500.0, 500.0), 85309, 5, 0.6};
75 NoiseParams nparams_v6_def_steepness =
76         {0.85, 0.5, v3f(125.0, 125.0, 125.0), -932, 5, 0.7};
77 NoiseParams nparams_v6_def_height_select =
78         {0.5, 1.0, v3f(250.0, 250.0, 250.0), 4213, 5, 0.69};
79 NoiseParams nparams_v6_def_trees =
80         {0.0, 1.0, v3f(125.0, 125.0, 125.0), 2, 4, 0.66};
81 NoiseParams nparams_v6_def_mud =
82         {AVERAGE_MUD_AMOUNT, 2.0, v3f(200.0, 200.0, 200.0), 91013, 3, 0.55};
83 NoiseParams nparams_v6_def_beach =
84         {0.0, 1.0, v3f(250.0, 250.0, 250.0), 59420, 3, 0.50};
85 NoiseParams nparams_v6_def_biome =
86         {0.0, 1.0, v3f(250.0, 250.0, 250.0), 9130, 3, 0.50};
87 NoiseParams nparams_v6_def_cave =
88         {6.0, 6.0, v3f(250.0, 250.0, 250.0), 34329, 3, 0.50};
89
90 /////////////////// Mapgen V7 perlin noise default values
91 NoiseParams nparams_v7_def_terrain =
92         {10.0, 12.0, v3f(350., 350., 350.), 82341, 5, 0.6}; //terrain
93 NoiseParams nparams_v7_def_bgroup =
94         {0.5, 1/(2*1.6), v3f(350., 350., 350.), 5923, 2, 0.60}; //0 to 1
95 NoiseParams nparams_v7_def_heat =
96         {25.0, 50.0, v3f(500., 500., 500.), 35293, 1, 0.00}; //-25 to 75
97 NoiseParams nparams_v7_def_humidity =
98         {50, 100/(2*1.6), v3f(750., 750., 750.), 12094, 2, 0.60}; //0 to 100
99
100 struct MapgenParams {
101         int seed;
102         int water_level;
103         int chunksize;
104         u32 flags;
105
106         MapgenParams() {
107                 seed        = 0;
108                 water_level = 1;
109                 chunksize   = 5;
110                 flags       = MG_TREES | MG_CAVES | MGV6_BIOME_BLEND;
111         }
112
113 };
114
115 struct MapgenV6Params : public MapgenParams {
116         float freq_desert;
117         float freq_beach;
118         NoiseParams *np_terrain_base;
119         NoiseParams *np_terrain_higher;
120         NoiseParams *np_steepness;
121         NoiseParams *np_height_select;
122         NoiseParams *np_trees;
123         NoiseParams *np_mud;
124         NoiseParams *np_beach;
125         NoiseParams *np_biome;
126         NoiseParams *np_cave;
127
128         MapgenV6Params() {
129                 freq_desert       = 0.45;
130                 freq_beach        = 0.15;
131                 np_terrain_base   = &nparams_v6_def_terrain_base;
132                 np_terrain_higher = &nparams_v6_def_terrain_higher;
133                 np_steepness      = &nparams_v6_def_steepness;
134                 np_height_select  = &nparams_v6_def_height_select;
135                 np_trees          = &nparams_v6_def_trees;
136                 np_mud            = &nparams_v6_def_mud;
137                 np_beach          = &nparams_v6_def_beach;
138                 np_biome          = &nparams_v6_def_biome;
139                 np_cave           = &nparams_v6_def_cave;
140         }
141 };
142
143 struct MapgenV7Params : public MapgenParams {
144         NoiseParams *np_terrain;
145         NoiseParams *np_bgroup;
146         NoiseParams *np_heat;
147         NoiseParams *np_humidity;
148
149         MapgenV7Params() {
150                 np_terrain  = &nparams_v7_def_terrain;
151                 np_bgroup   = &nparams_v7_def_bgroup;
152                 np_heat     = &nparams_v7_def_heat;
153                 np_humidity = &nparams_v7_def_humidity;
154         }
155 };
156
157
158 class Mapgen {
159 public:
160         int seed;
161         int water_level;
162         bool generating;
163         int id;
164
165         virtual void makeChunk(BlockMakeData *data) {};
166
167         //Legacy functions for Farmesh (pending removal)
168         static bool get_have_beach(u64 seed, v2s16 p2d);
169         static double tree_amount_2d(u64 seed, v2s16 p);
170         static s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision);
171 };
172
173
174 class MapgenV6 : public Mapgen {
175 public:
176         //ManualMapVoxelManipulator &vmanip;
177
178         int ystride;
179         v3s16 csize;
180
181         v3s16 node_min;
182         v3s16 node_max;
183
184         Noise *noise_terrain_base;
185         Noise *noise_terrain_higher;
186         Noise *noise_steepness;
187         Noise *noise_height_select;
188         Noise *noise_trees;
189         Noise *noise_mud;
190         Noise *noise_beach;
191         Noise *noise_biome;
192         Noise *noise_cave;
193
194         float *map_terrain_base;
195         float *map_terrain_higher;
196         float *map_steepness;
197         float *map_height_select;
198         float *map_trees;
199         float *map_mud;
200         float *map_beach;
201         float *map_biome;
202         float *map_cave;
203
204         u32 flags;
205         float freq_desert;
206         float freq_beach;
207
208         MapgenV6(int mapgenid, MapgenV6Params *params);
209         ~MapgenV6();
210
211         void makeChunk(BlockMakeData *data);
212
213
214         static s16 find_ground_level(VoxelManipulator &vmanip, v2s16 p2d, INodeDefManager *ndef);
215         static s16 find_stone_level(VoxelManipulator &vmanip, v2s16 p2d, INodeDefManager *ndef);
216         void make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0, bool is_apple_tree, INodeDefManager *ndef);
217         double tree_amount_2d(u64 seed, v2s16 p);
218         bool block_is_underground(u64 seed, v3s16 blockpos);
219         double base_rock_level_2d(u64 seed, v2s16 p);
220         s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision);
221         double get_mud_add_amount(u64 seed, v2s16 p);
222         bool get_have_beach(u64 seed, v2s16 p2d);
223         BiomeType get_biome(u64 seed, v2s16 p2d);
224         u32 get_blockseed(u64 seed, v3s16 p);
225 };
226
227
228 class MapgenV7 : public Mapgen {
229 public:
230         BlockMakeData *data;
231         ManualMapVoxelManipulator *vmanip;
232         INodeDefManager *ndef;
233         BiomeDefManager *biomedef;
234
235         int ystride;
236         int zstride;
237
238         v3s16 csize;
239
240         Noise *noise_terrain;
241         Noise *noise_bgroup;
242         Noise *noise_heat;
243         Noise *noise_humidity;
244
245         v3s16 node_min;
246         v3s16 node_max;
247
248         float *map_terrain;
249         float *map_bgroup;
250         float *map_heat;
251         float *map_humidity;
252
253         bool generating;
254         int id;
255         u32 flags;
256
257 /*
258         NoiseParams *np_terrain;
259         NoiseParams *np_bgroup;
260         NoiseParams *np_heat;
261         NoiseParams *np_humidity;*/
262
263         //should these be broken off into a "commonly used nodes" class?
264         MapNode n_air;
265         MapNode n_water;
266         MapNode n_lava;
267
268         MapgenV7(BiomeDefManager *biomedef, int mapgenid, MapgenV7Params *params);
269         ~MapgenV7();
270
271         void makeChunk(BlockMakeData *data);
272         void updateLiquid(v3s16 node_min, v3s16 node_max);
273         void updateLighting(v3s16 node_min, v3s16 node_max);
274
275         //Legacy functions for Farmesh (pending removal)
276 //      static bool get_have_beach(u64 seed, v2s16 p2d);
277 //      static double tree_amount_2d(u64 seed, v2s16 p);
278 //      static s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision);
279 };
280
281 class EmergeManager {
282 public:
283         //settings
284         int mg_version;
285         MapgenParams *params;
286
287         //mapgen objects here
288         Mapgen *mapgen;
289
290         //biome manager
291         BiomeDefManager *biomedef;
292
293         EmergeManager(IGameDef *gamedef, int mg_version=6);
294         ~EmergeManager();
295
296         Mapgen *getMapgen();
297         void setMapgenParams();
298         void addBlockToQueue();
299
300         //mapgen helper methods
301         Biome *getBiomeAtPoint(v3s16 p);
302         int getGroundLevelAtPoint(v2s16 p);
303         bool isBlockUnderground(v3s16 blockpos);
304         u32 getBlockSeed(v3s16 p);
305 };
306
307
308 /*
309 namespace mapgen
310 {
311         // Finds precise ground level at any position
312         s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision);
313
314         // Find out if block is completely underground
315         bool block_is_underground(u64 seed, v3s16 blockpos);
316
317         // Get a pseudorandom seed for a position on the map
318         u32 get_blockseed(u64 seed, v3s16 p);
319
320         // Main map generation routine
321         void make_block(BlockMakeData *data);
322
323
324         //These are used by FarMesh
325         bool get_have_beach(u64 seed, v2s16 p2d);
326         double tree_amount_2d(u64 seed, v2s16 p);
327
328         struct BlockMakeData
329         {
330                 bool no_op;
331                 ManualMapVoxelManipulator *vmanip; // Destructor deletes
332                 u64 seed;
333                 v3s16 blockpos_min;
334                 v3s16 blockpos_max;
335                 v3s16 blockpos_requested;
336                 UniqueQueue<v3s16> transforming_liquid;
337                 INodeDefManager *nodedef;
338
339                 BlockMakeData();
340                 ~BlockMakeData();
341         };
342
343 }; // namespace mapgen
344 */
345 #endif
346