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