]> git.lizzy.rs Git - minetest.git/blob - src/mapgen/mapgen_carpathian.cpp
Add 'ores' global mapgen flag (#10276)
[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 "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_carpathian.h"
40
41
42 FlagDesc flagdesc_mapgen_carpathian[] = {
43         {"caverns", MGCARPATHIAN_CAVERNS},
44         {"rivers",  MGCARPATHIAN_RIVERS},
45         {NULL,      0}
46 };
47
48
49 ///////////////////////////////////////////////////////////////////////////////
50
51
52 MapgenCarpathian::MapgenCarpathian(MapgenCarpathianParams *params, EmergeParams *emerge)
53         : MapgenBasic(MAPGEN_CARPATHIAN, params, emerge)
54 {
55         base_level       = params->base_level;
56         river_width      = params->river_width;
57         river_depth      = params->river_depth;
58         valley_width     = params->valley_width;
59
60         spflags            = params->spflags;
61         cave_width         = params->cave_width;
62         large_cave_depth   = params->large_cave_depth;
63         small_cave_num_min = params->small_cave_num_min;
64         small_cave_num_max = params->small_cave_num_max;
65         large_cave_num_min = params->large_cave_num_min;
66         large_cave_num_max = params->large_cave_num_max;
67         large_cave_flooded = params->large_cave_flooded;
68         cavern_limit       = params->cavern_limit;
69         cavern_taper       = params->cavern_taper;
70         cavern_threshold   = params->cavern_threshold;
71         dungeon_ymin       = params->dungeon_ymin;
72         dungeon_ymax       = params->dungeon_ymax;
73
74         grad_wl = 1 - water_level;
75
76         //// 2D Terrain noise
77         noise_filler_depth  = new Noise(&params->np_filler_depth,  seed, csize.X, csize.Z);
78         noise_height1       = new Noise(&params->np_height1,       seed, csize.X, csize.Z);
79         noise_height2       = new Noise(&params->np_height2,       seed, csize.X, csize.Z);
80         noise_height3       = new Noise(&params->np_height3,       seed, csize.X, csize.Z);
81         noise_height4       = new Noise(&params->np_height4,       seed, csize.X, csize.Z);
82         noise_hills_terrain = new Noise(&params->np_hills_terrain, seed, csize.X, csize.Z);
83         noise_ridge_terrain = new Noise(&params->np_ridge_terrain, seed, csize.X, csize.Z);
84         noise_step_terrain  = new Noise(&params->np_step_terrain,  seed, csize.X, csize.Z);
85         noise_hills         = new Noise(&params->np_hills,         seed, csize.X, csize.Z);
86         noise_ridge_mnt     = new Noise(&params->np_ridge_mnt,     seed, csize.X, csize.Z);
87         noise_step_mnt      = new Noise(&params->np_step_mnt,      seed, csize.X, csize.Z);
88         if (spflags & MGCARPATHIAN_RIVERS)
89                 noise_rivers    = new Noise(&params->np_rivers,        seed, csize.X, csize.Z);
90
91         //// 3D terrain noise
92         // 1 up 1 down overgeneration
93         noise_mnt_var = new Noise(&params->np_mnt_var, seed, csize.X, csize.Y + 2, csize.Z);
94
95         //// Cave noise
96         MapgenBasic::np_cave1  = params->np_cave1;
97         MapgenBasic::np_cave2  = params->np_cave2;
98         MapgenBasic::np_cavern = params->np_cavern;
99         MapgenBasic::np_dungeons = params->np_dungeons;
100 }
101
102
103 MapgenCarpathian::~MapgenCarpathian()
104 {
105         delete noise_filler_depth;
106         delete noise_height1;
107         delete noise_height2;
108         delete noise_height3;
109         delete noise_height4;
110         delete noise_hills_terrain;
111         delete noise_ridge_terrain;
112         delete noise_step_terrain;
113         delete noise_hills;
114         delete noise_ridge_mnt;
115         delete noise_step_mnt;
116         if (spflags & MGCARPATHIAN_RIVERS)
117                 delete noise_rivers;
118
119         delete noise_mnt_var;
120 }
121
122
123 MapgenCarpathianParams::MapgenCarpathianParams():
124         np_filler_depth  (0,   1,   v3f(128,  128,  128),  261,   3, 0.7,  2.0),
125         np_height1       (0,   5,   v3f(251,  251,  251),  9613,  5, 0.5,  2.0),
126         np_height2       (0,   5,   v3f(383,  383,  383),  1949,  5, 0.5,  2.0),
127         np_height3       (0,   5,   v3f(509,  509,  509),  3211,  5, 0.5,  2.0),
128         np_height4       (0,   5,   v3f(631,  631,  631),  1583,  5, 0.5,  2.0),
129         np_hills_terrain (1,   1,   v3f(1301, 1301, 1301), 1692,  5, 0.5,  2.0),
130         np_ridge_terrain (1,   1,   v3f(1889, 1889, 1889), 3568,  5, 0.5,  2.0),
131         np_step_terrain  (1,   1,   v3f(1889, 1889, 1889), 4157,  5, 0.5,  2.0),
132         np_hills         (0,   3,   v3f(257,  257,  257),  6604,  6, 0.5,  2.0),
133         np_ridge_mnt     (0,   12,  v3f(743,  743,  743),  5520,  6, 0.7,  2.0),
134         np_step_mnt      (0,   8,   v3f(509,  509,  509),  2590,  6, 0.6,  2.0),
135         np_rivers        (0,   1,   v3f(1000, 1000, 1000), 85039, 5, 0.6,  2.0),
136         np_mnt_var       (0,   1,   v3f(499,  499,  499),  2490,  5, 0.55, 2.0),
137         np_cave1         (0,   12,  v3f(61,   61,   61),   52534, 3, 0.5,  2.0),
138         np_cave2         (0,   12,  v3f(67,   67,   67),   10325, 3, 0.5,  2.0),
139         np_cavern        (0,   1,   v3f(384,  128,  384),  723,   5, 0.63, 2.0),
140         np_dungeons      (0.9, 0.5, v3f(500,  500,  500),  0,     2, 0.8,  2.0)
141 {
142 }
143
144
145 void MapgenCarpathianParams::readParams(const Settings *settings)
146 {
147         settings->getFlagStrNoEx("mgcarpathian_spflags", spflags, flagdesc_mapgen_carpathian);
148
149         settings->getFloatNoEx("mgcarpathian_base_level",   base_level);
150         settings->getFloatNoEx("mgcarpathian_river_width",  river_width);
151         settings->getFloatNoEx("mgcarpathian_river_depth",  river_depth);
152         settings->getFloatNoEx("mgcarpathian_valley_width", valley_width);
153
154         settings->getFloatNoEx("mgcarpathian_cave_width",         cave_width);
155         settings->getS16NoEx("mgcarpathian_large_cave_depth",     large_cave_depth);
156         settings->getU16NoEx("mgcarpathian_small_cave_num_min",   small_cave_num_min);
157         settings->getU16NoEx("mgcarpathian_small_cave_num_max",   small_cave_num_max);
158         settings->getU16NoEx("mgcarpathian_large_cave_num_min",   large_cave_num_min);
159         settings->getU16NoEx("mgcarpathian_large_cave_num_max",   large_cave_num_max);
160         settings->getFloatNoEx("mgcarpathian_large_cave_flooded", large_cave_flooded);
161         settings->getS16NoEx("mgcarpathian_cavern_limit",         cavern_limit);
162         settings->getS16NoEx("mgcarpathian_cavern_taper",         cavern_taper);
163         settings->getFloatNoEx("mgcarpathian_cavern_threshold",   cavern_threshold);
164         settings->getS16NoEx("mgcarpathian_dungeon_ymin",         dungeon_ymin);
165         settings->getS16NoEx("mgcarpathian_dungeon_ymax",         dungeon_ymax);
166
167         settings->getNoiseParams("mgcarpathian_np_filler_depth",  np_filler_depth);
168         settings->getNoiseParams("mgcarpathian_np_height1",       np_height1);
169         settings->getNoiseParams("mgcarpathian_np_height2",       np_height2);
170         settings->getNoiseParams("mgcarpathian_np_height3",       np_height3);
171         settings->getNoiseParams("mgcarpathian_np_height4",       np_height4);
172         settings->getNoiseParams("mgcarpathian_np_hills_terrain", np_hills_terrain);
173         settings->getNoiseParams("mgcarpathian_np_ridge_terrain", np_ridge_terrain);
174         settings->getNoiseParams("mgcarpathian_np_step_terrain",  np_step_terrain);
175         settings->getNoiseParams("mgcarpathian_np_hills",         np_hills);
176         settings->getNoiseParams("mgcarpathian_np_ridge_mnt",     np_ridge_mnt);
177         settings->getNoiseParams("mgcarpathian_np_step_mnt",      np_step_mnt);
178         settings->getNoiseParams("mgcarpathian_np_rivers",        np_rivers);
179         settings->getNoiseParams("mgcarpathian_np_mnt_var",       np_mnt_var);
180         settings->getNoiseParams("mgcarpathian_np_cave1",         np_cave1);
181         settings->getNoiseParams("mgcarpathian_np_cave2",         np_cave2);
182         settings->getNoiseParams("mgcarpathian_np_cavern",        np_cavern);
183         settings->getNoiseParams("mgcarpathian_np_dungeons",      np_dungeons);
184 }
185
186
187 void MapgenCarpathianParams::writeParams(Settings *settings) const
188 {
189         settings->setFlagStr("mgcarpathian_spflags", spflags, flagdesc_mapgen_carpathian);
190
191         settings->setFloat("mgcarpathian_base_level",   base_level);
192         settings->setFloat("mgcarpathian_river_width",  river_width);
193         settings->setFloat("mgcarpathian_river_depth",  river_depth);
194         settings->setFloat("mgcarpathian_valley_width", valley_width);
195
196         settings->setFloat("mgcarpathian_cave_width",         cave_width);
197         settings->setS16("mgcarpathian_large_cave_depth",     large_cave_depth);
198         settings->setU16("mgcarpathian_small_cave_num_min",   small_cave_num_min);
199         settings->setU16("mgcarpathian_small_cave_num_max",   small_cave_num_max);
200         settings->setU16("mgcarpathian_large_cave_num_min",   large_cave_num_min);
201         settings->setU16("mgcarpathian_large_cave_num_max",   large_cave_num_max);
202         settings->setFloat("mgcarpathian_large_cave_flooded", large_cave_flooded);
203         settings->setS16("mgcarpathian_cavern_limit",         cavern_limit);
204         settings->setS16("mgcarpathian_cavern_taper",         cavern_taper);
205         settings->setFloat("mgcarpathian_cavern_threshold",   cavern_threshold);
206         settings->setS16("mgcarpathian_dungeon_ymin",         dungeon_ymin);
207         settings->setS16("mgcarpathian_dungeon_ymax",         dungeon_ymax);
208
209         settings->setNoiseParams("mgcarpathian_np_filler_depth",  np_filler_depth);
210         settings->setNoiseParams("mgcarpathian_np_height1",       np_height1);
211         settings->setNoiseParams("mgcarpathian_np_height2",       np_height2);
212         settings->setNoiseParams("mgcarpathian_np_height3",       np_height3);
213         settings->setNoiseParams("mgcarpathian_np_height4",       np_height4);
214         settings->setNoiseParams("mgcarpathian_np_hills_terrain", np_hills_terrain);
215         settings->setNoiseParams("mgcarpathian_np_ridge_terrain", np_ridge_terrain);
216         settings->setNoiseParams("mgcarpathian_np_step_terrain",  np_step_terrain);
217         settings->setNoiseParams("mgcarpathian_np_hills",         np_hills);
218         settings->setNoiseParams("mgcarpathian_np_ridge_mnt",     np_ridge_mnt);
219         settings->setNoiseParams("mgcarpathian_np_step_mnt",      np_step_mnt);
220         settings->setNoiseParams("mgcarpathian_np_rivers",        np_rivers);
221         settings->setNoiseParams("mgcarpathian_np_mnt_var",       np_mnt_var);
222         settings->setNoiseParams("mgcarpathian_np_cave1",         np_cave1);
223         settings->setNoiseParams("mgcarpathian_np_cave2",         np_cave2);
224         settings->setNoiseParams("mgcarpathian_np_cavern",        np_cavern);
225         settings->setNoiseParams("mgcarpathian_np_dungeons",      np_dungeons);
226 }
227
228
229 void MapgenCarpathianParams::setDefaultSettings(Settings *settings)
230 {
231         settings->setDefault("mgcarpathian_spflags", flagdesc_mapgen_carpathian,
232                 MGCARPATHIAN_CAVERNS);
233 }
234
235 ////////////////////////////////////////////////////////////////////////////////
236
237
238 // Lerp function
239 inline float MapgenCarpathian::getLerp(float noise1, float noise2, float mod)
240 {
241         return noise1 + mod * (noise2 - noise1);
242 }
243
244 // Steps function
245 float MapgenCarpathian::getSteps(float noise)
246 {
247         float w = 0.5f;
248         float k = std::floor(noise / w);
249         float f = (noise - k * w) / w;
250         float s = std::fmin(2.f * f, 1.f);
251         return (k + s) * w;
252 }
253
254
255 ////////////////////////////////////////////////////////////////////////////////
256
257
258 void MapgenCarpathian::makeChunk(BlockMakeData *data)
259 {
260         // Pre-conditions
261         assert(data->vmanip);
262         assert(data->nodedef);
263         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
264                         data->blockpos_requested.Y >= data->blockpos_min.Y &&
265                         data->blockpos_requested.Z >= data->blockpos_min.Z);
266         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
267                         data->blockpos_requested.Y <= data->blockpos_max.Y &&
268                         data->blockpos_requested.Z <= data->blockpos_max.Z);
269
270         this->generating = true;
271         this->vm = data->vmanip;
272         this->ndef = data->nodedef;
273
274         v3s16 blockpos_min = data->blockpos_min;
275         v3s16 blockpos_max = data->blockpos_max;
276         node_min = blockpos_min * MAP_BLOCKSIZE;
277         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
278         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
279         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
280
281         // Create a block-specific seed
282         blockseed = getBlockSeed2(full_node_min, seed);
283
284         // Generate terrain
285         s16 stone_surface_max_y = generateTerrain();
286
287         // Create heightmap
288         updateHeightmap(node_min, node_max);
289
290         // Init biome generator, place biome-specific nodes, and build biomemap
291         if (flags & MG_BIOMES) {
292                 biomegen->calcBiomeNoise(node_min);
293                 generateBiomes();
294         }
295
296         // Generate tunnels, caverns and large randomwalk caves
297         if (flags & MG_CAVES) {
298                 // Generate tunnels first as caverns confuse them
299                 generateCavesNoiseIntersection(stone_surface_max_y);
300
301                 // Generate caverns
302                 bool near_cavern = false;
303                 if (spflags & MGCARPATHIAN_CAVERNS)
304                         near_cavern = generateCavernsNoise(stone_surface_max_y);
305
306                 // Generate large randomwalk caves
307                 if (near_cavern)
308                         // Disable large randomwalk caves in this mapchunk by setting
309                         // 'large cave depth' to world base. Avoids excessive liquid in
310                         // large caverns and floating blobs of overgenerated liquid.
311                         generateCavesRandomWalk(stone_surface_max_y,
312                                 -MAX_MAP_GENERATION_LIMIT);
313                 else
314                         generateCavesRandomWalk(stone_surface_max_y, large_cave_depth);
315         }
316
317         // Generate the registered ores
318         if (flags & MG_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 }