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