]> git.lizzy.rs Git - minetest.git/blobdiff - src/mapgen_v7.cpp
Mgv5/6/7: Re-add #include profiler.h as commented-out option
[minetest.git] / src / mapgen_v7.cpp
index 3e7188a2c9d11847b2d9ceaa3deb1704c6dd7600..92f3f49c4458002b080b2aee3ad420b4c9392d75 100644 (file)
@@ -27,9 +27,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "content_sao.h"
 #include "nodedef.h"
 #include "voxelalgorithms.h"
-#include "profiler.h"
+//#include "profiler.h" // For TimeTaker
 #include "settings.h" // For g_settings
-#include "main.h" // For g_profiler
 #include "emerge.h"
 #include "dungeongen.h"
 #include "cavegen.h"
@@ -60,8 +59,10 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge)
        this->ystride = csize.X;
        this->zstride = csize.X * (csize.Y + 2);
 
-       this->biomemap  = new u8[csize.X * csize.Z];
-       this->heightmap = new s16[csize.X * csize.Z];
+       this->biomemap        = new u8[csize.X * csize.Z];
+       this->heightmap       = new s16[csize.X * csize.Z];
+       this->heatmap         = NULL;
+       this->humidmap        = NULL;
        this->ridge_heightmap = new s16[csize.X * csize.Z];
 
        MapgenV7Params *sp = (MapgenV7Params *)params->sparams;
@@ -83,35 +84,37 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge)
        noise_cave2    = new Noise(&sp->np_cave2,    seed, csize.X, csize.Y + 2, csize.Z);
 
        //// Biome noise
-       noise_heat     = new Noise(&params->np_biome_heat,     seed, csize.X, csize.Z);
-       noise_humidity = new Noise(&params->np_biome_humidity, seed, csize.X, csize.Z);
+       noise_heat           = new Noise(&params->np_biome_heat,           seed, csize.X, csize.Z);
+       noise_humidity       = new Noise(&params->np_biome_humidity,       seed, csize.X, csize.Z);
+       noise_heat_blend     = new Noise(&params->np_biome_heat_blend,     seed, csize.X, csize.Z);
+       noise_humidity_blend = new Noise(&params->np_biome_humidity_blend, seed, csize.X, csize.Z);
 
        //// Resolve nodes to be used
        INodeDefManager *ndef = emerge->ndef;
 
-       c_stone           = ndef->getId("mapgen_stone");
-       c_dirt            = ndef->getId("mapgen_dirt");
-       c_dirt_with_grass = ndef->getId("mapgen_dirt_with_grass");
-       c_sand            = ndef->getId("mapgen_sand");
-       c_water_source    = ndef->getId("mapgen_water_source");
-       c_lava_source     = ndef->getId("mapgen_lava_source");
-       c_ice             = ndef->getId("default:ice");
-       c_cobble          = ndef->getId("mapgen_cobble");
-       c_desert_stone    = ndef->getId("mapgen_desert_stone");
-       c_mossycobble     = ndef->getId("mapgen_mossycobble");
-       c_sandbrick       = ndef->getId("mapgen_sandstonebrick");
-       c_stair_cobble    = ndef->getId("mapgen_stair_cobble");
-       c_stair_sandstone = ndef->getId("mapgen_stair_sandstone");
+       c_stone                = ndef->getId("mapgen_stone");
+       c_water_source         = ndef->getId("mapgen_water_source");
+       c_lava_source          = ndef->getId("mapgen_lava_source");
+       c_desert_stone         = ndef->getId("mapgen_desert_stone");
+       c_ice                  = ndef->getId("mapgen_ice");
+       c_sandstone            = ndef->getId("mapgen_sandstone");
+
+       c_cobble               = ndef->getId("mapgen_cobble");
+       c_stair_cobble         = ndef->getId("mapgen_stair_cobble");
+       c_mossycobble          = ndef->getId("mapgen_mossycobble");
+       c_sandstonebrick       = ndef->getId("mapgen_sandstonebrick");
+       c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
+
        if (c_ice == CONTENT_IGNORE)
                c_ice = CONTENT_AIR;
        if (c_mossycobble == CONTENT_IGNORE)
                c_mossycobble = c_cobble;
-       if (c_sandbrick == CONTENT_IGNORE)
-               c_sandbrick = c_desert_stone;
        if (c_stair_cobble == CONTENT_IGNORE)
                c_stair_cobble = c_cobble;
-       if (c_stair_sandstone == CONTENT_IGNORE)
-               c_stair_sandstone = c_sandbrick;
+       if (c_sandstonebrick == CONTENT_IGNORE)
+               c_sandstonebrick = c_sandstone;
+       if (c_stair_sandstonebrick == CONTENT_IGNORE)
+               c_stair_sandstonebrick = c_sandstone;
 }
 
 
@@ -131,6 +134,8 @@ MapgenV7::~MapgenV7()
 
        delete noise_heat;
        delete noise_humidity;
+       delete noise_heat_blend;
+       delete noise_humidity_blend;
 
        delete[] ridge_heightmap;
        delete[] heightmap;
@@ -142,17 +147,17 @@ MapgenV7Params::MapgenV7Params()
 {
        spflags = MGV7_MOUNTAINS | MGV7_RIDGES;
 
-       np_terrain_base    = NoiseParams(4,    70,  v3f(300, 300, 300), 82341, 6, 0.7,  2.0);
-       np_terrain_alt     = NoiseParams(4,    25,  v3f(600, 600, 600), 5934,  5, 0.6,  2.0);
-       np_terrain_persist = NoiseParams(0.6,  0.1, v3f(500, 500, 500), 539,   3, 0.6,  2.0);
-       np_height_select   = NoiseParams(-0.5, 1,   v3f(250, 250, 250), 4213,  5, 0.69, 2.0);
-       np_filler_depth    = NoiseParams(0,    1.2, v3f(150, 150, 150), 261,   4, 0.7,  2.0);
-       np_mount_height    = NoiseParams(100,  30,  v3f(500, 500, 500), 72449, 4, 0.6,  2.0);
-       np_ridge_uwater    = NoiseParams(0,    1,   v3f(500, 500, 500), 85039, 4, 0.6,  2.0);
-       np_mountain        = NoiseParams(-0.6, 1,   v3f(250, 350, 250), 5333,  5, 0.68, 2.0);
-       np_ridge           = NoiseParams(0,    1,   v3f(100, 100, 100), 6467,  4, 0.75, 2.0);
-       np_cave1           = NoiseParams(0,    12,  v3f(100, 100, 100), 52534, 4, 0.5,  2.0);
-       np_cave2           = NoiseParams(0,    12,  v3f(100, 100, 100), 10325, 4, 0.5,  2.0);
+       np_terrain_base    = NoiseParams(4,    70,  v3f(600,  600,  600),  82341, 5, 0.6,  2.0);
+       np_terrain_alt     = NoiseParams(4,    25,  v3f(600,  600,  600),  5934,  5, 0.6,  2.0);
+       np_terrain_persist = NoiseParams(0.6,  0.1, v3f(2000, 2000, 2000), 539,   3, 0.6,  2.0);
+       np_height_select   = NoiseParams(-12,  24,  v3f(500,  500,  500),  4213,  6, 0.7,  2.0);
+       np_filler_depth    = NoiseParams(0,    1.2, v3f(150,  150,  150),  261,   3, 0.7,  2.0);
+       np_mount_height    = NoiseParams(256,  112, v3f(1000, 1000, 1000), 72449, 3, 0.6,  2.0);
+       np_ridge_uwater    = NoiseParams(0,    1,   v3f(1000, 1000, 1000), 85039, 5, 0.6,  2.0);
+       np_mountain        = NoiseParams(-0.6, 1,   v3f(250,  350,  250),  5333,  5, 0.63, 2.0);
+       np_ridge           = NoiseParams(0,    1,   v3f(100,  100,  100),  6467,  4, 0.75, 2.0);
+       np_cave1           = NoiseParams(0,    12,  v3f(100,  100,  100),  52534, 4, 0.5,  2.0);
+       np_cave2           = NoiseParams(0,    12,  v3f(100,  100,  100),  10325, 4, 0.5,  2.0);
 }
 
 
@@ -228,11 +233,11 @@ void MapgenV7::makeChunk(BlockMakeData *data)
        assert(data->vmanip);
        assert(data->nodedef);
        assert(data->blockpos_requested.X >= data->blockpos_min.X &&
-                  data->blockpos_requested.Y >= data->blockpos_min.Y &&
-                  data->blockpos_requested.Z >= data->blockpos_min.Z);
+               data->blockpos_requested.Y >= data->blockpos_min.Y &&
+               data->blockpos_requested.Z >= data->blockpos_min.Z);
        assert(data->blockpos_requested.X <= data->blockpos_max.X &&
-                  data->blockpos_requested.Y <= data->blockpos_max.Y &&
-                  data->blockpos_requested.Z <= data->blockpos_max.Z);
+               data->blockpos_requested.Y <= data->blockpos_max.Y &&
+               data->blockpos_requested.Z <= data->blockpos_max.Z);
 
        this->generating = true;
        this->vm   = data->vmanip;
@@ -262,7 +267,7 @@ void MapgenV7::makeChunk(BlockMakeData *data)
                noise_humidity->result, heightmap, biomemap);
 
        // Actually place the biome-specific nodes
-       bool desert_stone = generateBiomes(noise_heat->result, noise_humidity->result);
+       MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result);
 
        if (flags & MG_CAVES)
                generateCaves(stone_surface_max_y);
@@ -273,27 +278,37 @@ void MapgenV7::makeChunk(BlockMakeData *data)
                dp.np_rarity  = nparams_dungeon_rarity;
                dp.np_density = nparams_dungeon_density;
                dp.np_wetness = nparams_dungeon_wetness;
-               dp.c_water = c_water_source;
-               if (desert_stone) {
-                       dp.c_cobble  = c_sandbrick;
-                       dp.c_moss    = c_sandbrick; // should make this 'cracked sandstone' later
-                       dp.c_stair   = c_stair_sandstone;
+               dp.c_water    = c_water_source;
+               if (stone_type == STONE) {
+                       dp.c_cobble = c_cobble;
+                       dp.c_moss   = c_mossycobble;
+                       dp.c_stair  = c_stair_cobble;
+
+                       dp.diagonal_dirs = false;
+                       dp.mossratio     = 3.0;
+                       dp.holesize      = v3s16(1, 2, 1);
+                       dp.roomsize      = v3s16(0, 0, 0);
+                       dp.notifytype    = GENNOTIFY_DUNGEON;
+               } else if (stone_type == DESERT_STONE) {
+                       dp.c_cobble = c_desert_stone;
+                       dp.c_moss   = c_desert_stone;
+                       dp.c_stair  = c_desert_stone;
 
                        dp.diagonal_dirs = true;
-                       dp.mossratio  = 0.0;
-                       dp.holesize   = v3s16(2, 3, 2);
-                       dp.roomsize   = v3s16(2, 5, 2);
-                       dp.notifytype = GENNOTIFY_TEMPLE;
-               } else {
-                       dp.c_cobble  = c_cobble;
-                       dp.c_moss    = c_mossycobble;
-                       dp.c_stair   = c_stair_cobble;
+                       dp.mossratio     = 0.0;
+                       dp.holesize      = v3s16(2, 3, 2);
+                       dp.roomsize      = v3s16(2, 5, 2);
+                       dp.notifytype    = GENNOTIFY_TEMPLE;
+               } else if (stone_type == SANDSTONE) {
+                       dp.c_cobble = c_sandstonebrick;
+                       dp.c_moss   = c_sandstonebrick;
+                       dp.c_stair  = c_sandstonebrick;
 
                        dp.diagonal_dirs = false;
-                       dp.mossratio  = 3.0;
-                       dp.holesize   = v3s16(1, 2, 1);
-                       dp.roomsize   = v3s16(0, 0, 0);
-                       dp.notifytype = GENNOTIFY_DUNGEON;
+                       dp.mossratio     = 0.0;
+                       dp.holesize      = v3s16(2, 2, 2);
+                       dp.roomsize      = v3s16(2, 0, 2);
+                       dp.notifytype    = GENNOTIFY_DUNGEON;
                }
 
                DungeonGen dgen(this, &dp);
@@ -348,31 +363,38 @@ void MapgenV7::calculateNoise()
                noise_ridge_uwater->perlinMap2D(x, z);
        }
 
-       if ((spflags & MGV7_MOUNTAINS) && node_max.Y >= 0) {
-               noise_mountain->perlinMap3D(x, y, z);
-               noise_mount_height->perlinMap2D(x, z);
-       }
+       // Mountain noises are calculated in generateMountainTerrain()
+
+       noise_filler_depth->perlinMap2D(x, z);
+       noise_heat->perlinMap2D(x, z);
+       noise_humidity->perlinMap2D(x, z);
+       noise_heat_blend->perlinMap2D(x, z);
+       noise_humidity_blend->perlinMap2D(x, z);
 
-       if (node_max.Y >= water_level) {
-               noise_filler_depth->perlinMap2D(x, z);
-               noise_heat->perlinMap2D(x, z);
-               noise_humidity->perlinMap2D(x, z);
+       for (s32 i = 0; i < csize.X * csize.Z; i++) {
+               noise_heat->result[i] += noise_heat_blend->result[i];
+               noise_humidity->result[i] += noise_humidity_blend->result[i];
        }
+
+       heatmap = noise_heat->result;
+       humidmap = noise_humidity->result;
        //printf("calculateNoise: %dus\n", t.stop());
 }
 
 
 Biome *MapgenV7::getBiomeAtPoint(v3s16 p)
 {
-       float heat      = NoisePerlin2D(&noise_heat->np, p.X, p.Z, seed);
-       float humidity  = NoisePerlin2D(&noise_humidity->np, p.X, p.Z, seed);
+       float heat = NoisePerlin2D(&noise_heat->np, p.X, p.Z, seed) +
+               NoisePerlin2D(&noise_heat_blend->np, p.X, p.Z, seed);
+       float humidity = NoisePerlin2D(&noise_humidity->np, p.X, p.Z, seed) +
+               NoisePerlin2D(&noise_humidity_blend->np, p.X, p.Z, seed);
        s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
 
        return bmgr->getBiome(heat, humidity, groundlevel);
 }
 
 //needs to be updated
-float MapgenV7::baseTerrainLevelAtPoint(int x, int z)
+float MapgenV7::baseTerrainLevelAtPoint(s16 x, s16 z)
 {
        float hselect = NoisePerlin2D(&noise_height_select->np, x, z, seed);
        hselect = rangelim(hselect, 0.0, 1.0);
@@ -405,19 +427,23 @@ float MapgenV7::baseTerrainLevelFromMap(int index)
 }
 
 
-bool MapgenV7::getMountainTerrainAtPoint(int x, int y, int z)
+bool MapgenV7::getMountainTerrainAtPoint(s16 x, s16 y, s16 z)
 {
        float mnt_h_n = NoisePerlin2D(&noise_mount_height->np, x, z, seed);
+       float density_gradient = -((float)y / mnt_h_n);
        float mnt_n = NoisePerlin3D(&noise_mountain->np, x, y, z, seed);
-       return mnt_n * mnt_h_n >= (float)y;
+
+       return mnt_n + density_gradient >= 0.0;
 }
 
 
-bool MapgenV7::getMountainTerrainFromMap(int idx_xyz, int idx_xz, int y)
+bool MapgenV7::getMountainTerrainFromMap(int idx_xyz, int idx_xz, s16 y)
 {
        float mounthn = noise_mount_height->result[idx_xz];
+       float density_gradient = -((float)y / mounthn);
        float mountn = noise_mountain->result[idx_xyz];
-       return mountn * mounthn >= (float)y;
+
+       return mountn + density_gradient >= 0.0;
 }
 
 
@@ -458,26 +484,30 @@ void MapgenV7::carveRivers() {
 
 int MapgenV7::generateTerrain()
 {
-       int ymax = generateBaseTerrain();
+       s16 stone_surface_min_y;
+       s16 stone_surface_max_y;
 
-       if (spflags & MGV7_MOUNTAINS)
-               ymax = generateMountainTerrain(ymax);
+       generateBaseTerrain(&stone_surface_min_y, &stone_surface_max_y);
+
+       if ((spflags & MGV7_MOUNTAINS) && stone_surface_min_y < node_max.Y)
+               stone_surface_max_y = generateMountainTerrain(stone_surface_max_y);
 
        if (spflags & MGV7_RIDGES)
                generateRidgeTerrain();
 
-       return ymax;
+       return stone_surface_max_y;
 }
 
 
-int MapgenV7::generateBaseTerrain()
+void MapgenV7::generateBaseTerrain(s16 *stone_surface_min_y, s16 *stone_surface_max_y)
 {
        MapNode n_air(CONTENT_AIR);
        MapNode n_stone(c_stone);
        MapNode n_water(c_water_source);
 
-       int stone_surface_max_y = -MAP_GENERATION_LIMIT;
        v3s16 em = vm->m_area.getExtent();
+       s16 surface_min_y = MAX_MAP_GENERATION_LIMIT;
+       s16 surface_max_y = -MAX_MAP_GENERATION_LIMIT;
        u32 index = 0;
 
        for (s16 z = node_min.Z; z <= node_max.Z; z++)
@@ -488,8 +518,11 @@ int MapgenV7::generateBaseTerrain()
                heightmap[index]       = surface_y;
                ridge_heightmap[index] = surface_y;
 
-               if (surface_y > stone_surface_max_y)
-                       stone_surface_max_y = surface_y;
+               if (surface_y < surface_min_y)
+                       surface_min_y = surface_y;
+
+               if (surface_y > surface_max_y)
+                       surface_max_y = surface_y;
 
                u32 i = vm->m_area.index(x, node_min.Y - 1, z);
                for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
@@ -505,14 +538,15 @@ int MapgenV7::generateBaseTerrain()
                }
        }
 
-       return stone_surface_max_y;
+       *stone_surface_min_y = surface_min_y;
+       *stone_surface_max_y = surface_max_y;
 }
 
 
-int MapgenV7::generateMountainTerrain(int ymax)
+int MapgenV7::generateMountainTerrain(s16 ymax)
 {
-       if (node_max.Y < 0)
-               return ymax;
+       noise_mountain->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
+       noise_mount_height->perlinMap2D(node_min.X, node_min.Z);
 
        MapNode n_stone(c_stone);
        u32 j = 0;
@@ -580,87 +614,100 @@ void MapgenV7::generateRidgeTerrain()
 }
 
 
-bool MapgenV7::generateBiomes(float *heat_map, float *humidity_map)
+MgStoneType MapgenV7::generateBiomes(float *heat_map, float *humidity_map)
 {
-       if (node_max.Y < water_level)
-               return false;
-
        v3s16 em = vm->m_area.getExtent();
        u32 index = 0;
-       bool desert_stone = false;
+       MgStoneType stone_type = STONE;
 
        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;
-               s16 dfiller = 0;
-               s16 y0_top = 0;
-               s16 y0_filler = 0;
-               s16 depth_water_top = 0;
+               u16 depth_top = 0;
+               u16 base_filler = 0;
+               u16 depth_water_top = 0;
+               u32 vi = vm->m_area.index(x, node_max.Y, z);
 
-               s16 nplaced = 0;
-               u32 i = vm->m_area.index(x, node_max.Y, z);
+               // Check node at base of mapchunk above, either a node of a previously
+               // generated mapchunk or if not, a node of overgenerated base terrain.
+               content_t c_above = vm->m_data[vi + em.X].getContent();
+               bool air_above = c_above == CONTENT_AIR;
+               bool water_above = c_above == c_water_source;
 
-               content_t c_above = vm->m_data[i + em.X].getContent();
-               bool have_air = c_above == CONTENT_AIR;
+               // If there is air or water above enable top/filler placement, otherwise force
+               // nplaced to stone level by setting a number exceeding any possible filler depth.
+               u16 nplaced = (air_above || water_above) ? 0 : (u16)-1;
 
                for (s16 y = node_max.Y; y >= node_min.Y; y--) {
-                       content_t c = vm->m_data[i].getContent();
+                       content_t c = vm->m_data[vi].getContent();
 
-                       if (c != CONTENT_IGNORE && c != CONTENT_AIR && (y == node_max.Y || have_air)) {
-                               biome           = bmgr->getBiome(heat_map[index], humidity_map[index], y);
-                               dfiller         = biome->depth_filler + noise_filler_depth->result[index];
-                               y0_top          = biome->depth_top;
-                               y0_filler       = biome->depth_top + dfiller;
+                       // Biome is recalculated each time an upper surface is detected while
+                       // working down a column. The selected biome then remains in effect for
+                       // all nodes below until the next surface and biome recalculation.
+                       // Biome is recalculated:
+                       // 1. At the surface of stone below air or water.
+                       // 2. At the surface of water below air.
+                       // 3. When stone or water is detected but biome has not yet been calculated.
+                       if ((c == c_stone && (air_above || water_above || !biome)) ||
+                                       (c == c_water_source && (air_above || !biome))) {
+                               biome = bmgr->getBiome(heat_map[index], humidity_map[index], y);
+                               depth_top = biome->depth_top;
+                               base_filler = MYMAX(depth_top + biome->depth_filler
+                                               + noise_filler_depth->result[index], 0);
                                depth_water_top = biome->depth_water_top;
 
+                               // Detect stone type for dungeons during every biome calculation.
+                               // This is more efficient than detecting per-node and will not
+                               // miss any desert stone or sandstone biomes.
                                if (biome->c_stone == c_desert_stone)
-                                       desert_stone = true;
+                                       stone_type = DESERT_STONE;
+                               else if (biome->c_stone == c_sandstone)
+                                       stone_type = SANDSTONE;
                        }
 
-                       if (c == c_stone && have_air) {
-                               content_t c_below = vm->m_data[i - em.X].getContent();
-
-                               if (c_below != CONTENT_AIR) {
-                                       if (nplaced < y0_top) {
-                                               vm->m_data[i] = MapNode(biome->c_top);
-                                               nplaced++;
-                                       } else if (nplaced < y0_filler && nplaced >= y0_top) {
-                                               vm->m_data[i] = MapNode(biome->c_filler);
-                                               nplaced++;
-                                       } else if (c == c_stone) {
-                                               have_air = false;
-                                               nplaced  = 0;
-                                               vm->m_data[i] = MapNode(biome->c_stone);
-                                       } else {
-                                               have_air = false;
-                                               nplaced  = 0;
-                                       }
-                               } else if (c == c_stone) {
-                                       have_air = false;
-                                       nplaced = 0;
-                                       vm->m_data[i] = MapNode(biome->c_stone);
+                       if (c == c_stone) {
+                               content_t c_below = vm->m_data[vi - em.X].getContent();
+
+                               // If the node below isn't solid, make this node stone, so that
+                               // any top/filler nodes above are structurally supported.
+                               // This is done by aborting the cycle of top/filler placement
+                               // immediately by forcing nplaced to stone level.
+                               if (c_below == CONTENT_AIR || c_below == c_water_source)
+                                       nplaced = (u16)-1;
+
+                               if (nplaced < depth_top) {
+                                       vm->m_data[vi] = MapNode(biome->c_top);
+                                       nplaced++;
+                               } else if (nplaced < base_filler) {
+                                       vm->m_data[vi] = MapNode(biome->c_filler);
+                                       nplaced++;
+                               } else {
+                                       vm->m_data[vi] = MapNode(biome->c_stone);
                                }
-                       } else if (c == c_stone) {
-                               have_air = false;
-                               nplaced = 0;
-                               vm->m_data[i] = MapNode(biome->c_stone);
+
+                               air_above = false;
+                               water_above = false;
                        } else if (c == c_water_source) {
-                               have_air = true;
-                               nplaced = 0;
-                               if (y > water_level - depth_water_top)
-                                       vm->m_data[i] = MapNode(biome->c_water_top);
-                               else
-                                       vm->m_data[i] = MapNode(biome->c_water);
+                               vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ?
+                                               biome->c_water_top : biome->c_water);
+                               nplaced = 0;  // Enable top/filler placement for next surface
+                               air_above = false;
+                               water_above = true;
                        } else if (c == CONTENT_AIR) {
-                               have_air = true;
-                               nplaced = 0;
+                               nplaced = 0;  // Enable top/filler placement for next surface
+                               air_above = true;
+                               water_above = false;
+                       } else {  // Possible various nodes overgenerated from neighbouring mapchunks
+                               nplaced = (u16)-1;  // Disable top/filler placement
+                               air_above = false;
+                               water_above = false;
                        }
 
-                       vm->m_area.add_y(em, i, -1);
+                       vm->m_area.add_y(em, vi, -1);
                }
        }
 
-       return desert_stone;
+       return stone_type;
 }
 
 
@@ -810,39 +857,32 @@ void MapgenV7::addTopNodes()
 #endif
 
 
-void MapgenV7::generateCaves(int max_stone_y)
+void MapgenV7::generateCaves(s16 max_stone_y)
 {
        if (max_stone_y >= node_min.Y) {
                u32 index   = 0;
-               u32 index2d = 0;
-
-               for (s16 z = node_min.Z; z <= node_max.Z; z++) {
-                       for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
-                               u32 i = vm->m_area.index(node_min.X, y, z);
-                               for (s16 x = node_min.X; x <= node_max.X;
-                                               x++, i++, index++, index2d++) {
-                                       Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]);
+
+               for (s16 z = node_min.Z; z <= node_max.Z; z++)
+               for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
+                       u32 i = vm->m_area.index(node_min.X, y, z);
+                       for (s16 x = node_min.X; x <= node_max.X; x++, i++, index++) {
+                               float d1 = contour(noise_cave1->result[index]);
+                               float d2 = contour(noise_cave2->result[index]);
+                               if (d1 * d2 > 0.3) {
                                        content_t c = vm->m_data[i].getContent();
-                                       if (c == CONTENT_AIR || (y <= water_level &&
-                                               c != biome->c_stone && c != c_stone))
+                                       if (!ndef->get(c).is_ground_content || c == CONTENT_AIR)
                                                continue;
 
-                                       float d1 = contour(noise_cave1->result[index]);
-                                       float d2 = contour(noise_cave2->result[index]);
-                                       if (d1 * d2 > 0.3)
-                                               vm->m_data[i] = MapNode(CONTENT_AIR);
+                                       vm->m_data[i] = MapNode(CONTENT_AIR);
                                }
-                               index2d -= ystride;
                        }
-                       index2d += ystride;
                }
        }
 
        PseudoRandom ps(blockseed + 21343);
-       u32 bruises_count = (ps.range(1, 5) == 1) ? ps.range(1, 2) : 0;
+       u32 bruises_count = (ps.range(1, 4) == 1) ? ps.range(1, 2) : 0;
        for (u32 i = 0; i < bruises_count; i++) {
                CaveV7 cave(this, &ps);
                cave.makeCave(node_min, node_max, max_stone_y);
        }
 }
-