Fix truncation of arctic tree probability distribution

This commit is contained in:
Jonathan G Rennison
2021-06-20 11:24:27 +01:00
parent 0eb32a55f9
commit c922f361b8

View File

@@ -136,7 +136,7 @@ static uint8 _previous_trees_around_snow_line_range = 255;
* Array of probabilities for artic trees to appear, * Array of probabilities for artic trees to appear,
* by normalised distance from snow line * by normalised distance from snow line
*/ */
static uint8 _arctic_tree_occurance[24]; static std::vector<uint8> _arctic_tree_occurance;
/** Recalculate _arctic_tree_occurance */ /** Recalculate _arctic_tree_occurance */
static void RecalculateArcticTreeOccuranceArray() static void RecalculateArcticTreeOccuranceArray()
@@ -149,10 +149,11 @@ static void RecalculateArcticTreeOccuranceArray()
*/ */
uint8 range = _settings_game.construction.trees_around_snow_line_range; uint8 range = _settings_game.construction.trees_around_snow_line_range;
_previous_trees_around_snow_line_range = range; _previous_trees_around_snow_line_range = range;
_arctic_tree_occurance[0] = 255; _arctic_tree_occurance.clear();
uint i = 1; _arctic_tree_occurance.reserve((range * 5) / 4);
for (; i < lengthof(_arctic_tree_occurance); i++) { _arctic_tree_occurance.push_back(255);
if (range == 0) break; if (range == 0) return;
for (uint i = 1; i < 256; i++) {
uint x = 256 - ((128 * i) / range); uint x = 256 - ((128 * i) / range);
uint32 output = x; uint32 output = x;
output *= x; output *= x;
@@ -163,10 +164,7 @@ static void RecalculateArcticTreeOccuranceArray()
output *= x; output *= x;
output >>= 24; output >>= 24;
if (output == 0) break; if (output == 0) break;
_arctic_tree_occurance[i] = static_cast<uint8>(output); _arctic_tree_occurance.push_back(static_cast<uint8>(output));
}
for (; i < lengthof(_arctic_tree_occurance); i++) {
_arctic_tree_occurance[i] = 0;
} }
} }
@@ -199,7 +197,7 @@ static TreeType GetRandomTreeType(TileIndex tile, uint seed)
int height_above_snow_line = z - _settings_game.game_creation.snow_line_height; int height_above_snow_line = z - _settings_game.game_creation.snow_line_height;
uint normalised_distance = (height_above_snow_line < 0) ? -height_above_snow_line : height_above_snow_line + 1; uint normalised_distance = (height_above_snow_line < 0) ? -height_above_snow_line : height_above_snow_line + 1;
bool arctic_tree = false; bool arctic_tree = false;
if (normalised_distance < lengthof(_arctic_tree_occurance)) { if (normalised_distance < _arctic_tree_occurance.size()) {
arctic_tree = RandomRange(256) < _arctic_tree_occurance[normalised_distance]; arctic_tree = RandomRange(256) < _arctic_tree_occurance[normalised_distance];
} }
if (height_above_snow_line < 0) { if (height_above_snow_line < 0) {
@@ -360,7 +358,7 @@ int MaxTreeCount(const TileIndex tile)
if (_settings_game.game_creation.landscape == LT_ARCTIC) { if (_settings_game.game_creation.landscape == LT_ARCTIC) {
const uint height_above_snow_line = std::max<int>(0, tile_z - _settings_game.game_creation.snow_line_height); const uint height_above_snow_line = std::max<int>(0, tile_z - _settings_game.game_creation.snow_line_height);
max_trees_snow_line_based = (height_above_snow_line < lengthof(_arctic_tree_occurance)) ? max_trees_snow_line_based = (height_above_snow_line < _arctic_tree_occurance.size()) ?
(1 + (_arctic_tree_occurance[height_above_snow_line] * 4) / 255) : (1 + (_arctic_tree_occurance[height_above_snow_line] * 4) / 255) :
0; 0;
} }