18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 22:04:59 +01:00

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
This commit is contained in:
jerrrrycho
2026-11-09 16:12:13 +01:00
parent 729f15e878
commit acf0716a9c
5 changed files with 73 additions and 2 deletions

View File

@@ -2566,6 +2566,13 @@ class TMoverParameters
{ {
return JointEngineStart ? range_t::consist : range_t::local; 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 // 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 // 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 // starts on its own (eu07/maszyna#427). the other vehicles with such a button keep ignoring the command

View File

@@ -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 // a save to the file would have cleared it
CHECK(states.modified()); 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);
}
}

View File

@@ -8817,7 +8817,7 @@ vehicle_table::save_odometers() {
auto &odometers { odometer::states() }; auto &odometers { odometer::states() };
for( auto const *vehicle : m_items ) { for( auto const *vehicle : m_items ) {
if( vehicle->MoverParameters != nullptr ) { if( vehicle->MoverParameters != nullptr ) {
odometers.record( vehicle->name(), vehicle->MoverParameters->Odometer ); odometer::record( odometers, vehicle->name(), vehicle->MoverParameters->HasDriverCab(), vehicle->MoverParameters->Odometer );
} }
} }
odometer::save(); 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 // 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) // kept on the list until the next save (eu07/maszyna#72)
if( vehicle->MoverParameters != nullptr ) { 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 if( vehicle->MyTrack != nullptr
&& true == vehicle->MyTrack->RemoveDynamicObject(vehicle) ) { && true == vehicle->MyTrack->RemoveDynamicObject(vehicle) ) {

View File

@@ -158,6 +158,12 @@ void store::record(std::string const &Vehicle, reading const &Odometer) {
m_modified = true; 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) { void store::merge(store const &Other) {
for (auto const &[name, other] : Other.m_states) { for (auto const &[name, other] : Other.m_states) {
auto const [lookup, added] {m_states.try_emplace(name, other)}; auto const [lookup, added] {m_states.try_emplace(name, other)};

View File

@@ -84,6 +84,13 @@ private:
bool m_modified{false}; 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 (<vehicle directory>/kilometry/<name>_przebieg.txt, // 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 // 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> read_legacy(std::istream &Input);