tgp.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include <math.h>
00014 #include "clear_map.h"
00015 #include "void_map.h"
00016 #include "genworld.h"
00017 #include "core/random_func.hpp"
00018 #include "landscape_type.h"
00019 
00020 /*
00021  *
00022  * Quickie guide to Perlin Noise
00023  * Perlin noise is a predictable pseudo random number sequence. By generating
00024  * it in 2 dimensions, it becomes a useful random map that, for a given seed
00025  * and starting X & Y, is entirely predictable. On the face of it, that may not
00026  * be useful. However, it means that if you want to replay a map in a different
00027  * terrain, or just vary the sea level, you just re-run the generator with the
00028  * same seed. The seed is an int32, and is randomised on each run of New Game.
00029  * The Scenario Generator does not randomise the value, so that you can
00030  * experiment with one terrain until you are happy, or click "Random" for a new
00031  * random seed.
00032  *
00033  * Perlin Noise is a series of "octaves" of random noise added together. By
00034  * reducing the amplitude of the noise with each octave, the first octave of
00035  * noise defines the main terrain sweep, the next the ripples on that, and the
00036  * next the ripples on that. I use 6 octaves, with the amplitude controlled by
00037  * a power ratio, usually known as a persistence or p value. This I vary by the
00038  * smoothness selection, as can be seen in the table below. The closer to 1,
00039  * the more of that octave is added. Each octave is however raised to the power
00040  * of its position in the list, so the last entry in the "smooth" row, 0.35, is
00041  * raised to the power of 6, so can only add 0.001838...  of the amplitude to
00042  * the running total.
00043  *
00044  * In other words; the first p value sets the general shape of the terrain, the
00045  * second sets the major variations to that, ... until finally the smallest
00046  * bumps are added.
00047  *
00048  * Usefully, this routine is totally scaleable; so when 32bpp comes along, the
00049  * terrain can be as bumpy as you like! It is also infinitely expandable; a
00050  * single random seed terrain continues in X & Y as far as you care to
00051  * calculate. In theory, we could use just one seed value, but randomly select
00052  * where in the Perlin XY space we use for the terrain. Personally I prefer
00053  * using a simple (0, 0) to (X, Y), with a varying seed.
00054  *
00055  *
00056  * Other things i have had to do: mountainous wasn't mountainous enough, and
00057  * since we only have 0..15 heights available, I add a second generated map
00058  * (with a modified seed), onto the original. This generally raises the
00059  * terrain, which then needs scaling back down. Overall effect is a general
00060  * uplift.
00061  *
00062  * However, the values on the top of mountains are then almost guaranteed to go
00063  * too high, so large flat plateaus appeared at height 15. To counter this, I
00064  * scale all heights above 12 to proportion up to 15. It still makes the
00065  * mountains have flattish tops, rather than craggy peaks, but at least they
00066  * aren't smooth as glass.
00067  *
00068  *
00069  * For a full discussion of Perlin Noise, please visit:
00070  * http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
00071  *
00072  *
00073  * Evolution II
00074  *
00075  * The algorithm as described in the above link suggests to compute each tile height
00076  * as composition of several noise waves. Some of them are computed directly by
00077  * noise(x, y) function, some are calculated using linear approximation. Our
00078  * first implementation of perlin_noise_2D() used 4 noise(x, y) calls plus
00079  * 3 linear interpolations. It was called 6 times for each tile. This was a bit
00080  * CPU expensive.
00081  *
00082  * The following implementation uses optimized algorithm that should produce
00083  * the same quality result with much less computations, but more memory accesses.
00084  * The overall speedup should be 300% to 800% depending on CPU and memory speed.
00085  *
00086  * I will try to explain it on the example below:
00087  *
00088  * Have a map of 4 x 4 tiles, our simplified noise generator produces only two
00089  * values -1 and +1, use 3 octaves with wave length 1, 2 and 4, with amplitudes
00090  * 3, 2, 1. Original algorithm produces:
00091  *
00092  * 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
00093  * 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
00094  * 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
00095  * 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
00096  *
00097  * 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
00098  * 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
00099  * 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
00100  * 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
00101  *
00102  *
00103  * Optimization 1:
00104  *
00105  * 1) we need to allocate a bit more tiles: (size_x + 1) * (size_y + 1) = (5 * 5):
00106  *
00107  * 2) setup corner values using amplitude 3
00108  * {    -3.0        X          X          X          3.0   }
00109  * {     X          X          X          X          X     }
00110  * {     X          X          X          X          X     }
00111  * {     X          X          X          X          X     }
00112  * {     3.0        X          X          X         -3.0   }
00113  *
00114  * 3a) interpolate values in the middle
00115  * {    -3.0        X          0.0        X          3.0   }
00116  * {     X          X          X          X          X     }
00117  * {     0.0        X          0.0        X          0.0   }
00118  * {     X          X          X          X          X     }
00119  * {     3.0        X          0.0        X         -3.0   }
00120  *
00121  * 3b) add patches with amplitude 2 to them
00122  * {    -5.0        X          2.0        X          1.0   }
00123  * {     X          X          X          X          X     }
00124  * {     2.0        X         -2.0        X          2.0   }
00125  * {     X          X          X          X          X     }
00126  * {     1.0        X          2.0        X         -5.0   }
00127  *
00128  * 4a) interpolate values in the middle
00129  * {    -5.0       -1.5        2.0        1.5        1.0   }
00130  * {    -1.5       -0.75       0.0        0.75       1.5   }
00131  * {     2.0        0.0       -2.0        0.0        2.0   }
00132  * {     1.5        0.75       0.0       -0.75      -1.5   }
00133  * {     1.0        1.5        2.0       -1.5       -5.0   }
00134  *
00135  * 4b) add patches with amplitude 1 to them
00136  * {    -6.0       -0.5        1.0        2.5        0.0   }
00137  * {    -0.5       -1.75       1.0       -0.25       2.5   }
00138  * {     1.0        1.0       -3.0        1.0        1.0   }
00139  * {     2.5       -0.25       1.0       -1.75      -0.5   }
00140  * {     0.0        2.5        1.0       -0.5       -6.0   }
00141  *
00142  *
00143  *
00144  * Optimization 2:
00145  *
00146  * As you can see above, each noise function was called just once. Therefore
00147  * we don't need to use noise function that calculates the noise from x, y and
00148  * some prime. The same quality result we can obtain using standard Random()
00149  * function instead.
00150  *
00151  */
00152 
00154 typedef int16 height_t;
00155 static const int height_decimal_bits = 4;
00156 static const height_t _invalid_height = -32768;
00157 
00159 typedef int amplitude_t;
00160 static const int amplitude_decimal_bits = 10;
00161 
00163 struct HeightMap
00164 {
00165   height_t *h;         //< array of heights
00166   uint     dim_x;      //< height map size_x MapSizeX() + 1
00167   uint     total_size; //< height map total size
00168   uint     size_x;     //< MapSizeX()
00169   uint     size_y;     //< MapSizeY()
00170 
00177   inline height_t &height(uint x, uint y)
00178   {
00179     return h[x + y * dim_x];
00180   }
00181 };
00182 
00184 static HeightMap _height_map = {NULL, 0, 0, 0, 0};
00185 
00187 #define I2H(i) ((i) << height_decimal_bits)
00188 
00189 #define H2I(i) ((i) >> height_decimal_bits)
00190 
00192 #define I2A(i) ((i) << amplitude_decimal_bits)
00193 
00194 #define A2I(i) ((i) >> amplitude_decimal_bits)
00195 
00197 #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
00198 
00199 
00201 #define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++)
00202 
00204 static const int TGP_FREQUENCY_MAX = 6;
00205 
00210 static const amplitude_t _amplitudes_by_smoothness_and_frequency[4][TGP_FREQUENCY_MAX + 1] = {
00211   /* lowest frequncy....  ...highest (every corner) */
00212   /* Very smooth */
00213   {16000,  5600,  1968,   688,   240,    16,    16},
00214   /* Smooth */
00215   {16000, 16000,  6448,  3200,  1024,   128,    16},
00216   /* Rough */
00217   {16000, 19200, 12800,  8000,  3200,   256,    64},
00218   /* Very Rough */
00219   {24000, 16000, 19200, 16000,  8000,   512,   320},
00220 };
00221 
00223 static const amplitude_t _water_percent[4] = {20, 80, 250, 400};
00224 
00226 static const int8 _max_height[4] = {
00227   6,       
00228   9,       
00229   12,      
00230   15,      
00231 };
00232 
00239 static inline bool IsValidXY(uint x, uint y)
00240 {
00241   return ((int)x) >= 0 && x < _height_map.size_x && ((int)y) >= 0 && y < _height_map.size_y;
00242 }
00243 
00244 
00249 static inline bool AllocHeightMap()
00250 {
00251   height_t *h;
00252 
00253   _height_map.size_x = MapSizeX();
00254   _height_map.size_y = MapSizeY();
00255 
00256   /* Allocate memory block for height map row pointers */
00257   _height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
00258   _height_map.dim_x = _height_map.size_x + 1;
00259   _height_map.h = CallocT<height_t>(_height_map.total_size);
00260 
00261   /* Iterate through height map initialize values */
00262   FOR_ALL_TILES_IN_HEIGHT(h) *h = _invalid_height;
00263 
00264   return true;
00265 }
00266 
00268 static inline void FreeHeightMap()
00269 {
00270   if (_height_map.h == NULL) return;
00271   free(_height_map.h);
00272   _height_map.h = NULL;
00273 }
00274 
00280 static inline height_t RandomHeight(amplitude_t rMax)
00281 {
00282   amplitude_t ra = (Random() << 16) | (Random() & 0x0000FFFF);
00283   height_t rh;
00284   /* Spread height into range -rMax..+rMax */
00285   rh = A2H(ra % (2 * rMax + 1) - rMax);
00286   return rh;
00287 }
00288 
00307 static bool ApplyNoise(uint log_frequency, amplitude_t amplitude)
00308 {
00309   uint size_min = min(_height_map.size_x, _height_map.size_y);
00310   uint step = size_min >> log_frequency;
00311   uint x, y;
00312 
00313   /* Trying to apply noise to uninitialized height map */
00314   assert(_height_map.h != NULL);
00315 
00316   /* Are we finished? */
00317   if (step == 0) return false;
00318 
00319   if (log_frequency == 0) {
00320     /* This is first round, we need to establish base heights with step = size_min */
00321     for (y = 0; y <= _height_map.size_y; y += step) {
00322       for (x = 0; x <= _height_map.size_x; x += step) {
00323         height_t height = (amplitude > 0) ? RandomHeight(amplitude) : 0;
00324         _height_map.height(x, y) = height;
00325       }
00326     }
00327     return true;
00328   }
00329 
00330   /* It is regular iteration round.
00331    * Interpolate height values at odd x, even y tiles */
00332   for (y = 0; y <= _height_map.size_y; y += 2 * step) {
00333     for (x = 0; x < _height_map.size_x; x += 2 * step) {
00334       height_t h00 = _height_map.height(x + 0 * step, y);
00335       height_t h02 = _height_map.height(x + 2 * step, y);
00336       height_t h01 = (h00 + h02) / 2;
00337       _height_map.height(x + 1 * step, y) = h01;
00338     }
00339   }
00340 
00341   /* Interpolate height values at odd y tiles */
00342   for (y = 0; y < _height_map.size_y; y += 2 * step) {
00343     for (x = 0; x <= _height_map.size_x; x += step) {
00344       height_t h00 = _height_map.height(x, y + 0 * step);
00345       height_t h20 = _height_map.height(x, y + 2 * step);
00346       height_t h10 = (h00 + h20) / 2;
00347       _height_map.height(x, y + 1 * step) = h10;
00348     }
00349   }
00350 
00351   /* Add noise for next higher frequency (smaller steps) */
00352   for (y = 0; y <= _height_map.size_y; y += step) {
00353     for (x = 0; x <= _height_map.size_x; x += step) {
00354       _height_map.height(x, y) += RandomHeight(amplitude);
00355     }
00356   }
00357 
00358   return (step > 1);
00359 }
00360 
00362 static void HeightMapGenerate()
00363 {
00364   uint size_min = min(_height_map.size_x, _height_map.size_y);
00365   uint iteration_round = 0;
00366   amplitude_t amplitude;
00367   bool continue_iteration;
00368   int log_size_min, log_frequency_min;
00369   int log_frequency;
00370 
00371   /* Find first power of two that fits, so that later log_frequency == TGP_FREQUENCY_MAX in the last iteration */
00372   for (log_size_min = TGP_FREQUENCY_MAX; (1U << log_size_min) < size_min; log_size_min++) { }
00373   log_frequency_min = log_size_min - TGP_FREQUENCY_MAX;
00374 
00375   /* Zero must be part of the iteration, else initialization will fail. */
00376   assert(log_frequency_min >= 0);
00377 
00378   /* Keep increasing the frequency until we reach the step size equal to one tile */
00379   do {
00380     log_frequency = iteration_round - log_frequency_min;
00381     if (log_frequency >= 0) {
00382       /* Apply noise for the next frequency */
00383       assert(log_frequency <= TGP_FREQUENCY_MAX);
00384       amplitude = _amplitudes_by_smoothness_and_frequency[_settings_game.game_creation.tgen_smoothness][log_frequency];
00385     } else {
00386       /* Amplitude for the low frequencies on big maps is 0, i.e. initialise with zero height */
00387       amplitude = 0;
00388     }
00389     continue_iteration = ApplyNoise(iteration_round, amplitude);
00390     iteration_round++;
00391   } while (continue_iteration);
00392   assert(log_frequency == TGP_FREQUENCY_MAX);
00393 }
00394 
00396 static void HeightMapGetMinMaxAvg(height_t *min_ptr, height_t *max_ptr, height_t *avg_ptr)
00397 {
00398   height_t h_min, h_max, h_avg, *h;
00399   int64 h_accu = 0;
00400   h_min = h_max = _height_map.height(0, 0);
00401 
00402   /* Get h_min, h_max and accumulate heights into h_accu */
00403   FOR_ALL_TILES_IN_HEIGHT(h) {
00404     if (*h < h_min) h_min = *h;
00405     if (*h > h_max) h_max = *h;
00406     h_accu += *h;
00407   }
00408 
00409   /* Get average height */
00410   h_avg = (height_t)(h_accu / (_height_map.size_x * _height_map.size_y));
00411 
00412   /* Return required results */
00413   if (min_ptr != NULL) *min_ptr = h_min;
00414   if (max_ptr != NULL) *max_ptr = h_max;
00415   if (avg_ptr != NULL) *avg_ptr = h_avg;
00416 }
00417 
00419 static int *HeightMapMakeHistogram(height_t h_min, height_t h_max, int *hist_buf)
00420 {
00421   int *hist = hist_buf - h_min;
00422   height_t *h;
00423 
00424   /* Count the heights and fill the histogram */
00425   FOR_ALL_TILES_IN_HEIGHT(h) {
00426     assert(*h >= h_min);
00427     assert(*h <= h_max);
00428     hist[*h]++;
00429   }
00430   return hist;
00431 }
00432 
00434 static void HeightMapSineTransform(height_t h_min, height_t h_max)
00435 {
00436   height_t *h;
00437 
00438   FOR_ALL_TILES_IN_HEIGHT(h) {
00439     double fheight;
00440 
00441     if (*h < h_min) continue;
00442 
00443     /* Transform height into 0..1 space */
00444     fheight = (double)(*h - h_min) / (double)(h_max - h_min);
00445     /* Apply sine transform depending on landscape type */
00446     switch (_settings_game.game_creation.landscape) {
00447       case LT_TOYLAND:
00448       case LT_TEMPERATE:
00449         /* Move and scale 0..1 into -1..+1 */
00450         fheight = 2 * fheight - 1;
00451         /* Sine transform */
00452         fheight = sin(fheight * M_PI_2);
00453         /* Transform it back from -1..1 into 0..1 space */
00454         fheight = 0.5 * (fheight + 1);
00455         break;
00456 
00457       case LT_ARCTIC:
00458         {
00459           /* Arctic terrain needs special height distribution.
00460            * Redistribute heights to have more tiles at highest (75%..100%) range */
00461           double sine_upper_limit = 0.75;
00462           double linear_compression = 2;
00463           if (fheight >= sine_upper_limit) {
00464             /* Over the limit we do linear compression up */
00465             fheight = 1.0 - (1.0 - fheight) / linear_compression;
00466           } else {
00467             double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
00468             /* Get 0..sine_upper_limit into -1..1 */
00469             fheight = 2.0 * fheight / sine_upper_limit - 1.0;
00470             /* Sine wave transform */
00471             fheight = sin(fheight * M_PI_2);
00472             /* Get -1..1 back to 0..(1 - (1 - sine_upper_limit) / linear_compression) == 0.0..m */
00473             fheight = 0.5 * (fheight + 1.0) * m;
00474           }
00475         }
00476         break;
00477 
00478       case LT_TROPIC:
00479         {
00480           /* Desert terrain needs special height distribution.
00481            * Half of tiles should be at lowest (0..25%) heights */
00482           double sine_lower_limit = 0.5;
00483           double linear_compression = 2;
00484           if (fheight <= sine_lower_limit) {
00485             /* Under the limit we do linear compression down */
00486             fheight = fheight / linear_compression;
00487           } else {
00488             double m = sine_lower_limit / linear_compression;
00489             /* Get sine_lower_limit..1 into -1..1 */
00490             fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
00491             /* Sine wave transform */
00492             fheight = sin(fheight * M_PI_2);
00493             /* Get -1..1 back to (sine_lower_limit / linear_compression)..1.0 */
00494             fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
00495           }
00496         }
00497         break;
00498 
00499       default:
00500         NOT_REACHED();
00501         break;
00502     }
00503     /* Transform it back into h_min..h_max space */
00504     *h = (height_t)(fheight * (h_max - h_min) + h_min);
00505     if (*h < 0) *h = I2H(0);
00506     if (*h >= h_max) *h = h_max - 1;
00507   }
00508 }
00509 
00510 /* Additional map variety is provided by applying different curve maps
00511  * to different parts of the map. A randomized low resolution grid contains
00512  * which curve map to use on each part of the make. This filtered non-linearly
00513  * to smooth out transitions between curves, so each tile could have between
00514  * 100% of one map applied or 25% of four maps.
00515  *
00516  * The curve maps define different land styles, i.e. lakes, low-lands, hills
00517  * and mountain ranges, although these are dependent on the landscape style
00518  * chosen as well.
00519  *
00520  * The level parameter dictates the resolution of the grid. A low resolution
00521  * grid will result in larger continuous areas of a land style, a higher
00522  * resolution grid splits the style into smaller areas.
00523  *
00524  * At this point in map generation, all height data has been normalized to 0
00525  * to 239.
00526  */
00527 struct control_point_t {
00528   height_t x;
00529   height_t y;
00530 };
00531 
00532 struct control_point_list_t {
00533   size_t length;
00534   const control_point_t *list;
00535 };
00536 
00537 static const control_point_t _curve_map_1[] = {
00538   { 0, 0 }, { 48, 24 }, { 192, 32 }, { 240, 96 }
00539 };
00540 
00541 static const control_point_t _curve_map_2[] = {
00542   { 0, 0 }, { 16, 24 }, { 128, 32 }, { 192, 64 }, { 240, 144 }
00543 };
00544 
00545 static const control_point_t _curve_map_3[] = {
00546   { 0, 0 }, { 16, 24 }, { 128, 64 }, { 192, 144 }, { 240, 192 }
00547 };
00548 
00549 static const control_point_t _curve_map_4[] = {
00550   { 0, 0 }, { 16, 24 }, { 96, 72 }, { 160, 192 }, { 220, 239 }, { 240, 239 }
00551 };
00552 
00553 static const control_point_list_t _curve_maps[] = {
00554   { lengthof(_curve_map_1), _curve_map_1 },
00555   { lengthof(_curve_map_2), _curve_map_2 },
00556   { lengthof(_curve_map_3), _curve_map_3 },
00557   { lengthof(_curve_map_4), _curve_map_4 },
00558 };
00559 
00560 static void HeightMapCurves(uint level)
00561 {
00562   height_t ht[lengthof(_curve_maps)];
00563 
00564   /* Set up a grid to choose curve maps based on location */
00565   uint sx = Clamp(1 << level, 2, 32);
00566   uint sy = Clamp(1 << level, 2, 32);
00567   byte *c = (byte *)alloca(sx * sy);
00568 
00569   for (uint i = 0; i < sx * sy; i++) {
00570     c[i] = Random() % lengthof(_curve_maps);
00571   }
00572 
00573   /* Apply curves */
00574   for (uint x = 0; x < _height_map.size_x; x++) {
00575 
00576     /* Get our X grid positions and bi-linear ratio */
00577     float fx = (float)(sx * x) / _height_map.size_x + 0.5f;
00578     uint x1 = (uint)fx;
00579     uint x2 = x1;
00580     float xr = 2.0f * (fx - x1) - 1.0f;
00581     xr = sin(xr * M_PI_2);
00582     xr = sin(xr * M_PI_2);
00583     xr = 0.5f * (xr + 1.0f);
00584     float xri = 1.0f - xr;
00585 
00586     if (x1 > 0) {
00587       x1--;
00588       if (x2 >= sx) x2--;
00589     }
00590 
00591     for (uint y = 0; y < _height_map.size_y; y++) {
00592 
00593       /* Get our Y grid position and bi-linear ratio */
00594       float fy = (float)(sy * y) / _height_map.size_y + 0.5f;
00595       uint y1 = (uint)fy;
00596       uint y2 = y1;
00597       float yr = 2.0f * (fy - y1) - 1.0f;
00598       yr = sin(yr * M_PI_2);
00599       yr = sin(yr * M_PI_2);
00600       yr = 0.5f * (yr + 1.0f);
00601       float yri = 1.0f - yr;
00602 
00603       if (y1 > 0) {
00604         y1--;
00605         if (y2 >= sy) y2--;
00606       }
00607 
00608       uint corner_a = c[x1 + sx * y1];
00609       uint corner_b = c[x1 + sx * y2];
00610       uint corner_c = c[x2 + sx * y1];
00611       uint corner_d = c[x2 + sx * y2];
00612 
00613       /* Bitmask of which curve maps are chosen, so that we do not bother
00614        * calculating a curve which won't be used. */
00615       uint corner_bits = 0;
00616       corner_bits |= 1 << corner_a;
00617       corner_bits |= 1 << corner_b;
00618       corner_bits |= 1 << corner_c;
00619       corner_bits |= 1 << corner_d;
00620 
00621       height_t *h = &_height_map.height(x, y);
00622 
00623       /* Apply all curve maps that are used on this tile. */
00624       for (uint t = 0; t < lengthof(_curve_maps); t++) {
00625         if (!HasBit(corner_bits, t)) continue;
00626 
00627         const control_point_t *cm = _curve_maps[t].list;
00628         for (uint i = 0; i < _curve_maps[t].length - 1; i++) {
00629           const control_point_t &p1 = cm[i];
00630           const control_point_t &p2 = cm[i + 1];
00631 
00632           if (*h >= p1.x && *h < p2.x) {
00633             ht[t] = p1.y + (*h - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
00634             break;
00635           }
00636         }
00637       }
00638 
00639       /* Apply interpolation of curve map results. */
00640       *h = (height_t)((ht[corner_a] * yri + ht[corner_b] * yr) * xri + (ht[corner_c] * yri + ht[corner_d] * yr) * xr);
00641     }
00642   }
00643 }
00644 
00646 static void HeightMapAdjustWaterLevel(amplitude_t water_percent, height_t h_max_new)
00647 {
00648   height_t h_min, h_max, h_avg, h_water_level;
00649   int64 water_tiles, desired_water_tiles;
00650   height_t *h;
00651   int *hist;
00652 
00653   HeightMapGetMinMaxAvg(&h_min, &h_max, &h_avg);
00654 
00655   /* Allocate histogram buffer and clear its cells */
00656   int *hist_buf = CallocT<int>(h_max - h_min + 1);
00657   /* Fill histogram */
00658   hist = HeightMapMakeHistogram(h_min, h_max, hist_buf);
00659 
00660   /* How many water tiles do we want? */
00661   desired_water_tiles = A2I(((int64)water_percent) * (int64)(_height_map.size_x * _height_map.size_y));
00662 
00663   /* Raise water_level and accumulate values from histogram until we reach required number of water tiles */
00664   for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
00665     water_tiles += hist[h_water_level];
00666     if (water_tiles >= desired_water_tiles) break;
00667   }
00668 
00669   /* We now have the proper water level value.
00670    * Transform the height map into new (normalized) height map:
00671    *   values from range: h_min..h_water_level will become negative so it will be clamped to 0
00672    *   values from range: h_water_level..h_max are transformed into 0..h_max_new
00673    *   where h_max_new is 4, 8, 12 or 16 depending on terrain type (very flat, flat, hilly, mountains)
00674    */
00675   FOR_ALL_TILES_IN_HEIGHT(h) {
00676     /* Transform height from range h_water_level..h_max into 0..h_max_new range */
00677     *h = (height_t)(((int)h_max_new) * (*h - h_water_level) / (h_max - h_water_level)) + I2H(1);
00678     /* Make sure all values are in the proper range (0..h_max_new) */
00679     if (*h < 0) *h = I2H(0);
00680     if (*h >= h_max_new) *h = h_max_new - 1;
00681   }
00682 
00683   free(hist_buf);
00684 }
00685 
00686 static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime);
00687 
00708 static void HeightMapCoastLines(uint8 water_borders)
00709 {
00710   int smallest_size = min(_settings_game.game_creation.map_x, _settings_game.game_creation.map_y);
00711   const int margin = 4;
00712   uint y, x;
00713   double max_x;
00714   double max_y;
00715 
00716   /* Lower to sea level */
00717   for (y = 0; y <= _height_map.size_y; y++) {
00718     if (HasBit(water_borders, BORDER_NE)) {
00719       /* Top right */
00720       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);
00721       max_x = max((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
00722       if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
00723       for (x = 0; x < max_x; x++) {
00724         _height_map.height(x, y) = 0;
00725       }
00726     }
00727 
00728     if (HasBit(water_borders, BORDER_SW)) {
00729       /* Bottom left */
00730       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);
00731       max_x = max((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
00732       if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
00733       for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x); x--) {
00734         _height_map.height(x, y) = 0;
00735       }
00736     }
00737   }
00738 
00739   /* Lower to sea level */
00740   for (x = 0; x <= _height_map.size_x; x++) {
00741     if (HasBit(water_borders, BORDER_NW)) {
00742       /* Top left */
00743       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);
00744       max_y = max((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
00745       if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
00746       for (y = 0; y < max_y; y++) {
00747         _height_map.height(x, y) = 0;
00748       }
00749     }
00750 
00751     if (HasBit(water_borders, BORDER_SE)) {
00752       /* Bottom right */
00753       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);
00754       max_y = max((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
00755       if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
00756       for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y); y--) {
00757         _height_map.height(x, y) = 0;
00758       }
00759     }
00760   }
00761 }
00762 
00764 static void HeightMapSmoothCoastInDirection(int org_x, int org_y, int dir_x, int dir_y)
00765 {
00766   const int max_coast_dist_from_edge = 35;
00767   const int max_coast_Smooth_depth = 35;
00768 
00769   int x, y;
00770   int ed; // coast distance from edge
00771   int depth;
00772 
00773   height_t h_prev = 16;
00774   height_t h;
00775 
00776   assert(IsValidXY(org_x, org_y));
00777 
00778   /* Search for the coast (first non-water tile) */
00779   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++) {
00780     /* Coast found? */
00781     if (_height_map.height(x, y) > 15) break;
00782 
00783     /* Coast found in the neighborhood? */
00784     if (IsValidXY(x + dir_y, y + dir_x) && _height_map.height(x + dir_y, y + dir_x) > 0) break;
00785 
00786     /* Coast found in the neighborhood on the other side */
00787     if (IsValidXY(x - dir_y, y - dir_x) && _height_map.height(x - dir_y, y - dir_x) > 0) break;
00788   }
00789 
00790   /* Coast found or max_coast_dist_from_edge has been reached.
00791    * Soften the coast slope */
00792   for (depth = 0; IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
00793     h = _height_map.height(x, y);
00794     h = min(h, h_prev + (4 + depth)); // coast softening formula
00795     _height_map.height(x, y) = h;
00796     h_prev = h;
00797   }
00798 }
00799 
00801 static void HeightMapSmoothCoasts(uint8 water_borders)
00802 {
00803   uint x, y;
00804   /* First Smooth NW and SE coasts (y close to 0 and y close to size_y) */
00805   for (x = 0; x < _height_map.size_x; x++) {
00806     if (HasBit(water_borders, BORDER_NW)) HeightMapSmoothCoastInDirection(x, 0, 0, 1);
00807     if (HasBit(water_borders, BORDER_SE)) HeightMapSmoothCoastInDirection(x, _height_map.size_y - 1, 0, -1);
00808   }
00809   /* First Smooth NE and SW coasts (x close to 0 and x close to size_x) */
00810   for (y = 0; y < _height_map.size_y; y++) {
00811     if (HasBit(water_borders, BORDER_NE)) HeightMapSmoothCoastInDirection(0, y, 1, 0);
00812     if (HasBit(water_borders, BORDER_SW)) HeightMapSmoothCoastInDirection(_height_map.size_x - 1, y, -1, 0);
00813   }
00814 }
00815 
00823 static void HeightMapSmoothSlopes(height_t dh_max)
00824 {
00825   int x, y;
00826   for (y = 0; y <= (int)_height_map.size_y; y++) {
00827     for (x = 0; x <= (int)_height_map.size_x; x++) {
00828       height_t h_max = min(_height_map.height(x > 0 ? x - 1 : x, y), _height_map.height(x, y > 0 ? y - 1 : y)) + dh_max;
00829       if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
00830     }
00831   }
00832   for (y = _height_map.size_y; y >= 0; y--) {
00833     for (x = _height_map.size_x; x >= 0; x--) {
00834       height_t h_max = min(_height_map.height((uint)x < _height_map.size_x ? x + 1 : x, y), _height_map.height(x, (uint)y < _height_map.size_y ? y + 1 : y)) + dh_max;
00835       if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
00836     }
00837   }
00838 }
00839 
00847 static void HeightMapNormalize()
00848 {
00849   int sea_level_setting = _settings_game.difficulty.quantity_sea_lakes;
00850   const amplitude_t water_percent = sea_level_setting != (int)CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY ? _water_percent[sea_level_setting] : _settings_game.game_creation.custom_sea_level * 1024 / 100;
00851   const height_t h_max_new = I2H(_max_height[_settings_game.difficulty.terrain_type]);
00852   const height_t roughness = 7 + 3 * _settings_game.game_creation.tgen_smoothness;
00853 
00854   HeightMapAdjustWaterLevel(water_percent, h_max_new);
00855 
00856   byte water_borders = _settings_game.construction.freeform_edges ? _settings_game.game_creation.water_borders : 0xF;
00857   if (water_borders == BORDERS_RANDOM) water_borders = GB(Random(), 0, 4);
00858 
00859   HeightMapCoastLines(water_borders);
00860   HeightMapSmoothSlopes(roughness);
00861 
00862   HeightMapSmoothCoasts(water_borders);
00863   HeightMapSmoothSlopes(roughness);
00864 
00865   HeightMapSineTransform(12, h_max_new);
00866 
00867   if (_settings_game.game_creation.variety > 0) {
00868     HeightMapCurves(_settings_game.game_creation.variety);
00869   }
00870 
00871   HeightMapSmoothSlopes(16);
00872 }
00873 
00881 static double int_noise(const long x, const long y, const int prime)
00882 {
00883   long n = x + y * prime + _settings_game.game_creation.generation_seed;
00884 
00885   n = (n << 13) ^ n;
00886 
00887   /* Pseudo-random number generator, using several large primes */
00888   return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
00889 }
00890 
00891 
00895 static inline double linear_interpolate(const double a, const double b, const double x)
00896 {
00897   return a + x * (b - a);
00898 }
00899 
00900 
00905 static double interpolated_noise(const double x, const double y, const int prime)
00906 {
00907   const int integer_X = (int)x;
00908   const int integer_Y = (int)y;
00909 
00910   const double fractional_X = x - (double)integer_X;
00911   const double fractional_Y = y - (double)integer_Y;
00912 
00913   const double v1 = int_noise(integer_X,     integer_Y,     prime);
00914   const double v2 = int_noise(integer_X + 1, integer_Y,     prime);
00915   const double v3 = int_noise(integer_X,     integer_Y + 1, prime);
00916   const double v4 = int_noise(integer_X + 1, integer_Y + 1, prime);
00917 
00918   const double i1 = linear_interpolate(v1, v2, fractional_X);
00919   const double i2 = linear_interpolate(v3, v4, fractional_X);
00920 
00921   return linear_interpolate(i1, i2, fractional_Y);
00922 }
00923 
00924 
00931 static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime)
00932 {
00933   double total = 0.0;
00934   int i;
00935 
00936   for (i = 0; i < 6; i++) {
00937     const double frequency = (double)(1 << i);
00938     const double amplitude = pow(p, (double)i);
00939 
00940     total += interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;
00941   }
00942 
00943   return total;
00944 }
00945 
00946 
00948 static void TgenSetTileHeight(TileIndex tile, int height)
00949 {
00950   SetTileHeight(tile, height);
00951 
00952   /* Only clear the tiles within the map area. */
00953   if (TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY() &&
00954       (!_settings_game.construction.freeform_edges || (TileX(tile) != 0 && TileY(tile) != 0))) {
00955     MakeClear(tile, CLEAR_GRASS, 3);
00956   }
00957 }
00958 
00966 void GenerateTerrainPerlin()
00967 {
00968   uint x, y;
00969 
00970   if (!AllocHeightMap()) return;
00971   GenerateWorldSetAbortCallback(FreeHeightMap);
00972 
00973   HeightMapGenerate();
00974 
00975   IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
00976 
00977   HeightMapNormalize();
00978 
00979   IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
00980 
00981   /* First make sure the tiles at the north border are void tiles if needed. */
00982   if (_settings_game.construction.freeform_edges) {
00983     for (y = 0; y < _height_map.size_y - 1; y++) MakeVoid(_height_map.size_x * y);
00984     for (x = 0; x < _height_map.size_x;     x++) MakeVoid(x);
00985   }
00986 
00987   /* Transfer height map into OTTD map */
00988   for (y = 0; y < _height_map.size_y; y++) {
00989     for (x = 0; x < _height_map.size_x; x++) {
00990       int height = H2I(_height_map.height(x, y));
00991       if (height < 0) height = 0;
00992       if (height > 15) height = 15;
00993       TgenSetTileHeight(TileXY(x, y), height);
00994     }
00995   }
00996 
00997   IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
00998 
00999   FreeHeightMap();
01000   GenerateWorldSetAbortCallback(NULL);
01001 }