]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/mapgen/mapgen.cpp
fix integer overflow in mapgen (#11641)
[dragonfireclient.git] / src / mapgen / mapgen.cpp
index a8b9873256fcba9957edc11e6afde0784c4a6660..1f2ac491e952331e41aac3249760d5d18be5c3f7 100644 (file)
@@ -1,8 +1,8 @@
 /*
 Minetest
-Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
-Copyright (C) 2013-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
-Copyright (C) 2015-2017 paramat
+Copyright (C) 2010-2018 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2013-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
+Copyright (C) 2015-2018 paramat
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include <cmath>
 #include "mapgen.h"
 #include "voxel.h"
 #include "noise.h"
@@ -27,7 +28,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapblock.h"
 #include "mapnode.h"
 #include "map.h"
-#include "content_sao.h"
 #include "nodedef.h"
 #include "emerge.h"
 #include "voxelalgorithms.h"
@@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "serialization.h"
 #include "util/serialize.h"
 #include "util/numeric.h"
+#include "util/directiontables.h"
 #include "filesys.h"
 #include "log.h"
 #include "mapgen_carpathian.h"
@@ -56,7 +57,9 @@ FlagDesc flagdesc_mapgen[] = {
        {"dungeons",    MG_DUNGEONS},
        {"light",       MG_LIGHT},
        {"decorations", MG_DECORATIONS},
-       {NULL,       0}
+       {"biomes",      MG_BIOMES},
+       {"ores",        MG_ORES},
+       {NULL,          0}
 };
 
 FlagDesc flagdesc_gennotify[] = {
@@ -79,15 +82,21 @@ struct MapgenDesc {
 //// Built-in mapgens
 ////
 
+// Order used here defines the order of appearence in mainmenu.
+// v6 always last to discourage selection.
+// Special mapgens flat, fractal, singlenode, next to last. Of these, singlenode
+// last to discourage selection.
+// Of the remaining, v5 last due to age, v7 first due to being the default.
+// The order of 'enum MapgenType' in mapgen.h must match this order.
 static MapgenDesc g_reg_mapgens[] = {
-       {"v5",         true},
-       {"v6",         true},
        {"v7",         true},
+       {"valleys",    true},
+       {"carpathian", true},
+       {"v5",         true},
        {"flat",       true},
        {"fractal",    true},
-       {"valleys",    true},
        {"singlenode", true},
-       {"carpathian", true},
+       {"v6",         true},
 };
 
 STATIC_ASSERT(
@@ -98,8 +107,8 @@ STATIC_ASSERT(
 //// Mapgen
 ////
 
-Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
-       gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
+Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeParams *emerge) :
+       gennotify(emerge->gen_notify_on, emerge->gen_notify_on_deco_ids)
 {
        id           = mapgenid;
        water_level  = params->water_level;
@@ -147,28 +156,28 @@ const char *Mapgen::getMapgenName(MapgenType mgtype)
 }
 
 
-Mapgen *Mapgen::createMapgen(MapgenType mgtype, int mgid,
-       MapgenParams *params, EmergeManager *emerge)
+Mapgen *Mapgen::createMapgen(MapgenType mgtype, MapgenParams *params,
+       EmergeParams *emerge)
 {
        switch (mgtype) {
        case MAPGEN_CARPATHIAN:
-               return new MapgenCarpathian(mgid, (MapgenCarpathianParams *)params, emerge);
+               return new MapgenCarpathian((MapgenCarpathianParams *)params, emerge);
        case MAPGEN_FLAT:
-               return new MapgenFlat(mgid, (MapgenFlatParams *)params, emerge);
+               return new MapgenFlat((MapgenFlatParams *)params, emerge);
        case MAPGEN_FRACTAL:
-               return new MapgenFractal(mgid, (MapgenFractalParams *)params, emerge);
+               return new MapgenFractal((MapgenFractalParams *)params, emerge);
        case MAPGEN_SINGLENODE:
-               return new MapgenSinglenode(mgid, (MapgenSinglenodeParams *)params, emerge);
+               return new MapgenSinglenode((MapgenSinglenodeParams *)params, emerge);
        case MAPGEN_V5:
-               return new MapgenV5(mgid, (MapgenV5Params *)params, emerge);
+               return new MapgenV5((MapgenV5Params *)params, emerge);
        case MAPGEN_V6:
-               return new MapgenV6(mgid, (MapgenV6Params *)params, emerge);
+               return new MapgenV6((MapgenV6Params *)params, emerge);
        case MAPGEN_V7:
-               return new MapgenV7(mgid, (MapgenV7Params *)params, emerge);
+               return new MapgenV7((MapgenV7Params *)params, emerge);
        case MAPGEN_VALLEYS:
-               return new MapgenValleys(mgid, (MapgenValleysParams *)params, emerge);
+               return new MapgenValleys((MapgenValleysParams *)params, emerge);
        default:
-               return NULL;
+               return nullptr;
        }
 }
 
@@ -193,7 +202,7 @@ MapgenParams *Mapgen::createMapgenParams(MapgenType mgtype)
        case MAPGEN_VALLEYS:
                return new MapgenValleysParams;
        default:
-               return NULL;
+               return nullptr;
        }
 }
 
@@ -206,6 +215,17 @@ void Mapgen::getMapgenNames(std::vector<const char *> *mgnames, bool include_hid
        }
 }
 
+void Mapgen::setDefaultSettings(Settings *settings)
+{
+       settings->setDefault("mg_flags", flagdesc_mapgen,
+                MG_CAVES | MG_DUNGEONS | MG_LIGHT | MG_DECORATIONS | MG_BIOMES | MG_ORES);
+
+       for (int i = 0; i < (int)MAPGEN_INVALID; ++i) {
+               MapgenParams *params = createMapgenParams((MapgenType)i);
+               params->setDefaultSettings(settings);
+               delete params;
+       }
+}
 
 u32 Mapgen::getBlockSeed(v3s16 p, s32 seed)
 {
@@ -218,32 +238,13 @@ u32 Mapgen::getBlockSeed(v3s16 p, s32 seed)
 
 u32 Mapgen::getBlockSeed2(v3s16 p, s32 seed)
 {
-       u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
+       // Multiply by unsigned number to avoid signed overflow (UB)
+       u32 n = 1619U * p.X + 31337U * p.Y + 52591U * p.Z + 1013U * seed;
        n = (n >> 13) ^ n;
        return (n * (n * n * 60493 + 19990303) + 1376312589);
 }
 
 
-// Returns Y one under area minimum if not found
-s16 Mapgen::findGroundLevelFull(v2s16 p2d)
-{
-       const v3s16 &em = vm->m_area.getExtent();
-       s16 y_nodes_max = vm->m_area.MaxEdge.Y;
-       s16 y_nodes_min = vm->m_area.MinEdge.Y;
-       u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
-       s16 y;
-
-       for (y = y_nodes_max; y >= y_nodes_min; y--) {
-               MapNode &n = vm->m_data[i];
-               if (ndef->get(n).walkable)
-                       break;
-
-               vm->m_area.add_y(em, i, -1);
-       }
-       return (y >= y_nodes_min) ? y : y_nodes_min - 1;
-}
-
-
 // Returns -MAX_MAP_GENERATION_LIMIT if not found
 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
 {
@@ -256,7 +257,7 @@ s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
                if (ndef->get(n).walkable)
                        break;
 
-               vm->m_area.add_y(em, i, -1);
+               VoxelArea::add_y(em, i, -1);
        }
        return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
 }
@@ -277,7 +278,7 @@ s16 Mapgen::findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax)
                if (ndef->get(n).isLiquid())
                        break;
 
-               vm->m_area.add_y(em, i, -1);
+               VoxelArea::add_y(em, i, -1);
        }
        return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
 }
@@ -301,64 +302,57 @@ void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
 
 
 void Mapgen::getSurfaces(v2s16 p2d, s16 ymin, s16 ymax,
-       s16 *floors, s16 *ceilings, u16 *num_floors, u16 *num_ceilings)
+       std::vector<s16> &floors, std::vector<s16> &ceilings)
 {
-       u16 floor_i = 0;
-       u16 ceiling_i = 0;
        const v3s16 &em = vm->m_area.getExtent();
 
        bool is_walkable = false;
        u32 vi = vm->m_area.index(p2d.X, ymax, p2d.Y);
        MapNode mn_max = vm->m_data[vi];
        bool walkable_above = ndef->get(mn_max).walkable;
-       vm->m_area.add_y(em, vi, -1);
+       VoxelArea::add_y(em, vi, -1);
 
        for (s16 y = ymax - 1; y >= ymin; y--) {
                MapNode mn = vm->m_data[vi];
                is_walkable = ndef->get(mn).walkable;
 
                if (is_walkable && !walkable_above) {
-                       floors[floor_i] = y;
-                       floor_i++;
+                       floors.push_back(y);
                } else if (!is_walkable && walkable_above) {
-                       ceilings[ceiling_i] = y + 1;
-                       ceiling_i++;
+                       ceilings.push_back(y + 1);
                }
 
-               vm->m_area.add_y(em, vi, -1);
+               VoxelArea::add_y(em, vi, -1);
                walkable_above = is_walkable;
        }
-
-       *num_floors = floor_i;
-       *num_ceilings = ceiling_i;
 }
 
 
 inline bool Mapgen::isLiquidHorizontallyFlowable(u32 vi, v3s16 em)
 {
        u32 vi_neg_x = vi;
-       vm->m_area.add_x(em, vi_neg_x, -1);
+       VoxelArea::add_x(em, vi_neg_x, -1);
        if (vm->m_data[vi_neg_x].getContent() != CONTENT_IGNORE) {
                const ContentFeatures &c_nx = ndef->get(vm->m_data[vi_neg_x]);
                if (c_nx.floodable && !c_nx.isLiquid())
                        return true;
        }
        u32 vi_pos_x = vi;
-       vm->m_area.add_x(em, vi_pos_x, +1);
+       VoxelArea::add_x(em, vi_pos_x, +1);
        if (vm->m_data[vi_pos_x].getContent() != CONTENT_IGNORE) {
                const ContentFeatures &c_px = ndef->get(vm->m_data[vi_pos_x]);
                if (c_px.floodable && !c_px.isLiquid())
                        return true;
        }
        u32 vi_neg_z = vi;
-       vm->m_area.add_z(em, vi_neg_z, -1);
+       VoxelArea::add_z(em, vi_neg_z, -1);
        if (vm->m_data[vi_neg_z].getContent() != CONTENT_IGNORE) {
                const ContentFeatures &c_nz = ndef->get(vm->m_data[vi_neg_z]);
                if (c_nz.floodable && !c_nz.isLiquid())
                        return true;
        }
        u32 vi_pos_z = vi;
-       vm->m_area.add_z(em, vi_pos_z, +1);
+       VoxelArea::add_z(em, vi_pos_z, +1);
        if (vm->m_data[vi_pos_z].getContent() != CONTENT_IGNORE) {
                const ContentFeatures &c_pz = ndef->get(vm->m_data[vi_pos_z]);
                if (c_pz.floodable && !c_pz.isLiquid())
@@ -402,7 +396,7 @@ void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nm
                        } else {
                                // This is the topmost node below a liquid column
                                u32 vi_above = vi;
-                               vm->m_area.add_y(em, vi_above, 1);
+                               VoxelArea::add_y(em, vi_above, 1);
                                if (!waspushed && (ndef->get(vm->m_data[vi]).floodable ||
                                                (!waschecked && isLiquidHorizontallyFlowable(vi_above, em)))) {
                                        // Push back the lowest node in the column which is one
@@ -413,7 +407,7 @@ void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nm
 
                        wasliquid = isliquid;
                        wasignored = isignored;
-                       vm->m_area.add_y(em, vi, -1);
+                       VoxelArea::add_y(em, vi, -1);
                }
        }
 }
@@ -421,7 +415,7 @@ void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nm
 
 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
 {
-       ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
+       ScopeProfiler sp(g_profiler, "EmergeThread: update lighting", SPT_AVG);
        VoxelArea a(nmin, nmax);
 
        for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
@@ -434,7 +428,8 @@ void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
 }
 
 
-void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
+void Mapgen::lightSpread(VoxelArea &a, std::queue<std::pair<v3s16, u8>> &queue,
+       const v3s16 &p, u8 light)
 {
        if (light <= 1 || !a.contains(p))
                return;
@@ -454,8 +449,8 @@ void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
        // Bail out only if we have no more light from either bank to propogate, or
        // we hit a solid block that light cannot pass through.
        if ((light_day  <= (n.param1 & 0x0F) &&
-               light_night <= (n.param1 & 0xF0)) ||
-               !ndef->get(n).light_propagates)
+                       light_night <= (n.param1 & 0xF0)) ||
+                       !ndef->get(n).light_propagates)
                return;
 
        // Since this recursive function only terminates when there is no light from
@@ -466,19 +461,15 @@ void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
 
        n.param1 = light;
 
-       lightSpread(a, p + v3s16(0, 0, 1), light);
-       lightSpread(a, p + v3s16(0, 1, 0), light);
-       lightSpread(a, p + v3s16(1, 0, 0), light);
-       lightSpread(a, p - v3s16(0, 0, 1), light);
-       lightSpread(a, p - v3s16(0, 1, 0), light);
-       lightSpread(a, p - v3s16(1, 0, 0), light);
+       // add to queue
+       queue.emplace(p, light);
 }
 
 
 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
        bool propagate_shadow)
 {
-       ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
+       ScopeProfiler sp(g_profiler, "EmergeThread: update lighting", SPT_AVG);
        //TimeTaker t("updateLighting");
 
        propagateSunlight(nmin, nmax, propagate_shadow);
@@ -509,14 +500,14 @@ void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow)
                                        propagate_shadow) {
                                continue;
                        }
-                       vm->m_area.add_y(em, i, -1);
+                       VoxelArea::add_y(em, i, -1);
 
                        for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
                                MapNode &n = vm->m_data[i];
                                if (!ndef->get(n).sunlight_propagates)
                                        break;
                                n.param1 = LIGHT_SUN;
-                               vm->m_area.add_y(em, i, -1);
+                               VoxelArea::add_y(em, i, -1);
                        }
                }
        }
@@ -524,9 +515,10 @@ void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow)
 }
 
 
-void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
+void Mapgen::spreadLight(const v3s16 &nmin, const v3s16 &nmax)
 {
        //TimeTaker t("spreadLight");
+       std::queue<std::pair<v3s16, u8>> queue;
        VoxelArea a(nmin, nmax);
 
        for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
@@ -550,18 +542,24 @@ void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
 
                                u8 light = n.param1;
                                if (light) {
-                                       lightSpread(a, v3s16(x,     y,     z + 1), light);
-                                       lightSpread(a, v3s16(x,     y + 1, z    ), light);
-                                       lightSpread(a, v3s16(x + 1, y,     z    ), light);
-                                       lightSpread(a, v3s16(x,     y,     z - 1), light);
-                                       lightSpread(a, v3s16(x,     y - 1, z    ), light);
-                                       lightSpread(a, v3s16(x - 1, y,     z    ), light);
+                                       const v3s16 p(x, y, z);
+                                       // spread to all 6 neighbor nodes
+                                       for (const auto &dir : g_6dirs)
+                                               lightSpread(a, queue, p + dir, light);
                                }
                        }
                }
        }
 
-       //printf("spreadLight: %dms\n", t.stop());
+       while (!queue.empty()) {
+               const auto &i = queue.front();
+               // spread to all 6 neighbor nodes
+               for (const auto &dir : g_6dirs)
+                       lightSpread(a, queue, i.first + dir, i.second);
+               queue.pop();
+       }
+
+       //printf("spreadLight: %lums\n", t.stop());
 }
 
 
@@ -569,7 +567,7 @@ void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
 //// MapgenBasic
 ////
 
-MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge)
+MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeParams *emerge)
        : Mapgen(mapgenid, params, emerge)
 {
        this->m_emerge = emerge;
@@ -598,57 +596,40 @@ MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emer
        this->heightmap = new s16[csize.X * csize.Z];
 
        //// Initialize biome generator
-       // TODO(hmmmm): should we have a way to disable biomemanager biomes?
-       biomegen = m_bmgr->createBiomeGen(BIOMEGEN_ORIGINAL, params->bparams, csize);
+       biomegen = emerge->biomegen;
+       biomegen->assertChunkSize(csize);
        biomemap = biomegen->biomemap;
 
        //// Look up some commonly used content
        c_stone              = ndef->getId("mapgen_stone");
-       c_desert_stone       = ndef->getId("mapgen_desert_stone");
-       c_sandstone          = ndef->getId("mapgen_sandstone");
        c_water_source       = ndef->getId("mapgen_water_source");
        c_river_water_source = ndef->getId("mapgen_river_water_source");
        c_lava_source        = ndef->getId("mapgen_lava_source");
-
-       // Fall back to more basic content if not defined
-       // river_water_source cannot fallback to water_source because river water
-       // needs to be non-renewable and have a short flow range.
-       if (c_desert_stone == CONTENT_IGNORE)
-               c_desert_stone = c_stone;
-       if (c_sandstone == CONTENT_IGNORE)
-               c_sandstone = c_stone;
-
-       //// Content used for dungeon generation
-       c_cobble                = ndef->getId("mapgen_cobble");
-       c_mossycobble           = ndef->getId("mapgen_mossycobble");
-       c_stair_cobble          = ndef->getId("mapgen_stair_cobble");
-       c_stair_desert_stone    = ndef->getId("mapgen_stair_desert_stone");
-       c_sandstonebrick        = ndef->getId("mapgen_sandstonebrick");
-       c_stair_sandstone_block = ndef->getId("mapgen_stair_sandstone_block");
-
-       // Fall back to more basic content if not defined
-       if (c_mossycobble == CONTENT_IGNORE)
-               c_mossycobble = c_cobble;
-       if (c_stair_cobble == CONTENT_IGNORE)
-               c_stair_cobble = c_cobble;
-       if (c_stair_desert_stone == CONTENT_IGNORE)
-               c_stair_desert_stone = c_desert_stone;
-       if (c_sandstonebrick == CONTENT_IGNORE)
-               c_sandstonebrick = c_sandstone;
-       if (c_stair_sandstone_block == CONTENT_IGNORE)
-               c_stair_sandstone_block = c_sandstonebrick;
+       c_cobble             = ndef->getId("mapgen_cobble");
+
+       // Fall back to more basic content if not defined.
+       // Lava falls back to water as both are suitable as cave liquids.
+       if (c_lava_source == CONTENT_IGNORE)
+               c_lava_source = c_water_source;
+
+       if (c_stone == CONTENT_IGNORE)
+               errorstream << "Mapgen: Mapgen alias 'mapgen_stone' is invalid!" << std::endl;
+       if (c_water_source == CONTENT_IGNORE)
+               errorstream << "Mapgen: Mapgen alias 'mapgen_water_source' is invalid!" << std::endl;
+       if (c_river_water_source == CONTENT_IGNORE)
+               warningstream << "Mapgen: Mapgen alias 'mapgen_river_water_source' is invalid!" << std::endl;
 }
 
 
 MapgenBasic::~MapgenBasic()
 {
-       delete biomegen;
        delete []heightmap;
+
+       delete m_emerge; // destroying EmergeParams is our responsibility
 }
 
 
-void MapgenBasic::generateBiomes(MgStoneType *mgstone_type,
-       content_t *biome_stone)
+void MapgenBasic::generateBiomes()
 {
        // can't generate biomes without a biome generator!
        assert(biomegen);
@@ -656,14 +637,13 @@ void MapgenBasic::generateBiomes(MgStoneType *mgstone_type,
 
        const v3s16 &em = vm->m_area.getExtent();
        u32 index = 0;
-       MgStoneType stone_type = MGSTONE_OTHER;
-       content_t c_biome_stone = c_stone;
 
        noise_filler_depth->perlinMap2D(node_min.X, node_min.Z);
 
        for (s16 z = node_min.Z; z <= node_max.Z; z++)
        for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
                Biome *biome = NULL;
+               biome_t water_biome_index = 0;
                u16 depth_top = 0;
                u16 base_filler = 0;
                u16 depth_water_top = 0;
@@ -700,29 +680,24 @@ void MapgenBasic::generateBiomes(MgStoneType *mgstone_type,
 
                        if (is_stone_surface || is_water_surface) {
                                // (Re)calculate biome
-                               biome = biomegen->getBiomeAtIndex(index, y);
+                               biome = biomegen->getBiomeAtIndex(index, v3s16(x, y, z));
 
+                               // Add biome to biomemap at first stone surface detected
                                if (biomemap[index] == BIOME_NONE && is_stone_surface)
                                        biomemap[index] = biome->index;
 
+                               // Store biome of first water surface detected, as a fallback
+                               // entry for the biomemap.
+                               if (water_biome_index == 0 && is_water_surface)
+                                       water_biome_index = biome->index;
+
                                depth_top = biome->depth_top;
                                base_filler = MYMAX(depth_top +
                                        biome->depth_filler +
                                        noise_filler_depth->result[index], 0.0f);
                                depth_water_top = biome->depth_water_top;
                                depth_riverbed = biome->depth_riverbed;
-                               biome_y_min = biome->y_min;
-
-                               // Detect stone type for dungeons during every biome calculation.
-                               // If none detected the last selected biome stone is chosen.
-                               if (biome->c_stone == c_stone)
-                                       stone_type = MGSTONE_STONE;
-                               else if (biome->c_stone == c_desert_stone)
-                                       stone_type = MGSTONE_DESERT_STONE;
-                               else if (biome->c_stone == c_sandstone)
-                                       stone_type = MGSTONE_SANDSTONE;
-
-                               c_biome_stone = biome->c_stone;
+                               biome_y_min = biome->min_pos.Y;
                        }
 
                        if (c == c_stone) {
@@ -780,12 +755,14 @@ void MapgenBasic::generateBiomes(MgStoneType *mgstone_type,
                                water_above = false;
                        }
 
-                       vm->m_area.add_y(em, vi, -1);
+                       VoxelArea::add_y(em, vi, -1);
                }
+               // If no stone surface detected in mapchunk column and a water surface
+               // biome fallback exists, add it to the biomemap. This avoids water
+               // surface decorations failing in deep water.
+               if (biomemap[index] == BIOME_NONE && water_biome_index != 0)
+                       biomemap[index] = water_biome_index;
        }
-
-       *mgstone_type = stone_type;
-       *biome_stone = c_biome_stone;
 }
 
 
@@ -804,6 +781,10 @@ void MapgenBasic::dustTopNodes()
                if (biome->c_dust == CONTENT_IGNORE)
                        continue;
 
+               // Check if mapchunk above has generated, if so, drop dust from 16 nodes
+               // above current mapchunk top, above decorations that will extend above
+               // the current mapchunk. If the mapchunk above has not generated, it
+               // will provide this required dust when it does.
                u32 vi = vm->m_area.index(x, full_node_max.Y, z);
                content_t c_full_max = vm->m_data[vi].getContent();
                s16 y_start;
@@ -827,52 +808,74 @@ void MapgenBasic::dustTopNodes()
                        if (vm->m_data[vi].getContent() != CONTENT_AIR)
                                break;
 
-                       vm->m_area.add_y(em, vi, -1);
+                       VoxelArea::add_y(em, vi, -1);
                }
 
                content_t c = vm->m_data[vi].getContent();
                NodeDrawType dtype = ndef->get(c).drawtype;
-               // Only place on walkable cubic non-liquid nodes
-               // Dust check needed due to vertical overgeneration
+               // Only place on cubic, walkable, non-dust nodes.
+               // Dust check needed due to avoid double layer of dust caused by
+               // dropping dust from 16 nodes above mapchunk top.
                if ((dtype == NDT_NORMAL ||
+                               dtype == NDT_ALLFACES ||
                                dtype == NDT_ALLFACES_OPTIONAL ||
-                               dtype == NDT_GLASSLIKE_FRAMED_OPTIONAL ||
                                dtype == NDT_GLASSLIKE ||
                                dtype == NDT_GLASSLIKE_FRAMED ||
-                               dtype == NDT_ALLFACES) &&
+                               dtype == NDT_GLASSLIKE_FRAMED_OPTIONAL) &&
                                ndef->get(c).walkable && c != biome->c_dust) {
-                       vm->m_area.add_y(em, vi, 1);
+                       VoxelArea::add_y(em, vi, 1);
                        vm->m_data[vi] = MapNode(biome->c_dust);
                }
        }
 }
 
 
-void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth)
+void MapgenBasic::generateCavesNoiseIntersection(s16 max_stone_y)
 {
-       if (max_stone_y < node_min.Y)
+       // cave_width >= 10 is used to disable generation and avoid the intensive
+       // 3D noise calculations. Tunnels already have zero width when cave_width > 1.
+       if (node_min.Y > max_stone_y || cave_width >= 10.0f)
                return;
 
        CavesNoiseIntersection caves_noise(ndef, m_bmgr, csize,
                &np_cave1, &np_cave2, seed, cave_width);
 
        caves_noise.generateCaves(vm, node_min, node_max, biomemap);
+}
+
 
-       if (node_max.Y > large_cave_depth)
+void MapgenBasic::generateCavesRandomWalk(s16 max_stone_y, s16 large_cave_ymax)
+{
+       if (node_min.Y > max_stone_y)
                return;
 
        PseudoRandom ps(blockseed + 21343);
-       u32 bruises_count = ps.range(0, 2);
-       for (u32 i = 0; i < bruises_count; i++) {
+       // Small randomwalk caves
+       u32 num_small_caves = ps.range(small_cave_num_min, small_cave_num_max);
+
+       for (u32 i = 0; i < num_small_caves; i++) {
                CavesRandomWalk cave(ndef, &gennotify, seed, water_level,
-                       c_water_source, CONTENT_IGNORE, lava_depth);
+                       c_water_source, c_lava_source, large_cave_flooded, biomegen);
+               cave.makeCave(vm, node_min, node_max, &ps, false, max_stone_y, heightmap);
+       }
+
+       if (node_max.Y > large_cave_ymax)
+               return;
 
+       // Large randomwalk caves below 'large_cave_ymax'.
+       // 'large_cave_ymax' can differ from the 'large_cave_depth' mapgen parameter,
+       // it is set to world base to disable large caves in or near caverns.
+       u32 num_large_caves = ps.range(large_cave_num_min, large_cave_num_max);
+
+       for (u32 i = 0; i < num_large_caves; i++) {
+               CavesRandomWalk cave(ndef, &gennotify, seed, water_level,
+                       c_water_source, c_lava_source, large_cave_flooded, biomegen);
                cave.makeCave(vm, node_min, node_max, &ps, true, max_stone_y, heightmap);
        }
 }
 
 
-bool MapgenBasic::generateCaverns(s16 max_stone_y)
+bool MapgenBasic::generateCavernsNoise(s16 max_stone_y)
 {
        if (node_min.Y > max_stone_y || node_min.Y > cavern_limit)
                return false;
@@ -884,83 +887,65 @@ bool MapgenBasic::generateCaverns(s16 max_stone_y)
 }
 
 
-void MapgenBasic::generateDungeons(s16 max_stone_y,
-       MgStoneType stone_type, content_t biome_stone)
+void MapgenBasic::generateDungeons(s16 max_stone_y)
 {
-       if (max_stone_y < node_min.Y)
+       if (node_min.Y > max_stone_y || node_min.Y > dungeon_ymax ||
+                       node_max.Y < dungeon_ymin)
                return;
 
-       DungeonParams dp;
-
-       dp.seed             = seed;
-       dp.c_water          = c_water_source;
-       dp.c_river_water    = c_river_water_source;
+       u16 num_dungeons = std::fmax(std::floor(
+               NoisePerlin3D(&np_dungeons, node_min.X, node_min.Y, node_min.Z, seed)), 0.0f);
+       if (num_dungeons == 0)
+               return;
 
-       dp.only_in_ground   = true;
-       dp.corridor_len_min = 1;
-       dp.corridor_len_max = 13;
-       dp.rooms_min        = 2;
-       dp.rooms_max        = 16;
-       dp.y_min            = -MAX_MAP_GENERATION_LIMIT;
-       dp.y_max            = MAX_MAP_GENERATION_LIMIT;
+       PseudoRandom ps(blockseed + 70033);
 
-       dp.np_density       = nparams_dungeon_density;
-       dp.np_alt_wall      = nparams_dungeon_alt_wall;
+       DungeonParams dp;
 
-       switch (stone_type) {
-       default:
-       case MGSTONE_STONE:
-               dp.c_wall              = c_cobble;
-               dp.c_alt_wall          = c_mossycobble;
-               dp.c_stair             = c_stair_cobble;
-
-               dp.diagonal_dirs       = false;
-               dp.holesize            = v3s16(1, 2, 1);
-               dp.room_size_min       = v3s16(4, 4, 4);
-               dp.room_size_max       = v3s16(8, 6, 8);
-               dp.room_size_large_min = v3s16(8, 8, 8);
-               dp.room_size_large_max = v3s16(16, 16, 16);
-               dp.notifytype          = GENNOTIFY_DUNGEON;
-               break;
-       case MGSTONE_DESERT_STONE:
-               dp.c_wall              = c_desert_stone;
-               dp.c_alt_wall          = CONTENT_IGNORE;
-               dp.c_stair             = c_stair_desert_stone;
-
-               dp.diagonal_dirs       = true;
-               dp.holesize            = v3s16(2, 3, 2);
-               dp.room_size_min       = v3s16(6, 9, 6);
-               dp.room_size_max       = v3s16(10, 11, 10);
-               dp.room_size_large_min = v3s16(10, 13, 10);
-               dp.room_size_large_max = v3s16(18, 21, 18);
-               dp.notifytype          = GENNOTIFY_TEMPLE;
-               break;
-       case MGSTONE_SANDSTONE:
-               dp.c_wall              = c_sandstonebrick;
-               dp.c_alt_wall          = CONTENT_IGNORE;
-               dp.c_stair             = c_stair_sandstone_block;
-
-               dp.diagonal_dirs       = false;
-               dp.holesize            = v3s16(2, 2, 2);
-               dp.room_size_min       = v3s16(6, 4, 6);
-               dp.room_size_max       = v3s16(10, 6, 10);
-               dp.room_size_large_min = v3s16(10, 8, 10);
-               dp.room_size_large_max = v3s16(18, 16, 18);
-               dp.notifytype          = GENNOTIFY_DUNGEON;
-               break;
-       case MGSTONE_OTHER:
-               dp.c_wall              = biome_stone;
-               dp.c_alt_wall          = biome_stone;
-               dp.c_stair             = biome_stone;
-
-               dp.diagonal_dirs       = false;
-               dp.holesize            = v3s16(1, 2, 1);
-               dp.room_size_min       = v3s16(4, 4, 4);
-               dp.room_size_max       = v3s16(8, 6, 8);
-               dp.room_size_large_min = v3s16(8, 8, 8);
-               dp.room_size_large_max = v3s16(16, 16, 16);
-               dp.notifytype          = GENNOTIFY_DUNGEON;
-               break;
+       dp.np_alt_wall =
+               NoiseParams(-0.4, 1.0, v3f(40.0, 40.0, 40.0), 32474, 6, 1.1, 2.0);
+
+       dp.seed                = seed;
+       dp.only_in_ground      = true;
+       dp.num_dungeons        = num_dungeons;
+       dp.notifytype          = GENNOTIFY_DUNGEON;
+       dp.num_rooms           = ps.range(2, 16);
+       dp.room_size_min       = v3s16(5, 5, 5);
+       dp.room_size_max       = v3s16(12, 6, 12);
+       dp.room_size_large_min = v3s16(12, 6, 12);
+       dp.room_size_large_max = v3s16(16, 16, 16);
+       dp.large_room_chance   = (ps.range(1, 4) == 1) ? 8 : 0;
+       dp.diagonal_dirs       = ps.range(1, 8) == 1;
+       // Diagonal corridors must have 'hole' width >=2 to be passable
+       u8 holewidth           = (dp.diagonal_dirs) ? 2 : ps.range(1, 2);
+       dp.holesize            = v3s16(holewidth, 3, holewidth);
+       dp.corridor_len_min    = 1;
+       dp.corridor_len_max    = 13;
+
+       // Get biome at mapchunk midpoint
+       v3s16 chunk_mid = node_min + (node_max - node_min) / v3s16(2, 2, 2);
+       Biome *biome = (Biome *)biomegen->getBiomeAtPoint(chunk_mid);
+
+       // Use biome-defined dungeon nodes if defined
+       if (biome->c_dungeon != CONTENT_IGNORE) {
+               dp.c_wall = biome->c_dungeon;
+               // If 'node_dungeon_alt' is not defined by biome, it and dp.c_alt_wall
+               // become CONTENT_IGNORE which skips the alt wall node placement loop in
+               // dungeongen.cpp.
+               dp.c_alt_wall = biome->c_dungeon_alt;
+               // Stairs fall back to 'c_dungeon' if not defined by biome
+               dp.c_stair = (biome->c_dungeon_stair != CONTENT_IGNORE) ?
+                       biome->c_dungeon_stair : biome->c_dungeon;
+       // Fallback to using cobble mapgen alias if defined
+       } else if (c_cobble != CONTENT_IGNORE) {
+               dp.c_wall     = c_cobble;
+               dp.c_alt_wall = CONTENT_IGNORE;
+               dp.c_stair    = c_cobble;
+       // Fallback to using biome-defined stone
+       } else {
+               dp.c_wall     = biome->c_stone;
+               dp.c_alt_wall = CONTENT_IGNORE;
+               dp.c_stair    = biome->c_stone;
        }
 
        DungeonGen dgen(ndef, &gennotify, &dp);
@@ -973,32 +958,20 @@ void MapgenBasic::generateDungeons(s16 max_stone_y,
 ////
 
 GenerateNotifier::GenerateNotifier(u32 notify_on,
-       std::set<u32> *notify_on_deco_ids)
+       const std::set<u32> *notify_on_deco_ids)
 {
        m_notify_on = notify_on;
        m_notify_on_deco_ids = notify_on_deco_ids;
 }
 
 
-void GenerateNotifier::setNotifyOn(u32 notify_on)
-{
-       m_notify_on = notify_on;
-}
-
-
-void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
-{
-       m_notify_on_deco_ids = notify_on_deco_ids;
-}
-
-
 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
 {
        if (!(m_notify_on & (1 << type)))
                return false;
 
        if (type == GENNOTIFY_DECORATION &&
-               m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
+               m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->cend())
                return false;
 
        GenNotifyEvent gne;
@@ -1012,8 +985,7 @@ bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
 
 
 void GenerateNotifier::getEvents(
-       std::map<std::string, std::vector<v3s16> > &event_map,
-       bool peek_events)
+       std::map<std::string, std::vector<v3s16> > &event_map)
 {
        std::list<GenNotifyEvent>::iterator it;
 
@@ -1025,9 +997,12 @@ void GenerateNotifier::getEvents(
 
                event_map[name].push_back(gn.pos);
        }
+}
 
-       if (!peek_events)
-               m_notify_events.clear();
+
+void GenerateNotifier::clearEvents()
+{
+       m_notify_events.clear();
 }
 
 
@@ -1044,10 +1019,11 @@ MapgenParams::~MapgenParams()
 
 void MapgenParams::readParams(const Settings *settings)
 {
-       std::string seed_str;
-       const char *seed_name = (settings == g_settings) ? "fixed_map_seed" : "seed";
+       // should always be used via MapSettingsManager
+       assert(settings != g_settings);
 
-       if (settings->getNoEx(seed_name, seed_str)) {
+       std::string seed_str;
+       if (settings->getNoEx("seed", seed_str)) {
                if (!seed_str.empty())
                        seed = read_seed(seed_str.c_str());
                else
@@ -1082,19 +1058,17 @@ void MapgenParams::writeParams(Settings *settings) const
        settings->setS16("water_level", water_level);
        settings->setS16("mapgen_limit", mapgen_limit);
        settings->setS16("chunksize", chunksize);
-       settings->setFlagStr("mg_flags", flags, flagdesc_mapgen, U32_MAX);
+       settings->setFlagStr("mg_flags", flags, flagdesc_mapgen);
 
        if (bparams)
                bparams->writeParams(settings);
 }
 
-// Calculate edges of outermost generated mapchunks (less than
-// 'mapgen_limit'), and corresponding exact limits for SAO entities.
+
+// Calculate exact edges of the outermost mapchunks that are within the
+// set 'mapgen_limit'.
 void MapgenParams::calcMapgenEdges()
 {
-       if (m_mapgen_edges_calculated)
-               return;
-
        // Central chunk offset, in blocks
        s16 ccoff_b = -chunksize / 2;
        // Chunksize, in nodes
@@ -1119,31 +1093,15 @@ void MapgenParams::calcMapgenEdges()
        // Mapgen edges, in nodes
        mapgen_edge_min = ccmin - numcmin * csize_n;
        mapgen_edge_max = ccmax + numcmax * csize_n;
-       // SAO position limits, in Irrlicht units
-       m_sao_limit_min = mapgen_edge_min * BS - 3.0f;
-       m_sao_limit_max = mapgen_edge_max * BS + 3.0f;
 
        m_mapgen_edges_calculated = true;
 }
 
 
-bool MapgenParams::saoPosOverLimit(const v3f &p)
+s32 MapgenParams::getSpawnRangeMax()
 {
        if (!m_mapgen_edges_calculated)
                calcMapgenEdges();
 
-       return p.X < m_sao_limit_min ||
-               p.X > m_sao_limit_max ||
-               p.Y < m_sao_limit_min ||
-               p.Y > m_sao_limit_max ||
-               p.Z < m_sao_limit_min ||
-               p.Z > m_sao_limit_max;
-}
-
-
-s32 MapgenParams::getSpawnRangeMax()
-{
-       calcMapgenEdges();
-
        return MYMIN(-mapgen_edge_min, mapgen_edge_max);
 }