From 487eeb29370fa4d0e6995ca66191eb449b580f9c Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Thu, 3 Dec 2026 16:22:48 +0100 Subject: [PATCH] fix(ai): a locomotive hauled dead counts into the train's length and mass for G/P MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The wagon inspector left every powered vehicle out of the length and mass that decide between G and P for a freight train (Cw-1 §19.2, §19.3), a locomotive hauled dead too. Cw-1 §6 counts every vehicle of the train into its mass, usually without the working powered one: a dead locomotive now counts, a working one still doesn't. It's no wagon still: it doesn't make the train a freight or a passenger one, nor is it one of the five wagons kept on G, and it keeps a locomotive's setting. AutoRewident() takes a powered vehicle for working when it's the driven one, has a driver of its own, or is worked from the driven one by multiple control (the test CheckVehicles() uses for ControlledEnginesCount). The inspector of the earlier releases is unchanged. Follow-up of #282. --- tests/unit/test_brake_setting.cpp | 88 ++++++++++++++++++++++++++++++- vehicle/ai/consist.cpp | 6 ++- vehicle/brakesetting.cpp | 23 +++++--- vehicle/brakesetting.h | 7 ++- 4 files changed, 113 insertions(+), 11 deletions(-) diff --git a/tests/unit/test_brake_setting.cpp b/tests/unit/test_brake_setting.cpp index b4f7cb26c..59f3c3d30 100644 --- a/tests/unit/test_brake_setting.cpp +++ b/tests/unit/test_brake_setting.cpp @@ -29,6 +29,12 @@ constexpr bool moving{false}; brake_setting::vehicle locomotive(int const Delays = G | P | R) { return {Delays, true, 17.0, 80000.0}; } +// a locomotive hauled dead in the train, with no one working it +brake_setting::vehicle dead_locomotive(int const Delays = G | P | R) { + auto dead{locomotive(Delays)}; + dead.hauled = true; + return dead; +} // 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}; @@ -123,7 +129,7 @@ TEST_CASE("brake setting: a heavy freight train runs on G, a wagon without G has 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(dead_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)}; @@ -586,3 +592,83 @@ TEST_CASE("brake setting: the inspector of the earlier releases counted the loco CHECK(brake_setting::choose_legacy(consist)[1] == G); CHECK(brake_setting::choose_legacy(consist)[6] == P); } + +TEST_CASE("brake setting: a locomotive hauled dead counts into the length and mass of a freight train (Cw-1 §6, §19.2)") { + // 24 wagons of 20 m and 52 t: 480 m and 1248 t, the long locomotive setting + auto consist{train(locomotive(), 24, freight_wagon(20.0, 52.0))}; + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_gp); + // a dead locomotive of 17 m and 80 t at the rear: 497 m and 1328 t, over 1300 t + consist.emplace_back(dead_locomotive()); + CHECK(brake_setting::choose_train_setting(consist) == train_setting::freight_g); + auto const settings{brake_setting::choose(consist, as_train)}; + CHECK(settings.front() == G); + CHECK(settings.back() == G); + // a working one there, a second locomotive with its crew, doesn't count + consist.back() = locomotive(); + CHECK(brake_setting::choose_train_setting(consist) == train_setting::freight_gp); +} + +TEST_CASE("brake setting: a dead locomotive isn't one of the five wagons kept on G (Cw-1 §19.3b)") { + // 300 m and 600 t of wagons and a dead locomotive behind the working one: 317 m and 680 t + auto consist{train(locomotive(), 20, freight_wagon(15.0, 30.0))}; + consist.insert(consist.begin() + 1, dead_locomotive()); + 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()); + CHECK(settings[0] == G); + CHECK(settings[1] == G); // the dead locomotive + for (std::size_t i = 2; i <= 6; ++i) { + CAPTURE(i); + CHECK(settings[i] == G); // the first five wagons + } + for (std::size_t i = 7; i < settings.size(); ++i) { + CAPTURE(i); + CHECK(settings[i] == P); + } + // with the second locomotive working the wagons alone count: 300 m and 600 t, all of them on P + consist[1] = locomotive(); + CHECK(brake_setting::choose_train_setting(consist) == train_setting::freight_p); +} + +TEST_CASE("brake setting: a dead locomotive without G in a train on G brakes on P, it isn't cut out") { + auto consist{train(locomotive(), 30, freight_wagon(15.0, 80.0))}; + consist.emplace_back(dead_locomotive(P | R)); + REQUIRE(brake_setting::choose_train_setting(consist) == train_setting::freight_g); + CHECK(brake_setting::choose(consist, as_train).back() == P); +} + +TEST_CASE("brake setting: a dead locomotive isn't a wagon, a light engine hauling one stays a light engine") { + std::vector const consist{locomotive(), dead_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 doesn't tell a dead locomotive from a working one") { + auto working{train(locomotive(), 20, freight_wagon(15.0, 30.0))}; + working.insert(working.begin() + 1, locomotive()); + auto dead{working}; + dead[1] = dead_locomotive(); + CHECK(brake_setting::choose_legacy(dead) == brake_setting::choose_legacy(working)); + auto heavy{train(locomotive(), 24, freight_wagon(20.0, 52.0))}; + heavy.emplace_back(locomotive()); + auto heavydead{heavy}; + heavydead.back() = dead_locomotive(); + CHECK(brake_setting::choose_legacy(heavydead) == brake_setting::choose_legacy(heavy)); +} + +TEST_CASE("brake setting: describe() takes a powered vehicle not working in the train for one hauled dead") { + test::vehicle shunter{test::definition::load("diesel_shunter")}; + auto const working{brake_setting::describe(shunter.physics(), false)}; + CHECK(working.powered); + CHECK_FALSE(working.hauled); + CHECK_FALSE(brake_setting::describe(shunter.physics(), false, true).hauled); + auto const dead{brake_setting::describe(shunter.physics(), false, false)}; + CHECK(dead.powered); + CHECK(dead.hauled); + // a wagon is never hauled dead, it's a wagon + test::vehicle wagon{test::definition::load("wagon")}; + CHECK_FALSE(brake_setting::describe(wagon.physics(), false, false).hauled); + CHECK_FALSE(brake_setting::describe(wagon.physics(), false, true).hauled); +} diff --git a/vehicle/ai/consist.cpp b/vehicle/ai/consist.cpp index ec7ef7a13..41ecfa886 100644 --- a/vehicle/ai/consist.cpp +++ b/vehicle/ai/consist.cpp @@ -143,7 +143,11 @@ 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()) { - consist.emplace_back(brake_setting::describe(*d->physics, d == pVehicle)); + // a powered vehicle works in the train when it's the one driven, has a driver of its own, or is worked from the + // driven one by multiple control (as CheckVehicles() counts ControlledEnginesCount); else it's hauled dead and + // counts into the length and mass of the train (Cw-1 §6, §19.2) + auto const working{d == pVehicle || d->Mechanik != nullptr || d->is_connected(pVehicle, Coupling::control)}; + consist.emplace_back(brake_setting::describe(*d->physics, d == pVehicle, working)); } // 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 diff --git a/vehicle/brakesetting.cpp b/vehicle/brakesetting.cpp index 7677ff19a..cb5267dd5 100644 --- a/vehicle/brakesetting.cpp +++ b/vehicle/brakesetting.cpp @@ -21,8 +21,9 @@ 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) +// the limits of Cw-1 §19.2 and §19.3 for the length and mass of a freight train (the "skład pociągu"): its +// wagons and the locomotives hauled dead, without the working ones (Cw-1 §6 counts the mass of the train +// "zwykle bez uwzględnienia czynnego pojazdu z napędem") 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] @@ -46,18 +47,24 @@ int pick(int const Delays, int const Wanted, int const Fallback) { return 0; } -// the wagons of the train by the fastest setting they have +// the wagons of the train by the fastest setting they have, and the length and mass of the train for Cw-1 §19.2 +// and §19.3: the wagons and the locomotives hauled dead. a dead locomotive isn't a wagon though: it doesn't make +// the train a freight or a passenger one (Cw-1 §18.3), nor is it one of the five wagons kept on G (Cw-1 §19.3b) 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] + double length{0.0}; // of the wagons and the dead locomotives [m] + double mass{0.0}; // of the wagons and the dead locomotives [kg] explicit wagon_counts(std::vector const &Consist) { for (auto const &item : Consist) { if (item.powered) { + if (item.hauled) { + length += item.length; + mass += item.mass; + } continue; } length += item.length; @@ -252,11 +259,13 @@ bool is_cut_out(physics::Vehicle const &Vehicle) { return (Vehicle.brakes.distributor->GetBrakeStatus() & b_dmg) == b_dmg; } -vehicle describe(physics::Vehicle const &Vehicle, bool const Driven) { +vehicle describe(physics::Vehicle const &Vehicle, bool const Driven, bool const Working) { vehicle described; described.delays = Vehicle.brakes.config.availableDelays; - // a locomotive has Power > 1, though it may be a dead one at the end of the train + // a locomotive has Power > 1, a working one or one hauled dead: the dead one counts into the length and mass + // of the train, but it's no wagon still described.powered = Vehicle.Power >= 1.0; + described.hauled = described.powered && false == Working; described.length = Vehicle.Dim.L; described.mass = Vehicle.TotalMass(); described.cut_out = is_cut_out(Vehicle) && false == Vehicle.brakes.cutOutByInspector; diff --git a/vehicle/brakesetting.h b/vehicle/brakesetting.h index d54075110..e3c4312c0 100644 --- a/vehicle/brakesetting.h +++ b/vehicle/brakesetting.h @@ -27,6 +27,7 @@ struct vehicle { 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 + bool hauled{false}; // a powered vehicle hauled dead, with no one working it: it counts into the length and mass of the train }; // the kind of train the inspector takes the consist for, and how it sets the brakes @@ -92,8 +93,10 @@ std::vector choose_legacy(std::vector const &Consist); bool is_cut_out(physics::Vehicle 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(physics::Vehicle const &Vehicle, bool Driven); +// cut out: the inspector decides about it anew. Working tells a powered vehicle works in the train (it's the one +// driven, it has a driver of its own, or it's worked from the driven one by multiple control), as opposed to +// one hauled dead, which counts into the train's length and mass (Cw-1 §6, §19.2) +vehicle describe(physics::Vehicle const &Vehicle, bool Driven, bool Working = true); // 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,