src/tgp.cpp
author Tero Marttila <terom@fixme.fi>
Fri, 19 Dec 2008 02:13:39 +0200
changeset 10440 0a91ea45b0e8
parent 10438 51bff16a04c9
permissions -rw-r--r--
adjust the random land gen a bit to work with mini-maps
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     1
/* $Id$ */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     2
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
     3
/** @file tgp.cpp OTTD Perlin Noise Landscape Generator, aka TerraGenesis Perlin */
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
     4
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     5
#include "stdafx.h"
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     6
#include <math.h>
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     7
#include "openttd.h"
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     8
#include "clear_map.h"
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     9
#include "clear_map.h"
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    10
#include "variables.h"
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    11
#include "void_map.h"
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    12
#include "tgp.h"
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    13
#include "genworld.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8108
diff changeset
    14
#include "core/alloc_func.hpp"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    15
#include "core/random_func.hpp"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8264
diff changeset
    16
#include "settings_type.h"
9126
5648d696456b (svn r12986) -Codechange: move the landscape and transport related types from openttd.h to their own headers.
rubidium
parents: 8920
diff changeset
    17
#include "landscape_type.h"
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    18
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8139
diff changeset
    19
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8139
diff changeset
    20
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    21
/*
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    22
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    23
 * Quickie guide to Perlin Noise
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    24
 * Perlin noise is a predictable pseudo random number sequence. By generating
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    25
 * it in 2 dimensions, it becomes a useful random map, that for a given seed
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    26
 * and starting X & Y is entirely predictable. On the face of it, that may not
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    27
 * be useful. However, it means that if you want to replay a map in a different
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    28
 * terrain, or just vary the sea level, you just re-run the generator with the
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    29
 * same seed. The seed is an int32, and is randomised on each run of New Game.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    30
 * The Scenario Generator does not randomise the value, so that you can
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    31
 * experiment with one terrain until you are happy, or click "Random" for a new
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    32
 * random seed.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    33
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    34
 * Perlin Noise is a series of "octaves" of random noise added together. By
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    35
 * reducing the amplitude of the noise with each octave, the first octave of
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    36
 * noise defines the main terrain sweep, the next the ripples on that, and the
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    37
 * next the ripples on that. I use 6 octaves, with the amplitude controlled by
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    38
 * a power ratio, usually known as a persistence or p value. This I vary by the
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    39
 * smoothness selection, as can be seen in the table below. The closer to 1,
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    40
 * the more of that octave is added. Each octave is however raised to the power
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    41
 * of its position in the list, so the last entry in the "smooth" row, 0.35, is
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    42
 * raised to the power of 6, so can only add 0.001838...  of the amplitude to
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    43
 * the running total.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    44
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    45
 * In other words; the first p value sets the general shape of the terrain, the
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    46
 * second sets the major variations to that, ... until finally the smallest
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    47
 * bumps are added.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    48
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    49
 * Usefully, this routine is totally scaleable; so when 32bpp comes along, the
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    50
 * terrain can be as bumpy as you like! It is also infinitely expandable; a
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    51
 * single random seed terrain continues in X & Y as far as you care to
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    52
 * calculate. In theory, we could use just one seed value, but randomly select
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    53
 * where in the Perlin XY space we use for the terrain. Personally I prefer
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    54
 * using a simple (0, 0) to (X, Y), with a varying seed.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    55
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    56
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    57
 * Other things i have had to do: mountainous wasnt mountainous enough, and
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    58
 * since we only have 0..15 heights available, I add a second generated map
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    59
 * (with a modified seed), onto the original. This generally raises the
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    60
 * terrain, which then needs scaling back down. Overall effect is a general
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    61
 * uplift.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    62
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    63
 * However, the values on the top of mountains are then almost guaranteed to go
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    64
 * too high, so large flat plateaus appeared at height 15. To counter this, I
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    65
 * scale all heights above 12 to proportion up to 15. It still makes the
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    66
 * mountains have flatish tops, rather than craggy peaks, but at least they
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    67
 * arent smooth as glass.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    68
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    69
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    70
 * For a full discussion of Perlin Noise, please visit:
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    71
 * http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    72
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    73
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    74
 * Evolution II
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    75
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    76
 * The algorithm as described in the above link suggests to compute each tile height
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    77
 * as composition of several noise waves. Some of them are computed directly by
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    78
 * noise(x, y) function, some are calculated using linear approximation. Our
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    79
 * first implementation of perlin_noise_2D() used 4 noise(x, y) calls plus
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    80
 * 3 linear interpolations. It was called 6 times for each tile. This was a bit
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    81
 * CPU expensive.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    82
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    83
 * The following implementation uses optimized algorithm that should produce
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    84
 * the same quality result with much less computations, but more memory accesses.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    85
 * The overal speedup should be 300% to 800% depending on CPU and memory speed.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    86
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    87
 * I will try to explain it on the example below:
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    88
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    89
 * Have a map of 4 x 4 tiles, our simplifiead noise generator produces only two
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    90
 * values -1 and +1, use 3 octaves with wave lenght 1, 2 and 4, with amplitudes
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    91
 * 3, 2, 1. Original algorithm produces:
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    92
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    93
 * 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
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    94
 * 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
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    95
 * 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
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    96
 * 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
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    97
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    98
 * 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
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    99
 * 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
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   100
 * 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
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   101
 * 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
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   102
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   103
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   104
 * Optimization 1:
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   105
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   106
 * 1) we need to allocate a bit more tiles: (size_x + 1) * (size_y + 1) = (5 * 5):
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   107
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   108
 * 2) setup corner values using amplitude 3
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   109
 * {    -3.0        X          X          X          3.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   110
 * {     X          X          X          X          X     }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   111
 * {     X          X          X          X          X     }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   112
 * {     X          X          X          X          X     }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   113
 * {     3.0        X          X          X         -3.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   114
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   115
 * 3a) interpolate values in the middle
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   116
 * {    -3.0        X          0.0        X          3.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   117
 * {     X          X          X          X          X     }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   118
 * {     0.0        X          0.0        X          0.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   119
 * {     X          X          X          X          X     }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   120
 * {     3.0        X          0.0        X         -3.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   121
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   122
 * 3b) add patches with amplitude 2 to them
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   123
 * {    -5.0        X          2.0        X          1.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   124
 * {     X          X          X          X          X     }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   125
 * {     2.0        X         -2.0        X          2.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   126
 * {     X          X          X          X          X     }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   127
 * {     1.0        X          2.0        X         -5.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   128
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   129
 * 4a) interpolate values in the middle
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   130
 * {    -5.0       -1.5        2.0        1.5        1.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   131
 * {    -1.5       -0.75       0.0        0.75       1.5   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   132
 * {     2.0        0.0       -2.0        0.0        2.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   133
 * {     1.5        0.75       0.0       -0.75      -1.5   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   134
 * {     1.0        1.5        2.0       -1.5       -5.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   135
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   136
 * 4b) add patches with amplitude 1 to them
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   137
 * {    -6.0       -0.5        1.0        2.5        0.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   138
 * {    -0.5       -1.75       1.0       -0.25       2.5   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   139
 * {     1.0        1.0       -3.0        1.0        1.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   140
 * {     2.5       -0.25       1.0       -1.75      -0.5   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   141
 * {     0.0        2.5        1.0       -0.5       -6.0   }
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   142
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   143
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   144
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   145
 * Optimization 2:
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   146
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   147
 * As you can see above, each noise function was called just once. Therefore
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   148
 * we don't need to use noise function that calculates the noise from x, y and
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   149
 * some prime. The same quality result we can obtain using standard Random()
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   150
 * function instead.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   151
 *
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   152
 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   153
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   154
#ifndef M_PI_2
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   155
#define M_PI_2 1.57079632679489661923
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   156
#define M_PI   3.14159265358979323846
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   157
#endif /* M_PI_2 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   158
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   159
/** Fixed point type for heights */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   160
typedef int16 height_t;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   161
static const int height_decimal_bits = 4;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   162
static const height_t _invalid_height = -32768;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   163
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   164
/** Fixed point array for amplitudes (and percent values) */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   165
typedef int amplitude_t;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   166
static const int amplitude_decimal_bits = 10;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   167
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   168
/** Height map - allocated array of heights (MapSizeX() + 1) x (MapSizeY() + 1) */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   169
struct HeightMap
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   170
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   171
	height_t *h;         //< array of heights
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   172
	uint     dim_x;      //< height map size_x MapSizeX() + 1
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   173
	uint     total_size; //< height map total size
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   174
	uint     size_x;     //< MapSizeX()
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   175
	uint     size_y;     //< MapSizeY()
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   176
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   177
	/**
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   178
	 * Height map accessor
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   179
	 * @param x X position
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   180
	 * @param y Y position
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   181
	 * @return height as fixed point number
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   182
	 */
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   183
	inline height_t &height(uint x, uint y) {
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   184
		return h[x + y * dim_x];
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   185
	}
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   186
};
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   187
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   188
/** Global height map instance */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   189
static HeightMap _height_map = {NULL, 0, 0, 0, 0};
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   190
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   191
/** Conversion: int to height_t */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   192
#define I2H(i) ((i) << height_decimal_bits)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   193
/** Conversion: height_t to int */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   194
#define H2I(i) ((i) >> height_decimal_bits)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   195
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   196
/** Conversion: int to amplitude_t */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   197
#define I2A(i) ((i) << amplitude_decimal_bits)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   198
/** Conversion: amplitude_t to int */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   199
#define A2I(i) ((i) >> amplitude_decimal_bits)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   200
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   201
/** Conversion: amplitude_t to height_t */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   202
#define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   203
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   204
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   205
/** Walk through all items of _height_map.h */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   206
#define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   207
9615
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   208
/** Maximum index into array of noise amplitudes */
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   209
static const int TGP_FREQUENCY_MAX = 6;
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   210
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   211
/** Noise amplitudes (multiplied by 1024)
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4300
diff changeset
   212
 * - indexed by "smoothness setting" and log2(frequency) */
9615
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   213
static const amplitude_t _amplitudes_by_smoothness_and_frequency[4][TGP_FREQUENCY_MAX + 1] = {
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   214
	/* lowest frequncy....  ...highest (every corner) */
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   215
	/* Very smooth */
9615
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   216
	{16000,  5600,  1968,   688,   240,    16,    16},
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   217
	/* Smooth */
9615
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   218
	{16000, 16000,  6448,  3200,  1024,   128,    16},
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   219
	/* Rough */
9615
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   220
	{16000, 19200, 12800,  8000,  3200,   256,    64},
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   221
	/* Very Rough */
9615
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   222
	{24000, 16000, 19200, 16000,  8000,   512,   320},
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   223
};
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   224
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   225
/** Desired water percentage (100% == 1024) - indexed by _settings_game.difficulty.quantity_sea_lakes */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   226
static const amplitude_t _water_percent[4] = {20, 80, 250, 400};
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   227
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   228
/** Desired maximum height - indexed by _settings_game.difficulty.terrain_type */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   229
static const int8 _max_height[4] = {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   230
	6,       ///< Very flat
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   231
	9,       ///< Flat
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   232
	12,      ///< Hilly
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   233
	15       ///< Mountainous
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   234
};
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   235
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   236
/** Check if a X/Y set are within the map.
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   237
 * @param x coordinate x
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   238
 * @param y coordinate y
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   239
 * @return true if within the map
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   240
 */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   241
static inline bool IsValidXY(uint x, uint y)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   242
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   243
	return ((int)x) >= 0 && x < _height_map.size_x && ((int)y) >= 0 && y < _height_map.size_y;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   244
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   245
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   246
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   247
/**
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   248
 * Allocate array of (MapSizeX()+1)*(MapSizeY()+1) heights and init the _height_map structure members
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   249
 * @return true on success
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   250
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   251
static inline bool AllocHeightMap()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   252
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   253
	height_t *h;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   254
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   255
	_height_map.size_x = MapSizeX();
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   256
	_height_map.size_y = MapSizeY();
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   257
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   258
	/* Allocate memory block for height map row pointers */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   259
	_height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   260
	_height_map.dim_x = _height_map.size_x + 1;
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5601
diff changeset
   261
	_height_map.h = CallocT<height_t>(_height_map.total_size);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   262
	if (_height_map.h == NULL) return false;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   263
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   264
	/* Iterate through height map initialize values */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   265
	FOR_ALL_TILES_IN_HEIGHT(h) *h = _invalid_height;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   266
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   267
	return true;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   268
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   269
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   270
/** Free height map */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   271
static inline void FreeHeightMap()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   272
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   273
	if (_height_map.h == NULL) return;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   274
	free(_height_map.h);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   275
	_height_map.h = NULL;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   276
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   277
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   278
/**
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   279
 * Generates new random height in given amplitude (generated numbers will range from - amplitude to + amplitude)
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   280
 * @param rMax Limit of result
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   281
 * @return generated height
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   282
 */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   283
static inline height_t RandomHeight(amplitude_t rMax)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   284
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   285
	amplitude_t ra = (Random() << 16) | (Random() & 0x0000FFFF);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   286
	height_t rh;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   287
	/* Spread height into range -rMax..+rMax */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   288
	rh = A2H(ra % (2 * rMax + 1) - rMax);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   289
	return rh;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   290
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   291
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   292
/**
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   293
 * One interpolation and noise round
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   294
 *
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   295
 * The heights on the map are generated in an iterative process.
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   296
 * We start off with a frequency of 1 (log_frequency == 0), and generate heights only for corners on the most coarsly mesh
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   297
 * (i.e. only for x/y coordinates which are multiples of the minimum edge length).
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   298
 *
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   299
 * After this initial step the frequency is doubled (log_frequency incremented) each iteration to generate corners on the next finer mesh.
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   300
 * The heights of the newly added corners are first set by interpolating the heights from the previous iteration.
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   301
 * Finally noise with the given amplitude is applied to all corners of the new mesh.
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   302
 *
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   303
 * Generation terminates, when the frequency has reached the map size. I.e. the mesh is as fine as the map, and every corner height
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   304
 * has been set.
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   305
 *
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   306
 * @param log_frequency frequency (logarithmic) to apply noise for
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   307
 * @param amplitude Amplitude for the noise
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   308
 * @return false if we are finished (reached the minimal step size / highest frequency)
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   309
 */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   310
static bool ApplyNoise(uint log_frequency, amplitude_t amplitude)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   311
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   312
	uint size_min = min(_height_map.size_x, _height_map.size_y);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   313
	uint step = size_min >> log_frequency;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   314
	uint x, y;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   315
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   316
	/* Trying to apply noise to uninitialized height map */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   317
	assert(_height_map.h != NULL);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   318
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   319
	/* Are we finished? */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   320
	if (step == 0) return false;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   321
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   322
	if (log_frequency == 0) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   323
		/* This is first round, we need to establish base heights with step = size_min */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   324
		for (y = 0; y <= _height_map.size_y; y += step) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   325
			for (x = 0; x <= _height_map.size_x; x += step) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   326
				height_t height = (amplitude > 0) ? RandomHeight(amplitude) : 0;
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   327
				_height_map.height(x, y) = height;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   328
			}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   329
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   330
		return true;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   331
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   332
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   333
	/* It is regular iteration round.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   334
	 * Interpolate height values at odd x, even y tiles */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   335
	for (y = 0; y <= _height_map.size_y; y += 2 * step) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   336
		for (x = 0; x < _height_map.size_x; x += 2 * step) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   337
			height_t h00 = _height_map.height(x + 0 * step, y);
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   338
			height_t h02 = _height_map.height(x + 2 * step, y);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   339
			height_t h01 = (h00 + h02) / 2;
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   340
			_height_map.height(x + 1 * step, y) = h01;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   341
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   342
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   343
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   344
	/* Interpolate height values at odd y tiles */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   345
	for (y = 0; y < _height_map.size_y; y += 2 * step) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   346
		for (x = 0; x <= _height_map.size_x; x += step) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   347
			height_t h00 = _height_map.height(x, y + 0 * step);
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   348
			height_t h20 = _height_map.height(x, y + 2 * step);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   349
			height_t h10 = (h00 + h20) / 2;
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   350
			_height_map.height(x, y + 1 * step) = h10;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   351
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   352
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   353
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   354
	/* Add noise for next higher frequency (smaller steps) */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   355
	for (y = 0; y <= _height_map.size_y; y += step) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   356
		for (x = 0; x <= _height_map.size_x; x += step) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   357
			_height_map.height(x, y) += RandomHeight(amplitude);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   358
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   359
	}
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   360
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   361
	return (step > 1);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   362
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   363
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   364
/** Base Perlin noise generator - fills height map with raw Perlin noise */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   365
static void HeightMapGenerate()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   366
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   367
	uint size_min = min(_height_map.size_x, _height_map.size_y);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   368
	uint iteration_round = 0;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   369
	amplitude_t amplitude;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   370
	bool continue_iteration;
9615
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   371
	int log_size_min, log_frequency_min;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   372
	int log_frequency;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   373
9615
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   374
	/* Find first power of two that fits, so that later log_frequency == TGP_FREQUENCY_MAX in the last iteration */
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   375
	for (log_size_min = 2; (1U << log_size_min) < size_min; log_size_min++) { }
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   376
	log_frequency_min = log_size_min - 2;
9615
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   377
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   378
	/* Zero must be part of the iteration, else initialization will fail. */
d532e3ba44c0 (svn r13671) -Cleanup: Zeros for sell.
frosch
parents: 9613
diff changeset
   379
	assert(log_frequency_min >= 0);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   380
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   381
	/* Keep increasing the frequency until we reach the step size equal to one tile */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   382
	do {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   383
		log_frequency = iteration_round - log_frequency_min;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   384
		if (log_frequency >= 0) {
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   385
			/* Apply noise for the next frequency */
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   386
			assert(log_frequency <= 2);
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   387
			amplitude = _amplitudes_by_smoothness_and_frequency[_settings_game.game_creation.tgen_smoothness][log_frequency];
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   388
		} else {
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   389
			/* Amplitude for the low frequencies on big maps is 0, i.e. initialise with zero height */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   390
			amplitude = 0;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   391
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   392
		continue_iteration = ApplyNoise(iteration_round, amplitude);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   393
		iteration_round++;
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   394
	} while (continue_iteration);
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   395
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   396
	assert(log_frequency == 2);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   397
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   398
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   399
/** Returns min, max and average height from height map */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   400
static void HeightMapGetMinMaxAvg(height_t *min_ptr, height_t *max_ptr, height_t *avg_ptr)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   401
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   402
	height_t h_min, h_max, h_avg, *h;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   403
	int64 h_accu = 0;
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   404
	h_min = h_max = _height_map.height(0, 0);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   405
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   406
	/* Get h_min, h_max and accumulate heights into h_accu */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   407
	FOR_ALL_TILES_IN_HEIGHT(h) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   408
		if (*h < h_min) h_min = *h;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   409
		if (*h > h_max) h_max = *h;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   410
		h_accu += *h;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   411
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   412
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   413
	/* Get average height */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   414
	h_avg = (height_t)(h_accu / (_height_map.size_x * _height_map.size_y));
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   415
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   416
	/* Return required results */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   417
	if (min_ptr != NULL) *min_ptr = h_min;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   418
	if (max_ptr != NULL) *max_ptr = h_max;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   419
	if (avg_ptr != NULL) *avg_ptr = h_avg;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   420
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   421
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   422
/** Dill histogram and return pointer to its base point - to the count of zero heights */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   423
static int *HeightMapMakeHistogram(height_t h_min, height_t h_max, int *hist_buf)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   424
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   425
	int *hist = hist_buf - h_min;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   426
	height_t *h;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   427
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   428
	/* Count the heights and fill the histogram */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   429
	FOR_ALL_TILES_IN_HEIGHT(h) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   430
		assert(*h >= h_min);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   431
		assert(*h <= h_max);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   432
		hist[*h]++;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   433
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   434
	return hist;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   435
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   436
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   437
/** Applies sine wave redistribution onto height map */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   438
static void HeightMapSineTransform(height_t h_min, height_t h_max)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   439
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   440
	height_t *h;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   441
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   442
	FOR_ALL_TILES_IN_HEIGHT(h) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   443
		double fheight;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   444
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   445
		if (*h < h_min) continue;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   446
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   447
		/* Transform height into 0..1 space */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   448
		fheight = (double)(*h - h_min) / (double)(h_max - h_min);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   449
		/* Apply sine transform depending on landscape type */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   450
		switch(_settings_game.game_creation.landscape) {
6357
f0f5e7d1713c (svn r9400) -Codechange: Use some more representative enum names for landscape types.
belugas
parents: 6248
diff changeset
   451
			case LT_TOYLAND:
f0f5e7d1713c (svn r9400) -Codechange: Use some more representative enum names for landscape types.
belugas
parents: 6248
diff changeset
   452
			case LT_TEMPERATE:
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   453
				/* Move and scale 0..1 into -1..+1 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   454
				fheight = 2 * fheight - 1;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   455
				/* Sine transform */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   456
				fheight = sin(fheight * M_PI_2);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   457
				/* Transform it back from -1..1 into 0..1 space */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   458
				fheight = 0.5 * (fheight + 1);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   459
				break;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   460
6357
f0f5e7d1713c (svn r9400) -Codechange: Use some more representative enum names for landscape types.
belugas
parents: 6248
diff changeset
   461
			case LT_ARCTIC:
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   462
				{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   463
					/* Arctic terrain needs special height distribution.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   464
					 * Redistribute heights to have more tiles at highest (75%..100%) range */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   465
					double sine_upper_limit = 0.75;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   466
					double linear_compression = 2;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   467
					if (fheight >= sine_upper_limit) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   468
						/* Over the limit we do linear compression up */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   469
						fheight = 1.0 - (1.0 - fheight) / linear_compression;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   470
					} else {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   471
						double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   472
						/* Get 0..sine_upper_limit into -1..1 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   473
						fheight = 2.0 * fheight / sine_upper_limit - 1.0;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   474
						/* Sine wave transform */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   475
						fheight = sin(fheight * M_PI_2);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   476
						/* Get -1..1 back to 0..(1 - (1 - sine_upper_limit) / linear_compression) == 0.0..m */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   477
						fheight = 0.5 * (fheight + 1.0) * m;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   478
					}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   479
				}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   480
				break;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   481
6357
f0f5e7d1713c (svn r9400) -Codechange: Use some more representative enum names for landscape types.
belugas
parents: 6248
diff changeset
   482
			case LT_TROPIC:
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   483
				{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   484
					/* Desert terrain needs special height distribution.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   485
					 * Half of tiles should be at lowest (0..25%) heights */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   486
					double sine_lower_limit = 0.5;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   487
					double linear_compression = 2;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   488
					if (fheight <= sine_lower_limit) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   489
						/* Under the limit we do linear compression down */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   490
						fheight = fheight / linear_compression;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   491
					} else {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   492
						double m = sine_lower_limit / linear_compression;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   493
						/* Get sine_lower_limit..1 into -1..1 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   494
						fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   495
						/* Sine wave transform */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   496
						fheight = sin(fheight * M_PI_2);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   497
						/* Get -1..1 back to (sine_lower_limit / linear_compression)..1.0 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   498
						fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   499
					}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   500
				}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   501
				break;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   502
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   503
			default:
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   504
				NOT_REACHED();
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   505
				break;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   506
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   507
		/* Transform it back into h_min..h_max space */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   508
		*h = (height_t)(fheight * (h_max - h_min) + h_min);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   509
		if (*h < 0) *h = I2H(0);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   510
		if (*h >= h_max) *h = h_max - 1;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   511
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   512
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   513
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   514
/** Adjusts heights in height map to contain required amount of water tiles */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   515
static void HeightMapAdjustWaterLevel(amplitude_t water_percent, height_t h_max_new)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   516
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   517
	height_t h_min, h_max, h_avg, h_water_level;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   518
	int water_tiles, desired_water_tiles;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   519
	height_t *h;
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5601
diff changeset
   520
	int *hist;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   521
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   522
	HeightMapGetMinMaxAvg(&h_min, &h_max, &h_avg);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   523
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   524
	/* Allocate histogram buffer and clear its cells */
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5601
diff changeset
   525
	int *hist_buf = CallocT<int>(h_max - h_min + 1);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   526
	/* Fill histogram */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   527
	hist = HeightMapMakeHistogram(h_min, h_max, hist_buf);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   528
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   529
	/* How many water tiles do we want? */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   530
	desired_water_tiles = (int)(((int64)water_percent) * (int64)(_height_map.size_x * _height_map.size_y)) >> amplitude_decimal_bits;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   531
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   532
	/* Raise water_level and accumulate values from histogram until we reach required number of water tiles */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   533
	for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   534
		water_tiles += hist[h_water_level];
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   535
		if (water_tiles >= desired_water_tiles) break;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   536
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   537
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   538
	/* We now have the proper water level value.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   539
	 * Transform the height map into new (normalized) height map:
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   540
	 *   values from range: h_min..h_water_level will become negative so it will be clamped to 0
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   541
	 *   values from range: h_water_level..h_max are transformed into 0..h_max_new
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   542
	 * , where h_max_new is 4, 8, 12 or 16 depending on terrain type (very flat, flat, hilly, mountains)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   543
	 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   544
	FOR_ALL_TILES_IN_HEIGHT(h) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   545
		/* Transform height from range h_water_level..h_max into 0..h_max_new range */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   546
		*h = (height_t)(((int)h_max_new) * (*h - h_water_level) / (h_max - h_water_level)) + I2H(1);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   547
		/* Make sure all values are in the proper range (0..h_max_new) */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   548
		if (*h < 0) *h = I2H(0);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   549
		if (*h >= h_max_new) *h = h_max_new - 1;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   550
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   551
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   552
	free(hist_buf);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   553
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   554
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   555
static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   556
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   557
/**
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   558
 * This routine sculpts in from the edge a random amount, again a Perlin
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   559
 * sequence, to avoid the rigid flat-edge slopes that were present before. The
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   560
 * Perlin noise map doesnt know where we are going to slice across, and so we
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   561
 * often cut straight through high terrain. the smoothing routine makes it
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   562
 * legal, gradually increasing up from the edge to the original terrain height.
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   563
 * By cutting parts of this away, it gives a far more irregular edge to the
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   564
 * map-edge. Sometimes it works beautifully with the existing sea & lakes, and
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   565
 * creates a very realistic coastline. Other times the variation is less, and
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   566
 * the map-edge shows its cliff-like roots.
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   567
 *
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   568
 * This routine may be extended to randomly sculpt the height of the terrain
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   569
 * near the edge. This will have the coast edge at low level (1-3), rising in
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   570
 * smoothed steps inland to about 15 tiles in. This should make it look as
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   571
 * though the map has been built for the map size, rather than a slice through
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   572
 * a larger map.
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   573
 *
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   574
 * Please note that all the small numbers; 53, 101, 167, etc. are small primes
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   575
 * to help give the perlin noise a bit more of a random feel.
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   576
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   577
static void HeightMapCoastLines()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   578
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   579
	int smallest_size = min(_settings_game.game_creation.map_x, _settings_game.game_creation.map_y);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   580
	const int margin = 4;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   581
	uint y, x;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   582
	double max_x;
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   583
	double max_y;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   584
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   585
    int factor = smallest_size * smallest_size / 16;
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   586
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   587
	/* Lower to sea level */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   588
	for (y = 0; y <= _height_map.size_y; y++) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   589
		/* Top right */
7923
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 6422
diff changeset
   590
		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);
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   591
		max_x = max(factor + max_x, factor + margin - max_x);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   592
		if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   593
        if (smallest_size < 6 && max_x > 4) max_x /= 4.0;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   594
		for (x = 0; x < max_x; x++) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   595
			_height_map.height(x, y) = 0;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   596
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   597
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   598
		/* Bottom left */
7923
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 6422
diff changeset
   599
		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);
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   600
		max_x = max(factor + max_x, factor + margin - max_x);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   601
		if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   602
        if (smallest_size < 6 && max_x > 4) max_x /= 4.0;
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   603
		for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x) && x > 0; x--) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   604
			_height_map.height(x, y) = 0;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   605
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   606
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   607
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   608
	/* Lower to sea level */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   609
	for (x = 0; x <= _height_map.size_x; x++) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   610
		/* Top left */
7923
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 6422
diff changeset
   611
		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);
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   612
		max_y = max(factor + max_y, factor + margin - max_y);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   613
		if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   614
        if (smallest_size < 6 && max_y > 4) max_y /= 4.0;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   615
		for (y = 0; y < max_y; y++) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   616
			_height_map.height(x, y) = 0;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   617
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   618
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   619
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   620
		/* Bottom right */
7923
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 6422
diff changeset
   621
		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);
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   622
		max_y = max(factor + max_y, factor + margin - max_y);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   623
		if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   624
        if (smallest_size < 6 && max_y > 4) max_y /= 4.0;
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   625
		for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y) && y > 0; y--) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   626
			_height_map.height(x, y) = 0;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   627
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   628
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   629
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   630
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   631
/** Start at given point, move in given direction, find and Smooth coast in that direction */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   632
static void HeightMapSmoothCoastInDirection(int org_x, int org_y, int dir_x, int dir_y)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   633
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   634
	const int max_coast_dist_from_edge = 35;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   635
	const int max_coast_Smooth_depth = 35;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   636
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   637
	int x, y;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   638
	int ed; // coast distance from edge
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   639
	int depth;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   640
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   641
	height_t h_prev = 16;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   642
	height_t h;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   643
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   644
	assert(IsValidXY(org_x, org_y));
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   645
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   646
	/* Search for the coast (first non-water tile) */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   647
	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++) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   648
		/* Coast found? */
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   649
		if (_height_map.height(x, y) > 15) break;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   650
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   651
		/* Coast found in the neighborhood? */
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   652
		if (IsValidXY(x + dir_y, y + dir_x) && _height_map.height(x + dir_y, y + dir_x) > 0) break;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   653
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   654
		/* Coast found in the neighborhood on the other side */
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   655
		if (IsValidXY(x - dir_y, y - dir_x) && _height_map.height(x - dir_y, y - dir_x) > 0) break;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   656
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   657
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   658
	/* Coast found or max_coast_dist_from_edge has been reached.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   659
	 * Soften the coast slope */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   660
	for (depth = 0; IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   661
		h = _height_map.height(x, y);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   662
		h = min(h, h_prev + (4 + depth)); // coast softening formula
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   663
		_height_map.height(x, y) = h;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   664
		h_prev = h;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   665
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   666
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   667
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   668
/** Smooth coasts by modulating height of tiles close to map edges with cosine of distance from edge */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   669
static void HeightMapSmoothCoasts()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   670
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   671
	uint x, y;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   672
	/* First Smooth NW and SE coasts (y close to 0 and y close to size_y) */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   673
	for (x = 0; x < _height_map.size_x; x++) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   674
		HeightMapSmoothCoastInDirection(x, 0, 0, 1);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   675
		HeightMapSmoothCoastInDirection(x, _height_map.size_y - 1, 0, -1);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   676
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   677
	/* First Smooth NE and SW coasts (x close to 0 and x close to size_x) */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   678
	for (y = 0; y < _height_map.size_y; y++) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   679
		HeightMapSmoothCoastInDirection(0, y, 1, 0);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   680
		HeightMapSmoothCoastInDirection(_height_map.size_x - 1, y, -1, 0);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   681
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   682
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   683
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   684
/**
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   685
 * This routine provides the essential cleanup necessary before OTTD can
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   686
 * display the terrain. When generated, the terrain heights can jump more than
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   687
 * one level between tiles. This routine smooths out those differences so that
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   688
 * the most it can change is one level. When OTTD can support cliffs, this
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   689
 * routine may not be necessary.
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   690
 */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   691
static void HeightMapSmoothSlopes(height_t dh_max)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   692
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   693
	int x, y;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   694
	for (y = 1; y <= (int)_height_map.size_y; y++) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   695
		for (x = 1; x <= (int)_height_map.size_x; x++) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   696
			height_t h_max = min(_height_map.height(x - 1, y), _height_map.height(x, y - 1)) + dh_max;
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   697
			if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   698
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   699
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   700
	for (y = _height_map.size_y - 1; y >= 0; y--) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   701
		for (x = _height_map.size_x - 1; x >= 0; x--) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   702
			height_t h_max = min(_height_map.height(x + 1, y), _height_map.height(x, y + 1)) + dh_max;
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   703
			if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   704
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   705
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   706
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   707
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   708
/** Height map terraform post processing:
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   709
 *  - water level adjusting
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   710
 *  - coast Smoothing
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   711
 *  - slope Smoothing
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   712
 *  - height histogram redistribution by sine wave transform */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   713
static void HeightMapNormalize()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   714
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   715
	const amplitude_t water_percent = _water_percent[_settings_game.difficulty.quantity_sea_lakes];
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   716
	const height_t h_max_new = I2H(_max_height[_settings_game.difficulty.terrain_type]);
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   717
	const height_t roughness = 7 + 3 * _settings_game.game_creation.tgen_smoothness;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   718
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   719
	HeightMapAdjustWaterLevel(water_percent, h_max_new);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   720
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   721
	HeightMapCoastLines();
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   722
	HeightMapSmoothSlopes(roughness);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   723
10440
0a91ea45b0e8 adjust the random land gen a bit to work with mini-maps
Tero Marttila <terom@fixme.fi>
parents: 10438
diff changeset
   724
	HeightMapSmoothCoasts();
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   725
	HeightMapSmoothSlopes(roughness);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   726
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   727
	HeightMapSineTransform(12, h_max_new);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   728
	HeightMapSmoothSlopes(16);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   729
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   730
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   731
/**
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   732
 * The Perlin Noise calculation using large primes
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   733
 * The initial number is adjusted by two values; the generation_seed, and the
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   734
 * passed parameter; prime.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   735
 * prime is used to allow the perlin noise generator to create useful random
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   736
 * numbers from slightly different series.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   737
 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   738
static double int_noise(const long x, const long y, const int prime)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   739
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   740
	long n = x + y * prime + _settings_game.game_creation.generation_seed;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   741
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   742
	n = (n << 13) ^ n;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   743
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   744
	/* Pseudo-random number generator, using several large primes */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   745
	return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   746
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   747
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   748
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   749
/**
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   750
 * Hj. Malthaner's routine included 2 different noise smoothing methods.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   751
 * We now use the "raw" int_noise one.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   752
 * However, it may be useful to move to the other routine in future.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   753
 * So it is included too.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   754
 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   755
static double smoothed_noise(const int x, const int y, const int prime)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   756
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   757
#if 0
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   758
	/* A hilly world (four corner smooth) */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   759
	const double sides = int_noise(x - 1, y) + int_noise(x + 1, y) + int_noise(x, y - 1) + int_noise(x, y + 1);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   760
	const double center  =  int_noise(x, y);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   761
	return (sides + sides + center * 4) / 8.0;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   762
#endif
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   763
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   764
	/* This gives very hilly world */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   765
	return int_noise(x, y, prime);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   766
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   767
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   768
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   769
/**
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   770
 * This routine determines the interpolated value between a and b
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   771
 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   772
static inline double linear_interpolate(const double a, const double b, const double x)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   773
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   774
	return a + x * (b - a);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   775
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   776
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   777
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   778
/**
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   779
 * This routine returns the smoothed interpolated noise for an x and y, using
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   780
 * the values from the surrounding positions.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   781
 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   782
static double interpolated_noise(const double x, const double y, const int prime)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   783
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   784
	const int integer_X = (int)x;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   785
	const int integer_Y = (int)y;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   786
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   787
	const double fractional_X = x - (double)integer_X;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   788
	const double fractional_Y = y - (double)integer_Y;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   789
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   790
	const double v1 = smoothed_noise(integer_X,     integer_Y,     prime);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   791
	const double v2 = smoothed_noise(integer_X + 1, integer_Y,     prime);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   792
	const double v3 = smoothed_noise(integer_X,     integer_Y + 1, prime);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   793
	const double v4 = smoothed_noise(integer_X + 1, integer_Y + 1, prime);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   794
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   795
	const double i1 = linear_interpolate(v1, v2, fractional_X);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   796
	const double i2 = linear_interpolate(v3, v4, fractional_X);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   797
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   798
	return linear_interpolate(i1, i2, fractional_Y);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   799
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   800
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   801
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   802
/**
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   803
 * This is a similar function to the main perlin noise calculation, but uses
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   804
 * the value p passed as a parameter rather than selected from the predefined
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   805
 * sequences. as you can guess by its title, i use this to create the indented
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   806
 * coastline, which is just another perlin sequence.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   807
 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   808
static double perlin_coast_noise_2D(const double x, const double y, const double p, const int prime)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   809
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   810
	double total = 0.0;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   811
	int i;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   812
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   813
	for (i = 0; i < 6; i++) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   814
		const double frequency = (double)(1 << i);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   815
		const double amplitude = pow(p, (double)i);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   816
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   817
		total += interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   818
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   819
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   820
	return total;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   821
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   822
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   823
9613
75ca8f0c8c78 (svn r13667) -Documentation: Comment 'tgp.cpp' a bit. Based on patch by CommanderZ.
frosch
parents: 9606
diff changeset
   824
/** A small helper function to initialize the terrain */
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   825
static void TgenSetTileHeight(TileIndex tile, int height)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   826
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   827
	SetTileHeight(tile, height);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   828
	MakeClear(tile, CLEAR_GRASS, 3);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   829
}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   830
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   831
/**
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   832
 * The main new land generator using Perlin noise. Desert landscape is handled
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   833
 * different to all others to give a desert valley between two high mountains.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   834
 * Clearly if a low height terrain (flat/very flat) is chosen, then the tropic
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   835
 * areas wont be high enough, and there will be very little tropic on the map.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   836
 * Thus Tropic works best on Hilly or Mountainous.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   837
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   838
void GenerateTerrainPerlin()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   839
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   840
	uint x, y;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   841
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   842
	if (!AllocHeightMap()) return;
5145
d4a8882b32ef (svn r7237) -Fix: TGP landscape generation could leak memory if aborted during the generation of the heightmap.
rubidium
parents: 4549
diff changeset
   843
	GenerateWorldSetAbortCallback(FreeHeightMap);
d4a8882b32ef (svn r7237) -Fix: TGP landscape generation could leak memory if aborted during the generation of the heightmap.
rubidium
parents: 4549
diff changeset
   844
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   845
	HeightMapGenerate();
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   846
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   847
	IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   848
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   849
	HeightMapNormalize();
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   850
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   851
	IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   852
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   853
	/* Transfer height map into OTTD map */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   854
	for (y = 2; y < _height_map.size_y - 2; y++) {
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   855
		for (x = 2; x < _height_map.size_x - 2; x++) {
9603
40a8d2079ebe (svn r13645) -Codechange: Convert a macro into an inlined member function.
frosch
parents: 9413
diff changeset
   856
			int height = H2I(_height_map.height(x, y));
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   857
			if (height < 0) height = 0;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   858
			if (height > 15) height = 15;
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   859
			TgenSetTileHeight(TileXY(x, y), height);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   860
		}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   861
	}
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   862
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   863
	IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   864
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   865
	/* Recreate void tiles at the border in case they have been affected by generation */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   866
	for (y = 0; y < _height_map.size_y - 1; y++) MakeVoid(_height_map.size_x * y + _height_map.size_x - 1);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   867
	for (x = 0; x < _height_map.size_x;     x++) MakeVoid(_height_map.size_x * y + x);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   868
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   869
	FreeHeightMap();
5145
d4a8882b32ef (svn r7237) -Fix: TGP landscape generation could leak memory if aborted during the generation of the heightmap.
rubidium
parents: 4549
diff changeset
   870
	GenerateWorldSetAbortCallback(NULL);
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   871
}