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 : MapgenBasic(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->heightmap = new s16[csize.X * csize.Z];
64 MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams;
66 this->spflags = sp->spflags;
67 this->cave_width = sp->cave_width;
68 this->fractal = sp->fractal;
69 this->iterations = sp->iterations;
70 this->scale = sp->scale;
71 this->offset = sp->offset;
72 this->slice_w = sp->slice_w;
73 this->julia_x = sp->julia_x;
74 this->julia_y = sp->julia_y;
75 this->julia_z = sp->julia_z;
76 this->julia_w = sp->julia_w;
79 noise_seabed = new Noise(&sp->np_seabed, seed, csize.X, csize.Z);
80 noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z);
83 // 1-down overgeneraion
84 noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z);
85 noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z);
87 //// Initialize biome generator
88 biomegen = emerge->biomemgr->createBiomeGen(
89 BIOMEGEN_ORIGINAL, params->bparams, csize);
90 biomemap = biomegen->biomemap;
92 this->formula = fractal / 2 + fractal % 2;
93 this->julia = fractal % 2 == 0;
95 //// Resolve nodes to be used
96 c_stone = ndef->getId("mapgen_stone");
97 c_water_source = ndef->getId("mapgen_water_source");
98 c_lava_source = ndef->getId("mapgen_lava_source");
99 c_desert_stone = ndef->getId("mapgen_desert_stone");
100 c_ice = ndef->getId("mapgen_ice");
101 c_sandstone = ndef->getId("mapgen_sandstone");
103 c_cobble = ndef->getId("mapgen_cobble");
104 c_stair_cobble = ndef->getId("mapgen_stair_cobble");
105 c_mossycobble = ndef->getId("mapgen_mossycobble");
106 c_sandstonebrick = ndef->getId("mapgen_sandstonebrick");
107 c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
109 if (c_ice == CONTENT_IGNORE)
111 if (c_mossycobble == CONTENT_IGNORE)
112 c_mossycobble = c_cobble;
113 if (c_stair_cobble == CONTENT_IGNORE)
114 c_stair_cobble = c_cobble;
115 if (c_sandstonebrick == CONTENT_IGNORE)
116 c_sandstonebrick = c_sandstone;
117 if (c_stair_sandstonebrick == CONTENT_IGNORE)
118 c_stair_sandstonebrick = c_sandstone;
122 MapgenFractal::~MapgenFractal()
125 delete noise_filler_depth;
135 MapgenFractalParams::MapgenFractalParams()
141 scale = v3f(4096.0, 1024.0, 4096.0);
142 offset = v3f(1.79, 0.0, 0.0);
149 np_seabed = NoiseParams(-14, 9, v3f(600, 600, 600), 41900, 5, 0.6, 2.0);
150 np_filler_depth = NoiseParams(0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0);
151 np_cave1 = NoiseParams(0, 12, v3f(96, 96, 96), 52534, 4, 0.5, 2.0);
152 np_cave2 = NoiseParams(0, 12, v3f(96, 96, 96), 10325, 4, 0.5, 2.0);
156 void MapgenFractalParams::readParams(const Settings *settings)
158 settings->getFlagStrNoEx("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
159 settings->getFloatNoEx("mgfractal_cave_width", cave_width);
160 settings->getU16NoEx("mgfractal_fractal", fractal);
161 settings->getU16NoEx("mgfractal_iterations", iterations);
162 settings->getV3FNoEx("mgfractal_scale", scale);
163 settings->getV3FNoEx("mgfractal_offset", offset);
164 settings->getFloatNoEx("mgfractal_slice_w", slice_w);
165 settings->getFloatNoEx("mgfractal_julia_x", julia_x);
166 settings->getFloatNoEx("mgfractal_julia_y", julia_y);
167 settings->getFloatNoEx("mgfractal_julia_z", julia_z);
168 settings->getFloatNoEx("mgfractal_julia_w", julia_w);
170 settings->getNoiseParams("mgfractal_np_seabed", np_seabed);
171 settings->getNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
172 settings->getNoiseParams("mgfractal_np_cave1", np_cave1);
173 settings->getNoiseParams("mgfractal_np_cave2", np_cave2);
177 void MapgenFractalParams::writeParams(Settings *settings) const
179 settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal, U32_MAX);
180 settings->setFloat("mgfractal_cave_width", cave_width);
181 settings->setU16("mgfractal_fractal", fractal);
182 settings->setU16("mgfractal_iterations", iterations);
183 settings->setV3F("mgfractal_scale", scale);
184 settings->setV3F("mgfractal_offset", offset);
185 settings->setFloat("mgfractal_slice_w", slice_w);
186 settings->setFloat("mgfractal_julia_x", julia_x);
187 settings->setFloat("mgfractal_julia_y", julia_y);
188 settings->setFloat("mgfractal_julia_z", julia_z);
189 settings->setFloat("mgfractal_julia_w", julia_w);
191 settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
192 settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
193 settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
194 settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
198 /////////////////////////////////////////////////////////////////
201 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
203 bool solid_below = false; // Dry solid node is present below to spawn on
204 u8 air_count = 0; // Consecutive air nodes above the dry solid node
205 s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
206 // Seabed can rise above water_level or might be raised to create dry land
207 s16 search_start = MYMAX(seabed_level, water_level + 1);
208 if (seabed_level > water_level)
211 for (s16 y = search_start; y <= search_start + 128; y++) {
212 if (getFractalAtPoint(p.X, y, p.Y)) { // Fractal node
215 } else if (solid_below) { // Air above solid node
222 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
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 // Init biome generator, place biome-specific nodes, and build biomemap
262 biomegen->calcBiomeNoise(node_min);
263 biomegen->getBiomes(heightmap);
264 MgStoneType stone_type = generateBiomes();
266 if (flags & MG_CAVES)
267 generateCaves(stone_surface_max_y, MGFRACTAL_LARGE_CAVE_DEPTH);
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);
343 noise_seabed->perlinMap2D(x, z);
345 // Cave noises are calculated in generateCaves()
346 // only if solid terrain is present in mapchunk
348 noise_filler_depth->perlinMap2D(x, z);
350 //printf("calculateNoise: %dus\n", t.stop());
354 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
356 float cx, cy, cz, cw, ox, oy, oz, ow;
358 if (julia) { // Julia set
363 ox = (float)x / scale.X - offset.X;
364 oy = (float)y / scale.Y - offset.Y;
365 oz = (float)z / scale.Z - offset.Z;
367 } else { // Mandelbrot set
368 cx = (float)x / scale.X - offset.X;
369 cy = (float)y / scale.Y - offset.Y;
370 cz = (float)z / scale.Z - offset.Z;
383 for (u16 iter = 0; iter < iterations; iter++) {
385 if (formula == 1) { // 4D "Roundy"
386 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
387 ny = 2.0f * (ox * oy + oz * ow) + cy;
388 nz = 2.0f * (ox * oz + oy * ow) + cz;
389 nw = 2.0f * (ox * ow + oy * oz) + cw;
390 } else if (formula == 2) { // 4D "Squarry"
391 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
392 ny = 2.0f * (ox * oy + oz * ow) + cy;
393 nz = 2.0f * (ox * oz + oy * ow) + cz;
394 nw = 2.0f * (ox * ow - oy * oz) + cw;
395 } else if (formula == 3) { // 4D "Mandy Cousin"
396 nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
397 ny = 2.0f * (ox * oy + oz * ow) + cy;
398 nz = 2.0f * (ox * oz + oy * ow) + cz;
399 nw = 2.0f * (ox * ow + oy * oz) + cw;
400 } else if (formula == 4) { // 4D "Variation"
401 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
402 ny = 2.0f * (ox * oy + oz * ow) + cy;
403 nz = 2.0f * (ox * oz - oy * ow) + cz;
404 nw = 2.0f * (ox * ow + oy * oz) + cw;
405 } else if (formula == 5) { // 3D "Mandelbrot/Mandelbar"
406 nx = ox * ox - oy * oy - oz * oz + cx;
407 ny = 2.0f * ox * oy + cy;
408 nz = -2.0f * ox * oz + cz;
409 } else if (formula == 6) { // 3D "Christmas Tree"
410 // Altering the formula here is necessary to avoid division by zero
411 if (fabs(oz) < 0.000000001f) {
412 nx = ox * ox - oy * oy - oz * oz + cx;
413 ny = 2.0f * oy * ox + cy;
414 nz = 4.0f * oz * ox + cz;
416 float a = (2.0f * ox) / (sqrt(oy * oy + oz * oz));
417 nx = ox * ox - oy * oy - oz * oz + cx;
418 ny = a * (oy * oy - oz * oz) + cy;
419 nz = a * 2.0f * oy * oz + cz;
421 } else if (formula == 7) { // 3D "Mandelbulb"
422 if (fabs(oy) < 0.000000001f) {
423 nx = ox * ox - oz * oz + cx;
425 nz = -2.0f * oz * sqrt(ox * ox) + cz;
427 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
428 nx = (ox * ox - oy * oy) * a + cx;
429 ny = 2.0f * ox * oy * a + cy;
430 nz = -2.0f * oz * sqrt(ox * ox + oy * oy) + cz;
432 } else if (formula == 8) { // 3D "Cosine Mandelbulb"
433 if (fabs(oy) < 0.000000001f) {
434 nx = 2.0f * ox * oz + cx;
435 ny = 4.0f * oy * oz + cy;
436 nz = oz * oz - ox * ox - oy * oy + cz;
438 float a = (2.0f * oz) / sqrt(ox * ox + oy * oy);
439 nx = (ox * ox - oy * oy) * a + cx;
440 ny = 2.0f * ox * oy * a + cy;
441 nz = oz * oz - ox * ox - oy * oy + cz;
443 } else if (formula == 9) { // 4D "Mandelbulb"
444 float rxy = sqrt(ox * ox + oy * oy);
445 float rxyz = sqrt(ox * ox + oy * oy + oz * oz);
446 if (fabs(ow) < 0.000000001f && fabs(oz) < 0.000000001f) {
447 nx = (ox * ox - oy * oy) + cx;
448 ny = 2.0f * ox * oy + cy;
449 nz = -2.0f * rxy * oz + cz;
450 nw = 2.0f * rxyz * ow + cw;
452 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
453 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
454 nx = (ox * ox - oy * oy) * b + cx;
455 ny = 2.0f * ox * oy * b + cy;
456 nz = -2.0f * rxy * oz * a + cz;
457 nw = 2.0f * rxyz * ow + cw;
461 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
474 s16 MapgenFractal::generateTerrain()
476 MapNode n_air(CONTENT_AIR);
477 MapNode n_stone(c_stone);
478 MapNode n_water(c_water_source);
480 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
483 for (s16 z = node_min.Z; z <= node_max.Z; z++) {
484 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
485 u32 vi = vm->m_area.index(node_min.X, y, z);
486 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
487 if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
488 s16 seabed_height = noise_seabed->result[index2d];
490 if (y <= seabed_height || getFractalAtPoint(x, y, z)) {
491 vm->m_data[vi] = n_stone;
492 if (y > stone_surface_max_y)
493 stone_surface_max_y = y;
494 } else if (y <= water_level) {
495 vm->m_data[vi] = n_water;
497 vm->m_data[vi] = n_air;
506 return stone_surface_max_y;