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_flat.h"
43 FlagDesc flagdesc_mapgen_flat[] = {
44 {"lakes", MGFLAT_LAKES},
45 {"hills", MGFLAT_HILLS},
49 ///////////////////////////////////////////////////////////////////////////////////////
52 MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge)
53 : Mapgen(mapgenid, params, emerge)
55 this->m_emerge = emerge;
56 this->bmgr = emerge->biomemgr;
58 //// amount of elements to skip for the next index
59 //// for noise/height/biome maps (not vmanip)
60 this->ystride = csize.X;
61 this->zstride = csize.X * (csize.Y + 2);
63 this->biomemap = new u8[csize.X * csize.Z];
64 this->heightmap = new s16[csize.X * csize.Z];
66 this->humidmap = NULL;
68 MapgenFlatParams *sp = (MapgenFlatParams *)params->sparams;
69 this->spflags = sp->spflags;
71 this->ground_level = sp->ground_level;
72 this->large_cave_depth = sp->large_cave_depth;
73 this->lake_threshold = sp->lake_threshold;
74 this->lake_steepness = sp->lake_steepness;
75 this->hill_threshold = sp->hill_threshold;
76 this->hill_steepness = sp->hill_steepness;
79 noise_terrain = new Noise(&sp->np_terrain, seed, csize.X, csize.Z);
80 noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z);
83 noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 2, csize.Z);
84 noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 2, csize.Z);
87 noise_heat = new Noise(¶ms->np_biome_heat, seed, csize.X, csize.Z);
88 noise_humidity = new Noise(¶ms->np_biome_humidity, seed, csize.X, csize.Z);
89 noise_heat_blend = new Noise(¶ms->np_biome_heat_blend, seed, csize.X, csize.Z);
90 noise_humidity_blend = new Noise(¶ms->np_biome_humidity_blend, seed, csize.X, csize.Z);
92 //// Resolve nodes to be used
93 INodeDefManager *ndef = emerge->ndef;
95 c_stone = ndef->getId("mapgen_stone");
96 c_water_source = ndef->getId("mapgen_water_source");
97 c_lava_source = ndef->getId("mapgen_lava_source");
98 c_desert_stone = ndef->getId("mapgen_desert_stone");
99 c_ice = ndef->getId("mapgen_ice");
100 c_sandstone = ndef->getId("mapgen_sandstone");
102 c_cobble = ndef->getId("mapgen_cobble");
103 c_stair_cobble = ndef->getId("mapgen_stair_cobble");
104 c_mossycobble = ndef->getId("mapgen_mossycobble");
105 c_sandstonebrick = ndef->getId("mapgen_sandstonebrick");
106 c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
108 if (c_ice == CONTENT_IGNORE)
110 if (c_mossycobble == CONTENT_IGNORE)
111 c_mossycobble = c_cobble;
112 if (c_stair_cobble == CONTENT_IGNORE)
113 c_stair_cobble = c_cobble;
114 if (c_sandstonebrick == CONTENT_IGNORE)
115 c_sandstonebrick = c_sandstone;
116 if (c_stair_sandstonebrick == CONTENT_IGNORE)
117 c_stair_sandstonebrick = c_sandstone;
121 MapgenFlat::~MapgenFlat()
123 delete noise_terrain;
124 delete noise_filler_depth;
129 delete noise_humidity;
130 delete noise_heat_blend;
131 delete noise_humidity_blend;
138 MapgenFlatParams::MapgenFlatParams()
143 large_cave_depth = -33;
144 lake_threshold = -0.45;
145 lake_steepness = 48.0;
146 hill_threshold = 0.45;
147 hill_steepness = 64.0;
149 np_terrain = NoiseParams(0, 1, v3f(600, 600, 600), 7244, 5, 0.6, 2.0);
150 np_filler_depth = NoiseParams(0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0);
151 np_cave1 = NoiseParams(0, 12, v3f(96, 96, 96), 52534, 4, 0.5, 2.0);
152 np_cave2 = NoiseParams(0, 12, v3f(96, 96, 96), 10325, 4, 0.5, 2.0);
156 void MapgenFlatParams::readParams(const Settings *settings)
158 settings->getFlagStrNoEx("mgflat_spflags", spflags, flagdesc_mapgen_flat);
160 settings->getS16NoEx("mgflat_ground_level", ground_level);
161 settings->getS16NoEx("mgflat_large_cave_depth", large_cave_depth);
162 settings->getFloatNoEx("mgflat_lake_threshold", lake_threshold);
163 settings->getFloatNoEx("mgflat_lake_steepness", lake_steepness);
164 settings->getFloatNoEx("mgflat_hill_threshold", hill_threshold);
165 settings->getFloatNoEx("mgflat_hill_steepness", hill_steepness);
167 settings->getNoiseParams("mgflat_np_terrain", np_terrain);
168 settings->getNoiseParams("mgflat_np_filler_depth", np_filler_depth);
169 settings->getNoiseParams("mgflat_np_cave1", np_cave1);
170 settings->getNoiseParams("mgflat_np_cave2", np_cave2);
174 void MapgenFlatParams::writeParams(Settings *settings) const
176 settings->setFlagStr("mgflat_spflags", spflags, flagdesc_mapgen_flat, U32_MAX);
178 settings->setS16("mgflat_ground_level", ground_level);
179 settings->setS16("mgflat_large_cave_depth", large_cave_depth);
180 settings->setFloat("mgflat_lake_threshold", lake_threshold);
181 settings->setFloat("mgflat_lake_steepness", lake_steepness);
182 settings->setFloat("mgflat_hill_threshold", hill_threshold);
183 settings->setFloat("mgflat_hill_steepness", hill_steepness);
185 settings->setNoiseParams("mgflat_np_terrain", np_terrain);
186 settings->setNoiseParams("mgflat_np_filler_depth", np_filler_depth);
187 settings->setNoiseParams("mgflat_np_cave1", np_cave1);
188 settings->setNoiseParams("mgflat_np_cave2", np_cave2);
192 /////////////////////////////////////////////////////////////////
195 int MapgenFlat::getSpawnLevelAtPoint(v2s16 p)
197 s16 level_at_point = ground_level;
198 float n_terrain = NoisePerlin2D(&noise_terrain->np, p.X, p.Y, seed);
200 if ((spflags & MGFLAT_LAKES) && n_terrain < lake_threshold) {
201 level_at_point = ground_level -
202 (lake_threshold - n_terrain) * lake_steepness;
203 } else if ((spflags & MGFLAT_HILLS) && n_terrain > hill_threshold) {
204 level_at_point = ground_level +
205 (n_terrain - hill_threshold) * hill_steepness;
208 if (ground_level < water_level) // Ocean world, allow spawn in water
209 return MYMAX(level_at_point, water_level);
210 else if (level_at_point > water_level)
211 return level_at_point; // Spawn on land
213 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
217 void MapgenFlat::makeChunk(BlockMakeData *data)
220 assert(data->vmanip);
221 assert(data->nodedef);
222 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
223 data->blockpos_requested.Y >= data->blockpos_min.Y &&
224 data->blockpos_requested.Z >= data->blockpos_min.Z);
225 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
226 data->blockpos_requested.Y <= data->blockpos_max.Y &&
227 data->blockpos_requested.Z <= data->blockpos_max.Z);
229 this->generating = true;
230 this->vm = data->vmanip;
231 this->ndef = data->nodedef;
232 //TimeTaker t("makeChunk");
234 v3s16 blockpos_min = data->blockpos_min;
235 v3s16 blockpos_max = data->blockpos_max;
236 node_min = blockpos_min * MAP_BLOCKSIZE;
237 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
238 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
239 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
241 blockseed = getBlockSeed2(full_node_min, seed);
246 // Generate base terrain, mountains, and ridges with initial heightmaps
247 s16 stone_surface_max_y = generateTerrain();
250 updateHeightmap(node_min, node_max);
252 // Create biomemap at heightmap surface
253 bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result,
254 noise_humidity->result, heightmap, biomemap);
256 // Actually place the biome-specific nodes
257 MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result);
259 if (flags & MG_CAVES)
260 generateCaves(stone_surface_max_y);
262 if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) {
265 dp.np_rarity = nparams_dungeon_rarity;
266 dp.np_density = nparams_dungeon_density;
267 dp.np_wetness = nparams_dungeon_wetness;
268 dp.c_water = c_water_source;
269 if (stone_type == STONE) {
270 dp.c_cobble = c_cobble;
271 dp.c_moss = c_mossycobble;
272 dp.c_stair = c_stair_cobble;
274 dp.diagonal_dirs = false;
276 dp.holesize = v3s16(1, 2, 1);
277 dp.roomsize = v3s16(0, 0, 0);
278 dp.notifytype = GENNOTIFY_DUNGEON;
279 } else if (stone_type == DESERT_STONE) {
280 dp.c_cobble = c_desert_stone;
281 dp.c_moss = c_desert_stone;
282 dp.c_stair = c_desert_stone;
284 dp.diagonal_dirs = true;
286 dp.holesize = v3s16(2, 3, 2);
287 dp.roomsize = v3s16(2, 5, 2);
288 dp.notifytype = GENNOTIFY_TEMPLE;
289 } else if (stone_type == SANDSTONE) {
290 dp.c_cobble = c_sandstonebrick;
291 dp.c_moss = c_sandstonebrick;
292 dp.c_stair = c_sandstonebrick;
294 dp.diagonal_dirs = false;
296 dp.holesize = v3s16(2, 2, 2);
297 dp.roomsize = v3s16(2, 0, 2);
298 dp.notifytype = GENNOTIFY_DUNGEON;
301 DungeonGen dgen(this, &dp);
302 dgen.generate(blockseed, full_node_min, full_node_max);
305 // Generate the registered decorations
306 if (flags & MG_DECORATIONS)
307 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
309 // Generate the registered ores
310 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
312 // Sprinkle some dust on top after everything else was generated
315 //printf("makeChunk: %dms\n", t.stop());
317 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
319 if (flags & MG_LIGHT)
320 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
321 full_node_min, full_node_max);
323 //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
324 // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
326 this->generating = false;
330 void MapgenFlat::calculateNoise()
332 //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
336 if ((spflags & MGFLAT_LAKES) || (spflags & MGFLAT_HILLS))
337 noise_terrain->perlinMap2D(x, z);
339 // Cave noises are calculated in generateCaves()
340 // only if solid terrain is present in mapchunk
342 noise_filler_depth->perlinMap2D(x, z);
343 noise_heat->perlinMap2D(x, z);
344 noise_humidity->perlinMap2D(x, z);
345 noise_heat_blend->perlinMap2D(x, z);
346 noise_humidity_blend->perlinMap2D(x, z);
348 for (s32 i = 0; i < csize.X * csize.Z; i++) {
349 noise_heat->result[i] += noise_heat_blend->result[i];
350 noise_humidity->result[i] += noise_humidity_blend->result[i];
353 heatmap = noise_heat->result;
354 humidmap = noise_humidity->result;
355 //printf("calculateNoise: %dus\n", t.stop());
359 s16 MapgenFlat::generateTerrain()
361 MapNode n_air(CONTENT_AIR);
362 MapNode n_stone(c_stone);
363 MapNode n_water(c_water_source);
365 v3s16 em = vm->m_area.getExtent();
366 s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
369 for (s16 z = node_min.Z; z <= node_max.Z; z++)
370 for (s16 x = node_min.X; x <= node_max.X; x++, ni2d++) {
371 s16 stone_level = ground_level;
372 float n_terrain = 0.0f;
374 if ((spflags & MGFLAT_LAKES) || (spflags & MGFLAT_HILLS))
375 n_terrain = noise_terrain->result[ni2d];
377 if ((spflags & MGFLAT_LAKES) && n_terrain < lake_threshold) {
378 s16 depress = (lake_threshold - n_terrain) * lake_steepness;
379 stone_level = ground_level - depress;
380 } else if ((spflags & MGFLAT_HILLS) && n_terrain > hill_threshold) {
381 s16 rise = (n_terrain - hill_threshold) * hill_steepness;
382 stone_level = ground_level + rise;
385 u32 vi = vm->m_area.index(x, node_min.Y - 1, z);
386 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
387 if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
388 if (y <= stone_level) {
389 vm->m_data[vi] = n_stone;
390 if (y > stone_surface_max_y)
391 stone_surface_max_y = y;
392 } else if (y <= water_level) {
393 vm->m_data[vi] = n_water;
395 vm->m_data[vi] = n_air;
398 vm->m_area.add_y(em, vi, 1);
402 return stone_surface_max_y;
406 MgStoneType MapgenFlat::generateBiomes(float *heat_map, float *humidity_map)
408 v3s16 em = vm->m_area.getExtent();
410 MgStoneType stone_type = STONE;
412 for (s16 z = node_min.Z; z <= node_max.Z; z++)
413 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
417 u16 depth_water_top = 0;
418 u32 vi = vm->m_area.index(x, node_max.Y, z);
420 // Check node at base of mapchunk above, either a node of a previously
421 // generated mapchunk or if not, a node of overgenerated base terrain.
422 content_t c_above = vm->m_data[vi + em.X].getContent();
423 bool air_above = c_above == CONTENT_AIR;
424 bool water_above = c_above == c_water_source;
426 // If there is air or water above enable top/filler placement, otherwise force
427 // nplaced to stone level by setting a number exceeding any possible filler depth.
428 u16 nplaced = (air_above || water_above) ? 0 : U16_MAX;
431 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
432 content_t c = vm->m_data[vi].getContent();
434 // Biome is recalculated each time an upper surface is detected while
435 // working down a column. The selected biome then remains in effect for
436 // all nodes below until the next surface and biome recalculation.
437 // Biome is recalculated:
438 // 1. At the surface of stone below air or water.
439 // 2. At the surface of water below air.
440 // 3. When stone or water is detected but biome has not yet been calculated.
441 if ((c == c_stone && (air_above || water_above || !biome)) ||
442 (c == c_water_source && (air_above || !biome))) {
443 biome = bmgr->getBiome(heat_map[index], humidity_map[index], y);
444 depth_top = biome->depth_top;
445 base_filler = MYMAX(depth_top + biome->depth_filler
446 + noise_filler_depth->result[index], 0);
447 depth_water_top = biome->depth_water_top;
449 // Detect stone type for dungeons during every biome calculation.
450 // This is more efficient than detecting per-node and will not
451 // miss any desert stone or sandstone biomes.
452 if (biome->c_stone == c_desert_stone)
453 stone_type = DESERT_STONE;
454 else if (biome->c_stone == c_sandstone)
455 stone_type = SANDSTONE;
459 content_t c_below = vm->m_data[vi - em.X].getContent();
461 // If the node below isn't solid, make this node stone, so that
462 // any top/filler nodes above are structurally supported.
463 // This is done by aborting the cycle of top/filler placement
464 // immediately by forcing nplaced to stone level.
465 if (c_below == CONTENT_AIR || c_below == c_water_source)
468 if (nplaced < depth_top) {
469 vm->m_data[vi] = MapNode(biome->c_top);
471 } else if (nplaced < base_filler) {
472 vm->m_data[vi] = MapNode(biome->c_filler);
475 vm->m_data[vi] = MapNode(biome->c_stone);
480 } else if (c == c_water_source) {
481 vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ?
482 biome->c_water_top : biome->c_water);
483 nplaced = 0; // Enable top/filler placement for next surface
486 } else if (c == CONTENT_AIR) {
487 nplaced = 0; // Enable top/filler placement for next surface
490 } else { // Possible various nodes overgenerated from neighbouring mapchunks
491 nplaced = U16_MAX; // Disable top/filler placement
496 vm->m_area.add_y(em, vi, -1);
504 void MapgenFlat::dustTopNodes()
506 if (node_max.Y < water_level)
509 v3s16 em = vm->m_area.getExtent();
512 for (s16 z = node_min.Z; z <= node_max.Z; z++)
513 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
514 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]);
516 if (biome->c_dust == CONTENT_IGNORE)
519 u32 vi = vm->m_area.index(x, full_node_max.Y, z);
520 content_t c_full_max = vm->m_data[vi].getContent();
523 if (c_full_max == CONTENT_AIR) {
524 y_start = full_node_max.Y - 1;
525 } else if (c_full_max == CONTENT_IGNORE) {
526 vi = vm->m_area.index(x, node_max.Y + 1, z);
527 content_t c_max = vm->m_data[vi].getContent();
529 if (c_max == CONTENT_AIR)
530 y_start = node_max.Y;
537 vi = vm->m_area.index(x, y_start, z);
538 for (s16 y = y_start; y >= node_min.Y - 1; y--) {
539 if (vm->m_data[vi].getContent() != CONTENT_AIR)
542 vm->m_area.add_y(em, vi, -1);
545 content_t c = vm->m_data[vi].getContent();
546 if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) {
547 vm->m_area.add_y(em, vi, 1);
548 vm->m_data[vi] = MapNode(biome->c_dust);
554 void MapgenFlat::generateCaves(s16 max_stone_y)
556 if (max_stone_y < node_min.Y)
559 noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
560 noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
562 v3s16 em = vm->m_area.getExtent();
565 for (s16 z = node_min.Z; z <= node_max.Z; z++)
566 for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
567 bool column_is_open = false; // Is column open to overground
568 bool is_tunnel = false; // Is tunnel or tunnel floor
569 u32 vi = vm->m_area.index(x, node_max.Y + 1, z);
570 u32 index3d = (z - node_min.Z) * zstride + (csize.Y + 1) * ystride +
573 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]);
575 for (s16 y = node_max.Y + 1; y >= node_min.Y - 1;
576 y--, index3d -= ystride, vm->m_area.add_y(em, vi, -1)) {
577 // Don't excavate the overgenerated stone at node_max.Y + 1,
578 // this creates a 'roof' over the tunnel, preventing light in
579 // tunnels at mapchunk borders when generating mapchunks upwards.
583 content_t c = vm->m_data[vi].getContent();
584 if (c == CONTENT_AIR || c == biome->c_water_top ||
585 c == biome->c_water) {
586 column_is_open = true;
590 float d1 = contour(noise_cave1->result[index3d]);
591 float d2 = contour(noise_cave2->result[index3d]);
592 if (d1 * d2 > 0.3f && ndef->get(c).is_ground_content) {
593 // In tunnel and ground content, excavate
594 vm->m_data[vi] = MapNode(CONTENT_AIR);
596 } else if (is_tunnel && column_is_open &&
597 (c == biome->c_filler || c == biome->c_stone)) {
598 // Tunnel entrance floor
599 vm->m_data[vi] = MapNode(biome->c_top);
600 column_is_open = false;
603 column_is_open = false;
609 if (node_max.Y > large_cave_depth)
612 PseudoRandom ps(blockseed + 21343);
613 u32 bruises_count = ps.range(0, 2);
614 for (u32 i = 0; i < bruises_count; i++) {
615 CaveV5 cave(this, &ps);
616 cave.makeCave(node_min, node_max, max_stone_y);