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