]> git.lizzy.rs Git - minetest.git/blob - src/mapgen_v7.cpp
e6801a24b75293ad38db2838cbb8894274803675
[minetest.git] / src / mapgen_v7.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 paramat, Matt Gregory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21
22 #include "mapgen.h"
23 #include "voxel.h"
24 #include "noise.h"
25 #include "mapblock.h"
26 #include "mapnode.h"
27 #include "map.h"
28 #include "content_sao.h"
29 #include "nodedef.h"
30 #include "voxelalgorithms.h"
31 //#include "profiler.h" // For TimeTaker
32 #include "settings.h" // For g_settings
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
53 MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge)
54         : MapgenBasic(mapgenid, params, emerge)
55 {
56         this->m_emerge = emerge;
57         this->bmgr     = emerge->biomemgr;
58
59         //// amount of elements to skip for the next index
60         //// for noise/height/biome maps (not vmanip)
61         this->ystride = csize.X;
62         // 1-up 1-down overgeneration
63         this->zstride_1u1d = csize.X * (csize.Y + 2);
64         // 1-down overgeneration
65         this->zstride_1d = csize.X * (csize.Y + 1);
66
67         this->heightmap       = new s16[csize.X * csize.Z];
68         this->ridge_heightmap = new s16[csize.X * csize.Z];
69
70         MapgenV7Params *sp = (MapgenV7Params *)params->sparams;
71
72         this->spflags    = sp->spflags;
73         this->cave_width = sp->cave_width;
74
75         //// Terrain noise
76         noise_terrain_base    = new Noise(&sp->np_terrain_base,    seed, csize.X, csize.Z);
77         noise_terrain_alt     = new Noise(&sp->np_terrain_alt,     seed, csize.X, csize.Z);
78         noise_terrain_persist = new Noise(&sp->np_terrain_persist, seed, csize.X, csize.Z);
79         noise_height_select   = new Noise(&sp->np_height_select,   seed, csize.X, csize.Z);
80         noise_filler_depth    = new Noise(&sp->np_filler_depth,    seed, csize.X, csize.Z);
81         noise_mount_height    = new Noise(&sp->np_mount_height,    seed, csize.X, csize.Z);
82         noise_ridge_uwater    = new Noise(&sp->np_ridge_uwater,    seed, csize.X, csize.Z);
83
84         //// 3d terrain noise
85         // 1-up 1-down overgeneration
86         noise_mountain = new Noise(&sp->np_mountain, seed, csize.X, csize.Y + 2, csize.Z);
87         noise_ridge    = new Noise(&sp->np_ridge,    seed, csize.X, csize.Y + 2, csize.Z);
88         // 1-down overgeneraion
89         noise_cave1    = new Noise(&sp->np_cave1,    seed, csize.X, csize.Y + 1, csize.Z);
90         noise_cave2    = new Noise(&sp->np_cave2,    seed, csize.X, csize.Y + 1, csize.Z);
91
92         // TODO(hmmmm): should we have a way to disable biomemanager biomes?
93         //// Initialize biome generator
94         biomegen = emerge->biomemgr->createBiomeGen(
95                 BIOMEGEN_ORIGINAL, params->bparams, csize);
96         biomemap = biomegen->biomemap;
97
98         //// Resolve nodes to be used
99         c_stone                = ndef->getId("mapgen_stone");
100         c_water_source         = ndef->getId("mapgen_water_source");
101         c_lava_source          = ndef->getId("mapgen_lava_source");
102         c_desert_stone         = ndef->getId("mapgen_desert_stone");
103         c_ice                  = ndef->getId("mapgen_ice");
104         c_sandstone            = ndef->getId("mapgen_sandstone");
105
106         c_cobble               = ndef->getId("mapgen_cobble");
107         c_stair_cobble         = ndef->getId("mapgen_stair_cobble");
108         c_mossycobble          = ndef->getId("mapgen_mossycobble");
109         c_sandstonebrick       = ndef->getId("mapgen_sandstonebrick");
110         c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
111
112         if (c_ice == CONTENT_IGNORE)
113                 c_ice = CONTENT_AIR;
114         if (c_mossycobble == CONTENT_IGNORE)
115                 c_mossycobble = c_cobble;
116         if (c_stair_cobble == CONTENT_IGNORE)
117                 c_stair_cobble = c_cobble;
118         if (c_sandstonebrick == CONTENT_IGNORE)
119                 c_sandstonebrick = c_sandstone;
120         if (c_stair_sandstonebrick == CONTENT_IGNORE)
121                 c_stair_sandstonebrick = c_sandstone;
122 }
123
124
125 MapgenV7::~MapgenV7()
126 {
127         delete noise_terrain_base;
128         delete noise_terrain_persist;
129         delete noise_height_select;
130         delete noise_terrain_alt;
131         delete noise_filler_depth;
132         delete noise_mount_height;
133         delete noise_ridge_uwater;
134         delete noise_mountain;
135         delete noise_ridge;
136         delete noise_cave1;
137         delete noise_cave2;
138
139         delete biomegen;
140
141         delete[] ridge_heightmap;
142         delete[] heightmap;
143 }
144
145
146 MapgenV7Params::MapgenV7Params()
147 {
148         spflags    = MGV7_MOUNTAINS | MGV7_RIDGES;
149         cave_width = 0.3;
150
151         np_terrain_base    = NoiseParams(4,    70,  v3f(600,  600,  600),  82341, 5, 0.6,  2.0);
152         np_terrain_alt     = NoiseParams(4,    25,  v3f(600,  600,  600),  5934,  5, 0.6,  2.0);
153         np_terrain_persist = NoiseParams(0.6,  0.1, v3f(2000, 2000, 2000), 539,   3, 0.6,  2.0);
154         np_height_select   = NoiseParams(-8,   16,  v3f(500,  500,  500),  4213,  6, 0.7,  2.0);
155         np_filler_depth    = NoiseParams(0,    1.2, v3f(150,  150,  150),  261,   3, 0.7,  2.0);
156         np_mount_height    = NoiseParams(256,  112, v3f(1000, 1000, 1000), 72449, 3, 0.6,  2.0);
157         np_ridge_uwater    = NoiseParams(0,    1,   v3f(1000, 1000, 1000), 85039, 5, 0.6,  2.0);
158         np_mountain        = NoiseParams(-0.6, 1,   v3f(250,  350,  250),  5333,  5, 0.63, 2.0);
159         np_ridge           = NoiseParams(0,    1,   v3f(100,  100,  100),  6467,  4, 0.75, 2.0);
160         np_cave1           = NoiseParams(0,    12,  v3f(96,   96,   96),   52534, 4, 0.5,  2.0);
161         np_cave2           = NoiseParams(0,    12,  v3f(96,   96,   96),   10325, 4, 0.5,  2.0);
162 }
163
164
165 void MapgenV7Params::readParams(const Settings *settings)
166 {
167         settings->getFlagStrNoEx("mgv7_spflags",  spflags, flagdesc_mapgen_v7);
168         settings->getFloatNoEx("mgv7_cave_width", cave_width);
169
170         settings->getNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
171         settings->getNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
172         settings->getNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
173         settings->getNoiseParams("mgv7_np_height_select",   np_height_select);
174         settings->getNoiseParams("mgv7_np_filler_depth",    np_filler_depth);
175         settings->getNoiseParams("mgv7_np_mount_height",    np_mount_height);
176         settings->getNoiseParams("mgv7_np_ridge_uwater",    np_ridge_uwater);
177         settings->getNoiseParams("mgv7_np_mountain",        np_mountain);
178         settings->getNoiseParams("mgv7_np_ridge",           np_ridge);
179         settings->getNoiseParams("mgv7_np_cave1",           np_cave1);
180         settings->getNoiseParams("mgv7_np_cave2",           np_cave2);
181 }
182
183
184 void MapgenV7Params::writeParams(Settings *settings) const
185 {
186         settings->setFlagStr("mgv7_spflags",  spflags, flagdesc_mapgen_v7, U32_MAX);
187         settings->setFloat("mgv7_cave_width", cave_width);
188
189         settings->setNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
190         settings->setNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
191         settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
192         settings->setNoiseParams("mgv7_np_height_select",   np_height_select);
193         settings->setNoiseParams("mgv7_np_filler_depth",    np_filler_depth);
194         settings->setNoiseParams("mgv7_np_mount_height",    np_mount_height);
195         settings->setNoiseParams("mgv7_np_ridge_uwater",    np_ridge_uwater);
196         settings->setNoiseParams("mgv7_np_mountain",        np_mountain);
197         settings->setNoiseParams("mgv7_np_ridge",           np_ridge);
198         settings->setNoiseParams("mgv7_np_cave1",           np_cave1);
199         settings->setNoiseParams("mgv7_np_cave2",           np_cave2);
200 }
201
202
203 ///////////////////////////////////////////////////////////////////////////////
204
205
206 int MapgenV7::getSpawnLevelAtPoint(v2s16 p)
207 {
208         // Base terrain calculation
209         s16 y = baseTerrainLevelAtPoint(p.X, p.Y);
210
211         // Ridge/river terrain calculation
212         float width = 0.2;
213         float uwatern = NoisePerlin2D(&noise_ridge_uwater->np, p.X, p.Y, seed) * 2;
214         // if inside a river this is an unsuitable spawn point
215         if (fabs(uwatern) <= width)
216                 return MAX_MAP_GENERATION_LIMIT;
217
218         // Mountain terrain calculation
219         int iters = 128;
220         while (iters--) {
221                 if (!getMountainTerrainAtPoint(p.X, y + 1, p.Y)) {  // Air, y is ground level
222                         if (y <= water_level || y > water_level + 16)
223                                 return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
224                         else
225                                 return y;
226                 }
227                 y++;
228         }
229
230         // Unsuitable spawn point, no ground surface found
231         return MAX_MAP_GENERATION_LIMIT;
232 }
233
234
235 void MapgenV7::makeChunk(BlockMakeData *data)
236 {
237         // Pre-conditions
238         assert(data->vmanip);
239         assert(data->nodedef);
240         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
241                 data->blockpos_requested.Y >= data->blockpos_min.Y &&
242                 data->blockpos_requested.Z >= data->blockpos_min.Z);
243         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
244                 data->blockpos_requested.Y <= data->blockpos_max.Y &&
245                 data->blockpos_requested.Z <= data->blockpos_max.Z);
246
247         this->generating = true;
248         this->vm   = data->vmanip;
249         this->ndef = data->nodedef;
250         //TimeTaker t("makeChunk");
251
252         v3s16 blockpos_min = data->blockpos_min;
253         v3s16 blockpos_max = data->blockpos_max;
254         node_min = blockpos_min * MAP_BLOCKSIZE;
255         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
256         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
257         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
258
259         blockseed = getBlockSeed2(full_node_min, seed);
260
261         // Make some noise
262         calculateNoise();
263
264         // Generate terrain and ridges with initial heightmaps
265         s16 stone_surface_max_y = generateTerrain();
266
267         if (spflags & MGV7_RIDGES)
268                 generateRidgeTerrain();
269
270         // Update heightmap to include mountain terrain
271         updateHeightmap(node_min, node_max);
272
273         // Init biome generator, place biome-specific nodes, and build biomemap
274         biomegen->calcBiomeNoise(node_min);
275         biomegen->getBiomes(heightmap);
276         MgStoneType stone_type = generateBiomes();
277
278         if (flags & MG_CAVES)
279                 generateCaves(stone_surface_max_y, water_level);
280
281         if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) {
282                 DungeonParams dp;
283
284                 dp.np_rarity  = nparams_dungeon_rarity;
285                 dp.np_density = nparams_dungeon_density;
286                 dp.np_wetness = nparams_dungeon_wetness;
287                 dp.c_water    = c_water_source;
288                 if (stone_type == MGSTONE_STONE) {
289                         dp.c_cobble = c_cobble;
290                         dp.c_moss   = c_mossycobble;
291                         dp.c_stair  = c_stair_cobble;
292
293                         dp.diagonal_dirs = false;
294                         dp.mossratio     = 3.0;
295                         dp.holesize      = v3s16(1, 2, 1);
296                         dp.roomsize      = v3s16(0, 0, 0);
297                         dp.notifytype    = GENNOTIFY_DUNGEON;
298                 } else if (stone_type == MGSTONE_DESERT_STONE) {
299                         dp.c_cobble = c_desert_stone;
300                         dp.c_moss   = c_desert_stone;
301                         dp.c_stair  = c_desert_stone;
302
303                         dp.diagonal_dirs = true;
304                         dp.mossratio     = 0.0;
305                         dp.holesize      = v3s16(2, 3, 2);
306                         dp.roomsize      = v3s16(2, 5, 2);
307                         dp.notifytype    = GENNOTIFY_TEMPLE;
308                 } else if (stone_type == MGSTONE_SANDSTONE) {
309                         dp.c_cobble = c_sandstonebrick;
310                         dp.c_moss   = c_sandstonebrick;
311                         dp.c_stair  = c_sandstonebrick;
312
313                         dp.diagonal_dirs = false;
314                         dp.mossratio     = 0.0;
315                         dp.holesize      = v3s16(2, 2, 2);
316                         dp.roomsize      = v3s16(2, 0, 2);
317                         dp.notifytype    = GENNOTIFY_DUNGEON;
318                 }
319
320                 DungeonGen dgen(this, &dp);
321                 dgen.generate(blockseed, full_node_min, full_node_max);
322         }
323
324         // Generate the registered decorations
325         if (flags & MG_DECORATIONS)
326                 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
327
328         // Generate the registered ores
329         m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
330
331         // Sprinkle some dust on top after everything else was generated
332         dustTopNodes();
333
334         //printf("makeChunk: %dms\n", t.stop());
335
336         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
337
338         if (flags & MG_LIGHT)
339                 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
340                         full_node_min, full_node_max);
341
342         //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
343         //                      node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
344
345         this->generating = false;
346 }
347
348
349 void MapgenV7::calculateNoise()
350 {
351         //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
352         s16 x = node_min.X;
353         s16 y = node_min.Y - 1;
354         s16 z = node_min.Z;
355
356         noise_terrain_persist->perlinMap2D(x, z);
357         float *persistmap = noise_terrain_persist->result;
358
359         noise_terrain_base->perlinMap2D(x, z, persistmap);
360         noise_terrain_alt->perlinMap2D(x, z, persistmap);
361         noise_height_select->perlinMap2D(x, z);
362
363         if (spflags & MGV7_MOUNTAINS) {
364                 noise_mountain->perlinMap3D(x, y, z);
365                 noise_mount_height->perlinMap2D(x, z);
366         }
367
368         if ((spflags & MGV7_RIDGES) && node_max.Y >= water_level) {
369                 noise_ridge->perlinMap3D(x, y, z);
370                 noise_ridge_uwater->perlinMap2D(x, z);
371         }
372
373         // Cave noises are calculated in generateCaves()
374         // only if solid terrain is present in mapchunk
375
376         //printf("calculateNoise: %dus\n", t.stop());
377 }
378
379
380 float MapgenV7::baseTerrainLevelAtPoint(s16 x, s16 z)
381 {
382         float hselect = NoisePerlin2D(&noise_height_select->np, x, z, seed);
383         hselect = rangelim(hselect, 0.0, 1.0);
384
385         float persist = NoisePerlin2D(&noise_terrain_persist->np, x, z, seed);
386
387         noise_terrain_base->np.persist = persist;
388         float height_base = NoisePerlin2D(&noise_terrain_base->np, x, z, seed);
389
390         noise_terrain_alt->np.persist = persist;
391         float height_alt = NoisePerlin2D(&noise_terrain_alt->np, x, z, seed);
392
393         if (height_alt > height_base)
394                 return height_alt;
395
396         return (height_base * hselect) + (height_alt * (1.0 - hselect));
397 }
398
399
400 float MapgenV7::baseTerrainLevelFromMap(int index)
401 {
402         float hselect     = rangelim(noise_height_select->result[index], 0.0, 1.0);
403         float height_base = noise_terrain_base->result[index];
404         float height_alt  = noise_terrain_alt->result[index];
405
406         if (height_alt > height_base)
407                 return height_alt;
408
409         return (height_base * hselect) + (height_alt * (1.0 - hselect));
410 }
411
412
413 bool MapgenV7::getMountainTerrainAtPoint(s16 x, s16 y, s16 z)
414 {
415         float mnt_h_n = NoisePerlin2D(&noise_mount_height->np, x, z, seed);
416         float density_gradient = -((float)y / mnt_h_n);
417         float mnt_n = NoisePerlin3D(&noise_mountain->np, x, y, z, seed);
418
419         return mnt_n + density_gradient >= 0.0;
420 }
421
422
423 bool MapgenV7::getMountainTerrainFromMap(int idx_xyz, int idx_xz, s16 y)
424 {
425         float mounthn = noise_mount_height->result[idx_xz];
426         float density_gradient = -((float)y / mounthn);
427         float mountn = noise_mountain->result[idx_xyz];
428
429         return mountn + density_gradient >= 0.0;
430 }
431
432
433 int MapgenV7::generateTerrain()
434 {
435         MapNode n_air(CONTENT_AIR);
436         MapNode n_stone(c_stone);
437         MapNode n_water(c_water_source);
438
439         v3s16 em = vm->m_area.getExtent();
440         s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
441         u32 index2d = 0;
442         bool mountain_flag = spflags & MGV7_MOUNTAINS;
443
444         for (s16 z = node_min.Z; z <= node_max.Z; z++)
445         for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
446                 s16 surface_y = baseTerrainLevelFromMap(index2d);
447                 heightmap[index2d]       = surface_y;  // Create base terrain heightmap
448                 ridge_heightmap[index2d] = surface_y;
449
450                 if (surface_y > stone_surface_max_y)
451                         stone_surface_max_y = surface_y;
452
453                 u32 vi = vm->m_area.index(x, node_min.Y - 1, z);
454                 u32 index3d = (z - node_min.Z) * zstride_1u1d + (x - node_min.X);
455
456                 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
457                         if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
458                                 if (y <= surface_y) {
459                                         vm->m_data[vi] = n_stone;  // Base terrain
460                                 } else if (mountain_flag &&
461                                                 getMountainTerrainFromMap(index3d, index2d, y)) {
462                                         vm->m_data[vi] = n_stone;  // Mountain terrain
463                                         if (y > stone_surface_max_y)
464                                                 stone_surface_max_y = y;
465                                 } else if (y <= water_level) {
466                                         vm->m_data[vi] = n_water;
467                                 } else {
468                                         vm->m_data[vi] = n_air;
469                                 }
470                         }
471                         vm->m_area.add_y(em, vi, 1);
472                         index3d += ystride;
473                 }
474         }
475
476         return stone_surface_max_y;
477 }
478
479
480 void MapgenV7::generateRidgeTerrain()
481 {
482         if (node_max.Y < water_level - 16)
483                 return;
484
485         MapNode n_water(c_water_source);
486         MapNode n_air(CONTENT_AIR);
487         u32 index = 0;
488         float width = 0.2;
489
490         for (s16 z = node_min.Z; z <= node_max.Z; z++)
491         for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
492                 u32 vi = vm->m_area.index(node_min.X, y, z);
493                 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
494                         int j = (z - node_min.Z) * csize.X + (x - node_min.X);
495
496                         if (heightmap[j] < water_level - 16)  // Use base terrain heightmap
497                                 continue;
498
499                         float uwatern = noise_ridge_uwater->result[j] * 2;
500                         if (fabs(uwatern) > width)
501                                 continue;
502
503                         float altitude = y - water_level;
504                         float height_mod = (altitude + 17) / 2.5;
505                         float width_mod  = width - fabs(uwatern);
506                         float nridge = noise_ridge->result[index] * MYMAX(altitude, 0) / 7.0;
507
508                         if (nridge + width_mod * height_mod < 0.6)
509                                 continue;
510
511                         if (y < ridge_heightmap[j])
512                                 ridge_heightmap[j] = y - 1;
513
514                         vm->m_data[vi] = (y > water_level) ? n_air : n_water;
515                 }
516         }
517 }
518
519
520 ////////////////////////////////////////////////////////////////////////////////
521 //// Code Boneyard
522 ////
523 //// Much of the stuff here has potential to become useful again at some point
524 //// in the future, but we don't want it to get lost or forgotten in version
525 //// control.
526 ////
527
528 #if 0
529 int MapgenV7::generateMountainTerrain(s16 ymax)
530 {
531         MapNode n_stone(c_stone);
532         u32 j = 0;
533
534         for (s16 z = node_min.Z; z <= node_max.Z; z++)
535         for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
536                 u32 vi = vm->m_area.index(node_min.X, y, z);
537                 for (s16 x = node_min.X; x <= node_max.X; x++) {
538                         int index = (z - node_min.Z) * csize.X + (x - node_min.X);
539                         content_t c = vm->m_data[vi].getContent();
540
541                         if (getMountainTerrainFromMap(j, index, y)
542                                         && (c == CONTENT_AIR || c == c_water_source)) {
543                                 vm->m_data[vi] = n_stone;
544                                 if (y > ymax)
545                                         ymax = y;
546                         }
547
548                         vi++;
549                         j++;
550                 }
551         }
552
553         return ymax;
554 }
555 #endif
556
557
558 #if 0
559 void MapgenV7::carveRivers() {
560         MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
561         MapNode n_stone(c_stone);
562         u32 index = 0;
563
564         int river_depth = 4;
565
566         for (s16 z = node_min.Z; z <= node_max.Z; z++)
567         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
568                 float terrain_mod  = noise_terrain_mod->result[index];
569                 NoiseParams *np = noise_terrain_river->np;
570                 np.persist = noise_terrain_persist->result[index];
571                 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
572                 float height = terrain_river * (1 - abs(terrain_mod)) *
573                                                 noise_terrain_river->np.scale;
574                 height = log(height * height); //log(h^3) is pretty interesting for terrain
575
576                 s16 y = heightmap[index];
577                 if (height < 1.0 && y > river_depth &&
578                         y - river_depth >= node_min.Y && y <= node_max.Y) {
579
580                         for (s16 ry = y; ry != y - river_depth; ry--) {
581                                 u32 vi = vm->m_area.index(x, ry, z);
582                                 vm->m_data[vi] = n_air;
583                         }
584
585                         u32 vi = vm->m_area.index(x, y - river_depth, z);
586                         vm->m_data[vi] = n_water_source;
587                 }
588         }
589 }
590 #endif
591
592
593 #if 0
594 void MapgenV7::addTopNodes()
595 {
596         v3s16 em = vm->m_area.getExtent();
597         s16 ntopnodes;
598         u32 index = 0;
599
600         for (s16 z = node_min.Z; z <= node_max.Z; z++)
601         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
602                 Biome *biome = bmgr->biomes[biomemap[index]];
603
604                 //////////////////// First, add top nodes below the ridge
605                 s16 y = ridge_heightmap[index];
606
607                 // This cutoff is good enough, but not perfect.
608                 // It will cut off potentially placed top nodes at chunk boundaries
609                 if (y < node_min.Y)
610                         continue;
611                 if (y > node_max.Y) {
612                         y = node_max.Y; // Let's see if we can still go downward anyway
613                         u32 vi = vm->m_area.index(x, y, z);
614                         content_t c = vm->m_data[vi].getContent();
615                         if (ndef->get(c).walkable)
616                                 continue;
617                 }
618
619                 // N.B.  It is necessary to search downward since ridge_heightmap[i]
620                 // might not be the actual height, just the lowest part in the chunk
621                 // where a ridge had been carved
622                 u32 i = vm->m_area.index(x, y, z);
623                 for (; y >= node_min.Y; y--) {
624                         content_t c = vm->m_data[i].getContent();
625                         if (ndef->get(c).walkable)
626                                 break;
627                         vm->m_area.add_y(em, i, -1);
628                 }
629
630                 if (y != node_min.Y - 1 && y >= water_level) {
631                         ridge_heightmap[index] = y; //update ridgeheight
632                         ntopnodes = biome->top_depth;
633                         for (; y <= node_max.Y && ntopnodes; y++) {
634                                 ntopnodes--;
635                                 vm->m_data[i] = MapNode(biome->c_top);
636                                 vm->m_area.add_y(em, i, 1);
637                         }
638                         // If dirt, grow grass on it.
639                         if (y > water_level - 10 &&
640                                 vm->m_data[i].getContent() == CONTENT_AIR) {
641                                 vm->m_area.add_y(em, i, -1);
642                                 if (vm->m_data[i].getContent() == c_dirt)
643                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
644                         }
645                 }
646
647                 //////////////////// Now, add top nodes on top of the ridge
648                 y = heightmap[index];
649                 if (y > node_max.Y) {
650                         y = node_max.Y; // Let's see if we can still go downward anyway
651                         u32 vi = vm->m_area.index(x, y, z);
652                         content_t c = vm->m_data[vi].getContent();
653                         if (ndef->get(c).walkable)
654                                 continue;
655                 }
656
657                 i = vm->m_area.index(x, y, z);
658                 for (; y >= node_min.Y; y--) {
659                         content_t c = vm->m_data[i].getContent();
660                         if (ndef->get(c).walkable)
661                                 break;
662                         vm->m_area.add_y(em, i, -1);
663                 }
664
665                 if (y != node_min.Y - 1) {
666                         ntopnodes = biome->top_depth;
667                         // Let's see if we've already added it...
668                         if (y == ridge_heightmap[index] + ntopnodes - 1)
669                                 continue;
670
671                         for (; y <= node_max.Y && ntopnodes; y++) {
672                                 ntopnodes--;
673                                 vm->m_data[i] = MapNode(biome->c_top);
674                                 vm->m_area.add_y(em, i, 1);
675                         }
676                         // If dirt, grow grass on it.
677                         if (y > water_level - 10 &&
678                                 vm->m_data[i].getContent() == CONTENT_AIR) {
679                                 vm->m_area.add_y(em, i, -1);
680                                 if (vm->m_data[i].getContent() == c_dirt)
681                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
682                         }
683                 }
684         }
685 }
686 #endif