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