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/alloc_func.hpp"
#include "core/random_func.hpp"
#include "landscape_type.h"
#include "tgp.h"
#include "debug.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... | |
struct | control_point_t |
struct | control_point_list_t |
struct | MHScontrol_point_t |
Applies variation to the map if AllowMoreHeightlevels() is true and the variety setting is not set to none. More... | |
Defines | |
#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. | |
Typedefs | |
typedef int16 | height_t |
Fixed point type for heights. | |
typedef int | amplitude_t |
Fixed point array for amplitudes (and percent values). | |
Functions | |
static bool | IsValidXY (uint x, uint 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_t | RandomHeight (amplitude_t rMax) |
Generates new random height in given amplitude (generated numbers will range from - amplitude to + amplitude). | |
static bool | ApplyNoise (uint log_frequency, amplitude_t amplitude) |
One interpolation and noise round. | |
void | Smooth (uint log_frequency, int radius, int weakness, int min_step) |
This function prevents inverted pyramids on map generation by applying some extra smoothing. | |
static void | HeightMapGenerate () |
Base Perlin noise generator - fills height map with raw Perlin noise. | |
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) |
static void | MHSHeightMapCurves () |
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. | |
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. | |
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. | |
static int | TGPGetMaxAllowedHeight () |
Gets the maximum allowed height while generating a map based on mapsize, terrain_type and the setting to allow moreheightlevels. | |
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 height_t | _invalid_height = -32768 |
static const int | amplitude_decimal_bits = 10 |
static HeightMap | _height_map = {NULL, 0, 0, 0, 0} |
Global height map instance. | |
static const amplitude_t | _amplitudes_by_smoothness_and_frequency [7][TGP_FREQUENCY_MAX+1] |
Noise amplitudes(multiplied by 1024). | |
static const int | _smoothing_parameters [7][3] |
Array holding some extra smoothing parameters that are applied after _amplitudes_and_frequency. | |
static const amplitude_t | _water_percent [4] = {70, 170, 270, 420} |
Desired water percentage (100% == 1024) - indexed by _settings_game.difficulty.quantity_sea_lakes. | |
static const int | _max_height [7][9] |
Desired maximum height - indexed by _settings_game.difficulty.terrain_type and the map size. | |
static const control_point_t | _curve_map_1 [] |
static const control_point_t | _curve_map_2 [] |
static const control_point_t | _curve_map_3 [] |
static const control_point_t | _curve_map_4 [] |
static const control_point_list_t | _curve_maps [] |
static const MHScontrol_point_t | _curve_map [] |
OTTD Perlin Noise Landscape Generator, aka TerraGenesis Perlin.
Definition in file tgp.cpp.
static bool AllocHeightMap | ( | ) | [inline, static] |
Allocate array of (MapSizeX()+1)*(MapSizeY()+1) heights and init the _height_map structure members.
Definition at line 294 of file tgp.cpp.
References FOR_ALL_TILES_IN_HEIGHT, MapSizeX(), and MapSizeY().
Referenced by GenerateTerrainPerlin().
static bool ApplyNoise | ( | uint | log_frequency, | |
amplitude_t | amplitude | |||
) | [static] |
One interpolation and noise round.
The heights on the map are generated in an iterative process. We start off with a frequency of 1 (log_frequency == 0), and generate heights only for corners on the most coarsly mesh (i.e. only for x/y coordinates which are multiples of the minimum edge length).
After this initial step the frequency is doubled (log_frequency incremented) each iteration to generate corners on the next finer mesh. The heights of the newly added corners are first set by interpolating the heights from the previous iteration. Finally noise with the given amplitude is applied to all corners of the new mesh.
Generation terminates, when the frequency has reached the map size. I.e. the mesh is as fine as the map, and every corner height has been set.
log_frequency | frequency (logarithmic) to apply noise for | |
amplitude | Amplitude for the noise |
Definition at line 352 of file tgp.cpp.
References HeightMap::height(), min(), and RandomHeight().
Referenced by HeightMapGenerate().
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 1192 of file tgp.cpp.
References _settings_game, AllocHeightMap(), GameSettings::construction, ConstructionSettings::freeform_edges, FreeHeightMap(), GenerateWorldSetAbortCallback(), GetMaxTileHeight(), GWP_LANDSCAPE, H2I, HeightMap::height(), HeightMapGenerate(), HeightMapNormalize(), IncreaseGeneratingWorldProgress(), MakeVoid(), TgenSetTileHeight(), TGPGetMaxAllowedHeight(), and TileXY().
Referenced by GenerateLandscape().
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 doesnt 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 892 of file tgp.cpp.
References _settings_game, abs(), GameSettings::game_creation, HasBit(), HeightMap::height(), GameCreationSettings::map_x, GameCreationSettings::map_y, max(), min(), and perlin_coast_noise_2D().
Referenced by HeightMapNormalize().
static void HeightMapSmoothCoastInDirection | ( | int | org_x, | |
int | org_y, | |||
int | dir_x, | |||
int | dir_y | |||
) | [static] |
Start at given point, move in given direction, find and Smooth coast in that direction.
Definition at line 952 of file tgp.cpp.
References _settings_game, GameSettings::game_creation, HeightMap::height(), IsValidXY(), min(), and GameCreationSettings::tgen_smoothness.
Referenced by HeightMapSmoothCoasts().
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 1017 of file tgp.cpp.
References HeightMap::height(), and min().
Referenced by HeightMapNormalize().
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 1107 of file tgp.cpp.
References _settings_game, GameSettings::game_creation, and GameCreationSettings::generation_seed.
Referenced by interpolated_noise().
static bool IsValidXY | ( | uint | x, | |
uint | y | |||
) | [inline, static] |
Check if a X/Y set are within the map.
x | coordinate x | |
y | coordinate y |
Definition at line 285 of file tgp.cpp.
Referenced by HeightMapSmoothCoastInDirection().
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 1157 of file tgp.cpp.
References interpolated_noise().
Referenced by HeightMapCoastLines().
static height_t RandomHeight | ( | amplitude_t | rMax | ) | [inline, static] |
Generates new random height in given amplitude (generated numbers will range from - amplitude to + amplitude).
rMax | Limit of result |
Definition at line 325 of file tgp.cpp.
References A2H.
Referenced by ApplyNoise().
void Smooth | ( | uint | log_frequency, | |
int | radius, | |||
int | weakness, | |||
int | min_step | |||
) |
This function prevents inverted pyramids on map generation by applying some extra smoothing.
log_frequency | what round of mapgeneration are we in? | |
radius | The radius to use for the extra smoothing. | |
weakness | The strongness / weakness for the extra smoothing. | |
min_step | The step in tiles before I apply the extra smoothing again. |
Definition at line 418 of file tgp.cpp.
References HeightMap::height(), and min().
Referenced by HeightMapGenerate().
const amplitude_t _amplitudes_by_smoothness_and_frequency[7][TGP_FREQUENCY_MAX+1] [static] |
{ { 2143, 27832, 51429, 51657, 41657, 51429, 1597, 233, 37, 16}, { 6448, 911, 27832, 31131, 16511, 1427, 1597, 233, 29, 8}, {17657, 29832, 36832, 51429, 16511, 51429, 233, 1597, 1301, 16}, {17657, 6448, 36832, 51429, 16511, 17657, 233, 1597, 1301, 16}, { 17657, 31832, 39832, 29832, 16511, 43429, 233, 2597, 37, 16}, {25000, 10000, 8000, 24000, 12000, 2500, 250, 100, 80, 25}, {13511, 22739, 51429, 253, 421, 21131, 137, 7, 37, 261} }
Noise amplitudes(multiplied by 1024).
Definition at line 213 of file tgp.cpp.
Referenced by HeightMapGenerate().
const MHScontrol_point_t _curve_map[] [static] |
const control_point_t _curve_map_1[] [static] |
const control_point_t _curve_map_2[] [static] |
const control_point_t _curve_map_3[] [static] |
const control_point_t _curve_map_4[] [static] |
const control_point_list_t _curve_maps[] [static] |
const int _max_height[7][9] [static] |
{ {32, 48, 72, 170, 340, 680, 1360, 2720, 5440}, {21, 40, 50, 64, 128, 380, 760, 1520, 3040}, {16, 27, 38, 51, 102, 204, 408, 916, 1832}, {12, 21, 30, 39, 60, 120, 240, 480, 960}, {10, 13, 17, 20, 33, 66, 132, 264, 528}, {9, 10, 11, 12, 13, 24, 48, 96, 192}, {5, 6, 7, 7, 7, 12, 24, 48, 96} }
Desired maximum height - indexed by _settings_game.difficulty.terrain_type and the map size.
Sense of indexing by mapsize too: Below, h_max_new is calculated as min(map_size_x, map_size_y) / the constant defined here. Later, for calculating the height of some individual tile, the formula: h_max_new * (h - h_waterlevel) / (h_max - h_waterlevel), where h_max is the maximal heightlevel existing anywhere on the map at the time of that computation step. So, to summarize: For mapsize 64x64, the above division gives 1. And, h - h_waterlevel is always smaller than h_max - h_waterlevel. So, the result is a map consisting just of water
Definition at line 269 of file tgp.cpp.
Referenced by TGPGetMaxAllowedHeight().
const int _smoothing_parameters[7][3] [static] |
{ {3, 3, 8}, {4, 3, 8}, {2, 7, 8}, {2, 4, 8}, {2, 5, 8}, {2, 6, 8}, {2, 5, 4} }
Array holding some extra smoothing parameters that are applied after _amplitudes_and_frequency.
They represent in order of appearance : { smoothing radius, power of smoothing (1= smoothest, 10 = almost unsmoothed), min. smoothing step size }
Definition at line 238 of file tgp.cpp.
Referenced by HeightMapGenerate().