Mgv5: Make spawn position search more reliable
authorparamat <paramat@users.noreply.github.com>
Wed, 9 Aug 2017 13:23:30 +0000 (14:23 +0100)
committerrubenwardy <rubenwardy@gmail.com>
Sat, 12 Aug 2017 18:37:53 +0000 (19:37 +0100)
src/mapgen_v5.cpp

index faca0e16ad93d1f5ddec0b36b383deedfa5a5efa..2cfb065f999638f28c5c8aedf6c75362090f9020 100644 (file)
@@ -134,7 +134,6 @@ void MapgenV5Params::writeParams(Settings *settings) const
 
 int MapgenV5::getSpawnLevelAtPoint(v2s16 p)
 {
-       //TimeTaker t("getGroundLevelAtPoint", NULL, PRECISION_MICRO);
 
        float f = 0.55 + NoisePerlin2D(&noise_factor->np, p.X, p.Y, seed);
        if (f < 0.01)
@@ -143,25 +142,27 @@ int MapgenV5::getSpawnLevelAtPoint(v2s16 p)
                f *= 1.6;
        float h = NoisePerlin2D(&noise_height->np, p.X, p.Y, seed);
 
-       for (s16 y = 128; y >= -128; y--) {
+       // noise_height 'offset' is the average level of terrain. At least 50% of
+       // terrain will be below this.
+       // Raising the maximum spawn level above 'water_level + 16' is necessary
+       // for when noise_height 'offset' is set much higher than water_level.
+       s16 max_spawn_y = MYMAX(noise_height->np.offset, water_level + 16);
+
+       // Starting spawn search at max_spawn_y + 128 ensures 128 nodes of open
+       // space above spawn position. Avoids spawning in possibly sealed voids.
+       for (s16 y = max_spawn_y + 128; y >= water_level; y--) {
                float n_ground = NoisePerlin3D(&noise_ground->np, p.X, y, p.Y, seed);
 
                if (n_ground * f > y - h) {  // If solid
-                       // If either top 2 nodes of search are solid this is inside a
-                       // mountain or floatland with possibly no space for the player to spawn.
-                       if (y >= 127) {
+                       if (y < water_level || y > max_spawn_y)
                                return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
-                       } else {  // Ground below at least 2 nodes of empty space
-                               if (y <= water_level || y > water_level + 16)
-                                       return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
-                               else
-                                       return y;
-                       }
+                       else
+                               // y + 2 because y is surface and due to biome 'dust' nodes.
+                               return y + 2;
                }
        }
-
-       //printf("getGroundLevelAtPoint: %dus\n", t.stop());
-       return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn position, no ground found
+       // Unsuitable spawn position, no ground found
+       return MAX_MAP_GENERATION_LIMIT;
 }