Split up mapgen.cpp
[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         this->generating  = false;
54         this->id     = mapgenid;
55         this->emerge = emerge;
56         this->bmgr   = emerge->biomedef;
57
58         this->seed        = (int)params->seed;
59         this->water_level = params->water_level;
60         this->flags       = params->flags;
61         this->gennotify   = emerge->gennotify;
62
63         this->csize   = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
64
65         //// amount of elements to skip for the next index
66         //// for noise/height/biome maps (not vmanip)
67         this->ystride = csize.X;
68         this->zstride = csize.X * csize.Y;
69
70         this->biomemap  = new u8[csize.X * csize.Z];
71         this->heightmap = new s16[csize.X * csize.Z];
72         this->ridge_heightmap = new s16[csize.X * csize.Z];
73
74         MapgenV7Params *sp = (MapgenV7Params *)params->sparams;
75
76         //// Terrain noise
77         noise_terrain_base    = new Noise(&sp->np_terrain_base,    seed, csize.X, csize.Z);
78         noise_terrain_alt     = new Noise(&sp->np_terrain_alt,     seed, csize.X, csize.Z);
79         noise_terrain_persist = new Noise(&sp->np_terrain_persist, seed, csize.X, csize.Z);
80         noise_height_select   = new Noise(&sp->np_height_select,   seed, csize.X, csize.Z);
81         noise_filler_depth    = new Noise(&sp->np_filler_depth,    seed, csize.X, csize.Z);
82         noise_mount_height    = new Noise(&sp->np_mount_height,    seed, csize.X, csize.Z);
83         noise_ridge_uwater    = new Noise(&sp->np_ridge_uwater,    seed, csize.X, csize.Z);
84
85         //// 3d terrain noise
86         noise_mountain = new Noise(&sp->np_mountain, seed, csize.X, csize.Y, csize.Z);
87         noise_ridge    = new Noise(&sp->np_ridge,    seed, csize.X, csize.Y, csize.Z);
88
89         //// Biome noise
90         noise_heat     = new Noise(bmgr->np_heat,     seed, csize.X, csize.Z);
91         noise_humidity = new Noise(bmgr->np_humidity, seed, csize.X, csize.Z);
92
93         //// Resolve nodes to be used
94         INodeDefManager *ndef = emerge->ndef;
95
96         c_stone           = ndef->getId("mapgen_stone");
97         c_dirt            = ndef->getId("mapgen_dirt");
98         c_dirt_with_grass = ndef->getId("mapgen_dirt_with_grass");
99         c_sand            = ndef->getId("mapgen_sand");
100         c_water_source    = ndef->getId("mapgen_water_source");
101         c_lava_source     = ndef->getId("mapgen_lava_source");
102         c_ice             = ndef->getId("default:ice");
103         if (c_ice == CONTENT_IGNORE)
104                 c_ice = CONTENT_AIR;
105 }
106
107
108 MapgenV7::~MapgenV7() {
109         delete noise_terrain_base;
110         delete noise_terrain_persist;
111         delete noise_height_select;
112         delete noise_terrain_alt;
113         delete noise_filler_depth;
114         delete noise_mount_height;
115         delete noise_ridge_uwater;
116         delete noise_mountain;
117         delete noise_ridge;
118
119         delete noise_heat;
120         delete noise_humidity;
121         
122         delete[] ridge_heightmap;
123         delete[] heightmap;
124         delete[] biomemap;
125 }
126
127
128 MapgenV7Params::MapgenV7Params() {
129         spflags = MGV7_MOUNTAINS | MGV7_RIDGES;
130
131         np_terrain_base    = NoiseParams(4,    70,  v3f(300, 300, 300), 82341, 6, 0.7);
132         np_terrain_alt     = NoiseParams(4,    25,  v3f(600, 600, 600), 5934,  5, 0.6);
133         np_terrain_persist = NoiseParams(0.6,  0.1, v3f(500, 500, 500), 539,   3, 0.6);
134         np_height_select   = NoiseParams(-0.5, 1,   v3f(250, 250, 250), 4213,  5, 0.69);
135         np_filler_depth    = NoiseParams(0,    1.2, v3f(150, 150, 150), 261,   4, 0.7);
136         np_mount_height    = NoiseParams(100,  30,  v3f(500, 500, 500), 72449, 4, 0.6);
137         np_ridge_uwater    = NoiseParams(0,    1,   v3f(500, 500, 500), 85039, 4, 0.6);
138         np_mountain        = NoiseParams(0,    1,   v3f(250, 350, 250), 5333,  5, 0.68);
139         np_ridge           = NoiseParams(0,    1,   v3f(100, 100, 100), 6467,  4, 0.75);
140 }
141
142
143 void MapgenV7Params::readParams(Settings *settings) {
144         settings->getFlagStrNoEx("mgv7_spflags", spflags, flagdesc_mapgen_v7);
145
146         settings->getNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
147         settings->getNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
148         settings->getNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
149         settings->getNoiseParams("mgv7_np_height_select",   np_height_select);
150         settings->getNoiseParams("mgv7_np_filler_depth",    np_filler_depth);
151         settings->getNoiseParams("mgv7_np_mount_height",    np_mount_height);
152         settings->getNoiseParams("mgv7_np_ridge_uwater",    np_ridge_uwater);
153         settings->getNoiseParams("mgv7_np_mountain",        np_mountain);
154         settings->getNoiseParams("mgv7_np_ridge",           np_ridge);
155 }
156
157
158 void MapgenV7Params::writeParams(Settings *settings) {
159         settings->setFlagStr("mgv7_spflags", spflags, flagdesc_mapgen_v7, (u32)-1);
160
161         settings->setNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
162         settings->setNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
163         settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
164         settings->setNoiseParams("mgv7_np_height_select",   np_height_select);
165         settings->setNoiseParams("mgv7_np_filler_depth",    np_filler_depth);
166         settings->setNoiseParams("mgv7_np_mount_height",    np_mount_height);
167         settings->setNoiseParams("mgv7_np_ridge_uwater",    np_ridge_uwater);
168         settings->setNoiseParams("mgv7_np_mountain",        np_mountain);
169         settings->setNoiseParams("mgv7_np_ridge",           np_ridge);
170 }
171
172
173 ///////////////////////////////////////
174
175
176 int MapgenV7::getGroundLevelAtPoint(v2s16 p) {
177         // Base terrain calculation
178         s16 y = baseTerrainLevelAtPoint(p.X, p.Y);
179         
180         // Ridge/river terrain calculation
181         float width = 0.3;
182         float uwatern = NoisePerlin2DNoTxfm(noise_ridge_uwater->np, p.X, p.Y, seed) * 2;
183         // actually computing the depth of the ridge is much more expensive;
184         // if inside a river, simply guess
185         if (uwatern >= -width && uwatern <= width)
186                 return water_level - 10;
187         
188         // Mountain terrain calculation
189         int iters = 128; // don't even bother iterating more than 128 times..
190         while (iters--) {
191                 //current point would have been air
192                 if (!getMountainTerrainAtPoint(p.X, y, p.Y))
193                         return y;
194                         
195                 y++;
196         }
197
198         return y;
199 }
200
201
202 void MapgenV7::makeChunk(BlockMakeData *data) {
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 = emerge->getBlockSeed(full_node_min);  //////use getBlockSeed2()!
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         BiomeNoiseInput binput;
236         binput.mapsize      = v2s16(csize.X, csize.Z);
237         binput.heat_map     = noise_heat->result;
238         binput.humidity_map = noise_humidity->result;
239         binput.height_map   = heightmap;
240         bmgr->calcBiomes(&binput, biomemap);
241         
242         // Actually place the biome-specific nodes and what not
243         generateBiomes();
244
245         if (flags & MG_CAVES)
246                 generateCaves(stone_surface_max_y);
247
248         if (flags & MG_DUNGEONS) {
249                 DungeonGen dgen(this, NULL);
250                 dgen.generate(blockseed, full_node_min, full_node_max);
251         }
252
253         for (size_t i = 0; i != emerge->decorations.size(); i++) {
254                 Decoration *deco = emerge->decorations[i];
255                 deco->placeDeco(this, blockseed + i, node_min, node_max);
256         }
257
258         for (size_t i = 0; i != emerge->ores.size(); i++) {
259                 Ore *ore = emerge->ores[i];
260                 ore->placeOre(this, blockseed + i, node_min, node_max);
261         }
262         
263         // Sprinkle some dust on top after everything else was generated
264         dustTopNodes();
265         
266         //printf("makeChunk: %dms\n", t.stop());
267         
268         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
269         
270         if (flags & MG_LIGHT)
271                 calcLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
272                                          node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
273         //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
274         //                      node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
275         
276         this->generating = false;
277 }
278
279
280 void MapgenV7::calculateNoise() {
281         //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
282         int x = node_min.X;
283         int y = node_min.Y;
284         int z = node_min.Z;
285         
286         noise_height_select->perlinMap2D(x, z);
287         noise_height_select->transformNoiseMap();
288         
289         noise_terrain_persist->perlinMap2D(x, z);
290         noise_terrain_persist->transformNoiseMap();
291         float *persistmap = noise_terrain_persist->result;
292         for (int i = 0; i != csize.X * csize.Z; i++)
293                 persistmap[i] = rangelim(persistmap[i], 0.4, 0.9);
294         
295         noise_terrain_base->perlinMap2DModulated(x, z, persistmap);
296         noise_terrain_base->transformNoiseMap();
297         
298         noise_terrain_alt->perlinMap2DModulated(x, z, persistmap);
299         noise_terrain_alt->transformNoiseMap();
300         
301         noise_filler_depth->perlinMap2D(x, z);
302         
303         if (spflags & MGV7_MOUNTAINS) {
304                 noise_mountain->perlinMap3D(x, y, z);
305                 noise_mount_height->perlinMap2D(x, z);
306                 noise_mount_height->transformNoiseMap();
307         }
308
309         if (spflags & MGV7_RIDGES) {
310                 noise_ridge->perlinMap3D(x, y, z);
311                 noise_ridge_uwater->perlinMap2D(x, z);
312         }
313         
314         noise_heat->perlinMap2D(x, z);
315         noise_humidity->perlinMap2D(x, z);
316         
317         //printf("calculateNoise: %dus\n", t.stop());
318 }
319
320
321 Biome *MapgenV7::getBiomeAtPoint(v3s16 p) {
322         float heat      = NoisePerlin2D(bmgr->np_heat, p.X, p.Z, seed);
323         float humidity  = NoisePerlin2D(bmgr->np_humidity, p.X, p.Z, seed);
324         s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
325         
326         return bmgr->getBiome(heat, humidity, groundlevel);
327 }
328
329 //needs to be updated
330 float MapgenV7::baseTerrainLevelAtPoint(int x, int z) {
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         persist = rangelim(persist, 0.4, 0.9);
336
337         noise_terrain_base->np->persist = persist;
338         float height_base = NoisePerlin2D(noise_terrain_base->np, x, z, seed);
339
340         noise_terrain_alt->np->persist = persist;
341         float height_alt = NoisePerlin2D(noise_terrain_alt->np, x, z, seed);
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 float MapgenV7::baseTerrainLevelFromMap(int index) {
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         float mnt_h_n = NoisePerlin2D(noise_mount_height->np, x, z, seed);
364         float height_modifier = -((float)y / rangelim(mnt_h_n, 80.0, 150.0));
365         float mnt_n = NoisePerlin3D(noise_mountain->np, x, y, z, seed);
366
367         return mnt_n + height_modifier >= 0.6;
368 }
369
370
371 bool MapgenV7::getMountainTerrainFromMap(int idx_xyz, int idx_xz, int y) {
372         float mounthn = noise_mount_height->result[idx_xz];
373         float height_modifier = -((float)y / rangelim(mounthn, 80.0, 150.0));
374         return (noise_mountain->result[idx_xyz] + height_modifier >= 0.6);
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         int ymax = generateBaseTerrain();
415
416         if (spflags & MGV7_MOUNTAINS)
417                 generateMountainTerrain();
418
419         if (spflags & MGV7_RIDGES)
420                 generateRidgeTerrain();
421                 
422         return ymax;
423 }
424
425
426 int MapgenV7::generateBaseTerrain() {
427         MapNode n_air(CONTENT_AIR);
428         MapNode n_stone(c_stone);
429         MapNode n_water(c_water_source);
430         
431         int stone_surface_max_y = -MAP_GENERATION_LIMIT;
432         v3s16 em = vm->m_area.getExtent();
433         u32 index = 0;
434         
435         for (s16 z = node_min.Z; z <= node_max.Z; z++)
436         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
437                 float surface_height = baseTerrainLevelFromMap(index);
438                 s16 surface_y = (s16)surface_height;
439                 
440                 heightmap[index]       = surface_y; 
441                 ridge_heightmap[index] = surface_y;
442                 
443                 if (surface_y > stone_surface_max_y)
444                         stone_surface_max_y = surface_y;
445
446                 u32 i = vm->m_area.index(x, node_min.Y, z);             
447                 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
448                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
449                                 if (y <= surface_y)
450                                         vm->m_data[i] = n_stone;
451                                 else if (y <= water_level)
452                                         vm->m_data[i] = n_water;
453                                 else
454                                         vm->m_data[i] = n_air;
455                         }
456                         vm->m_area.add_y(em, i, 1);
457                 }
458         }
459         
460         return stone_surface_max_y;
461 }
462
463
464 void MapgenV7::generateMountainTerrain() {
465         if (node_max.Y <= water_level)
466                 return;
467                 
468         MapNode n_stone(c_stone);
469         u32 j = 0;
470         
471         for (s16 z = node_min.Z; z <= node_max.Z; z++)
472         for (s16 y = node_min.Y; y <= node_max.Y; y++) {
473                 u32 vi = vm->m_area.index(node_min.X, y, z);
474                 for (s16 x = node_min.X; x <= node_max.X; x++) {
475                         int index = (z - node_min.Z) * csize.X + (x - node_min.X);
476
477                         if (getMountainTerrainFromMap(j, index, y))
478                                 vm->m_data[vi] = n_stone;
479                                 
480                         vi++;
481                         j++;
482                 }
483         }
484 }
485
486
487 void MapgenV7::generateRidgeTerrain() {
488         MapNode n_water(c_water_source);
489         MapNode n_air(CONTENT_AIR);
490         u32 index = 0;
491         
492         for (s16 z = node_min.Z; z <= node_max.Z; z++)
493         for (s16 y = node_min.Y; y <= node_max.Y; y++) {
494                 u32 vi = vm->m_area.index(node_min.X, y, z);
495                 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
496                         int j = (z - node_min.Z) * csize.X + (x - node_min.X);
497                         
498                         if (heightmap[j] < water_level - 4)
499                                 continue;
500                         
501                         float widthn = (noise_terrain_persist->result[j] - 0.6) / 0.1;
502                         //widthn = rangelim(widthn, -0.05, 0.5);
503                         
504                         float width = 0.3; // TODO: figure out acceptable perlin noise values
505                         float uwatern = noise_ridge_uwater->result[j] * 2;
506                         if (uwatern < -width || uwatern > width)
507                                 continue;
508                         
509                         float height_mod = (float)(y + 17) / 2.5;
510                         float width_mod  = (width - fabs(uwatern));
511                         float nridge = noise_ridge->result[index] * (float)y / 7.0;
512
513                         if (y < water_level)
514                                 nridge = -fabs(nridge) * 3.0 * widthn * 0.3;
515                         
516                         if (nridge + width_mod * height_mod < 0.6)
517                                 continue;
518                         
519                         if (y < ridge_heightmap[j])
520                                 ridge_heightmap[j] = y - 1; 
521
522                         vm->m_data[vi] = (y > water_level) ? n_air : n_water;
523                 }
524         }
525 }
526
527
528 void MapgenV7::generateBiomes() {
529         if (node_max.Y < water_level)
530                 return;
531
532         MapNode n_air(CONTENT_AIR);
533         MapNode n_stone(c_stone);
534         MapNode n_water(c_water_source);
535
536         v3s16 em = vm->m_area.getExtent();
537         u32 index = 0;
538         
539         for (s16 z = node_min.Z; z <= node_max.Z; z++)
540         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
541                 Biome *biome  = bmgr->biomes[biomemap[index]];
542                 s16 dfiller   = biome->depth_filler + noise_filler_depth->result[index];
543                 s16 y0_top    = biome->depth_top;
544                 s16 y0_filler = biome->depth_filler + biome->depth_top + dfiller;
545                 
546                 s16 nplaced = 0;
547                 u32 i = vm->m_area.index(x, node_max.Y, z);     
548
549                 content_t c_above = vm->m_data[i + em.X].getContent();
550                 bool have_air = c_above == CONTENT_AIR;
551                 
552                 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
553                         content_t c = vm->m_data[i].getContent();
554                         
555                         // It could be the case that the elevation is equal to the chunk
556                         // boundary, but the chunk above has not been generated yet
557                         if (y == node_max.Y && c_above == CONTENT_IGNORE &&
558                                 y == heightmap[index] && c == c_stone) {
559                                 int j = (z - node_min.Z) * zstride +
560                                                 (y - node_min.Y) * ystride +
561                                                 (x - node_min.X);
562                                 have_air = !getMountainTerrainFromMap(j, index, y);
563                         }
564                         
565                         if (c == c_stone && have_air) {
566                                 content_t c_below = vm->m_data[i - em.X].getContent();
567                                 
568                                 if (c_below != CONTENT_AIR) {
569                                         if (nplaced < y0_top) {
570                                                 // A hack to prevent dirt_with_grass from being
571                                                 // placed below water.  TODO: fix later
572                                                 content_t c_place = ((y < water_level) &&
573                                                                 (biome->c_top == c_dirt_with_grass)) ?
574                                                                  c_dirt : biome->c_top;
575                                                 
576                                                 vm->m_data[i] = MapNode(c_place);
577                                                 nplaced++;
578                                         } else if (nplaced < y0_filler && nplaced >= y0_top) {
579                                                 vm->m_data[i] = MapNode(biome->c_filler);
580                                                 nplaced++;
581                                         } else {
582                                                 have_air = false;
583                                                 nplaced  = 0;
584                                         }
585                                 }
586                         } else if (c == c_water_source) {
587                                 have_air = true;
588                                 nplaced = 0;
589                                 vm->m_data[i] = MapNode(biome->c_water);
590                         } else if (c == CONTENT_AIR) {
591                                 have_air = true;
592                                 nplaced = 0;
593                         }
594                         
595                         vm->m_area.add_y(em, i, -1);
596                 }
597         }
598 }
599
600
601 void MapgenV7::dustTopNodes() {
602         v3s16 em = vm->m_area.getExtent();
603         u32 index = 0;
604         
605         if (water_level > node_max.Y)
606                 return;
607
608         for (s16 z = node_min.Z; z <= node_max.Z; z++)
609         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
610                 Biome *biome = bmgr->biomes[biomemap[index]];
611         
612                 if (biome->c_dust == CONTENT_IGNORE)
613                         continue;
614
615                 s16 y = node_max.Y;
616                 u32 vi = vm->m_area.index(x, y, z);
617                 for (; y >= node_min.Y; y--) {
618                         if (vm->m_data[vi].getContent() != CONTENT_AIR)
619                                 break;
620
621                         vm->m_area.add_y(em, vi, -1);
622                 }
623                         
624                 content_t c = vm->m_data[vi].getContent();
625                 if (c == biome->c_water && biome->c_dust_water != CONTENT_IGNORE) {
626                         if (y < node_min.Y)
627                                 continue;
628                                 
629                         vm->m_data[vi] = MapNode(biome->c_dust_water);
630                 } else if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE) {
631                         if (y == node_max.Y)
632                                 continue;
633                                 
634                         vm->m_area.add_y(em, vi, 1);
635                         vm->m_data[vi] = MapNode(biome->c_dust);
636                 }
637         }
638 }
639
640
641 #if 0
642 void MapgenV7::addTopNodes() {
643         v3s16 em = vm->m_area.getExtent();
644         s16 ntopnodes;
645         u32 index = 0;
646
647         for (s16 z = node_min.Z; z <= node_max.Z; z++)
648         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
649                 Biome *biome = bmgr->biomes[biomemap[index]];
650                 
651                 //////////////////// First, add top nodes below the ridge
652                 s16 y = ridge_heightmap[index];
653                 
654                 // This cutoff is good enough, but not perfect.
655                 // It will cut off potentially placed top nodes at chunk boundaries
656                 if (y < node_min.Y)
657                         continue;
658                 if (y > node_max.Y) {
659                         y = node_max.Y; // Let's see if we can still go downward anyway
660                         u32 vi = vm->m_area.index(x, y, z);
661                         content_t c = vm->m_data[vi].getContent();
662                         if (ndef->get(c).walkable)
663                                 continue;
664                 }
665                 
666                 // N.B.  It is necessary to search downward since ridge_heightmap[i]
667                 // might not be the actual height, just the lowest part in the chunk
668                 // where a ridge had been carved
669                 u32 i = vm->m_area.index(x, y, z);
670                 for (; y >= node_min.Y; y--) {
671                         content_t c = vm->m_data[i].getContent();
672                         if (ndef->get(c).walkable)
673                                 break;
674                         vm->m_area.add_y(em, i, -1);
675                 }
676
677                 if (y != node_min.Y - 1 && y >= water_level) {
678                         ridge_heightmap[index] = y; //update ridgeheight
679                         ntopnodes = biome->top_depth;
680                         for (; y <= node_max.Y && ntopnodes; y++) {
681                                 ntopnodes--;
682                                 vm->m_data[i] = MapNode(biome->c_top);
683                                 vm->m_area.add_y(em, i, 1);
684                         }
685                         // If dirt, grow grass on it.
686                         if (y > water_level - 10 &&
687                                 vm->m_data[i].getContent() == CONTENT_AIR) {
688                                 vm->m_area.add_y(em, i, -1);
689                                 if (vm->m_data[i].getContent() == c_dirt)
690                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
691                         }
692                 }
693                 
694                 //////////////////// Now, add top nodes on top of the ridge
695                 y = heightmap[index];
696                 if (y > node_max.Y) {
697                         y = node_max.Y; // Let's see if we can still go downward anyway
698                         u32 vi = vm->m_area.index(x, y, z);
699                         content_t c = vm->m_data[vi].getContent();
700                         if (ndef->get(c).walkable)
701                                 continue;
702                 }
703
704                 i = vm->m_area.index(x, y, z);
705                 for (; y >= node_min.Y; y--) {
706                         content_t c = vm->m_data[i].getContent();
707                         if (ndef->get(c).walkable)
708                                 break;
709                         vm->m_area.add_y(em, i, -1);
710                 }
711
712                 if (y != node_min.Y - 1) {
713                         ntopnodes = biome->top_depth;
714                         // Let's see if we've already added it...
715                         if (y == ridge_heightmap[index] + ntopnodes - 1)
716                                 continue;
717
718                         for (; y <= node_max.Y && ntopnodes; y++) {
719                                 ntopnodes--;
720                                 vm->m_data[i] = MapNode(biome->c_top);
721                                 vm->m_area.add_y(em, i, 1);
722                         }
723                         // If dirt, grow grass on it.
724                         if (y > water_level - 10 &&
725                                 vm->m_data[i].getContent() == CONTENT_AIR) {
726                                 vm->m_area.add_y(em, i, -1);
727                                 if (vm->m_data[i].getContent() == c_dirt)
728                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
729                         }
730                 }
731         }
732 }
733 #endif
734
735
736 NoiseParams nparams_v7_def_cave(6, 6.0, v3f(250.0, 250.0, 250.0), 34329, 3, 0.50);
737
738 void MapgenV7::generateCaves(int max_stone_y) {
739         PseudoRandom ps(blockseed + 21343);
740
741         int volume_nodes = (node_max.X - node_min.X + 1) *
742                                            (node_max.Y - node_min.Y + 1) *
743                                            (node_max.Z - node_min.Z + 1);
744         float cave_amount = NoisePerlin2D(&nparams_v7_def_cave,
745                                                                 node_min.X, node_min.Y, seed);
746
747         u32 caves_count = MYMAX(0.0, cave_amount) * volume_nodes / 250000;
748         for (u32 i = 0; i < caves_count; i++) {
749                 CaveV7 cave(this, &ps, false);
750                 cave.makeCave(node_min, node_max, max_stone_y);
751         }
752
753         u32 bruises_count = (ps.range(1, 8) == 1) ? ps.range(0, ps.range(0, 2)) : 1;
754         for (u32 i = 0; i < bruises_count; i++) {
755                 CaveV7 cave(this, &ps, true);
756                 cave.makeCave(node_min, node_max, max_stone_y);
757         }
758 }