mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 15:55:27 +01:00
a9236731c8
A derailed vehicle came back on the rails. Its entry now keeps what derailed it (outcome, cause, speed, the cause and side of the motion off the track, the acceleration, the collision derailment) and the distance its criteria were last checked over; the derailment the scenery and the test mode were told of (new_derailment()), so it isn't told again; and with physics.derailment new the motion of the body off the track (derail_motion::body::state()) with the timers of the vehicle off the track. The terrain under it is gathered anew around it when it loads. sleep_brakes, what a sleeping vehicle's brake force came from, is kept too. All keys are new: an entry without them leaves the vehicle as the scenery made it, so files written before still load. New scenario case snapshot_td_off_track.
787 lines
31 KiB
C++
787 lines
31 KiB
C++
#include "stdafx.h"
|
|
#include "doctest.h"
|
|
|
|
#include "consist.h"
|
|
#include "drive.h"
|
|
#include "physics/brakes.h"
|
|
#include "physics/derailment.h"
|
|
#include "real_vehicles.h"
|
|
#include "simulation/simulationsnapshot.h"
|
|
#include "simulation/simulationsnapshotmover.h"
|
|
#include "utilities/Globals.h"
|
|
#include "vehicle.h"
|
|
#include "vehicle/derailmotion.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <memory>
|
|
#include <set>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// the state of a vehicle in a simulation snapshot (simulation/simulationsnapshotmover.cpp): saved from a vehicle which
|
|
// has been running, put on a vehicle fresh from its definition and saved again it's the same, and the fresh vehicle
|
|
// carries on the way the first one does
|
|
|
|
namespace {
|
|
|
|
namespace snapshot = simulation::snapshot;
|
|
using test::definition;
|
|
using test::vehicle;
|
|
|
|
test::track const wired{.voltage = test::catenary_3kv};
|
|
|
|
snapshot::record state_of(physics::Vehicle const &Mover)
|
|
{
|
|
snapshot::record entry{"vehicle", Mover.Name};
|
|
snapshot::capture_mover(entry, Mover);
|
|
return entry;
|
|
}
|
|
|
|
// the entry as a reader of the file gets it
|
|
snapshot::record through_text(snapshot::record const &Entry)
|
|
{
|
|
snapshot::document document;
|
|
document.add("snapshot", "1");
|
|
document.records.emplace_back(Entry);
|
|
auto const read{snapshot::parse(snapshot::to_text(document))};
|
|
REQUIRE(read.problems.empty());
|
|
REQUIRE(read.records.size() == 2);
|
|
return read.records[1];
|
|
}
|
|
|
|
// the keys whose values differ between the entries, with the values: "pipe 5 4.9, "
|
|
std::string differences(snapshot::record const &Left, snapshot::record const &Right)
|
|
{
|
|
std::string text;
|
|
for (auto const &[key, value] : Left.fields)
|
|
{
|
|
if (auto const other{Right.text(key, "(none)")}; other != value)
|
|
text += key + ' ' + value + ' ' + other + ", ";
|
|
}
|
|
if (Left.fields.size() != Right.fields.size())
|
|
text += "the number of keys";
|
|
return text;
|
|
}
|
|
|
|
// an electric locomotive started up, with the brakes off, notched up and run for a while
|
|
void run_locomotive(vehicle &Locomotive, int const Notches, double const Seconds)
|
|
{
|
|
auto const driven{test::driving(Locomotive)};
|
|
REQUIRE(test::start_up(driven, wired));
|
|
test::release_brakes(Locomotive.physics());
|
|
test::drive(driven, wired, 20.0);
|
|
for (auto notch{0}; notch < Notches; ++notch)
|
|
{
|
|
Locomotive->IncMainCtrl(1);
|
|
test::drive(driven, wired, 2.0);
|
|
}
|
|
test::drive(driven, wired, Seconds);
|
|
}
|
|
|
|
// runs the vehicle on with its driver acknowledging the vigilance device, as the AI does
|
|
void drive_on(vehicle &Vehicle, double const Seconds, test::track const &Track = wired)
|
|
{
|
|
test::drive(test::driving(Vehicle), Track, Seconds);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("snapshot: the state of every real vehicle, saved and put on a fresh one, saves the same")
|
|
{
|
|
for (auto const &path : test::real_vehicles())
|
|
{
|
|
CAPTURE(path);
|
|
test::reseed_random();
|
|
vehicle first{definition::fiz(path), 0.0, "test", 1};
|
|
// something for the snapshot to hold: the battery on, the brakes handled, a few seconds of air moving about
|
|
first->BatterySwitch(true);
|
|
first->CabActivisation();
|
|
first->BrakeLevelSet(first->brakes.driverValve->GetPos(bh_RP));
|
|
first.run(3.0, wired);
|
|
first->BrakeLevelSet(first->brakes.driverValve->GetPos(bh_FB));
|
|
first.run(2.0, wired);
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
|
|
test::reseed_random();
|
|
vehicle second{definition::fiz(path), 0.0, "test", 1};
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
auto const restored{state_of(second.physics())};
|
|
CHECK_MESSAGE(restored == saved, differences(saved, restored));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("snapshot: the state of a vehicle with a load and one without")
|
|
{
|
|
// the first real vehicle made to carry something
|
|
std::string path;
|
|
for (auto const &candidate : test::real_vehicles())
|
|
{
|
|
if (vehicle probe{definition::fiz(candidate)}; probe->MaxLoad() > 0.f && false == probe->LoadAttributes.empty())
|
|
{
|
|
path = candidate;
|
|
break;
|
|
}
|
|
}
|
|
REQUIRE_FALSE(path.empty());
|
|
CAPTURE(path);
|
|
auto const wagon{definition::fiz(path)};
|
|
vehicle loaded{wagon};
|
|
auto const loadname{loaded->LoadAttributes.front().name};
|
|
REQUIRE(loaded->AssignLoad(loadname, 0.5f * loaded->MaxLoad()));
|
|
loaded->ComputeMass();
|
|
auto const saved{through_text(state_of(loaded.physics()))};
|
|
CHECK(saved.text("loadtype") == loadname);
|
|
|
|
vehicle empty{wagon};
|
|
auto const emptymass{empty->TotalMass()};
|
|
snapshot::apply_mover(empty.physics(), saved);
|
|
CHECK(empty->LoadAmount() == loaded->LoadAmount());
|
|
CHECK(empty->LoadType.name == loadname);
|
|
CHECK(empty->TotalMass() == doctest::Approx(loaded->TotalMass()));
|
|
CHECK(empty->TotalMass() > emptymass);
|
|
|
|
// and the other way: a vehicle loaded by the scenery, empty in the snapshot
|
|
vehicle unloaded{wagon};
|
|
auto const emptied{through_text(state_of(unloaded.physics()))};
|
|
snapshot::apply_mover(empty.physics(), emptied);
|
|
CHECK(empty->LoadAmount() == 0.f);
|
|
CHECK(empty->LoadType.name.empty());
|
|
CHECK(empty->TotalMass() == doctest::Approx(emptymass));
|
|
}
|
|
|
|
TEST_CASE("snapshot: an entry without the state of the machinery leaves the vehicle as it is")
|
|
{
|
|
vehicle locomotive{definition::fiz("4e_v1/4e"), 0.0, "test", 1};
|
|
run_locomotive(locomotive, 5, 5.0);
|
|
auto const before{state_of(locomotive.physics())};
|
|
// what an older file, or one written by hand, may hold
|
|
snapshot::record entry{"vehicle", "test"};
|
|
entry.set("unknown", 12).set("mainctrl", locomotive->MainCtrlPos());
|
|
snapshot::apply_mover(locomotive.physics(), entry);
|
|
auto const after{state_of(locomotive.physics())};
|
|
CHECK_MESSAGE(after == before, differences(before, after));
|
|
}
|
|
|
|
TEST_CASE("snapshot: a locomotive restored on the move runs on like the one it was saved from")
|
|
{
|
|
test::reseed_random();
|
|
vehicle first{definition::fiz("4e_v1/4e"), 0.0, "test", 1};
|
|
run_locomotive(first, 12, 20.0);
|
|
REQUIRE(first->lineBreaker.IsClosed());
|
|
REQUIRE(first.speed() > 20.0);
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
|
|
test::reseed_random();
|
|
vehicle second{definition::fiz("4e_v1/4e"), 0.0, "test", 1};
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
CHECK(second.speed() == first.speed());
|
|
CHECK(second->lineBreaker.IsClosed());
|
|
CHECK(second->MainCtrlPos() == first->MainCtrlPos());
|
|
|
|
// both carry on under power for a minute
|
|
auto const firststart{first->DistCounter()};
|
|
auto const secondstart{second->DistCounter()};
|
|
drive_on(first, 60.0);
|
|
drive_on(second, 60.0);
|
|
CAPTURE(first.speed());
|
|
CAPTURE(second.speed());
|
|
CHECK(second->lineBreaker.IsClosed());
|
|
CHECK(second->converter.IsRunning() == first->converter.IsRunning());
|
|
CHECK(second.speed() == doctest::Approx(first.speed()).epsilon(0.02));
|
|
CHECK(second->DistCounter() - secondstart == doctest::Approx(first->DistCounter() - firststart).epsilon(0.02));
|
|
CHECK(second->brakePipe.Pressure() == doctest::Approx(first->brakePipe.Pressure()).epsilon(0.02));
|
|
CHECK(second->mainPipe.Pressure() == doctest::Approx(first->mainPipe.Pressure()).epsilon(0.05));
|
|
|
|
// then both brake to a stand
|
|
for (auto *locomotive : {&first, &second})
|
|
{
|
|
while ((*locomotive)->DecMainCtrl(1))
|
|
{
|
|
// power off
|
|
}
|
|
(*locomotive)->BrakeLevelSet((*locomotive)->brakes.driverValve->GetPos(bh_FB));
|
|
}
|
|
auto const firstbraking{first->DistCounter()};
|
|
auto const secondbraking{second->DistCounter()};
|
|
drive_on(first, 120.0);
|
|
drive_on(second, 120.0);
|
|
CHECK(first.speed() == doctest::Approx(0.0).epsilon(0.01));
|
|
CHECK(second.speed() == doctest::Approx(0.0).epsilon(0.01));
|
|
CHECK(second->DistCounter() - secondbraking == doctest::Approx(first->DistCounter() - firstbraking).epsilon(0.03));
|
|
CHECK(second->brakes.cylinderPressure == doctest::Approx(first->brakes.cylinderPressure).epsilon(0.03));
|
|
}
|
|
|
|
TEST_CASE("snapshot: a locomotive restored in the middle of braking stops where the one it was saved from stops")
|
|
{
|
|
test::reseed_random();
|
|
vehicle first{definition::fiz("4e_v1/4e"), 0.0, "test", 1};
|
|
run_locomotive(first, 14, 40.0);
|
|
REQUIRE(first.speed() > 40.0);
|
|
while (first->DecMainCtrl(1))
|
|
{
|
|
// power off
|
|
}
|
|
first->BrakeLevelSet(first->brakes.driverValve->GetPos(bh_FB));
|
|
// the pressure in the cylinders still rising
|
|
drive_on(first, 2.0);
|
|
REQUIRE(first->brakes.cylinderPressure > 0.1);
|
|
REQUIRE(first.speed() > 20.0);
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
|
|
test::reseed_random();
|
|
vehicle second{definition::fiz("4e_v1/4e"), 0.0, "test", 1};
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
CHECK(second->brakes.cylinderPressure == first->brakes.cylinderPressure);
|
|
CHECK(second->brakes.distributor->GetBCP() == first->brakes.distributor->GetBCP());
|
|
CHECK(second->brakes.distributor->GetCRP() == first->brakes.distributor->GetCRP());
|
|
CHECK(second->brakes.distributor->GetBRP() == first->brakes.distributor->GetBRP());
|
|
|
|
auto const firststart{first->DistCounter()};
|
|
auto const secondstart{second->DistCounter()};
|
|
drive_on(first, 120.0);
|
|
drive_on(second, 120.0);
|
|
CHECK(first.speed() == doctest::Approx(0.0).epsilon(0.01));
|
|
CHECK(second.speed() == doctest::Approx(0.0).epsilon(0.01));
|
|
CAPTURE(first->DistCounter() - firststart);
|
|
CAPTURE(second->DistCounter() - secondstart);
|
|
CHECK(second->DistCounter() - secondstart == doctest::Approx(first->DistCounter() - firststart).epsilon(0.03));
|
|
CHECK(second->brakes.cylinderPressure == doctest::Approx(first->brakes.cylinderPressure).epsilon(0.03));
|
|
CHECK(second->brakePipe.Pressure() == doctest::Approx(first->brakePipe.Pressure()).epsilon(0.03));
|
|
}
|
|
|
|
namespace {
|
|
|
|
// a train of the given vehicles, the first one with the driver
|
|
void make_train(test::consist &Train, std::vector<char const *> const &Vehicles)
|
|
{
|
|
test::reseed_random();
|
|
for (auto const *path : Vehicles)
|
|
Train.add(definition::fiz(path), Train.size() == 0 ? 1 : 0);
|
|
}
|
|
|
|
// puts the state of each vehicle of a train on the same vehicle of another, and checks that it took
|
|
void restore_train(test::consist &To, test::consist &From)
|
|
{
|
|
REQUIRE(To.size() == From.size());
|
|
for (std::size_t index{0}; index < From.size(); ++index)
|
|
snapshot::apply_mover(To[index].physics(), through_text(state_of(From[index].physics())));
|
|
for (std::size_t index{0}; index < From.size(); ++index)
|
|
{
|
|
CAPTURE(index);
|
|
auto const saved{state_of(From[index].physics())};
|
|
auto const restored{state_of(To[index].physics())};
|
|
CHECK_MESSAGE(restored == saved, differences(saved, restored));
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("snapshot: a multiple unit restored on the move runs on like the one it was saved from")
|
|
{
|
|
std::vector<char const *> const cars{"ed72_v1/5bs-ra", "ed72_v1/6bs-sa", "ed72_v1/6bs-sb", "ed72_v1/5bs-rb"};
|
|
test::consist first;
|
|
make_train(first, cars);
|
|
auto const driven{test::driving(first)};
|
|
REQUIRE(test::start_up(driven, wired));
|
|
test::release_brakes(first[0].physics());
|
|
test::drive(driven, wired, 20.0);
|
|
for (auto notch{0}; notch < 2; ++notch)
|
|
{
|
|
first[0]->IncMainCtrl(1);
|
|
test::drive(driven, wired, 2.0);
|
|
}
|
|
test::drive(driven, wired, 40.0);
|
|
REQUIRE(first[0].speed() > 20.0);
|
|
|
|
test::consist second;
|
|
make_train(second, cars);
|
|
restore_train(second, first);
|
|
|
|
test::drive(driven, wired, 60.0);
|
|
test::drive(test::driving(second), wired, 60.0);
|
|
CAPTURE(first[0].speed());
|
|
CAPTURE(second[0].speed());
|
|
for (std::size_t index{0}; index < first.size(); ++index)
|
|
{
|
|
CAPTURE(index);
|
|
CHECK(second[index]->lineBreaker.IsClosed() == first[index]->lineBreaker.IsClosed());
|
|
CHECK(second[index]->converter.IsRunning() == first[index]->converter.IsRunning());
|
|
CHECK(second[index].speed() == doctest::Approx(first[index].speed()).epsilon(0.02));
|
|
CHECK(second[index]->brakePipe.Pressure() == doctest::Approx(first[index]->brakePipe.Pressure()).epsilon(0.02));
|
|
CHECK(second[index]->mainPipe.Pressure() == doctest::Approx(first[index]->mainPipe.Pressure()).epsilon(0.03));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("snapshot: a locomotive with wagons restored while it brakes stops like the train it was saved from")
|
|
{
|
|
std::vector<char const *> const vehicles{"4e_v1/4e", "3wl_v2/3wl", "11xa_v2/93a", "bdhpumn_v3/bdhpumn", "z1_v2/152a"};
|
|
test::consist first;
|
|
make_train(first, vehicles);
|
|
auto const driven{test::driving(first)};
|
|
REQUIRE(test::start_up(driven, wired));
|
|
test::release_brakes(first[0].physics());
|
|
test::drive(driven, wired, 30.0);
|
|
for (auto notch{0}; notch < 14; ++notch)
|
|
{
|
|
first[0]->IncMainCtrl(1);
|
|
test::drive(driven, wired, 2.0);
|
|
}
|
|
test::drive(driven, wired, 40.0);
|
|
REQUIRE(first[0].speed() > 30.0);
|
|
while (first[0]->DecMainCtrl(1))
|
|
{
|
|
// power off
|
|
}
|
|
first[0]->BrakeLevelSet(first[0]->brakes.driverValve->GetPos(bh_FB));
|
|
// the brake pipe still emptying along the train, the cylinders filling
|
|
test::drive(driven, wired, 3.0);
|
|
REQUIRE(first[4]->brakes.cylinderPressure > 0.05);
|
|
|
|
test::consist second;
|
|
make_train(second, vehicles);
|
|
restore_train(second, first);
|
|
|
|
auto const firststart{first[0]->DistCounter()};
|
|
auto const secondstart{second[0]->DistCounter()};
|
|
test::drive(driven, wired, 120.0);
|
|
test::drive(test::driving(second), wired, 120.0);
|
|
CHECK(first[0].speed() == doctest::Approx(0.0).epsilon(0.01));
|
|
CHECK(second[0].speed() == doctest::Approx(0.0).epsilon(0.01));
|
|
CAPTURE(first[0]->DistCounter() - firststart);
|
|
CAPTURE(second[0]->DistCounter() - secondstart);
|
|
CHECK(second[0]->DistCounter() - secondstart == doctest::Approx(first[0]->DistCounter() - firststart).epsilon(0.02));
|
|
for (std::size_t index{0}; index < first.size(); ++index)
|
|
{
|
|
CAPTURE(index);
|
|
CHECK(second[index]->brakes.cylinderPressure == doctest::Approx(first[index]->brakes.cylinderPressure).epsilon(0.02).scale(4.0));
|
|
CHECK(second[index]->brakePipe.Pressure() == doctest::Approx(first[index]->brakePipe.Pressure()).epsilon(0.02).scale(5.0));
|
|
CHECK(second[index]->brakes.distributor->GetCRP() == doctest::Approx(first[index]->brakes.distributor->GetCRP()).epsilon(0.02).scale(5.0));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("snapshot: a diesel locomotive restored with its engine running keeps it running")
|
|
{
|
|
test::reseed_random();
|
|
vehicle first{definition::fiz("sm42_v1/6d1"), 0.0, "test", 1};
|
|
auto const driven{test::driving(first)};
|
|
REQUIRE(test::start_up(driven, test::track{}));
|
|
test::release_brakes(first.physics());
|
|
drive_on(first, 30.0, test::track{});
|
|
REQUIRE(first->lineBreaker.IsClosed());
|
|
REQUIRE(first->enrot > 0.0);
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
|
|
test::reseed_random();
|
|
vehicle second{definition::fiz("sm42_v1/6d1"), 0.0, "test", 1};
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
drive_on(first, 30.0, test::track{});
|
|
drive_on(second, 30.0, test::track{});
|
|
CHECK(second->lineBreaker.IsClosed());
|
|
CHECK(second->enrot == doctest::Approx(first->enrot).epsilon(0.02));
|
|
CHECK(second->diesel.heat.Ts == doctest::Approx(first->diesel.heat.Ts).epsilon(0.02));
|
|
CHECK(second->mainPipe.Pressure() == doctest::Approx(first->mainPipe.Pressure()).epsilon(0.05));
|
|
}
|
|
|
|
TEST_CASE("snapshot: every real vehicle which starts up, restored while it runs, runs on like the one it was saved from")
|
|
{
|
|
for (auto const &path : test::real_vehicles())
|
|
{
|
|
CAPTURE(path);
|
|
test::reseed_random();
|
|
vehicle first{definition::fiz(path), 0.0, "test", 1};
|
|
auto const track{first->EnginePowerSource.SourceType == TPowerSource::CurrentCollector ? wired : test::track{}};
|
|
if (false == test::is_powered(first.physics()) || false == test::start_up(test::driving(first), track))
|
|
continue;
|
|
test::release_brakes(first.physics());
|
|
for (auto notch{0}; notch < 3; ++notch)
|
|
{
|
|
first->IncMainCtrl(1);
|
|
drive_on(first, 3.0, track);
|
|
}
|
|
drive_on(first, 10.0, track);
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
|
|
test::reseed_random();
|
|
vehicle second{definition::fiz(path), 0.0, "test", 1};
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
drive_on(first, 20.0, track);
|
|
drive_on(second, 20.0, track);
|
|
CHECK(second->lineBreaker.IsClosed() == first->lineBreaker.IsClosed());
|
|
CHECK(second->converter.IsRunning() == first->converter.IsRunning());
|
|
CHECK(second->air.compressor.IsRunning() == first->air.compressor.IsRunning());
|
|
CHECK(second.speed() == doctest::Approx(first.speed()).epsilon(0.02).scale(10.0));
|
|
CHECK(second->enrot == doctest::Approx(first->enrot).epsilon(0.02).scale(1.0));
|
|
CHECK(second->brakePipe.Pressure() == doctest::Approx(first->brakePipe.Pressure()).epsilon(0.01).scale(5.0));
|
|
CHECK(second->brakes.cylinderPressure == doctest::Approx(first->brakes.cylinderPressure).epsilon(0.01).scale(5.0));
|
|
CHECK(second->mainPipe.Pressure() == doctest::Approx(first->mainPipe.Pressure()).epsilon(0.01).scale(8.0));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("snapshot: what a brake valve keeps beside its air comes back on a fresh vehicle")
|
|
{
|
|
for (auto const &path : test::real_vehicles())
|
|
{
|
|
CAPTURE(path);
|
|
test::reseed_random();
|
|
vehicle first{definition::fiz(path), 0.0, "test", 1};
|
|
REQUIRE(first->brakes.distributor != nullptr);
|
|
// every latch the other way than a fresh valve has it, every level another one
|
|
auto latches{first->brakes.distributor->latches()};
|
|
REQUIRE(latches.size() >= 2);
|
|
for (auto &value : latches)
|
|
value = (value == 0.0 ? 1.0 : 0.0);
|
|
first->brakes.distributor->latches(latches);
|
|
REQUIRE(first->brakes.distributor->latches() == latches);
|
|
// and a flow its auxiliary reservoir has yet to take in
|
|
auto const reservoirs{first->brakes.distributor->reservoirs()};
|
|
REQUIRE(reservoirs.size() >= 3);
|
|
REQUIRE(reservoirs[1] != nullptr);
|
|
reservoirs[1]->Flow(0.125);
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
|
|
test::reseed_random();
|
|
vehicle second{definition::fiz(path), 0.0, "test", 1};
|
|
CHECK(second->brakes.distributor->latches() != latches);
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
CHECK(second->brakes.distributor->latches() == latches);
|
|
CHECK(second->brakes.distributor->reservoirs()[1]->GetPendingFlow() == 0.125);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("snapshot: positions of the controllers past the ranges of the vehicle are brought within them")
|
|
{
|
|
snapshot::take_refusals();
|
|
vehicle locomotive{definition::fiz("4e_v1/4e"), 0.0, "test", 1};
|
|
REQUIRE(locomotive->MainCtrlPosNo > 0);
|
|
auto entry{state_of(locomotive.physics())};
|
|
// what an edited file, or one saved with a definition of more positions than the vehicle has now, may hold
|
|
entry.set("mainctrl", 999).set("mainctrlactual", -4).set("scndctrl", locomotive->ScndCtrlPosNo() + 5).set("scndctrlactual", 1000);
|
|
entry.set("brakectrl", 99).set("manualbrake", -3).set("lightspos", 500).set("compressorlistpos", 40).set("wiperswitchpos", 16);
|
|
entry.set("caboccupied", 7).set("diractive", -2).set("dimmer", 1000);
|
|
snapshot::apply_mover(locomotive.physics(), through_text(entry));
|
|
CHECK(locomotive->MainCtrlPos() == locomotive->MainCtrlPosNo);
|
|
CHECK(locomotive->MainCtrlActualPos == 0);
|
|
CHECK(locomotive->ScndCtrlPos() == locomotive->ScndCtrlPosNo());
|
|
CHECK(locomotive->ScndCtrlActualPos == locomotive->ScndCtrlPosNo());
|
|
CHECK(locomotive->brakes.HandlePosition() == std::max(locomotive->brakes.config.handlePositions, 0));
|
|
CHECK(locomotive->brakes.HandBrakePosition() == 0);
|
|
CHECK(locomotive->LightsPos() <= 18);
|
|
CHECK(locomotive->LightsPos() == std::min(locomotive->LightsPosNo, 18));
|
|
CHECK(locomotive->air.compressor.ListPosition() <= 8);
|
|
CHECK(locomotive->WiperSwitchPos() == 15);
|
|
CHECK(locomotive->CabOccupied() == 1);
|
|
CHECK(locomotive->DirActive == -1);
|
|
CHECK(locomotive->DimmerPosition() == static_cast<int>(locomotive->dimPositions.size()) - 1);
|
|
auto const refusals{snapshot::take_refusals()};
|
|
CAPTURE(refusals.size());
|
|
CHECK(refusals.size() >= 10);
|
|
|
|
// a value which isn't a whole number at all isn't taken, and the position stays as it was
|
|
auto const before{locomotive->MainCtrlPos()};
|
|
snapshot::record broken{"vehicle", "test"};
|
|
broken.fields = {{"mainctrl", "1e18"}, {"mainctrlactual", "nan"}, {"scndctrl", "2.5"}};
|
|
snapshot::apply_mover(locomotive.physics(), broken);
|
|
CHECK(locomotive->MainCtrlPos() == before);
|
|
CHECK(snapshot::take_refusals().size() == 3);
|
|
}
|
|
|
|
TEST_CASE("snapshot: the pullee mode and the timer of the release of the holding brake come back")
|
|
{
|
|
// nobody in it: a vehicle with a cab taken isn't prepared for being hauled
|
|
vehicle first{definition::fiz("4e_v1/4e")};
|
|
REQUIRE_FALSE(first->PulleeMode());
|
|
REQUIRE(first->PulleeModeSwitch(true));
|
|
first->brakes.holding.releaseTimer = 1.25;
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
CHECK(saved.text("pulleemode") == "1");
|
|
CHECK(saved.text("stopbrakereleasetimer") == "1.25");
|
|
|
|
vehicle second{definition::fiz("4e_v1/4e")};
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
CHECK(second->PulleeMode());
|
|
CHECK(second->brakes.holding.releaseTimer == 1.25);
|
|
auto const restored{state_of(second.physics())};
|
|
CHECK_MESSAGE(restored == saved, differences(saved, restored));
|
|
}
|
|
|
|
TEST_CASE("snapshot: a load which can't be one is refused")
|
|
{
|
|
snapshot::take_refusals();
|
|
std::string path;
|
|
for (auto const &candidate : test::real_vehicles())
|
|
{
|
|
if (vehicle probe{definition::fiz(candidate)}; probe->MaxLoad() > 0.f && false == probe->LoadAttributes.empty())
|
|
{
|
|
path = candidate;
|
|
break;
|
|
}
|
|
}
|
|
REQUIRE_FALSE(path.empty());
|
|
vehicle wagon{definition::fiz(path)};
|
|
auto const loadname{wagon->LoadAttributes.front().name};
|
|
for (auto const *amount : {"nan", "-1", "inf", "1e30"})
|
|
{
|
|
CAPTURE(amount);
|
|
snapshot::record entry{"vehicle", "test"};
|
|
entry.fields = {{"load", amount}, {"loadtype", loadname}};
|
|
snapshot::apply_mover(wagon.physics(), entry);
|
|
CHECK(wagon->LoadAmount() == 0.f);
|
|
CHECK(wagon->LoadType.name.empty());
|
|
}
|
|
// a load is named by its model, which is looked up in the folder of the vehicle
|
|
snapshot::record entry{"vehicle", "test"};
|
|
entry.fields = {{"load", "1"}, {"loadtype", "../../../models/elsewhere"}};
|
|
snapshot::apply_mover(wagon.physics(), entry);
|
|
CHECK(wagon->LoadType.name.empty());
|
|
CHECK(snapshot::take_refusals().size() == 5);
|
|
}
|
|
|
|
namespace {
|
|
|
|
// the pressures a driver's handle of the kind gives to a snapshot: each of its variables once, as a second copy of
|
|
// one would be saved twice and put back twice
|
|
template <typename Handle>
|
|
void pressures_listed_once(char const *Kind)
|
|
{
|
|
CAPTURE(Kind);
|
|
Handle handle;
|
|
auto const pressures{handle.pressures()};
|
|
std::set<double const *> const distinct(pressures.begin(), pressures.end());
|
|
CHECK(distinct.size() == pressures.size());
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("snapshot: a driver's handle lists each of its pressures once")
|
|
{
|
|
pressures_listed_once<TFV4a>("FV4a");
|
|
pressures_listed_once<TFV4aM>("FV4aM");
|
|
pressures_listed_once<TMHZ_EN57>("MHZ_EN57");
|
|
pressures_listed_once<TMHZ_K5P>("MHZ_K5P");
|
|
pressures_listed_once<TMHZ_6P>("MHZ_6P");
|
|
pressures_listed_once<TM394>("M394");
|
|
pressures_listed_once<TH14K1>("H14K1");
|
|
pressures_listed_once<TSt113>("St113");
|
|
pressures_listed_once<Ttest>("test");
|
|
pressures_listed_once<TFD1>("FD1");
|
|
pressures_listed_once<TH1405>("H1405");
|
|
pressures_listed_once<TFVel6>("FVel6");
|
|
pressures_listed_once<TFVE408>("FVE408");
|
|
}
|
|
|
|
TEST_CASE("snapshot: the pressures of the Knorr St113 come back on a fresh vehicle")
|
|
{
|
|
test::reseed_random();
|
|
vehicle first{definition::fiz("en57_v1/6bai"), 0.0, "test", 1};
|
|
REQUIRE(first->brakes.config.driverValveType == TBrakeHandle::St113);
|
|
REQUIRE(first->brakes.driverValve != nullptr);
|
|
// the control chamber, the reductor and the EP intensity, none of them what a fresh handle has
|
|
auto const pressures{first->brakes.driverValve->pressures()};
|
|
std::vector<double> const values{4.625, 0.125, -1.0};
|
|
REQUIRE(pressures.size() == values.size());
|
|
for (std::size_t index{0}; index < values.size(); ++index)
|
|
*pressures[index] = values[index];
|
|
CHECK(first->brakes.driverValve->GetCP() == values[0]);
|
|
CHECK(first->brakes.driverValve->GetRP() == 5.0 + values[1]);
|
|
CHECK(first->brakes.driverValve->GetEP() == values[2]);
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
CHECK(snapshot::text_numbers(saved.text("handle")) == values);
|
|
|
|
test::reseed_random();
|
|
vehicle second{definition::fiz("en57_v1/6bai"), 0.0, "test", 1};
|
|
REQUIRE(second->brakes.driverValve != nullptr);
|
|
CHECK(second->brakes.driverValve->GetCP() != values[0]);
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
CHECK(second->brakes.driverValve->GetCP() == values[0]);
|
|
CHECK(second->brakes.driverValve->GetRP() == 5.0 + values[1]);
|
|
CHECK(second->brakes.driverValve->GetEP() == values[2]);
|
|
auto const restored{state_of(second.physics())};
|
|
CHECK_MESSAGE(restored == saved, differences(saved, restored));
|
|
}
|
|
|
|
namespace {
|
|
|
|
// the given model of derailment for the test, restored after it
|
|
class derailment_model {
|
|
public:
|
|
explicit derailment_model(global_settings::derailment_model const Model) : m_previous{Global.derailment} { Global.derailment = Model; }
|
|
~derailment_model() { Global.derailment = m_previous; }
|
|
derailment_model(derailment_model const &) = delete;
|
|
derailment_model &operator=(derailment_model const &) = delete;
|
|
|
|
private:
|
|
global_settings::derailment_model m_previous;
|
|
};
|
|
|
|
// the entry without the keys
|
|
snapshot::record without(snapshot::record Entry, std::vector<std::string> const &Keys)
|
|
{
|
|
std::erase_if(Entry.fields, [&Keys](auto const &Field) { return std::ranges::find(Keys, Field.first) != Keys.end(); });
|
|
return Entry;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("snapshot: a derailed vehicle comes back derailed, by what derailed it")
|
|
{
|
|
derailment_model const physical{global_settings::derailment_model::physical};
|
|
// an EU07 over the speed it overturns at in a curve of 300 m, without cant
|
|
test::track curve;
|
|
curve.radius = 300.0;
|
|
auto const critical{derailment::overturning_speed(300.0, 0.0, derailment::default_centre_of_gravity(4.348, derailment::vehicle_class::powered), 1.435) * 3.6};
|
|
vehicle first{definition::fiz("4e_v1/4e"), critical + 20.0};
|
|
for (int step = 0; step < 10; ++step)
|
|
first.step(curve);
|
|
REQUIRE(first->Derailed() == derailment::outcome::overturned);
|
|
REQUIRE(first->DerailedBy() == derailment::reason::overturned);
|
|
REQUIRE(first->leaves_track());
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
CHECK(saved.text("derailed") == "2");
|
|
CHECK(saved.text("derailedby") == std::to_string(static_cast<int>(derailment::reason::overturned)));
|
|
CHECK(saved.has("derailmentdistance"));
|
|
|
|
vehicle second{definition::fiz("4e_v1/4e")};
|
|
REQUIRE(second->Derailed() == derailment::outcome::none);
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
CHECK(second->Derailed() == first->Derailed());
|
|
CHECK(second->DerailedBy() == first->DerailedBy());
|
|
CHECK(second->DerailVelocity() == first->DerailVelocity());
|
|
CHECK(second->DerailCause() == first->DerailCause());
|
|
CHECK(second->DerailAcceleration() == first->DerailAcceleration());
|
|
CHECK(second->DerailSide() == first->DerailSide());
|
|
CHECK(second->CollisionDerail() == first->CollisionDerail());
|
|
CHECK(second->DerailmentDistance() == first->DerailmentDistance());
|
|
CHECK(second->DamageFlag.HasAll(VehicleDamage::derailed));
|
|
// its motion on the ground begins with the next step, as it does in the run it was saved from
|
|
CHECK(second->leaves_track());
|
|
auto const restored{state_of(second.physics())};
|
|
CHECK_MESSAGE(restored == saved, differences(saved, restored));
|
|
|
|
SUBCASE("a snapshot written before the derailment was kept leaves the vehicle on the rails as the scenery put it")
|
|
{
|
|
vehicle third{definition::fiz("4e_v1/4e")};
|
|
snapshot::apply_mover(third.physics(),
|
|
without(saved, {"derailed", "derailedby", "derailvelocity", "derailcause", "derailacceleration", "derailside", "collisionderail"}));
|
|
CHECK(third->Derailed() == derailment::outcome::none);
|
|
CHECK(third->DerailedBy() == derailment::reason::none);
|
|
}
|
|
SUBCASE("a cause out of the range of the causes is refused")
|
|
{
|
|
auto edited{saved};
|
|
edited.set("derailed", 7).set("derailedby", 99).set("derailcause", -1).set("derailside", 3);
|
|
vehicle third{definition::fiz("4e_v1/4e")};
|
|
snapshot::take_refusals();
|
|
snapshot::apply_mover(third.physics(), edited);
|
|
CHECK(third->Derailed() == derailment::outcome::none);
|
|
CHECK(third->DerailedBy() == derailment::reason::none);
|
|
CHECK(third->DerailCause() == derail_motion::cause::off_rails);
|
|
CHECK(third->DerailSide() == 0);
|
|
CHECK(snapshot::take_refusals().size() >= 4);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("snapshot: a vehicle on the rails has no derailment in its entry")
|
|
{
|
|
vehicle locomotive{definition::fiz("4e_v1/4e")};
|
|
auto const saved{state_of(locomotive.physics())};
|
|
CHECK_FALSE(saved.has("derailed"));
|
|
CHECK_FALSE(saved.has("derailedby"));
|
|
CHECK(saved.has("derailmentdistance"));
|
|
}
|
|
|
|
TEST_CASE("snapshot: the brakes a vehicle fell asleep with come back")
|
|
{
|
|
vehicle first{definition::fiz("4e_v1/4e")};
|
|
first->sleep_brakes.cylinder = 2.5;
|
|
first->sleep_brakes.spring = 0.25;
|
|
first->sleep_brakes.manual = 3;
|
|
first->sleep_brakes.local = 0.5;
|
|
auto const saved{through_text(state_of(first.physics()))};
|
|
CHECK(snapshot::text_numbers(saved.text("sleepbrakes")) == std::vector<double>{2.5, 0.25, 3.0, 0.5});
|
|
|
|
vehicle second{definition::fiz("4e_v1/4e")};
|
|
snapshot::apply_mover(second.physics(), saved);
|
|
CHECK(second->sleep_brakes.cylinder == 2.5);
|
|
CHECK(second->sleep_brakes.spring == 0.25);
|
|
CHECK(second->sleep_brakes.manual == 3);
|
|
CHECK(second->sleep_brakes.local == 0.5);
|
|
|
|
// a hand brake position the vehicle hasn't: the whole key is refused
|
|
auto edited{saved};
|
|
edited.set("sleepbrakes", snapshot::numbers_text(std::vector<double>{1.0, 1.0, 99.0, 1.0}));
|
|
vehicle third{definition::fiz("4e_v1/4e")};
|
|
snapshot::take_refusals();
|
|
snapshot::apply_mover(third.physics(), edited);
|
|
CHECK(third->sleep_brakes.manual == 0);
|
|
CHECK(third->sleep_brakes.cylinder == 0.0);
|
|
CHECK(snapshot::take_refusals().size() == 1);
|
|
}
|
|
|
|
TEST_CASE("snapshot: the motion of a vehicle off the track, put on another one, goes on the same")
|
|
{
|
|
namespace dm = derail_motion;
|
|
dm::vehicle_shape shape;
|
|
shape.length = 15.915;
|
|
shape.width = 2.903;
|
|
shape.height = 4.348;
|
|
shape.centre_of_gravity = derailment::default_centre_of_gravity(4.348, derailment::vehicle_class::powered);
|
|
shape.contact_spacing = derailment::contact_spacing(1.435);
|
|
shape.base = 8.0;
|
|
dm::start start;
|
|
start.speed = 25.0;
|
|
start.reason = dm::cause::overturned;
|
|
start.outward = 1;
|
|
start.acceleration = 1.05 * derailment::overturning_acceleration(shape.centre_of_gravity, derailment::contact_spacing(1.435));
|
|
dm::flat_ground const ballast{-0.2};
|
|
dm::scatter scatter;
|
|
scatter.yaw = 0.01;
|
|
scatter.dig_in = 0.5;
|
|
scatter.dig_in_time = 0.4;
|
|
scatter.dig_in_friction = 3.0;
|
|
|
|
// the speed falls with the friction of the motion, as physics::Vehicle has it
|
|
auto const run = [&ballast](dm::body &Body, double &Speed, int const Steps) {
|
|
constexpr double dt{0.01};
|
|
for (int step = 0; step < Steps; ++step)
|
|
{
|
|
auto const next{std::max(Speed - dm::gravity * Body.friction() * dt, 0.0)};
|
|
Body.step(dt, 0.5 * (Speed + next) * dt, next, ballast);
|
|
Speed = next;
|
|
}
|
|
};
|
|
dm::body first;
|
|
first.begin(start, shape, ballast, scatter);
|
|
auto speed{start.speed};
|
|
run(first, speed, 120);
|
|
REQUIRE(first.active());
|
|
auto const saved{first.state()};
|
|
auto const text{snapshot::numbers_text(saved)};
|
|
|
|
dm::body second;
|
|
REQUIRE(second.state(snapshot::text_numbers(text)));
|
|
CHECK(second.state() == saved);
|
|
CHECK(second.active());
|
|
CHECK(second.current().position == first.current().position);
|
|
// both run on by the same steps and come out the same, bit for bit
|
|
auto secondspeed{speed};
|
|
run(first, speed, 300);
|
|
run(second, secondspeed, 300);
|
|
CHECK(second.state() == first.state());
|
|
|
|
SUBCASE("a list of another length, or with a cause out of its range, leaves the motion as it was")
|
|
{
|
|
dm::body third;
|
|
auto shorter{saved};
|
|
shorter.pop_back();
|
|
CHECK_FALSE(third.state(shorter));
|
|
CHECK_FALSE(third.active());
|
|
auto wrong{saved};
|
|
wrong[1] = 9.0; // the cause
|
|
CHECK_FALSE(third.state(wrong));
|
|
CHECK_FALSE(third.active());
|
|
wrong[1] = 0.5;
|
|
CHECK_FALSE(third.state(wrong));
|
|
}
|
|
}
|