Cavegen: Remove CavesRandomWalk 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
44 FlagDesc flagdesc_mapgen[] = {
45         {"trees",       MG_TREES},
46         {"caves",       MG_CAVES},
47         {"dungeons",    MG_DUNGEONS},
48         {"flat",        MG_FLAT},
49         {"light",       MG_LIGHT},
50         {"decorations", MG_DECORATIONS},
51         {NULL,       0}
52 };
53
54 FlagDesc flagdesc_gennotify[] = {
55         {"dungeon",          1 << GENNOTIFY_DUNGEON},
56         {"temple",           1 << GENNOTIFY_TEMPLE},
57         {"cave_begin",       1 << GENNOTIFY_CAVE_BEGIN},
58         {"cave_end",         1 << GENNOTIFY_CAVE_END},
59         {"large_cave_begin", 1 << GENNOTIFY_LARGECAVE_BEGIN},
60         {"large_cave_end",   1 << GENNOTIFY_LARGECAVE_END},
61         {"decoration",       1 << GENNOTIFY_DECORATION},
62         {NULL,               0}
63 };
64
65
66 ////
67 //// Mapgen
68 ////
69
70 Mapgen::Mapgen()
71 {
72         generating  = false;
73         id          = -1;
74         seed        = 0;
75         water_level = 0;
76         flags       = 0;
77
78         vm        = NULL;
79         ndef      = NULL;
80         biomegen  = NULL;
81         biomemap  = NULL;
82         heightmap = NULL;
83 }
84
85
86 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
87         gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
88 {
89         generating  = false;
90         id          = mapgenid;
91         seed        = (int)params->seed;
92         water_level = params->water_level;
93         flags       = params->flags;
94         csize       = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
95
96         vm        = NULL;
97         ndef      = emerge->ndef;
98         biomegen  = NULL;
99         biomemap  = NULL;
100         heightmap = NULL;
101 }
102
103
104 Mapgen::~Mapgen()
105 {
106 }
107
108
109 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
110 {
111         return (u32)seed   +
112                 p.Z * 38134234 +
113                 p.Y * 42123    +
114                 p.X * 23;
115 }
116
117
118 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
119 {
120         u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
121         n = (n >> 13) ^ n;
122         return (n * (n * n * 60493 + 19990303) + 1376312589);
123 }
124
125
126 // Returns Y one under area minimum if not found
127 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
128 {
129         v3s16 em = vm->m_area.getExtent();
130         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
131         s16 y_nodes_min = vm->m_area.MinEdge.Y;
132         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
133         s16 y;
134
135         for (y = y_nodes_max; y >= y_nodes_min; y--) {
136                 MapNode &n = vm->m_data[i];
137                 if (ndef->get(n).walkable)
138                         break;
139
140                 vm->m_area.add_y(em, i, -1);
141         }
142         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
143 }
144
145
146 // Returns -MAX_MAP_GENERATION_LIMIT if not found
147 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
148 {
149         v3s16 em = vm->m_area.getExtent();
150         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
151         s16 y;
152
153         for (y = ymax; y >= ymin; y--) {
154                 MapNode &n = vm->m_data[i];
155                 if (ndef->get(n).walkable)
156                         break;
157
158                 vm->m_area.add_y(em, i, -1);
159         }
160         return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
161 }
162
163
164 // Returns -MAX_MAP_GENERATION_LIMIT if not found or if ground is found first
165 s16 Mapgen::findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax)
166 {
167         v3s16 em = vm->m_area.getExtent();
168         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
169         s16 y;
170
171         for (y = ymax; y >= ymin; y--) {
172                 MapNode &n = vm->m_data[i];
173                 if (ndef->get(n).walkable)
174                         return -MAX_MAP_GENERATION_LIMIT;
175                 else if (ndef->get(n).isLiquid())
176                         break;
177
178                 vm->m_area.add_y(em, i, -1);
179         }
180         return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
181 }
182
183
184 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
185 {
186         if (!heightmap)
187                 return;
188
189         //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
190         int index = 0;
191         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
192                 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
193                         s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
194
195                         heightmap[index] = y;
196                 }
197         }
198         //printf("updateHeightmap: %dus\n", t.stop());
199 }
200
201
202 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
203 {
204         bool isliquid, wasliquid;
205         v3s16 em  = vm->m_area.getExtent();
206
207         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
208                 for (s16 x = nmin.X; x <= nmax.X; x++) {
209                         wasliquid = true;
210
211                         u32 i = vm->m_area.index(x, nmax.Y, z);
212                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
213                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
214
215                                 // there was a change between liquid and nonliquid, add to queue.
216                                 if (isliquid != wasliquid)
217                                         trans_liquid->push_back(v3s16(x, y, z));
218
219                                 wasliquid = isliquid;
220                                 vm->m_area.add_y(em, i, -1);
221                         }
222                 }
223         }
224 }
225
226
227 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
228 {
229         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
230         VoxelArea a(nmin, nmax);
231
232         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
233                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
234                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
235                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
236                                 vm->m_data[i].param1 = light;
237                 }
238         }
239 }
240
241
242 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
243 {
244         if (light <= 1 || !a.contains(p))
245                 return;
246
247         u32 vi = vm->m_area.index(p);
248         MapNode &n = vm->m_data[vi];
249
250         // Decay light in each of the banks separately
251         u8 light_day = light & 0x0F;
252         if (light_day > 0)
253                 light_day -= 0x01;
254
255         u8 light_night = light & 0xF0;
256         if (light_night > 0)
257                 light_night -= 0x10;
258
259         // Bail out only if we have no more light from either bank to propogate, or
260         // we hit a solid block that light cannot pass through.
261         if ((light_day  <= (n.param1 & 0x0F) &&
262                 light_night <= (n.param1 & 0xF0)) ||
263                 !ndef->get(n).light_propagates)
264                 return;
265
266         // Since this recursive function only terminates when there is no light from
267         // either bank left, we need to take the max of both banks into account for
268         // the case where spreading has stopped for one light bank but not the other.
269         light = MYMAX(light_day, n.param1 & 0x0F) |
270                         MYMAX(light_night, n.param1 & 0xF0);
271
272         n.param1 = light;
273
274         lightSpread(a, p + v3s16(0, 0, 1), light);
275         lightSpread(a, p + v3s16(0, 1, 0), light);
276         lightSpread(a, p + v3s16(1, 0, 0), light);
277         lightSpread(a, p - v3s16(0, 0, 1), light);
278         lightSpread(a, p - v3s16(0, 1, 0), light);
279         lightSpread(a, p - v3s16(1, 0, 0), light);
280 }
281
282
283 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
284         bool propagate_shadow)
285 {
286         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
287         //TimeTaker t("updateLighting");
288
289         propagateSunlight(nmin, nmax, propagate_shadow);
290         spreadLight(full_nmin, full_nmax);
291
292         //printf("updateLighting: %dms\n", t.stop());
293 }
294
295
296 void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow)
297 {
298         //TimeTaker t("propagateSunlight");
299         VoxelArea a(nmin, nmax);
300         bool block_is_underground = (water_level >= nmax.Y);
301         v3s16 em = vm->m_area.getExtent();
302
303         // NOTE: Direct access to the low 4 bits of param1 is okay here because,
304         // by definition, sunlight will never be in the night lightbank.
305
306         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
307                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
308                         // see if we can get a light value from the overtop
309                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
310                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
311                                 if (block_is_underground)
312                                         continue;
313                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN &&
314                                         propagate_shadow) {
315                                 continue;
316                         }
317                         vm->m_area.add_y(em, i, -1);
318
319                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
320                                 MapNode &n = vm->m_data[i];
321                                 if (!ndef->get(n).sunlight_propagates)
322                                         break;
323                                 n.param1 = LIGHT_SUN;
324                                 vm->m_area.add_y(em, i, -1);
325                         }
326                 }
327         }
328         //printf("propagateSunlight: %dms\n", t.stop());
329 }
330
331
332 void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
333 {
334         //TimeTaker t("spreadLight");
335         VoxelArea a(nmin, nmax);
336
337         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
338                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
339                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
340                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
341                                 MapNode &n = vm->m_data[i];
342                                 if (n.getContent() == CONTENT_IGNORE)
343                                         continue;
344
345                                 const ContentFeatures &cf = ndef->get(n);
346                                 if (!cf.light_propagates)
347                                         continue;
348
349                                 // TODO(hmmmmm): Abstract away direct param1 accesses with a
350                                 // wrapper, but something lighter than MapNode::get/setLight
351
352                                 u8 light_produced = cf.light_source;
353                                 if (light_produced)
354                                         n.param1 = light_produced | (light_produced << 4);
355
356                                 u8 light = n.param1;
357                                 if (light) {
358                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
359                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
360                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
361                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
362                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
363                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
364                                 }
365                         }
366                 }
367         }
368
369         //printf("spreadLight: %dms\n", t.stop());
370 }
371
372
373 ////
374 //// MapgenBasic
375 ////
376
377 MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge)
378         : Mapgen(mapgenid, params, emerge)
379 {
380
381 }
382
383 MgStoneType MapgenBasic::generateBiomes()
384 {
385         v3s16 em = vm->m_area.getExtent();
386         u32 index = 0;
387         MgStoneType stone_type = MGSTONE_STONE;
388
389         for (s16 z = node_min.Z; z <= node_max.Z; z++)
390         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
391                 Biome *biome = NULL;
392                 u16 depth_top = 0;
393                 u16 base_filler = 0;
394                 u16 depth_water_top = 0;
395                 u32 vi = vm->m_area.index(x, node_max.Y, z);
396
397                 // Check node at base of mapchunk above, either a node of a previously
398                 // generated mapchunk or if not, a node of overgenerated base terrain.
399                 content_t c_above = vm->m_data[vi + em.X].getContent();
400                 bool air_above = c_above == CONTENT_AIR;
401                 bool water_above = (c_above == c_water_source || c_above == c_river_water_source);
402
403                 // If there is air or water above enable top/filler placement, otherwise force
404                 // nplaced to stone level by setting a number exceeding any possible filler depth.
405                 u16 nplaced = (air_above || water_above) ? 0 : U16_MAX;
406
407                 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
408                         content_t c = vm->m_data[vi].getContent();
409
410                         // Biome is recalculated each time an upper surface is detected while
411                         // working down a column. The selected biome then remains in effect for
412                         // all nodes below until the next surface and biome recalculation.
413                         // Biome is recalculated:
414                         // 1. At the surface of stone below air or water.
415                         // 2. At the surface of water below air.
416                         // 3. When stone or water is detected but biome has not yet been calculated.
417                         if ((c == c_stone && (air_above || water_above || !biome))
418                                         || ((c == c_water_source || c == c_river_water_source)
419                                                 && (air_above || !biome))) {
420                                 biome = biomegen->getBiomeAtIndex(index, y);
421
422                                 depth_top = biome->depth_top;
423                                 base_filler = MYMAX(depth_top
424                                                 + biome->depth_filler
425                                                 + noise_filler_depth->result[index], 0.f);
426                                 depth_water_top = biome->depth_water_top;
427
428                                 // Detect stone type for dungeons during every biome calculation.
429                                 // This is more efficient than detecting per-node and will not
430                                 // miss any desert stone or sandstone biomes.
431                                 if (biome->c_stone == c_desert_stone)
432                                         stone_type = MGSTONE_DESERT_STONE;
433                                 else if (biome->c_stone == c_sandstone)
434                                         stone_type = MGSTONE_SANDSTONE;
435                         }
436
437                         if (c == c_stone) {
438                                 content_t c_below = vm->m_data[vi - em.X].getContent();
439
440                                 // If the node below isn't solid, make this node stone, so that
441                                 // any top/filler nodes above are structurally supported.
442                                 // This is done by aborting the cycle of top/filler placement
443                                 // immediately by forcing nplaced to stone level.
444                                 if (c_below == CONTENT_AIR
445                                                 || c_below == c_water_source
446                                                 || c_below == c_river_water_source)
447                                         nplaced = U16_MAX;
448
449                                 if (nplaced < depth_top) {
450                                         vm->m_data[vi] = MapNode(biome->c_top);
451                                         nplaced++;
452                                 } else if (nplaced < base_filler) {
453                                         vm->m_data[vi] = MapNode(biome->c_filler);
454                                         nplaced++;
455                                 } else {
456                                         vm->m_data[vi] = MapNode(biome->c_stone);
457                                 }
458
459                                 air_above = false;
460                                 water_above = false;
461                         } else if (c == c_water_source) {
462                                 vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top))
463                                                 ? biome->c_water_top : biome->c_water);
464                                 nplaced = 0;  // Enable top/filler placement for next surface
465                                 air_above = false;
466                                 water_above = true;
467                         } else if (c == c_river_water_source) {
468                                 vm->m_data[vi] = MapNode(biome->c_river_water);
469                                 nplaced = depth_top;  // Enable filler placement for next surface
470                                 air_above = false;
471                                 water_above = true;
472                         } else if (c == CONTENT_AIR) {
473                                 nplaced = 0;  // Enable top/filler placement for next surface
474                                 air_above = true;
475                                 water_above = false;
476                         } else {  // Possible various nodes overgenerated from neighbouring mapchunks
477                                 nplaced = U16_MAX;  // Disable top/filler placement
478                                 air_above = false;
479                                 water_above = false;
480                         }
481
482                         vm->m_area.add_y(em, vi, -1);
483                 }
484         }
485
486         return stone_type;
487 }
488
489
490 void MapgenBasic::dustTopNodes()
491 {
492         if (node_max.Y < water_level)
493                 return;
494
495         v3s16 em = vm->m_area.getExtent();
496         u32 index = 0;
497
498         for (s16 z = node_min.Z; z <= node_max.Z; z++)
499         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
500                 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]);
501
502                 if (biome->c_dust == CONTENT_IGNORE)
503                         continue;
504
505                 u32 vi = vm->m_area.index(x, full_node_max.Y, z);
506                 content_t c_full_max = vm->m_data[vi].getContent();
507                 s16 y_start;
508
509                 if (c_full_max == CONTENT_AIR) {
510                         y_start = full_node_max.Y - 1;
511                 } else if (c_full_max == CONTENT_IGNORE) {
512                         vi = vm->m_area.index(x, node_max.Y + 1, z);
513                         content_t c_max = vm->m_data[vi].getContent();
514
515                         if (c_max == CONTENT_AIR)
516                                 y_start = node_max.Y;
517                         else
518                                 continue;
519                 } else {
520                         continue;
521                 }
522
523                 vi = vm->m_area.index(x, y_start, z);
524                 for (s16 y = y_start; y >= node_min.Y - 1; y--) {
525                         if (vm->m_data[vi].getContent() != CONTENT_AIR)
526                                 break;
527
528                         vm->m_area.add_y(em, vi, -1);
529                 }
530
531                 content_t c = vm->m_data[vi].getContent();
532                 if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) {
533                         vm->m_area.add_y(em, vi, 1);
534                         vm->m_data[vi] = MapNode(biome->c_dust);
535                 }
536         }
537 }
538
539
540 void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth)
541 {
542         if (max_stone_y < node_min.Y)
543                 return;
544
545         noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
546         noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
547
548         v3s16 em = vm->m_area.getExtent();
549         u32 index2d = 0;
550
551         for (s16 z = node_min.Z; z <= node_max.Z; z++)
552         for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
553                 bool column_is_open = false;  // Is column open to overground
554                 bool is_tunnel = false;  // Is tunnel or tunnel floor
555                 u32 vi = vm->m_area.index(x, node_max.Y, z);
556                 u32 index3d = (z - node_min.Z) * zstride_1d + csize.Y * ystride +
557                         (x - node_min.X);
558                 // Biome of column
559                 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]);
560
561                 // Don't excavate the overgenerated stone at node_max.Y + 1,
562                 // this creates a 'roof' over the tunnel, preventing light in
563                 // tunnels at mapchunk borders when generating mapchunks upwards.
564                 // This 'roof' is removed when the mapchunk above is generated.
565                 for (s16 y = node_max.Y; y >= node_min.Y - 1; y--,
566                                 index3d -= ystride,
567                                 vm->m_area.add_y(em, vi, -1)) {
568
569                         content_t c = vm->m_data[vi].getContent();
570                         if (c == CONTENT_AIR || c == biome->c_water_top ||
571                                         c == biome->c_water) {
572                                 column_is_open = true;
573                                 continue;
574                         }
575                         // Ground
576                         float d1 = contour(noise_cave1->result[index3d]);
577                         float d2 = contour(noise_cave2->result[index3d]);
578
579                         if (d1 * d2 > cave_width && ndef->get(c).is_ground_content) {
580                                 // In tunnel and ground content, excavate
581                                 vm->m_data[vi] = MapNode(CONTENT_AIR);
582                                 is_tunnel = true;
583                         } else {
584                                 // Not in tunnel or not ground content
585                                 if (is_tunnel && column_is_open &&
586                                                 (c == biome->c_filler || c == biome->c_stone))
587                                         // Tunnel entrance floor
588                                         vm->m_data[vi] = MapNode(biome->c_top);
589
590                                 column_is_open = false;
591                                 is_tunnel = false;
592                         }
593                 }
594         }
595
596         if (node_max.Y > large_cave_depth)
597                 return;
598
599         PseudoRandom ps(blockseed + 21343);
600         u32 bruises_count = ps.range(0, 2);
601         for (u32 i = 0; i < bruises_count; i++) {
602                 CavesRandomWalk cave(ndef, &gennotify, seed, water_level,
603                         c_water_source, CONTENT_IGNORE);
604
605                 cave.makeCave(vm, node_min, node_max, &ps, max_stone_y, heightmap);
606         }
607 }
608
609
610 ////
611 //// GenerateNotifier
612 ////
613
614 GenerateNotifier::GenerateNotifier()
615 {
616         m_notify_on = 0;
617 }
618
619
620 GenerateNotifier::GenerateNotifier(u32 notify_on,
621         std::set<u32> *notify_on_deco_ids)
622 {
623         m_notify_on = notify_on;
624         m_notify_on_deco_ids = notify_on_deco_ids;
625 }
626
627
628 void GenerateNotifier::setNotifyOn(u32 notify_on)
629 {
630         m_notify_on = notify_on;
631 }
632
633
634 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
635 {
636         m_notify_on_deco_ids = notify_on_deco_ids;
637 }
638
639
640 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
641 {
642         if (!(m_notify_on & (1 << type)))
643                 return false;
644
645         if (type == GENNOTIFY_DECORATION &&
646                 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
647                 return false;
648
649         GenNotifyEvent gne;
650         gne.type = type;
651         gne.pos  = pos;
652         gne.id   = id;
653         m_notify_events.push_back(gne);
654
655         return true;
656 }
657
658
659 void GenerateNotifier::getEvents(
660         std::map<std::string, std::vector<v3s16> > &event_map,
661         bool peek_events)
662 {
663         std::list<GenNotifyEvent>::iterator it;
664
665         for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
666                 GenNotifyEvent &gn = *it;
667                 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
668                         "decoration#"+ itos(gn.id) :
669                         flagdesc_gennotify[gn.type].name;
670
671                 event_map[name].push_back(gn.pos);
672         }
673
674         if (!peek_events)
675                 m_notify_events.clear();
676 }
677
678
679 ////
680 //// MapgenParams
681 ////
682
683
684 MapgenParams::~MapgenParams()
685 {
686         delete bparams;
687         delete sparams;
688 }
689
690
691 void MapgenParams::load(const Settings &settings)
692 {
693         std::string seed_str;
694         const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed";
695
696         if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty())
697                 seed = read_seed(seed_str.c_str());
698         else
699                 myrand_bytes(&seed, sizeof(seed));
700
701         settings.getNoEx("mg_name", mg_name);
702         settings.getS16NoEx("water_level", water_level);
703         settings.getS16NoEx("chunksize", chunksize);
704         settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen);
705
706         delete bparams;
707         bparams = BiomeManager::createBiomeParams(BIOMEGEN_ORIGINAL);
708         if (bparams) {
709                 bparams->readParams(&settings);
710                 bparams->seed = seed;
711         }
712
713         delete sparams;
714         MapgenFactory *mgfactory = EmergeManager::getMapgenFactory(mg_name);
715         if (mgfactory) {
716                 sparams = mgfactory->createMapgenParams();
717                 sparams->readParams(&settings);
718         }
719 }
720
721
722 void MapgenParams::save(Settings &settings) const
723 {
724         settings.set("mg_name", mg_name);
725         settings.setU64("seed", seed);
726         settings.setS16("water_level", water_level);
727         settings.setS16("chunksize", chunksize);
728         settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, U32_MAX);
729
730         if (bparams)
731                 bparams->writeParams(&settings);
732
733         if (sparams)
734                 sparams->writeParams(&settings);
735 }