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, U32_MAX);
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 /////////////////////////////////////////////////////////////////
173 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
175 bool solid_below = false; // Fractal node is present below to spawn on
176 u8 air_count = 0; // Consecutive air nodes above a fractal node
177 s16 search_start = 0; // No terrain search start
179 // If terrain present, don't start search below terrain or water level
181 s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
182 search_start = MYMAX(search_start, MYMAX(seabed_level, water_level));
185 for (s16 y = search_start; y <= search_start + 4096; y++) {
186 if (getFractalAtPoint(p.X, y, p.Y)) {
190 } else if (solid_below) {
191 // Air above fractal node
193 // 3 and -2 to account for biome dust nodes
199 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
203 void MapgenFractal::makeChunk(BlockMakeData *data)
206 assert(data->vmanip);
207 assert(data->nodedef);
208 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
209 data->blockpos_requested.Y >= data->blockpos_min.Y &&
210 data->blockpos_requested.Z >= data->blockpos_min.Z);
211 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
212 data->blockpos_requested.Y <= data->blockpos_max.Y &&
213 data->blockpos_requested.Z <= data->blockpos_max.Z);
215 //TimeTaker t("makeChunk");
217 this->generating = true;
218 this->vm = data->vmanip;
219 this->ndef = data->nodedef;
221 v3s16 blockpos_min = data->blockpos_min;
222 v3s16 blockpos_max = data->blockpos_max;
223 node_min = blockpos_min * MAP_BLOCKSIZE;
224 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
225 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
226 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
228 blockseed = getBlockSeed2(full_node_min, seed);
230 // Generate fractal and optional terrain
231 s16 stone_surface_max_y = generateTerrain();
234 updateHeightmap(node_min, node_max);
236 // Init biome generator, place biome-specific nodes, and build biomemap
237 if (flags & MG_BIOMES) {
238 biomegen->calcBiomeNoise(node_min);
242 // Generate tunnels and randomwalk caves
243 if (flags & MG_CAVES) {
244 generateCavesNoiseIntersection(stone_surface_max_y);
245 generateCavesRandomWalk(stone_surface_max_y, large_cave_depth);
248 // Generate the registered ores
249 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
252 if (flags & MG_DUNGEONS)
253 generateDungeons(stone_surface_max_y);
255 // Generate the registered decorations
256 if (flags & MG_DECORATIONS)
257 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
259 // Sprinkle some dust on top after everything else was generated
260 if (flags & MG_BIOMES)
264 if (spflags & MGFRACTAL_TERRAIN)
265 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
267 // Calculate lighting
268 if (flags & MG_LIGHT)
269 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
270 full_node_min, full_node_max);
272 this->generating = false;
274 //printf("makeChunk: %lums\n", t.stop());
278 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
280 float cx, cy, cz, cw, ox, oy, oz, ow;
282 if (julia) { // Julia set
287 ox = (float)x / scale.X - offset.X;
288 oy = (float)y / scale.Y - offset.Y;
289 oz = (float)z / scale.Z - offset.Z;
291 } else { // Mandelbrot set
292 cx = (float)x / scale.X - offset.X;
293 cy = (float)y / scale.Y - offset.Y;
294 cz = (float)z / scale.Z - offset.Z;
307 for (u16 iter = 0; iter < iterations; iter++) {
310 case 1: // 4D "Roundy"
311 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
312 ny = 2.0f * (ox * oy + oz * ow) + cy;
313 nz = 2.0f * (ox * oz + oy * ow) + cz;
314 nw = 2.0f * (ox * ow + oy * oz) + cw;
316 case 2: // 4D "Squarry"
317 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
318 ny = 2.0f * (ox * oy + oz * ow) + cy;
319 nz = 2.0f * (ox * oz + oy * ow) + cz;
320 nw = 2.0f * (ox * ow - oy * oz) + cw;
322 case 3: // 4D "Mandy Cousin"
323 nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
324 ny = 2.0f * (ox * oy + oz * ow) + cy;
325 nz = 2.0f * (ox * oz + oy * ow) + cz;
326 nw = 2.0f * (ox * ow + oy * oz) + cw;
328 case 4: // 4D "Variation"
329 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
330 ny = 2.0f * (ox * oy + oz * ow) + cy;
331 nz = 2.0f * (ox * oz - oy * ow) + cz;
332 nw = 2.0f * (ox * ow + oy * oz) + cw;
334 case 5: // 3D "Mandelbrot/Mandelbar"
335 nx = ox * ox - oy * oy - oz * oz + cx;
336 ny = 2.0f * ox * oy + cy;
337 nz = -2.0f * ox * oz + cz;
339 case 6: // 3D "Christmas Tree"
340 // Altering the formula here is necessary to avoid division by zero
341 if (std::fabs(oz) < 0.000000001f) {
342 nx = ox * ox - oy * oy - oz * oz + cx;
343 ny = 2.0f * oy * ox + cy;
344 nz = 4.0f * oz * ox + cz;
346 float a = (2.0f * ox) / (std::sqrt(oy * oy + oz * oz));
347 nx = ox * ox - oy * oy - oz * oz + cx;
348 ny = a * (oy * oy - oz * oz) + cy;
349 nz = a * 2.0f * oy * oz + cz;
352 case 7: // 3D "Mandelbulb"
353 if (std::fabs(oy) < 0.000000001f) {
354 nx = ox * ox - oz * oz + cx;
356 nz = -2.0f * oz * std::sqrt(ox * ox) + cz;
358 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
359 nx = (ox * ox - oy * oy) * a + cx;
360 ny = 2.0f * ox * oy * a + cy;
361 nz = -2.0f * oz * std::sqrt(ox * ox + oy * oy) + cz;
364 case 8: // 3D "Cosine Mandelbulb"
365 if (std::fabs(oy) < 0.000000001f) {
366 nx = 2.0f * ox * oz + cx;
367 ny = 4.0f * oy * oz + cy;
368 nz = oz * oz - ox * ox - oy * oy + cz;
370 float a = (2.0f * oz) / std::sqrt(ox * ox + oy * oy);
371 nx = (ox * ox - oy * oy) * a + cx;
372 ny = 2.0f * ox * oy * a + cy;
373 nz = oz * oz - ox * ox - oy * oy + cz;
376 case 9: // 4D "Mandelbulb"
377 float rxy = std::sqrt(ox * ox + oy * oy);
378 float rxyz = std::sqrt(ox * ox + oy * oy + oz * oz);
379 if (std::fabs(ow) < 0.000000001f && std::fabs(oz) < 0.000000001f) {
380 nx = (ox * ox - oy * oy) + cx;
381 ny = 2.0f * ox * oy + cy;
382 nz = -2.0f * rxy * oz + cz;
383 nw = 2.0f * rxyz * ow + cw;
385 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
386 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
387 nx = (ox * ox - oy * oy) * b + cx;
388 ny = 2.0f * ox * oy * b + cy;
389 nz = -2.0f * rxy * oz * a + cz;
390 nw = 2.0f * rxyz * ow + cw;
395 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
408 s16 MapgenFractal::generateTerrain()
410 MapNode n_air(CONTENT_AIR);
411 MapNode n_stone(c_stone);
412 MapNode n_water(c_water_source);
414 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
418 noise_seabed->perlinMap2D(node_min.X, node_min.Z);
420 for (s16 z = node_min.Z; z <= node_max.Z; z++) {
421 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
422 u32 vi = vm->m_area.index(node_min.X, y, z);
423 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
424 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
427 s16 seabed_height = -MAX_MAP_GENERATION_LIMIT;
429 seabed_height = noise_seabed->result[index2d];
431 if (((spflags & MGFRACTAL_TERRAIN) && y <= seabed_height) ||
432 getFractalAtPoint(x, y, z)) {
433 vm->m_data[vi] = n_stone;
434 if (y > stone_surface_max_y)
435 stone_surface_max_y = y;
436 } else if ((spflags & MGFRACTAL_TERRAIN) && y <= water_level) {
437 vm->m_data[vi] = n_water;
439 vm->m_data[vi] = n_air;
447 return stone_surface_max_y;