]> git.lizzy.rs Git - minetest.git/blob - src/mapgen_valleys.cpp
[CSM] Add flavour limits controlled by server (#5930)
[minetest.git] / src / mapgen_valleys.cpp
1 /*
2 Minetest Valleys C
3 Copyright (C) 2016-2017 Duane Robertson <duane@duanerobertson.com>
4 Copyright (C) 2016-2017 paramat
5
6 Based on Valleys Mapgen by Gael de Sailly
7  (https://forum.minetest.net/viewtopic.php?f=9&t=11430)
8 and mapgen_v7, mapgen_flat by kwolekr and paramat.
9
10 Licensing changed by permission of Gael de Sailly.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 2.1 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU Lesser General Public License for more details.
21
22 You should have received a copy of the GNU Lesser General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27 #include "mapgen.h"
28 #include "voxel.h"
29 #include "noise.h"
30 #include "mapblock.h"
31 #include "mapnode.h"
32 #include "map.h"
33 #include "content_sao.h"
34 #include "nodedef.h"
35 #include "voxelalgorithms.h"
36 #include "settings.h" // For g_settings
37 #include "emerge.h"
38 #include "dungeongen.h"
39 #include "mg_biome.h"
40 #include "mg_ore.h"
41 #include "mg_decoration.h"
42 #include "mapgen_valleys.h"
43 #include "cavegen.h"
44
45
46 //#undef NDEBUG
47 //#include "assert.h"
48
49 //#include "util/timetaker.h"
50 //#include "profiler.h"
51
52
53 //static Profiler mapgen_prof;
54 //Profiler *mapgen_profiler = &mapgen_prof;
55
56 static FlagDesc flagdesc_mapgen_valleys[] = {
57         {"altitude_chill", MGVALLEYS_ALT_CHILL},
58         {"humid_rivers",   MGVALLEYS_HUMID_RIVERS},
59         {NULL,             0}
60 };
61
62 ///////////////////////////////////////////////////////////////////////////////
63
64
65 MapgenValleys::MapgenValleys(int mapgenid, MapgenValleysParams *params, EmergeManager *emerge)
66         : MapgenBasic(mapgenid, params, emerge)
67 {
68         // NOTE: MapgenValleys has a hard dependency on BiomeGenOriginal
69         m_bgen = (BiomeGenOriginal *)biomegen;
70
71         BiomeParamsOriginal *bp = (BiomeParamsOriginal *)params->bparams;
72
73         spflags            = params->spflags;
74         altitude_chill     = params->altitude_chill;
75         large_cave_depth   = params->large_cave_depth;
76         lava_features_lim  = rangelim(params->lava_features, 0, 10);
77         massive_cave_depth = params->massive_cave_depth;
78         river_depth_bed    = params->river_depth + 1.f;
79         river_size_factor  = params->river_size / 100.f;
80         water_features_lim = rangelim(params->water_features, 0, 10);
81         cave_width         = params->cave_width;
82
83         //// 2D Terrain noise
84         noise_filler_depth       = new Noise(&params->np_filler_depth,       seed, csize.X, csize.Z);
85         noise_inter_valley_slope = new Noise(&params->np_inter_valley_slope, seed, csize.X, csize.Z);
86         noise_rivers             = new Noise(&params->np_rivers,             seed, csize.X, csize.Z);
87         noise_terrain_height     = new Noise(&params->np_terrain_height,     seed, csize.X, csize.Z);
88         noise_valley_depth       = new Noise(&params->np_valley_depth,       seed, csize.X, csize.Z);
89         noise_valley_profile     = new Noise(&params->np_valley_profile,     seed, csize.X, csize.Z);
90
91         //// 3D Terrain noise
92         // 1-up 1-down overgeneration
93         noise_inter_valley_fill = new Noise(&params->np_inter_valley_fill, seed, csize.X, csize.Y + 2, csize.Z);
94         // 1-down overgeneraion
95         noise_cave1             = new Noise(&params->np_cave1,             seed, csize.X, csize.Y + 1, csize.Z);
96         noise_cave2             = new Noise(&params->np_cave2,             seed, csize.X, csize.Y + 1, csize.Z);
97         noise_massive_caves     = new Noise(&params->np_massive_caves,     seed, csize.X, csize.Y + 1, csize.Z);
98
99         humid_rivers       = (spflags & MGVALLEYS_HUMID_RIVERS);
100         use_altitude_chill = (spflags & MGVALLEYS_ALT_CHILL);
101         humidity_adjust    = bp->np_humidity.offset - 50.f;
102
103         // a small chance of overflows if the settings are very high
104         cave_water_max_height = water_level + MYMAX(0, water_features_lim - 4) * 50;
105         lava_max_height       = water_level + MYMAX(0, lava_features_lim - 4) * 50;
106
107         tcave_cache = new float[csize.Y + 2];
108 }
109
110
111 MapgenValleys::~MapgenValleys()
112 {
113         delete noise_cave1;
114         delete noise_cave2;
115         delete noise_filler_depth;
116         delete noise_inter_valley_fill;
117         delete noise_inter_valley_slope;
118         delete noise_rivers;
119         delete noise_massive_caves;
120         delete noise_terrain_height;
121         delete noise_valley_depth;
122         delete noise_valley_profile;
123
124         delete[] tcave_cache;
125 }
126
127
128 MapgenValleysParams::MapgenValleysParams()
129 {
130         np_cave1              = NoiseParams(0,     12,   v3f(61,   61,   61),   52534, 3, 0.5,   2.0);
131         np_cave2              = NoiseParams(0,     12,   v3f(67,   67,   67),   10325, 3, 0.5,   2.0);
132         np_filler_depth       = NoiseParams(0.f,   1.2f, v3f(256,  256,  256),  1605,  3, 0.5f,  2.f);
133         np_inter_valley_fill  = NoiseParams(0.f,   1.f,  v3f(256,  512,  256),  1993,  6, 0.8f,  2.f);
134         np_inter_valley_slope = NoiseParams(0.5f,  0.5f, v3f(128,  128,  128),  746,   1, 1.f,   2.f);
135         np_rivers             = NoiseParams(0.f,   1.f,  v3f(256,  256,  256),  -6050, 5, 0.6f,  2.f);
136         np_massive_caves      = NoiseParams(0.f,   1.f,  v3f(768,  256,  768),  59033, 6, 0.63f, 2.f);
137         np_terrain_height     = NoiseParams(-10.f, 50.f, v3f(1024, 1024, 1024), 5202,  6, 0.4f,  2.f);
138         np_valley_depth       = NoiseParams(5.f,   4.f,  v3f(512,  512,  512),  -1914, 1, 1.f,   2.f);
139         np_valley_profile     = NoiseParams(0.6f,  0.5f, v3f(512,  512,  512),  777,   1, 1.f,   2.f);
140 }
141
142
143 void MapgenValleysParams::readParams(const Settings *settings)
144 {
145         settings->getFlagStrNoEx("mgvalleys_spflags",        spflags, flagdesc_mapgen_valleys);
146         settings->getU16NoEx("mgvalleys_altitude_chill",     altitude_chill);
147         settings->getS16NoEx("mgvalleys_large_cave_depth",   large_cave_depth);
148         settings->getU16NoEx("mgvalleys_lava_features",      lava_features);
149         settings->getS16NoEx("mgvalleys_massive_cave_depth", massive_cave_depth);
150         settings->getU16NoEx("mgvalleys_river_depth",        river_depth);
151         settings->getU16NoEx("mgvalleys_river_size",         river_size);
152         settings->getU16NoEx("mgvalleys_water_features",     water_features);
153         settings->getFloatNoEx("mgvalleys_cave_width",       cave_width);
154
155         settings->getNoiseParams("mgvalleys_np_cave1",              np_cave1);
156         settings->getNoiseParams("mgvalleys_np_cave2",              np_cave2);
157         settings->getNoiseParams("mgvalleys_np_filler_depth",       np_filler_depth);
158         settings->getNoiseParams("mgvalleys_np_inter_valley_fill",  np_inter_valley_fill);
159         settings->getNoiseParams("mgvalleys_np_inter_valley_slope", np_inter_valley_slope);
160         settings->getNoiseParams("mgvalleys_np_rivers",             np_rivers);
161         settings->getNoiseParams("mgvalleys_np_massive_caves",      np_massive_caves);
162         settings->getNoiseParams("mgvalleys_np_terrain_height",     np_terrain_height);
163         settings->getNoiseParams("mgvalleys_np_valley_depth",       np_valley_depth);
164         settings->getNoiseParams("mgvalleys_np_valley_profile",     np_valley_profile);
165 }
166
167
168 void MapgenValleysParams::writeParams(Settings *settings) const
169 {
170         settings->setFlagStr("mgvalleys_spflags",        spflags, flagdesc_mapgen_valleys, U32_MAX);
171         settings->setU16("mgvalleys_altitude_chill",     altitude_chill);
172         settings->setS16("mgvalleys_large_cave_depth",   large_cave_depth);
173         settings->setU16("mgvalleys_lava_features",      lava_features);
174         settings->setS16("mgvalleys_massive_cave_depth", massive_cave_depth);
175         settings->setU16("mgvalleys_river_depth",        river_depth);
176         settings->setU16("mgvalleys_river_size",         river_size);
177         settings->setU16("mgvalleys_water_features",     water_features);
178         settings->setFloat("mgvalleys_cave_width",       cave_width);
179
180         settings->setNoiseParams("mgvalleys_np_cave1",              np_cave1);
181         settings->setNoiseParams("mgvalleys_np_cave2",              np_cave2);
182         settings->setNoiseParams("mgvalleys_np_filler_depth",       np_filler_depth);
183         settings->setNoiseParams("mgvalleys_np_inter_valley_fill",  np_inter_valley_fill);
184         settings->setNoiseParams("mgvalleys_np_inter_valley_slope", np_inter_valley_slope);
185         settings->setNoiseParams("mgvalleys_np_rivers",             np_rivers);
186         settings->setNoiseParams("mgvalleys_np_massive_caves",      np_massive_caves);
187         settings->setNoiseParams("mgvalleys_np_terrain_height",     np_terrain_height);
188         settings->setNoiseParams("mgvalleys_np_valley_depth",       np_valley_depth);
189         settings->setNoiseParams("mgvalleys_np_valley_profile",     np_valley_profile);
190 }
191
192
193 ///////////////////////////////////////
194
195
196 void MapgenValleys::makeChunk(BlockMakeData *data)
197 {
198         // Pre-conditions
199         assert(data->vmanip);
200         assert(data->nodedef);
201         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
202                 data->blockpos_requested.Y >= data->blockpos_min.Y &&
203                 data->blockpos_requested.Z >= data->blockpos_min.Z);
204         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
205                 data->blockpos_requested.Y <= data->blockpos_max.Y &&
206                 data->blockpos_requested.Z <= data->blockpos_max.Z);
207
208         this->generating = true;
209         this->vm = data->vmanip;
210         this->ndef = data->nodedef;
211
212         //TimeTaker t("makeChunk");
213
214         v3s16 blockpos_min = data->blockpos_min;
215         v3s16 blockpos_max = data->blockpos_max;
216         node_min = blockpos_min * MAP_BLOCKSIZE;
217         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
218         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
219         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
220
221         blockseed = getBlockSeed2(full_node_min, seed);
222
223         // Generate biome noises.  Note this must be executed strictly before
224         // generateTerrain, because generateTerrain depends on intermediate
225         // biome-related noises.
226         m_bgen->calcBiomeNoise(node_min);
227
228         // Generate noise maps and base terrain height.
229         // Modify heat and humidity maps.
230         calculateNoise();
231
232         // Generate base terrain with initial heightmaps
233         s16 stone_surface_max_y = generateTerrain();
234
235         // Recalculate heightmap
236         updateHeightmap(node_min, node_max);
237
238         // Place biome-specific nodes and build biomemap
239         MgStoneType stone_type = generateBiomes(water_level - 1);
240
241         // Cave creation.
242         if (flags & MG_CAVES)
243                 generateCaves(stone_surface_max_y, large_cave_depth);
244
245         // Dungeon creation
246         if ((flags & MG_DUNGEONS) && node_max.Y < 50)
247                 generateDungeons(stone_surface_max_y, stone_type);
248
249         // Generate the registered decorations
250         if (flags & MG_DECORATIONS)
251                 m_emerge->decomgr->placeAllDecos(this, blockseed,
252                         node_min, node_max, water_level - 1);
253
254         // Generate the registered ores
255         m_emerge->oremgr->placeAllOres(this, blockseed,
256                 node_min, node_max, water_level - 1);
257
258         // Sprinkle some dust on top after everything else was generated
259         dustTopNodes();
260
261         //TimeTaker tll("liquid_lighting");
262
263         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
264
265         if (flags & MG_LIGHT)
266                 calcLighting(
267                                 node_min - v3s16(0, 1, 0),
268                                 node_max + v3s16(0, 1, 0),
269                                 full_node_min,
270                                 full_node_max);
271
272         //mapgen_profiler->avg("liquid_lighting", tll.stop() / 1000.f);
273         //mapgen_profiler->avg("makeChunk", t.stop() / 1000.f);
274
275         this->generating = false;
276 }
277
278
279 // Populate the noise tables and do most of the
280 // calculation necessary to determine terrain height.
281 void MapgenValleys::calculateNoise()
282 {
283         //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
284
285         int x = node_min.X;
286         int y = node_min.Y - 1;
287         int z = node_min.Z;
288
289         //TimeTaker tcn("actualNoise");
290
291         noise_inter_valley_slope->perlinMap2D(x, z);
292         noise_rivers->perlinMap2D(x, z);
293         noise_terrain_height->perlinMap2D(x, z);
294         noise_valley_depth->perlinMap2D(x, z);
295         noise_valley_profile->perlinMap2D(x, z);
296
297         noise_inter_valley_fill->perlinMap3D(x, y, z);
298
299         //mapgen_profiler->avg("noisemaps", tcn.stop() / 1000.f);
300
301         float heat_offset = 0.f;
302         float humidity_scale = 1.f;
303
304         // Altitude chill tends to reduce the average heat.
305         if (use_altitude_chill)
306                 heat_offset = 5.f;
307
308         // River humidity tends to increase the humidity range.
309         if (humid_rivers) {
310                 humidity_scale = 0.8f;
311         }
312
313         for (s32 index = 0; index < csize.X * csize.Z; index++) {
314                 m_bgen->heatmap[index] += heat_offset;
315                 m_bgen->humidmap[index] *= humidity_scale;
316         }
317
318         TerrainNoise tn;
319
320         u32 index = 0;
321         for (tn.z = node_min.Z; tn.z <= node_max.Z; tn.z++)
322         for (tn.x = node_min.X; tn.x <= node_max.X; tn.x++, index++) {
323                 // The parameters that we actually need to generate terrain
324                 //  are passed by address (and the return value).
325                 tn.terrain_height    = noise_terrain_height->result[index];
326                 // River noise is replaced with base terrain, which
327                 // is basically the height of the water table.
328                 tn.rivers            = &noise_rivers->result[index];
329                 // Valley depth noise is replaced with the valley
330                 // number that represents the height of terrain
331                 // over rivers and is used to determine about
332                 // how close a river is for humidity calculation.
333                 tn.valley            = &noise_valley_depth->result[index];
334                 tn.valley_profile    = noise_valley_profile->result[index];
335                 // Slope noise is replaced by the calculated slope
336                 // which is used to get terrain height in the slow
337                 // method, to create sharper mountains.
338                 tn.slope             = &noise_inter_valley_slope->result[index];
339                 tn.inter_valley_fill = noise_inter_valley_fill->result[index];
340
341                 // This is the actual terrain height.
342                 float mount = terrainLevelFromNoise(&tn);
343                 noise_terrain_height->result[index] = mount;
344         }
345 }
346
347
348 // This keeps us from having to maintain two similar sets of
349 //  complicated code to determine ground level.
350 float MapgenValleys::terrainLevelFromNoise(TerrainNoise *tn)
351 {
352         // The square function changes the behaviour of this noise:
353         //  very often small, and sometimes very high.
354         float valley_d = MYSQUARE(*tn->valley);
355
356         // valley_d is here because terrain is generally higher where valleys
357         //  are deep (mountains). base represents the height of the
358         //  rivers, most of the surface is above.
359         float base = tn->terrain_height + valley_d;
360
361         // "river" represents the distance from the river, in arbitrary units.
362         float river = fabs(*tn->rivers) - river_size_factor;
363
364         // Use the curve of the function 1-exp(-(x/a)^2) to model valleys.
365         //  Making "a" vary (0 < a <= 1) changes the shape of the valleys.
366         //  Try it with a geometry software !
367         //   (here x = "river" and a = valley_profile).
368         //  "valley" represents the height of the terrain, from the rivers.
369         {
370                 float t = river / tn->valley_profile;
371                 *tn->valley = valley_d * (1.f - exp(- MYSQUARE(t)));
372         }
373
374         // approximate height of the terrain at this point
375         float mount = base + *tn->valley;
376
377         *tn->slope *= *tn->valley;
378
379         // Rivers are placed where "river" is negative, so where the original
380         //  noise value is close to zero.
381         // Base ground is returned as rivers since it's basically the water table.
382         *tn->rivers = base;
383         if (river < 0.f) {
384                 // Use the the function -sqrt(1-x^2) which models a circle.
385                 float depth;
386                 {
387                         float t = river / river_size_factor + 1;
388                         depth = (river_depth_bed * sqrt(MYMAX(0, 1.f - MYSQUARE(t))));
389                 }
390
391                 // base - depth : height of the bottom of the river
392                 // water_level - 3 : don't make rivers below 3 nodes under the surface
393                 // We use three because that's as low as the swamp biomes go.
394                 // There is no logical equivalent to this using rangelim.
395                 mount = MYMIN(MYMAX(base - depth, (float)(water_level - 3)), mount);
396
397                 // Slope has no influence on rivers.
398                 *tn->slope = 0.f;
399         }
400
401         return mount;
402 }
403
404
405 // This avoids duplicating the code in terrainLevelFromNoise, adding
406 // only the final step of terrain generation without a noise map.
407 float MapgenValleys::adjustedTerrainLevelFromNoise(TerrainNoise *tn)
408 {
409         float mount = terrainLevelFromNoise(tn);
410         s16 y_start = myround(mount);
411
412         for (s16 y = y_start; y <= y_start + 1000; y++) {
413                 float fill = NoisePerlin3D(&noise_inter_valley_fill->np, tn->x, y, tn->z, seed);
414
415                 if (fill * *tn->slope < y - mount) {
416                         mount = MYMAX(y - 1, mount);
417                         break;
418                 }
419         }
420
421         return mount;
422 }
423
424
425 int MapgenValleys::getSpawnLevelAtPoint(v2s16 p)
426 {
427         // Check to make sure this isn't a request for a location in a river.
428         float rivers = NoisePerlin2D(&noise_rivers->np, p.X, p.Y, seed);
429         if (fabs(rivers) < river_size_factor)
430                 return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
431
432         s16 level_at_point = terrainLevelAtPoint(p.X, p.Y);
433         if (level_at_point <= water_level ||
434                         level_at_point > water_level + 32)
435                 return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
436         else
437                 return level_at_point;
438 }
439
440
441 float MapgenValleys::terrainLevelAtPoint(s16 x, s16 z)
442 {
443         TerrainNoise tn;
444
445         float rivers = NoisePerlin2D(&noise_rivers->np, x, z, seed);
446         float valley = NoisePerlin2D(&noise_valley_depth->np, x, z, seed);
447         float inter_valley_slope = NoisePerlin2D(&noise_inter_valley_slope->np, x, z, seed);
448
449         tn.x                 = x;
450         tn.z                 = z;
451         tn.terrain_height    = NoisePerlin2D(&noise_terrain_height->np, x, z, seed);
452         tn.rivers            = &rivers;
453         tn.valley            = &valley;
454         tn.valley_profile    = NoisePerlin2D(&noise_valley_profile->np, x, z, seed);
455         tn.slope             = &inter_valley_slope;
456         tn.inter_valley_fill = 0.f;
457
458         return adjustedTerrainLevelFromNoise(&tn);
459 }
460
461
462 int MapgenValleys::generateTerrain()
463 {
464         // Raising this reduces the rate of evaporation.
465         static const float evaporation = 300.f;
466         // from the lua
467         static const float humidity_dropoff = 4.f;
468         // constant to convert altitude chill (compatible with lua) to heat
469         static const float alt_to_heat = 20.f;
470         // humidity reduction by altitude
471         static const float alt_to_humid = 10.f;
472
473         MapNode n_air(CONTENT_AIR);
474         MapNode n_river_water(c_river_water_source);
475         MapNode n_stone(c_stone);
476         MapNode n_water(c_water_source);
477
478         v3s16 em = vm->m_area.getExtent();
479         s16 surface_max_y = -MAX_MAP_GENERATION_LIMIT;
480         u32 index_2d = 0;
481
482         for (s16 z = node_min.Z; z <= node_max.Z; z++)
483         for (s16 x = node_min.X; x <= node_max.X; x++, index_2d++) {
484                 float river_y = noise_rivers->result[index_2d];
485                 float surface_y = noise_terrain_height->result[index_2d];
486                 float slope = noise_inter_valley_slope->result[index_2d];
487                 float t_heat = m_bgen->heatmap[index_2d];
488
489                 heightmap[index_2d] = -MAX_MAP_GENERATION_LIMIT;
490
491                 if (surface_y > surface_max_y)
492                         surface_max_y = ceil(surface_y);
493
494                 if (humid_rivers) {
495                         // Derive heat from (base) altitude. This will be most correct
496                         // at rivers, since other surface heights may vary below.
497                         if (use_altitude_chill && (surface_y > 0.f || river_y > 0.f))
498                                 t_heat -= alt_to_heat * MYMAX(surface_y, river_y) / altitude_chill;
499
500                         // If humidity is low or heat is high, lower the water table.
501                         float delta = m_bgen->humidmap[index_2d] - 50.f;
502                         if (delta < 0.f) {
503                                 float t_evap = (t_heat - 32.f) / evaporation;
504                                 river_y += delta * MYMAX(t_evap, 0.08f);
505                         }
506                 }
507
508                 u32 index_3d = (z - node_min.Z) * zstride_1u1d + (x - node_min.X);
509                 u32 index_data = vm->m_area.index(x, node_min.Y - 1, z);
510
511                 // Mapgens concern themselves with stone and water.
512                 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
513                         if (vm->m_data[index_data].getContent() == CONTENT_IGNORE) {
514                                 float fill = noise_inter_valley_fill->result[index_3d];
515                                 float surface_delta = (float)y - surface_y;
516                                 bool river = y + 1 < river_y;
517
518                                 if (slope * fill > surface_delta) {
519                                         // ground
520                                         vm->m_data[index_data] = n_stone;
521                                         if (y > heightmap[index_2d])
522                                                 heightmap[index_2d] = y;
523                                         if (y > surface_max_y)
524                                                 surface_max_y = y;
525                                 } else if (y <= water_level) {
526                                         // sea
527                                         vm->m_data[index_data] = n_water;
528                                 } else if (river) {
529                                         // river
530                                         vm->m_data[index_data] = n_river_water;
531                                 } else {  // air
532                                         vm->m_data[index_data] = n_air;
533                                 }
534                         }
535
536                         vm->m_area.add_y(em, index_data, 1);
537                         index_3d += ystride;
538                 }
539
540                 if (heightmap[index_2d] == -MAX_MAP_GENERATION_LIMIT) {
541                         s16 surface_y_int = myround(surface_y);
542                         if (surface_y_int > node_max.Y + 1 || surface_y_int < node_min.Y - 1) {
543                                 // If surface_y is outside the chunk, it's good enough.
544                                 heightmap[index_2d] = surface_y_int;
545                         } else {
546                                 // If the ground is outside of this chunk, but surface_y
547                                 // is within the chunk, give a value outside.
548                                 heightmap[index_2d] = node_min.Y - 2;
549                         }
550                 }
551
552                 if (humid_rivers) {
553                         // Use base ground (water table) in a riverbed, to
554                         // avoid an unnatural rise in humidity.
555                         float t_alt = MYMAX(noise_rivers->result[index_2d], (float)heightmap[index_2d]);
556                         float humid = m_bgen->humidmap[index_2d];
557                         float water_depth = (t_alt - river_y) / humidity_dropoff;
558                         humid *= 1.f + pow(0.5f, MYMAX(water_depth, 1.f));
559
560                         // Reduce humidity with altitude (ignoring riverbeds).
561                         // This is similar to the lua version's seawater adjustment,
562                         // but doesn't increase the base humidity, which causes
563                         // problems with the default biomes.
564                         if (t_alt > 0.f)
565                                 humid -= alt_to_humid * t_alt / altitude_chill;
566
567                         m_bgen->humidmap[index_2d] = humid;
568                 }
569
570                 // Assign the heat adjusted by any changed altitudes.
571                 // The altitude will change about half the time.
572                 if (use_altitude_chill) {
573                         // ground height ignoring riverbeds
574                         float t_alt = MYMAX(noise_rivers->result[index_2d], (float)heightmap[index_2d]);
575                         if (humid_rivers && heightmap[index_2d] == (s16)myround(surface_y))
576                                 // The altitude hasn't changed. Use the first result.
577                                 m_bgen->heatmap[index_2d] = t_heat;
578                         else if (t_alt > 0.f)
579                                 m_bgen->heatmap[index_2d] -= alt_to_heat * t_alt / altitude_chill;
580                 }
581         }
582
583         return surface_max_y;
584 }
585
586 void MapgenValleys::generateCaves(s16 max_stone_y, s16 large_cave_depth)
587 {
588         if (max_stone_y < node_min.Y)
589                 return;
590
591         noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
592         noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
593
594         PseudoRandom ps(blockseed + 72202);
595
596         MapNode n_air(CONTENT_AIR);
597         MapNode n_lava(c_lava_source);
598         MapNode n_water(c_river_water_source);
599
600         v3s16 em = vm->m_area.getExtent();
601
602         // Cave blend distance near YMIN, YMAX
603         const float massive_cave_blend = 128.f;
604         // noise threshold for massive caves
605         const float massive_cave_threshold = 0.6f;
606         // mct: 1 = small rare caves, 0.5 1/3rd ground volume, 0 = 1/2 ground volume.
607
608         float yblmin = -mapgen_limit + massive_cave_blend * 1.5f;
609         float yblmax = massive_cave_depth - massive_cave_blend * 1.5f;
610         bool made_a_big_one = false;
611
612         // Cache the tcave values as they only vary by altitude.
613         if (node_max.Y <= massive_cave_depth) {
614                 noise_massive_caves->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
615
616                 for (s16 y = node_min.Y - 1; y <= node_max.Y; y++) {
617                         float tcave = massive_cave_threshold;
618
619                         if (y < yblmin) {
620                                 float t = (yblmin - y) / massive_cave_blend;
621                                 tcave += MYSQUARE(t);
622                         } else if (y > yblmax) {
623                                 float t = (y - yblmax) / massive_cave_blend;
624                                 tcave += MYSQUARE(t);
625                         }
626
627                         tcave_cache[y - node_min.Y + 1] = tcave;
628                 }
629         }
630
631         // lava_depth varies between one and ten as you approach
632         //  the bottom of the world.
633         s16 lava_depth = ceil((lava_max_height - node_min.Y + 1) * 10.f / mapgen_limit);
634         // This allows random lava spawns to be less common at the surface.
635         s16 lava_chance = MYCUBE(lava_features_lim) * lava_depth;
636         // water_depth varies between ten and one on the way down.
637         s16 water_depth = ceil((mapgen_limit - abs(node_min.Y) + 1) * 10.f / mapgen_limit);
638         // This allows random water spawns to be more common at the surface.
639         s16 water_chance = MYCUBE(water_features_lim) * water_depth;
640
641         // Reduce the odds of overflows even further.
642         if (node_max.Y > water_level) {
643                 lava_chance /= 3;
644                 water_chance /= 3;
645         }
646
647         u32 index_2d = 0;
648         for (s16 z = node_min.Z; z <= node_max.Z; z++)
649         for (s16 x = node_min.X; x <= node_max.X; x++, index_2d++) {
650                 Biome *biome = (Biome *)m_bmgr->getRaw(biomemap[index_2d]);
651                 bool tunnel_air_above = false;
652                 bool is_under_river = false;
653                 bool underground = false;
654                 u32 index_data = vm->m_area.index(x, node_max.Y, z);
655                 u32 index_3d = (z - node_min.Z) * zstride_1d + csize.Y * ystride + (x - node_min.X);
656
657                 // Dig caves on down loop to check for air above.
658                 // Don't excavate the overgenerated stone at node_max.Y + 1,
659                 // this creates a 'roof' over the tunnel, preventing light in
660                 // tunnels at mapchunk borders when generating mapchunks upwards.
661                 // This 'roof' is removed when the mapchunk above is generated.
662                 for (s16 y = node_max.Y; y >= node_min.Y - 1; y--,
663                                 index_3d -= ystride,
664                                 vm->m_area.add_y(em, index_data, -1)) {
665
666                         float terrain = noise_terrain_height->result[index_2d];
667
668                         // Saves some time.
669                         if (y > terrain + 10)
670                                 continue;
671                         else if (y < terrain - 40)
672                                 underground = true;
673
674                         // Dig massive caves.
675                         if (node_max.Y <= massive_cave_depth
676                                         && noise_massive_caves->result[index_3d]
677                                         > tcave_cache[y - node_min.Y + 1]) {
678                                 vm->m_data[index_data] = n_air;
679                                 made_a_big_one = true;
680                                 continue;
681                         }
682
683                         content_t c = vm->m_data[index_data].getContent();
684                         // Detect river water to place riverbed nodes in tunnels
685                         if (c == biome->c_river_water)
686                                 is_under_river = true;
687
688                         float d1 = contour(noise_cave1->result[index_3d]);
689                         float d2 = contour(noise_cave2->result[index_3d]);
690
691                         if (d1 * d2 > cave_width && ndef->get(c).is_ground_content) {
692                                 // in a tunnel
693                                 vm->m_data[index_data] = n_air;
694                                 tunnel_air_above = true;
695                         } else if (c == biome->c_filler || c == biome->c_stone) {
696                                 if (tunnel_air_above) {
697                                         // at the tunnel floor
698                                         s16 sr = ps.range(0, 39);
699                                         u32 j = index_data;
700                                         vm->m_area.add_y(em, j, 1);
701
702                                         if (sr > terrain - y) {
703                                                 // Put biome nodes in tunnels near the surface
704                                                 if (is_under_river)
705                                                         vm->m_data[index_data] = MapNode(biome->c_riverbed);
706                                                 else if (underground)
707                                                         vm->m_data[index_data] = MapNode(biome->c_filler);
708                                                 else
709                                                         vm->m_data[index_data] = MapNode(biome->c_top);
710                                         } else if (sr < 3 && underground) {
711                                                 sr = abs(ps.next());
712                                                 if (lava_features_lim > 0 && y <= lava_max_height
713                                                                 && c == biome->c_stone && sr < lava_chance)
714                                                         vm->m_data[j] = n_lava;
715
716                                                 sr -= lava_chance;
717
718                                                 // If sr < 0 then we should have already placed lava --
719                                                 // don't immediately dump water on it.
720                                                 if (water_features_lim > 0 && y <= cave_water_max_height
721                                                                 && sr >= 0 && sr < water_chance)
722                                                         vm->m_data[j] = n_water;
723                                         }
724                                 }
725
726                                 tunnel_air_above = false;
727                                 underground = true;
728                         } else {
729                                 tunnel_air_above = false;
730                         }
731                 }
732         }
733
734         if (node_max.Y <= large_cave_depth && !made_a_big_one) {
735                 u32 bruises_count = ps.range(0, 2);
736                 for (u32 i = 0; i < bruises_count; i++) {
737                         CavesRandomWalk cave(ndef, &gennotify, seed, water_level,
738                                 c_water_source, c_lava_source, lava_max_height);
739
740                         cave.makeCave(vm, node_min, node_max, &ps, true, max_stone_y, heightmap);
741                 }
742         }
743 }