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 "settings.h" // For g_settings
33 #include "dungeongen.h"
38 #include "mg_decoration.h"
39 #include "mapgen_fractal.h"
42 FlagDesc flagdesc_mapgen_fractal[] = {
43 {"julia", MGFRACTAL_JULIA},
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 this->zstride = csize.X * (csize.Y + 2);
61 this->biomemap = new u8[csize.X * csize.Z];
62 this->heightmap = new s16[csize.X * csize.Z];
64 this->humidmap = NULL;
66 MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams;
67 this->spflags = sp->spflags;
68 this->iterations = sp->iterations;
69 this->scale_x = sp->scale_x;
70 this->scale_y = sp->scale_y;
71 this->scale_z = sp->scale_z;
72 this->offset_x = sp->offset_x;
73 this->offset_y = sp->offset_y;
74 this->offset_z = sp->offset_z;
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);
85 noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 2, csize.Z);
86 noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 2, csize.Z);
89 noise_heat = new Noise(¶ms->np_biome_heat, seed, csize.X, csize.Z);
90 noise_humidity = new Noise(¶ms->np_biome_humidity, seed, csize.X, csize.Z);
91 noise_heat_blend = new Noise(¶ms->np_biome_heat_blend, seed, csize.X, csize.Z);
92 noise_humidity_blend = new Noise(¶ms->np_biome_humidity_blend, seed, csize.X, csize.Z);
94 //// Resolve nodes to be used
95 INodeDefManager *ndef = emerge->ndef;
97 c_stone = ndef->getId("mapgen_stone");
98 c_water_source = ndef->getId("mapgen_water_source");
99 c_lava_source = ndef->getId("mapgen_lava_source");
100 c_desert_stone = ndef->getId("mapgen_desert_stone");
101 c_ice = ndef->getId("mapgen_ice");
102 c_sandstone = ndef->getId("mapgen_sandstone");
104 c_cobble = ndef->getId("mapgen_cobble");
105 c_stair_cobble = ndef->getId("mapgen_stair_cobble");
106 c_mossycobble = ndef->getId("mapgen_mossycobble");
107 c_sandstonebrick = ndef->getId("mapgen_sandstonebrick");
108 c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
110 if (c_ice == CONTENT_IGNORE)
112 if (c_mossycobble == CONTENT_IGNORE)
113 c_mossycobble = c_cobble;
114 if (c_stair_cobble == CONTENT_IGNORE)
115 c_stair_cobble = c_cobble;
116 if (c_sandstonebrick == CONTENT_IGNORE)
117 c_sandstonebrick = c_sandstone;
118 if (c_stair_sandstonebrick == CONTENT_IGNORE)
119 c_stair_sandstonebrick = c_sandstone;
123 MapgenFractal::~MapgenFractal()
131 delete noise_humidity;
132 delete noise_heat_blend;
133 delete noise_humidity_blend;
140 MapgenFractalParams::MapgenFractalParams()
157 np_seabed = NoiseParams(-14, 9, v3f(600, 600, 600), 41900, 5, 0.6, 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_iterations", iterations);
168 settings->getFloatNoEx("mgfractal_scale_x", scale_x);
169 settings->getFloatNoEx("mgfractal_scale_y", scale_y);
170 settings->getFloatNoEx("mgfractal_scale_z", scale_z);
171 settings->getFloatNoEx("mgfractal_offset_x", offset_x);
172 settings->getFloatNoEx("mgfractal_offset_y", offset_y);
173 settings->getFloatNoEx("mgfractal_offset_z", offset_z);
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_cave1", np_cave1);
182 settings->getNoiseParams("mgfractal_np_cave2", np_cave2);
186 void MapgenFractalParams::writeParams(Settings *settings) const
188 settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal, U32_MAX);
190 settings->setU16("mgfractal_iterations", iterations);
191 settings->setFloat("mgfractal_scale_x", scale_x);
192 settings->setFloat("mgfractal_scale_y", scale_y);
193 settings->setFloat("mgfractal_scale_z", scale_z);
194 settings->setFloat("mgfractal_offset_x", offset_x);
195 settings->setFloat("mgfractal_offset_y", offset_y);
196 settings->setFloat("mgfractal_offset_z", offset_z);
197 settings->setFloat("mgfractal_slice_w", slice_w);
198 settings->setFloat("mgfractal_julia_x", julia_x);
199 settings->setFloat("mgfractal_julia_y", julia_y);
200 settings->setFloat("mgfractal_julia_z", julia_z);
201 settings->setFloat("mgfractal_julia_w", julia_w);
203 settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
204 settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
205 settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
209 /////////////////////////////////////////////////////////////////
212 int MapgenFractal::getGroundLevelAtPoint(v2s16 p)
214 s16 search_start = 128;
215 s16 search_end = -128;
217 for (s16 y = search_start; y >= search_end; y--) {
218 if (getFractalAtPoint(p.X, y, p.Y))
222 return -MAX_MAP_GENERATION_LIMIT;
226 void MapgenFractal::makeChunk(BlockMakeData *data)
229 assert(data->vmanip);
230 assert(data->nodedef);
231 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
232 data->blockpos_requested.Y >= data->blockpos_min.Y &&
233 data->blockpos_requested.Z >= data->blockpos_min.Z);
234 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
235 data->blockpos_requested.Y <= data->blockpos_max.Y &&
236 data->blockpos_requested.Z <= data->blockpos_max.Z);
238 this->generating = true;
239 this->vm = data->vmanip;
240 this->ndef = data->nodedef;
241 //TimeTaker t("makeChunk");
243 v3s16 blockpos_min = data->blockpos_min;
244 v3s16 blockpos_max = data->blockpos_max;
245 node_min = blockpos_min * MAP_BLOCKSIZE;
246 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
247 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
248 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
250 blockseed = getBlockSeed2(full_node_min, seed);
255 // Generate base terrain, mountains, and ridges with initial heightmaps
256 s16 stone_surface_max_y = generateTerrain();
259 updateHeightmap(node_min, node_max);
261 // Create biomemap at heightmap surface
262 bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result,
263 noise_humidity->result, heightmap, biomemap);
265 // Actually place the biome-specific nodes
266 MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result);
268 if (flags & MG_CAVES)
269 generateCaves(stone_surface_max_y);
271 if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) {
274 dp.np_rarity = nparams_dungeon_rarity;
275 dp.np_density = nparams_dungeon_density;
276 dp.np_wetness = nparams_dungeon_wetness;
277 dp.c_water = c_water_source;
278 if (stone_type == STONE) {
279 dp.c_cobble = c_cobble;
280 dp.c_moss = c_mossycobble;
281 dp.c_stair = c_stair_cobble;
283 dp.diagonal_dirs = false;
285 dp.holesize = v3s16(1, 2, 1);
286 dp.roomsize = v3s16(0, 0, 0);
287 dp.notifytype = GENNOTIFY_DUNGEON;
288 } else if (stone_type == DESERT_STONE) {
289 dp.c_cobble = c_desert_stone;
290 dp.c_moss = c_desert_stone;
291 dp.c_stair = c_desert_stone;
293 dp.diagonal_dirs = true;
295 dp.holesize = v3s16(2, 3, 2);
296 dp.roomsize = v3s16(2, 5, 2);
297 dp.notifytype = GENNOTIFY_TEMPLE;
298 } else if (stone_type == SANDSTONE) {
299 dp.c_cobble = c_sandstonebrick;
300 dp.c_moss = c_sandstonebrick;
301 dp.c_stair = c_sandstonebrick;
303 dp.diagonal_dirs = false;
305 dp.holesize = v3s16(2, 2, 2);
306 dp.roomsize = v3s16(2, 0, 2);
307 dp.notifytype = GENNOTIFY_DUNGEON;
310 DungeonGen dgen(this, &dp);
311 dgen.generate(blockseed, full_node_min, full_node_max);
314 // Generate the registered decorations
315 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
317 // Generate the registered ores
318 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
320 // Sprinkle some dust on top after everything else was generated
323 //printf("makeChunk: %dms\n", t.stop());
325 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
327 if (flags & MG_LIGHT)
328 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
329 full_node_min, full_node_max);
331 //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
332 // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
334 this->generating = false;
338 void MapgenFractal::calculateNoise()
340 //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
342 int y = node_min.Y - 1;
345 noise_seabed->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++) {
393 // 4D "Roundy" Mandelbrot set
394 float nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
395 float ny = 2.0f * (ox * oy + oz * ow) + cy;
396 float nz = 2.0f * (ox * oz + oy * ow) + cz;
397 float nw = 2.0f * (ox * ow + oy * oz) + cw;
399 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
412 s16 MapgenFractal::generateTerrain()
414 MapNode n_air(CONTENT_AIR);
415 MapNode n_stone(c_stone);
416 MapNode n_water(c_water_source);
418 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
421 for (s16 z = node_min.Z; z <= node_max.Z; z++) {
422 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
423 u32 vi = vm->m_area.index(node_min.X, y, z);
424 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
425 if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
426 s16 seabed_height = noise_seabed->result[index2d];
428 if (y <= seabed_height || getFractalAtPoint(x, y, z)) {
429 vm->m_data[vi] = n_stone;
430 if (y > stone_surface_max_y)
431 stone_surface_max_y = y;
432 } else if (y <= water_level) {
433 vm->m_data[vi] = n_water;
435 vm->m_data[vi] = n_air;
444 return stone_surface_max_y;
448 MgStoneType MapgenFractal::generateBiomes(float *heat_map, float *humidity_map)
450 v3s16 em = vm->m_area.getExtent();
452 MgStoneType stone_type = STONE;
454 for (s16 z = node_min.Z; z <= node_max.Z; z++)
455 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
459 u16 depth_water_top = 0;
460 u32 vi = vm->m_area.index(x, node_max.Y, z);
462 // Check node at base of mapchunk above, either a node of a previously
463 // generated mapchunk or if not, a node of overgenerated base terrain.
464 content_t c_above = vm->m_data[vi + em.X].getContent();
465 bool air_above = c_above == CONTENT_AIR;
466 bool water_above = c_above == c_water_source;
468 // If there is air or water above enable top/filler placement, otherwise force
469 // nplaced to stone level by setting a number exceeding any possible filler depth.
470 u16 nplaced = (air_above || water_above) ? 0 : U16_MAX;
473 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
474 content_t c = vm->m_data[vi].getContent();
476 // Biome is recalculated each time an upper surface is detected while
477 // working down a column. The selected biome then remains in effect for
478 // all nodes below until the next surface and biome recalculation.
479 // Biome is recalculated:
480 // 1. At the surface of stone below air or water.
481 // 2. At the surface of water below air.
482 // 3. When stone or water is detected but biome has not yet been calculated.
483 if ((c == c_stone && (air_above || water_above || !biome)) ||
484 (c == c_water_source && (air_above || !biome))) {
485 biome = bmgr->getBiome(heat_map[index], humidity_map[index], y);
486 depth_top = biome->depth_top;
487 base_filler = depth_top + biome->depth_filler;
488 depth_water_top = biome->depth_water_top;
490 // Detect stone type for dungeons during every biome calculation.
491 // This is more efficient than detecting per-node and will not
492 // miss any desert stone or sandstone biomes.
493 if (biome->c_stone == c_desert_stone)
494 stone_type = DESERT_STONE;
495 else if (biome->c_stone == c_sandstone)
496 stone_type = SANDSTONE;
500 content_t c_below = vm->m_data[vi - em.X].getContent();
502 // If the node below isn't solid, make this node stone, so that
503 // any top/filler nodes above are structurally supported.
504 // This is done by aborting the cycle of top/filler placement
505 // immediately by forcing nplaced to stone level.
506 if (c_below == CONTENT_AIR || c_below == c_water_source)
509 if (nplaced < depth_top) {
510 vm->m_data[vi] = MapNode(biome->c_top);
512 } else if (nplaced < base_filler) {
513 vm->m_data[vi] = MapNode(biome->c_filler);
516 vm->m_data[vi] = MapNode(biome->c_stone);
521 } else if (c == c_water_source) {
522 vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ?
523 biome->c_water_top : biome->c_water);
524 nplaced = 0; // Enable top/filler placement for next surface
527 } else if (c == CONTENT_AIR) {
528 nplaced = 0; // Enable top/filler placement for next surface
531 } else { // Possible various nodes overgenerated from neighbouring mapchunks
532 nplaced = U16_MAX; // Disable top/filler placement
537 vm->m_area.add_y(em, vi, -1);
545 void MapgenFractal::dustTopNodes()
547 if (node_max.Y < water_level)
550 v3s16 em = vm->m_area.getExtent();
553 for (s16 z = node_min.Z; z <= node_max.Z; z++)
554 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
555 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]);
557 if (biome->c_dust == CONTENT_IGNORE)
560 u32 vi = vm->m_area.index(x, full_node_max.Y, z);
561 content_t c_full_max = vm->m_data[vi].getContent();
564 if (c_full_max == CONTENT_AIR) {
565 y_start = full_node_max.Y - 1;
566 } else if (c_full_max == CONTENT_IGNORE) {
567 vi = vm->m_area.index(x, node_max.Y + 1, z);
568 content_t c_max = vm->m_data[vi].getContent();
570 if (c_max == CONTENT_AIR)
571 y_start = node_max.Y;
578 vi = vm->m_area.index(x, y_start, z);
579 for (s16 y = y_start; y >= node_min.Y - 1; y--) {
580 if (vm->m_data[vi].getContent() != CONTENT_AIR)
583 vm->m_area.add_y(em, vi, -1);
586 content_t c = vm->m_data[vi].getContent();
587 if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) {
588 vm->m_area.add_y(em, vi, 1);
589 vm->m_data[vi] = MapNode(biome->c_dust);
595 void MapgenFractal::generateCaves(s16 max_stone_y)
597 if (max_stone_y >= node_min.Y) {
600 for (s16 z = node_min.Z; z <= node_max.Z; z++)
601 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
602 u32 vi = vm->m_area.index(node_min.X, y, z);
603 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index++) {
604 float d1 = contour(noise_cave1->result[index]);
605 float d2 = contour(noise_cave2->result[index]);
607 content_t c = vm->m_data[vi].getContent();
608 if (!ndef->get(c).is_ground_content || c == CONTENT_AIR)
611 vm->m_data[vi] = MapNode(CONTENT_AIR);
617 if (node_max.Y > MGFRACTAL_LARGE_CAVE_DEPTH)
620 PseudoRandom ps(blockseed + 21343);
621 u32 bruises_count = (ps.range(1, 4) == 1) ? ps.range(1, 2) : 0;
622 for (u32 i = 0; i < bruises_count; i++) {
623 CaveFractal cave(this, &ps);
624 cave.makeCave(node_min, node_max, max_stone_y);