3 Copyright (C) 2015-2019 paramat
4 Copyright (C) 2015-2016 kwolekr, Ryan Kwolek
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.
29 #include "content_sao.h"
31 #include "voxelalgorithms.h"
32 //#include "profiler.h" // For TimeTaker
33 #include "settings.h" // For g_settings
35 #include "dungeongen.h"
39 #include "mg_decoration.h"
40 #include "mapgen_fractal.h"
43 FlagDesc flagdesc_mapgen_fractal[] = {
44 {"terrain", MGFRACTAL_TERRAIN},
48 ///////////////////////////////////////////////////////////////////////////////////////
51 MapgenFractal::MapgenFractal(MapgenFractalParams *params, EmergeManager *emerge)
52 : MapgenBasic(MAPGEN_FRACTAL, params, emerge)
54 spflags = params->spflags;
55 cave_width = params->cave_width;
56 large_cave_depth = params->large_cave_depth;
57 small_cave_num_min = params->small_cave_num_min;
58 small_cave_num_max = params->small_cave_num_max;
59 large_cave_num_min = params->large_cave_num_min;
60 large_cave_num_max = params->large_cave_num_max;
61 large_cave_flooded = params->large_cave_flooded;
62 dungeon_ymin = params->dungeon_ymin;
63 dungeon_ymax = params->dungeon_ymax;
64 fractal = params->fractal;
65 iterations = params->iterations;
66 scale = params->scale;
67 offset = params->offset;
68 slice_w = params->slice_w;
69 julia_x = params->julia_x;
70 julia_y = params->julia_y;
71 julia_z = params->julia_z;
72 julia_w = params->julia_w;
75 if (spflags & MGFRACTAL_TERRAIN)
76 noise_seabed = new Noise(¶ms->np_seabed, seed, csize.X, csize.Z);
78 noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z);
81 MapgenBasic::np_dungeons = params->np_dungeons;
82 // Overgeneration to node_min.Y - 1
83 MapgenBasic::np_cave1 = params->np_cave1;
84 MapgenBasic::np_cave2 = params->np_cave2;
86 formula = fractal / 2 + fractal % 2;
87 julia = fractal % 2 == 0;
91 MapgenFractal::~MapgenFractal()
96 delete noise_filler_depth;
100 MapgenFractalParams::MapgenFractalParams():
101 np_seabed (-14, 9, v3f(600, 600, 600), 41900, 5, 0.6, 2.0),
102 np_filler_depth (0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0),
103 np_cave1 (0, 12, v3f(61, 61, 61), 52534, 3, 0.5, 2.0),
104 np_cave2 (0, 12, v3f(67, 67, 67), 10325, 3, 0.5, 2.0),
105 np_dungeons (0.9, 0.5, v3f(500, 500, 500), 0, 2, 0.8, 2.0)
110 void MapgenFractalParams::readParams(const Settings *settings)
112 settings->getFlagStrNoEx("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
113 settings->getFloatNoEx("mgfractal_cave_width", cave_width);
114 settings->getS16NoEx("mgfractal_large_cave_depth", large_cave_depth);
115 settings->getU16NoEx("mgfractal_small_cave_num_min", small_cave_num_min);
116 settings->getU16NoEx("mgfractal_small_cave_num_max", small_cave_num_max);
117 settings->getU16NoEx("mgfractal_large_cave_num_min", large_cave_num_min);
118 settings->getU16NoEx("mgfractal_large_cave_num_max", large_cave_num_max);
119 settings->getFloatNoEx("mgfractal_large_cave_flooded", large_cave_flooded);
120 settings->getS16NoEx("mgfractal_dungeon_ymin", dungeon_ymin);
121 settings->getS16NoEx("mgfractal_dungeon_ymax", dungeon_ymax);
122 settings->getU16NoEx("mgfractal_fractal", fractal);
123 settings->getU16NoEx("mgfractal_iterations", iterations);
124 settings->getV3FNoEx("mgfractal_scale", scale);
125 settings->getV3FNoEx("mgfractal_offset", offset);
126 settings->getFloatNoEx("mgfractal_slice_w", slice_w);
127 settings->getFloatNoEx("mgfractal_julia_x", julia_x);
128 settings->getFloatNoEx("mgfractal_julia_y", julia_y);
129 settings->getFloatNoEx("mgfractal_julia_z", julia_z);
130 settings->getFloatNoEx("mgfractal_julia_w", julia_w);
132 settings->getNoiseParams("mgfractal_np_seabed", np_seabed);
133 settings->getNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
134 settings->getNoiseParams("mgfractal_np_cave1", np_cave1);
135 settings->getNoiseParams("mgfractal_np_cave2", np_cave2);
136 settings->getNoiseParams("mgfractal_np_dungeons", np_dungeons);
140 void MapgenFractalParams::writeParams(Settings *settings) const
142 settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
143 settings->setFloat("mgfractal_cave_width", cave_width);
144 settings->setS16("mgfractal_large_cave_depth", large_cave_depth);
145 settings->setU16("mgfractal_small_cave_num_min", small_cave_num_min);
146 settings->setU16("mgfractal_small_cave_num_max", small_cave_num_max);
147 settings->setU16("mgfractal_large_cave_num_min", large_cave_num_min);
148 settings->setU16("mgfractal_large_cave_num_max", large_cave_num_max);
149 settings->setFloat("mgfractal_large_cave_flooded", large_cave_flooded);
150 settings->setS16("mgfractal_dungeon_ymin", dungeon_ymin);
151 settings->setS16("mgfractal_dungeon_ymax", dungeon_ymax);
152 settings->setU16("mgfractal_fractal", fractal);
153 settings->setU16("mgfractal_iterations", iterations);
154 settings->setV3F("mgfractal_scale", scale);
155 settings->setV3F("mgfractal_offset", offset);
156 settings->setFloat("mgfractal_slice_w", slice_w);
157 settings->setFloat("mgfractal_julia_x", julia_x);
158 settings->setFloat("mgfractal_julia_y", julia_y);
159 settings->setFloat("mgfractal_julia_z", julia_z);
160 settings->setFloat("mgfractal_julia_w", julia_w);
162 settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
163 settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
164 settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
165 settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
166 settings->setNoiseParams("mgfractal_np_dungeons", np_dungeons);
170 void MapgenFractalParams::setDefaultSettings(Settings *settings)
172 settings->setDefault("mgfractal_spflags", flagdesc_mapgen_fractal,
177 /////////////////////////////////////////////////////////////////
180 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
182 bool solid_below = false; // Fractal node is present below to spawn on
183 u8 air_count = 0; // Consecutive air nodes above a fractal node
184 s16 search_start = 0; // No terrain search start
186 // If terrain present, don't start search below terrain or water level
188 s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
189 search_start = MYMAX(search_start, MYMAX(seabed_level, water_level));
192 for (s16 y = search_start; y <= search_start + 4096; y++) {
193 if (getFractalAtPoint(p.X, y, p.Y)) {
197 } else if (solid_below) {
198 // Air above fractal node
200 // 3 and -2 to account for biome dust nodes
206 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
210 void MapgenFractal::makeChunk(BlockMakeData *data)
213 assert(data->vmanip);
214 assert(data->nodedef);
215 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
216 data->blockpos_requested.Y >= data->blockpos_min.Y &&
217 data->blockpos_requested.Z >= data->blockpos_min.Z);
218 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
219 data->blockpos_requested.Y <= data->blockpos_max.Y &&
220 data->blockpos_requested.Z <= data->blockpos_max.Z);
222 //TimeTaker t("makeChunk");
224 this->generating = true;
225 this->vm = data->vmanip;
226 this->ndef = data->nodedef;
228 v3s16 blockpos_min = data->blockpos_min;
229 v3s16 blockpos_max = data->blockpos_max;
230 node_min = blockpos_min * MAP_BLOCKSIZE;
231 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
232 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
233 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
235 blockseed = getBlockSeed2(full_node_min, seed);
237 // Generate fractal and optional terrain
238 s16 stone_surface_max_y = generateTerrain();
241 updateHeightmap(node_min, node_max);
243 // Init biome generator, place biome-specific nodes, and build biomemap
244 if (flags & MG_BIOMES) {
245 biomegen->calcBiomeNoise(node_min);
249 // Generate tunnels and randomwalk caves
250 if (flags & MG_CAVES) {
251 generateCavesNoiseIntersection(stone_surface_max_y);
252 generateCavesRandomWalk(stone_surface_max_y, large_cave_depth);
255 // Generate the registered ores
256 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
259 if (flags & MG_DUNGEONS)
260 generateDungeons(stone_surface_max_y);
262 // Generate the registered decorations
263 if (flags & MG_DECORATIONS)
264 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
266 // Sprinkle some dust on top after everything else was generated
267 if (flags & MG_BIOMES)
271 if (spflags & MGFRACTAL_TERRAIN)
272 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
274 // Calculate lighting
275 if (flags & MG_LIGHT)
276 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
277 full_node_min, full_node_max);
279 this->generating = false;
281 //printf("makeChunk: %lums\n", t.stop());
285 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
287 float cx, cy, cz, cw, ox, oy, oz, ow;
289 if (julia) { // Julia set
294 ox = (float)x / scale.X - offset.X;
295 oy = (float)y / scale.Y - offset.Y;
296 oz = (float)z / scale.Z - offset.Z;
298 } else { // Mandelbrot set
299 cx = (float)x / scale.X - offset.X;
300 cy = (float)y / scale.Y - offset.Y;
301 cz = (float)z / scale.Z - offset.Z;
314 for (u16 iter = 0; iter < iterations; iter++) {
317 case 1: // 4D "Roundy"
318 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
319 ny = 2.0f * (ox * oy + oz * ow) + cy;
320 nz = 2.0f * (ox * oz + oy * ow) + cz;
321 nw = 2.0f * (ox * ow + oy * oz) + cw;
323 case 2: // 4D "Squarry"
324 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
325 ny = 2.0f * (ox * oy + oz * ow) + cy;
326 nz = 2.0f * (ox * oz + oy * ow) + cz;
327 nw = 2.0f * (ox * ow - oy * oz) + cw;
329 case 3: // 4D "Mandy Cousin"
330 nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
331 ny = 2.0f * (ox * oy + oz * ow) + cy;
332 nz = 2.0f * (ox * oz + oy * ow) + cz;
333 nw = 2.0f * (ox * ow + oy * oz) + cw;
335 case 4: // 4D "Variation"
336 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
337 ny = 2.0f * (ox * oy + oz * ow) + cy;
338 nz = 2.0f * (ox * oz - oy * ow) + cz;
339 nw = 2.0f * (ox * ow + oy * oz) + cw;
341 case 5: // 3D "Mandelbrot/Mandelbar"
342 nx = ox * ox - oy * oy - oz * oz + cx;
343 ny = 2.0f * ox * oy + cy;
344 nz = -2.0f * ox * oz + cz;
346 case 6: // 3D "Christmas Tree"
347 // Altering the formula here is necessary to avoid division by zero
348 if (std::fabs(oz) < 0.000000001f) {
349 nx = ox * ox - oy * oy - oz * oz + cx;
350 ny = 2.0f * oy * ox + cy;
351 nz = 4.0f * oz * ox + cz;
353 float a = (2.0f * ox) / (std::sqrt(oy * oy + oz * oz));
354 nx = ox * ox - oy * oy - oz * oz + cx;
355 ny = a * (oy * oy - oz * oz) + cy;
356 nz = a * 2.0f * oy * oz + cz;
359 case 7: // 3D "Mandelbulb"
360 if (std::fabs(oy) < 0.000000001f) {
361 nx = ox * ox - oz * oz + cx;
363 nz = -2.0f * oz * std::sqrt(ox * ox) + cz;
365 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
366 nx = (ox * ox - oy * oy) * a + cx;
367 ny = 2.0f * ox * oy * a + cy;
368 nz = -2.0f * oz * std::sqrt(ox * ox + oy * oy) + cz;
371 case 8: // 3D "Cosine Mandelbulb"
372 if (std::fabs(oy) < 0.000000001f) {
373 nx = 2.0f * ox * oz + cx;
374 ny = 4.0f * oy * oz + cy;
375 nz = oz * oz - ox * ox - oy * oy + cz;
377 float a = (2.0f * oz) / std::sqrt(ox * ox + oy * oy);
378 nx = (ox * ox - oy * oy) * a + cx;
379 ny = 2.0f * ox * oy * a + cy;
380 nz = oz * oz - ox * ox - oy * oy + cz;
383 case 9: // 4D "Mandelbulb"
384 float rxy = std::sqrt(ox * ox + oy * oy);
385 float rxyz = std::sqrt(ox * ox + oy * oy + oz * oz);
386 if (std::fabs(ow) < 0.000000001f && std::fabs(oz) < 0.000000001f) {
387 nx = (ox * ox - oy * oy) + cx;
388 ny = 2.0f * ox * oy + cy;
389 nz = -2.0f * rxy * oz + cz;
390 nw = 2.0f * rxyz * ow + cw;
392 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
393 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
394 nx = (ox * ox - oy * oy) * b + cx;
395 ny = 2.0f * ox * oy * b + cy;
396 nz = -2.0f * rxy * oz * a + cz;
397 nw = 2.0f * rxyz * ow + cw;
402 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
415 s16 MapgenFractal::generateTerrain()
417 MapNode n_air(CONTENT_AIR);
418 MapNode n_stone(c_stone);
419 MapNode n_water(c_water_source);
421 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
425 noise_seabed->perlinMap2D(node_min.X, node_min.Z);
427 for (s16 z = node_min.Z; z <= node_max.Z; z++) {
428 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
429 u32 vi = vm->m_area.index(node_min.X, y, z);
430 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
431 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
434 s16 seabed_height = -MAX_MAP_GENERATION_LIMIT;
436 seabed_height = noise_seabed->result[index2d];
438 if (((spflags & MGFRACTAL_TERRAIN) && y <= seabed_height) ||
439 getFractalAtPoint(x, y, z)) {
440 vm->m_data[vi] = n_stone;
441 if (y > stone_surface_max_y)
442 stone_surface_max_y = y;
443 } else if ((spflags & MGFRACTAL_TERRAIN) && y <= water_level) {
444 vm->m_data[vi] = n_water;
446 vm->m_data[vi] = n_air;
454 return stone_surface_max_y;