155 static const int height_decimal_bits = 4;
159 static const int amplitude_decimal_bits = 10;
180 return h[x + y * dim_x];
188 #define I2H(i) ((i) << height_decimal_bits) 190 #define H2I(i) ((i) >> height_decimal_bits) 193 #define I2A(i) ((i) << amplitude_decimal_bits) 195 #define A2I(i) ((i) >> amplitude_decimal_bits) 198 #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits)) 202 #define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++) 230 { 3, 3, 3, 3, 4, 5, 7 },
231 { 5, 7, 8, 9, 14, 19, 31 },
232 { 8, 9, 10, 15, 23, 37, 61 },
233 { 10, 11, 17, 19, 49, 63, 73 },
234 { 12, 19, 25, 31, 67, 75, 87 },
252 {16000, 5600, 1968, 688, 240, 16, 16},
253 {24000, 12800, 6400, 2700, 1024, 128, 16},
254 {32000, 19200, 12800, 8000, 3200, 256, 64},
255 {48000, 24000, 19200, 16000, 8000, 512, 320},
267 static const double extrapolation_factors[] = { 3.3, 2.8, 2.3, 1.8 };
274 if (index >= 0)
return amplitude;
277 double extrapolation_factor = extrapolation_factors[smoothness];
278 int height_range =
I2H(16);
280 amplitude = (
amplitude_t)(extrapolation_factor * (
double)amplitude);
296 return x >= 0 && x < _height_map.size_x && y >= 0 && y < _height_map.size_y;
312 _height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
313 _height_map.dim_x = _height_map.size_x + 1;
314 _height_map.h = CallocT<height_t>(_height_map.total_size);
326 _height_map.h =
nullptr;
350 assert(_height_map.h !=
nullptr);
360 if (amplitude == 0)
continue;
362 const int step = 1 << (MAX_TGP_FREQUENCIES - frequency - 1);
366 for (
int y = 0; y <= _height_map.size_y; y += step) {
367 for (
int x = 0; x <= _height_map.size_x; x += step) {
378 for (
int y = 0; y <= _height_map.size_y; y += 2 * step) {
379 for (
int x = 0; x <= _height_map.size_x - 2 * step; x += 2 * step) {
383 _height_map.
height(x + 1 * step, y) = h01;
388 for (
int y = 0; y <= _height_map.size_y - 2 * step; y += 2 * step) {
389 for (
int x = 0; x <= _height_map.size_x; x += step) {
393 _height_map.
height(x, y + 1 * step) = h10;
398 for (
int y = 0; y <= _height_map.size_y; y += step) {
399 for (
int x = 0; x <= _height_map.size_x; x += step) {
411 h_min = h_max = _height_map.
height(0, 0);
415 if (*h < h_min) h_min = *h;
416 if (*h > h_max) h_max = *h;
421 h_avg = (
height_t)(h_accu / (_height_map.size_x * _height_map.size_y));
424 if (min_ptr !=
nullptr) *min_ptr = h_min;
425 if (max_ptr !=
nullptr) *max_ptr = h_max;
426 if (avg_ptr !=
nullptr) *avg_ptr = h_avg;
432 int *hist = hist_buf - h_min;
452 if (*h < h_min)
continue;
455 fheight = (double)(*h - h_min) / (double)(h_max - h_min);
461 fheight = 2 * fheight - 1;
463 fheight = sin(fheight * M_PI_2);
465 fheight = 0.5 * (fheight + 1);
472 double sine_upper_limit = 0.75;
473 double linear_compression = 2;
474 if (fheight >= sine_upper_limit) {
476 fheight = 1.0 - (1.0 - fheight) / linear_compression;
478 double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
480 fheight = 2.0 * fheight / sine_upper_limit - 1.0;
482 fheight = sin(fheight * M_PI_2);
484 fheight = 0.5 * (fheight + 1.0) * m;
493 double sine_lower_limit = 0.5;
494 double linear_compression = 2;
495 if (fheight <= sine_lower_limit) {
497 fheight = fheight / linear_compression;
499 double m = sine_lower_limit / linear_compression;
501 fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
503 fheight = sin(fheight * M_PI_2);
505 fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
515 *h = (
height_t)(fheight * (h_max - h_min) + h_min);
516 if (*h < 0) *h =
I2H(0);
517 if (*h >= h_max) *h = h_max - 1;
542 struct control_point_t {
547 #define F(fraction) ((height_t)(fraction * mh)) 548 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) } };
549 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) } };
550 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) } };
551 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) } };
555 struct control_point_list_t {
557 const control_point_t *list;
559 const control_point_list_t curve_maps[] = {
560 {
lengthof(curve_map_1), curve_map_1 },
561 {
lengthof(curve_map_2), curve_map_2 },
562 {
lengthof(curve_map_3), curve_map_3 },
563 {
lengthof(curve_map_4), curve_map_4 },
570 float factor = sqrt((
float)_height_map.size_x / (
float)_height_map.size_y);
571 uint sx =
Clamp((
int)(((1 << level) * factor) + 0.5), 1, 128);
572 uint sy =
Clamp((
int)(((1 << level) / factor) + 0.5), 1, 128);
573 byte *c =
AllocaM(byte, sx * sy);
575 for (uint i = 0; i < sx * sy; i++) {
576 c[i] = Random() %
lengthof(curve_maps);
580 for (
int x = 0; x < _height_map.size_x; x++) {
583 float fx = (float)(sx * x) / _height_map.size_x + 1.0f;
586 float xr = 2.0f * (fx - x1) - 1.0f;
587 xr = sin(xr * M_PI_2);
588 xr = sin(xr * M_PI_2);
589 xr = 0.5f * (xr + 1.0f);
590 float xri = 1.0f - xr;
597 for (
int y = 0; y < _height_map.size_y; y++) {
600 float fy = (float)(sy * y) / _height_map.size_y + 1.0f;
603 float yr = 2.0f * (fy - y1) - 1.0f;
604 yr = sin(yr * M_PI_2);
605 yr = sin(yr * M_PI_2);
606 yr = 0.5f * (yr + 1.0f);
607 float yri = 1.0f - yr;
614 uint corner_a = c[x1 + sx * y1];
615 uint corner_b = c[x1 + sx * y2];
616 uint corner_c = c[x2 + sx * y1];
617 uint corner_d = c[x2 + sx * y2];
621 uint corner_bits = 0;
622 corner_bits |= 1 << corner_a;
623 corner_bits |= 1 << corner_b;
624 corner_bits |= 1 << corner_c;
625 corner_bits |= 1 << corner_d;
630 if (*h <
I2H(1))
continue;
636 for (uint t = 0; t <
lengthof(curve_maps); t++) {
637 if (!
HasBit(corner_bits, t))
continue;
640 const control_point_t *cm = curve_maps[t].list;
641 for (uint i = 0; i < curve_maps[t].length - 1; i++) {
642 const control_point_t &p1 = cm[i];
643 const control_point_t &p2 = cm[i + 1];
645 if (*h >= p1.x && *h < p2.x) {
646 ht[t] = p1.y + (*h - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
655 *h = (
height_t)((ht[corner_a] * yri + ht[corner_b] * yr) * xri + (ht[corner_c] * yri + ht[corner_d] * yr) * xr);
666 height_t h_min, h_max, h_avg, h_water_level;
667 int64 water_tiles, desired_water_tiles;
674 int *hist_buf = CallocT<int>(h_max - h_min + 1);
679 desired_water_tiles =
A2I(((int64)water_percent) * (int64)(_height_map.size_x * _height_map.size_y));
682 for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
683 water_tiles += hist[h_water_level];
684 if (water_tiles >= desired_water_tiles)
break;
695 *h = (
height_t)(((
int)h_max_new) * (*h - h_water_level) / (h_max - h_water_level)) +
I2H(1);
697 if (*h < 0) *h =
I2H(0);
698 if (*h >= h_max_new) *h = h_max_new - 1;
704 static double perlin_coast_noise_2D(
const double x,
const double y,
const double p,
const int prime);
729 const int margin = 4;
735 for (y = 0; y <= _height_map.size_y; y++) {
736 if (
HasBit(water_borders, BORDER_NE)) {
739 max_x =
max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
740 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
741 for (x = 0; x < max_x; x++) {
742 _height_map.
height(x, y) = 0;
746 if (
HasBit(water_borders, BORDER_SW)) {
749 max_x =
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 = _height_map.size_x; x > (_height_map.size_x - 1 - max_x); x--) {
752 _height_map.
height(x, y) = 0;
758 for (x = 0; x <= _height_map.size_x; x++) {
759 if (
HasBit(water_borders, BORDER_NW)) {
762 max_y =
max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
763 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
764 for (y = 0; y < max_y; y++) {
765 _height_map.
height(x, y) = 0;
769 if (
HasBit(water_borders, BORDER_SE)) {
772 max_y =
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 = _height_map.size_y; y > (_height_map.size_y - 1 - max_y); y--) {
775 _height_map.
height(x, y) = 0;
784 const int max_coast_dist_from_edge = 35;
785 const int max_coast_Smooth_depth = 35;
797 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++) {
799 if (_height_map.
height(x, y) >=
I2H(1))
break;
802 if (
IsValidXY(x + dir_y, y + dir_x) && _height_map.
height(x + dir_y, y + dir_x) > 0)
break;
805 if (
IsValidXY(x - dir_y, y - dir_x) && _height_map.
height(x - dir_y, y - dir_x) > 0)
break;
810 for (depth = 0;
IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
811 h = _height_map.
height(x, y);
812 h =
min(h, h_prev + (4 + depth));
813 _height_map.
height(x, y) = h;
823 for (x = 0; x < _height_map.size_x; x++) {
828 for (y = 0; y < _height_map.size_y; y++) {
843 for (
int y = 0; y <= (int)_height_map.size_y; y++) {
844 for (
int x = 0; x <= (int)_height_map.size_x; x++) {
845 height_t h_max =
min(_height_map.
height(x > 0 ? x - 1 : x, y), _height_map.
height(x, y > 0 ? y - 1 : y)) + dh_max;
846 if (_height_map.
height(x, y) > h_max) _height_map.
height(x, y) = h_max;
849 for (
int y = _height_map.size_y; y >= 0; y--) {
850 for (
int x = _height_map.size_x; x >= 0; x--) {
851 height_t h_max =
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;
852 if (_height_map.
height(x, y) > h_max) _height_map.
height(x, y) = h_max;
874 if (water_borders == BORDERS_RANDOM) water_borders =
GB(Random(), 0, 4);
898 static double int_noise(
const long x,
const long y,
const int prime)
905 return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
914 return a + x * (b - a);
924 const int integer_X = (int)x;
925 const int integer_Y = (int)y;
927 const double fractional_X = x - (double)integer_X;
928 const double fractional_Y = y - (double)integer_Y;
930 const double v1 =
int_noise(integer_X, integer_Y, prime);
931 const double v2 =
int_noise(integer_X + 1, integer_Y, prime);
932 const double v3 =
int_noise(integer_X, integer_Y + 1, prime);
933 const double v4 =
int_noise(integer_X + 1, integer_Y + 1, prime);
952 for (
int i = 0; i < 6; i++) {
953 const double frequency = (double)(1 << i);
954 const double amplitude = pow(p, (
double)i);
956 total +=
interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;
1003 for (
int y = 0; y < _height_map.size_y; y++) {
1004 for (
int x = 0; x < _height_map.size_x; x++) {
#define FOR_ALL_TILES_IN_HEIGHT(h)
Walk through all items of _height_map.h.
uint8 max_heightlevel
maximum allowed heightlevel
static uint MapSizeX()
Get the size of the map along the X.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static uint MapSizeY()
Get the size of the map along the Y.
static void MakeVoid(TileIndex t)
Make a nice void tile ;)
byte landscape
the landscape we're currently in
#define I2H(i)
Conversion: int to height_t.
static void HeightMapCurves(uint level)
Additional map variety is provided by applying different curve maps to different parts of the map...
static void TgenSetTileHeight(TileIndex tile, int height)
A small helper function to initialize the terrain.
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...
static uint MapLogX()
Logarithm of the map size along the X side.
static const uint CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY
Value for custom sea level in difficulty settings.
static void HeightMapSmoothCoasts(uint8 water_borders)
Smooth coasts by modulating height of tiles close to map edges with cosine of distance from edge...
static HeightMap _height_map
Global height map instance.
int amplitude_t
Fixed point array for amplitudes (and percent values)
DifficultySettings difficulty
settings related to the difficulty
#define A2H(a)
Conversion: amplitude_t to height_t.
Functions related to world/map generation.
static height_t TGPGetMaxHeight()
Gets the maximum allowed height while generating a map based on mapsize, terraintype, and the maximum height level.
static uint MapLogY()
Logarithm of the map size along the y side.
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
uint8 map_x
X size of map.
static T max(const T a, const T b)
Returns the maximum of two values.
byte custom_sea_level
manually entered percentage of water in the map
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Pseudo random number generator.
uint8 map_y
Y size of map.
void GenerateWorldSetAbortCallback(GWAbortProc *proc)
Set here the function, if any, that you want to be called when landscape generation is aborted...
static void HeightMapGetMinMaxAvg(height_t *min_ptr, height_t *max_ptr, height_t *avg_ptr)
Returns min, max and average height from height map.
static void SetTileHeight(TileIndex tile, uint height)
Sets the height of a tile.
bool freeform_edges
allow terraforming the tiles at the map edges
byte tgen_smoothness
how rough is the terrain from 0-3
Height map - allocated array of heights (MapSizeX() + 1) x (MapSizeY() + 1)
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...
static void HeightMapNormalize()
Height map terraform post processing:
static const int MAX_TGP_FREQUENCIES
Maximum number of TGP noise frequencies.
byte water_borders
bitset of the borders that are water
static bool AllocHeightMap()
Allocate array of (MapSizeX()+1)*(MapSizeY()+1) heights and init the _height_map structure members...
int16 height_t
Fixed point type for heights.
#define H2I(i)
Conversion: height_t to int.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
static void HeightMapGenerate()
Base Perlin noise generator - fills height map with raw Perlin noise.
#define A2I(i)
Conversion: amplitude_t to int.
void GenerateTerrainPerlin()
The main new land generator using Perlin noise.
static double linear_interpolate(const double a, const double b, const double x)
This routine determines the interpolated value between a and b.
#define lengthof(x)
Return the length of an fixed size array.
static void HeightMapAdjustWaterLevel(amplitude_t water_percent, height_t h_max_new)
Adjusts heights in height map to contain required amount of water tiles.
static T min(const T a, const T b)
Returns the minimum of two values.
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.
static void HeightMapSineTransform(height_t h_min, height_t h_max)
Applies sine wave redistribution onto height map.
height_t & height(uint x, uint y)
Height map accessor.
byte variety
variety level applied to TGP
static amplitude_t GetAmplitude(int frequency)
Get the amplitude associated with the currently selected smoothness and maximum height level...
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
byte quantity_sea_lakes
the amount of seas/lakes
uint32 generation_seed
noise seed for world generation
static height_t RandomHeight(amplitude_t rMax)
Generates new random height in given amplitude (generated numbers will range from - amplitude to + am...
static const amplitude_t _water_percent[4]
Desired water percentage (100% == 1024) - indexed by _settings_game.difficulty.quantity_sea_lakes.
Types related to the landscape.
uint32 TileIndex
The index/ID of a Tile.
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...
Map accessors for 'clear' tiles.
Map accessors for void tiles.
static bool IsValidXY(int x, int y)
Check if a X/Y set are within the map.
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...
static void MakeClear(TileIndex t, ClearGround g, uint density)
Make a clear tile.
static T abs(const T a)
Returns the absolute value of (scalar) variable.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static void FreeHeightMap()
Free height map.
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.
byte terrain_type
the mountainousness of the landscape
ConstructionSettings construction
construction of things in-game
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
static void HeightMapSmoothSlopes(height_t dh_max)
This routine provides the essential cleanup necessary before OTTD can display the terrain...
static const uint MAX_MAP_SIZE_BITS
Maximal size of map is equal to 2 ^ MAX_MAP_SIZE_BITS.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static bool IsInnerTile(TileIndex tile)
Check if a tile is within the map (not a border)
GameCreationSettings game_creation
settings used during the creation of a game (map)
void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
Increases the current stage of the world generation with one.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
static const uint MIN_MAP_SIZE_BITS
Minimal and maximal map width and height.
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().