Mgfractal: Make non-fractal terrain optional (#8702)
[oweals/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         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;
69
70         //// 2D noise
71         if (spflags & MGFRACTAL_TERRAIN)
72                 noise_seabed = new Noise(&params->np_seabed, seed, csize.X, csize.Z);
73
74         noise_filler_depth = new Noise(&params->np_filler_depth, seed, csize.X, csize.Z);
75
76         //// 3D noise
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;
81
82         formula = fractal / 2 + fractal % 2;
83         julia   = fractal % 2 == 0;
84 }
85
86
87 MapgenFractal::~MapgenFractal()
88 {
89         if (noise_seabed)
90                 delete noise_seabed;
91
92         delete noise_filler_depth;
93 }
94
95
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)
102 {
103 }
104
105
106 void MapgenFractalParams::readParams(const Settings *settings)
107 {
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);
123
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);
129 }
130
131
132 void MapgenFractalParams::writeParams(Settings *settings) const
133 {
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);
149
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);
155 }
156
157
158 /////////////////////////////////////////////////////////////////
159
160
161 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
162 {
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
166
167         // If terrain present, don't start search below terrain or water level
168         if (noise_seabed) {
169                 s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
170                 search_start = MYMAX(search_start, MYMAX(seabed_level, water_level));
171         }
172
173         for (s16 y = search_start; y <= search_start + 4096; y++) {
174                 if (getFractalAtPoint(p.X, y, p.Y)) {
175                         // Fractal node
176                         solid_below = true;
177                         air_count = 0;
178                 } else if (solid_below) {
179                         // Air above fractal node
180                         air_count++;
181                         // 3 and -2 to account for biome dust nodes
182                         if (air_count == 3)
183                                 return y - 2;
184                 }
185         }
186
187         return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
188 }
189
190
191 void MapgenFractal::makeChunk(BlockMakeData *data)
192 {
193         // Pre-conditions
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);
202
203         //TimeTaker t("makeChunk");
204
205         this->generating = true;
206         this->vm = data->vmanip;
207         this->ndef = data->nodedef;
208
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);
215
216         blockseed = getBlockSeed2(full_node_min, seed);
217
218         // Generate fractal and optional terrain
219         s16 stone_surface_max_y = generateTerrain();
220
221         // Create heightmap
222         updateHeightmap(node_min, node_max);
223
224         // Init biome generator, place biome-specific nodes, and build biomemap
225         if (flags & MG_BIOMES) {
226                 biomegen->calcBiomeNoise(node_min);
227                 generateBiomes();
228         }
229
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);
234         }
235
236         // Generate the registered ores
237         m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
238
239         // Generate dungeons
240         if ((flags & MG_DUNGEONS) && full_node_min.Y >= dungeon_ymin &&
241                         full_node_max.Y <= dungeon_ymax)
242                 generateDungeons(stone_surface_max_y);
243
244         // Generate the registered decorations
245         if (flags & MG_DECORATIONS)
246                 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
247
248         // Sprinkle some dust on top after everything else was generated
249         if (flags & MG_BIOMES)
250                 dustTopNodes();
251
252         // Update liquids
253         if (spflags & MGFRACTAL_TERRAIN)
254                 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
255
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);
260
261         this->generating = false;
262
263         //printf("makeChunk: %lums\n", t.stop());
264 }
265
266
267 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
268 {
269         float cx, cy, cz, cw, ox, oy, oz, ow;
270
271         if (julia) {  // Julia set
272                 cx = julia_x;
273                 cy = julia_y;
274                 cz = julia_z;
275                 cw = julia_w;
276                 ox = (float)x / scale.X - offset.X;
277                 oy = (float)y / scale.Y - offset.Y;
278                 oz = (float)z / scale.Z - offset.Z;
279                 ow = slice_w;
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;
284                 cw = slice_w;
285                 ox = 0.0f;
286                 oy = 0.0f;
287                 oz = 0.0f;
288                 ow = 0.0f;
289         }
290
291         float nx = 0.0f;
292         float ny = 0.0f;
293         float nz = 0.0f;
294         float nw = 0.0f;
295
296         for (u16 iter = 0; iter < iterations; iter++) {
297                 switch (formula) {
298                 default:
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;
304                         break;
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;
310                         break;
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;
316                         break;
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;
322                         break;
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;
327                         break;
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;
334                         } else {
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;
339                         }
340                         break;
341                 case 7: // 3D "Mandelbulb"
342                         if (std::fabs(oy) < 0.000000001f) {
343                                 nx = ox * ox - oz * oz + cx;
344                                 ny = cy;
345                                 nz = -2.0f * oz * std::sqrt(ox * ox) + cz;
346                         } else {
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;
351                         }
352                         break;
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;
358                         } else {
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;
363                         }
364                         break;
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;
373                         } else {
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;
380                         }
381                         break;
382                 }
383
384                 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
385                         return false;
386
387                 ox = nx;
388                 oy = ny;
389                 oz = nz;
390                 ow = nw;
391         }
392
393         return true;
394 }
395
396
397 s16 MapgenFractal::generateTerrain()
398 {
399         MapNode n_air(CONTENT_AIR);
400         MapNode n_stone(c_stone);
401         MapNode n_water(c_water_source);
402
403         s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
404         u32 index2d = 0;
405
406         if (noise_seabed)
407                 noise_seabed->perlinMap2D(node_min.X, node_min.Z);
408
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)
414                                         continue;
415
416                                 s16 seabed_height;
417                                 if (noise_seabed)
418                                         seabed_height = noise_seabed->result[index2d];
419
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;
427                                 } else {
428                                         vm->m_data[vi] = n_air;
429                                 }
430                         }
431                         index2d -= ystride;
432                 }
433                 index2d += ystride;
434         }
435
436         return stone_surface_max_y;
437 }