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.
30 #include "voxelalgorithms.h"
31 //#include "profiler.h" // For TimeTaker
32 #include "settings.h" // For g_settings
34 #include "dungeongen.h"
38 #include "mg_decoration.h"
39 #include "mapgen_fractal.h"
42 FlagDesc flagdesc_mapgen_fractal[] = {
43 {"terrain", MGFRACTAL_TERRAIN},
47 ///////////////////////////////////////////////////////////////////////////////////////
50 MapgenFractal::MapgenFractal(MapgenFractalParams *params, EmergeManager *emerge)
51 : MapgenBasic(MAPGEN_FRACTAL, params, emerge)
53 spflags = params->spflags;
54 cave_width = params->cave_width;
55 large_cave_depth = params->large_cave_depth;
56 small_cave_num_min = params->small_cave_num_min;
57 small_cave_num_max = params->small_cave_num_max;
58 large_cave_num_min = params->large_cave_num_min;
59 large_cave_num_max = params->large_cave_num_max;
60 large_cave_flooded = params->large_cave_flooded;
61 dungeon_ymin = params->dungeon_ymin;
62 dungeon_ymax = params->dungeon_ymax;
63 fractal = params->fractal;
64 iterations = params->iterations;
65 scale = params->scale;
66 offset = params->offset;
67 slice_w = params->slice_w;
68 julia_x = params->julia_x;
69 julia_y = params->julia_y;
70 julia_z = params->julia_z;
71 julia_w = params->julia_w;
74 if (spflags & MGFRACTAL_TERRAIN)
75 noise_seabed = new Noise(¶ms->np_seabed, seed, csize.X, csize.Z);
77 noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z);
80 MapgenBasic::np_dungeons = params->np_dungeons;
81 // Overgeneration to node_min.Y - 1
82 MapgenBasic::np_cave1 = params->np_cave1;
83 MapgenBasic::np_cave2 = params->np_cave2;
85 formula = fractal / 2 + fractal % 2;
86 julia = fractal % 2 == 0;
90 MapgenFractal::~MapgenFractal()
93 delete noise_filler_depth;
97 MapgenFractalParams::MapgenFractalParams():
98 np_seabed (-14, 9, v3f(600, 600, 600), 41900, 5, 0.6, 2.0),
99 np_filler_depth (0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0),
100 np_cave1 (0, 12, v3f(61, 61, 61), 52534, 3, 0.5, 2.0),
101 np_cave2 (0, 12, v3f(67, 67, 67), 10325, 3, 0.5, 2.0),
102 np_dungeons (0.9, 0.5, v3f(500, 500, 500), 0, 2, 0.8, 2.0)
107 void MapgenFractalParams::readParams(const Settings *settings)
109 settings->getFlagStrNoEx("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
110 settings->getFloatNoEx("mgfractal_cave_width", cave_width);
111 settings->getS16NoEx("mgfractal_large_cave_depth", large_cave_depth);
112 settings->getU16NoEx("mgfractal_small_cave_num_min", small_cave_num_min);
113 settings->getU16NoEx("mgfractal_small_cave_num_max", small_cave_num_max);
114 settings->getU16NoEx("mgfractal_large_cave_num_min", large_cave_num_min);
115 settings->getU16NoEx("mgfractal_large_cave_num_max", large_cave_num_max);
116 settings->getFloatNoEx("mgfractal_large_cave_flooded", large_cave_flooded);
117 settings->getS16NoEx("mgfractal_dungeon_ymin", dungeon_ymin);
118 settings->getS16NoEx("mgfractal_dungeon_ymax", dungeon_ymax);
119 settings->getU16NoEx("mgfractal_fractal", fractal);
120 settings->getU16NoEx("mgfractal_iterations", iterations);
121 settings->getV3FNoEx("mgfractal_scale", scale);
122 settings->getV3FNoEx("mgfractal_offset", offset);
123 settings->getFloatNoEx("mgfractal_slice_w", slice_w);
124 settings->getFloatNoEx("mgfractal_julia_x", julia_x);
125 settings->getFloatNoEx("mgfractal_julia_y", julia_y);
126 settings->getFloatNoEx("mgfractal_julia_z", julia_z);
127 settings->getFloatNoEx("mgfractal_julia_w", julia_w);
129 settings->getNoiseParams("mgfractal_np_seabed", np_seabed);
130 settings->getNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
131 settings->getNoiseParams("mgfractal_np_cave1", np_cave1);
132 settings->getNoiseParams("mgfractal_np_cave2", np_cave2);
133 settings->getNoiseParams("mgfractal_np_dungeons", np_dungeons);
137 void MapgenFractalParams::writeParams(Settings *settings) const
139 settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
140 settings->setFloat("mgfractal_cave_width", cave_width);
141 settings->setS16("mgfractal_large_cave_depth", large_cave_depth);
142 settings->setU16("mgfractal_small_cave_num_min", small_cave_num_min);
143 settings->setU16("mgfractal_small_cave_num_max", small_cave_num_max);
144 settings->setU16("mgfractal_large_cave_num_min", large_cave_num_min);
145 settings->setU16("mgfractal_large_cave_num_max", large_cave_num_max);
146 settings->setFloat("mgfractal_large_cave_flooded", large_cave_flooded);
147 settings->setS16("mgfractal_dungeon_ymin", dungeon_ymin);
148 settings->setS16("mgfractal_dungeon_ymax", dungeon_ymax);
149 settings->setU16("mgfractal_fractal", fractal);
150 settings->setU16("mgfractal_iterations", iterations);
151 settings->setV3F("mgfractal_scale", scale);
152 settings->setV3F("mgfractal_offset", offset);
153 settings->setFloat("mgfractal_slice_w", slice_w);
154 settings->setFloat("mgfractal_julia_x", julia_x);
155 settings->setFloat("mgfractal_julia_y", julia_y);
156 settings->setFloat("mgfractal_julia_z", julia_z);
157 settings->setFloat("mgfractal_julia_w", julia_w);
159 settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
160 settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
161 settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
162 settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
163 settings->setNoiseParams("mgfractal_np_dungeons", np_dungeons);
167 void MapgenFractalParams::setDefaultSettings(Settings *settings)
169 settings->setDefault("mgfractal_spflags", flagdesc_mapgen_fractal,
174 /////////////////////////////////////////////////////////////////
177 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
179 bool solid_below = false; // Fractal node is present below to spawn on
180 u8 air_count = 0; // Consecutive air nodes above a fractal node
181 s16 search_start = 0; // No terrain search start
183 // If terrain present, don't start search below terrain or water level
185 s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
186 search_start = MYMAX(search_start, MYMAX(seabed_level, water_level));
189 for (s16 y = search_start; y <= search_start + 4096; y++) {
190 if (getFractalAtPoint(p.X, y, p.Y)) {
194 } else if (solid_below) {
195 // Air above fractal node
197 // 3 and -2 to account for biome dust nodes
203 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
207 void MapgenFractal::makeChunk(BlockMakeData *data)
210 assert(data->vmanip);
211 assert(data->nodedef);
212 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
213 data->blockpos_requested.Y >= data->blockpos_min.Y &&
214 data->blockpos_requested.Z >= data->blockpos_min.Z);
215 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
216 data->blockpos_requested.Y <= data->blockpos_max.Y &&
217 data->blockpos_requested.Z <= data->blockpos_max.Z);
219 //TimeTaker t("makeChunk");
221 this->generating = true;
222 this->vm = data->vmanip;
223 this->ndef = data->nodedef;
225 v3s16 blockpos_min = data->blockpos_min;
226 v3s16 blockpos_max = data->blockpos_max;
227 node_min = blockpos_min * MAP_BLOCKSIZE;
228 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
229 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
230 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
232 blockseed = getBlockSeed2(full_node_min, seed);
234 // Generate fractal and optional terrain
235 s16 stone_surface_max_y = generateTerrain();
238 updateHeightmap(node_min, node_max);
240 // Init biome generator, place biome-specific nodes, and build biomemap
241 if (flags & MG_BIOMES) {
242 biomegen->calcBiomeNoise(node_min);
246 // Generate tunnels and randomwalk caves
247 if (flags & MG_CAVES) {
248 generateCavesNoiseIntersection(stone_surface_max_y);
249 generateCavesRandomWalk(stone_surface_max_y, large_cave_depth);
252 // Generate the registered ores
253 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
256 if (flags & MG_DUNGEONS)
257 generateDungeons(stone_surface_max_y);
259 // Generate the registered decorations
260 if (flags & MG_DECORATIONS)
261 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
263 // Sprinkle some dust on top after everything else was generated
264 if (flags & MG_BIOMES)
268 if (spflags & MGFRACTAL_TERRAIN)
269 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
271 // Calculate lighting
272 if (flags & MG_LIGHT)
273 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
274 full_node_min, full_node_max);
276 this->generating = false;
278 //printf("makeChunk: %lums\n", t.stop());
282 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
284 float cx, cy, cz, cw, ox, oy, oz, ow;
286 if (julia) { // Julia set
291 ox = (float)x / scale.X - offset.X;
292 oy = (float)y / scale.Y - offset.Y;
293 oz = (float)z / scale.Z - offset.Z;
295 } else { // Mandelbrot set
296 cx = (float)x / scale.X - offset.X;
297 cy = (float)y / scale.Y - offset.Y;
298 cz = (float)z / scale.Z - offset.Z;
311 for (u16 iter = 0; iter < iterations; iter++) {
314 case 1: // 4D "Roundy"
315 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
316 ny = 2.0f * (ox * oy + oz * ow) + cy;
317 nz = 2.0f * (ox * oz + oy * ow) + cz;
318 nw = 2.0f * (ox * ow + oy * oz) + cw;
320 case 2: // 4D "Squarry"
321 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
322 ny = 2.0f * (ox * oy + oz * ow) + cy;
323 nz = 2.0f * (ox * oz + oy * ow) + cz;
324 nw = 2.0f * (ox * ow - oy * oz) + cw;
326 case 3: // 4D "Mandy Cousin"
327 nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
328 ny = 2.0f * (ox * oy + oz * ow) + cy;
329 nz = 2.0f * (ox * oz + oy * ow) + cz;
330 nw = 2.0f * (ox * ow + oy * oz) + cw;
332 case 4: // 4D "Variation"
333 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
334 ny = 2.0f * (ox * oy + oz * ow) + cy;
335 nz = 2.0f * (ox * oz - oy * ow) + cz;
336 nw = 2.0f * (ox * ow + oy * oz) + cw;
338 case 5: // 3D "Mandelbrot/Mandelbar"
339 nx = ox * ox - oy * oy - oz * oz + cx;
340 ny = 2.0f * ox * oy + cy;
341 nz = -2.0f * ox * oz + cz;
343 case 6: // 3D "Christmas Tree"
344 // Altering the formula here is necessary to avoid division by zero
345 if (std::fabs(oz) < 0.000000001f) {
346 nx = ox * ox - oy * oy - oz * oz + cx;
347 ny = 2.0f * oy * ox + cy;
348 nz = 4.0f * oz * ox + cz;
350 float a = (2.0f * ox) / (std::sqrt(oy * oy + oz * oz));
351 nx = ox * ox - oy * oy - oz * oz + cx;
352 ny = a * (oy * oy - oz * oz) + cy;
353 nz = a * 2.0f * oy * oz + cz;
356 case 7: // 3D "Mandelbulb"
357 if (std::fabs(oy) < 0.000000001f) {
358 nx = ox * ox - oz * oz + cx;
360 nz = -2.0f * oz * std::sqrt(ox * ox) + cz;
362 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
363 nx = (ox * ox - oy * oy) * a + cx;
364 ny = 2.0f * ox * oy * a + cy;
365 nz = -2.0f * oz * std::sqrt(ox * ox + oy * oy) + cz;
368 case 8: // 3D "Cosine Mandelbulb"
369 if (std::fabs(oy) < 0.000000001f) {
370 nx = 2.0f * ox * oz + cx;
371 ny = 4.0f * oy * oz + cy;
372 nz = oz * oz - ox * ox - oy * oy + cz;
374 float a = (2.0f * oz) / std::sqrt(ox * ox + oy * oy);
375 nx = (ox * ox - oy * oy) * a + cx;
376 ny = 2.0f * ox * oy * a + cy;
377 nz = oz * oz - ox * ox - oy * oy + cz;
380 case 9: // 4D "Mandelbulb"
381 float rxy = std::sqrt(ox * ox + oy * oy);
382 float rxyz = std::sqrt(ox * ox + oy * oy + oz * oz);
383 if (std::fabs(ow) < 0.000000001f && std::fabs(oz) < 0.000000001f) {
384 nx = (ox * ox - oy * oy) + cx;
385 ny = 2.0f * ox * oy + cy;
386 nz = -2.0f * rxy * oz + cz;
387 nw = 2.0f * rxyz * ow + cw;
389 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
390 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
391 nx = (ox * ox - oy * oy) * b + cx;
392 ny = 2.0f * ox * oy * b + cy;
393 nz = -2.0f * rxy * oz * a + cz;
394 nw = 2.0f * rxyz * ow + 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;
422 noise_seabed->perlinMap2D(node_min.X, node_min.Z);
424 for (s16 z = node_min.Z; z <= node_max.Z; z++) {
425 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
426 u32 vi = vm->m_area.index(node_min.X, y, z);
427 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
428 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
431 s16 seabed_height = -MAX_MAP_GENERATION_LIMIT;
433 seabed_height = noise_seabed->result[index2d];
435 if (((spflags & MGFRACTAL_TERRAIN) && y <= seabed_height) ||
436 getFractalAtPoint(x, y, z)) {
437 vm->m_data[vi] = n_stone;
438 if (y > stone_surface_max_y)
439 stone_surface_max_y = y;
440 } else if ((spflags & MGFRACTAL_TERRAIN) && y <= water_level) {
441 vm->m_data[vi] = n_water;
443 vm->m_data[vi] = n_air;
451 return stone_surface_max_y;