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