3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 paramat, Matt Gregory
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.
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.
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.
28 #include "content_sao.h"
30 #include "voxelalgorithms.h"
31 //#include "profiler.h" // For TimeTaker
32 #include "settings.h" // For g_settings
34 #include "dungeongen.h"
39 #include "mg_decoration.h"
40 #include "mapgen_fractal.h"
43 FlagDesc flagdesc_mapgen_fractal[] = {
47 ///////////////////////////////////////////////////////////////////////////////////////
50 MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge)
51 : Mapgen(mapgenid, params, emerge)
53 this->m_emerge = emerge;
54 this->bmgr = emerge->biomemgr;
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);
62 this->biomemap = new u8[csize.X * csize.Z];
63 this->heightmap = new s16[csize.X * csize.Z];
65 this->humidmap = NULL;
67 MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams;
69 this->spflags = sp->spflags;
70 this->cave_width = sp->cave_width;
71 this->fractal = sp->fractal;
72 this->iterations = sp->iterations;
73 this->scale = sp->scale;
74 this->offset = sp->offset;
75 this->slice_w = sp->slice_w;
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;
82 noise_seabed = new Noise(&sp->np_seabed, seed, csize.X, csize.Z);
83 noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z);
86 // 1-down overgeneraion
87 noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z);
88 noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z);
91 noise_heat = new Noise(¶ms->np_biome_heat, seed, csize.X, csize.Z);
92 noise_humidity = new Noise(¶ms->np_biome_humidity, seed, csize.X, csize.Z);
93 noise_heat_blend = new Noise(¶ms->np_biome_heat_blend, seed, csize.X, csize.Z);
94 noise_humidity_blend = new Noise(¶ms->np_biome_humidity_blend, seed, csize.X, csize.Z);
96 this->formula = fractal / 2 + fractal % 2;
97 this->julia = fractal % 2 == 0;
99 //// Resolve nodes to be used
100 INodeDefManager *ndef = emerge->ndef;
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");
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");
115 if (c_ice == CONTENT_IGNORE)
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;
128 MapgenFractal::~MapgenFractal()
131 delete noise_filler_depth;
136 delete noise_humidity;
137 delete noise_heat_blend;
138 delete noise_humidity_blend;
145 MapgenFractalParams::MapgenFractalParams()
151 scale = v3f(4096.0, 1024.0, 4096.0);
152 offset = v3f(1.79, 0.0, 0.0);
159 np_seabed = NoiseParams(-14, 9, v3f(600, 600, 600), 41900, 5, 0.6, 2.0);
160 np_filler_depth = NoiseParams(0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0);
161 np_cave1 = NoiseParams(0, 12, v3f(96, 96, 96), 52534, 4, 0.5, 2.0);
162 np_cave2 = NoiseParams(0, 12, v3f(96, 96, 96), 10325, 4, 0.5, 2.0);
166 void MapgenFractalParams::readParams(const Settings *settings)
168 settings->getFlagStrNoEx("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
169 settings->getFloatNoEx("mgfractal_cave_width", cave_width);
170 settings->getU16NoEx("mgfractal_fractal", fractal);
171 settings->getU16NoEx("mgfractal_iterations", iterations);
172 settings->getV3FNoEx("mgfractal_scale", scale);
173 settings->getV3FNoEx("mgfractal_offset", offset);
174 settings->getFloatNoEx("mgfractal_slice_w", slice_w);
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);
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);
187 void MapgenFractalParams::writeParams(Settings *settings) const
189 settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal, U32_MAX);
190 settings->setFloat("mgfractal_cave_width", cave_width);
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 settings->setFloat("mgfractal_julia_x", julia_x);
197 settings->setFloat("mgfractal_julia_y", julia_y);
198 settings->setFloat("mgfractal_julia_z", julia_z);
199 settings->setFloat("mgfractal_julia_w", julia_w);
201 settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
202 settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
203 settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
204 settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
208 /////////////////////////////////////////////////////////////////
211 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
213 bool solid_below = false; // Dry solid node is present below to spawn on
214 u8 air_count = 0; // Consecutive air nodes above the dry solid node
215 s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
216 // Seabed can rise above water_level or might be raised to create dry land
217 s16 search_start = MYMAX(seabed_level, water_level + 1);
218 if (seabed_level > water_level)
221 for (s16 y = search_start; y <= search_start + 128; y++) {
222 if (getFractalAtPoint(p.X, y, p.Y)) { // Fractal node
225 } else if (solid_below) { // Air above solid node
232 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
236 void MapgenFractal::makeChunk(BlockMakeData *data)
239 assert(data->vmanip);
240 assert(data->nodedef);
241 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
242 data->blockpos_requested.Y >= data->blockpos_min.Y &&
243 data->blockpos_requested.Z >= data->blockpos_min.Z);
244 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
245 data->blockpos_requested.Y <= data->blockpos_max.Y &&
246 data->blockpos_requested.Z <= data->blockpos_max.Z);
248 this->generating = true;
249 this->vm = data->vmanip;
250 this->ndef = data->nodedef;
251 //TimeTaker t("makeChunk");
253 v3s16 blockpos_min = data->blockpos_min;
254 v3s16 blockpos_max = data->blockpos_max;
255 node_min = blockpos_min * MAP_BLOCKSIZE;
256 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
257 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
258 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
260 blockseed = getBlockSeed2(full_node_min, seed);
265 // Generate base terrain, mountains, and ridges with initial heightmaps
266 s16 stone_surface_max_y = generateTerrain();
269 updateHeightmap(node_min, node_max);
271 // Create biomemap at heightmap surface
272 bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result,
273 noise_humidity->result, heightmap, biomemap);
275 // Actually place the biome-specific nodes
276 MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result);
278 if (flags & MG_CAVES)
279 generateCaves(stone_surface_max_y);
281 if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) {
284 dp.np_rarity = nparams_dungeon_rarity;
285 dp.np_density = nparams_dungeon_density;
286 dp.np_wetness = nparams_dungeon_wetness;
287 dp.c_water = c_water_source;
288 if (stone_type == STONE) {
289 dp.c_cobble = c_cobble;
290 dp.c_moss = c_mossycobble;
291 dp.c_stair = c_stair_cobble;
293 dp.diagonal_dirs = false;
295 dp.holesize = v3s16(1, 2, 1);
296 dp.roomsize = v3s16(0, 0, 0);
297 dp.notifytype = GENNOTIFY_DUNGEON;
298 } else if (stone_type == DESERT_STONE) {
299 dp.c_cobble = c_desert_stone;
300 dp.c_moss = c_desert_stone;
301 dp.c_stair = c_desert_stone;
303 dp.diagonal_dirs = true;
305 dp.holesize = v3s16(2, 3, 2);
306 dp.roomsize = v3s16(2, 5, 2);
307 dp.notifytype = GENNOTIFY_TEMPLE;
308 } else if (stone_type == SANDSTONE) {
309 dp.c_cobble = c_sandstonebrick;
310 dp.c_moss = c_sandstonebrick;
311 dp.c_stair = c_sandstonebrick;
313 dp.diagonal_dirs = false;
315 dp.holesize = v3s16(2, 2, 2);
316 dp.roomsize = v3s16(2, 0, 2);
317 dp.notifytype = GENNOTIFY_DUNGEON;
320 DungeonGen dgen(this, &dp);
321 dgen.generate(blockseed, full_node_min, full_node_max);
324 // Generate the registered decorations
325 if (flags & MG_DECORATIONS)
326 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
328 // Generate the registered ores
329 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
331 // Sprinkle some dust on top after everything else was generated
334 //printf("makeChunk: %dms\n", t.stop());
336 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
338 if (flags & MG_LIGHT)
339 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
340 full_node_min, full_node_max);
342 //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
343 // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
345 this->generating = false;
349 void MapgenFractal::calculateNoise()
351 //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
355 noise_seabed->perlinMap2D(x, z);
357 // Cave noises are calculated in generateCaves()
358 // only if solid terrain is present in mapchunk
360 noise_filler_depth->perlinMap2D(x, z);
361 noise_heat->perlinMap2D(x, z);
362 noise_humidity->perlinMap2D(x, z);
363 noise_heat_blend->perlinMap2D(x, z);
364 noise_humidity_blend->perlinMap2D(x, z);
366 for (s32 i = 0; i < csize.X * csize.Z; i++) {
367 noise_heat->result[i] += noise_heat_blend->result[i];
368 noise_humidity->result[i] += noise_humidity_blend->result[i];
371 heatmap = noise_heat->result;
372 humidmap = noise_humidity->result;
373 //printf("calculateNoise: %dus\n", t.stop());
377 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
379 float cx, cy, cz, cw, ox, oy, oz, ow;
381 if (julia) { // Julia set
386 ox = (float)x / scale.X - offset.X;
387 oy = (float)y / scale.Y - offset.Y;
388 oz = (float)z / scale.Z - offset.Z;
390 } else { // Mandelbrot set
391 cx = (float)x / scale.X - offset.X;
392 cy = (float)y / scale.Y - offset.Y;
393 cz = (float)z / scale.Z - offset.Z;
406 for (u16 iter = 0; iter < iterations; iter++) {
408 if (formula == 1) { // 4D "Roundy"
409 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
410 ny = 2.0f * (ox * oy + oz * ow) + cy;
411 nz = 2.0f * (ox * oz + oy * ow) + cz;
412 nw = 2.0f * (ox * ow + oy * oz) + cw;
413 } else if (formula == 2) { // 4D "Squarry"
414 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
415 ny = 2.0f * (ox * oy + oz * ow) + cy;
416 nz = 2.0f * (ox * oz + oy * ow) + cz;
417 nw = 2.0f * (ox * ow - oy * oz) + cw;
418 } else if (formula == 3) { // 4D "Mandy Cousin"
419 nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
420 ny = 2.0f * (ox * oy + oz * ow) + cy;
421 nz = 2.0f * (ox * oz + oy * ow) + cz;
422 nw = 2.0f * (ox * ow + oy * oz) + cw;
423 } else if (formula == 4) { // 4D "Variation"
424 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
425 ny = 2.0f * (ox * oy + oz * ow) + cy;
426 nz = 2.0f * (ox * oz - oy * ow) + cz;
427 nw = 2.0f * (ox * ow + oy * oz) + cw;
428 } else if (formula == 5) { // 3D "Mandelbrot/Mandelbar"
429 nx = ox * ox - oy * oy - oz * oz + cx;
430 ny = 2.0f * ox * oy + cy;
431 nz = -2.0f * ox * oz + cz;
432 } else if (formula == 6) { // 3D "Christmas Tree"
433 // Altering the formula here is necessary to avoid division by zero
434 if (fabs(oz) < 0.000000001f) {
435 nx = ox * ox - oy * oy - oz * oz + cx;
436 ny = 2.0f * oy * ox + cy;
437 nz = 4.0f * oz * ox + cz;
439 float a = (2.0f * ox) / (sqrt(oy * oy + oz * oz));
440 nx = ox * ox - oy * oy - oz * oz + cx;
441 ny = a * (oy * oy - oz * oz) + cy;
442 nz = a * 2.0f * oy * oz + cz;
444 } else if (formula == 7) { // 3D "Mandelbulb"
445 if (fabs(oy) < 0.000000001f) {
446 nx = ox * ox - oz * oz + cx;
448 nz = -2.0f * oz * sqrt(ox * ox) + cz;
450 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
451 nx = (ox * ox - oy * oy) * a + cx;
452 ny = 2.0f * ox * oy * a + cy;
453 nz = -2.0f * oz * sqrt(ox * ox + oy * oy) + cz;
455 } else if (formula == 8) { // 3D "Cosine Mandelbulb"
456 if (fabs(oy) < 0.000000001f) {
457 nx = 2.0f * ox * oz + cx;
458 ny = 4.0f * oy * oz + cy;
459 nz = oz * oz - ox * ox - oy * oy + cz;
461 float a = (2.0f * oz) / sqrt(ox * ox + oy * oy);
462 nx = (ox * ox - oy * oy) * a + cx;
463 ny = 2.0f * ox * oy * a + cy;
464 nz = oz * oz - ox * ox - oy * oy + cz;
466 } else if (formula == 9) { // 4D "Mandelbulb"
467 float rxy = sqrt(ox * ox + oy * oy);
468 float rxyz = sqrt(ox * ox + oy * oy + oz * oz);
469 if (fabs(ow) < 0.000000001f && fabs(oz) < 0.000000001f) {
470 nx = (ox * ox - oy * oy) + cx;
471 ny = 2.0f * ox * oy + cy;
472 nz = -2.0f * rxy * oz + cz;
473 nw = 2.0f * rxyz * ow + cw;
475 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
476 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
477 nx = (ox * ox - oy * oy) * b + cx;
478 ny = 2.0f * ox * oy * b + cy;
479 nz = -2.0f * rxy * oz * a + cz;
480 nw = 2.0f * rxyz * ow + cw;
484 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
497 s16 MapgenFractal::generateTerrain()
499 MapNode n_air(CONTENT_AIR);
500 MapNode n_stone(c_stone);
501 MapNode n_water(c_water_source);
503 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
506 for (s16 z = node_min.Z; z <= node_max.Z; z++) {
507 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
508 u32 vi = vm->m_area.index(node_min.X, y, z);
509 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
510 if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
511 s16 seabed_height = noise_seabed->result[index2d];
513 if (y <= seabed_height || getFractalAtPoint(x, y, z)) {
514 vm->m_data[vi] = n_stone;
515 if (y > stone_surface_max_y)
516 stone_surface_max_y = y;
517 } else if (y <= water_level) {
518 vm->m_data[vi] = n_water;
520 vm->m_data[vi] = n_air;
529 return stone_surface_max_y;
533 MgStoneType MapgenFractal::generateBiomes(float *heat_map, float *humidity_map)
535 v3s16 em = vm->m_area.getExtent();
537 MgStoneType stone_type = STONE;
539 for (s16 z = node_min.Z; z <= node_max.Z; z++)
540 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
544 u16 depth_water_top = 0;
545 u32 vi = vm->m_area.index(x, node_max.Y, z);
547 // Check node at base of mapchunk above, either a node of a previously
548 // generated mapchunk or if not, a node of overgenerated base terrain.
549 content_t c_above = vm->m_data[vi + em.X].getContent();
550 bool air_above = c_above == CONTENT_AIR;
551 bool water_above = c_above == c_water_source;
553 // If there is air or water above enable top/filler placement, otherwise force
554 // nplaced to stone level by setting a number exceeding any possible filler depth.
555 u16 nplaced = (air_above || water_above) ? 0 : U16_MAX;
558 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
559 content_t c = vm->m_data[vi].getContent();
561 // Biome is recalculated each time an upper surface is detected while
562 // working down a column. The selected biome then remains in effect for
563 // all nodes below until the next surface and biome recalculation.
564 // Biome is recalculated:
565 // 1. At the surface of stone below air or water.
566 // 2. At the surface of water below air.
567 // 3. When stone or water is detected but biome has not yet been calculated.
568 if ((c == c_stone && (air_above || water_above || !biome)) ||
569 (c == c_water_source && (air_above || !biome))) {
570 biome = bmgr->getBiome(heat_map[index], humidity_map[index], y);
571 depth_top = biome->depth_top;
572 base_filler = MYMAX(depth_top + biome->depth_filler
573 + noise_filler_depth->result[index], 0);
574 depth_water_top = biome->depth_water_top;
576 // Detect stone type for dungeons during every biome calculation.
577 // This is more efficient than detecting per-node and will not
578 // miss any desert stone or sandstone biomes.
579 if (biome->c_stone == c_desert_stone)
580 stone_type = DESERT_STONE;
581 else if (biome->c_stone == c_sandstone)
582 stone_type = SANDSTONE;
586 content_t c_below = vm->m_data[vi - em.X].getContent();
588 // If the node below isn't solid, make this node stone, so that
589 // any top/filler nodes above are structurally supported.
590 // This is done by aborting the cycle of top/filler placement
591 // immediately by forcing nplaced to stone level.
592 if (c_below == CONTENT_AIR || c_below == c_water_source)
595 if (nplaced < depth_top) {
596 vm->m_data[vi] = MapNode(biome->c_top);
598 } else if (nplaced < base_filler) {
599 vm->m_data[vi] = MapNode(biome->c_filler);
602 vm->m_data[vi] = MapNode(biome->c_stone);
607 } else if (c == c_water_source) {
608 vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ?
609 biome->c_water_top : biome->c_water);
610 nplaced = 0; // Enable top/filler placement for next surface
613 } else if (c == CONTENT_AIR) {
614 nplaced = 0; // Enable top/filler placement for next surface
617 } else { // Possible various nodes overgenerated from neighbouring mapchunks
618 nplaced = U16_MAX; // Disable top/filler placement
623 vm->m_area.add_y(em, vi, -1);
631 void MapgenFractal::dustTopNodes()
633 if (node_max.Y < water_level)
636 v3s16 em = vm->m_area.getExtent();
639 for (s16 z = node_min.Z; z <= node_max.Z; z++)
640 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
641 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]);
643 if (biome->c_dust == CONTENT_IGNORE)
646 u32 vi = vm->m_area.index(x, full_node_max.Y, z);
647 content_t c_full_max = vm->m_data[vi].getContent();
650 if (c_full_max == CONTENT_AIR) {
651 y_start = full_node_max.Y - 1;
652 } else if (c_full_max == CONTENT_IGNORE) {
653 vi = vm->m_area.index(x, node_max.Y + 1, z);
654 content_t c_max = vm->m_data[vi].getContent();
656 if (c_max == CONTENT_AIR)
657 y_start = node_max.Y;
664 vi = vm->m_area.index(x, y_start, z);
665 for (s16 y = y_start; y >= node_min.Y - 1; y--) {
666 if (vm->m_data[vi].getContent() != CONTENT_AIR)
669 vm->m_area.add_y(em, vi, -1);
672 content_t c = vm->m_data[vi].getContent();
673 if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) {
674 vm->m_area.add_y(em, vi, 1);
675 vm->m_data[vi] = MapNode(biome->c_dust);
681 void MapgenFractal::generateCaves(s16 max_stone_y)
683 if (max_stone_y < node_min.Y)
686 noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
687 noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
689 v3s16 em = vm->m_area.getExtent();
692 for (s16 z = node_min.Z; z <= node_max.Z; z++)
693 for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
694 bool column_is_open = false; // Is column open to overground
695 bool is_tunnel = false; // Is tunnel or tunnel floor
696 u32 vi = vm->m_area.index(x, node_max.Y, z);
697 u32 index3d = (z - node_min.Z) * zstride_1d + csize.Y * ystride +
700 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]);
702 // Don't excavate the overgenerated stone at node_max.Y + 1,
703 // this creates a 'roof' over the tunnel, preventing light in
704 // tunnels at mapchunk borders when generating mapchunks upwards.
705 // This 'roof' is removed when the mapchunk above is generated.
706 for (s16 y = node_max.Y; y >= node_min.Y - 1; y--,
708 vm->m_area.add_y(em, vi, -1)) {
710 content_t c = vm->m_data[vi].getContent();
711 if (c == CONTENT_AIR || c == biome->c_water_top ||
712 c == biome->c_water) {
713 column_is_open = true;
717 float d1 = contour(noise_cave1->result[index3d]);
718 float d2 = contour(noise_cave2->result[index3d]);
720 if (d1 * d2 > cave_width && ndef->get(c).is_ground_content) {
721 // In tunnel and ground content, excavate
722 vm->m_data[vi] = MapNode(CONTENT_AIR);
725 // Not in tunnel or not ground content
726 if (is_tunnel && column_is_open &&
727 (c == biome->c_filler || c == biome->c_stone))
728 // Tunnel entrance floor
729 vm->m_data[vi] = MapNode(biome->c_top);
731 column_is_open = false;
737 if (node_max.Y > MGFRACTAL_LARGE_CAVE_DEPTH)
740 PseudoRandom ps(blockseed + 21343);
741 u32 bruises_count = ps.range(0, 2);
742 for (u32 i = 0; i < bruises_count; i++) {
743 CaveV5 cave(this, &ps);
744 cave.makeCave(node_min, node_max, max_stone_y);