Dungeongen: Remove dependency on Mapgen
[oweals/minetest.git] / src / mapgen.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "mapgen.h"
22 #include "voxel.h"
23 #include "noise.h"
24 #include "gamedef.h"
25 #include "mg_biome.h"
26 #include "mapblock.h"
27 #include "mapnode.h"
28 #include "map.h"
29 #include "content_sao.h"
30 #include "nodedef.h"
31 #include "emerge.h"
32 #include "voxelalgorithms.h"
33 #include "porting.h"
34 #include "profiler.h"
35 #include "settings.h"
36 #include "treegen.h"
37 #include "serialization.h"
38 #include "util/serialize.h"
39 #include "util/numeric.h"
40 #include "filesys.h"
41 #include "log.h"
42 #include "cavegen.h"
43 #include "dungeongen.h"
44
45 FlagDesc flagdesc_mapgen[] = {
46         {"trees",       MG_TREES},
47         {"caves",       MG_CAVES},
48         {"dungeons",    MG_DUNGEONS},
49         {"flat",        MG_FLAT},
50         {"light",       MG_LIGHT},
51         {"decorations", MG_DECORATIONS},
52         {NULL,       0}
53 };
54
55 FlagDesc flagdesc_gennotify[] = {
56         {"dungeon",          1 << GENNOTIFY_DUNGEON},
57         {"temple",           1 << GENNOTIFY_TEMPLE},
58         {"cave_begin",       1 << GENNOTIFY_CAVE_BEGIN},
59         {"cave_end",         1 << GENNOTIFY_CAVE_END},
60         {"large_cave_begin", 1 << GENNOTIFY_LARGECAVE_BEGIN},
61         {"large_cave_end",   1 << GENNOTIFY_LARGECAVE_END},
62         {"decoration",       1 << GENNOTIFY_DECORATION},
63         {NULL,               0}
64 };
65
66
67 ////
68 //// Mapgen
69 ////
70
71 Mapgen::Mapgen()
72 {
73         generating  = false;
74         id          = -1;
75         seed        = 0;
76         water_level = 0;
77         flags       = 0;
78
79         vm        = NULL;
80         ndef      = NULL;
81         biomegen  = NULL;
82         biomemap  = NULL;
83         heightmap = NULL;
84 }
85
86
87 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
88         gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
89 {
90         generating  = false;
91         id          = mapgenid;
92         seed        = (int)params->seed;
93         water_level = params->water_level;
94         flags       = params->flags;
95         csize       = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
96
97         vm        = NULL;
98         ndef      = emerge->ndef;
99         biomegen  = NULL;
100         biomemap  = NULL;
101         heightmap = NULL;
102 }
103
104
105 Mapgen::~Mapgen()
106 {
107 }
108
109
110 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
111 {
112         return (u32)seed   +
113                 p.Z * 38134234 +
114                 p.Y * 42123    +
115                 p.X * 23;
116 }
117
118
119 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
120 {
121         u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
122         n = (n >> 13) ^ n;
123         return (n * (n * n * 60493 + 19990303) + 1376312589);
124 }
125
126
127 // Returns Y one under area minimum if not found
128 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
129 {
130         v3s16 em = vm->m_area.getExtent();
131         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
132         s16 y_nodes_min = vm->m_area.MinEdge.Y;
133         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
134         s16 y;
135
136         for (y = y_nodes_max; y >= y_nodes_min; y--) {
137                 MapNode &n = vm->m_data[i];
138                 if (ndef->get(n).walkable)
139                         break;
140
141                 vm->m_area.add_y(em, i, -1);
142         }
143         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
144 }
145
146
147 // Returns -MAX_MAP_GENERATION_LIMIT if not found
148 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
149 {
150         v3s16 em = vm->m_area.getExtent();
151         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
152         s16 y;
153
154         for (y = ymax; y >= ymin; y--) {
155                 MapNode &n = vm->m_data[i];
156                 if (ndef->get(n).walkable)
157                         break;
158
159                 vm->m_area.add_y(em, i, -1);
160         }
161         return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
162 }
163
164
165 // Returns -MAX_MAP_GENERATION_LIMIT if not found or if ground is found first
166 s16 Mapgen::findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax)
167 {
168         v3s16 em = vm->m_area.getExtent();
169         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
170         s16 y;
171
172         for (y = ymax; y >= ymin; y--) {
173                 MapNode &n = vm->m_data[i];
174                 if (ndef->get(n).walkable)
175                         return -MAX_MAP_GENERATION_LIMIT;
176                 else if (ndef->get(n).isLiquid())
177                         break;
178
179                 vm->m_area.add_y(em, i, -1);
180         }
181         return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
182 }
183
184
185 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
186 {
187         if (!heightmap)
188                 return;
189
190         //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
191         int index = 0;
192         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
193                 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
194                         s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
195
196                         heightmap[index] = y;
197                 }
198         }
199         //printf("updateHeightmap: %dus\n", t.stop());
200 }
201
202
203 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
204 {
205         bool isliquid, wasliquid;
206         v3s16 em  = vm->m_area.getExtent();
207
208         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
209                 for (s16 x = nmin.X; x <= nmax.X; x++) {
210                         wasliquid = true;
211
212                         u32 i = vm->m_area.index(x, nmax.Y, z);
213                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
214                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
215
216                                 // there was a change between liquid and nonliquid, add to queue.
217                                 if (isliquid != wasliquid)
218                                         trans_liquid->push_back(v3s16(x, y, z));
219
220                                 wasliquid = isliquid;
221                                 vm->m_area.add_y(em, i, -1);
222                         }
223                 }
224         }
225 }
226
227
228 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
229 {
230         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
231         VoxelArea a(nmin, nmax);
232
233         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
234                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
235                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
236                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
237                                 vm->m_data[i].param1 = light;
238                 }
239         }
240 }
241
242
243 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
244 {
245         if (light <= 1 || !a.contains(p))
246                 return;
247
248         u32 vi = vm->m_area.index(p);
249         MapNode &n = vm->m_data[vi];
250
251         // Decay light in each of the banks separately
252         u8 light_day = light & 0x0F;
253         if (light_day > 0)
254                 light_day -= 0x01;
255
256         u8 light_night = light & 0xF0;
257         if (light_night > 0)
258                 light_night -= 0x10;
259
260         // Bail out only if we have no more light from either bank to propogate, or
261         // we hit a solid block that light cannot pass through.
262         if ((light_day  <= (n.param1 & 0x0F) &&
263                 light_night <= (n.param1 & 0xF0)) ||
264                 !ndef->get(n).light_propagates)
265                 return;
266
267         // Since this recursive function only terminates when there is no light from
268         // either bank left, we need to take the max of both banks into account for
269         // the case where spreading has stopped for one light bank but not the other.
270         light = MYMAX(light_day, n.param1 & 0x0F) |
271                         MYMAX(light_night, n.param1 & 0xF0);
272
273         n.param1 = light;
274
275         lightSpread(a, p + v3s16(0, 0, 1), light);
276         lightSpread(a, p + v3s16(0, 1, 0), light);
277         lightSpread(a, p + v3s16(1, 0, 0), light);
278         lightSpread(a, p - v3s16(0, 0, 1), light);
279         lightSpread(a, p - v3s16(0, 1, 0), light);
280         lightSpread(a, p - v3s16(1, 0, 0), light);
281 }
282
283
284 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
285         bool propagate_shadow)
286 {
287         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
288         //TimeTaker t("updateLighting");
289
290         propagateSunlight(nmin, nmax, propagate_shadow);
291         spreadLight(full_nmin, full_nmax);
292
293         //printf("updateLighting: %dms\n", t.stop());
294 }
295
296
297 void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow)
298 {
299         //TimeTaker t("propagateSunlight");
300         VoxelArea a(nmin, nmax);
301         bool block_is_underground = (water_level >= nmax.Y);
302         v3s16 em = vm->m_area.getExtent();
303
304         // NOTE: Direct access to the low 4 bits of param1 is okay here because,
305         // by definition, sunlight will never be in the night lightbank.
306
307         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
308                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
309                         // see if we can get a light value from the overtop
310                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
311                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
312                                 if (block_is_underground)
313                                         continue;
314                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN &&
315                                         propagate_shadow) {
316                                 continue;
317                         }
318                         vm->m_area.add_y(em, i, -1);
319
320                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
321                                 MapNode &n = vm->m_data[i];
322                                 if (!ndef->get(n).sunlight_propagates)
323                                         break;
324                                 n.param1 = LIGHT_SUN;
325                                 vm->m_area.add_y(em, i, -1);
326                         }
327                 }
328         }
329         //printf("propagateSunlight: %dms\n", t.stop());
330 }
331
332
333 void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
334 {
335         //TimeTaker t("spreadLight");
336         VoxelArea a(nmin, nmax);
337
338         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
339                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
340                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
341                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
342                                 MapNode &n = vm->m_data[i];
343                                 if (n.getContent() == CONTENT_IGNORE)
344                                         continue;
345
346                                 const ContentFeatures &cf = ndef->get(n);
347                                 if (!cf.light_propagates)
348                                         continue;
349
350                                 // TODO(hmmmmm): Abstract away direct param1 accesses with a
351                                 // wrapper, but something lighter than MapNode::get/setLight
352
353                                 u8 light_produced = cf.light_source;
354                                 if (light_produced)
355                                         n.param1 = light_produced | (light_produced << 4);
356
357                                 u8 light = n.param1;
358                                 if (light) {
359                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
360                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
361                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
362                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
363                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
364                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
365                                 }
366                         }
367                 }
368         }
369
370         //printf("spreadLight: %dms\n", t.stop());
371 }
372
373
374 ////
375 //// MapgenBasic
376 ////
377
378 MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge)
379         : Mapgen(mapgenid, params, emerge)
380 {
381         this->m_emerge = emerge;
382         this->m_bmgr   = emerge->biomemgr;
383
384         //// Here, 'stride' refers to the number of elements needed to skip to index
385         //// an adjacent element for that coordinate in noise/height/biome maps
386         //// (*not* vmanip content map!)
387
388         // Note there is no X stride explicitly defined.  Items adjacent in the X
389         // coordinate are assumed to be adjacent in memory as well (i.e. stride of 1).
390
391         // Number of elements to skip to get to the next Y coordinate
392         this->ystride = csize.X;
393
394         // Number of elements to skip to get to the next Z coordinate
395         this->zstride = csize.X * csize.Y;
396
397         // Z-stride value for maps oversized for 1-down overgeneration
398         this->zstride_1d = csize.X * (csize.Y + 1);
399
400         // Z-stride value for maps oversized for 1-up 1-down overgeneration
401         this->zstride_1u1d = csize.X * (csize.Y + 2);
402
403         //// Allocate heightmap
404         this->heightmap = new s16[csize.X * csize.Z];
405
406         //// Initialize biome generator
407         // TODO(hmmmm): should we have a way to disable biomemanager biomes?
408         biomegen = m_bmgr->createBiomeGen(BIOMEGEN_ORIGINAL, params->bparams, csize);
409         biomemap = biomegen->biomemap;
410
411         //// Look up some commonly used content
412         c_stone              = ndef->getId("mapgen_stone");
413         c_water_source       = ndef->getId("mapgen_water_source");
414         c_desert_stone       = ndef->getId("mapgen_desert_stone");
415         c_sandstone          = ndef->getId("mapgen_sandstone");
416         c_river_water_source = ndef->getId("mapgen_river_water_source");
417
418         // Fall back to more basic content if not defined
419         if (c_desert_stone == CONTENT_IGNORE)
420                 c_desert_stone = c_stone;
421         if (c_sandstone == CONTENT_IGNORE)
422                 c_sandstone = c_stone;
423         if (c_river_water_source == CONTENT_IGNORE)
424                 c_river_water_source = c_water_source;
425
426         //// Content used for dungeon generation
427         c_cobble               = ndef->getId("mapgen_cobble");
428         c_stair_cobble         = ndef->getId("mapgen_stair_cobble");
429         c_mossycobble          = ndef->getId("mapgen_mossycobble");
430         c_sandstonebrick       = ndef->getId("mapgen_sandstonebrick");
431         c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
432
433         // Fall back to more basic content if not defined
434         if (c_mossycobble == CONTENT_IGNORE)
435                 c_mossycobble = c_cobble;
436         if (c_stair_cobble == CONTENT_IGNORE)
437                 c_stair_cobble = c_cobble;
438         if (c_sandstonebrick == CONTENT_IGNORE)
439                 c_sandstonebrick = c_sandstone;
440         if (c_stair_sandstonebrick == CONTENT_IGNORE)
441                 c_stair_sandstonebrick = c_sandstone;
442 }
443
444
445 MapgenBasic::~MapgenBasic()
446 {
447         delete biomegen;
448         delete []heightmap;
449 }
450
451
452 MgStoneType MapgenBasic::generateBiomes()
453 {
454         v3s16 em = vm->m_area.getExtent();
455         u32 index = 0;
456         MgStoneType stone_type = MGSTONE_STONE;
457
458         noise_filler_depth->perlinMap2D(node_min.X, node_min.Z);
459
460         for (s16 z = node_min.Z; z <= node_max.Z; z++)
461         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
462                 Biome *biome = NULL;
463                 u16 depth_top = 0;
464                 u16 base_filler = 0;
465                 u16 depth_water_top = 0;
466                 u32 vi = vm->m_area.index(x, node_max.Y, z);
467
468                 // Check node at base of mapchunk above, either a node of a previously
469                 // generated mapchunk or if not, a node of overgenerated base terrain.
470                 content_t c_above = vm->m_data[vi + em.X].getContent();
471                 bool air_above = c_above == CONTENT_AIR;
472                 bool water_above = (c_above == c_water_source || c_above == c_river_water_source);
473
474                 // If there is air or water above enable top/filler placement, otherwise force
475                 // nplaced to stone level by setting a number exceeding any possible filler depth.
476                 u16 nplaced = (air_above || water_above) ? 0 : U16_MAX;
477
478                 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
479                         content_t c = vm->m_data[vi].getContent();
480
481                         // Biome is recalculated each time an upper surface is detected while
482                         // working down a column. The selected biome then remains in effect for
483                         // all nodes below until the next surface and biome recalculation.
484                         // Biome is recalculated:
485                         // 1. At the surface of stone below air or water.
486                         // 2. At the surface of water below air.
487                         // 3. When stone or water is detected but biome has not yet been calculated.
488                         if ((c == c_stone && (air_above || water_above || !biome))
489                                         || ((c == c_water_source || c == c_river_water_source)
490                                                 && (air_above || !biome))) {
491                                 biome = biomegen->getBiomeAtIndex(index, y);
492
493                                 depth_top = biome->depth_top;
494                                 base_filler = MYMAX(depth_top
495                                                 + biome->depth_filler
496                                                 + noise_filler_depth->result[index], 0.f);
497                                 depth_water_top = biome->depth_water_top;
498
499                                 // Detect stone type for dungeons during every biome calculation.
500                                 // This is more efficient than detecting per-node and will not
501                                 // miss any desert stone or sandstone biomes.
502                                 if (biome->c_stone == c_desert_stone)
503                                         stone_type = MGSTONE_DESERT_STONE;
504                                 else if (biome->c_stone == c_sandstone)
505                                         stone_type = MGSTONE_SANDSTONE;
506                         }
507
508                         if (c == c_stone) {
509                                 content_t c_below = vm->m_data[vi - em.X].getContent();
510
511                                 // If the node below isn't solid, make this node stone, so that
512                                 // any top/filler nodes above are structurally supported.
513                                 // This is done by aborting the cycle of top/filler placement
514                                 // immediately by forcing nplaced to stone level.
515                                 if (c_below == CONTENT_AIR
516                                                 || c_below == c_water_source
517                                                 || c_below == c_river_water_source)
518                                         nplaced = U16_MAX;
519
520                                 if (nplaced < depth_top) {
521                                         vm->m_data[vi] = MapNode(biome->c_top);
522                                         nplaced++;
523                                 } else if (nplaced < base_filler) {
524                                         vm->m_data[vi] = MapNode(biome->c_filler);
525                                         nplaced++;
526                                 } else {
527                                         vm->m_data[vi] = MapNode(biome->c_stone);
528                                 }
529
530                                 air_above = false;
531                                 water_above = false;
532                         } else if (c == c_water_source) {
533                                 vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top))
534                                                 ? biome->c_water_top : biome->c_water);
535                                 nplaced = 0;  // Enable top/filler placement for next surface
536                                 air_above = false;
537                                 water_above = true;
538                         } else if (c == c_river_water_source) {
539                                 vm->m_data[vi] = MapNode(biome->c_river_water);
540                                 nplaced = depth_top;  // Enable filler placement for next surface
541                                 air_above = false;
542                                 water_above = true;
543                         } else if (c == CONTENT_AIR) {
544                                 nplaced = 0;  // Enable top/filler placement for next surface
545                                 air_above = true;
546                                 water_above = false;
547                         } else {  // Possible various nodes overgenerated from neighbouring mapchunks
548                                 nplaced = U16_MAX;  // Disable top/filler placement
549                                 air_above = false;
550                                 water_above = false;
551                         }
552
553                         vm->m_area.add_y(em, vi, -1);
554                 }
555         }
556
557         return stone_type;
558 }
559
560
561 void MapgenBasic::dustTopNodes()
562 {
563         if (node_max.Y < water_level)
564                 return;
565
566         v3s16 em = vm->m_area.getExtent();
567         u32 index = 0;
568
569         for (s16 z = node_min.Z; z <= node_max.Z; z++)
570         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
571                 Biome *biome = (Biome *)m_bmgr->getRaw(biomemap[index]);
572
573                 if (biome->c_dust == CONTENT_IGNORE)
574                         continue;
575
576                 u32 vi = vm->m_area.index(x, full_node_max.Y, z);
577                 content_t c_full_max = vm->m_data[vi].getContent();
578                 s16 y_start;
579
580                 if (c_full_max == CONTENT_AIR) {
581                         y_start = full_node_max.Y - 1;
582                 } else if (c_full_max == CONTENT_IGNORE) {
583                         vi = vm->m_area.index(x, node_max.Y + 1, z);
584                         content_t c_max = vm->m_data[vi].getContent();
585
586                         if (c_max == CONTENT_AIR)
587                                 y_start = node_max.Y;
588                         else
589                                 continue;
590                 } else {
591                         continue;
592                 }
593
594                 vi = vm->m_area.index(x, y_start, z);
595                 for (s16 y = y_start; y >= node_min.Y - 1; y--) {
596                         if (vm->m_data[vi].getContent() != CONTENT_AIR)
597                                 break;
598
599                         vm->m_area.add_y(em, vi, -1);
600                 }
601
602                 content_t c = vm->m_data[vi].getContent();
603                 if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) {
604                         vm->m_area.add_y(em, vi, 1);
605                         vm->m_data[vi] = MapNode(biome->c_dust);
606                 }
607         }
608 }
609
610
611 void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth)
612 {
613         if (max_stone_y < node_min.Y)
614                 return;
615
616         CavesNoiseIntersection caves_noise(ndef, m_bmgr, csize,
617                 &np_cave1, &np_cave2, seed, cave_width);
618
619         caves_noise.generateCaves(vm, node_min, node_max, biomemap);
620
621         if (node_max.Y > large_cave_depth)
622                 return;
623
624         PseudoRandom ps(blockseed + 21343);
625         u32 bruises_count = ps.range(0, 2);
626         for (u32 i = 0; i < bruises_count; i++) {
627                 CavesRandomWalk cave(ndef, &gennotify, seed, water_level,
628                         c_water_source, CONTENT_IGNORE);
629
630                 cave.makeCave(vm, node_min, node_max, &ps, true, max_stone_y, heightmap);
631         }
632 }
633
634
635 void MapgenBasic::generateDungeons(s16 max_stone_y, MgStoneType stone_type)
636 {
637         if (max_stone_y < node_min.Y)
638                 return;
639
640         DungeonParams dp;
641
642         dp.seed = seed;
643
644         dp.np_rarity  = nparams_dungeon_rarity;
645         dp.np_density = nparams_dungeon_density;
646         dp.np_wetness = nparams_dungeon_wetness;
647         dp.c_water    = c_water_source;
648
649         switch (stone_type) {
650         default:
651         case MGSTONE_STONE:
652                 dp.c_cobble = c_cobble;
653                 dp.c_moss   = c_mossycobble;
654                 dp.c_stair  = c_stair_cobble;
655
656                 dp.diagonal_dirs = false;
657                 dp.mossratio     = 3.0;
658                 dp.holesize      = v3s16(1, 2, 1);
659                 dp.roomsize      = v3s16(0, 0, 0);
660                 dp.notifytype    = GENNOTIFY_DUNGEON;
661                 break;
662         case MGSTONE_DESERT_STONE:
663                 dp.c_cobble = c_desert_stone;
664                 dp.c_moss   = c_desert_stone;
665                 dp.c_stair  = c_desert_stone;
666
667                 dp.diagonal_dirs = true;
668                 dp.mossratio     = 0.0;
669                 dp.holesize      = v3s16(2, 3, 2);
670                 dp.roomsize      = v3s16(2, 5, 2);
671                 dp.notifytype    = GENNOTIFY_TEMPLE;
672                 break;
673         case MGSTONE_SANDSTONE:
674                 dp.c_cobble = c_sandstonebrick;
675                 dp.c_moss   = c_sandstonebrick;
676                 dp.c_stair  = c_sandstonebrick;
677
678                 dp.diagonal_dirs = false;
679                 dp.mossratio     = 0.0;
680                 dp.holesize      = v3s16(2, 2, 2);
681                 dp.roomsize      = v3s16(2, 0, 2);
682                 dp.notifytype    = GENNOTIFY_DUNGEON;
683                 break;
684         }
685
686         DungeonGen dgen(ndef, &gennotify, &dp);
687         dgen.generate(vm, blockseed, full_node_min, full_node_max);
688 }
689
690
691 ////
692 //// GenerateNotifier
693 ////
694
695 GenerateNotifier::GenerateNotifier()
696 {
697         m_notify_on = 0;
698 }
699
700
701 GenerateNotifier::GenerateNotifier(u32 notify_on,
702         std::set<u32> *notify_on_deco_ids)
703 {
704         m_notify_on = notify_on;
705         m_notify_on_deco_ids = notify_on_deco_ids;
706 }
707
708
709 void GenerateNotifier::setNotifyOn(u32 notify_on)
710 {
711         m_notify_on = notify_on;
712 }
713
714
715 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
716 {
717         m_notify_on_deco_ids = notify_on_deco_ids;
718 }
719
720
721 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
722 {
723         if (!(m_notify_on & (1 << type)))
724                 return false;
725
726         if (type == GENNOTIFY_DECORATION &&
727                 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
728                 return false;
729
730         GenNotifyEvent gne;
731         gne.type = type;
732         gne.pos  = pos;
733         gne.id   = id;
734         m_notify_events.push_back(gne);
735
736         return true;
737 }
738
739
740 void GenerateNotifier::getEvents(
741         std::map<std::string, std::vector<v3s16> > &event_map,
742         bool peek_events)
743 {
744         std::list<GenNotifyEvent>::iterator it;
745
746         for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
747                 GenNotifyEvent &gn = *it;
748                 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
749                         "decoration#"+ itos(gn.id) :
750                         flagdesc_gennotify[gn.type].name;
751
752                 event_map[name].push_back(gn.pos);
753         }
754
755         if (!peek_events)
756                 m_notify_events.clear();
757 }
758
759
760 ////
761 //// MapgenParams
762 ////
763
764
765 MapgenParams::~MapgenParams()
766 {
767         delete bparams;
768         delete sparams;
769 }
770
771
772 void MapgenParams::load(const Settings &settings)
773 {
774         std::string seed_str;
775         const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed";
776
777         if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty())
778                 seed = read_seed(seed_str.c_str());
779         else
780                 myrand_bytes(&seed, sizeof(seed));
781
782         settings.getNoEx("mg_name", mg_name);
783         settings.getS16NoEx("water_level", water_level);
784         settings.getS16NoEx("chunksize", chunksize);
785         settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen);
786
787         delete bparams;
788         bparams = BiomeManager::createBiomeParams(BIOMEGEN_ORIGINAL);
789         if (bparams) {
790                 bparams->readParams(&settings);
791                 bparams->seed = seed;
792         }
793
794         delete sparams;
795         MapgenFactory *mgfactory = EmergeManager::getMapgenFactory(mg_name);
796         if (mgfactory) {
797                 sparams = mgfactory->createMapgenParams();
798                 sparams->readParams(&settings);
799         }
800 }
801
802
803 void MapgenParams::save(Settings &settings) const
804 {
805         settings.set("mg_name", mg_name);
806         settings.setU64("seed", seed);
807         settings.setS16("water_level", water_level);
808         settings.setS16("chunksize", chunksize);
809         settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, U32_MAX);
810
811         if (bparams)
812                 bparams->writeParams(&settings);
813
814         if (sparams)
815                 sparams->writeParams(&settings);
816 }