Fix mapgen using unitialised height map values
authorCraig Robbins <kde.psych@gmail.com>
Thu, 5 Mar 2015 06:25:53 +0000 (16:25 +1000)
committerCraig Robbins <kde.psych@gmail.com>
Thu, 5 Mar 2015 15:42:55 +0000 (01:42 +1000)
src/mapgen.cpp
src/mapgen.h
src/mapgen_v5.cpp
src/mapgen_v6.cpp
src/mapgen_v7.cpp

index 071c60138876e3fe11f39cb93c663fb0a8dba59c..17aa1dd92e2c33847bd02af326694653945485b6 100644 (file)
@@ -41,6 +41,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 const char *GenElementManager::ELEMENT_TITLE = "element";
 
+static const s16 INVALID_HEIGHT = MAP_GENERATION_LIMIT + 1;
+
 FlagDesc flagdesc_mapgen[] = {
        {"trees",    MG_TREES},
        {"caves",    MG_CAVES},
@@ -155,6 +157,12 @@ s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
 }
 
 
+void Mapgen::initHeightMap(s16 *dest, size_t len)
+{
+       for (size_t i = 0; i < len; i++)
+               dest[i] = INVALID_HEIGHT;
+}
+
 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
 {
        if (!heightmap)
@@ -166,11 +174,13 @@ void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
                for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
                        s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
 
-                       // if the values found are out of range, trust the old heightmap
-                       if (y == nmax.Y && heightmap[index] > nmax.Y)
-                               continue;
-                       if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
-                               continue;
+                       if (heightmap[index] != INVALID_HEIGHT) {
+                               // if the values found are out of range, trust the old heightmap
+                               if (y == nmax.Y && heightmap[index] > nmax.Y)
+                                       continue;
+                               if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
+                                       continue;
+                       }
 
                        heightmap[index] = y;
                }
index f2e63e53300752ea7fa4542fc3ea140dc5fe5142..01710786b0766cbffc9d07d6da3b19f829e63317 100644 (file)
@@ -151,6 +151,7 @@ public:
        static u32 getBlockSeed2(v3s16 p, int seed);
        s16 findGroundLevelFull(v2s16 p2d);
        s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
+       void initHeightMap(s16 *dest, size_t len);
        void updateHeightmap(v3s16 nmin, v3s16 nmax);
        void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
 
index 9392c35b1f0734a4e257da717ed63a0358ba8b44..34484c7e5e0229bab179cc8a8851b6aa0c7ee11e 100644 (file)
@@ -60,6 +60,8 @@ MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge)
        this->biomemap  = new u8[csize.X * csize.Z];
        this->heightmap = new s16[csize.X * csize.Z];
 
+       initHeightMap(this->heightmap, csize.X * csize.Z);
+
        MapgenV5Params *sp = (MapgenV5Params *)params->sparams;
        this->spflags      = sp->spflags;
 
index 58e6022f8e9876ea210ce70e74314b1909d5285d..8ea4cd21dea0e861c766d7b5dceab84210395c8f 100644 (file)
@@ -57,6 +57,8 @@ MapgenV6::MapgenV6(int mapgenid, MapgenParams *params, EmergeManager *emerge)
 
        this->heightmap = new s16[csize.X * csize.Z];
 
+       initHeightMap(this->heightmap, csize.X * csize.Z);
+
        MapgenV6Params *sp = (MapgenV6Params *)params->sparams;
        this->spflags     = sp->spflags;
        this->freq_desert = sp->freq_desert;
index 6bdb5a1775c8b80a947b9f8dd259b9d1435c5287..21ee967c69080d2ecd7fccbf5b8260d825c60731 100644 (file)
@@ -64,6 +64,8 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge)
        this->heightmap = new s16[csize.X * csize.Z];
        this->ridge_heightmap = new s16[csize.X * csize.Z];
 
+       initHeightMap(this->heightmap, csize.X * csize.Z);
+
        MapgenV7Params *sp = (MapgenV7Params *)params->sparams;
        this->spflags = sp->spflags;