mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 06:12:41 +01:00
84f1aa2515
The positions of the master and the secondary controller, of the analog
controller and of the lights selector, the occupied cab, the horns, the radio
and its fuse, the radio-stop, the wipers' switch, the dimmer, the mirrors'
button, the end-of-train signalling and the forced resistor fans: fourteen
fields the cab, the AI, the scenery and the tests wrote straight into
physics::Vehicle are private now. The rest of the simulator reads them through
queries named as the fields were (MainCtrlPos(), Radio(), ...) and changes them
through operations (SetMainCtrlPos(), SetRadio(), ...), so every place that
moves a controller past its own stepping (IncMainCtrl()) is named and can be
found; the compiler keeps anything else out. Inside the vehicle they keep
their meaning under camelCase names. The queries return const references, as
the gauges of the cab bind to them.
The .fiz keys are untouched.
No change in behaviour: the golden traces recorded on dfb43c4f match bit for bit.
383 lines
13 KiB
C++
383 lines
13 KiB
C++
#include "stdafx.h"
|
|
#include "vehicle.h"
|
|
#include "golden.h"
|
|
#include "real_vehicles.h"
|
|
#include "fiz.h"
|
|
#include "vehicle/DynObj.h"
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <stdexcept>
|
|
|
|
#include "utilities/Globals.h"
|
|
|
|
namespace test {
|
|
|
|
namespace {
|
|
|
|
fiz::section const &format_of(std::string const &Section) {
|
|
auto const section{fiz::named(Section)};
|
|
if (section == nullptr) {
|
|
throw definition_error("vehicle definition section " + Section + " isn't one the loader reads, see tests/unit/fiz.cpp");
|
|
}
|
|
return *section;
|
|
}
|
|
|
|
std::string value_text(nlohmann::ordered_json const &Value) {
|
|
if (Value.is_string()) {
|
|
return Value.get<std::string>();
|
|
}
|
|
if (Value.is_boolean()) {
|
|
return Value.get<bool>() ? "Yes" : "No";
|
|
}
|
|
return Value.dump();
|
|
}
|
|
|
|
// the rows of a list section, each a list of values, and the line closing them
|
|
std::string table_text(std::string const &Section, fiz::section const &Format, nlohmann::ordered_json const &Rows) {
|
|
if (Format.end == nullptr) {
|
|
throw definition_error("vehicle definition section " + Section + " has no rows");
|
|
}
|
|
std::string text;
|
|
for (auto const &row : Rows) {
|
|
for (auto const &value : row) {
|
|
text += value_text(value) + " ";
|
|
}
|
|
text += "\n";
|
|
}
|
|
return text + Format.end + "\n";
|
|
}
|
|
|
|
nlohmann::ordered_json sections_of(std::string const &Path) {
|
|
std::ifstream file(Path);
|
|
if (false == file.is_open()) {
|
|
throw definition_error("no test vehicle definition " + Path);
|
|
}
|
|
// not brace-initialised: json{other} makes an array of other with GCC and Clang (the initializer list constructor wins)
|
|
auto const json = nlohmann::ordered_json::parse(file, nullptr, true, true);
|
|
if (false == json.is_object()) {
|
|
throw definition_error("test vehicle definition " + Path + " isn't a JSON object");
|
|
}
|
|
auto result = nlohmann::ordered_json::object();
|
|
for (auto const &[section, keys] : json.items()) {
|
|
// keys starting with _ are comments
|
|
if (section.starts_with('_')) {
|
|
continue;
|
|
}
|
|
format_of(section);
|
|
result[section] = keys;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
definition definition::load(std::string const &Name) {
|
|
definition result;
|
|
result.m_sections = sections_of(std::string{EU07_UNIT_TESTS_DATA} + "/" + Name + ".json");
|
|
return result;
|
|
}
|
|
|
|
definition definition::fiz(std::string const &Path) {
|
|
definition result;
|
|
result.m_sections = sections_of(std::string{EU07_UNIT_TESTS_DATA} + "/fiz/" + Path + ".json");
|
|
auto const slash{Path.rfind('/')};
|
|
result.m_typename = slash == std::string::npos ? Path : Path.substr(slash + 1);
|
|
return result;
|
|
}
|
|
|
|
definition &definition::set(std::string const &Section, std::string const &Key, nlohmann::ordered_json const &Value) {
|
|
format_of(Section);
|
|
m_sections[Section][Key] = Value;
|
|
return *this;
|
|
}
|
|
|
|
definition &definition::remove(std::string const &Section) {
|
|
m_sections.erase(Section);
|
|
return *this;
|
|
}
|
|
|
|
definition &definition::remove(std::string const &Section, std::string const &Key) {
|
|
if (m_sections.contains(Section)) {
|
|
m_sections[Section].erase(Key);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
std::string definition::text() const {
|
|
std::string text;
|
|
for (auto const &[section, keys] : m_sections.items()) {
|
|
auto const &format{format_of(section)};
|
|
text += section + format.separator;
|
|
for (auto const &[key, value] : keys.items()) {
|
|
if (key != "rows") {
|
|
text += " " + key + "=" + value_text(value);
|
|
}
|
|
}
|
|
text += "\n";
|
|
if (keys.contains("rows")) {
|
|
text += table_text(section, format, keys["rows"]);
|
|
}
|
|
}
|
|
return text;
|
|
}
|
|
|
|
vehicle::vehicle(definition const &Definition, double Velocity, std::string const &Name, int Cab) : m_cab(Cab) {
|
|
// the same steps as TDynamicObject::Init(), for a vehicle set the usual way round (iDirection 1)
|
|
auto const typename_{Definition.type_name().empty() ? Name : Definition.type_name()};
|
|
m_physics = std::make_unique<physics::Vehicle>(Velocity, typename_, Name, Cab);
|
|
if (false == m_physics->LoadFIZ_Text(Definition.text())) {
|
|
throw definition_error("the test vehicle definition didn't load");
|
|
}
|
|
m_physics->AssignLoad("", 0.f);
|
|
m_physics->ComputeMass();
|
|
auto const driveractive{Velocity != 0.0};
|
|
if (false == m_physics->CheckLocomotiveParameters(driveractive, (Velocity > 0 ? 1 : -1) * Cab)) {
|
|
throw definition_error("parameters mismatch in the test vehicle definition");
|
|
}
|
|
m_physics->SetMainCtrlPos(m_physics->MainCtrlNoPowerPos());
|
|
m_physics->brakes.independentPosition = 0.0;
|
|
if (driveractive) {
|
|
m_physics->brakes.handlePosition = static_cast<int>(std::floor(m_physics->brakes.driverValve->GetPos(Cab == 0 ? bh_NP : bh_RP)));
|
|
// a driver keeps a vehicle on standby on its independent brake
|
|
if (Cab != 0 && m_physics->brakes.config.localKind != TLocalBrake::ManualBrake && Velocity < 1.0) {
|
|
m_physics->IncLocalBrakeLevel(LocalBrakePosNo);
|
|
if (m_physics->EIMCtrlEmergency) {
|
|
m_physics->DecLocalBrakeLevel(1);
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
m_physics->brakes.handlePosition = static_cast<int>(std::floor(m_physics->brakes.driverValve->GetPos(bh_NP)));
|
|
}
|
|
m_physics->BrakeLevelSet(m_physics->brakes.handlePosition);
|
|
}
|
|
|
|
void vehicle::step(track const &Track, double Dt) {
|
|
begin_step();
|
|
TLocation const location{0.0, 0.0, 0.0};
|
|
for (int iteration = 1; iteration < Track.iterations; ++iteration) {
|
|
forces(Dt);
|
|
fast_move(Track, Dt, location);
|
|
}
|
|
forces(Dt);
|
|
move(Track, Dt, location, m_physics->brakePipe.Pressure());
|
|
}
|
|
|
|
namespace {
|
|
|
|
TTrackShape shape_of(track const &Track) {
|
|
TTrackShape shape;
|
|
shape.Len = 1000.0;
|
|
shape.dHtrack = Track.gradient;
|
|
shape.R = Track.radius;
|
|
shape.dHrail = Track.cant;
|
|
return shape;
|
|
}
|
|
|
|
TTrackParam parameters_of(track const &Track, physics::Vehicle const &Physics) {
|
|
TTrackParam param;
|
|
param.Width = Track.width;
|
|
param.friction = Track.friction;
|
|
param.Name = &Track.name;
|
|
// the vehicle's own kind of way: a road vehicle on rails derails as soon as it moves
|
|
if (Track.category != 0) {
|
|
param.CategoryFlag = Track.category;
|
|
}
|
|
else {
|
|
param.CategoryFlag = Physics.CategoryFlag != 0 ? Physics.CategoryFlag : 1;
|
|
}
|
|
return param;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
double vehicle::fast_move(track const &Track, double Dt, TLocation const &Location) {
|
|
// TDynamicObject::FastUpdate(), the iterations of a frame before the last one
|
|
auto param{parameters_of(Track, *m_physics)};
|
|
param.DamageFlag = m_trackdamage;
|
|
return m_physics->FastComputeMovement(Dt, shape_of(Track), param, Location, TRotation{0.0, 0.0, 0.0});
|
|
}
|
|
|
|
void vehicle::begin_step() {
|
|
// the conditions the test keeps
|
|
if (m_pipepressure >= 0.0) {
|
|
m_physics->brakePipe.reservoir->CreatePress(m_pipepressure);
|
|
}
|
|
if (m_reservoirpressure >= 0.0 && m_physics->air.config.mainReservoirVolume > 0.0) {
|
|
m_physics->air.Fill(m_reservoirpressure * m_physics->air.config.mainReservoirVolume);
|
|
}
|
|
// vehicle_table::update(), before the forces
|
|
m_physics->ComputeConstans();
|
|
}
|
|
|
|
void vehicle::forces(double Dt) {
|
|
// TDynamicObject::UpdateForce()
|
|
m_physics->ComputeTotalForce(Dt);
|
|
}
|
|
|
|
double vehicle::move(track const &Track, double Dt, TLocation const &Location, double Trainpipe) {
|
|
auto const shape{shape_of(Track)};
|
|
auto param{parameters_of(Track, *m_physics)};
|
|
param.DamageFlag = m_trackdamage;
|
|
TRotation const rotation{0.0, 0.0, 0.0};
|
|
|
|
// what TDynamicObject::Update() does before the movement. a raised pantograph touches the wire at once,
|
|
// the game animates its rise
|
|
auto &physics{*m_physics};
|
|
physics.pantographCircuit.SetFrontVoltage(physics.pantographs[VehicleEnd::front].is_active ? Track.voltage : 0.0);
|
|
physics.pantographCircuit.SetRearVoltage(physics.pantographs[VehicleEnd::rear].is_active ? Track.voltage : 0.0);
|
|
TTractionParam traction;
|
|
traction.TractionVoltage =
|
|
physics.EnginePowerSource.SourceType == TPowerSource::CurrentCollector ? std::max(std::abs(physics.pantographCircuit.RearVoltage()), std::abs(physics.pantographCircuit.FrontVoltage())) : 0.95 * physics.EnginePowerSource.MaxVoltage;
|
|
traction.TractionFreq = 0;
|
|
traction.TractionMaxCurrent = 7500;
|
|
traction.TractionResistivity = 0.3;
|
|
physics.pantographCircuit.SetSupplyVoltage(traction.TractionVoltage);
|
|
if (m_cab != 0) {
|
|
using enum TEngineType;
|
|
// the driver's own vehicle, which gets the average pressure of the train's brake pipe
|
|
physics.brakes.equivalentPipePressure = Trainpipe;
|
|
if (auto const diesel{physics.EngineType == DieselEngine || physics.EngineType == DieselElectric}; diesel && physics.EIMCtrlType > 0) {
|
|
physics.CheckEIMIC(Dt);
|
|
if (physics.SpeedCtrl) {
|
|
physics.CheckSpeedCtrl(Dt);
|
|
}
|
|
physics.eimic_real = std::min(physics.eimic, physics.eimicSpeedCtrl);
|
|
}
|
|
if (physics.EngineType == ElectricInductionMotor) {
|
|
// the share of the electric and pneumatic brake over the train (TDynamicObject's MED) isn't here
|
|
physics.CheckEIMIC(Dt);
|
|
physics.CheckSpeedCtrl(Dt);
|
|
physics.eimic_real = std::min(physics.eimic, physics.eimicSpeedCtrl);
|
|
}
|
|
}
|
|
|
|
// the whole frame's time, of all its iterations
|
|
auto const frame{Dt * Track.iterations};
|
|
auto const distance{m_physics->ComputeMovement(Dt, frame, shape, param, traction, Location, rotation)};
|
|
m_time += frame;
|
|
if (m_trace != nullptr) {
|
|
m_trace->sample(m_time, *m_physics);
|
|
}
|
|
return distance;
|
|
}
|
|
|
|
void vehicle::run(double Seconds, track const &Track, double Dt) {
|
|
// whole frames: summing up Dt would make their count depend on the rounding
|
|
auto const steps{std::lround(Seconds / (Dt * Track.iterations))};
|
|
for (long i = 0; i < steps; ++i) {
|
|
step(Track, Dt);
|
|
}
|
|
}
|
|
|
|
// the physics reaches the vehicle next to it through TDynamicObject::physics only (neighbour_data). the
|
|
// tests can't make a TDynamicObject, whose constructor and members come with the rest of the engine, so the
|
|
// neighbour is a zeroed block of its size with just that pointer set: enough for the physics, which reads nothing
|
|
// else of it, and never used as a whole object
|
|
static_assert(alignof(TDynamicObject) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__);
|
|
|
|
namespace {
|
|
|
|
std::vector<std::byte> make_standin(physics::Vehicle &Physics) {
|
|
std::vector<std::byte> standin(sizeof(TDynamicObject)); // zeroed
|
|
reinterpret_cast<TDynamicObject *>(standin.data())->physics = &Physics; // NOSONAR see above
|
|
return standin;
|
|
}
|
|
|
|
TDynamicObject *as_neighbour(std::byte *Standin) {
|
|
return reinterpret_cast<TDynamicObject *>(Standin); // NOSONAR see above
|
|
}
|
|
|
|
} // namespace
|
|
|
|
pair::pair(vehicle &First, VehicleEnd FirstEnd, vehicle &Second, VehicleEnd SecondEnd, double Gap) {
|
|
auto const &first{First.physics()};
|
|
auto const &second{Second.physics()};
|
|
// the first vehicle in the middle, its FirstEnd towards the second one
|
|
m_vehicles[0] = {&First, FirstEnd, 0.0, FirstEnd == VehicleEnd::front ? 1.0 : -1.0, make_standin(First.physics())};
|
|
m_vehicles[1] = {&Second, SecondEnd, 0.5 * first.Dim.L + Gap + 0.5 * second.Dim.L, SecondEnd == VehicleEnd::front ? -1.0 : 1.0, make_standin(Second.physics())};
|
|
}
|
|
|
|
pair::~pair() {
|
|
for (auto const &placed : m_vehicles) {
|
|
// the vehicles outlive the pair, they mustn't point at its stand-ins
|
|
for (auto &neighbour : placed.owner->physics().Neighbours) {
|
|
neighbour = neighbour_data();
|
|
}
|
|
}
|
|
}
|
|
|
|
void pair::find_neighbours() {
|
|
// as TDynamicObject::update_neighbours()
|
|
for (auto const &placed : m_vehicles) {
|
|
placed.owner->physics().Loc = TLocation{placed.position, 0.0, 0.0};
|
|
}
|
|
for (int i = 0; i < 2; ++i) {
|
|
auto const &self{m_vehicles[i]};
|
|
auto &other{m_vehicles[1 - i]}; // its stand-in is what this one points at
|
|
auto &physics{self.owner->physics()};
|
|
auto const &otherPhysics{other.owner->physics()};
|
|
auto &neighbour{physics.Neighbours[self.end]};
|
|
neighbour.vehicle = as_neighbour(other.standin.data());
|
|
neighbour.vehicle_end = NumberOf(other.end);
|
|
neighbour.distance = static_cast<float>(physics::Vehicle::CouplerDist(&physics, &otherPhysics) - physics.Couplers[self.end].adapter_length -
|
|
otherPhysics.Couplers[other.end].adapter_length);
|
|
physics.Neighbours[Opposite(self.end)] = neighbour_data();
|
|
}
|
|
}
|
|
|
|
void pair::step(track const &Track, double Dt) {
|
|
find_neighbours();
|
|
for (auto const &placed : m_vehicles) {
|
|
placed.owner->begin_step();
|
|
placed.owner->forces(Dt);
|
|
}
|
|
for (auto &placed : m_vehicles) {
|
|
auto const &physics{placed.owner->physics()};
|
|
placed.position += placed.heading * placed.owner->move(Track, Dt, TLocation{placed.position, 0.0, 0.0}, physics.brakePipe.Pressure());
|
|
}
|
|
// vehicle_table::update_brake_pipes()
|
|
physics::Vehicle::UpdatePipeCouplers({&m_vehicles[0].owner->physics(), &m_vehicles[1].owner->physics()}, Dt);
|
|
}
|
|
|
|
void pair::run(double Seconds, track const &Track, double Dt) {
|
|
// whole steps, as in vehicle::run()
|
|
auto const steps{std::lround(Seconds / Dt)};
|
|
for (long i = 0; i < steps; ++i) {
|
|
step(Track, Dt);
|
|
}
|
|
}
|
|
|
|
double pair::gap() const {
|
|
auto const &first{m_vehicles[0].owner->physics()};
|
|
auto const &second{m_vehicles[1].owner->physics()};
|
|
return std::abs(m_vehicles[1].position - m_vehicles[0].position) - 0.5 * (first.Dim.L + second.Dim.L);
|
|
}
|
|
|
|
CouplingSet pair::coupling() const {
|
|
return m_vehicles[0].owner->physics().Couplers[m_vehicles[0].end].CouplingFlag;
|
|
}
|
|
|
|
void reseed_random() {
|
|
Global.random_engine.seed(1);
|
|
Global.local_random_engine.seed(1);
|
|
}
|
|
|
|
std::vector<std::string> const &real_vehicles() {
|
|
static std::vector<std::string> const paths{[] {
|
|
std::vector<std::string> result;
|
|
auto const root{std::filesystem::path{EU07_UNIT_TESTS_DATA} / "fiz"};
|
|
for (auto const &entry : std::filesystem::recursive_directory_iterator(root)) {
|
|
if (entry.is_regular_file() && entry.path().extension() == ".json") {
|
|
result.push_back(std::filesystem::relative(entry.path(), root).replace_extension().generic_string());
|
|
}
|
|
}
|
|
std::ranges::sort(result);
|
|
return result;
|
|
}()};
|
|
return paths;
|
|
}
|
|
|
|
} // namespace test
|