From 6dc1239553f41070f7439786ad3d29d86005caf8 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 21:16:19 +0100 Subject: [PATCH 1/2] fix: no multiple-unit control between electric and diesel locomotives An electric and a diesel locomotive got their control coupled whenever the scenario (and so the Starter) asked for it, since the scenery coupling ignores ControlType, or when neither coupler named a control system, so a Vectron and an SM42 ran in multiple unit. TMoverParameters::Attach() now leaves the control connection out between an electric (ElectricSeriesMotor, ElectricInductionMotor) and a diesel (DieselEngine, DieselElectric/DumbDE) traction vehicle, for the scenery, the AI, the coupling keys and the automatic couplers alike, and logs it. Vehicles without own traction (cars, control trailers) are not affected, nor are permanent couplings within a unit, nor couplers that both name the same ControlType, which stays the vehicle authors' way to allow such a pair. The AI coupling order expects the coupling the vehicles can take, so it doesn't wait for the control connection forever. Existing scenarios coupling such a pair still load, without the control connection. "controlcoupling.enginecheck no" in eu07.ini turns the check off. No SVN data change is needed. (eu07/maszyna#520) --- McZapkie/MOVER.h | 8 +++ McZapkie/Mover.cpp | 69 +++++++++++++++++- tests/unit/test_couplers.cpp | 135 +++++++++++++++++++++++++++++++++++ utilities/Globals.cpp | 7 ++ utilities/Globals.h | 1 + vehicle/Driver.cpp | 12 +++- vehicle/DynObj.cpp | 3 +- 7 files changed, 230 insertions(+), 5 deletions(-) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 199c65a0c..55a175145 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2453,6 +2453,14 @@ class TMoverParameters // whether the automatic couplers at the End of this vehicle and the OtherEnd of the other one can catch each other on // contact, rather than the vehicles colliding (with crash damage enabled, only at a closing speed which doesn't derail them) bool AutomaticCouplingSafe(int const End, TMoverParameters const &Other, int const OtherEnd) const; + // whether the control coupling (multiple-unit control) can join the End of this vehicle and the OtherEnd of the other + // one: not between an electric and a diesel traction vehicle, unless both couplers name the same control system + // (ControlType). Vehicles without own traction (cars, control trailers) take any. Global.control_coupling_engine_check + // off allows it always (eu07/maszyna#520) + bool ControlCouplingAllowed(int const End, TMoverParameters const &Other, int const OtherEnd) const; + // the CouplingType to join the End of this vehicle and the OtherEnd of the other one with, without the control coupling + // when ControlCouplingAllowed() rules it out. a permanent coupling (of the vehicles of one unit) is kept whole + int CompatibleCoupling(int const End, TMoverParameters const &Other, int const OtherEnd, int const CouplingType) const; // the horns of the Signal (WarningSignal bits) which have the air or the voltage they need to sound int SoundingHorns(int const Signal) const; // whether the signal lamps of the main circuit (line contactors, ground relay, motor overload relays) can light: diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 330e8eb8f..4b10f4f9c 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -599,7 +599,8 @@ bool TMoverParameters::Attach(int ConnectNo, int ConnectToNr, TMoverParameters * } auto &thiscoupler{Couplers[ConnectNo]}; - auto &othercoupler = ConnectTo->Couplers[ConnectToNr != 2 ? ConnectToNr : thiscoupler.ConnectedNr]; + auto const otherend{ConnectToNr != 2 ? ConnectToNr : thiscoupler.ConnectedNr}; + auto &othercoupler = ConnectTo->Couplers[otherend]; auto const distance{CouplerDist(this, ConnectTo) - (thiscoupler.adapter_length + othercoupler.adapter_length)}; @@ -608,6 +609,18 @@ bool TMoverParameters::Attach(int ConnectNo, int ConnectToNr, TMoverParameters * return false; } + // no multiple-unit control between an electric and a diesel locomotive, whoever asks for it: the scenario, the AI + // or the player (eu07/maszyna#520) + if (auto const compatible{CompatibleCoupling(ConnectNo, *ConnectTo, otherend, CouplingType)}; compatible != CouplingType) + { + WriteLog(std::format("Vehicles \"{}\" and \"{}\" can't share their control, coupled without the control connection", Name, ConnectTo->Name)); + CouplingType = compatible; + if (CouplingType == coupling::faux) + { + return false; + } + } + // stykaja sie zderzaki i kompatybilne typy sprzegow, chyba że łączenie na starcie if (thiscoupler.CouplingFlag == coupling::faux) { @@ -1206,6 +1219,59 @@ bool TMoverParameters::AutomaticCouplingSafe(int const End, TMoverParameters con return false == Global.crash_damage || CollisionVelocity(End, Other, OtherEnd) <= SafeCollisionVelocity; } +namespace +{ + +// the kind of traction a vehicle's engine stands for, as far as its multiple-unit control goes +enum class traction_kind +{ + none, + electric, + diesel +}; + +traction_kind traction_of(TEngineType const Engine) +{ + switch (Engine) + { + case TEngineType::ElectricSeriesMotor: + case TEngineType::ElectricInductionMotor: + return traction_kind::electric; + case TEngineType::DieselEngine: + case TEngineType::DieselElectric: + return traction_kind::diesel; + default: + return traction_kind::none; + } +} + +} // namespace + +bool TMoverParameters::ControlCouplingAllowed(int const End, TMoverParameters const &Other, int const OtherEnd) const +{ + if (false == Global.control_coupling_engine_check) + { + return true; + } + // the same control system named on both couplers is the vehicle authors' word that they work together + if (auto const &controltype{Couplers[End].control_type}; false == controltype.empty() && controltype == Other.Couplers[OtherEnd].control_type) + { + return true; + } + auto const traction{traction_of(EngineType)}; + auto const othertraction{traction_of(Other.EngineType)}; + return traction == traction_kind::none || othertraction == traction_kind::none || traction == othertraction; +} + +int TMoverParameters::CompatibleCoupling(int const End, TMoverParameters const &Other, int const OtherEnd, int const CouplingType) const +{ + if ((CouplingType & coupling::control) == 0 || (CouplingType & coupling::permanent) != 0 || ControlCouplingAllowed(End, Other, OtherEnd)) + { + return CouplingType; + } + return CouplingType & ~coupling::control; +} + int TMoverParameters::SoundingHorns(int const Signal) const { auto const supplied{[this](horn_supply const Supply) { @@ -5454,6 +5520,7 @@ double TMoverParameters::CouplerForce(int const End, double dt) { couplingtype &= ~coupling::control; } + couplingtype = CompatibleCoupling(End, *othervehicle, otherend, couplingtype); // too fast for the couplers to catch, the vehicles collide instead (and may derail, see CollisionDetect()) auto const safe{AutomaticCouplingSafe(End, *othervehicle, otherend)}; diff --git a/tests/unit/test_couplers.cpp b/tests/unit/test_couplers.cpp index 2a0ff6e50..bed4d7a73 100644 --- a/tests/unit/test_couplers.cpp +++ b/tests/unit/test_couplers.cpp @@ -525,3 +525,138 @@ TEST_CASE("couplers: a collision of coupled vehicles keeps the momentum of the p CHECK(0.5 * (front_wagon.speed() + rear_wagon.speed()) > 13.5); CHECK(0.5 * (front_wagon.speed() + rear_wagon.speed()) < 15.05 + 1e-6); } + +// eu07/maszyna#520: an electric and a diesel locomotive got their control coupled (multiple-unit control) when the +// scenario or the Starter asked for it, or when the couplers let it through, as a Vectron and an SM42 did + +namespace { + +// the given setting of the control coupling check for the test, restored after it +class control_coupling_check_setting { +public: + explicit control_coupling_check_setting(bool Enabled) { Global.control_coupling_engine_check = Enabled; } + ~control_coupling_check_setting() { Global.control_coupling_engine_check = m_previous; } + control_coupling_check_setting(control_coupling_check_setting const &) = delete; + control_coupling_check_setting &operator=(control_coupling_check_setting const &) = delete; + +private: + bool m_previous{Global.control_coupling_engine_check}; +}; + +constexpr int scenario_coupling{coupling::coupler | coupling::brakehose | coupling::mainhose | coupling::control}; + +// couples the rear of the first vehicle to the front of the second one the way the scenery does (forced, from the +// first one only), returns the coupling the pair got +int scenario_couple(vehicle &First, vehicle &Second, int Coupling = scenario_coupling) { + REQUIRE(First->Attach(end::rear, end::front, &Second.mover(), Coupling, true, false)); + CHECK(Second->Couplers[end::front].CouplingFlag == First->Couplers[end::rear].CouplingFlag); + return First->Couplers[end::rear].CouplingFlag; +} + +} // namespace + +TEST_CASE("couplers: an electric and a diesel locomotive don't share their control") { + control_coupling_check_setting const check{true}; + // the E6ACT names no control system (ControlType), as the Vectron doesn't; the SM42 names its own, the SM03 none + auto electric{vehicle{definition::fiz("e6act_v2/e6acta"), 0.0, "electric"}}; + REQUIRE(electric->EngineType == TEngineType::ElectricInductionMotor); + + SUBCASE("a diesel-electric one with its own control system") { + auto diesel{vehicle{definition::fiz("sm42_v1/6d1"), 0.0, "diesel"}}; + REQUIRE(diesel->EngineType == TEngineType::DieselElectric); + CHECK_FALSE(electric->ControlCouplingAllowed(end::rear, diesel.mover(), end::front)); + CHECK(scenario_couple(electric, diesel) == (scenario_coupling & ~coupling::control)); + } + SUBCASE("a diesel-mechanical one without a control system named, either") { + auto diesel{vehicle{definition::fiz("sm03_v1/sm03"), 0.0, "diesel"}}; + REQUIRE(diesel->EngineType == TEngineType::DieselEngine); + CHECK_FALSE(diesel->ControlCouplingAllowed(end::front, electric.mover(), end::rear)); + CHECK(scenario_couple(diesel, electric) == (scenario_coupling & ~coupling::control)); + } + SUBCASE("unless both name the same control system") { + auto diesel{vehicle{definition::fiz("sm03_v1/sm03").set("BuffCoupl", "ControlType", "shared"), 0.0, "diesel"}}; + auto shared{vehicle{definition::fiz("e6act_v2/e6acta").set("BuffCoupl", "ControlType", "shared"), 0.0, "electric"}}; + CHECK(scenario_couple(shared, diesel) == scenario_coupling); + } + SUBCASE("unless the vehicles make one unit, coupled for good") { + auto diesel{vehicle{definition::fiz("sm03_v1/sm03"), 0.0, "diesel"}}; + auto const permanent{scenario_coupling | coupling::permanent}; + CHECK(scenario_couple(electric, diesel, permanent) == permanent); + } + SUBCASE("unless the check is turned off") { + control_coupling_check_setting const off{false}; + auto diesel{vehicle{definition::fiz("sm42_v1/6d1"), 0.0, "diesel"}}; + CHECK(electric->ControlCouplingAllowed(end::rear, diesel.mover(), end::front)); + CHECK(scenario_couple(electric, diesel) == scenario_coupling); + } +} + +TEST_CASE("couplers: locomotives of the same traction and the cars keep their control coupling") { + control_coupling_check_setting const check{true}; + + SUBCASE("two electric ones") { + auto first{vehicle{definition::fiz("ep09_v2/104e"), 0.0, "first"}}; + auto second{vehicle{definition::fiz("et22_v3/201e-w"), 0.0, "second"}}; + CHECK(scenario_couple(first, second) == scenario_coupling); + } + SUBCASE("an electric one of each motor type") { + auto first{vehicle{definition::fiz("e6act_v2/e6acta"), 0.0, "first"}}; + auto second{vehicle{definition::fiz("eu04_v1/eu04-01"), 0.0, "second"}}; + CHECK(scenario_couple(first, second) == scenario_coupling); + } + SUBCASE("two diesel ones") { + auto first{vehicle{definition::fiz("sm03_v1/sm03"), 0.0, "first"}}; + auto second{vehicle{definition::fiz("wmb10_v1/wmb10"), 0.0, "second"}}; + CHECK(scenario_couple(first, second) == scenario_coupling); + } + SUBCASE("a diesel-mechanical and a diesel-electric one") { + auto first{vehicle{definition::fiz("sm03_v1/sm03"), 0.0, "first"}}; + auto second{vehicle{definition::fiz("st44_v2/m62-1151"), 0.0, "second"}}; + CHECK(scenario_couple(first, second) == scenario_coupling); + } + SUBCASE("an electric multiple unit's motor car and its control trailer") { + auto trailer{vehicle{definition::fiz("en57_v1/5bgb"), 0.0, "trailer"}}; + auto motorcar{vehicle{definition::fiz("en57_v1/6bai"), 0.0, "motorcar"}}; + CHECK(scenario_couple(trailer, motorcar) == scenario_coupling); + } + SUBCASE("a diesel locomotive and a car") { + auto diesel{vehicle{definition::fiz("sm42_v1/6d1"), 0.0, "diesel"}}; + auto car{vehicle{definition::load("wagon"), 0.0, "car"}}; + CHECK(scenario_couple(diesel, car) == scenario_coupling); + } +} + +TEST_CASE("couplers: automatic couplers of an electric and a diesel locomotive couple without the control connection") { + crash_damage_setting const crashdamage{true}; + control_coupling_check_setting const check{true}; + auto const flag{coupling::coupler | coupling::brakehose | coupling::control}; + auto automatic = [flag](std::string const &Path) { + return definition::fiz(Path).set("BuffCoupl", "CType", "Automatic").set("BuffCoupl", "AutomaticFlag", flag); + }; + auto moving{vehicle{automatic("e6act_v2/e6acta"), 5.0, "moving"}}; + + SUBCASE("a diesel one") { + auto standing{vehicle{automatic("sm03_v1/sm03"), 0.0, "standing"}}; + test::pair pair{moving, end::front, standing, end::rear, 1.0}; + REQUIRE(couple_during(pair, 3.0)); + CHECK(pair.coupling() == (coupling::coupler | coupling::brakehose)); + } + SUBCASE("an electric one") { + auto standing{vehicle{automatic("ep09_v2/104e"), 0.0, "standing"}}; + test::pair pair{moving, end::front, standing, end::rear, 1.0}; + REQUIRE(couple_during(pair, 3.0)); + CHECK(pair.coupling() == flag); + } +} + +TEST_CASE("couplers: the AI and the player get the coupling the vehicles can take") { + control_coupling_check_setting const check{true}; + auto electric{vehicle{definition::fiz("e6act_v2/e6acta"), 0.0, "electric"}}; + auto diesel{vehicle{definition::fiz("sm42_v1/6d1"), 0.0, "diesel"}}; + auto other{vehicle{definition::fiz("ep09_v2/104e"), 0.0, "other"}}; + + CHECK(electric->CompatibleCoupling(end::rear, diesel.mover(), end::front, scenario_coupling) == (scenario_coupling & ~coupling::control)); + CHECK(electric->CompatibleCoupling(end::rear, other.mover(), end::front, scenario_coupling) == scenario_coupling); + // nothing to take away without the control connection asked for + CHECK(electric->CompatibleCoupling(end::rear, diesel.mover(), end::front, coupling::coupler) == coupling::coupler); +} diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index b4f2e14be..99a2decb9 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -751,6 +751,12 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& return true; } + if (token == "controlcoupling.enginecheck") + { + ParseOne(Parser, control_coupling_engine_check, 1); + return true; + } + return false; } @@ -1590,6 +1596,7 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "scenario.time.current", ScenarioTimeCurrent ); export_as_text( Output, "scenario.weather.temperature", AirTemperature ); export_as_text( Output, "ai.trainman", AITrainman ); + export_as_text( Output, "controlcoupling.enginecheck", control_coupling_engine_check ); export_as_text( Output, "scalespeculars", ScaleSpecularValues ); export_as_text( Output, "gfxrenderer", GfxRenderer ); export_as_text( Output, "shadows", RenderShadows ); diff --git a/utilities/Globals.h b/utilities/Globals.h index ad216cd96..d079db2cb 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -260,6 +260,7 @@ struct global_settings { bool captureonstart = true; bool render_cab = true; bool crash_damage = true; + bool control_coupling_engine_check = true; // no multiple-unit control between electric and diesel traction vehicles (eu07/maszyna#520) bool gui_defaultwindows = true; bool gui_showtranscripts = true; bool gui_trainingdefault = false; diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 08f0c0d24..b267b390b 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -6907,8 +6907,14 @@ TController::UpdateConnect() { auto *vehicle { iCouplingVehicle.value().first }; auto const couplingend { iCouplingVehicle.value().second }; auto *vehicleparameters { vehicle->MoverParameters }; - if( vehicleparameters->Couplers[ couplingend ].CouplingFlag != iCoupler ) { - auto const &neighbour { vehicleparameters->Neighbours[ couplingend ] }; + auto const &neighbour { vehicleparameters->Neighbours[ couplingend ] }; + // vehicles which can't share their control get coupled without the control connection (eu07/maszyna#520) + auto expectedcoupling { iCoupler }; + if( neighbour.vehicle != nullptr ) { + auto const neighbourend { neighbour.vehicle_end != 2 ? neighbour.vehicle_end : vehicleparameters->Couplers[ couplingend ].ConnectedNr }; + expectedcoupling = vehicleparameters->CompatibleCoupling( couplingend, *neighbour.vehicle->MoverParameters, neighbourend, iCoupler ); + } + if( vehicleparameters->Couplers[ couplingend ].CouplingFlag != expectedcoupling ) { if( neighbour.vehicle != nullptr && neighbour.distance < 10 ) { // check whether we need to attach coupler adapter auto coupleriscompatible{true}; @@ -6930,7 +6936,7 @@ TController::UpdateConnect() { } } // NOTE: no else as the preceeding block can potentially establish expected connection - if( vehicleparameters->Couplers[ couplingend ].CouplingFlag == iCoupler ) { + if( vehicleparameters->Couplers[ couplingend ].CouplingFlag == expectedcoupling ) { // jeżeli został podłączony CheckVehicles( Connect ); // sprawdzić światła nowego składu diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index d5f60e332..659b718fa 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -1713,7 +1713,8 @@ TDynamicObject::couple( int const Side ) { if( false == TestFlag( MoverParameters->Couplers[ Side ].CouplingFlag, coupling::control ) ) { // ukrotnionko if( (coupler.AllowedFlag & othercoupler.AllowedFlag & coupling::control) == coupling::control - && coupler.control_type == othercoupler.control_type ) { + && coupler.control_type == othercoupler.control_type + && ( MoverParameters->CompatibleCoupling( Side, *othervehicleparams, neighbour.vehicle_end, coupler.CouplingFlag | coupling::control ) & coupling::control ) != 0 ) { if( MoverParameters->Attach( Side, neighbour.vehicle_end, othervehicleparams, From 3a718695695fa01ac56f9a8277594edaf90285d5 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 04:22:19 +0100 Subject: [PATCH 2/2] fix: table-drive the same-traction coupling test for CI The repeated subcases were duplicated code above the Sonar threshold; also renames a local shadowing coupling::permanent. --- tests/unit/test_couplers.cpp | 44 +++++++++++++++--------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/tests/unit/test_couplers.cpp b/tests/unit/test_couplers.cpp index bed4d7a73..bbb1c0dd9 100644 --- a/tests/unit/test_couplers.cpp +++ b/tests/unit/test_couplers.cpp @@ -580,8 +580,8 @@ TEST_CASE("couplers: an electric and a diesel locomotive don't share their contr } SUBCASE("unless the vehicles make one unit, coupled for good") { auto diesel{vehicle{definition::fiz("sm03_v1/sm03"), 0.0, "diesel"}}; - auto const permanent{scenario_coupling | coupling::permanent}; - CHECK(scenario_couple(electric, diesel, permanent) == permanent); + auto const forgood{scenario_coupling | coupling::permanent}; + CHECK(scenario_couple(electric, diesel, forgood) == forgood); } SUBCASE("unless the check is turned off") { control_coupling_check_setting const off{false}; @@ -594,30 +594,22 @@ TEST_CASE("couplers: an electric and a diesel locomotive don't share their contr TEST_CASE("couplers: locomotives of the same traction and the cars keep their control coupling") { control_coupling_check_setting const check{true}; - SUBCASE("two electric ones") { - auto first{vehicle{definition::fiz("ep09_v2/104e"), 0.0, "first"}}; - auto second{vehicle{definition::fiz("et22_v3/201e-w"), 0.0, "second"}}; - CHECK(scenario_couple(first, second) == scenario_coupling); - } - SUBCASE("an electric one of each motor type") { - auto first{vehicle{definition::fiz("e6act_v2/e6acta"), 0.0, "first"}}; - auto second{vehicle{definition::fiz("eu04_v1/eu04-01"), 0.0, "second"}}; - CHECK(scenario_couple(first, second) == scenario_coupling); - } - SUBCASE("two diesel ones") { - auto first{vehicle{definition::fiz("sm03_v1/sm03"), 0.0, "first"}}; - auto second{vehicle{definition::fiz("wmb10_v1/wmb10"), 0.0, "second"}}; - CHECK(scenario_couple(first, second) == scenario_coupling); - } - SUBCASE("a diesel-mechanical and a diesel-electric one") { - auto first{vehicle{definition::fiz("sm03_v1/sm03"), 0.0, "first"}}; - auto second{vehicle{definition::fiz("st44_v2/m62-1151"), 0.0, "second"}}; - CHECK(scenario_couple(first, second) == scenario_coupling); - } - SUBCASE("an electric multiple unit's motor car and its control trailer") { - auto trailer{vehicle{definition::fiz("en57_v1/5bgb"), 0.0, "trailer"}}; - auto motorcar{vehicle{definition::fiz("en57_v1/6bai"), 0.0, "motorcar"}}; - CHECK(scenario_couple(trailer, motorcar) == scenario_coupling); + SUBCASE("two locomotives or motor cars of the same traction") { + // two electric ones, an electric one of each motor type, two diesel ones, a diesel-mechanical and a + // diesel-electric one, an electric multiple unit's control trailer and its motor car + std::initializer_list> const pairs{ + {"ep09_v2/104e", "et22_v3/201e-w"}, + {"e6act_v2/e6acta", "eu04_v1/eu04-01"}, + {"sm03_v1/sm03", "wmb10_v1/wmb10"}, + {"sm03_v1/sm03", "st44_v2/m62-1151"}, + {"en57_v1/5bgb", "en57_v1/6bai"}}; + for (auto const &paths : pairs) { + CAPTURE(paths.first); + CAPTURE(paths.second); + auto first{vehicle{definition::fiz(paths.first), 0.0, "first"}}; + auto second{vehicle{definition::fiz(paths.second), 0.0, "second"}}; + CHECK(scenario_couple(first, second) == scenario_coupling); + } } SUBCASE("a diesel locomotive and a car") { auto diesel{vehicle{definition::fiz("sm42_v1/6d1"), 0.0, "diesel"}};