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