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