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