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