From 6dc42e976fae695205c707fbbb4ec411eec12e61 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 22:08:42 +0100 Subject: [PATCH] feat: keep the vehicle odometers between the sessions (#72) Every vehicle gets an odometer (TMoverParameters::Odometer) counting along with the per-session DistCounter, starting from its state saved in eu07_odometers.txt in the user's settings directory, else from the file a python screen kept it in (kilometry/_przebieg.txt), else from a state made up from the vehicle name. The store is saved every minute of simulation time and on closing. The cab's distcounter: gauge shows the odometer; its .mmd offset seeds a vehicle with no known state. Python screens get it as state["odometer_total"]; state["odometer"] stays the session distance the logbook and mirror scripts count differences of. --- CMakeLists.txt | 1 + McZapkie/MOVER.h | 2 + McZapkie/Mover.cpp | 2 + application/application.cpp | 2 + tests/unit/test_odometer.cpp | 199 +++++++++++++++++++++++++++++++ vehicle/DynObj.cpp | 23 ++++ vehicle/DynObj.h | 5 + vehicle/Gauge.h | 7 ++ vehicle/Train.cpp | 10 +- vehicle/odometer.cpp | 225 +++++++++++++++++++++++++++++++++++ vehicle/odometer.h | 94 +++++++++++++++ 11 files changed, 568 insertions(+), 2 deletions(-) create mode 100644 tests/unit/test_odometer.cpp create mode 100644 vehicle/odometer.cpp create mode 100644 vehicle/odometer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f34ad61f..d3152915e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -465,6 +465,7 @@ set(CORE_SOURCES "vehicle/handover.cpp" "vehicle/horn.cpp" "vehicle/smoke.cpp" +"vehicle/odometer.cpp" ) add_library(eu07_core STATIC ${CORE_SOURCES}) target_link_libraries(eu07_core PUBLIC eu07_settings) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 16bb207fa..1f7942e84 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -13,6 +13,7 @@ http://mozilla.org/MPL/2.0/. #include #include "hamulce.h" #include "scripting/ladderlogic.h" +#include "vehicle/odometer.h" class cParser; /* @@ -1963,6 +1964,7 @@ class TMoverParameters int SoundFlag = 0; /*!o patrz stale sound_ */ int AIFlag{0}; // HACK: events of interest for consist owner double DistCounter = 0.0; /*! licznik kilometrow */ + odometer::reading Odometer; // total distance run, kept between the sessions (eu07/maszyna#72) std::pair EnergyMeter; // energy from grid [kWh] double V = 0.0; // predkosc w [m/s] względem sprzęgów (dodania gdy jedzie w stronę 0) double Vel = 0.0; // moduł prędkości w [km/h], używany przez AI diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 20761bacb..48a36ccb6 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -1510,6 +1510,7 @@ double TMoverParameters::ComputeMovement(double dt, double dt1, const TTrackShap dL)}; DistCounter += fabs(dL) / 1000.0; + Odometer.run(dL); dL = 0; // koniec procedury, tu nastepuja dodatkowe procedury pomocnicze @@ -1576,6 +1577,7 @@ double TMoverParameters::FastComputeMovement(double dt, const TTrackShape & /*Sh dL)}; DistCounter += fabs(dL) / 1000.0; + Odometer.run(dL); dL = 0; // koniec procedury, tu nastepuja dodatkowe procedury pomocnicze diff --git a/application/application.cpp b/application/application.cpp index 9b7b82853..bd0526269 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -708,6 +708,8 @@ void eu07_application::exit() m_network.reset(); // SafeDelete( simulation::Train ); + step("the odometers"); + simulation::Vehicles.save_odometers(); step("the scenery"); SafeDelete(simulation::Region); diff --git a/tests/unit/test_odometer.cpp b/tests/unit/test_odometer.cpp new file mode 100644 index 000000000..8297d3167 --- /dev/null +++ b/tests/unit/test_odometer.cpp @@ -0,0 +1,199 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" +#include "vehicle/odometer.h" + +#include +#include +#include + +// the odometer of a vehicle, kept between the sessions (eu07/maszyna#72): one state the cab's counters and the +// python screens show, saved in one store, taking over the states the python screens kept in their own files + +namespace { + +odometer::reading started(double const Km, odometer::origin const Source = odometer::origin::saved) { + odometer::reading reading; + reading.begin(Km, Source); + return reading; +} + +odometer::reading run(double const Km, double const Metres) { + auto reading{started(Km)}; + reading.run(Metres); + return reading; +} + +// a directory of its own for a test, removed afterwards +struct scratch_directory { + std::filesystem::path path; + explicit scratch_directory(std::string const &Name) : path{std::filesystem::temp_directory_path() / ("eu07_unit_" + Name)} { + std::filesystem::remove_all(path); + std::filesystem::create_directories(path); + } + ~scratch_directory() { + std::error_code error; + std::filesystem::remove_all(path, error); + } +}; + +void write_file(std::filesystem::path const &Path, std::string const &Content) { + std::filesystem::create_directories(Path.parent_path()); + std::ofstream{Path, std::ios::binary} << Content; +} + +} // namespace + +TEST_CASE("odometer: a vehicle counts the distance it runs either way, as the session's counter does") { + for (auto const velocity : {40.0, -40.0}) { + CAPTURE(velocity); + test::vehicle vehicle{test::definition::load("wagon"), velocity}; + vehicle->Odometer.begin(123456.0, odometer::origin::saved); + vehicle.run(10.0); + CHECK(vehicle->DistCounter > 0.05); + CHECK(vehicle->Odometer.km - 123456.0 == doctest::Approx(vehicle->DistCounter)); + CHECK(vehicle->Odometer.start == 123456.0); + CHECK(vehicle->Odometer.moved()); + } +} + +TEST_CASE("odometer: the store reads back what it writes") { + odometer::store store; + store.record("EN76-012a", run(1361539.4283, 1000.0)); + store.record("loco with spaces", run(42.0, 500.0)); + CHECK(store.modified()); + + std::stringstream file; + store.write(file); + odometer::store read; + CHECK(read.read(file) == 2); + CHECK(read.find("EN76-012a").value() == doctest::Approx(1361540.4283)); + CHECK(read.find("loco with spaces").value() == doctest::Approx(42.5)); + CHECK_FALSE(read.find("EN76-012").has_value()); + CHECK_FALSE(read.modified()); +} + +TEST_CASE("odometer: the store skips the comments and the malformed lines") { + std::stringstream file{"# comment\r\n" + "100.5 good\r\n" + "\n" + "nonsense\n" + "abc name\n" + "-5 negative\n" + "nan notanumber\n" + "7 \n" + "200\ttabbed\n"}; + odometer::store store; + CHECK(store.read(file) == 2); + CHECK(store.find("good").value() == 100.5); + CHECK(store.find("tabbed").value() == 200.0); + CHECK(store.size() == 2); +} + +TEST_CASE("odometer: a vehicle gets in the store once it has run, then it's kept up to date") { + odometer::store store; + SUBCASE("a vehicle which only stood doesn't get in") { + store.record("standing", started(5000.0, odometer::origin::guessed)); + CHECK_FALSE(store.find("standing").has_value()); + CHECK_FALSE(store.modified()); + } + SUBCASE("one which ran does") { + store.record("running", run(5000.0, 250.0)); + CHECK(store.find("running").value() == doctest::Approx(5000.25)); + CHECK(store.modified()); + } + SUBCASE("one already in the store follows its state, and an unchanged one doesn't make the store modified") { + std::stringstream file{"300 known\n"}; + store.read(file); + store.record("known", started(300.0)); + CHECK_FALSE(store.modified()); + store.record("known", run(300.0, 1000.0)); + CHECK(store.find("known").value() == doctest::Approx(301.0)); + CHECK(store.modified()); + store.saved(); + CHECK_FALSE(store.modified()); + } + SUBCASE("a nameless vehicle doesn't") { + store.record("", run(1.0, 1000.0)); + CHECK(store.size() == 0); + } +} + +TEST_CASE("odometer: the state the python screens kept is read") { + auto const legacy = [](std::string const &Content) { + std::stringstream file{Content}; + return odometer::read_legacy(file); + }; + // written as python's str() of a float + CHECK(legacy("1361539.4283109533").value() == doctest::Approx(1361539.4283109533)); + CHECK(legacy("170650.0\n").value() == 170650.0); + CHECK(legacy(" 5000 ").value() == 5000.0); + CHECK_FALSE(legacy("").has_value()); + CHECK_FALSE(legacy("12 km").has_value()); + CHECK_FALSE(legacy("inf").has_value()); +} + +TEST_CASE("odometer: the state starts from the store, else the python screens' file, else a guess") { + scratch_directory directory{"odometer_legacy"}; + // the python screens dropped the last letter of the vehicle name: the units of the EN76 shared the file + write_file(directory.path / "kilometry" / "EN76-012_przebieg.txt", "1361539.5"); + std::stringstream file{"2000 EN76-012b\n"}; + odometer::store store; + store.read(file); + + auto const saved{odometer::starting_state(store, "EN76-012b", directory.path)}; + CHECK(saved.source == odometer::origin::saved); + CHECK(saved.km == 2000.0); + + auto const legacy{odometer::starting_state(store, "EN76-012a", directory.path)}; + CHECK(legacy.source == odometer::origin::legacy); + CHECK(legacy.km == 1361539.5); + CHECK(legacy.start == legacy.km); + + auto const guessed{odometer::starting_state(store, "SM42-001", directory.path)}; + CHECK(guessed.source == odometer::origin::guessed); + CHECK(guessed.km == odometer::guessed_state("SM42-001")); + CHECK_FALSE(guessed.moved()); + + // an unreadable legacy file is no state + write_file(directory.path / "kilometry" / "ET22-100_przebieg.txt", ""); + CHECK(odometer::starting_state(store, "ET22-100a", directory.path).source == odometer::origin::guessed); +} + +TEST_CASE("odometer: a guessed state is the same for the same vehicle, in range") { + for (auto const *name : {"", "a", "EN57-1001ra", "ET22-100", "SM42-001"}) { + CAPTURE(name); + auto const state{odometer::guessed_state(name)}; + CHECK(state == odometer::guessed_state(name)); + CHECK(state >= 10000.0); + CHECK(state < 500000.0); + } + CHECK(odometer::guessed_state("ET22-100") != odometer::guessed_state("ET22-101")); +} + +TEST_CASE("odometer: the cab's counter offset replaces only a guessed state, the distance run kept") { + CHECK(odometer::counter_seed(101532.f, 1.f).value() == 101532.0); + CHECK(odometer::counter_seed(1015320.f, 10.f).value() == doctest::Approx(101532.0)); + CHECK_FALSE(odometer::counter_seed(0.f, 1.f).has_value()); + CHECK_FALSE(odometer::counter_seed(100.f, 0.f).has_value()); + CHECK_FALSE(odometer::counter_seed(-100.f, 1.f).has_value()); + + SUBCASE("guessed") { + auto reading{started(33333.0, odometer::origin::guessed)}; + reading.run(2000.0); + reading.seed(101532.0); + CHECK(reading.source == odometer::origin::counter); + CHECK(reading.start == 101532.0); + CHECK(reading.km == doctest::Approx(101534.0)); + // the next cab load doesn't seed it again + reading.seed(5.0); + CHECK(reading.start == 101532.0); + } + for (auto const source : {odometer::origin::saved, odometer::origin::legacy}) { + CAPTURE(static_cast(source)); + auto reading{started(777.0, source)}; + reading.seed(101532.0); + CHECK(reading.km == 777.0); + CHECK(reading.source == source); + } +} diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index d5f60e332..ff7587169 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -19,6 +19,7 @@ http://mozilla.org/MPL/2.0/. #include "rendering/lightarray.h" #include "vehicle/Camera.h" #include "vehicle/Train.h" +#include "vehicle/odometer.h" #include "vehicle/Driver.h" #include "utilities/Globals.h" #include "utilities/Timer.h" @@ -1983,6 +1984,8 @@ TDynamicObject::Init(std::string Name, // nazwa pojazdu, np. "EU07-424" */ // utworzenie parametrów fizyki MoverParameters = new TMoverParameters(iDirection ? fVel : -fVel, Type_Name, asName, Cab); + // the vehicle carries on with the odometer state of its earlier sessions (eu07/maszyna#72) + MoverParameters->Odometer = odometer::starting_state( odometer::states(), asName, asBaseDir ); // McZapkie: TypeName musi byc nazwą CHK/MMD pojazdu if (!MoverParameters->LoadFIZ(asBaseDir)) { // jak wczytanie CHK się nie uda, to błąd @@ -8721,6 +8724,26 @@ vehicle_table::update( double Deltatime, int Iterationcount ) { // jeśli jest coś do usunięcia z listy, to trzeba na końcu erase_disabled(); + + // the odometers are saved now and then, a crash doesn't lose more than the last minute + m_odometersavetimer += totaltime; + if( m_odometersavetimer >= 60.0 ) { + save_odometers(); + } +} + +// records the odometer states of the vehicles and saves them for the next sessions (eu07/maszyna#72) +void +vehicle_table::save_odometers() { + + m_odometersavetimer = 0.0; + auto &odometers { odometer::states() }; + for( auto const *vehicle : m_items ) { + if( vehicle->MoverParameters != nullptr ) { + odometers.record( vehicle->name(), vehicle->MoverParameters->Odometer ); + } + } + odometer::save(); } // legacy method, checks for presence and height of traction wire for specified vehicle diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index 577f0f020..b8eec1031 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -901,11 +901,16 @@ public: // legacy method, sends list of vehicles over network void DynamicList( bool const Onlycontrolled = false ) const; + // records the odometer states of the vehicles and saves them for the next sessions + void + save_odometers(); private: // maintenance; removes from tracks consists with vehicles marked as disabled bool erase_disabled(); + + double m_odometersavetimer { 0.0 }; // simulation time since the odometers were last saved }; diff --git a/vehicle/Gauge.h b/vehicle/Gauge.h index fdef9c58e..9aa539f15 100644 --- a/vehicle/Gauge.h +++ b/vehicle/Gauge.h @@ -55,6 +55,13 @@ public: void AssignBool(bool *bValue); void UpdateValue(); void AssignState( bool const *State ); + // the value shown is value * scale + offset + float offset() const { + return m_offset; } + void offset( float const Offset ) { + m_offset = Offset; } + float scale() const { + return m_scale; } // returns offset of submodel associated with the button from the model centre glm::vec3 model_offset() const; TGaugeType type() const; diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index e30635f4f..5a918809a 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -796,7 +796,8 @@ std::shared_ptr TTrain::GetTrainState(dictionary_source const dict->insert("tractionforce", std::abs(mvOccupied->Ft)); dict->insert("slipping_wheels", mvOccupied->SlippingWheels); dict->insert("sanding", mvOccupied->SandDose); - dict->insert("odometer", mvOccupied->DistCounter); + dict->insert("odometer", mvOccupied->DistCounter); // distance run in the session, km + dict->insert("odometer_total", mvOccupied->Odometer.km); // the vehicle's odometer, kept between the sessions // electric current data dict->insert("traction_voltage", std::abs(mvPantographUnit->PantographVoltage)); dict->insert("voltage", std::abs(mvControlled->EngineVoltage)); @@ -12320,7 +12321,12 @@ bool TTrain::initialize_gauge(cParser &Parser, std::string const &Label, int con // Ra 2014-07: licznik kilometrów auto &gauge = Cabine[Cabindex].Gauge(-1); // pierwsza wolna gałka gauge.Load(Parser, DynamicObject); - gauge.AssignDouble(&mvControlled->DistCounter); + // the counter shows the odometer of the vehicle, kept between the sessions (eu07/maszyna#72). its offset + // was the state it started each session with, now it is that only for a vehicle with no known state + if (auto const seed{odometer::counter_seed(gauge.offset(), gauge.scale())}) + mvControlled->Odometer.seed(*seed); + gauge.offset(0.f); + gauge.AssignDouble(&mvControlled->Odometer.km); } else if (Label == "shuntmodepower:") { diff --git a/vehicle/odometer.cpp b/vehicle/odometer.cpp new file mode 100644 index 000000000..5f2a185d1 --- /dev/null +++ b/vehicle/odometer.cpp @@ -0,0 +1,225 @@ +/* +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/odometer.h" + +#include "utilities/Logs.h" +#include "utilities/utilities.h" + +#include +#include +#include +#include +#include + +namespace odometer { + +namespace { + +bool valid(double const Km) { + return std::isfinite(Km) && Km >= 0.0; +} + +// number making up all of the text, surrounding whitespace aside +std::optional parse_number(std::string const &Text) { + auto const first{Text.find_first_not_of(" \t\r\n")}; + if (first == std::string::npos) + return std::nullopt; + auto const last{Text.find_last_not_of(" \t\r\n")}; + auto const number{Text.substr(first, last - first + 1)}; + try { + std::size_t used{0}; + auto const value{std::stod(number, &used)}; + if (used != number.size() || false == valid(value)) + return std::nullopt; + return value; + } catch (std::exception const &) { + return std::nullopt; + } +} + +} // namespace + +void reading::begin(double const Km, origin const Source) { + km = Km; + start = Km; + source = Source; +} + +void reading::seed(double const Km) { + if (source != origin::guessed || false == valid(Km)) + return; + km += Km - start; + start = Km; + source = origin::counter; +} + +void reading::run(double const Metres) { + km += std::abs(Metres) / 1000.0; +} + +bool reading::moved() const { + return km != start; +} + +std::size_t store::read(std::istream &Input) { + std::size_t count{0}; + std::string line; + while (std::getline(Input, line)) { + if (false == line.empty() && line.back() == '\r') + line.pop_back(); + if (line.empty() || line.front() == '#') + continue; + // the name goes last, it's the rest of the line after the separator + auto const separator{line.find_first_of(" \t")}; + if (separator == std::string::npos) + continue; + auto const km{parse_number(line.substr(0, separator))}; + auto const name{line.substr(separator + 1)}; + if (false == km.has_value() || name.empty()) + continue; + m_states[name] = *km; + ++count; + } + return count; +} + +void store::write(std::ostream &Output) const { + Output << "# odometer states of the vehicles: \n"; + Output << std::fixed << std::setprecision(4); + for (auto const &[name, km] : m_states) + Output << km << ' ' << name << '\n'; +} + +std::optional store::find(std::string const &Vehicle) const { + auto const lookup{m_states.find(Vehicle)}; + if (lookup == m_states.end()) + return std::nullopt; + return lookup->second; +} + +void store::record(std::string const &Vehicle, reading const &Odometer) { + if (Vehicle.empty() || false == valid(Odometer.km)) + return; + auto const lookup{m_states.find(Vehicle)}; + if (lookup == m_states.end()) { + if (false == Odometer.moved()) + return; + m_states.emplace(Vehicle, Odometer.km); + m_modified = true; + return; + } + if (lookup->second == Odometer.km) + return; + lookup->second = Odometer.km; + m_modified = true; +} + +std::optional read_legacy(std::istream &Input) { + std::stringstream content; + content << Input.rdbuf(); + return parse_number(content.str()); +} + +std::optional legacy_state(std::filesystem::path const &Vehicledirectory, std::string const &Vehicle) { + if (Vehicle.size() < 2) + return std::nullopt; + auto const path{Vehicledirectory / "kilometry" / (Vehicle.substr(0, Vehicle.size() - 1) + "_przebieg.txt")}; + std::error_code error; + if (false == std::filesystem::is_regular_file(path, error)) + return std::nullopt; + std::ifstream input{path, std::ios::binary}; + if (false == input.is_open()) + return std::nullopt; + return read_legacy(input); +} + +double guessed_state(std::string const &Vehicle) { + // FNV-1a, stable between the builds unlike std::hash + std::uint64_t hash{14695981039346656037ull}; + for (auto const character : Vehicle) { + hash ^= static_cast(character); + hash *= 1099511628211ull; + } + return 10000.0 + static_cast(hash % 490000ull); +} + +reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory) { + reading result; + if (auto const saved{States.find(Vehicle)}) { + result.begin(*saved, origin::saved); + } else if (auto const legacy{legacy_state(Vehicledirectory, Vehicle)}) { + result.begin(*legacy, origin::legacy); + } else { + result.begin(guessed_state(Vehicle), origin::guessed); + } + return result; +} + +std::optional counter_seed(float const Offset, float const Scale) { + if (Offset == 0.f || Scale == 0.f) + return std::nullopt; + auto const km{static_cast(Offset) / static_cast(Scale)}; + if (false == valid(km)) + return std::nullopt; + return km; +} + +std::filesystem::path file() { + auto path{user_config_path("eu07_odometers.txt")}; + return path.empty() ? std::filesystem::path{"eu07_odometers.txt"} : path; +} + +store &states() { + static store odometers = [] { + store result; + std::ifstream input{file(), std::ios::binary}; + if (input.is_open()) { + auto const count{result.read(input)}; + WriteLog("Odometer states of " + std::to_string(count) + " vehicles read from \"" + file().string() + "\""); + } + return result; + }(); + return odometers; +} + +bool save() { + auto &odometers{states()}; + if (false == odometers.modified()) + return true; + auto const path{file()}; + std::error_code error; + if (path.has_parent_path()) + std::filesystem::create_directories(path.parent_path(), error); + // written aside first, a crash in the middle of a write doesn't lose the states + auto temporary{path}; + temporary += ".tmp"; + { + std::ofstream output{temporary, std::ios::binary | std::ios::trunc}; + if (false == output.is_open()) { + ErrorLog("Failed to save the odometer states to \"" + temporary.string() + "\""); + return false; + } + odometers.write(output); + if (false == output.good()) { + ErrorLog("Failed to save the odometer states to \"" + temporary.string() + "\""); + return false; + } + } + std::filesystem::rename(temporary, path, error); + if (error) { + ErrorLog("Failed to save the odometer states to \"" + path.string() + "\": " + error.message()); + return false; + } + odometers.saved(); + return true; +} + +} // namespace odometer diff --git a/vehicle/odometer.h b/vehicle/odometer.h new file mode 100644 index 000000000..65313c34b --- /dev/null +++ b/vehicle/odometer.h @@ -0,0 +1,94 @@ +/* +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 +#include +#include +#include +#include + +// the odometer of a vehicle: the total distance it has run, kept between the sessions (eu07/maszyna#72). +// the analog counters in the cab (distcounter:) and the python screens (state["odometer_total"]) show +// this one state, instead of the counters starting from their .mmd offset and the screens each keeping +// their own files +namespace odometer { + +// where the state of a vehicle came from +enum class origin { + saved, // the odometer store of the earlier sessions + legacy, // the file a python screen kept it in before + counter, // the offset of the cab's counter, which used to show it on top of the distance run in the session + guessed // nothing known, a made up state which the cab's counter can still replace +}; + +// odometer of one vehicle +struct reading { + double km{0.0}; // the state, km + double start{0.0}; // the state at the start of the session, km + origin source{origin::guessed}; + + // sets the state the vehicle starts the session with + void begin(double Km, origin Source); + // replaces a guessed state with the one the cab's counter showed at the start, the distance run kept. + // a known state stays + void seed(double Km); + // counts the distance run, in metres either way + void run(double Metres); + // the vehicle has run in this session + bool moved() const; +}; + +// the saved odometer states of the vehicles, km by vehicle name +class store { +public: + // reads the states written by write(). lines " ", '#' starts a comment line, a malformed + // line is skipped. returns the number of states read + std::size_t read(std::istream &Input); + void write(std::ostream &Output) const; + + std::optional find(std::string const &Vehicle) const; + // records the state of a vehicle. one not in the store yet gets in only once it has run, so the vehicles + // standing in the sceneries don't fill the file + void record(std::string const &Vehicle, reading const &Odometer); + + // there are states recorded since they were last saved + bool modified() const { return m_modified; } + void saved() { m_modified = false; } + std::size_t size() const { return m_states.size(); } + +private: + std::map m_states; + bool m_modified{false}; +}; + +// 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); +std::optional legacy_state(std::filesystem::path const &Vehicledirectory, std::string const &Vehicle); + +// made up state of a vehicle nothing is known about: a number from the name, in [10 000, 500 000) km, +// the same each session until the vehicle runs and gets its state saved +double guessed_state(std::string const &Vehicle); + +// state a vehicle starts the session with: the saved one, else the legacy one, else a guessed one +reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory); + +// state the cab's counter with offset Offset and scale Scale shows, once its offset no longer applies +// (the counter shows the odometer). nothing for a counter without an offset +std::optional counter_seed(float Offset, float Scale); + +// the odometer store of the simulation, read from the user's settings directory on the first use +store &states(); +std::filesystem::path file(); +// writes the store when anything was recorded since. returns false when the write failed +bool save(); + +} // namespace odometer