OpenTTD
Data Structures | Macros | Typedefs | Functions | Variables
tgp.cpp File Reference

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 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 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.
 

Detailed Description

OTTD Perlin Noise Landscape Generator, aka TerraGenesis Perlin.

Definition in file tgp.cpp.

Function Documentation

◆ AllocHeightMap()

static bool AllocHeightMap ( )
inlinestatic

Allocate array of (MapSizeX()+1)*(MapSizeY()+1) heights and init the _height_map structure members.

Returns
true on success

Definition at line 306 of file tgp.cpp.

References FOR_ALL_TILES_IN_HEIGHT, MapSizeX(), and MapSizeY().

Referenced by GenerateTerrainPerlin().

◆ 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().

◆ GetAmplitude()

static amplitude_t GetAmplitude ( int  frequency)
static

Get the amplitude associated with the currently selected smoothness and maximum height level.

Parameters
frequencyThe frequency to get the amplitudes for
Returns
The amplitudes to apply to the map.

< 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().

◆ HeightMapCoastLines()

static void HeightMapCoastLines ( uint8  water_borders)
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 728 of file tgp.cpp.

References _settings_game, GameSettings::game_creation, GameCreationSettings::map_x, GameCreationSettings::map_y, and min().

◆ HeightMapCurves()

static void HeightMapCurves ( uint  level)
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.

Parameters
levelRough 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().

◆ HeightMapGenerate()

static void HeightMapGenerate ( )
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().

◆ HeightMapNormalize()

static void HeightMapNormalize ( )
static

◆ HeightMapSmoothSlopes()

static void HeightMapSmoothSlopes ( height_t  dh_max)
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 843 of file tgp.cpp.

◆ int_noise()

static double int_noise ( const long  x,
const long  y,
const int  prime 
)
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 900 of file tgp.cpp.

◆ IsValidXY()

static bool IsValidXY ( int  x,
int  y 
)
inlinestatic

Check if a X/Y set are within the map.

Parameters
xcoordinate x
ycoordinate y
Returns
true if within the map

Definition at line 296 of file tgp.cpp.

◆ perlin_coast_noise_2D()

static double perlin_coast_noise_2D ( const double  x,
const double  y,
const double  p,
const int  prime 
)
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().

◆ RandomHeight()

static height_t RandomHeight ( amplitude_t  rMax)
inlinestatic

Generates new random height in given amplitude (generated numbers will range from - amplitude to + amplitude)

Parameters
rMaxLimit of result
Returns
generated height

Definition at line 336 of file tgp.cpp.

References A2H, and RandomRange().

◆ TGPGetMaxHeight()

static height_t TGPGetMaxHeight ( )
static

Gets the maximum allowed height while generating a map based on mapsize, terraintype, and the maximum height level.

Returns
The maximum height for the map generation.
Note
Values should never be lower than 3 since the minimum snowline height is 2.

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().

Variable Documentation

◆ MAX_TGP_FREQUENCIES

const int MAX_TGP_FREQUENCIES = 10
static

Maximum number of TGP noise frequencies.

Definition at line 207 of file tgp.cpp.

Referenced by GetAmplitude().