From 636d29eb228f8ccb5737cb558168be45bd8a10f8 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 21:53:25 +0100 Subject: [PATCH 1/6] fix: AI wagon inspector sets G/P/R by the instruction Cw-1 (eu07/maszyna#350) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The brake settings the AI chooses for a train (TController::AutoRewident) now follow Cw-1 (Mw56) §18 and §19: - freight trains: the 500 m / 1300 t and 300 m / 600 t limits apply to the wagons without the locomotives, limits included; - long locomotive setting: the five wagons on G are counted from the head of the train, not again after every locomotive in it; - a vehicle without the setting the train needs gets the other of G and P; - passenger trains run on R only with at most three cars on P, fewer than those on R; cars with a magnetic rail brake run on R+Mg. The choice moves to vehicle/brakesetting.{h,cpp} in eu07_core, with unit tests. The old inspector stays behind the new eu07.ini key ai.brakesetting.cw1 (default yes; no restores the earlier behaviour). No game data changes needed. (eu07/maszyna#350) --- CMakeLists.txt | 1 + tests/unit/test_brake_setting.cpp | 171 +++++++++++++++++++++++ utilities/Globals.cpp | 7 + utilities/Globals.h | 1 + vehicle/Driver.cpp | 123 ++++------------- vehicle/brakesetting.cpp | 216 ++++++++++++++++++++++++++++++ vehicle/brakesetting.h | 57 ++++++++ 7 files changed, 477 insertions(+), 99 deletions(-) create mode 100644 tests/unit/test_brake_setting.cpp create mode 100644 vehicle/brakesetting.cpp create mode 100644 vehicle/brakesetting.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f34ad61f..c8422e62f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -462,6 +462,7 @@ set(CORE_SOURCES "utilities/parser.cpp" "utilities/utilities.cpp" "scripting/ladderlogic.cpp" +"vehicle/brakesetting.cpp" "vehicle/handover.cpp" "vehicle/horn.cpp" "vehicle/smoke.cpp" diff --git a/tests/unit/test_brake_setting.cpp b/tests/unit/test_brake_setting.cpp new file mode 100644 index 000000000..7e598ad6e --- /dev/null +++ b/tests/unit/test_brake_setting.cpp @@ -0,0 +1,171 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "McZapkie/hamulce.h" +#include "vehicle/brakesetting.h" + +// the brake settings the AI wagon inspector (TController::AutoRewident) chooses for a train, by the +// brake instruction Cw-1 (Mw56) §18 and §19 (eu07/maszyna#350) + +namespace { + +using brake_setting::train_setting; + +constexpr int G{bdelay_G}; +constexpr int P{bdelay_P}; +constexpr int R{bdelay_R}; +constexpr int M{bdelay_M}; + +brake_setting::vehicle locomotive(int const Delays = G | P | R) { + return {Delays, true, 17.0, 80000.0}; +} +// a freight wagon of the given length [m] and mass [t] +brake_setting::vehicle freight_wagon(double const Length = 15.0, double const Tonnes = 80.0) { + return {G | P, false, Length, Tonnes * 1000.0}; +} +brake_setting::vehicle passenger_car(int const Delays = P | R) { + return {Delays, false, 24.5, 45000.0}; +} + +std::vector train(brake_setting::vehicle const &Locomotive, int const Count, brake_setting::vehicle const &Wagon) { + std::vector consist{Locomotive}; + consist.insert(consist.end(), Count, Wagon); + return consist; +} + +} // namespace + +TEST_CASE("brake setting: a freight train runs on P only up to 500 m and 1300 t of wagons (Cw-1 §19.2)") { + // 25 wagons of 20 m and 52 t: 500 m and 1300 t exactly, the limits included + CHECK(brake_setting::choose_train_setting(train(locomotive(), 25, freight_wagon(20.0, 52.0))) == train_setting::freight_gp); + // one metre longer + CHECK(brake_setting::choose_train_setting(train(locomotive(), 25, freight_wagon(20.04, 52.0))) == train_setting::freight_g); + // one tonne heavier + CHECK(brake_setting::choose_train_setting(train(locomotive(), 25, freight_wagon(20.0, 52.04))) == train_setting::freight_g); +} + +TEST_CASE("brake setting: the locomotives don't count into the length and mass of a freight train's wagons") { + // 300 m and 600 t of wagons behind two locomotives of 80 t: all the wagons on P (Cw-1 §19.3a) + auto consist{train(locomotive(), 20, freight_wagon(15.0, 30.0))}; + consist.insert(consist.begin(), locomotive()); + CHECK(brake_setting::choose_train_setting(consist) == train_setting::freight_p); + auto const settings{brake_setting::choose(consist)}; + CHECK(settings[0] == G); + CHECK(settings[1] == G); + for (std::size_t i = 2; i < settings.size(); ++i) { + CAPTURE(i); + CHECK(settings[i] == P); + } + // a wagon more makes it the long locomotive setting (Cw-1 §19.3b) + consist.emplace_back(freight_wagon(15.0, 30.0)); + CHECK(brake_setting::choose_train_setting(consist) == train_setting::freight_gp); +} + +TEST_CASE("brake setting: the locomotive and the first five wagons on G, the rest on P (Cw-1 §19.3b)") { + auto const consist{train(locomotive(), 12, freight_wagon(15.0, 60.0))}; + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_gp); + auto const settings{brake_setting::choose(consist)}; + REQUIRE(settings.size() == consist.size()); + for (std::size_t i = 0; i < settings.size(); ++i) { + CAPTURE(i); + CHECK(settings[i] == (i <= 5 ? G : P)); + } +} + +TEST_CASE("brake setting: a locomotive in the middle of the train doesn't put five more wagons on G") { + auto consist{train(locomotive(), 8, freight_wagon(15.0, 60.0))}; + consist.emplace_back(locomotive()); + consist.insert(consist.end(), 8, freight_wagon(15.0, 60.0)); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_gp); + auto const settings{brake_setting::choose(consist)}; + CHECK(settings[1] == G); + CHECK(settings[5] == G); + CHECK(settings[6] == P); + CHECK(settings[9] == G); // the locomotive + CHECK(settings[10] == P); + + // the inspector of the earlier releases started counting anew at it + auto const legacy{brake_setting::choose_legacy(consist)}; + CHECK(legacy[10] == G); + CHECK(legacy[14] == G); + CHECK(legacy[15] == P); +} + +TEST_CASE("brake setting: a heavy freight train runs on G, a vehicle without G on P") { + auto consist{train(locomotive(), 30, freight_wagon(15.0, 80.0))}; + consist.emplace_back(passenger_car(P)); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_g); + auto const settings{brake_setting::choose(consist)}; + for (std::size_t i = 0; i + 1 < settings.size(); ++i) { + CAPTURE(i); + CHECK(settings[i] == G); + } + CHECK(settings.back() == P); +} + +TEST_CASE("brake setting: a vehicle without the setting the train needs gets the other one of G and P") { + // a light freight train with a wagon which has G only + auto consist{train(locomotive(), 4, freight_wagon(15.0, 20.0))}; + consist.emplace_back(G, false, 15.0, 20000.0); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_p); + CHECK(brake_setting::choose(consist).back() == G); + // and a vehicle with neither is left alone + consist.emplace_back(R, false, 15.0, 20000.0); + CHECK(brake_setting::choose(consist).back() == 0); +} + +TEST_CASE("brake setting: a passenger train on R carries at most three cars on P, fewer than those on R (Cw-1 §18.3)") { + auto consist{train(locomotive(), 4, passenger_car())}; + consist.insert(consist.end(), 3, passenger_car(P)); + CHECK(brake_setting::choose_train_setting(consist) == train_setting::passenger_r); + auto const settings{brake_setting::choose(consist)}; + CHECK(settings[0] == R); + CHECK(settings[1] == R); + CHECK(settings.back() == P); + + // as many cars on P as on R: all on P + consist.emplace_back(passenger_car(P)); + CHECK(brake_setting::choose_train_setting(consist) == train_setting::passenger_p); + for (auto const setting : brake_setting::choose(consist)) { + CHECK(setting == P); + } + // the inspector of the earlier releases kept such a train on R + CHECK(brake_setting::choose_legacy(consist)[1] == R); + + // four cars on P are too many even with more on R + consist.insert(consist.end(), 6, passenger_car()); + CHECK(brake_setting::choose_train_setting(consist) == train_setting::passenger_p); +} + +TEST_CASE("brake setting: freight wagons in a passenger train on R count as cars on P (Cw-1 §18.4)") { + auto consist{train(locomotive(), 6, passenger_car())}; + consist.emplace_back(freight_wagon()); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::passenger_r); + CHECK(brake_setting::choose(consist).back() == P); +} + +TEST_CASE("brake setting: cars with a magnetic rail brake run on R+Mg in a train on R (Cw-1 §18.2)") { + auto consist{train(locomotive(G | P | R | M), 3, passenger_car(P | R | M))}; + consist.emplace_back(passenger_car()); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::passenger_r); + auto const settings{brake_setting::choose(consist)}; + CHECK(settings[0] == R); // the locomotive stays on R + CHECK(settings[1] == (R | M)); + CHECK(settings.back() == R); +} + +TEST_CASE("brake setting: a light engine runs on R where it has it") { + std::vector const consist{locomotive(), locomotive(G | P)}; + CHECK(brake_setting::choose_train_setting(consist) == train_setting::light_engine); + auto const settings{brake_setting::choose(consist)}; + CHECK(settings[0] == R); + CHECK(settings[1] == P); +} + +TEST_CASE("brake setting: the inspector of the earlier releases counted the locomotives into the train's mass") { + // 300 m and 600 t of wagons: P by Cw-1, the long locomotive setting before + auto const consist{train(locomotive(), 20, freight_wagon(15.0, 30.0))}; + CHECK(brake_setting::choose(consist)[1] == P); + CHECK(brake_setting::choose_legacy(consist)[1] == G); + CHECK(brake_setting::choose_legacy(consist)[6] == P); +} diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index b4f2e14be..9a3c81fc2 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -699,6 +699,12 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& return true; } + if (token == "ai.brakesetting.cw1") + { + ParseOne(Parser, AIBrakeSettingCw1, 1, false); + return true; + } + if (token == "pause") { std::string value; @@ -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, "ai.brakesetting.cw1", AIBrakeSettingCw1 ); 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..31510aa34 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -130,6 +130,7 @@ struct global_settings { bool bJoinEvents{ false }; // czy grupować eventy o tych samych nazwach int iHiddenEvents{ 1 }; // czy łączyć eventy z torami poprzez nazwę toru bool AITrainman{ true }; // virtual assistant performing consist coupling/decoupling and other maintenance tasks + bool AIBrakeSettingCw1{ true }; // ai.brakesetting.cw1: the AI wagon inspector sets the brakes (G/P/R/Mg) by the instruction Cw-1, no: as before eu07/maszyna#350 // ui int PythonScreenUpdateRate{ 200 }; // delay between python-based screen updates, in milliseconds int iTextMode{ 0 }; // tryb pracy wyświetlacza tekstowego diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 9f83b6d53..db6a8b7fd 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -19,6 +19,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/translation.h" #include "utilities/Logs.h" #include "vehicle/Train.h" +#include "vehicle/brakesetting.h" #include "vehicle/handover.h" #include "world/mtable.h" #include "vehicle/DynObj.h" @@ -2080,106 +2081,30 @@ void TController::Activation() void TController::AutoRewident() { // autorewident: nastawianie hamulców w składzie - int r = 0, g = 0, p = 0; // ilości wagonów poszczególnych typów - TDynamicObject *d = pVehicles[0]; // pojazd na czele składu - // 1. Zebranie informacji o składzie pociągu — przejście wzdłuż składu i odczyt parametrów: - // · ilość wagonów -> są zliczane, wszystkich pojazdów jest (iVehicles) - // · długość (jako suma) -> jest w (fLength) - // · masa (jako suma) -> jest w (fMass) - while (d) - { // klasyfikacja pojazdów wg BrakeDelays i mocy (licznik) - if (d->MoverParameters->Power < 1) // - lokomotywa - Power>1 - ale może być nieczynna na końcu... - if (TestFlag(d->MoverParameters->BrakeDelays, bdelay_R)) - ++r; // - wagon pospieszny - jest R - else if (TestFlag(d->MoverParameters->BrakeDelays, bdelay_G)) - ++g; // - wagon towarowy - jest G (nie ma R) - else - ++p; // - wagon osobowy - reszta (bez G i bez R) - d = d->Next(); // kolejny pojazd, podłączony od tyłu (licząc od czoła) + // 1. Zebranie informacji o składzie pociągu, od czoła do końca + std::vector consist; + for (auto const *d = pVehicles[0]; d != nullptr; d = d->Next()) { + auto const *mover{d->MoverParameters}; + // a locomotive has Power > 1, though it may be a dead one at the end of the train + consist.emplace_back(mover->BrakeDelays, mover->Power >= 1.0, mover->Dim.L, mover->TotalMass); } - // 2. Określenie typu pociągu i nastawy: - int ustaw; //+16 dla pasażerskiego - if (r + g + p == 0) - ustaw = 16 + bdelay_R; // lokomotywa luzem (może być wieloczłonowa) - else - { // jeśli są wagony - ustaw = g < std::min(4, r + p) ? 16 : 0; - if (ustaw) // jeśli towarowe < Min(4, pospieszne+osobowe) - { // to skład pasażerski - nastawianie pasażerskiego - ustaw += g && r < g + p ? bdelay_P : bdelay_R; - // jeżeli towarowe>0 oraz pospiesze<=towarowe+osobowe to P (0) - // inaczej R (2) - } - else - { // inaczej towarowy - nastawianie towarowego - if (fLength < 300.0 && fMass < 600000.0) //[kg] - ustaw |= bdelay_P; // jeżeli długość<300 oraz masa<600 to P (0) - else if (fLength < 500.0 && fMass < 1300000.0) - ustaw |= bdelay_R; // jeżeli długość<500 oraz masa<1300 to GP (2) - else - ustaw |= bdelay_G; // inaczej G (1) - } - // zasadniczo na sieci PKP kilka lat temu na P/GP jeździły tylko kontenerowce o - // rozkładowej 90 km/h. Pozostałe jeździły 70 km/h i były nastawione na G. - } - d = pVehicles[0]; // pojazd na czele składu - p = 0; // będziemy tu liczyć wagony od lokomotywy dla nastawy GP - while (d) - { // 3. Nastawianie - if( true == AIControllFlag - || d != pVehicle ) { + // 2. Określenie typu pociągu i nastaw: by the brake instruction Cw-1 (eu07/maszyna#350), or as the releases before it did + auto const settings{Global.AIBrakeSettingCw1 ? brake_setting::choose(consist) : brake_setting::choose_legacy(consist)}; + // a light engine counts as a passenger train for the braking below + auto const passengertrain{brake_setting::passenger(brake_setting::choose_train_setting(consist))}; + // 3. Nastawianie + auto setting{settings.begin()}; + for (auto *d = pVehicles[0]; d != nullptr && setting != settings.end(); d = d->Next(), ++setting) { + if (false == AIControllFlag && d == pVehicle) { // don't touch human-controlled vehicle, but others are free game - switch( ustaw ) { - - case bdelay_P: { - // towarowy P - lokomotywa na G, reszta na P. - d->MoverParameters->BrakeDelaySwitch( - d->MoverParameters->Power > 1 ? - bdelay_G : - bdelay_P ); - break; - } - case bdelay_G: { - // towarowy G - wszystko na G, jeśli nie ma to P (powinno się wyłączyć hamulec) - d->MoverParameters->BrakeDelaySwitch( - TestFlag( d->MoverParameters->BrakeDelays, bdelay_G ) ? - bdelay_G : - bdelay_P ); - break; - } - case bdelay_R: { - // towarowy GP - lokomotywa oraz 5 pierwszych pojazdów przy niej na G, reszta na P - if( d->MoverParameters->Power > 1 ) { - d->MoverParameters->BrakeDelaySwitch( bdelay_G ); - p = 0; // a jak będzie druga w środku? - } - else { - d->MoverParameters->BrakeDelaySwitch( - ++p <= 5 ? - bdelay_G : - bdelay_P ); - } - break; - } - case 16 + bdelay_R: { - // pasażerski R - na R, jeśli nie ma to P - d->MoverParameters->BrakeDelaySwitch( - TestFlag( d->MoverParameters->BrakeDelays, bdelay_R ) ? - bdelay_R : - bdelay_P ); - break; - } - case 16 + bdelay_P: { - // pasażerski P - wszystko na P - d->MoverParameters->BrakeDelaySwitch( bdelay_P ); - break; - } - } - // potentially release manual brake - d->MoverParameters->DecManualBrakeLevel( ManualBrakePosNo ); - d->MoverParameters->SpringBrake.Activate = false; + continue; } - d = d->Next(); // kolejny pojazd, podłączony od tyłu (licząc od czoła) + if (*setting != 0) { + d->MoverParameters->BrakeDelaySwitch(*setting); + } + // potentially release manual brake + d->MoverParameters->DecManualBrakeLevel( ManualBrakePosNo ); + d->MoverParameters->SpringBrake.Activate = false; } //ustawianie trybu pracy zadajnika hamulca, wystarczy raz po inicjalizacji AI if( true == AIControllFlag ) { @@ -2218,7 +2143,7 @@ void TController::AutoRewident() if( OrderCurrentGet() & ( Obey_train | Bank ) ) { // 4. Przeliczanie siły hamowania double const velstep = mvOccupied->Vmax * 0.5 / BrakeAccTableSize; - d = pVehicles[0]; // pojazd na czele składu + auto *d = pVehicles[0]; // pojazd na czele składu while (d) { for( int i = 0; i < BrakeAccTableSize; ++i ) { fBrake_a0[ i + 1 ] += d->MoverParameters->BrakeForceR( 0.25, velstep*( 1 + 2 * i ) ); @@ -2257,7 +2182,7 @@ void TController::AutoRewident() fNominalAccThreshold = std::max( -0.75, -fBrake_a0[ BrakeAccTableSize ] - 8 * fBrake_a1[ BrakeAccTableSize ] ); fBrakeReaction = 0.25; } - else if (ustaw > 16) { + else if (passengertrain) { fNominalAccThreshold = -fBrake_a0[ BrakeAccTableSize ] - 4 * fBrake_a1[ BrakeAccTableSize ]; fBrakeReaction = 1.00 + fLength*0.004; } diff --git a/vehicle/brakesetting.cpp b/vehicle/brakesetting.cpp new file mode 100644 index 000000000..785bfbd6d --- /dev/null +++ b/vehicle/brakesetting.cpp @@ -0,0 +1,216 @@ +/* +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/brakesetting.h" + +#include "McZapkie/hamulce.h" + +#include + +namespace brake_setting { + +namespace { + +// the limits of Cw-1 §19.2 and §19.3 for the wagons of a freight train (the "skład pociągu", without +// the locomotives) +constexpr double freight_p_max_length{500.0}; // [m] +constexpr double freight_p_max_mass{1300000.0}; // [kg] +constexpr double freight_p_all_max_length{300.0}; // [m] +constexpr double freight_p_all_max_mass{600000.0}; // [kg] + +// the most P-braked vehicles a passenger train on R may carry (Cw-1 §18.3, §18.4) +constexpr int passenger_r_max_p_wagons{3}; + +bool has(int const Delays, int const Setting) { + return (Delays & Setting) == Setting; +} + +// Wanted when the vehicle has it, else Fallback when it has that, else 0 +int pick(int const Delays, int const Wanted, int const Fallback) { + if (has(Delays, Wanted)) { + return Wanted; + } + if (has(Delays, Fallback)) { + return Fallback; + } + return 0; +} + +// the wagons of the train by the fastest setting they have +struct wagon_counts { + int r{0}; // with R: passenger cars of fast trains + int g{0}; // with G but no R: freight wagons + int p{0}; // the rest: passenger cars with P only + double length{0.0}; // [m] + double mass{0.0}; // [kg] + + explicit wagon_counts(std::vector const &Consist) { + for (auto const &item : Consist) { + if (item.powered) { + continue; + } + length += item.length; + mass += item.mass; + if (has(item.delays, bdelay_R)) { + ++r; + } + else if (has(item.delays, bdelay_G)) { + ++g; + } + else { + ++p; + } + } + } + + int wagons() const { + return r + g + p; + } + // a train is a passenger one unless it has at least as many freight wagons as there are others, + // or four of them (the inspector's rule of the earlier releases, Cw-1 doesn't tell) + bool passenger() const { + return g < std::min(4, r + p); + } +}; + +int passenger_r_setting(vehicle const &Item) { + if (false == Item.powered && has(Item.delays, bdelay_R | bdelay_M)) { + return bdelay_R | bdelay_M; + } + return pick(Item.delays, bdelay_R, bdelay_P); +} + +} // namespace + +train_setting choose_train_setting(std::vector const &Consist) { + wagon_counts const counts{Consist}; + if (counts.wagons() == 0) { + return train_setting::light_engine; + } + if (counts.passenger()) { + // wagons without R run on P, and there may be no more than three of them, fewer than those on R + auto const pwagons{counts.g + counts.p}; + return pwagons <= passenger_r_max_p_wagons && counts.r > pwagons ? train_setting::passenger_r : train_setting::passenger_p; + } + if (counts.length > freight_p_max_length || counts.mass > freight_p_max_mass) { + return train_setting::freight_g; + } + if (counts.length > freight_p_all_max_length || counts.mass > freight_p_all_max_mass) { + return train_setting::freight_gp; + } + return train_setting::freight_p; +} + +std::vector choose(std::vector const &Consist) { + auto const setting{choose_train_setting(Consist)}; + std::vector settings; + settings.reserve(Consist.size()); + auto wagonsfromhead{0}; + for (auto const &item : Consist) { + if (false == item.powered) { + ++wagonsfromhead; + } + switch (setting) { + case train_setting::light_engine: { + settings.emplace_back(pick(item.delays, bdelay_R, bdelay_P)); + break; + } + case train_setting::passenger_r: { + settings.emplace_back(passenger_r_setting(item)); + break; + } + case train_setting::passenger_p: { + settings.emplace_back(pick(item.delays, bdelay_P, bdelay_P)); + break; + } + case train_setting::freight_p: { + settings.emplace_back(item.powered ? pick(item.delays, bdelay_G, bdelay_P) : pick(item.delays, bdelay_P, bdelay_G)); + break; + } + case train_setting::freight_gp: { + // the wagons are counted from the head of the train, past any locomotive in the middle of it + auto const ong{item.powered || wagonsfromhead <= freight_gp_wagons_on_g}; + settings.emplace_back(ong ? pick(item.delays, bdelay_G, bdelay_P) : pick(item.delays, bdelay_P, bdelay_G)); + break; + } + case train_setting::freight_g: + default: { + settings.emplace_back(pick(item.delays, bdelay_G, bdelay_P)); + break; + } + } + } + return settings; +} + +std::vector choose_legacy(std::vector const &Consist) { + wagon_counts const counts{Consist}; + auto length{0.0}; + auto mass{0.0}; + for (auto const &item : Consist) { + length += item.length; + mass += item.mass; + } + auto setting{train_setting::light_engine}; + if (counts.wagons() > 0) { + if (counts.passenger()) { + setting = counts.g && counts.r < counts.g + counts.p ? train_setting::passenger_p : train_setting::passenger_r; + } + else if (length < 300.0 && mass < 600000.0) { + setting = train_setting::freight_p; + } + else if (length < 500.0 && mass < 1300000.0) { + setting = train_setting::freight_gp; + } + else { + setting = train_setting::freight_g; + } + } + std::vector settings; + settings.reserve(Consist.size()); + auto wagonsfromloco{0}; + for (auto const &item : Consist) { + switch (setting) { + case train_setting::light_engine: + case train_setting::passenger_r: { + settings.emplace_back(has(item.delays, bdelay_R) ? bdelay_R : bdelay_P); + break; + } + case train_setting::passenger_p: { + settings.emplace_back(bdelay_P); + break; + } + case train_setting::freight_p: { + settings.emplace_back(item.powered ? bdelay_G : bdelay_P); + break; + } + case train_setting::freight_gp: { + // the count started anew at every locomotive + if (item.powered) { + wagonsfromloco = 0; + settings.emplace_back(bdelay_G); + } + else { + ++wagonsfromloco; + settings.emplace_back(wagonsfromloco <= freight_gp_wagons_on_g ? bdelay_G : bdelay_P); + } + break; + } + case train_setting::freight_g: + default: { + settings.emplace_back(has(item.delays, bdelay_G) ? bdelay_G : bdelay_P); + break; + } + } + } + return settings; +} + +} // namespace brake_setting diff --git a/vehicle/brakesetting.h b/vehicle/brakesetting.h new file mode 100644 index 000000000..cfb421d55 --- /dev/null +++ b/vehicle/brakesetting.h @@ -0,0 +1,57 @@ +/* +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 + +#include + +// the brake setting (G/P/R/Mg) the virtual wagon inspector (TController::AutoRewident) chooses for every +// vehicle of a train, by the rules of the brake instruction Cw-1 (Mw56), §18 and §19 (eu07/maszyna#350) +namespace brake_setting { + +// what the inspector needs to know about a vehicle of the train +struct vehicle { + int delays{0}; // the settings the vehicle has (BrakeDelays, a combination of bdelay_*) + bool powered{false}; // a locomotive or a powered unit, as opposed to a wagon + double length{0.0}; // [m] + double mass{0.0}; // with its load [kg] +}; + +// the kind of train the inspector takes the consist for, and how it sets the brakes +enum class train_setting { + light_engine, // no wagons: R where there is one, else P + passenger_r, // passenger train on R, wagons with Mg on R+Mg (Cw-1 §18.2) + passenger_p, // passenger train on P (Cw-1 §18.3, §18.4) + freight_p, // freight train on P: locomotives on G, wagons on P (Cw-1 §19.3a) + freight_gp, // freight train on P: locomotives and the first five wagons on G, the rest on P (Cw-1 §19.3b) + freight_g // freight train on G (Cw-1 §19.1) +}; + +// how many wagons next to the locomotive stay on G in a freight_gp train +inline constexpr int freight_gp_wagons_on_g{5}; + +// the train setting by Cw-1. Consist runs from the head of the train to its rear +train_setting choose_train_setting(std::vector const &Consist); + +// a passenger train or a light engine, as opposed to a freight train. the inspector of the earlier +// releases tells them apart the same way +constexpr bool passenger(train_setting const Setting) { + return Setting == train_setting::light_engine || Setting == train_setting::passenger_r || Setting == train_setting::passenger_p; +} + +// the brake setting (bdelay_*) for every vehicle of Consist, in the same order, by Cw-1. a vehicle +// without the setting the train needs gets the other one of G and P it has, and 0 (left as it is) +// when it has neither. Cw-1 §19.9 would have the brake of such a vehicle cut out instead +std::vector choose(std::vector const &Consist); + +// the inspector of the releases before eu07/maszyna#350, kept for ai.brakesetting.cw1 no. the settings +// are requests: a vehicle which doesn't have the one asked for keeps its own +std::vector choose_legacy(std::vector const &Consist); + +} // namespace brake_setting From 1e650e375a72d03ed4c68cd6a2619ab1c7529589 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 12:32:28 +0100 Subject: [PATCH 2/6] docs: why the inspector puts a vehicle without G on P in a train on G (eu07/maszyna#350) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cw-1 §19.9 cuts the brake of such a vehicle out; the inspector keeps the earlier P, as it has no record to put a cut out brake back with. --- vehicle/brakesetting.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vehicle/brakesetting.cpp b/vehicle/brakesetting.cpp index 785bfbd6d..a55d3d751 100644 --- a/vehicle/brakesetting.cpp +++ b/vehicle/brakesetting.cpp @@ -142,6 +142,10 @@ std::vector choose(std::vector const &Consist) { } case train_setting::freight_g: default: { + // NOTE: Cw-1 §19.9 wants the brake of a vehicle without G (a passenger car with P and R only) cut out in a + // train braked on G. It's put on P instead, as before: a brake cut out by the inspector would have to be + // remembered and put back when the train is made up again, and the simulator has no such record yet. + // The same goes for the vehicles kept on G in the long locomotive setting above (freight_gp) settings.emplace_back(pick(item.delays, bdelay_G, bdelay_P)); break; } From 81f10e4d901ff54ac40331216f32086e5ec6e115 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 13:03:06 +0100 Subject: [PATCH 3/6] fix: AI wagon inspector cuts out the brake of wagons without G in a train on G (eu07/maszyna#350) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cw-1 §19.9: in a freight train braked on G, the brake of a vehicle that has no G setting (e.g. a passenger car with P and R) is cut out. The inspector put such wagons on P; now it cuts their brake out the way the scenery's '0' flag does (b_dmg, the distributor's reservoirs emptied), so the brake releases and stays off. - The vehicle remembers the inspector cut it out (TMoverParameters::BrakeCutOutByInspector); the next inspection puts such a brake back and sets it to what the new train needs. A brake cut out for any other reason (scenery, player, damage) is never put back; the cab's cut-out toggle hands an inspector's cut-out over to the player. - Locomotives and the vehicle the train is driven from keep a working brake (on P). In the long locomotive setting (§19.3b) the train runs on P, so a wagon without G stays on P there. - The braking tables after the settings leave cut-out brakes out. - §18.3: a car with R whose brake is cut out counts as one on P. - ai.brakesetting.cw1 no keeps the earlier behaviour: nothing cut out. The choice and the cutting out stay in vehicle/brakesetting.{h,cpp}, with unit tests on real wagons. --- McZapkie/MOVER.h | 3 + tests/unit/test_brake_setting.cpp | 170 +++++++++++++++++++++++++++++- vehicle/Driver.cpp | 14 +-- vehicle/Train.cpp | 2 + vehicle/brakesetting.cpp | 77 ++++++++++++-- vehicle/brakesetting.h | 26 ++++- 6 files changed, 273 insertions(+), 19 deletions(-) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 16bb207fa..77a122467 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2060,6 +2060,9 @@ class TMoverParameters bool UnlockPipe = false; /*unlockig brake pipe button pressed*/ int BrakeDelayFlag = 0; /*nastawa opoznienia ham. osob/towar/posp/exp 0/1/2/4*/ int BrakeDelays = 0; /*nastawy mozliwe do uzyskania*/ + // the AI wagon inspector cut the brake out (b_dmg) and puts it back when the train no longer needs it + // cut out (brake_setting::apply). Anyone else cutting it out or putting it back takes it over + bool BrakeCutOutByInspector = false; int BrakeOpModeFlag = 0; /*nastawa trybu pracy PS/PN/EP/MED 1/2/4/8*/ int BrakeOpModes = 0; /*nastawy mozliwe do uzyskania*/ bool DynamicBrakeFlag = false; /*czy wlaczony hamulec elektrodymiczny*/ diff --git a/tests/unit/test_brake_setting.cpp b/tests/unit/test_brake_setting.cpp index 7e598ad6e..aba1e80bb 100644 --- a/tests/unit/test_brake_setting.cpp +++ b/tests/unit/test_brake_setting.cpp @@ -1,7 +1,9 @@ #include "stdafx.h" #include "doctest.h" +#include "McZapkie/MOVER.h" #include "McZapkie/hamulce.h" +#include "vehicle.h" #include "vehicle/brakesetting.h" // the brake settings the AI wagon inspector (TController::AutoRewident) chooses for a train, by the @@ -91,18 +93,182 @@ TEST_CASE("brake setting: a locomotive in the middle of the train doesn't put fi CHECK(legacy[15] == P); } -TEST_CASE("brake setting: a heavy freight train runs on G, a vehicle without G on P") { +TEST_CASE("brake setting: a heavy freight train runs on G, a wagon without G has its brake cut out (Cw-1 §19.9)") { auto consist{train(locomotive(), 30, freight_wagon(15.0, 80.0))}; consist.emplace_back(passenger_car(P)); + consist.emplace_back(passenger_car()); REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_g); auto const settings{brake_setting::choose(consist)}; - for (std::size_t i = 0; i + 1 < settings.size(); ++i) { + for (std::size_t i = 0; i + 2 < settings.size(); ++i) { CAPTURE(i); CHECK(settings[i] == G); } + CHECK(settings[settings.size() - 2] == brake_setting::cut_out); + CHECK(settings.back() == brake_setting::cut_out); + + // the inspector of the earlier releases put them on P + auto const legacy{brake_setting::choose_legacy(consist)}; + CHECK(legacy[settings.size() - 2] == P); + CHECK(legacy.back() == P); +} + +TEST_CASE("brake setting: in a train on G a locomotive and the vehicle it's driven from keep a working brake") { + auto consist{train(locomotive(P | R), 30, freight_wagon(15.0, 80.0))}; + // a dead locomotive without G at the end, and a car which tells no settings at all + consist.emplace_back(locomotive(P | R)); + consist.emplace_back(0, false, 20.0, 40000.0); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_g); + auto settings{brake_setting::choose(consist)}; + CHECK(settings.front() == P); + CHECK(settings[settings.size() - 2] == P); + CHECK(settings.back() == 0); + + // a control car without G, the train driven from it, pushed by the locomotive at the rear + brake_setting::vehicle controlcar{P | R, false, 24.5, 45000.0}; + controlcar.driven = true; + consist.front() = controlcar; + consist.back() = locomotive(); + settings = brake_setting::choose(consist); + CHECK(settings.front() == P); + CHECK(settings.back() == G); +} + +TEST_CASE("brake setting: in the long locomotive setting a vehicle without G brakes on P, not cut out (Cw-1 §19.3b)") { + // the train runs on P, so §19.9 doesn't apply to it, among the first five wagons kept on G or behind them + auto consist{train(locomotive(), 2, freight_wagon(15.0, 60.0))}; + consist.emplace_back(passenger_car()); + consist.insert(consist.end(), 8, freight_wagon(15.0, 60.0)); + consist.emplace_back(passenger_car()); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_gp); + auto const settings{brake_setting::choose(consist)}; + CHECK(settings[2] == G); + CHECK(settings[3] == P); + CHECK(settings[4] == G); + CHECK(settings[6] == P); CHECK(settings.back() == P); } +TEST_CASE("brake setting: a car with its brake cut out counts as one on P in a passenger train (Cw-1 §18.3)") { + auto consist{train(locomotive(), 5, passenger_car())}; + consist.insert(consist.end(), 3, passenger_car(P)); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::passenger_r); + // one car with R cut out makes four on P + consist[1].cut_out = true; + CHECK(brake_setting::choose_train_setting(consist) == train_setting::passenger_p); + // with fewer P-only cars, three on P and four on R still run on R + consist.pop_back(); + CHECK(brake_setting::choose_train_setting(consist) == train_setting::passenger_r); + // a cut out brake changes nothing about a train being a passenger or a freight one + auto freight{train(locomotive(), 4, freight_wagon())}; + freight.emplace_back(passenger_car()); + freight.back().cut_out = true; + CHECK(brake_setting::choose_train_setting(freight) == train_setting::freight_p); +} + +namespace { + +// a passenger car with P and R, with the KE distributor of the test wagon +test::definition passenger_car_definition() { + return test::definition::load("wagon").set("Cntrl", "BrakeDelays", "PR"); +} + +// charges the brake of the vehicle from a brake pipe at 5 bar, then makes a full service application +void charge_and_apply(test::vehicle &Vehicle) { + Vehicle.hold_brake_pipe(5.0); + Vehicle.run(90.0); + Vehicle.hold_brake_pipe(3.5); + Vehicle.run(20.0); +} + +// the inspector at work on a heavy train on G with the vehicle at its end, as AutoRewident() does it +int inspect_heavy_train(test::vehicle &Vehicle) { + auto consist{train(locomotive(), 30, freight_wagon(15.0, 80.0))}; + consist.emplace_back(brake_setting::describe(Vehicle.mover(), false)); + auto const setting{brake_setting::choose(consist).back()}; + brake_setting::apply(Vehicle.mover(), setting); + return setting; +} + +// the same on a light train, which runs on P +int inspect_light_train(test::vehicle &Vehicle) { + auto consist{train(locomotive(), 4, freight_wagon())}; + consist.emplace_back(brake_setting::describe(Vehicle.mover(), false)); + auto const setting{brake_setting::choose(consist).back()}; + brake_setting::apply(Vehicle.mover(), setting); + return setting; +} + +} // namespace + +TEST_CASE("brake setting: the inspector cuts the brake out in a train on G and puts it back in a train on P") { + test::vehicle car{passenger_car_definition()}; + charge_and_apply(car); + REQUIRE(car->BrakePress > 1.0); + + // made up into a heavy train on G: the brake is cut out and released + REQUIRE(inspect_heavy_train(car) == brake_setting::cut_out); + CHECK(brake_setting::is_cut_out(car.mover())); + CHECK(car->BrakeCutOutByInspector); + car.run(30.0); + CHECK(car->BrakePress < 0.1); + // and it stays off whatever the brake pipe does + car.hold_brake_pipe(5.0); + car.run(60.0); + car.hold_brake_pipe(3.5); + car.run(20.0); + CHECK(car->BrakePress < 0.1); + car.hold_brake_pipe(0.0); + car.run(20.0); + CHECK(car->BrakePress < 0.1); + // to the inspector looking at the train again it's a car like any other, and the train needs it cut out still + CHECK(false == brake_setting::describe(car.mover(), false).cut_out); + REQUIRE(inspect_heavy_train(car) == brake_setting::cut_out); + CHECK(car->BrakeCutOutByInspector); + + // made up into a light train: the brake is back on P, charges and brakes again + CHECK(inspect_light_train(car) == P); + CHECK(false == brake_setting::is_cut_out(car.mover())); + CHECK(false == car->BrakeCutOutByInspector); + CHECK(car->BrakeDelayFlag == P); + // the reservoirs emptied when it was cut out take some five minutes to charge from the brake pipe + car.hold_brake_pipe(5.0); + car.run(300.0); + CHECK(car->Hamulec->GetBRP() > 4.9); + charge_and_apply(car); + CHECK(car->BrakePress > 1.0); +} + +TEST_CASE("brake setting: the inspector never puts back a brake cut out for another reason") { + test::vehicle car{passenger_car_definition()}; + charge_and_apply(car); + // cut out the way the scenery's '0' does it + car->Hamulec->ForceEmptiness(); + car->Hamulec->SetBrakeStatus(car->Hamulec->GetBrakeStatus() | b_dmg); + CHECK(brake_setting::describe(car.mover(), false).cut_out); + + // a train on G wants it cut out anyway: it isn't the inspector's to put back + CHECK(inspect_heavy_train(car) == brake_setting::cut_out); + CHECK(false == car->BrakeCutOutByInspector); + // a train on P would want it working: it stays cut out + CHECK(inspect_light_train(car) == P); + CHECK(brake_setting::is_cut_out(car.mover())); + charge_and_apply(car); + CHECK(car->BrakePress < 0.1); + + // a brake the inspector cut out, which someone else puts back and cuts out again, is theirs from then on + test::vehicle other{passenger_car_definition()}; + REQUIRE(inspect_heavy_train(other) == brake_setting::cut_out); + other->Hamulec->SetBrakeStatus(other->Hamulec->GetBrakeStatus() & ~b_dmg); + CHECK(inspect_heavy_train(other) == brake_setting::cut_out); // the inspector cuts it out again, and it's its own again + CHECK(other->BrakeCutOutByInspector); + other->Hamulec->SetBrakeStatus(other->Hamulec->GetBrakeStatus() & ~b_dmg); + CHECK(inspect_light_train(other) == P); + CHECK(false == other->BrakeCutOutByInspector); + other->Hamulec->SetBrakeStatus(other->Hamulec->GetBrakeStatus() | b_dmg); + CHECK(inspect_light_train(other) == P); + CHECK(brake_setting::is_cut_out(other.mover())); +} + TEST_CASE("brake setting: a vehicle without the setting the train needs gets the other one of G and P") { // a light freight train with a wagon which has G only auto consist{train(locomotive(), 4, freight_wagon(15.0, 20.0))}; diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index db6a8b7fd..0bb569c81 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2084,9 +2084,7 @@ void TController::AutoRewident() // 1. Zebranie informacji o składzie pociągu, od czoła do końca std::vector consist; for (auto const *d = pVehicles[0]; d != nullptr; d = d->Next()) { - auto const *mover{d->MoverParameters}; - // a locomotive has Power > 1, though it may be a dead one at the end of the train - consist.emplace_back(mover->BrakeDelays, mover->Power >= 1.0, mover->Dim.L, mover->TotalMass); + consist.emplace_back(brake_setting::describe(*d->MoverParameters, d == pVehicle)); } // 2. Określenie typu pociągu i nastaw: by the brake instruction Cw-1 (eu07/maszyna#350), or as the releases before it did auto const settings{Global.AIBrakeSettingCw1 ? brake_setting::choose(consist) : brake_setting::choose_legacy(consist)}; @@ -2099,9 +2097,8 @@ void TController::AutoRewident() // don't touch human-controlled vehicle, but others are free game continue; } - if (*setting != 0) { - d->MoverParameters->BrakeDelaySwitch(*setting); - } + // the setting, a brake cut out in a train on G (Cw-1 §19.9) or put back when the train no longer needs it cut out + brake_setting::apply(*d->MoverParameters, *setting); // potentially release manual brake d->MoverParameters->DecManualBrakeLevel( ManualBrakePosNo ); d->MoverParameters->SpringBrake.Activate = false; @@ -2145,7 +2142,10 @@ void TController::AutoRewident() double const velstep = mvOccupied->Vmax * 0.5 / BrakeAccTableSize; auto *d = pVehicles[0]; // pojazd na czele składu while (d) { - for( int i = 0; i < BrakeAccTableSize; ++i ) { + // a vehicle with its brake cut out (by the inspector above or for a reason of its own) adds its mass only; + // the inspector of the earlier releases counted its brake in + auto const brakes{false == Global.AIBrakeSettingCw1 || false == brake_setting::is_cut_out(*d->MoverParameters)}; + for( int i = 0; i < BrakeAccTableSize && brakes; ++i ) { fBrake_a0[ i + 1 ] += d->MoverParameters->BrakeForceR( 0.25, velstep*( 1 + 2 * i ) ); fBrake_a1[ i + 1 ] += d->MoverParameters->BrakeForceR( 1.00, velstep*( 1 + 2 * i ) ); } diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index e30635f4f..004e8092c 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -2114,6 +2114,8 @@ void TTrain::OnCommand_trainbrakeoperationtoggle(TTrain *Train, command_data con } vehicle->MoverParameters->Hamulec->SetBrakeStatus(vehicle->MoverParameters->Hamulec->GetBrakeStatus() ^ b_dmg); + // the brake is the player's now, the wagon inspector won't put it back on its own + vehicle->MoverParameters->BrakeCutOutByInspector = false; } } diff --git a/vehicle/brakesetting.cpp b/vehicle/brakesetting.cpp index a55d3d751..bf42f8ab4 100644 --- a/vehicle/brakesetting.cpp +++ b/vehicle/brakesetting.cpp @@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "vehicle/brakesetting.h" +#include "McZapkie/MOVER.h" #include "McZapkie/hamulce.h" #include @@ -48,6 +49,7 @@ struct wagon_counts { int r{0}; // with R: passenger cars of fast trains int g{0}; // with G but no R: freight wagons int p{0}; // the rest: passenger cars with P only + int r_cut_out{0}; // of those with R, the ones with their brake cut out double length{0.0}; // [m] double mass{0.0}; // [kg] @@ -60,6 +62,9 @@ struct wagon_counts { mass += item.mass; if (has(item.delays, bdelay_R)) { ++r; + if (item.cut_out) { + ++r_cut_out; + } } else if (has(item.delays, bdelay_G)) { ++g; @@ -95,9 +100,11 @@ train_setting choose_train_setting(std::vector const &Consist) { return train_setting::light_engine; } if (counts.passenger()) { - // wagons without R run on P, and there may be no more than three of them, fewer than those on R - auto const pwagons{counts.g + counts.p}; - return pwagons <= passenger_r_max_p_wagons && counts.r > pwagons ? train_setting::passenger_r : train_setting::passenger_p; + // wagons without R run on P, and a wagon with its brake cut out counts as one on P (Cw-1 §18.3). there may + // be no more than three of them, fewer than those on R + auto const pwagons{counts.g + counts.p + counts.r_cut_out}; + auto const rwagons{counts.r - counts.r_cut_out}; + return pwagons <= passenger_r_max_p_wagons && rwagons > pwagons ? train_setting::passenger_r : train_setting::passenger_p; } if (counts.length > freight_p_max_length || counts.mass > freight_p_max_mass) { return train_setting::freight_g; @@ -135,18 +142,27 @@ std::vector choose(std::vector const &Consist) { break; } case train_setting::freight_gp: { - // the wagons are counted from the head of the train, past any locomotive in the middle of it + // the wagons are counted from the head of the train, past any locomotive in the middle of it. + // the train runs on P (Cw-1 §19.3b), so §19.9 doesn't apply: a vehicle without G among the ones + // kept on G brakes on P like the rest of the train, rather than being cut out auto const ong{item.powered || wagonsfromhead <= freight_gp_wagons_on_g}; settings.emplace_back(ong ? pick(item.delays, bdelay_G, bdelay_P) : pick(item.delays, bdelay_P, bdelay_G)); break; } case train_setting::freight_g: default: { - // NOTE: Cw-1 §19.9 wants the brake of a vehicle without G (a passenger car with P and R only) cut out in a - // train braked on G. It's put on P instead, as before: a brake cut out by the inspector would have to be - // remembered and put back when the train is made up again, and the simulator has no such record yet. - // The same goes for the vehicles kept on G in the long locomotive setting above (freight_gp) - settings.emplace_back(pick(item.delays, bdelay_G, bdelay_P)); + if (has(item.delays, bdelay_G)) { + settings.emplace_back(bdelay_G); + } + else if (item.powered || item.driven || item.delays == 0) { + // a locomotive, or the vehicle the train is driven from, keeps a working brake: on P. a vehicle + // which tells no settings at all is left alone + settings.emplace_back(pick(item.delays, bdelay_P, bdelay_P)); + } + else { + // a wagon without G (a passenger car with P and R) has its brake cut out (Cw-1 §19.9) + settings.emplace_back(cut_out); + } break; } } @@ -217,4 +233,47 @@ std::vector choose_legacy(std::vector const &Consist) { return settings; } +bool is_cut_out(TMoverParameters const &Vehicle) { + return (Vehicle.Hamulec->GetBrakeStatus() & b_dmg) == b_dmg; +} + +vehicle describe(TMoverParameters const &Vehicle, bool const Driven) { + vehicle described; + described.delays = Vehicle.BrakeDelays; + // a locomotive has Power > 1, though it may be a dead one at the end of the train + described.powered = Vehicle.Power >= 1.0; + described.length = Vehicle.Dim.L; + described.mass = Vehicle.TotalMass; + described.cut_out = is_cut_out(Vehicle) && false == Vehicle.BrakeCutOutByInspector; + described.driven = Driven; + return described; +} + +void apply(TMoverParameters &Vehicle, int const Setting) { + auto &brake{*Vehicle.Hamulec}; + if (Vehicle.BrakeCutOutByInspector && false == is_cut_out(Vehicle)) { + // someone put the brake back in the meantime, it's theirs now + Vehicle.BrakeCutOutByInspector = false; + } + if (Setting == cut_out) { + if (false == is_cut_out(Vehicle)) { + // close the cut-out cock and empty the brake with the releaser, as the scenery's '0' does: the + // distributor, cut off the brake pipe, has no air left to brake with and lets the cylinders go + brake.ForceEmptiness(); + brake.SetBrakeStatus(brake.GetBrakeStatus() | b_dmg); + Vehicle.BrakeCutOutByInspector = true; + } + // a brake cut out already, by the inspector or for a reason of its own, stays as it is + return; + } + if (Vehicle.BrakeCutOutByInspector) { + // open the cut-out cock: the distributor charges from the brake pipe again + brake.SetBrakeStatus(brake.GetBrakeStatus() & ~b_dmg); + Vehicle.BrakeCutOutByInspector = false; + } + if (Setting != 0) { + Vehicle.BrakeDelaySwitch(Setting); + } +} + } // namespace brake_setting diff --git a/vehicle/brakesetting.h b/vehicle/brakesetting.h index cfb421d55..cce2bbeb0 100644 --- a/vehicle/brakesetting.h +++ b/vehicle/brakesetting.h @@ -11,6 +11,8 @@ http://mozilla.org/MPL/2.0/. #include +class TMoverParameters; + // the brake setting (G/P/R/Mg) the virtual wagon inspector (TController::AutoRewident) chooses for every // vehicle of a train, by the rules of the brake instruction Cw-1 (Mw56), §18 and §19 (eu07/maszyna#350) namespace brake_setting { @@ -21,6 +23,8 @@ struct vehicle { bool powered{false}; // a locomotive or a powered unit, as opposed to a wagon double length{0.0}; // [m] double mass{0.0}; // with its load [kg] + bool cut_out{false}; // the brake is cut out for a reason of its own (damage, the scenario, the player) + bool driven{false}; // the vehicle the train is driven from: its brake is never cut out }; // the kind of train the inspector takes the consist for, and how it sets the brakes @@ -36,6 +40,9 @@ enum class train_setting { // how many wagons next to the locomotive stay on G in a freight_gp train inline constexpr int freight_gp_wagons_on_g{5}; +// the setting choose() gives a vehicle whose brake is to be cut out (Cw-1 §19.9) +inline constexpr int cut_out{-1}; + // the train setting by Cw-1. Consist runs from the head of the train to its rear train_setting choose_train_setting(std::vector const &Consist); @@ -47,11 +54,28 @@ constexpr bool passenger(train_setting const Setting) { // the brake setting (bdelay_*) for every vehicle of Consist, in the same order, by Cw-1. a vehicle // without the setting the train needs gets the other one of G and P it has, and 0 (left as it is) -// when it has neither. Cw-1 §19.9 would have the brake of such a vehicle cut out instead +// when it has neither. In a train on G (freight_g) a wagon without G gets cut_out instead: its brake +// is to be cut out (Cw-1 §19.9). A locomotive and the vehicle the train is driven from keep their +// brake working, on P std::vector choose(std::vector const &Consist); // the inspector of the releases before eu07/maszyna#350, kept for ai.brakesetting.cw1 no. the settings // are requests: a vehicle which doesn't have the one asked for keeps its own std::vector choose_legacy(std::vector const &Consist); +// the brake of the vehicle is cut out (b_dmg), whoever did it +bool is_cut_out(TMoverParameters const &Vehicle); + +// what choose() needs to know of the vehicle. a brake the inspector cut out itself doesn't count as +// cut out: the inspector decides about it anew +vehicle describe(TMoverParameters const &Vehicle, bool Driven); + +// the inspector puts the vehicle on Setting, as choose() gives it. cut_out cuts the brake out the way +// the scenery's '0' does: b_dmg, which cuts the distributor off the brake pipe (as the cab's cut-out, +// trainbrakeoperationtoggle, does), and its reservoirs emptied, so the brake releases and stays off. +// The vehicle remembers the inspector did it. Any other setting first puts back a brake the inspector +// cut out (it charges from the brake pipe again), then sets the vehicle to it (0: leaves the setting +// as it is). A brake cut out by anyone else stays cut out +void apply(TMoverParameters &Vehicle, int Setting); + } // namespace brake_setting From 7db67c3ce71caf1c8e5e3ff34992750cb856fbaf Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 18:03:13 +0100 Subject: [PATCH 4/6] fix: the inspector cuts brakes out only in a train at a standstill, R+Mg keeps the rapid stage (review of #282) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - §19.9 cut-outs only when the AI runs the consist as a train (Obey_train); in shunting a wagon without G brakes on P, which also puts back a brake the inspector cut out before - no new cut-out while the consist moves: left to the next inspection at a standstill (putting back still happens) - ESt4R, LSt and EStED test the bit of R, so R+Mg keeps the high-speed stage of R as on the KE and Oerlikon valves --- McZapkie/hamulce.cpp | 6 +- tests/unit/test_brake_setting.cpp | 141 +++++++++++++++++++++++++----- vehicle/Driver.cpp | 10 ++- vehicle/brakesetting.cpp | 14 +-- vehicle/brakesetting.h | 16 ++-- 5 files changed, 148 insertions(+), 39 deletions(-) diff --git a/McZapkie/hamulce.cpp b/McZapkie/hamulce.cpp index c0a7d2a0e..b4441eac0 100644 --- a/McZapkie/hamulce.cpp +++ b/McZapkie/hamulce.cpp @@ -1449,7 +1449,7 @@ double TESt4R::GetPF(double const PP, double const dt, double const Vel) result = dv - dV1; - RapidStatus = BrakeDelayFlag == bdelay_R && ((Vel > 55 && RapidStatus == true) || Vel > 70); + RapidStatus = (BrakeDelayFlag & bdelay_R) == bdelay_R && ((Vel > 55 && RapidStatus == true) || Vel > 70); RapidTemp = RapidTemp + (0.9 * int(RapidStatus) - RapidTemp) * dt / 2; temp = 1.9 - RapidTemp; @@ -1752,7 +1752,7 @@ double TLSt::GetPF(double const PP, double const dt, double const Vel) // if Vel>55 then temp:=0.72 else // temp:=1;{R} // cisnienie PP - RapidTemp = RapidTemp + (RM * int(Vel > 55 && BrakeDelayFlag == bdelay_R) - RapidTemp) * dt / 2; + RapidTemp = RapidTemp + (RM * int(Vel > 55 && (BrakeDelayFlag & bdelay_R) == bdelay_R) - RapidTemp) * dt / 2; temp = 1 - RapidTemp; if (EDFlag > 0.2) temp = 10000; @@ -1971,7 +1971,7 @@ double TEStED::GetPF(double const PP, double const dt, double const Vel) dv = dv + PF(MPP, VVP, 0.02); Miedzypoj->Flow(dv * dt * 0.15); - RapidTemp = RapidTemp + (RM * int(Vel > RV && BrakeDelayFlag == bdelay_R) - RapidTemp) * dt / 2; + RapidTemp = RapidTemp + (RM * int(Vel > RV && (BrakeDelayFlag & bdelay_R) == bdelay_R) - RapidTemp) * dt / 2; temp = std::max(1 - RapidTemp, 0.001); // if EDFlag then temp:=1000; // temp:=temp/(1-); diff --git a/tests/unit/test_brake_setting.cpp b/tests/unit/test_brake_setting.cpp index aba1e80bb..2d5b02a41 100644 --- a/tests/unit/test_brake_setting.cpp +++ b/tests/unit/test_brake_setting.cpp @@ -18,6 +18,13 @@ constexpr int P{bdelay_P}; constexpr int R{bdelay_R}; constexpr int M{bdelay_M}; +// how the AI runs the consist the inspector looks at: as a train (Obey_train), or shunting it +constexpr bool as_train{true}; +constexpr bool shunting{false}; +// whether the train stands, as AutoRewident() tells the inspector +constexpr bool standstill{true}; +constexpr bool moving{false}; + brake_setting::vehicle locomotive(int const Delays = G | P | R) { return {Delays, true, 17.0, 80000.0}; } @@ -51,7 +58,7 @@ TEST_CASE("brake setting: the locomotives don't count into the length and mass o auto consist{train(locomotive(), 20, freight_wagon(15.0, 30.0))}; consist.insert(consist.begin(), locomotive()); CHECK(brake_setting::choose_train_setting(consist) == train_setting::freight_p); - auto const settings{brake_setting::choose(consist)}; + auto const settings{brake_setting::choose(consist, as_train)}; CHECK(settings[0] == G); CHECK(settings[1] == G); for (std::size_t i = 2; i < settings.size(); ++i) { @@ -66,7 +73,7 @@ TEST_CASE("brake setting: the locomotives don't count into the length and mass o TEST_CASE("brake setting: the locomotive and the first five wagons on G, the rest on P (Cw-1 §19.3b)") { auto const consist{train(locomotive(), 12, freight_wagon(15.0, 60.0))}; REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_gp); - auto const settings{brake_setting::choose(consist)}; + auto const settings{brake_setting::choose(consist, as_train)}; REQUIRE(settings.size() == consist.size()); for (std::size_t i = 0; i < settings.size(); ++i) { CAPTURE(i); @@ -79,7 +86,7 @@ TEST_CASE("brake setting: a locomotive in the middle of the train doesn't put fi consist.emplace_back(locomotive()); consist.insert(consist.end(), 8, freight_wagon(15.0, 60.0)); REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_gp); - auto const settings{brake_setting::choose(consist)}; + auto const settings{brake_setting::choose(consist, as_train)}; CHECK(settings[1] == G); CHECK(settings[5] == G); CHECK(settings[6] == P); @@ -98,7 +105,7 @@ TEST_CASE("brake setting: a heavy freight train runs on G, a wagon without G has consist.emplace_back(passenger_car(P)); consist.emplace_back(passenger_car()); REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_g); - auto const settings{brake_setting::choose(consist)}; + auto const settings{brake_setting::choose(consist, as_train)}; for (std::size_t i = 0; i + 2 < settings.size(); ++i) { CAPTURE(i); CHECK(settings[i] == G); @@ -118,7 +125,7 @@ TEST_CASE("brake setting: in a train on G a locomotive and the vehicle it's driv consist.emplace_back(locomotive(P | R)); consist.emplace_back(0, false, 20.0, 40000.0); REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_g); - auto settings{brake_setting::choose(consist)}; + auto settings{brake_setting::choose(consist, as_train)}; CHECK(settings.front() == P); CHECK(settings[settings.size() - 2] == P); CHECK(settings.back() == 0); @@ -128,7 +135,7 @@ TEST_CASE("brake setting: in a train on G a locomotive and the vehicle it's driv controlcar.driven = true; consist.front() = controlcar; consist.back() = locomotive(); - settings = brake_setting::choose(consist); + settings = brake_setting::choose(consist, as_train); CHECK(settings.front() == P); CHECK(settings.back() == G); } @@ -140,7 +147,7 @@ TEST_CASE("brake setting: in the long locomotive setting a vehicle without G bra consist.insert(consist.end(), 8, freight_wagon(15.0, 60.0)); consist.emplace_back(passenger_car()); REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_gp); - auto const settings{brake_setting::choose(consist)}; + auto const settings{brake_setting::choose(consist, as_train)}; CHECK(settings[2] == G); CHECK(settings[3] == P); CHECK(settings[4] == G); @@ -148,6 +155,23 @@ TEST_CASE("brake setting: in the long locomotive setting a vehicle without G bra CHECK(settings.back() == P); } +TEST_CASE("brake setting: a heavy consist being shunted has nothing cut out, a wagon without G brakes on P") { + // §19.9 is about trains: the same consist the AI shunts rather than runs as a train + auto consist{train(locomotive(), 30, freight_wagon(15.0, 80.0))}; + consist.emplace_back(passenger_car(P)); + consist.emplace_back(passenger_car()); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_g); + auto const settings{brake_setting::choose(consist, shunting)}; + for (std::size_t i = 0; i + 2 < settings.size(); ++i) { + CAPTURE(i); + CHECK(settings[i] == G); + } + CHECK(settings[settings.size() - 2] == P); + CHECK(settings.back() == P); + // as a train they're cut out + CHECK(brake_setting::choose(consist, as_train).back() == brake_setting::cut_out); +} + TEST_CASE("brake setting: a car with its brake cut out counts as one on P in a passenger train (Cw-1 §18.3)") { auto consist{train(locomotive(), 5, passenger_car())}; consist.insert(consist.end(), 3, passenger_car(P)); @@ -180,12 +204,13 @@ void charge_and_apply(test::vehicle &Vehicle) { Vehicle.run(20.0); } -// the inspector at work on a heavy train on G with the vehicle at its end, as AutoRewident() does it -int inspect_heavy_train(test::vehicle &Vehicle) { +// the inspector at work on a heavy train on G with the vehicle at its end, as AutoRewident() does it, the AI +// running it as a train or shunting it, standing or on the run +int inspect_heavy_train(test::vehicle &Vehicle, bool const Train = as_train, bool const Standstill = standstill) { auto consist{train(locomotive(), 30, freight_wagon(15.0, 80.0))}; consist.emplace_back(brake_setting::describe(Vehicle.mover(), false)); - auto const setting{brake_setting::choose(consist).back()}; - brake_setting::apply(Vehicle.mover(), setting); + auto const setting{brake_setting::choose(consist, Train).back()}; + brake_setting::apply(Vehicle.mover(), setting, Standstill); return setting; } @@ -193,8 +218,8 @@ int inspect_heavy_train(test::vehicle &Vehicle) { int inspect_light_train(test::vehicle &Vehicle) { auto consist{train(locomotive(), 4, freight_wagon())}; consist.emplace_back(brake_setting::describe(Vehicle.mover(), false)); - auto const setting{brake_setting::choose(consist).back()}; - brake_setting::apply(Vehicle.mover(), setting); + auto const setting{brake_setting::choose(consist, as_train).back()}; + brake_setting::apply(Vehicle.mover(), setting, standstill); return setting; } @@ -269,22 +294,96 @@ TEST_CASE("brake setting: the inspector never puts back a brake cut out for anot CHECK(brake_setting::is_cut_out(other.mover())); } +TEST_CASE("brake setting: shunting puts back a brake the inspector cut out, and cuts none out") { + test::vehicle car{passenger_car_definition()}; + charge_and_apply(car); + // shunted in a heavy consist: nothing is cut out, the car is on P and brakes + CHECK(inspect_heavy_train(car, shunting) == P); + CHECK(false == brake_setting::is_cut_out(car.mover())); + CHECK(car->BrakeDelayFlag == P); + CHECK(car->BrakePress > 1.0); + + // cut out when the consist became a train, then shunted again (e.g. left in a yard): the brake is back + REQUIRE(inspect_heavy_train(car) == brake_setting::cut_out); + REQUIRE(car->BrakeCutOutByInspector); + CHECK(inspect_heavy_train(car, shunting) == P); + CHECK(false == brake_setting::is_cut_out(car.mover())); + CHECK(false == car->BrakeCutOutByInspector); + car.hold_brake_pipe(5.0); + car.run(300.0); + charge_and_apply(car); + CHECK(car->BrakePress > 1.0); + + // a brake cut out for a reason of its own stays cut out in shunting too + test::vehicle other{passenger_car_definition()}; + other->Hamulec->SetBrakeStatus(other->Hamulec->GetBrakeStatus() | b_dmg); + CHECK(inspect_heavy_train(other, shunting) == P); + CHECK(brake_setting::is_cut_out(other.mover())); +} + +TEST_CASE("brake setting: the inspector doesn't cut a brake out on the run, it waits for a standstill") { + test::vehicle car{passenger_car_definition()}; + charge_and_apply(car); + REQUIRE(car->BrakePress > 1.0); + // the train checked on the run (the order changed mid-run): the car keeps its brake and its setting + auto const setting{car->BrakeDelayFlag}; + CHECK(inspect_heavy_train(car, as_train, moving) == brake_setting::cut_out); + CHECK(false == brake_setting::is_cut_out(car.mover())); + CHECK(false == car->BrakeCutOutByInspector); + CHECK(car->BrakeDelayFlag == setting); + car.run(5.0); + CHECK(car->BrakePress > 1.0); + // the next check, standing, cuts it out + CHECK(inspect_heavy_train(car) == brake_setting::cut_out); + CHECK(brake_setting::is_cut_out(car.mover())); + CHECK(car->BrakeCutOutByInspector); + // a brake cut out already stays cut out on the run, and one put back on the run is back + CHECK(inspect_heavy_train(car, as_train, moving) == brake_setting::cut_out); + CHECK(car->BrakeCutOutByInspector); + CHECK(inspect_heavy_train(car, shunting, moving) == P); + CHECK(false == brake_setting::is_cut_out(car.mover())); + CHECK(false == car->BrakeCutOutByInspector); +} + +TEST_CASE("brake setting: R+Mg brakes as hard as R at speed, the rapid stage of an LSt kept") { + // the inspector sets R+Mg (Cw-1 §18.2), the bits of R and Mg together: the valves test the bit of R, not + // the whole setting, so the high stage of R at speed stays on with Mg. the EU07's LSt given an Mg here + auto const definition{test::definition::fiz("4e_v1/4e").set("Cntrl", "BrakeDelays", "GPR+Mg")}; + auto const service = [&](int const Setting) { + test::reseed_random(); + test::vehicle real{definition, 160.0}; + real->DecManualBrakeLevel(ManualBrakePosNo); + real->BrakeDelaySwitch(Setting); // false when already set + REQUIRE(real->BrakeDelayFlag == Setting); + real.hold_brake_pipe(5.0); + real.run(60.0); + real.hold_brake_pipe(3.5); + real.run(10.0); + REQUIRE(real->Vel > 100.0); + return real->BrakePress; + }; + auto const rapid{service(R)}; + CHECK(service(R | M) == doctest::Approx(rapid).epsilon(0.01)); + // and P has no high stage + CHECK(service(P) < rapid - 1.0); +} + TEST_CASE("brake setting: a vehicle without the setting the train needs gets the other one of G and P") { // a light freight train with a wagon which has G only auto consist{train(locomotive(), 4, freight_wagon(15.0, 20.0))}; consist.emplace_back(G, false, 15.0, 20000.0); REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_p); - CHECK(brake_setting::choose(consist).back() == G); + CHECK(brake_setting::choose(consist, as_train).back() == G); // and a vehicle with neither is left alone consist.emplace_back(R, false, 15.0, 20000.0); - CHECK(brake_setting::choose(consist).back() == 0); + CHECK(brake_setting::choose(consist, as_train).back() == 0); } TEST_CASE("brake setting: a passenger train on R carries at most three cars on P, fewer than those on R (Cw-1 §18.3)") { auto consist{train(locomotive(), 4, passenger_car())}; consist.insert(consist.end(), 3, passenger_car(P)); CHECK(brake_setting::choose_train_setting(consist) == train_setting::passenger_r); - auto const settings{brake_setting::choose(consist)}; + auto const settings{brake_setting::choose(consist, as_train)}; CHECK(settings[0] == R); CHECK(settings[1] == R); CHECK(settings.back() == P); @@ -292,7 +391,7 @@ TEST_CASE("brake setting: a passenger train on R carries at most three cars on P // as many cars on P as on R: all on P consist.emplace_back(passenger_car(P)); CHECK(brake_setting::choose_train_setting(consist) == train_setting::passenger_p); - for (auto const setting : brake_setting::choose(consist)) { + for (auto const setting : brake_setting::choose(consist, as_train)) { CHECK(setting == P); } // the inspector of the earlier releases kept such a train on R @@ -307,14 +406,14 @@ TEST_CASE("brake setting: freight wagons in a passenger train on R count as cars auto consist{train(locomotive(), 6, passenger_car())}; consist.emplace_back(freight_wagon()); REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::passenger_r); - CHECK(brake_setting::choose(consist).back() == P); + CHECK(brake_setting::choose(consist, as_train).back() == P); } TEST_CASE("brake setting: cars with a magnetic rail brake run on R+Mg in a train on R (Cw-1 §18.2)") { auto consist{train(locomotive(G | P | R | M), 3, passenger_car(P | R | M))}; consist.emplace_back(passenger_car()); REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::passenger_r); - auto const settings{brake_setting::choose(consist)}; + auto const settings{brake_setting::choose(consist, as_train)}; CHECK(settings[0] == R); // the locomotive stays on R CHECK(settings[1] == (R | M)); CHECK(settings.back() == R); @@ -323,7 +422,7 @@ TEST_CASE("brake setting: cars with a magnetic rail brake run on R+Mg in a train TEST_CASE("brake setting: a light engine runs on R where it has it") { std::vector const consist{locomotive(), locomotive(G | P)}; CHECK(brake_setting::choose_train_setting(consist) == train_setting::light_engine); - auto const settings{brake_setting::choose(consist)}; + auto const settings{brake_setting::choose(consist, as_train)}; CHECK(settings[0] == R); CHECK(settings[1] == P); } @@ -331,7 +430,7 @@ TEST_CASE("brake setting: a light engine runs on R where it has it") { TEST_CASE("brake setting: the inspector of the earlier releases counted the locomotives into the train's mass") { // 300 m and 600 t of wagons: P by Cw-1, the long locomotive setting before auto const consist{train(locomotive(), 20, freight_wagon(15.0, 30.0))}; - CHECK(brake_setting::choose(consist)[1] == P); + CHECK(brake_setting::choose(consist, as_train)[1] == P); CHECK(brake_setting::choose_legacy(consist)[1] == G); CHECK(brake_setting::choose_legacy(consist)[6] == P); } diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 0bb569c81..35856c412 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2086,8 +2086,12 @@ void TController::AutoRewident() for (auto const *d = pVehicles[0]; d != nullptr; d = d->Next()) { consist.emplace_back(brake_setting::describe(*d->MoverParameters, d == pVehicle)); } - // 2. Określenie typu pociągu i nastaw: by the brake instruction Cw-1 (eu07/maszyna#350), or as the releases before it did - auto const settings{Global.AIBrakeSettingCw1 ? brake_setting::choose(consist) : brake_setting::choose_legacy(consist)}; + // 2. Określenie typu pociągu i nastaw: by the brake instruction Cw-1 (eu07/maszyna#350), or as the releases before it did. + // brakes are cut out (Cw-1 §19.9) only in a train, not in a consist being shunted + auto const train{(OrderCurrentGet() & Obey_train) != 0}; + auto const settings{Global.AIBrakeSettingCw1 ? brake_setting::choose(consist, train) : brake_setting::choose_legacy(consist)}; + // and only at a standstill: a check on the run (the order changed mid-run) leaves them to the next one + auto const standstill{false == (mvOccupied->Vel > EU07_AI_NOMOVEMENT)}; // a light engine counts as a passenger train for the braking below auto const passengertrain{brake_setting::passenger(brake_setting::choose_train_setting(consist))}; // 3. Nastawianie @@ -2098,7 +2102,7 @@ void TController::AutoRewident() continue; } // the setting, a brake cut out in a train on G (Cw-1 §19.9) or put back when the train no longer needs it cut out - brake_setting::apply(*d->MoverParameters, *setting); + brake_setting::apply(*d->MoverParameters, *setting, standstill); // potentially release manual brake d->MoverParameters->DecManualBrakeLevel( ManualBrakePosNo ); d->MoverParameters->SpringBrake.Activate = false; diff --git a/vehicle/brakesetting.cpp b/vehicle/brakesetting.cpp index bf42f8ab4..bafe8a95b 100644 --- a/vehicle/brakesetting.cpp +++ b/vehicle/brakesetting.cpp @@ -115,7 +115,7 @@ train_setting choose_train_setting(std::vector const &Consist) { return train_setting::freight_p; } -std::vector choose(std::vector const &Consist) { +std::vector choose(std::vector const &Consist, bool const Train) { auto const setting{choose_train_setting(Consist)}; std::vector settings; settings.reserve(Consist.size()); @@ -154,9 +154,10 @@ std::vector choose(std::vector const &Consist) { if (has(item.delays, bdelay_G)) { settings.emplace_back(bdelay_G); } - else if (item.powered || item.driven || item.delays == 0) { + else if (item.powered || item.driven || item.delays == 0 || false == Train) { // a locomotive, or the vehicle the train is driven from, keeps a working brake: on P. a vehicle - // which tells no settings at all is left alone + // which tells no settings at all is left alone. a consist being shunted isn't a train and §19.9 + // doesn't apply to it: its wagons keep their brakes, on P settings.emplace_back(pick(item.delays, bdelay_P, bdelay_P)); } else { @@ -249,21 +250,22 @@ vehicle describe(TMoverParameters const &Vehicle, bool const Driven) { return described; } -void apply(TMoverParameters &Vehicle, int const Setting) { +void apply(TMoverParameters &Vehicle, int const Setting, bool const Standstill) { auto &brake{*Vehicle.Hamulec}; if (Vehicle.BrakeCutOutByInspector && false == is_cut_out(Vehicle)) { // someone put the brake back in the meantime, it's theirs now Vehicle.BrakeCutOutByInspector = false; } if (Setting == cut_out) { - if (false == is_cut_out(Vehicle)) { + if (false == is_cut_out(Vehicle) && Standstill) { // close the cut-out cock and empty the brake with the releaser, as the scenery's '0' does: the // distributor, cut off the brake pipe, has no air left to brake with and lets the cylinders go brake.ForceEmptiness(); brake.SetBrakeStatus(brake.GetBrakeStatus() | b_dmg); Vehicle.BrakeCutOutByInspector = true; } - // a brake cut out already, by the inspector or for a reason of its own, stays as it is + // a brake cut out already, by the inspector or for a reason of its own, stays as it is. on the run the + // vehicle is left alone, brake and setting, until the inspector looks at the train standing return; } if (Vehicle.BrakeCutOutByInspector) { diff --git a/vehicle/brakesetting.h b/vehicle/brakesetting.h index cce2bbeb0..789f52808 100644 --- a/vehicle/brakesetting.h +++ b/vehicle/brakesetting.h @@ -56,8 +56,10 @@ constexpr bool passenger(train_setting const Setting) { // without the setting the train needs gets the other one of G and P it has, and 0 (left as it is) // when it has neither. In a train on G (freight_g) a wagon without G gets cut_out instead: its brake // is to be cut out (Cw-1 §19.9). A locomotive and the vehicle the train is driven from keep their -// brake working, on P -std::vector choose(std::vector const &Consist); +// brake working, on P. Train tells the AI runs the consist as a train (Obey_train): §19.9 is about +// trains, so in shunting (or banking) nothing is cut out and a wagon without G brakes on P, which puts +// back a brake the inspector cut out before +std::vector choose(std::vector const &Consist, bool Train); // the inspector of the releases before eu07/maszyna#350, kept for ai.brakesetting.cw1 no. the settings // are requests: a vehicle which doesn't have the one asked for keeps its own @@ -73,9 +75,11 @@ vehicle describe(TMoverParameters const &Vehicle, bool Driven); // the inspector puts the vehicle on Setting, as choose() gives it. cut_out cuts the brake out the way // the scenery's '0' does: b_dmg, which cuts the distributor off the brake pipe (as the cab's cut-out, // trainbrakeoperationtoggle, does), and its reservoirs emptied, so the brake releases and stays off. -// The vehicle remembers the inspector did it. Any other setting first puts back a brake the inspector -// cut out (it charges from the brake pipe again), then sets the vehicle to it (0: leaves the setting -// as it is). A brake cut out by anyone else stays cut out -void apply(TMoverParameters &Vehicle, int Setting); +// The vehicle remembers the inspector did it. A brake is cut out only with the train at a standstill +// (Standstill): on the run the vehicle is left as it is until the inspector looks at the train again +// standing, rather than losing its brake while moving. Any other setting first puts back a brake the +// inspector cut out (it charges from the brake pipe again), then sets the vehicle to it (0: leaves the +// setting as it is). A brake cut out by anyone else stays cut out +void apply(TMoverParameters &Vehicle, int Setting, bool Standstill); } // namespace brake_setting From 64d1b7f8474e68f33cebf04759a2b94c82d27c69 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 21:38:16 +0100 Subject: [PATCH 5/6] fix: the inspector takes a train below 1 km/h for standing and a bank run for a train (review of #282) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The AI inspects the train while it still creeps to a stop (W4, Shunt to Obey_train) and as it couples, so the 0.05 km/h threshold missed the §19.9 cut-out; it now uses EU07_AI_MOVEMENT. Bank counts as a train, as in the braking tables. --- tests/unit/test_brake_setting.cpp | 48 ++++++++++++++++++++++++++++++- vehicle/Driver.cpp | 9 +++--- vehicle/brakesetting.cpp | 14 +++++++-- vehicle/brakesetting.h | 16 +++++++++-- 4 files changed, 77 insertions(+), 10 deletions(-) diff --git a/tests/unit/test_brake_setting.cpp b/tests/unit/test_brake_setting.cpp index 2d5b02a41..93fb54a76 100644 --- a/tests/unit/test_brake_setting.cpp +++ b/tests/unit/test_brake_setting.cpp @@ -5,6 +5,7 @@ #include "McZapkie/hamulce.h" #include "vehicle.h" #include "vehicle/brakesetting.h" +#include "vehicle/Driver.h" // the brake settings the AI wagon inspector (TController::AutoRewident) chooses for a train, by the // brake instruction Cw-1 (Mw56) §18 and §19 (eu07/maszyna#350) @@ -18,7 +19,7 @@ constexpr int P{bdelay_P}; constexpr int R{bdelay_R}; constexpr int M{bdelay_M}; -// how the AI runs the consist the inspector looks at: as a train (Obey_train), or shunting it +// how the AI runs the consist the inspector looks at: as a train (Obey_train or Bank), or shunting it constexpr bool as_train{true}; constexpr bool shunting{false}; // whether the train stands, as AutoRewident() tells the inspector @@ -345,6 +346,51 @@ TEST_CASE("brake setting: the inspector doesn't cut a brake out on the run, it w CHECK(false == car->BrakeCutOutByInspector); } +TEST_CASE("brake setting: a train creeping to a stop or coupling counts as standing, one on the run doesn't") { + // the AI looks at the train at a W4 and when it switches from shunting to the train run below 1 km/h, and as it + // couples at the coupling speed: the inspector takes those for a standstill + CHECK(brake_setting::at_standstill(0.0)); + CHECK(brake_setting::at_standstill(0.04)); + CHECK(brake_setting::at_standstill(0.9)); + CHECK(brake_setting::at_standstill(-0.9)); + CHECK(false == brake_setting::at_standstill(EU07_AI_MOVEMENT)); + CHECK(false == brake_setting::at_standstill(30.0)); + + // checked at 0.9 km/h: the brake is cut out + test::vehicle creeping{passenger_car_definition()}; + charge_and_apply(creeping); + CHECK(inspect_heavy_train(creeping, as_train, brake_setting::at_standstill(0.9)) == brake_setting::cut_out); + CHECK(brake_setting::is_cut_out(creeping.mover())); + CHECK(creeping->BrakeCutOutByInspector); + // checked at 30 km/h: the car keeps its brake + test::vehicle running{passenger_car_definition()}; + charge_and_apply(running); + CHECK(inspect_heavy_train(running, as_train, brake_setting::at_standstill(30.0)) == brake_setting::cut_out); + CHECK(false == brake_setting::is_cut_out(running.mover())); + CHECK(false == running->BrakeCutOutByInspector); + running.run(5.0); + CHECK(running->BrakePress > 1.0); +} + +TEST_CASE("brake setting: a bank engine's run counts as a train, shunting doesn't") { + CHECK(brake_setting::runs_as_train(Obey_train)); + CHECK(brake_setting::runs_as_train(Bank)); + CHECK(brake_setting::runs_as_train(Obey_train | Connect)); + CHECK(false == brake_setting::runs_as_train(Shunt)); + CHECK(false == brake_setting::runs_as_train(Loose_shunt)); + CHECK(false == brake_setting::runs_as_train(Connect)); + CHECK(false == brake_setting::runs_as_train(Wait_for_orders)); + + // a heavy consist on G banked: the wagon without G has its brake cut out, as in a train + test::vehicle car{passenger_car_definition()}; + charge_and_apply(car); + CHECK(inspect_heavy_train(car, brake_setting::runs_as_train(Bank)) == brake_setting::cut_out); + CHECK(brake_setting::is_cut_out(car.mover())); + // shunted: it's back on P + CHECK(inspect_heavy_train(car, brake_setting::runs_as_train(Shunt)) == P); + CHECK(false == brake_setting::is_cut_out(car.mover())); +} + TEST_CASE("brake setting: R+Mg brakes as hard as R at speed, the rapid stage of an LSt kept") { // the inspector sets R+Mg (Cw-1 §18.2), the bits of R and Mg together: the valves test the bit of R, not // the whole setting, so the high stage of R at speed stays on with Mg. the EU07's LSt given an Mg here diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index b4f790768..0099c799c 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2167,11 +2167,12 @@ void TController::AutoRewident() consist.emplace_back(brake_setting::describe(*d->MoverParameters, d == pVehicle)); } // 2. Określenie typu pociągu i nastaw: by the brake instruction Cw-1 (eu07/maszyna#350), or as the releases before it did. - // brakes are cut out (Cw-1 §19.9) only in a train, not in a consist being shunted - auto const train{(OrderCurrentGet() & Obey_train) != 0}; + // brakes are cut out (Cw-1 §19.9) only in a train (a bank engine's run included), not in a consist being shunted + auto const train{brake_setting::runs_as_train(OrderCurrentGet())}; auto const settings{Global.AIBrakeSettingCw1 ? brake_setting::choose(consist, train) : brake_setting::choose_legacy(consist)}; - // and only at a standstill: a check on the run (the order changed mid-run) leaves them to the next one - auto const standstill{false == (mvOccupied->Vel > EU07_AI_NOMOVEMENT)}; + // and only at a standstill, the train creeping to a stop or coupling included: a check on the run (the order changed + // mid-run) leaves them to the next one + auto const standstill{brake_setting::at_standstill(mvOccupied->Vel)}; // a light engine counts as a passenger train for the braking below auto const passengertrain{brake_setting::passenger(brake_setting::choose_train_setting(consist))}; // 3. Nastawianie diff --git a/vehicle/brakesetting.cpp b/vehicle/brakesetting.cpp index bafe8a95b..f27c136ca 100644 --- a/vehicle/brakesetting.cpp +++ b/vehicle/brakesetting.cpp @@ -12,8 +12,10 @@ http://mozilla.org/MPL/2.0/. #include "McZapkie/MOVER.h" #include "McZapkie/hamulce.h" +#include "vehicle/Driver.h" #include +#include namespace brake_setting { @@ -156,8 +158,8 @@ std::vector choose(std::vector const &Consist, bool const Train) { } else if (item.powered || item.driven || item.delays == 0 || false == Train) { // a locomotive, or the vehicle the train is driven from, keeps a working brake: on P. a vehicle - // which tells no settings at all is left alone. a consist being shunted isn't a train and §19.9 - // doesn't apply to it: its wagons keep their brakes, on P + // which tells no settings at all is left alone. a consist being shunted isn't a train (a bank + // engine's run is) and §19.9 doesn't apply to it: its wagons keep their brakes, on P settings.emplace_back(pick(item.delays, bdelay_P, bdelay_P)); } else { @@ -171,6 +173,14 @@ std::vector choose(std::vector const &Consist, bool const Train) { return settings; } +bool runs_as_train(int const Orders) { + return (Orders & (Obey_train | Bank)) != 0; +} + +bool at_standstill(double const Velocity) { + return std::abs(Velocity) < EU07_AI_MOVEMENT; +} + std::vector choose_legacy(std::vector const &Consist) { wagon_counts const counts{Consist}; auto length{0.0}; diff --git a/vehicle/brakesetting.h b/vehicle/brakesetting.h index 789f52808..b555850fb 100644 --- a/vehicle/brakesetting.h +++ b/vehicle/brakesetting.h @@ -56,11 +56,21 @@ constexpr bool passenger(train_setting const Setting) { // without the setting the train needs gets the other one of G and P it has, and 0 (left as it is) // when it has neither. In a train on G (freight_g) a wagon without G gets cut_out instead: its brake // is to be cut out (Cw-1 §19.9). A locomotive and the vehicle the train is driven from keep their -// brake working, on P. Train tells the AI runs the consist as a train (Obey_train): §19.9 is about -// trains, so in shunting (or banking) nothing is cut out and a wagon without G brakes on P, which puts -// back a brake the inspector cut out before +// brake working, on P. Train tells the AI runs the consist as a train (runs_as_train()): §19.9 is about +// trains, so in shunting nothing is cut out and a wagon without G brakes on P, which puts back a brake +// the inspector cut out before std::vector choose(std::vector const &Consist, bool Train); +// the AI runs the consist as a train, rather than shunting it: Obey_train, or Bank, which it drives as a +// train run too (with the braking of a train). Orders is the order of the AI (TOrders) +bool runs_as_train(int Orders); + +// the train stands for the inspector: slower than the AI takes for a deliberate movement (EU07_AI_MOVEMENT). +// the AI looks at the train while it still creeps to a stop (at a W4, or switching from shunting to the train +// run) and as it couples (at the coupling speed), so a stricter threshold would miss those checks; a train +// really on the run (an order changed mid-run) stays above it +bool at_standstill(double Velocity); + // the inspector of the releases before eu07/maszyna#350, kept for ai.brakesetting.cw1 no. the settings // are requests: a vehicle which doesn't have the one asked for keeps its own std::vector choose_legacy(std::vector const &Consist); From 9043d2d5fcb2854b67a1b8ecf5f99328b5bf9ef9 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 11 Nov 2026 08:17:06 +0100 Subject: [PATCH 6/6] fix(ai): a brake cut-out skipped on the run is done once the train stands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A train formed by coupling is inspected at the first buffer contact, above the speed the inspector takes for a standstill, so its wagons without G kept their brakes (Cw-1 §19.9) until some later stop, if any. brake_setting::apply() now tells when it left a cut-out because the train was moving; AutoRewident keeps that as a pending re-inspection and TController::reinspect_brakes() runs it again once the train is below EU07_AI_MOVEMENT. The flag is dropped when the order stops being a train run. The legacy inspector (ai.brakesetting.cw1 no) never cuts a brake out and is unchanged. --- tests/unit/test_brake_setting.cpp | 89 +++++++++++++++++++++++++++++-- vehicle/Driver.cpp | 32 +++++++++-- vehicle/Driver.h | 2 + vehicle/brakesetting.cpp | 26 +++++---- vehicle/brakesetting.h | 12 +++-- 5 files changed, 141 insertions(+), 20 deletions(-) diff --git a/tests/unit/test_brake_setting.cpp b/tests/unit/test_brake_setting.cpp index 93fb54a76..5034bf3c5 100644 --- a/tests/unit/test_brake_setting.cpp +++ b/tests/unit/test_brake_setting.cpp @@ -224,6 +224,24 @@ int inspect_light_train(test::vehicle &Vehicle) { return setting; } +// the inspector at work on a whole heavy train on G, the vehicles at its end, the way AutoRewident() goes through +// the train. tells whether it left a cut-out for the next standstill, as AutoRewident() notes for reinspect_brakes() +bool inspection_pending(std::vector const &Vehicles, bool const Train, bool const Standstill) { + auto consist{train(locomotive(), 30, freight_wagon(15.0, 80.0))}; + for (auto *vehicle : Vehicles) { + consist.emplace_back(brake_setting::describe(vehicle->mover(), false)); + } + auto const settings{brake_setting::choose(consist, Train)}; + auto setting{settings.end() - static_cast(Vehicles.size())}; + auto pending{false}; + for (auto *vehicle : Vehicles) { + if (brake_setting::apply(vehicle->mover(), *setting++, Standstill)) { + pending = true; + } + } + return pending; +} + } // namespace TEST_CASE("brake setting: the inspector cuts the brake out in a train on G and puts it back in a train on P") { @@ -346,9 +364,74 @@ TEST_CASE("brake setting: the inspector doesn't cut a brake out on the run, it w CHECK(false == car->BrakeCutOutByInspector); } -TEST_CASE("brake setting: a train creeping to a stop or coupling counts as standing, one on the run doesn't") { - // the AI looks at the train at a W4 and when it switches from shunting to the train run below 1 km/h, and as it - // couples at the coupling speed: the inspector takes those for a standstill +TEST_CASE("brake setting: a cut-out skipped on the run is reported, and done at the next inspection standing") { + // a train formed by coupling: the AI looks at it as the buffers meet, above 1 km/h. two cars without G and + // a freight wagon at the end of a heavy train on G + test::vehicle first{passenger_car_definition()}; + test::vehicle second{passenger_car_definition()}; + test::vehicle wagon{test::definition::load("wagon").set("Cntrl", "BrakeDelays", "GP")}; + std::vector const vehicles{&first, &second, &wagon}; + for (auto *vehicle : vehicles) { + charge_and_apply(*vehicle); + } + auto const setting{first->BrakeDelayFlag}; + // moving: nothing is cut out, and the inspector tells it has to come back + CHECK(inspection_pending(vehicles, as_train, brake_setting::at_standstill(3.0))); + for (auto *vehicle : vehicles) { + CHECK(false == brake_setting::is_cut_out(vehicle->mover())); + CHECK(false == vehicle->mover().BrakeCutOutByInspector); + } + CHECK(first->BrakeDelayFlag == setting); + CHECK(wagon->BrakeDelayFlag == G); + // still moving: still pending + CHECK(inspection_pending(vehicles, as_train, moving)); + // the train stands (its brake pipe charging after the coupling): the cars get cut out, nothing is left + CHECK(false == inspection_pending(vehicles, as_train, brake_setting::at_standstill(0.0))); + CHECK(brake_setting::is_cut_out(first.mover())); + CHECK(first->BrakeCutOutByInspector); + CHECK(brake_setting::is_cut_out(second.mover())); + CHECK(second->BrakeCutOutByInspector); + CHECK(false == brake_setting::is_cut_out(wagon.mover())); + // and nothing is pending when the train moves off + CHECK(false == inspection_pending(vehicles, as_train, moving)); + CHECK(brake_setting::is_cut_out(first.mover())); +} + +TEST_CASE("brake setting: an inspection on the run with nothing to cut out leaves nothing pending") { + // a wagon with G in a heavy train on G + test::vehicle wagon{test::definition::load("wagon").set("Cntrl", "BrakeDelays", "GP")}; + CHECK(false == inspection_pending({&wagon}, as_train, moving)); + CHECK(wagon->BrakeDelayFlag == G); + // a car without G in a consist being shunted: it keeps its brake, on P + test::vehicle car{passenger_car_definition()}; + CHECK(false == inspection_pending({&car}, shunting, moving)); + CHECK(false == brake_setting::is_cut_out(car.mover())); + // a car whose brake is cut out already, for a reason of its own or by the inspector + test::vehicle damaged{passenger_car_definition()}; + damaged->Hamulec->SetBrakeStatus(damaged->Hamulec->GetBrakeStatus() | b_dmg); + CHECK(false == inspection_pending({&damaged}, as_train, moving)); + CHECK(false == damaged->BrakeCutOutByInspector); + test::vehicle done{passenger_car_definition()}; + REQUIRE(false == inspection_pending({&done}, as_train, standstill)); + REQUIRE(done->BrakeCutOutByInspector); + CHECK(false == inspection_pending({&done}, as_train, moving)); + // a brake put back on the run (the train became a consist being shunted) leaves nothing either + CHECK(false == inspection_pending({&done}, shunting, moving)); + CHECK(false == brake_setting::is_cut_out(done.mover())); + // a single vehicle's apply() tells the same + test::vehicle single{passenger_car_definition()}; + CHECK(brake_setting::apply(single.mover(), brake_setting::cut_out, moving)); + CHECK(false == brake_setting::apply(single.mover(), P, moving)); + CHECK(false == brake_setting::apply(single.mover(), 0, moving)); + CHECK(false == brake_setting::apply(single.mover(), brake_setting::cut_out, standstill)); + CHECK(false == brake_setting::apply(single.mover(), brake_setting::cut_out, moving)); +} + +TEST_CASE("brake setting: a train creeping to a stop counts as standing, one on the run or coupling doesn't") { + // the AI looks at the train at a W4 and when it switches from shunting to the train run below 1 km/h: the + // inspector takes those for a standstill. it couples faster than that (it drives onto the standing cut at up + // to 4 km/h and looks at the new train at the first buffer contact), so the brakes to cut out then wait for + // the look the AI takes once the train stands (the test above) CHECK(brake_setting::at_standstill(0.0)); CHECK(brake_setting::at_standstill(0.04)); CHECK(brake_setting::at_standstill(0.9)); diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index dab54bd1a..6e626bec2 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2435,9 +2435,10 @@ void TController::AutoRewident() // brakes are cut out (Cw-1 §19.9) only in a train (a bank engine's run included), not in a consist being shunted auto const train{brake_setting::runs_as_train(OrderCurrentGet())}; auto const settings{Global.AIBrakeSettingCw1 ? brake_setting::choose(consist, train) : brake_setting::choose_legacy(consist)}; - // and only at a standstill, the train creeping to a stop or coupling included: a check on the run (the order changed - // mid-run) leaves them to the next one + // and only at a standstill, the train creeping to a stop included: a check on the run (the order changed mid-run, or + // the train was just formed by coupling, which the AI does above that speed) leaves them to reinspect_brakes() auto const standstill{brake_setting::at_standstill(mvOccupied->Vel)}; + auto reinspect{false}; // a light engine counts as a passenger train for the braking below auto const passengertrain{brake_setting::passenger(brake_setting::choose_train_setting(consist))}; // 3. Nastawianie @@ -2448,11 +2449,15 @@ void TController::AutoRewident() continue; } // the setting, a brake cut out in a train on G (Cw-1 §19.9) or put back when the train no longer needs it cut out - brake_setting::apply(*d->MoverParameters, *setting, standstill); + if (brake_setting::apply(*d->MoverParameters, *setting, standstill)) { + // a brake to cut out, with the train moving: left for the next standstill + reinspect = true; + } // potentially release manual brake d->MoverParameters->DecManualBrakeLevel( ManualBrakePosNo ); d->MoverParameters->SpringBrake.Activate = false; } + BrakeReinspectionPending = reinspect; //ustawianie trybu pracy zadajnika hamulca, wystarczy raz po inicjalizacji AI if( true == AIControllFlag ) { // if a human is in charge leave the brake mode up to them, otherwise do as you like @@ -2545,6 +2550,25 @@ void TController::AutoRewident() } +// a brake the inspector had to cut out (Cw-1 §19.9) while the train was moving gets cut out once the train stands. +// a train formed by coupling is looked at as the buffers meet, above the speed the inspector takes for a standstill, +// and with no stop ahead nothing would look at it standing again (eu07/maszyna#350) +void TController::reinspect_brakes() +{ + if (false == BrakeReinspectionPending) { + return; + } + if (false == brake_setting::runs_as_train(OrderCurrentGet())) { + // no longer a train: nothing is cut out in a consist being shunted + BrakeReinspectionPending = false; + return; + } + if (brake_setting::at_standstill(mvOccupied->Vel)) { + // standing, the inspector leaves nothing for later: this clears the flag + AutoRewident(); + } +} + double TController::ESMVelocity(bool Main) { double fCurrentCoeff = 0.9; @@ -5320,6 +5344,8 @@ TController::Update( double const Timedelta ) { // mode specific actions handle_engine(); handle_orders(); + // a brake the wagon inspector left to cut out while the train was moving + reinspect_brakes(); // situational velocity and acceleration adjustments pick_optimal_speed( awarenessrange ); if( iEngineActive ) { diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 69f5cffc0..1657825dc 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -674,6 +674,8 @@ private: bool doors_open() const; bool doors_permit_active() const; void AutoRewident(); // ustawia hamulce w składzie + void reinspect_brakes(); // AutoRewident() again once the train stands, when it left a brake to cut out on the run + bool BrakeReinspectionPending{ false }; // AutoRewident() left a brake to cut out (Cw-1 §19.9) for the next standstill void announce( announcement_t const Announcement ); // members double fLength = 0.0; // długość składu (do wyciągania z ograniczeń) diff --git a/vehicle/brakesetting.cpp b/vehicle/brakesetting.cpp index f27c136ca..ceffe3180 100644 --- a/vehicle/brakesetting.cpp +++ b/vehicle/brakesetting.cpp @@ -260,23 +260,28 @@ vehicle describe(TMoverParameters const &Vehicle, bool const Driven) { return described; } -void apply(TMoverParameters &Vehicle, int const Setting, bool const Standstill) { +bool apply(TMoverParameters &Vehicle, int const Setting, bool const Standstill) { auto &brake{*Vehicle.Hamulec}; if (Vehicle.BrakeCutOutByInspector && false == is_cut_out(Vehicle)) { // someone put the brake back in the meantime, it's theirs now Vehicle.BrakeCutOutByInspector = false; } if (Setting == cut_out) { - if (false == is_cut_out(Vehicle) && Standstill) { - // close the cut-out cock and empty the brake with the releaser, as the scenery's '0' does: the - // distributor, cut off the brake pipe, has no air left to brake with and lets the cylinders go - brake.ForceEmptiness(); - brake.SetBrakeStatus(brake.GetBrakeStatus() | b_dmg); - Vehicle.BrakeCutOutByInspector = true; + if (is_cut_out(Vehicle)) { + // a brake cut out already, by the inspector or for a reason of its own, stays as it is + return false; } - // a brake cut out already, by the inspector or for a reason of its own, stays as it is. on the run the - // vehicle is left alone, brake and setting, until the inspector looks at the train standing - return; + if (false == Standstill) { + // on the run the vehicle is left alone, brake and setting, until the inspector looks at the train + // standing: the caller is told to come back + return true; + } + // close the cut-out cock and empty the brake with the releaser, as the scenery's '0' does: the + // distributor, cut off the brake pipe, has no air left to brake with and lets the cylinders go + brake.ForceEmptiness(); + brake.SetBrakeStatus(brake.GetBrakeStatus() | b_dmg); + Vehicle.BrakeCutOutByInspector = true; + return false; } if (Vehicle.BrakeCutOutByInspector) { // open the cut-out cock: the distributor charges from the brake pipe again @@ -286,6 +291,7 @@ void apply(TMoverParameters &Vehicle, int const Setting, bool const Standstill) if (Setting != 0) { Vehicle.BrakeDelaySwitch(Setting); } + return false; } } // namespace brake_setting diff --git a/vehicle/brakesetting.h b/vehicle/brakesetting.h index b555850fb..cb7ad5225 100644 --- a/vehicle/brakesetting.h +++ b/vehicle/brakesetting.h @@ -67,8 +67,10 @@ bool runs_as_train(int Orders); // the train stands for the inspector: slower than the AI takes for a deliberate movement (EU07_AI_MOVEMENT). // the AI looks at the train while it still creeps to a stop (at a W4, or switching from shunting to the train -// run) and as it couples (at the coupling speed), so a stricter threshold would miss those checks; a train -// really on the run (an order changed mid-run) stays above it +// run), so a stricter threshold would miss those checks; a train really on the run (an order changed mid-run) +// stays above it, and so does one being formed by coupling: the AI drives onto the standing cut faster than +// that and looks at the new train at the first buffer contact. apply() tells of the cut-outs such a look +// left for the next one, at a standstill bool at_standstill(double Velocity); // the inspector of the releases before eu07/maszyna#350, kept for ai.brakesetting.cw1 no. the settings @@ -89,7 +91,9 @@ vehicle describe(TMoverParameters const &Vehicle, bool Driven); // (Standstill): on the run the vehicle is left as it is until the inspector looks at the train again // standing, rather than losing its brake while moving. Any other setting first puts back a brake the // inspector cut out (it charges from the brake pipe again), then sets the vehicle to it (0: leaves the -// setting as it is). A brake cut out by anyone else stays cut out -void apply(TMoverParameters &Vehicle, int Setting, bool Standstill); +// setting as it is). A brake cut out by anyone else stays cut out. +// Returns true when a cut-out was left for later because the train was moving: the inspector has to look +// at the train again once it stands (TController::reinspect_brakes() does) +bool apply(TMoverParameters &Vehicle, int Setting, bool Standstill); } // namespace brake_setting