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