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 lava_depth = params->lava_depth;
58 dungeon_ymin = params->dungeon_ymin;
59 dungeon_ymax = params->dungeon_ymax;
60 fractal = params->fractal;
61 iterations = params->iterations;
62 scale = params->scale;
63 offset = params->offset;
64 slice_w = params->slice_w;
65 julia_x = params->julia_x;
66 julia_y = params->julia_y;
67 julia_z = params->julia_z;
68 julia_w = params->julia_w;
71 if (spflags & MGFRACTAL_TERRAIN)
72 noise_seabed = new Noise(¶ms->np_seabed, seed, csize.X, csize.Z);
74 noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z);
77 MapgenBasic::np_dungeons = params->np_dungeons;
78 // Overgeneration to node_min.Y - 1
79 MapgenBasic::np_cave1 = params->np_cave1;
80 MapgenBasic::np_cave2 = params->np_cave2;
82 formula = fractal / 2 + fractal % 2;
83 julia = fractal % 2 == 0;
87 MapgenFractal::~MapgenFractal()
92 delete noise_filler_depth;
96 MapgenFractalParams::MapgenFractalParams():
97 np_seabed (-14, 9, v3f(600, 600, 600), 41900, 5, 0.6, 2.0),
98 np_filler_depth (0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0),
99 np_cave1 (0, 12, v3f(61, 61, 61), 52534, 3, 0.5, 2.0),
100 np_cave2 (0, 12, v3f(67, 67, 67), 10325, 3, 0.5, 2.0),
101 np_dungeons (0.9, 0.5, v3f(500, 500, 500), 0, 2, 0.8, 2.0)
106 void MapgenFractalParams::readParams(const Settings *settings)
108 settings->getFlagStrNoEx("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
109 settings->getFloatNoEx("mgfractal_cave_width", cave_width);
110 settings->getS16NoEx("mgfractal_large_cave_depth", large_cave_depth);
111 settings->getS16NoEx("mgfractal_lava_depth", lava_depth);
112 settings->getS16NoEx("mgfractal_dungeon_ymin", dungeon_ymin);
113 settings->getS16NoEx("mgfractal_dungeon_ymax", dungeon_ymax);
114 settings->getU16NoEx("mgfractal_fractal", fractal);
115 settings->getU16NoEx("mgfractal_iterations", iterations);
116 settings->getV3FNoEx("mgfractal_scale", scale);
117 settings->getV3FNoEx("mgfractal_offset", offset);
118 settings->getFloatNoEx("mgfractal_slice_w", slice_w);
119 settings->getFloatNoEx("mgfractal_julia_x", julia_x);
120 settings->getFloatNoEx("mgfractal_julia_y", julia_y);
121 settings->getFloatNoEx("mgfractal_julia_z", julia_z);
122 settings->getFloatNoEx("mgfractal_julia_w", julia_w);
124 settings->getNoiseParams("mgfractal_np_seabed", np_seabed);
125 settings->getNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
126 settings->getNoiseParams("mgfractal_np_cave1", np_cave1);
127 settings->getNoiseParams("mgfractal_np_cave2", np_cave2);
128 settings->getNoiseParams("mgfractal_np_dungeons", np_dungeons);
132 void MapgenFractalParams::writeParams(Settings *settings) const
134 settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal, U32_MAX);
135 settings->setFloat("mgfractal_cave_width", cave_width);
136 settings->setS16("mgfractal_large_cave_depth", large_cave_depth);
137 settings->setS16("mgfractal_lava_depth", lava_depth);
138 settings->setS16("mgfractal_dungeon_ymin", dungeon_ymin);
139 settings->setS16("mgfractal_dungeon_ymax", dungeon_ymax);
140 settings->setU16("mgfractal_fractal", fractal);
141 settings->setU16("mgfractal_iterations", iterations);
142 settings->setV3F("mgfractal_scale", scale);
143 settings->setV3F("mgfractal_offset", offset);
144 settings->setFloat("mgfractal_slice_w", slice_w);
145 settings->setFloat("mgfractal_julia_x", julia_x);
146 settings->setFloat("mgfractal_julia_y", julia_y);
147 settings->setFloat("mgfractal_julia_z", julia_z);
148 settings->setFloat("mgfractal_julia_w", julia_w);
150 settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
151 settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
152 settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
153 settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
154 settings->setNoiseParams("mgfractal_np_dungeons", np_dungeons);
158 /////////////////////////////////////////////////////////////////
161 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
163 bool solid_below = false; // Fractal node is present below to spawn on
164 u8 air_count = 0; // Consecutive air nodes above a fractal node
165 s16 search_start = 0; // No terrain search start
167 // If terrain present, don't start search below terrain or water level
169 s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
170 search_start = MYMAX(search_start, MYMAX(seabed_level, water_level));
173 for (s16 y = search_start; y <= search_start + 4096; y++) {
174 if (getFractalAtPoint(p.X, y, p.Y)) {
178 } else if (solid_below) {
179 // Air above fractal node
181 // 3 and -2 to account for biome dust nodes
187 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
191 void MapgenFractal::makeChunk(BlockMakeData *data)
194 assert(data->vmanip);
195 assert(data->nodedef);
196 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
197 data->blockpos_requested.Y >= data->blockpos_min.Y &&
198 data->blockpos_requested.Z >= data->blockpos_min.Z);
199 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
200 data->blockpos_requested.Y <= data->blockpos_max.Y &&
201 data->blockpos_requested.Z <= data->blockpos_max.Z);
203 //TimeTaker t("makeChunk");
205 this->generating = true;
206 this->vm = data->vmanip;
207 this->ndef = data->nodedef;
209 v3s16 blockpos_min = data->blockpos_min;
210 v3s16 blockpos_max = data->blockpos_max;
211 node_min = blockpos_min * MAP_BLOCKSIZE;
212 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
213 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
214 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
216 blockseed = getBlockSeed2(full_node_min, seed);
218 // Generate fractal and optional terrain
219 s16 stone_surface_max_y = generateTerrain();
222 updateHeightmap(node_min, node_max);
224 // Init biome generator, place biome-specific nodes, and build biomemap
225 if (flags & MG_BIOMES) {
226 biomegen->calcBiomeNoise(node_min);
230 // Generate tunnels and randomwalk caves
231 if (flags & MG_CAVES) {
232 generateCavesNoiseIntersection(stone_surface_max_y);
233 generateCavesRandomWalk(stone_surface_max_y, large_cave_depth);
236 // Generate the registered ores
237 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
240 if ((flags & MG_DUNGEONS) && full_node_min.Y >= dungeon_ymin &&
241 full_node_max.Y <= dungeon_ymax)
242 generateDungeons(stone_surface_max_y);
244 // Generate the registered decorations
245 if (flags & MG_DECORATIONS)
246 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
248 // Sprinkle some dust on top after everything else was generated
249 if (flags & MG_BIOMES)
253 if (spflags & MGFRACTAL_TERRAIN)
254 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
256 // Calculate lighting
257 if (flags & MG_LIGHT)
258 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
259 full_node_min, full_node_max);
261 this->generating = false;
263 //printf("makeChunk: %lums\n", t.stop());
267 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
269 float cx, cy, cz, cw, ox, oy, oz, ow;
271 if (julia) { // Julia set
276 ox = (float)x / scale.X - offset.X;
277 oy = (float)y / scale.Y - offset.Y;
278 oz = (float)z / scale.Z - offset.Z;
280 } else { // Mandelbrot set
281 cx = (float)x / scale.X - offset.X;
282 cy = (float)y / scale.Y - offset.Y;
283 cz = (float)z / scale.Z - offset.Z;
296 for (u16 iter = 0; iter < iterations; iter++) {
299 case 1: // 4D "Roundy"
300 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
301 ny = 2.0f * (ox * oy + oz * ow) + cy;
302 nz = 2.0f * (ox * oz + oy * ow) + cz;
303 nw = 2.0f * (ox * ow + oy * oz) + cw;
305 case 2: // 4D "Squarry"
306 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
307 ny = 2.0f * (ox * oy + oz * ow) + cy;
308 nz = 2.0f * (ox * oz + oy * ow) + cz;
309 nw = 2.0f * (ox * ow - oy * oz) + cw;
311 case 3: // 4D "Mandy Cousin"
312 nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
313 ny = 2.0f * (ox * oy + oz * ow) + cy;
314 nz = 2.0f * (ox * oz + oy * ow) + cz;
315 nw = 2.0f * (ox * ow + oy * oz) + cw;
317 case 4: // 4D "Variation"
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 5: // 3D "Mandelbrot/Mandelbar"
324 nx = ox * ox - oy * oy - oz * oz + cx;
325 ny = 2.0f * ox * oy + cy;
326 nz = -2.0f * ox * oz + cz;
328 case 6: // 3D "Christmas Tree"
329 // Altering the formula here is necessary to avoid division by zero
330 if (std::fabs(oz) < 0.000000001f) {
331 nx = ox * ox - oy * oy - oz * oz + cx;
332 ny = 2.0f * oy * ox + cy;
333 nz = 4.0f * oz * ox + cz;
335 float a = (2.0f * ox) / (std::sqrt(oy * oy + oz * oz));
336 nx = ox * ox - oy * oy - oz * oz + cx;
337 ny = a * (oy * oy - oz * oz) + cy;
338 nz = a * 2.0f * oy * oz + cz;
341 case 7: // 3D "Mandelbulb"
342 if (std::fabs(oy) < 0.000000001f) {
343 nx = ox * ox - oz * oz + cx;
345 nz = -2.0f * oz * std::sqrt(ox * ox) + cz;
347 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
348 nx = (ox * ox - oy * oy) * a + cx;
349 ny = 2.0f * ox * oy * a + cy;
350 nz = -2.0f * oz * std::sqrt(ox * ox + oy * oy) + cz;
353 case 8: // 3D "Cosine Mandelbulb"
354 if (std::fabs(oy) < 0.000000001f) {
355 nx = 2.0f * ox * oz + cx;
356 ny = 4.0f * oy * oz + cy;
357 nz = oz * oz - ox * ox - oy * oy + cz;
359 float a = (2.0f * oz) / std::sqrt(ox * ox + oy * oy);
360 nx = (ox * ox - oy * oy) * a + cx;
361 ny = 2.0f * ox * oy * a + cy;
362 nz = oz * oz - ox * ox - oy * oy + cz;
365 case 9: // 4D "Mandelbulb"
366 float rxy = std::sqrt(ox * ox + oy * oy);
367 float rxyz = std::sqrt(ox * ox + oy * oy + oz * oz);
368 if (std::fabs(ow) < 0.000000001f && std::fabs(oz) < 0.000000001f) {
369 nx = (ox * ox - oy * oy) + cx;
370 ny = 2.0f * ox * oy + cy;
371 nz = -2.0f * rxy * oz + cz;
372 nw = 2.0f * rxyz * ow + cw;
374 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
375 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
376 nx = (ox * ox - oy * oy) * b + cx;
377 ny = 2.0f * ox * oy * b + cy;
378 nz = -2.0f * rxy * oz * a + cz;
379 nw = 2.0f * rxyz * ow + cw;
384 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
397 s16 MapgenFractal::generateTerrain()
399 MapNode n_air(CONTENT_AIR);
400 MapNode n_stone(c_stone);
401 MapNode n_water(c_water_source);
403 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
407 noise_seabed->perlinMap2D(node_min.X, node_min.Z);
409 for (s16 z = node_min.Z; z <= node_max.Z; z++) {
410 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
411 u32 vi = vm->m_area.index(node_min.X, y, z);
412 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
413 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
416 s16 seabed_height = -MAX_MAP_GENERATION_LIMIT;
418 seabed_height = noise_seabed->result[index2d];
420 if (((spflags & MGFRACTAL_TERRAIN) && y <= seabed_height) ||
421 getFractalAtPoint(x, y, z)) {
422 vm->m_data[vi] = n_stone;
423 if (y > stone_surface_max_y)
424 stone_surface_max_y = y;
425 } else if ((spflags & MGFRACTAL_TERRAIN) && y <= water_level) {
426 vm->m_data[vi] = n_water;
428 vm->m_data[vi] = n_air;
436 return stone_surface_max_y;