3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "content_sao.h"
29 #include "voxelalgorithms.h"
30 //#include "profiler.h" // For TimeTaker
31 #include "settings.h" // For g_settings
33 #include "dungeongen.h"
38 #include "mg_decoration.h"
39 #include "mapgen_v7.h"
42 FlagDesc flagdesc_mapgen_v7[] = {
43 {"mountains", MGV7_MOUNTAINS},
44 {"ridges", MGV7_RIDGES},
48 ///////////////////////////////////////////////////////////////////////////////
51 MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge)
52 : Mapgen(mapgenid, params, emerge)
54 this->m_emerge = emerge;
55 this->bmgr = emerge->biomemgr;
57 //// amount of elements to skip for the next index
58 //// for noise/height/biome maps (not vmanip)
59 this->ystride = csize.X;
60 this->zstride = csize.X * (csize.Y + 2);
62 this->biomemap = new u8[csize.X * csize.Z];
63 this->heightmap = new s16[csize.X * csize.Z];
65 this->humidmap = NULL;
66 this->ridge_heightmap = new s16[csize.X * csize.Z];
68 MapgenV7Params *sp = (MapgenV7Params *)params->sparams;
69 this->spflags = sp->spflags;
72 noise_terrain_base = new Noise(&sp->np_terrain_base, seed, csize.X, csize.Z);
73 noise_terrain_alt = new Noise(&sp->np_terrain_alt, seed, csize.X, csize.Z);
74 noise_terrain_persist = new Noise(&sp->np_terrain_persist, seed, csize.X, csize.Z);
75 noise_height_select = new Noise(&sp->np_height_select, seed, csize.X, csize.Z);
76 noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z);
77 noise_mount_height = new Noise(&sp->np_mount_height, seed, csize.X, csize.Z);
78 noise_ridge_uwater = new Noise(&sp->np_ridge_uwater, seed, csize.X, csize.Z);
81 noise_mountain = new Noise(&sp->np_mountain, seed, csize.X, csize.Y + 2, csize.Z);
82 noise_ridge = new Noise(&sp->np_ridge, seed, csize.X, csize.Y + 2, 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 MapgenV7::~MapgenV7()
123 delete noise_terrain_base;
124 delete noise_terrain_persist;
125 delete noise_height_select;
126 delete noise_terrain_alt;
127 delete noise_filler_depth;
128 delete noise_mount_height;
129 delete noise_ridge_uwater;
130 delete noise_mountain;
136 delete noise_humidity;
137 delete noise_heat_blend;
138 delete noise_humidity_blend;
140 delete[] ridge_heightmap;
146 MapgenV7Params::MapgenV7Params()
148 spflags = MGV7_MOUNTAINS | MGV7_RIDGES;
150 np_terrain_base = NoiseParams(4, 70, v3f(600, 600, 600), 82341, 5, 0.6, 2.0);
151 np_terrain_alt = NoiseParams(4, 25, v3f(600, 600, 600), 5934, 5, 0.6, 2.0);
152 np_terrain_persist = NoiseParams(0.6, 0.1, v3f(2000, 2000, 2000), 539, 3, 0.6, 2.0);
153 np_height_select = NoiseParams(-12, 24, v3f(500, 500, 500), 4213, 6, 0.7, 2.0);
154 np_filler_depth = NoiseParams(0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0);
155 np_mount_height = NoiseParams(256, 112, v3f(1000, 1000, 1000), 72449, 3, 0.6, 2.0);
156 np_ridge_uwater = NoiseParams(0, 1, v3f(1000, 1000, 1000), 85039, 5, 0.6, 2.0);
157 np_mountain = NoiseParams(-0.6, 1, v3f(250, 350, 250), 5333, 5, 0.63, 2.0);
158 np_ridge = NoiseParams(0, 1, v3f(100, 100, 100), 6467, 4, 0.75, 2.0);
159 np_cave1 = NoiseParams(0, 12, v3f(100, 100, 100), 52534, 4, 0.5, 2.0);
160 np_cave2 = NoiseParams(0, 12, v3f(100, 100, 100), 10325, 4, 0.5, 2.0);
164 void MapgenV7Params::readParams(const Settings *settings)
166 settings->getFlagStrNoEx("mgv7_spflags", spflags, flagdesc_mapgen_v7);
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_cave1", np_cave1);
178 settings->getNoiseParams("mgv7_np_cave2", np_cave2);
182 void MapgenV7Params::writeParams(Settings *settings) const
184 settings->setFlagStr("mgv7_spflags", spflags, flagdesc_mapgen_v7, U32_MAX);
186 settings->setNoiseParams("mgv7_np_terrain_base", np_terrain_base);
187 settings->setNoiseParams("mgv7_np_terrain_alt", np_terrain_alt);
188 settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
189 settings->setNoiseParams("mgv7_np_height_select", np_height_select);
190 settings->setNoiseParams("mgv7_np_filler_depth", np_filler_depth);
191 settings->setNoiseParams("mgv7_np_mount_height", np_mount_height);
192 settings->setNoiseParams("mgv7_np_ridge_uwater", np_ridge_uwater);
193 settings->setNoiseParams("mgv7_np_mountain", np_mountain);
194 settings->setNoiseParams("mgv7_np_ridge", np_ridge);
195 settings->setNoiseParams("mgv7_np_cave1", np_cave1);
196 settings->setNoiseParams("mgv7_np_cave2", np_cave2);
200 ///////////////////////////////////////
203 int MapgenV7::getGroundLevelAtPoint(v2s16 p)
205 // Base terrain calculation
206 s16 y = baseTerrainLevelAtPoint(p.X, p.Y);
208 // Ridge/river terrain calculation
210 float uwatern = NoisePerlin2D(&noise_ridge_uwater->np, p.X, p.Y, seed) * 2;
211 // actually computing the depth of the ridge is much more expensive;
212 // if inside a river, simply guess
213 if (fabs(uwatern) <= width)
214 return water_level - 10;
216 // Mountain terrain calculation
217 int iters = 128; // don't even bother iterating more than 128 times..
219 //current point would have been air
220 if (!getMountainTerrainAtPoint(p.X, y, p.Y))
230 void MapgenV7::makeChunk(BlockMakeData *data)
233 assert(data->vmanip);
234 assert(data->nodedef);
235 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
236 data->blockpos_requested.Y >= data->blockpos_min.Y &&
237 data->blockpos_requested.Z >= data->blockpos_min.Z);
238 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
239 data->blockpos_requested.Y <= data->blockpos_max.Y &&
240 data->blockpos_requested.Z <= data->blockpos_max.Z);
242 this->generating = true;
243 this->vm = data->vmanip;
244 this->ndef = data->nodedef;
245 //TimeTaker t("makeChunk");
247 v3s16 blockpos_min = data->blockpos_min;
248 v3s16 blockpos_max = data->blockpos_max;
249 node_min = blockpos_min * MAP_BLOCKSIZE;
250 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
251 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
252 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
254 blockseed = getBlockSeed2(full_node_min, seed);
259 // Generate base terrain, mountains, and ridges with initial heightmaps
260 s16 stone_surface_max_y = generateTerrain();
263 updateHeightmap(node_min, node_max);
265 // Create biomemap at heightmap surface
266 bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result,
267 noise_humidity->result, heightmap, biomemap);
269 // Actually place the biome-specific nodes
270 MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result);
272 if (flags & MG_CAVES)
273 generateCaves(stone_surface_max_y);
275 if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) {
278 dp.np_rarity = nparams_dungeon_rarity;
279 dp.np_density = nparams_dungeon_density;
280 dp.np_wetness = nparams_dungeon_wetness;
281 dp.c_water = c_water_source;
282 if (stone_type == STONE) {
283 dp.c_cobble = c_cobble;
284 dp.c_moss = c_mossycobble;
285 dp.c_stair = c_stair_cobble;
287 dp.diagonal_dirs = false;
289 dp.holesize = v3s16(1, 2, 1);
290 dp.roomsize = v3s16(0, 0, 0);
291 dp.notifytype = GENNOTIFY_DUNGEON;
292 } else if (stone_type == DESERT_STONE) {
293 dp.c_cobble = c_desert_stone;
294 dp.c_moss = c_desert_stone;
295 dp.c_stair = c_desert_stone;
297 dp.diagonal_dirs = true;
299 dp.holesize = v3s16(2, 3, 2);
300 dp.roomsize = v3s16(2, 5, 2);
301 dp.notifytype = GENNOTIFY_TEMPLE;
302 } else if (stone_type == SANDSTONE) {
303 dp.c_cobble = c_sandstonebrick;
304 dp.c_moss = c_sandstonebrick;
305 dp.c_stair = c_sandstonebrick;
307 dp.diagonal_dirs = false;
309 dp.holesize = v3s16(2, 2, 2);
310 dp.roomsize = v3s16(2, 0, 2);
311 dp.notifytype = GENNOTIFY_DUNGEON;
314 DungeonGen dgen(this, &dp);
315 dgen.generate(blockseed, full_node_min, full_node_max);
318 // Generate the registered decorations
319 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
321 // Generate the registered ores
322 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
324 // Sprinkle some dust on top after everything else was generated
327 //printf("makeChunk: %dms\n", t.stop());
329 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
331 if (flags & MG_LIGHT)
332 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
333 full_node_min, full_node_max);
335 //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
336 // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
338 this->generating = false;
342 void MapgenV7::calculateNoise()
344 //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
346 int y = node_min.Y - 1;
349 noise_terrain_persist->perlinMap2D(x, z);
350 float *persistmap = noise_terrain_persist->result;
352 noise_terrain_base->perlinMap2D(x, z, persistmap);
353 noise_terrain_alt->perlinMap2D(x, z, persistmap);
354 noise_height_select->perlinMap2D(x, z);
356 if (flags & MG_CAVES) {
357 noise_cave1->perlinMap3D(x, y, z);
358 noise_cave2->perlinMap3D(x, y, z);
361 if ((spflags & MGV7_RIDGES) && node_max.Y >= water_level) {
362 noise_ridge->perlinMap3D(x, y, z);
363 noise_ridge_uwater->perlinMap2D(x, z);
366 // Mountain noises are calculated in generateMountainTerrain()
368 noise_filler_depth->perlinMap2D(x, z);
369 noise_heat->perlinMap2D(x, z);
370 noise_humidity->perlinMap2D(x, z);
371 noise_heat_blend->perlinMap2D(x, z);
372 noise_humidity_blend->perlinMap2D(x, z);
374 for (s32 i = 0; i < csize.X * csize.Z; i++) {
375 noise_heat->result[i] += noise_heat_blend->result[i];
376 noise_humidity->result[i] += noise_humidity_blend->result[i];
379 heatmap = noise_heat->result;
380 humidmap = noise_humidity->result;
381 //printf("calculateNoise: %dus\n", t.stop());
385 Biome *MapgenV7::getBiomeAtPoint(v3s16 p)
387 float heat = NoisePerlin2D(&noise_heat->np, p.X, p.Z, seed) +
388 NoisePerlin2D(&noise_heat_blend->np, p.X, p.Z, seed);
389 float humidity = NoisePerlin2D(&noise_humidity->np, p.X, p.Z, seed) +
390 NoisePerlin2D(&noise_humidity_blend->np, p.X, p.Z, seed);
391 s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
393 return bmgr->getBiome(heat, humidity, groundlevel);
396 //needs to be updated
397 float MapgenV7::baseTerrainLevelAtPoint(s16 x, s16 z)
399 float hselect = NoisePerlin2D(&noise_height_select->np, x, z, seed);
400 hselect = rangelim(hselect, 0.0, 1.0);
402 float persist = NoisePerlin2D(&noise_terrain_persist->np, x, z, seed);
404 noise_terrain_base->np.persist = persist;
405 float height_base = NoisePerlin2D(&noise_terrain_base->np, x, z, seed);
407 noise_terrain_alt->np.persist = persist;
408 float height_alt = NoisePerlin2D(&noise_terrain_alt->np, x, z, seed);
410 if (height_alt > height_base)
413 return (height_base * hselect) + (height_alt * (1.0 - hselect));
417 float MapgenV7::baseTerrainLevelFromMap(int index)
419 float hselect = rangelim(noise_height_select->result[index], 0.0, 1.0);
420 float height_base = noise_terrain_base->result[index];
421 float height_alt = noise_terrain_alt->result[index];
423 if (height_alt > height_base)
426 return (height_base * hselect) + (height_alt * (1.0 - hselect));
430 bool MapgenV7::getMountainTerrainAtPoint(s16 x, s16 y, s16 z)
432 float mnt_h_n = NoisePerlin2D(&noise_mount_height->np, x, z, seed);
433 float density_gradient = -((float)y / mnt_h_n);
434 float mnt_n = NoisePerlin3D(&noise_mountain->np, x, y, z, seed);
436 return mnt_n + density_gradient >= 0.0;
440 bool MapgenV7::getMountainTerrainFromMap(int idx_xyz, int idx_xz, s16 y)
442 float mounthn = noise_mount_height->result[idx_xz];
443 float density_gradient = -((float)y / mounthn);
444 float mountn = noise_mountain->result[idx_xyz];
446 return mountn + density_gradient >= 0.0;
451 void MapgenV7::carveRivers() {
452 MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
453 MapNode n_stone(c_stone);
458 for (s16 z = node_min.Z; z <= node_max.Z; z++)
459 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
460 float terrain_mod = noise_terrain_mod->result[index];
461 NoiseParams *np = noise_terrain_river->np;
462 np.persist = noise_terrain_persist->result[index];
463 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
464 float height = terrain_river * (1 - abs(terrain_mod)) *
465 noise_terrain_river->np.scale;
466 height = log(height * height); //log(h^3) is pretty interesting for terrain
468 s16 y = heightmap[index];
469 if (height < 1.0 && y > river_depth &&
470 y - river_depth >= node_min.Y && y <= node_max.Y) {
472 for (s16 ry = y; ry != y - river_depth; ry--) {
473 u32 vi = vm->m_area.index(x, ry, z);
474 vm->m_data[vi] = n_air;
477 u32 vi = vm->m_area.index(x, y - river_depth, z);
478 vm->m_data[vi] = n_water_source;
485 int MapgenV7::generateTerrain()
487 s16 stone_surface_min_y;
488 s16 stone_surface_max_y;
490 generateBaseTerrain(&stone_surface_min_y, &stone_surface_max_y);
492 if ((spflags & MGV7_MOUNTAINS) && stone_surface_min_y < node_max.Y)
493 stone_surface_max_y = generateMountainTerrain(stone_surface_max_y);
495 if (spflags & MGV7_RIDGES)
496 generateRidgeTerrain();
498 return stone_surface_max_y;
502 void MapgenV7::generateBaseTerrain(s16 *stone_surface_min_y, s16 *stone_surface_max_y)
504 MapNode n_air(CONTENT_AIR);
505 MapNode n_stone(c_stone);
506 MapNode n_water(c_water_source);
508 v3s16 em = vm->m_area.getExtent();
509 s16 surface_min_y = MAX_MAP_GENERATION_LIMIT;
510 s16 surface_max_y = -MAX_MAP_GENERATION_LIMIT;
513 for (s16 z = node_min.Z; z <= node_max.Z; z++)
514 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
515 float surface_height = baseTerrainLevelFromMap(index);
516 s16 surface_y = (s16)surface_height;
518 heightmap[index] = surface_y;
519 ridge_heightmap[index] = surface_y;
521 if (surface_y < surface_min_y)
522 surface_min_y = surface_y;
524 if (surface_y > surface_max_y)
525 surface_max_y = surface_y;
527 u32 i = vm->m_area.index(x, node_min.Y - 1, z);
528 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
529 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
531 vm->m_data[i] = n_stone;
532 else if (y <= water_level)
533 vm->m_data[i] = n_water;
535 vm->m_data[i] = n_air;
537 vm->m_area.add_y(em, i, 1);
541 *stone_surface_min_y = surface_min_y;
542 *stone_surface_max_y = surface_max_y;
546 int MapgenV7::generateMountainTerrain(s16 ymax)
548 noise_mountain->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
549 noise_mount_height->perlinMap2D(node_min.X, node_min.Z);
551 MapNode n_stone(c_stone);
554 for (s16 z = node_min.Z; z <= node_max.Z; z++)
555 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
556 u32 vi = vm->m_area.index(node_min.X, y, z);
557 for (s16 x = node_min.X; x <= node_max.X; x++) {
558 int index = (z - node_min.Z) * csize.X + (x - node_min.X);
559 content_t c = vm->m_data[vi].getContent();
561 if (getMountainTerrainFromMap(j, index, y)
562 && (c == CONTENT_AIR || c == c_water_source)) {
563 vm->m_data[vi] = n_stone;
577 void MapgenV7::generateRidgeTerrain()
579 if (node_max.Y < water_level)
582 MapNode n_water(c_water_source);
583 MapNode n_air(CONTENT_AIR);
585 float width = 0.2; // TODO: figure out acceptable perlin noise values
587 for (s16 z = node_min.Z; z <= node_max.Z; z++)
588 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
589 u32 vi = vm->m_area.index(node_min.X, y, z);
590 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
591 int j = (z - node_min.Z) * csize.X + (x - node_min.X);
593 if (heightmap[j] < water_level - 16)
596 float uwatern = noise_ridge_uwater->result[j] * 2;
597 if (fabs(uwatern) > width)
600 float altitude = y - water_level;
601 float height_mod = (altitude + 17) / 2.5;
602 float width_mod = width - fabs(uwatern);
603 float nridge = noise_ridge->result[index] * MYMAX(altitude, 0) / 7.0;
605 if (nridge + width_mod * height_mod < 0.6)
608 if (y < ridge_heightmap[j])
609 ridge_heightmap[j] = y - 1;
611 vm->m_data[vi] = (y > water_level) ? n_air : n_water;
617 MgStoneType MapgenV7::generateBiomes(float *heat_map, float *humidity_map)
619 v3s16 em = vm->m_area.getExtent();
621 MgStoneType stone_type = STONE;
623 for (s16 z = node_min.Z; z <= node_max.Z; z++)
624 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
628 u16 depth_water_top = 0;
629 u32 vi = vm->m_area.index(x, node_max.Y, z);
631 // Check node at base of mapchunk above, either a node of a previously
632 // generated mapchunk or if not, a node of overgenerated base terrain.
633 content_t c_above = vm->m_data[vi + em.X].getContent();
634 bool air_above = c_above == CONTENT_AIR;
635 bool water_above = c_above == c_water_source;
637 // If there is air or water above enable top/filler placement, otherwise force
638 // nplaced to stone level by setting a number exceeding any possible filler depth.
639 u16 nplaced = (air_above || water_above) ? 0 : U16_MAX;
641 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
642 content_t c = vm->m_data[vi].getContent();
644 // Biome is recalculated each time an upper surface is detected while
645 // working down a column. The selected biome then remains in effect for
646 // all nodes below until the next surface and biome recalculation.
647 // Biome is recalculated:
648 // 1. At the surface of stone below air or water.
649 // 2. At the surface of water below air.
650 // 3. When stone or water is detected but biome has not yet been calculated.
651 if ((c == c_stone && (air_above || water_above || !biome)) ||
652 (c == c_water_source && (air_above || !biome))) {
653 biome = bmgr->getBiome(heat_map[index], humidity_map[index], y);
654 depth_top = biome->depth_top;
655 base_filler = MYMAX(depth_top + biome->depth_filler
656 + noise_filler_depth->result[index], 0);
657 depth_water_top = biome->depth_water_top;
659 // Detect stone type for dungeons during every biome calculation.
660 // This is more efficient than detecting per-node and will not
661 // miss any desert stone or sandstone biomes.
662 if (biome->c_stone == c_desert_stone)
663 stone_type = DESERT_STONE;
664 else if (biome->c_stone == c_sandstone)
665 stone_type = SANDSTONE;
669 content_t c_below = vm->m_data[vi - em.X].getContent();
671 // If the node below isn't solid, make this node stone, so that
672 // any top/filler nodes above are structurally supported.
673 // This is done by aborting the cycle of top/filler placement
674 // immediately by forcing nplaced to stone level.
675 if (c_below == CONTENT_AIR || c_below == c_water_source)
678 if (nplaced < depth_top) {
679 vm->m_data[vi] = MapNode(biome->c_top);
681 } else if (nplaced < base_filler) {
682 vm->m_data[vi] = MapNode(biome->c_filler);
685 vm->m_data[vi] = MapNode(biome->c_stone);
690 } else if (c == c_water_source) {
691 vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ?
692 biome->c_water_top : biome->c_water);
693 nplaced = 0; // Enable top/filler placement for next surface
696 } else if (c == CONTENT_AIR) {
697 nplaced = 0; // Enable top/filler placement for next surface
700 } else { // Possible various nodes overgenerated from neighbouring mapchunks
701 nplaced = U16_MAX; // Disable top/filler placement
706 vm->m_area.add_y(em, vi, -1);
714 void MapgenV7::dustTopNodes()
716 if (node_max.Y < water_level)
719 v3s16 em = vm->m_area.getExtent();
722 for (s16 z = node_min.Z; z <= node_max.Z; z++)
723 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
724 Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]);
726 if (biome->c_dust == CONTENT_IGNORE)
729 u32 vi = vm->m_area.index(x, full_node_max.Y, z);
730 content_t c_full_max = vm->m_data[vi].getContent();
733 if (c_full_max == CONTENT_AIR) {
734 y_start = full_node_max.Y - 1;
735 } else if (c_full_max == CONTENT_IGNORE) {
736 vi = vm->m_area.index(x, node_max.Y + 1, z);
737 content_t c_max = vm->m_data[vi].getContent();
739 if (c_max == CONTENT_AIR)
740 y_start = node_max.Y;
747 vi = vm->m_area.index(x, y_start, z);
748 for (s16 y = y_start; y >= node_min.Y - 1; y--) {
749 if (vm->m_data[vi].getContent() != CONTENT_AIR)
752 vm->m_area.add_y(em, vi, -1);
755 content_t c = vm->m_data[vi].getContent();
756 if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) {
757 vm->m_area.add_y(em, vi, 1);
758 vm->m_data[vi] = MapNode(biome->c_dust);
765 void MapgenV7::addTopNodes()
767 v3s16 em = vm->m_area.getExtent();
771 for (s16 z = node_min.Z; z <= node_max.Z; z++)
772 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
773 Biome *biome = bmgr->biomes[biomemap[index]];
775 //////////////////// First, add top nodes below the ridge
776 s16 y = ridge_heightmap[index];
778 // This cutoff is good enough, but not perfect.
779 // It will cut off potentially placed top nodes at chunk boundaries
782 if (y > node_max.Y) {
783 y = node_max.Y; // Let's see if we can still go downward anyway
784 u32 vi = vm->m_area.index(x, y, z);
785 content_t c = vm->m_data[vi].getContent();
786 if (ndef->get(c).walkable)
790 // N.B. It is necessary to search downward since ridge_heightmap[i]
791 // might not be the actual height, just the lowest part in the chunk
792 // where a ridge had been carved
793 u32 i = vm->m_area.index(x, y, z);
794 for (; y >= node_min.Y; y--) {
795 content_t c = vm->m_data[i].getContent();
796 if (ndef->get(c).walkable)
798 vm->m_area.add_y(em, i, -1);
801 if (y != node_min.Y - 1 && y >= water_level) {
802 ridge_heightmap[index] = y; //update ridgeheight
803 ntopnodes = biome->top_depth;
804 for (; y <= node_max.Y && ntopnodes; y++) {
806 vm->m_data[i] = MapNode(biome->c_top);
807 vm->m_area.add_y(em, i, 1);
809 // If dirt, grow grass on it.
810 if (y > water_level - 10 &&
811 vm->m_data[i].getContent() == CONTENT_AIR) {
812 vm->m_area.add_y(em, i, -1);
813 if (vm->m_data[i].getContent() == c_dirt)
814 vm->m_data[i] = MapNode(c_dirt_with_grass);
818 //////////////////// Now, add top nodes on top of the ridge
819 y = heightmap[index];
820 if (y > node_max.Y) {
821 y = node_max.Y; // Let's see if we can still go downward anyway
822 u32 vi = vm->m_area.index(x, y, z);
823 content_t c = vm->m_data[vi].getContent();
824 if (ndef->get(c).walkable)
828 i = vm->m_area.index(x, y, z);
829 for (; y >= node_min.Y; y--) {
830 content_t c = vm->m_data[i].getContent();
831 if (ndef->get(c).walkable)
833 vm->m_area.add_y(em, i, -1);
836 if (y != node_min.Y - 1) {
837 ntopnodes = biome->top_depth;
838 // Let's see if we've already added it...
839 if (y == ridge_heightmap[index] + ntopnodes - 1)
842 for (; y <= node_max.Y && ntopnodes; y++) {
844 vm->m_data[i] = MapNode(biome->c_top);
845 vm->m_area.add_y(em, i, 1);
847 // If dirt, grow grass on it.
848 if (y > water_level - 10 &&
849 vm->m_data[i].getContent() == CONTENT_AIR) {
850 vm->m_area.add_y(em, i, -1);
851 if (vm->m_data[i].getContent() == c_dirt)
852 vm->m_data[i] = MapNode(c_dirt_with_grass);
860 void MapgenV7::generateCaves(s16 max_stone_y)
862 if (max_stone_y >= node_min.Y) {
865 for (s16 z = node_min.Z; z <= node_max.Z; z++)
866 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
867 u32 i = vm->m_area.index(node_min.X, y, z);
868 for (s16 x = node_min.X; x <= node_max.X; x++, i++, index++) {
869 float d1 = contour(noise_cave1->result[index]);
870 float d2 = contour(noise_cave2->result[index]);
872 content_t c = vm->m_data[i].getContent();
873 if (!ndef->get(c).is_ground_content || c == CONTENT_AIR)
876 vm->m_data[i] = MapNode(CONTENT_AIR);
882 PseudoRandom ps(blockseed + 21343);
883 u32 bruises_count = (ps.range(1, 4) == 1) ? ps.range(1, 2) : 0;
884 for (u32 i = 0; i < bruises_count; i++) {
885 CaveV7 cave(this, &ps);
886 cave.makeCave(node_min, node_max, max_stone_y);