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[] = {
44 {"julia", MGFRACTAL_JULIA},
48 ///////////////////////////////////////////////////////////////////////////////////////
51 MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge)
52 : Mapgen(mapgenid, params, emerge)
54 this->m_emerge = emerge;
55 this->bmgr = emerge->biomemgr;
57 //// amount of elements to skip for the next index
58 //// for noise/height/biome maps (not vmanip)
59 this->ystride = csize.X;
60 this->zstride = csize.X * (csize.Y + 2);
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;
68 this->spflags = sp->spflags;
70 this->formula = sp->formula;
71 this->iterations = sp->iterations;
72 this->scale = sp->scale;
73 this->offset = sp->offset;
74 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 noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 2, csize.Z);
87 noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 2, csize.Z);
90 noise_heat = new Noise(¶ms->np_biome_heat, seed, csize.X, csize.Z);
91 noise_humidity = new Noise(¶ms->np_biome_humidity, seed, csize.X, csize.Z);
92 noise_heat_blend = new Noise(¶ms->np_biome_heat_blend, seed, csize.X, csize.Z);
93 noise_humidity_blend = new Noise(¶ms->np_biome_humidity_blend, seed, csize.X, csize.Z);
95 //// Resolve nodes to be used
96 INodeDefManager *ndef = emerge->ndef;
98 c_stone = ndef->getId("mapgen_stone");
99 c_water_source = ndef->getId("mapgen_water_source");
100 c_lava_source = ndef->getId("mapgen_lava_source");
101 c_desert_stone = ndef->getId("mapgen_desert_stone");
102 c_ice = ndef->getId("mapgen_ice");
103 c_sandstone = ndef->getId("mapgen_sandstone");
105 c_cobble = ndef->getId("mapgen_cobble");
106 c_stair_cobble = ndef->getId("mapgen_stair_cobble");
107 c_mossycobble = ndef->getId("mapgen_mossycobble");
108 c_sandstonebrick = ndef->getId("mapgen_sandstonebrick");
109 c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
111 if (c_ice == CONTENT_IGNORE)
113 if (c_mossycobble == CONTENT_IGNORE)
114 c_mossycobble = c_cobble;
115 if (c_stair_cobble == CONTENT_IGNORE)
116 c_stair_cobble = c_cobble;
117 if (c_sandstonebrick == CONTENT_IGNORE)
118 c_sandstonebrick = c_sandstone;
119 if (c_stair_sandstonebrick == CONTENT_IGNORE)
120 c_stair_sandstonebrick = c_sandstone;
124 MapgenFractal::~MapgenFractal()
127 delete noise_filler_depth;
132 delete noise_humidity;
133 delete noise_heat_blend;
134 delete noise_humidity_blend;
141 MapgenFractalParams::MapgenFractalParams()
147 scale = v3f(1024.0, 256.0, 1024.0);
148 offset = v3f(1.75, 0.0, 0.0);
156 np_seabed = NoiseParams(-14, 9, v3f(600, 600, 600), 41900, 5, 0.6, 2.0);
157 np_filler_depth = NoiseParams(0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0);
158 np_cave1 = NoiseParams(0, 12, v3f(128, 128, 128), 52534, 4, 0.5, 2.0);
159 np_cave2 = NoiseParams(0, 12, v3f(128, 128, 128), 10325, 4, 0.5, 2.0);
163 void MapgenFractalParams::readParams(const Settings *settings)
165 settings->getFlagStrNoEx("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
167 settings->getU16NoEx("mgfractal_formula", formula);
168 settings->getU16NoEx("mgfractal_iterations", iterations);
169 settings->getV3FNoEx("mgfractal_scale", scale);
170 settings->getV3FNoEx("mgfractal_offset", offset);
171 settings->getFloatNoEx("mgfractal_slice_w", slice_w);
173 settings->getFloatNoEx("mgfractal_julia_x", julia_x);
174 settings->getFloatNoEx("mgfractal_julia_y", julia_y);
175 settings->getFloatNoEx("mgfractal_julia_z", julia_z);
176 settings->getFloatNoEx("mgfractal_julia_w", julia_w);
178 settings->getNoiseParams("mgfractal_np_seabed", np_seabed);
179 settings->getNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
180 settings->getNoiseParams("mgfractal_np_cave1", np_cave1);
181 settings->getNoiseParams("mgfractal_np_cave2", np_cave2);
185 void MapgenFractalParams::writeParams(Settings *settings) const
187 settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal, U32_MAX);
189 settings->setU16("mgfractal_formula", formula);
190 settings->setU16("mgfractal_iterations", iterations);
191 settings->setV3F("mgfractal_scale", scale);
192 settings->setV3F("mgfractal_offset", offset);
193 settings->setFloat("mgfractal_slice_w", slice_w);
195 settings->setFloat("mgfractal_julia_x", julia_x);
196 settings->setFloat("mgfractal_julia_y", julia_y);
197 settings->setFloat("mgfractal_julia_z", julia_z);
198 settings->setFloat("mgfractal_julia_w", julia_w);
200 settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
201 settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
202 settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
203 settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
207 /////////////////////////////////////////////////////////////////
210 int MapgenFractal::getGroundLevelAtPoint(v2s16 p)
212 s16 search_start = 128;
213 s16 search_end = -128;
215 for (s16 y = search_start; y >= search_end; y--) {
216 if (getFractalAtPoint(p.X, y, p.Y))
220 return -MAX_MAP_GENERATION_LIMIT;
224 void MapgenFractal::makeChunk(BlockMakeData *data)
227 assert(data->vmanip);
228 assert(data->nodedef);
229 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
230 data->blockpos_requested.Y >= data->blockpos_min.Y &&
231 data->blockpos_requested.Z >= data->blockpos_min.Z);
232 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
233 data->blockpos_requested.Y <= data->blockpos_max.Y &&
234 data->blockpos_requested.Z <= data->blockpos_max.Z);
236 this->generating = true;
237 this->vm = data->vmanip;
238 this->ndef = data->nodedef;
239 TimeTaker t("makeChunk");
241 v3s16 blockpos_min = data->blockpos_min;
242 v3s16 blockpos_max = data->blockpos_max;
243 node_min = blockpos_min * MAP_BLOCKSIZE;
244 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
245 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
246 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
248 blockseed = getBlockSeed2(full_node_min, seed);
253 // Generate base terrain, mountains, and ridges with initial heightmaps
254 s16 stone_surface_max_y = generateTerrain();
257 updateHeightmap(node_min, node_max);
259 // Create biomemap at heightmap surface
260 bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result,
261 noise_humidity->result, heightmap, biomemap);
263 // Actually place the biome-specific nodes
264 MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result);
266 if (flags & MG_CAVES)
267 generateCaves(stone_surface_max_y);
269 if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) {
272 dp.np_rarity = nparams_dungeon_rarity;
273 dp.np_density = nparams_dungeon_density;
274 dp.np_wetness = nparams_dungeon_wetness;
275 dp.c_water = c_water_source;
276 if (stone_type == STONE) {
277 dp.c_cobble = c_cobble;
278 dp.c_moss = c_mossycobble;
279 dp.c_stair = c_stair_cobble;
281 dp.diagonal_dirs = false;
283 dp.holesize = v3s16(1, 2, 1);
284 dp.roomsize = v3s16(0, 0, 0);
285 dp.notifytype = GENNOTIFY_DUNGEON;
286 } else if (stone_type == DESERT_STONE) {
287 dp.c_cobble = c_desert_stone;
288 dp.c_moss = c_desert_stone;
289 dp.c_stair = c_desert_stone;
291 dp.diagonal_dirs = true;
293 dp.holesize = v3s16(2, 3, 2);
294 dp.roomsize = v3s16(2, 5, 2);
295 dp.notifytype = GENNOTIFY_TEMPLE;
296 } else if (stone_type == SANDSTONE) {
297 dp.c_cobble = c_sandstonebrick;
298 dp.c_moss = c_sandstonebrick;
299 dp.c_stair = c_sandstonebrick;
301 dp.diagonal_dirs = false;
303 dp.holesize = v3s16(2, 2, 2);
304 dp.roomsize = v3s16(2, 0, 2);
305 dp.notifytype = GENNOTIFY_DUNGEON;
308 DungeonGen dgen(this, &dp);
309 dgen.generate(blockseed, full_node_min, full_node_max);
312 // Generate the registered decorations
313 if (flags & MG_DECORATIONS)
314 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
316 // Generate the registered ores
317 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
319 // Sprinkle some dust on top after everything else was generated
322 printf("makeChunk: %dms\n", t.stop());
324 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
326 if (flags & MG_LIGHT)
327 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
328 full_node_min, full_node_max);
330 //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
331 // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
333 this->generating = false;
337 void MapgenFractal::calculateNoise()
339 //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
341 int y = node_min.Y - 1;
344 noise_seabed->perlinMap2D(x, z);
345 noise_filler_depth->perlinMap2D(x, z);
347 if (flags & MG_CAVES) {
348 noise_cave1->perlinMap3D(x, y, z);
349 noise_cave2->perlinMap3D(x, y, z);
352 noise_heat->perlinMap2D(x, z);
353 noise_humidity->perlinMap2D(x, z);
354 noise_heat_blend->perlinMap2D(x, z);
355 noise_humidity_blend->perlinMap2D(x, z);
357 for (s32 i = 0; i < csize.X * csize.Z; i++) {
358 noise_heat->result[i] += noise_heat_blend->result[i];
359 noise_humidity->result[i] += noise_humidity_blend->result[i];
362 heatmap = noise_heat->result;
363 humidmap = noise_humidity->result;
364 //printf("calculateNoise: %dus\n", t.stop());
368 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
370 float cx, cy, cz, cw, ox, oy, oz, ow;
372 if (spflags & MGFRACTAL_JULIA) { // Julia set
377 ox = (float)x / scale.X - offset.X;
378 oy = (float)y / scale.Y - offset.Y;
379 oz = (float)z / scale.Z - offset.Z;
381 } else { // Mandelbrot set
382 cx = (float)x / scale.X - offset.X;
383 cy = (float)y / scale.Y - offset.Y;
384 cz = (float)z / scale.Z - offset.Z;
392 for (u16 iter = 0; iter < iterations; iter++) {
398 if (formula == 1) { // 4D "Roundy" Mandelbrot Set
399 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
400 ny = 2.0f * (ox * oy + oz * ow) + cy;
401 nz = 2.0f * (ox * oz + oy * ow) + cz;
402 nw = 2.0f * (ox * ow + oy * oz) + cw;
403 } else if (formula == 2) { // 4D "Squarry" Mandelbrot Set
404 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
405 ny = 2.0f * (ox * oy + oz * ow) + cy;
406 nz = 2.0f * (ox * oz + oy * ow) + cz;
407 nw = 2.0f * (ox * ow - oy * oz) + cw;
408 } else if (formula == 3) { // 4D "Mandy Cousin" Mandelbrot Set
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 == 4) { // 4D Mandelbrot Set Variation
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;
420 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
433 s16 MapgenFractal::generateTerrain()
435 MapNode n_air(CONTENT_AIR);
436 MapNode n_stone(c_stone);
437 MapNode n_water(c_water_source);
439 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
442 for (s16 z = node_min.Z; z <= node_max.Z; z++) {
443 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
444 u32 vi = vm->m_area.index(node_min.X, y, z);
445 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
446 if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
447 s16 seabed_height = noise_seabed->result[index2d];
449 if (y <= seabed_height || getFractalAtPoint(x, y, z)) {
450 vm->m_data[vi] = n_stone;
451 if (y > stone_surface_max_y)
452 stone_surface_max_y = y;
453 } else if (y <= water_level) {
454 vm->m_data[vi] = n_water;
456 vm->m_data[vi] = n_air;
465 return stone_surface_max_y;
469 MgStoneType MapgenFractal::generateBiomes(float *heat_map, float *humidity_map)
471 v3s16 em = vm->m_area.getExtent();
473 MgStoneType stone_type = STONE;
475 for (s16 z = node_min.Z; z <= node_max.Z; z++)
476 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
480 u16 depth_water_top = 0;
481 u32 vi = vm->m_area.index(x, node_max.Y, z);
483 // Check node at base of mapchunk above, either a node of a previously
484 // generated mapchunk or if not, a node of overgenerated base terrain.
485 content_t c_above = vm->m_data[vi + em.X].getContent();
486 bool air_above = c_above == CONTENT_AIR;
487 bool water_above = c_above == c_water_source;
489 // If there is air or water above enable top/filler placement, otherwise force
490 // nplaced to stone level by setting a number exceeding any possible filler depth.
491 u16 nplaced = (air_above || water_above) ? 0 : U16_MAX;
494 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
495 content_t c = vm->m_data[vi].getContent();
497 // Biome is recalculated each time an upper surface is detected while
498 // working down a column. The selected biome then remains in effect for
499 // all nodes below until the next surface and biome recalculation.
500 // Biome is recalculated:
501 // 1. At the surface of stone below air or water.
502 // 2. At the surface of water below air.
503 // 3. When stone or water is detected but biome has not yet been calculated.
504 if ((c == c_stone && (air_above || water_above || !biome)) ||
505 (c == c_water_source && (air_above || !biome))) {
506 biome = bmgr->getBiome(heat_map[index], humidity_map[index], y);
507 depth_top = biome->depth_top;
508 base_filler = MYMAX(depth_top + biome->depth_filler
509 + noise_filler_depth->result[index], 0);
510 depth_water_top = biome->depth_water_top;
512 // Detect stone type for dungeons during every biome calculation.
513 // This is more efficient than detecting per-node and will not
514 // miss any desert stone or sandstone biomes.
515 if (biome->c_stone == c_desert_stone)
516 stone_type = DESERT_STONE;
517 else if (biome->c_stone == c_sandstone)
518 stone_type = SANDSTONE;
522 content_t c_below = vm->m_data[vi - em.X].getContent();
524 // If the node below isn't solid, make this node stone, so that
525 // any top/filler nodes above are structurally supported.
526 // This is done by aborting the cycle of top/filler placement
527 // immediately by forcing nplaced to stone level.
528 if (c_below == CONTENT_AIR || c_below == c_water_source)
531 if (nplaced < depth_top) {
532 vm->m_data[vi] = MapNode(biome->c_top);
534 } else if (nplaced < base_filler) {
535 vm->m_data[vi] = MapNode(biome->c_filler);
538 vm->m_data[vi] = MapNode(biome->c_stone);
543 } else if (c == c_water_source) {
544 vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ?
545 biome->c_water_top : biome->c_water);
546 nplaced = 0; // Enable top/filler placement for next surface
549 } else if (c == CONTENT_AIR) {
550 nplaced = 0; // Enable top/filler placement for next surface
553 } else { // Possible various nodes overgenerated from neighbouring mapchunks
554 nplaced = U16_MAX; // Disable top/filler placement
559 vm->m_area.add_y(em, vi, -1);
567 void MapgenFractal::dustTopNodes()
569 if (node_max.Y < water_level)
572 v3s16 em = vm->m_area.getExtent();
575 for (s16 z = node_min.Z; z <= node_max.Z; z++)
576 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
577 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]);
579 if (biome->c_dust == CONTENT_IGNORE)
582 u32 vi = vm->m_area.index(x, full_node_max.Y, z);
583 content_t c_full_max = vm->m_data[vi].getContent();
586 if (c_full_max == CONTENT_AIR) {
587 y_start = full_node_max.Y - 1;
588 } else if (c_full_max == CONTENT_IGNORE) {
589 vi = vm->m_area.index(x, node_max.Y + 1, z);
590 content_t c_max = vm->m_data[vi].getContent();
592 if (c_max == CONTENT_AIR)
593 y_start = node_max.Y;
600 vi = vm->m_area.index(x, y_start, z);
601 for (s16 y = y_start; y >= node_min.Y - 1; y--) {
602 if (vm->m_data[vi].getContent() != CONTENT_AIR)
605 vm->m_area.add_y(em, vi, -1);
608 content_t c = vm->m_data[vi].getContent();
609 if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) {
610 vm->m_area.add_y(em, vi, 1);
611 vm->m_data[vi] = MapNode(biome->c_dust);
617 void MapgenFractal::generateCaves(s16 max_stone_y)
619 if (max_stone_y >= node_min.Y) {
622 for (s16 z = node_min.Z; z <= node_max.Z; z++)
623 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
624 u32 vi = vm->m_area.index(node_min.X, y, z);
625 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index++) {
626 float d1 = contour(noise_cave1->result[index]);
627 float d2 = contour(noise_cave2->result[index]);
628 if (d1 * d2 > 0.4f) {
629 content_t c = vm->m_data[vi].getContent();
630 if (!ndef->get(c).is_ground_content || c == CONTENT_AIR)
633 vm->m_data[vi] = MapNode(CONTENT_AIR);
639 if (node_max.Y > MGFRACTAL_LARGE_CAVE_DEPTH)
642 PseudoRandom ps(blockseed + 21343);
643 u32 bruises_count = (ps.range(1, 4) == 1) ? ps.range(1, 2) : 0;
644 for (u32 i = 0; i < bruises_count; i++) {
645 CaveV5 cave(this, &ps);
646 cave.makeCave(node_min, node_max, max_stone_y);