From 54d3a1040a370eaec52800f54b892795d207e4ba Mon Sep 17 00:00:00 2001 From: Hirek193 Date: Sat, 28 Nov 2026 01:22:57 +0100 Subject: [PATCH] refactor(physics): EnumSet for the sets of flags; the damage of a vehicle is VehicleDamage and DamageSet - utilities/enumset.h: EnumSet, a set of the values of an enum class whose values are bits, with the operations CouplingSet had (HasAll, HasAny, Empty, Set, Clear, Without, |, &, ^, ==, FromBits, Bits). CouplingSet is EnumSet now; an enum is a kind of the sets by IsEnumSetKind, which gives it its operator |. - the damage of a vehicle (DamageFlag) is a DamageSet of VehicleDamage instead of the int and the dtrain_* constants. A bit means one thing for a locomotive and another for a wagon; both names are there (thinWheel and loadShift, ventilator and loadDamage, engine and loadDestroyed). - the SetDamage command of the scenery adds or takes away the damage by its number as SetFlag() did (setOrClear()); "WRECKED" is still the set of the number 255; the bearing divisor of the resistance stays the number 4. - the damage of the tracks and of the catenary (dtrack_*, the traction's DamageFlag) are other flags and stay as they were. No change in behaviour: 1144/1144 tests, golden traces exact against staging 62f7a29b. --- application/driveruipanels.cpp | 2 +- physics/components/body.cpp | 38 +++--- physics/components/collisions.cpp | 32 +++--- physics/components/commands.cpp | 28 ++++- physics/components/couplers.cpp | 2 +- physics/components/electrics.cpp | 2 +- physics/components/traction.cpp | 10 +- physics/mover.h | 4 +- physics/vehicletypes.h | 153 +++++-------------------- scene/scene.cpp | 2 +- tests/unit/test_cab.cpp | 8 +- tests/unit/test_collisions.cpp | 12 +- tests/unit/test_commands.cpp | 2 +- tests/unit/test_controls.cpp | 2 +- tests/unit/test_coupler_physics.cpp | 12 +- tests/unit/test_couplers.cpp | 2 +- tests/unit/test_couplers_trains.cpp | 12 +- tests/unit/test_derail_motion.cpp | 10 +- tests/unit/test_derailment_vehicle.cpp | 24 ++-- tests/unit/test_queries.cpp | 26 ++--- utilities/enumset.h | 130 +++++++++++++++++++++ vehicle/DynObj.cpp | 6 +- world/station.cpp | 4 +- 23 files changed, 286 insertions(+), 237 deletions(-) create mode 100644 utilities/enumset.h diff --git a/application/driveruipanels.cpp b/application/driveruipanels.cpp index 2d7c3fc9a..cb9f9e6d8 100644 --- a/application/driveruipanels.cpp +++ b/application/driveruipanels.cpp @@ -618,7 +618,7 @@ debug_panel::render() { } // sections ImGui::Separator(); - if (true == render_section( "Vehicle", m_vehiclelines ) && DebugModeFlag && m_input.mover && m_input.mover->DamageFlag != 0 && true == ImGui::Button( "Stop and repair consist" )) { + if (true == render_section( "Vehicle", m_vehiclelines ) && DebugModeFlag && m_input.mover && false == m_input.mover->DamageFlag.Empty() && true == ImGui::Button( "Stop and repair consist" )) { command_relay commandrelay; commandrelay.post(user_command::resetconsist, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &m_input.vehicle->name()); } diff --git a/physics/components/body.cpp b/physics/components/body.cpp index d34396304..90acf4544 100644 --- a/physics/components/body.cpp +++ b/physics/components/body.cpp @@ -50,7 +50,7 @@ namespace // whatever pushes it, and the train behind it stopped at once against it, in a hit no train had bool legacy_derailed_slides(TMoverParameters const &Vehicle) { - return Global.collisions == global_settings::collision_model::physical && TestFlag(Vehicle.CategoryFlag, 1) && TestFlag(Vehicle.DamageFlag, dtrain_out) && + return Global.collisions == global_settings::collision_model::physical && TestFlag(Vehicle.CategoryFlag, 1) && Vehicle.DamageFlag.HasAll(VehicleDamage::derailed) && Vehicle.Derailed == derailment::outcome::none; } @@ -210,7 +210,7 @@ void TMoverParameters::change_train_velocity(double const Change) double TMoverParameters::ComputeMovement(double dt, double dt1, const TTrackShape &Shape, TTrackParam const &Track, TTractionParam const &ElectricTraction, TLocation const &NewLoc, TRotation const &NewRot) { - if (!TestFlag(DamageFlag, dtrain_out)) + if (!DamageFlag.HasAll(VehicleDamage::derailed)) { // Ra: to przepisywanie tu jest bez sensu RunningShape = Shape; RunningTrack = Track; @@ -234,7 +234,7 @@ double TMoverParameters::ComputeMovement(double dt, double dt1, const TTrackShap if (CategoryFlag == 4) OffsetTrackV = TotalMass / (Dim.L * Dim.W * 1000.0); - else if (TestFlag(CategoryFlag, 1) && TestFlag(RunningTrack.CategoryFlag, 1) && TestFlag(DamageFlag, dtrain_out)) + else if (TestFlag(CategoryFlag, 1) && TestFlag(RunningTrack.CategoryFlag, 1) && DamageFlag.HasAll(VehicleDamage::derailed)) { OffsetTrackV = -0.2; OffsetTrackH = Sign(RunningShape.R) * 0.2; @@ -253,7 +253,7 @@ double TMoverParameters::ComputeMovement(double dt, double dt1, const TTrackShap // a vehicle derailed by the physical criteria slides on with the friction of ComputeTotalForce(), and so does one // derailed the legacy way with physics.collisions new - if (TestFlag(DamageFlag, dtrain_out) && Derailed == derailment::outcome::none && false == legacy_derailed_slides(*this)) + if (DamageFlag.HasAll(VehicleDamage::derailed) && Derailed == derailment::outcome::none && false == legacy_derailed_slides(*this)) AccS = -Sign(V) * g * 1; // random(0.0, 0.1) // stuck in the ballast: the friction holds a derailed vehicle once it stopped, until the forces on it are larger auto const stuck{(Derailed != derailment::outcome::none || legacy_derailed_slides(*this)) && V == 0.0 && std::abs(FTrain) <= std::abs(FStand)}; @@ -365,7 +365,7 @@ double TMoverParameters::FastComputeMovement(double dt, const TTrackShape & /*Sh AccS = std::lerp(AccSprev, FTotal / TotalMass, 0.5); // std::clamp( dt * 3.0, 0.0, 1.0 ) ); // prawo Newtona ale z wygladzaniem (średnia z poprzednim) - if (TestFlag(DamageFlag, dtrain_out) && Derailed == derailment::outcome::none && false == legacy_derailed_slides(*this)) + if (DamageFlag.HasAll(VehicleDamage::derailed) && Derailed == derailment::outcome::none && false == legacy_derailed_slides(*this)) AccS = -Sign(V) * g * 1; // * random(0.0, 0.1) auto const stuck{(Derailed != derailment::outcome::none || legacy_derailed_slides(*this)) && V == 0.0 && std::abs(FTrain) <= std::abs(FStand)}; if (stuck) @@ -392,7 +392,7 @@ double TMoverParameters::FastComputeMovement(double dt, const TTrackShape & /*Sh // a vehicle derailed by the physical criteria slides on with the friction of ComputeTotalForce(), and so does one // derailed the legacy way with physics.collisions new - if (true == TestFlag(DamageFlag, dtrain_out) && Derailed == derailment::outcome::none && false == legacy_derailed_slides(*this) && Vel < 1.0) + if (true == DamageFlag.HasAll(VehicleDamage::derailed) && Derailed == derailment::outcome::none && false == legacy_derailed_slides(*this) && Vel < 1.0) { V = 0.0; AccS = 0.0; @@ -717,7 +717,7 @@ void TMoverParameters::ComputeConstans(void) double Curvature; // Ra 2014-07: odwrotność promienia TotalMassxg = TotalMass * g; // TotalMass*g - BearingF = (DamageFlag & dtrain_bearing) != 0 ? 2.0 : 0; + BearingF = DamageFlag.HasAny(VehicleDamage::bearing) ? 2.0 : 0; HideModifier = 0; // int(Couplers[0].CouplingFlag>0)+int(Couplers[1].CouplingFlag>0); @@ -763,8 +763,8 @@ void TMoverParameters::ComputeConstans(void) Curvature = 0.0; } // opór składu na łuku (youBy): +(500*TrackW/R)*TotalMassxg*0.001 do FrictConst2s/d - FrictConst2s = TotalMassxg * (500.0 * TrackW * Curvature + 2.5 - HideModifier + 2 * BearingF / dtrain_bearing) * 0.001; - FrictConst2d = TotalMassxg * (500.0 * TrackW * Curvature + 2.0 - HideModifier + BearingF / dtrain_bearing) * 0.001; + FrictConst2s = TotalMassxg * (500.0 * TrackW * Curvature + 2.5 - HideModifier + 2 * BearingF / 4.0) * 0.001; + FrictConst2d = TotalMassxg * (500.0 * TrackW * Curvature + 2.0 - HideModifier + BearingF / 4.0) * 0.001; } // ************************************************************************************************* @@ -839,7 +839,7 @@ void TMoverParameters::ComputeTotalForce(double dt) // juz zoptymalizowane: FStand = FrictionForce(); // siła oporów ruchu - if (true == TestFlag(DamageFlag, dtrain_out)) + if (true == DamageFlag.HasAll(VehicleDamage::derailed)) { if (Derailed != derailment::outcome::none) { @@ -1220,50 +1220,50 @@ std::string TMoverParameters::EngineDescription(int what) const std::string outstr{"OK"}; if (what == 0) { - if (DamageFlag == 255) + if (DamageFlag == DamageSet::FromBits(255)) // every damage but the pantograph { outstr = "WRECKED"; } else { - if (TestFlag(DamageFlag, dtrain_thinwheel)) + if (DamageFlag.HasAll(VehicleDamage::thinWheel)) { if (Power > 0.1) outstr = "Thin wheel"; else outstr = "Load shifted"; } - if (WheelFlat > 5.0 || TestFlag(DamageFlag, dtrain_wheelwear)) + if (WheelFlat > 5.0 || DamageFlag.HasAll(VehicleDamage::wheelWear)) { outstr = "Wheel wear"; } - if (TestFlag(DamageFlag, dtrain_bearing)) + if (DamageFlag.HasAll(VehicleDamage::bearing)) { outstr = "Bearing damaged"; } - if (TestFlag(DamageFlag, dtrain_coupling)) + if (DamageFlag.HasAll(VehicleDamage::coupling)) { outstr = "Coupler broken"; } - if (TestFlag(DamageFlag, dtrain_loaddamage)) + if (DamageFlag.HasAll(VehicleDamage::loadDamage)) { if (Power > 0.1) outstr = "Ventilator damaged"; else outstr = "Load damaged"; } - if (TestFlag(DamageFlag, dtrain_loaddestroyed)) + if (DamageFlag.HasAll(VehicleDamage::loadDestroyed)) { if (Power > 0.1) outstr = "Engine damaged"; else outstr = "LOAD DESTROYED"; } - if (TestFlag(DamageFlag, dtrain_axle)) + if (DamageFlag.HasAll(VehicleDamage::axle)) { outstr = "Axle broken"; } - if (TestFlag(DamageFlag, dtrain_out)) + if (DamageFlag.HasAll(VehicleDamage::derailed)) { outstr = "DERAILED"; } diff --git a/physics/components/collisions.cpp b/physics/components/collisions.cpp index 948db6fa5..12266fed8 100644 --- a/physics/components/collisions.cpp +++ b/physics/components/collisions.cpp @@ -141,13 +141,13 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) } // HACK: limit excessive speed derailment checks to vehicles which aren't part of the same consist - auto const outsideconsist{thiscoupler.CouplingFlag == Coupling::none || true == TestFlag(othervehicle->DamageFlag, dtrain_out)}; + auto const outsideconsist{thiscoupler.CouplingFlag == Coupling::none || true == othervehicle->DamageFlag.HasAll(VehicleDamage::derailed)}; auto const velocitydifference{outsideconsist ? CollisionVelocity(End, *othervehicle, otherend) : 0.0}; if (velocitydifference > SafeCollisionVelocity) { // HACK: crude estimation for potential derail, will take place with velocity difference > 15 km/h adjusted for vehicle mass ratio - if (false == TestFlag(DamageFlag, dtrain_out) || false == TestFlag(othervehicle->DamageFlag, dtrain_out)) + if (false == DamageFlag.HasAll(VehicleDamage::derailed) || false == othervehicle->DamageFlag.HasAll(VehicleDamage::derailed)) { WriteLog("Bad driving: " + Name + " and " + othervehicle->Name + " collided with velocity " + to_string(velocitydifference, 0) + " km/h"); } @@ -171,7 +171,7 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) // the crush, the vehicles coupled to the wall push it on (crush_hit()) auto const coupled{thiscoupler.CouplingFlag != Coupling::none}; auto const wall = [crushing, coupled](TMoverParameters const &Vehicle) { - return TestFlag(Vehicle.DamageFlag, dtrain_out) && (false == crushing || (Vehicle.Derailed == derailment::outcome::none && false == coupled)); + return Vehicle.DamageFlag.HasAll(VehicleDamage::derailed) && (false == crushing || (Vehicle.Derailed == derailment::outcome::none && false == coupled)); }; if (false == wall(*this)) { @@ -198,7 +198,7 @@ void TMoverParameters::damage_coupler(int const End) return; } // HACK: don't break articulated couplings no matter what - if (SetFlag(DamageFlag, dtrain_coupling)) + if (DamageFlag.Set(VehicleDamage::coupling)) EventFlag = true; auto const hosetorn{thiscoupler.CouplingFlag.HasAll(Coupling::brakeHose)}; @@ -254,7 +254,7 @@ double TMoverParameters::buffer_height_at(int const End) const void TMoverParameters::derail_in_collision(DerailReason const Reason, std::string const &Why) { - if (false == SetFlag(DamageFlag, dtrain_out)) + if (false == DamageFlag.Set(VehicleDamage::derailed)) { return; } @@ -378,8 +378,8 @@ bool TMoverParameters::crush_hit(int const End, double const dt) // train. the vehicles coupled to it push it on, as the train behind a derailed vehicle does: a wall to them, it // stopped the whole train at once, and the vehicles behind it crushed by the energy of the train auto const coupled{thiscoupler.CouplingFlag != Coupling::none}; - auto const wall{false == coupled && TestFlag(DamageFlag, dtrain_out) && Derailed == derailment::outcome::none}; - auto const otherwall{false == coupled && TestFlag(othervehicle->DamageFlag, dtrain_out) && othervehicle->Derailed == derailment::outcome::none}; + auto const wall{false == coupled && DamageFlag.HasAll(VehicleDamage::derailed) && Derailed == derailment::outcome::none}; + auto const otherwall{false == coupled && othervehicle->DamageFlag.HasAll(VehicleDamage::derailed) && othervehicle->Derailed == derailment::outcome::none}; if (wall && otherwall) { return false; @@ -547,7 +547,7 @@ bool TMoverParameters::crush_hit(int const End, double const dt) void TMoverParameters::check_pushed_off(double const dt, double const Distance) { - if (Global.collisions != global_settings::collision_model::physical || false == Global.crash_damage || TestFlag(DamageFlag, dtrain_out) || false == TestFlag(CategoryFlag, 1) || + if (Global.collisions != global_settings::collision_model::physical || false == Global.crash_damage || DamageFlag.HasAll(VehicleDamage::derailed) || false == TestFlag(CategoryFlag, 1) || DebugModeFlag) { pushed_off_monitor.reset(); @@ -645,7 +645,7 @@ void repair(TMoverParameters &Vehicle, TCoupling &Coupler) { return; } - ClearFlag(Vehicle.DamageFlag, dtrain_coupling); + Vehicle.DamageFlag.Clear(VehicleDamage::coupling); // either vehicle of the pair may have been braking through the torn hose Vehicle.AlarmChainFlag = false; } @@ -683,7 +683,7 @@ double TMoverParameters::centre_of_gravity() const void TMoverParameters::check_derailment(TTrackShape const &Shape, TTrackParam const &Track, double const Distance) { - if (TestFlag(DamageFlag, dtrain_out) || false == TestFlag(CategoryFlag, 1) || DebugModeFlag) + if (DamageFlag.HasAll(VehicleDamage::derailed) || false == TestFlag(CategoryFlag, 1) || DebugModeFlag) { derailment_monitor.reset(); return; @@ -736,7 +736,7 @@ void TMoverParameters::check_derailment(TTrackShape const &Shape, TTrackParam co void TMoverParameters::derail(derailment::verdict const &Verdict, derailment::vehicle_state const &Vehicle, derailment::track_state const &Track, std::string const *TrackName) { - if (false == SetFlag(DamageFlag, dtrain_out)) + if (false == DamageFlag.Set(VehicleDamage::derailed)) { return; } @@ -759,7 +759,7 @@ void TMoverParameters::derail(derailment::verdict const &Verdict, derailment::ve void TMoverParameters::rerail() { - ClearFlag(DamageFlag, dtrain_out); + DamageFlag.Clear(VehicleDamage::derailed); Derailed = derailment::outcome::none; // and back on the track; what derailed it before has no say in how it leaves the track the next time OffTrack.reset(); @@ -787,7 +787,7 @@ void TMoverParameters::rerail() void TMoverParameters::repair_damage() { - DamageFlag = 0; + DamageFlag = {}; EngDmgFlag = 0; WheelFlat = 0.0; AlarmChainFlag = false; @@ -801,7 +801,7 @@ void TMoverParameters::repair_damage() void TMoverParameters::Derail(DerailReason const Reason) { - if (SetFlag(DamageFlag, dtrain_out)) + if (DamageFlag.Set(VehicleDamage::derailed)) { EventFlag = true; MainSwitch(false, range_t::local); @@ -852,13 +852,13 @@ void TMoverParameters::Derail(DerailReason const Reason) bool TMoverParameters::leaves_track() const { - return Global.derailment == global_settings::derailment_model::physical && TestFlag(CategoryFlag, 1) && TestFlag(DamageFlag, dtrain_out) && Derailed != derailment::outcome::none && + return Global.derailment == global_settings::derailment_model::physical && TestFlag(CategoryFlag, 1) && DamageFlag.HasAll(VehicleDamage::derailed) && Derailed != derailment::outcome::none && false == OffTrack.active(); } void TMoverParameters::pull_off(int const Side, double const Acceleration, std::string const &Why) { - if (Global.derailment != global_settings::derailment_model::physical || false == SetFlag(DamageFlag, dtrain_out)) + if (Global.derailment != global_settings::derailment_model::physical || false == DamageFlag.Set(VehicleDamage::derailed)) { return; } diff --git a/physics/components/commands.cpp b/physics/components/commands.cpp index 74f1a37bc..57d33dc8e 100644 --- a/physics/components/commands.cpp +++ b/physics/components/commands.cpp @@ -710,6 +710,26 @@ void TMoverParameters::DoorRemoteCommand(bool const Open, double const CValue1, } } +namespace +{ + +// the number of SetDamage on the set of the damage, as SetFlag() did on the number: a positive one adds its values, a +// negative one takes them away where all of them are there; returns whether it changed the set +bool setOrClear(DamageSet &damage, int const number) +{ + if (number > 0) + { + return damage.Set(DamageSet::FromBits(number)); + } + if (number < 0) + { + return damage.Clear(DamageSet::FromBits(-number)); + } + return false; +} + +} // namespace + // RunCommand() for the load and the damage; returns whether the command is one of them bool TMoverParameters::RunLoadCommand(VehicleCommand const Command, double CValue1, double CValue2, std::string_view const Argument, bool &OK) { @@ -753,13 +773,13 @@ bool TMoverParameters::RunLoadCommand(VehicleCommand const Command, double CValu else if (Command == VehicleCommand::setDamage) { if (CValue2 == 1) - OK = SetFlag(DamageFlag, static_cast(floor(CValue1))); + OK = setOrClear(DamageFlag, static_cast(floor(CValue1))); if (CValue2 == -1) { - auto const derailed{TestFlag(DamageFlag, dtrain_out)}; - OK = SetFlag(DamageFlag, static_cast(-floor(CValue1))); + auto const derailed{DamageFlag.HasAll(VehicleDamage::derailed)}; + OK = setOrClear(DamageFlag, static_cast(-floor(CValue1))); // taking the derailment off puts the vehicle back on the rails, with what the criteria of derailment held - if (derailed && false == TestFlag(DamageFlag, dtrain_out)) + if (derailed && false == DamageFlag.HasAll(VehicleDamage::derailed)) rerail(); } } diff --git a/physics/components/couplers.cpp b/physics/components/couplers.cpp index 5fb3b55ef..b6f750e86 100644 --- a/physics/components/couplers.cpp +++ b/physics/components/couplers.cpp @@ -126,7 +126,7 @@ int TMoverParameters::DettachStatus(int ConnectNo) return 0; // nie ma nic, to rozłączanie jest OK if (false == Couplers[ConnectNo].CouplingFlag.HasAny(Coupling::coupler)) return -Couplers[ConnectNo].CouplingFlag.Bits(); // hak nie połączony - rozłączanie jest OK - if (TestFlag(DamageFlag, dtrain_coupling)) + if (DamageFlag.HasAll(VehicleDamage::coupling)) return -Couplers[ConnectNo].CouplingFlag.Bits(); // hak urwany - rozłączanie jest OK // CouplerDist(ConnectNo); if (Couplers[ConnectNo].type() != TCouplerType::Screw || Neighbours[ConnectNo].distance < 0.01) diff --git a/physics/components/electrics.cpp b/physics/components/electrics.cpp index 536d24579..a14840bc3 100644 --- a/physics/components/electrics.cpp +++ b/physics/components/electrics.cpp @@ -1019,7 +1019,7 @@ bool TMoverParameters::MainSwitchCheck() const (HasCamshaft ? IsMainCtrlActualNoPowerPos() : LineBreakerClosesOnlyAtNoPowerPos ? IsMainCtrlNoPowerPos() : true) && - false == TestFlag(DamageFlag, dtrain_out) && false == TestFlag(EngDmgFlag, 1) && false == EmergencyBrakeHoldsMainOff(); + false == DamageFlag.HasAll(VehicleDamage::derailed) && false == TestFlag(EngDmgFlag, 1) && false == EmergencyBrakeHoldsMainOff(); } // ************************************************************************************************* diff --git a/physics/components/traction.cpp b/physics/components/traction.cpp index 00b760db6..ff3051c70 100644 --- a/physics/components/traction.cpp +++ b/physics/components/traction.cpp @@ -250,10 +250,10 @@ void TMoverParameters::MotorFailuresCheck(double const n) { if (FuzzyLogic(abs(n), nmax * 1.1, p_elengproblem) && MainSwitch(false)) EventFlag = true; /*zbyt duze obroty - wywalanie wskutek ognia okreznego*/ - if (TestFlag(DamageFlag, dtrain_engine) && FuzzyLogic(MotorCurrent, (double)ImaxLo / 10.0, p_elengproblem) && MainSwitch(false)) + if (DamageFlag.HasAll(VehicleDamage::engine) && FuzzyLogic(MotorCurrent, (double)ImaxLo / 10.0, p_elengproblem) && MainSwitch(false)) EventFlag = true; /*uszkodzony silnik (uplywy)*/ /* or FuzzyLogic(Abs(U/Mn),2*NominalVoltage,1)) then */ /*poprawic potem*/ - if ((FuzzyLogic(abs(Im), Imax * 2, p_elengproblem) || FuzzyLogic(abs(n), nmax * 1.11, p_elengproblem)) && SetFlag(DamageFlag, dtrain_engine)) + if ((FuzzyLogic(abs(Im), Imax * 2, p_elengproblem) || FuzzyLogic(abs(n), nmax * 1.11, p_elengproblem)) && DamageFlag.Set(VehicleDamage::engine)) EventFlag = true; /*! dorobic grzanie oporow rozruchowych i silnika*/ } @@ -1573,9 +1573,9 @@ double TMoverParameters::v2n(void) SlippingWheels = false; // wygaszenie poslizgu */ //poslizg jest w innym miejscu wygaszany też if (SlippingWheels) // nie ma zwiazku z predkoscia liniowa V { // McZapkie-221103: uszkodzenia kol podczas poslizgu - if (deltan > dmgn && FuzzyLogic(deltan, dmgn, p_slippdmg) && SetFlag(DamageFlag, dtrain_wheelwear)) // podkucie + if (deltan > dmgn && FuzzyLogic(deltan, dmgn, p_slippdmg) && DamageFlag.Set(VehicleDamage::wheelWear)) // podkucie EventFlag = true; - if (deltan < -dmgn && FuzzyLogic(-deltan, dmgn, p_slippdmg) && SetFlag(DamageFlag, dtrain_thinwheel)) // wycieranie sie obreczy + if (deltan < -dmgn && FuzzyLogic(-deltan, dmgn, p_slippdmg) && DamageFlag.Set(VehicleDamage::thinWheel)) // wycieranie sie obreczy EventFlag = true; n = nrot; // predkosc obrotowa nie zalezy od predkosci liniowej } @@ -1617,7 +1617,7 @@ double TMoverParameters::MomentumF(double I, double Iw, int SCP) const bool TMoverParameters::CutOffEngine(void) { bool COE = false; // Ra: wartość domyślna, sprawdzić to trzeba - if (NPoweredAxles > 0 && CabActive == 0 && EngineType == TEngineType::ElectricSeriesMotor && SetFlag(DamageFlag, -dtrain_engine)) + if (NPoweredAxles > 0 && CabActive == 0 && EngineType == TEngineType::ElectricSeriesMotor && DamageFlag.Clear(VehicleDamage::engine)) { NPoweredAxles = NPoweredAxles / 2; // bylo div czyli mod? COE = true; diff --git a/physics/mover.h b/physics/mover.h index b07ee559e..25db352e1 100644 --- a/physics/mover.h +++ b/physics/mover.h @@ -1485,7 +1485,7 @@ class TMoverParameters double LimPipePress = 0.0; /*stabilizator cisnienia*/ double ActFlowSpeed = 0.0; /*szybkosc stabilizatora*/ - int DamageFlag = 0; // kombinacja bitowa stalych dtrain_* } + DamageSet DamageFlag; // the damage of the vehicle int EngDmgFlag = 0; // kombinacja bitowa stalych usterek} // EndSignalsFlag: byte; {ABu 060205: zmiany - koncowki: 1/16 - swiatla prz/tyl, 2/31 - blachy prz/tyl} @@ -1787,7 +1787,7 @@ class TMoverParameters void derail(derailment::verdict const &Verdict, derailment::vehicle_state const &Vehicle, derailment::track_state const &Track, std::string const *TrackName = nullptr); // height of the centre of gravity above the rail with the load [m] double centre_of_gravity() const; - // the vehicle is back on the rails (resetconsist, SetDamage taking dtrain_out off): not derailed any more, and the + // the vehicle is back on the rails (resetconsist, SetDamage taking VehicleDamage::derailed off): not derailed any more, and the // criteria of derailment start from scratch, so a later derailment goes the way its model has it void rerail(); // what resetconsist does to a vehicle: no damage of any kind, the alarm chain released, back on the rails diff --git a/physics/vehicletypes.h b/physics/vehicletypes.h index 4cd26d21b..d61ad32b1 100644 --- a/physics/vehicletypes.h +++ b/physics/vehicletypes.h @@ -15,6 +15,8 @@ http://mozilla.org/MPL/2.0/. #include #include +#include "utilities/enumset.h" + #include "physics/vehiclecommand.h" /// Global counter incremented when a string-to-numeric conversion fails during config parsing. @@ -65,31 +67,27 @@ static int const dtrack_nomove = 64; /// Track damage flag: rails missing entirely. static int const dtrack_norail = 128; -/*uszkodzenia taboru*/ -/// Vehicle damage flag: thin wheel (locomotives). -static int const dtrain_thinwheel = 1; /*dla lokomotyw*/ -/// Vehicle damage flag: shifted load (wagons). -static int const dtrain_loadshift = 1; /*dla wagonow*/ -/// Vehicle damage flag: wheel wear. -static int const dtrain_wheelwear = 2; -/// Vehicle damage flag: bearing failure. -static int const dtrain_bearing = 4; -/// Vehicle damage flag: coupling damage. -static int const dtrain_coupling = 8; -/// Vehicle damage flag: ventilator failure (electric locomotive). -static int const dtrain_ventilator = 16; /*dla lokomotywy el.*/ -/// Vehicle damage flag: load damage (wagons). -static int const dtrain_loaddamage = 16; /*dla wagonow*/ -/// Vehicle damage flag: engine failure (locomotives). -static int const dtrain_engine = 32; /*dla lokomotyw*/ -/// Vehicle damage flag: load destroyed (wagons). -static int const dtrain_loaddestroyed = 32; /*dla wagonow*/ -/// Vehicle damage flag: axle damage. -static int const dtrain_axle = 64; -/// Vehicle damage flag: derailment. -static int const dtrain_out = 128; /*wykolejenie*/ -/// Vehicle damage flag: broken pantograph. -static int const dtrain_pantograph = 256; /*polamanie pantografu*/ +/// The damage of a vehicle, DamageFlag. A bit means one thing for a locomotive and another for a wagon, +/// where two values share it. The numbers are the ones of the SetDamage command of the scenery. +enum class VehicleDamage : unsigned +{ + thinWheel = 0x1, // a locomotive: a wheel worn thin + loadShift = 0x1, // a wagon: the load shifted + wheelWear = 0x2, // the wheels worn + bearing = 0x4, // a bearing failed + coupling = 0x8, // the coupling broken + ventilator = 0x10, // an electric locomotive: a ventilator failed + loadDamage = 0x10, // a wagon: the load damaged + engine = 0x20, // a locomotive: the engine failed + loadDestroyed = 0x20, // a wagon: the load destroyed + axle = 0x40, // an axle broken + derailed = 0x80, // off the rails + pantograph = 0x100, // a pantograph broken +}; +template <> struct IsEnumSetKind : std::true_type +{ +}; +using DamageSet = EnumSet; /// Reason for a derailment event. enum DerailReason @@ -165,111 +163,12 @@ enum class Coupling : unsigned }; /// A set of the kinds of connection of a coupling: what a coupling joins, what a vehicle allows, what a -/// command needs to pass. Read from and written to the numbers of the files only through FromBits() and Bits(). -class CouplingSet +/// command needs to pass. +template <> struct IsEnumSetKind : std::true_type { -public: - constexpr CouplingSet() = default; - constexpr CouplingSet(Coupling const kind) : bits{static_cast(kind)} {} // NOSONAR: a kind is a set of one - // the set the number of a file gives (the sum of the numbers of the kinds) - static constexpr CouplingSet FromBits(int const number) - { - CouplingSet set; - set.bits = static_cast(number); - return set; - } - // every kind of coupling: a coupling of any kind has one of them - static constexpr CouplingSet All() - { - return FromBits(-1); - } - // any kind at all: as the old number was true when it wasn't zero - constexpr explicit operator bool() const - { - return bits != 0; - } - // the number of the set, for the files and the log - constexpr int Bits() const - { - return static_cast(bits); - } - constexpr bool Empty() const - { - return bits == 0; - } - // all the kinds of the other set are in this one - constexpr bool HasAll(CouplingSet const other) const - { - return (bits & other.bits) == other.bits; - } - // any of the kinds of the other set is in this one - constexpr bool HasAny(CouplingSet const other) const - { - return (bits & other.bits) != 0; - } - // adds the kinds; returns whether any of them was missing - constexpr bool Set(CouplingSet const other) - { - if (HasAll(other)) - { - return false; - } - bits |= other.bits; - return true; - } - // takes the kinds away, where all of them are there; returns whether it did - constexpr bool Clear(CouplingSet const other) - { - if (false == HasAll(other)) - { - return false; - } - bits &= ~other.bits; - return true; - } - // this set without the kinds of the other - constexpr CouplingSet Without(CouplingSet const other) const - { - return FromBits(static_cast(bits & ~other.bits)); - } - constexpr CouplingSet &operator|=(CouplingSet const other) - { - bits |= other.bits; - return *this; - } - constexpr CouplingSet &operator&=(CouplingSet const other) - { - bits &= other.bits; - return *this; - } - friend constexpr CouplingSet operator|(CouplingSet left, CouplingSet const right) - { - return left |= right; - } - friend constexpr CouplingSet operator&(CouplingSet left, CouplingSet const right) - { - return left &= right; - } - // the kinds in one of the sets and not in the other: what changed between them - constexpr CouplingSet &operator^=(CouplingSet const other) - { - bits ^= other.bits; - return *this; - } - friend constexpr CouplingSet operator^(CouplingSet left, CouplingSet const right) - { - return left ^= right; - } - friend constexpr bool operator==(CouplingSet const &left, CouplingSet const &right) = default; - -private: - unsigned bits{0}; }; +using CouplingSet = EnumSet; -constexpr CouplingSet operator|(Coupling const left, Coupling const right) -{ - return CouplingSet{left} | CouplingSet{right}; -} /// Range of effect for a control command (mutually exclusive). enum class range_t { diff --git a/scene/scene.cpp b/scene/scene.cpp index bdcb3efb4..3f67b4d31 100644 --- a/scene/scene.cpp +++ b/scene/scene.cpp @@ -131,7 +131,7 @@ basic_cell::update_traction( TDynamicObject *Vehicle, int const Pantographindex // i do tego jeszcze wejdzie pod ślizg if( fHorizontal <= 0.0 ) { // 0.635 dla AKP-1 AKP-4E - SetFlag( Vehicle->MoverParameters->DamageFlag, dtrain_pantograph ); + Vehicle->MoverParameters->DamageFlag.Set(VehicleDamage::pantograph); pantograph->PantWys = -1.0; // ujemna liczba oznacza połamanie pantograph->hvPowerWire = nullptr; // bo inaczej się zasila w nieskończoność z połamanego if( Vehicle->MoverParameters->EnginePowerSource.CollectorParameters.CollectorsNo > 0 ) { diff --git a/tests/unit/test_cab.cpp b/tests/unit/test_cab.cpp index 40a94d552..c0896e6e3 100644 --- a/tests/unit/test_cab.cpp +++ b/tests/unit/test_cab.cpp @@ -164,17 +164,17 @@ TEST_CASE("cab: damaged motors cut off in a car without a driver") { vehicle driven{definition::fiz("ew58_v1/ew58-sa"), 0.0, "test", 1}; driven->BatterySwitch(true); REQUIRE(driven->CabActivisation()); - driven->DamageFlag |= dtrain_engine; + driven->DamageFlag |= VehicleDamage::engine; CHECK_FALSE(driven->CutOffEngine()); vehicle car{definition::fiz("ew58_v1/ew58-sa")}; auto const axles{car->NPoweredAxles}; CHECK_FALSE(car->CutOffEngine()); - car->DamageFlag |= dtrain_engine; + car->DamageFlag |= VehicleDamage::engine; CHECK(car->CutOffEngine()); CHECK(car->NPoweredAxles == axles / 2); - CHECK_FALSE(TestFlag(car->DamageFlag, dtrain_engine)); + CHECK_FALSE(car->DamageFlag.HasAll(VehicleDamage::engine)); vehicle wagon{definition::fiz("3wl_v2/3wl")}; - wagon->DamageFlag |= dtrain_engine; + wagon->DamageFlag |= VehicleDamage::engine; CHECK_FALSE(wagon->CutOffEngine()); } diff --git a/tests/unit/test_collisions.cpp b/tests/unit/test_collisions.cpp index be5910011..efc144e69 100644 --- a/tests/unit/test_collisions.cpp +++ b/tests/unit/test_collisions.cpp @@ -112,7 +112,7 @@ cl::crush_curve const locomotive_curve{cl::default_crush(cl::vehicle_kind::locom // ---- trains of real vehicles bool derailed(TMoverParameters const &Vehicle) { - return TestFlag(Vehicle.DamageFlag, dtrain_out); + return Vehicle.DamageFlag.HasAll(VehicleDamage::derailed); } double crush_of(TMoverParameters const &Vehicle) { @@ -689,7 +689,7 @@ TEST_CASE("collisions: a repaired vehicle has its ends rebuilt and is back on th for (auto *vehicle : {&wagon, &locomotive}) { auto &mover{vehicle->mover()}; mover.repair_damage(); - CHECK(mover.DamageFlag == 0); + CHECK(mover.DamageFlag.Empty()); CHECK(mover.Derailed == derailment::outcome::none); CHECK(mover.CollisionDerail == NONE); CHECK(mover.pushed_off_monitor.distance() == 0.0); @@ -712,12 +712,12 @@ TEST_CASE("collisions: the repair of a coupler torn off by the crush rebuilds it coupler.crush = 0.3; coupler.broken = true; coupler.hit_force = 2.0e6; - SetFlag(wagon->DamageFlag, dtrain_coupling); + wagon->DamageFlag.Set(VehicleDamage::coupling); REQUIRE(wagon->repair_coupler(end::rear)); CHECK(coupler.crush == 0.0); CHECK(coupler.hit_force == 0.0); CHECK_FALSE(coupler.broken); - CHECK_FALSE(TestFlag(wagon->DamageFlag, dtrain_coupling)); + CHECK_FALSE(wagon->DamageFlag.HasAll(VehicleDamage::coupling)); } TEST_CASE("collisions: SetDamage taking the derailment off clears the collision's") { @@ -725,10 +725,10 @@ TEST_CASE("collisions: SetDamage taking the derailment off clears the collision' wagon->derail_in_collision(COLLISION_PUSHED_OFF, "test"); REQUIRE(wagon->CollisionDerail == COLLISION_PUSHED_OFF); REQUIRE(wagon->Derailed == derailment::outcome::off_rails); - wagon->RunCommand(VehicleCommand::setDamage, dtrain_out, -1); + wagon->RunCommand(VehicleCommand::setDamage, DamageSet{VehicleDamage::derailed}.Bits(), -1); CHECK(wagon->CollisionDerail == NONE); CHECK(wagon->Derailed == derailment::outcome::none); - CHECK_FALSE(TestFlag(wagon->DamageFlag, dtrain_out)); + CHECK_FALSE(wagon->DamageFlag.HasAll(VehicleDamage::derailed)); } // ---- the model of eu07.ini diff --git a/tests/unit/test_commands.cpp b/tests/unit/test_commands.cpp index bd06bb26b..ac9d72fee 100644 --- a/tests/unit/test_commands.cpp +++ b/tests/unit/test_commands.cpp @@ -64,7 +64,7 @@ std::vector state() { {"PantsValve", [](auto const &M) { return flag(M.PantsValve.is_enabled) + 2 * flag(M.PantsValve.is_disabled); }}, {"MaxCurrentHigh", [](auto const &M) { return flag(M.MotorOverloadRelayHighThreshold); }}, {"Imin", [](auto const &M) { return M.Imin; }}, - {"DamageFlag", [](auto const &M) { return M.DamageFlag; }}, + {"DamageFlag", [](auto const &M) { return M.DamageFlag.Bits(); }}, {"RadioStop", [](auto const &M) { return flag(M.RadioStopFlag); }}, {"BrakeDelayFlag", [](auto const &M) { return M.BrakeDelayFlag; }}, {"SandDose", [](auto const &M) { return flag(M.SandDose); }}, diff --git a/tests/unit/test_controls.cpp b/tests/unit/test_controls.cpp index 47b852f2d..133794ccd 100644 --- a/tests/unit/test_controls.cpp +++ b/tests/unit/test_controls.cpp @@ -514,7 +514,7 @@ TEST_CASE("controls: a locomotive running into a standing wagon") { // slow, the locomotive pushes the wagon off, the buffers only touching, and both roll to a stop; fast, // the wagon derails where it stands auto const derailed{speed > 30.0}; - CHECK(TestFlag(wagon.DamageFlag, dtrain_out) == derailed); + CHECK(wagon.DamageFlag.HasAll(VehicleDamage::derailed) == derailed); CHECK((std::abs(wagon.DistCounter) > 0.0001) != derailed); CHECK(stopped(scenario)); scenario.check(); diff --git a/tests/unit/test_coupler_physics.cpp b/tests/unit/test_coupler_physics.cpp index 31b46a165..bbb935365 100644 --- a/tests/unit/test_coupler_physics.cpp +++ b/tests/unit/test_coupler_physics.cpp @@ -279,7 +279,7 @@ vehicle loose_wagon(double Slack, double Velocity, std::string const &Name) { } bool coupler_damaged(vehicle &Vehicle) { - return TestFlag(Vehicle->DamageFlag, dtrain_coupling); + return Vehicle->DamageFlag.HasAll(VehicleDamage::coupling); } } // namespace @@ -448,7 +448,7 @@ TEST_CASE("coupler physics: automatic couplers still catch a wagon rolling into REQUIRE(pair.coupled()); pair.run(2.0); CHECK(moving.speed() == doctest::Approx(standing.speed()).epsilon(0.1)); - CHECK_FALSE(TestFlag(moving->DamageFlag, dtrain_out)); + CHECK_FALSE(moving->DamageFlag.HasAll(VehicleDamage::derailed)); } TEST_CASE("couplers: a broken coupling of vehicles facing each other uncouples the ends coupled") { @@ -479,7 +479,7 @@ TEST_CASE("couplers: a broken coupler is repaired alone, with the one of the veh REQUIRE(second->Couplers[end::front].broken); REQUIRE(first->Couplers[end::rear].broken); REQUIRE(third->Couplers[end::front].broken); - REQUIRE(TestFlag(second->DamageFlag, dtrain_coupling)); + REQUIRE(second->DamageFlag.HasAll(VehicleDamage::coupling)); // nothing to repair at the far end of the first wagon CHECK_FALSE(first->repair_coupler(end::front)); @@ -491,12 +491,12 @@ TEST_CASE("couplers: a broken coupler is repaired alone, with the one of the veh CHECK(second->Couplers[end::rear].broken); CHECK(third->Couplers[end::front].broken); // the second wagon keeps its damage, and its emergency braking, while a coupler of it is broken - CHECK(TestFlag(second->DamageFlag, dtrain_coupling)); + CHECK(second->DamageFlag.HasAll(VehicleDamage::coupling)); CHECK(second->AlarmChainFlag); CHECK(third->repair_coupler(end::front)); CHECK_FALSE(second->Couplers[end::rear].broken); - CHECK_FALSE(TestFlag(second->DamageFlag, dtrain_coupling)); + CHECK_FALSE(second->DamageFlag.HasAll(VehicleDamage::coupling)); CHECK_FALSE(second->AlarmChainFlag); // repaired, not coupled: the vehicles are coupled again as usual CHECK(second->Couplers[end::rear].CouplingFlag == Coupling::none); @@ -560,7 +560,7 @@ TEST_CASE("coupler physics: a hit on a braked train runs through it as a wave, w CAPTURE(i); CAPTURE(peak[i]); REQUIRE(started[i] >= 0.0); - CHECK_FALSE(TestFlag(train[i]->DamageFlag, dtrain_out)); + CHECK_FALSE(train[i]->DamageFlag.HasAll(VehicleDamage::derailed)); } // the wagon hit first takes less than the hit, the one at the far end the least CHECK(peak[wagons - 1] < 12.0); diff --git a/tests/unit/test_couplers.cpp b/tests/unit/test_couplers.cpp index 44b3ad663..825102f9a 100644 --- a/tests/unit/test_couplers.cpp +++ b/tests/unit/test_couplers.cpp @@ -24,7 +24,7 @@ vehicle heavy_automatic_wagon(double Velocity, std::string const &Name) { } bool derailed(vehicle &Vehicle) { - return TestFlag(Vehicle->DamageFlag, dtrain_out); + return Vehicle->DamageFlag.HasAll(VehicleDamage::derailed); } // runs the pair for Seconds, returns whether the vehicles were coupled at any point diff --git a/tests/unit/test_couplers_trains.cpp b/tests/unit/test_couplers_trains.cpp index cf4dc58b8..ccd0ffae4 100644 --- a/tests/unit/test_couplers_trains.cpp +++ b/tests/unit/test_couplers_trains.cpp @@ -90,7 +90,7 @@ TEST_CASE("couplers: a screw coupler uncoupled with the buffers apart, or with t auto &wagon{train.add(definition::fiz("3wl_v2/3wl"), 0, Coupling::none, 0.0, gap).mover()}; REQUIRE(locomotive.Attach(end::rear, end::front, &wagon, couplingtype, true)); if (damaged) { - SetFlag(locomotive.DamageFlag, dtrain_coupling); + locomotive.DamageFlag.Set(VehicleDamage::coupling); } train.step(); CHECK((locomotive.DettachStatus(end::rear) < 0) == apart); @@ -145,20 +145,20 @@ TEST_CASE("couplers: a coupler broken, with its brake hose torn off") { auto &wagon{train.add(definition::fiz("3wl_v2/3wl")).mover()}; REQUIRE(locomotive.Couplers[end::rear].Connected == &wagon); locomotive.damage_coupler(end::rear); - CHECK(TestFlag(locomotive.DamageFlag, dtrain_coupling)); + CHECK(locomotive.DamageFlag.HasAll(VehicleDamage::coupling)); CHECK(locomotive.AlarmChainFlag); CHECK(locomotive.Couplers[end::rear].CouplingFlag == Coupling::none); CHECK(wagon.Couplers[end::front].CouplingFlag == Coupling::none); // the front coupler of the wagon, broken on its own: no hose to tear wagon.damage_coupler(end::front); - CHECK(TestFlag(wagon.DamageFlag, dtrain_coupling)); + CHECK(wagon.DamageFlag.HasAll(VehicleDamage::coupling)); CHECK_FALSE(wagon.AlarmChainFlag); // articulated couplers don't break consist articulated; auto &unit{articulated.add(definition::fiz("impuls_v1/31we-a_pr")).mover()}; articulated.add(definition::fiz("impuls_v1/31we-b_pr")); unit.damage_coupler(end::rear); - CHECK_FALSE(TestFlag(unit.DamageFlag, dtrain_coupling)); + CHECK_FALSE(unit.DamageFlag.HasAll(VehicleDamage::coupling)); CHECK(unit.Couplers[end::rear].CouplingFlag != Coupling::none); } @@ -186,8 +186,8 @@ TEST_CASE("couplers: collisions fast enough to derail") { scenario.add(frontpath, 0, consist::full_coupling, frontspeed); scenario.add(back, 0, Coupling::none, backspeed, 20.0); scenario.wait(10.0); - CHECK(TestFlag(scenario.vehicle(0).DamageFlag, dtrain_out) == frontout); - CHECK(TestFlag(scenario.vehicle(1).DamageFlag, dtrain_out) == backout); + CHECK(scenario.vehicle(0).DamageFlag.HasAll(VehicleDamage::derailed) == frontout); + CHECK(scenario.vehicle(1).DamageFlag.HasAll(VehicleDamage::derailed) == backout); scenario.check(); } } diff --git a/tests/unit/test_derail_motion.cpp b/tests/unit/test_derail_motion.cpp index d4a44edda..2256ea455 100644 --- a/tests/unit/test_derail_motion.cpp +++ b/tests/unit/test_derail_motion.cpp @@ -441,7 +441,7 @@ TEST_CASE("derail motion: the vehicle leaves the track with physics.derailment n vehicle eu07vehicle{definition::fiz("4e_v1/4e"), 40.0}; eu07vehicle.damage_track(dtrack_norail); eu07vehicle.step(); - REQUIRE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + REQUIRE(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(eu07vehicle->Derailed == dr::outcome::off_rails); CHECK(eu07vehicle->DerailCause == dm::cause::end_of_track); CHECK(eu07vehicle->leaves_track()); @@ -450,7 +450,7 @@ TEST_CASE("derail motion: the vehicle leaves the track with physics.derailment n model_setting const physical{global_settings::derailment_model::physical}; vehicle pulled{definition::fiz("4e_v1/4e"), 40.0}; pulled->pull_off(-1, 5.0, "test"); - CHECK(TestFlag(pulled->DamageFlag, dtrain_out)); + CHECK(pulled->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(pulled->Derailed == dr::outcome::off_rails); CHECK(pulled->DerailCause == dm::cause::pulled_off); CHECK(pulled->DerailSide == -1); @@ -473,7 +473,7 @@ TEST_CASE("derail motion: with physics.derailment monitor and old the vehicle st for (int step = 0; step < 600; ++step) { fast.step(curve); } - REQUIRE(TestFlag(fast->DamageFlag, dtrain_out)); + REQUIRE(fast->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(fast->Derailed == dr::outcome::none); CHECK_FALSE(fast->leaves_track()); CHECK_FALSE(fast->OffTrack.active()); @@ -482,13 +482,13 @@ TEST_CASE("derail motion: with physics.derailment monitor and old the vehicle st vehicle ending{definition::fiz("4e_v1/4e"), 40.0}; ending.damage_track(dtrack_norail); ending.step(); - REQUIRE(TestFlag(ending->DamageFlag, dtrain_out)); + REQUIRE(ending->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(ending->Derailed == dr::outcome::none); CHECK_FALSE(ending->leaves_track()); // nor is a vehicle pulled off it vehicle pulled{definition::fiz("4e_v1/4e"), 40.0}; pulled->pull_off(1, 5.0, "test"); - CHECK_FALSE(TestFlag(pulled->DamageFlag, dtrain_out)); + CHECK_FALSE(pulled->DamageFlag.HasAll(VehicleDamage::derailed)); } } diff --git a/tests/unit/test_derailment_vehicle.cpp b/tests/unit/test_derailment_vehicle.cpp index 8fee07d9a..1c96b4f52 100644 --- a/tests/unit/test_derailment_vehicle.cpp +++ b/tests/unit/test_derailment_vehicle.cpp @@ -47,7 +47,7 @@ TEST_CASE("derailment: a vehicle too fast in a curve") { for (int step = 0; step < 300; ++step) { eu07vehicle.step(curve); } - CHECK_FALSE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + CHECK_FALSE(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(eu07vehicle->Derailed == dr::outcome::none); } SUBCASE("over it, it falls over and slides to a stop on its side") { @@ -57,7 +57,7 @@ TEST_CASE("derailment: a vehicle too fast in a curve") { for (int step = 0; step < 10; ++step) { eu07vehicle.step(curve); } - REQUIRE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + REQUIRE(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(eu07vehicle->Derailed == dr::outcome::overturned); CHECK_FALSE(eu07vehicle->Mains); // not stopped dead as the legacy model has it: it slows down at half of g on the 80 of its 96 t @@ -116,7 +116,7 @@ TEST_CASE("derailment: the model of eu07.ini") { for (int step = 0; step < 300; ++step) { eu07vehicle.step(curve); } - CHECK_FALSE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + CHECK_FALSE(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); } SUBCASE("old: the legacy draw stops the vehicle dead") { model_setting const old{global_settings::derailment_model::old}; @@ -125,7 +125,7 @@ TEST_CASE("derailment: the model of eu07.ini") { eu07vehicle.step(curve); } // far over its threshold the draw comes out at once - CHECK(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + CHECK(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(eu07vehicle->Derailed == dr::outcome::none); CHECK(eu07vehicle->V == doctest::Approx(0.0).epsilon(0.01)); } @@ -180,7 +180,7 @@ TEST_CASE("derailment: a repaired vehicle is back on the rails") { REQUIRE(repaired->Derailed == dr::outcome::overturned); REQUIRE(repaired->V == 0.0); repaired->repair_damage(); - CHECK(repaired->DamageFlag == 0); + CHECK(repaired->DamageFlag.Empty()); CHECK(repaired->Derailed == dr::outcome::none); CHECK(repaired->derailment_distance == 0.0); CHECK(repaired->OffsetTrackH == 0.0); @@ -235,11 +235,11 @@ TEST_CASE("derailment: SetDamage taking the derailment off puts the vehicle back } REQUIRE(eu07vehicle->Derailed == dr::outcome::overturned); // another flag taken off leaves it derailed - eu07vehicle->RunCommand(VehicleCommand::setDamage, dtrain_engine, 1); - eu07vehicle->RunCommand(VehicleCommand::setDamage, dtrain_engine, -1); + eu07vehicle->RunCommand(VehicleCommand::setDamage, DamageSet{VehicleDamage::engine}.Bits(), 1); + eu07vehicle->RunCommand(VehicleCommand::setDamage, DamageSet{VehicleDamage::engine}.Bits(), -1); CHECK(eu07vehicle->Derailed == dr::outcome::overturned); - CHECK(eu07vehicle->RunCommand(VehicleCommand::setDamage, dtrain_out, -1)); - CHECK_FALSE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + CHECK(eu07vehicle->RunCommand(VehicleCommand::setDamage, DamageSet{VehicleDamage::derailed}.Bits(), -1)); + CHECK_FALSE(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(eu07vehicle->Derailed == dr::outcome::none); } @@ -274,7 +274,7 @@ TEST_CASE("derailment: monitor, the legacy draw decides and the log tells what t for (int step = 0; step < 100; ++step) { eu07vehicle.step(curve); } - CHECK_FALSE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + CHECK_FALSE(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(eu07vehicle->Derailed == dr::outcome::none); // once on a track, however long it stays over the criteria CHECK(eu07vehicle->derailment_reports == 1); @@ -287,7 +287,7 @@ TEST_CASE("derailment: monitor, the legacy draw decides and the log tells what t eu07vehicle.step(next); } CHECK(eu07vehicle->derailment_reports == 2); - CHECK_FALSE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + CHECK_FALSE(eu07vehicle->DamageFlag.HasAll(VehicleDamage::derailed)); SUBCASE("under the criteria nothing is told") { vehicle slower{definition::fiz("4e_v1/4e"), critical - 5.0}; @@ -301,7 +301,7 @@ TEST_CASE("derailment: monitor, the legacy draw decides and the log tells what t for (int step = 0; step < 600; ++step) { faster.step(curve); } - CHECK(TestFlag(faster->DamageFlag, dtrain_out)); + CHECK(faster->DamageFlag.HasAll(VehicleDamage::derailed)); CHECK(faster->Derailed == dr::outcome::none); CHECK(faster->V == doctest::Approx(0.0).epsilon(0.01)); } diff --git a/tests/unit/test_queries.cpp b/tests/unit/test_queries.cpp index 797e40534..677b993eb 100644 --- a/tests/unit/test_queries.cpp +++ b/tests/unit/test_queries.cpp @@ -16,23 +16,23 @@ TEST_CASE("queries: the damage of a vehicle as the driver's panel describes it") REQUIRE(wagon->Power < 0.1); struct damage { - int flag; + DamageSet flag; char const *locomotive; char const *wagon; }; for (auto const &[flag, locomotivetext, wagontext] : { - damage{0, "OK", "OK"}, - damage{dtrain_thinwheel, "Thin wheel", "Load shifted"}, - damage{dtrain_wheelwear, "Wheel wear", "Wheel wear"}, - damage{dtrain_bearing, "Bearing damaged", "Bearing damaged"}, - damage{dtrain_coupling, "Coupler broken", "Coupler broken"}, - damage{dtrain_loaddamage, "Ventilator damaged", "Load damaged"}, - damage{dtrain_loaddestroyed, "Engine damaged", "LOAD DESTROYED"}, - damage{dtrain_axle, "Axle broken", "Axle broken"}, - damage{dtrain_out, "DERAILED", "DERAILED"}, + damage{{}, "OK", "OK"}, + damage{VehicleDamage::thinWheel, "Thin wheel", "Load shifted"}, + damage{VehicleDamage::wheelWear, "Wheel wear", "Wheel wear"}, + damage{VehicleDamage::bearing, "Bearing damaged", "Bearing damaged"}, + damage{VehicleDamage::coupling, "Coupler broken", "Coupler broken"}, + damage{VehicleDamage::loadDamage, "Ventilator damaged", "Load damaged"}, + damage{VehicleDamage::loadDestroyed, "Engine damaged", "LOAD DESTROYED"}, + damage{VehicleDamage::axle, "Axle broken", "Axle broken"}, + damage{VehicleDamage::derailed, "DERAILED", "DERAILED"}, // the last flag of the list wins - damage{dtrain_bearing | dtrain_axle, "Axle broken", "Axle broken"}, - damage{255, "WRECKED", "WRECKED"}, + damage{VehicleDamage::bearing | VehicleDamage::axle, "Axle broken", "Axle broken"}, + damage{DamageSet::FromBits(255), "WRECKED", "WRECKED"}, }) { CAPTURE(flag); locomotive->DamageFlag = flag; @@ -41,7 +41,7 @@ TEST_CASE("queries: the damage of a vehicle as the driver's panel describes it") CHECK(wagon->EngineDescription(0) == wagontext); } // flat wheels count as worn ones - wagon->DamageFlag = 0; + wagon->DamageFlag = {}; wagon->WheelFlat = 6.0; CHECK(wagon->EngineDescription(0) == "Wheel wear"); // nothing else is described diff --git a/utilities/enumset.h b/utilities/enumset.h new file mode 100644 index 000000000..ae5dea20e --- /dev/null +++ b/utilities/enumset.h @@ -0,0 +1,130 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +// a set of the values of an enum class whose values are bits: what a coupling joins, the damage of a vehicle. the +// numbers of the files (their sums of the bits) come in only through FromBits() and go out through Bits() + +#pragma once + +#include + +// an enum is a kind of the sets by a specialisation of this, which also gives its operator | +template struct IsEnumSetKind : std::false_type +{ +}; + +template class EnumSet +{ + static_assert(std::is_enum_v, "a set of the values of an enum"); + +public: + constexpr EnumSet() = default; + constexpr EnumSet(Enum const value) : bits{static_cast(value)} {} // NOSONAR: a value is a set of one + // the set the number of a file gives (the sum of the numbers of the values) + static constexpr EnumSet FromBits(int const number) + { + EnumSet set; + set.bits = static_cast(number); + return set; + } + // every value: a set of any of them has one of these + static constexpr EnumSet All() + { + return FromBits(-1); + } + // the number of the set, for the files and the log + constexpr int Bits() const + { + return static_cast(bits); + } + constexpr bool Empty() const + { + return bits == 0; + } + // any value at all: as the old number was true when it wasn't zero + constexpr explicit operator bool() const + { + return bits != 0; + } + // all the values of the other set are in this one + constexpr bool HasAll(EnumSet const other) const + { + return (bits & other.bits) == other.bits; + } + // any of the values of the other set is in this one + constexpr bool HasAny(EnumSet const other) const + { + return (bits & other.bits) != 0; + } + // adds the values; returns whether any of them was missing (as SetFlag()) + constexpr bool Set(EnumSet const other) + { + if (HasAll(other)) + { + return false; + } + bits |= other.bits; + return true; + } + // takes the values away, where all of them are there; returns whether it did (as ClearFlag()) + constexpr bool Clear(EnumSet const other) + { + if (false == HasAll(other)) + { + return false; + } + bits &= ~other.bits; + return true; + } + // this set without the values of the other + constexpr EnumSet Without(EnumSet const other) const + { + return FromBits(static_cast(bits & ~other.bits)); + } + constexpr EnumSet &operator|=(EnumSet const other) + { + bits |= other.bits; + return *this; + } + constexpr EnumSet &operator&=(EnumSet const other) + { + bits &= other.bits; + return *this; + } + // the values in one of the sets and not in the other: what changed between them + constexpr EnumSet &operator^=(EnumSet const other) + { + bits ^= other.bits; + return *this; + } + friend constexpr EnumSet operator|(EnumSet left, EnumSet const right) + { + return left |= right; + } + friend constexpr EnumSet operator&(EnumSet left, EnumSet const right) + { + return left &= right; + } + friend constexpr EnumSet operator^(EnumSet left, EnumSet const right) + { + return left ^= right; + } + friend constexpr bool operator==(EnumSet const &left, EnumSet const &right) = default; + +private: + unsigned bits{0}; +}; + +// two values make a set of them, for the enums which are kinds of the sets +template + requires IsEnumSetKind::value +constexpr EnumSet operator|(Enum const left, Enum const right) +{ + return EnumSet{left} | EnumSet{right}; +} diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 951e614fb..a89fe5146 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -3279,7 +3279,7 @@ void TDynamicObject::pull_neighbours( double const Distance ) { auto const &coupler { MoverParameters->Couplers[ end ] }; auto *neighbour { MoverParameters->Neighbours[ end ].vehicle }; if( coupler.CouplingFlag == Coupling::none || neighbour == nullptr || neighbour->MoverParameters->OffTrack.active() - || TestFlag( neighbour->MoverParameters->DamageFlag, dtrain_out ) ) { + || neighbour->MoverParameters->DamageFlag.HasAll(VehicleDamage::derailed) ) { m_pulledoff[ end ].reset(); continue; } @@ -5807,7 +5807,7 @@ void TDynamicObject::RenderSounds() { // special events if( MoverParameters->EventFlag ) { // McZapkie: w razie wykolejenia - if( true == TestFlag( MoverParameters->DamageFlag, dtrain_out ) ) { + if( true == MoverParameters->DamageFlag.HasAll(VehicleDamage::derailed) ) { rsDerailment.play( sound_flags::exclusive ); } @@ -9950,7 +9950,7 @@ vehicle_table::update_off_track_contacts( double const Deltatime ) { // one on the rails: along them into its speed, across them on its wheels, as a coupling pulls them off auto &othermover { *other->MoverParameters }; othermover.V += glm::dot( impulse, otherbody.guide ) / std::max( othermover.TotalMass, 1.0 ); - if( TestFlag( othermover.DamageFlag, dtrain_out ) ) { + if( othermover.DamageFlag.HasAll(VehicleDamage::derailed) ) { continue; } glm::dvec3 const otherleft { otherbody.guide.z, 0.0, -otherbody.guide.x }; diff --git a/world/station.cpp b/world/station.cpp index 5e4dd9590..8ac77db30 100644 --- a/world/station.cpp +++ b/world/station.cpp @@ -58,7 +58,7 @@ basic_station::update_load( TDynamicObject *First, Mtable::TTrainParameters &Sch // NOTE: for the time being we're doing simple, random load change calculation // TODO: exchange driven by station parameters and time of the day auto unloadcount = static_cast( - TestFlag( parameters.DamageFlag, dtrain_out ) ? parameters.LoadAmount : + parameters.DamageFlag.HasAll(VehicleDamage::derailed) ? parameters.LoadAmount : laststop ? parameters.LoadAmount : firststop ? 0 : maintenancestop ? 0 : @@ -66,7 +66,7 @@ basic_station::update_load( TDynamicObject *First, Mtable::TTrainParameters &Sch parameters.LoadAmount, static_cast(Random( parameters.MaxLoad * 0.15f * stationsizemodifier )) ) ); auto loadcount = static_cast( - TestFlag( parameters.DamageFlag, dtrain_out ) ? 0 : + parameters.DamageFlag.HasAll(VehicleDamage::derailed) ? 0 : laststop ? 0 : maintenancestop ? 0 : Random( parameters.MaxLoad * 0.15f * stationsizemodifier ) );