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