Codechange: Use EnumBitSet for RoadTypeFlags and RailTypeFlags. (#13415)

This commit is contained in:
Peter Nelson 2025-01-30 22:08:51 +00:00 committed by GitHub
parent a4d23c072d
commit 917d5cc75d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 56 additions and 76 deletions

View File

@ -2627,11 +2627,11 @@ static void ConDumpRoadTypes()
(uint)rt,
RoadTypeIsTram(rt) ? "Tram" : "Road",
FormatLabel(rti->label),
HasBit(rti->flags, ROTF_CATENARY) ? 'c' : '-',
HasBit(rti->flags, ROTF_NO_LEVEL_CROSSING) ? 'l' : '-',
HasBit(rti->flags, ROTF_NO_HOUSES) ? 'X' : '-',
HasBit(rti->flags, ROTF_HIDDEN) ? 'h' : '-',
HasBit(rti->flags, ROTF_TOWN_BUILD) ? 'T' : '-',
rti->flags.Test(RoadTypeFlag::Catenary) ? 'c' : '-',
rti->flags.Test(RoadTypeFlag::NoLevelCrossing) ? 'l' : '-',
rti->flags.Test(RoadTypeFlag::NoHouses) ? 'X' : '-',
rti->flags.Test(RoadTypeFlag::Hidden) ? 'h' : '-',
rti->flags.Test(RoadTypeFlag::TownBuild) ? 'T' : '-',
std::byteswap(grfid),
GetStringPtr(rti->strings.name)
);
@ -2664,12 +2664,12 @@ static void ConDumpRailTypes()
IConsolePrint(CC_DEFAULT, " {:02d} {}, Flags: {}{}{}{}{}{}, GRF: {:08X}, {}",
(uint)rt,
FormatLabel(rti->label),
HasBit(rti->flags, RTF_CATENARY) ? 'c' : '-',
HasBit(rti->flags, RTF_NO_LEVEL_CROSSING) ? 'l' : '-',
HasBit(rti->flags, RTF_HIDDEN) ? 'h' : '-',
HasBit(rti->flags, RTF_NO_SPRITE_COMBINE) ? 's' : '-',
HasBit(rti->flags, RTF_ALLOW_90DEG) ? 'a' : '-',
HasBit(rti->flags, RTF_DISALLOW_90DEG) ? 'd' : '-',
rti->flags.Test(RailTypeFlag::Catenary) ? 'c' : '-',
rti->flags.Test(RailTypeFlag::NoLevelCrossing) ? 'l' : '-',
rti->flags.Test(RailTypeFlag::Hidden) ? 'h' : '-',
rti->flags.Test(RailTypeFlag::NoSpriteCombine) ? 's' : '-',
rti->flags.Test(RailTypeFlag::Allow90Deg) ? 'a' : '-',
rti->flags.Test(RailTypeFlag::Disallow90Deg) ? 'd' : '-',
std::byteswap(grfid),
GetStringPtr(rti->strings.name)
);

View File

@ -20,7 +20,7 @@
*/
inline bool HasRailCatenary(RailType rt)
{
return HasBit(GetRailTypeInfo(rt)->flags, RTF_CATENARY);
return GetRailTypeInfo(rt)->flags.Test(RailTypeFlag::Catenary);
}
/**

View File

@ -4340,7 +4340,7 @@ static ChangeInfoResult RailTypeChangeInfo(uint first, uint last, int prop, Byte
}
case 0x10: // Rail Type flags
rti->flags = (RailTypeFlags)buf.ReadByte();
rti->flags = static_cast<RailTypeFlags>(buf.ReadByte());
break;
case 0x11: // Curve speed advantage
@ -4557,7 +4557,7 @@ static ChangeInfoResult RoadTypeChangeInfo(uint first, uint last, int prop, Byte
}
case 0x10: // Road Type flags
rti->flags = (RoadTypeFlags)buf.ReadByte();
rti->flags = static_cast<RoadTypeFlags>(buf.ReadByte());
break;
case 0x13: // Construction cost factor

View File

@ -565,7 +565,7 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec
case VEH_TRAIN: {
RailType rt = GetTileRailType(v->tile);
const RailTypeInfo *rti = GetRailTypeInfo(rt);
return ((rti->flags & RTFB_CATENARY) ? 0x200 : 0) |
return (rti->flags.Test(RailTypeFlag::Catenary) ? 0x200 : 0) |
(HasPowerOnRail(Train::From(v)->railtype, rt) ? 0x100 : 0) |
GetReverseRailTypeTranslation(rt, object->ro.grffile);
}
@ -573,7 +573,7 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec
case VEH_ROAD: {
RoadType rt = GetRoadType(v->tile, GetRoadTramType(RoadVehicle::From(v)->roadtype));
const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
return ((rti->flags & ROTFB_CATENARY) ? 0x200 : 0) |
return (rti->flags.Test(RoadTypeFlag::Catenary) ? 0x200 : 0) |
0x100 |
GetReverseRoadTypeTranslation(rt, object->ro.grffile);
}

View File

@ -14,6 +14,7 @@
#include "track_type.h"
#include "gfx_type.h"
#include "core/bitmath_func.hpp"
#include "core/enum_type.hpp"
#include "economy_func.h"
#include "slope_type.h"
#include "strings_type.h"
@ -22,26 +23,15 @@
#include "settings_type.h"
/** Railtype flag bit numbers. */
enum RailTypeFlag : uint8_t {
RTF_CATENARY = 0, ///< Bit number for drawing a catenary.
RTF_NO_LEVEL_CROSSING = 1, ///< Bit number for disallowing level crossings.
RTF_HIDDEN = 2, ///< Bit number for hiding from selection.
RTF_NO_SPRITE_COMBINE = 3, ///< Bit number for using non-combined junctions.
RTF_ALLOW_90DEG = 4, ///< Bit number for always allowed 90 degree turns, regardless of setting.
RTF_DISALLOW_90DEG = 5, ///< Bit number for never allowed 90 degree turns, regardless of setting.
enum class RailTypeFlag : uint8_t {
Catenary = 0, ///< Bit number for drawing a catenary.
NoLevelCrossing = 1, ///< Bit number for disallowing level crossings.
Hidden = 2, ///< Bit number for hiding from selection.
NoSpriteCombine = 3, ///< Bit number for using non-combined junctions.
Allow90Deg = 4, ///< Bit number for always allowed 90 degree turns, regardless of setting.
Disallow90Deg = 5, ///< Bit number for never allowed 90 degree turns, regardless of setting.
};
/** Railtype flags. */
enum RailTypeFlags : uint8_t {
RTFB_NONE = 0, ///< All flags cleared.
RTFB_CATENARY = 1 << RTF_CATENARY, ///< Value for drawing a catenary.
RTFB_NO_LEVEL_CROSSING = 1 << RTF_NO_LEVEL_CROSSING, ///< Value for disallowing level crossings.
RTFB_HIDDEN = 1 << RTF_HIDDEN, ///< Value for hiding from selection.
RTFB_NO_SPRITE_COMBINE = 1 << RTF_NO_SPRITE_COMBINE, ///< Value for using non-combined junctions.
RTFB_ALLOW_90DEG = 1 << RTF_ALLOW_90DEG, ///< Value for always allowed 90 degree turns, regardless of setting.
RTFB_DISALLOW_90DEG = 1 << RTF_DISALLOW_90DEG, ///< Value for never allowed 90 degree turns, regardless of setting.
};
DECLARE_ENUM_AS_BIT_SET(RailTypeFlags)
using RailTypeFlags = EnumBitSet<RailTypeFlag, uint8_t>;
struct SpriteGroup;
@ -344,7 +334,7 @@ inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
*/
inline bool RailNoLevelCrossings(RailType rt)
{
return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
return GetRailTypeInfo(rt)->flags.Test(RailTypeFlag::NoLevelCrossing);
}
/**
@ -361,8 +351,8 @@ inline bool Rail90DegTurnDisallowed(RailType rt1, RailType rt2, bool def = _sett
const RailTypeInfo *rti1 = GetRailTypeInfo(rt1);
const RailTypeInfo *rti2 = GetRailTypeInfo(rt2);
bool rt1_90deg = HasBit(rti1->flags, RTF_DISALLOW_90DEG) || (!HasBit(rti1->flags, RTF_ALLOW_90DEG) && def);
bool rt2_90deg = HasBit(rti2->flags, RTF_DISALLOW_90DEG) || (!HasBit(rti2->flags, RTF_ALLOW_90DEG) && def);
bool rt1_90deg = rti1->flags.Test(RailTypeFlag::Disallow90Deg) || (!rti1->flags.Test(RailTypeFlag::Allow90Deg) && def);
bool rt2_90deg = rti2->flags.Test(RailTypeFlag::Disallow90Deg) || (!rti2->flags.Test(RailTypeFlag::Allow90Deg) && def);
return rt1_90deg || rt2_90deg;
}

View File

@ -132,7 +132,7 @@ void InitRailTypes()
for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
RailTypeInfo *rti = &_railtypes[rt];
ResolveRailTypeGUISprites(rti);
if (HasBit(rti->flags, RTF_HIDDEN)) SetBit(_railtypes_hidden_mask, rt);
if (rti->flags.Test(RailTypeFlag::Hidden)) SetBit(_railtypes_hidden_mask, rt);
}
_sorted_railtypes.clear();
@ -2109,7 +2109,7 @@ static void DrawTrackBitsOverlay(TileInfo *ti, TrackBits track, const RailTypeIn
DrawGroundSprite(image, PAL_NONE);
}
bool no_combine = ti->tileh == SLOPE_FLAT && HasBit(rti->flags, RTF_NO_SPRITE_COMBINE);
bool no_combine = ti->tileh == SLOPE_FLAT && rti->flags.Test(RailTypeFlag::NoSpriteCombine);
SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
SpriteID ground = GetCustomRailSprite(rti, ti->tile, no_combine ? RTSG_GROUND_COMPLETE : RTSG_GROUND);
TrackBits pbs = _settings_client.gui.show_track_reservation ? GetRailReservationTrackBits(ti->tile) : TRACK_BIT_NONE;

View File

@ -122,7 +122,7 @@ bool HasRoadTypeAvail(const CompanyID company, RoadType roadtype)
* The GS under deity mode, as well as anybody in the editor builds roads that are
* owned by towns. So if a town may build it, it should be buildable by them too.
*/
return (rti->flags & ROTFB_HIDDEN) == 0 || (rti->flags & ROTFB_TOWN_BUILD) != 0;
return !rti->flags.Test( RoadTypeFlag::Hidden) || rti->flags.Test( RoadTypeFlag::TownBuild);
} else {
const Company *c = Company::GetIfValid(company);
if (c == nullptr) return false;

View File

@ -33,25 +33,15 @@ DECLARE_ENUM_AS_BIT_SET(RoadTramTypes)
static const RoadTramType _roadtramtypes[] = { RTT_ROAD, RTT_TRAM };
/** Roadtype flag bit numbers. Starts with RO instead of R because R is used for rails */
enum RoadTypeFlag {
ROTF_CATENARY = 0, ///< Bit number for adding catenary
ROTF_NO_LEVEL_CROSSING, ///< Bit number for disabling level crossing
ROTF_NO_HOUSES, ///< Bit number for setting this roadtype as not house friendly
ROTF_HIDDEN, ///< Bit number for hidden from construction.
ROTF_TOWN_BUILD, ///< Bit number for allowing towns to build this roadtype.
/** Roadtype flag bit numbers. */
enum class RoadTypeFlag : uint8_t {
Catenary = 0, ///< Bit number for adding catenary
NoLevelCrossing = 1, ///< Bit number for disabling level crossing
NoHouses = 2, ///< Bit number for setting this roadtype as not house friendly
Hidden = 3, ///< Bit number for hidden from construction.
TownBuild = 4, ///< Bit number for allowing towns to build this roadtype.
};
/** Roadtype flags. Starts with RO instead of R because R is used for rails */
enum RoadTypeFlags : uint8_t {
ROTFB_NONE = 0, ///< All flags cleared.
ROTFB_CATENARY = 1 << ROTF_CATENARY, ///< Value for drawing a catenary.
ROTFB_NO_LEVEL_CROSSING = 1 << ROTF_NO_LEVEL_CROSSING, ///< Value for disabling a level crossing.
ROTFB_NO_HOUSES = 1 << ROTF_NO_HOUSES, ///< Value for for setting this roadtype as not house friendly.
ROTFB_HIDDEN = 1 << ROTF_HIDDEN, ///< Value for hidden from construction.
ROTFB_TOWN_BUILD = 1 << ROTF_TOWN_BUILD, ///< Value for allowing towns to build this roadtype.
};
DECLARE_ENUM_AS_BIT_SET(RoadTypeFlags)
using RoadTypeFlags = EnumBitSet<RoadTypeFlag, uint8_t>;
struct SpriteGroup;
@ -295,7 +285,7 @@ inline Money RoadConvertCost(RoadType from, RoadType to)
inline bool RoadNoLevelCrossing(RoadType roadtype)
{
assert(roadtype < ROADTYPE_END);
return HasBit(GetRoadTypeInfo(roadtype)->flags, ROTF_NO_LEVEL_CROSSING);
return GetRoadTypeInfo(roadtype)->flags.Test(RoadTypeFlag::NoLevelCrossing);
}
RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels = true);

View File

@ -116,7 +116,7 @@ void InitRoadTypes()
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
RoadTypeInfo *rti = &_roadtypes[rt];
ResolveRoadTypeGUISprites(rti);
if (HasBit(rti->flags, ROTF_HIDDEN)) SetBit(_roadtypes_hidden_mask, rt);
if (rti->flags.Test(RoadTypeFlag::Hidden)) SetBit(_roadtypes_hidden_mask, rt);
}
_sorted_roadtypes.clear();
@ -141,7 +141,7 @@ RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt)
*rti = _original_roadtypes[(rtt == RTT_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD];
rti->label = label;
rti->alternate_labels.clear();
rti->flags = ROTFB_NONE;
rti->flags = {};
rti->introduction_date = CalendarTime::INVALID_DATE;
/* Make us compatible with ourself. */
@ -1830,7 +1830,7 @@ static void DrawTile_Road(TileInfo *ti)
int relocation = GetCustomRoadSprite(rti, ti->tile, ROTSG_DEPOT);
bool default_gfx = relocation == 0;
if (default_gfx) {
if (HasBit(rti->flags, ROTF_CATENARY)) {
if (rti->flags.Test(RoadTypeFlag::Catenary)) {
if (_loaded_newgrf_features.tram == TRAMWAY_REPLACE_DEPOT_WITH_TRACK && road_rt == INVALID_ROADTYPE && !rti->UsesOverlay()) {
/* Sprites with track only work for default tram */
relocation = SPR_TRAMWAY_DEPOT_WITH_TRACK - SPR_ROAD_DEPOT;
@ -1880,7 +1880,7 @@ void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
int relocation = GetCustomRoadSprite(rti, INVALID_TILE, ROTSG_DEPOT);
bool default_gfx = relocation == 0;
if (default_gfx) {
if (HasBit(rti->flags, ROTF_CATENARY)) {
if (rti->flags.Test(RoadTypeFlag::Catenary)) {
if (_loaded_newgrf_features.tram == TRAMWAY_REPLACE_DEPOT_WITH_TRACK && RoadTypeIsTram(rt) && !rti->UsesOverlay()) {
/* Sprites with track only work for default tram */
relocation = SPR_TRAMWAY_DEPOT_WITH_TRACK - SPR_ROAD_DEPOT;

View File

@ -135,7 +135,7 @@ inline Money RoadMaintenanceCost(RoadType roadtype, uint32_t num, uint32_t total
inline bool HasRoadCatenary(RoadType roadtype)
{
assert(roadtype < ROADTYPE_END);
return HasBit(GetRoadTypeInfo(roadtype)->flags, ROTF_CATENARY);
return GetRoadTypeInfo(roadtype)->flags.Test(RoadTypeFlag::Catenary);
}
/**

View File

@ -75,7 +75,7 @@ static const RailTypeInfo _original_railtypes[] = {
0,
/* flags */
RTFB_NONE,
{},
/* cost multiplier */
8,
@ -176,7 +176,7 @@ static const RailTypeInfo _original_railtypes[] = {
0,
/* flags */
RTFB_CATENARY,
{RailTypeFlag::Catenary},
/* cost multiplier */
12,
@ -273,7 +273,7 @@ static const RailTypeInfo _original_railtypes[] = {
1,
/* flags */
RTFB_NONE,
{},
/* cost multiplier */
16,
@ -370,7 +370,7 @@ static const RailTypeInfo _original_railtypes[] = {
2,
/* flags */
RTFB_NONE,
{},
/* cost multiplier */
24,

View File

@ -61,7 +61,7 @@ static const RoadTypeInfo _original_roadtypes[] = {
ROADTYPES_ROAD,
/* flags */
ROTFB_TOWN_BUILD,
{RoadTypeFlag::TownBuild},
/* cost multiplier */
8,
@ -141,7 +141,7 @@ static const RoadTypeInfo _original_roadtypes[] = {
ROADTYPES_TRAM,
/* flags */
ROTFB_CATENARY | ROTFB_NO_HOUSES,
{RoadTypeFlag::Catenary, RoadTypeFlag::NoHouses},
/* cost multiplier */
16,

View File

@ -972,7 +972,7 @@ RoadType GetTownRoadType()
if (rti->label == 0) continue;
/* Can town build this road. */
if (!HasBit(rti->flags, ROTF_TOWN_BUILD)) continue;
if (!rti->flags.Test(RoadTypeFlag::TownBuild)) continue;
/* Not yet introduced at this date. */
if (IsInsideMM(rti->introduction_date, 0, CalendarTime::MAX_DATE.base()) && rti->introduction_date > TimerGameCalendar::date) continue;
@ -999,7 +999,7 @@ static TimerGameCalendar::Date GetTownRoadTypeFirstIntroductionDate()
if (RoadTypeIsTram(rt)) continue;
const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
if (rti->label == 0) continue; // Unused road type.
if (!HasBit(rti->flags, ROTF_TOWN_BUILD)) continue; // Town can't build this road type.
if (!rti->flags.Test(RoadTypeFlag::TownBuild)) continue; // Town can't build this road type.
if (best != nullptr && rti->introduction_date >= best->introduction_date) continue;
best = rti;
@ -1496,8 +1496,8 @@ static inline bool RoadTypesAllowHouseHere(TileIndex t)
RoadType road_rt = GetRoadTypeRoad(cur_tile);
RoadType tram_rt = GetRoadTypeTram(cur_tile);
if (road_rt != INVALID_ROADTYPE && !HasBit(GetRoadTypeInfo(road_rt)->flags, ROTF_NO_HOUSES)) return true;
if (tram_rt != INVALID_ROADTYPE && !HasBit(GetRoadTypeInfo(tram_rt)->flags, ROTF_NO_HOUSES)) return true;
if (road_rt != INVALID_ROADTYPE && !GetRoadTypeInfo(road_rt)->flags.Test(RoadTypeFlag::NoHouses)) return true;
if (tram_rt != INVALID_ROADTYPE && !GetRoadTypeInfo(tram_rt)->flags.Test(RoadTypeFlag::NoHouses)) return true;
}
/* If no road was found surrounding the tile we can allow building the house since there is
@ -1516,7 +1516,7 @@ static bool TownCanGrowRoad(TileIndex tile)
/* Allow extending on roadtypes which can be built by town, or if the road type matches the type the town will build. */
RoadType rt = GetRoadTypeRoad(tile);
return HasBit(GetRoadTypeInfo(rt)->flags, ROTF_TOWN_BUILD) || GetTownRoadType() == rt;
return GetRoadTypeInfo(rt)->flags.Test(RoadTypeFlag::TownBuild) || GetTownRoadType() == rt;
}
/**