Mapgen: Use getBlockSeed2() for blockseeds (much better uniformity)
[oweals/minetest.git] / src / mapgen_v7.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
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.
9
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.
14
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.
18 */
19
20
21 #include "mapgen.h"
22 #include "voxel.h"
23 #include "noise.h"
24 #include "mapblock.h"
25 #include "mapnode.h"
26 #include "map.h"
27 #include "content_sao.h"
28 #include "nodedef.h"
29 #include "voxelalgorithms.h"
30 #include "profiler.h"
31 #include "settings.h" // For g_settings
32 #include "main.h" // For g_profiler
33 #include "emerge.h"
34 #include "dungeongen.h"
35 #include "cavegen.h"
36 #include "treegen.h"
37 #include "mg_biome.h"
38 #include "mg_ore.h"
39 #include "mg_decoration.h"
40 #include "mapgen_v7.h"
41
42
43 FlagDesc flagdesc_mapgen_v7[] = {
44         {"mountains", MGV7_MOUNTAINS},
45         {"ridges",    MGV7_RIDGES},
46         {NULL,        0}
47 };
48
49 ///////////////////////////////////////////////////////////////////////////////
50
51
52 MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge)
53         : Mapgen(mapgenid, params, emerge)
54 {
55         this->m_emerge = emerge;
56         this->bmgr     = emerge->biomemgr;
57
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;
62
63         this->biomemap  = new u8[csize.X * csize.Z];
64         this->heightmap = new s16[csize.X * csize.Z];
65         this->ridge_heightmap = new s16[csize.X * csize.Z];
66
67         MapgenV7Params *sp = (MapgenV7Params *)params->sparams;
68         this->spflags = sp->spflags;
69
70         //// Terrain noise
71         noise_terrain_base    = new Noise(&sp->np_terrain_base,    seed, csize.X, csize.Z);
72         noise_terrain_alt     = new Noise(&sp->np_terrain_alt,     seed, csize.X, csize.Z);
73         noise_terrain_persist = new Noise(&sp->np_terrain_persist, seed, csize.X, csize.Z);
74         noise_height_select   = new Noise(&sp->np_height_select,   seed, csize.X, csize.Z);
75         noise_filler_depth    = new Noise(&sp->np_filler_depth,    seed, csize.X, csize.Z);
76         noise_mount_height    = new Noise(&sp->np_mount_height,    seed, csize.X, csize.Z);
77         noise_ridge_uwater    = new Noise(&sp->np_ridge_uwater,    seed, csize.X, csize.Z);
78
79         //// 3d terrain noise
80         noise_mountain = new Noise(&sp->np_mountain, seed, csize.X, csize.Y, csize.Z);
81         noise_ridge    = new Noise(&sp->np_ridge,    seed, csize.X, csize.Y, csize.Z);
82
83         //// Biome noise
84         noise_heat     = new Noise(&params->np_biome_heat,     seed, csize.X, csize.Z);
85         noise_humidity = new Noise(&params->np_biome_humidity, seed, csize.X, csize.Z);
86
87         //// Resolve nodes to be used
88         INodeDefManager *ndef = emerge->ndef;
89
90         c_stone           = ndef->getId("mapgen_stone");
91         c_dirt            = ndef->getId("mapgen_dirt");
92         c_dirt_with_grass = ndef->getId("mapgen_dirt_with_grass");
93         c_sand            = ndef->getId("mapgen_sand");
94         c_water_source    = ndef->getId("mapgen_water_source");
95         c_lava_source     = ndef->getId("mapgen_lava_source");
96         c_ice             = ndef->getId("default:ice");
97         if (c_ice == CONTENT_IGNORE)
98                 c_ice = CONTENT_AIR;
99 }
100
101
102 MapgenV7::~MapgenV7() {
103         delete noise_terrain_base;
104         delete noise_terrain_persist;
105         delete noise_height_select;
106         delete noise_terrain_alt;
107         delete noise_filler_depth;
108         delete noise_mount_height;
109         delete noise_ridge_uwater;
110         delete noise_mountain;
111         delete noise_ridge;
112
113         delete noise_heat;
114         delete noise_humidity;
115
116         delete[] ridge_heightmap;
117         delete[] heightmap;
118         delete[] biomemap;
119 }
120
121
122 MapgenV7Params::MapgenV7Params() {
123         spflags = MGV7_MOUNTAINS | MGV7_RIDGES;
124
125         np_terrain_base    = NoiseParams(4,    70,  v3f(300, 300, 300), 82341, 6, 0.7,  2.0);
126         np_terrain_alt     = NoiseParams(4,    25,  v3f(600, 600, 600), 5934,  5, 0.6,  2.0);
127         np_terrain_persist = NoiseParams(0.6,  0.1, v3f(500, 500, 500), 539,   3, 0.6,  2.0);
128         np_height_select   = NoiseParams(-0.5, 1,   v3f(250, 250, 250), 4213,  5, 0.69, 2.0);
129         np_filler_depth    = NoiseParams(0,    1.2, v3f(150, 150, 150), 261,   4, 0.7,  2.0);
130         np_mount_height    = NoiseParams(100,  30,  v3f(500, 500, 500), 72449, 4, 0.6,  2.0);
131         np_ridge_uwater    = NoiseParams(0,    1,   v3f(500, 500, 500), 85039, 4, 0.6,  2.0);
132         np_mountain        = NoiseParams(0,    1,   v3f(250, 350, 250), 5333,  5, 0.68, 2.0);
133         np_ridge           = NoiseParams(0,    1,   v3f(100, 100, 100), 6467,  4, 0.75, 2.0);
134 }
135
136
137 void MapgenV7Params::readParams(Settings *settings) {
138         settings->getFlagStrNoEx("mgv7_spflags", spflags, flagdesc_mapgen_v7);
139
140         settings->getNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
141         settings->getNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
142         settings->getNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
143         settings->getNoiseParams("mgv7_np_height_select",   np_height_select);
144         settings->getNoiseParams("mgv7_np_filler_depth",    np_filler_depth);
145         settings->getNoiseParams("mgv7_np_mount_height",    np_mount_height);
146         settings->getNoiseParams("mgv7_np_ridge_uwater",    np_ridge_uwater);
147         settings->getNoiseParams("mgv7_np_mountain",        np_mountain);
148         settings->getNoiseParams("mgv7_np_ridge",           np_ridge);
149 }
150
151
152 void MapgenV7Params::writeParams(Settings *settings) {
153         settings->setFlagStr("mgv7_spflags", spflags, flagdesc_mapgen_v7, (u32)-1);
154
155         settings->setNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
156         settings->setNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
157         settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
158         settings->setNoiseParams("mgv7_np_height_select",   np_height_select);
159         settings->setNoiseParams("mgv7_np_filler_depth",    np_filler_depth);
160         settings->setNoiseParams("mgv7_np_mount_height",    np_mount_height);
161         settings->setNoiseParams("mgv7_np_ridge_uwater",    np_ridge_uwater);
162         settings->setNoiseParams("mgv7_np_mountain",        np_mountain);
163         settings->setNoiseParams("mgv7_np_ridge",           np_ridge);
164 }
165
166
167 ///////////////////////////////////////
168
169
170 int MapgenV7::getGroundLevelAtPoint(v2s16 p) {
171         // Base terrain calculation
172         s16 y = baseTerrainLevelAtPoint(p.X, p.Y);
173
174         // Ridge/river terrain calculation
175         float width = 0.3;
176         float uwatern = NoisePerlin2D(&noise_ridge_uwater->np, p.X, p.Y, seed) * 2;
177         // actually computing the depth of the ridge is much more expensive;
178         // if inside a river, simply guess
179         if (uwatern >= -width && uwatern <= width)
180                 return water_level - 10;
181
182         // Mountain terrain calculation
183         int iters = 128; // don't even bother iterating more than 128 times..
184         while (iters--) {
185                 //current point would have been air
186                 if (!getMountainTerrainAtPoint(p.X, y, p.Y))
187                         return y;
188
189                 y++;
190         }
191
192         return y;
193 }
194
195
196 void MapgenV7::makeChunk(BlockMakeData *data) {
197         assert(data->vmanip);
198         assert(data->nodedef);
199         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
200                    data->blockpos_requested.Y >= data->blockpos_min.Y &&
201                    data->blockpos_requested.Z >= data->blockpos_min.Z);
202         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
203                    data->blockpos_requested.Y <= data->blockpos_max.Y &&
204                    data->blockpos_requested.Z <= data->blockpos_max.Z);
205
206         this->generating = true;
207         this->vm   = data->vmanip;
208         this->ndef = data->nodedef;
209         //TimeTaker t("makeChunk");
210
211         v3s16 blockpos_min = data->blockpos_min;
212         v3s16 blockpos_max = data->blockpos_max;
213         node_min = blockpos_min * MAP_BLOCKSIZE;
214         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
215         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
216         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
217
218         blockseed = getBlockSeed2(full_node_min, seed);
219
220         // Make some noise
221         calculateNoise();
222
223         // Generate base terrain, mountains, and ridges with initial heightmaps
224         s16 stone_surface_max_y = generateTerrain();
225
226         updateHeightmap(node_min, node_max);
227
228         // Calculate biomes
229         bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result,
230                 noise_humidity->result, heightmap, biomemap);
231
232         // Actually place the biome-specific nodes and what not
233         generateBiomes();
234
235         if (flags & MG_CAVES)
236                 generateCaves(stone_surface_max_y);
237
238         if (flags & MG_DUNGEONS) {
239                 DungeonGen dgen(this, NULL);
240                 dgen.generate(blockseed, full_node_min, full_node_max);
241         }
242
243         // Generate the registered decorations
244         m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
245
246         // Generate the registered ores
247         m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
248
249         // Sprinkle some dust on top after everything else was generated
250         dustTopNodes();
251
252         //printf("makeChunk: %dms\n", t.stop());
253
254         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
255
256         if (flags & MG_LIGHT)
257                 calcLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
258                                          node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
259         //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
260         //                      node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
261
262         this->generating = false;
263 }
264
265
266 void MapgenV7::calculateNoise() {
267         //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
268         int x = node_min.X;
269         int y = node_min.Y;
270         int z = node_min.Z;
271
272         noise_height_select->perlinMap2D(x, z);
273         noise_terrain_persist->perlinMap2D(x, z);
274         float *persistmap = noise_terrain_persist->result;
275         for (int i = 0; i != csize.X * csize.Z; i++)
276                 persistmap[i] = rangelim(persistmap[i], 0.4, 0.9);
277
278         noise_terrain_base->perlinMap2D(x, z, persistmap);
279         noise_terrain_alt->perlinMap2D(x, z, persistmap);
280         noise_filler_depth->perlinMap2D(x, z);
281
282         if (spflags & MGV7_MOUNTAINS) {
283                 noise_mountain->perlinMap3D(x, y, z);
284                 noise_mount_height->perlinMap2D(x, z);
285         }
286
287         if (spflags & MGV7_RIDGES) {
288                 noise_ridge->perlinMap3D(x, y, z);
289                 noise_ridge_uwater->perlinMap2D(x, z);
290         }
291
292         noise_heat->perlinMap2D(x, z);
293         noise_humidity->perlinMap2D(x, z);
294
295         //printf("calculateNoise: %dus\n", t.stop());
296 }
297
298
299 Biome *MapgenV7::getBiomeAtPoint(v3s16 p) {
300         float heat      = NoisePerlin2D(&noise_heat->np, p.X, p.Z, seed);
301         float humidity  = NoisePerlin2D(&noise_humidity->np, p.X, p.Z, seed);
302         s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
303
304         return bmgr->getBiome(heat, humidity, groundlevel);
305 }
306
307 //needs to be updated
308 float MapgenV7::baseTerrainLevelAtPoint(int x, int z) {
309         float hselect = NoisePerlin2D(&noise_height_select->np, x, z, seed);
310         hselect = rangelim(hselect, 0.0, 1.0);
311
312         float persist = NoisePerlin2D(&noise_terrain_persist->np, x, z, seed);
313         persist = rangelim(persist, 0.4, 0.9);
314
315         noise_terrain_base->np.persist = persist;
316         float height_base = NoisePerlin2D(&noise_terrain_base->np, x, z, seed);
317
318         noise_terrain_alt->np.persist = persist;
319         float height_alt = NoisePerlin2D(&noise_terrain_alt->np, x, z, seed);
320
321         if (height_alt > height_base)
322                 return height_alt;
323
324         return (height_base * hselect) + (height_alt * (1.0 - hselect));
325 }
326
327
328 float MapgenV7::baseTerrainLevelFromMap(int index) {
329         float hselect     = rangelim(noise_height_select->result[index], 0.0, 1.0);
330         float height_base = noise_terrain_base->result[index];
331         float height_alt  = noise_terrain_alt->result[index];
332
333         if (height_alt > height_base)
334                 return height_alt;
335
336         return (height_base * hselect) + (height_alt * (1.0 - hselect));
337 }
338
339
340 bool MapgenV7::getMountainTerrainAtPoint(int x, int y, int z) {
341         float mnt_h_n = NoisePerlin2D(&noise_mount_height->np, x, z, seed);
342         float height_modifier = -((float)y / rangelim(mnt_h_n, 80.0, 150.0));
343         float mnt_n = NoisePerlin3D(&noise_mountain->np, x, y, z, seed);
344
345         return mnt_n + height_modifier >= 0.6;
346 }
347
348
349 bool MapgenV7::getMountainTerrainFromMap(int idx_xyz, int idx_xz, int y) {
350         float mounthn = noise_mount_height->result[idx_xz];
351         float height_modifier = -((float)y / rangelim(mounthn, 80.0, 150.0));
352         return (noise_mountain->result[idx_xyz] + height_modifier >= 0.6);
353 }
354
355
356 #if 0
357 void MapgenV7::carveRivers() {
358         MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
359         MapNode n_stone(c_stone);
360         u32 index = 0;
361
362         int river_depth = 4;
363
364         for (s16 z = node_min.Z; z <= node_max.Z; z++)
365         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
366                 float terrain_mod  = noise_terrain_mod->result[index];
367                 NoiseParams *np = noise_terrain_river->np;
368                 np.persist = noise_terrain_persist->result[index];
369                 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
370                 float height = terrain_river * (1 - abs(terrain_mod)) *
371                                                 noise_terrain_river->np.scale;
372                 height = log(height * height); //log(h^3) is pretty interesting for terrain
373
374                 s16 y = heightmap[index];
375                 if (height < 1.0 && y > river_depth &&
376                         y - river_depth >= node_min.Y && y <= node_max.Y) {
377
378                         for (s16 ry = y; ry != y - river_depth; ry--) {
379                                 u32 vi = vm->m_area.index(x, ry, z);
380                                 vm->m_data[vi] = n_air;
381                         }
382
383                         u32 vi = vm->m_area.index(x, y - river_depth, z);
384                         vm->m_data[vi] = n_water_source;
385                 }
386         }
387 }
388 #endif
389
390
391 int MapgenV7::generateTerrain() {
392         int ymax = generateBaseTerrain();
393
394         if (spflags & MGV7_MOUNTAINS)
395                 generateMountainTerrain();
396
397         if (spflags & MGV7_RIDGES)
398                 generateRidgeTerrain();
399
400         return ymax;
401 }
402
403
404 int MapgenV7::generateBaseTerrain() {
405         MapNode n_air(CONTENT_AIR);
406         MapNode n_stone(c_stone);
407         MapNode n_water(c_water_source);
408
409         int stone_surface_max_y = -MAP_GENERATION_LIMIT;
410         v3s16 em = vm->m_area.getExtent();
411         u32 index = 0;
412
413         for (s16 z = node_min.Z; z <= node_max.Z; z++)
414         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
415                 float surface_height = baseTerrainLevelFromMap(index);
416                 s16 surface_y = (s16)surface_height;
417
418                 heightmap[index]       = surface_y;
419                 ridge_heightmap[index] = surface_y;
420
421                 if (surface_y > stone_surface_max_y)
422                         stone_surface_max_y = surface_y;
423
424                 u32 i = vm->m_area.index(x, node_min.Y, z);
425                 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
426                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
427                                 if (y <= surface_y)
428                                         vm->m_data[i] = n_stone;
429                                 else if (y <= water_level)
430                                         vm->m_data[i] = n_water;
431                                 else
432                                         vm->m_data[i] = n_air;
433                         }
434                         vm->m_area.add_y(em, i, 1);
435                 }
436         }
437
438         return stone_surface_max_y;
439 }
440
441
442 void MapgenV7::generateMountainTerrain() {
443         if (node_max.Y <= water_level)
444                 return;
445
446         MapNode n_stone(c_stone);
447         u32 j = 0;
448
449         for (s16 z = node_min.Z; z <= node_max.Z; z++)
450         for (s16 y = node_min.Y; y <= node_max.Y; y++) {
451                 u32 vi = vm->m_area.index(node_min.X, y, z);
452                 for (s16 x = node_min.X; x <= node_max.X; x++) {
453                         int index = (z - node_min.Z) * csize.X + (x - node_min.X);
454
455                         if (getMountainTerrainFromMap(j, index, y))
456                                 vm->m_data[vi] = n_stone;
457
458                         vi++;
459                         j++;
460                 }
461         }
462 }
463
464
465 void MapgenV7::generateRidgeTerrain() {
466         MapNode n_water(c_water_source);
467         MapNode n_air(CONTENT_AIR);
468         u32 index = 0;
469
470         for (s16 z = node_min.Z; z <= node_max.Z; z++)
471         for (s16 y = node_min.Y; y <= node_max.Y; y++) {
472                 u32 vi = vm->m_area.index(node_min.X, y, z);
473                 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
474                         int j = (z - node_min.Z) * csize.X + (x - node_min.X);
475
476                         if (heightmap[j] < water_level - 4)
477                                 continue;
478
479                         float widthn = (noise_terrain_persist->result[j] - 0.6) / 0.1;
480                         //widthn = rangelim(widthn, -0.05, 0.5);
481
482                         float width = 0.3; // TODO: figure out acceptable perlin noise values
483                         float uwatern = noise_ridge_uwater->result[j] * 2;
484                         if (uwatern < -width || uwatern > width)
485                                 continue;
486
487                         float height_mod = (float)(y + 17) / 2.5;
488                         float width_mod  = (width - fabs(uwatern));
489                         float nridge = noise_ridge->result[index] * (float)y / 7.0;
490
491                         if (y < water_level)
492                                 nridge = -fabs(nridge) * 3.0 * widthn * 0.3;
493
494                         if (nridge + width_mod * height_mod < 0.6)
495                                 continue;
496
497                         if (y < ridge_heightmap[j])
498                                 ridge_heightmap[j] = y - 1;
499
500                         vm->m_data[vi] = (y > water_level) ? n_air : n_water;
501                 }
502         }
503 }
504
505
506 void MapgenV7::generateBiomes() {
507         if (node_max.Y < water_level)
508                 return;
509
510         MapNode n_air(CONTENT_AIR);
511         MapNode n_stone(c_stone);
512         MapNode n_water(c_water_source);
513
514         v3s16 em = vm->m_area.getExtent();
515         u32 index = 0;
516
517         for (s16 z = node_min.Z; z <= node_max.Z; z++)
518         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
519                 Biome *biome        = (Biome *)bmgr->get(biomemap[index]);
520                 s16 dfiller         = biome->depth_filler + noise_filler_depth->result[index];
521                 s16 y0_top          = biome->depth_top;
522                 s16 y0_filler       = biome->depth_top + dfiller;
523                 s16 shore_max       = water_level + biome->height_shore;
524                 s16 depth_water_top = biome->depth_water_top;
525
526                 s16 nplaced = 0;
527                 u32 i = vm->m_area.index(x, node_max.Y, z);
528
529                 content_t c_above = vm->m_data[i + em.X].getContent();
530                 bool have_air = c_above == CONTENT_AIR;
531
532                 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
533                         content_t c = vm->m_data[i].getContent();
534
535                         // It could be the case that the elevation is equal to the chunk
536                         // boundary, but the chunk above has not been generated yet
537                         if (y == node_max.Y && c_above == CONTENT_IGNORE &&
538                                 y == heightmap[index] && c == c_stone) {
539                                 int j = (z - node_min.Z) * zstride +
540                                                 (y - node_min.Y) * ystride +
541                                                 (x - node_min.X);
542                                 have_air = !getMountainTerrainFromMap(j, index, y);
543                         }
544
545                         if (c == c_stone && have_air) {
546                                 content_t c_below = vm->m_data[i - em.X].getContent();
547
548                                 if (c_below != CONTENT_AIR) {
549                                         if (nplaced < y0_top) {
550                                                 if(y < water_level)
551                                                         vm->m_data[i] = MapNode(biome->c_underwater);
552                                                 else if(y <= shore_max)
553                                                         vm->m_data[i] = MapNode(biome->c_shore_top);
554                                                 else
555                                                         vm->m_data[i] = MapNode(biome->c_top);
556                                                 nplaced++;
557                                         } else if (nplaced < y0_filler && nplaced >= y0_top) {
558                                                 if(y < water_level)
559                                                         vm->m_data[i] = MapNode(biome->c_underwater);
560                                                 else if(y <= shore_max)
561                                                         vm->m_data[i] = MapNode(biome->c_shore_filler);
562                                                 else
563                                                         vm->m_data[i] = MapNode(biome->c_filler);
564                                                 nplaced++;
565                                         } else if (c == c_stone) {
566                                                 have_air = false;
567                                                 nplaced  = 0;
568                                                 vm->m_data[i] = MapNode(biome->c_stone);
569                                         } else {
570                                                 have_air = false;
571                                                 nplaced  = 0;
572                                         }
573                                 } else if (c == c_stone) {
574                                         have_air = false;
575                                         nplaced = 0;
576                                         vm->m_data[i] = MapNode(biome->c_stone);
577                                 }
578                         } else if (c == c_stone) {
579                                 have_air = false;
580                                 nplaced = 0;
581                                 vm->m_data[i] = MapNode(biome->c_stone);
582                         } else if (c == c_water_source) {
583                                 have_air = true;
584                                 nplaced = 0;
585                                 if(y > water_level - depth_water_top)
586                                         vm->m_data[i] = MapNode(biome->c_water_top);
587                                 else
588                                         vm->m_data[i] = MapNode(biome->c_water);
589                         } else if (c == CONTENT_AIR) {
590                                 have_air = true;
591                                 nplaced = 0;
592                         }
593
594                         vm->m_area.add_y(em, i, -1);
595                 }
596         }
597 }
598
599
600 void MapgenV7::dustTopNodes() {
601         v3s16 em = vm->m_area.getExtent();
602         u32 index = 0;
603
604         if (water_level > node_max.Y)
605                 return;
606
607         for (s16 z = node_min.Z; z <= node_max.Z; z++)
608         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
609                 Biome *biome = (Biome *)bmgr->get(biomemap[index]);
610
611                 if (biome->c_dust == CONTENT_IGNORE)
612                         continue;
613
614                 s16 y = node_max.Y;
615                 u32 vi = vm->m_area.index(x, y, z);
616                 for (; y >= node_min.Y; y--) {
617                         if (vm->m_data[vi].getContent() != CONTENT_AIR)
618                                 break;
619
620                         vm->m_area.add_y(em, vi, -1);
621                 }
622
623                 content_t c = vm->m_data[vi].getContent();
624                 if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE) {
625                         if (y == node_max.Y)
626                                 continue;
627
628                         vm->m_area.add_y(em, vi, 1);
629                         vm->m_data[vi] = MapNode(biome->c_dust);
630                 }
631         }
632 }
633
634
635 #if 0
636 void MapgenV7::addTopNodes() {
637         v3s16 em = vm->m_area.getExtent();
638         s16 ntopnodes;
639         u32 index = 0;
640
641         for (s16 z = node_min.Z; z <= node_max.Z; z++)
642         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
643                 Biome *biome = bmgr->biomes[biomemap[index]];
644
645                 //////////////////// First, add top nodes below the ridge
646                 s16 y = ridge_heightmap[index];
647
648                 // This cutoff is good enough, but not perfect.
649                 // It will cut off potentially placed top nodes at chunk boundaries
650                 if (y < node_min.Y)
651                         continue;
652                 if (y > node_max.Y) {
653                         y = node_max.Y; // Let's see if we can still go downward anyway
654                         u32 vi = vm->m_area.index(x, y, z);
655                         content_t c = vm->m_data[vi].getContent();
656                         if (ndef->get(c).walkable)
657                                 continue;
658                 }
659
660                 // N.B.  It is necessary to search downward since ridge_heightmap[i]
661                 // might not be the actual height, just the lowest part in the chunk
662                 // where a ridge had been carved
663                 u32 i = vm->m_area.index(x, y, z);
664                 for (; y >= node_min.Y; y--) {
665                         content_t c = vm->m_data[i].getContent();
666                         if (ndef->get(c).walkable)
667                                 break;
668                         vm->m_area.add_y(em, i, -1);
669                 }
670
671                 if (y != node_min.Y - 1 && y >= water_level) {
672                         ridge_heightmap[index] = y; //update ridgeheight
673                         ntopnodes = biome->top_depth;
674                         for (; y <= node_max.Y && ntopnodes; y++) {
675                                 ntopnodes--;
676                                 vm->m_data[i] = MapNode(biome->c_top);
677                                 vm->m_area.add_y(em, i, 1);
678                         }
679                         // If dirt, grow grass on it.
680                         if (y > water_level - 10 &&
681                                 vm->m_data[i].getContent() == CONTENT_AIR) {
682                                 vm->m_area.add_y(em, i, -1);
683                                 if (vm->m_data[i].getContent() == c_dirt)
684                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
685                         }
686                 }
687
688                 //////////////////// Now, add top nodes on top of the ridge
689                 y = heightmap[index];
690                 if (y > node_max.Y) {
691                         y = node_max.Y; // Let's see if we can still go downward anyway
692                         u32 vi = vm->m_area.index(x, y, z);
693                         content_t c = vm->m_data[vi].getContent();
694                         if (ndef->get(c).walkable)
695                                 continue;
696                 }
697
698                 i = vm->m_area.index(x, y, z);
699                 for (; y >= node_min.Y; y--) {
700                         content_t c = vm->m_data[i].getContent();
701                         if (ndef->get(c).walkable)
702                                 break;
703                         vm->m_area.add_y(em, i, -1);
704                 }
705
706                 if (y != node_min.Y - 1) {
707                         ntopnodes = biome->top_depth;
708                         // Let's see if we've already added it...
709                         if (y == ridge_heightmap[index] + ntopnodes - 1)
710                                 continue;
711
712                         for (; y <= node_max.Y && ntopnodes; y++) {
713                                 ntopnodes--;
714                                 vm->m_data[i] = MapNode(biome->c_top);
715                                 vm->m_area.add_y(em, i, 1);
716                         }
717                         // If dirt, grow grass on it.
718                         if (y > water_level - 10 &&
719                                 vm->m_data[i].getContent() == CONTENT_AIR) {
720                                 vm->m_area.add_y(em, i, -1);
721                                 if (vm->m_data[i].getContent() == c_dirt)
722                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
723                         }
724                 }
725         }
726 }
727 #endif
728
729
730 NoiseParams nparams_v7_def_cave(6, 6.0, v3f(250.0, 250.0, 250.0), 34329, 3, 0.50, 2.0);
731
732 void MapgenV7::generateCaves(int max_stone_y) {
733         PseudoRandom ps(blockseed + 21343);
734
735         int volume_nodes = (node_max.X - node_min.X + 1) *
736                                            (node_max.Y - node_min.Y + 1) *
737                                            (node_max.Z - node_min.Z + 1);
738         float cave_amount = NoisePerlin2D(&nparams_v7_def_cave,
739                                                                 node_min.X, node_min.Y, seed);
740
741         u32 caves_count = MYMAX(0.0, cave_amount) * volume_nodes / 250000;
742         for (u32 i = 0; i < caves_count; i++) {
743                 CaveV7 cave(this, &ps, false);
744                 cave.makeCave(node_min, node_max, max_stone_y);
745         }
746
747         u32 bruises_count = (ps.range(1, 8) == 1) ? ps.range(0, ps.range(0, 2)) : 1;
748         for (u32 i = 0; i < bruises_count; i++) {
749                 CaveV7 cave(this, &ps, true);
750                 cave.makeCave(node_min, node_max, max_stone_y);
751         }
752 }