tgp.cpp

Go to the documentation of this file.
00001 /* $Id: tgp.cpp 15568 2009-02-24 20:59:17Z smatz $ */
00002 
00005 #include "stdafx.h"
00006 #include <math.h>
00007 #include "clear_map.h"
00008 #include "void_map.h"
00009 #include "genworld.h"
00010 #include "core/alloc_func.hpp"
00011 #include "core/random_func.hpp"
00012 #include "landscape_type.h"
00013 #include "settings_type.h"
00014 
00015 /*
00016  *
00017  * Quickie guide to Perlin Noise
00018  * Perlin noise is a predictable pseudo random number sequence. By generating
00019  * it in 2 dimensions, it becomes a useful random map, that for a given seed
00020  * and starting X & Y is entirely predictable. On the face of it, that may not
00021  * be useful. However, it means that if you want to replay a map in a different
00022  * terrain, or just vary the sea level, you just re-run the generator with the
00023  * same seed. The seed is an int32, and is randomised on each run of New Game.
00024  * The Scenario Generator does not randomise the value, so that you can
00025  * experiment with one terrain until you are happy, or click "Random" for a new
00026  * random seed.
00027  *
00028  * Perlin Noise is a series of "octaves" of random noise added together. By
00029  * reducing the amplitude of the noise with each octave, the first octave of
00030  * noise defines the main terrain sweep, the next the ripples on that, and the
00031  * next the ripples on that. I use 6 octaves, with the amplitude controlled by
00032  * a power ratio, usually known as a persistence or p value. This I vary by the
00033  * smoothness selection, as can be seen in the table below. The closer to 1,
00034  * the more of that octave is added. Each octave is however raised to the power
00035  * of its position in the list, so the last entry in the "smooth" row, 0.35, is
00036  * raised to the power of 6, so can only add 0.001838...  of the amplitude to
00037  * the running total.
00038  *
00039  * In other words; the first p value sets the general shape of the terrain, the
00040  * second sets the major variations to that, ... until finally the smallest
00041  * bumps are added.
00042  *
00043  * Usefully, this routine is totally scaleable; so when 32bpp comes along, the
00044  * terrain can be as bumpy as you like! It is also infinitely expandable; a
00045  * single random seed terrain continues in X & Y as far as you care to
00046  * calculate. In theory, we could use just one seed value, but randomly select
00047  * where in the Perlin XY space we use for the terrain. Personally I prefer
00048  * using a simple (0, 0) to (X, Y), with a varying seed.
00049  *
00050  *
00051  * Other things i have had to do: mountainous wasnt mountainous enough, and
00052  * since we only have 0..15 heights available, I add a second generated map
00053  * (with a modified seed), onto the original. This generally raises the
00054  * terrain, which then needs scaling back down. Overall effect is a general
00055  * uplift.
00056  *
00057  * However, the values on the top of mountains are then almost guaranteed to go
00058  * too high, so large flat plateaus appeared at height 15. To counter this, I
00059  * scale all heights above 12 to proportion up to 15. It still makes the
00060  * mountains have flatish tops, rather than craggy peaks, but at least they
00061  * arent smooth as glass.
00062  *
00063  *
00064  * For a full discussion of Perlin Noise, please visit:
00065  * http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
00066  *
00067  *
00068  * Evolution II
00069  *
00070  * The algorithm as described in the above link suggests to compute each tile height
00071  * as composition of several noise waves. Some of them are computed directly by
00072  * noise(x, y) function, some are calculated using linear approximation. Our
00073  * first implementation of perlin_noise_2D() used 4 noise(x, y) calls plus
00074  * 3 linear interpolations. It was called 6 times for each tile. This was a bit
00075  * CPU expensive.
00076  *
00077  * The following implementation uses optimized algorithm that should produce
00078  * the same quality result with much less computations, but more memory accesses.
00079  * The overal speedup should be 300% to 800% depending on CPU and memory speed.
00080  *
00081  * I will try to explain it on the example below:
00082  *
00083  * Have a map of 4 x 4 tiles, our simplifiead noise generator produces only two
00084  * values -1 and +1, use 3 octaves with wave lenght 1, 2 and 4, with amplitudes
00085  * 3, 2, 1. Original algorithm produces:
00086  *
00087  * 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
00088  * 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
00089  * 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
00090  * 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
00091  *
00092  * 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
00093  * 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
00094  * 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
00095  * 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
00096  *
00097  *
00098  * Optimization 1:
00099  *
00100  * 1) we need to allocate a bit more tiles: (size_x + 1) * (size_y + 1) = (5 * 5):
00101  *
00102  * 2) setup corner values using amplitude 3
00103  * {    -3.0        X          X          X          3.0   }
00104  * {     X          X          X          X          X     }
00105  * {     X          X          X          X          X     }
00106  * {     X          X          X          X          X     }
00107  * {     3.0        X          X          X         -3.0   }
00108  *
00109  * 3a) interpolate values in the middle
00110  * {    -3.0        X          0.0        X          3.0   }
00111  * {     X          X          X          X          X     }
00112  * {     0.0        X          0.0        X          0.0   }
00113  * {     X          X          X          X          X     }
00114  * {     3.0        X          0.0        X         -3.0   }
00115  *
00116  * 3b) add patches with amplitude 2 to them
00117  * {    -5.0        X          2.0        X          1.0   }
00118  * {     X          X          X          X          X     }
00119  * {     2.0        X         -2.0        X          2.0   }
00120  * {     X          X          X          X          X     }
00121  * {     1.0        X          2.0        X         -5.0   }
00122  *
00123  * 4a) interpolate values in the middle
00124  * {    -5.0       -1.5        2.0        1.5        1.0   }
00125  * {    -1.5       -0.75       0.0        0.75       1.5   }
00126  * {     2.0        0.0       -2.0        0.0        2.0   }
00127  * {     1.5        0.75       0.0       -0.75      -1.5   }
00128  * {     1.0        1.5        2.0       -1.5       -5.0   }
00129  *
00130  * 4b) add patches with amplitude 1 to them
00131  * {    -6.0       -0.5        1.0        2.5        0.0   }
00132  * {    -0.5       -1.75       1.0       -0.25       2.5   }
00133  * {     1.0        1.0       -3.0        1.0        1.0   }
00134  * {     2.5       -0.25       1.0       -1.75      -0.5   }
00135  * {     0.0        2.5        1.0       -0.5       -6.0   }
00136  *
00137  *
00138  *
00139  * Optimization 2:
00140  *
00141  * As you can see above, each noise function was called just once. Therefore
00142  * we don't need to use noise function that calculates the noise from x, y and
00143  * some prime. The same quality result we can obtain using standard Random()
00144  * function instead.
00145  *
00146  */
00147 
00148 #ifndef M_PI_2
00149 #define M_PI_2 1.57079632679489661923
00150 #define M_PI   3.14159265358979323846
00151 #endif /* M_PI_2 */
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     return h[x + y * dim_x];
00179   }
00180 };
00181 
00183 static HeightMap _height_map = {NULL, 0, 0, 0, 0};
00184 
00186 #define I2H(i) ((i) << height_decimal_bits)
00187 
00188 #define H2I(i) ((i) >> height_decimal_bits)
00189 
00191 #define I2A(i) ((i) << amplitude_decimal_bits)
00192 
00193 #define A2I(i) ((i) >> amplitude_decimal_bits)
00194 
00196 #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
00197 
00198 
00200 #define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++)
00201 
00203 static const int TGP_FREQUENCY_MAX = 6;
00204 
00207 static const amplitude_t _amplitudes_by_smoothness_and_frequency[4][TGP_FREQUENCY_MAX + 1] = {
00208   /* lowest frequncy....  ...highest (every corner) */
00209   /* Very smooth */
00210   {16000,  5600,  1968,   688,   240,    16,    16},
00211   /* Smooth */
00212   {16000, 16000,  6448,  3200,  1024,   128,    16},
00213   /* Rough */
00214   {16000, 19200, 12800,  8000,  3200,   256,    64},
00215   /* Very Rough */
00216   {24000, 16000, 19200, 16000,  8000,   512,   320},
00217 };
00218 
00220 static const amplitude_t _water_percent[4] = {20, 80, 250, 400};
00221 
00223 static const int8 _max_height[4] = {
00224   6,       
00225   9,       
00226   12,      
00227   15       
00228 };
00229 
00235 static inline bool IsValidXY(uint x, uint y)
00236 {
00237   return ((int)x) >= 0 && x < _height_map.size_x && ((int)y) >= 0 && y < _height_map.size_y;
00238 }
00239 
00240 
00245 static inline bool AllocHeightMap()
00246 {
00247   height_t *h;
00248 
00249   _height_map.size_x = MapSizeX();
00250   _height_map.size_y = MapSizeY();
00251 
00252   /* Allocate memory block for height map row pointers */
00253   _height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
00254   _height_map.dim_x = _height_map.size_x + 1;
00255   _height_map.h = CallocT<height_t>(_height_map.total_size);
00256 
00257   /* Iterate through height map initialize values */
00258   FOR_ALL_TILES_IN_HEIGHT(h) *h = _invalid_height;
00259 
00260   return true;
00261 }
00262 
00264 static inline void FreeHeightMap()
00265 {
00266   if (_height_map.h == NULL) return;
00267   free(_height_map.h);
00268   _height_map.h = NULL;
00269 }
00270 
00276 static inline height_t RandomHeight(amplitude_t rMax)
00277 {
00278   amplitude_t ra = (Random() << 16) | (Random() & 0x0000FFFF);
00279   height_t rh;
00280   /* Spread height into range -rMax..+rMax */
00281   rh = A2H(ra % (2 * rMax + 1) - rMax);
00282   return rh;
00283 }
00284 
00303 static bool ApplyNoise(uint log_frequency, amplitude_t amplitude)
00304 {
00305   uint size_min = min(_height_map.size_x, _height_map.size_y);
00306   uint step = size_min >> log_frequency;
00307   uint x, y;
00308 
00309   /* Trying to apply noise to uninitialized height map */
00310   assert(_height_map.h != NULL);
00311 
00312   /* Are we finished? */
00313   if (step == 0) return false;
00314 
00315   if (log_frequency == 0) {
00316     /* This is first round, we need to establish base heights with step = size_min */
00317     for (y = 0; y <= _height_map.size_y; y += step) {
00318       for (x = 0; x <= _height_map.size_x; x += step) {
00319         height_t height = (amplitude > 0) ? RandomHeight(amplitude) : 0;
00320         _height_map.height(x, y) = height;
00321       }
00322     }
00323     return true;
00324   }
00325 
00326   /* It is regular iteration round.
00327    * Interpolate height values at odd x, even y tiles */
00328   for (y = 0; y <= _height_map.size_y; y += 2 * step) {
00329     for (x = 0; x < _height_map.size_x; x += 2 * step) {
00330       height_t h00 = _height_map.height(x + 0 * step, y);
00331       height_t h02 = _height_map.height(x + 2 * step, y);
00332       height_t h01 = (h00 + h02) / 2;
00333       _height_map.height(x + 1 * step, y) = h01;
00334     }
00335   }
00336 
00337   /* Interpolate height values at odd y tiles */
00338   for (y = 0; y < _height_map.size_y; y += 2 * step) {
00339     for (x = 0; x <= _height_map.size_x; x += step) {
00340       height_t h00 = _height_map.height(x, y + 0 * step);
00341       height_t h20 = _height_map.height(x, y + 2 * step);
00342       height_t h10 = (h00 + h20) / 2;
00343       _height_map.height(x, y + 1 * step) = h10;
00344     }
00345   }
00346 
00347   /* Add noise for next higher frequency (smaller steps) */
00348   for (y = 0; y <= _height_map.size_y; y += step) {
00349     for (x = 0; x <= _height_map.size_x; x += step) {
00350       _height_map.height(x, y) += RandomHeight(amplitude);
00351     }
00352   }
00353 
00354   return (step > 1);
00355 }
00356 
00358 static void HeightMapGenerate()
00359 {
00360   uint size_min = min(_height_map.size_x, _height_map.size_y);
00361   uint iteration_round = 0;
00362   amplitude_t amplitude;
00363   bool continue_iteration;
00364   int log_size_min, log_frequency_min;
00365   int log_frequency;
00366 
00367   /* Find first power of two that fits, so that later log_frequency == TGP_FREQUENCY_MAX in the last iteration */
00368   for (log_size_min = TGP_FREQUENCY_MAX; (1U << log_size_min) < size_min; log_size_min++) { }
00369   log_frequency_min = log_size_min - TGP_FREQUENCY_MAX;
00370 
00371   /* Zero must be part of the iteration, else initialization will fail. */
00372   assert(log_frequency_min >= 0);
00373 
00374   /* Keep increasing the frequency until we reach the step size equal to one tile */
00375   do {
00376     log_frequency = iteration_round - log_frequency_min;
00377     if (log_frequency >= 0) {
00378       /* Apply noise for the next frequency */
00379       assert(log_frequency <= TGP_FREQUENCY_MAX);
00380       amplitude = _amplitudes_by_smoothness_and_frequency[_settings_game.game_creation.tgen_smoothness][log_frequency];
00381     } else {
00382       /* Amplitude for the low frequencies on big maps is 0, i.e. initialise with zero height */
00383       amplitude = 0;
00384     }
00385     continue_iteration = ApplyNoise(iteration_round, amplitude);
00386     iteration_round++;
00387   } while (continue_iteration);
00388   assert(log_frequency == TGP_FREQUENCY_MAX);
00389 }
00390 
00392 static void HeightMapGetMinMaxAvg(height_t *min_ptr, height_t *max_ptr, height_t *avg_ptr)
00393 {
00394   height_t h_min, h_max, h_avg, *h;
00395   int64 h_accu = 0;
00396   h_min = h_max = _height_map.height(0, 0);
00397 
00398   /* Get h_min, h_max and accumulate heights into h_accu */
00399   FOR_ALL_TILES_IN_HEIGHT(h) {
00400     if (*h < h_min) h_min = *h;
00401     if (*h > h_max) h_max = *h;
00402     h_accu += *h;
00403   }
00404 
00405   /* Get average height */
00406   h_avg = (height_t)(h_accu / (_height_map.size_x * _height_map.size_y));
00407 
00408   /* Return required results */
00409   if (min_ptr != NULL) *min_ptr = h_min;
00410   if (max_ptr != NULL) *max_ptr = h_max;
00411   if (avg_ptr != NULL) *avg_ptr = h_avg;
00412 }
00413 
00415 static int *HeightMapMakeHistogram(height_t h_min, height_t h_max, int *hist_buf)
00416 {
00417   int *hist = hist_buf - h_min;
00418   height_t *h;
00419 
00420   /* Count the heights and fill the histogram */
00421   FOR_ALL_TILES_IN_HEIGHT(h) {
00422     assert(*h >= h_min);
00423     assert(*h <= h_max);
00424     hist[*h]++;
00425   }
00426   return hist;
00427 }
00428 
00430 static void HeightMapSineTransform(height_t h_min, height_t h_max)
00431 {
00432   height_t *h;
00433 
00434   FOR_ALL_TILES_IN_HEIGHT(h) {
00435     double fheight;
00436 
00437     if (*h < h_min) continue;
00438 
00439     /* Transform height into 0..1 space */
00440     fheight = (double)(*h - h_min) / (double)(h_max - h_min);
00441     /* Apply sine transform depending on landscape type */
00442     switch(_settings_game.game_creation.landscape) {
00443       case LT_TOYLAND:
00444       case LT_TEMPERATE:
00445         /* Move and scale 0..1 into -1..+1 */
00446         fheight = 2 * fheight - 1;
00447         /* Sine transform */
00448         fheight = sin(fheight * M_PI_2);
00449         /* Transform it back from -1..1 into 0..1 space */
00450         fheight = 0.5 * (fheight + 1);
00451         break;
00452 
00453       case LT_ARCTIC:
00454         {
00455           /* Arctic terrain needs special height distribution.
00456            * Redistribute heights to have more tiles at highest (75%..100%) range */
00457           double sine_upper_limit = 0.75;
00458           double linear_compression = 2;
00459           if (fheight >= sine_upper_limit) {
00460             /* Over the limit we do linear compression up */
00461             fheight = 1.0 - (1.0 - fheight) / linear_compression;
00462           } else {
00463             double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
00464             /* Get 0..sine_upper_limit into -1..1 */
00465             fheight = 2.0 * fheight / sine_upper_limit - 1.0;
00466             /* Sine wave transform */
00467             fheight = sin(fheight * M_PI_2);
00468             /* Get -1..1 back to 0..(1 - (1 - sine_upper_limit) / linear_compression) == 0.0..m */
00469             fheight = 0.5 * (fheight + 1.0) * m;
00470           }
00471         }
00472         break;
00473 
00474       case LT_TROPIC:
00475         {
00476           /* Desert terrain needs special height distribution.
00477            * Half of tiles should be at lowest (0..25%) heights */
00478           double sine_lower_limit = 0.5;
00479           double linear_compression = 2;
00480           if (fheight <= sine_lower_limit) {
00481             /* Under the limit we do linear compression down */
00482             fheight = fheight / linear_compression;
00483           } else {
00484             double m = sine_lower_limit / linear_compression;
00485             /* Get sine_lower_limit..1 into -1..1 */
00486             fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
00487             /* Sine wave transform */
00488             fheight = sin(fheight * M_PI_2);
00489             /* Get -1..1 back to (sine_lower_limit / linear_compression)..1.0 */
00490             fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
00491           }
00492         }
00493         break;
00494 
00495       default:
00496         NOT_REACHED();
00497         break;
00498     }
00499     /* Transform it back into h_min..h_max space */
00500     *h = (height_t)(fheight * (h_max - h_min) + h_min);
00501     if (*h < 0) *h = I2H(0);
00502     if (*h >= h_max) *h = h_max - 1;
00503   }
00504 }
00505 
00507 static void HeightMapAdjustWaterLevel(amplitude_t water_percent, height_t h_max_new)
00508 {
00509   height_t h_min, h_max, h_avg, h_water_level;
00510   int water_tiles, desired_water_tiles;
00511   height_t *h;
00512   int *hist;
00513 
00514   HeightMapGetMinMaxAvg(&h_min, &h_max, &h_avg);
00515 
00516   /* Allocate histogram buffer and clear its cells */
00517   int *hist_buf = CallocT<int>(h_max - h_min + 1);
00518   /* Fill histogram */
00519   hist = HeightMapMakeHistogram(h_min, h_max, hist_buf);
00520 
00521   /* How many water tiles do we want? */
00522   desired_water_tiles = (int)(((int64)water_percent) * (int64)(_height_map.size_x * _height_map.size_y)) >> amplitude_decimal_bits;
00523 
00524   /* Raise water_level and accumulate values from histogram until we reach required number of water tiles */
00525   for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
00526     water_tiles += hist[h_water_level];
00527     if (water_tiles >= desired_water_tiles) break;
00528   }
00529 
00530   /* We now have the proper water level value.
00531    * Transform the height map into new (normalized) height map:
00532    *   values from range: h_min..h_water_level will become negative so it will be clamped to 0
00533    *   values from range: h_water_level..h_max are transformed into 0..h_max_new
00534    * , where h_max_new is 4, 8, 12 or 16 depending on terrain type (very flat, flat, hilly, mountains)
00535    */
00536   FOR_ALL_TILES_IN_HEIGHT(h) {
00537     /* Transform height from range h_water_level..h_max into 0..h_max_new range */
00538     *h = (height_t)(((int)h_max_new) * (*h - h_water_level) / (h_max - h_water_level)) + I2H(1);
00539     /* Make sure all values are in the proper range (0..h_max_new) */
00540     if (*h < 0) *h = I2H(0);
00541     if (*h >= h_max_new) *h = h_max_new - 1;
00542   }
00543 
00544   free(hist_buf);
00545 }
00546 
00547 static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime);
00548 
00569 static void HeightMapCoastLines(uint8 water_borders)
00570 {
00571   int smallest_size = min(_settings_game.game_creation.map_x, _settings_game.game_creation.map_y);
00572   const int margin = 4;
00573   uint y, x;
00574   double max_x;
00575   double max_y;
00576 
00577   /* Lower to sea level */
00578   for (y = 0; y <= _height_map.size_y; y++) {
00579     if (HasBit(water_borders, BORDER_NE)) {
00580       /* Top right */
00581       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);
00582       max_x = max((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
00583       if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
00584       for (x = 0; x < max_x; x++) {
00585         _height_map.height(x, y) = 0;
00586       }
00587     }
00588 
00589     if (HasBit(water_borders, BORDER_SW)) {
00590       /* Bottom left */
00591       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);
00592       max_x = max((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
00593       if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
00594       for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x); x--) {
00595         _height_map.height(x, y) = 0;
00596       }
00597     }
00598   }
00599 
00600   /* Lower to sea level */
00601   for (x = 0; x <= _height_map.size_x; x++) {
00602     if (HasBit(water_borders, BORDER_NW)) {
00603       /* Top left */
00604       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);
00605       max_y = max((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
00606       if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
00607       for (y = 0; y < max_y; y++) {
00608         _height_map.height(x, y) = 0;
00609       }
00610     }
00611 
00612     if (HasBit(water_borders, BORDER_SE)) {
00613       /* Bottom right */
00614       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);
00615       max_y = max((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
00616       if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
00617       for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y); y--) {
00618         _height_map.height(x, y) = 0;
00619       }
00620     }
00621   }
00622 }
00623 
00625 static void HeightMapSmoothCoastInDirection(int org_x, int org_y, int dir_x, int dir_y)
00626 {
00627   const int max_coast_dist_from_edge = 35;
00628   const int max_coast_Smooth_depth = 35;
00629 
00630   int x, y;
00631   int ed; // coast distance from edge
00632   int depth;
00633 
00634   height_t h_prev = 16;
00635   height_t h;
00636 
00637   assert(IsValidXY(org_x, org_y));
00638 
00639   /* Search for the coast (first non-water tile) */
00640   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++) {
00641     /* Coast found? */
00642     if (_height_map.height(x, y) > 15) break;
00643 
00644     /* Coast found in the neighborhood? */
00645     if (IsValidXY(x + dir_y, y + dir_x) && _height_map.height(x + dir_y, y + dir_x) > 0) break;
00646 
00647     /* Coast found in the neighborhood on the other side */
00648     if (IsValidXY(x - dir_y, y - dir_x) && _height_map.height(x - dir_y, y - dir_x) > 0) break;
00649   }
00650 
00651   /* Coast found or max_coast_dist_from_edge has been reached.
00652    * Soften the coast slope */
00653   for (depth = 0; IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
00654     h = _height_map.height(x, y);
00655     h = min(h, h_prev + (4 + depth)); // coast softening formula
00656     _height_map.height(x, y) = h;
00657     h_prev = h;
00658   }
00659 }
00660 
00662 static void HeightMapSmoothCoasts(uint8 water_borders)
00663 {
00664   uint x, y;
00665   /* First Smooth NW and SE coasts (y close to 0 and y close to size_y) */
00666   for (x = 0; x < _height_map.size_x; x++) {
00667     if (HasBit(water_borders, BORDER_NW)) HeightMapSmoothCoastInDirection(x, 0, 0, 1);
00668     if (HasBit(water_borders, BORDER_SE)) HeightMapSmoothCoastInDirection(x, _height_map.size_y - 1, 0, -1);
00669   }
00670   /* First Smooth NE and SW coasts (x close to 0 and x close to size_x) */
00671   for (y = 0; y < _height_map.size_y; y++) {
00672     if (HasBit(water_borders, BORDER_NE)) HeightMapSmoothCoastInDirection(0, y, 1, 0);
00673     if (HasBit(water_borders, BORDER_SW)) HeightMapSmoothCoastInDirection(_height_map.size_x - 1, y, -1, 0);
00674   }
00675 }
00676 
00684 static void HeightMapSmoothSlopes(height_t dh_max)
00685 {
00686   int x, y;
00687   for (y = 0; y <= (int)_height_map.size_y; y++) {
00688     for (x = 0; x <= (int)_height_map.size_x; x++) {
00689       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;
00690       if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
00691     }
00692   }
00693   for (y = _height_map.size_y; y >= 0; y--) {
00694     for (x = _height_map.size_x; x >= 0; x--) {
00695       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;
00696       if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
00697     }
00698   }
00699 }
00700 
00706 static void HeightMapNormalize()
00707 {
00708   const amplitude_t water_percent = _water_percent[_settings_game.difficulty.quantity_sea_lakes];
00709   const height_t h_max_new = I2H(_max_height[_settings_game.difficulty.terrain_type]);
00710   const height_t roughness = 7 + 3 * _settings_game.game_creation.tgen_smoothness;
00711 
00712   HeightMapAdjustWaterLevel(water_percent, h_max_new);
00713 
00714   byte water_borders = _settings_game.construction.freeform_edges ? _settings_game.game_creation.water_borders : 0xF;
00715   if (water_borders == BORDERS_RANDOM) water_borders = GB(Random(), 0, 4);
00716 
00717   HeightMapCoastLines(water_borders);
00718   HeightMapSmoothSlopes(roughness);
00719 
00720   HeightMapSmoothCoasts(water_borders);
00721   HeightMapSmoothSlopes(roughness);
00722 
00723   HeightMapSineTransform(12, h_max_new);
00724   HeightMapSmoothSlopes(16);
00725 }
00726 
00734 static double int_noise(const long x, const long y, const int prime)
00735 {
00736   long n = x + y * prime + _settings_game.game_creation.generation_seed;
00737 
00738   n = (n << 13) ^ n;
00739 
00740   /* Pseudo-random number generator, using several large primes */
00741   return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
00742 }
00743 
00744 
00751 static double smoothed_noise(const int x, const int y, const int prime)
00752 {
00753 #if 0
00754   /* A hilly world (four corner smooth) */
00755   const double sides = int_noise(x - 1, y) + int_noise(x + 1, y) + int_noise(x, y - 1) + int_noise(x, y + 1);
00756   const double center  =  int_noise(x, y);
00757   return (sides + sides + center * 4) / 8.0;
00758 #endif
00759 
00760   /* This gives very hilly world */
00761   return int_noise(x, y, prime);
00762 }
00763 
00764 
00768 static inline double linear_interpolate(const double a, const double b, const double x)
00769 {
00770   return a + x * (b - a);
00771 }
00772 
00773 
00778 static double interpolated_noise(const double x, const double y, const int prime)
00779 {
00780   const int integer_X = (int)x;
00781   const int integer_Y = (int)y;
00782 
00783   const double fractional_X = x - (double)integer_X;
00784   const double fractional_Y = y - (double)integer_Y;
00785 
00786   const double v1 = smoothed_noise(integer_X,     integer_Y,     prime);
00787   const double v2 = smoothed_noise(integer_X + 1, integer_Y,     prime);
00788   const double v3 = smoothed_noise(integer_X,     integer_Y + 1, prime);
00789   const double v4 = smoothed_noise(integer_X + 1, integer_Y + 1, prime);
00790 
00791   const double i1 = linear_interpolate(v1, v2, fractional_X);
00792   const double i2 = linear_interpolate(v3, v4, fractional_X);
00793 
00794   return linear_interpolate(i1, i2, fractional_Y);
00795 }
00796 
00797 
00804 static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime)
00805 {
00806   double total = 0.0;
00807   int i;
00808 
00809   for (i = 0; i < 6; i++) {
00810     const double frequency = (double)(1 << i);
00811     const double amplitude = pow(p, (double)i);
00812 
00813     total += interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;
00814   }
00815 
00816   return total;
00817 }
00818 
00819 
00821 static void TgenSetTileHeight(TileIndex tile, int height)
00822 {
00823   SetTileHeight(tile, height);
00824 
00825   /* Only clear the tiles within the map area. */
00826   if (TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY() &&
00827       (!_settings_game.construction.freeform_edges || (TileX(tile) != 0 && TileY(tile) != 0))) {
00828     MakeClear(tile, CLEAR_GRASS, 3);
00829   }
00830 }
00831 
00839 void GenerateTerrainPerlin()
00840 {
00841   uint x, y;
00842 
00843   if (!AllocHeightMap()) return;
00844   GenerateWorldSetAbortCallback(FreeHeightMap);
00845 
00846   HeightMapGenerate();
00847 
00848   IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
00849 
00850   HeightMapNormalize();
00851 
00852   IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
00853 
00854   /* First make sure the tiles at the north border are void tiles if needed. */
00855   if (_settings_game.construction.freeform_edges) {
00856     for (y = 0; y < _height_map.size_y - 1; y++) MakeVoid(_height_map.size_x * y);
00857     for (x = 0; x < _height_map.size_x;     x++) MakeVoid(x);
00858   }
00859 
00860   /* Transfer height map into OTTD map */
00861   for (y = 0; y < _height_map.size_y; y++) {
00862     for (x = 0; x < _height_map.size_x; x++) {
00863       int height = H2I(_height_map.height(x, y));
00864       if (height < 0) height = 0;
00865       if (height > 15) height = 15;
00866       TgenSetTileHeight(TileXY(x, y), height);
00867     }
00868   }
00869 
00870   IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
00871 
00872   FreeHeightMap();
00873   GenerateWorldSetAbortCallback(NULL);
00874 }

Generated on Mon Mar 9 23:33:52 2009 for openttd by  doxygen 1.5.6