OpenTTD Source
20241108-master-g80f628063a
|
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. More... | |
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. More... | |
static bool | IsValidXY (int x, int y) |
Check if a X/Y set are within the map. More... | |
static bool | AllocHeightMap () |
Allocate array of (MapSizeX()+1)*(MapSizeY()+1) heights and init the _height_map structure members. More... | |
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) More... | |
static void | HeightMapGenerate () |
Base Perlin noise generator - fills height map with raw Perlin noise. More... | |
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, [[maybe_unused]] 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. More... | |
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. More... | |
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. More... | |
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. More... | |
static void | HeightMapNormalize () |
Height map terraform post processing: More... | |
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. More... | |
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. More... | |
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.
|
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().
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 _settings_game, AllocHeightMap(), GameSettings::construction, ConstructionSettings::freeform_edges, FreeHeightMap(), GenerateWorldSetAbortCallback(), GWP_LANDSCAPE, HeightMapGenerate(), HeightMapNormalize(), and IncreaseGeneratingWorldProgress().
|
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().
|
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 _settings_game, GameSettings::game_creation, GameCreationSettings::map_x, and GameCreationSettings::map_y.
|
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 I2H, and TGPGetMaxHeight().
|
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(), Map::LogX(), Map::LogY(), and MAX_TGP_FREQUENCIES.
Referenced by GenerateTerrainPerlin().
|
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, HeightMapAdjustWaterLevel(), DifficultySettings::quantity_sea_lakes, GameCreationSettings::tgen_smoothness, TGPGetMaxHeight(), and GameCreationSettings::water_borders.
Referenced by GenerateTerrainPerlin().
|
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.
|
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.
|
inlinestatic |
|
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.
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().
|
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 GetAmplitude(), GetEstimationTGPMapHeight(), HeightMapCurves(), and HeightMapNormalize().