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