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