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