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