From fdbcdde140b1409d24647891a784b52c68ec80f4 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Thu, 26 Nov 2026 22:27:38 +0100 Subject: [PATCH] Diesel-electrics: a switch for the field shunting, with its cab control (#346) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: a switch of the field shunting of the diesel-electrics, and its cab control (GE#167) The SU45 and SP45 shunt the field of their motors by relays (RelayType 45), which the driver lets work with a toggle switch on the desk ("załączenie bocznikowania"). The game had no such switch: AutoRelay=Yes shunts always, and AutoRelay=Optional on a diesel-electric meant manual shunting with the switch off. No cab control showed the switch either, so the lever of the SU45's cab (zalaczbocznikow) had nothing to follow. - AutoRelay=Optional on a diesel-electric: switched off, no shunting at all (fieldshunting::diesel_electric() in the core library, used by IncScndCtrl() and the shunting relays in TractionForce()). No diesel-electric of the game data uses AutoRelay=Optional; AutoRelay=Yes and No don't change. - A new cab control, autorelay_sw:, showing AutoRelayFlag and toggling it with notchingrelaytoggle on a click. - The AI switches it on as it gets a diesel-electric ready. Needs the game data (SVN) to take effect, after the release: - dynamic/pkp/su45_v2/301d.fiz, sp45_v1/301db.fiz, 301db-152.fiz: AutoRelay=Yes -> AutoRelay=Optional - dynamic/pkp/su45_v2/301d.mmd, sp45_v1/301db.mmd, 301db-152.mmd, 301db_factory.mmd: add autorelay_sw: { zalaczbocznikow rot 0.11 0 0.1 soundinc: 20786_203E_switch_converter_2_on.wav sounddec: 20786_203E_switch_converter_2_of.wav } Without them the vehicles behave as before. * refactor(mover): the field shunting switch without Sonar issues The control is declared in the if that uses it, the relays' branch tests AutoRelayFlag as before (the same condition as the automatic control, and no deeper nesting on a changed line), using enum in diesel_electric(), and the test no longer shadows the name control. No change in behaviour. * fix(ai): the AI turns the field shunting switch on while running too It did so only in PrepareEngine(), which isn't called again while the engine runs, so a running SU45 handed to the AI with the switch off stayed on full field. * fix(mover): AutoRelay=Optional keeps its meaning, the field shunting switch is FieldShuntSwitch=Yes AutoRelay=Optional means again what it did for every diesel-electric: with the switch off, the driver shunts the field by hand with the second controller. The switch that leaves the field full when off, as on the SU45 and SP45, is opted into with a new Cntrl. key, FieldShuntSwitch=Yes, next to AutoRelay=Optional. A key rather than a new AutoRelay value: the parser reads an AutoRelay value it doesn't know as No, so an older exe would take the relays away from the SU45, while it skips an unknown key. The value stays Optional, which every check of the switch looks for. The AI switches it on only in a vehicle with the key; one with AutoRelay=Optional alone is left as before. The SVN data then needs "AutoRelay=Optional FieldShuntSwitch=Yes" on line 9 of su45_v2/301d.fiz, sp45_v1/301db.fiz and sp45_v1/301db-152.fiz, after this is released; without it nothing changes. --- CMakeLists.txt | 1 + McZapkie/MOVER.h | 3 + McZapkie/Mover.cpp | 11 +++- input/drivermouseinput.cpp | 3 + tests/unit/drive.cpp | 7 ++- tests/unit/test_field_shunting.cpp | 99 ++++++++++++++++++++++++++++++ vehicle/Driver.cpp | 17 +++++ vehicle/Driver.h | 1 + vehicle/Train.cpp | 8 +++ vehicle/Train.h | 1 + vehicle/fieldshunting.cpp | 27 ++++++++ vehicle/fieldshunting.h | 32 ++++++++++ 12 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 tests/unit/test_field_shunting.cpp create mode 100644 vehicle/fieldshunting.cpp create mode 100644 vehicle/fieldshunting.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ed3b325e..93d374b94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -574,6 +574,7 @@ set(CORE_SOURCES "utilities/physicslog.cpp" "vehicle/enginestart.cpp" "vehicle/brakesetting.cpp" +"vehicle/fieldshunting.cpp" "vehicle/gaugewrap.cpp" "vehicle/drivingaid.cpp" "vehicle/brakingcurve.cpp" diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 84a597e87..1458c632e 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -1903,6 +1903,9 @@ class TMoverParameters int FastSerialCircuit = 0; /*0 - po kolei zamyka styczniki az do osiagniecia szeregowej, 1 - natychmiastowe wejscie na szeregowa*/ /*hunter-111012*/ int BackwardsBranchesAllowed = 1; int AutoRelayType = 0; /*0 -brak, 1 - jest, 2 - opcja*/ + // FieldShuntSwitch=Yes: the switch of AutoRelay=Optional of a diesel-electric is its switch of the field shunting, as the SU45's + // and SP45's (fieldshunting.h): off, the field stays full. without it, off hands the shunting to the second controller, as before + bool FieldShuntSwitch = false; bool CoupledCtrl = false; /*czy mainctrl i scndctrl sa sprzezone*/ bool HasCamshaft{false}; int DynamicBrakeType = 0; /*patrz dbrake_**/ diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 1a25aac20..037e52fee 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Logs.h" #include "utilities/parser.h" #include "simulation/simulation.h" +#include "vehicle/fieldshunting.h" #include #include @@ -3714,7 +3715,8 @@ bool TMoverParameters::IncScndCtrl(int CtrlSpeed) { // TBD, TODO: refactor this validation as part of relay check routine (currently in tractionforce() // TBD, TODO: diesel electric engine utilize scndctrlactualpos like the other types? - if (EngineType == TEngineType::DieselElectric && (IsMainCtrlNoPowerPos() || AutoRelayFlag || ShuntMode || false == Mains)) + if (EngineType == TEngineType::DieselElectric && + (IsMainCtrlNoPowerPos() || fieldshunting::diesel_electric(AutoRelayType, FieldShuntSwitch, AutoRelayFlag) != fieldshunting::control::manual || ShuntMode || false == Mains)) { OK = false; } @@ -7470,12 +7472,14 @@ void TMoverParameters::DieselElectricTractionForce(double tmp) EngineVoltage = 0; // przekazniki bocznikowania, kazdy inny dla kazdej pozycji - if (false == StLinFlag || ShuntMode) + if (auto const fieldshuntingcontrol{fieldshunting::diesel_electric(AutoRelayType, FieldShuntSwitch, AutoRelayFlag)}; + false == StLinFlag || ShuntMode || fieldshuntingcontrol == fieldshunting::control::off) { ScndCtrlPos = 0; } else if (AutoRelayFlag) { + // the shunting relays set the field shunting (fieldshunting::control::automatic) ShuntFieldRelaysCheck(tmpV, PosRatio, tmp); } } @@ -13307,6 +13311,9 @@ void TMoverParameters::LoadFIZ_CntrlControllers(std::string const &line) { AutoRelayType = 0; } + // a key of its own rather than a new AutoRelay value: an exe that doesn't know it reads the line as before, and the value + // stays Optional, which every check of the switch looks for + extract_value(FieldShuntSwitch, "FieldShuntSwitch", line, ""); extract_value(CoupledCtrl, "CoupledCtrl", line, ""); extract_value(HasCamshaft, "Camshaft", line, ""); diff --git a/input/drivermouseinput.cpp b/input/drivermouseinput.cpp index d8d389f44..90414d312 100644 --- a/input/drivermouseinput.cpp +++ b/input/drivermouseinput.cpp @@ -616,6 +616,9 @@ drivermouse_input::default_bindings() { { "maxcurrent_sw:", { motoroverloadrelaythresholdtoggle, none } }, + { "autorelay_sw:", { + notchingrelaytoggle, + none } }, { "waterpumpbreaker_sw:", { waterpumpbreakertoggle, none } }, diff --git a/tests/unit/drive.cpp b/tests/unit/drive.cpp index b03eeac2d..02fc74c03 100644 --- a/tests/unit/drive.cpp +++ b/tests/unit/drive.cpp @@ -5,6 +5,7 @@ #include "doctest.h" #include "McZapkie/hamulce.h" +#include "vehicle/fieldshunting.h" namespace test { @@ -137,7 +138,8 @@ void close_line_breaker(driven const &Driven) { Driven.cab.MainSwitch(true); } -// the converter, the compressor and the motor blowers, once the line breaker is closed +// the converter, the compressor and the motor blowers, once the line breaker is closed; and the switch of the +// field shunting of a diesel-electric that has one (AutoRelay=Optional with FieldShuntSwitch=Yes), as the AI does void switch_auxiliaries_on(TMoverParameters &Cab) { Cab.ConverterSwitch(true); Cab.CompressorSwitch(true); @@ -147,6 +149,9 @@ void switch_auxiliaries_on(TMoverParameters &Cab) { Cab.MotorBlowersSwitch(true, side); } } + if (Cab.EngineType == TEngineType::DieselElectric && fieldshunting::has_switch(Cab.AutoRelayType, Cab.FieldShuntSwitch)) { + Cab.AutoRelaySwitch(true); + } } } // namespace diff --git a/tests/unit/test_field_shunting.cpp b/tests/unit/test_field_shunting.cpp new file mode 100644 index 000000000..e61783b0e --- /dev/null +++ b/tests/unit/test_field_shunting.cpp @@ -0,0 +1,99 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scenario.h" + +#include "vehicle/fieldshunting.h" + +// the field shunting of the diesel-electric locomotives, set by the driver or by the shunting relays, and the switch +// of the SU45 and the SP45 that lets the relays shunt the field ("załączenie bocznikowania", eu07/maszyna#167) + +using test::definition; +using test::scenario; +using namespace test::channels; + +TEST_CASE("field shunting: who shunts the field of a diesel-electric, by AutoRelay, FieldShuntSwitch and the switch") { + using enum fieldshunting::control; + struct setting { + int type; // AutoRelay: 0 No, 1 Yes, 2 Optional + bool fieldshuntswitch; // FieldShuntSwitch + bool flag; // the switch, AutoRelayFlag + fieldshunting::control expected; + }; + for (auto const &[type, fieldshuntswitch, flag, expected] : + {setting{0, false, false, manual}, setting{1, false, true, automatic}, + // AutoRelay=Optional alone, as before: off, the driver shunts by hand + setting{2, false, true, automatic}, setting{2, false, false, manual}, + // with FieldShuntSwitch=Yes the switch of the field shunting: off, nobody + setting{2, true, true, automatic}, setting{2, true, false, off}, + // FieldShuntSwitch=Yes without the switch (AutoRelay=Yes or No) changes nothing + setting{1, true, true, automatic}, setting{0, true, false, manual}, + // AutoRelaySwitch from a coupled vehicle turns the flag of every one off, as before + setting{1, false, false, manual}, setting{1, true, false, manual}}) { + CAPTURE(type); + CAPTURE(fieldshuntswitch); + CAPTURE(flag); + CHECK(fieldshunting::diesel_electric(type, fieldshuntswitch, flag) == expected); + CHECK(fieldshunting::has_switch(type, fieldshuntswitch) == (type == 2 && fieldshuntswitch)); + } +} + +TEST_CASE("field shunting: the SU45 with its switch, off at full power and switched on and off on the move") { + // the SU45 (RelayType 45) given the switch (AutoRelay=Optional, FieldShuntSwitch=Yes) its cab has, with a coach: + // off, the field stays full and the second controller can't shunt it; on, the relays shunt it as they do with + // AutoRelay=Yes; off again, the shunting drops + scenario scenario{"traction/field shunting switch/su45", motion() + traction()}; + scenario.train().add(definition::fiz("su45_v2/301d").set("Cntrl", "AutoRelay", "Optional").set("Cntrl", "FieldShuntSwitch", "Yes"), 1); + scenario.add("bdhpumn_v3/bdhpumn"); + REQUIRE(scenario.start_up()); + auto &su45{scenario.driver().controlling}; + REQUIRE(su45.AutoRelayType == 2); + REQUIRE(su45.FieldShuntSwitch); + // the driver switches it on as the AI does, getting the locomotive ready + CHECK(su45.AutoRelayFlag); + scenario.act("field shunting off"); + CHECK(su45.AutoRelaySwitch(false)); + scenario.get_going(); + scenario.full_power(120.0); + CHECK(su45.IsMainCtrlMaxPowerPos()); + CHECK(su45.StLinFlag); + CHECK(su45.ScndCtrlPos == 0); + CHECK_FALSE(su45.IncScndCtrl(1)); + scenario.wait(1.0); + CHECK(su45.ScndCtrlPos == 0); + // over 50 km/h, where the relays of the SU45 let the third stage in + CHECK(su45.Vel > 50.0); + scenario.act("field shunting on"); + CHECK(su45.AutoRelaySwitch(true)); + scenario.wait(30.0); + CHECK(su45.ScndCtrlPos == su45.ScndCtrlPosNo); + scenario.act("field shunting off on the move"); + CHECK(su45.AutoRelaySwitch(false)); + scenario.wait(1.0); + CHECK(su45.ScndCtrlPos == 0); + scenario.check(); +} + +TEST_CASE("field shunting: a diesel-electric with AutoRelay=Optional alone shunts by hand with the switch off, as before") { + // the SU45 with AutoRelay=Optional but no FieldShuntSwitch: the switch is the old one, off hands the shunting to the + // second controller, and the start-up (as the AI) leaves it off + scenario scenario{"traction/field shunting switch/su45 optional alone", motion() + traction()}; + scenario.train().add(definition::fiz("su45_v2/301d").set("Cntrl", "AutoRelay", "Optional"), 1); + scenario.add("bdhpumn_v3/bdhpumn"); + REQUIRE(scenario.start_up()); + auto &su45{scenario.driver().controlling}; + REQUIRE(su45.AutoRelayType == 2); + REQUIRE_FALSE(su45.FieldShuntSwitch); + CHECK_FALSE(su45.AutoRelayFlag); + scenario.get_going(); + scenario.full_power(120.0); + CHECK(su45.IsMainCtrlMaxPowerPos()); + CHECK(su45.StLinFlag); + // over 50 km/h: the relays don't shunt, the driver does + CHECK(su45.Vel > 50.0); + CHECK(su45.ScndCtrlPos == 0); + scenario.act("field shunting by hand"); + CHECK(su45.IncScndCtrl(1)); + scenario.wait(1.0); + CHECK(su45.ScndCtrlPos == 1); + scenario.check(); +} diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index dfe4ed1c6..a8477a061 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/brakereleaser.h" #include "vehicle/brakesetting.h" #include "vehicle/brakingcurve.h" +#include "vehicle/fieldshunting.h" #include "vehicle/handover.h" #include "vehicle/signalpassing.h" #include "vehicle/timetableservice.h" @@ -3330,6 +3331,7 @@ bool TController::PrepareEngine() cue_action( driver_hint::rearmotorblowerson ); } } + control_field_shunting(); // sync virtual brake state with the 'real' one if( mvOccupied->BrakeHandle != TBrakeHandle::NoHandle ) { std::unordered_map const brakepositions{ @@ -8778,9 +8780,24 @@ TController::control_tractive_and_braking_force() { #endif } +// the switch of the field shunting of a diesel-electric that has one (AutoRelay=Optional with FieldShuntSwitch=Yes): off, +// the field stays full. checked while running too, since a vehicle taken over with its engine running doesn't go through +// PrepareEngine(). a diesel-electric with AutoRelay=Optional alone is left as before, shunting by hand with the switch off +void TController::control_field_shunting() { + + if( AIControllFlag + && mvControlling->EngineType == TEngineType::DieselElectric + && fieldshunting::has_switch( mvControlling->AutoRelayType, mvControlling->FieldShuntSwitch ) + && false == mvControlling->AutoRelayFlag ) { + mvControlling->AutoRelaySwitch( true ); + } +} + // ensure relays are active void TController::control_relays() { + control_field_shunting(); + if( fActionTime < 0.0 ) { return; } if( IsAnyMotorOverloadRelayOpen ) { diff --git a/vehicle/Driver.h b/vehicle/Driver.h index a00a3935f..866c913b6 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -391,6 +391,7 @@ private: bool is_brake_charging_done() const; double consist_max_crp() const; bool is_departure_blocked( double const Delay ) const; + void control_field_shunting(); void control_relays(); void control_motor_connectors(); void control_tractive_force(); diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index f28f11d3b..2c5a8d560 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -1519,6 +1519,8 @@ void TTrain::OnCommand_notchingrelaytoggle(TTrain *Train, command_data const &Co // turn off Train->mvOccupied->AutoRelaySwitch(false); } + // visual feedback + Train->ggAutoRelayButton.UpdateValue(Train->mvOccupied->AutoRelayFlag ? 1.f : 0.f, Train->dsbSwitch); } } @@ -9890,6 +9892,8 @@ bool TTrain::Update(double const Deltatime) ggWiperSw.Update(); ggMaxCurrentCtrl.UpdateValue(true == mvControlled->ShuntModeAllow ? (true == mvControlled->ShuntMode ? 1.f : 0.f) : mvControlled->MotorOverloadRelayHighThreshold ? 1.f : 0.f); ggMaxCurrentCtrl.Update(); + ggAutoRelayButton.UpdateValue(mvOccupied->AutoRelayFlag ? 1.f : 0.f); + ggAutoRelayButton.Update(); // NBMX wrzesien 2003 - drzwi ggDoorLeftPermitButton.Update(lowvoltagepower); ggDoorRightPermitButton.Update(lowvoltagepower); @@ -11552,6 +11556,7 @@ void TTrain::clear_cab_controls() ggBrakeOperationModeCtrl.Clear(); ggWiperSw.Clear(); ggMaxCurrentCtrl.Clear(); + ggAutoRelayButton.Clear(); ggMainOffButton.Clear(); ggMainOnButton.Clear(); for (auto &remotelinebreaker : m_remotelinebreakers) @@ -11924,6 +11929,8 @@ void TTrain::set_cab_controls(int const Cab) ggCompressorListButton.PutValue(static_cast(mvOccupied->CompressorListPos - 1)); // motor overload relay threshold / shunt mode ggMaxCurrentCtrl.PutValue(true == mvControlled->ShuntModeAllow ? (true == mvControlled->ShuntMode ? 1.f : 0.f) : mvControlled->MotorOverloadRelayHighThreshold ? 1.f : 0.f); + // automatic relays (AutoRelay) + ggAutoRelayButton.PutValue(mvOccupied->AutoRelayFlag ? 1.f : 0.f); // lights ggLightsButton.PutValue(static_cast(mvOccupied->LightsPos - 1)); @@ -12329,6 +12336,7 @@ bool TTrain::initialize_gauge(cParser &Parser, std::string const &Label, int con {"brakeprofiler_sw:", ggBrakeProfileR}, {"brakeopmode_sw:", ggBrakeOperationModeCtrl}, {"maxcurrent_sw:", ggMaxCurrentCtrl}, + {"autorelay_sw:", ggAutoRelayButton}, {"main_off_bt:", ggMainOffButton}, {"main_on_bt:", ggMainOnButton}, {"mainb_sw:", m_remotelinebreakers[0].main_switch}, diff --git a/vehicle/Train.h b/vehicle/Train.h index c079c3f18..d0c3aaa8b 100644 --- a/vehicle/Train.h +++ b/vehicle/Train.h @@ -599,6 +599,7 @@ public: // reszta może by?publiczna TGauge ggWiperSw; // przelacznik wycieraczek TGauge ggMaxCurrentCtrl; + TGauge ggAutoRelayButton; // przełącznik AutoRelay: rozruch automatyczny, załączenie bocznikowania spalinowozu TGauge ggMainOffButton; TGauge ggMainOnButton; diff --git a/vehicle/fieldshunting.cpp b/vehicle/fieldshunting.cpp new file mode 100644 index 000000000..49300f50b --- /dev/null +++ b/vehicle/fieldshunting.cpp @@ -0,0 +1,27 @@ +/* +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/. +*/ + +#include "stdafx.h" +#include "vehicle/fieldshunting.h" + +namespace fieldshunting { + +bool has_switch(int const Autorelaytype, bool const Fieldshuntswitch) { + return Autorelaytype == 2 && Fieldshuntswitch; +} + +control diesel_electric(int const Autorelaytype, bool const Fieldshuntswitch, bool const Autorelayflag) { + using enum control; + if (Autorelayflag) { + return automatic; + } + return has_switch(Autorelaytype, Fieldshuntswitch) ? off : manual; +} + +} // namespace fieldshunting diff --git a/vehicle/fieldshunting.h b/vehicle/fieldshunting.h new file mode 100644 index 000000000..2dec04be5 --- /dev/null +++ b/vehicle/fieldshunting.h @@ -0,0 +1,32 @@ +/* +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/. +*/ + +#pragma once + +// the field shunting (ScndCtrlPos) of the diesel-electric locomotives +namespace fieldshunting { + +// who sets the field shunting +enum class control : int { + manual, // the driver, with the second controller + automatic, // the shunting relays (RelayType) + off // nobody: the shunting is switched off, the field stays full +}; + +// whether a diesel-electric has the switch that lets the relays shunt the field, as the SU45's and SP45's "załączenie +// bocznikowania" (eu07/maszyna#167): AutoRelay=Optional (Autorelaytype 2) with FieldShuntSwitch=Yes (Fieldshuntswitch) +bool has_switch(int Autorelaytype, bool Fieldshuntswitch); + +// who sets the field shunting of a diesel-electric locomotive, by its AutoRelay (Autorelaytype: 0 No, 1 Yes, 2 Optional), +// its FieldShuntSwitch (Fieldshuntswitch) and its AutoRelayFlag (Autorelayflag). with the flag set, the relays. with the +// switch of the field shunting (has_switch()) switched off, nobody. otherwise the driver, AutoRelay=Optional without +// FieldShuntSwitch included +control diesel_electric(int Autorelaytype, bool Fieldshuntswitch, bool Autorelayflag); + +} // namespace fieldshunting