mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
* feat(physics): the motion of a derailed vehicle on the ground With physics.derailment new a vehicle off the rails gets a motion of its own on the ground (vehicle/derailmotion.h, in eu07_core): it runs on along its heading, turned out of the curve by the sideways speed it left the rail with, slides across it down slopes steeper than the friction holds, follows the height and the slope of the ground under its supports, and tips over about the wheels of one side (while still guided by the curve, or on a slope, or sliding sideways) until it lies on its side. The vehicle holds the motion and what derailed it (the cause, the lateral acceleration out of the curve, the side a coupling pulled it to). While the motion runs, its friction (0.1 tipping on the outer wheels, 0.3 upright, up to 0.5 on its side) and the grade under it go into the longitudinal force. Off the end of the track the vehicle now slides on with the friction of the ballast instead of stopping within g, and a coupling can pull a vehicle off the rails (pull_off). Nothing of it with physics.derailment monitor, old or off. * feat(physics): a derailed vehicle leaves the track in the scenery TDynamicObject begins the motion of vehicle/derailmotion.h once its vehicle derails with physics.derailment new, and from then on places the model by it: off the axis of the track, pitched and rolled by the ground, on its side once it falls over, the bogies straight under the body. The ground is the scenery's terrain around the place (its opaque shapes and the heightmap terrain), with the ballast bed of the track over it; level ground at the top of the ballast where the scenery has none. The axles keep going along the track by the projection of the move, so the vehicle stays on the track nearest to it for its neighbours, the events and the drawing. A coupled vehicle still on the rails is pulled off when the sideways part of the coupler force puts its leading wheel over the climbing limit over 2 m. An uncoupled vehicle on the track and one off it run into each other only when in line. The derail sound loops while the vehicle scrapes along, and the log tells where it is every second and where it came to rest. Move() is split into move_axles() and place_by_axles(), the code of both unchanged. * feat(blackbox): a vehicle leaving the track and coming to rest off it A new record of the black box, 0x38 derailment, written when a vehicle leaves the track (physics.derailment new) and when it comes to rest off it: what derailed it, its position, heading, speed, offset from the axis of the track, roll, the distance it went and whether it lies on its side (blackbox_doc.md). The scenario runner reads it with the leaves_track check, and seven cases run it on td.scn: an EU07 with coaches into the buckled curve at 46 km/h (the AI) and at 133 km/h, over the overturning speed of the R 600 m curve at 190 km/h, off the end of a track, and overturning at 150 km/h on a curve of R 300 m on level ground, on an embankment and in a cutting. The runner also drops the cached geometry of a case's scenery (.sbt) once the case's fragment is newer than it, as it did for the base scenery: the curve cases carry their own terrain. * feat(physics): the yaw of a derailed body, and what differs between vehicles The body off the track (physics.derailment new) now turns about its vertical axis by itself, apart from the way it travels. Its heading is the way the vehicle goes, along which the Mover's speed runs; its axis is the way its front faces, with a yaw rate and the inertia of a box of its length and width. The friction of the ground holds it: under the two ends of its base while upright, along all of its length on its side, 0.3 to 0.5 along the body and 0.5 across it, each point against the way it slides there. Turning resists the yaw; a body going askew slides harder (mu along cos^2 + mu across sin^2 of the slip, which the Mover takes), its heading is drawn to the body and the sideways scrubbing trips it over. At a standstill it doesn't turn while the friction holds. The forces of the couplings at its ends turn it: two ends meet along the bisector of the bend between the bodies, the push goes through the inner buffer of the bend, off the middle of the beam by up to half the buffer spacing, as far as the buffers meet in height (a derailed body is lower than one on the rails, a rolled one misses them) and as much as a buffer takes (1 MN). Pushed from behind past what the buffers hold, a train of bodies bends further at each joint, the next one the other way: the zigzag of a train folding up. The gap of two ends is taken where they are, not from the distance between the middles, which would press the buffers of every bent pair. Past a right angle the heading turns about with the speed, so the Mover's front stays the body's front. Each vehicle draws its own scatter from the seed of the run and its name: the departure angle +-0.01 rad, yaw +-0.02 rad and +-0.02 rad/s, the friction under each half of it 0.85..1.15, and in two vehicles of three its leading end digging in 0.2..1.5 s after it left the rails, for 0.3..0.8 s, two to four times the friction under that half at one wheel or corner. The same seed gives the same outcome. * feat(physics): a derailed train folds up, and the derailment watches its own cost TDynamicObject gives the body off the track the forces of its couplings as vectors: the gap of a coupling with a vehicle off the track (coupled or meeting it) is that of the two ends as they are, and the force of each end goes to the body at the buffer it pushes through, or towards the other beam it is pulled to. A vehicle still on the rails coupled to one off it is taken off by the part of that force across its axis on its leading wheels (Y/Q over 1.2 over 2 m), pushed by the buffers as well as pulled by the coupling, to the side the force goes. Each vehicle off the track draws its scatter from the seed of the run and its name, and the model and the coupling points follow the yaw of the body. What a derailment costs is watched: the terrain is gathered again at most once a second per vehicle and kept to the nearest 400 000 triangles, the axles of a vehicle off the track follow it along the track by 10 cm steps (a body rocking in place moved them to and fro over the ends of tracks, firing their events every frame), and the gathering, a frame of the motion and the whole update of a vehicle off the track write a warning to the log when they take over 20 ms. The scenario runner gets the consist_folds check: the headings of a train's vehicles at rest off the track differ by a given angle at a given number of joints, optionally alternating, optionally on both sides of the track. eu07_td_off_track_buckle_fast checks the folding, eu07_td_off_track_buckle the coupled coach pulled off at an angle to the locomotive, and the bounds of the other cases are narrowed to the new outcomes. * feat(physics): derailed bodies run into each other instead of through With physics.derailment new the bodies of the vehicles off the track no longer go into each other, nor into the vehicles near them: once a frame, before the vehicles update, every vehicle off the track is checked against those around it (its own train coupled or not, a vehicle still on the rails, another train), by bounding circles and then by the footprints of their bodies: the box of each, less its buffers, rolled and pitched as it lies, seen from above, with the height it reaches. Two footprints in each other are separated along the axis they overlap least across. A hit is an impulse at the point of contact, which takes away the speed the two close in with there (they part with a fifth of it) with the mass, the vertical inertia and the arm of each, and the friction of their sides sliding along each other (0.3). It goes into the velocity and the yaw rate of a body off the track, which then goes the way it is thrown, and into the speed of a vehicle on the rails along them; across the rails it pushes on the leading wheels of that vehicle, which is pushed off when the Y/Q passes 1.2 over 2 m of sliding, or three times that at once. What the bodies still overlap by is taken out by moving them apart. The buffers of two ends keep meeting through meet() and force_on(); the bodies, shorter by the buffers, touch only once the ends are pressed past them or folded corner to corner. The log has the most any two bodies overlapped by every second, and the runner checks it with no_overlap in all off-track cases: before this the coaches went up to 1.64 m into each other at 190 km/h and 0.68 m stayed in each other at rest at 133 km/h; now at most 0.10 m on the way and none at rest. * fix(sonar): the reliability and security issues of the derailment SonarCloud failed the quality gate of the pull request on the new reliability and security ratings: - cpp:S836 (dereference of an undefined pointer value) in the timing watch of TDynamicObject::Update: the analyser lost the reference held in a std::optional. The watch is now always made, with a null vehicle when it is on the track, and does nothing then; copying it is deleted. Only the timing of the update off the track and its warning use it, the physics is the same. - cpp:S2245 (pseudorandom number generator) on the std::mt19937 of draw_scatter: it is seeded on purpose so that a derailment repeats, and nothing secret is drawn; marked NOSONAR like the seeded generators of the editor self tests. - cpp:S2193 (a for loop counter of floating type) in three loops of tests/unit/test_derail_motion.cpp: they count integer steps now. * fix(physics): the scraping of a derailed vehicle stops when it is put back on the rails The derail: sound loops while a vehicle off the track slides, and it was stopped only while the vehicle was still off the track, once it came to rest. rerail() (resetconsist, SetDamage taking dtrain_out off) ends the motion off the track at once, also while the vehicle still slides, and the scraping then went on for good. The vehicle now remembers that it plays the loop and stops it as soon as it isn't sliding off the track any more, at rest or back on the rails. The one-shot derail: sound of the other derailment models is not touched. * fix(physics): a vehicle put back on the rails forgets what derailed it rerail() reset only the side a coupling pulled the vehicle off to, and kept the cause and the lateral acceleration of the derailment. A vehicle that is put back on the rails and later leaves them in a way which doesn't set those (as the fork derailment of #496 does now) would leave the track with the cause and the acceleration of the earlier derailment. rerail() resets both now; the unit tests check it after repair_damage(). * fix(physics): the neighbour scan goes on past a derailed vehicle beside the line update_neighbours() took the nearest vehicle the scan of the track found, and when one of the two was off the track and the other not in line with it, gave up on that end. A vehicle lying beside the line so hid the vehicles beyond it: in a pile-up a wagon still on the rails had no front neighbour while running into a wagon in line further on, until the bodies were already deep in each other. The scan now passes such a vehicle and goes on to the next one (ABuFindObject() with InLine, only from update_neighbours(); the obstacle scan of the AI is as it was). * fix(physics): the moves out of other bodies go into the axles of a derailed vehicle The axles of a vehicle off the track follow it along the track by the displacement of the body, which move_off_track() measured from where the body was at the start of its own step. The contacts of the bodies move it out of the others between the frames, after that, so those moves never reached the axles, and the axles drifted along the track away from the body (the track it is registered on, the events it fires). The displacement is now measured from where the body was when its move went into the axles last. * perf(physics): derailed vehicles share the terrain and let it go at rest Each vehicle off the track gathered its own copy of the terrain, up to 600 m around it and 400 000 triangles (some 30-50 MB with the cell index), and kept it until the scenery unloaded: not let go at rest nor when put back on the rails, and gathered again for every vehicle of a pile-up, with a stall of tens of ms each. - The terrain gathered for a vehicle is kept in vehicle_table as a weak reference. A vehicle leaving the track (or gathering again near the edge) takes one which covers the way it may slide - around it and around where it stops going straight on, by its length and the margin the terrain is gathered again at - instead of gathering its own. A terrain cut down to the nearest triangles of a dense scenery is not shared. - At rest a vehicle keeps only the triangles under it (half its length and 40 m around, mesh_ground::cropped()); the big terrain goes once no vehicle holds it. Moving again, it gathers the terrain anew as before. - Back on the rails (rerail()) the vehicle lets its terrain go. The ground under a vehicle is the same as before: a shared terrain holds every triangle of the scenery reaching into its area. Unit tests: mesh_ground::covers() of an area and cropped().
852 lines
39 KiB
C++
852 lines
39 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 "simulation/blackboxrecorder.h"
|
|
|
|
#include "simulation/simulation.h"
|
|
#include "simulation/simulationtime.h"
|
|
#include "input/command.h"
|
|
#include "vehicle/Driver.h"
|
|
#include "vehicle/DynObj.h"
|
|
#include "vehicle/Train.h"
|
|
#include "widgets/map_objects.h"
|
|
#include "world/Event.h"
|
|
#include "world/MemCell.h"
|
|
#include "world/Segment.h"
|
|
#include "world/Track.h"
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/Logs.h"
|
|
#include "utilities/physicslog.h"
|
|
#include "utilities/utilities.h"
|
|
#include "world/stationnames.h"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <cmath>
|
|
#include <ctime>
|
|
#include <utility>
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <limits>
|
|
|
|
namespace simulation {
|
|
|
|
blackbox_recorder Blackbox;
|
|
|
|
namespace {
|
|
|
|
// [s] between the samples of the user's train, the looks at everything else and the full looks at the vehicles
|
|
constexpr double cabinterval{0.1};
|
|
constexpr double tapeinterval{0.05}; // as the physics logs (deltalog of Driver.cpp)
|
|
constexpr double lookinterval{1.0};
|
|
constexpr double slowlook{20.0}; // [ms] a look around taking longer is told in the log, part by part
|
|
constexpr double keyframeinterval{60.0};
|
|
constexpr double camerainterval{0.5};
|
|
|
|
// the camera is written again once it moved more than this [m], or turned more than this [rad]
|
|
constexpr double cameramove{0.5};
|
|
constexpr float cameraturn{0.05f};
|
|
|
|
// a vehicle is listed again once it moved more than this [m], or changed speed more than this [km/h]
|
|
constexpr float listmove{0.1f};
|
|
constexpr float listspeed{0.5f};
|
|
|
|
std::string text_of(std::string_view const Win1250) {
|
|
return win1250_to_utf8(std::string{Win1250});
|
|
}
|
|
|
|
blackbox::driver driver_of(TDynamicObject const &Vehicle) {
|
|
if (Vehicle.Mechanik == nullptr)
|
|
return blackbox::driver::none;
|
|
return Vehicle.Mechanik->AIControllFlag ? blackbox::driver::ai : blackbox::driver::human;
|
|
}
|
|
|
|
// the switches of the user's train: those of its own vehicle, and those of the one with the motors it drives
|
|
std::uint32_t devices_of(TMoverParameters const &Occupied, TMoverParameters const &Controlling) {
|
|
namespace device = blackbox::cab_devices;
|
|
auto const on{[](bool const State, std::uint32_t const Bit) { return State ? Bit : 0u; }};
|
|
return on(Occupied.Battery || Controlling.Battery, device::battery) | on(Controlling.Mains, device::main_breaker) |
|
|
on(Controlling.Pantographs[end::front].is_active, device::pantograph_front) | on(Controlling.Pantographs[end::rear].is_active, device::pantograph_rear) |
|
|
on(Controlling.ConverterFlag, device::converter) | on(Controlling.CompressorFlag, device::compressor) | on(Controlling.StLinFlag, device::line_contactors) |
|
|
on(Controlling.FuseFlag, device::overload_relay) | on(Controlling.SandDose, device::sanding) | on(Occupied.WarningSignal != 0, device::horn) |
|
|
on(false == Occupied.Doors.instances[side::left].is_closed, device::doors_left) |
|
|
on(false == Occupied.Doors.instances[side::right].is_closed, device::doors_right) | on(Occupied.Radio, device::radio) |
|
|
on(Controlling.Heating, device::heating) | on(Occupied.SpringBrake.IsActive, device::spring_brake) | on(Controlling.ShuntMode, device::shunt_mode) |
|
|
// the doors as the cab works them: the permits, the departure signal, the steps and the lock
|
|
on(Occupied.DepartureSignal, device::departure_signal) | on(Occupied.Doors.instances[side::left].open_permit, device::door_permit_left) |
|
|
on(Occupied.Doors.instances[side::right].open_permit, device::door_permit_right) | on(Occupied.Doors.step_enabled, device::door_step) |
|
|
on(Occupied.Doors.lock_enabled, device::door_lock) | on(Occupied.Doors.is_locked, device::doors_locked) |
|
|
on(Occupied.Hamulec != nullptr && Occupied.Hamulec->Releaser(), device::brake_releaser) | on(Controlling.FuelPump.is_active, device::fuel_pump) |
|
|
on(Controlling.OilPump.is_active, device::oil_pump) |
|
|
on(Controlling.MotorBlowers[end::front].is_active || Controlling.MotorBlowers[end::rear].is_active, device::motor_blowers) |
|
|
on(Controlling.dizel_heat.engine_is_hot, device::engine_hot) | on(Controlling.SpeedCtrl && Controlling.SpeedCtrlUnit.IsActive, device::speed_control);
|
|
}
|
|
|
|
std::uint8_t engine_of(TMoverParameters const &Mover) {
|
|
switch (Mover.EngineType) {
|
|
case TEngineType::ElectricSeriesMotor:
|
|
return blackbox::engines::electric;
|
|
case TEngineType::ElectricInductionMotor:
|
|
return blackbox::engines::electric_induction;
|
|
case TEngineType::DieselEngine:
|
|
return blackbox::engines::diesel;
|
|
case TEngineType::DieselElectric:
|
|
return blackbox::engines::diesel_electric;
|
|
case TEngineType::SteamEngine:
|
|
return blackbox::engines::steam;
|
|
default:
|
|
return blackbox::engines::none;
|
|
}
|
|
}
|
|
|
|
// the lights at an end of the train: of its head or of its tail, at the end of that vehicle turned away from the rest
|
|
std::uint16_t outward_lights(TDynamicObject const *Vehicle, bool const Head) {
|
|
if (Vehicle == nullptr || Vehicle->MoverParameters == nullptr)
|
|
return 0;
|
|
auto const along{Vehicle->iDirection > 0};
|
|
return static_cast<std::uint16_t>(Vehicle->MoverParameters->iLights[Head == along ? end::front : end::rear]);
|
|
}
|
|
|
|
// the lamps the vehicle has at that end: the driver checks a pattern against those of it there are
|
|
std::uint16_t outward_lamps(TDynamicObject const *Vehicle, bool const Head) {
|
|
if (Vehicle == nullptr)
|
|
return 0;
|
|
auto const along{Vehicle->iDirection > 0};
|
|
return static_cast<std::uint16_t>(Vehicle->LightList(Head == along ? end::front : end::rear));
|
|
}
|
|
|
|
// the scenario file without the starter's $, the folders and the extension, safe for a file name
|
|
std::string scenery_stem(std::string_view const Scenery) {
|
|
auto stem{std::filesystem::path{Scenery}.stem().string()};
|
|
if (false == stem.empty() && stem.front() == '$')
|
|
stem.erase(0, 1);
|
|
for (auto &c : stem)
|
|
if (false == (std::isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '_'))
|
|
c = '_';
|
|
return stem.empty() ? "scenery" : stem;
|
|
}
|
|
|
|
std::string local_time(char const *Format) {
|
|
auto const now{std::time(nullptr)};
|
|
std::tm local{};
|
|
#ifdef _WIN32
|
|
localtime_s(&local, &now);
|
|
#else
|
|
localtime_r(&now, &local);
|
|
#endif
|
|
std::array<char, 32> text{};
|
|
std::strftime(text.data(), text.size(), Format, &local);
|
|
return text.data();
|
|
}
|
|
|
|
double second_of_day() {
|
|
auto const &time{simulation::Time.data()};
|
|
return time.wHour * 3600.0 + time.wMinute * 60.0 + simulation::Time.second();
|
|
}
|
|
|
|
// the points along a path, as many as the map of the simulator draws it with
|
|
std::vector<glm::vec3> points_of(TSegment const &Segment) {
|
|
auto const steps{std::max(1, static_cast<int>(Segment.RaSegCount() * 0.5))};
|
|
std::vector<glm::vec3> points;
|
|
points.reserve(steps + 1);
|
|
for (int i = 0; i <= steps; ++i)
|
|
points.emplace_back(Segment.FastGetPoint(static_cast<double>(i) / steps));
|
|
return points;
|
|
}
|
|
|
|
// the ways of a switch, one track record each, under the number of the switch
|
|
void write_switch(blackbox::writer &Writer, TTrack const &Switch, blackbox::track_data &Data) {
|
|
Data.kind = blackbox::track_kinds::switch_branch;
|
|
for (std::uint8_t branch = 0; branch < 2; ++branch) {
|
|
auto const &segment{Switch.SwitchExtension->Segments[branch]};
|
|
if (segment == nullptr)
|
|
continue;
|
|
Data.branch = branch;
|
|
Data.points = points_of(*segment);
|
|
Writer.track(Data);
|
|
}
|
|
}
|
|
|
|
// the heading [rad, from +z towards +x] of a train going along the segment past the place given, from its first
|
|
// end to its second or the other way
|
|
float heading_along(TSegment const &Segment, glm::dvec3 const &Location, bool const Forward) {
|
|
auto const steps{std::max(4, Segment.RaSegCount())};
|
|
auto nearest{0};
|
|
auto best{std::numeric_limits<double>::max()};
|
|
for (int i = 0; i <= steps; ++i) {
|
|
auto const distance{glm::distance(Segment.FastGetPoint(static_cast<double>(i) / steps), Location)};
|
|
if (distance < best) {
|
|
best = distance;
|
|
nearest = i;
|
|
}
|
|
}
|
|
auto const from{std::min(nearest, steps - 1)};
|
|
auto way{Segment.FastGetPoint(static_cast<double>(from + 1) / steps) - Segment.FastGetPoint(static_cast<double>(from) / steps)};
|
|
if (false == Forward)
|
|
way = -way;
|
|
return static_cast<float>(std::atan2(way.x, way.z));
|
|
}
|
|
|
|
// whether two vehicles are of one consist, coupled through whatever vehicles stand between them
|
|
bool coupled(TDynamicObject const *From, TDynamicObject const *To) {
|
|
if (From == nullptr || To == nullptr)
|
|
return false;
|
|
if (From == To)
|
|
return true;
|
|
for (int start = 0; start < 2; ++start) {
|
|
auto const *vehicle{From};
|
|
auto side{start};
|
|
// going on away from the first vehicle: through the coupler opposite the one each was reached by
|
|
for (int step = 0; step < 1000 && vehicle != nullptr; ++step) {
|
|
auto const *mover{vehicle->MoverParameters};
|
|
auto const &neighbour{mover->Neighbours[side]};
|
|
if (mover->Couplers[side].CouplingFlag == 0 || neighbour.vehicle == nullptr)
|
|
break;
|
|
if (neighbour.vehicle == To)
|
|
return true;
|
|
side = 1 - neighbour.vehicle_end;
|
|
vehicle = neighbour.vehicle;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
blackbox::vehicle_state state_of(TDynamicObject const &Vehicle, std::uint32_t const Id) {
|
|
blackbox::vehicle_state state;
|
|
state.id = Id;
|
|
state.position = glm::vec3{Vehicle.GetPosition()};
|
|
auto const front{Vehicle.VectorFront()};
|
|
state.heading = static_cast<float>(std::atan2(front.x, front.z));
|
|
state.speed_kmh = static_cast<float>(Vehicle.MoverParameters->V * 3.6);
|
|
state.driving = driver_of(Vehicle);
|
|
return state;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void blackbox_recorder::update(double const Deltatime) {
|
|
if (false == m_started) {
|
|
if (false == simulation::is_ready)
|
|
return;
|
|
m_started = true;
|
|
// "blackbox yes" or "no" of eu07.ini decides by itself, with no entry it goes with the physics logs
|
|
if (false == Global.BlackBox.value_or(WriteLogFlag || WriteTrainLogFlag) || Global.editor_session)
|
|
return;
|
|
m_taping = true;
|
|
open();
|
|
}
|
|
if (false == m_taping)
|
|
return;
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
m_time += Deltatime;
|
|
// with the physics logs on, each driver writes its own all the time. the tapes are files of their own: they go on
|
|
// after the recording ends in the editor, and with a recording which couldn't be made
|
|
if (false == WriteLogFlag && m_time >= m_tapedue) {
|
|
keep_tape_line();
|
|
m_tapedue = std::max(m_tapedue + tapeinterval, m_time);
|
|
}
|
|
if (m_writer == nullptr)
|
|
return;
|
|
if (m_time >= m_cabdue) {
|
|
write_cab();
|
|
m_cabdue = std::max(m_cabdue + cabinterval, m_time);
|
|
}
|
|
if (m_time >= m_cameradue) {
|
|
write_camera();
|
|
m_cameradue = std::max(m_cameradue + camerainterval, m_time);
|
|
}
|
|
if (m_time >= m_lookdue) {
|
|
auto const keyframe{m_time >= m_keyframedue};
|
|
auto const lookstart{std::chrono::steady_clock::now()};
|
|
look_around(keyframe);
|
|
auto const looktime{std::chrono::steady_clock::now() - lookstart};
|
|
m_looktime += looktime;
|
|
m_looklongest = std::max(m_looklongest, looktime);
|
|
++m_looks;
|
|
if (keyframe)
|
|
m_keyframedue = std::max(m_keyframedue + keyframeinterval, m_time);
|
|
m_lookdue = std::max(m_lookdue + lookinterval, m_time);
|
|
m_file.flush();
|
|
}
|
|
}
|
|
|
|
void blackbox_recorder::close() {
|
|
std::scoped_lock lock{m_mutex};
|
|
m_started = true;
|
|
if (m_writer == nullptr)
|
|
return;
|
|
m_writer->end(m_time);
|
|
m_writer.reset();
|
|
m_file.close();
|
|
using milliseconds = std::chrono::duration<double, std::milli>;
|
|
WriteLog(std::format("Black box: recording closed; {} looks around, {:.3f} ms each on average, the longest {:.3f} ms", m_looks,
|
|
m_looks > 0 ? milliseconds{m_looktime}.count() / m_looks : 0.0, milliseconds{m_looklongest}.count()));
|
|
}
|
|
|
|
void blackbox_recorder::open() {
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
std::error_code error;
|
|
std::filesystem::create_directories("physicslog", error);
|
|
auto const filename{std::format("physicslog/{}_{}.blackbox", scenery_stem(Global.SceneryFile), local_time("%Y-%m-%d_%H-%M-%S"))};
|
|
m_file.open(filename, std::ios::out | std::ios::binary | std::ios::trunc);
|
|
if (false == m_file.is_open()) {
|
|
ErrorLog(std::format("Black box: can't create \"{}\"", filename));
|
|
return;
|
|
}
|
|
// a buffer of its own, so the file is written to the disk in large pieces, mostly at the flush after a look;
|
|
// given once the file is open and before anything is written: MSVC's file buffer passes over one given earlier
|
|
m_file.rdbuf()->pubsetbuf(m_filebuffer.data(), static_cast<std::streamsize>(m_filebuffer.size()));
|
|
m_writer = std::make_unique<blackbox::writer>(m_file);
|
|
WriteLog(std::format("Black box: recording to \"{}\"", filename));
|
|
|
|
auto const &time{simulation::Time.data()};
|
|
m_writer->header();
|
|
m_writer->info({text_of(Global.SceneryFile), local_time("%Y-%m-%dT%H:%M:%S"), static_cast<std::uint16_t>(time.wYear), static_cast<std::uint16_t>(simulation::Time.year_day()),
|
|
second_of_day(), text_of(Global.local_start_vehicle), text_of(Global.asVersion), static_cast<float>(cabinterval), static_cast<float>(lookinterval),
|
|
static_cast<float>(keyframeinterval)});
|
|
write_map();
|
|
for (auto *vehicle : simulation::Vehicles.sequence())
|
|
if (vehicle->bEnabled)
|
|
vehicle_id(vehicle);
|
|
// the state at the start, the look listing all of it
|
|
look_around(true);
|
|
m_keyframedue = keyframeinterval;
|
|
m_lookdue = lookinterval;
|
|
m_cabdue = 0.0;
|
|
m_file.flush();
|
|
}
|
|
|
|
// where the stop points read off the tracks are and which way the trains they are for go: the driver reads the events
|
|
// of the second end of a track going from its first end to the second, those of the first end going the other way
|
|
std::vector<std::pair<glm::dvec3, float>> blackbox_recorder::stop_point_headings() {
|
|
std::vector<std::pair<glm::dvec3, float>> headings;
|
|
for (auto *track : simulation::Paths.sequence()) {
|
|
if (track->Segment == nullptr || track->m_editorremoved)
|
|
continue;
|
|
for (auto const forward : {true, false})
|
|
for (auto const &[name, event] : (forward ? track->m_events2 : track->m_events1))
|
|
if (event != nullptr && event->input_command() == TCommandType::cm_PassengerStopPoint)
|
|
headings.emplace_back(event->input_location(), heading_along(*track->Segment, event->input_location(), forward));
|
|
}
|
|
return headings;
|
|
}
|
|
|
|
void blackbox_recorder::write_map() {
|
|
std::uint32_t index{0};
|
|
for (auto *track : simulation::Paths.sequence()) {
|
|
if ((track->iCategoryFlag & 1) == 0 || track->m_editorremoved)
|
|
continue;
|
|
blackbox::track_data data;
|
|
data.index = index;
|
|
data.name = text_of(track->name());
|
|
data.category = 1;
|
|
if (track->eType == tt_Switch && track->SwitchExtension) {
|
|
write_switch(*m_writer, *track, data);
|
|
m_switches.emplace_back(track, index, -1);
|
|
}
|
|
else if (track->Segment) {
|
|
data.points = points_of(*track->Segment);
|
|
m_writer->track(data);
|
|
}
|
|
++index;
|
|
}
|
|
index = 0;
|
|
for (auto const &object : map::Objects.entries) {
|
|
auto const *semaphore{dynamic_cast<map::semaphore const *>(object.get())};
|
|
if (semaphore == nullptr || semaphore->memcell == nullptr)
|
|
continue;
|
|
auto const location{semaphore->memcell->location()};
|
|
m_writer->signal(index, text_of(semaphore->name), location);
|
|
m_signals.emplace_back(semaphore->memcell, location);
|
|
++index;
|
|
}
|
|
// the stop points (W4) and the horn points (W6): events the trains passing by read from the track, telling where
|
|
// the station they are for is and to sound the horn. a stop point may be a memory cell too
|
|
auto const headings{stop_point_headings()};
|
|
auto const heading_of{[&headings](glm::dvec3 const &Location) {
|
|
// the way the trains it is for go, unless it is on no track or on tracks of both ways
|
|
auto heading{std::numeric_limits<float>::quiet_NaN()};
|
|
for (auto const &[location, way] : headings) {
|
|
if (glm::distance(location, Location) > 0.5)
|
|
continue;
|
|
if (std::isnan(heading))
|
|
heading = way;
|
|
else if (std::abs(std::remainder(heading - way, 2.0f * glm::pi<float>())) > 0.5f)
|
|
return std::numeric_limits<float>::quiet_NaN();
|
|
}
|
|
return heading;
|
|
}};
|
|
constexpr std::string_view stoppoint{"PassengerStopPoint:"};
|
|
std::uint32_t stoppoints{0};
|
|
std::uint32_t hornpoints{0};
|
|
for (auto const *event : simulation::Events.sequence()) {
|
|
auto const *values{dynamic_cast<putvalues_event const *>(event)};
|
|
if (values == nullptr)
|
|
continue;
|
|
auto const &text{values->input_text()};
|
|
// the name in the form the station records and the rows of the timetable have theirs, as it always was
|
|
if (text.starts_with(stoppoint))
|
|
m_writer->stop_point(stoppoints++, text_of(timetable::station_ascii(text.substr(stoppoint.size()))), values->input_location(),
|
|
static_cast<float>(values->input_value(1)), static_cast<float>(values->input_value(2)), heading_of(values->input_location()));
|
|
else if (text == "Warning_signal")
|
|
m_writer->horn_point(hornpoints++, text_of(values->name()), values->input_location(), static_cast<float>(values->input_value(1)),
|
|
static_cast<float>(values->input_value(2)));
|
|
}
|
|
for (auto const *cell : simulation::Memory.sequence()) {
|
|
if (cell == nullptr || false == cell->Text().starts_with(stoppoint))
|
|
continue;
|
|
m_writer->stop_point(stoppoints++, text_of(std::string_view{cell->Text()}.substr(stoppoint.size())), cell->location(), static_cast<float>(cell->Value1()),
|
|
static_cast<float>(cell->Value2()), heading_of(cell->location()));
|
|
}
|
|
}
|
|
|
|
void blackbox_recorder::write_cab() {
|
|
auto *train{simulation::Train};
|
|
if (train == nullptr || train->Dynamic() == nullptr)
|
|
return;
|
|
auto const *vehicle{train->Dynamic()};
|
|
auto const *occupied{vehicle->MoverParameters};
|
|
// the one which drives the consist, as the driving aid tells it
|
|
auto const *owner{vehicle->ctOwner != nullptr ? vehicle->ctOwner : vehicle->Mechanik};
|
|
auto const *controlling{owner != nullptr ? owner->Controlling() : occupied};
|
|
|
|
blackbox::cab_sample sample;
|
|
sample.time = m_time;
|
|
sample.vehicle = vehicle_id(vehicle);
|
|
sample.position = vehicle->GetPosition();
|
|
sample.speed_kmh = static_cast<float>(occupied->V * 3.6);
|
|
// of the wheels as they turn, which the speedometer shows: apart from that of the vehicle when they slip or skid.
|
|
// those of the powered vehicle, as the speedometer has it (TTrain::DynamicSet), which in a unit driven from an
|
|
// unpowered car is another one, maybe facing the other way: signed as the occupied one's speed
|
|
// the vehicle itself, if none in the unit is powered
|
|
auto const *powered{train->Dynamic()->FindPowered()};
|
|
auto const facing{powered->DirectionGet() * vehicle->DirectionGet()};
|
|
sample.wheel_speed_kmh = static_cast<float>(facing * M_PI * powered->MoverParameters->WheelDiameter * powered->MoverParameters->nrot * 3.6);
|
|
sample.acceleration = static_cast<float>(controlling->AccS);
|
|
sample.lateral_acceleration = static_cast<float>(occupied->AccN);
|
|
if (owner != nullptr && owner->is_train()) {
|
|
auto const order{owner->order_current()};
|
|
sample.movement = (order & Obey_train) != 0 ? blackbox::movements::train :
|
|
(order & (Shunt | Loose_shunt | Connect)) != 0 ? blackbox::movements::shunting : blackbox::movements::other;
|
|
sample.light_hint_head = static_cast<std::int16_t>(owner->light_hint(end::front));
|
|
sample.light_hint_tail = static_cast<std::int16_t>(owner->light_hint(end::rear));
|
|
}
|
|
sample.odometer_km = static_cast<float>(occupied->DistCounter);
|
|
sample.driving = driver_of(*vehicle);
|
|
sample.flags = (FreeFlyModeFlag ? 0 : blackbox::cab_flags::in_cab) | (owner != nullptr && owner->is_active() ? blackbox::cab_flags::ai_valid : 0) |
|
|
(controlling->SecuritySystem.is_blinking() ? blackbox::cab_flags::alerter_blinking : 0) |
|
|
(controlling->SecuritySystem.is_beeping() ? blackbox::cab_flags::alerter_beeping : 0) | (controlling->SlippingWheels ? blackbox::cab_flags::wheel_slip : 0) |
|
|
(occupied->RadioStopFlag ? blackbox::cab_flags::radio_stop : 0) | (occupied->AlarmChainFlag ? blackbox::cab_flags::alarm_chain : 0);
|
|
sample.main_controller = static_cast<std::int8_t>(controlling->MainCtrlPos);
|
|
sample.scnd_controller = static_cast<std::int8_t>(controlling->ScndCtrlPos);
|
|
sample.reverser = static_cast<std::int8_t>(controlling->DirActive);
|
|
sample.train_brake = static_cast<float>(occupied->fBrakeCtrlPos);
|
|
sample.local_brake = static_cast<float>(occupied->LocalBrakePosA);
|
|
sample.brake_cylinder = static_cast<float>(occupied->BrakePress);
|
|
sample.brake_pipe = static_cast<float>(occupied->PipePress);
|
|
sample.motor_current = static_cast<float>(controlling->Im);
|
|
if (owner != nullptr && owner->is_active())
|
|
sample.ai = owner->ai_reference();
|
|
sample.devices = devices_of(*occupied, *controlling);
|
|
sample.main_reservoir = static_cast<float>(controlling->Compressor);
|
|
sample.voltage = static_cast<float>(controlling->EngineVoltage);
|
|
sample.engine_rpm = static_cast<float>(std::abs(controlling->enrot) * 60.0);
|
|
// the lights at the ends of the train: of the vehicles at its ends, at their ends turned away from the rest
|
|
sample.lights_head = outward_lights(owner != nullptr ? owner->Vehicle(end::front) : vehicle, true);
|
|
sample.lights_tail = outward_lights(owner != nullptr ? owner->Vehicle(end::rear) : vehicle, false);
|
|
sample.equipped_head = outward_lamps(owner != nullptr ? owner->Vehicle(end::front) : vehicle, true);
|
|
sample.equipped_tail = outward_lamps(owner != nullptr ? owner->Vehicle(end::rear) : vehicle, false);
|
|
auto const &security{controlling->SecuritySystem};
|
|
namespace flag = blackbox::security_flags;
|
|
sample.security = static_cast<std::uint8_t>((security.is_vigilance_blinking() ? flag::vigilance : 0) | (security.is_cabsignal_blinking() ? flag::cabsignal : 0) |
|
|
(security.is_beeping() ? flag::vigilance_beeping : 0) | (security.is_cabsignal_beeping() ? flag::cabsignal_beeping : 0) |
|
|
(security.is_braking() ? flag::braking : 0));
|
|
sample.cab = static_cast<std::int8_t>(occupied->CabOccupied);
|
|
// what drives the train, the setting of the brakes, the radio, the engine's oil and temperatures
|
|
sample.engine = engine_of(*controlling);
|
|
sample.brake_delay = static_cast<std::uint8_t>(occupied->BrakeDelayFlag);
|
|
// the const one: with no driver the first channel
|
|
sample.radio_channel = static_cast<std::uint8_t>(std::clamp(std::as_const(*train).RadioChannel(), 0, 255));
|
|
sample.driver_valve = occupied->Handle != nullptr ? static_cast<float>(occupied->Handle->GetCP()) : 0.0f;
|
|
sample.feed_pipe = static_cast<float>(occupied->ScndPipePress);
|
|
sample.oil_pressure = controlling->OilPump.pressure;
|
|
sample.water_temperature = controlling->dizel_heat.Twy;
|
|
sample.oil_temperature = controlling->dizel_heat.To;
|
|
sample.engine_temperature = controlling->dizel_heat.Ts;
|
|
sample.speed_control_kmh = controlling->SpeedCtrl ? static_cast<float>(controlling->SpeedCtrlUnit.DesiredVelocity) : 0.0f;
|
|
sample.engine_power = static_cast<float>(controlling->EnginePower);
|
|
sample.hand_brake = static_cast<std::int8_t>(std::clamp(occupied->ManualBrakePos, -128, 127));
|
|
sample.brake_mode = static_cast<std::uint8_t>(occupied->BrakeOpModeFlag);
|
|
sample.horn = static_cast<std::uint8_t>(occupied->WarningSignal);
|
|
sample.integrated_controller = static_cast<float>(controlling->eimic);
|
|
sample.clutch = static_cast<float>(controlling->dizel_engagestate);
|
|
sample.converter_fill = static_cast<float>(controlling->hydro_TC_Fill);
|
|
// the brakes of the whole train: the highest of its cylinders, from the head to the tail
|
|
auto brakes{occupied->BrakePress};
|
|
for (auto const *each{owner != nullptr ? owner->Vehicle(end::front) : nullptr}; each != nullptr; each = each->Next())
|
|
if (each->MoverParameters != nullptr)
|
|
brakes = std::max(brakes, each->MoverParameters->BrakePress);
|
|
sample.consist_brake_cylinder = static_cast<float>(brakes);
|
|
m_writer->cab(sample);
|
|
}
|
|
|
|
void blackbox_recorder::keep_tape_line() {
|
|
auto const *train{simulation::Train};
|
|
if (train == nullptr || train->Dynamic() == nullptr)
|
|
return;
|
|
auto const *vehicle{train->Dynamic()};
|
|
// the driver of the vehicle, whose log it would be: it keeps the name of its first vehicle, so a change of the cab
|
|
// in a unit goes on in the same log. a passive one stands still (its clock too), so then the driver of the train
|
|
auto const *driver{vehicle->Mechanik != nullptr && vehicle->Mechanik->primary() ? vehicle->Mechanik : vehicle->ctOwner};
|
|
if (driver == nullptr)
|
|
return;
|
|
// a driver writing its own, of a train with "physicslog.trains yes", isn't written twice
|
|
if (driver->writes_physicslog())
|
|
return;
|
|
auto const &name{driver->physicslog_name()};
|
|
if (m_tapes.insert(name).second) {
|
|
PhysicsLogs.begin(name, TController::physicslog_header());
|
|
WriteLog(std::format("Black box: the physics log \"{}\" begun", PhysicsLogs.shown(name)));
|
|
}
|
|
PhysicsLogs.append(name, driver->physicslog_line());
|
|
}
|
|
|
|
void blackbox_recorder::look_around(bool const Keyframe) {
|
|
auto const &time{simulation::Time.data()};
|
|
m_writer->clock(m_time, static_cast<std::uint16_t>(time.wYear), static_cast<std::uint16_t>(simulation::Time.year_day()), second_of_day());
|
|
|
|
auto *train{simulation::Train};
|
|
auto const uservehicle{train != nullptr && train->Dynamic() != nullptr ? vehicle_id(train->Dynamic()) : blackbox::no_vehicle};
|
|
auto const userincab{uservehicle != blackbox::no_vehicle && false == FreeFlyModeFlag};
|
|
auto const usercab{uservehicle != blackbox::no_vehicle ? static_cast<std::int8_t>(train->Dynamic()->MoverParameters->CabOccupied) : std::int8_t{0}};
|
|
if (false == m_userknown || uservehicle != m_uservehicle || userincab != m_userincab || usercab != m_usercab) {
|
|
m_writer->user_vehicle(m_time, uservehicle, userincab, usercab);
|
|
m_uservehicle = uservehicle;
|
|
m_userincab = userincab;
|
|
m_usercab = usercab;
|
|
m_userknown = true;
|
|
}
|
|
if (uservehicle != blackbox::no_vehicle)
|
|
look_at_timetable(*train->Dynamic(), uservehicle);
|
|
|
|
// each part timed, to tell which one it was when a look takes long
|
|
using clock = std::chrono::steady_clock;
|
|
auto const start{clock::now()};
|
|
look_at_vehicles(Keyframe);
|
|
auto const vehicles{clock::now()};
|
|
look_at_couplers();
|
|
look_at_derailments();
|
|
auto const couplers{clock::now()};
|
|
look_at_switches();
|
|
auto const switches{clock::now()};
|
|
look_at_signals();
|
|
auto const signals{clock::now()};
|
|
using milliseconds = std::chrono::duration<double, std::milli>;
|
|
if (milliseconds{signals - start}.count() > slowlook) {
|
|
WriteLog(std::format("Black box: a look around at {:.1f} s took {:.1f} ms: vehicles {:.1f} ({}{}), couplers {:.1f}, switches {:.1f}, signals {:.1f}", m_time,
|
|
milliseconds{signals - start}.count(), milliseconds{vehicles - start}.count(), m_vehicles.size(), Keyframe ? ", keyframe" : "",
|
|
milliseconds{couplers - vehicles}.count(), milliseconds{switches - couplers}.count(), milliseconds{signals - switches}.count()));
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
|
|
std::uint8_t cause_of(derail_motion::cause const Cause) {
|
|
switch (Cause) {
|
|
case derail_motion::cause::overturned:
|
|
return blackbox::derailment_causes::overturned;
|
|
case derail_motion::cause::end_of_track:
|
|
return blackbox::derailment_causes::end_of_track;
|
|
case derail_motion::cause::pulled_off:
|
|
return blackbox::derailment_causes::pulled_off;
|
|
case derail_motion::cause::collision:
|
|
return blackbox::derailment_causes::collision;
|
|
default:
|
|
return blackbox::derailment_causes::flange_climbed;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void blackbox_recorder::look_at_derailments() {
|
|
for (auto *vehicle : simulation::Vehicles.sequence()) {
|
|
if (false == vehicle->bEnabled)
|
|
continue;
|
|
auto const &motion{vehicle->MoverParameters->OffTrack};
|
|
auto const id{vehicle_id(vehicle)};
|
|
auto &entry{m_vehicles[vehicle]};
|
|
if (false == motion.active()) {
|
|
entry.derailment = 0;
|
|
continue;
|
|
}
|
|
auto const phase{motion.at_rest() ? 2 : 1};
|
|
if (entry.derailment >= phase)
|
|
continue;
|
|
blackbox::derailment_state state;
|
|
state.time = m_time;
|
|
state.vehicle = id;
|
|
state.cause = cause_of(motion.reason());
|
|
state.position = vehicle->GetPosition();
|
|
state.heading = static_cast<float>(std::atan2(vehicle->VectorFront().x, vehicle->VectorFront().z));
|
|
state.speed_kmh = static_cast<float>(vehicle->MoverParameters->V * 3.6);
|
|
state.offset = static_cast<float>(vehicle->off_track_offset());
|
|
state.roll = static_cast<float>(motion.roll());
|
|
state.travelled = static_cast<float>(motion.travelled());
|
|
state.lying = motion.lying();
|
|
// seen first at rest, it left the track before
|
|
for (auto written = entry.derailment + 1; written <= phase; ++written) {
|
|
state.phase = (written == 1 ? blackbox::derailment_phases::left_track : blackbox::derailment_phases::at_rest);
|
|
m_writer->derailment(state);
|
|
}
|
|
entry.derailment = phase;
|
|
}
|
|
}
|
|
|
|
void blackbox_recorder::look_at_couplers() {
|
|
for (auto *vehicle : simulation::Vehicles.sequence()) {
|
|
if (false == vehicle->bEnabled)
|
|
continue;
|
|
auto &entry{m_vehicles[vehicle]};
|
|
auto const *mover{vehicle->MoverParameters};
|
|
for (int side = 0; side < 2; ++side) {
|
|
auto const flags{mover->Couplers[side].CouplingFlag};
|
|
// a neighbour not coupled is only seen on the same track
|
|
auto const *other{flags != 0 ? mover->Neighbours[side].vehicle : nullptr};
|
|
coupler_entry const now{other, other != nullptr ? mover->Neighbours[side].vehicle_end : 0, other != nullptr ? flags : 0};
|
|
auto &last{entry.couplers[side]};
|
|
auto const changed{now.other != last.other || now.other_side != last.other_side || now.flags != last.flags};
|
|
auto const before{last};
|
|
last = now;
|
|
// at the first look only what is coupled; then each change
|
|
if ((entry.couplers_known && false == changed) || (false == entry.couplers_known && now.other == nullptr))
|
|
continue;
|
|
// both vehicles see a coupling change: the one with the lower number tells it
|
|
auto const *counterpart{now.other != nullptr ? now.other : before.other};
|
|
if (counterpart != nullptr) {
|
|
auto const found{m_vehicles.find(counterpart)};
|
|
if (found != m_vehicles.end() && found->second.id < entry.id)
|
|
continue;
|
|
}
|
|
blackbox::coupling_change change;
|
|
change.time = m_time;
|
|
change.vehicle = entry.id;
|
|
change.side = static_cast<std::uint8_t>(side);
|
|
change.other = known_id(now.other);
|
|
change.other_side = static_cast<std::uint8_t>(now.other_side);
|
|
change.flags = static_cast<std::uint8_t>(now.flags);
|
|
change.previous_flags = static_cast<std::uint8_t>(entry.couplers_known ? before.flags : 0);
|
|
change.previous_other = entry.couplers_known ? known_id(before.other) : blackbox::no_vehicle;
|
|
change.position = vehicle->CouplerPosition(static_cast<end>(side));
|
|
m_writer->coupling(change);
|
|
}
|
|
entry.couplers_known = true;
|
|
}
|
|
}
|
|
|
|
void blackbox_recorder::camera(blackbox::camera_state Camera, TDynamicObject const *Owner) {
|
|
std::scoped_lock lock{m_mutex};
|
|
m_camera = Camera;
|
|
m_cameraowner = Owner;
|
|
m_cameraknown = true;
|
|
}
|
|
|
|
void blackbox_recorder::write_camera() {
|
|
if (false == m_cameraknown)
|
|
return;
|
|
auto camera{m_camera};
|
|
camera.time = m_time;
|
|
camera.vehicle = known_id(m_cameraowner);
|
|
auto const same{m_camerawrote && camera.mode == m_camerawritten.mode && camera.view == m_camerawritten.view && camera.vehicle == m_camerawritten.vehicle};
|
|
// In the cab and in the views of the train the camera goes with the train, whose way is written
|
|
// anyway: only a change of the view counts. The free camera is written as it moves and turns.
|
|
auto const attached{camera.mode == blackbox::camera_modes::cab || camera.mode == blackbox::camera_modes::external};
|
|
auto const turned{std::abs(std::remainder(camera.heading - m_camerawritten.heading, 2.0f * glm::pi<float>())) > cameraturn ||
|
|
std::abs(camera.pitch - m_camerawritten.pitch) > cameraturn};
|
|
if (same && (attached || (glm::distance(camera.position, m_camerawritten.position) <= cameramove && false == turned)))
|
|
return;
|
|
m_writer->camera(camera);
|
|
m_camerawritten = camera;
|
|
m_camerawrote = true;
|
|
}
|
|
|
|
void blackbox_recorder::look_at_vehicles(bool const Keyframe) {
|
|
std::vector<blackbox::vehicle_state> states;
|
|
for (auto *vehicle : simulation::Vehicles.sequence()) {
|
|
if (false == vehicle->bEnabled)
|
|
continue;
|
|
auto const id{vehicle_id(vehicle)};
|
|
auto &entry{m_vehicles[vehicle]};
|
|
auto const state{state_of(*vehicle, id)};
|
|
auto const changed{false == entry.listed || glm::distance(state.position, entry.last.position) > listmove || std::abs(state.speed_kmh - entry.last.speed_kmh) > listspeed ||
|
|
state.driving != entry.last.driving};
|
|
if (Keyframe || changed) {
|
|
states.push_back(state);
|
|
entry.last = state;
|
|
entry.listed = true;
|
|
}
|
|
}
|
|
if (Keyframe || false == states.empty())
|
|
m_writer->vehicles(m_time, Keyframe, states);
|
|
}
|
|
|
|
void blackbox_recorder::look_at_switches() {
|
|
for (auto &entry : m_switches) {
|
|
auto const state{entry.track->GetSwitchState()};
|
|
if (state == entry.state || state < 0)
|
|
continue;
|
|
entry.state = state;
|
|
m_writer->switch_state(m_time, entry.index, static_cast<std::uint8_t>(state));
|
|
}
|
|
}
|
|
|
|
void blackbox_recorder::look_at_signals() {
|
|
for (std::size_t i = 0; i < m_signals.size(); ++i) {
|
|
auto &entry{m_signals[i]};
|
|
auto const speed1{static_cast<float>(entry.cell->Value1())};
|
|
auto const speed2{static_cast<float>(entry.cell->Value2())};
|
|
auto const &command{entry.cell->Text()};
|
|
if (entry.known && speed1 == entry.speed1 && speed2 == entry.speed2 && command == entry.command)
|
|
continue;
|
|
entry.speed1 = speed1;
|
|
entry.speed2 = speed2;
|
|
entry.command = command;
|
|
entry.known = true;
|
|
m_writer->signal_aspect(m_time, static_cast<std::uint32_t>(i), speed1, speed2, text_of(command));
|
|
}
|
|
}
|
|
|
|
void blackbox_recorder::look_at_timetable(TDynamicObject const &Vehicle, std::uint32_t const Id) {
|
|
auto const *owner{Vehicle.ctOwner != nullptr ? Vehicle.ctOwner : Vehicle.Mechanik};
|
|
if (owner == nullptr)
|
|
return;
|
|
auto const &timetable{owner->TrainTimetable()};
|
|
// the user gone over to a vehicle of another consist: its timetable is written even if it has the same name, and so
|
|
// is the one of the train the user comes back to; the other cab or a car of the same train keeps it
|
|
if (false == coupled(&Vehicle, m_timetablevehicle)) {
|
|
m_timetabletrain.clear();
|
|
m_timetablestations = 0;
|
|
}
|
|
m_timetablevehicle = &Vehicle;
|
|
// the timetable is cleared at the end of the route, where the train stays the one it was
|
|
if (timetable.StationCount <= 0 || (timetable.TrainName == m_timetabletrain && timetable.StationCount == m_timetablestations))
|
|
return;
|
|
m_timetabletrain = timetable.TrainName;
|
|
m_timetablestations = timetable.StationCount;
|
|
blackbox::timetable_data data;
|
|
data.time = m_time;
|
|
data.vehicle = Id;
|
|
data.train = text_of(timetable.TrainName);
|
|
data.category = text_of(timetable.TrainCategory);
|
|
data.from = text_of(timetable.Relation1);
|
|
data.to = text_of(timetable.Relation2);
|
|
data.vmax = static_cast<float>(timetable.TTVmax);
|
|
auto const minutes{[](int const Hour, float const Minute) { return Hour >= 0 ? static_cast<float>(Hour * 60) + Minute : -1.0f; }};
|
|
for (int i = 1; i <= timetable.StationCount; ++i) {
|
|
auto const &line{timetable.TimeTable[i]};
|
|
data.rows.push_back({text_of(line.StationName), line.km, line.vmax, minutes(line.Ah, line.Am), minutes(line.Dh, line.Dm), line.is_maintenance});
|
|
}
|
|
m_writer->timetable(data);
|
|
}
|
|
|
|
std::uint32_t blackbox_recorder::vehicle_id(TDynamicObject const *Vehicle) {
|
|
if (auto const lookup{m_vehicles.find(Vehicle)}; lookup != m_vehicles.end())
|
|
return lookup->second.id;
|
|
|
|
auto &entry{m_vehicles[Vehicle]};
|
|
entry.id = m_nextvehicle++;
|
|
auto const *mover{Vehicle->MoverParameters};
|
|
auto const *driver{Vehicle->Mechanik};
|
|
auto const train{driver != nullptr && driver->TrainName() != "none" ? driver->TrainName() : std::string{}};
|
|
m_writer->vehicle({entry.id, text_of(Vehicle->name()), text_of(mover->TypeName), text_of(train), static_cast<float>(mover->Dim.L), static_cast<float>(mover->Dim.W),
|
|
static_cast<std::uint8_t>(mover->CategoryFlag)});
|
|
return entry.id;
|
|
}
|
|
|
|
std::uint32_t blackbox_recorder::known_id(TDynamicObject const *Vehicle) {
|
|
// a vehicle taken off the scenery is gone from the recording: one still pointed at gets no new number
|
|
return Vehicle != nullptr && Vehicle->bEnabled ? vehicle_id(Vehicle) : blackbox::no_vehicle;
|
|
}
|
|
|
|
std::uint32_t blackbox_recorder::signal_of(TMemCell const *Cell) const {
|
|
// by the memory cell, not by its position: two signals can have their cells at one point
|
|
auto const found{std::ranges::find(m_signals, Cell, &signal_entry::cell)};
|
|
if (found == m_signals.end())
|
|
return blackbox::no_signal;
|
|
return static_cast<std::uint32_t>(std::distance(m_signals.begin(), found));
|
|
}
|
|
|
|
void blackbox_recorder::command(TDynamicObject const *Vehicle, user_command const Command, int const Action, double const Param1, double const Param2) {
|
|
if (Action != GLFW_PRESS && Action != GLFW_RELEASE)
|
|
return;
|
|
std::scoped_lock lock{m_mutex};
|
|
if (m_writer == nullptr)
|
|
return;
|
|
m_writer->command({m_time, Vehicle != nullptr ? vehicle_id(Vehicle) : blackbox::no_vehicle, Action == GLFW_PRESS, Param1, Param2,
|
|
simulation::Commands_descriptions[static_cast<std::size_t>(Command)].name});
|
|
}
|
|
|
|
void blackbox_recorder::driver_changed(TDynamicObject const *Vehicle, bool const Ai) {
|
|
std::scoped_lock lock{m_mutex};
|
|
if (m_writer == nullptr || Vehicle == nullptr)
|
|
return;
|
|
m_writer->driver_change(m_time, vehicle_id(Vehicle), Ai ? blackbox::driver::ai : blackbox::driver::human);
|
|
}
|
|
|
|
void blackbox_recorder::signal_passed(TDynamicObject const *Vehicle, basic_event const &Event, double const Speed1, double const Speed2, double const Trainspeed, double const Overrun,
|
|
std::uint8_t const Flags) {
|
|
std::scoped_lock lock{m_mutex};
|
|
if (m_writer == nullptr || Vehicle == nullptr)
|
|
return;
|
|
auto const location{Event.input_location()};
|
|
m_writer->signal_passed({m_time, vehicle_id(Vehicle), signal_of(Event.input_cell()), text_of(Event.name()), location, static_cast<float>(Speed1), static_cast<float>(Speed2),
|
|
static_cast<float>(Trainspeed), static_cast<float>(Overrun), Flags});
|
|
}
|
|
|
|
void blackbox_recorder::station(TDynamicObject const *Vehicle, blackbox::station_event Station) {
|
|
std::scoped_lock lock{m_mutex};
|
|
if (m_writer == nullptr || Vehicle == nullptr)
|
|
return;
|
|
// a train given its next timetable may come to the first station of it at once, before the next look: the
|
|
// timetable goes first, or the station would be read against the one before
|
|
// the vehicle the user is in now, which may have changed since the last look
|
|
if (auto const *train{simulation::Train}; m_userknown && train != nullptr && train->Dynamic() != nullptr)
|
|
look_at_timetable(*train->Dynamic(), vehicle_id(train->Dynamic()));
|
|
Station.time = m_time;
|
|
Station.vehicle = vehicle_id(Vehicle);
|
|
Station.station = text_of(Station.station);
|
|
m_writer->station(Station);
|
|
}
|
|
|
|
void blackbox_recorder::horn_requested(TDynamicObject const *Vehicle, double const Duration, double const Tone, glm::dvec3 const &Position) {
|
|
std::scoped_lock lock{m_mutex};
|
|
if (m_writer == nullptr || Vehicle == nullptr)
|
|
return;
|
|
m_writer->horn_request(m_time, vehicle_id(Vehicle), static_cast<float>(Duration), static_cast<float>(Tone), Position);
|
|
}
|
|
|
|
void blackbox_recorder::vehicle_removed(TDynamicObject const *Vehicle) {
|
|
std::scoped_lock lock{m_mutex};
|
|
if (Vehicle == m_timetablevehicle)
|
|
m_timetablevehicle = nullptr;
|
|
if (Vehicle == m_cameraowner)
|
|
m_cameraowner = nullptr;
|
|
auto const lookup{m_vehicles.find(Vehicle)};
|
|
if (lookup == m_vehicles.end())
|
|
return;
|
|
if (m_writer != nullptr)
|
|
m_writer->vehicle_removed(m_time, lookup->second.id);
|
|
// the memory may be given to another vehicle later, which is to get a number of its own
|
|
m_vehicles.erase(lookup);
|
|
}
|
|
|
|
} // namespace simulation
|