]> git.lizzy.rs Git - minetest.git/blob - src/mapgen/mapgen_carpathian.cpp
GUIHyperText: Fix bug with UTF8 chars in action name + simplify UTF8 stringw conversi...
[minetest.git] / src / mapgen / mapgen_carpathian.cpp
1 /*
2 Minetest
3 Copyright (C) 2017-2019 vlapsley, Vaughan Lapsley <vlapsley@gmail.com>
4 Copyright (C) 2017-2019 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 <cmath>
23 #include "mapgen.h"
24 #include "voxel.h"
25 #include "noise.h"
26 #include "mapblock.h"
27 #include "mapnode.h"
28 #include "map.h"
29 #include "content_sao.h"
30 #include "nodedef.h"
31 #include "voxelalgorithms.h"
32 //#include "profiler.h" // For TimeTaker
33 #include "settings.h" // For g_settings
34 #include "emerge.h"
35 #include "dungeongen.h"
36 #include "cavegen.h"
37 #include "mg_biome.h"
38 #include "mg_ore.h"
39 #include "mg_decoration.h"
40 #include "mapgen_carpathian.h"
41
42
43 FlagDesc flagdesc_mapgen_carpathian[] = {
44         {"caverns", MGCARPATHIAN_CAVERNS},
45         {"rivers",  MGCARPATHIAN_RIVERS},
46         {NULL,      0}
47 };
48
49
50 ///////////////////////////////////////////////////////////////////////////////
51
52
53 MapgenCarpathian::MapgenCarpathian(MapgenCarpathianParams *params, EmergeManager *emerge)
54         : MapgenBasic(MAPGEN_CARPATHIAN, params, emerge)
55 {
56         base_level       = params->base_level;
57         river_width      = params->river_width;
58         river_depth      = params->river_depth;
59         valley_width     = params->valley_width;
60
61         spflags            = params->spflags;
62         cave_width         = params->cave_width;
63         large_cave_depth   = params->large_cave_depth;
64         small_cave_num_min = params->small_cave_num_min;
65         small_cave_num_max = params->small_cave_num_max;
66         large_cave_num_min = params->large_cave_num_min;
67         large_cave_num_max = params->large_cave_num_max;
68         large_cave_flooded = params->large_cave_flooded;
69         cavern_limit       = params->cavern_limit;
70         cavern_taper       = params->cavern_taper;
71         cavern_threshold   = params->cavern_threshold;
72         dungeon_ymin       = params->dungeon_ymin;
73         dungeon_ymax       = params->dungeon_ymax;
74
75         grad_wl = 1 - water_level;
76
77         //// 2D Terrain noise
78         noise_filler_depth  = new Noise(&params->np_filler_depth,  seed, csize.X, csize.Z);
79         noise_height1       = new Noise(&params->np_height1,       seed, csize.X, csize.Z);
80         noise_height2       = new Noise(&params->np_height2,       seed, csize.X, csize.Z);
81         noise_height3       = new Noise(&params->np_height3,       seed, csize.X, csize.Z);
82         noise_height4       = new Noise(&params->np_height4,       seed, csize.X, csize.Z);
83         noise_hills_terrain = new Noise(&params->np_hills_terrain, seed, csize.X, csize.Z);
84         noise_ridge_terrain = new Noise(&params->np_ridge_terrain, seed, csize.X, csize.Z);
85         noise_step_terrain  = new Noise(&params->np_step_terrain,  seed, csize.X, csize.Z);
86         noise_hills         = new Noise(&params->np_hills,         seed, csize.X, csize.Z);
87         noise_ridge_mnt     = new Noise(&params->np_ridge_mnt,     seed, csize.X, csize.Z);
88         noise_step_mnt      = new Noise(&params->np_step_mnt,      seed, csize.X, csize.Z);
89         if (spflags & MGCARPATHIAN_RIVERS)
90                 noise_rivers    = new Noise(&params->np_rivers,        seed, csize.X, csize.Z);
91
92         //// 3D terrain noise
93         // 1 up 1 down overgeneration
94         noise_mnt_var = new Noise(&params->np_mnt_var, seed, csize.X, csize.Y + 2, csize.Z);
95
96         //// Cave noise
97         MapgenBasic::np_cave1  = params->np_cave1;
98         MapgenBasic::np_cave2  = params->np_cave2;
99         MapgenBasic::np_cavern = params->np_cavern;
100         MapgenBasic::np_dungeons = params->np_dungeons;
101 }
102
103
104 MapgenCarpathian::~MapgenCarpathian()
105 {
106         delete noise_filler_depth;
107         delete noise_height1;
108         delete noise_height2;
109         delete noise_height3;
110         delete noise_height4;
111         delete noise_hills_terrain;
112         delete noise_ridge_terrain;
113         delete noise_step_terrain;
114         delete noise_hills;
115         delete noise_ridge_mnt;
116         delete noise_step_mnt;
117         if (spflags & MGCARPATHIAN_RIVERS)
118                 delete noise_rivers;
119
120         delete noise_mnt_var;
121 }
122
123
124 MapgenCarpathianParams::MapgenCarpathianParams():
125         np_filler_depth  (0,   1,   v3f(128,  128,  128),  261,   3, 0.7,  2.0),
126         np_height1       (0,   5,   v3f(251,  251,  251),  9613,  5, 0.5,  2.0),
127         np_height2       (0,   5,   v3f(383,  383,  383),  1949,  5, 0.5,  2.0),
128         np_height3       (0,   5,   v3f(509,  509,  509),  3211,  5, 0.5,  2.0),
129         np_height4       (0,   5,   v3f(631,  631,  631),  1583,  5, 0.5,  2.0),
130         np_hills_terrain (1,   1,   v3f(1301, 1301, 1301), 1692,  5, 0.5,  2.0),
131         np_ridge_terrain (1,   1,   v3f(1889, 1889, 1889), 3568,  5, 0.5,  2.0),
132         np_step_terrain  (1,   1,   v3f(1889, 1889, 1889), 4157,  5, 0.5,  2.0),
133         np_hills         (0,   3,   v3f(257,  257,  257),  6604,  6, 0.5,  2.0),
134         np_ridge_mnt     (0,   12,  v3f(743,  743,  743),  5520,  6, 0.7,  2.0),
135         np_step_mnt      (0,   8,   v3f(509,  509,  509),  2590,  6, 0.6,  2.0),
136         np_rivers        (0,   1,   v3f(1000, 1000, 1000), 85039, 5, 0.6,  2.0),
137         np_mnt_var       (0,   1,   v3f(499,  499,  499),  2490,  5, 0.55, 2.0),
138         np_cave1         (0,   12,  v3f(61,   61,   61),   52534, 3, 0.5,  2.0),
139         np_cave2         (0,   12,  v3f(67,   67,   67),   10325, 3, 0.5,  2.0),
140         np_cavern        (0,   1,   v3f(384,  128,  384),  723,   5, 0.63, 2.0),
141         np_dungeons      (0.9, 0.5, v3f(500,  500,  500),  0,     2, 0.8,  2.0)
142 {
143 }
144
145
146 void MapgenCarpathianParams::readParams(const Settings *settings)
147 {
148         settings->getFlagStrNoEx("mgcarpathian_spflags", spflags, flagdesc_mapgen_carpathian);
149
150         settings->getFloatNoEx("mgcarpathian_base_level",   base_level);
151         settings->getFloatNoEx("mgcarpathian_river_width",  river_width);
152         settings->getFloatNoEx("mgcarpathian_river_depth",  river_depth);
153         settings->getFloatNoEx("mgcarpathian_valley_width", valley_width);
154
155         settings->getFloatNoEx("mgcarpathian_cave_width",         cave_width);
156         settings->getS16NoEx("mgcarpathian_large_cave_depth",     large_cave_depth);
157         settings->getU16NoEx("mgcarpathian_small_cave_num_min",   small_cave_num_min);
158         settings->getU16NoEx("mgcarpathian_small_cave_num_max",   small_cave_num_max);
159         settings->getU16NoEx("mgcarpathian_large_cave_num_min",   large_cave_num_min);
160         settings->getU16NoEx("mgcarpathian_large_cave_num_max",   large_cave_num_max);
161         settings->getFloatNoEx("mgcarpathian_large_cave_flooded", large_cave_flooded);
162         settings->getS16NoEx("mgcarpathian_cavern_limit",         cavern_limit);
163         settings->getS16NoEx("mgcarpathian_cavern_taper",         cavern_taper);
164         settings->getFloatNoEx("mgcarpathian_cavern_threshold",   cavern_threshold);
165         settings->getS16NoEx("mgcarpathian_dungeon_ymin",         dungeon_ymin);
166         settings->getS16NoEx("mgcarpathian_dungeon_ymax",         dungeon_ymax);
167
168         settings->getNoiseParams("mgcarpathian_np_filler_depth",  np_filler_depth);
169         settings->getNoiseParams("mgcarpathian_np_height1",       np_height1);
170         settings->getNoiseParams("mgcarpathian_np_height2",       np_height2);
171         settings->getNoiseParams("mgcarpathian_np_height3",       np_height3);
172         settings->getNoiseParams("mgcarpathian_np_height4",       np_height4);
173         settings->getNoiseParams("mgcarpathian_np_hills_terrain", np_hills_terrain);
174         settings->getNoiseParams("mgcarpathian_np_ridge_terrain", np_ridge_terrain);
175         settings->getNoiseParams("mgcarpathian_np_step_terrain",  np_step_terrain);
176         settings->getNoiseParams("mgcarpathian_np_hills",         np_hills);
177         settings->getNoiseParams("mgcarpathian_np_ridge_mnt",     np_ridge_mnt);
178         settings->getNoiseParams("mgcarpathian_np_step_mnt",      np_step_mnt);
179         settings->getNoiseParams("mgcarpathian_np_rivers",        np_rivers);
180         settings->getNoiseParams("mgcarpathian_np_mnt_var",       np_mnt_var);
181         settings->getNoiseParams("mgcarpathian_np_cave1",         np_cave1);
182         settings->getNoiseParams("mgcarpathian_np_cave2",         np_cave2);
183         settings->getNoiseParams("mgcarpathian_np_cavern",        np_cavern);
184         settings->getNoiseParams("mgcarpathian_np_dungeons",      np_dungeons);
185 }
186
187
188 void MapgenCarpathianParams::writeParams(Settings *settings) const
189 {
190         settings->setFlagStr("mgcarpathian_spflags", spflags, flagdesc_mapgen_carpathian);
191
192         settings->setFloat("mgcarpathian_base_level",   base_level);
193         settings->setFloat("mgcarpathian_river_width",  river_width);
194         settings->setFloat("mgcarpathian_river_depth",  river_depth);
195         settings->setFloat("mgcarpathian_valley_width", valley_width);
196
197         settings->setFloat("mgcarpathian_cave_width",         cave_width);
198         settings->setS16("mgcarpathian_large_cave_depth",     large_cave_depth);
199         settings->setU16("mgcarpathian_small_cave_num_min",   small_cave_num_min);
200         settings->setU16("mgcarpathian_small_cave_num_max",   small_cave_num_max);
201         settings->setU16("mgcarpathian_large_cave_num_min",   large_cave_num_min);
202         settings->setU16("mgcarpathian_large_cave_num_max",   large_cave_num_max);
203         settings->setFloat("mgcarpathian_large_cave_flooded", large_cave_flooded);
204         settings->setS16("mgcarpathian_cavern_limit",         cavern_limit);
205         settings->setS16("mgcarpathian_cavern_taper",         cavern_taper);
206         settings->setFloat("mgcarpathian_cavern_threshold",   cavern_threshold);
207         settings->setS16("mgcarpathian_dungeon_ymin",         dungeon_ymin);
208         settings->setS16("mgcarpathian_dungeon_ymax",         dungeon_ymax);
209
210         settings->setNoiseParams("mgcarpathian_np_filler_depth",  np_filler_depth);
211         settings->setNoiseParams("mgcarpathian_np_height1",       np_height1);
212         settings->setNoiseParams("mgcarpathian_np_height2",       np_height2);
213         settings->setNoiseParams("mgcarpathian_np_height3",       np_height3);
214         settings->setNoiseParams("mgcarpathian_np_height4",       np_height4);
215         settings->setNoiseParams("mgcarpathian_np_hills_terrain", np_hills_terrain);
216         settings->setNoiseParams("mgcarpathian_np_ridge_terrain", np_ridge_terrain);
217         settings->setNoiseParams("mgcarpathian_np_step_terrain",  np_step_terrain);
218         settings->setNoiseParams("mgcarpathian_np_hills",         np_hills);
219         settings->setNoiseParams("mgcarpathian_np_ridge_mnt",     np_ridge_mnt);
220         settings->setNoiseParams("mgcarpathian_np_step_mnt",      np_step_mnt);
221         settings->setNoiseParams("mgcarpathian_np_rivers",        np_rivers);
222         settings->setNoiseParams("mgcarpathian_np_mnt_var",       np_mnt_var);
223         settings->setNoiseParams("mgcarpathian_np_cave1",         np_cave1);
224         settings->setNoiseParams("mgcarpathian_np_cave2",         np_cave2);
225         settings->setNoiseParams("mgcarpathian_np_cavern",        np_cavern);
226         settings->setNoiseParams("mgcarpathian_np_dungeons",      np_dungeons);
227 }
228
229
230 void MapgenCarpathianParams::setDefaultSettings(Settings *settings)
231 {
232         settings->setDefault("mgcarpathian_spflags", flagdesc_mapgen_carpathian,
233                 MGCARPATHIAN_CAVERNS);
234 }
235
236 ////////////////////////////////////////////////////////////////////////////////
237
238
239 // Lerp function
240 inline float MapgenCarpathian::getLerp(float noise1, float noise2, float mod)
241 {
242         return noise1 + mod * (noise2 - noise1);
243 }
244
245 // Steps function
246 float MapgenCarpathian::getSteps(float noise)
247 {
248         float w = 0.5f;
249         float k = std::floor(noise / w);
250         float f = (noise - k * w) / w;
251         float s = std::fmin(2.f * f, 1.f);
252         return (k + s) * w;
253 }
254
255
256 ////////////////////////////////////////////////////////////////////////////////
257
258
259 void MapgenCarpathian::makeChunk(BlockMakeData *data)
260 {
261         // Pre-conditions
262         assert(data->vmanip);
263         assert(data->nodedef);
264         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
265                         data->blockpos_requested.Y >= data->blockpos_min.Y &&
266                         data->blockpos_requested.Z >= data->blockpos_min.Z);
267         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
268                         data->blockpos_requested.Y <= data->blockpos_max.Y &&
269                         data->blockpos_requested.Z <= data->blockpos_max.Z);
270
271         this->generating = true;
272         this->vm = data->vmanip;
273         this->ndef = data->nodedef;
274
275         v3s16 blockpos_min = data->blockpos_min;
276         v3s16 blockpos_max = data->blockpos_max;
277         node_min = blockpos_min * MAP_BLOCKSIZE;
278         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
279         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
280         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
281
282         // Create a block-specific seed
283         blockseed = getBlockSeed2(full_node_min, seed);
284
285         // Generate terrain
286         s16 stone_surface_max_y = generateTerrain();
287
288         // Create heightmap
289         updateHeightmap(node_min, node_max);
290
291         // Init biome generator, place biome-specific nodes, and build biomemap
292         if (flags & MG_BIOMES) {
293                 biomegen->calcBiomeNoise(node_min);
294                 generateBiomes();
295         }
296
297         // Generate tunnels, caverns and large randomwalk caves
298         if (flags & MG_CAVES) {
299                 // Generate tunnels first as caverns confuse them
300                 generateCavesNoiseIntersection(stone_surface_max_y);
301
302                 // Generate caverns
303                 bool near_cavern = false;
304                 if (spflags & MGCARPATHIAN_CAVERNS)
305                         near_cavern = generateCavernsNoise(stone_surface_max_y);
306
307                 // Generate large randomwalk caves
308                 if (near_cavern)
309                         // Disable large randomwalk caves in this mapchunk by setting
310                         // 'large cave depth' to world base. Avoids excessive liquid in
311                         // large caverns and floating blobs of overgenerated liquid.
312                         generateCavesRandomWalk(stone_surface_max_y,
313                                 -MAX_MAP_GENERATION_LIMIT);
314                 else
315                         generateCavesRandomWalk(stone_surface_max_y, large_cave_depth);
316         }
317
318         // Generate the registered ores
319         m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
320
321         // Generate dungeons
322         if (flags & MG_DUNGEONS)
323                 generateDungeons(stone_surface_max_y);
324
325         // Generate the registered decorations
326         if (flags & MG_DECORATIONS)
327                 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
328
329         // Sprinkle some dust on top after everything else was generated
330         if (flags & MG_BIOMES)
331                 dustTopNodes();
332
333         // Update liquids
334         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
335
336         // Calculate lighting
337         if (flags & MG_LIGHT) {
338                 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
339                                 full_node_min, full_node_max);
340         }
341
342         this->generating = false;
343 }
344
345
346 ////////////////////////////////////////////////////////////////////////////////
347
348
349 int MapgenCarpathian::getSpawnLevelAtPoint(v2s16 p)
350 {
351         // If rivers are enabled, first check if in a river channel
352         if (spflags & MGCARPATHIAN_RIVERS) {
353                 float river = std::fabs(NoisePerlin2D(&noise_rivers->np, p.X, p.Y, seed)) -
354                         river_width;
355                 if (river < 0.0f)
356                         return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
357         }
358
359         float height1 = NoisePerlin2D(&noise_height1->np, p.X, p.Y, seed);
360         float height2 = NoisePerlin2D(&noise_height2->np, p.X, p.Y, seed);
361         float height3 = NoisePerlin2D(&noise_height3->np, p.X, p.Y, seed);
362         float height4 = NoisePerlin2D(&noise_height4->np, p.X, p.Y, seed);
363
364         float hterabs = std::fabs(NoisePerlin2D(&noise_hills_terrain->np, p.X, p.Y, seed));
365         float n_hills = NoisePerlin2D(&noise_hills->np, p.X, p.Y, seed);
366         float hill_mnt = hterabs * hterabs * hterabs * n_hills * n_hills;
367
368         float rterabs = std::fabs(NoisePerlin2D(&noise_ridge_terrain->np, p.X, p.Y, seed));
369         float n_ridge_mnt = NoisePerlin2D(&noise_ridge_mnt->np, p.X, p.Y, seed);
370         float ridge_mnt = rterabs * rterabs * rterabs * (1.0f - std::fabs(n_ridge_mnt));
371
372         float sterabs = std::fabs(NoisePerlin2D(&noise_step_terrain->np, p.X, p.Y, seed));
373         float n_step_mnt = NoisePerlin2D(&noise_step_mnt->np, p.X, p.Y, seed);
374         float step_mnt = sterabs * sterabs * sterabs * getSteps(n_step_mnt);
375
376         float valley = 1.0f;
377         float river = 0.0f;
378
379         if ((spflags & MGCARPATHIAN_RIVERS) && node_max.Y >= water_level - 16) {
380                 river = std::fabs(NoisePerlin2D(&noise_rivers->np, p.X, p.Y, seed)) - river_width;
381                 if (river <= valley_width) {
382                         // Within river valley
383                         if (river < 0.0f) {
384                                 // River channel
385                                 valley = river;
386                         } else {
387                                 // Valley slopes.
388                                 // 0 at river edge, 1 at valley edge.
389                                 float riversc = river / valley_width;
390                                 // Smoothstep
391                                 valley = riversc * riversc * (3.0f - 2.0f * riversc);
392                         }
393                 }
394         }
395
396         bool solid_below = false;
397         u8 cons_non_solid = 0; // consecutive non-solid nodes
398
399         for (s16 y = water_level; y <= water_level + 32; y++) {
400                 float mnt_var = NoisePerlin3D(&noise_mnt_var->np, p.X, y, p.Y, seed);
401                 float hill1 = getLerp(height1, height2, mnt_var);
402                 float hill2 = getLerp(height3, height4, mnt_var);
403                 float hill3 = getLerp(height3, height2, mnt_var);
404                 float hill4 = getLerp(height1, height4, mnt_var);
405
406                 float hilliness = std::fmax(std::fmin(hill1, hill2), std::fmin(hill3, hill4));
407                 float hills = hill_mnt * hilliness;
408                 float ridged_mountains = ridge_mnt * hilliness;
409                 float step_mountains = step_mnt * hilliness;
410
411                 s32 grad = 1 - y;
412
413                 float mountains = hills + ridged_mountains + step_mountains;
414                 float surface_level = base_level + mountains + grad;
415
416                 if ((spflags & MGCARPATHIAN_RIVERS) && river <= valley_width) {
417                         if (valley < 0.0f) {
418                                 // River channel
419                                 surface_level = std::fmin(surface_level,
420                                         water_level - std::sqrt(-valley) * river_depth);
421                         } else if (surface_level > water_level) {
422                                 // Valley slopes
423                                 surface_level = water_level + (surface_level - water_level) * valley;
424                         }
425                 }
426
427                 if (y < surface_level) { //TODO '<=' fix from generateTerrain()
428                         // solid node
429                         solid_below = true;
430                         cons_non_solid = 0;
431                 } else {
432                         // non-solid node
433                         cons_non_solid++;
434                         if (cons_non_solid == 3 && solid_below)
435                                 return y - 1;
436                 }
437         }
438
439         return MAX_MAP_GENERATION_LIMIT; // No suitable spawn point found
440 }
441
442
443 ////////////////////////////////////////////////////////////////////////////////
444
445
446 int MapgenCarpathian::generateTerrain()
447 {
448         MapNode mn_air(CONTENT_AIR);
449         MapNode mn_stone(c_stone);
450         MapNode mn_water(c_water_source);
451
452         // Calculate noise for terrain generation
453         noise_height1->perlinMap2D(node_min.X, node_min.Z);
454         noise_height2->perlinMap2D(node_min.X, node_min.Z);
455         noise_height3->perlinMap2D(node_min.X, node_min.Z);
456         noise_height4->perlinMap2D(node_min.X, node_min.Z);
457         noise_hills_terrain->perlinMap2D(node_min.X, node_min.Z);
458         noise_ridge_terrain->perlinMap2D(node_min.X, node_min.Z);
459         noise_step_terrain->perlinMap2D(node_min.X, node_min.Z);
460         noise_hills->perlinMap2D(node_min.X, node_min.Z);
461         noise_ridge_mnt->perlinMap2D(node_min.X, node_min.Z);
462         noise_step_mnt->perlinMap2D(node_min.X, node_min.Z);
463         noise_mnt_var->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
464
465         if (spflags & MGCARPATHIAN_RIVERS)
466                 noise_rivers->perlinMap2D(node_min.X, node_min.Z);
467
468         //// Place nodes
469         const v3s16 &em = vm->m_area.getExtent();
470         s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
471         u32 index2d = 0;
472
473         for (s16 z = node_min.Z; z <= node_max.Z; z++)
474         for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
475                 // Hill/Mountain height (hilliness)
476                 float height1 = noise_height1->result[index2d];
477                 float height2 = noise_height2->result[index2d];
478                 float height3 = noise_height3->result[index2d];
479                 float height4 = noise_height4->result[index2d];
480
481                 // Rolling hills
482                 float hterabs = std::fabs(noise_hills_terrain->result[index2d]);
483                 float n_hills = noise_hills->result[index2d];
484                 float hill_mnt = hterabs * hterabs * hterabs * n_hills * n_hills;
485
486                 // Ridged mountains
487                 float rterabs = std::fabs(noise_ridge_terrain->result[index2d]);
488                 float n_ridge_mnt = noise_ridge_mnt->result[index2d];
489                 float ridge_mnt = rterabs * rterabs * rterabs *
490                         (1.0f - std::fabs(n_ridge_mnt));
491
492                 // Step (terraced) mountains
493                 float sterabs = std::fabs(noise_step_terrain->result[index2d]);
494                 float n_step_mnt = noise_step_mnt->result[index2d];
495                 float step_mnt = sterabs * sterabs * sterabs * getSteps(n_step_mnt);
496
497                 // Rivers
498                 float valley = 1.0f;
499                 float river = 0.0f;
500
501                 if ((spflags & MGCARPATHIAN_RIVERS) && node_max.Y >= water_level - 16) {
502                         river = std::fabs(noise_rivers->result[index2d]) - river_width;
503                         if (river <= valley_width) {
504                                 // Within river valley
505                                 if (river < 0.0f) {
506                                         // River channel
507                                         valley = river;
508                                 } else {
509                                         // Valley slopes.
510                                         // 0 at river edge, 1 at valley edge.
511                                         float riversc = river / valley_width;
512                                         // Smoothstep
513                                         valley = riversc * riversc * (3.0f - 2.0f * riversc);
514                                 }
515                         }
516                 }
517
518                 // Initialise 3D noise index and voxelmanip index to column base
519                 u32 index3d = (z - node_min.Z) * zstride_1u1d + (x - node_min.X);
520                 u32 vi = vm->m_area.index(x, node_min.Y - 1, z);
521
522                 for (s16 y = node_min.Y - 1; y <= node_max.Y + 1;
523                                 y++,
524                                 index3d += ystride,
525                                 VoxelArea::add_y(em, vi, 1)) {
526                         if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
527                                 continue;
528
529                         // Combine height noises and apply 3D variation
530                         float mnt_var = noise_mnt_var->result[index3d];
531                         float hill1 = getLerp(height1, height2, mnt_var);
532                         float hill2 = getLerp(height3, height4, mnt_var);
533                         float hill3 = getLerp(height3, height2, mnt_var);
534                         float hill4 = getLerp(height1, height4, mnt_var);
535
536                         // 'hilliness' determines whether hills/mountains are
537                         // small or large
538                         float hilliness =
539                                 std::fmax(std::fmin(hill1, hill2), std::fmin(hill3, hill4));
540                         float hills = hill_mnt * hilliness;
541                         float ridged_mountains = ridge_mnt * hilliness;
542                         float step_mountains = step_mnt * hilliness;
543
544                         // Gradient & shallow seabed
545                         s32 grad = (y < water_level) ? grad_wl + (water_level - y) * 3 :
546                                 1 - y;
547
548                         // Final terrain level
549                         float mountains = hills + ridged_mountains + step_mountains;
550                         float surface_level = base_level + mountains + grad;
551
552                         // Rivers
553                         if ((spflags & MGCARPATHIAN_RIVERS) && node_max.Y >= water_level - 16 &&
554                                         river <= valley_width) {
555                                 if (valley < 0.0f) {
556                                         // River channel
557                                         surface_level = std::fmin(surface_level,
558                                                 water_level - std::sqrt(-valley) * river_depth);
559                                 } else if (surface_level > water_level) {
560                                         // Valley slopes
561                                         surface_level = water_level + (surface_level - water_level) * valley;
562                                 }
563                         }
564
565                         if (y < surface_level) { //TODO '<='
566                                 vm->m_data[vi] = mn_stone; // Stone
567                                 if (y > stone_surface_max_y)
568                                         stone_surface_max_y = y;
569                         } else if (y <= water_level) {
570                                 vm->m_data[vi] = mn_water; // Sea water
571                         } else {
572                                 vm->m_data[vi] = mn_air; // Air
573                         }
574                 }
575         }
576
577         return stone_surface_max_y;
578 }