Fractal mapgen: Add seabed and large pseudorandom caves
[oweals/minetest.git] / src / mapgen_fractal.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 paramat, Matt Gregory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21
22 #include "mapgen.h"
23 #include "voxel.h"
24 #include "noise.h"
25 #include "mapblock.h"
26 #include "mapnode.h"
27 #include "map.h"
28 #include "content_sao.h"
29 #include "nodedef.h"
30 #include "voxelalgorithms.h"
31 #include "settings.h" // For g_settings
32 #include "emerge.h"
33 #include "dungeongen.h"
34 #include "cavegen.h"
35 #include "treegen.h"
36 #include "mg_biome.h"
37 #include "mg_ore.h"
38 #include "mg_decoration.h"
39 #include "mapgen_fractal.h"
40
41
42 FlagDesc flagdesc_mapgen_fractal[] = {
43         {"julia", MGFRACTAL_JULIA},
44         {NULL,    0}
45 };
46
47 ///////////////////////////////////////////////////////////////////////////////////////
48
49
50 MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge)
51         : Mapgen(mapgenid, params, emerge)
52 {
53         this->m_emerge = emerge;
54         this->bmgr     = emerge->biomemgr;
55
56         //// amount of elements to skip for the next index
57         //// for noise/height/biome maps (not vmanip)
58         this->ystride = csize.X;
59         this->zstride = csize.X * (csize.Y + 2);
60
61         this->biomemap        = new u8[csize.X * csize.Z];
62         this->heightmap       = new s16[csize.X * csize.Z];
63         this->heatmap         = NULL;
64         this->humidmap        = NULL;
65
66         MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams;
67         this->spflags = sp->spflags;
68         this->iterations = sp->iterations;
69         this->scale_x = sp->scale_x;
70         this->scale_y = sp->scale_y;
71         this->scale_z = sp->scale_z;
72         this->offset_x = sp->offset_x;
73         this->offset_y = sp->offset_y;
74         this->offset_z = sp->offset_z;
75
76         //// 2D terrain noise
77         noise_seabed = new Noise(&sp->np_seabed, seed, csize.X, csize.Z);
78
79         //// 3D terrain noise
80         noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 2, csize.Z);
81         noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 2, csize.Z);
82
83         //// Biome noise
84         noise_heat           = new Noise(&params->np_biome_heat,           seed, csize.X, csize.Z);
85         noise_humidity       = new Noise(&params->np_biome_humidity,       seed, csize.X, csize.Z);
86         noise_heat_blend     = new Noise(&params->np_biome_heat_blend,     seed, csize.X, csize.Z);
87         noise_humidity_blend = new Noise(&params->np_biome_humidity_blend, seed, csize.X, csize.Z);
88
89         //// Resolve nodes to be used
90         INodeDefManager *ndef = emerge->ndef;
91
92         c_stone                = ndef->getId("mapgen_stone");
93         c_water_source         = ndef->getId("mapgen_water_source");
94         c_lava_source          = ndef->getId("mapgen_lava_source");
95         c_desert_stone         = ndef->getId("mapgen_desert_stone");
96         c_ice                  = ndef->getId("mapgen_ice");
97         c_sandstone            = ndef->getId("mapgen_sandstone");
98
99         c_cobble               = ndef->getId("mapgen_cobble");
100         c_stair_cobble         = ndef->getId("mapgen_stair_cobble");
101         c_mossycobble          = ndef->getId("mapgen_mossycobble");
102         c_sandstonebrick       = ndef->getId("mapgen_sandstonebrick");
103         c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
104
105         if (c_ice == CONTENT_IGNORE)
106                 c_ice = CONTENT_AIR;
107         if (c_mossycobble == CONTENT_IGNORE)
108                 c_mossycobble = c_cobble;
109         if (c_stair_cobble == CONTENT_IGNORE)
110                 c_stair_cobble = c_cobble;
111         if (c_sandstonebrick == CONTENT_IGNORE)
112                 c_sandstonebrick = c_sandstone;
113         if (c_stair_sandstonebrick == CONTENT_IGNORE)
114                 c_stair_sandstonebrick = c_sandstone;
115 }
116
117
118 MapgenFractal::~MapgenFractal()
119 {
120         delete noise_seabed;
121
122         delete noise_cave1;
123         delete noise_cave2;
124
125         delete noise_heat;
126         delete noise_humidity;
127         delete noise_heat_blend;
128         delete noise_humidity_blend;
129
130         delete[] heightmap;
131         delete[] biomemap;
132 }
133
134
135 MapgenFractalParams::MapgenFractalParams()
136 {
137         spflags = 0;
138
139         iterations = 9;
140         scale_x = 1024.0;
141         scale_y = 256.0;
142         scale_z = 1024.0;
143         offset_x = -1.75;
144         offset_y = 0.0;
145         offset_z = 0.0;
146         slice_w = 0.5;
147         julia_x = 0.33;
148         julia_y = 0.33;
149         julia_z = 0.33;
150         julia_w = 0.33;
151
152         np_seabed = NoiseParams(-14, 9,  v3f(600, 600, 600), 41900, 5, 0.6, 2.0);
153         np_cave1  = NoiseParams(0,   12, v3f(128, 128, 128), 52534, 4, 0.5, 2.0);
154         np_cave2  = NoiseParams(0,   12, v3f(128, 128, 128), 10325, 4, 0.5, 2.0);
155 }
156
157
158 void MapgenFractalParams::readParams(const Settings *settings)
159 {
160         settings->getFlagStrNoEx("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
161
162         settings->getU16NoEx("mgfractal_iterations", iterations);
163         settings->getFloatNoEx("mgfractal_scale_x", scale_x);
164         settings->getFloatNoEx("mgfractal_scale_y", scale_y);
165         settings->getFloatNoEx("mgfractal_scale_z", scale_z);
166         settings->getFloatNoEx("mgfractal_offset_x", offset_x);
167         settings->getFloatNoEx("mgfractal_offset_y", offset_y);
168         settings->getFloatNoEx("mgfractal_offset_z", offset_z);
169         settings->getFloatNoEx("mgfractal_slice_w", slice_w);
170         settings->getFloatNoEx("mgfractal_julia_x", julia_x);
171         settings->getFloatNoEx("mgfractal_julia_y", julia_y);
172         settings->getFloatNoEx("mgfractal_julia_z", julia_z);
173         settings->getFloatNoEx("mgfractal_julia_w", julia_w);
174
175         settings->getNoiseParams("mgfractal_np_seabed", np_seabed);
176         settings->getNoiseParams("mgfractal_np_cave1", np_cave1);
177         settings->getNoiseParams("mgfractal_np_cave2", np_cave2);
178 }
179
180
181 void MapgenFractalParams::writeParams(Settings *settings) const
182 {
183         settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal, U32_MAX);
184
185         settings->setU16("mgfractal_iterations", iterations);
186         settings->setFloat("mgfractal_scale_x", scale_x);
187         settings->setFloat("mgfractal_scale_y", scale_y);
188         settings->setFloat("mgfractal_scale_z", scale_z);
189         settings->setFloat("mgfractal_offset_x", offset_x);
190         settings->setFloat("mgfractal_offset_y", offset_y);
191         settings->setFloat("mgfractal_offset_z", offset_z);
192         settings->setFloat("mgfractal_slice_w", slice_w);
193         settings->setFloat("mgfractal_julia_x", julia_x);
194         settings->setFloat("mgfractal_julia_y", julia_y);
195         settings->setFloat("mgfractal_julia_z", julia_z);
196         settings->setFloat("mgfractal_julia_w", julia_w);
197
198         settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
199         settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
200         settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
201 }
202
203
204 /////////////////////////////////////////////////////////////////
205
206
207 int MapgenFractal::getGroundLevelAtPoint(v2s16 p)
208 {
209         s16 search_start = 128;
210         s16 search_end = -128;
211
212         for (s16 y = search_start; y >= search_end; y--) {
213                 if (getFractalAtPoint(p.X, y, p.Y))
214                         return y;
215         }
216
217         return -MAX_MAP_GENERATION_LIMIT;
218 }
219
220
221 void MapgenFractal::makeChunk(BlockMakeData *data)
222 {
223         // Pre-conditions
224         assert(data->vmanip);
225         assert(data->nodedef);
226         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
227                 data->blockpos_requested.Y >= data->blockpos_min.Y &&
228                 data->blockpos_requested.Z >= data->blockpos_min.Z);
229         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
230                 data->blockpos_requested.Y <= data->blockpos_max.Y &&
231                 data->blockpos_requested.Z <= data->blockpos_max.Z);
232
233         this->generating = true;
234         this->vm   = data->vmanip;
235         this->ndef = data->nodedef;
236         //TimeTaker t("makeChunk");
237
238         v3s16 blockpos_min = data->blockpos_min;
239         v3s16 blockpos_max = data->blockpos_max;
240         node_min = blockpos_min * MAP_BLOCKSIZE;
241         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
242         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
243         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
244
245         blockseed = getBlockSeed2(full_node_min, seed);
246
247         // Make some noise
248         calculateNoise();
249
250         // Generate base terrain, mountains, and ridges with initial heightmaps
251         s16 stone_surface_max_y = generateTerrain();
252
253         // Create heightmap
254         updateHeightmap(node_min, node_max);
255
256         // Create biomemap at heightmap surface
257         bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result,
258                 noise_humidity->result, heightmap, biomemap);
259
260         // Actually place the biome-specific nodes
261         MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result);
262
263         if (flags & MG_CAVES)
264                 generateCaves(stone_surface_max_y);
265
266         if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) {
267                 DungeonParams dp;
268
269                 dp.np_rarity  = nparams_dungeon_rarity;
270                 dp.np_density = nparams_dungeon_density;
271                 dp.np_wetness = nparams_dungeon_wetness;
272                 dp.c_water    = c_water_source;
273                 if (stone_type == STONE) {
274                         dp.c_cobble = c_cobble;
275                         dp.c_moss   = c_mossycobble;
276                         dp.c_stair  = c_stair_cobble;
277
278                         dp.diagonal_dirs = false;
279                         dp.mossratio     = 3.0;
280                         dp.holesize      = v3s16(1, 2, 1);
281                         dp.roomsize      = v3s16(0, 0, 0);
282                         dp.notifytype    = GENNOTIFY_DUNGEON;
283                 } else if (stone_type == DESERT_STONE) {
284                         dp.c_cobble = c_desert_stone;
285                         dp.c_moss   = c_desert_stone;
286                         dp.c_stair  = c_desert_stone;
287
288                         dp.diagonal_dirs = true;
289                         dp.mossratio     = 0.0;
290                         dp.holesize      = v3s16(2, 3, 2);
291                         dp.roomsize      = v3s16(2, 5, 2);
292                         dp.notifytype    = GENNOTIFY_TEMPLE;
293                 } else if (stone_type == SANDSTONE) {
294                         dp.c_cobble = c_sandstonebrick;
295                         dp.c_moss   = c_sandstonebrick;
296                         dp.c_stair  = c_sandstonebrick;
297
298                         dp.diagonal_dirs = false;
299                         dp.mossratio     = 0.0;
300                         dp.holesize      = v3s16(2, 2, 2);
301                         dp.roomsize      = v3s16(2, 0, 2);
302                         dp.notifytype    = GENNOTIFY_DUNGEON;
303                 }
304
305                 DungeonGen dgen(this, &dp);
306                 dgen.generate(blockseed, full_node_min, full_node_max);
307         }
308
309         // Generate the registered decorations
310         m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
311
312         // Generate the registered ores
313         m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
314
315         // Sprinkle some dust on top after everything else was generated
316         dustTopNodes();
317
318         //printf("makeChunk: %dms\n", t.stop());
319
320         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
321
322         if (flags & MG_LIGHT)
323                 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
324                         full_node_min, full_node_max);
325
326         //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
327         //                      node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
328
329         this->generating = false;
330 }
331
332
333 void MapgenFractal::calculateNoise()
334 {
335         //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
336         int x = node_min.X;
337         int y = node_min.Y - 1;
338         int z = node_min.Z;
339
340         noise_seabed->perlinMap2D(x, z);
341
342         if (flags & MG_CAVES) {
343                 noise_cave1->perlinMap3D(x, y, z);
344                 noise_cave2->perlinMap3D(x, y, z);
345         }
346
347         noise_heat->perlinMap2D(x, z);
348         noise_humidity->perlinMap2D(x, z);
349         noise_heat_blend->perlinMap2D(x, z);
350         noise_humidity_blend->perlinMap2D(x, z);
351
352         for (s32 i = 0; i < csize.X * csize.Z; i++) {
353                 noise_heat->result[i] += noise_heat_blend->result[i];
354                 noise_humidity->result[i] += noise_humidity_blend->result[i];
355         }
356
357         heatmap = noise_heat->result;
358         humidmap = noise_humidity->result;
359         //printf("calculateNoise: %dus\n", t.stop());
360 }
361
362
363 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
364 {
365         float cx, cy, cz, cw, ox, oy, oz, ow;
366
367         if (spflags & MGFRACTAL_JULIA) {  // Julia set
368                 cx = julia_x;
369                 cy = julia_y;
370                 cz = julia_z;
371                 cw = julia_w;
372                 ox = (float)x / scale_x + offset_x;
373                 oy = (float)y / scale_y + offset_y;
374                 oz = (float)z / scale_z + offset_z;
375                 ow = slice_w;
376         } else {  // Mandelbrot set
377                 cx = (float)x / scale_x + offset_x;
378                 cy = (float)y / scale_y + offset_y;
379                 cz = (float)z / scale_z + offset_z;
380                 cw = slice_w;
381                 ox = 0.0f;
382                 oy = 0.0f;
383                 oz = 0.0f;
384                 ow = 0.0f;
385         }
386
387         for (u16 iter = 0; iter < iterations; iter++) {
388                 // 4D "Roundy" Mandelbrot set
389                 float nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
390                 float ny = 2.0f * (ox * oy + oz * ow) + cy;
391                 float nz = 2.0f * (ox * oz + oy * ow) + cz;
392                 float nw = 2.0f * (ox * ow + oy * oz) + cw;
393
394                 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
395                         return false;
396
397                 ox = nx;
398                 oy = ny;
399                 oz = nz;
400                 ow = nw;
401         }
402
403         return true;
404 }
405
406
407 s16 MapgenFractal::generateTerrain()
408 {
409         MapNode n_air(CONTENT_AIR);
410         MapNode n_stone(c_stone);
411         MapNode n_water(c_water_source);
412
413         s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
414         u32 index2d = 0;
415
416         for (s16 z = node_min.Z; z <= node_max.Z; z++) {
417                 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
418                         u32 vi = vm->m_area.index(node_min.X, y, z);
419                         for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
420                                 if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
421                                         s16 seabed_height = noise_seabed->result[index2d];
422
423                                         if (y <= seabed_height || getFractalAtPoint(x, y, z)) {
424                                                 vm->m_data[vi] = n_stone;
425                                                 if (y > stone_surface_max_y)
426                                                         stone_surface_max_y = y;
427                                         } else if (y <= water_level) {
428                                                 vm->m_data[vi] = n_water;
429                                         } else {
430                                                 vm->m_data[vi] = n_air;
431                                         }
432                                 }
433                         }
434                         index2d -= ystride;
435                 }
436                 index2d += ystride;
437         }
438
439         return stone_surface_max_y;
440 }
441
442
443 MgStoneType MapgenFractal::generateBiomes(float *heat_map, float *humidity_map)
444 {
445         v3s16 em = vm->m_area.getExtent();
446         u32 index = 0;
447         MgStoneType stone_type = STONE;
448
449         for (s16 z = node_min.Z; z <= node_max.Z; z++)
450         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
451                 Biome *biome = NULL;
452                 u16 depth_top = 0;
453                 u16 base_filler = 0;
454                 u16 depth_water_top = 0;
455                 u32 vi = vm->m_area.index(x, node_max.Y, z);
456
457                 // Check node at base of mapchunk above, either a node of a previously
458                 // generated mapchunk or if not, a node of overgenerated base terrain.
459                 content_t c_above = vm->m_data[vi + em.X].getContent();
460                 bool air_above = c_above == CONTENT_AIR;
461                 bool water_above = c_above == c_water_source;
462
463                 // If there is air or water above enable top/filler placement, otherwise force
464                 // nplaced to stone level by setting a number exceeding any possible filler depth.
465                 u16 nplaced = (air_above || water_above) ? 0 : U16_MAX;
466
467
468                 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
469                         content_t c = vm->m_data[vi].getContent();
470
471                         // Biome is recalculated each time an upper surface is detected while
472                         // working down a column. The selected biome then remains in effect for
473                         // all nodes below until the next surface and biome recalculation.
474                         // Biome is recalculated:
475                         // 1. At the surface of stone below air or water.
476                         // 2. At the surface of water below air.
477                         // 3. When stone or water is detected but biome has not yet been calculated.
478                         if ((c == c_stone && (air_above || water_above || !biome)) ||
479                                         (c == c_water_source && (air_above || !biome))) {
480                                 biome = bmgr->getBiome(heat_map[index], humidity_map[index], y);
481                                 depth_top = biome->depth_top;
482                                 base_filler = depth_top + biome->depth_filler;
483                                 depth_water_top = biome->depth_water_top;
484
485                                 // Detect stone type for dungeons during every biome calculation.
486                                 // This is more efficient than detecting per-node and will not
487                                 // miss any desert stone or sandstone biomes.
488                                 if (biome->c_stone == c_desert_stone)
489                                         stone_type = DESERT_STONE;
490                                 else if (biome->c_stone == c_sandstone)
491                                         stone_type = SANDSTONE;
492                         }
493
494                         if (c == c_stone) {
495                                 content_t c_below = vm->m_data[vi - em.X].getContent();
496
497                                 // If the node below isn't solid, make this node stone, so that
498                                 // any top/filler nodes above are structurally supported.
499                                 // This is done by aborting the cycle of top/filler placement
500                                 // immediately by forcing nplaced to stone level.
501                                 if (c_below == CONTENT_AIR || c_below == c_water_source)
502                                         nplaced = U16_MAX;
503
504                                 if (nplaced < depth_top) {
505                                         vm->m_data[vi] = MapNode(biome->c_top);
506                                         nplaced++;
507                                 } else if (nplaced < base_filler) {
508                                         vm->m_data[vi] = MapNode(biome->c_filler);
509                                         nplaced++;
510                                 } else {
511                                         vm->m_data[vi] = MapNode(biome->c_stone);
512                                 }
513
514                                 air_above = false;
515                                 water_above = false;
516                         } else if (c == c_water_source) {
517                                 vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ?
518                                                 biome->c_water_top : biome->c_water);
519                                 nplaced = 0;  // Enable top/filler placement for next surface
520                                 air_above = false;
521                                 water_above = true;
522                         } else if (c == CONTENT_AIR) {
523                                 nplaced = 0;  // Enable top/filler placement for next surface
524                                 air_above = true;
525                                 water_above = false;
526                         } else {  // Possible various nodes overgenerated from neighbouring mapchunks
527                                 nplaced = U16_MAX;  // Disable top/filler placement
528                                 air_above = false;
529                                 water_above = false;
530                         }
531
532                         vm->m_area.add_y(em, vi, -1);
533                 }
534         }
535
536         return stone_type;
537 }
538
539
540 void MapgenFractal::dustTopNodes()
541 {
542         if (node_max.Y < water_level)
543                 return;
544
545         v3s16 em = vm->m_area.getExtent();
546         u32 index = 0;
547
548         for (s16 z = node_min.Z; z <= node_max.Z; z++)
549         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
550                 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]);
551
552                 if (biome->c_dust == CONTENT_IGNORE)
553                         continue;
554
555                 u32 vi = vm->m_area.index(x, full_node_max.Y, z);
556                 content_t c_full_max = vm->m_data[vi].getContent();
557                 s16 y_start;
558
559                 if (c_full_max == CONTENT_AIR) {
560                         y_start = full_node_max.Y - 1;
561                 } else if (c_full_max == CONTENT_IGNORE) {
562                         vi = vm->m_area.index(x, node_max.Y + 1, z);
563                         content_t c_max = vm->m_data[vi].getContent();
564
565                         if (c_max == CONTENT_AIR)
566                                 y_start = node_max.Y;
567                         else
568                                 continue;
569                 } else {
570                         continue;
571                 }
572
573                 vi = vm->m_area.index(x, y_start, z);
574                 for (s16 y = y_start; y >= node_min.Y - 1; y--) {
575                         if (vm->m_data[vi].getContent() != CONTENT_AIR)
576                                 break;
577
578                         vm->m_area.add_y(em, vi, -1);
579                 }
580
581                 content_t c = vm->m_data[vi].getContent();
582                 if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) {
583                         vm->m_area.add_y(em, vi, 1);
584                         vm->m_data[vi] = MapNode(biome->c_dust);
585                 }
586         }
587 }
588
589
590 void MapgenFractal::generateCaves(s16 max_stone_y)
591 {
592         if (max_stone_y >= node_min.Y) {
593                 u32 index = 0;
594
595                 for (s16 z = node_min.Z; z <= node_max.Z; z++)
596                 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
597                         u32 vi = vm->m_area.index(node_min.X, y, z);
598                         for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index++) {
599                                 float d1 = contour(noise_cave1->result[index]);
600                                 float d2 = contour(noise_cave2->result[index]);
601                                 if (d1 * d2 > 0.3) {
602                                         content_t c = vm->m_data[vi].getContent();
603                                         if (!ndef->get(c).is_ground_content || c == CONTENT_AIR)
604                                                 continue;
605
606                                         vm->m_data[vi] = MapNode(CONTENT_AIR);
607                                 }
608                         }
609                 }
610         }
611
612         if (node_max.Y > MGFRACTAL_LARGE_CAVE_DEPTH)
613                 return;
614
615         PseudoRandom ps(blockseed + 21343);
616         u32 bruises_count = (ps.range(1, 4) == 1) ? ps.range(1, 2) : 0;
617         for (u32 i = 0; i < bruises_count; i++) {
618                 CaveFractal cave(this, &ps);
619                 cave.makeCave(node_min, node_max, max_stone_y);
620         }
621 }