diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 3a64609d9..8b440f2b1 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2513,6 +2513,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 ae32909f2..b94a1e943 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -600,7 +600,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)}; @@ -609,6 +610,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) { @@ -1207,6 +1220,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) { @@ -5467,6 +5533,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..bbb1c0dd9 100644 --- a/tests/unit/test_couplers.cpp +++ b/tests/unit/test_couplers.cpp @@ -525,3 +525,130 @@ 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 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}; + 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 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"}}; + 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 8957a4a0e..ce299ce5d 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -811,6 +811,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; } @@ -1653,6 +1659,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 50e42085b..872338b46 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -275,6 +275,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 110193af1..b7a9359db 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -7047,8 +7047,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}; @@ -7070,7 +7076,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 38122c450..e38b03a6b 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -1743,7 +1743,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,