From 0e41d27032df27e06c56ba3f4de8fe6ec2787a2e Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Mon, 16 Nov 2026 07:44:31 +0100 Subject: [PATCH] fix(physics): a repaired vehicle is back on the rails, the state of the criteria of derailment cleared resetconsist (Stop and repair consist of the debug panel and of the vehicle window) cleared the damage flags, but kept Derailed, the stretches of the criteria, the distance gathered for them and the report of the monitor. A vehicle once derailed by the criteria and repaired then slid on with their friction after a derailment of the legacy kind, instead of stopping dead, and the friction of the ballast kept holding it whenever it stood. TMoverParameters::rerail() puts a vehicle back on the rails: dtrain_out off, Derailed none, the criteria from scratch, the monitor free to report the track again, a rail vehicle back on the axis of the track. repair_damage() does what resetconsist did to the damage (the flags, the engine damage, the wheel flats, the alarm chain) and rerails it; resetconsist and the road vehicles coming back from the pool of a road point use it. SetDamage taking dtrain_out off rerails the vehicle as well. A vehicle put on a track anew (consistteleport) starts the stretches of the criteria over, they were gathered on the track it left. --- McZapkie/MOVER.h | 5 ++ McZapkie/Mover.cpp | 31 ++++++++ simulation/simulation.cpp | 6 +- tests/unit/test_derailment_vehicle.cpp | 97 ++++++++++++++++++++++++++ vehicle/DynObj.cpp | 3 + world/RoadPoint.cpp | 3 +- 6 files changed, 139 insertions(+), 6 deletions(-) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 1d2280bcc..a6c36168b 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2448,6 +2448,11 @@ 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 + // 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 + void repair_damage(); // how the vehicle left the rails by the criteria of derailment.h: upright, or on its side. none for any other // derailment, which stops it as the legacy model does derailment::outcome Derailed{derailment::outcome::none}; diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 2dfb8d484..ab42af462 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -1584,6 +1584,31 @@ void TMoverParameters::derail(derailment::verdict const &Verdict, derailment::ve ErrorLog("Bad driving: " + Name + " " + derailment::report(Verdict, Vehicle, Track, V, TrackName != nullptr ? *TrackName : std::string{})); } +void TMoverParameters::rerail() +{ + ClearFlag(DamageFlag, dtrain_out); + Derailed = derailment::outcome::none; + derailment_monitor.reset(); + derailment_distance = 0.0; + derailment_reported.reset(); + // back on the axis of the track, which the derailment shifted the vehicle off aside and down (a road vehicle keeps + // its place on the road) + if (TestFlag(CategoryFlag, 1)) + { + OffsetTrackH = 0.0; + OffsetTrackV = 0.0; + } +} + +void TMoverParameters::repair_damage() +{ + DamageFlag = 0; + EngDmgFlag = 0; + WheelFlat = 0.0; + AlarmChainFlag = false; + rerail(); +} + void TMoverParameters::Derail(DerailReason const Reason) { if (SetFlag(DamageFlag, dtrain_out)) @@ -13488,7 +13513,13 @@ bool TMoverParameters::RunCommand(std::string const &Command, double CValue1, do if (CValue2 == 1) OK = SetFlag(DamageFlag, static_cast(floor(CValue1))); if (CValue2 == -1) + { + auto const derailed{TestFlag(DamageFlag, dtrain_out)}; OK = SetFlag(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)) + rerail(); + } } else if (Command == "Emergency_brake") { diff --git a/simulation/simulation.cpp b/simulation/simulation.cpp index 87cce81b6..b47bc752b 100644 --- a/simulation/simulation.cpp +++ b/simulation/simulation.cpp @@ -398,12 +398,10 @@ void state_manager::process_commands() { } while (vehicle) { - vehicle->MoverParameters->DamageFlag = 0; - vehicle->MoverParameters->EngDmgFlag = 0; + // no damage, back on the rails with the state of the derailment cleared + vehicle->MoverParameters->repair_damage(); vehicle->MoverParameters->V = 0.000001; // HACK: force vehicle position re-calculation vehicle->MoverParameters->DistCounter = 0.0; - vehicle->MoverParameters->WheelFlat = 0.0; - vehicle->MoverParameters->AlarmChainFlag = false; vehicle->MoverParameters->OffsetTrackH = 0.0; vehicle->MoverParameters->OffsetTrackV = 0.0; diff --git a/tests/unit/test_derailment_vehicle.cpp b/tests/unit/test_derailment_vehicle.cpp index 6e8e4661a..d6c94b9b0 100644 --- a/tests/unit/test_derailment_vehicle.cpp +++ b/tests/unit/test_derailment_vehicle.cpp @@ -165,6 +165,103 @@ TEST_CASE("derailment: a stopped derailed vehicle stays put on a gradient") { CHECK(still); } +TEST_CASE("derailment: a repaired vehicle is back on the rails") { + // resetconsist (the debug panel's "Stop and repair consist") clears the damage flags; the state of the criteria + // has to go with them, or a vehicle once derailed by the criteria slides on with their friction after a later + // derailment of any other kind, and the friction of the ballast keeps holding it once it stops + model_setting const physical{global_settings::derailment_model::physical}; + test::track curve; + curve.radius = 300.0; + curve.gradient = 0.02; + vehicle repaired{definition::fiz("4e_v1/4e"), 150.0}; + for (int step = 0; step < 2000; ++step) { + repaired.step(curve); + } + REQUIRE(repaired->Derailed == dr::outcome::overturned); + REQUIRE(repaired->V == 0.0); + repaired->repair_damage(); + CHECK(repaired->DamageFlag == 0); + CHECK(repaired->Derailed == dr::outcome::none); + CHECK(repaired->derailment_distance == 0.0); + CHECK(repaired->OffsetTrackH == 0.0); + CHECK(repaired->OffsetTrackV == 0.0); + + SUBCASE("a derailment of the legacy kind stops it dead, as one never derailed") { + test::track straight; + vehicle fresh{definition::fiz("4e_v1/4e"), 60.0}; + repaired->V = fresh->V; + repaired->AccS = 0.0; + repaired->Derail(TOO_HIGH_SPEED); + fresh->Derail(TOO_HIGH_SPEED); + REQUIRE(repaired->Derailed == dr::outcome::none); + auto const start{fresh->V}; + REQUIRE(start > 10.0); + for (int step = 0; step < 150; ++step) { + repaired.step(straight); + fresh.step(straight); + } + // the legacy model takes g off a derailed vehicle and holds it with a friction 1e20 times its own: 1.1 s from + // 39 km/h. with the friction of the overturned one (half of g on 80 of 96 t) it would still go at 23 km/h + CHECK(fresh->V == doctest::Approx(0.0).epsilon(0.01)); + CHECK(repaired->V == doctest::Approx(fresh->V).epsilon(0.01)); + } + SUBCASE("the friction of the ballast doesn't hold it any more") { + // unbraked on the gradient of 20 per mille it rolls away, at g / 50 less the running resistance + test::track straight; + straight.gradient = 0.02; + for (int step = 0; step < 300; ++step) { + repaired.step(straight); + } + CHECK(std::abs(repaired->V) > 0.4); + CHECK(std::abs(repaired->V) < 3.0 * 0.02 * dr::gravity); + } + SUBCASE("too fast in a curve again it derails again, by the criteria") { + repaired->V = 150.0 * kmh; + curve.gradient = 0.0; + for (int step = 0; step < 20; ++step) { + repaired.step(curve); + } + CHECK(repaired->Derailed == dr::outcome::overturned); + } +} + +TEST_CASE("derailment: SetDamage taking the derailment off puts the vehicle back on the rails") { + model_setting const physical{global_settings::derailment_model::physical}; + test::track curve; + curve.radius = 300.0; + vehicle eu07vehicle{definition::fiz("4e_v1/4e"), 150.0}; + for (int step = 0; step < 20; ++step) { + eu07vehicle.step(curve); + } + REQUIRE(eu07vehicle->Derailed == dr::outcome::overturned); + // another flag taken off leaves it derailed + eu07vehicle->RunCommand("SetDamage", dtrain_engine, 1); + eu07vehicle->RunCommand("SetDamage", dtrain_engine, -1); + CHECK(eu07vehicle->Derailed == dr::outcome::overturned); + CHECK(eu07vehicle->RunCommand("SetDamage", dtrain_out, -1)); + CHECK_FALSE(TestFlag(eu07vehicle->DamageFlag, dtrain_out)); + CHECK(eu07vehicle->Derailed == dr::outcome::none); +} + +TEST_CASE("derailment: monitor tells again on the same track after a repair") { + model_setting const monitor{global_settings::derailment_model::monitor}; + test::track curve; + curve.radius = 300.0; + curve.name = "curve_1"; + auto const critical{dr::overturning_speed(300.0, 0.0, eu07, 1.435) / kmh}; + vehicle eu07vehicle{definition::fiz("4e_v1/4e"), critical + 2.0}; + for (int step = 0; step < 100; ++step) { + eu07vehicle.step(curve); + } + REQUIRE(eu07vehicle->derailment_reports == 1); + eu07vehicle->repair_damage(); + CHECK_FALSE(eu07vehicle->derailment_reported.has_value()); + for (int step = 0; step < 100; ++step) { + eu07vehicle.step(curve); + } + CHECK(eu07vehicle->derailment_reports == 2); +} + TEST_CASE("derailment: monitor, the legacy draw decides and the log tells what the criteria would do") { model_setting const monitor{global_settings::derailment_model::monitor}; test::track curve; diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 39ab5c4b6..7f266d099 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -1986,6 +1986,9 @@ void TDynamicObject::place_on_track(TTrack *Track, double fDist, bool Reversed) Axle1.Move((iDirection ? fDist : -fDist) - fAxleDistHalf, false); // false, żeby nie generować eventów break; } + // the stretches the criteria of derailment held over were on the track the vehicle left (consistteleport) + MoverParameters->derailment_monitor.reset(); + MoverParameters->derailment_distance = 0.0; // potrzebne do wyliczenia aktualnej pozycji; nie może być zero, bo nie przeliczy pozycji // teraz jeszcze trzeba przypisać pojazdy do nowego toru, bo przesuwanie początkowe osi nie // zrobiło tego diff --git a/world/RoadPoint.cpp b/world/RoadPoint.cpp index 96eb4814f..ecf39f2ba 100644 --- a/world/RoadPoint.cpp +++ b/world/RoadPoint.cpp @@ -994,8 +994,7 @@ void roadpoint_node::update_spawn(double const Deltatime) auto *vehicle{entry.vehicle}; // it comes back the way it was made: whole, at speed, with the driver told to drive on vehicle->bEnabled = true; - vehicle->MoverParameters->DamageFlag = 0; - vehicle->MoverParameters->EngDmgFlag = 0; + vehicle->MoverParameters->repair_damage(); vehicle->MoverParameters->V = entry.velocity; vehicle->place_on_track(m_lanes.front(), m_offset + 0.5 * vehicle->MoverParameters->Dim.L, false); if (vehicle->Mechanik != nullptr)