Drop content_sao.{cpp,h}
[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 "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, EmergeManager *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         m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
254
255         // Generate dungeons
256         if (flags & MG_DUNGEONS)
257                 generateDungeons(stone_surface_max_y);
258
259         // Generate the registered decorations
260         if (flags & MG_DECORATIONS)
261                 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
262
263         // Sprinkle some dust on top after everything else was generated
264         if (flags & MG_BIOMES)
265                 dustTopNodes();
266
267         // Update liquids
268         if (spflags & MGFRACTAL_TERRAIN)
269                 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
270
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);
275
276         this->generating = false;
277
278         //printf("makeChunk: %lums\n", t.stop());
279 }
280
281
282 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
283 {
284         float cx, cy, cz, cw, ox, oy, oz, ow;
285
286         if (julia) {  // Julia set
287                 cx = julia_x;
288                 cy = julia_y;
289                 cz = julia_z;
290                 cw = julia_w;
291                 ox = (float)x / scale.X - offset.X;
292                 oy = (float)y / scale.Y - offset.Y;
293                 oz = (float)z / scale.Z - offset.Z;
294                 ow = slice_w;
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;
299                 cw = slice_w;
300                 ox = 0.0f;
301                 oy = 0.0f;
302                 oz = 0.0f;
303                 ow = 0.0f;
304         }
305
306         float nx = 0.0f;
307         float ny = 0.0f;
308         float nz = 0.0f;
309         float nw = 0.0f;
310
311         for (u16 iter = 0; iter < iterations; iter++) {
312                 switch (formula) {
313                 default:
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;
319                         break;
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;
325                         break;
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;
331                         break;
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;
337                         break;
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;
342                         break;
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;
349                         } else {
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;
354                         }
355                         break;
356                 case 7: // 3D "Mandelbulb"
357                         if (std::fabs(oy) < 0.000000001f) {
358                                 nx = ox * ox - oz * oz + cx;
359                                 ny = cy;
360                                 nz = -2.0f * oz * std::sqrt(ox * ox) + cz;
361                         } else {
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;
366                         }
367                         break;
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;
373                         } else {
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;
378                         }
379                         break;
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;
388                         } else {
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;
395                         }
396                         break;
397                 }
398
399                 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
400                         return false;
401
402                 ox = nx;
403                 oy = ny;
404                 oz = nz;
405                 ow = nw;
406         }
407
408         return true;
409 }
410
411
412 s16 MapgenFractal::generateTerrain()
413 {
414         MapNode n_air(CONTENT_AIR);
415         MapNode n_stone(c_stone);
416         MapNode n_water(c_water_source);
417
418         s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
419         u32 index2d = 0;
420
421         if (noise_seabed)
422                 noise_seabed->perlinMap2D(node_min.X, node_min.Z);
423
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)
429                                         continue;
430
431                                 s16 seabed_height = -MAX_MAP_GENERATION_LIMIT;
432                                 if (noise_seabed)
433                                         seabed_height = noise_seabed->result[index2d];
434
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;
442                                 } else {
443                                         vm->m_data[vi] = n_air;
444                                 }
445                         }
446                         index2d -= ystride;
447                 }
448                 index2d += ystride;
449         }
450
451         return stone_surface_max_y;
452 }