]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen_v7.cpp
Mapgen v7: Fix uninitialized spflags
[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         BiomeNoiseInput binput;
237         binput.mapsize      = v2s16(csize.X, csize.Z);
238         binput.heat_map     = noise_heat->result;
239         binput.humidity_map = noise_humidity->result;
240         binput.height_map   = heightmap;
241         bmgr->calcBiomes(&binput, biomemap);
242         
243         // Actually place the biome-specific nodes and what not
244         generateBiomes();
245
246         if (flags & MG_CAVES)
247                 generateCaves(stone_surface_max_y);
248
249         if (flags & MG_DUNGEONS) {
250                 DungeonGen dgen(this, NULL);
251                 dgen.generate(blockseed, full_node_min, full_node_max);
252         }
253
254         // Generate the registered decorations
255         emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
256
257         // Generate the registered ores
258         emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
259
260         // Sprinkle some dust on top after everything else was generated
261         dustTopNodes();
262         
263         //printf("makeChunk: %dms\n", t.stop());
264         
265         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
266         
267         if (flags & MG_LIGHT)
268                 calcLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
269                                          node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
270         //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
271         //                      node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
272         
273         this->generating = false;
274 }
275
276
277 void MapgenV7::calculateNoise() {
278         //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
279         int x = node_min.X;
280         int y = node_min.Y;
281         int z = node_min.Z;
282         
283         noise_height_select->perlinMap2D(x, z);
284         noise_height_select->transformNoiseMap();
285         
286         noise_terrain_persist->perlinMap2D(x, z);
287         noise_terrain_persist->transformNoiseMap();
288         float *persistmap = noise_terrain_persist->result;
289         for (int i = 0; i != csize.X * csize.Z; i++)
290                 persistmap[i] = rangelim(persistmap[i], 0.4, 0.9);
291         
292         noise_terrain_base->perlinMap2DModulated(x, z, persistmap);
293         noise_terrain_base->transformNoiseMap();
294         
295         noise_terrain_alt->perlinMap2DModulated(x, z, persistmap);
296         noise_terrain_alt->transformNoiseMap();
297         
298         noise_filler_depth->perlinMap2D(x, z);
299         
300         if (spflags & MGV7_MOUNTAINS) {
301                 noise_mountain->perlinMap3D(x, y, z);
302                 noise_mount_height->perlinMap2D(x, z);
303                 noise_mount_height->transformNoiseMap();
304         }
305
306         if (spflags & MGV7_RIDGES) {
307                 noise_ridge->perlinMap3D(x, y, z);
308                 noise_ridge_uwater->perlinMap2D(x, z);
309         }
310         
311         noise_heat->perlinMap2D(x, z);
312         noise_humidity->perlinMap2D(x, z);
313         
314         //printf("calculateNoise: %dus\n", t.stop());
315 }
316
317
318 Biome *MapgenV7::getBiomeAtPoint(v3s16 p) {
319         float heat      = NoisePerlin2D(bmgr->np_heat, p.X, p.Z, seed);
320         float humidity  = NoisePerlin2D(bmgr->np_humidity, p.X, p.Z, seed);
321         s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
322         
323         return bmgr->getBiome(heat, humidity, groundlevel);
324 }
325
326 //needs to be updated
327 float MapgenV7::baseTerrainLevelAtPoint(int x, int z) {
328         float hselect = NoisePerlin2D(noise_height_select->np, x, z, seed);
329         hselect = rangelim(hselect, 0.0, 1.0);
330         
331         float persist = NoisePerlin2D(noise_terrain_persist->np, x, z, seed);
332         persist = rangelim(persist, 0.4, 0.9);
333
334         noise_terrain_base->np->persist = persist;
335         float height_base = NoisePerlin2D(noise_terrain_base->np, x, z, seed);
336
337         noise_terrain_alt->np->persist = persist;
338         float height_alt = NoisePerlin2D(noise_terrain_alt->np, x, z, seed);
339
340         if (height_alt > height_base)
341                 return height_alt;
342                 
343         return (height_base * hselect) + (height_alt * (1.0 - hselect));
344 }
345
346
347 float MapgenV7::baseTerrainLevelFromMap(int index) {
348         float hselect     = rangelim(noise_height_select->result[index], 0.0, 1.0);
349         float height_base = noise_terrain_base->result[index];
350         float height_alt  = noise_terrain_alt->result[index];
351         
352         if (height_alt > height_base)
353                 return height_alt;
354
355         return (height_base * hselect) + (height_alt * (1.0 - hselect));
356 }
357
358
359 bool MapgenV7::getMountainTerrainAtPoint(int x, int y, int z) {
360         float mnt_h_n = NoisePerlin2D(noise_mount_height->np, x, z, seed);
361         float height_modifier = -((float)y / rangelim(mnt_h_n, 80.0, 150.0));
362         float mnt_n = NoisePerlin3D(noise_mountain->np, x, y, z, seed);
363
364         return mnt_n + height_modifier >= 0.6;
365 }
366
367
368 bool MapgenV7::getMountainTerrainFromMap(int idx_xyz, int idx_xz, int y) {
369         float mounthn = noise_mount_height->result[idx_xz];
370         float height_modifier = -((float)y / rangelim(mounthn, 80.0, 150.0));
371         return (noise_mountain->result[idx_xyz] + height_modifier >= 0.6);
372 }
373
374
375 #if 0
376 void MapgenV7::carveRivers() {
377         MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
378         MapNode n_stone(c_stone);
379         u32 index = 0;
380         
381         int river_depth = 4;
382
383         for (s16 z = node_min.Z; z <= node_max.Z; z++)
384         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
385                 float terrain_mod  = noise_terrain_mod->result[index];
386                 NoiseParams *np = noise_terrain_river->np;
387                 np->persist = noise_terrain_persist->result[index];
388                 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
389                 float height = terrain_river * (1 - abs(terrain_mod)) *
390                                                 noise_terrain_river->np->scale;
391                 height = log(height * height); //log(h^3) is pretty interesting for terrain
392                 
393                 s16 y = heightmap[index];
394                 if (height < 1.0 && y > river_depth &&
395                         y - river_depth >= node_min.Y && y <= node_max.Y) {
396                         
397                         for (s16 ry = y; ry != y - river_depth; ry--) {
398                                 u32 vi = vm->m_area.index(x, ry, z);
399                                 vm->m_data[vi] = n_air;
400                         }
401                         
402                         u32 vi = vm->m_area.index(x, y - river_depth, z);
403                         vm->m_data[vi] = n_water_source;
404                 }
405         }
406 }
407 #endif
408
409
410 int MapgenV7::generateTerrain() {
411         int ymax = generateBaseTerrain();
412
413         if (spflags & MGV7_MOUNTAINS)
414                 generateMountainTerrain();
415
416         if (spflags & MGV7_RIDGES)
417                 generateRidgeTerrain();
418                 
419         return ymax;
420 }
421
422
423 int MapgenV7::generateBaseTerrain() {
424         MapNode n_air(CONTENT_AIR);
425         MapNode n_stone(c_stone);
426         MapNode n_water(c_water_source);
427         
428         int stone_surface_max_y = -MAP_GENERATION_LIMIT;
429         v3s16 em = vm->m_area.getExtent();
430         u32 index = 0;
431         
432         for (s16 z = node_min.Z; z <= node_max.Z; z++)
433         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
434                 float surface_height = baseTerrainLevelFromMap(index);
435                 s16 surface_y = (s16)surface_height;
436                 
437                 heightmap[index]       = surface_y; 
438                 ridge_heightmap[index] = surface_y;
439                 
440                 if (surface_y > stone_surface_max_y)
441                         stone_surface_max_y = surface_y;
442
443                 u32 i = vm->m_area.index(x, node_min.Y, z);             
444                 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
445                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
446                                 if (y <= surface_y)
447                                         vm->m_data[i] = n_stone;
448                                 else if (y <= water_level)
449                                         vm->m_data[i] = n_water;
450                                 else
451                                         vm->m_data[i] = n_air;
452                         }
453                         vm->m_area.add_y(em, i, 1);
454                 }
455         }
456         
457         return stone_surface_max_y;
458 }
459
460
461 void MapgenV7::generateMountainTerrain() {
462         if (node_max.Y <= water_level)
463                 return;
464                 
465         MapNode n_stone(c_stone);
466         u32 j = 0;
467         
468         for (s16 z = node_min.Z; z <= node_max.Z; z++)
469         for (s16 y = node_min.Y; y <= node_max.Y; y++) {
470                 u32 vi = vm->m_area.index(node_min.X, y, z);
471                 for (s16 x = node_min.X; x <= node_max.X; x++) {
472                         int index = (z - node_min.Z) * csize.X + (x - node_min.X);
473
474                         if (getMountainTerrainFromMap(j, index, y))
475                                 vm->m_data[vi] = n_stone;
476                                 
477                         vi++;
478                         j++;
479                 }
480         }
481 }
482
483
484 void MapgenV7::generateRidgeTerrain() {
485         MapNode n_water(c_water_source);
486         MapNode n_air(CONTENT_AIR);
487         u32 index = 0;
488         
489         for (s16 z = node_min.Z; z <= node_max.Z; z++)
490         for (s16 y = node_min.Y; y <= node_max.Y; y++) {
491                 u32 vi = vm->m_area.index(node_min.X, y, z);
492                 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
493                         int j = (z - node_min.Z) * csize.X + (x - node_min.X);
494                         
495                         if (heightmap[j] < water_level - 4)
496                                 continue;
497                         
498                         float widthn = (noise_terrain_persist->result[j] - 0.6) / 0.1;
499                         //widthn = rangelim(widthn, -0.05, 0.5);
500                         
501                         float width = 0.3; // TODO: figure out acceptable perlin noise values
502                         float uwatern = noise_ridge_uwater->result[j] * 2;
503                         if (uwatern < -width || uwatern > width)
504                                 continue;
505                         
506                         float height_mod = (float)(y + 17) / 2.5;
507                         float width_mod  = (width - fabs(uwatern));
508                         float nridge = noise_ridge->result[index] * (float)y / 7.0;
509
510                         if (y < water_level)
511                                 nridge = -fabs(nridge) * 3.0 * widthn * 0.3;
512                         
513                         if (nridge + width_mod * height_mod < 0.6)
514                                 continue;
515                         
516                         if (y < ridge_heightmap[j])
517                                 ridge_heightmap[j] = y - 1; 
518
519                         vm->m_data[vi] = (y > water_level) ? n_air : n_water;
520                 }
521         }
522 }
523
524
525 void MapgenV7::generateBiomes() {
526         if (node_max.Y < water_level)
527                 return;
528
529         MapNode n_air(CONTENT_AIR);
530         MapNode n_stone(c_stone);
531         MapNode n_water(c_water_source);
532
533         v3s16 em = vm->m_area.getExtent();
534         u32 index = 0;
535         
536         for (s16 z = node_min.Z; z <= node_max.Z; z++)
537         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
538                 Biome *biome  = (Biome *)bmgr->get(biomemap[index]);
539                 s16 dfiller   = biome->depth_filler + noise_filler_depth->result[index];
540                 s16 y0_top    = biome->depth_top;
541                 s16 y0_filler = biome->depth_filler + biome->depth_top + dfiller;
542
543                 s16 nplaced = 0;
544                 u32 i = vm->m_area.index(x, node_max.Y, z);     
545
546                 content_t c_above = vm->m_data[i + em.X].getContent();
547                 bool have_air = c_above == CONTENT_AIR;
548                 
549                 for (s16 y = node_max.Y; y >= node_min.Y; y--) {
550                         content_t c = vm->m_data[i].getContent();
551                         
552                         // It could be the case that the elevation is equal to the chunk
553                         // boundary, but the chunk above has not been generated yet
554                         if (y == node_max.Y && c_above == CONTENT_IGNORE &&
555                                 y == heightmap[index] && c == c_stone) {
556                                 int j = (z - node_min.Z) * zstride +
557                                                 (y - node_min.Y) * ystride +
558                                                 (x - node_min.X);
559                                 have_air = !getMountainTerrainFromMap(j, index, y);
560                         }
561                         
562                         if (c == c_stone && have_air) {
563                                 content_t c_below = vm->m_data[i - em.X].getContent();
564                                 
565                                 if (c_below != CONTENT_AIR) {
566                                         if (nplaced < y0_top) {
567                                                 // A hack to prevent dirt_with_grass from being
568                                                 // placed below water.  TODO: fix later
569                                                 content_t c_place = ((y < water_level) &&
570                                                                 (biome->c_top == c_dirt_with_grass)) ?
571                                                                  c_dirt : biome->c_top;
572                                                 
573                                                 vm->m_data[i] = MapNode(c_place);
574                                                 nplaced++;
575                                         } else if (nplaced < y0_filler && nplaced >= y0_top) {
576                                                 vm->m_data[i] = MapNode(biome->c_filler);
577                                                 nplaced++;
578                                         } else {
579                                                 have_air = false;
580                                                 nplaced  = 0;
581                                         }
582                                 }
583                         } else if (c == c_water_source) {
584                                 have_air = true;
585                                 nplaced = 0;
586                                 vm->m_data[i] = MapNode(biome->c_water);
587                         } else if (c == CONTENT_AIR) {
588                                 have_air = true;
589                                 nplaced = 0;
590                         }
591                         
592                         vm->m_area.add_y(em, i, -1);
593                 }
594         }
595 }
596
597
598 void MapgenV7::dustTopNodes() {
599         v3s16 em = vm->m_area.getExtent();
600         u32 index = 0;
601         
602         if (water_level > node_max.Y)
603                 return;
604
605         for (s16 z = node_min.Z; z <= node_max.Z; z++)
606         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
607                 Biome *biome = (Biome *)bmgr->get(biomemap[index]);
608         
609                 if (biome->c_dust == CONTENT_IGNORE)
610                         continue;
611
612                 s16 y = node_max.Y;
613                 u32 vi = vm->m_area.index(x, y, z);
614                 for (; y >= node_min.Y; y--) {
615                         if (vm->m_data[vi].getContent() != CONTENT_AIR)
616                                 break;
617
618                         vm->m_area.add_y(em, vi, -1);
619                 }
620                         
621                 content_t c = vm->m_data[vi].getContent();
622                 if (c == biome->c_water && biome->c_dust_water != CONTENT_IGNORE) {
623                         if (y < node_min.Y)
624                                 continue;
625                                 
626                         vm->m_data[vi] = MapNode(biome->c_dust_water);
627                 } else if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE) {
628                         if (y == node_max.Y)
629                                 continue;
630                                 
631                         vm->m_area.add_y(em, vi, 1);
632                         vm->m_data[vi] = MapNode(biome->c_dust);
633                 }
634         }
635 }
636
637
638 #if 0
639 void MapgenV7::addTopNodes() {
640         v3s16 em = vm->m_area.getExtent();
641         s16 ntopnodes;
642         u32 index = 0;
643
644         for (s16 z = node_min.Z; z <= node_max.Z; z++)
645         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
646                 Biome *biome = bmgr->biomes[biomemap[index]];
647                 
648                 //////////////////// First, add top nodes below the ridge
649                 s16 y = ridge_heightmap[index];
650                 
651                 // This cutoff is good enough, but not perfect.
652                 // It will cut off potentially placed top nodes at chunk boundaries
653                 if (y < node_min.Y)
654                         continue;
655                 if (y > node_max.Y) {
656                         y = node_max.Y; // Let's see if we can still go downward anyway
657                         u32 vi = vm->m_area.index(x, y, z);
658                         content_t c = vm->m_data[vi].getContent();
659                         if (ndef->get(c).walkable)
660                                 continue;
661                 }
662                 
663                 // N.B.  It is necessary to search downward since ridge_heightmap[i]
664                 // might not be the actual height, just the lowest part in the chunk
665                 // where a ridge had been carved
666                 u32 i = vm->m_area.index(x, y, z);
667                 for (; y >= node_min.Y; y--) {
668                         content_t c = vm->m_data[i].getContent();
669                         if (ndef->get(c).walkable)
670                                 break;
671                         vm->m_area.add_y(em, i, -1);
672                 }
673
674                 if (y != node_min.Y - 1 && y >= water_level) {
675                         ridge_heightmap[index] = y; //update ridgeheight
676                         ntopnodes = biome->top_depth;
677                         for (; y <= node_max.Y && ntopnodes; y++) {
678                                 ntopnodes--;
679                                 vm->m_data[i] = MapNode(biome->c_top);
680                                 vm->m_area.add_y(em, i, 1);
681                         }
682                         // If dirt, grow grass on it.
683                         if (y > water_level - 10 &&
684                                 vm->m_data[i].getContent() == CONTENT_AIR) {
685                                 vm->m_area.add_y(em, i, -1);
686                                 if (vm->m_data[i].getContent() == c_dirt)
687                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
688                         }
689                 }
690                 
691                 //////////////////// Now, add top nodes on top of the ridge
692                 y = heightmap[index];
693                 if (y > node_max.Y) {
694                         y = node_max.Y; // Let's see if we can still go downward anyway
695                         u32 vi = vm->m_area.index(x, y, z);
696                         content_t c = vm->m_data[vi].getContent();
697                         if (ndef->get(c).walkable)
698                                 continue;
699                 }
700
701                 i = vm->m_area.index(x, y, z);
702                 for (; y >= node_min.Y; y--) {
703                         content_t c = vm->m_data[i].getContent();
704                         if (ndef->get(c).walkable)
705                                 break;
706                         vm->m_area.add_y(em, i, -1);
707                 }
708
709                 if (y != node_min.Y - 1) {
710                         ntopnodes = biome->top_depth;
711                         // Let's see if we've already added it...
712                         if (y == ridge_heightmap[index] + ntopnodes - 1)
713                                 continue;
714
715                         for (; y <= node_max.Y && ntopnodes; y++) {
716                                 ntopnodes--;
717                                 vm->m_data[i] = MapNode(biome->c_top);
718                                 vm->m_area.add_y(em, i, 1);
719                         }
720                         // If dirt, grow grass on it.
721                         if (y > water_level - 10 &&
722                                 vm->m_data[i].getContent() == CONTENT_AIR) {
723                                 vm->m_area.add_y(em, i, -1);
724                                 if (vm->m_data[i].getContent() == c_dirt)
725                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
726                         }
727                 }
728         }
729 }
730 #endif
731
732
733 NoiseParams nparams_v7_def_cave(6, 6.0, v3f(250.0, 250.0, 250.0), 34329, 3, 0.50);
734
735 void MapgenV7::generateCaves(int max_stone_y) {
736         PseudoRandom ps(blockseed + 21343);
737
738         int volume_nodes = (node_max.X - node_min.X + 1) *
739                                            (node_max.Y - node_min.Y + 1) *
740                                            (node_max.Z - node_min.Z + 1);
741         float cave_amount = NoisePerlin2D(&nparams_v7_def_cave,
742                                                                 node_min.X, node_min.Y, seed);
743
744         u32 caves_count = MYMAX(0.0, cave_amount) * volume_nodes / 250000;
745         for (u32 i = 0; i < caves_count; i++) {
746                 CaveV7 cave(this, &ps, false);
747                 cave.makeCave(node_min, node_max, max_stone_y);
748         }
749
750         u32 bruises_count = (ps.range(1, 8) == 1) ? ps.range(0, ps.range(0, 2)) : 1;
751         for (u32 i = 0; i < bruises_count; i++) {
752                 CaveV7 cave(this, &ps, true);
753                 cave.makeCave(node_min, node_max, max_stone_y);
754         }
755 }