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