3217d52e2f0812f5613ff84db70de98955ebe1fa
[oweals/minetest.git] / src / mapgen_fractal.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 paramat, Matt Gregory
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 "treegen.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(int mapgenid, MapgenParams *params, EmergeManager *emerge)
51         : MapgenBasic(mapgenid, params, emerge)
52 {
53         this->m_emerge = emerge;
54         this->bmgr     = emerge->biomemgr;
55
56         //// amount of elements to skip for the next index
57         //// for noise/height/biome maps (not vmanip)
58         this->ystride = csize.X;
59         // 1-down overgeneration
60         this->zstride_1d = csize.X * (csize.Y + 1);
61
62         this->heightmap = new s16[csize.X * csize.Z];
63
64         MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams;
65
66         this->spflags    = sp->spflags;
67         this->cave_width = sp->cave_width;
68         this->fractal    = sp->fractal;
69         this->iterations = sp->iterations;
70         this->scale      = sp->scale;
71         this->offset     = sp->offset;
72         this->slice_w    = sp->slice_w;
73         this->julia_x    = sp->julia_x;
74         this->julia_y    = sp->julia_y;
75         this->julia_z    = sp->julia_z;
76         this->julia_w    = sp->julia_w;
77
78         //// 2D terrain noise
79         noise_seabed       = new Noise(&sp->np_seabed, seed, csize.X, csize.Z);
80         noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z);
81
82         //// 3D terrain noise
83         // 1-down overgeneraion
84         noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z);
85         noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z);
86
87         //// Initialize biome generator
88         biomegen = emerge->biomemgr->createBiomeGen(
89                 BIOMEGEN_ORIGINAL, params->bparams, csize);
90         biomemap = biomegen->biomemap;
91
92         this->formula = fractal / 2 + fractal % 2;
93         this->julia   = fractal % 2 == 0;
94
95         //// Resolve nodes to be used
96         c_stone                = ndef->getId("mapgen_stone");
97         c_water_source         = ndef->getId("mapgen_water_source");
98         c_lava_source          = ndef->getId("mapgen_lava_source");
99         c_desert_stone         = ndef->getId("mapgen_desert_stone");
100         c_ice                  = ndef->getId("mapgen_ice");
101         c_sandstone            = ndef->getId("mapgen_sandstone");
102
103         c_cobble               = ndef->getId("mapgen_cobble");
104         c_stair_cobble         = ndef->getId("mapgen_stair_cobble");
105         c_mossycobble          = ndef->getId("mapgen_mossycobble");
106         c_sandstonebrick       = ndef->getId("mapgen_sandstonebrick");
107         c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
108
109         if (c_ice == CONTENT_IGNORE)
110                 c_ice = CONTENT_AIR;
111         if (c_mossycobble == CONTENT_IGNORE)
112                 c_mossycobble = c_cobble;
113         if (c_stair_cobble == CONTENT_IGNORE)
114                 c_stair_cobble = c_cobble;
115         if (c_sandstonebrick == CONTENT_IGNORE)
116                 c_sandstonebrick = c_sandstone;
117         if (c_stair_sandstonebrick == CONTENT_IGNORE)
118                 c_stair_sandstonebrick = c_sandstone;
119 }
120
121
122 MapgenFractal::~MapgenFractal()
123 {
124         delete noise_seabed;
125         delete noise_filler_depth;
126         delete noise_cave1;
127         delete noise_cave2;
128
129         delete biomegen;
130
131         delete[] heightmap;
132 }
133
134
135 MapgenFractalParams::MapgenFractalParams()
136 {
137         spflags    = 0;
138         cave_width = 0.3;
139         fractal    = 1;
140         iterations = 11;
141         scale      = v3f(4096.0, 1024.0, 4096.0);
142         offset     = v3f(1.79, 0.0, 0.0);
143         slice_w    = 0.0;
144         julia_x    = 0.33;
145         julia_y    = 0.33;
146         julia_z    = 0.33;
147         julia_w    = 0.33;
148
149         np_seabed       = NoiseParams(-14, 9,   v3f(600, 600, 600), 41900, 5, 0.6, 2.0);
150         np_filler_depth = NoiseParams(0,   1.2, v3f(150, 150, 150), 261,   3, 0.7, 2.0);
151         np_cave1        = NoiseParams(0,   12,  v3f(96,  96,  96),  52534, 4, 0.5, 2.0);
152         np_cave2        = NoiseParams(0,   12,  v3f(96,  96,  96),  10325, 4, 0.5, 2.0);
153 }
154
155
156 void MapgenFractalParams::readParams(const Settings *settings)
157 {
158         settings->getFlagStrNoEx("mgfractal_spflags",  spflags, flagdesc_mapgen_fractal);
159         settings->getFloatNoEx("mgfractal_cave_width", cave_width);
160         settings->getU16NoEx("mgfractal_fractal",      fractal);
161         settings->getU16NoEx("mgfractal_iterations",   iterations);
162         settings->getV3FNoEx("mgfractal_scale",        scale);
163         settings->getV3FNoEx("mgfractal_offset",       offset);
164         settings->getFloatNoEx("mgfractal_slice_w",    slice_w);
165         settings->getFloatNoEx("mgfractal_julia_x",    julia_x);
166         settings->getFloatNoEx("mgfractal_julia_y",    julia_y);
167         settings->getFloatNoEx("mgfractal_julia_z",    julia_z);
168         settings->getFloatNoEx("mgfractal_julia_w",    julia_w);
169
170         settings->getNoiseParams("mgfractal_np_seabed",       np_seabed);
171         settings->getNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
172         settings->getNoiseParams("mgfractal_np_cave1",        np_cave1);
173         settings->getNoiseParams("mgfractal_np_cave2",        np_cave2);
174 }
175
176
177 void MapgenFractalParams::writeParams(Settings *settings) const
178 {
179         settings->setFlagStr("mgfractal_spflags",  spflags, flagdesc_mapgen_fractal, U32_MAX);
180         settings->setFloat("mgfractal_cave_width", cave_width);
181         settings->setU16("mgfractal_fractal",      fractal);
182         settings->setU16("mgfractal_iterations",   iterations);
183         settings->setV3F("mgfractal_scale",        scale);
184         settings->setV3F("mgfractal_offset",       offset);
185         settings->setFloat("mgfractal_slice_w",    slice_w);
186         settings->setFloat("mgfractal_julia_x",    julia_x);
187         settings->setFloat("mgfractal_julia_y",    julia_y);
188         settings->setFloat("mgfractal_julia_z",    julia_z);
189         settings->setFloat("mgfractal_julia_w",    julia_w);
190
191         settings->setNoiseParams("mgfractal_np_seabed",       np_seabed);
192         settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
193         settings->setNoiseParams("mgfractal_np_cave1",        np_cave1);
194         settings->setNoiseParams("mgfractal_np_cave2",        np_cave2);
195 }
196
197
198 /////////////////////////////////////////////////////////////////
199
200
201 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
202 {
203         bool solid_below = false;  // Dry solid node is present below to spawn on
204         u8 air_count = 0;  // Consecutive air nodes above the dry solid node
205         s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
206         // Seabed can rise above water_level or might be raised to create dry land
207         s16 search_start = MYMAX(seabed_level, water_level + 1);
208         if (seabed_level > water_level)
209                 solid_below = true;
210
211         for (s16 y = search_start; y <= search_start + 128; y++) {
212                 if (getFractalAtPoint(p.X, y, p.Y)) {  // Fractal node
213                         solid_below = true;
214                         air_count = 0;
215                 } else if (solid_below) {  // Air above solid node
216                         air_count++;
217                         if (air_count == 2)
218                                 return y - 2;
219                 }
220         }
221
222         return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
223 }
224
225
226 void MapgenFractal::makeChunk(BlockMakeData *data)
227 {
228         // Pre-conditions
229         assert(data->vmanip);
230         assert(data->nodedef);
231         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
232                 data->blockpos_requested.Y >= data->blockpos_min.Y &&
233                 data->blockpos_requested.Z >= data->blockpos_min.Z);
234         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
235                 data->blockpos_requested.Y <= data->blockpos_max.Y &&
236                 data->blockpos_requested.Z <= data->blockpos_max.Z);
237
238         this->generating = true;
239         this->vm   = data->vmanip;
240         this->ndef = data->nodedef;
241         //TimeTaker t("makeChunk");
242
243         v3s16 blockpos_min = data->blockpos_min;
244         v3s16 blockpos_max = data->blockpos_max;
245         node_min = blockpos_min * MAP_BLOCKSIZE;
246         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
247         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
248         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
249
250         blockseed = getBlockSeed2(full_node_min, seed);
251
252         // Make some noise
253         calculateNoise();
254
255         // Generate base terrain, mountains, and ridges with initial heightmaps
256         s16 stone_surface_max_y = generateTerrain();
257
258         // Create heightmap
259         updateHeightmap(node_min, node_max);
260
261         // Init biome generator, place biome-specific nodes, and build biomemap
262         biomegen->calcBiomeNoise(node_min);
263         biomegen->getBiomes(heightmap);
264         MgStoneType stone_type = generateBiomes();
265
266         if (flags & MG_CAVES)
267                 generateCaves(stone_surface_max_y, MGFRACTAL_LARGE_CAVE_DEPTH);
268
269         if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) {
270                 DungeonParams dp;
271
272                 dp.np_rarity  = nparams_dungeon_rarity;
273                 dp.np_density = nparams_dungeon_density;
274                 dp.np_wetness = nparams_dungeon_wetness;
275                 dp.c_water    = c_water_source;
276                 if (stone_type == MGSTONE_STONE) {
277                         dp.c_cobble = c_cobble;
278                         dp.c_moss   = c_mossycobble;
279                         dp.c_stair  = c_stair_cobble;
280
281                         dp.diagonal_dirs = false;
282                         dp.mossratio     = 3.0;
283                         dp.holesize      = v3s16(1, 2, 1);
284                         dp.roomsize      = v3s16(0, 0, 0);
285                         dp.notifytype    = GENNOTIFY_DUNGEON;
286                 } else if (stone_type == MGSTONE_DESERT_STONE) {
287                         dp.c_cobble = c_desert_stone;
288                         dp.c_moss   = c_desert_stone;
289                         dp.c_stair  = c_desert_stone;
290
291                         dp.diagonal_dirs = true;
292                         dp.mossratio     = 0.0;
293                         dp.holesize      = v3s16(2, 3, 2);
294                         dp.roomsize      = v3s16(2, 5, 2);
295                         dp.notifytype    = GENNOTIFY_TEMPLE;
296                 } else if (stone_type == MGSTONE_SANDSTONE) {
297                         dp.c_cobble = c_sandstonebrick;
298                         dp.c_moss   = c_sandstonebrick;
299                         dp.c_stair  = c_sandstonebrick;
300
301                         dp.diagonal_dirs = false;
302                         dp.mossratio     = 0.0;
303                         dp.holesize      = v3s16(2, 2, 2);
304                         dp.roomsize      = v3s16(2, 0, 2);
305                         dp.notifytype    = GENNOTIFY_DUNGEON;
306                 }
307
308                 DungeonGen dgen(this, &dp);
309                 dgen.generate(blockseed, full_node_min, full_node_max);
310         }
311
312         // Generate the registered decorations
313         if (flags & MG_DECORATIONS)
314                 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
315
316         // Generate the registered ores
317         m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
318
319         // Sprinkle some dust on top after everything else was generated
320         dustTopNodes();
321
322         //printf("makeChunk: %dms\n", t.stop());
323
324         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
325
326         if (flags & MG_LIGHT)
327                 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
328                         full_node_min, full_node_max);
329
330         //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
331         //                      node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
332
333         this->generating = false;
334 }
335
336
337 void MapgenFractal::calculateNoise()
338 {
339         //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
340         s16 x = node_min.X;
341         s16 z = node_min.Z;
342
343         noise_seabed->perlinMap2D(x, z);
344
345         // Cave noises are calculated in generateCaves()
346         // only if solid terrain is present in mapchunk
347
348         noise_filler_depth->perlinMap2D(x, z);
349
350         //printf("calculateNoise: %dus\n", t.stop());
351 }
352
353
354 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
355 {
356         float cx, cy, cz, cw, ox, oy, oz, ow;
357
358         if (julia) {  // Julia set
359                 cx = julia_x;
360                 cy = julia_y;
361                 cz = julia_z;
362                 cw = julia_w;
363                 ox = (float)x / scale.X - offset.X;
364                 oy = (float)y / scale.Y - offset.Y;
365                 oz = (float)z / scale.Z - offset.Z;
366                 ow = slice_w;
367         } else {  // Mandelbrot set
368                 cx = (float)x / scale.X - offset.X;
369                 cy = (float)y / scale.Y - offset.Y;
370                 cz = (float)z / scale.Z - offset.Z;
371                 cw = slice_w;
372                 ox = 0.0f;
373                 oy = 0.0f;
374                 oz = 0.0f;
375                 ow = 0.0f;
376         }
377
378         float nx = 0.0f;
379         float ny = 0.0f;
380         float nz = 0.0f;
381         float nw = 0.0f;
382
383         for (u16 iter = 0; iter < iterations; iter++) {
384
385                 if (formula == 1) {  // 4D "Roundy"
386                         nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
387                         ny = 2.0f * (ox * oy + oz * ow) + cy;
388                         nz = 2.0f * (ox * oz + oy * ow) + cz;
389                         nw = 2.0f * (ox * ow + oy * oz) + cw;
390                 } else if (formula == 2) {  // 4D "Squarry"
391                         nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
392                         ny = 2.0f * (ox * oy + oz * ow) + cy;
393                         nz = 2.0f * (ox * oz + oy * ow) + cz;
394                         nw = 2.0f * (ox * ow - oy * oz) + cw;
395                 } else if (formula == 3) {  // 4D "Mandy Cousin"
396                         nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
397                         ny = 2.0f * (ox * oy + oz * ow) + cy;
398                         nz = 2.0f * (ox * oz + oy * ow) + cz;
399                         nw = 2.0f * (ox * ow + oy * oz) + cw;
400                 } else if (formula == 4) {  // 4D "Variation"
401                         nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
402                         ny = 2.0f * (ox * oy + oz * ow) + cy;
403                         nz = 2.0f * (ox * oz - oy * ow) + cz;
404                         nw = 2.0f * (ox * ow + oy * oz) + cw;
405                 } else if (formula == 5) {  // 3D "Mandelbrot/Mandelbar"
406                         nx = ox * ox - oy * oy - oz * oz + cx;
407                         ny = 2.0f * ox * oy + cy;
408                         nz = -2.0f * ox * oz + cz;
409                 } else if (formula == 6) {  // 3D "Christmas Tree"
410                         // Altering the formula here is necessary to avoid division by zero
411                         if (fabs(oz) < 0.000000001f) {
412                                 nx = ox * ox - oy * oy - oz * oz + cx;
413                                 ny = 2.0f * oy * ox + cy;
414                                 nz = 4.0f * oz * ox + cz;
415                         } else {
416                                 float a = (2.0f * ox) / (sqrt(oy * oy + oz * oz));
417                                 nx = ox * ox - oy * oy - oz * oz + cx;
418                                 ny = a * (oy * oy - oz * oz) + cy;
419                                 nz = a * 2.0f * oy * oz + cz;
420                         }
421                 } else if (formula == 7) {  // 3D "Mandelbulb"
422                         if (fabs(oy) < 0.000000001f) {
423                                 nx = ox * ox - oz * oz + cx;
424                                 ny = cy;
425                                 nz = -2.0f * oz * sqrt(ox * ox) + cz;
426                         } else {
427                                 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
428                                 nx = (ox * ox - oy * oy) * a + cx;
429                                 ny = 2.0f * ox * oy * a + cy;
430                                 nz = -2.0f * oz * sqrt(ox * ox + oy * oy) + cz;
431                         }
432                 } else if (formula == 8) {  // 3D "Cosine Mandelbulb"
433                         if (fabs(oy) < 0.000000001f) {
434                                 nx = 2.0f * ox * oz + cx;
435                                 ny = 4.0f * oy * oz + cy;
436                                 nz = oz * oz - ox * ox - oy * oy + cz;
437                         } else {
438                                 float a = (2.0f * oz) / sqrt(ox * ox + oy * oy);
439                                 nx = (ox * ox - oy * oy) * a + cx;
440                                 ny = 2.0f * ox * oy * a + cy;
441                                 nz = oz * oz - ox * ox - oy * oy + cz;
442                         }
443                 } else if (formula == 9) {  // 4D "Mandelbulb"
444                         float rxy = sqrt(ox * ox + oy * oy);
445                         float rxyz = sqrt(ox * ox + oy * oy + oz * oz);
446                         if (fabs(ow) < 0.000000001f && fabs(oz) < 0.000000001f) {
447                                 nx = (ox * ox - oy * oy) + cx;
448                                 ny = 2.0f * ox * oy + cy;
449                                 nz = -2.0f * rxy * oz + cz;
450                                 nw = 2.0f * rxyz * ow + cw;
451                         } else {
452                                 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
453                                 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
454                                 nx = (ox * ox - oy * oy) * b + cx;
455                                 ny = 2.0f * ox * oy * b + cy;
456                                 nz = -2.0f * rxy * oz * a + cz;
457                                 nw = 2.0f * rxyz * ow + cw;
458                         }
459                 }
460
461                 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
462                         return false;
463
464                 ox = nx;
465                 oy = ny;
466                 oz = nz;
467                 ow = nw;
468         }
469
470         return true;
471 }
472
473
474 s16 MapgenFractal::generateTerrain()
475 {
476         MapNode n_air(CONTENT_AIR);
477         MapNode n_stone(c_stone);
478         MapNode n_water(c_water_source);
479
480         s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
481         u32 index2d = 0;
482
483         for (s16 z = node_min.Z; z <= node_max.Z; z++) {
484                 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
485                         u32 vi = vm->m_area.index(node_min.X, y, z);
486                         for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
487                                 if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
488                                         s16 seabed_height = noise_seabed->result[index2d];
489
490                                         if (y <= seabed_height || getFractalAtPoint(x, y, z)) {
491                                                 vm->m_data[vi] = n_stone;
492                                                 if (y > stone_surface_max_y)
493                                                         stone_surface_max_y = y;
494                                         } else if (y <= water_level) {
495                                                 vm->m_data[vi] = n_water;
496                                         } else {
497                                                 vm->m_data[vi] = n_air;
498                                         }
499                                 }
500                         }
501                         index2d -= ystride;
502                 }
503                 index2d += ystride;
504         }
505
506         return stone_surface_max_y;
507 }