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