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, MapgenFractalParams *params, EmergeManager *emerge)
51 : MapgenBasic(mapgenid, params, emerge)
53 this->spflags = params->spflags;
54 this->cave_width = params->cave_width;
55 this->fractal = params->fractal;
56 this->iterations = params->iterations;
57 this->scale = params->scale;
58 this->offset = params->offset;
59 this->slice_w = params->slice_w;
60 this->julia_x = params->julia_x;
61 this->julia_y = params->julia_y;
62 this->julia_z = params->julia_z;
63 this->julia_w = params->julia_w;
66 noise_seabed = new Noise(¶ms->np_seabed, seed, csize.X, csize.Z);
67 noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z);
69 MapgenBasic::np_cave1 = params->np_cave1;
70 MapgenBasic::np_cave2 = params->np_cave2;
72 this->formula = fractal / 2 + fractal % 2;
73 this->julia = fractal % 2 == 0;
77 MapgenFractal::~MapgenFractal()
80 delete noise_filler_depth;
84 MapgenFractalParams::MapgenFractalParams()
90 scale = v3f(4096.0, 1024.0, 4096.0);
91 offset = v3f(1.79, 0.0, 0.0);
98 np_seabed = NoiseParams(-14, 9, v3f(600, 600, 600), 41900, 5, 0.6, 2.0);
99 np_filler_depth = NoiseParams(0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0);
100 np_cave1 = NoiseParams(0, 12, v3f(61, 61, 61), 52534, 3, 0.5, 2.0);
101 np_cave2 = NoiseParams(0, 12, v3f(67, 67, 67), 10325, 3, 0.5, 2.0);
105 void MapgenFractalParams::readParams(const Settings *settings)
107 settings->getFlagStrNoEx("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
108 settings->getFloatNoEx("mgfractal_cave_width", cave_width);
109 settings->getU16NoEx("mgfractal_fractal", fractal);
110 settings->getU16NoEx("mgfractal_iterations", iterations);
111 settings->getV3FNoEx("mgfractal_scale", scale);
112 settings->getV3FNoEx("mgfractal_offset", offset);
113 settings->getFloatNoEx("mgfractal_slice_w", slice_w);
114 settings->getFloatNoEx("mgfractal_julia_x", julia_x);
115 settings->getFloatNoEx("mgfractal_julia_y", julia_y);
116 settings->getFloatNoEx("mgfractal_julia_z", julia_z);
117 settings->getFloatNoEx("mgfractal_julia_w", julia_w);
119 settings->getNoiseParams("mgfractal_np_seabed", np_seabed);
120 settings->getNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
121 settings->getNoiseParams("mgfractal_np_cave1", np_cave1);
122 settings->getNoiseParams("mgfractal_np_cave2", np_cave2);
126 void MapgenFractalParams::writeParams(Settings *settings) const
128 settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal, U32_MAX);
129 settings->setFloat("mgfractal_cave_width", cave_width);
130 settings->setU16("mgfractal_fractal", fractal);
131 settings->setU16("mgfractal_iterations", iterations);
132 settings->setV3F("mgfractal_scale", scale);
133 settings->setV3F("mgfractal_offset", offset);
134 settings->setFloat("mgfractal_slice_w", slice_w);
135 settings->setFloat("mgfractal_julia_x", julia_x);
136 settings->setFloat("mgfractal_julia_y", julia_y);
137 settings->setFloat("mgfractal_julia_z", julia_z);
138 settings->setFloat("mgfractal_julia_w", julia_w);
140 settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
141 settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
142 settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
143 settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
147 /////////////////////////////////////////////////////////////////
150 int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
152 bool solid_below = false; // Dry solid node is present below to spawn on
153 u8 air_count = 0; // Consecutive air nodes above the dry solid node
154 s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
155 // Seabed can rise above water_level or might be raised to create dry land
156 s16 search_start = MYMAX(seabed_level, water_level + 1);
157 if (seabed_level > water_level)
160 for (s16 y = search_start; y <= search_start + 128; y++) {
161 if (getFractalAtPoint(p.X, y, p.Y)) { // Fractal node
164 } else if (solid_below) { // Air above solid node
171 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
175 void MapgenFractal::makeChunk(BlockMakeData *data)
178 assert(data->vmanip);
179 assert(data->nodedef);
180 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
181 data->blockpos_requested.Y >= data->blockpos_min.Y &&
182 data->blockpos_requested.Z >= data->blockpos_min.Z);
183 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
184 data->blockpos_requested.Y <= data->blockpos_max.Y &&
185 data->blockpos_requested.Z <= data->blockpos_max.Z);
187 this->generating = true;
188 this->vm = data->vmanip;
189 this->ndef = data->nodedef;
190 //TimeTaker t("makeChunk");
192 v3s16 blockpos_min = data->blockpos_min;
193 v3s16 blockpos_max = data->blockpos_max;
194 node_min = blockpos_min * MAP_BLOCKSIZE;
195 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
196 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
197 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
199 blockseed = getBlockSeed2(full_node_min, seed);
201 // Generate base terrain, mountains, and ridges with initial heightmaps
202 s16 stone_surface_max_y = generateTerrain();
205 updateHeightmap(node_min, node_max);
207 // Init biome generator, place biome-specific nodes, and build biomemap
208 biomegen->calcBiomeNoise(node_min);
209 MgStoneType stone_type = generateBiomes();
211 if (flags & MG_CAVES)
212 generateCaves(stone_surface_max_y, MGFRACTAL_LARGE_CAVE_DEPTH);
214 if (flags & MG_DUNGEONS)
215 generateDungeons(stone_surface_max_y, stone_type);
217 // Generate the registered decorations
218 if (flags & MG_DECORATIONS)
219 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
221 // Generate the registered ores
222 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
224 // Sprinkle some dust on top after everything else was generated
227 //printf("makeChunk: %dms\n", t.stop());
229 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
231 if (flags & MG_LIGHT)
232 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
233 full_node_min, full_node_max);
235 //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
236 // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
238 this->generating = false;
242 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
244 float cx, cy, cz, cw, ox, oy, oz, ow;
246 if (julia) { // Julia set
251 ox = (float)x / scale.X - offset.X;
252 oy = (float)y / scale.Y - offset.Y;
253 oz = (float)z / scale.Z - offset.Z;
255 } else { // Mandelbrot set
256 cx = (float)x / scale.X - offset.X;
257 cy = (float)y / scale.Y - offset.Y;
258 cz = (float)z / scale.Z - offset.Z;
271 for (u16 iter = 0; iter < iterations; iter++) {
273 if (formula == 1) { // 4D "Roundy"
274 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
275 ny = 2.0f * (ox * oy + oz * ow) + cy;
276 nz = 2.0f * (ox * oz + oy * ow) + cz;
277 nw = 2.0f * (ox * ow + oy * oz) + cw;
278 } else if (formula == 2) { // 4D "Squarry"
279 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
280 ny = 2.0f * (ox * oy + oz * ow) + cy;
281 nz = 2.0f * (ox * oz + oy * ow) + cz;
282 nw = 2.0f * (ox * ow - oy * oz) + cw;
283 } else if (formula == 3) { // 4D "Mandy Cousin"
284 nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
285 ny = 2.0f * (ox * oy + oz * ow) + cy;
286 nz = 2.0f * (ox * oz + oy * ow) + cz;
287 nw = 2.0f * (ox * ow + oy * oz) + cw;
288 } else if (formula == 4) { // 4D "Variation"
289 nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
290 ny = 2.0f * (ox * oy + oz * ow) + cy;
291 nz = 2.0f * (ox * oz - oy * ow) + cz;
292 nw = 2.0f * (ox * ow + oy * oz) + cw;
293 } else if (formula == 5) { // 3D "Mandelbrot/Mandelbar"
294 nx = ox * ox - oy * oy - oz * oz + cx;
295 ny = 2.0f * ox * oy + cy;
296 nz = -2.0f * ox * oz + cz;
297 } else if (formula == 6) { // 3D "Christmas Tree"
298 // Altering the formula here is necessary to avoid division by zero
299 if (fabs(oz) < 0.000000001f) {
300 nx = ox * ox - oy * oy - oz * oz + cx;
301 ny = 2.0f * oy * ox + cy;
302 nz = 4.0f * oz * ox + cz;
304 float a = (2.0f * ox) / (sqrt(oy * oy + oz * oz));
305 nx = ox * ox - oy * oy - oz * oz + cx;
306 ny = a * (oy * oy - oz * oz) + cy;
307 nz = a * 2.0f * oy * oz + cz;
309 } else if (formula == 7) { // 3D "Mandelbulb"
310 if (fabs(oy) < 0.000000001f) {
311 nx = ox * ox - oz * oz + cx;
313 nz = -2.0f * oz * sqrt(ox * ox) + cz;
315 float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
316 nx = (ox * ox - oy * oy) * a + cx;
317 ny = 2.0f * ox * oy * a + cy;
318 nz = -2.0f * oz * sqrt(ox * ox + oy * oy) + cz;
320 } else if (formula == 8) { // 3D "Cosine Mandelbulb"
321 if (fabs(oy) < 0.000000001f) {
322 nx = 2.0f * ox * oz + cx;
323 ny = 4.0f * oy * oz + cy;
324 nz = oz * oz - ox * ox - oy * oy + cz;
326 float a = (2.0f * oz) / sqrt(ox * ox + oy * oy);
327 nx = (ox * ox - oy * oy) * a + cx;
328 ny = 2.0f * ox * oy * a + cy;
329 nz = oz * oz - ox * ox - oy * oy + cz;
331 } else if (formula == 9) { // 4D "Mandelbulb"
332 float rxy = sqrt(ox * ox + oy * oy);
333 float rxyz = sqrt(ox * ox + oy * oy + oz * oz);
334 if (fabs(ow) < 0.000000001f && fabs(oz) < 0.000000001f) {
335 nx = (ox * ox - oy * oy) + cx;
336 ny = 2.0f * ox * oy + cy;
337 nz = -2.0f * rxy * oz + cz;
338 nw = 2.0f * rxyz * ow + cw;
340 float a = 1.0f - (ow * ow) / (rxyz * rxyz);
341 float b = a * (1.0f - (oz * oz) / (rxy * rxy));
342 nx = (ox * ox - oy * oy) * b + cx;
343 ny = 2.0f * ox * oy * b + cy;
344 nz = -2.0f * rxy * oz * a + cz;
345 nw = 2.0f * rxyz * ow + cw;
349 if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
362 s16 MapgenFractal::generateTerrain()
364 MapNode n_air(CONTENT_AIR);
365 MapNode n_stone(c_stone);
366 MapNode n_water(c_water_source);
368 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
371 noise_seabed->perlinMap2D(node_min.X, node_min.Z);
373 for (s16 z = node_min.Z; z <= node_max.Z; z++) {
374 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
375 u32 vi = vm->m_area.index(node_min.X, y, z);
376 for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
377 if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
378 s16 seabed_height = noise_seabed->result[index2d];
380 if (y <= seabed_height || getFractalAtPoint(x, y, z)) {
381 vm->m_data[vi] = n_stone;
382 if (y > stone_surface_max_y)
383 stone_surface_max_y = y;
384 } else if (y <= water_level) {
385 vm->m_data[vi] = n_water;
387 vm->m_data[vi] = n_air;
396 return stone_surface_max_y;