3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 paramat, Matt Gregory
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.
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.
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.
28 #include "content_sao.h"
30 #include "voxelalgorithms.h"
31 //#include "profiler.h" // For TimeTaker
32 #include "settings.h" // For g_settings
34 #include "dungeongen.h"
39 #include "mg_decoration.h"
40 #include "mapgen_fractal.h"
43 FlagDesc flagdesc_mapgen_fractal[] = {
47 ///////////////////////////////////////////////////////////////////////////////////////
50 MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge)
51 : MapgenBasic(mapgenid, params, emerge)
53 MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams;
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;
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);
71 MapgenBasic::np_cave1 = sp->np_cave1;
72 MapgenBasic::np_cave2 = sp->np_cave2;
74 this->formula = fractal / 2 + fractal % 2;
75 this->julia = fractal % 2 == 0;
79 MapgenFractal::~MapgenFractal()
82 delete noise_filler_depth;
86 MapgenFractalParams::MapgenFractalParams()
92 scale = v3f(4096.0, 1024.0, 4096.0);
93 offset = v3f(1.79, 0.0, 0.0);
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);
107 void MapgenFractalParams::readParams(const Settings *settings)
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);
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);
128 void MapgenFractalParams::writeParams(Settings *settings) const
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);
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);
149 /////////////////////////////////////////////////////////////////
152 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
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)
162 for (s16 y = search_start; y <= search_start + 128; y++) {
163 if (getFractalAtPoint(p.X, y, p.Y)) { // Fractal node
166 } else if (solid_below) { // Air above solid node
173 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
177 void MapgenFractal::makeChunk(BlockMakeData *data)
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);
189 this->generating = true;
190 this->vm = data->vmanip;
191 this->ndef = data->nodedef;
192 //TimeTaker t("makeChunk");
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);
201 blockseed = getBlockSeed2(full_node_min, seed);
203 // Generate base terrain, mountains, and ridges with initial heightmaps
204 s16 stone_surface_max_y = generateTerrain();
207 updateHeightmap(node_min, node_max);
209 // Init biome generator, place biome-specific nodes, and build biomemap
210 biomegen->calcBiomeNoise(node_min);
211 MgStoneType stone_type = generateBiomes();
213 if (flags & MG_CAVES)
214 generateCaves(stone_surface_max_y, MGFRACTAL_LARGE_CAVE_DEPTH);
216 if (flags & MG_DUNGEONS)
217 generateDungeons(stone_surface_max_y, stone_type);
219 // Generate the registered decorations
220 if (flags & MG_DECORATIONS)
221 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
223 // Generate the registered ores
224 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
226 // Sprinkle some dust on top after everything else was generated
229 //printf("makeChunk: %dms\n", t.stop());
231 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
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);
237 //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
238 // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
240 this->generating = false;
244 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
246 float cx, cy, cz, cw, ox, oy, oz, ow;
248 if (julia) { // Julia set
253 ox = (float)x / scale.X - offset.X;
254 oy = (float)y / scale.Y - offset.Y;
255 oz = (float)z / scale.Z - offset.Z;
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;
273 for (u16 iter = 0; iter < iterations; iter++) {
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;
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;
311 } else if (formula == 7) { // 3D "Mandelbulb"
312 if (fabs(oy) < 0.000000001f) {
313 nx = ox * ox - oz * oz + cx;
315 nz = -2.0f * oz * sqrt(ox * ox) + cz;
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;
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;
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;
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;
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;
351 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
364 s16 MapgenFractal::generateTerrain()
366 MapNode n_air(CONTENT_AIR);
367 MapNode n_stone(c_stone);
368 MapNode n_water(c_water_source);
370 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
373 noise_seabed->perlinMap2D(node_min.X, node_min.Z);
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];
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;
389 vm->m_data[vi] = n_air;
398 return stone_surface_max_y;