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