]> git.lizzy.rs Git - minetest.git/blobdiff - src/mapgen_valleys.cpp
Set acceleration only once in falling node
[minetest.git] / src / mapgen_valleys.cpp
index f003ae63c1d89cf8759847be2d9a356c98a8b74e..34a316abddbd9ef117634ce27b77d4d4a4bf7082 100644 (file)
@@ -56,8 +56,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 //Profiler *mapgen_profiler = &mapgen_prof;
 
 static FlagDesc flagdesc_mapgen_valleys[] = {
-       {"altitude_chill", MG_VALLEYS_ALT_CHILL},
-       {"humid_rivers",   MG_VALLEYS_HUMID_RIVERS},
+       {"altitude_chill", MGVALLEYS_ALT_CHILL},
+       {"humid_rivers",   MGVALLEYS_HUMID_RIVERS},
        {NULL,             0}
 };
 
@@ -86,8 +86,8 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *
        MapgenValleysParams *sp = (MapgenValleysParams *)params->sparams;
        this->spflags = sp->spflags;
 
-       this->humid_rivers       = (spflags & MG_VALLEYS_HUMID_RIVERS);
-       this->use_altitude_chill = (spflags & MG_VALLEYS_ALT_CHILL);
+       this->humid_rivers       = (spflags & MGVALLEYS_HUMID_RIVERS);
+       this->use_altitude_chill = (spflags & MGVALLEYS_ALT_CHILL);
 
        this->altitude_chill     = sp->altitude_chill;
        this->humidity_adjust    = params->np_biome_humidity.offset - 50.f;
@@ -99,8 +99,8 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *
        this->water_features_lim = rangelim(sp->water_features, 0, 10);
 
        // a small chance of overflows if the settings are very high
-       this->cave_water_max_height = water_level + MYMAX(0, water_features_lim - 6) * 50;
-       this->lava_max_height       = water_level + MYMAX(0, lava_features_lim - 6) * 50;
+       this->cave_water_max_height = water_level + MYMAX(0, water_features_lim - 4) * 50;
+       this->lava_max_height       = water_level + MYMAX(0, lava_features_lim - 4) * 50;
 
        tcave_cache = new float[csize.Y + 2];
 
@@ -181,7 +181,7 @@ MapgenValleys::~MapgenValleys()
 
 MapgenValleysParams::MapgenValleysParams()
 {
-       spflags = MG_VALLEYS_HUMID_RIVERS | MG_VALLEYS_ALT_CHILL;
+       spflags = MGVALLEYS_HUMID_RIVERS | MGVALLEYS_ALT_CHILL;
 
        altitude_chill     = 90; // The altitude at which temperature drops by 20C.
        large_cave_depth   = -33;
@@ -399,8 +399,21 @@ void MapgenValleys::calculateNoise()
 
        //mapgen_profiler->avg("noisemaps", tcn.stop() / 1000.f);
 
+       float heat_offset = 0.f;
+       float humidity_scale = 1.f;
+
+       // Altitude chill tends to reduce the average heat.
+       if (use_altitude_chill)
+               heat_offset = 5.f;
+
+       // River humidity tends to increase the humidity range.
+       if (humid_rivers) {
+               humidity_scale = 0.8f;
+       }
+
        for (s32 index = 0; index < csize.X * csize.Z; index++) {
-               noise_heat->result[index] += noise_heat_blend->result[index];
+               noise_heat->result[index] += noise_heat_blend->result[index] + heat_offset;
+               noise_humidity->result[index] *= humidity_scale;
                noise_humidity->result[index] += noise_humidity_blend->result[index];
        }
 
@@ -481,8 +494,10 @@ float MapgenValleys::terrainLevelFromNoise(TerrainNoise *tn)
                }
 
                // base - depth : height of the bottom of the river
-               // water_level - 6 : don't make rivers below 6 nodes under the surface
-               mount = rangelim(base - depth, (float) (water_level - 6), mount);
+               // water_level - 3 : don't make rivers below 3 nodes under the surface
+               // We use three because that's as low as the swamp biomes go.
+               // There is no logical equivalent to this using rangelim.
+               mount = MYMIN(MYMAX(base - depth, (float)(water_level - 3)), mount);
 
                // Slope has no influence on rivers.
                *tn->slope = 0.f;
@@ -502,7 +517,7 @@ float MapgenValleys::adjustedTerrainLevelFromNoise(TerrainNoise *tn)
        for (s16 y = y_start; y <= y_start + 1000; y++) {
                float fill = NoisePerlin3D(&noise_inter_valley_fill->np, tn->x, y, tn->z, seed);
 
-               if (fill * *tn->slope <= y - mount) {
+               if (fill * *tn->slope < y - mount) {
                        mount = MYMAX(y - 1, mount);
                        break;
                }
@@ -512,24 +527,19 @@ float MapgenValleys::adjustedTerrainLevelFromNoise(TerrainNoise *tn)
 }
 
 
-int MapgenValleys::getGroundLevelAtPoint(v2s16 p)
+int MapgenValleys::getSpawnLevelAtPoint(v2s16 p)
 {
-       // ***********************************
-       // This method (deliberately) does not
-       // return correct terrain values.
-       // ***********************************
-
-       // Since MT doesn't normally deal with rivers, check
-       // to make sure this isn't a request for a location
-       // in a river.
+       // Check to make sure this isn't a request for a location in a river.
        float rivers = NoisePerlin2D(&noise_rivers->np, p.X, p.Y, seed);
-
-       // If it's wet, return an unusable number.
        if (fabs(rivers) < river_size_factor)
-               return MAX_MAP_GENERATION_LIMIT;
-
-       // Otherwise, return the real result.
-       return terrainLevelAtPoint(p.X, p.Y);
+               return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
+
+       s16 level_at_point = terrainLevelAtPoint(p.X, p.Y);
+       if (level_at_point <= water_level ||
+                       level_at_point > water_level + 32)
+               return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
+       else
+               return level_at_point;
 }
 
 
@@ -556,6 +566,15 @@ float MapgenValleys::terrainLevelAtPoint(s16 x, s16 z)
 
 int MapgenValleys::generateTerrain()
 {
+       // Raising this reduces the rate of evaporation.
+       static const float evaporation = 300.f;
+       // from the lua
+       static const float humidity_dropoff = 4.f;
+       // constant to convert altitude chill (compatible with lua) to heat
+       static const float alt_to_heat = 20.f;
+       // humidity reduction by altitude
+       static const float alt_to_humid = 10.f;
+
        MapNode n_air(CONTENT_AIR);
        MapNode n_river_water(c_river_water_source);
        MapNode n_sand(c_sand);
@@ -568,42 +587,56 @@ int MapgenValleys::generateTerrain()
 
        for (s16 z = node_min.Z; z <= node_max.Z; z++)
        for (s16 x = node_min.X; x <= node_max.X; x++, index_2d++) {
-               s16 river_y = floor(noise_rivers->result[index_2d]);
-               s16 surface_y = floor(noise_terrain_height->result[index_2d]);
+               float river_y = noise_rivers->result[index_2d];
+               float surface_y = noise_terrain_height->result[index_2d];
                float slope = noise_inter_valley_slope->result[index_2d];
+               float t_heat = noise_heat->result[index_2d];
 
-               heightmap[index_2d] = surface_y;
+               heightmap[index_2d] = -MAX_MAP_GENERATION_LIMIT;
 
                if (surface_y > surface_max_y)
-                       surface_max_y = surface_y;
+                       surface_max_y = ceil(surface_y);
+
+               if (humid_rivers) {
+                       // Derive heat from (base) altitude. This will be most correct
+                       // at rivers, since other surface heights may vary below.
+                       if (use_altitude_chill && (surface_y > 0.f || river_y > 0.f))
+                               t_heat -= alt_to_heat * MYMAX(surface_y, river_y) / altitude_chill;
+
+                       // If humidity is low or heat is high, lower the water table.
+                       float delta = noise_humidity->result[index_2d] - 50.f;
+                       if (delta < 0.f) {
+                               float t_evap = (t_heat - 32.f) / evaporation;
+                               river_y += delta * MYMAX(t_evap, 0.08f);
+                       }
+               }
 
                u32 index_3d = (z - node_min.Z) * zstride + (x - node_min.X);
                u32 index_data = vm->m_area.index(x, node_min.Y - 1, z);
 
                // Mapgens concern themselves with stone and water.
                for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
-                       float fill = 0.f;
-                       fill = noise_inter_valley_fill->result[index_3d];
-
                        if (vm->m_data[index_data].getContent() == CONTENT_IGNORE) {
-                               bool river = (river_y > surface_y);
+                               float fill = noise_inter_valley_fill->result[index_3d];
+                               float surface_delta = (float)y - surface_y;
+                               bool river = y + 1 < river_y;
 
-                               if (river && y == surface_y) {
+                               if (fabs(surface_delta) <= 0.5f && y > water_level && river) {
                                        // river bottom
                                        vm->m_data[index_data] = n_sand;
-                               } else if (river && y <= surface_y) {
+                               } else if (slope * fill > surface_delta) {
                                        // ground
                                        vm->m_data[index_data] = n_stone;
-                               } else if (river && y < river_y) {
-                                       // river
-                                       vm->m_data[index_data] = n_river_water;
-                               } else if ((!river) && myround(fill * slope) >= y - surface_y) {
-                                       // ground
-                                       vm->m_data[index_data] = n_stone;
-                                       heightmap[index_2d] = surface_max_y = y;
+                                       if (y > heightmap[index_2d])
+                                               heightmap[index_2d] = y;
+                                       if (y > surface_max_y)
+                                               surface_max_y = y;
                                } else if (y <= water_level) {
                                        // sea
                                        vm->m_data[index_data] = n_water;
+                               } else if (river) {
+                                       // river
+                                       vm->m_data[index_data] = n_river_water;
                                } else {
                                        vm->m_data[index_data] = n_air;
                                }
@@ -613,18 +646,51 @@ int MapgenValleys::generateTerrain()
                        index_3d += ystride;
                }
 
-               // Although the original valleys adjusts humidity by distance
-               // from seawater, this causes problems with the default biomes.
-               // Adjust only by freshwater proximity.
-               const float humidity_offset = 0.8f;  // derived by testing
-               if (humid_rivers)
-                       noise_humidity->result[index_2d] *= (1 + pow(0.5f, MYMAX((surface_max_y
-                                       - noise_rivers->result[index_2d]) / 3.f, 0.f))) * humidity_offset;
-
-               // Assign the heat adjusted by altitude.
-               if (use_altitude_chill && surface_max_y > 0)
-                       noise_heat->result[index_2d] *=
-                               pow(0.5f, (surface_max_y - altitude_chill / 3.f) / altitude_chill);
+               // This happens if we're generating a chunk that doesn't
+               // contain the terrain surface, in which case, we need
+               // to set heightmap to a value outside of the chunk,
+               // to avoid confusing lua mods that use heightmap.
+               if (heightmap[index_2d] == -MAX_MAP_GENERATION_LIMIT) {
+                       s16 surface_y_int = myround(surface_y);
+                       if (surface_y_int > node_max.Y + 1 || surface_y_int < node_min.Y - 1) {
+                               // If surface_y is outside the chunk, it's good enough.
+                               heightmap[index_2d] = surface_y_int;
+                       } else {
+                               // If the ground is outside of this chunk, but surface_y
+                               // is within the chunk, give a value outside.
+                               heightmap[index_2d] = node_min.Y - 2;
+                       }
+               }
+
+               if (humid_rivers) {
+                       // Use base ground (water table) in a riverbed, to
+                       // avoid an unnatural rise in humidity.
+                       float t_alt = MYMAX(noise_rivers->result[index_2d], (float)heightmap[index_2d]);
+                       float humid = noise_humidity->result[index_2d];
+                       float water_depth = (t_alt - river_y) / humidity_dropoff;
+                       humid *= 1.f + pow(0.5f, MYMAX(water_depth, 1.f));
+
+                       // Reduce humidity with altitude (ignoring riverbeds).
+                       // This is similar to the lua version's seawater adjustment,
+                       // but doesn't increase the base humidity, which causes
+                       // problems with the default biomes.
+                       if (t_alt > 0.f)
+                               humid -= alt_to_humid * t_alt / altitude_chill;
+
+                       noise_humidity->result[index_2d] = humid;
+               }
+
+               // Assign the heat adjusted by any changed altitudes.
+               // The altitude will change about half the time.
+               if (use_altitude_chill) {
+                       // ground height ignoring riverbeds
+                       float t_alt = MYMAX(noise_rivers->result[index_2d], (float)heightmap[index_2d]);
+                       if (humid_rivers && heightmap[index_2d] == (s16)myround(surface_y))
+                               // The altitude hasn't changed. Use the first result.
+                               noise_heat->result[index_2d] = t_heat;
+                       else if (t_alt > 0.f)
+                               noise_heat->result[index_2d] -= alt_to_heat * t_alt / altitude_chill;
+               }
        }
 
        return surface_max_y;
@@ -649,7 +715,7 @@ MgStoneType MapgenValleys::generateBiomes(float *heat_map, float *humidity_map)
                // 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);
+               bool water_above = (c_above == c_water_source || c_above == c_river_water_source);
 
                // 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.
@@ -718,7 +784,7 @@ MgStoneType MapgenValleys::generateBiomes(float *heat_map, float *humidity_map)
                                water_above = true;
                        } else if (c == c_river_water_source) {
                                vm->m_data[vi] = MapNode(biome->c_river_water);
-                               nplaced = U16_MAX;  // Sand was already placed under rivers.
+                               nplaced = depth_top;  // Enable filler placement for next surface
                                air_above = false;
                                water_above = true;
                        } else if (c == CONTENT_AIR) {
@@ -846,8 +912,8 @@ void MapgenValleys::generateCaves(s16 max_stone_y)
 
        // Reduce the odds of overflows even further.
        if (node_max.Y > water_level) {
-               lava_chance /= 5;
-               water_chance /= 5;
+               lava_chance /= 3;
+               water_chance /= 3;
        }
 
        u32 index_2d = 0;
@@ -865,6 +931,12 @@ void MapgenValleys::generateCaves(s16 max_stone_y)
                for (s16 y = node_max.Y + 1;
                                y >= node_min.Y - 1;
                                y--, index_3d -= ystride, vm->m_area.add_y(em, index_data, -1)) {
+                       // Don't excavate the overgenerated stone at node_max.Y + 1,
+                       // this creates a 'roof' over the tunnel, preventing light in
+                       // tunnels at mapchunk borders when generating mapchunks upwards.
+                       if (y > node_max.Y)
+                               continue;
+
                        float terrain = noise_terrain_height->result[index_2d];
 
                        // Saves some time.