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