Codechange: Use EnumBitSet for Border(Flag)s.

This commit is contained in:
Peter Nelson 2025-01-29 17:47:51 +00:00 committed by Peter Nelson
parent 473728f181
commit 2560339472
4 changed files with 38 additions and 38 deletions

View File

@ -479,11 +479,11 @@ struct GenerateLandscapeWindow : public Window {
case WID_GL_RIVER_PULLDOWN: SetDParam(0, _rivers[_settings_newgame.game_creation.amount_of_rivers]); break;
case WID_GL_SMOOTHNESS_PULLDOWN: SetDParam(0, _smoothness[_settings_newgame.game_creation.tgen_smoothness]); break;
case WID_GL_VARIETY_PULLDOWN: SetDParam(0, _variety[_settings_newgame.game_creation.variety]); break;
case WID_GL_BORDERS_RANDOM: SetDParam(0, (_settings_newgame.game_creation.water_borders == Borders::Random) ? STR_MAPGEN_BORDER_RANDOMIZE : STR_MAPGEN_BORDER_MANUAL); break;
case WID_GL_WATER_NE: SetDParam(0, (_settings_newgame.game_creation.water_borders == Borders::Random) ? STR_MAPGEN_BORDER_RANDOM : HasFlag(_settings_newgame.game_creation.water_borders, Borders::NorthEast) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
case WID_GL_WATER_NW: SetDParam(0, (_settings_newgame.game_creation.water_borders == Borders::Random) ? STR_MAPGEN_BORDER_RANDOM : HasFlag(_settings_newgame.game_creation.water_borders, Borders::NorthWest) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
case WID_GL_WATER_SE: SetDParam(0, (_settings_newgame.game_creation.water_borders == Borders::Random) ? STR_MAPGEN_BORDER_RANDOM : HasFlag(_settings_newgame.game_creation.water_borders, Borders::SouthEast) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
case WID_GL_WATER_SW: SetDParam(0, (_settings_newgame.game_creation.water_borders == Borders::Random) ? STR_MAPGEN_BORDER_RANDOM : HasFlag(_settings_newgame.game_creation.water_borders, Borders::SouthWest) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
case WID_GL_BORDERS_RANDOM: SetDParam(0, (_settings_newgame.game_creation.water_borders == BorderFlag::Random) ? STR_MAPGEN_BORDER_RANDOMIZE : STR_MAPGEN_BORDER_MANUAL); break;
case WID_GL_WATER_NE: SetDParam(0, (_settings_newgame.game_creation.water_borders == BorderFlag::Random) ? STR_MAPGEN_BORDER_RANDOM : _settings_newgame.game_creation.water_borders.Test(BorderFlag::NorthEast) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
case WID_GL_WATER_NW: SetDParam(0, (_settings_newgame.game_creation.water_borders == BorderFlag::Random) ? STR_MAPGEN_BORDER_RANDOM : _settings_newgame.game_creation.water_borders.Test(BorderFlag::NorthWest) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
case WID_GL_WATER_SE: SetDParam(0, (_settings_newgame.game_creation.water_borders == BorderFlag::Random) ? STR_MAPGEN_BORDER_RANDOM : _settings_newgame.game_creation.water_borders.Test(BorderFlag::SouthEast) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
case WID_GL_WATER_SW: SetDParam(0, (_settings_newgame.game_creation.water_borders == BorderFlag::Random) ? STR_MAPGEN_BORDER_RANDOM : _settings_newgame.game_creation.water_borders.Test(BorderFlag::SouthWest) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: SetDParam(0, _rotation[_settings_newgame.game_creation.heightmap_rotation]); break;
case WID_GL_HEIGHTMAP_SIZE_TEXT:
@ -517,15 +517,15 @@ struct GenerateLandscapeWindow : public Window {
this->SetWidgetDisabledState(WID_GL_SMOOTHNESS_PULLDOWN, _settings_newgame.game_creation.land_generator == LG_ORIGINAL);
this->SetWidgetDisabledState(WID_GL_VARIETY_PULLDOWN, _settings_newgame.game_creation.land_generator == LG_ORIGINAL);
this->SetWidgetDisabledState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.land_generator == LG_ORIGINAL || !_settings_newgame.construction.freeform_edges);
this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == LG_ORIGINAL || !_settings_newgame.construction.freeform_edges || _settings_newgame.game_creation.water_borders == Borders::Random,
this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == LG_ORIGINAL || !_settings_newgame.construction.freeform_edges || _settings_newgame.game_creation.water_borders == BorderFlag::Random,
WID_GL_WATER_NW, WID_GL_WATER_NE, WID_GL_WATER_SE, WID_GL_WATER_SW);
this->SetWidgetLoweredState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.water_borders == Borders::Random);
this->SetWidgetLoweredState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.water_borders == BorderFlag::Random);
this->SetWidgetLoweredState(WID_GL_WATER_NW, HasFlag(_settings_newgame.game_creation.water_borders, Borders::NorthWest));
this->SetWidgetLoweredState(WID_GL_WATER_NE, HasFlag(_settings_newgame.game_creation.water_borders, Borders::NorthEast));
this->SetWidgetLoweredState(WID_GL_WATER_SE, HasFlag(_settings_newgame.game_creation.water_borders, Borders::SouthEast));
this->SetWidgetLoweredState(WID_GL_WATER_SW, HasFlag(_settings_newgame.game_creation.water_borders, Borders::SouthWest));
this->SetWidgetLoweredState(WID_GL_WATER_NW, _settings_newgame.game_creation.water_borders.Test(BorderFlag::NorthWest));
this->SetWidgetLoweredState(WID_GL_WATER_NE, _settings_newgame.game_creation.water_borders.Test(BorderFlag::NorthEast));
this->SetWidgetLoweredState(WID_GL_WATER_SE, _settings_newgame.game_creation.water_borders.Test(BorderFlag::SouthEast));
this->SetWidgetLoweredState(WID_GL_WATER_SW, _settings_newgame.game_creation.water_borders.Test(BorderFlag::SouthWest));
this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == LG_ORIGINAL && (_settings_newgame.game_creation.landscape == LT_ARCTIC || _settings_newgame.game_creation.landscape == LT_TROPIC),
WID_GL_TERRAIN_PULLDOWN, WID_GL_WATER_PULLDOWN);
@ -829,27 +829,27 @@ struct GenerateLandscapeWindow : public Window {
/* Freetype map borders */
case WID_GL_WATER_NW:
ToggleFlag(_settings_newgame.game_creation.water_borders, Borders::NorthWest);
_settings_newgame.game_creation.water_borders.Flip(BorderFlag::NorthWest);
this->InvalidateData();
break;
case WID_GL_WATER_NE:
ToggleFlag(_settings_newgame.game_creation.water_borders, Borders::NorthEast);
_settings_newgame.game_creation.water_borders.Flip(BorderFlag::NorthEast);
this->InvalidateData();
break;
case WID_GL_WATER_SE:
ToggleFlag(_settings_newgame.game_creation.water_borders, Borders::SouthEast);
_settings_newgame.game_creation.water_borders.Flip(BorderFlag::SouthEast);
this->InvalidateData();
break;
case WID_GL_WATER_SW:
ToggleFlag(_settings_newgame.game_creation.water_borders, Borders::SouthWest);
_settings_newgame.game_creation.water_borders.Flip(BorderFlag::SouthWest);
this->InvalidateData();
break;
case WID_GL_BORDERS_RANDOM:
_settings_newgame.game_creation.water_borders = (_settings_newgame.game_creation.water_borders == Borders::Random) ? Borders::None : Borders::Random;
_settings_newgame.game_creation.water_borders = (_settings_newgame.game_creation.water_borders == BorderFlag::Random) ? BorderFlag{} : BorderFlag::Random;
this->InvalidateData();
break;

View File

@ -26,15 +26,15 @@ enum LandscapeType : uint8_t {
/**
* For storing the water borders which shall be retained.
*/
enum class Borders : uint8_t {
None = 0,
NorthEast = 1U << 0, ///< Border on North East.
SouthEast = 1U << 1, ///< Border on South East.
SouthWest = 1U << 2, ///< Border on South West.
NorthWest = 1U << 3, ///< Border on North West.
Random = 1U << 4, ///< Randomise borders.
All = NorthEast | SouthEast | SouthWest | NorthWest, ///< Border on all sides.
enum class BorderFlag : uint8_t {
NorthEast, ///< Border on North East.
SouthEast, ///< Border on South East.
SouthWest, ///< Border on South West.
NorthWest, ///< Border on North West.
Random, ///< Randomise borders.
};
DECLARE_ENUM_AS_BIT_SET(Borders)
using BorderFlags = EnumBitSet<BorderFlag, uint8_t>;
static constexpr BorderFlags BORDERFLAGS_ALL = BorderFlags{BorderFlag::NorthEast, BorderFlag::SouthEast, BorderFlag::SouthWest, BorderFlag::NorthWest}; ///< Border on all sides.
#endif /* LANDSCAPE_TYPE_H */

View File

@ -366,7 +366,7 @@ struct GameCreationSettings {
uint8_t se_flat_world_height; ///< land height a flat world gets in SE
uint8_t town_name; ///< the town name generator used for town names
uint8_t landscape; ///< the landscape we're currently in
Borders water_borders; ///< bitset of the borders that are water
BorderFlags water_borders; ///< bitset of the borders that are water
uint16_t custom_town_number; ///< manually entered number of towns
uint16_t custom_industry_number; ///< manually entered number of industries
uint8_t variety; ///< variety level applied to TGP

View File

@ -727,7 +727,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(Borders water_borders)
static void HeightMapCoastLines(BorderFlags water_borders)
{
int smallest_size = std::min(_settings_game.game_creation.map_x, _settings_game.game_creation.map_y);
const int margin = 4;
@ -737,7 +737,7 @@ static void HeightMapCoastLines(Borders water_borders)
/* Lower to sea level */
for (y = 0; y <= _height_map.size_y; y++) {
if (HasFlag(water_borders, Borders::NorthEast)) {
if (water_borders.Test(BorderFlag::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);
@ -747,7 +747,7 @@ static void HeightMapCoastLines(Borders water_borders)
}
}
if (HasFlag(water_borders, Borders::SouthWest)) {
if (water_borders.Test(BorderFlag::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);
@ -760,7 +760,7 @@ static void HeightMapCoastLines(Borders water_borders)
/* Lower to sea level */
for (x = 0; x <= _height_map.size_x; x++) {
if (HasFlag(water_borders, Borders::NorthWest)) {
if (water_borders.Test(BorderFlag::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);
@ -770,7 +770,7 @@ static void HeightMapCoastLines(Borders water_borders)
}
}
if (HasFlag(water_borders, Borders::SouthEast)) {
if (water_borders.Test(BorderFlag::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);
@ -820,18 +820,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(Borders water_borders)
static void HeightMapSmoothCoasts(BorderFlags 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 (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);
if (water_borders.Test(BorderFlag::NorthWest)) HeightMapSmoothCoastInDirection(x, 0, 0, 1);
if (water_borders.Test(BorderFlag::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 (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);
if (water_borders.Test(BorderFlag::NorthEast)) HeightMapSmoothCoastInDirection(0, y, 1, 0);
if (water_borders.Test(BorderFlag::SouthWest)) HeightMapSmoothCoastInDirection(_height_map.size_x - 1, y, -1, 0);
}
}
@ -873,8 +873,8 @@ static void HeightMapNormalize()
HeightMapAdjustWaterLevel(water_percent, h_max_new);
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));
BorderFlags water_borders = _settings_game.construction.freeform_edges ? _settings_game.game_creation.water_borders : BORDERFLAGS_ALL;
if (water_borders == BorderFlag::Random) water_borders = static_cast<BorderFlags>(GB(Random(), 0, 4));
HeightMapCoastLines(water_borders);
HeightMapSmoothSlopes(roughness);