From aba868767842d612370512c56b9846a426b36d84 Mon Sep 17 00:00:00 2001 From: Hirek193 Date: Wed, 11 Nov 2026 02:52:00 +0100 Subject: [PATCH] feat(ui): repair the broken coupler next to the camera from the driving aid --- McZapkie/MOVER.h | 5 ++++ McZapkie/Mover.cpp | 44 ++++++++++++++++++++++++++++- application/driveruipanels.cpp | 30 ++++++++++++++++++-- application/driveruipanels.h | 3 ++ input/command.cpp | 2 ++ input/command.h | 1 + simulation/simulation.cpp | 6 ++++ tests/unit/test_coupler_physics.cpp | 37 ++++++++++++++++++++++++ 8 files changed, 125 insertions(+), 3 deletions(-) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 4158825c2..0e11916f0 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -1256,6 +1256,7 @@ struct TCoupling double Dist = 0.0; /*strzalka ugiecia zderzaków*/ bool CheckCollision = false; /*czy sprawdzac sile czy pedy*/ float stretch_duration{0.f}; // seconds, elapsed time with excessive force applied to the coupler + bool broken{false}; // the coupler broke (damage_coupler()), until it's repaired (repair_coupler()) // optional adapter piece double adapter_length{0.0}; // meters, value added on the given end to standard vehicle (half)length double adapter_height{0.0}; // meters, distance from rail level @@ -2389,6 +2390,10 @@ class TMoverParameters int DettachStatus(int ConnectNo); bool Dettach(int ConnectNo); void damage_coupler(int const End); + // repairs the broken coupler of the end, and the one of the vehicle it was coupled with, so they can be coupled + // again; the vehicles clear their coupler damage once neither of their couplers is broken. returns: whether there + // was a broken coupler to repair + bool repair_coupler(int const End); void Derail(DerailReason Reason); bool DirectionForward(); bool DirectionBackward(void); /*! kierunek ruchu*/ diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 4ed1f0048..b835d63ef 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -1441,12 +1441,16 @@ void TMoverParameters::damage_coupler(int const End) } thiscoupler.CouplingFlag = 0; + thiscoupler.broken = true; if (thiscoupler.Connected != nullptr) { // break connection with other vehicle, if there's any: on the end it's coupled with, which is the front one too // for vehicles facing each other - thiscoupler.Connected->Couplers[thiscoupler.ConnectedNr].CouplingFlag = coupling::faux; + auto &othercoupler{thiscoupler.Connected->Couplers[thiscoupler.ConnectedNr]}; + othercoupler.CouplingFlag = coupling::faux; + // the coupling broke between the two, either end can be repaired + othercoupler.broken = true; if (hosetorn && Global.coupler_physics) { // the brake pipe of the other part of the train vents through the torn hose as well @@ -1457,6 +1461,44 @@ void TMoverParameters::damage_coupler(int const End) WriteLog("Bad driving: " + Name + " broke a coupler"); } +namespace +{ + +// the coupler as good as new, the vehicle without the coupler damage (and the emergency braking of the torn brake +// hose) once neither of its couplers is broken +void repair(TMoverParameters &Vehicle, TCoupling &Coupler) +{ + Coupler.broken = false; + Coupler.stretch_duration = 0.f; + Coupler.physics_state.couple(); + if (Vehicle.Couplers[end::front].broken || Vehicle.Couplers[end::rear].broken) + { + return; + } + ClearFlag(Vehicle.DamageFlag, dtrain_coupling); + // either vehicle of the pair may have been braking through the torn hose + Vehicle.AlarmChainFlag = false; +} + +} // namespace + +bool TMoverParameters::repair_coupler(int const End) +{ + auto &thiscoupler{Couplers[End]}; + if (false == thiscoupler.broken) + { + return false; + } + repair(*this, thiscoupler); + // the other half of the coupling, which may be the vehicle flagged with the damage + if (auto *other{thiscoupler.Connected}; other != nullptr && other->Couplers[thiscoupler.ConnectedNr].broken) + { + repair(*other, other->Couplers[thiscoupler.ConnectedNr]); + } + WriteLog(std::format("Coupler of {} repaired, at its {} end", Name, End == end::front ? "front" : "rear")); + return true; +} + void TMoverParameters::Derail(DerailReason const Reason) { if (SetFlag(DamageFlag, dtrain_out)) diff --git a/application/driveruipanels.cpp b/application/driveruipanels.cpp index 09e4a0540..c86d017c6 100644 --- a/application/driveruipanels.cpp +++ b/application/driveruipanels.cpp @@ -40,6 +40,24 @@ http://mozilla.org/MPL/2.0/. #include +namespace { + +// how close to a broken coupler the camera has to be for the driving aid to offer its repair [m] +constexpr float coupler_repair_radius { 5.f }; + +// the broken coupler nearest to the camera, within reach: the vehicle and its end, or no vehicle +std::pair broken_coupler_nearby( glm::dvec3 const &Camera ) { + auto const *vehicle { std::get( simulation::Region->find_vehicle( Camera, coupler_repair_radius, false, true ) ) }; + if( vehicle == nullptr || vehicle->MoverParameters == nullptr ) { return { nullptr, end::front }; } + auto const front { glm::distance( vehicle->CouplerPosition( end::front ), Camera ) }; + auto const rear { glm::distance( vehicle->CouplerPosition( end::rear ), Camera ) }; + auto const nearest { front <= rear ? end::front : end::rear }; + if( std::min( front, rear ) > coupler_repair_radius || false == vehicle->MoverParameters->Couplers[ nearest ].broken ) { return { nullptr, end::front }; } + return { vehicle, nearest }; +} + +} // namespace + void drivingaid_panel::update() { @@ -47,6 +65,8 @@ drivingaid_panel::update() { text_lines.clear(); + std::tie( m_brokenvehicle, m_brokenend ) = broken_coupler_nearby( Global.pCamera.Pos ); + auto const *train { simulation::Train }; auto const *controlled { ( train ? train->Dynamic() : nullptr ) }; @@ -223,6 +243,12 @@ drivingaid_panel::render_contents() { } } + // only the coupler the camera is next to, and the one of the vehicle it was coupled with + if( m_brokenvehicle != nullptr && ImGui::Button( STR_C( "Repair coupler" ) ) ) { + command_relay commandrelay; + commandrelay.post( user_command::repaircoupler, m_brokenend, 0.0, GLFW_PRESS, 0, glm::vec3( 0.0f ), &m_brokenvehicle->name() ); + } + ui_panel::render_contents(); } @@ -1628,8 +1654,8 @@ debug_panel::update_section_coupler( std::vector &Output ) { Output.emplace_back( std::format( "Stretch: {:.1f} mm, stop: {}, overloaded for {:.1f} s", state.elongation * 1000.0, coupler_stop_name( state.stop ), coupler.stretch_duration ), Global.UITextColor ); - if( TestFlag( mover.DamageFlag, dtrain_coupling ) ) { - Output.emplace_back( "Coupler broken", glm::vec4( 1.f, 0.25f, 0.25f, 1.f ) ); + if( coupler.broken ) { + Output.emplace_back( "Coupler broken (repair it in the driving aid, F1)", glm::vec4( 1.f, 0.25f, 0.25f, 1.f ) ); } } diff --git a/application/driveruipanels.h b/application/driveruipanels.h index 3b5a8dd07..8c21a6240 100644 --- a/application/driveruipanels.h +++ b/application/driveruipanels.h @@ -25,6 +25,9 @@ public: private: // members std::array m_buffer; + // the broken coupler next to the camera, which the panel offers to repair: the vehicle and its end + TDynamicObject const *m_brokenvehicle { nullptr }; + int m_brokenend { 0 }; }; class timetable_panel : public ui_expandable_panel { diff --git a/input/command.cpp b/input/command.cpp index 7d3fe7d1d..32ba02e94 100644 --- a/input/command.cpp +++ b/input/command.cpp @@ -415,6 +415,7 @@ commanddescription_sequence Commands_descriptions = { {"linebreakertogglec", command_target::vehicle, command_mode::oneoff}, {"linebreakeropenc", command_target::vehicle, command_mode::oneoff}, {"linebreakerclosec", command_target::vehicle, command_mode::oneoff}, + {"repaircoupler", command_target::simulation, command_mode::oneoff}, }; // Maps of command and coresponding strings @@ -809,6 +810,7 @@ std::unordered_map const commandMap = { {"linebreakertogglec", user_command::linebreakertogglec}, {"linebreakeropenc", user_command::linebreakeropenc}, {"linebreakerclosec", user_command::linebreakerclosec}, + {"repaircoupler", user_command::repaircoupler}, {"none", user_command::none}}; } // namespace simulation diff --git a/input/command.h b/input/command.h index aaf361e25..664f6a3ba 100644 --- a/input/command.h +++ b/input/command.h @@ -413,6 +413,7 @@ enum class user_command linebreakertogglec, linebreakeropenc, linebreakerclosec, + repaircoupler, // the broken coupler of the vehicle (payload) at the end (param1) none = -1 }; diff --git a/simulation/simulation.cpp b/simulation/simulation.cpp index d10d555a3..31058900c 100644 --- a/simulation/simulation.cpp +++ b/simulation/simulation.cpp @@ -422,6 +422,12 @@ void state_manager::process_commands() { } } + if (commanddata.command == user_command::repaircoupler) { + if (auto *vehicle{simulation::Vehicles.find(commanddata.payload)}; vehicle != nullptr) { + vehicle->MoverParameters->repair_coupler(static_cast(commanddata.param1) == end::rear ? end::rear : end::front); + } + } + if (commanddata.command == user_command::fillcompressor) { TDynamicObject *vehicle = simulation::Vehicles.find(commanddata.payload); vehicle->MoverParameters->CompressedVolume = 8.0f * vehicle->MoverParameters->VeselVolume; diff --git a/tests/unit/test_coupler_physics.cpp b/tests/unit/test_coupler_physics.cpp index d8d1003b9..29a9438fa 100644 --- a/tests/unit/test_coupler_physics.cpp +++ b/tests/unit/test_coupler_physics.cpp @@ -458,3 +458,40 @@ TEST_CASE("couplers: a broken coupling of vehicles facing each other uncouples t // the other coupling of the second vehicle stays CHECK(second->Couplers[end::rear].CouplingFlag == coupling::coupler); } + +TEST_CASE("couplers: a broken coupler is repaired alone, with the one of the vehicle it was coupled with") { + auto first{vehicle{definition::load("wagon"), 0.0, "first"}}; + auto second{vehicle{definition::load("wagon"), 0.0, "second"}}; + auto third{vehicle{definition::load("wagon"), 0.0, "third"}}; + join(first, end::rear, second, end::front, coupling::coupler | coupling::brakehose); + join(second, end::rear, third, end::front, coupling::coupler | coupling::brakehose); + + // both couplings of the second wagon break + second->damage_coupler(end::front); + second->damage_coupler(end::rear); + REQUIRE(second->Couplers[end::front].broken); + REQUIRE(first->Couplers[end::rear].broken); + REQUIRE(third->Couplers[end::front].broken); + REQUIRE(TestFlag(second->DamageFlag, dtrain_coupling)); + + // nothing to repair at the far end of the first wagon + CHECK_FALSE(first->repair_coupler(end::front)); + + // repaired from the first wagon: the coupling between the first two, the other one stays broken + CHECK(first->repair_coupler(end::rear)); + CHECK_FALSE(first->Couplers[end::rear].broken); + CHECK_FALSE(second->Couplers[end::front].broken); + 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->AlarmChainFlag); + + CHECK(third->repair_coupler(end::front)); + CHECK_FALSE(second->Couplers[end::rear].broken); + CHECK_FALSE(TestFlag(second->DamageFlag, dtrain_coupling)); + CHECK_FALSE(second->AlarmChainFlag); + // repaired, not coupled: the vehicles are coupled again as usual + CHECK(second->Couplers[end::rear].CouplingFlag == coupling::faux); + CHECK(second->Attach(end::rear, end::front, &third.mover(), coupling::coupler, true, false)); +}