/* 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