OpenTTD

OTTD Perlin Noise Landscape Generator, aka TerraGenesis Perlin. More...
#include "stdafx.h"
#include <math.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_t.  
#define  H2I(i) ((i) >> height_decimal_bits) 
Conversion: height_t to int.  
#define  I2A(i) ((i) << amplitude_decimal_bits) 
Conversion: int to amplitude_t.  
#define  A2I(i) ((i) >> amplitude_decimal_bits) 
Conversion: amplitude_t to int.  
#define  A2H(a) ((a) >> (amplitude_decimal_bits  height_decimal_bits)) 
Conversion: amplitude_t to height_t.  
#define  FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++) 
Walk through all items of _height_map.h.  
#define  F(fraction) ((height_t)(fraction * mh)) 
Typedefs  
typedef int16  height_t 
Fixed point type for heights.  
typedef int  amplitude_t 
Fixed point array for amplitudes (and percent values)  
Functions  
static height_t  TGPGetMaxHeight () 
Gets the maximum allowed height while generating a map based on mapsize, terraintype, and the maximum height level. More...  
static amplitude_t  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_t  RandomHeight (amplitude_t 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_t *min_ptr, height_t *max_ptr, height_t *avg_ptr) 
Returns min, max and average height from 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.  
static void  HeightMapSineTransform (height_t h_min, height_t 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_t water_percent, height_t 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 water_borders) 
This routine sculpts in from the edge a random amount, again a Perlin sequence, to avoid the rigid flatedge 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 water_borders) 
Smooth coasts by modulating height of tiles close to map edges with cosine of distance from edge.  
static void  HeightMapSmoothSlopes (height_t 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 = {NULL, 0, 0, 0, 0} 
Global height map instance.  
static const int  MAX_TGP_FREQUENCIES = 10 
Maximum number of TGP noise frequencies. More...  
static const amplitude_t  _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 306 of file tgp.cpp.
References FOR_ALL_TILES_IN_HEIGHT, MapSizeX(), and MapSizeY().
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 wont be high enough, and there will be very little tropic on the map. Thus Tropic works best on Hilly or Mountainous.
Definition at line 983 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 249 of file tgp.cpp.
References _settings_game, Clamp(), GameSettings::game_creation, I2H, lengthof, max(), MAX_TGP_FREQUENCIES, GameCreationSettings::tgen_smoothness, and TGPGetMaxHeight().

static 
This routine sculpts in from the edge a random amount, again a Perlin sequence, to avoid the rigid flatedge 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 mapedge. Sometimes it works beautifully with the existing sea & lakes, and creates a very realistic coastline. Other times the variation is less, and the mapedge shows its clifflike 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 (13), 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 728 of file tgp.cpp.
References _settings_game, GameSettings::game_creation, GameCreationSettings::map_x, GameCreationSettings::map_y, and min().

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 nonlinearly 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, lowlands, 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.
Helper structure to index the different curve maps.
< The length of the curve map.
< The actual curve map.
Definition at line 539 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 349 of file tgp.cpp.
Referenced by GenerateTerrainPerlin().

static 
Height map terraform post processing:
Definition at line 866 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.
Definition at line 950 of file tgp.cpp.
Referenced by HeightMapAdjustWaterLevel().

inlinestatic 
Generates new random height in given amplitude (generated numbers will range from  amplitude to + amplitude)
rMax  Limit of result 
Definition at line 336 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 218 of file tgp.cpp.
References _settings_game, GameSettings::construction, GameSettings::difficulty, I2H, MapLogX(), MapLogY(), ConstructionSettings::max_heightlevel, MAX_MAP_SIZE_BITS, min(), MIN_MAP_SIZE_BITS, and DifficultySettings::terrain_type.
Referenced by GetAmplitude(), HeightMapCurves(), and HeightMapNormalize().

static 
Maximum number of TGP noise frequencies.
Definition at line 207 of file tgp.cpp.
Referenced by GetAmplitude().