18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 22:04:59 +01:00
Files
maszyna/vehicle/odometer.h
jerrrrycho 6dc42e976f 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/<name>_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.
2026-11-07 22:08:42 +01:00

95 lines
3.7 KiB
C++

/*
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 <filesystem>
#include <iosfwd>
#include <map>
#include <optional>
#include <string>
// 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 "<km> <vehicle name>", '#' 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<double> 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<std::string, double> m_states;
bool m_modified{false};
};
// state kept in the file of the python screens before (<vehicle directory>/kilometry/<name>_przebieg.txt,
// where the screens dropped the last character of the vehicle name), when it holds a number
std::optional<double> read_legacy(std::istream &Input);
std::optional<double> 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<double> 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