3 Copyright (C) 2013-2019 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2014-2019 paramat
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.
29 #include "content_sao.h"
31 #include "voxelalgorithms.h"
32 //#include "profiler.h" // For TimeTaker
33 #include "settings.h" // For g_settings
35 #include "dungeongen.h"
39 #include "mg_decoration.h"
40 #include "mapgen_v7.h"
43 FlagDesc flagdesc_mapgen_v7[] = {
44 {"mountains", MGV7_MOUNTAINS},
45 {"ridges", MGV7_RIDGES},
46 {"floatlands", MGV7_FLOATLANDS},
47 {"caverns", MGV7_CAVERNS},
52 ////////////////////////////////////////////////////////////////////////////////
55 MapgenV7::MapgenV7(MapgenV7Params *params, EmergeManager *emerge)
56 : MapgenBasic(MAPGEN_V7, params, emerge)
58 spflags = params->spflags;
59 mount_zero_level = params->mount_zero_level;
61 cave_width = params->cave_width;
62 large_cave_depth = params->large_cave_depth;
63 small_cave_num_min = params->small_cave_num_min;
64 small_cave_num_max = params->small_cave_num_max;
65 large_cave_num_min = params->large_cave_num_min;
66 large_cave_num_max = params->large_cave_num_max;
67 large_cave_flooded = params->large_cave_flooded;
68 cavern_limit = params->cavern_limit;
69 cavern_taper = params->cavern_taper;
70 cavern_threshold = params->cavern_threshold;
71 dungeon_ymin = params->dungeon_ymin;
72 dungeon_ymax = params->dungeon_ymax;
76 new Noise(¶ms->np_terrain_base, seed, csize.X, csize.Z);
78 new Noise(¶ms->np_terrain_alt, seed, csize.X, csize.Z);
79 noise_terrain_persist =
80 new Noise(¶ms->np_terrain_persist, seed, csize.X, csize.Z);
82 new Noise(¶ms->np_height_select, seed, csize.X, csize.Z);
84 new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z);
86 if (spflags & MGV7_MOUNTAINS) {
89 new Noise(¶ms->np_mount_height, seed, csize.X, csize.Z);
90 // 3D noise, 1 up, 1 down overgeneration
92 new Noise(¶ms->np_mountain, seed, csize.X, csize.Y + 2, csize.Z);
95 if (spflags & MGV7_RIDGES) {
98 new Noise(¶ms->np_ridge_uwater, seed, csize.X, csize.Z);
99 // 3D noise, 1 up, 1 down overgeneration
101 new Noise(¶ms->np_ridge, seed, csize.X, csize.Y + 2, csize.Z);
104 // 3D noise, 1 down overgeneration
105 MapgenBasic::np_cave1 = params->np_cave1;
106 MapgenBasic::np_cave2 = params->np_cave2;
107 MapgenBasic::np_cavern = params->np_cavern;
109 MapgenBasic::np_dungeons = params->np_dungeons;
113 MapgenV7::~MapgenV7()
115 delete noise_terrain_base;
116 delete noise_terrain_alt;
117 delete noise_terrain_persist;
118 delete noise_height_select;
119 delete noise_filler_depth;
121 if (spflags & MGV7_MOUNTAINS) {
122 delete noise_mount_height;
123 delete noise_mountain;
126 if (spflags & MGV7_RIDGES) {
127 delete noise_ridge_uwater;
133 MapgenV7Params::MapgenV7Params():
134 np_terrain_base (4.0, 70.0, v3f(600, 600, 600), 82341, 5, 0.6, 2.0),
135 np_terrain_alt (4.0, 25.0, v3f(600, 600, 600), 5934, 5, 0.6, 2.0),
136 np_terrain_persist (0.6, 0.1, v3f(2000, 2000, 2000), 539, 3, 0.6, 2.0),
137 np_height_select (-8.0, 16.0, v3f(500, 500, 500), 4213, 6, 0.7, 2.0),
138 np_filler_depth (0.0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0),
139 np_mount_height (256.0, 112.0, v3f(1000, 1000, 1000), 72449, 3, 0.6, 2.0),
140 np_ridge_uwater (0.0, 1.0, v3f(1000, 1000, 1000), 85039, 5, 0.6, 2.0),
141 np_mountain (-0.6, 1.0, v3f(250, 350, 250), 5333, 5, 0.63, 2.0),
142 np_ridge (0.0, 1.0, v3f(100, 100, 100), 6467, 4, 0.75, 2.0),
143 np_cavern (0.0, 1.0, v3f(384, 128, 384), 723, 5, 0.63, 2.0),
144 np_cave1 (0.0, 12.0, v3f(61, 61, 61), 52534, 3, 0.5, 2.0),
145 np_cave2 (0.0, 12.0, v3f(67, 67, 67), 10325, 3, 0.5, 2.0),
146 np_dungeons (0.9, 0.5, v3f(500, 500, 500), 0, 2, 0.8, 2.0)
151 void MapgenV7Params::readParams(const Settings *settings)
153 settings->getFlagStrNoEx("mgv7_spflags", spflags, flagdesc_mapgen_v7);
154 settings->getS16NoEx("mgv7_mount_zero_level", mount_zero_level);
155 settings->getFloatNoEx("mgv7_cave_width", cave_width);
156 settings->getS16NoEx("mgv7_large_cave_depth", large_cave_depth);
157 settings->getU16NoEx("mgv7_small_cave_num_min", small_cave_num_min);
158 settings->getU16NoEx("mgv7_small_cave_num_max", small_cave_num_max);
159 settings->getU16NoEx("mgv7_large_cave_num_min", large_cave_num_min);
160 settings->getU16NoEx("mgv7_large_cave_num_max", large_cave_num_max);
161 settings->getFloatNoEx("mgv7_large_cave_flooded", large_cave_flooded);
162 settings->getS16NoEx("mgv7_cavern_limit", cavern_limit);
163 settings->getS16NoEx("mgv7_cavern_taper", cavern_taper);
164 settings->getFloatNoEx("mgv7_cavern_threshold", cavern_threshold);
165 settings->getS16NoEx("mgv7_dungeon_ymin", dungeon_ymin);
166 settings->getS16NoEx("mgv7_dungeon_ymax", dungeon_ymax);
168 settings->getNoiseParams("mgv7_np_terrain_base", np_terrain_base);
169 settings->getNoiseParams("mgv7_np_terrain_alt", np_terrain_alt);
170 settings->getNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
171 settings->getNoiseParams("mgv7_np_height_select", np_height_select);
172 settings->getNoiseParams("mgv7_np_filler_depth", np_filler_depth);
173 settings->getNoiseParams("mgv7_np_mount_height", np_mount_height);
174 settings->getNoiseParams("mgv7_np_ridge_uwater", np_ridge_uwater);
175 settings->getNoiseParams("mgv7_np_mountain", np_mountain);
176 settings->getNoiseParams("mgv7_np_ridge", np_ridge);
177 settings->getNoiseParams("mgv7_np_cavern", np_cavern);
178 settings->getNoiseParams("mgv7_np_cave1", np_cave1);
179 settings->getNoiseParams("mgv7_np_cave2", np_cave2);
180 settings->getNoiseParams("mgv7_np_dungeons", np_dungeons);
184 void MapgenV7Params::writeParams(Settings *settings) const
186 settings->setFlagStr("mgv7_spflags", spflags, flagdesc_mapgen_v7);
187 settings->setS16("mgv7_mount_zero_level", mount_zero_level);
188 settings->setFloat("mgv7_cave_width", cave_width);
189 settings->setS16("mgv7_large_cave_depth", large_cave_depth);
190 settings->setU16("mgv7_small_cave_num_min", small_cave_num_min);
191 settings->setU16("mgv7_small_cave_num_max", small_cave_num_max);
192 settings->setU16("mgv7_large_cave_num_min", large_cave_num_min);
193 settings->setU16("mgv7_large_cave_num_max", large_cave_num_max);
194 settings->setFloat("mgv7_large_cave_flooded", large_cave_flooded);
195 settings->setS16("mgv7_cavern_limit", cavern_limit);
196 settings->setS16("mgv7_cavern_taper", cavern_taper);
197 settings->setFloat("mgv7_cavern_threshold", cavern_threshold);
198 settings->setS16("mgv7_dungeon_ymin", dungeon_ymin);
199 settings->setS16("mgv7_dungeon_ymax", dungeon_ymax);
201 settings->setNoiseParams("mgv7_np_terrain_base", np_terrain_base);
202 settings->setNoiseParams("mgv7_np_terrain_alt", np_terrain_alt);
203 settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
204 settings->setNoiseParams("mgv7_np_height_select", np_height_select);
205 settings->setNoiseParams("mgv7_np_filler_depth", np_filler_depth);
206 settings->setNoiseParams("mgv7_np_mount_height", np_mount_height);
207 settings->setNoiseParams("mgv7_np_ridge_uwater", np_ridge_uwater);
208 settings->setNoiseParams("mgv7_np_mountain", np_mountain);
209 settings->setNoiseParams("mgv7_np_ridge", np_ridge);
210 settings->setNoiseParams("mgv7_np_cavern", np_cavern);
211 settings->setNoiseParams("mgv7_np_cave1", np_cave1);
212 settings->setNoiseParams("mgv7_np_cave2", np_cave2);
213 settings->setNoiseParams("mgv7_np_dungeons", np_dungeons);
217 void MapgenV7Params::setDefaultSettings(Settings *settings)
219 settings->setDefault("mgv7_spflags", flagdesc_mapgen_v7,
220 MGV7_MOUNTAINS | MGV7_RIDGES | MGV7_CAVERNS);
224 ////////////////////////////////////////////////////////////////////////////////
227 int MapgenV7::getSpawnLevelAtPoint(v2s16 p)
229 // If rivers are enabled, first check if in a river
230 if (spflags & MGV7_RIDGES) {
232 float uwatern = NoisePerlin2D(&noise_ridge_uwater->np, p.X, p.Y, seed) *
234 if (std::fabs(uwatern) <= width)
235 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
238 // Terrain noise 'offset' is the average level of that terrain.
239 // At least 50% of terrain will be below the higher of base and alt terrain
241 // Raising the maximum spawn level above 'water_level + 16' is necessary
242 // for when terrain 'offset's are set much higher than water_level.
243 s16 max_spawn_y = std::fmax(std::fmax(noise_terrain_alt->np.offset,
244 noise_terrain_base->np.offset),
246 // Base terrain calculation
247 s16 y = baseTerrainLevelAtPoint(p.X, p.Y);
249 // If mountains are disabled, terrain level is base terrain level.
250 // Avoids mid-air spawn where mountain terrain would have been.
251 if (!(spflags & MGV7_MOUNTAINS)) {
252 if (y < water_level || y > max_spawn_y)
253 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
255 // y + 2 because y is surface level and due to biome 'dust'
259 // Search upwards for first node without mountain terrain
261 while (iters > 0 && y <= max_spawn_y) {
262 if (!getMountainTerrainAtPoint(p.X, y + 1, p.Y)) {
263 if (y <= water_level || y > max_spawn_y)
264 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
266 // y + 1 due to biome 'dust'
273 // Unsuitable spawn point
274 return MAX_MAP_GENERATION_LIMIT;
278 void MapgenV7::makeChunk(BlockMakeData *data)
281 assert(data->vmanip);
282 assert(data->nodedef);
283 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
284 data->blockpos_requested.Y >= data->blockpos_min.Y &&
285 data->blockpos_requested.Z >= data->blockpos_min.Z);
286 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
287 data->blockpos_requested.Y <= data->blockpos_max.Y &&
288 data->blockpos_requested.Z <= data->blockpos_max.Z);
290 //TimeTaker t("makeChunk");
292 this->generating = true;
293 this->vm = data->vmanip;
294 this->ndef = data->nodedef;
296 v3s16 blockpos_min = data->blockpos_min;
297 v3s16 blockpos_max = data->blockpos_max;
298 node_min = blockpos_min * MAP_BLOCKSIZE;
299 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
300 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
301 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
303 blockseed = getBlockSeed2(full_node_min, seed);
305 // Generate base and mountain terrain
306 s16 stone_surface_max_y = generateTerrain();
309 if (spflags & MGV7_RIDGES)
310 generateRidgeTerrain();
313 updateHeightmap(node_min, node_max);
315 // Init biome generator, place biome-specific nodes, and build biomemap
316 if (flags & MG_BIOMES) {
317 biomegen->calcBiomeNoise(node_min);
321 // Generate tunnels, caverns and large randomwalk caves
322 if (flags & MG_CAVES) {
323 // Generate tunnels first as caverns confuse them
324 generateCavesNoiseIntersection(stone_surface_max_y);
327 bool near_cavern = false;
328 if (spflags & MGV7_CAVERNS)
329 near_cavern = generateCavernsNoise(stone_surface_max_y);
331 // Generate large randomwalk caves
333 // Disable large randomwalk caves in this mapchunk by setting
334 // 'large cave depth' to world base. Avoids excessive liquid in
335 // large caverns and floating blobs of overgenerated liquid.
336 generateCavesRandomWalk(stone_surface_max_y,
337 -MAX_MAP_GENERATION_LIMIT);
339 generateCavesRandomWalk(stone_surface_max_y, large_cave_depth);
342 // Generate the registered ores
343 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
346 if (flags & MG_DUNGEONS)
347 generateDungeons(stone_surface_max_y);
349 // Generate the registered decorations
350 if (flags & MG_DECORATIONS)
351 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
353 // Sprinkle some dust on top after everything else was generated
354 if (flags & MG_BIOMES)
358 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
360 // Calculate lighting
361 // TODO disable in and just below floatlands
362 bool propagate_shadow = true;
364 if (flags & MG_LIGHT)
365 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
366 full_node_min, full_node_max, propagate_shadow);
368 this->generating = false;
370 //printf("makeChunk: %lums\n", t.stop());
374 ////////////////////////////////////////////////////////////////////////////////
377 float MapgenV7::baseTerrainLevelAtPoint(s16 x, s16 z)
379 float hselect = NoisePerlin2D(&noise_height_select->np, x, z, seed);
380 hselect = rangelim(hselect, 0.0f, 1.0f);
382 float persist = NoisePerlin2D(&noise_terrain_persist->np, x, z, seed);
384 noise_terrain_base->np.persist = persist;
385 float height_base = NoisePerlin2D(&noise_terrain_base->np, x, z, seed);
387 noise_terrain_alt->np.persist = persist;
388 float height_alt = NoisePerlin2D(&noise_terrain_alt->np, x, z, seed);
390 if (height_alt > height_base)
393 return (height_base * hselect) + (height_alt * (1.0f - hselect));
397 float MapgenV7::baseTerrainLevelFromMap(int index)
399 float hselect = rangelim(noise_height_select->result[index], 0.0f, 1.0f);
400 float height_base = noise_terrain_base->result[index];
401 float height_alt = noise_terrain_alt->result[index];
403 if (height_alt > height_base)
406 return (height_base * hselect) + (height_alt * (1.0f - hselect));
410 bool MapgenV7::getMountainTerrainAtPoint(s16 x, s16 y, s16 z)
413 std::fmax(NoisePerlin2D(&noise_mount_height->np, x, z, seed), 1.0f);
414 float density_gradient = -((float)(y - mount_zero_level) / mnt_h_n);
415 float mnt_n = NoisePerlin3D(&noise_mountain->np, x, y, z, seed);
417 return mnt_n + density_gradient >= 0.0f;
421 bool MapgenV7::getMountainTerrainFromMap(int idx_xyz, int idx_xz, s16 y)
423 float mounthn = std::fmax(noise_mount_height->result[idx_xz], 1.0f);
424 float density_gradient = -((float)(y - mount_zero_level) / mounthn);
425 float mountn = noise_mountain->result[idx_xyz];
427 return mountn + density_gradient >= 0.0f;
431 int MapgenV7::generateTerrain()
433 MapNode n_air(CONTENT_AIR);
434 MapNode n_stone(c_stone);
435 MapNode n_water(c_water_source);
437 //// Calculate noise for terrain generation
438 noise_terrain_persist->perlinMap2D(node_min.X, node_min.Z);
439 float *persistmap = noise_terrain_persist->result;
441 noise_terrain_base->perlinMap2D(node_min.X, node_min.Z, persistmap);
442 noise_terrain_alt->perlinMap2D(node_min.X, node_min.Z, persistmap);
443 noise_height_select->perlinMap2D(node_min.X, node_min.Z);
445 if (spflags & MGV7_MOUNTAINS) {
446 noise_mount_height->perlinMap2D(node_min.X, node_min.Z);
447 noise_mountain->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
451 const v3s16 &em = vm->m_area.getExtent();
452 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
455 for (s16 z = node_min.Z; z <= node_max.Z; z++)
456 for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
457 s16 surface_y = baseTerrainLevelFromMap(index2d);
458 if (surface_y > stone_surface_max_y)
459 stone_surface_max_y = surface_y;
461 u32 vi = vm->m_area.index(x, node_min.Y - 1, z);
462 u32 index3d = (z - node_min.Z) * zstride_1u1d + (x - node_min.X);
464 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1;
467 VoxelArea::add_y(em, vi, 1)) {
468 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
471 if (y <= surface_y) {
472 vm->m_data[vi] = n_stone; // Base terrain
473 } else if ((spflags & MGV7_MOUNTAINS) &&
474 getMountainTerrainFromMap(index3d, index2d, y)) {
475 vm->m_data[vi] = n_stone; // Mountain terrain
476 if (y > stone_surface_max_y)
477 stone_surface_max_y = y;
478 } else if (y <= water_level) {
479 vm->m_data[vi] = n_water;
481 vm->m_data[vi] = n_air;
486 return stone_surface_max_y;
490 void MapgenV7::generateRidgeTerrain()
492 // TODO disable river canyons in floatlands
493 if (node_max.Y < water_level - 16)
496 noise_ridge->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
497 noise_ridge_uwater->perlinMap2D(node_min.X, node_min.Z);
499 MapNode n_water(c_water_source);
500 MapNode n_air(CONTENT_AIR);
504 for (s16 z = node_min.Z; z <= node_max.Z; z++)
505 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
506 u32 vi = vm->m_area.index(node_min.X, y, z);
507 for (s16 x = node_min.X; x <= node_max.X; x++, index3d++, vi++) {
508 u32 index2d = (z - node_min.Z) * csize.X + (x - node_min.X);
509 float uwatern = noise_ridge_uwater->result[index2d] * 2.0f;
510 if (std::fabs(uwatern) > width)
512 // Optimises, but also avoids removing nodes placed by mods in
513 // 'on-generated', when generating outside mapchunk.
514 content_t c = vm->m_data[vi].getContent();
518 float altitude = y - water_level;
519 float height_mod = (altitude + 17.0f) / 2.5f;
520 float width_mod = width - std::fabs(uwatern);
521 float nridge = noise_ridge->result[index3d] *
522 std::fmax(altitude, 0.0f) / 7.0f;
523 if (nridge + width_mod * height_mod < 0.6f)
526 vm->m_data[vi] = (y > water_level) ? n_air : n_water;