mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-04 23:45:17 +01:00
444 lines
19 KiB
C++
444 lines
19 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/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "vehicle/DynObj.h"
|
|
|
|
#include "physics/vehicle.h"
|
|
#include "simulation/simulation.h"
|
|
#include "simulation/simulationsnapshotmover.h"
|
|
#include "simulation/simulationsnapshotworld.h"
|
|
#include "vehicle/Driver.h"
|
|
#include "world/Track.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <format>
|
|
|
|
// the part of a simulation snapshot kept by a vehicle of the scenery: where its axles are on the tracks, which way it
|
|
// faces, what it's coupled to, where it's bound for, and through its physics (simulationsnapshotmover.cpp) how fast
|
|
// it goes, what it carries and what its machinery is doing
|
|
|
|
namespace {
|
|
|
|
namespace snapshot = simulation::snapshot;
|
|
|
|
// keys of the two axles and the two couplers: "track0", "coupler1flags"
|
|
std::string indexed(std::string_view const Part, int const Index, std::string_view const Key = {})
|
|
{
|
|
return std::format("{}{}{}", Part, Index, Key);
|
|
}
|
|
|
|
void save_axle(snapshot::record &Entry, int const Index, TTrackFollower const &Axle)
|
|
{
|
|
Entry.set(indexed("track", Index), snapshot::track_reference(Axle.GetTrack()));
|
|
Entry.set(indexed("at", Index), Axle.GetTranslation());
|
|
Entry.set(indexed("dir", Index), Axle.GetDirection());
|
|
if (auto const path{Axle.Path()}; path != 0)
|
|
Entry.set(indexed("path", Index), path);
|
|
// which events of the track the axle has yet to set off: of the trains with a driver, and of any vehicle
|
|
Entry.set(indexed("events", Index), Axle.EventFlags() | (Axle.EventallFlags() << 2));
|
|
// the ways of the switches the axle ran over last, the oldest first: the axle behind it coming onto one of them
|
|
// after a throw is checked against them for running forked (world/switchfork.h)
|
|
auto const paths{Axle.Paths().entries()};
|
|
for (std::size_t index{0}; index < paths.size(); ++index)
|
|
{
|
|
Entry.set(indexed("axle", Index, std::format("switch{}", index)), snapshot::track_reference(static_cast<TTrack const *>(paths[index].first)));
|
|
Entry.set(indexed("axle", Index, std::format("way{}", index)), paths[index].second);
|
|
}
|
|
}
|
|
|
|
// the ways of the switches the axle ran over last as the entry has them; an entry without them leaves none
|
|
void load_paths(snapshot::record const &Entry, int const Index, TTrackFollower &Axle)
|
|
{
|
|
switch_fork::path_memory paths;
|
|
for (std::size_t index{0}; index < switch_fork::path_memory::size; ++index)
|
|
{
|
|
auto const key{indexed("axle", Index, std::format("switch{}", index))};
|
|
if (false == Entry.has(key))
|
|
break;
|
|
auto const *track{snapshot::find_track(Entry.text(key))};
|
|
if (track == nullptr || track->eType != tt_Switch)
|
|
{
|
|
Entry.refuse(key, "it isn't a switch of the scenery");
|
|
continue;
|
|
}
|
|
if (auto way{0}; Entry.get(indexed("axle", Index, std::format("way{}", index)), way, 0, 1))
|
|
paths.record(track, way);
|
|
}
|
|
Axle.SetPaths(paths);
|
|
}
|
|
|
|
// where an entry has an axle
|
|
struct axle_place {
|
|
TTrack *track{nullptr};
|
|
int path{0};
|
|
double distance{0.0};
|
|
double direction{1.0};
|
|
int events{15};
|
|
|
|
bool put(TTrackFollower &Axle, double const Inset) const
|
|
{
|
|
// Inset keeps the axle off the very ends of the track
|
|
auto const length{track->Length()};
|
|
auto const place{Inset > 0.0 && length > 2.0 * Inset ? std::clamp(distance, Inset, length - Inset) : distance};
|
|
return Axle.Place(track, path, place, direction, events & 3, (events >> 2) & 3);
|
|
}
|
|
};
|
|
|
|
// returns: the place, without its track if the entry doesn't give one which can be used
|
|
axle_place read_axle(snapshot::record const &Entry, int const Index)
|
|
{
|
|
axle_place place;
|
|
place.track = snapshot::find_track(Entry.text(indexed("track", Index)));
|
|
// the path is a segment of a switch (0, 1) or of a crossing (1..6, the sign the way through it), the events two
|
|
// pairs of flags. the axle is put at a distance along its track, which has to be a number
|
|
if (auto const usable{(Entry.get(indexed("path", Index), place.path, -6, 6) || false == Entry.has(indexed("path", Index))) &&
|
|
(Entry.get(indexed("at", Index), place.distance, -1e7, 1e7) || false == Entry.has(indexed("at", Index))) &&
|
|
(Entry.get(indexed("dir", Index), place.direction, -1e7, 1e7) || false == Entry.has(indexed("dir", Index))) &&
|
|
(Entry.get(indexed("events", Index), place.events, 0, 15) || false == Entry.has(indexed("events", Index)))};
|
|
false == usable)
|
|
place.track = nullptr;
|
|
return place;
|
|
}
|
|
|
|
// where the axle is now
|
|
axle_place axle_at(TTrackFollower const &Axle)
|
|
{
|
|
return {Axle.GetTrack(), Axle.Path(), Axle.GetTranslation(), Axle.GetDirection(), Axle.EventFlags() | (Axle.EventallFlags() << 2)};
|
|
}
|
|
|
|
// takes the vehicle off the list of the track it's on, if it's on it
|
|
void leave_track(TDynamicObject &Vehicle)
|
|
{
|
|
if (Vehicle.MyTrack != nullptr && Vehicle.MyTrack->CheckDynamicObject(&Vehicle))
|
|
Vehicle.MyTrack->RemoveDynamicObject(&Vehicle);
|
|
Vehicle.MyTrack = nullptr;
|
|
}
|
|
|
|
// puts the axles of the vehicle at the places, and its body after them. returns: false if an axle can't be put there
|
|
bool put_axles(TDynamicObject &Vehicle, std::array<axle_place, 2> const &Places)
|
|
{
|
|
// the axles a hair off the ends of their tracks first, for the body of the vehicle to be worked out from them:
|
|
// Move() does that, and takes an axle standing at the very end of a track over to the next one
|
|
if (constexpr double inset{1e-6}; false == Places[0].put(Vehicle.Axle0, inset) || false == Places[1].put(Vehicle.Axle1, inset))
|
|
return false;
|
|
// too short a way for the axles to move at all
|
|
Vehicle.Move(1e-300);
|
|
// then exactly where they were
|
|
Places[0].put(Vehicle.Axle0, 0.0);
|
|
Places[1].put(Vehicle.Axle1, 0.0);
|
|
return true;
|
|
}
|
|
|
|
// the vehicle is on the list of the track its leading axle is on, and its physics where its body is
|
|
void settle(TDynamicObject &Vehicle)
|
|
{
|
|
if (auto *track{Vehicle.iAxleFirst != 0 ? Vehicle.Axle1.GetTrack() : Vehicle.Axle0.GetTrack()}; track != nullptr && track != Vehicle.MyTrack)
|
|
{
|
|
leave_track(Vehicle);
|
|
track->AddDynamicObject(&Vehicle);
|
|
}
|
|
Vehicle.physics->SetLoc({-Vehicle.vPosition.x, Vehicle.vPosition.z, Vehicle.vPosition.y});
|
|
}
|
|
|
|
// the vehicle whose physics a coupler is joined to: the neighbour at that end as a rule, otherwise looked up
|
|
TDynamicObject const *coupled_vehicle(TCoupling const &Coupler, TDynamicObject const *Neighbour)
|
|
{
|
|
if (Coupler.Connected == nullptr || Coupler.CouplingFlag.Empty())
|
|
return nullptr;
|
|
if (Neighbour != nullptr && Neighbour->physics == Coupler.Connected)
|
|
return Neighbour;
|
|
auto const &vehicles{simulation::Vehicles.sequence()};
|
|
auto const lookup{std::ranges::find_if(vehicles, [&](TDynamicObject const *Vehicle) { return Vehicle != nullptr && Vehicle->physics == Coupler.Connected; })};
|
|
return lookup != vehicles.end() ? *lookup : nullptr;
|
|
}
|
|
|
|
void save_coupler(snapshot::record &Entry, int const Index, TCoupling const &Coupler, TDynamicObject const *Neighbour)
|
|
{
|
|
Neighbour = coupled_vehicle(Coupler, Neighbour);
|
|
auto const coupled{Neighbour != nullptr};
|
|
// an end with nothing coupled says so too: the snapshot knows the vehicle stands free there
|
|
Entry.set(indexed("coupler", Index), coupled ? snapshot::vehicle_reference(Neighbour) : std::string{});
|
|
Entry.set(indexed("coupler", Index, "end"), coupled ? Coupler.ConnectedNr : 0);
|
|
Entry.set(indexed("coupler", Index, "flags"), coupled ? Coupler.CouplingFlag.Bits() : 0);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// what the vehicle keeps of its machinery beside its physics: the pantographs as they stand, the passengers getting on and off
|
|
void TDynamicObject::snapshot_save_machinery(simulation::snapshot::record &Entry) const
|
|
{
|
|
Entry.set("overheadmask", iOverheadMask);
|
|
Entry.set("novolttime", NoVoltTime);
|
|
for (auto index{0}; index < iAnimType[ANIM_PANTS] && pants != nullptr; ++index)
|
|
{
|
|
auto const *pantograph{pants[index].fParamPants};
|
|
if (pantograph == nullptr)
|
|
continue;
|
|
Entry.set(indexed("pant", index, "anglel"), pantograph->fAngleL);
|
|
Entry.set(indexed("pant", index, "angleu"), pantograph->fAngleU);
|
|
Entry.set(indexed("pant", index, "height"), pantograph->PantWys);
|
|
Entry.set(indexed("pant", index, "wire"), pantograph->PantTraction);
|
|
Entry.set(indexed("pant", index, "novolttime"), pantograph->NoVoltTime);
|
|
}
|
|
if (auto const &exchange{m_exchange}; exchange.unload_count != 0.f || exchange.load_count != 0.f)
|
|
{
|
|
Entry.set("exchangeunload", exchange.unload_count).set("exchangeload", exchange.load_count);
|
|
Entry.set("exchangeplatforms", exchange.platforms).set("exchangetime", exchange.time);
|
|
}
|
|
// the rails under the wheel sets: the length of a rail, and for each wheel set how far along its rail it is and
|
|
// whether that's by the joints of the track. a joint of a worn track jolts the vehicle
|
|
Entry.set("raillength", dRailLength);
|
|
if (false == m_axlesounds.empty())
|
|
{
|
|
std::vector<double> joints;
|
|
for (auto const &axle : m_axlesounds)
|
|
{
|
|
joints.emplace_back(axle.distance);
|
|
joints.emplace_back(axle.atjoints ? 1.0 : 0.0);
|
|
}
|
|
Entry.set("railjoints", snapshot::numbers_text(joints));
|
|
}
|
|
// the lighting of the compartments: which of them are lit is drawn at random when it comes on
|
|
Entry.set("sectionlights", SectionLightsActive);
|
|
if (false == Sections.empty())
|
|
{
|
|
std::vector<double> levels;
|
|
for (auto const §ion : Sections)
|
|
levels.emplace_back(section.light_level);
|
|
Entry.set("sectionlightlevels", snapshot::numbers_text(levels));
|
|
}
|
|
Entry.set("dimheadlights", DimHeadlights).set("highbeamlights", HighBeamLights);
|
|
Entry.set("headlightsaoff", HeadlightsAoff).set("headlightsboff", HeadlightsBoff);
|
|
// the force the electrodynamic brake of a unit was asked for in the step before, which the next one is held to
|
|
if (brakeBlending.LastElectrodynamicDemand() != 0.0)
|
|
Entry.set("medoldfed", brakeBlending.LastElectrodynamicDemand());
|
|
// the derailment the scenery and the test mode were told of (new_derailment()): told once, it isn't told again
|
|
if (m_derailment.has_value())
|
|
{
|
|
Entry.set("derailtold", static_cast<int>(m_derailment->why)).set("derailtoldtrack", m_derailment->track);
|
|
Entry.set("derailtoldvelocity", m_derailment->velocity);
|
|
}
|
|
// off the track: how long, when it's told of in the log next, the move along the track its axles haven't made yet and
|
|
// where its body was when they last did
|
|
if (physics->OffTrack.active())
|
|
{
|
|
Entry.set("offtracktime", m_offtracktotal).set("offtracklog", m_offtracklogdue).set("offtrackaxles", m_offtrackaxles);
|
|
Entry.set("offtrackfrom", snapshot::numbers_text(std::array{m_offtrackfrom.x, m_offtrackfrom.y, m_offtrackfrom.z}));
|
|
}
|
|
// a switch thrown under the train (world/switchfork.h): the switch the bogies of the vehicle, or an end of it and
|
|
// the vehicle coupled there, run forked on, and whether that came past what the vehicle takes and was told of
|
|
auto const save_fork = [&Entry](std::string const &Key, fork_watch const &Watch) {
|
|
if (Watch.Switch != nullptr)
|
|
Entry.set(Key, snapshot::track_reference(Watch.Switch)).set(Key + "told", Watch.told);
|
|
};
|
|
save_fork("forkbogies", m_forkbogies);
|
|
for (auto const end : VehicleEnds)
|
|
save_fork(indexed("fork", NumberOf(end)), m_forkends[end]);
|
|
}
|
|
|
|
void TDynamicObject::snapshot_load_machinery(simulation::snapshot::record const &Entry)
|
|
{
|
|
Entry.get("overheadmask", iOverheadMask);
|
|
Entry.get("novolttime", NoVoltTime);
|
|
for (auto index{0}; index < iAnimType[ANIM_PANTS] && pants != nullptr; ++index)
|
|
{
|
|
auto *pantograph{pants[index].fParamPants};
|
|
if (pantograph == nullptr)
|
|
continue;
|
|
Entry.get(indexed("pant", index, "anglel"), pantograph->fAngleL);
|
|
Entry.get(indexed("pant", index, "angleu"), pantograph->fAngleU);
|
|
Entry.get(indexed("pant", index, "height"), pantograph->PantWys);
|
|
Entry.get(indexed("pant", index, "wire"), pantograph->PantTraction);
|
|
Entry.get(indexed("pant", index, "novolttime"), pantograph->NoVoltTime);
|
|
}
|
|
auto &exchange{m_exchange};
|
|
exchange = {};
|
|
// the counts of the passengers getting off and on are counted down step by step
|
|
constexpr auto most_passengers{1e6f};
|
|
Entry.get("exchangeunload", exchange.unload_count, -most_passengers, most_passengers);
|
|
Entry.get("exchangeload", exchange.load_count, -most_passengers, most_passengers);
|
|
Entry.get("exchangeplatforms", exchange.platforms);
|
|
Entry.get("exchangetime", exchange.time);
|
|
Entry.get("raillength", dRailLength, -1.0, 1e6);
|
|
if (auto const joints{snapshot::text_numbers(Entry.text("railjoints"))}; joints.size() == 2 * m_axlesounds.size())
|
|
{
|
|
for (std::size_t index{0}; index < m_axlesounds.size(); ++index)
|
|
{
|
|
m_axlesounds[index].distance = joints[2 * index];
|
|
m_axlesounds[index].atjoints = joints[2 * index + 1] != 0.0;
|
|
}
|
|
}
|
|
Entry.get("sectionlights", SectionLightsActive);
|
|
if (auto const levels{snapshot::text_numbers(Entry.text("sectionlightlevels"))}; levels.size() == Sections.size())
|
|
{
|
|
for (std::size_t index{0}; index < levels.size(); ++index)
|
|
Sections[index].light_level = static_cast<float>(levels[index]);
|
|
}
|
|
Entry.get("dimheadlights", DimHeadlights);
|
|
Entry.get("highbeamlights", HighBeamLights);
|
|
Entry.get("headlightsaoff", HeadlightsAoff);
|
|
Entry.get("headlightsboff", HeadlightsBoff);
|
|
brakeBlending.RestoreLastElectrodynamicDemand(Entry.value("medoldfed", 0.0, -1e9, 1e9));
|
|
m_derailment.reset();
|
|
if (auto why{0}; Entry.get("derailtold", why, 1, static_cast<int>(derailment::reason::flank_collision)))
|
|
{
|
|
std::string track;
|
|
Entry.get("derailtoldtrack", track);
|
|
m_derailment = derailment::notice{asName, static_cast<derailment::reason>(why), track, Entry.value("derailtoldvelocity", 0.0)};
|
|
}
|
|
else if (false == Entry.has("derailtold") && physics->DamageFlag.HasAll(VehicleDamage::derailed))
|
|
{
|
|
// a snapshot written before this was kept: a vehicle derailed then isn't told of again
|
|
new_derailment();
|
|
}
|
|
if (physics->OffTrack.active())
|
|
{
|
|
Entry.get("offtracktime", m_offtracktotal);
|
|
Entry.get("offtracklog", m_offtracklogdue);
|
|
Entry.get("offtrackaxles", m_offtrackaxles);
|
|
if (auto const from{snapshot::text_numbers(Entry.text("offtrackfrom"))}; from.size() == 3)
|
|
m_offtrackfrom = {from[0], from[1], from[2]};
|
|
else
|
|
m_offtrackfrom = physics->OffTrack.current().position;
|
|
// the terrain it slides over, gathered anew around where it is, as when it left the rails
|
|
gather_off_track_terrain();
|
|
m_offtracksite = derail_motion::site{m_offtrackterrain.get(), track_bed()};
|
|
if (physics->OffTrack.at_rest())
|
|
crop_off_track_terrain();
|
|
}
|
|
auto const load_fork = [&Entry](std::string const &Key, fork_watch &Watch) {
|
|
Watch = {};
|
|
if (false == Entry.has(Key))
|
|
return;
|
|
auto const *track{snapshot::find_track(Entry.text(Key))};
|
|
if (track == nullptr || track->eType != tt_Switch)
|
|
{
|
|
Entry.refuse(Key, "it isn't a switch of the scenery");
|
|
return;
|
|
}
|
|
Watch.Switch = track;
|
|
Entry.get(Key + "told", Watch.told);
|
|
};
|
|
load_fork("forkbogies", m_forkbogies);
|
|
for (auto const end : VehicleEnds)
|
|
load_fork(indexed("fork", NumberOf(end)), m_forkends[end]);
|
|
}
|
|
|
|
void TDynamicObject::snapshot_save(simulation::snapshot::record &Entry) const
|
|
{
|
|
if (false == bEnabled)
|
|
{
|
|
// gone from the simulation, through a portal say
|
|
Entry.set("removed", true);
|
|
return;
|
|
}
|
|
// place
|
|
save_axle(Entry, 0, Axle0);
|
|
save_axle(Entry, 1, Axle1);
|
|
Entry.set("first", iAxleFirst);
|
|
Entry.set("facing", iDirection);
|
|
// where that is in the world: the reader of the file sees it, and a loaded snapshot is checked against it
|
|
Entry.set("x", vPosition.x).set("y", vPosition.y).set("z", vPosition.z);
|
|
// physics.derailment new, off the track: its body on the ground, which the place above is the place of
|
|
if (physics->OffTrack.active())
|
|
Entry.set("offtrack", snapshot::numbers_text(physics->OffTrack.state()));
|
|
// consist
|
|
for (auto const side : VehicleEnds)
|
|
save_coupler(Entry, NumberOf(side), physics->Couplers[side], physics->Neighbours[side].vehicle);
|
|
if (ctOwner != nullptr && ctOwner->Vehicle() != nullptr)
|
|
Entry.set("owner", snapshot::vehicle_reference(ctOwner->Vehicle()));
|
|
Entry.set("destination", asDestination);
|
|
// the physics
|
|
snapshot::capture_mover(Entry, *physics);
|
|
snapshot_save_machinery(Entry);
|
|
}
|
|
|
|
bool TDynamicObject::snapshot_place(simulation::snapshot::record const &Entry)
|
|
{
|
|
if (Entry.value("removed", false))
|
|
{
|
|
if (false == bEnabled)
|
|
return true;
|
|
// the vehicle left the simulation before the snapshot was taken: off the track, and off the list at the next update
|
|
leave_track(*this);
|
|
// the scenery counted its axles in the isolated sections of the tracks it put it on, the run the snapshot comes
|
|
// from counted them out as the train drove off. the events are held while a snapshot is put back, and the saved
|
|
// cells and queue carry what followed from it
|
|
for (auto const *axle : {&Axle0, &Axle1})
|
|
{
|
|
if (auto *track{axle->GetTrack()}; track != nullptr)
|
|
track->AxleCounter(-1, this);
|
|
}
|
|
// nobody's in charge of it: its driver, if it had one, doesn't go on (apply_drivers())
|
|
ctOwner = nullptr;
|
|
bEnabled = false;
|
|
bDynamicRemove = true;
|
|
return true;
|
|
}
|
|
auto const places{std::array{read_axle(Entry, 0), read_axle(Entry, 1)}};
|
|
if (places[0].track == nullptr || places[1].track == nullptr)
|
|
return false;
|
|
|
|
// where the scenery put the vehicle, for it to stay there if it can't be put where the snapshot has it
|
|
auto const scenery{std::array{axle_at(Axle0), axle_at(Axle1)}};
|
|
auto const facing{iDirection};
|
|
auto const first{iAxleFirst};
|
|
// which of the ends of the vehicle leads in its consist, which of its axles is the first: 0 or 1
|
|
Entry.get("facing", iDirection, 0, 1);
|
|
if (put_axles(*this, places))
|
|
{
|
|
Entry.get("first", iAxleFirst, 0, 1);
|
|
// a vehicle off the track: its axles go along the track beside it, its body is where its motion on the ground has it
|
|
if (Entry.has("offtrack"))
|
|
{
|
|
if (physics->OffTrack.state(snapshot::text_numbers(Entry.text("offtrack"))) && physics->OffTrack.active())
|
|
place_off_track();
|
|
else
|
|
Entry.refuse("offtrack", "it isn't the motion of a vehicle off the track");
|
|
}
|
|
// a track named by its place in the scenery ("#12") is another one after a track was added before it: the
|
|
// place in the world the entry gives tells
|
|
if (snapshot::placed_where_saved(Entry, vPosition.x, vPosition.y, vPosition.z))
|
|
{
|
|
load_paths(Entry, 0, Axle0);
|
|
load_paths(Entry, 1, Axle1);
|
|
settle(*this);
|
|
return true;
|
|
}
|
|
Entry.refuse("x", std::format("the vehicle put on track {} is more than {} m from there", Entry.text("track0"), snapshot::misplaced_distance));
|
|
}
|
|
// back where the scenery put it, on the rails
|
|
physics->OffTrack.reset();
|
|
m_offtrackplaced = false;
|
|
iDirection = facing;
|
|
iAxleFirst = first;
|
|
if (scenery[0].track != nullptr && scenery[1].track != nullptr)
|
|
put_axles(*this, scenery);
|
|
settle(*this);
|
|
return false;
|
|
}
|
|
|
|
void TDynamicObject::snapshot_load(simulation::snapshot::record const &Entry)
|
|
{
|
|
if (false == bEnabled)
|
|
return;
|
|
Entry.get("destination", asDestination);
|
|
|
|
snapshot::apply_mover(*physics, Entry);
|
|
// a load of another kind than the scenery gave comes with its model. it's loaded here and not by the first frame:
|
|
// laying the load out in the vehicle draws at random, which doesn't count while a snapshot is put back
|
|
if (physics->LoadTypeChange())
|
|
LoadUpdate();
|
|
update_load_visibility();
|
|
|
|
snapshot_load_machinery(Entry);
|
|
}
|