From acf0716a9ca1ee1f4459e4f69802f2d875a74146 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Mon, 9 Nov 2026 16:12:13 +0100 Subject: [PATCH] fix: keep the odometers of the vehicles with a driver's cab only every vehicle which ran got a line in the store, the wagons of the AI's trains in every scenario too. the store records a vehicle with a driver's brake valve (BrakeHandle in the .fiz), which can show its odometer; the locomotives and units the AI runs before a player's cab loads them still get in with their replaceable made up states. the states already in the file stay as they are --- McZapkie/MOVER.h | 7 +++++ tests/unit/test_odometer.cpp | 51 ++++++++++++++++++++++++++++++++++++ vehicle/DynObj.cpp | 4 +-- vehicle/odometer.cpp | 6 +++++ vehicle/odometer.h | 7 +++++ 5 files changed, 73 insertions(+), 2 deletions(-) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index f5de00931..d461269b1 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2566,6 +2566,13 @@ class TMoverParameters { return JointEngineStart ? range_t::consist : range_t::local; } + // whether the vehicle has a driver's cab: it has the driver's brake valve (BrakeHandle in the Cntrl. section of the + // .fiz), which the wagons and most cars of the units without a cab don't (a few copy the Cntrl. section of the cab + // car, as the EN57 s car does). it's the vehicle type's, unlike the cab the scenario puts a driver in + bool HasDriverCab() const + { + return BrakeHandle != TBrakeHandle::NoHandle; + } // whether the cab command opening the main circuit opens it where the only cab switch of the circuit is the button // closing it: on a diesel that's the starter button, and the command is the one way to stop an engine whose fuel pump // starts on its own (eu07/maszyna#427). the other vehicles with such a button keep ignoring the command diff --git a/tests/unit/test_odometer.cpp b/tests/unit/test_odometer.cpp index 4ae5c454a..5e287116b 100644 --- a/tests/unit/test_odometer.cpp +++ b/tests/unit/test_odometer.cpp @@ -383,3 +383,54 @@ TEST_CASE("odometer: a test run neither reads nor saves the player's store") { // a save to the file would have cleared it CHECK(states.modified()); } + +TEST_CASE("odometer: the vehicles with a driver's cab are the ones with the driver's brake valve") { + // the definitions of the game: a locomotive, cab cars of units, a motor car of a unit without a cab, the wagons + struct expected { + char const *path; + bool cab; + }; + for (auto const &[path, cab] : {expected{"4e_v1/4e", true}, expected{"sm42_v1/6d1", true}, expected{"en57_v1/5bgb", true}, + expected{"21xm_v1/214ma", true}, expected{"impuls_v1/31we-a_pr", true}, + expected{"ed72_v1/6bs-sa", false}, expected{"impuls_v1/31we-b_pr", false}, + expected{"11xa_v2/93a", false}, expected{"bdhpumn_v3/bdhpumn", false}, + expected{"430w_v2/430w", false}}) { + CAPTURE(path); + test::vehicle vehicle{test::definition::fiz(path)}; + CHECK(vehicle->HasDriverCab() == cab); + } + test::vehicle wagon{test::definition::load("wagon")}; + CHECK_FALSE(wagon->HasDriverCab()); + test::vehicle locomotive{test::definition::load("wagon").set("Cntrl", "BrakeHandle", "FV4a")}; + CHECK(locomotive->HasDriverCab()); +} + +TEST_CASE("odometer: only the vehicles with a driver's cab are recorded") { + odometer::store store; + SUBCASE("a wagon which ran doesn't get in") { + odometer::record(store, "wagon", false, run(5000.0, 1000.0)); + CHECK_FALSE(store.find("wagon").has_value()); + CHECK_FALSE(store.modified()); + } + SUBCASE("a locomotive the AI ran before any cab loaded it gets in with its made up state, still replaceable") { + odometer::reading ai; + ai.begin(odometer::guessed_state("ET22-100"), odometer::origin::guessed); + ai.run(2000.0); + odometer::record(store, "ET22-100", true, ai); + CHECK(store.find("ET22-100").value() == doctest::Approx(ai.km)); + CHECK(store.guessed("ET22-100")); + CHECK(store.modified()); + } + SUBCASE("the state of a wagon already in the file stays as it was, and is written back") { + std::stringstream file{"300 old wagon\n"}; + store.read(file); + odometer::record(store, "old wagon", false, run(300.0, 1000.0)); + CHECK(store.find("old wagon").value() == 300.0); + CHECK_FALSE(store.modified()); + std::stringstream written; + store.write(written); + odometer::store read; + CHECK(read.read(written) == 1); + CHECK(read.find("old wagon").value() == 300.0); + } +} diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 46e634b9f..06955e694 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -8817,7 +8817,7 @@ vehicle_table::save_odometers() { auto &odometers { odometer::states() }; for( auto const *vehicle : m_items ) { if( vehicle->MoverParameters != nullptr ) { - odometers.record( vehicle->name(), vehicle->MoverParameters->Odometer ); + odometer::record( odometers, vehicle->name(), vehicle->MoverParameters->HasDriverCab(), vehicle->MoverParameters->Odometer ); } } odometer::save(); @@ -8970,7 +8970,7 @@ vehicle_table::erase_disabled() { // the distance the vehicle ran goes in the odometer store now, it doesn't depend on the vehicle being // kept on the list until the next save (eu07/maszyna#72) if( vehicle->MoverParameters != nullptr ) { - odometer::states().record( vehicle->name(), vehicle->MoverParameters->Odometer ); + odometer::record( odometer::states(), vehicle->name(), vehicle->MoverParameters->HasDriverCab(), vehicle->MoverParameters->Odometer ); } if( vehicle->MyTrack != nullptr && true == vehicle->MyTrack->RemoveDynamicObject(vehicle) ) { diff --git a/vehicle/odometer.cpp b/vehicle/odometer.cpp index e7e671d25..60c119f12 100644 --- a/vehicle/odometer.cpp +++ b/vehicle/odometer.cpp @@ -158,6 +158,12 @@ void store::record(std::string const &Vehicle, reading const &Odometer) { m_modified = true; } +void record(store &States, std::string const &Vehicle, bool const Drivercab, reading const &Odometer) { + if (false == Drivercab) + return; + States.record(Vehicle, Odometer); +} + void store::merge(store const &Other) { for (auto const &[name, other] : Other.m_states) { auto const [lookup, added] {m_states.try_emplace(name, other)}; diff --git a/vehicle/odometer.h b/vehicle/odometer.h index 110554ef6..083d00d1a 100644 --- a/vehicle/odometer.h +++ b/vehicle/odometer.h @@ -84,6 +84,13 @@ private: bool m_modified{false}; }; +// records the state of a vehicle in the store when it's one the store keeps: a vehicle with a driver's cab +// (TMoverParameters::HasDriverCab()), which can show its odometer on the cab's counter or a python screen. that +// includes the locomotives and units the AI runs before a player's cab loads them, their made up states stay +// replaceable by the cab's counter. the wagons, which make most of the AI's trains, aren't recorded; the states the +// store already holds for any of them stay as they are +void record(store &States, std::string const &Vehicle, bool Drivercab, reading const &Odometer); + // state kept in the file of the python screens before (/kilometry/_przebieg.txt, // where the screens dropped the last character of the vehicle name), when it holds a number std::optional read_legacy(std::istream &Input);