diff --git a/tests/unit/test_odometer.cpp b/tests/unit/test_odometer.cpp index 5ec1b572f..067e31037 100644 --- a/tests/unit/test_odometer.cpp +++ b/tests/unit/test_odometer.cpp @@ -435,6 +435,85 @@ TEST_CASE("odometer: only the vehicles with a driver's cab are recorded") { } } +TEST_CASE("odometer: a vehicle driven without a driver's brake valve is recorded once a cab shows its odometer") { + // the draisines (DL2, DR2) have a cab and no BrakeHandle, so their type doesn't tell of the cab; the cab of the + // DL2 has a counter with an offset of 85 + auto const definition{test::definition::fiz("dr2_v1/dr2")}; + test::vehicle draisine{definition}; + REQUIRE_FALSE(draisine->HasDriverCab()); + scratch_directory directory{"odometer_cab_without_brake_valve"}; + auto const file{directory.path / "eu07_odometers.txt"}; + std::string const name{"DL2-001"}; + // the state a vehicle of the name starts a session with, nothing kept by the python screens + auto const start = [&](odometer::store const &States) { return odometer::starting_state(States, name, directory.path, directory.path); }; + odometer::store store; + draisine->Odometer = start(store); + REQUIRE(draisine->Odometer.source == odometer::origin::guessed); + + SUBCASE("run by the AI, no cab loaded and no counter bound, it isn't recorded") { + draisine->Odometer.run(1500.0); + CHECK_FALSE(odometer::kept(draisine->HasDriverCab(), draisine->Odometer)); + odometer::record(store, name, draisine->HasDriverCab(), draisine->Odometer); + CHECK_FALSE(store.find(name).has_value()); + CHECK_FALSE(store.modified()); + } + SUBCASE("its state seeded by the cab's counter is saved, read back, and counted on in the next session") { + // the cab loads: the counter seeds the state (TTrain, distcounter:) + draisine->Odometer.seed(odometer::counter_seed(85.f, 1.f).value()); + REQUIRE(draisine->Odometer.source == odometer::origin::counter); + draisine->Odometer.run(2500.0); + CHECK(odometer::kept(draisine->HasDriverCab(), draisine->Odometer)); + odometer::record(store, name, draisine->HasDriverCab(), draisine->Odometer); + CHECK(store.find(name).value() == doctest::Approx(87.5)); + CHECK_FALSE(store.guessed(name)); + REQUIRE(odometer::save(store, file)); + + // the next session: the vehicle starts from the saved state, not from the offset of the counter + odometer::store next; + { + std::ifstream input{file, std::ios::binary}; + CHECK(next.read(input) == 1); + } + test::vehicle again{definition}; + again->Odometer = start(next); + CHECK(again->Odometer.km == doctest::Approx(87.5)); + CHECK(again->Odometer.source == odometer::origin::saved); + // the cab loads again: the counter has nothing to seed, a saved state isn't the counter's any more. the + // counter bound to the vehicle is what tells of its cab now + again->Odometer.seed(odometer::counter_seed(85.f, 1.f).value()); + CHECK(again->Odometer.km == doctest::Approx(87.5)); + CHECK(again->Odometer.source == odometer::origin::saved); + again->Odometer.shown = true; + again->Odometer.run(1000.0); + CHECK(odometer::kept(again->HasDriverCab(), again->Odometer)); + odometer::record(next, name, again->HasDriverCab(), again->Odometer); + CHECK(next.find(name).value() == doctest::Approx(88.5)); + CHECK(next.modified()); + REQUIRE(odometer::save(next, file)); + + // a session the AI runs it in, with no cab loaded: the saved state stays as it is + test::vehicle ai{definition}; + ai->Odometer = start(next); + CHECK(ai->Odometer.km == doctest::Approx(88.5)); + ai->Odometer.run(4000.0); + odometer::record(next, name, ai->HasDriverCab(), ai->Odometer); + CHECK(next.find(name).value() == doctest::Approx(88.5)); + CHECK_FALSE(next.modified()); + } + SUBCASE("its cab loaded for a player, with no counter in it, it's recorded with its made up state") { + draisine->Odometer.shown = true; + draisine->Odometer.run(1500.0); + odometer::record(store, name, draisine->HasDriverCab(), draisine->Odometer); + CHECK(store.find(name).value() == doctest::Approx(draisine->Odometer.km)); + CHECK(store.guessed(name)); + } + SUBCASE("a state the session starts with doesn't carry the cab of an earlier one") { + draisine->Odometer.shown = true; + draisine->Odometer = start(store); + CHECK_FALSE(draisine->Odometer.shown); + } +} + TEST_CASE("odometer: the files of both kinds of python screens are read, by the name each used") { scratch_directory directory{"odometer_legacy_screens"}; auto const vehicles{directory.path / "dynamic" / "elf_v1"}; diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 7d3824f59..2b55a8aea 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -11162,7 +11162,12 @@ bool TTrain::InitializeCab(int NewCabNo, std::string const &asFileName) } if (DynamicObject->mdKabina) + { DynamicObject->mdKabina->Init(); // obrócenie modelu oraz optymalizacja, również zapisanie binarnego + // the vehicle has a cab, a player is in it: the odometer store keeps the vehicle also when its type has no + // driver's brake valve (eu07/maszyna#72) + DynamicObject->MoverParameters->Odometer.shown = true; + } set_cab_controls(NewCabNo < 0 ? 2 : NewCabNo); @@ -12788,6 +12793,9 @@ bool TTrain::initialize_gauge(cParser &Parser, std::string const &Label, int con mvControlled->Odometer.seed(*seed); gauge.offset(0.f); gauge.AssignDouble(&mvControlled->Odometer.km); + // the vehicle the counter counts for is one the odometer store keeps, also in the sessions the counter seeds + // nothing (its state is a saved one by then) and when it's the motor car of the unit the cab is in + mvControlled->Odometer.shown = true; } else if (Label == "shuntmodepower:") { diff --git a/vehicle/odometer.cpp b/vehicle/odometer.cpp index 8ecf9a9c1..8f98cb084 100644 --- a/vehicle/odometer.cpp +++ b/vehicle/odometer.cpp @@ -158,8 +158,12 @@ void store::record(std::string const &Vehicle, reading const &Odometer) { m_modified = true; } +bool kept(bool const Drivercab, reading const &Odometer) { + return Drivercab || Odometer.shown || Odometer.source == origin::counter; +} + void record(store &States, std::string const &Vehicle, bool const Drivercab, reading const &Odometer) { - if (false == Drivercab) + if (false == kept(Drivercab, Odometer)) return; States.record(Vehicle, Odometer); } diff --git a/vehicle/odometer.h b/vehicle/odometer.h index 93685d281..9708311f4 100644 --- a/vehicle/odometer.h +++ b/vehicle/odometer.h @@ -64,11 +64,20 @@ 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 +// the vehicle is one the store keeps: one with a driver's cab, which can show its odometer on the cab's counter or a +// python screen. that's +// - a vehicle whose type has a cab (Drivercab, TMoverParameters::HasDriverCab(): it has the driver's brake valve). 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 +// - a vehicle whose cab showed the odometer in this session (reading::shown): the cab was loaded for a player, or a +// cab's counter was bound to it. the type alone misses the vehicles driven without a driver's brake valve (the DL2 +// draisine, with a counter in its cab) and the motor cars the counter of a unit's cab counts for. such a vehicle is +// recorded in the sessions a player's cab shows it; in the others its saved state stays as it is +// - a vehicle whose state a cab's counter seeded (origin::counter), which is the same thing, told by the state +// the wagons, which make most of the AI's trains, aren't +bool kept(bool Drivercab, reading const &Odometer); +// records the state of a vehicle in the store when it's one the store keeps (kept()). the others 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, when it holds a number. the screens opened the file in the diff --git a/vehicle/odometerreading.h b/vehicle/odometerreading.h index 1b7d028da..286b2da5a 100644 --- a/vehicle/odometerreading.h +++ b/vehicle/odometerreading.h @@ -27,6 +27,9 @@ struct reading { double start{0.0}; // the state at the start of the session, km origin source{origin::guessed}; double guess{0.0}; // the made up state a guessed one counts from, km + // a cab shows the state in this session: a cab of the vehicle was loaded for a player (its python screens can + // show it), or a cab's counter was bound to it. such a vehicle has a cab, whatever its type tells + bool shown{false}; // sets the state the vehicle starts the session with void begin(double Km, origin Source);