diff --git a/CMakeLists.txt b/CMakeLists.txt index 422ad9de9..a14c686fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -483,6 +483,7 @@ set(CORE_SOURCES "simulation/simulationtestrules.cpp" "vehicle/blackbox.cpp" "vehicle/enginestart.cpp" +"vehicle/brakesetting.cpp" "vehicle/handover.cpp" "vehicle/horn.cpp" "vehicle/linebreaker.cpp" diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 27271b817..777edc7c3 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2124,6 +2124,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/McZapkie/hamulce.cpp b/McZapkie/hamulce.cpp index 9411dc425..b30372e5e 100644 --- a/McZapkie/hamulce.cpp +++ b/McZapkie/hamulce.cpp @@ -1467,7 +1467,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; @@ -1770,7 +1770,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; @@ -1989,7 +1989,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 new file mode 100644 index 000000000..5034bf3c5 --- /dev/null +++ b/tests/unit/test_brake_setting.cpp @@ -0,0 +1,565 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "McZapkie/MOVER.h" +#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) + +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}; + +// 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 +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}; +} +// 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, as_train)}; + 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, as_train)}; + 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, as_train)}; + 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 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, as_train)}; + 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, as_train)}; + 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, as_train); + 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, as_train)}; + 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 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)); + 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, 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, Train).back()}; + brake_setting::apply(Vehicle.mover(), setting, Standstill); + 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, as_train).back()}; + brake_setting::apply(Vehicle.mover(), setting, standstill); + 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") { + 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: 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: 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)); + 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 + 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, 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, 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, as_train)}; + 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, as_train)) { + 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, 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, as_train)}; + 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, as_train)}; + 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, as_train)[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 ca3b7507f..47756b7ef 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -773,6 +773,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; @@ -1729,6 +1735,7 @@ global_settings::export_as_text( std::ostream &Output ) const { couplerphysics = "all"; } export_as_text( Output, "couplerphysics", couplerphysics ); + 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 2fff6e484..943cb3cf7 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -146,6 +146,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 790d6f699..03c471904 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" @@ -2550,107 +2551,38 @@ 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()) { + consist.emplace_back(brake_setting::describe(*d->MoverParameters, d == pVehicle)); } - // 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. + // 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 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 + 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) + // 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 + 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 @@ -2688,9 +2620,12 @@ 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 ) { + // 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 ) ); } @@ -2727,7 +2662,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; } @@ -2740,6 +2675,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; @@ -5515,6 +5469,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 b30701911..752d41839 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -682,6 +682,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/Train.cpp b/vehicle/Train.cpp index 2b55a8aea..ec4bea03d 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -2136,6 +2136,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 new file mode 100644 index 000000000..ceffe3180 --- /dev/null +++ b/vehicle/brakesetting.cpp @@ -0,0 +1,297 @@ +/* +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/MOVER.h" +#include "McZapkie/hamulce.h" +#include "vehicle/Driver.h" + +#include +#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 + 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] + + 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; + if (item.cut_out) { + ++r_cut_out; + } + } + 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 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; + } + 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, bool const Train) { + 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. + // 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: { + if (has(item.delays, bdelay_G)) { + settings.emplace_back(bdelay_G); + } + 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 (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 { + // 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; + } + } + } + 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}; + 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; +} + +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; +} + +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 (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; + } + 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 + brake.SetBrakeStatus(brake.GetBrakeStatus() & ~b_dmg); + Vehicle.BrakeCutOutByInspector = false; + } + if (Setting != 0) { + Vehicle.BrakeDelaySwitch(Setting); + } + return false; +} + +} // namespace brake_setting diff --git a/vehicle/brakesetting.h b/vehicle/brakesetting.h new file mode 100644 index 000000000..cb7ad5225 --- /dev/null +++ b/vehicle/brakesetting.h @@ -0,0 +1,99 @@ +/* +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 + +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 { + +// 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] + 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 +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 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); + +// 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. 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 (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), 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 +// 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. 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. +// 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