3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
29 #include "content_sao.h"
32 #include "voxelalgorithms.h"
37 #include "serialization.h"
38 #include "util/serialize.h"
39 #include "util/numeric.h"
44 FlagDesc flagdesc_mapgen[] = {
47 {"dungeons", MG_DUNGEONS},
50 {"decorations", MG_DECORATIONS},
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},
86 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
87 gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
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);
109 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
118 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
120 u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
122 return (n * (n * n * 60493 + 19990303) + 1376312589);
126 // Returns Y one under area minimum if not found
127 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
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);
135 for (y = y_nodes_max; y >= y_nodes_min; y--) {
136 MapNode &n = vm->m_data[i];
137 if (ndef->get(n).walkable)
140 vm->m_area.add_y(em, i, -1);
142 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
146 // Returns -MAX_MAP_GENERATION_LIMIT if not found
147 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
149 v3s16 em = vm->m_area.getExtent();
150 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
153 for (y = ymax; y >= ymin; y--) {
154 MapNode &n = vm->m_data[i];
155 if (ndef->get(n).walkable)
158 vm->m_area.add_y(em, i, -1);
160 return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
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)
167 v3s16 em = vm->m_area.getExtent();
168 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
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())
178 vm->m_area.add_y(em, i, -1);
180 return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
184 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
189 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
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);
195 heightmap[index] = y;
198 //printf("updateHeightmap: %dus\n", t.stop());
202 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
204 bool isliquid, wasliquid;
205 v3s16 em = vm->m_area.getExtent();
207 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
208 for (s16 x = nmin.X; x <= nmax.X; x++) {
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();
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));
219 wasliquid = isliquid;
220 vm->m_area.add_y(em, i, -1);
227 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
229 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
230 VoxelArea a(nmin, nmax);
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;
242 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
244 if (light <= 1 || !a.contains(p))
247 u32 vi = vm->m_area.index(p);
248 MapNode &n = vm->m_data[vi];
250 // Decay light in each of the banks separately
251 u8 light_day = light & 0x0F;
255 u8 light_night = light & 0xF0;
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)
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);
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);
283 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
284 bool propagate_shadow)
286 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
287 //TimeTaker t("updateLighting");
289 propagateSunlight(nmin, nmax, propagate_shadow);
290 spreadLight(full_nmin, full_nmax);
292 //printf("updateLighting: %dms\n", t.stop());
296 void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow)
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();
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.
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)
313 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN &&
317 vm->m_area.add_y(em, i, -1);
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)
323 n.param1 = LIGHT_SUN;
324 vm->m_area.add_y(em, i, -1);
328 //printf("propagateSunlight: %dms\n", t.stop());
332 void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
334 //TimeTaker t("spreadLight");
335 VoxelArea a(nmin, nmax);
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)
345 const ContentFeatures &cf = ndef->get(n);
346 if (!cf.light_propagates)
349 // TODO(hmmmmm): Abstract away direct param1 accesses with a
350 // wrapper, but something lighter than MapNode::get/setLight
352 u8 light_produced = cf.light_source;
354 n.param1 = light_produced | (light_produced << 4);
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);
369 //printf("spreadLight: %dms\n", t.stop());
377 MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge)
378 : Mapgen(mapgenid, params, emerge)
383 MgStoneType MapgenBasic::generateBiomes()
385 v3s16 em = vm->m_area.getExtent();
387 MgStoneType stone_type = MGSTONE_STONE;
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++) {
394 u16 depth_water_top = 0;
395 u32 vi = vm->m_area.index(x, node_max.Y, z);
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);
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;
407 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
408 content_t c = vm->m_data[vi].getContent();
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);
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;
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;
438 content_t c_below = vm->m_data[vi - em.X].getContent();
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)
449 if (nplaced < depth_top) {
450 vm->m_data[vi] = MapNode(biome->c_top);
452 } else if (nplaced < base_filler) {
453 vm->m_data[vi] = MapNode(biome->c_filler);
456 vm->m_data[vi] = MapNode(biome->c_stone);
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
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
472 } else if (c == CONTENT_AIR) {
473 nplaced = 0; // Enable top/filler placement for next surface
476 } else { // Possible various nodes overgenerated from neighbouring mapchunks
477 nplaced = U16_MAX; // Disable top/filler placement
482 vm->m_area.add_y(em, vi, -1);
490 void MapgenBasic::dustTopNodes()
492 if (node_max.Y < water_level)
495 v3s16 em = vm->m_area.getExtent();
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]);
502 if (biome->c_dust == CONTENT_IGNORE)
505 u32 vi = vm->m_area.index(x, full_node_max.Y, z);
506 content_t c_full_max = vm->m_data[vi].getContent();
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();
515 if (c_max == CONTENT_AIR)
516 y_start = node_max.Y;
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)
528 vm->m_area.add_y(em, vi, -1);
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);
540 void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth)
542 if (max_stone_y < node_min.Y)
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);
548 v3s16 em = vm->m_area.getExtent();
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 +
559 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]);
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--,
567 vm->m_area.add_y(em, vi, -1)) {
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;
576 float d1 = contour(noise_cave1->result[index3d]);
577 float d2 = contour(noise_cave2->result[index3d]);
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);
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);
590 column_is_open = false;
596 if (node_max.Y > large_cave_depth)
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(this, &ps);
603 cave.makeCave(node_min, node_max, max_stone_y);
609 //// GenerateNotifier
612 GenerateNotifier::GenerateNotifier()
618 GenerateNotifier::GenerateNotifier(u32 notify_on,
619 std::set<u32> *notify_on_deco_ids)
621 m_notify_on = notify_on;
622 m_notify_on_deco_ids = notify_on_deco_ids;
626 void GenerateNotifier::setNotifyOn(u32 notify_on)
628 m_notify_on = notify_on;
632 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
634 m_notify_on_deco_ids = notify_on_deco_ids;
638 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
640 if (!(m_notify_on & (1 << type)))
643 if (type == GENNOTIFY_DECORATION &&
644 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
651 m_notify_events.push_back(gne);
657 void GenerateNotifier::getEvents(
658 std::map<std::string, std::vector<v3s16> > &event_map,
661 std::list<GenNotifyEvent>::iterator it;
663 for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
664 GenNotifyEvent &gn = *it;
665 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
666 "decoration#"+ itos(gn.id) :
667 flagdesc_gennotify[gn.type].name;
669 event_map[name].push_back(gn.pos);
673 m_notify_events.clear();
682 MapgenParams::~MapgenParams()
689 void MapgenParams::load(const Settings &settings)
691 std::string seed_str;
692 const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed";
694 if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty())
695 seed = read_seed(seed_str.c_str());
697 myrand_bytes(&seed, sizeof(seed));
699 settings.getNoEx("mg_name", mg_name);
700 settings.getS16NoEx("water_level", water_level);
701 settings.getS16NoEx("chunksize", chunksize);
702 settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen);
705 bparams = BiomeManager::createBiomeParams(BIOMEGEN_ORIGINAL);
707 bparams->readParams(&settings);
708 bparams->seed = seed;
712 MapgenFactory *mgfactory = EmergeManager::getMapgenFactory(mg_name);
714 sparams = mgfactory->createMapgenParams();
715 sparams->readParams(&settings);
720 void MapgenParams::save(Settings &settings) const
722 settings.set("mg_name", mg_name);
723 settings.setU64("seed", seed);
724 settings.setS16("water_level", water_level);
725 settings.setS16("chunksize", chunksize);
726 settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, U32_MAX);
729 bparams->writeParams(&settings);
732 sparams->writeParams(&settings);