OpenTTD Source 20241224-master-gee860a5c8e
|
OTTD Perlin Noise Landscape Generator, aka TerraGenesis Perlin. More...
#include "stdafx.h"
#include "clear_map.h"
#include "void_map.h"
#include "genworld.h"
#include "core/random_func.hpp"
#include "landscape_type.h"
#include "safeguards.h"
Go to the source code of this file.
Data Structures | |
struct | HeightMap |
Height map - allocated array of heights (MapSizeX() + 1) x (MapSizeY() + 1) More... | |
Macros | |
#define | I2H(i) ((i) << height_decimal_bits) |
Conversion: int to Height. | |
#define | H2I(i) ((i) >> height_decimal_bits) |
Conversion: Height to int. | |
#define | A2I(i) ((i) >> amplitude_decimal_bits) |
Conversion: Amplitude to int. | |
#define | A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits)) |
Conversion: Amplitude to Height. | |
#define | F(fraction) ((Height)(fraction * mh)) |
Typedefs | |
using | Height = int16_t |
Fixed point type for heights. | |
using | Amplitude = int |
Fixed point array for amplitudes (and percent values) | |
Functions | |
static Height | TGPGetMaxHeight () |
Gets the maximum allowed height while generating a map based on mapsize, terraintype, and the maximum height level. | |
uint | GetEstimationTGPMapHeight () |
Get an overestimation of the highest peak TGP wants to generate. | |
static Amplitude | GetAmplitude (int frequency) |
Get the amplitude associated with the currently selected smoothness and maximum height level. | |
static bool | IsValidXY (int x, int y) |
Check if a X/Y set are within the map. | |
static bool | AllocHeightMap () |
Allocate array of (MapSizeX()+1)*(MapSizeY()+1) heights and init the _height_map structure members. | |
static void | FreeHeightMap () |
Free height map. | |
static Height | RandomHeight (Amplitude rMax) |
Generates new random height in given amplitude (generated numbers will range from - amplitude to + amplitude) | |
static void | HeightMapGenerate () |
Base Perlin noise generator - fills height map with raw Perlin noise. | |
static void | HeightMapGetMinMaxAvg (Height *min_ptr, Height *max_ptr, Height *avg_ptr) |
Returns min, max and average height from height map. | |
static int * | HeightMapMakeHistogram (Height h_min, Height h_max, int *hist_buf) |
Dill histogram and return pointer to its base point - to the count of zero heights. | |
static void | HeightMapSineTransform (Height h_min, Height h_max) |
Applies sine wave redistribution onto height map. | |
static void | HeightMapCurves (uint level) |
Additional map variety is provided by applying different curve maps to different parts of the map. | |
static void | HeightMapAdjustWaterLevel (Amplitude water_percent, Height h_max_new) |
Adjusts heights in height map to contain required amount of water tiles. | |
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 parameter rather than selected from the predefined sequences. | |
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 flat-edge slopes that were present before. | |
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 | HeightMapSmoothCoasts (uint8_t water_borders) |
Smooth coasts by modulating height of tiles close to map edges with cosine of distance from edge. | |
static void | HeightMapSmoothSlopes (Height dh_max) |
This routine provides the essential cleanup necessary before OTTD can display the terrain. | |
static void | HeightMapNormalize () |
Height map terraform post processing: | |
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 generation_seed, and the passed parameter; prime. | |
static double | linear_interpolate (const double a, const double b, const double x) |
This routine determines the interpolated value between a and b. | |
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 surrounding positions. | |
static void | TgenSetTileHeight (TileIndex tile, int height) |
A small helper function to initialize the terrain. | |
void | GenerateTerrainPerlin () |
The main new land generator using Perlin noise. | |
Variables | |
static const int | height_decimal_bits = 4 |
static const int | amplitude_decimal_bits = 10 |
static HeightMap | _height_map = { {}, 0, 0, 0 } |
Global height map instance. | |
static const int | MAX_TGP_FREQUENCIES = 10 |
Maximum number of TGP noise frequencies. | |
static const Amplitude | _water_percent [4] = {70, 170, 270, 420} |
Desired water percentage (100% == 1024) - indexed by _settings_game.difficulty.quantity_sea_lakes. | |
OTTD Perlin Noise Landscape Generator, aka TerraGenesis Perlin.
Definition in file tgp.cpp.
#define A2H | ( | a | ) | ((a) >> (amplitude_decimal_bits - height_decimal_bits)) |
#define A2I | ( | i | ) | ((i) >> amplitude_decimal_bits) |
#define H2I | ( | i | ) | ((i) >> height_decimal_bits) |
#define I2H | ( | i | ) | ((i) << height_decimal_bits) |
using Amplitude = int |
|
inlinestatic |
Allocate array of (MapSizeX()+1)*(MapSizeY()+1) heights and init the _height_map structure members.
Definition at line 318 of file tgp.cpp.
References _height_map, Map::SizeX(), and Map::SizeY().
Referenced by GenerateTerrainPerlin().
|
inlinestatic |
Free height map.
Definition at line 334 of file tgp.cpp.
References _height_map.
Referenced by GenerateTerrainPerlin().
void GenerateTerrainPerlin | ( | ) |
The main new land generator using Perlin noise.
Desert landscape is handled different to all others to give a desert valley between two high mountains. Clearly if a low height terrain (flat/very flat) is chosen, then the tropic areas won't be high enough, and there will be very little tropic on the map. Thus Tropic works best on Hilly or Mountainous.
Definition at line 976 of file tgp.cpp.
References _height_map, _settings_game, AllocHeightMap(), Clamp(), GameSettings::construction, ConstructionSettings::freeform_edges, FreeHeightMap(), GenerateWorldSetAbortCallback(), GWP_LANDSCAPE, H2I, HeightMap::height(), HeightMapGenerate(), HeightMapNormalize(), IncreaseGeneratingWorldProgress(), MakeVoid(), Map::SizeX(), Map::SizeY(), TgenSetTileHeight(), TGPGetMaxHeight(), and TileXY().
Referenced by GenerateLandscape().
|
static |
Get the amplitude associated with the currently selected smoothness and maximum height level.
frequency | The frequency to get the amplitudes for |
< Very smooth
< Smooth
< Rough
< Very rough
Definition at line 261 of file tgp.cpp.
References _settings_game, Clamp(), GameSettings::game_creation, I2H, MAX_TGP_FREQUENCIES, GameCreationSettings::tgen_smoothness, and TGPGetMaxHeight().
Referenced by HeightMapGenerate().
uint GetEstimationTGPMapHeight | ( | ) |
Get an overestimation of the highest peak TGP wants to generate.
Definition at line 250 of file tgp.cpp.
References H2I, and TGPGetMaxHeight().
Referenced by GenerateWorld().
Adjusts heights in height map to contain required amount of water tiles.
Definition at line 662 of file tgp.cpp.
References _height_map, A2I, HeightMapGetMinMaxAvg(), HeightMapMakeHistogram(), and I2H.
Referenced by HeightMapNormalize().
|
static |
This routine sculpts in from the edge a random amount, again a Perlin sequence, to avoid the rigid flat-edge slopes that were present before.
The Perlin noise map doesn't know where we are going to slice across, and so we often cut straight through high terrain. The smoothing routine makes it legal, gradually increasing up from the edge to the original terrain height. By cutting parts of this away, it gives a far more irregular edge to the map-edge. Sometimes it works beautifully with the existing sea & lakes, and creates a very realistic coastline. Other times the variation is less, and the map-edge shows its cliff-like roots.
This routine may be extended to randomly sculpt the height of the terrain near the edge. This will have the coast edge at low level (1-3), rising in smoothed steps inland to about 15 tiles in. This should make it look as though the map has been built for the map size, rather than a slice through a larger map.
Please note that all the small numbers; 53, 101, 167, etc. are small primes to help give the perlin noise a bit more of a random feel.
Definition at line 721 of file tgp.cpp.
References _height_map, _settings_game, abs(), GameSettings::game_creation, HasBit(), HeightMap::height(), GameCreationSettings::map_x, GameCreationSettings::map_y, and perlin_coast_noise_2D().
Referenced by HeightMapNormalize().
|
static |
Additional map variety is provided by applying different curve maps to different parts of the map.
A randomized low resolution grid contains which curve map to use on each part of the make. This filtered non-linearly to smooth out transitions between curves, so each tile could have between 100% of one map applied or 25% of four maps.
The curve maps define different land styles, i.e. lakes, low-lands, hills and mountain ranges, although these are dependent on the landscape style chosen as well.
The level parameter dictates the resolution of the grid. A low resolution grid will result in larger continuous areas of a land style, a higher resolution grid splits the style into smaller areas.
level | Rough indication of the size of the grid sections to style. Small level means large grid sections. |
Basically scale height X to height Y. Everything in between is interpolated.
< The height to scale from.
< The height to scale to.
Definition at line 544 of file tgp.cpp.
References _height_map, Clamp(), HasBit(), HeightMap::height(), I2H, RandomRange(), and TGPGetMaxHeight().
Referenced by HeightMapNormalize().
|
static |
Base Perlin noise generator - fills height map with raw Perlin noise.
This runs several iterations with increasing precision; the last iteration looks at areas of 1 by 1 tiles, the second to last at 2 by 2 tiles and the initial 2**MAX_TGP_FREQUENCIES by 2**MAX_TGP_FREQUENCIES tiles.
Definition at line 357 of file tgp.cpp.
References _height_map, GetAmplitude(), HeightMap::height(), Map::LogX(), Map::LogY(), MAX_TGP_FREQUENCIES, and RandomHeight().
Referenced by GenerateTerrainPerlin().
Returns min, max and average height from height map.
Definition at line 417 of file tgp.cpp.
References _height_map, and HeightMap::height().
Referenced by HeightMapAdjustWaterLevel().
Dill histogram and return pointer to its base point - to the count of zero heights.
Definition at line 440 of file tgp.cpp.
References _height_map.
Referenced by HeightMapAdjustWaterLevel().
|
static |
Height map terraform post processing:
Definition at line 859 of file tgp.cpp.
References _settings_game, _water_percent, GameSettings::construction, GameCreationSettings::custom_sea_level, CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY, GameSettings::difficulty, ConstructionSettings::freeform_edges, GameSettings::game_creation, GB(), HeightMapAdjustWaterLevel(), HeightMapCoastLines(), HeightMapCurves(), HeightMapSineTransform(), HeightMapSmoothCoasts(), HeightMapSmoothSlopes(), I2H, DifficultySettings::quantity_sea_lakes, GameCreationSettings::tgen_smoothness, TGPGetMaxHeight(), GameCreationSettings::variety, and GameCreationSettings::water_borders.
Referenced by GenerateTerrainPerlin().
Applies sine wave redistribution onto height map.
Definition at line 454 of file tgp.cpp.
References _height_map, _settings_game, GameSettings::game_creation, I2H, and GameCreationSettings::landscape.
Referenced by HeightMapNormalize().
|
static |
Start at given point, move in given direction, find and Smooth coast in that direction.
Definition at line 777 of file tgp.cpp.
References _height_map, HeightMap::height(), I2H, and IsValidXY().
Referenced by HeightMapSmoothCoasts().
|
static |
Smooth coasts by modulating height of tiles close to map edges with cosine of distance from edge.
Definition at line 814 of file tgp.cpp.
References _height_map, HasBit(), and HeightMapSmoothCoastInDirection().
Referenced by HeightMapNormalize().
|
static |
This routine provides the essential cleanup necessary before OTTD can display the terrain.
When generated, the terrain heights can jump more than one level between tiles. This routine smooths out those differences so that the most it can change is one level. When OTTD can support cliffs, this routine may not be necessary.
Definition at line 836 of file tgp.cpp.
References _height_map, and HeightMap::height().
Referenced by HeightMapNormalize().
|
static |
The Perlin Noise calculation using large primes The initial number is adjusted by two values; the generation_seed, and the passed parameter; prime.
prime is used to allow the perlin noise generator to create useful random numbers from slightly different series.
Definition at line 893 of file tgp.cpp.
References _settings_game, GameSettings::game_creation, and GameCreationSettings::generation_seed.
Referenced by interpolated_noise().
|
static |
This routine returns the smoothed interpolated noise for an x and y, using the values from the surrounding positions.
Definition at line 917 of file tgp.cpp.
References int_noise(), and linear_interpolate().
Referenced by perlin_coast_noise_2D().
|
inlinestatic |
Check if a X/Y set are within the map.
x | coordinate x |
y | coordinate y |
Definition at line 308 of file tgp.cpp.
References _height_map.
Referenced by HeightMapSmoothCoastInDirection().
|
inlinestatic |
This routine determines the interpolated value between a and b.
Definition at line 907 of file tgp.cpp.
Referenced by interpolated_noise().
|
static |
This is a similar function to the main perlin noise calculation, but uses the value p passed as a parameter rather than selected from the predefined sequences.
as you can guess by its title, i use this to create the indented coastline, which is just another perlin sequence.
Definition at line 943 of file tgp.cpp.
References interpolated_noise().
Referenced by HeightMapCoastLines().
Generates new random height in given amplitude (generated numbers will range from - amplitude to + amplitude)
rMax | Limit of result |
Definition at line 344 of file tgp.cpp.
References A2H, and RandomRange().
Referenced by HeightMapGenerate().
|
static |
A small helper function to initialize the terrain.
Definition at line 959 of file tgp.cpp.
References CLEAR_GRASS, IsInnerTile(), MakeClear(), and SetTileHeight().
Referenced by GenerateTerrainPerlin().
|
static |
Gets the maximum allowed height while generating a map based on mapsize, terraintype, and the maximum height level.
Desired maximum height - indexed by:
It is indexed by map size as well as terrain type since the map size limits the height of a usable mountain. For example, on a 64x64 map a 24 high single peak mountain (as if you raised land 24 times in the center of the map) will leave only a ring of about 10 tiles around the mountain to build on. On a 4096x4096 map, it won't cover any major part of the map.
< Very flat
< Flat
< Hilly
< Mountainous
< Alpinist
Definition at line 208 of file tgp.cpp.
References _settings_game, GameSettings::construction, GameCreationSettings::custom_terrain_type, CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY, GameSettings::difficulty, GameSettings::game_creation, I2H, Map::LogX(), Map::LogY(), ConstructionSettings::map_height_limit, MAX_MAP_SIZE_BITS, MIN_MAP_SIZE_BITS, and DifficultySettings::terrain_type.
Referenced by GenerateTerrainPerlin(), GetAmplitude(), GetEstimationTGPMapHeight(), HeightMapCurves(), and HeightMapNormalize().
|
static |
Global height map instance.
Definition at line 183 of file tgp.cpp.
Referenced by AllocHeightMap(), FreeHeightMap(), GenerateTerrainPerlin(), HeightMapAdjustWaterLevel(), HeightMapCoastLines(), HeightMapCurves(), HeightMapGenerate(), HeightMapGetMinMaxAvg(), HeightMapMakeHistogram(), HeightMapSineTransform(), HeightMapSmoothCoastInDirection(), HeightMapSmoothCoasts(), HeightMapSmoothSlopes(), and IsValidXY().
|
static |
Desired water percentage (100% == 1024) - indexed by _settings_game.difficulty.quantity_sea_lakes.
Definition at line 200 of file tgp.cpp.
Referenced by HeightMapNormalize().
|
static |
Maximum number of TGP noise frequencies.
Definition at line 197 of file tgp.cpp.
Referenced by GetAmplitude(), and HeightMapGenerate().