Performance fix + SAO factorization
[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.09;
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         // Make rim 2 nodes thick to match floatland base terrain
350         float density_gradient = (y >= floatland_level) ?
351                 -pow((float)(y - floatland_level) / float_mount_height, 0.75f) :
352                 -pow((float)(floatland_level - 1 - y) / float_mount_height, 0.75f);
353
354         float floatn = noise_mountain->result[idx_xyz] + float_mount_density;
355
356         return floatn + density_gradient >= 0.0f;
357 }
358
359
360 void MapgenV7::floatBaseExtentFromMap(s16 *float_base_min, s16 *float_base_max, int idx_xz)
361 {
362         // '+1' to avoid a layer of stone at y = MAX_MAP_GENERATION_LIMIT
363         s16 base_min = MAX_MAP_GENERATION_LIMIT + 1;
364         s16 base_max = MAX_MAP_GENERATION_LIMIT;
365
366         float n_base = noise_floatland_base->result[idx_xz];
367         if (n_base > 0.0f) {
368                 float n_base_height = noise_float_base_height->result[idx_xz];
369                 float amp = n_base * n_base_height;
370                 float ridge = n_base_height / 3.0f;
371                 base_min = floatland_level - amp / 1.5f;
372
373                 if (amp > ridge * 2.0f) {
374                         // Lake bed
375                         base_max = floatland_level - (amp - ridge * 2.0f) / 2.0f;
376                 } else {
377                         // Hills and ridges
378                         float diff = fabs(amp - ridge) / ridge;
379                         // Smooth ridges using the 'smoothstep function'
380                         float smooth_diff = diff * diff * (3.0f - 2.0f * diff);
381                         base_max = floatland_level + ridge - smooth_diff * ridge;
382                 }
383         }
384
385         *float_base_min = base_min;
386         *float_base_max = base_max;
387 }
388
389
390 int MapgenV7::generateTerrain()
391 {
392         MapNode n_air(CONTENT_AIR);
393         MapNode n_stone(c_stone);
394         MapNode n_water(c_water_source);
395
396         //// Calculate noise for terrain generation
397         noise_terrain_persist->perlinMap2D(node_min.X, node_min.Z);
398         float *persistmap = noise_terrain_persist->result;
399
400         noise_terrain_base->perlinMap2D(node_min.X, node_min.Z, persistmap);
401         noise_terrain_alt->perlinMap2D(node_min.X, node_min.Z, persistmap);
402         noise_height_select->perlinMap2D(node_min.X, node_min.Z);
403
404         if ((spflags & MGV7_MOUNTAINS) || (spflags & MGV7_FLOATLANDS)) {
405                 noise_mountain->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
406         }
407
408         if (spflags & MGV7_MOUNTAINS) {
409                 noise_mount_height->perlinMap2D(node_min.X, node_min.Z);
410         }
411
412         if (spflags & MGV7_FLOATLANDS) {
413                 noise_floatland_base->perlinMap2D(node_min.X, node_min.Z);
414                 noise_float_base_height->perlinMap2D(node_min.X, node_min.Z);
415         }
416
417         //// Place nodes
418         v3s16 em = vm->m_area.getExtent();
419         s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
420         u32 index2d = 0;
421
422         for (s16 z = node_min.Z; z <= node_max.Z; z++)
423         for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
424                 s16 surface_y = baseTerrainLevelFromMap(index2d);
425                 if (surface_y > stone_surface_max_y)
426                         stone_surface_max_y = surface_y;
427
428                 // Get extent of floatland base terrain
429                 // '+1' to avoid a layer of stone at y = MAX_MAP_GENERATION_LIMIT
430                 s16 float_base_min = MAX_MAP_GENERATION_LIMIT + 1;
431                 s16 float_base_max = MAX_MAP_GENERATION_LIMIT;
432                 if (spflags & MGV7_FLOATLANDS)
433                         floatBaseExtentFromMap(&float_base_min, &float_base_max, index2d);
434
435                 u32 vi = vm->m_area.index(x, node_min.Y - 1, z);
436                 u32 index3d = (z - node_min.Z) * zstride_1u1d + (x - node_min.X);
437
438                 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
439                         if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
440                                 if (y <= surface_y) {
441                                         vm->m_data[vi] = n_stone;  // Base terrain
442                                 } else if ((spflags & MGV7_MOUNTAINS) &&
443                                                 getMountainTerrainFromMap(index3d, index2d, y)) {
444                                         vm->m_data[vi] = n_stone;  // Mountain terrain
445                                         if (y > stone_surface_max_y)
446                                                 stone_surface_max_y = y;
447                                 } else if ((spflags & MGV7_FLOATLANDS) &&
448                                                 ((y >= float_base_min && y <= float_base_max) ||
449                                                 getFloatlandMountainFromMap(index3d, index2d, y))) {
450                                         vm->m_data[vi] = n_stone;  // Floatland terrain
451                                         stone_surface_max_y = node_max.Y;
452                                 } else if (y <= water_level) {
453                                         vm->m_data[vi] = n_water;  // Ground level water
454                                 } else if ((spflags & MGV7_FLOATLANDS) &&
455                                                 (y >= float_base_max && y <= floatland_level)) {
456                                         vm->m_data[vi] = n_water;  // Floatland water
457                                 } else {
458                                         vm->m_data[vi] = n_air;
459                                 }
460                         }
461                         vm->m_area.add_y(em, vi, 1);
462                         index3d += ystride;
463                 }
464         }
465
466         return stone_surface_max_y;
467 }
468
469
470 void MapgenV7::generateRidgeTerrain()
471 {
472         if ((node_max.Y < water_level - 16) || (node_max.Y > shadow_limit))
473                 return;
474
475         noise_ridge->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
476         noise_ridge_uwater->perlinMap2D(node_min.X, node_min.Z);
477
478         MapNode n_water(c_water_source);
479         MapNode n_air(CONTENT_AIR);
480         u32 index = 0;
481         float width = 0.2;
482
483         for (s16 z = node_min.Z; z <= node_max.Z; z++)
484         for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
485                 u32 vi = vm->m_area.index(node_min.X, y, z);
486                 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
487                         int j = (z - node_min.Z) * csize.X + (x - node_min.X);
488
489                         float uwatern = noise_ridge_uwater->result[j] * 2;
490                         if (fabs(uwatern) > width)
491                                 continue;
492
493                         float altitude = y - water_level;
494                         float height_mod = (altitude + 17) / 2.5;
495                         float width_mod  = width - fabs(uwatern);
496                         float nridge = noise_ridge->result[index] * MYMAX(altitude, 0) / 7.0;
497
498                         if (nridge + width_mod * height_mod < 0.6)
499                                 continue;
500
501                         vm->m_data[vi] = (y > water_level) ? n_air : n_water;
502                 }
503         }
504 }
505
506
507 ////////////////////////////////////////////////////////////////////////////////
508 //// Code Boneyard
509 ////
510 //// Much of the stuff here has potential to become useful again at some point
511 //// in the future, but we don't want it to get lost or forgotten in version
512 //// control.
513 ////
514
515 #if 0
516 int MapgenV7::generateMountainTerrain(s16 ymax)
517 {
518         MapNode n_stone(c_stone);
519         u32 j = 0;
520
521         for (s16 z = node_min.Z; z <= node_max.Z; z++)
522         for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
523                 u32 vi = vm->m_area.index(node_min.X, y, z);
524                 for (s16 x = node_min.X; x <= node_max.X; x++) {
525                         int index = (z - node_min.Z) * csize.X + (x - node_min.X);
526                         content_t c = vm->m_data[vi].getContent();
527
528                         if (getMountainTerrainFromMap(j, index, y)
529                                         && (c == CONTENT_AIR || c == c_water_source)) {
530                                 vm->m_data[vi] = n_stone;
531                                 if (y > ymax)
532                                         ymax = y;
533                         }
534
535                         vi++;
536                         j++;
537                 }
538         }
539
540         return ymax;
541 }
542 #endif
543
544
545 #if 0
546 void MapgenV7::carveRivers() {
547         MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
548         MapNode n_stone(c_stone);
549         u32 index = 0;
550
551         int river_depth = 4;
552
553         for (s16 z = node_min.Z; z <= node_max.Z; z++)
554         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
555                 float terrain_mod  = noise_terrain_mod->result[index];
556                 NoiseParams *np = noise_terrain_river->np;
557                 np.persist = noise_terrain_persist->result[index];
558                 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
559                 float height = terrain_river * (1 - abs(terrain_mod)) *
560                                                 noise_terrain_river->np.scale;
561                 height = log(height * height); //log(h^3) is pretty interesting for terrain
562
563                 s16 y = heightmap[index];
564                 if (height < 1.0 && y > river_depth &&
565                         y - river_depth >= node_min.Y && y <= node_max.Y) {
566
567                         for (s16 ry = y; ry != y - river_depth; ry--) {
568                                 u32 vi = vm->m_area.index(x, ry, z);
569                                 vm->m_data[vi] = n_air;
570                         }
571
572                         u32 vi = vm->m_area.index(x, y - river_depth, z);
573                         vm->m_data[vi] = n_water_source;
574                 }
575         }
576 }
577 #endif
578
579
580 #if 0
581 void MapgenV7::addTopNodes()
582 {
583         v3s16 em = vm->m_area.getExtent();
584         s16 ntopnodes;
585         u32 index = 0;
586
587         for (s16 z = node_min.Z; z <= node_max.Z; z++)
588         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
589                 Biome *biome = bmgr->biomes[biomemap[index]];
590
591                 //////////////////// First, add top nodes below the ridge
592                 s16 y = ridge_heightmap[index];
593
594                 // This cutoff is good enough, but not perfect.
595                 // It will cut off potentially placed top nodes at chunk boundaries
596                 if (y < node_min.Y)
597                         continue;
598                 if (y > node_max.Y) {
599                         y = node_max.Y; // Let's see if we can still go downward anyway
600                         u32 vi = vm->m_area.index(x, y, z);
601                         content_t c = vm->m_data[vi].getContent();
602                         if (ndef->get(c).walkable)
603                                 continue;
604                 }
605
606                 // N.B.  It is necessary to search downward since ridge_heightmap[i]
607                 // might not be the actual height, just the lowest part in the chunk
608                 // where a ridge had been carved
609                 u32 i = vm->m_area.index(x, y, z);
610                 for (; y >= node_min.Y; y--) {
611                         content_t c = vm->m_data[i].getContent();
612                         if (ndef->get(c).walkable)
613                                 break;
614                         vm->m_area.add_y(em, i, -1);
615                 }
616
617                 if (y != node_min.Y - 1 && y >= water_level) {
618                         ridge_heightmap[index] = y; //update ridgeheight
619                         ntopnodes = biome->top_depth;
620                         for (; y <= node_max.Y && ntopnodes; y++) {
621                                 ntopnodes--;
622                                 vm->m_data[i] = MapNode(biome->c_top);
623                                 vm->m_area.add_y(em, i, 1);
624                         }
625                         // If dirt, grow grass on it.
626                         if (y > water_level - 10 &&
627                                 vm->m_data[i].getContent() == CONTENT_AIR) {
628                                 vm->m_area.add_y(em, i, -1);
629                                 if (vm->m_data[i].getContent() == c_dirt)
630                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
631                         }
632                 }
633
634                 //////////////////// Now, add top nodes on top of the ridge
635                 y = heightmap[index];
636                 if (y > node_max.Y) {
637                         y = node_max.Y; // Let's see if we can still go downward anyway
638                         u32 vi = vm->m_area.index(x, y, z);
639                         content_t c = vm->m_data[vi].getContent();
640                         if (ndef->get(c).walkable)
641                                 continue;
642                 }
643
644                 i = vm->m_area.index(x, y, z);
645                 for (; y >= node_min.Y; y--) {
646                         content_t c = vm->m_data[i].getContent();
647                         if (ndef->get(c).walkable)
648                                 break;
649                         vm->m_area.add_y(em, i, -1);
650                 }
651
652                 if (y != node_min.Y - 1) {
653                         ntopnodes = biome->top_depth;
654                         // Let's see if we've already added it...
655                         if (y == ridge_heightmap[index] + ntopnodes - 1)
656                                 continue;
657
658                         for (; y <= node_max.Y && ntopnodes; y++) {
659                                 ntopnodes--;
660                                 vm->m_data[i] = MapNode(biome->c_top);
661                                 vm->m_area.add_y(em, i, 1);
662                         }
663                         // If dirt, grow grass on it.
664                         if (y > water_level - 10 &&
665                                 vm->m_data[i].getContent() == CONTENT_AIR) {
666                                 vm->m_area.add_y(em, i, -1);
667                                 if (vm->m_data[i].getContent() == c_dirt)
668                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
669                         }
670                 }
671         }
672 }
673 #endif