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