Cpp11 initializers 2 (#5999)
[oweals/minetest.git] / src / mapgen_fractal.cpp
1 /*
2 Minetest
3 Copyright (C) 2015-2017 paramat
4 Copyright (C) 2015-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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 "voxel.h"
24 #include "noise.h"
25 #include "mapblock.h"
26 #include "mapnode.h"
27 #include "map.h"
28 #include "content_sao.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         {NULL,    0}
44 };
45
46 ///////////////////////////////////////////////////////////////////////////////////////
47
48
49 MapgenFractal::MapgenFractal(int mapgenid, MapgenFractalParams *params, EmergeManager *emerge)
50         : MapgenBasic(mapgenid, params, emerge)
51 {
52         this->spflags    = params->spflags;
53         this->cave_width = params->cave_width;
54         this->fractal    = params->fractal;
55         this->iterations = params->iterations;
56         this->scale      = params->scale;
57         this->offset     = params->offset;
58         this->slice_w    = params->slice_w;
59         this->julia_x    = params->julia_x;
60         this->julia_y    = params->julia_y;
61         this->julia_z    = params->julia_z;
62         this->julia_w    = params->julia_w;
63
64         //// 2D terrain noise
65         noise_seabed       = new Noise(&params->np_seabed, seed, csize.X, csize.Z);
66         noise_filler_depth = new Noise(&params->np_filler_depth, seed, csize.X, csize.Z);
67
68         MapgenBasic::np_cave1 = params->np_cave1;
69         MapgenBasic::np_cave2 = params->np_cave2;
70
71         this->formula = fractal / 2 + fractal % 2;
72         this->julia   = fractal % 2 == 0;
73 }
74
75
76 MapgenFractal::~MapgenFractal()
77 {
78         delete noise_seabed;
79         delete noise_filler_depth;
80 }
81
82
83 MapgenFractalParams::MapgenFractalParams()
84 {
85         np_seabed       = NoiseParams(-14, 9,   v3f(600, 600, 600), 41900, 5, 0.6, 2.0);
86         np_filler_depth = NoiseParams(0,   1.2, v3f(150, 150, 150), 261,   3, 0.7, 2.0);
87         np_cave1        = NoiseParams(0,   12,  v3f(61,  61,  61),  52534, 3, 0.5, 2.0);
88         np_cave2        = NoiseParams(0,   12,  v3f(67,  67,  67),  10325, 3, 0.5, 2.0);
89 }
90
91
92 void MapgenFractalParams::readParams(const Settings *settings)
93 {
94         settings->getFlagStrNoEx("mgfractal_spflags",  spflags, flagdesc_mapgen_fractal);
95         settings->getFloatNoEx("mgfractal_cave_width", cave_width);
96         settings->getU16NoEx("mgfractal_fractal",      fractal);
97         settings->getU16NoEx("mgfractal_iterations",   iterations);
98         settings->getV3FNoEx("mgfractal_scale",        scale);
99         settings->getV3FNoEx("mgfractal_offset",       offset);
100         settings->getFloatNoEx("mgfractal_slice_w",    slice_w);
101         settings->getFloatNoEx("mgfractal_julia_x",    julia_x);
102         settings->getFloatNoEx("mgfractal_julia_y",    julia_y);
103         settings->getFloatNoEx("mgfractal_julia_z",    julia_z);
104         settings->getFloatNoEx("mgfractal_julia_w",    julia_w);
105
106         settings->getNoiseParams("mgfractal_np_seabed",       np_seabed);
107         settings->getNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
108         settings->getNoiseParams("mgfractal_np_cave1",        np_cave1);
109         settings->getNoiseParams("mgfractal_np_cave2",        np_cave2);
110 }
111
112
113 void MapgenFractalParams::writeParams(Settings *settings) const
114 {
115         settings->setFlagStr("mgfractal_spflags",  spflags, flagdesc_mapgen_fractal, U32_MAX);
116         settings->setFloat("mgfractal_cave_width", cave_width);
117         settings->setU16("mgfractal_fractal",      fractal);
118         settings->setU16("mgfractal_iterations",   iterations);
119         settings->setV3F("mgfractal_scale",        scale);
120         settings->setV3F("mgfractal_offset",       offset);
121         settings->setFloat("mgfractal_slice_w",    slice_w);
122         settings->setFloat("mgfractal_julia_x",    julia_x);
123         settings->setFloat("mgfractal_julia_y",    julia_y);
124         settings->setFloat("mgfractal_julia_z",    julia_z);
125         settings->setFloat("mgfractal_julia_w",    julia_w);
126
127         settings->setNoiseParams("mgfractal_np_seabed",       np_seabed);
128         settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
129         settings->setNoiseParams("mgfractal_np_cave1",        np_cave1);
130         settings->setNoiseParams("mgfractal_np_cave2",        np_cave2);
131 }
132
133
134 /////////////////////////////////////////////////////////////////
135
136
137 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
138 {
139         bool solid_below = false;  // Dry solid node is present below to spawn on
140         u8 air_count = 0;  // Consecutive air nodes above the dry solid node
141         s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
142         // Seabed can rise above water_level or might be raised to create dry land
143         s16 search_start = MYMAX(seabed_level, water_level + 1);
144         if (seabed_level > water_level)
145                 solid_below = true;
146
147         for (s16 y = search_start; y <= search_start + 128; y++) {
148                 if (getFractalAtPoint(p.X, y, p.Y)) {  // Fractal node
149                         solid_below = true;
150                         air_count = 0;
151                 } else if (solid_below) {  // Air above solid node
152                         air_count++;
153                         if (air_count == 2)
154                                 return y - 2;
155                 }
156         }
157
158         return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
159 }
160
161
162 void MapgenFractal::makeChunk(BlockMakeData *data)
163 {
164         // Pre-conditions
165         assert(data->vmanip);
166         assert(data->nodedef);
167         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
168                 data->blockpos_requested.Y >= data->blockpos_min.Y &&
169                 data->blockpos_requested.Z >= data->blockpos_min.Z);
170         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
171                 data->blockpos_requested.Y <= data->blockpos_max.Y &&
172                 data->blockpos_requested.Z <= data->blockpos_max.Z);
173
174         this->generating = true;
175         this->vm   = data->vmanip;
176         this->ndef = data->nodedef;
177         //TimeTaker t("makeChunk");
178
179         v3s16 blockpos_min = data->blockpos_min;
180         v3s16 blockpos_max = data->blockpos_max;
181         node_min = blockpos_min * MAP_BLOCKSIZE;
182         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
183         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
184         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
185
186         blockseed = getBlockSeed2(full_node_min, seed);
187
188         // Generate base terrain, mountains, and ridges with initial heightmaps
189         s16 stone_surface_max_y = generateTerrain();
190
191         // Create heightmap
192         updateHeightmap(node_min, node_max);
193
194         // Init biome generator, place biome-specific nodes, and build biomemap
195         biomegen->calcBiomeNoise(node_min);
196         MgStoneType stone_type = generateBiomes();
197
198         if (flags & MG_CAVES)
199                 generateCaves(stone_surface_max_y, MGFRACTAL_LARGE_CAVE_DEPTH);
200
201         if (flags & MG_DUNGEONS)
202                 generateDungeons(stone_surface_max_y, stone_type);
203
204         // Generate the registered decorations
205         if (flags & MG_DECORATIONS)
206                 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
207
208         // Generate the registered ores
209         m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
210
211         // Sprinkle some dust on top after everything else was generated
212         dustTopNodes();
213
214         //printf("makeChunk: %dms\n", t.stop());
215
216         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
217
218         if (flags & MG_LIGHT)
219                 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
220                         full_node_min, full_node_max);
221
222         //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
223         //                      node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
224
225         this->generating = false;
226 }
227
228
229 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
230 {
231         float cx, cy, cz, cw, ox, oy, oz, ow;
232
233         if (julia) {  // Julia set
234                 cx = julia_x;
235                 cy = julia_y;
236                 cz = julia_z;
237                 cw = julia_w;
238                 ox = (float)x / scale.X - offset.X;
239                 oy = (float)y / scale.Y - offset.Y;
240                 oz = (float)z / scale.Z - offset.Z;
241                 ow = slice_w;
242         } else {  // Mandelbrot set
243                 cx = (float)x / scale.X - offset.X;
244                 cy = (float)y / scale.Y - offset.Y;
245                 cz = (float)z / scale.Z - offset.Z;
246                 cw = slice_w;
247                 ox = 0.0f;
248                 oy = 0.0f;
249                 oz = 0.0f;
250                 ow = 0.0f;
251         }
252
253         float nx = 0.0f;
254         float ny = 0.0f;
255         float nz = 0.0f;
256         float nw = 0.0f;
257
258         for (u16 iter = 0; iter < iterations; iter++) {
259
260                 if (formula == 1) {  // 4D "Roundy"
261                         nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
262                         ny = 2.0f * (ox * oy + oz * ow) + cy;
263                         nz = 2.0f * (ox * oz + oy * ow) + cz;
264                         nw = 2.0f * (ox * ow + oy * oz) + cw;
265                 } else if (formula == 2) {  // 4D "Squarry"
266                         nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
267                         ny = 2.0f * (ox * oy + oz * ow) + cy;
268                         nz = 2.0f * (ox * oz + oy * ow) + cz;
269                         nw = 2.0f * (ox * ow - oy * oz) + cw;
270                 } else if (formula == 3) {  // 4D "Mandy Cousin"
271                         nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
272                         ny = 2.0f * (ox * oy + oz * ow) + cy;
273                         nz = 2.0f * (ox * oz + oy * ow) + cz;
274                         nw = 2.0f * (ox * ow + oy * oz) + cw;
275                 } else if (formula == 4) {  // 4D "Variation"
276                         nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
277                         ny = 2.0f * (ox * oy + oz * ow) + cy;
278                         nz = 2.0f * (ox * oz - oy * ow) + cz;
279                         nw = 2.0f * (ox * ow + oy * oz) + cw;
280                 } else if (formula == 5) {  // 3D "Mandelbrot/Mandelbar"
281                         nx = ox * ox - oy * oy - oz * oz + cx;
282                         ny = 2.0f * ox * oy + cy;
283                         nz = -2.0f * ox * oz + cz;
284                 } else if (formula == 6) {  // 3D "Christmas Tree"
285                         // Altering the formula here is necessary to avoid division by zero
286                         if (fabs(oz) < 0.000000001f) {
287                                 nx = ox * ox - oy * oy - oz * oz + cx;
288                                 ny = 2.0f * oy * ox + cy;
289                                 nz = 4.0f * oz * ox + cz;
290                         } else {
291                                 float a = (2.0f * ox) / (sqrt(oy * oy + oz * oz));
292                                 nx = ox * ox - oy * oy - oz * oz + cx;
293                                 ny = a * (oy * oy - oz * oz) + cy;
294                                 nz = a * 2.0f * oy * oz + cz;
295                         }
296                 } else if (formula == 7) {  // 3D "Mandelbulb"
297                         if (fabs(oy) < 0.000000001f) {
298                                 nx = ox * ox - oz * oz + cx;
299                                 ny = cy;
300                                 nz = -2.0f * oz * sqrt(ox * ox) + cz;
301                         } else {
302                                 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
303                                 nx = (ox * ox - oy * oy) * a + cx;
304                                 ny = 2.0f * ox * oy * a + cy;
305                                 nz = -2.0f * oz * sqrt(ox * ox + oy * oy) + cz;
306                         }
307                 } else if (formula == 8) {  // 3D "Cosine Mandelbulb"
308                         if (fabs(oy) < 0.000000001f) {
309                                 nx = 2.0f * ox * oz + cx;
310                                 ny = 4.0f * oy * oz + cy;
311                                 nz = oz * oz - ox * ox - oy * oy + cz;
312                         } else {
313                                 float a = (2.0f * oz) / sqrt(ox * ox + oy * oy);
314                                 nx = (ox * ox - oy * oy) * a + cx;
315                                 ny = 2.0f * ox * oy * a + cy;
316                                 nz = oz * oz - ox * ox - oy * oy + cz;
317                         }
318                 } else if (formula == 9) {  // 4D "Mandelbulb"
319                         float rxy = sqrt(ox * ox + oy * oy);
320                         float rxyz = sqrt(ox * ox + oy * oy + oz * oz);
321                         if (fabs(ow) < 0.000000001f && fabs(oz) < 0.000000001f) {
322                                 nx = (ox * ox - oy * oy) + cx;
323                                 ny = 2.0f * ox * oy + cy;
324                                 nz = -2.0f * rxy * oz + cz;
325                                 nw = 2.0f * rxyz * ow + cw;
326                         } else {
327                                 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
328                                 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
329                                 nx = (ox * ox - oy * oy) * b + cx;
330                                 ny = 2.0f * ox * oy * b + cy;
331                                 nz = -2.0f * rxy * oz * a + cz;
332                                 nw = 2.0f * rxyz * ow + cw;
333                         }
334                 }
335
336                 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
337                         return false;
338
339                 ox = nx;
340                 oy = ny;
341                 oz = nz;
342                 ow = nw;
343         }
344
345         return true;
346 }
347
348
349 s16 MapgenFractal::generateTerrain()
350 {
351         MapNode n_air(CONTENT_AIR);
352         MapNode n_stone(c_stone);
353         MapNode n_water(c_water_source);
354
355         s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
356         u32 index2d = 0;
357
358         noise_seabed->perlinMap2D(node_min.X, node_min.Z);
359
360         for (s16 z = node_min.Z; z <= node_max.Z; z++) {
361                 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
362                         u32 vi = vm->m_area.index(node_min.X, y, z);
363                         for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
364                                 if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
365                                         s16 seabed_height = noise_seabed->result[index2d];
366
367                                         if (y <= seabed_height || getFractalAtPoint(x, y, z)) {
368                                                 vm->m_data[vi] = n_stone;
369                                                 if (y > stone_surface_max_y)
370                                                         stone_surface_max_y = y;
371                                         } else if (y <= water_level) {
372                                                 vm->m_data[vi] = n_water;
373                                         } else {
374                                                 vm->m_data[vi] = n_air;
375                                         }
376                                 }
377                         }
378                         index2d -= ystride;
379                 }
380                 index2d += ystride;
381         }
382
383         return stone_surface_max_y;
384 }