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