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