Changeset - r9603:317cd54d5ce8
[Not reviewed]
master
0 1 0
frosch - 16 years ago 2008-06-27 15:01:18
frosch@openttd.org
(svn r13645) -Codechange: Convert a macro into an inlined member function.
1 file changed with 27 insertions and 32 deletions:
0 comments (0 inline, 0 general)
src/tgp.cpp
Show inline comments
 
@@ -152,56 +152,57 @@
 
 */
 

	
 
#ifndef M_PI_2
 
#define M_PI_2 1.57079632679489661923
 
#define M_PI   3.14159265358979323846
 
#endif /* M_PI_2 */
 

	
 
/** Fixed point type for heights */
 
typedef int16 height_t;
 
static const int height_decimal_bits = 4;
 
static const height_t _invalid_height = -32768;
 

	
 
/** Fixed point array for amplitudes (and percent values) */
 
typedef int amplitude_t;
 
static const int amplitude_decimal_bits = 10;
 

	
 
/** Height map - allocated array of heights (MapSizeX() + 1) x (MapSizeY() + 1) */
 
struct HeightMap
 
{
 
	height_t *h;         //< array of heights
 
	uint     dim_x;      //< height map size_x MapSizeX() + 1
 
	uint     total_size; //< height map total size
 
	uint     size_x;     //< MapSizeX()
 
	uint     size_y;     //< MapSizeY()
 

	
 
	inline height_t &height(uint x, uint y) {
 
		return h[x + y * dim_x];
 
	}
 
};
 

	
 
/** Global height map instance */
 
static HeightMap _height_map = {NULL, 0, 0, 0, 0};
 

	
 
/** Height map accessors */
 
#define HeightMapXY(x, y) _height_map.h[(x) + (y) * _height_map.dim_x]
 

	
 
/** Conversion: int to height_t */
 
#define I2H(i) ((i) << height_decimal_bits)
 
/** Conversion: height_t to int */
 
#define H2I(i) ((i) >> height_decimal_bits)
 

	
 
/** Conversion: int to amplitude_t */
 
#define I2A(i) ((i) << amplitude_decimal_bits)
 
/** Conversion: amplitude_t to int */
 
#define A2I(i) ((i) >> amplitude_decimal_bits)
 

	
 
/** Conversion: amplitude_t to height_t */
 
#define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
 

	
 

	
 
/** Walk through all items of _height_map.h */
 
#define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++)
 

	
 
/** Noise amplitudes (multiplied by 1024)
 
 * - indexed by "smoothness setting" and log2(frequency) */
 
static const amplitude_t _amplitudes_by_smoothness_and_frequency[4][12] = {
 
	/* Very smooth */
 
	{1000,  350,  123,   43,   15,    1,     1,    0,    0,    0,    0,    0},
 
	/* Smooth */
 
	{1000, 1000,  403,  200,   64,    8,     1,    0,    0,    0,    0,    0},
 
@@ -269,115 +270,115 @@ static inline height_t RandomHeight(ampl
 
	/* Scale the amplitude for better resolution */
 
	rMax *= 16;
 
	/* Spread height into range -rMax..+rMax */
 
	rh = A2H(ra % (2 * rMax + 1) - rMax);
 
	return rh;
 
}
 

	
 
/** One interpolation and noise round */
 
static bool ApplyNoise(uint log_frequency, amplitude_t amplitude)
 
{
 
	uint size_min = min(_height_map.size_x, _height_map.size_y);
 
	uint step = size_min >> log_frequency;
 
	uint x, y;
 

	
 
	assert(_height_map.h != NULL);
 

	
 
	/* Are we finished? */
 
	if (step == 0) return false;
 

	
 
	if (log_frequency == 0) {
 
		/* This is first round, we need to establish base heights with step = size_min */
 
		for (y = 0; y <= _height_map.size_y; y += step) {
 
			for (x = 0; x <= _height_map.size_x; x += step) {
 
				height_t height = (amplitude > 0) ? RandomHeight(amplitude) : 0;
 
				HeightMapXY(x, y) = height;
 
				_height_map.height(x, y) = height;
 
			}
 
		}
 
		return true;
 
	}
 

	
 
	/* It is regular iteration round.
 
	 * Interpolate height values at odd x, even y tiles */
 
	for (y = 0; y <= _height_map.size_y; y += 2 * step) {
 
		for (x = 0; x < _height_map.size_x; x += 2 * step) {
 
			height_t h00 = HeightMapXY(x + 0 * step, y);
 
			height_t h02 = HeightMapXY(x + 2 * step, y);
 
			height_t h00 = _height_map.height(x + 0 * step, y);
 
			height_t h02 = _height_map.height(x + 2 * step, y);
 
			height_t h01 = (h00 + h02) / 2;
 
			HeightMapXY(x + 1 * step, y) = h01;
 
			_height_map.height(x + 1 * step, y) = h01;
 
		}
 
	}
 

	
 
	/* Interpolate height values at odd y tiles */
 
	for (y = 0; y < _height_map.size_y; y += 2 * step) {
 
		for (x = 0; x <= _height_map.size_x; x += step) {
 
			height_t h00 = HeightMapXY(x, y + 0 * step);
 
			height_t h20 = HeightMapXY(x, y + 2 * step);
 
			height_t h00 = _height_map.height(x, y + 0 * step);
 
			height_t h20 = _height_map.height(x, y + 2 * step);
 
			height_t h10 = (h00 + h20) / 2;
 
			HeightMapXY(x, y + 1 * step) = h10;
 
			_height_map.height(x, y + 1 * step) = h10;
 
		}
 
	}
 

	
 
	for (y = 0; y <= _height_map.size_y; y += step) {
 
		for (x = 0; x <= _height_map.size_x; x += step) {
 
			HeightMapXY(x, y) += RandomHeight(amplitude);
 
			_height_map.height(x, y) += RandomHeight(amplitude);
 
		}
 
	}
 
	return (step > 1);
 
}
 

	
 
/** Base Perlin noise generator - fills height map with raw Perlin noise */
 
static void HeightMapGenerate()
 
{
 
	uint size_min = min(_height_map.size_x, _height_map.size_y);
 
	uint iteration_round = 0;
 
	amplitude_t amplitude;
 
	bool continue_iteration;
 
	uint log_size_min, log_frequency_min;
 
	int log_frequency;
 

	
 
	/* Find first power of two that fits */
 
	for (log_size_min = 6; (1U << log_size_min) < size_min; log_size_min++) { }
 
	log_frequency_min = log_size_min - 6;
 

	
 
	do {
 
		log_frequency = iteration_round - log_frequency_min;
 
		if (log_frequency >= 0) {
 
			amplitude = _amplitudes_by_smoothness_and_frequency[_settings_game.game_creation.tgen_smoothness][log_frequency];
 
		} else {
 
			amplitude = 0;
 
		}
 
		continue_iteration = ApplyNoise(iteration_round, amplitude);
 
		iteration_round++;
 
	} while(continue_iteration);
 
}
 

	
 
/** Returns min, max and average height from height map */
 
static void HeightMapGetMinMaxAvg(height_t *min_ptr, height_t *max_ptr, height_t *avg_ptr)
 
{
 
	height_t h_min, h_max, h_avg, *h;
 
	int64 h_accu = 0;
 
	h_min = h_max = HeightMapXY(0, 0);
 
	h_min = h_max = _height_map.height(0, 0);
 

	
 
	/* Get h_min, h_max and accumulate heights into h_accu */
 
	FOR_ALL_TILES_IN_HEIGHT(h) {
 
		if (*h < h_min) h_min = *h;
 
		if (*h > h_max) h_max = *h;
 
		h_accu += *h;
 
	}
 

	
 
	/* Get average height */
 
	h_avg = (height_t)(h_accu / (_height_map.size_x * _height_map.size_y));
 

	
 
	/* Return required results */
 
	if (min_ptr != NULL) *min_ptr = h_min;
 
	if (max_ptr != NULL) *max_ptr = h_max;
 
	if (avg_ptr != NULL) *avg_ptr = h_avg;
 
}
 

	
 
/** Dill histogram and return pointer to its base point - to the count of zero heights */
 
static int *HeightMapMakeHistogram(height_t h_min, height_t h_max, int *hist_buf)
 
{
 
	int *hist = hist_buf - h_min;
 
	height_t *h;
 

	
 
	/* Fill histogram */
 
@@ -523,187 +524,181 @@ static double perlin_coast_noise_2D(cons
 
 * This routine may be extended to randomly sculpt the height of the terrain
 
 * near the edge. This will have the coast edge at low level (1-3), rising in
 
 * smoothed steps inland to about 15 tiles in. This should make it look as
 
 * though the map has been built for the map size, rather than a slice through
 
 * a larger map.
 
 *
 
 * Please note that all the small numbers; 53, 101, 167, etc. are small primes
 
 * to help give the perlin noise a bit more of a random feel.
 
 */
 
static void HeightMapCoastLines()
 
{
 
	int smallest_size = min(_settings_game.game_creation.map_x, _settings_game.game_creation.map_y);
 
	const int margin = 4;
 
	uint y, x;
 
	double max_x;
 
	double max_y;
 

	
 
	/* Lower to sea level */
 
	for (y = 0; y <= _height_map.size_y; y++) {
 
		/* Top right */
 
		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);
 
		max_x = max((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
 
		if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
 
		for (x = 0; x < max_x; x++) {
 
			HeightMapXY(x, y) = 0;
 
			_height_map.height(x, y) = 0;
 
		}
 

	
 
		/* Bottom left */
 
		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);
 
		max_x = max((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
 
		if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
 
		for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x); x--) {
 
			HeightMapXY(x, y) = 0;
 
			_height_map.height(x, y) = 0;
 
		}
 
	}
 

	
 
	/* Lower to sea level */
 
	for (x = 0; x <= _height_map.size_x; x++) {
 
		/* Top left */
 
		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);
 
		max_y = max((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
 
		if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
 
		for (y = 0; y < max_y; y++) {
 
			HeightMapXY(x, y) = 0;
 
			_height_map.height(x, y) = 0;
 
		}
 

	
 

	
 
		/* Bottom right */
 
		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);
 
		max_y = max((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
 
		if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
 
		for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y); y--) {
 
			HeightMapXY(x, y) = 0;
 
			_height_map.height(x, y) = 0;
 
		}
 
	}
 
}
 

	
 
/** Start at given point, move in given direction, find and Smooth coast in that direction */
 
static void HeightMapSmoothCoastInDirection(int org_x, int org_y, int dir_x, int dir_y)
 
{
 
	const int max_coast_dist_from_edge = 35;
 
	const int max_coast_Smooth_depth = 35;
 

	
 
	int x, y;
 
	int ed; // coast distance from edge
 
	int depth;
 

	
 
	height_t h_prev = 16;
 
	height_t h;
 

	
 
	assert(IsValidXY(org_x, org_y));
 

	
 
	/* Search for the coast (first non-water tile) */
 
	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++) {
 
		/* Coast found? */
 
		if (HeightMapXY(x, y) > 15) break;
 
		if (_height_map.height(x, y) > 15) break;
 

	
 
		/* Coast found in the neighborhood? */
 
		if (IsValidXY(x + dir_y, y + dir_x) && HeightMapXY(x + dir_y, y + dir_x) > 0) break;
 
		if (IsValidXY(x + dir_y, y + dir_x) && _height_map.height(x + dir_y, y + dir_x) > 0) break;
 

	
 
		/* Coast found in the neighborhood on the other side */
 
		if (IsValidXY(x - dir_y, y - dir_x) && HeightMapXY(x - dir_y, y - dir_x) > 0) break;
 
		if (IsValidXY(x - dir_y, y - dir_x) && _height_map.height(x - dir_y, y - dir_x) > 0) break;
 
	}
 

	
 
	/* Coast found or max_coast_dist_from_edge has been reached.
 
	 * Soften the coast slope */
 
	for (depth = 0; IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
 
		h = HeightMapXY(x, y);
 
		h = _height_map.height(x, y);
 
		h = min(h, h_prev + (4 + depth)); // coast softening formula
 
		HeightMapXY(x, y) = h;
 
		_height_map.height(x, y) = h;
 
		h_prev = h;
 
	}
 
}
 

	
 
/** Smooth coasts by modulating height of tiles close to map edges with cosine of distance from edge */
 
static void HeightMapSmoothCoasts()
 
{
 
	uint x, y;
 
	/* First Smooth NW and SE coasts (y close to 0 and y close to size_y) */
 
	for (x = 0; x < _height_map.size_x; x++) {
 
		HeightMapSmoothCoastInDirection(x, 0, 0, 1);
 
		HeightMapSmoothCoastInDirection(x, _height_map.size_y - 1, 0, -1);
 
	}
 
	/* First Smooth NE and SW coasts (x close to 0 and x close to size_x) */
 
	for (y = 0; y < _height_map.size_y; y++) {
 
		HeightMapSmoothCoastInDirection(0, y, 1, 0);
 
		HeightMapSmoothCoastInDirection(_height_map.size_x - 1, y, -1, 0);
 
	}
 
}
 

	
 
/**
 
 * This routine provides the essential cleanup necessary before OTTD can
 
 * display the terrain. When generated, the terrain heights can jump more than
 
 * one level between tiles. This routine smooths out those differences so that
 
 * the most it can change is one level. When OTTD can support cliffs, this
 
 * routine may not be necessary.
 
 */
 
static void HeightMapSmoothSlopes(height_t dh_max)
 
{
 
	int x, y;
 
	for (y = 1; y <= (int)_height_map.size_y; y++) {
 
		for (x = 1; x <= (int)_height_map.size_x; x++) {
 
			height_t h_max = min(HeightMapXY(x - 1, y), HeightMapXY(x, y - 1)) + dh_max;
 
			if (HeightMapXY(x, y) > h_max) HeightMapXY(x, y) = h_max;
 
			height_t h_max = min(_height_map.height(x - 1, y), _height_map.height(x, y - 1)) + dh_max;
 
			if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
 
		}
 
	}
 
	for (y = _height_map.size_y - 1; y >= 0; y--) {
 
		for (x = _height_map.size_x - 1; x >= 0; x--) {
 
			height_t h_max = min(HeightMapXY(x + 1, y), HeightMapXY(x, y + 1)) + dh_max;
 
			if (HeightMapXY(x, y) > h_max) HeightMapXY(x, y) = h_max;
 
			height_t h_max = min(_height_map.height(x + 1, y), _height_map.height(x, y + 1)) + dh_max;
 
			if (_height_map.height(x, y) > h_max) _height_map.height(x, y) = h_max;
 
		}
 
	}
 
}
 

	
 
/** Height map terraform post processing:
 
 *  - water level adjusting
 
 *  - coast Smoothing
 
 *  - slope Smoothing
 
 *  - height histogram redistribution by sine wave transform */
 
static void HeightMapNormalize()
 
{
 
	const amplitude_t water_percent = _water_percent[_settings_game.difficulty.quantity_sea_lakes];
 
	const height_t h_max_new = I2H(_max_height[_settings_game.difficulty.terrain_type]);
 
	const height_t roughness = 7 + 3 * _settings_game.game_creation.tgen_smoothness;
 

	
 
	HeightMapAdjustWaterLevel(water_percent, h_max_new);
 

	
 
	HeightMapCoastLines();
 
	HeightMapSmoothSlopes(roughness);
 

	
 
	HeightMapSmoothCoasts();
 
	HeightMapSmoothSlopes(roughness);
 

	
 
	HeightMapSineTransform(12, h_max_new);
 
	HeightMapSmoothSlopes(16);
 
}
 

	
 
static inline int perlin_landXY(uint x, uint y)
 
{
 
	return HeightMapXY(x, y);
 
}
 

	
 

	
 
/**
 
 * The Perlin Noise calculation using large primes
 
 * The initial number is adjusted by two values; the generation_seed, and the
 
 * passed parameter; prime.
 
 * prime is used to allow the perlin noise generator to create useful random
 
 * numbers from slightly different series.
 
 */
 
static double int_noise(const long x, const long y, const int prime)
 
{
 
	long n = x + y * prime + _settings_game.game_creation.generation_seed;
 

	
 
	n = (n << 13) ^ n;
 

	
 
	/* Pseudo-random number generator, using several large primes */
 
	return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
 
}
 

	
 

	
 
/**
 
 * Hj. Malthaner's routine included 2 different noise smoothing methods.
 
 * We now use the "raw" int_noise one.
 
 * However, it may be useful to move to the other routine in future.
 
 * So it is included too.
 
 */
 
@@ -787,40 +782,40 @@ static void TgenSetTileHeight(TileIndex 
 
 * The main new land generator using Perlin noise. Desert landscape is handled
 
 * different to all others to give a desert valley between two high mountains.
 
 * Clearly if a low height terrain (flat/very flat) is chosen, then the tropic
 
 * areas wont be high enough, and there will be very little tropic on the map.
 
 * Thus Tropic works best on Hilly or Mountainous.
 
 */
 
void GenerateTerrainPerlin()
 
{
 
	uint x, y;
 

	
 
	if (!AllocHeightMap()) return;
 
	GenerateWorldSetAbortCallback(FreeHeightMap);
 

	
 
	HeightMapGenerate();
 

	
 
	IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
 

	
 
	HeightMapNormalize();
 

	
 
	IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
 

	
 
	/* Transfer height map into OTTD map */
 
	for (y = 2; y < _height_map.size_y - 2; y++) {
 
		for (x = 2; x < _height_map.size_x - 2; x++) {
 
			int height = H2I(HeightMapXY(x, y));
 
			int height = H2I(_height_map.height(x, y));
 
			if (height < 0) height = 0;
 
			if (height > 15) height = 15;
 
			TgenSetTileHeight(TileXY(x, y), height);
 
		}
 
	}
 

	
 
	IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
 

	
 
	/* Recreate void tiles at the border in case they have been affected by generation */
 
	for (y = 0; y < _height_map.size_y - 1; y++) MakeVoid(_height_map.size_x * y + _height_map.size_x - 1);
 
	for (x = 0; x < _height_map.size_x;     x++) MakeVoid(_height_map.size_x * y + x);
 

	
 
	FreeHeightMap();
 
	GenerateWorldSetAbortCallback(NULL);
 
}
0 comments (0 inline, 0 general)