src/tgp.cpp
branchcustombridgeheads
changeset 5650 aefc131bf5ce
parent 5649 55c8267c933f
child 5852 cb3f71b16e1a
equal deleted inserted replaced
5649:55c8267c933f 5650:aefc131bf5ce
    12 #include "variables.h"
    12 #include "variables.h"
    13 #include "void_map.h"
    13 #include "void_map.h"
    14 #include "tgp.h"
    14 #include "tgp.h"
    15 #include "console.h"
    15 #include "console.h"
    16 #include "genworld.h"
    16 #include "genworld.h"
       
    17 #include "helpers.hpp"
    17 
    18 
    18 /*
    19 /*
    19  * OTTD Perlin Noise Landscape Generator, aka TerraGenesis Perlin
    20  * OTTD Perlin Noise Landscape Generator, aka TerraGenesis Perlin
    20  *
    21  *
    21  * Quickie guide to Perlin Noise
    22  * Quickie guide to Perlin Noise
   188 #define I2A(i) ((i) << amplitude_decimal_bits)
   189 #define I2A(i) ((i) << amplitude_decimal_bits)
   189 /** Conversion: amplitude_t to int */
   190 /** Conversion: amplitude_t to int */
   190 #define A2I(i) ((i) >> amplitude_decimal_bits)
   191 #define A2I(i) ((i) >> amplitude_decimal_bits)
   191 
   192 
   192 /** Conversion: amplitude_t to height_t */
   193 /** Conversion: amplitude_t to height_t */
   193 #define A2H(a) ((height_decimal_bits < amplitude_decimal_bits) \
   194 #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
   194 	? ((a) >> (amplitude_decimal_bits - height_decimal_bits)) \
   195 
   195 	: ((a) << (height_decimal_bits - amplitude_decimal_bits)))
       
   196 
   196 
   197 /** Walk through all items of _height_map.h */
   197 /** Walk through all items of _height_map.h */
   198 #define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++)
   198 #define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++)
   199 
   199 
   200 /** Noise amplitudes (multiplied by 1024)
   200 /** Noise amplitudes (multiplied by 1024)
   237 	_height_map.size_y = MapSizeY();
   237 	_height_map.size_y = MapSizeY();
   238 
   238 
   239 	/* Allocate memory block for height map row pointers */
   239 	/* Allocate memory block for height map row pointers */
   240 	_height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
   240 	_height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
   241 	_height_map.dim_x = _height_map.size_x + 1;
   241 	_height_map.dim_x = _height_map.size_x + 1;
   242 	_height_map.h = calloc(_height_map.total_size, sizeof(*_height_map.h));
   242 	CallocT(&_height_map.h, _height_map.total_size);
   243 	if (_height_map.h == NULL) return false;
   243 	if (_height_map.h == NULL) return false;
   244 
   244 
   245 	/* Iterate through height map initialize values */
   245 	/* Iterate through height map initialize values */
   246 	FOR_ALL_TILES_IN_HEIGHT(h) *h = _invalid_height;
   246 	FOR_ALL_TILES_IN_HEIGHT(h) *h = _invalid_height;
   247 
   247 
   453 			default:
   453 			default:
   454 				NOT_REACHED();
   454 				NOT_REACHED();
   455 				break;
   455 				break;
   456 		}
   456 		}
   457 		/* Transform it back into h_min..h_max space */
   457 		/* Transform it back into h_min..h_max space */
   458 		*h = fheight * (h_max - h_min) + h_min;
   458 		*h = (height_t)(fheight * (h_max - h_min) + h_min);
   459 		if (*h < 0) *h = I2H(0);
   459 		if (*h < 0) *h = I2H(0);
   460 		if (*h >= h_max) *h = h_max - 1;
   460 		if (*h >= h_max) *h = h_max - 1;
   461 	}
   461 	}
   462 }
   462 }
   463 
   463 
   470 	int *hist_buf, *hist;
   470 	int *hist_buf, *hist;
   471 
   471 
   472 	HeightMapGetMinMaxAvg(&h_min, &h_max, &h_avg);
   472 	HeightMapGetMinMaxAvg(&h_min, &h_max, &h_avg);
   473 
   473 
   474 	/* Allocate histogram buffer and clear its cells */
   474 	/* Allocate histogram buffer and clear its cells */
   475 	hist_buf = calloc(h_max - h_min + 1, sizeof(*hist_buf));
   475 	CallocT(&hist_buf, h_max - h_min + 1);
   476 	/* Fill histogram */
   476 	/* Fill histogram */
   477 	hist = HeightMapMakeHistogram(h_min, h_max, hist_buf);
   477 	hist = HeightMapMakeHistogram(h_min, h_max, hist_buf);
   478 
   478 
   479 	/* How many water tiles do we want? */
   479 	/* How many water tiles do we want? */
   480 	desired_water_tiles = (int)(((int64)water_percent) * (int64)(_height_map.size_x * _height_map.size_y)) >> amplitude_decimal_bits;
   480 	desired_water_tiles = (int)(((int64)water_percent) * (int64)(_height_map.size_x * _height_map.size_y)) >> amplitude_decimal_bits;
   527 static void HeightMapCoastLines(void)
   527 static void HeightMapCoastLines(void)
   528 {
   528 {
   529 	int smallest_size = min(_patches.map_x, _patches.map_y);
   529 	int smallest_size = min(_patches.map_x, _patches.map_y);
   530 	const int margin = 4;
   530 	const int margin = 4;
   531 	uint y, x;
   531 	uint y, x;
   532 	uint max_x;
   532 	double max_x;
   533 	uint max_y;
   533 	double max_y;
   534 
   534 
   535 	/* Lower to sea level */
   535 	/* Lower to sea level */
   536 	for (y = 0; y <= _height_map.size_y; y++) {
   536 	for (y = 0; y <= _height_map.size_y; y++) {
   537 		/* Top right */
   537 		/* Top right */
   538 		max_x = myabs((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);
   538 		max_x = myabs((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);
   539 		max_x = max((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
   539 		max_x = dmax((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
   540 		if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
   540 		if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
   541 		for (x = 0; x < max_x; x++) {
   541 		for (x = 0; x < max_x; x++) {
   542 			HeightMapXY(x, y) = 0;
   542 			HeightMapXY(x, y) = 0;
   543 		}
   543 		}
   544 
   544 
   545 		/* Bottom left */
   545 		/* Bottom left */
   546 		max_x = myabs((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);
   546 		max_x = myabs((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);
   547 		max_x = max((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
   547 		max_x = dmax((smallest_size * smallest_size / 16) + max_x, (smallest_size * smallest_size / 16) + margin - max_x);
   548 		if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
   548 		if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
   549 		for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x); x--) {
   549 		for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x); x--) {
   550 			HeightMapXY(x, y) = 0;
   550 			HeightMapXY(x, y) = 0;
   551 		}
   551 		}
   552 	}
   552 	}
   553 
   553 
   554 	/* Lower to sea level */
   554 	/* Lower to sea level */
   555 	for (x = 0; x <= _height_map.size_x; x++) {
   555 	for (x = 0; x <= _height_map.size_x; x++) {
   556 		/* Top left */
   556 		/* Top left */
   557 		max_y = myabs((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);
   557 		max_y = myabs((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);
   558 		max_y = max((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
   558 		max_y = dmax((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
   559 		if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
   559 		if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
   560 		for (y = 0; y < max_y; y++) {
   560 		for (y = 0; y < max_y; y++) {
   561 			HeightMapXY(x, y) = 0;
   561 			HeightMapXY(x, y) = 0;
   562 		}
   562 		}
   563 
   563 
   564 
   564 
   565 		/* Bottom right */
   565 		/* Bottom right */
   566 		max_y = myabs((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);
   566 		max_y = myabs((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);
   567 		max_y = max((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
   567 		max_y = dmax((smallest_size * smallest_size / 16) + max_y, (smallest_size * smallest_size / 16) + margin - max_y);
   568 		if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
   568 		if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
   569 		for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y); y--) {
   569 		for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y); y--) {
   570 			HeightMapXY(x, y) = 0;
   570 			HeightMapXY(x, y) = 0;
   571 		}
   571 		}
   572 	}
   572 	}