OpenTTD Source: src/tgp.cpp Source File

OpenTTD Source 14.0-RC3

tgp.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "stdafx.h"
11 #include "clear_map.h"
12 #include "void_map.h"
13 #include "genworld.h"
14 #include "core/random_func.hpp"
15 #include "landscape_type.h"
16
17 #include "safeguards.h"
18
19 /*
20 *
21 * Quickie guide to Perlin Noise
22 * Perlin noise is a predictable pseudo random number sequence. By generating
23 * it in 2 dimensions, it becomes a useful random map that, for a given seed
24 * and starting X & Y, is entirely predictable. On the face of it, that may not
25 * be useful. However, it means that if you want to replay a map in a different
26 * terrain, or just vary the sea level, you just re-run the generator with the
27 * same seed. The seed is an int32_t, and is randomised on each run of New Game.
28 * The Scenario Generator does not randomise the value, so that you can
29 * experiment with one terrain until you are happy, or click "Random" for a new
30 * random seed.
31 *
32 * Perlin Noise is a series of "octaves" of random noise added together. By
33 * reducing the amplitude of the noise with each octave, the first octave of
34 * noise defines the main terrain sweep, the next the ripples on that, and the
35 * next the ripples on that. I use 6 octaves, with the amplitude controlled by
36 * a power ratio, usually known as a persistence or p value. This I vary by the
37 * smoothness selection, as can be seen in the table below. The closer to 1,
38 * the more of that octave is added. Each octave is however raised to the power
39 * of its position in the list, so the last entry in the "smooth" row, 0.35, is
40 * raised to the power of 6, so can only add 0.001838... of the amplitude to
41 * the running total.
42 *
43 * In other words; the first p value sets the general shape of the terrain, the
44 * second sets the major variations to that, ... until finally the smallest
45 * bumps are added.
46 *
47 * Usefully, this routine is totally scalable; so when 32bpp comes along, the
48 * terrain can be as bumpy as you like! It is also infinitely expandable; a
49 * single random seed terrain continues in X & Y as far as you care to
50 * calculate. In theory, we could use just one seed value, but randomly select
51 * where in the Perlin XY space we use for the terrain. Personally I prefer
52 * using a simple (0, 0) to (X, Y), with a varying seed.
53 *
54 *
55 * Other things i have had to do: mountainous wasn't mountainous enough, and
56 * since we only have 0..15 heights available, I add a second generated map
57 * (with a modified seed), onto the original. This generally raises the
58 * terrain, which then needs scaling back down. Overall effect is a general
59 * uplift.
60 *
61 * However, the values on the top of mountains are then almost guaranteed to go
62 * too high, so large flat plateaus appeared at height 15. To counter this, I
63 * scale all heights above 12 to proportion up to 15. It still makes the
64 * mountains have flattish tops, rather than craggy peaks, but at least they
65 * aren't smooth as glass.
66 *
67 *
68 * For a full discussion of Perlin Noise, please visit:
69 * http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
70 *
71 *
72 * Evolution II
73 *
74 * The algorithm as described in the above link suggests to compute each tile height
75 * as composition of several noise waves. Some of them are computed directly by
76 * noise(x, y) function, some are calculated using linear approximation. Our
77 * first implementation of perlin_noise_2D() used 4 noise(x, y) calls plus
78 * 3 linear interpolations. It was called 6 times for each tile. This was a bit
79 * CPU expensive.
80 *
81 * The following implementation uses optimized algorithm that should produce
82 * the same quality result with much less computations, but more memory accesses.
83 * The overall speedup should be 300% to 800% depending on CPU and memory speed.
84 *
85 * I will try to explain it on the example below:
86 *
87 * Have a map of 4 x 4 tiles, our simplified noise generator produces only two
88 * values -1 and +1, use 3 octaves with wave length 1, 2 and 4, with amplitudes
89 * 3, 2, 1. Original algorithm produces:
90 *
91 * h00 = lerp(lerp(-3, 3, 0/4), lerp(3, -3, 0/4), 0/4) + lerp(lerp(-2, 2, 0/2), lerp( 2, -2, 0/2), 0/2) + -1 = lerp(-3.0, 3.0, 0/4) + lerp(-2, 2, 0/2) + -1 = -3.0 + -2 + -1 = -6.0
92 * h01 = lerp(lerp(-3, 3, 1/4), lerp(3, -3, 1/4), 0/4) + lerp(lerp(-2, 2, 1/2), lerp( 2, -2, 1/2), 0/2) + 1 = lerp(-1.5, 1.5, 0/4) + lerp( 0, 0, 0/2) + 1 = -1.5 + 0 + 1 = -0.5
93 * h02 = lerp(lerp(-3, 3, 2/4), lerp(3, -3, 2/4), 0/4) + lerp(lerp( 2, -2, 0/2), lerp(-2, 2, 0/2), 0/2) + -1 = lerp( 0, 0, 0/4) + lerp( 2, -2, 0/2) + -1 = 0 + 2 + -1 = 1.0
94 * h03 = lerp(lerp(-3, 3, 3/4), lerp(3, -3, 3/4), 0/4) + lerp(lerp( 2, -2, 1/2), lerp(-2, 2, 1/2), 0/2) + 1 = lerp( 1.5, -1.5, 0/4) + lerp( 0, 0, 0/2) + 1 = 1.5 + 0 + 1 = 2.5
95 *
96 * h10 = lerp(lerp(-3, 3, 0/4), lerp(3, -3, 0/4), 1/4) + lerp(lerp(-2, 2, 0/2), lerp( 2, -2, 0/2), 1/2) + 1 = lerp(-3.0, 3.0, 1/4) + lerp(-2, 2, 1/2) + 1 = -1.5 + 0 + 1 = -0.5
97 * h11 = lerp(lerp(-3, 3, 1/4), lerp(3, -3, 1/4), 1/4) + lerp(lerp(-2, 2, 1/2), lerp( 2, -2, 1/2), 1/2) + -1 = lerp(-1.5, 1.5, 1/4) + lerp( 0, 0, 1/2) + -1 = -0.75 + 0 + -1 = -1.75
98 * h12 = lerp(lerp(-3, 3, 2/4), lerp(3, -3, 2/4), 1/4) + lerp(lerp( 2, -2, 0/2), lerp(-2, 2, 0/2), 1/2) + 1 = lerp( 0, 0, 1/4) + lerp( 2, -2, 1/2) + 1 = 0 + 0 + 1 = 1.0
99 * h13 = lerp(lerp(-3, 3, 3/4), lerp(3, -3, 3/4), 1/4) + lerp(lerp( 2, -2, 1/2), lerp(-2, 2, 1/2), 1/2) + -1 = lerp( 1.5, -1.5, 1/4) + lerp( 0, 0, 1/2) + -1 = 0.75 + 0 + -1 = -0.25
100 *
101 *
102 * Optimization 1:
103 *
104 * 1) we need to allocate a bit more tiles: (size_x + 1) * (size_y + 1) = (5 * 5):
105 *
106 * 2) setup corner values using amplitude 3
107 * { -3.0 X X X 3.0 }
108 * { X X X X X }
109 * { X X X X X }
110 * { X X X X X }
111 * { 3.0 X X X -3.0 }
112 *
113 * 3a) interpolate values in the middle
114 * { -3.0 X 0.0 X 3.0 }
115 * { X X X X X }
116 * { 0.0 X 0.0 X 0.0 }
117 * { X X X X X }
118 * { 3.0 X 0.0 X -3.0 }
119 *
120 * 3b) add patches with amplitude 2 to them
121 * { -5.0 X 2.0 X 1.0 }
122 * { X X X X X }
123 * { 2.0 X -2.0 X 2.0 }
124 * { X X X X X }
125 * { 1.0 X 2.0 X -5.0 }
126 *
127 * 4a) interpolate values in the middle
128 * { -5.0 -1.5 2.0 1.5 1.0 }
129 * { -1.5 -0.75 0.0 0.75 1.5 }
130 * { 2.0 0.0 -2.0 0.0 2.0 }
131 * { 1.5 0.75 0.0 -0.75 -1.5 }
132 * { 1.0 1.5 2.0 -1.5 -5.0 }
133 *
134 * 4b) add patches with amplitude 1 to them
135 * { -6.0 -0.5 1.0 2.5 0.0 }
136 * { -0.5 -1.75 1.0 -0.25 2.5 }
137 * { 1.0 1.0 -3.0 1.0 1.0 }
138 * { 2.5 -0.25 1.0 -1.75 -0.5 }
139 * { 0.0 2.5 1.0 -0.5 -6.0 }
140 *
141 *
142 *
143 * Optimization 2:
144 *
145 * As you can see above, each noise function was called just once. Therefore
146 * we don't need to use noise function that calculates the noise from x, y and
147 * some prime. The same quality result we can obtain using standard Random()
148 * function instead.
149 *
150 */
151
153 using Height = int16_t;
154 static const int height_decimal_bits = 4;
155
157 using Amplitude = int;
158 static const int amplitude_decimal_bits = 10;
159
161 struct HeightMap
162 {
163 std::vector<Height> h; //< array of heights
164 /* Even though the sizes are always positive, there are many cases where
165 * X and Y need to be signed integers due to subtractions. */
166 int dim_x; //< height map size_x Map::SizeX() + 1
167 int size_x; //< Map::SizeX()
168 int size_y; //< Map::SizeY()
169
176 inline Height &height(uint x, uint y)
177 {
178 return h[x + y * dim_x];
179 }
180 };
181
183 static HeightMap _height_map = { {}, 0, 0, 0 };
184
186 #define I2H(i) ((i) << height_decimal_bits)
187
188 #define H2I(i) ((i) >> height_decimal_bits)
189
191 #define I2A(i) ((i) << amplitude_decimal_bits)
192
193 #define A2I(i) ((i) >> amplitude_decimal_bits)
194
196 #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
197
199 static const int MAX_TGP_FREQUENCIES = 10;
200
202 static const Amplitude _water_percent[4] = {70, 170, 270, 420};
203
210 static Height TGPGetMaxHeight()
211 {
212 if (_settings_game.difficulty.terrain_type == CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY) {
213 /* TGP never reaches this height; this means that if a user inputs "2",
214 * it would create a flat map without the "+ 1". But that would
215 * overflow on "255". So we reduce it by 1 to get back in range. */
216 return I2H(_settings_game.game_creation.custom_terrain_type + 1) - 1;
217 }
218
229 static const int max_height[5][MAX_MAP_SIZE_BITS - MIN_MAP_SIZE_BITS + 1] = {
230 /* 64 128 256 512 1024 2048 4096 */
231 { 3, 3, 3, 3, 4, 5, 7 },
232 { 5, 7, 8, 9, 14, 19, 31 },
233 { 8, 9, 10, 15, 23, 37, 61 },
234 { 10, 11, 17, 19, 49, 63, 73 },
235 { 12, 19, 25, 31, 67, 75, 87 },
236 };
237
238 int map_size_bucket = std::min(Map::LogX(), Map::LogY()) - MIN_MAP_SIZE_BITS;
239 int max_height_from_table = max_height[_settings_game.difficulty.terrain_type][map_size_bucket];
240
241 /* If there is a manual map height limit, clamp to it. */
242 if (_settings_game.construction.map_height_limit != 0) {
243 max_height_from_table = std::min<uint>(max_height_from_table, _settings_game.construction.map_height_limit);
244 }
245
246 return I2H(max_height_from_table);
247 }
248
252 uint GetEstimationTGPMapHeight()
253 {
254 return H2I(TGPGetMaxHeight());
255 }
256
263 static Amplitude GetAmplitude(int frequency)
264 {
265 /* Base noise amplitudes (multiplied by 1024) and indexed by "smoothness setting" and log2(frequency). */
266 static const Amplitude amplitudes[][7] = {
267 /* lowest frequency ...... highest (every corner) */
268 {16000, 5600, 1968, 688, 240, 16, 16},
269 {24000, 12800, 6400, 2700, 1024, 128, 16},
270 {32000, 19200, 12800, 8000, 3200, 256, 64},
271 {48000, 24000, 19200, 16000, 8000, 512, 320},
272 };
273 /*
274 * Extrapolation factors for ranges before the table.
275 * The extrapolation is needed to account for the higher map heights. They need larger
276 * areas with a particular gradient so that we are able to create maps without too
277 * many steep slopes up to the wanted height level. It's definitely not perfect since
278 * it will bring larger rectangles with similar slopes which makes the rectangular
279 * behaviour of TGP more noticeable. However, these height differentiations cannot
280 * happen over much smaller areas; we basically double the "range" to give a similar
281 * slope for every doubling of map height.
282 */
283 static const double extrapolation_factors[] = { 3.3, 2.8, 2.3, 1.8 };
284
285 int smoothness = _settings_game.game_creation.tgen_smoothness;
286
287 /* Get the table index, and return that value if possible. */
288 int index = frequency - MAX_TGP_FREQUENCIES + lengthof(amplitudes[smoothness]);
289 Amplitude amplitude = amplitudes[smoothness][std::max(0, index)];
290 if (index >= 0) return amplitude;
291
292 /* We need to extrapolate the amplitude. */
293 double extrapolation_factor = extrapolation_factors[smoothness];
294 int height_range = I2H(16);
295 do {
296 amplitude = (Amplitude)(extrapolation_factor * (double)amplitude);
297 height_range <<= 1;
298 index++;
299 } while (index < 0);
300
301 return Clamp((TGPGetMaxHeight() - height_range) / height_range, 0, 1) * amplitude;
302 }
303
310 static inline bool IsValidXY(int x, int y)
311 {
312 return x >= 0 && x < _height_map.size_x && y >= 0 && y < _height_map.size_y;
313 }
314
315
320 static inline bool AllocHeightMap()
321 {
322 assert(_height_map.h.empty());
323
324 _height_map.size_x = Map::SizeX();
325 _height_map.size_y = Map::SizeY();
326
327 /* Allocate memory block for height map row pointers */
328 size_t total_size = static_cast<size_t>(_height_map.size_x + 1) * (_height_map.size_y + 1);
329 _height_map.dim_x = _height_map.size_x + 1;
330 _height_map.h.resize(total_size);
331
332 return true;
333 }
334
336 static inline void FreeHeightMap()
337 {
338 _height_map.h.clear();
339 }
340
346 static inline Height RandomHeight(Amplitude rMax)
347 {
348 /* Spread height into range -rMax..+rMax */
349 return A2H(RandomRange(2 * rMax + 1) - rMax);
350 }
351
359 static void HeightMapGenerate()
360 {
361 /* Trying to apply noise to uninitialized height map */
362 assert(!_height_map.h.empty());
363
364 int start = std::max(MAX_TGP_FREQUENCIES - (int)std::min(Map::LogX(), Map::LogY()), 0);
365 bool first = true;
366
367 for (int frequency = start; frequency < MAX_TGP_FREQUENCIES; frequency++) {
368 const Amplitude amplitude = GetAmplitude(frequency);
369
370 /* Ignore zero amplitudes; it means our map isn't height enough for this
371 * amplitude, so ignore it and continue with the next set of amplitude. */
372 if (amplitude == 0) continue;
373
374 const int step = 1 << (MAX_TGP_FREQUENCIES - frequency - 1);
375
376 if (first) {
377 /* This is first round, we need to establish base heights with step = size_min */
378 for (int y = 0; y <= _height_map.size_y; y += step) {
379 for (int x = 0; x <= _height_map.size_x; x += step) {
380 Height height = (amplitude > 0) ? RandomHeight(amplitude) : 0;
381 _height_map.height(x, y) = height;
382 }
383 }
384 first = false;
385 continue;
386 }
387
388 /* It is regular iteration round.
389 * Interpolate height values at odd x, even y tiles */
390 for (int y = 0; y <= _height_map.size_y; y += 2 * step) {
391 for (int x = 0; x <= _height_map.size_x - 2 * step; x += 2 * step) {
392 Height h00 = _height_map.height(x + 0 * step, y);
393 Height h02 = _height_map.height(x + 2 * step, y);
394 Height h01 = (h00 + h02) / 2;
395 _height_map.height(x + 1 * step, y) = h01;
396 }
397 }
398
399 /* Interpolate height values at odd y tiles */
400 for (int y = 0; y <= _height_map.size_y - 2 * step; y += 2 * step) {
401 for (int x = 0; x <= _height_map.size_x; x += step) {
402 Height h00 = _height_map.height(x, y + 0 * step);
403 Height h20 = _height_map.height(x, y + 2 * step);
404 Height h10 = (h00 + h20) / 2;
405 _height_map.height(x, y + 1 * step) = h10;
406 }
407 }
408
409 /* Add noise for next higher frequency (smaller steps) */
410 for (int y = 0; y <= _height_map.size_y; y += step) {
411 for (int x = 0; x <= _height_map.size_x; x += step) {
412 _height_map.height(x, y) += RandomHeight(amplitude);
413 }
414 }
415 }
416 }
417
419 static void HeightMapGetMinMaxAvg(Height *min_ptr, Height *max_ptr, Height *avg_ptr)
420 {
421 Height h_min, h_max, h_avg;
422 int64_t h_accu = 0;
423 h_min = h_max = _height_map.height(0, 0);
424
425 /* Get h_min, h_max and accumulate heights into h_accu */
426 for (const Height &h : _height_map.h) {
427 if (h < h_min) h_min = h;
428 if (h > h_max) h_max = h;
429 h_accu += h;
430 }
431
432 /* Get average height */
433 h_avg = (Height)(h_accu / (_height_map.size_x * _height_map.size_y));
434
435 /* Return required results */
436 if (min_ptr != nullptr) *min_ptr = h_min;
437 if (max_ptr != nullptr) *max_ptr = h_max;
438 if (avg_ptr != nullptr) *avg_ptr = h_avg;
439 }
440
442 static int *HeightMapMakeHistogram(Height h_min, [[maybe_unused]] Height h_max, int *hist_buf)
443 {
444 int *hist = hist_buf - h_min;
445
446 /* Count the heights and fill the histogram */
447 for (const Height &h : _height_map.h){
448 assert(h >= h_min);
449 assert(h <= h_max);
450 hist[h]++;
451 }
452 return hist;
453 }
454
456 static void HeightMapSineTransform(Height h_min, Height h_max)
457 {
458 for (Height &h : _height_map.h) {
459 double fheight;
460
461 if (h < h_min) continue;
462
463 /* Transform height into 0..1 space */
464 fheight = (double)(h - h_min) / (double)(h_max - h_min);
465 /* Apply sine transform depending on landscape type */
466 switch (_settings_game.game_creation.landscape) {
467 case LT_TOYLAND:
468 case LT_TEMPERATE:
469 /* Move and scale 0..1 into -1..+1 */
470 fheight = 2 * fheight - 1;
471 /* Sine transform */
472 fheight = sin(fheight * M_PI_2);
473 /* Transform it back from -1..1 into 0..1 space */
474 fheight = 0.5 * (fheight + 1);
475 break;
476
477 case LT_ARCTIC:
478 {
479 /* Arctic terrain needs special height distribution.
480 * Redistribute heights to have more tiles at highest (75%..100%) range */
481 double sine_upper_limit = 0.75;
482 double linear_compression = 2;
483 if (fheight >= sine_upper_limit) {
484 /* Over the limit we do linear compression up */
485 fheight = 1.0 - (1.0 - fheight) / linear_compression;
486 } else {
487 double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
488 /* Get 0..sine_upper_limit into -1..1 */
489 fheight = 2.0 * fheight / sine_upper_limit - 1.0;
490 /* Sine wave transform */
491 fheight = sin(fheight * M_PI_2);
492 /* Get -1..1 back to 0..(1 - (1 - sine_upper_limit) / linear_compression) == 0.0..m */
493 fheight = 0.5 * (fheight + 1.0) * m;
494 }
495 }
496 break;
497
498 case LT_TROPIC:
499 {
500 /* Desert terrain needs special height distribution.
501 * Half of tiles should be at lowest (0..25%) heights */
502 double sine_lower_limit = 0.5;
503 double linear_compression = 2;
504 if (fheight <= sine_lower_limit) {
505 /* Under the limit we do linear compression down */
506 fheight = fheight / linear_compression;
507 } else {
508 double m = sine_lower_limit / linear_compression;
509 /* Get sine_lower_limit..1 into -1..1 */
510 fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
511 /* Sine wave transform */
512 fheight = sin(fheight * M_PI_2);
513 /* Get -1..1 back to (sine_lower_limit / linear_compression)..1.0 */
514 fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
515 }
516 }
517 break;
518
519 default:
520 NOT_REACHED();
521 break;
522 }
523 /* Transform it back into h_min..h_max space */
524 h = (Height)(fheight * (h_max - h_min) + h_min);
525 if (h < 0) h = I2H(0);
526 if (h >= h_max) h = h_max - 1;
527 }
528 }
529
546 static void HeightMapCurves(uint level)
547 {
548 Height mh = TGPGetMaxHeight() - I2H(1); // height levels above sea level only
549
551 struct ControlPoint {
552 Height x;
553 Height y;
554 };
555 /* Scaled curve maps; value is in height_ts. */
556 #define F(fraction) ((Height)(fraction * mh))
557 const ControlPoint curve_map_1[] = { { F(0.0), F(0.0) }, { F(0.8), F(0.13) }, { F(1.0), F(0.4) } };
558 const ControlPoint curve_map_2[] = { { F(0.0), F(0.0) }, { F(0.53), F(0.13) }, { F(0.8), F(0.27) }, { F(1.0), F(0.6) } };
559 const ControlPoint curve_map_3[] = { { F(0.0), F(0.0) }, { F(0.53), F(0.27) }, { F(0.8), F(0.57) }, { F(1.0), F(0.8) } };
560 const ControlPoint curve_map_4[] = { { F(0.0), F(0.0) }, { F(0.4), F(0.3) }, { F(0.7), F(0.8) }, { F(0.92), F(0.99) }, { F(1.0), F(0.99) } };
561 #undef F
562
564 struct ControlPointList {
565 size_t length;
566 const ControlPoint *list;
567 };
568 static const ControlPointList curve_maps[] = {
569 { lengthof(curve_map_1), curve_map_1 },
570 { lengthof(curve_map_2), curve_map_2 },
571 { lengthof(curve_map_3), curve_map_3 },
572 { lengthof(curve_map_4), curve_map_4 },
573 };
574
575 Height ht[lengthof(curve_maps)];
576 MemSetT(ht, 0, lengthof(ht));
577
578 /* Set up a grid to choose curve maps based on location; attempt to get a somewhat square grid */
579 float factor = sqrt((float)_height_map.size_x / (float)_height_map.size_y);
580 uint sx = Clamp((int)(((1 << level) * factor) + 0.5), 1, 128);
581 uint sy = Clamp((int)(((1 << level) / factor) + 0.5), 1, 128);
582 std::vector<byte> c(static_cast<size_t>(sx) * sy);
583
584 for (uint i = 0; i < sx * sy; i++) {
585 c[i] = Random() % lengthof(curve_maps);
586 }
587
588 /* Apply curves */
589 for (int x = 0; x < _height_map.size_x; x++) {
590
591 /* Get our X grid positions and bi-linear ratio */
592 float fx = (float)(sx * x) / _height_map.size_x + 1.0f;
593 uint x1 = (uint)fx;
594 uint x2 = x1;
595 float xr = 2.0f * (fx - x1) - 1.0f;
596 xr = sin(xr * M_PI_2);
597 xr = sin(xr * M_PI_2);
598 xr = 0.5f * (xr + 1.0f);
599 float xri = 1.0f - xr;
600
601 if (x1 > 0) {
602 x1--;
603 if (x2 >= sx) x2--;
604 }
605
606 for (int y = 0; y < _height_map.size_y; y++) {
607
608 /* Get our Y grid position and bi-linear ratio */
609 float fy = (float)(sy * y) / _height_map.size_y + 1.0f;
610 uint y1 = (uint)fy;
611 uint y2 = y1;
612 float yr = 2.0f * (fy - y1) - 1.0f;
613 yr = sin(yr * M_PI_2);
614 yr = sin(yr * M_PI_2);
615 yr = 0.5f * (yr + 1.0f);
616 float yri = 1.0f - yr;
617
618 if (y1 > 0) {
619 y1--;
620 if (y2 >= sy) y2--;
621 }
622
623 uint corner_a = c[x1 + sx * y1];
624 uint corner_b = c[x1 + sx * y2];
625 uint corner_c = c[x2 + sx * y1];
626 uint corner_d = c[x2 + sx * y2];
627
628 /* Bitmask of which curve maps are chosen, so that we do not bother
629 * calculating a curve which won't be used. */
630 uint corner_bits = 0;
631 corner_bits |= 1 << corner_a;
632 corner_bits |= 1 << corner_b;
633 corner_bits |= 1 << corner_c;
634 corner_bits |= 1 << corner_d;
635
636 Height *h = &_height_map.height(x, y);
637
638 /* Do not touch sea level */
639 if (*h < I2H(1)) continue;
640
641 /* Only scale above sea level */
642 *h -= I2H(1);
643
644 /* Apply all curve maps that are used on this tile. */
645 for (uint t = 0; t < lengthof(curve_maps); t++) {
646 if (!HasBit(corner_bits, t)) continue;
647
648 [[maybe_unused]] bool found = false;
649 const ControlPoint *cm = curve_maps[t].list;
650 for (uint i = 0; i < curve_maps[t].length - 1; i++) {
651 const ControlPoint &p1 = cm[i];
652 const ControlPoint &p2 = cm[i + 1];
653
654 if (*h >= p1.x && *h < p2.x) {
655 ht[t] = p1.y + (*h - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
656 #ifdef WITH_ASSERT
657 found = true;
658 #endif
659 break;
660 }
661 }
662 assert(found);
663 }
664
665 /* Apply interpolation of curve map results. */
666 *h = (Height)((ht[corner_a] * yri + ht[corner_b] * yr) * xri + (ht[corner_c] * yri + ht[corner_d] * yr) * xr);
667
668 /* Readd sea level */
669 *h += I2H(1);
670 }
671 }
672 }
673
675 static void HeightMapAdjustWaterLevel(Amplitude water_percent, Height h_max_new)
676 {
677 Height h_min, h_max, h_avg, h_water_level;
678 int64_t water_tiles, desired_water_tiles;
679 int *hist;
680
681 HeightMapGetMinMaxAvg(&h_min, &h_max, &h_avg);
682
683 /* Allocate histogram buffer and clear its cells */
684 int *hist_buf = CallocT<int>(h_max - h_min + 1);
685 /* Fill histogram */
686 hist = HeightMapMakeHistogram(h_min, h_max, hist_buf);
687
688 /* How many water tiles do we want? */
689 desired_water_tiles = A2I(((int64_t)water_percent) * (int64_t)(_height_map.size_x * _height_map.size_y));
690
691 /* Raise water_level and accumulate values from histogram until we reach required number of water tiles */
692 for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
693 water_tiles += hist[h_water_level];
694 if (water_tiles >= desired_water_tiles) break;
695 }
696
697 /* We now have the proper water level value.
698 * Transform the height map into new (normalized) height map:
699 * values from range: h_min..h_water_level will become negative so it will be clamped to 0
700 * values from range: h_water_level..h_max are transformed into 0..h_max_new
701 * where h_max_new is depending on terrain type and map size.
702 */
703 for (Height &h : _height_map.h) {
704 /* Transform height from range h_water_level..h_max into 0..h_max_new range */
705 h = (Height)(((int)h_max_new) * (h - h_water_level) / (h_max - h_water_level)) + I2H(1);
706 /* Make sure all values are in the proper range (0..h_max_new) */
707 if (h < 0) h = I2H(0);
708 if (h >= h_max_new) h = h_max_new - 1;
709 }
710
711 free(hist_buf);
712 }
713
714 static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime);
715
736 static void HeightMapCoastLines(uint8_t water_borders)
737 {
738 int smallest_size = std::min(_settings_game.game_creation.map_x, _settings_game.game_creation.map_y);
739 const int margin = 4;
740 int y, x;
741 double max_x;
742 double max_y;
743
744 /* Lower to sea level */
745 for (y = 0; y <= _height_map.size_y; y++) {
746 if (HasBit(water_borders, BORDER_NE)) {
747 /* Top right */
748 max_x = abs((perlin_coast_noise_2D(_height_map.size_y - y, y, 0.9, 53) + 0.25) * 5 + (perlin_coast_noise_2D(y, y, 0.35, 179) + 1) * 12);
749 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
750 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
751 for (x = 0; x < max_x; x++) {
752 _height_map.height(x, y) = 0;
753 }
754 }
755
756 if (HasBit(water_borders, BORDER_SW)) {
757 /* Bottom left */
758 max_x = abs((perlin_coast_noise_2D(_height_map.size_y - y, y, 0.85, 101) + 0.3) * 6 + (perlin_coast_noise_2D(y, y, 0.45, 67) + 0.75) * 8);
759 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
760 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
761 for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x); x--) {
762 _height_map.height(x, y) = 0;
763 }
764 }
765 }
766
767 /* Lower to sea level */
768 for (x = 0; x <= _height_map.size_x; x++) {
769 if (HasBit(water_borders, BORDER_NW)) {
770 /* Top left */
771 max_y = abs((perlin_coast_noise_2D(x, _height_map.size_y / 2, 0.9, 167) + 0.4) * 5 + (perlin_coast_noise_2D(x, _height_map.size_y / 3, 0.4, 211) + 0.7) * 9);
772 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
773 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
774 for (y = 0; y < max_y; y++) {
775 _height_map.height(x, y) = 0;
776 }
777 }
778
779 if (HasBit(water_borders, BORDER_SE)) {
780 /* Bottom right */
781 max_y = abs((perlin_coast_noise_2D(x, _height_map.size_y / 3, 0.85, 71) + 0.25) * 6 + (perlin_coast_noise_2D(x, _height_map.size_y / 3, 0.35, 193) + 0.75) * 12);
782 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
783 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
784 for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y); y--) {
785 _height_map.height(x, y) = 0;
786 }
787 }
788 }
789 }
790
792 static void HeightMapSmoothCoastInDirection(int org_x, int org_y, int dir_x, int dir_y)
793 {
794 const int max_coast_dist_from_edge = 35;
795 const int max_coast_Smooth_depth = 35;
796
797 int x, y;
798 int ed; // coast distance from edge
799 int depth;
800
801 Height h_prev = I2H(1);
802 Height h;
803
804 assert(IsValidXY(org_x, org_y));
805
806 /* Search for the coast (first non-water tile) */
807 for (x = org_x, y = org_y, ed = 0; IsValidXY(x, y) && ed < max_coast_dist_from_edge; x += dir_x, y += dir_y, ed++) {
808 /* Coast found? */
809 if (_height_map.height(x, y) >= I2H(1)) break;
810
811 /* Coast found in the neighborhood? */
812 if (IsValidXY(x + dir_y, y + dir_x) && _height_map.height(x + dir_y, y + dir_x) > 0) break;
813
814 /* Coast found in the neighborhood on the other side */
815 if (IsValidXY(x - dir_y, y - dir_x) && _height_map.height(x - dir_y, y - dir_x) > 0) break;
816 }
817
818 /* Coast found or max_coast_dist_from_edge has been reached.
819 * Soften the coast slope */
820 for (depth = 0; IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
821 h = _height_map.height(x, y);
822 h = static_cast<Height>(std::min<uint>(h, h_prev + (4 + depth))); // coast softening formula
823 _height_map.height(x, y) = h;
824 h_prev = h;
825 }
826 }
827
829 static void HeightMapSmoothCoasts(uint8_t water_borders)
830 {
831 int x, y;
832 /* First Smooth NW and SE coasts (y close to 0 and y close to size_y) */
833 for (x = 0; x < _height_map.size_x; x++) {
834 if (HasBit(water_borders, BORDER_NW)) HeightMapSmoothCoastInDirection(x, 0, 0, 1);
835 if (HasBit(water_borders, BORDER_SE)) HeightMapSmoothCoastInDirection(x, _height_map.size_y - 1, 0, -1);
836 }
837 /* First Smooth NE and SW coasts (x close to 0 and x close to size_x) */
838 for (y = 0; y < _height_map.size_y; y++) {
839 if (HasBit(water_borders, BORDER_NE)) HeightMapSmoothCoastInDirection(0, y, 1, 0);
840 if (HasBit(water_borders, BORDER_SW)) HeightMapSmoothCoastInDirection(_height_map.size_x - 1, y, -1, 0);
841 }
842 }
843
851 static void HeightMapSmoothSlopes(Height dh_max)
852 {
853 for (int y = 0; y <= (int)_height_map.size_y; y++) {
854 for (int x = 0; x <= (int)_height_map.size_x; x++) {
855 Height h_max = std::min(_height_map.height(x > 0 ? x - 1 : x, y), _height_map.height(x, y > 0 ? y - 1 : y)) + dh_max;
856 if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
857 }
858 }
859 for (int y = _height_map.size_y; y >= 0; y--) {
860 for (int x = _height_map.size_x; x >= 0; x--) {
861 Height h_max = std::min(_height_map.height(x < _height_map.size_x ? x + 1 : x, y), _height_map.height(x, y < _height_map.size_y ? y + 1 : y)) + dh_max;
862 if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
863 }
864 }
865 }
866
874 static void HeightMapNormalize()
875 {
876 int sea_level_setting = _settings_game.difficulty.quantity_sea_lakes;
877 const Amplitude water_percent = sea_level_setting != (int)CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY ? _water_percent[sea_level_setting] : _settings_game.game_creation.custom_sea_level * 1024 / 100;
878 const Height h_max_new = TGPGetMaxHeight();
879 const Height roughness = 7 + 3 * _settings_game.game_creation.tgen_smoothness;
880
881 HeightMapAdjustWaterLevel(water_percent, h_max_new);
882
883 byte water_borders = _settings_game.construction.freeform_edges ? _settings_game.game_creation.water_borders : 0xF;
884 if (water_borders == BORDERS_RANDOM) water_borders = GB(Random(), 0, 4);
885
886 HeightMapCoastLines(water_borders);
887 HeightMapSmoothSlopes(roughness);
888
889 HeightMapSmoothCoasts(water_borders);
890 HeightMapSmoothSlopes(roughness);
891
892 HeightMapSineTransform(I2H(1), h_max_new);
893
894 if (_settings_game.game_creation.variety > 0) {
895 HeightMapCurves(_settings_game.game_creation.variety);
896 }
897
898 HeightMapSmoothSlopes(I2H(1));
899 }
900
908 static double int_noise(const long x, const long y, const int prime)
909 {
910 long n = x + y * prime + _settings_game.game_creation.generation_seed;
911
912 n = (n << 13) ^ n;
913
914 /* Pseudo-random number generator, using several large primes */
915 return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
916 }
917
918
922 static inline double linear_interpolate(const double a, const double b, const double x)
923 {
924 return a + x * (b - a);
925 }
926
927
932 static double interpolated_noise(const double x, const double y, const int prime)
933 {
934 const int integer_X = (int)x;
935 const int integer_Y = (int)y;
936
937 const double fractional_X = x - (double)integer_X;
938 const double fractional_Y = y - (double)integer_Y;
939
940 const double v1 = int_noise(integer_X, integer_Y, prime);
941 const double v2 = int_noise(integer_X + 1, integer_Y, prime);
942 const double v3 = int_noise(integer_X, integer_Y + 1, prime);
943 const double v4 = int_noise(integer_X + 1, integer_Y + 1, prime);
944
945 const double i1 = linear_interpolate(v1, v2, fractional_X);
946 const double i2 = linear_interpolate(v3, v4, fractional_X);
947
948 return linear_interpolate(i1, i2, fractional_Y);
949 }
950
951
958 static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime)
959 {
960 double total = 0.0;
961
962 for (int i = 0; i < 6; i++) {
963 const double frequency = (double)(1 << i);
964 const double amplitude = pow(p, (double)i);
965
966 total += interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;
967 }
968
969 return total;
970 }
971
972
974 static void TgenSetTileHeight(TileIndex tile, int height)
975 {
976 SetTileHeight(tile, height);
977
978 /* Only clear the tiles within the map area. */
979 if (IsInnerTile(tile)) {
980 MakeClear(tile, CLEAR_GRASS, 3);
981 }
982 }
983
991 void GenerateTerrainPerlin()
992 {
993 if (!AllocHeightMap()) return;
994 GenerateWorldSetAbortCallback(FreeHeightMap);
995
996 HeightMapGenerate();
997
998 IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
999
1000 HeightMapNormalize();
1001
1002 IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
1003
1004 /* First make sure the tiles at the north border are void tiles if needed. */
1005 if (_settings_game.construction.freeform_edges) {
1006 for (uint x = 0; x < Map::SizeX(); x++) MakeVoid(TileXY(x, 0));
1007 for (uint y = 0; y < Map::SizeY(); y++) MakeVoid(TileXY(0, y));
1008 }
1009
1010 int max_height = H2I(TGPGetMaxHeight());
1011
1012 /* Transfer height map into OTTD map */
1013 for (int y = 0; y < _height_map.size_y; y++) {
1014 for (int x = 0; x < _height_map.size_x; x++) {
1015 TgenSetTileHeight(TileXY(x, y), Clamp(H2I(_height_map.height(x, y)), 0, max_height));
1016 }
1017 }
1018
1019 IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
1020
1021 FreeHeightMap();
1022 GenerateWorldSetAbortCallback(nullptr);
1023 }
GenerateTerrainPerlin
void GenerateTerrainPerlin()
The main new land generator using Perlin noise.
Definition: tgp.cpp:991
AllocHeightMap
static bool AllocHeightMap()
Allocate array of (MapSizeX()+1)*(MapSizeY()+1) heights and init the _height_map structure members.
Definition: tgp.cpp:320
Height
int16_t Height
Fixed point type for heights.
Definition: tgp.cpp:153
landscape_type.h
GameCreationSettings::custom_sea_level
byte custom_sea_level
manually entered percentage of water in the map
Definition: settings_type.h:361
GameCreationSettings::landscape
byte landscape
the landscape we're currently in
Definition: settings_type.h:355
Map::LogX
static debug_inline uint LogX()
Logarithm of the map size along the X side.
Definition: map_func.h:251
GameCreationSettings::tgen_smoothness
byte tgen_smoothness
how rough is the terrain from 0-3
Definition: settings_type.h:350
HeightMapSineTransform
static void HeightMapSineTransform(Height h_min, Height h_max)
Applies sine wave redistribution onto height map.
Definition: tgp.cpp:456
HeightMapCoastLines
static void HeightMapCoastLines(uint8_t water_borders)
This routine sculpts in from the edge a random amount, again a Perlin sequence, to avoid the rigid fl...
Definition: tgp.cpp:736
MIN_MAP_SIZE_BITS
static const uint MIN_MAP_SIZE_BITS
Minimal and maximal map width and height.
Definition: map_type.h:37
A2I
#define A2I(i)
Conversion: Amplitude to int.
Definition: tgp.cpp:193
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
HeightMapCurves
static void HeightMapCurves(uint level)
Additional map variety is provided by applying different curve maps to different parts of the map.
Definition: tgp.cpp:546
HeightMapGenerate
static void HeightMapGenerate()
Base Perlin noise generator - fills height map with raw Perlin noise.
Definition: tgp.cpp:359
SetTileHeight
void SetTileHeight(Tile tile, uint height)
Sets the height of a tile.
Definition: tile_map.h:57
I2H
#define I2H(i)
Conversion: int to Height.
Definition: tgp.cpp:186
GameSettings::difficulty
DifficultySettings difficulty
settings related to the difficulty
Definition: settings_type.h:617
CLEAR_GRASS
@ CLEAR_GRASS
0-3
Definition: clear_map.h:20
GameCreationSettings::variety
byte variety
variety level applied to TGP
Definition: settings_type.h:359
void_map.h
HeightMap
Height map - allocated array of heights (MapSizeX() + 1) x (MapSizeY() + 1)
Definition: tgp.cpp:161
GetEstimationTGPMapHeight
uint GetEstimationTGPMapHeight()
Get an overestimation of the highest peak TGP wants to generate.
Definition: tgp.cpp:252
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
IsValidXY
static bool IsValidXY(int x, int y)
Check if a X/Y set are within the map.
Definition: tgp.cpp:310
clear_map.h
linear_interpolate
static double linear_interpolate(const double a, const double b, const double x)
This routine determines the interpolated value between a and b.
Definition: tgp.cpp:922
genworld.h
IncreaseGeneratingWorldProgress
void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
Increases the current stage of the world generation with one.
Definition: genworld_gui.cpp:1550
HeightMapNormalize
static void HeightMapNormalize()
Height map terraform post processing:
Definition: tgp.cpp:874
interpolated_noise
static double interpolated_noise(const double x, const double y, const int prime)
This routine returns the smoothed interpolated noise for an x and y, using the values from the surrou...
Definition: tgp.cpp:932
RandomRange
static uint32_t RandomRange(uint32_t limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
A2H
#define A2H(a)
Conversion: Amplitude to Height.
Definition: tgp.cpp:196
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:618
MakeClear
void MakeClear(Tile t, ClearGround g, uint density)
Make a clear tile.
Definition: clear_map.h:259
GWP_LANDSCAPE
@ GWP_LANDSCAPE
Create the landscape.
Definition: genworld.h:71
Amplitude
int Amplitude
Fixed point array for amplitudes (and percent values)
Definition: tgp.cpp:157
perlin_coast_noise_2D
static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime)
This is a similar function to the main perlin noise calculation, but uses the value p passed as a par...
Definition: tgp.cpp:958
GetAmplitude
static Amplitude GetAmplitude(int frequency)
Get the amplitude associated with the currently selected smoothness and maximum height level.
Definition: tgp.cpp:263
HeightMapSmoothCoasts
static void HeightMapSmoothCoasts(uint8_t water_borders)
Smooth coasts by modulating height of tiles close to map edges with cosine of distance from edge.
Definition: tgp.cpp:829
DifficultySettings::terrain_type
byte terrain_type
the mountainousness of the landscape
Definition: settings_type.h:111
_height_map
static HeightMap _height_map
Global height map instance.
Definition: tgp.cpp:183
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:379
MAX_TGP_FREQUENCIES
static const int MAX_TGP_FREQUENCIES
Maximum number of TGP noise frequencies.
Definition: tgp.cpp:199
ConstructionSettings::map_height_limit
uint8_t map_height_limit
the maximum allowed heightlevel
Definition: settings_type.h:369
H2I
#define H2I(i)
Conversion: Height to int.
Definition: tgp.cpp:188
MakeVoid
void MakeVoid(Tile t)
Make a nice void tile ;)
Definition: void_map.h:19
TGPGetMaxHeight
static Height TGPGetMaxHeight()
Gets the maximum allowed height while generating a map based on mapsize, terraintype,...
Definition: tgp.cpp:210
GameCreationSettings::water_borders
byte water_borders
bitset of the borders that are water
Definition: settings_type.h:356
GenerateWorldSetAbortCallback
void GenerateWorldSetAbortCallback(GWAbortProc *proc)
Set here the function, if any, that you want to be called when landscape generation is aborted.
Definition: genworld.cpp:235
HeightMapAdjustWaterLevel
static void HeightMapAdjustWaterLevel(Amplitude water_percent, Height h_max_new)
Adjusts heights in height map to contain required amount of water tiles.
Definition: tgp.cpp:675
FreeHeightMap
static void FreeHeightMap()
Free height map.
Definition: tgp.cpp:336
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
safeguards.h
GameCreationSettings::custom_terrain_type
byte custom_terrain_type
manually entered height for TGP to aim for
Definition: settings_type.h:360
ConstructionSettings::freeform_edges
bool freeform_edges
allow terraforming the tiles at the map edges
Definition: settings_type.h:382
HeightMapSmoothCoastInDirection
static void HeightMapSmoothCoastInDirection(int org_x, int org_y, int dir_x, int dir_y)
Start at given point, move in given direction, find and Smooth coast in that direction.
Definition: tgp.cpp:792
DifficultySettings::quantity_sea_lakes
byte quantity_sea_lakes
the amount of seas/lakes
Definition: settings_type.h:112
stdafx.h
Map::SizeX
static debug_inline uint SizeX()
Get the size of the map along the X.
Definition: map_func.h:270
_water_percent
static const Amplitude _water_percent[4]
Desired water percentage (100% == 1024) - indexed by _settings_game.difficulty.quantity_sea_lakes.
Definition: tgp.cpp:202
GameCreationSettings::generation_seed
uint32_t generation_seed
noise seed for world generation
Definition: settings_type.h:339
MAX_MAP_SIZE_BITS
static const uint MAX_MAP_SIZE_BITS
Maximal size of map is equal to 2 ^ MAX_MAP_SIZE_BITS.
Definition: map_type.h:38
GameCreationSettings::map_y
uint8_t map_y
Y size of map.
Definition: settings_type.h:343
GameCreationSettings::map_x
uint8_t map_x
X size of map.
Definition: settings_type.h:342
abs
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:23
HeightMap::height
Height & height(uint x, uint y)
Height map accessor.
Definition: tgp.cpp:176
CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY
static const uint CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY
Value for custom terrain type in difficulty settings.
Definition: genworld.h:45
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:300
Map::LogY
static uint LogY()
Logarithm of the map size along the y side.
Definition: map_func.h:261
MemSetT
void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
TileXY
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:385
RandomHeight
static Height RandomHeight(Amplitude rMax)
Generates new random height in given amplitude (generated numbers will range from - amplitude to + am...
Definition: tgp.cpp:346
random_func.hpp
TgenSetTileHeight
static void TgenSetTileHeight(TileIndex tile, int height)
A small helper function to initialize the terrain.
Definition: tgp.cpp:974
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:619
HeightMapMakeHistogram
static int * HeightMapMakeHistogram(Height h_min, [[maybe_unused]] Height h_max, int *hist_buf)
Dill histogram and return pointer to its base point - to the count of zero heights.
Definition: tgp.cpp:442
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
HeightMapGetMinMaxAvg
static void HeightMapGetMinMaxAvg(Height *min_ptr, Height *max_ptr, Height *avg_ptr)
Returns min, max and average height from height map.
Definition: tgp.cpp:419
CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY
static const uint CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY
Value for custom sea level in difficulty settings.
Definition: genworld.h:47
int_noise
static double int_noise(const long x, const long y, const int prime)
The Perlin Noise calculation using large primes The initial number is adjusted by two values; the gen...
Definition: tgp.cpp:908
HeightMapSmoothSlopes
static void HeightMapSmoothSlopes(Height dh_max)
This routine provides the essential cleanup necessary before OTTD can display the terrain.
Definition: tgp.cpp:851
Map::SizeY
static uint SizeY()
Get the size of the map along the Y.
Definition: map_func.h:279
IsInnerTile
bool IsInnerTile(Tile tile)
Check if a tile is within the map (not a border)
Definition: tile_map.h:109
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103