1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-24 04:34:16 +01:00

Codechange: Use enum class for Borders values. (#13290)

This makes water border settings type-safe, and avoids mixing bit numbers with masks.
This commit is contained in:
Peter Nelson
2025-01-08 17:55:53 +00:00
committed by GitHub
parent d79ae73c14
commit ce4012b7c7
5 changed files with 54 additions and 35 deletions

View File

@@ -718,7 +718,7 @@ static double perlin_coast_noise_2D(const double x, const double y, const double
* 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(uint8_t water_borders)
static void HeightMapCoastLines(Borders water_borders)
{
int smallest_size = std::min(_settings_game.game_creation.map_x, _settings_game.game_creation.map_y);
const int margin = 4;
@@ -728,7 +728,7 @@ static void HeightMapCoastLines(uint8_t water_borders)
/* Lower to sea level */
for (y = 0; y <= _height_map.size_y; y++) {
if (HasBit(water_borders, BORDER_NE)) {
if (HasFlag(water_borders, Borders::NorthEast)) {
/* 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 = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
@@ -738,7 +738,7 @@ static void HeightMapCoastLines(uint8_t water_borders)
}
}
if (HasBit(water_borders, BORDER_SW)) {
if (HasFlag(water_borders, Borders::SouthWest)) {
/* 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 = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
@@ -751,7 +751,7 @@ static void HeightMapCoastLines(uint8_t water_borders)
/* Lower to sea level */
for (x = 0; x <= _height_map.size_x; x++) {
if (HasBit(water_borders, BORDER_NW)) {
if (HasFlag(water_borders, Borders::NorthWest)) {
/* 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 = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
@@ -761,7 +761,7 @@ static void HeightMapCoastLines(uint8_t water_borders)
}
}
if (HasBit(water_borders, BORDER_SE)) {
if (HasFlag(water_borders, Borders::SouthEast)) {
/* 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 = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
@@ -811,18 +811,18 @@ static void HeightMapSmoothCoastInDirection(int org_x, int org_y, int dir_x, int
}
/** Smooth coasts by modulating height of tiles close to map edges with cosine of distance from edge */
static void HeightMapSmoothCoasts(uint8_t water_borders)
static void HeightMapSmoothCoasts(Borders water_borders)
{
int 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++) {
if (HasBit(water_borders, BORDER_NW)) HeightMapSmoothCoastInDirection(x, 0, 0, 1);
if (HasBit(water_borders, BORDER_SE)) HeightMapSmoothCoastInDirection(x, _height_map.size_y - 1, 0, -1);
if (HasFlag(water_borders, Borders::NorthWest)) HeightMapSmoothCoastInDirection(x, 0, 0, 1);
if (HasFlag(water_borders, Borders::SouthEast)) 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++) {
if (HasBit(water_borders, BORDER_NE)) HeightMapSmoothCoastInDirection(0, y, 1, 0);
if (HasBit(water_borders, BORDER_SW)) HeightMapSmoothCoastInDirection(_height_map.size_x - 1, y, -1, 0);
if (HasFlag(water_borders, Borders::NorthEast)) HeightMapSmoothCoastInDirection(0, y, 1, 0);
if (HasFlag(water_borders, Borders::SouthWest)) HeightMapSmoothCoastInDirection(_height_map.size_x - 1, y, -1, 0);
}
}
@@ -865,8 +865,8 @@ static void HeightMapNormalize()
HeightMapAdjustWaterLevel(water_percent, h_max_new);
uint8_t water_borders = _settings_game.construction.freeform_edges ? _settings_game.game_creation.water_borders : 0xF;
if (water_borders == BORDERS_RANDOM) water_borders = GB(Random(), 0, 4);
Borders water_borders = _settings_game.construction.freeform_edges ? _settings_game.game_creation.water_borders : Borders::All;
if (water_borders == Borders::Random) water_borders = static_cast<Borders>(GB(Random(), 0, 4));
HeightMapCoastLines(water_borders);
HeightMapSmoothSlopes(roughness);