truelight@4300: /* $Id$ */ truelight@4300: belugas@6422: /** @file tgp.cpp OTTD Perlin Noise Landscape Generator, aka TerraGenesis Perlin */ belugas@6422: truelight@4300: #include "stdafx.h" truelight@4300: #include truelight@4300: #include "openttd.h" truelight@4300: #include "clear_map.h" truelight@4300: #include "clear_map.h" truelight@4300: #include "variables.h" truelight@4300: #include "void_map.h" truelight@4300: #include "tgp.h" truelight@4300: #include "genworld.h" rubidium@8130: #include "core/alloc_func.hpp" rubidium@8131: #include "core/random_func.hpp" rubidium@8270: #include "settings_type.h" rubidium@9126: #include "landscape_type.h" truelight@4300: rubidium@8264: #include "table/strings.h" rubidium@8264: truelight@4300: /* truelight@4300: * truelight@4300: * Quickie guide to Perlin Noise truelight@4300: * Perlin noise is a predictable pseudo random number sequence. By generating truelight@4300: * it in 2 dimensions, it becomes a useful random map, that for a given seed truelight@4300: * and starting X & Y is entirely predictable. On the face of it, that may not truelight@4300: * be useful. However, it means that if you want to replay a map in a different truelight@4300: * terrain, or just vary the sea level, you just re-run the generator with the truelight@4300: * same seed. The seed is an int32, and is randomised on each run of New Game. truelight@4300: * The Scenario Generator does not randomise the value, so that you can truelight@4300: * experiment with one terrain until you are happy, or click "Random" for a new truelight@4300: * random seed. truelight@4300: * truelight@4300: * Perlin Noise is a series of "octaves" of random noise added together. By truelight@4300: * reducing the amplitude of the noise with each octave, the first octave of truelight@4300: * noise defines the main terrain sweep, the next the ripples on that, and the truelight@4300: * next the ripples on that. I use 6 octaves, with the amplitude controlled by truelight@4300: * a power ratio, usually known as a persistence or p value. This I vary by the truelight@4300: * smoothness selection, as can be seen in the table below. The closer to 1, truelight@4300: * the more of that octave is added. Each octave is however raised to the power truelight@4300: * of its position in the list, so the last entry in the "smooth" row, 0.35, is truelight@4300: * raised to the power of 6, so can only add 0.001838... of the amplitude to truelight@4300: * the running total. truelight@4300: * truelight@4300: * In other words; the first p value sets the general shape of the terrain, the truelight@4300: * second sets the major variations to that, ... until finally the smallest truelight@4300: * bumps are added. truelight@4300: * truelight@4300: * Usefully, this routine is totally scaleable; so when 32bpp comes along, the truelight@4300: * terrain can be as bumpy as you like! It is also infinitely expandable; a truelight@4300: * single random seed terrain continues in X & Y as far as you care to truelight@4300: * calculate. In theory, we could use just one seed value, but randomly select truelight@4300: * where in the Perlin XY space we use for the terrain. Personally I prefer truelight@4300: * using a simple (0, 0) to (X, Y), with a varying seed. truelight@4300: * truelight@4300: * truelight@4300: * Other things i have had to do: mountainous wasnt mountainous enough, and truelight@4300: * since we only have 0..15 heights available, I add a second generated map truelight@4300: * (with a modified seed), onto the original. This generally raises the truelight@4300: * terrain, which then needs scaling back down. Overall effect is a general truelight@4300: * uplift. truelight@4300: * truelight@4300: * However, the values on the top of mountains are then almost guaranteed to go truelight@4300: * too high, so large flat plateaus appeared at height 15. To counter this, I truelight@4300: * scale all heights above 12 to proportion up to 15. It still makes the truelight@4300: * mountains have flatish tops, rather than craggy peaks, but at least they truelight@4300: * arent smooth as glass. truelight@4300: * truelight@4300: * truelight@4300: * For a full discussion of Perlin Noise, please visit: truelight@4300: * http://freespace.virgin.net/hugo.elias/models/m_perlin.htm truelight@4300: * truelight@4300: * truelight@4300: * Evolution II truelight@4300: * truelight@4300: * The algorithm as described in the above link suggests to compute each tile height truelight@4300: * as composition of several noise waves. Some of them are computed directly by truelight@4300: * noise(x, y) function, some are calculated using linear approximation. Our truelight@4300: * first implementation of perlin_noise_2D() used 4 noise(x, y) calls plus truelight@4300: * 3 linear interpolations. It was called 6 times for each tile. This was a bit truelight@4300: * CPU expensive. truelight@4300: * truelight@4300: * The following implementation uses optimized algorithm that should produce truelight@4300: * the same quality result with much less computations, but more memory accesses. truelight@4300: * The overal speedup should be 300% to 800% depending on CPU and memory speed. truelight@4300: * truelight@4300: * I will try to explain it on the example below: truelight@4300: * truelight@4300: * Have a map of 4 x 4 tiles, our simplifiead noise generator produces only two truelight@4300: * values -1 and +1, use 3 octaves with wave lenght 1, 2 and 4, with amplitudes truelight@4300: * 3, 2, 1. Original algorithm produces: truelight@4300: * truelight@4300: * h00 = lerp(lerp(-3, 3, 0/4), lerp(3, -3, 0/4), 0/4) + lerp(lerp(-2, 2, 0/2), lerp( 2, -2, 0/2), 0/2) + -1 = lerp(-3.0, 3.0, 0/4) + lerp(-2, 2, 0/2) + -1 = -3.0 + -2 + -1 = -6.0 truelight@4300: * h01 = lerp(lerp(-3, 3, 1/4), lerp(3, -3, 1/4), 0/4) + lerp(lerp(-2, 2, 1/2), lerp( 2, -2, 1/2), 0/2) + 1 = lerp(-1.5, 1.5, 0/4) + lerp( 0, 0, 0/2) + 1 = -1.5 + 0 + 1 = -0.5 truelight@4300: * h02 = lerp(lerp(-3, 3, 2/4), lerp(3, -3, 2/4), 0/4) + lerp(lerp( 2, -2, 0/2), lerp(-2, 2, 0/2), 0/2) + -1 = lerp( 0, 0, 0/4) + lerp( 2, -2, 0/2) + -1 = 0 + 2 + -1 = 1.0 truelight@4300: * h03 = lerp(lerp(-3, 3, 3/4), lerp(3, -3, 3/4), 0/4) + lerp(lerp( 2, -2, 1/2), lerp(-2, 2, 1/2), 0/2) + 1 = lerp( 1.5, -1.5, 0/4) + lerp( 0, 0, 0/2) + 1 = 1.5 + 0 + 1 = 2.5 truelight@4300: * truelight@4300: * h10 = lerp(lerp(-3, 3, 0/4), lerp(3, -3, 0/4), 1/4) + lerp(lerp(-2, 2, 0/2), lerp( 2, -2, 0/2), 1/2) + 1 = lerp(-3.0, 3.0, 1/4) + lerp(-2, 2, 1/2) + 1 = -1.5 + 0 + 1 = -0.5 truelight@4300: * h11 = lerp(lerp(-3, 3, 1/4), lerp(3, -3, 1/4), 1/4) + lerp(lerp(-2, 2, 1/2), lerp( 2, -2, 1/2), 1/2) + -1 = lerp(-1.5, 1.5, 1/4) + lerp( 0, 0, 1/2) + -1 = -0.75 + 0 + -1 = -1.75 truelight@4300: * h12 = lerp(lerp(-3, 3, 2/4), lerp(3, -3, 2/4), 1/4) + lerp(lerp( 2, -2, 0/2), lerp(-2, 2, 0/2), 1/2) + 1 = lerp( 0, 0, 1/4) + lerp( 2, -2, 1/2) + 1 = 0 + 0 + 1 = 1.0 truelight@4300: * h13 = lerp(lerp(-3, 3, 3/4), lerp(3, -3, 3/4), 1/4) + lerp(lerp( 2, -2, 1/2), lerp(-2, 2, 1/2), 1/2) + -1 = lerp( 1.5, -1.5, 1/4) + lerp( 0, 0, 1/2) + -1 = 0.75 + 0 + -1 = -0.25 truelight@4300: * truelight@4300: * truelight@4300: * Optimization 1: truelight@4300: * truelight@4300: * 1) we need to allocate a bit more tiles: (size_x + 1) * (size_y + 1) = (5 * 5): truelight@4300: * truelight@4300: * 2) setup corner values using amplitude 3 truelight@4300: * { -3.0 X X X 3.0 } truelight@4300: * { X X X X X } truelight@4300: * { X X X X X } truelight@4300: * { X X X X X } truelight@4300: * { 3.0 X X X -3.0 } truelight@4300: * truelight@4300: * 3a) interpolate values in the middle truelight@4300: * { -3.0 X 0.0 X 3.0 } truelight@4300: * { X X X X X } truelight@4300: * { 0.0 X 0.0 X 0.0 } truelight@4300: * { X X X X X } truelight@4300: * { 3.0 X 0.0 X -3.0 } truelight@4300: * truelight@4300: * 3b) add patches with amplitude 2 to them truelight@4300: * { -5.0 X 2.0 X 1.0 } truelight@4300: * { X X X X X } truelight@4300: * { 2.0 X -2.0 X 2.0 } truelight@4300: * { X X X X X } truelight@4300: * { 1.0 X 2.0 X -5.0 } truelight@4300: * truelight@4300: * 4a) interpolate values in the middle truelight@4300: * { -5.0 -1.5 2.0 1.5 1.0 } truelight@4300: * { -1.5 -0.75 0.0 0.75 1.5 } truelight@4300: * { 2.0 0.0 -2.0 0.0 2.0 } truelight@4300: * { 1.5 0.75 0.0 -0.75 -1.5 } truelight@4300: * { 1.0 1.5 2.0 -1.5 -5.0 } truelight@4300: * truelight@4300: * 4b) add patches with amplitude 1 to them truelight@4300: * { -6.0 -0.5 1.0 2.5 0.0 } truelight@4300: * { -0.5 -1.75 1.0 -0.25 2.5 } truelight@4300: * { 1.0 1.0 -3.0 1.0 1.0 } truelight@4300: * { 2.5 -0.25 1.0 -1.75 -0.5 } truelight@4300: * { 0.0 2.5 1.0 -0.5 -6.0 } truelight@4300: * truelight@4300: * truelight@4300: * truelight@4300: * Optimization 2: truelight@4300: * truelight@4300: * As you can see above, each noise function was called just once. Therefore truelight@4300: * we don't need to use noise function that calculates the noise from x, y and truelight@4300: * some prime. The same quality result we can obtain using standard Random() truelight@4300: * function instead. truelight@4300: * truelight@4300: */ truelight@4300: truelight@4300: #ifndef M_PI_2 truelight@4300: #define M_PI_2 1.57079632679489661923 truelight@4300: #define M_PI 3.14159265358979323846 truelight@4300: #endif /* M_PI_2 */ truelight@4300: truelight@4300: /** Fixed point type for heights */ truelight@4300: typedef int16 height_t; truelight@4300: static const int height_decimal_bits = 4; truelight@4300: static const height_t _invalid_height = -32768; truelight@4300: truelight@4300: /** Fixed point array for amplitudes (and percent values) */ truelight@4300: typedef int amplitude_t; truelight@4300: static const int amplitude_decimal_bits = 10; truelight@4300: truelight@4300: /** Height map - allocated array of heights (MapSizeX() + 1) x (MapSizeY() + 1) */ rubidium@6248: struct HeightMap truelight@4300: { belugas@6422: height_t *h; //< array of heights belugas@6422: uint dim_x; //< height map size_x MapSizeX() + 1 belugas@6422: uint total_size; //< height map total size belugas@6422: uint size_x; //< MapSizeX() belugas@6422: uint size_y; //< MapSizeY() frosch@9603: frosch@9613: /** frosch@9613: * Height map accessor frosch@9613: * @param x X position frosch@9613: * @param y Y position frosch@9613: * @return height as fixed point number frosch@9613: */ frosch@9603: inline height_t &height(uint x, uint y) { frosch@9603: return h[x + y * dim_x]; frosch@9603: } rubidium@6248: }; truelight@4300: truelight@4300: /** Global height map instance */ truelight@4300: static HeightMap _height_map = {NULL, 0, 0, 0, 0}; truelight@4300: truelight@4300: /** Conversion: int to height_t */ truelight@4300: #define I2H(i) ((i) << height_decimal_bits) truelight@4300: /** Conversion: height_t to int */ truelight@4300: #define H2I(i) ((i) >> height_decimal_bits) truelight@4300: truelight@4300: /** Conversion: int to amplitude_t */ truelight@4300: #define I2A(i) ((i) << amplitude_decimal_bits) truelight@4300: /** Conversion: amplitude_t to int */ truelight@4300: #define A2I(i) ((i) >> amplitude_decimal_bits) truelight@4300: truelight@4300: /** Conversion: amplitude_t to height_t */ rubidium@5587: #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits)) rubidium@5587: truelight@4300: truelight@4300: /** Walk through all items of _height_map.h */ truelight@4300: #define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++) truelight@4300: frosch@9615: /** Maximum index into array of noise amplitudes */ frosch@9615: static const int TGP_FREQUENCY_MAX = 6; frosch@9615: truelight@4300: /** Noise amplitudes (multiplied by 1024) rubidium@4434: * - indexed by "smoothness setting" and log2(frequency) */ frosch@9615: static const amplitude_t _amplitudes_by_smoothness_and_frequency[4][TGP_FREQUENCY_MAX + 1] = { frosch@9615: /* lowest frequncy.... ...highest (every corner) */ belugas@6422: /* Very smooth */ frosch@9615: {16000, 5600, 1968, 688, 240, 16, 16}, belugas@6422: /* Smooth */ frosch@9615: {16000, 16000, 6448, 3200, 1024, 128, 16}, belugas@6422: /* Rough */ frosch@9615: {16000, 19200, 12800, 8000, 3200, 256, 64}, belugas@6422: /* Very Rough */ frosch@9615: {24000, 16000, 19200, 16000, 8000, 512, 320}, truelight@4300: }; truelight@4300: rubidium@9413: /** Desired water percentage (100% == 1024) - indexed by _settings_game.difficulty.quantity_sea_lakes */ truelight@4300: static const amplitude_t _water_percent[4] = {20, 80, 250, 400}; truelight@4300: rubidium@9413: /** Desired maximum height - indexed by _settings_game.difficulty.terrain_type */ truelight@4300: static const int8 _max_height[4] = { belugas@6422: 6, ///< Very flat belugas@6422: 9, ///< Flat belugas@6422: 12, ///< Hilly belugas@6422: 15 ///< Mountainous truelight@4300: }; truelight@4300: belugas@6422: /** Check if a X/Y set are within the map. belugas@6422: * @param x coordinate x belugas@6422: * @param y coordinate y belugas@6422: * @return true if within the map belugas@6422: */ truelight@4300: static inline bool IsValidXY(uint x, uint y) truelight@4300: { truelight@4300: return ((int)x) >= 0 && x < _height_map.size_x && ((int)y) >= 0 && y < _height_map.size_y; truelight@4300: } truelight@4300: truelight@4300: frosch@9613: /** frosch@9613: * Allocate array of (MapSizeX()+1)*(MapSizeY()+1) heights and init the _height_map structure members frosch@9613: * @return true on success frosch@9613: */ rubidium@6247: static inline bool AllocHeightMap() truelight@4300: { truelight@4300: height_t *h; truelight@4300: truelight@4300: _height_map.size_x = MapSizeX(); truelight@4300: _height_map.size_y = MapSizeY(); truelight@4300: truelight@4300: /* Allocate memory block for height map row pointers */ truelight@4300: _height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1); truelight@4300: _height_map.dim_x = _height_map.size_x + 1; KUDr@5609: _height_map.h = CallocT(_height_map.total_size); truelight@4300: if (_height_map.h == NULL) return false; truelight@4300: truelight@4300: /* Iterate through height map initialize values */ truelight@4300: FOR_ALL_TILES_IN_HEIGHT(h) *h = _invalid_height; truelight@4300: truelight@4300: return true; truelight@4300: } truelight@4300: truelight@4300: /** Free height map */ rubidium@6247: static inline void FreeHeightMap() truelight@4300: { truelight@4300: if (_height_map.h == NULL) return; truelight@4300: free(_height_map.h); truelight@4300: _height_map.h = NULL; truelight@4300: } truelight@4300: frosch@9613: /** frosch@9613: * Generates new random height in given amplitude (generated numbers will range from - amplitude to + amplitude) frosch@9613: * @param rMax Limit of result frosch@9613: * @return generated height frosch@9613: */ truelight@4300: static inline height_t RandomHeight(amplitude_t rMax) truelight@4300: { truelight@4300: amplitude_t ra = (Random() << 16) | (Random() & 0x0000FFFF); truelight@4300: height_t rh; truelight@4300: /* Spread height into range -rMax..+rMax */ truelight@4300: rh = A2H(ra % (2 * rMax + 1) - rMax); truelight@4300: return rh; truelight@4300: } truelight@4300: frosch@9613: /** frosch@9613: * One interpolation and noise round frosch@9613: * frosch@9613: * The heights on the map are generated in an iterative process. frosch@9613: * We start off with a frequency of 1 (log_frequency == 0), and generate heights only for corners on the most coarsly mesh frosch@9613: * (i.e. only for x/y coordinates which are multiples of the minimum edge length). frosch@9613: * frosch@9613: * After this initial step the frequency is doubled (log_frequency incremented) each iteration to generate corners on the next finer mesh. frosch@9613: * The heights of the newly added corners are first set by interpolating the heights from the previous iteration. frosch@9613: * Finally noise with the given amplitude is applied to all corners of the new mesh. frosch@9613: * frosch@9613: * Generation terminates, when the frequency has reached the map size. I.e. the mesh is as fine as the map, and every corner height frosch@9613: * has been set. frosch@9613: * frosch@9613: * @param log_frequency frequency (logarithmic) to apply noise for frosch@9613: * @param amplitude Amplitude for the noise frosch@9613: * @return false if we are finished (reached the minimal step size / highest frequency) frosch@9613: */ truelight@4300: static bool ApplyNoise(uint log_frequency, amplitude_t amplitude) truelight@4300: { truelight@4300: uint size_min = min(_height_map.size_x, _height_map.size_y); truelight@4300: uint step = size_min >> log_frequency; truelight@4300: uint x, y; truelight@4300: frosch@9613: /* Trying to apply noise to uninitialized height map */ truelight@4300: assert(_height_map.h != NULL); truelight@4300: truelight@4300: /* Are we finished? */ truelight@4300: if (step == 0) return false; truelight@4300: truelight@4300: if (log_frequency == 0) { truelight@4300: /* This is first round, we need to establish base heights with step = size_min */ truelight@4300: for (y = 0; y <= _height_map.size_y; y += step) { truelight@4300: for (x = 0; x <= _height_map.size_x; x += step) { truelight@4300: height_t height = (amplitude > 0) ? RandomHeight(amplitude) : 0; frosch@9603: _height_map.height(x, y) = height; truelight@4300: } truelight@4300: } truelight@4300: return true; truelight@4300: } truelight@4300: truelight@4300: /* It is regular iteration round. truelight@4300: * Interpolate height values at odd x, even y tiles */ truelight@4300: for (y = 0; y <= _height_map.size_y; y += 2 * step) { truelight@4300: for (x = 0; x < _height_map.size_x; x += 2 * step) { frosch@9603: height_t h00 = _height_map.height(x + 0 * step, y); frosch@9603: height_t h02 = _height_map.height(x + 2 * step, y); truelight@4300: height_t h01 = (h00 + h02) / 2; frosch@9603: _height_map.height(x + 1 * step, y) = h01; truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /* Interpolate height values at odd y tiles */ truelight@4300: for (y = 0; y < _height_map.size_y; y += 2 * step) { truelight@4300: for (x = 0; x <= _height_map.size_x; x += step) { frosch@9603: height_t h00 = _height_map.height(x, y + 0 * step); frosch@9603: height_t h20 = _height_map.height(x, y + 2 * step); truelight@4300: height_t h10 = (h00 + h20) / 2; frosch@9603: _height_map.height(x, y + 1 * step) = h10; truelight@4300: } truelight@4300: } truelight@4300: frosch@9613: /* Add noise for next higher frequency (smaller steps) */ truelight@4300: for (y = 0; y <= _height_map.size_y; y += step) { truelight@4300: for (x = 0; x <= _height_map.size_x; x += step) { frosch@9603: _height_map.height(x, y) += RandomHeight(amplitude); truelight@4300: } truelight@4300: } frosch@9613: truelight@4300: return (step > 1); truelight@4300: } truelight@4300: truelight@4300: /** Base Perlin noise generator - fills height map with raw Perlin noise */ rubidium@6247: static void HeightMapGenerate() truelight@4300: { truelight@4300: uint size_min = min(_height_map.size_x, _height_map.size_y); truelight@4300: uint iteration_round = 0; truelight@4300: amplitude_t amplitude; truelight@4300: bool continue_iteration; frosch@9615: int log_size_min, log_frequency_min; truelight@4300: int log_frequency; truelight@4300: frosch@9615: /* Find first power of two that fits, so that later log_frequency == TGP_FREQUENCY_MAX in the last iteration */ terom@10440: for (log_size_min = 2; (1U << log_size_min) < size_min; log_size_min++) { } terom@10440: log_frequency_min = log_size_min - 2; frosch@9615: frosch@9615: /* Zero must be part of the iteration, else initialization will fail. */ frosch@9615: assert(log_frequency_min >= 0); truelight@4300: frosch@9613: /* Keep increasing the frequency until we reach the step size equal to one tile */ truelight@4300: do { truelight@4300: log_frequency = iteration_round - log_frequency_min; truelight@4300: if (log_frequency >= 0) { frosch@9613: /* Apply noise for the next frequency */ terom@10440: assert(log_frequency <= 2); rubidium@9413: amplitude = _amplitudes_by_smoothness_and_frequency[_settings_game.game_creation.tgen_smoothness][log_frequency]; truelight@4300: } else { frosch@9613: /* Amplitude for the low frequencies on big maps is 0, i.e. initialise with zero height */ truelight@4300: amplitude = 0; truelight@4300: } truelight@4300: continue_iteration = ApplyNoise(iteration_round, amplitude); truelight@4300: iteration_round++; frosch@9613: } while (continue_iteration); terom@10440: terom@10440: assert(log_frequency == 2); truelight@4300: } truelight@4300: truelight@4300: /** Returns min, max and average height from height map */ truelight@4300: static void HeightMapGetMinMaxAvg(height_t *min_ptr, height_t *max_ptr, height_t *avg_ptr) truelight@4300: { truelight@4300: height_t h_min, h_max, h_avg, *h; truelight@4300: int64 h_accu = 0; frosch@9603: h_min = h_max = _height_map.height(0, 0); truelight@4300: truelight@4300: /* Get h_min, h_max and accumulate heights into h_accu */ truelight@4300: FOR_ALL_TILES_IN_HEIGHT(h) { truelight@4300: if (*h < h_min) h_min = *h; truelight@4300: if (*h > h_max) h_max = *h; truelight@4300: h_accu += *h; truelight@4300: } truelight@4300: truelight@4300: /* Get average height */ truelight@4300: h_avg = (height_t)(h_accu / (_height_map.size_x * _height_map.size_y)); truelight@4300: truelight@4300: /* Return required results */ truelight@4300: if (min_ptr != NULL) *min_ptr = h_min; truelight@4300: if (max_ptr != NULL) *max_ptr = h_max; truelight@4300: if (avg_ptr != NULL) *avg_ptr = h_avg; truelight@4300: } truelight@4300: truelight@4300: /** Dill histogram and return pointer to its base point - to the count of zero heights */ truelight@4300: static int *HeightMapMakeHistogram(height_t h_min, height_t h_max, int *hist_buf) truelight@4300: { truelight@4300: int *hist = hist_buf - h_min; truelight@4300: height_t *h; truelight@4300: frosch@9613: /* Count the heights and fill the histogram */ truelight@4300: FOR_ALL_TILES_IN_HEIGHT(h) { truelight@4300: assert(*h >= h_min); truelight@4300: assert(*h <= h_max); truelight@4300: hist[*h]++; truelight@4300: } truelight@4300: return hist; truelight@4300: } truelight@4300: truelight@4300: /** Applies sine wave redistribution onto height map */ truelight@4300: static void HeightMapSineTransform(height_t h_min, height_t h_max) truelight@4300: { truelight@4300: height_t *h; truelight@4300: truelight@4300: FOR_ALL_TILES_IN_HEIGHT(h) { truelight@4300: double fheight; truelight@4300: truelight@4300: if (*h < h_min) continue; truelight@4300: truelight@4300: /* Transform height into 0..1 space */ truelight@4300: fheight = (double)(*h - h_min) / (double)(h_max - h_min); truelight@4300: /* Apply sine transform depending on landscape type */ rubidium@9413: switch(_settings_game.game_creation.landscape) { belugas@6357: case LT_TOYLAND: belugas@6357: case LT_TEMPERATE: truelight@4300: /* Move and scale 0..1 into -1..+1 */ truelight@4300: fheight = 2 * fheight - 1; truelight@4300: /* Sine transform */ truelight@4300: fheight = sin(fheight * M_PI_2); truelight@4300: /* Transform it back from -1..1 into 0..1 space */ truelight@4300: fheight = 0.5 * (fheight + 1); truelight@4300: break; truelight@4300: belugas@6357: case LT_ARCTIC: truelight@4300: { truelight@4300: /* Arctic terrain needs special height distribution. truelight@4300: * Redistribute heights to have more tiles at highest (75%..100%) range */ truelight@4300: double sine_upper_limit = 0.75; truelight@4300: double linear_compression = 2; truelight@4300: if (fheight >= sine_upper_limit) { truelight@4300: /* Over the limit we do linear compression up */ truelight@4300: fheight = 1.0 - (1.0 - fheight) / linear_compression; truelight@4300: } else { truelight@4300: double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression; truelight@4300: /* Get 0..sine_upper_limit into -1..1 */ truelight@4300: fheight = 2.0 * fheight / sine_upper_limit - 1.0; truelight@4300: /* Sine wave transform */ truelight@4300: fheight = sin(fheight * M_PI_2); truelight@4300: /* Get -1..1 back to 0..(1 - (1 - sine_upper_limit) / linear_compression) == 0.0..m */ truelight@4300: fheight = 0.5 * (fheight + 1.0) * m; truelight@4300: } truelight@4300: } truelight@4300: break; truelight@4300: belugas@6357: case LT_TROPIC: truelight@4300: { truelight@4300: /* Desert terrain needs special height distribution. truelight@4300: * Half of tiles should be at lowest (0..25%) heights */ truelight@4300: double sine_lower_limit = 0.5; truelight@4300: double linear_compression = 2; truelight@4300: if (fheight <= sine_lower_limit) { truelight@4300: /* Under the limit we do linear compression down */ truelight@4300: fheight = fheight / linear_compression; truelight@4300: } else { truelight@4300: double m = sine_lower_limit / linear_compression; truelight@4300: /* Get sine_lower_limit..1 into -1..1 */ truelight@4300: fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0; truelight@4300: /* Sine wave transform */ truelight@4300: fheight = sin(fheight * M_PI_2); truelight@4300: /* Get -1..1 back to (sine_lower_limit / linear_compression)..1.0 */ truelight@4300: fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m)); truelight@4300: } truelight@4300: } truelight@4300: break; truelight@4300: truelight@4300: default: truelight@4300: NOT_REACHED(); truelight@4300: break; truelight@4300: } truelight@4300: /* Transform it back into h_min..h_max space */ rubidium@5587: *h = (height_t)(fheight * (h_max - h_min) + h_min); truelight@4300: if (*h < 0) *h = I2H(0); truelight@4300: if (*h >= h_max) *h = h_max - 1; truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /** Adjusts heights in height map to contain required amount of water tiles */ truelight@4300: static void HeightMapAdjustWaterLevel(amplitude_t water_percent, height_t h_max_new) truelight@4300: { truelight@4300: height_t h_min, h_max, h_avg, h_water_level; truelight@4300: int water_tiles, desired_water_tiles; truelight@4300: height_t *h; KUDr@5609: int *hist; truelight@4300: truelight@4300: HeightMapGetMinMaxAvg(&h_min, &h_max, &h_avg); truelight@4300: truelight@4300: /* Allocate histogram buffer and clear its cells */ KUDr@5609: int *hist_buf = CallocT(h_max - h_min + 1); truelight@4300: /* Fill histogram */ truelight@4300: hist = HeightMapMakeHistogram(h_min, h_max, hist_buf); truelight@4300: truelight@4300: /* How many water tiles do we want? */ truelight@4300: desired_water_tiles = (int)(((int64)water_percent) * (int64)(_height_map.size_x * _height_map.size_y)) >> amplitude_decimal_bits; truelight@4300: truelight@4300: /* Raise water_level and accumulate values from histogram until we reach required number of water tiles */ truelight@4300: for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) { truelight@4300: water_tiles += hist[h_water_level]; truelight@4300: if (water_tiles >= desired_water_tiles) break; truelight@4300: } truelight@4300: truelight@4300: /* We now have the proper water level value. truelight@4300: * Transform the height map into new (normalized) height map: truelight@4300: * values from range: h_min..h_water_level will become negative so it will be clamped to 0 truelight@4300: * values from range: h_water_level..h_max are transformed into 0..h_max_new truelight@4300: * , where h_max_new is 4, 8, 12 or 16 depending on terrain type (very flat, flat, hilly, mountains) truelight@4300: */ truelight@4300: FOR_ALL_TILES_IN_HEIGHT(h) { truelight@4300: /* Transform height from range h_water_level..h_max into 0..h_max_new range */ truelight@4300: *h = (height_t)(((int)h_max_new) * (*h - h_water_level) / (h_max - h_water_level)) + I2H(1); truelight@4300: /* Make sure all values are in the proper range (0..h_max_new) */ truelight@4300: if (*h < 0) *h = I2H(0); truelight@4300: if (*h >= h_max_new) *h = h_max_new - 1; truelight@4300: } truelight@4300: truelight@4300: free(hist_buf); truelight@4300: } truelight@4300: truelight@4300: static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime); truelight@4300: truelight@4300: /** rubidium@4549: * This routine sculpts in from the edge a random amount, again a Perlin rubidium@4549: * sequence, to avoid the rigid flat-edge slopes that were present before. The rubidium@4549: * Perlin noise map doesnt know where we are going to slice across, and so we rubidium@4549: * often cut straight through high terrain. the smoothing routine makes it rubidium@4549: * legal, gradually increasing up from the edge to the original terrain height. rubidium@4549: * By cutting parts of this away, it gives a far more irregular edge to the rubidium@4549: * map-edge. Sometimes it works beautifully with the existing sea & lakes, and rubidium@4549: * creates a very realistic coastline. Other times the variation is less, and rubidium@4549: * the map-edge shows its cliff-like roots. rubidium@4549: * rubidium@4549: * This routine may be extended to randomly sculpt the height of the terrain rubidium@4549: * near the edge. This will have the coast edge at low level (1-3), rising in rubidium@4549: * smoothed steps inland to about 15 tiles in. This should make it look as rubidium@4549: * though the map has been built for the map size, rather than a slice through rubidium@4549: * a larger map. rubidium@4549: * rubidium@4549: * Please note that all the small numbers; 53, 101, 167, etc. are small primes rubidium@4549: * to help give the perlin noise a bit more of a random feel. rubidium@4549: */ rubidium@6247: static void HeightMapCoastLines() truelight@4300: { rubidium@9413: int smallest_size = min(_settings_game.game_creation.map_x, _settings_game.game_creation.map_y); truelight@4300: const int margin = 4; truelight@4300: uint y, x; rubidium@5587: double max_x; rubidium@5587: double max_y; truelight@4300: terom@10440: int factor = smallest_size * smallest_size / 16; terom@10440: truelight@4300: /* Lower to sea level */ truelight@4300: for (y = 0; y <= _height_map.size_y; y++) { truelight@4300: /* Top right */ skidd13@7923: max_x = abs((perlin_coast_noise_2D(_height_map.size_y - y, y, 0.9, 53) + 0.25) * 5 + (perlin_coast_noise_2D(y, y, 0.35, 179) + 1) * 12); terom@10440: max_x = max(factor + max_x, factor + margin - max_x); truelight@4300: if (smallest_size < 8 && max_x > 5) max_x /= 1.5; terom@10440: if (smallest_size < 6 && max_x > 4) max_x /= 4.0; truelight@4300: for (x = 0; x < max_x; x++) { frosch@9603: _height_map.height(x, y) = 0; truelight@4300: } truelight@4300: truelight@4300: /* Bottom left */ skidd13@7923: max_x = abs((perlin_coast_noise_2D(_height_map.size_y - y, y, 0.85, 101) + 0.3) * 6 + (perlin_coast_noise_2D(y, y, 0.45, 67) + 0.75) * 8); terom@10440: max_x = max(factor + max_x, factor + margin - max_x); truelight@4300: if (smallest_size < 8 && max_x > 5) max_x /= 1.5; terom@10440: if (smallest_size < 6 && max_x > 4) max_x /= 4.0; terom@10440: for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x) && x > 0; x--) { frosch@9603: _height_map.height(x, y) = 0; truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /* Lower to sea level */ truelight@4300: for (x = 0; x <= _height_map.size_x; x++) { truelight@4300: /* Top left */ skidd13@7923: max_y = abs((perlin_coast_noise_2D(x, _height_map.size_y / 2, 0.9, 167) + 0.4) * 5 + (perlin_coast_noise_2D(x, _height_map.size_y / 3, 0.4, 211) + 0.7) * 9); terom@10440: max_y = max(factor + max_y, factor + margin - max_y); truelight@4300: if (smallest_size < 8 && max_y > 5) max_y /= 1.5; terom@10440: if (smallest_size < 6 && max_y > 4) max_y /= 4.0; truelight@4300: for (y = 0; y < max_y; y++) { frosch@9603: _height_map.height(x, y) = 0; truelight@4300: } truelight@4300: truelight@4300: truelight@4300: /* Bottom right */ skidd13@7923: max_y = abs((perlin_coast_noise_2D(x, _height_map.size_y / 3, 0.85, 71) + 0.25) * 6 + (perlin_coast_noise_2D(x, _height_map.size_y / 3, 0.35, 193) + 0.75) * 12); terom@10440: max_y = max(factor + max_y, factor + margin - max_y); truelight@4300: if (smallest_size < 8 && max_y > 5) max_y /= 1.5; terom@10440: if (smallest_size < 6 && max_y > 4) max_y /= 4.0; terom@10440: for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y) && y > 0; y--) { frosch@9603: _height_map.height(x, y) = 0; truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /** Start at given point, move in given direction, find and Smooth coast in that direction */ truelight@4300: static void HeightMapSmoothCoastInDirection(int org_x, int org_y, int dir_x, int dir_y) truelight@4300: { truelight@4300: const int max_coast_dist_from_edge = 35; truelight@4300: const int max_coast_Smooth_depth = 35; truelight@4300: truelight@4300: int x, y; truelight@4300: int ed; // coast distance from edge truelight@4300: int depth; truelight@4300: truelight@4300: height_t h_prev = 16; truelight@4300: height_t h; truelight@4300: truelight@4300: assert(IsValidXY(org_x, org_y)); truelight@4300: truelight@4300: /* Search for the coast (first non-water tile) */ truelight@4300: for (x = org_x, y = org_y, ed = 0; IsValidXY(x, y) && ed < max_coast_dist_from_edge; x += dir_x, y += dir_y, ed++) { truelight@4300: /* Coast found? */ frosch@9603: if (_height_map.height(x, y) > 15) break; truelight@4300: truelight@4300: /* Coast found in the neighborhood? */ frosch@9603: if (IsValidXY(x + dir_y, y + dir_x) && _height_map.height(x + dir_y, y + dir_x) > 0) break; truelight@4300: truelight@4300: /* Coast found in the neighborhood on the other side */ frosch@9603: if (IsValidXY(x - dir_y, y - dir_x) && _height_map.height(x - dir_y, y - dir_x) > 0) break; truelight@4300: } truelight@4300: truelight@4300: /* Coast found or max_coast_dist_from_edge has been reached. truelight@4300: * Soften the coast slope */ truelight@4300: for (depth = 0; IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) { frosch@9603: h = _height_map.height(x, y); truelight@4300: h = min(h, h_prev + (4 + depth)); // coast softening formula frosch@9603: _height_map.height(x, y) = h; truelight@4300: h_prev = h; truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /** Smooth coasts by modulating height of tiles close to map edges with cosine of distance from edge */ rubidium@6247: static void HeightMapSmoothCoasts() truelight@4300: { truelight@4300: uint x, y; truelight@4300: /* First Smooth NW and SE coasts (y close to 0 and y close to size_y) */ truelight@4300: for (x = 0; x < _height_map.size_x; x++) { truelight@4300: HeightMapSmoothCoastInDirection(x, 0, 0, 1); truelight@4300: HeightMapSmoothCoastInDirection(x, _height_map.size_y - 1, 0, -1); truelight@4300: } truelight@4300: /* First Smooth NE and SW coasts (x close to 0 and x close to size_x) */ truelight@4300: for (y = 0; y < _height_map.size_y; y++) { truelight@4300: HeightMapSmoothCoastInDirection(0, y, 1, 0); truelight@4300: HeightMapSmoothCoastInDirection(_height_map.size_x - 1, y, -1, 0); truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /** rubidium@4549: * This routine provides the essential cleanup necessary before OTTD can rubidium@4549: * display the terrain. When generated, the terrain heights can jump more than rubidium@4549: * one level between tiles. This routine smooths out those differences so that rubidium@4549: * the most it can change is one level. When OTTD can support cliffs, this rubidium@4549: * routine may not be necessary. rubidium@4549: */ truelight@4300: static void HeightMapSmoothSlopes(height_t dh_max) truelight@4300: { truelight@4300: int x, y; truelight@4300: for (y = 1; y <= (int)_height_map.size_y; y++) { truelight@4300: for (x = 1; x <= (int)_height_map.size_x; x++) { frosch@9603: height_t h_max = min(_height_map.height(x - 1, y), _height_map.height(x, y - 1)) + dh_max; frosch@9603: if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max; truelight@4300: } truelight@4300: } truelight@4300: for (y = _height_map.size_y - 1; y >= 0; y--) { truelight@4300: for (x = _height_map.size_x - 1; x >= 0; x--) { frosch@9603: height_t h_max = min(_height_map.height(x + 1, y), _height_map.height(x, y + 1)) + dh_max; frosch@9603: if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max; truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /** Height map terraform post processing: truelight@4300: * - water level adjusting truelight@4300: * - coast Smoothing truelight@4300: * - slope Smoothing truelight@4300: * - height histogram redistribution by sine wave transform */ rubidium@6247: static void HeightMapNormalize() truelight@4300: { rubidium@9413: const amplitude_t water_percent = _water_percent[_settings_game.difficulty.quantity_sea_lakes]; rubidium@9413: const height_t h_max_new = I2H(_max_height[_settings_game.difficulty.terrain_type]); rubidium@9413: const height_t roughness = 7 + 3 * _settings_game.game_creation.tgen_smoothness; truelight@4300: truelight@4300: HeightMapAdjustWaterLevel(water_percent, h_max_new); truelight@4300: terom@10440: HeightMapCoastLines(); truelight@4300: HeightMapSmoothSlopes(roughness); truelight@4300: terom@10440: HeightMapSmoothCoasts(); truelight@4300: HeightMapSmoothSlopes(roughness); truelight@4300: truelight@4300: HeightMapSineTransform(12, h_max_new); truelight@4300: HeightMapSmoothSlopes(16); truelight@4300: } truelight@4300: truelight@4300: /** truelight@4300: * The Perlin Noise calculation using large primes truelight@4300: * The initial number is adjusted by two values; the generation_seed, and the truelight@4300: * passed parameter; prime. truelight@4300: * prime is used to allow the perlin noise generator to create useful random truelight@4300: * numbers from slightly different series. truelight@4300: */ truelight@4300: static double int_noise(const long x, const long y, const int prime) truelight@4300: { rubidium@9413: long n = x + y * prime + _settings_game.game_creation.generation_seed; truelight@4300: truelight@4300: n = (n << 13) ^ n; truelight@4300: truelight@4300: /* Pseudo-random number generator, using several large primes */ truelight@4300: return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0; truelight@4300: } truelight@4300: truelight@4300: truelight@4300: /** truelight@4300: * Hj. Malthaner's routine included 2 different noise smoothing methods. truelight@4300: * We now use the "raw" int_noise one. truelight@4300: * However, it may be useful to move to the other routine in future. truelight@4300: * So it is included too. truelight@4300: */ truelight@4300: static double smoothed_noise(const int x, const int y, const int prime) truelight@4300: { truelight@4300: #if 0 truelight@4300: /* A hilly world (four corner smooth) */ truelight@4300: const double sides = int_noise(x - 1, y) + int_noise(x + 1, y) + int_noise(x, y - 1) + int_noise(x, y + 1); truelight@4300: const double center = int_noise(x, y); truelight@4300: return (sides + sides + center * 4) / 8.0; truelight@4300: #endif truelight@4300: truelight@4300: /* This gives very hilly world */ truelight@4300: return int_noise(x, y, prime); truelight@4300: } truelight@4300: truelight@4300: truelight@4300: /** truelight@4300: * This routine determines the interpolated value between a and b truelight@4300: */ truelight@4300: static inline double linear_interpolate(const double a, const double b, const double x) truelight@4300: { truelight@4300: return a + x * (b - a); truelight@4300: } truelight@4300: truelight@4300: truelight@4300: /** truelight@4300: * This routine returns the smoothed interpolated noise for an x and y, using truelight@4300: * the values from the surrounding positions. truelight@4300: */ truelight@4300: static double interpolated_noise(const double x, const double y, const int prime) truelight@4300: { truelight@4300: const int integer_X = (int)x; truelight@4300: const int integer_Y = (int)y; truelight@4300: truelight@4300: const double fractional_X = x - (double)integer_X; truelight@4300: const double fractional_Y = y - (double)integer_Y; truelight@4300: truelight@4300: const double v1 = smoothed_noise(integer_X, integer_Y, prime); truelight@4300: const double v2 = smoothed_noise(integer_X + 1, integer_Y, prime); truelight@4300: const double v3 = smoothed_noise(integer_X, integer_Y + 1, prime); truelight@4300: const double v4 = smoothed_noise(integer_X + 1, integer_Y + 1, prime); truelight@4300: truelight@4300: const double i1 = linear_interpolate(v1, v2, fractional_X); truelight@4300: const double i2 = linear_interpolate(v3, v4, fractional_X); truelight@4300: truelight@4300: return linear_interpolate(i1, i2, fractional_Y); truelight@4300: } truelight@4300: truelight@4300: truelight@4300: /** truelight@4300: * This is a similar function to the main perlin noise calculation, but uses truelight@4300: * the value p passed as a parameter rather than selected from the predefined truelight@4300: * sequences. as you can guess by its title, i use this to create the indented truelight@4300: * coastline, which is just another perlin sequence. truelight@4300: */ truelight@4300: static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime) truelight@4300: { truelight@4300: double total = 0.0; truelight@4300: int i; truelight@4300: truelight@4300: for (i = 0; i < 6; i++) { truelight@4300: const double frequency = (double)(1 << i); truelight@4300: const double amplitude = pow(p, (double)i); truelight@4300: truelight@4300: total += interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude; truelight@4300: } truelight@4300: truelight@4300: return total; truelight@4300: } truelight@4300: truelight@4300: frosch@9613: /** A small helper function to initialize the terrain */ truelight@4300: static void TgenSetTileHeight(TileIndex tile, int height) truelight@4300: { truelight@4300: SetTileHeight(tile, height); truelight@4300: MakeClear(tile, CLEAR_GRASS, 3); truelight@4300: } truelight@4300: truelight@4300: /** truelight@4300: * The main new land generator using Perlin noise. Desert landscape is handled truelight@4300: * different to all others to give a desert valley between two high mountains. truelight@4300: * Clearly if a low height terrain (flat/very flat) is chosen, then the tropic truelight@4300: * areas wont be high enough, and there will be very little tropic on the map. truelight@4300: * Thus Tropic works best on Hilly or Mountainous. truelight@4300: */ rubidium@6247: void GenerateTerrainPerlin() truelight@4300: { truelight@4300: uint x, y; truelight@4300: truelight@4300: if (!AllocHeightMap()) return; rubidium@5145: GenerateWorldSetAbortCallback(FreeHeightMap); rubidium@5145: truelight@4300: HeightMapGenerate(); truelight@4300: truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@4300: HeightMapNormalize(); truelight@4300: truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@4300: /* Transfer height map into OTTD map */ truelight@4300: for (y = 2; y < _height_map.size_y - 2; y++) { truelight@4300: for (x = 2; x < _height_map.size_x - 2; x++) { frosch@9603: int height = H2I(_height_map.height(x, y)); truelight@4300: if (height < 0) height = 0; truelight@4300: if (height > 15) height = 15; truelight@4300: TgenSetTileHeight(TileXY(x, y), height); truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@4300: /* Recreate void tiles at the border in case they have been affected by generation */ truelight@4300: for (y = 0; y < _height_map.size_y - 1; y++) MakeVoid(_height_map.size_x * y + _height_map.size_x - 1); truelight@4300: for (x = 0; x < _height_map.size_x; x++) MakeVoid(_height_map.size_x * y + x); truelight@4300: truelight@4300: FreeHeightMap(); rubidium@5145: GenerateWorldSetAbortCallback(NULL); truelight@4300: }