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