18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/vehicle/blackbox.h
jerrrrycho bc2d1323a3 feat(physics): a derailed vehicle leaves the track and moves on the ground (physics.derailment new) (#451)
* 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().
2026-11-26 22:26:37 +01:00

464 lines
20 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/.
*/
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <iosfwd>
#include <string>
#include <string_view>
#include <vector>
#include <glm/glm.hpp>
// the black box: a binary recording of a whole session, from which another program can replay it on the
// map of the scenery. the player's train in detail (with what the AI would have done at the time, every
// command, the autopilot, the signals passed), the other vehicles sparingly, the tracks and the signals
// with the changes of the switches and the aspects. the layout of the file is described in blackbox_doc.md;
// this is only the writer, which doesn't know the simulation, so the tests can check what it writes
namespace blackbox {
inline constexpr std::array<char, 8> magic{'E', 'U', '0', '7', 'B', 'B', 'X', '\0'};
inline constexpr std::uint16_t format_version{1};
// a record is its type, the length of what follows and that many bytes
enum class record : std::uint8_t {
info = 0x01,
track = 0x10,
signal = 0x11,
vehicle = 0x12,
vehicle_removed = 0x13,
stop_point = 0x14,
horn_point = 0x15,
cab = 0x20,
vehicles = 0x21,
switch_state = 0x22,
signal_aspect = 0x23,
timetable = 0x24,
camera = 0x25,
command = 0x30,
driver_change = 0x31,
signal_passed = 0x32,
user_vehicle = 0x33,
clock = 0x34,
station = 0x35,
horn_request = 0x36,
coupling = 0x37,
derailment = 0x38,
end = 0x3f
};
// who drives a vehicle
enum class driver : std::uint8_t { none = 0, human = 1, ai = 2 };
// no vehicle: a command of the simulation, the user out of any vehicle
inline constexpr std::uint32_t no_vehicle{0};
// a signal passed which isn't on the map
inline constexpr std::uint32_t no_signal{0xffffffff};
namespace cab_flags {
inline constexpr std::uint8_t in_cab{0x01}; // the user is in the cab, not looking around outside
inline constexpr std::uint8_t ai_valid{0x02}; // the consist is started, so what the AI would do is worked out
inline constexpr std::uint8_t alerter_blinking{0x04};
inline constexpr std::uint8_t alerter_beeping{0x08};
inline constexpr std::uint8_t wheel_slip{0x10};
inline constexpr std::uint8_t radio_stop{0x20};
inline constexpr std::uint8_t alarm_chain{0x40};
} // namespace cab_flags
// what the driver of the user's train goes by: the lights it is to show depend on it
namespace movements {
inline constexpr std::uint8_t other{0}; // no driver, or standing by
inline constexpr std::uint8_t train{1}; // a train run (Pc 1 or Pc 2 at the head, Pc 5 at the tail)
inline constexpr std::uint8_t shunting{2}; // shunting (Tb 1)
} // namespace movements
// what is switched on in the user's train
namespace cab_devices {
inline constexpr std::uint32_t battery{0x0001};
inline constexpr std::uint32_t main_breaker{0x0002};
inline constexpr std::uint32_t pantograph_front{0x0004};
inline constexpr std::uint32_t pantograph_rear{0x0008};
inline constexpr std::uint32_t converter{0x0010};
inline constexpr std::uint32_t compressor{0x0020};
inline constexpr std::uint32_t line_contactors{0x0040}; // the motors are connected
inline constexpr std::uint32_t overload_relay{0x0080}; // tripped
inline constexpr std::uint32_t sanding{0x0100};
inline constexpr std::uint32_t horn{0x0200};
inline constexpr std::uint32_t doors_left{0x0400}; // open
inline constexpr std::uint32_t doors_right{0x0800}; // open
inline constexpr std::uint32_t radio{0x1000};
inline constexpr std::uint32_t heating{0x2000}; // of the train
inline constexpr std::uint32_t spring_brake{0x4000}; // applied
inline constexpr std::uint32_t shunt_mode{0x8000};
inline constexpr std::uint32_t departure_signal{0x0001'0000}; // sounding
inline constexpr std::uint32_t door_permit_left{0x0002'0000}; // the doors may be opened
inline constexpr std::uint32_t door_permit_right{0x0004'0000};
inline constexpr std::uint32_t door_step{0x0008'0000}; // the steps come out with the doors
inline constexpr std::uint32_t door_lock{0x0010'0000}; // the doors lock above a speed
inline constexpr std::uint32_t doors_locked{0x0020'0000}; // locked now
inline constexpr std::uint32_t brake_releaser{0x0040'0000}; // releasing the brakes of the vehicle
inline constexpr std::uint32_t fuel_pump{0x0080'0000};
inline constexpr std::uint32_t oil_pump{0x0100'0000};
inline constexpr std::uint32_t motor_blowers{0x0200'0000}; // the fans of the traction motors
inline constexpr std::uint32_t engine_hot{0x0400'0000}; // the diesel engine is over its temperature
inline constexpr std::uint32_t speed_control{0x0800'0000}; // the cruise control holds a speed
} // namespace cab_devices
// what drives the vehicle whose motors the cab drives, for a reader to show what matters for it
namespace engines {
inline constexpr std::uint8_t none{0}; // no engine of its own: a draisine, a car
inline constexpr std::uint8_t electric{1}; // series motors
inline constexpr std::uint8_t electric_induction{2}; // induction motors
inline constexpr std::uint8_t diesel{3}; // with a mechanical or a hydraulic transmission
inline constexpr std::uint8_t diesel_electric{4};
inline constexpr std::uint8_t steam{5};
} // namespace engines
// the security system of the user's train: the alerter (CA) and the cab signalling (SHP)
namespace security_flags {
inline constexpr std::uint8_t vigilance{0x01}; // the alerter lamp blinks: the driver has to acknowledge
inline constexpr std::uint8_t cabsignal{0x02}; // the cab signal lamp blinks: an SHP magnet was passed
inline constexpr std::uint8_t vigilance_beeping{0x04};
inline constexpr std::uint8_t cabsignal_beeping{0x08};
inline constexpr std::uint8_t braking{0x10}; // not acknowledged in time: the system brakes the train
} // namespace security_flags
namespace station_kinds {
inline constexpr std::uint8_t stopped{1}; // stood at the stop point of a timetabled stop
inline constexpr std::uint8_t passed{2}; // passed a station where the timetable has no stop
// left the stop point of a timetabled stop behind before it was time to go: without stopping at it, or, after a
// stop told before, leaving before the departure
inline constexpr std::uint8_t missed{3};
inline constexpr std::uint8_t ready{4}; // still at the stop point when it was time to go: the train may leave
} // namespace station_kinds
// where the user looks from
namespace camera_modes {
inline constexpr std::uint8_t cab{0}; // from the cab of the vehicle the user is in
inline constexpr std::uint8_t external{1}; // one of the outside views of the user's train
inline constexpr std::uint8_t free{2}; // the free camera, flying about
inline constexpr std::uint8_t debug{3}; // the debug camera
} // namespace camera_modes
namespace passing_flags {
inline constexpr std::uint8_t shunt{0x01}; // a shunting signal
inline constexpr std::uint8_t at_stop{0x02}; // passed showing stop
inline constexpr std::uint8_t automatic_block{0x04}; // stop of an automatic block signal, which may be passed slowly
} // namespace passing_flags
namespace track_kinds {
inline constexpr std::uint8_t normal{0};
inline constexpr std::uint8_t switch_branch{1};
} // namespace track_kinds
struct info_data {
std::string scenery; // the scenario file, as the simulator was given it
std::string started; // local time the recording started at, ISO 8601
std::uint16_t year{0}; // simulation date and time at the start
std::uint16_t year_day{0}; // 1..366
double second_of_day{0.0};
std::string start_vehicle; // the vehicle the user was to start in, ghostview for none
std::string simulator_version;
float cab_interval{0.0f}; // [s] between the samples of the user's train
float vehicles_interval{0.0f}; // [s] between the looks at the other vehicles
float keyframe_interval{0.0f}; // [s] between the looks which list all the vehicles
};
// one rail track of the map, or one of the two ways of a switch
struct track_data {
std::uint32_t index{0};
std::string name;
std::uint8_t kind{track_kinds::normal};
std::uint8_t branch{0}; // the switch position this way is taken in
std::uint8_t category{1}; // 1 rail
std::vector<glm::vec3> points; // world frame, y up
};
struct vehicle_data {
std::uint32_t id{no_vehicle};
std::string name;
std::string type;
std::string train; // train number of its timetable, if it leads one
float length{0.0f}; // [m]
float width{0.0f}; // [m]
std::uint8_t category{1}; // 1 rail, 2 road
};
// what the AI driver would do at this place and speed: the speed it would hold, the next change of the
// speed limit it sees and how hard it would accelerate (negative: brake)
struct ai_reference {
float target_kmh{0.0f};
float next_limit_kmh{-1.0f}; // -1 none in sight
float next_limit_distance{-1.0f}; // [m], -1 none in sight
float acceleration{0.0f}; // [m/s2]
float braking_distance{0.0f}; // [m]
float timetable_vmax_kmh{-1.0f}; // -1 none
float track_vmax_kmh{-1.0f}; // -1 none
};
// one look at the user's train
struct cab_sample {
double time{0.0};
std::uint32_t vehicle{no_vehicle};
glm::dvec3 position{0.0};
float speed_kmh{0.0f}; // signed: positive along the front of the vehicle
float acceleration{0.0f}; // [m/s2]
float odometer_km{0.0f};
driver driving{driver::none};
std::uint8_t flags{0}; // cab_flags
std::int8_t main_controller{0};
std::int8_t scnd_controller{0};
std::int8_t reverser{0};
float train_brake{0.0f}; // position of the driver's brake valve
float local_brake{0.0f}; // 0..1
float brake_cylinder{0.0f}; // [bar]
float brake_pipe{0.0f}; // [bar]
float motor_current{0.0f}; // [A]
ai_reference ai;
std::uint32_t devices{0}; // cab_devices
float main_reservoir{0.0f}; // [bar]
float voltage{0.0f}; // given to the motors [V]
float engine_rpm{0.0f}; // of the engine or the motors
std::uint16_t lights_head{0}; // the lights lit at the head of the train, bits of the simulator's light
std::uint16_t lights_tail{0}; // at its tail: the last vehicle
std::int8_t cab{0}; // the cab the user is in: 1 front, -1 rear, 0 none
std::uint8_t security{0}; // security_flags
float lateral_acceleration{0.0f}; // [m/s2] across the track, in the curves
std::uint8_t movement{0}; // movements: what the driver of the train goes by, which sets the lights it is to show
std::int16_t light_hint_head{-1}; // the light pattern the timetable or the scenery asks for at the head, -1 the usual
std::int16_t light_hint_tail{-1}; // at the tail
std::uint16_t equipped_head{0}; // the lamps the vehicle at the head has at that end: what of a pattern it can show
std::uint16_t equipped_tail{0}; // at the tail
std::uint8_t engine{engines::none}; // engines
std::uint8_t brake_delay{0}; // the setting of the brakes: 1 G, 2 P, 4 R, 8 Mg, as bits
std::uint8_t radio_channel{0};
// what the driver's brake valve shows on its gauge [bar]: the equalizing (control) reservoir of most valves,
// the time reservoir of the FV4a, which is empty but after an overcharge
float driver_valve{0.0f};
float feed_pipe{0.0f}; // the main reservoir pipe along the train [bar]
float oil_pressure{0.0f}; // of the diesel engine [MPa]
float water_temperature{0.0f}; // of the diesel engine's cooling [°C]
float oil_temperature{0.0f}; // [°C]
float engine_temperature{0.0f}; // [°C]
float speed_control_kmh{0.0f}; // the speed the cruise control is set to
float engine_power{0.0f}; // [kW]
std::int8_t hand_brake{0}; // the position of the hand brake, 0 released
std::uint8_t brake_mode{0}; // the mode of the brakes: 1 PS, 2 PN, 4 EP, 8 MED, as bits
std::uint8_t horn{0}; // the horn's tones sounding: 1 low, 2 high, 4 a third
float integrated_controller{0.0f}; // the one handle of driving and braking of a unit, -1..1
float clutch{0.0f}; // the clutch of a diesel's gearbox, 0 open .. 1 closed
float converter_fill{0.0f}; // the filling of a diesel's hydraulic torque converter, 0..1
float consist_brake_cylinder{0.0f}; // the highest brake cylinder pressure of the vehicles of the train [bar]
// [km/h] of the wheels as they turn, which the speedometer shows, signed as speed_kmh: apart from it while the wheels
// slip, and 0 while they are locked in a skid
float wheel_speed_kmh{0.0f};
};
// a station of the timetable of the user's train; the times in minutes of the day, -1 none
struct timetable_row {
std::string station;
float km{0.0f};
float vmax{-1.0f}; // [km/h] before the station
float arrival{-1.0f};
float departure{-1.0f};
bool maintenance{false}; // a technical stop
};
struct timetable_data {
double time{0.0};
std::uint32_t vehicle{no_vehicle};
std::string train;
std::string category;
std::string from;
std::string to;
float vmax{-1.0f}; // [km/h] of the train
std::vector<timetable_row> rows;
};
// a train at a station of its timetable, at its stop point (W4)
struct station_event {
double time{0.0};
std::uint32_t vehicle{no_vehicle};
std::uint8_t kind{station_kinds::stopped};
std::uint16_t row{0}; // of the timetable, from 1
std::string station;
glm::dvec3 position{0.0}; // of the stop point
float distance{0.0f}; // [m] of the head of the train before the stop point, below 0 past it
float stop_at{0.0f}; // [m] before the stop point, where its parameters put the head
float train_length{0.0f}; // [m]
float platform{0.0f}; // the second parameter of the stop point: above 0 the length of the platform, below 0 for shorter trains only (W32)
float arrival{-1.0f}; // the timetable's, in minutes of the day, -1 none
float departure{-1.0f};
};
// one vehicle in a look at the vehicles
// the camera of the user
struct camera_state {
double time{0.0};
std::uint8_t mode{camera_modes::free};
std::uint8_t view{0}; // of an outside view: 0 the front of the train, 1 its rear, 2 a bogie, 3 drive-by, 4 orbit, 5 shunting, 6 coupling, 7 trackside
std::uint32_t vehicle{no_vehicle}; // the vehicle the camera goes with, no_vehicle for none
glm::dvec3 position{0.0};
float heading{0.0f}; // [rad] of where it looks, from +z towards +x, as the heading of a vehicle
float pitch{0.0f}; // [rad] up from the horizon
};
// a coupler of a vehicle coupled, uncoupled, or coupled otherwise (the hoses, the cables)
struct coupling_change {
double time{0.0};
std::uint32_t vehicle{no_vehicle};
std::uint8_t side{0}; // the coupler of the vehicle: 0 the front one, 1 the rear one
std::uint32_t other{no_vehicle}; // the vehicle coupled to it now, no_vehicle for none
std::uint8_t other_side{0}; // the coupler of that one
std::uint8_t flags{0}; // what is coupled now, bits of the simulator's coupling: 1 the coupler, 2 the brake pipe, 4 the control, 8 the power...
std::uint8_t previous_flags{0}; // and before
std::uint32_t previous_other{no_vehicle}; // the vehicle coupled before
glm::dvec3 position{0.0}; // of the coupler
};
// what put a vehicle off the rails, for the derailment record (vehicle/derailmotion.h)
namespace derailment_causes {
inline constexpr std::uint8_t flange_climbed{1};
inline constexpr std::uint8_t overturned{2};
inline constexpr std::uint8_t end_of_track{3};
inline constexpr std::uint8_t pulled_off{4}; // by the coupling of a vehicle off the track
inline constexpr std::uint8_t collision{5};
} // namespace derailment_causes
namespace derailment_phases {
inline constexpr std::uint8_t left_track{0}; // it left the track
inline constexpr std::uint8_t at_rest{1}; // it came to rest off the track
} // namespace derailment_phases
// a vehicle off the track (physics.derailment new): as it leaves it, and where it comes to rest
struct derailment_state {
double time{0.0};
std::uint32_t vehicle{no_vehicle};
std::uint8_t phase{derailment_phases::left_track};
std::uint8_t cause{derailment_causes::flange_climbed};
glm::dvec3 position{0.0}; // of the vehicle
float heading{0.0f}; // [rad] of its front, from +z towards +x
float speed_kmh{0.0f}; // signed: positive along its front
float offset{0.0f}; // [m] from the axis of the track at the place nearest to it, to the left of its front positive
float roll{0.0f}; // [rad] of its body against the level, to the left positive; 90 degrees on its side
float travelled{0.0f}; // [m] on the ground since it left the rails
bool lying{false}; // on its side
};
struct vehicle_state {
std::uint32_t id{no_vehicle};
glm::vec3 position{0.0f};
float heading{0.0f}; // [rad] of the front of the vehicle, from +z towards +x
float speed_kmh{0.0f}; // signed: positive along the front of the vehicle
driver driving{driver::none};
};
struct command_entry {
double time{0.0};
std::uint32_t vehicle{no_vehicle}; // no_vehicle for a command of the simulation
bool pressed{true}; // false: released
double param1{0.0};
double param2{0.0};
std::string name;
};
struct signal_passing {
double time{0.0};
std::uint32_t vehicle{no_vehicle};
std::uint32_t signal{no_signal};
std::string event; // the event which tells the aspect
glm::dvec3 position{0.0}; // of the signal
float speed1{0.0f}; // the aspect when passed: speed at the signal [km/h], 0 stop, -1 no limit
float speed2{0.0f}; // speed at the next signal
float speed_kmh{0.0f}; // of the train
float overrun{0.0f}; // [m] the train had gone past the signal when it was noticed
std::uint8_t flags{0}; // passing_flags
};
// sizes of the parts of fixed size, as the readers expect them
inline constexpr std::size_t header_size{12};
inline constexpr std::size_t record_header_size{5};
inline constexpr std::size_t cab_sample_size{194};
inline constexpr std::size_t vehicle_state_size{25};
// writes the records to a stream. nothing is written until header() is
class writer {
public:
explicit writer(std::ostream &Output);
void header();
void info(info_data const &Info);
void track(track_data const &Track);
void signal(std::uint32_t Index, std::string_view Name, glm::dvec3 const &Position);
// a stop point (W4): the station it is for and its two parameters
void stop_point(std::uint32_t Index, std::string_view Station, glm::dvec3 const &Position, float Stop_at, float Platform, float Heading);
// a place where the trains are told to sound the horn (W6)
void horn_point(std::uint32_t Index, std::string_view Name, glm::dvec3 const &Position, float Duration, float Tone);
void timetable(timetable_data const &Timetable);
void station(station_event const &Station);
void horn_request(double Time, std::uint32_t Vehicle, float Duration, float Tone, glm::dvec3 const &Position);
void camera(camera_state const &Camera);
void coupling(coupling_change const &Change);
void derailment(derailment_state const &State);
void vehicle(vehicle_data const &Vehicle);
void vehicle_removed(double Time, std::uint32_t Id);
void cab(cab_sample const &Sample);
// Keyframe: the list holds all the vehicles, not only those which changed
void vehicles(double Time, bool Keyframe, std::vector<vehicle_state> const &States);
void switch_state(double Time, std::uint32_t Track, std::uint8_t State);
void signal_aspect(double Time, std::uint32_t Signal, float Speed1, float Speed2, std::string_view Command);
void command(command_entry const &Command);
void driver_change(double Time, std::uint32_t Vehicle, driver Driving);
void signal_passed(signal_passing const &Passing);
void user_vehicle(double Time, std::uint32_t Vehicle, bool In_cab, std::int8_t Cab);
void clock(double Time, std::uint16_t Year, std::uint16_t Year_day, double Second_of_day);
void end(double Time);
// bytes written so far
std::uint64_t size() const {
return m_size; }
private:
// the payload of a record, assembled before its length is known
class payload {
public:
void u8(std::uint8_t Value);
void i8(std::int8_t Value);
void u16(std::uint16_t Value);
void u32(std::uint32_t Value);
void f32(float Value);
void f64(double Value);
void vec3(glm::vec3 const &Value);
void dvec3(glm::dvec3 const &Value);
// UTF-8 text after its length in 7 bit groups, low first: .NET BinaryWriter.Write(string)
void text(std::string_view Value);
std::string const &bytes() const {
return m_bytes; }
void clear() {
m_bytes.clear(); }
private:
template <typename Type_>
void raw(Type_ Value);
std::string m_bytes;
};
payload &begin();
void emit(record Type);
std::ostream &m_output;
payload m_payload;
std::uint64_t m_size{0};
};
} // namespace blackbox