]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/mapgen/mapgen.cpp
Make Mapgen::spreadLight use a queue (#8838)
[dragonfireclient.git] / src / mapgen / mapgen.cpp
index 85aab363b86b687a0ed740cd4fa6cef17053992c..9f17af866e2a44474130e6294dfc913aae4fb55c 100644 (file)
@@ -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"
@@ -38,6 +39,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"
@@ -80,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(
@@ -148,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,
+       EmergeManager *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;
        }
 }
 
@@ -194,7 +202,7 @@ MapgenParams *Mapgen::createMapgenParams(MapgenType mgtype)
        case MAPGEN_VALLEYS:
                return new MapgenValleysParams;
        default:
-               return NULL;
+               return nullptr;
        }
 }
 
@@ -415,7 +423,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++) {
@@ -428,7 +436,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;
@@ -448,8 +457,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
@@ -460,19 +469,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);
@@ -518,9 +523,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++) {
@@ -544,18 +550,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());
 }
 
 
@@ -592,45 +604,20 @@ 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);
        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");
+       c_cobble             = ndef->getId("mapgen_cobble");
 
-       // 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;
+       // 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;
 }
 
 
@@ -655,6 +642,7 @@ void MapgenBasic::generateBiomes()
        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;
@@ -697,6 +685,11 @@ void MapgenBasic::generateBiomes()
                                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 +
@@ -763,6 +756,11 @@ void MapgenBasic::generateBiomes()
 
                        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;
        }
 }
 
@@ -878,94 +876,57 @@ void MapgenBasic::generateDungeons(s16 max_stone_y)
        if (max_stone_y < node_min.Y)
                return;
 
-       // 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);
+       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;
+
+       PseudoRandom ps(blockseed + 70033);
 
        DungeonParams dp;
 
-       dp.seed             = seed;
-       dp.only_in_ground   = true;
-       dp.corridor_len_min = 1;
-       dp.corridor_len_max = 13;
-       dp.rooms_min        = 2;
-       dp.rooms_max        = 16;
+       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.num_dungeons        = num_dungeons;
+       dp.only_in_ground      = true;
+       dp.num_rooms           = ps.range(2, 16);
+       dp.room_size_min       = v3s16(6, 5, 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.large_room_chance   = (ps.range(1, 4) == 1) ? 1 : 0;
+       dp.holesize            = v3s16(2, 3, 2);
+       dp.corridor_len_min    = 1;
+       dp.corridor_len_max    = 13;
+       dp.diagonal_dirs       = ps.range(1, 12) == 1;
+       dp.notifytype          = GENNOTIFY_DUNGEON;
 
-       dp.np_density       = nparams_dungeon_density;
-       dp.np_alt_wall      = nparams_dungeon_alt_wall;
+       // 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);
 
-       // Biome-defined dungeon nodes
+       // Use biome-defined dungeon nodes if defined
        if (biome->c_dungeon != CONTENT_IGNORE) {
-               dp.c_wall              = biome->c_dungeon;
+               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;
+               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;
-
-               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;
-
-       // Otherwise classic behaviour
-       } else if (biome->c_stone == c_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;
-
-       } else if (biome->c_stone == c_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;
-
-       } else if (biome->c_stone == c_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;
-
-       // Fallback to using biome 'node_stone'
+       // 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;
-
-               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;
+               dp.c_wall     = biome->c_stone;
+               dp.c_alt_wall = CONTENT_IGNORE;
+               dp.c_stair    = biome->c_stone;
        }
 
        DungeonGen dgen(ndef, &gennotify, &dp);