1
0
Fork 0

Codechange: Use EnumBitSet for EngineMiscFlags.

pull/13421/head
Peter Nelson 2025-01-30 21:03:43 +00:00 committed by Peter Nelson
parent 5664b1e2f6
commit 6c4ddb242a
13 changed files with 53 additions and 49 deletions

View File

@ -83,7 +83,7 @@ bool CheckAutoreplaceValidity(EngineID from, EngineID to, CompanyID company)
if ((GetRoadTypeInfo(e_from->u.road.roadtype)->powered_roadtypes & GetRoadTypeInfo(e_to->u.road.roadtype)->powered_roadtypes) == ROADTYPES_NONE) return false;
/* make sure that we do not replace a tram with a normal road vehicles or vice versa */
if (HasBit(e_from->info.misc_flags, EF_ROAD_TRAM) != HasBit(e_to->info.misc_flags, EF_ROAD_TRAM)) return false;
if (e_from->info.misc_flags.Test(EngineMiscFlag::RoadIsTram) != e_to->info.misc_flags.Test(EngineMiscFlag::RoadIsTram)) return false;
break;
case VEH_AIRCRAFT:

View File

@ -1346,7 +1346,7 @@ static uint GetLoadAmount(Vehicle *v)
}
/* Scale load amount the same as capacity */
if (HasBit(e->info.misc_flags, EF_NO_DEFAULT_CARGO_MULTIPLIER) && !air_mail) load_amount = CeilDiv(load_amount * CargoSpec::Get(v->cargo_type)->multiplier, 0x100);
if (e->info.misc_flags.Test(EngineMiscFlag::NoDefaultCargoMultiplier) && !air_mail) load_amount = CeilDiv(load_amount * CargoSpec::Get(v->cargo_type)->multiplier, 0x100);
/* Zero load amount breaks a lot of things. */
return std::max(1u, load_amount);

View File

@ -205,7 +205,7 @@ uint Engine::DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity) const
if (!this->CanCarryCargo()) return 0;
bool new_multipliers = HasBit(this->info.misc_flags, EF_NO_DEFAULT_CARGO_MULTIPLIER);
bool new_multipliers = this->info.misc_flags.Test(EngineMiscFlag::NoDefaultCargoMultiplier);
CargoType default_cargo = this->GetDefaultCargoType();
CargoType cargo_type = (v != nullptr) ? v->cargo_type : default_cargo;

View File

@ -141,6 +141,25 @@ enum class ExtraEngineFlag : uint8_t {
};
using ExtraEngineFlags = EnumBitSet<ExtraEngineFlag, uint8_t>;
/**
* EngineInfo.misc_flags is a bitmask, with the following values
*/
enum class EngineMiscFlag : uint8_t {
RailTilts = 0, ///< Rail vehicle tilts in curves
RoadIsTram = 0, ///< Road vehicle is a tram/light rail vehicle
Uses2CC = 1, ///< Vehicle uses two company colours
RailIsMU = 2, ///< Rail vehicle is a multiple-unit (DMU/EMU)
RailFlips = 3, ///< Rail vehicle has old depot-flip handling
AutoRefit = 4, ///< Automatic refitting is allowed
NoDefaultCargoMultiplier = 5, ///< Use the new capacity algorithm. The default cargotype of the vehicle does not affect capacity multipliers. CB 15 is also called in purchase list.
NoBreakdownSmoke = 6, ///< Do not show black smoke during a breakdown.
SpriteStack = 7, ///< Draw vehicle by stacking multiple sprites.
};
using EngineMiscFlags = EnumBitSet<EngineMiscFlag, uint8_t>;
/**
* Information about a vehicle
* @see table/engines.h
@ -156,7 +175,7 @@ struct EngineInfo {
std::variant<CargoLabel, MixedCargoType> cargo_label;
CargoTypes refit_mask;
uint8_t refit_cost;
uint8_t misc_flags; ///< Miscellaneous flags. @see EngineMiscFlags
EngineMiscFlags misc_flags; ///< Miscellaneous flags. @see EngineMiscFlags
VehicleCallbackMasks callback_mask; ///< Bitmask of vehicle callbacks that have to be called
int8_t retire_early; ///< Number of years early to retire vehicle
ExtraEngineFlags extra_flags;
@ -165,21 +184,6 @@ struct EngineInfo {
EngineID variant_id; ///< Engine variant ID. If set, will be treated specially in purchase lists.
};
/**
* EngineInfo.misc_flags is a bitmask, with the following values
*/
enum EngineMiscFlags : uint8_t {
EF_RAIL_TILTS = 0, ///< Rail vehicle tilts in curves
EF_ROAD_TRAM = 0, ///< Road vehicle is a tram/light rail vehicle
EF_USES_2CC = 1, ///< Vehicle uses two company colours
EF_RAIL_IS_MU = 2, ///< Rail vehicle is a multiple-unit (DMU/EMU)
EF_RAIL_FLIPS = 3, ///< Rail vehicle has old depot-flip handling
EF_AUTO_REFIT = 4, ///< Automatic refitting is allowed
EF_NO_DEFAULT_CARGO_MULTIPLIER = 5, ///< Use the new capacity algorithm. The default cargotype of the vehicle does not affect capacity multipliers. CB 15 is also called in purchase list.
EF_NO_BREAKDOWN_SMOKE = 6, ///< Do not show black smoke during a breakdown.
EF_SPRITE_STACK = 7, ///< Draw vehicle by stacking multiple sprites.
};
/**
* Engine.flags is a bitmask, with the following values.
*/

View File

@ -1294,8 +1294,8 @@ static ChangeInfoResult RailVehicleChangeInfo(uint first, uint last, int prop, B
break;
case 0x27: // Miscellaneous flags
ei->misc_flags = buf.ReadByte();
_loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
ei->misc_flags = static_cast<EngineMiscFlags>(buf.ReadByte());
_loaded_newgrf_features.has_2CC |= ei->misc_flags.Test(EngineMiscFlag::Uses2CC);
break;
case 0x28: // Cargo classes allowed
@ -1491,8 +1491,8 @@ static ChangeInfoResult RoadVehicleChangeInfo(uint first, uint last, int prop, B
break;
case 0x1C: // Miscellaneous flags
ei->misc_flags = buf.ReadByte();
_loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
ei->misc_flags = static_cast<EngineMiscFlags>(buf.ReadByte());
_loaded_newgrf_features.has_2CC |= ei->misc_flags.Test(EngineMiscFlag::Uses2CC);
break;
case 0x1D: // Cargo classes allowed
@ -1684,8 +1684,8 @@ static ChangeInfoResult ShipVehicleChangeInfo(uint first, uint last, int prop, B
break;
case 0x17: // Miscellaneous flags
ei->misc_flags = buf.ReadByte();
_loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
ei->misc_flags = static_cast<EngineMiscFlags>(buf.ReadByte());
_loaded_newgrf_features.has_2CC |= ei->misc_flags.Test(EngineMiscFlag::Uses2CC);
break;
case 0x18: // Cargo classes allowed
@ -1873,8 +1873,8 @@ static ChangeInfoResult AircraftVehicleChangeInfo(uint first, uint last, int pro
break;
case 0x17: // Miscellaneous flags
ei->misc_flags = buf.ReadByte();
_loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
ei->misc_flags = static_cast<EngineMiscFlags>(buf.ReadByte());
_loaded_newgrf_features.has_2CC |= ei->misc_flags.Test(EngineMiscFlag::Uses2CC);
break;
case 0x18: // Cargo classes allowed
@ -10016,7 +10016,7 @@ static void AfterLoadGRFs()
e->u.road.max_speed = _gted[e->index].rv_max_speed * 4;
}
RoadTramType rtt = HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? RTT_TRAM : RTT_ROAD;
RoadTramType rtt = e->info.misc_flags.Test(EngineMiscFlag::RoadIsTram) ? RTT_TRAM : RTT_ROAD;
const GRFFile *file = e->GetGRF();
if (file == nullptr || _gted[e->index].roadtramtype == 0) {

View File

@ -1076,7 +1076,7 @@ void GetCustomEngineSprite(EngineID engine, const Vehicle *v, Direction directio
VehicleResolverObject object(engine, v, VehicleResolverObject::WO_CACHED, false, CBID_NO_CALLBACK);
result->Clear();
bool sprite_stack = HasBit(EngInfo(engine)->misc_flags, EF_SPRITE_STACK);
bool sprite_stack = EngInfo(engine)->misc_flags.Test(EngineMiscFlag::SpriteStack);
uint max_stack = sprite_stack ? lengthof(result->seq) : 1;
for (uint stack = 0; stack < max_stack; ++stack) {
object.ResetState();
@ -1110,7 +1110,7 @@ void GetRotorOverrideSprite(EngineID engine, const struct Aircraft *v, EngineIma
result->Clear();
uint rotor_pos = v == nullptr || rotor_in_gui ? 0 : v->Next()->Next()->state;
bool sprite_stack = HasBit(e->info.misc_flags, EF_SPRITE_STACK);
bool sprite_stack = e->info.misc_flags.Test(EngineMiscFlag::SpriteStack);
uint max_stack = sprite_stack ? lengthof(result->seq) : 1;
for (uint stack = 0; stack < max_stack; ++stack) {
object.ResetState();

View File

@ -800,7 +800,7 @@ private:
this->can_do_autorefit = false;
for (const Vehicle *w = this->vehicle; w != nullptr; w = w->IsGroundVehicle() ? w->Next() : nullptr) {
if (IsEngineRefittable(w->engine_type)) this->can_do_refit = true;
if (HasBit(Engine::Get(w->engine_type)->info.misc_flags, EF_AUTO_REFIT)) this->can_do_autorefit = true;
if (Engine::Get(w->engine_type)->info.misc_flags.Test(EngineMiscFlag::AutoRefit)) this->can_do_autorefit = true;
}
}

View File

@ -1940,7 +1940,7 @@ bool AfterLoadGame()
/* Remove all trams from savegames without tram support.
* There would be trams without tram track under causing crashes sooner or later. */
for (RoadVehicle *v : RoadVehicle::Iterate()) {
if (v->First() == v && HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM)) {
if (v->First() == v && EngInfo(v->engine_type)->misc_flags.Test(EngineMiscFlag::RoadIsTram)) {
ShowErrorMessage(STR_WARNING_LOADGAME_REMOVED_TRAMS, INVALID_STRING_ID, WL_CRITICAL);
delete v;
}

View File

@ -24,7 +24,7 @@
* @param f Bitmask of the climates
* @note the 5 between b and f is the load amount
*/
#define MT(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 5, f, INVALID_CARGO, e, 0, 8, 0, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
#define MT(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 5, f, INVALID_CARGO, e, 0, 8, EngineMiscFlags{}, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
/**
* Writes the properties of a multiple-unit train into the EngineInfo struct.
@ -37,7 +37,7 @@
* @param f Bitmask of the climates
* @note the 5 between b and f is the load amount
*/
#define MM(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 5, f, INVALID_CARGO, e, 0, 8, 1 << EF_RAIL_IS_MU, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
#define MM(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 5, f, INVALID_CARGO, e, 0, 8, EngineMiscFlags{EngineMiscFlag::RailIsMU}, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
/**
* Writes the properties of a train carriage into the EngineInfo struct.
@ -50,7 +50,7 @@
* @see MT
* @note the 5 between b and f is the load amount
*/
#define MW(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 5, f, INVALID_CARGO, e, 0, 8, 0, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
#define MW(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 5, f, INVALID_CARGO, e, 0, 8, EngineMiscFlags{}, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
/**
* Writes the properties of a road vehicle into the EngineInfo struct.
@ -63,7 +63,7 @@
* @param f Bitmask of the climates
* @note the 5 between b and f is the load amount
*/
#define MR(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 5, f, INVALID_CARGO, e, 0, 8, 0, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
#define MR(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 5, f, INVALID_CARGO, e, 0, 8, EngineMiscFlags{}, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
/**
* Writes the properties of a ship into the EngineInfo struct.
@ -75,7 +75,7 @@
* @param f Bitmask of the climates
* @note the 10 between b and f is the load amount
*/
#define MS(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 10, f, INVALID_CARGO, e, 0, 8, 0, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
#define MS(a, b, c, d, e, f) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 10, f, INVALID_CARGO, e, 0, 8, EngineMiscFlags{}, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
/**
* Writes the properties of an aeroplane into the EngineInfo struct.
@ -86,7 +86,7 @@
* @param e Bitmask of the climates
* @note the 20 between b and e is the load amount
*/
#define MA(a, b, c, d, e) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 20, e, INVALID_CARGO, CT_INVALID, 0, 8, 0, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
#define MA(a, b, c, d, e) { CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + a, TimerGameCalendar::Year{c}, TimerGameCalendar::Year{d}, b, 20, e, INVALID_CARGO, CT_INVALID, 0, 8, EngineMiscFlags{}, VehicleCallbackMasks{}, 0, {}, STR_EMPTY, Ticks::CARGO_AGING_TICKS, INVALID_ENGINE }
/* Climates
* T = Temperate

View File

@ -150,7 +150,7 @@ void Train::ConsistChanged(ConsistChangeFlags allowed_changes)
const Engine *e_u = u->GetEngine();
const RailVehicleInfo *rvi_u = &e_u->u.rail;
if (!HasBit(e_u->info.misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
if (!e_u->info.misc_flags.Test(EngineMiscFlag::RailTilts)) train_can_tilt = false;
min_curve_speed_mod = std::min(min_curve_speed_mod, u->GetCurveSpeedModifier());
/* Cache wagon override sprite group. nullptr is returned if there is none */
@ -439,7 +439,7 @@ void Train::UpdateAcceleration()
int Train::GetCursorImageOffset() const
{
if (this->gcache.cached_veh_length != 8 && HasBit(this->flags, VRF_REVERSE_DIRECTION) && !HasBit(EngInfo(this->engine_type)->misc_flags, EF_RAIL_FLIPS)) {
if (this->gcache.cached_veh_length != 8 && HasBit(this->flags, VRF_REVERSE_DIRECTION) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips)) {
int reference_width = TRAININFO_DEFAULT_VEHICLE_WIDTH;
const Engine *e = this->GetEngine();
@ -469,7 +469,7 @@ int Train::GetDisplayImageWidth(Point *offset) const
}
if (offset != nullptr) {
if (HasBit(this->flags, VRF_REVERSE_DIRECTION) && !HasBit(EngInfo(this->engine_type)->misc_flags, EF_RAIL_FLIPS)) {
if (HasBit(this->flags, VRF_REVERSE_DIRECTION) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips)) {
offset->x = ScaleSpriteTrad(((int)this->gcache.cached_veh_length - (int)VEHICLE_LENGTH / 2) * reference_width / (int)VEHICLE_LENGTH);
} else {
offset->x = ScaleSpriteTrad(reference_width) / 2;
@ -1474,7 +1474,7 @@ void Train::UpdateDeltaXY()
this->y_bb_offs = 0;
/* Set if flipped and engine is NOT flagged with custom flip handling. */
int flipped = HasBit(this->flags, VRF_REVERSE_DIRECTION) && !HasBit(EngInfo(this->engine_type)->misc_flags, EF_RAIL_FLIPS);
int flipped = HasBit(this->flags, VRF_REVERSE_DIRECTION) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips);
/* If flipped and vehicle length is odd, we need to adjust the bounding box offset slightly. */
int flip_offs = flipped && (this->gcache.cached_veh_length & 1);

View File

@ -1391,7 +1391,7 @@ bool Vehicle::HandleBreakdown()
(train_or_ship ? SND_3A_BREAKDOWN_TRAIN_SHIP_TOYLAND : SND_35_BREAKDOWN_ROADVEHICLE_TOYLAND), this);
}
if (!(this->vehstatus & VS_HIDDEN) && !HasBit(EngInfo(this->engine_type)->misc_flags, EF_NO_BREAKDOWN_SMOKE)) {
if (!(this->vehstatus & VS_HIDDEN) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::NoBreakdownSmoke)) {
EffectVehicle *u = CreateEffectVehicleRel(this, 4, 4, 5, EV_BREAKDOWN_SMOKE);
if (u != nullptr) u->animation_state = this->breakdown_delay * 2;
}
@ -1990,7 +1990,7 @@ LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_
if (parent_engine_type == INVALID_ENGINE) {
return LS_PASSENGER_WAGON_STEAM;
} else {
bool is_mu = HasBit(EngInfo(parent_engine_type)->misc_flags, EF_RAIL_IS_MU);
bool is_mu = EngInfo(parent_engine_type)->misc_flags.Test(EngineMiscFlag::RailIsMU);
switch (RailVehInfo(parent_engine_type)->engclass) {
default: NOT_REACHED();
case EC_STEAM: return LS_PASSENGER_WAGON_STEAM;
@ -2004,7 +2004,7 @@ LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_
return LS_FREIGHT_WAGON;
}
} else {
bool is_mu = HasBit(e->info.misc_flags, EF_RAIL_IS_MU);
bool is_mu = e->info.misc_flags.Test(EngineMiscFlag::RailIsMU);
switch (e->u.rail.engclass) {
default: NOT_REACHED();
@ -2028,7 +2028,7 @@ LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_
assert(IsValidCargoType(cargo_type));
/* Important: Use Tram Flag of front part. Luckily engine_type refers to the front part here. */
if (HasBit(e->info.misc_flags, EF_ROAD_TRAM)) {
if (e->info.misc_flags.Test(EngineMiscFlag::RoadIsTram)) {
/* Tram */
return IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_TRAM : LS_FREIGHT_TRAM;
} else {
@ -2116,7 +2116,7 @@ static PaletteID GetEngineColourMap(EngineID engine_type, CompanyID company, Eng
}
}
bool twocc = HasBit(e->info.misc_flags, EF_USES_2CC);
bool twocc = e->info.misc_flags.Test(EngineMiscFlag::Uses2CC);
if (map == PAL_NONE) map = twocc ? (PaletteID)SPR_2CCMAP_BASE : (PaletteID)PALETTE_RECOLOUR_START;

View File

@ -386,7 +386,7 @@ static std::tuple<CommandCost, uint, uint16_t, CargoArray> RefitVehicle(Vehicle
/* If the vehicle is not refittable, or does not allow automatic refitting,
* count its capacity nevertheless if the cargo matches */
bool refittable = HasBit(e->info.refit_mask, new_cargo_type) && (!auto_refit || HasBit(e->info.misc_flags, EF_AUTO_REFIT));
bool refittable = HasBit(e->info.refit_mask, new_cargo_type) && (!auto_refit || e->info.misc_flags.Test(EngineMiscFlag::AutoRefit));
if (!refittable && v->cargo_type != new_cargo_type) {
uint amount = e->DetermineCapacity(v, nullptr);
if (amount > 0) cargo_capacities[v->cargo_type] += amount;

View File

@ -795,7 +795,7 @@ struct RefitWindow : public Window {
/* Skip this engine if it does not carry anything */
if (!e->CanCarryCargo()) continue;
/* Skip this engine if we build the list for auto-refitting and engine doesn't allow it. */
if (this->auto_refit && !HasBit(e->info.misc_flags, EF_AUTO_REFIT)) continue;
if (this->auto_refit && !e->info.misc_flags.Test(EngineMiscFlag::AutoRefit)) continue;
/* Loop through all cargoes in the refit mask */
for (const auto &cs : _sorted_cargo_specs) {