]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen/mapgen_fractal.cpp
Remove unused functions reported by cppcheck (#10463)
[dragonfireclient.git] / src / mapgen / mapgen_fractal.cpp
1 /*
2 Minetest
3 Copyright (C) 2015-2019 paramat
4 Copyright (C) 2015-2016 kwolekr, Ryan Kwolek
5
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.
10
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.
15
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.
19 */
20
21
22 #include "mapgen.h"
23 #include <cmath>
24 #include "voxel.h"
25 #include "noise.h"
26 #include "mapblock.h"
27 #include "mapnode.h"
28 #include "map.h"
29 #include "nodedef.h"
30 #include "voxelalgorithms.h"
31 //#include "profiler.h" // For TimeTaker
32 #include "settings.h" // For g_settings
33 #include "emerge.h"
34 #include "dungeongen.h"
35 #include "cavegen.h"
36 #include "mg_biome.h"
37 #include "mg_ore.h"
38 #include "mg_decoration.h"
39 #include "mapgen_fractal.h"
40
41
42 FlagDesc flagdesc_mapgen_fractal[] = {
43         {"terrain", MGFRACTAL_TERRAIN},
44         {NULL,      0}
45 };
46
47 ///////////////////////////////////////////////////////////////////////////////////////
48
49
50 MapgenFractal::MapgenFractal(MapgenFractalParams *params, EmergeParams *emerge)
51         : MapgenBasic(MAPGEN_FRACTAL, params, emerge)
52 {
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;
72
73         //// 2D noise
74         if (spflags & MGFRACTAL_TERRAIN)
75                 noise_seabed = new Noise(&params->np_seabed, seed, csize.X, csize.Z);
76
77         noise_filler_depth = new Noise(&params->np_filler_depth, seed, csize.X, csize.Z);
78
79         //// 3D noise
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;
84
85         formula = fractal / 2 + fractal % 2;
86         julia   = fractal % 2 == 0;
87 }
88
89
90 MapgenFractal::~MapgenFractal()
91 {
92         delete noise_seabed;
93         delete noise_filler_depth;
94 }
95
96
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)
103 {
104 }
105
106
107 void MapgenFractalParams::readParams(const Settings *settings)
108 {
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);
128
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);
134 }
135
136
137 void MapgenFractalParams::writeParams(Settings *settings) const
138 {
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);
158
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);
164 }
165
166
167 void MapgenFractalParams::setDefaultSettings(Settings *settings)
168 {
169         settings->setDefault("mgfractal_spflags", flagdesc_mapgen_fractal,
170                 MGFRACTAL_TERRAIN);
171 }
172
173
174 /////////////////////////////////////////////////////////////////
175
176
177 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
178 {
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
182
183         // If terrain present, don't start search below terrain or water level
184         if (noise_seabed) {
185                 s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
186                 search_start = MYMAX(search_start, MYMAX(seabed_level, water_level));
187         }
188
189         for (s16 y = search_start; y <= search_start + 4096; y++) {
190                 if (getFractalAtPoint(p.X, y, p.Y)) {
191                         // Fractal node
192                         solid_below = true;
193                         air_count = 0;
194                 } else if (solid_below) {
195                         // Air above fractal node
196                         air_count++;
197                         // 3 and -2 to account for biome dust nodes
198                         if (air_count == 3)
199                                 return y - 2;
200                 }
201         }
202
203         return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
204 }
205
206
207 void MapgenFractal::makeChunk(BlockMakeData *data)
208 {
209         // Pre-conditions
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);
218
219         //TimeTaker t("makeChunk");
220
221         this->generating = true;
222         this->vm = data->vmanip;
223         this->ndef = data->nodedef;
224
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);
231
232         blockseed = getBlockSeed2(full_node_min, seed);
233
234         // Generate fractal and optional terrain
235         s16 stone_surface_max_y = generateTerrain();
236
237         // Create heightmap
238         updateHeightmap(node_min, node_max);
239
240         // Init biome generator, place biome-specific nodes, and build biomemap
241         if (flags & MG_BIOMES) {
242                 biomegen->calcBiomeNoise(node_min);
243                 generateBiomes();
244         }
245
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);
250         }
251
252         // Generate the registered ores
253         if (flags & MG_ORES)
254                 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
255
256         // Generate dungeons
257         if (flags & MG_DUNGEONS)
258                 generateDungeons(stone_surface_max_y);
259
260         // Generate the registered decorations
261         if (flags & MG_DECORATIONS)
262                 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
263
264         // Sprinkle some dust on top after everything else was generated
265         if (flags & MG_BIOMES)
266                 dustTopNodes();
267
268         // Update liquids
269         if (spflags & MGFRACTAL_TERRAIN)
270                 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
271
272         // Calculate lighting
273         if (flags & MG_LIGHT)
274                 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
275                         full_node_min, full_node_max);
276
277         this->generating = false;
278
279         //printf("makeChunk: %lums\n", t.stop());
280 }
281
282
283 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
284 {
285         float cx, cy, cz, cw, ox, oy, oz, ow;
286
287         if (julia) {  // Julia set
288                 cx = julia_x;
289                 cy = julia_y;
290                 cz = julia_z;
291                 cw = julia_w;
292                 ox = (float)x / scale.X - offset.X;
293                 oy = (float)y / scale.Y - offset.Y;
294                 oz = (float)z / scale.Z - offset.Z;
295                 ow = slice_w;
296         } else {  // Mandelbrot set
297                 cx = (float)x / scale.X - offset.X;
298                 cy = (float)y / scale.Y - offset.Y;
299                 cz = (float)z / scale.Z - offset.Z;
300                 cw = slice_w;
301                 ox = 0.0f;
302                 oy = 0.0f;
303                 oz = 0.0f;
304                 ow = 0.0f;
305         }
306
307         float nx = 0.0f;
308         float ny = 0.0f;
309         float nz = 0.0f;
310         float nw = 0.0f;
311
312         for (u16 iter = 0; iter < iterations; iter++) {
313                 switch (formula) {
314                 default:
315                 case 1: // 4D "Roundy"
316                         nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
317                         ny = 2.0f * (ox * oy + oz * ow) + cy;
318                         nz = 2.0f * (ox * oz + oy * ow) + cz;
319                         nw = 2.0f * (ox * ow + oy * oz) + cw;
320                         break;
321                 case 2: // 4D "Squarry"
322                         nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
323                         ny = 2.0f * (ox * oy + oz * ow) + cy;
324                         nz = 2.0f * (ox * oz + oy * ow) + cz;
325                         nw = 2.0f * (ox * ow - oy * oz) + cw;
326                         break;
327                 case 3: // 4D "Mandy Cousin"
328                         nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
329                         ny = 2.0f * (ox * oy + oz * ow) + cy;
330                         nz = 2.0f * (ox * oz + oy * ow) + cz;
331                         nw = 2.0f * (ox * ow + oy * oz) + cw;
332                         break;
333                 case 4: // 4D "Variation"
334                         nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
335                         ny = 2.0f * (ox * oy + oz * ow) + cy;
336                         nz = 2.0f * (ox * oz - oy * ow) + cz;
337                         nw = 2.0f * (ox * ow + oy * oz) + cw;
338                         break;
339                 case 5: // 3D "Mandelbrot/Mandelbar"
340                         nx = ox * ox - oy * oy - oz * oz + cx;
341                         ny = 2.0f * ox * oy + cy;
342                         nz = -2.0f * ox * oz + cz;
343                         break;
344                 case 6: // 3D "Christmas Tree"
345                         // Altering the formula here is necessary to avoid division by zero
346                         if (std::fabs(oz) < 0.000000001f) {
347                                 nx = ox * ox - oy * oy - oz * oz + cx;
348                                 ny = 2.0f * oy * ox + cy;
349                                 nz = 4.0f * oz * ox + cz;
350                         } else {
351                                 float a = (2.0f * ox) / (std::sqrt(oy * oy + oz * oz));
352                                 nx = ox * ox - oy * oy - oz * oz + cx;
353                                 ny = a * (oy * oy - oz * oz) + cy;
354                                 nz = a * 2.0f * oy * oz + cz;
355                         }
356                         break;
357                 case 7: // 3D "Mandelbulb"
358                         if (std::fabs(oy) < 0.000000001f) {
359                                 nx = ox * ox - oz * oz + cx;
360                                 ny = cy;
361                                 nz = -2.0f * oz * std::sqrt(ox * ox) + cz;
362                         } else {
363                                 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
364                                 nx = (ox * ox - oy * oy) * a + cx;
365                                 ny = 2.0f * ox * oy * a + cy;
366                                 nz = -2.0f * oz * std::sqrt(ox * ox + oy * oy) + cz;
367                         }
368                         break;
369                 case 8: // 3D "Cosine Mandelbulb"
370                         if (std::fabs(oy) < 0.000000001f) {
371                                 nx = 2.0f * ox * oz + cx;
372                                 ny = 4.0f * oy * oz + cy;
373                                 nz = oz * oz - ox * ox - oy * oy + cz;
374                         } else {
375                                 float a = (2.0f * oz) / std::sqrt(ox * ox + oy * oy);
376                                 nx = (ox * ox - oy * oy) * a + cx;
377                                 ny = 2.0f * ox * oy * a + cy;
378                                 nz = oz * oz - ox * ox - oy * oy + cz;
379                         }
380                         break;
381                 case 9: // 4D "Mandelbulb"
382                         float rxy = std::sqrt(ox * ox + oy * oy);
383                         float rxyz = std::sqrt(ox * ox + oy * oy + oz * oz);
384                         if (std::fabs(ow) < 0.000000001f && std::fabs(oz) < 0.000000001f) {
385                                 nx = (ox * ox - oy * oy) + cx;
386                                 ny = 2.0f * ox * oy + cy;
387                                 nz = -2.0f * rxy * oz + cz;
388                                 nw = 2.0f * rxyz * ow + cw;
389                         } else {
390                                 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
391                                 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
392                                 nx = (ox * ox - oy * oy) * b + cx;
393                                 ny = 2.0f * ox * oy * b + cy;
394                                 nz = -2.0f * rxy * oz * a + cz;
395                                 nw = 2.0f * rxyz * ow + cw;
396                         }
397                         break;
398                 }
399
400                 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
401                         return false;
402
403                 ox = nx;
404                 oy = ny;
405                 oz = nz;
406                 ow = nw;
407         }
408
409         return true;
410 }
411
412
413 s16 MapgenFractal::generateTerrain()
414 {
415         MapNode n_air(CONTENT_AIR);
416         MapNode n_stone(c_stone);
417         MapNode n_water(c_water_source);
418
419         s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
420         u32 index2d = 0;
421
422         if (noise_seabed)
423                 noise_seabed->perlinMap2D(node_min.X, node_min.Z);
424
425         for (s16 z = node_min.Z; z <= node_max.Z; z++) {
426                 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
427                         u32 vi = vm->m_area.index(node_min.X, y, z);
428                         for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
429                                 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
430                                         continue;
431
432                                 s16 seabed_height = -MAX_MAP_GENERATION_LIMIT;
433                                 if (noise_seabed)
434                                         seabed_height = noise_seabed->result[index2d];
435
436                                 if (((spflags & MGFRACTAL_TERRAIN) && y <= seabed_height) ||
437                                                 getFractalAtPoint(x, y, z)) {
438                                         vm->m_data[vi] = n_stone;
439                                         if (y > stone_surface_max_y)
440                                                 stone_surface_max_y = y;
441                                 } else if ((spflags & MGFRACTAL_TERRAIN) && y <= water_level) {
442                                         vm->m_data[vi] = n_water;
443                                 } else {
444                                         vm->m_data[vi] = n_air;
445                                 }
446                         }
447                         index2d -= ystride;
448                 }
449                 index2d += ystride;
450         }
451
452         return stone_surface_max_y;
453 }