diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f2184311..708f421cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,6 +153,7 @@ set(SOURCES "scene/scene.cpp" "scene/scenenode.cpp" "simulation/simulation.cpp" +"simulation/blackboxrecorder.cpp" "model/vertex.cpp" "audio/audio.cpp" "audio/audiorenderer.cpp" @@ -480,6 +481,7 @@ set(CORE_SOURCES "scripting/scriptscreenkernels.cpp" "scripting/scriptscreendrawing.cpp" "simulation/simulationtestrules.cpp" +"vehicle/blackbox.cpp" "vehicle/enginestart.cpp" "vehicle/handover.cpp" "vehicle/horn.cpp" diff --git a/application/application.cpp b/application/application.cpp index 5cf291cc1..c5ddf1030 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/utilities.h" #include "simulation/simulation.h" #include "simulation/simulationsounds.h" +#include "simulation/blackboxrecorder.h" #include "vehicle/Train.h" #include "utilities/dictionary.h" #include "scene/sceneeditor.h" @@ -726,6 +727,8 @@ void eu07_application::exit() auto const step = [closing](char const *What) { WriteLog(std::format("Closing: {} at {} ms", What, std::chrono::duration_cast(std::chrono::steady_clock::now() - closing).count())); }; + step("the black box"); + simulation::Blackbox.close(); step("the modes"); for (auto &mode : m_modes) mode.reset(); diff --git a/application/drivermode.cpp b/application/drivermode.cpp index deb906a4a..cde7e80d7 100644 --- a/application/drivermode.cpp +++ b/application/drivermode.cpp @@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/. #include "simulation/simulationtime.h" #include "simulation/simulationenvironment.h" #include "simulation/simulationtest.h" +#include "simulation/blackboxrecorder.h" #include "scene/scene.h" #include "rendering/lightarray.h" #include "rendering/particles.h" @@ -160,6 +161,30 @@ bool driver_mode::init() } // mode-specific update of simulation data. returns: false on error, true otherwise +namespace { + +// the camera as the black box tells it: where it is and where it looks, as the heading of a vehicle +blackbox::camera_state blackbox_camera(TCamera const &Camera, std::uint8_t const Mode, int const View) { + blackbox::camera_state state; + state.mode = Mode; + state.view = static_cast(View); + state.position = Camera.Pos; + state.pitch = Camera.Angle.x; + if (Camera.m_owner != nullptr && false == DebugCameraFlag) { + // it looks at a point, then turns by its yaw from there + auto const where{Camera.LookAt - Camera.Pos}; + state.heading = static_cast(std::atan2(where.x, where.z)) + Camera.Angle.y; + } + else { + // the yaw alone: 0 looks towards -z + state.heading = static_cast(std::atan2(-std::sin(Camera.Angle.y), -std::cos(Camera.Angle.y))); + } + state.heading = std::remainder(state.heading, 2.0f * glm::pi()); + return state; +} + +} // namespace + bool driver_mode::update() { @@ -306,6 +331,17 @@ bool driver_mode::update() simulation::Events.update(); simulation::Region->update_events(); simulation::Lights.update(); + { + // where the user looks from, for the black box + auto const &camera{DebugCameraFlag ? DebugCamera : Camera}; + auto const mode{DebugCameraFlag ? blackbox::camera_modes::debug : + false == FreeFlyModeFlag ? blackbox::camera_modes::cab : + m_externalview ? blackbox::camera_modes::external : + blackbox::camera_modes::free}; + simulation::Blackbox.camera(blackbox_camera(camera, mode, mode == blackbox::camera_modes::external ? m_externalviewmode : 0), + DebugCameraFlag ? nullptr : camera.m_owner); + } + simulation::Blackbox.update(deltatime); if (Global.test.enabled && simulation::is_ready) { diff --git a/application/driveruipanels.cpp b/application/driveruipanels.cpp index d83911e7b..ba8e7556d 100644 --- a/application/driveruipanels.cpp +++ b/application/driveruipanels.cpp @@ -92,53 +92,7 @@ drivingaid_panel::update() { } // next speed limit auto const speedlimit { static_cast( owner->VelDesired ) }; - auto nextspeedlimit { speedlimit }; - auto nextspeedlimitdistance { std::numeric_limits::max() }; - if( speedlimit != 0 ) { // if we aren't allowed to move then any next speed limit is irrelevant - // nie przekraczać rozkladowej - auto const schedulespeedlimit { ( - (owner->OrderCurrentGet() & (Obey_train | Bank)) != 0 && owner->TrainParams.TTVmax > 0.0 ? static_cast( owner->TrainParams.TTVmax ) : - (owner->OrderCurrentGet() & (Obey_train | Bank)) == 0 ? static_cast( owner->fShuntVelocity ) : - -1 ) }; - // first take note of any speed change which should occur after passing potential current speed limit - if( owner->VelLimitLastDist.second > 0 ) { - nextspeedlimit = min_speed( schedulespeedlimit, static_cast( owner->VelLimitLastDist.first ) ); - nextspeedlimitdistance = owner->VelLimitLastDist.second; - } - // then take into account speed change ahead, compare it with speed after potentially clearing last limit - // lower of these two takes priority; otherwise limit lasts at least until potential last limit is cleared - auto const noactivespeedlimit { owner->VelLimitLastDist.second < 0 }; - if( auto const speedatproximitydistance { min_speed( schedulespeedlimit, static_cast( owner->VelNext ) ) }; speedatproximitydistance == nextspeedlimit ) { - if( noactivespeedlimit ) { - nextspeedlimit = speedatproximitydistance; - nextspeedlimitdistance = owner->ActualProximityDist; - } - } - else if( speedatproximitydistance < nextspeedlimit ) { - // if the speed limit ahead is more strict than our current limit, it's important enough to report - if( speedatproximitydistance < owner->VelDesired ) { - nextspeedlimit = speedatproximitydistance; - nextspeedlimitdistance = owner->ActualProximityDist; - } - // otherwise report it only if it's located after our current (lower) limit ends - else if( owner->ActualProximityDist > nextspeedlimitdistance ) { - nextspeedlimit = speedatproximitydistance; - nextspeedlimitdistance = owner->ActualProximityDist; - } - } - else if( noactivespeedlimit ) { // implicit proximity > last, report only if last limit isn't present - nextspeedlimit = speedatproximitydistance; - nextspeedlimitdistance = owner->ActualProximityDist; - } - // HACK: if our current speed limit extends beyond our scan range don't display potentially misleading information about its length - if( nextspeedlimitdistance >= EU07_AI_SPEEDLIMITEXTENDSBEYONDSCANRANGE ) { - nextspeedlimit = speedlimit; - } - // HACK: hide next speed limit if the 'limit' is a vehicle in front of us - else if( owner->ActualProximityDist == std::abs( owner->TrackObstacle() ) ) { - nextspeedlimit = speedlimit; - } - } + auto const [ nextspeedlimit, nextspeedlimitdistance ] { owner->next_speed_limit() }; std::string nextspeedlimittext; if( nextspeedlimit != speedlimit ) { std::snprintf( diff --git a/application/editormode.cpp b/application/editormode.cpp index 1ad3ef6ed..9b9a70692 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -36,6 +36,7 @@ extern char **environ; #include "simulation/simulation.h" #include "simulation/simulationtime.h" #include "simulation/simulationenvironment.h" +#include "simulation/blackboxrecorder.h" #include "utilities/Timer.h" #include "Console.h" #include "rendering/renderer.h" @@ -5060,6 +5061,8 @@ void editor_mode::update_camera(double const Deltatime) void editor_mode::enter() { m_statebackup = {Global.pCamera, FreeFlyModeFlag, Global.ControlPicking}; + // the editor may remove the tracks and the vehicles the recording refers to: it ends here + simulation::Blackbox.close(); if (simulation::Region != nullptr) { diff --git a/blackbox_doc.md b/blackbox_doc.md new file mode 100644 index 000000000..c58cae100 --- /dev/null +++ b/blackbox_doc.md @@ -0,0 +1,580 @@ +# The black box + +A binary recording of a whole session: the map of the scenery (the rail tracks, the signals, the stop +points W4 and the horn points W6), every vehicle over time, the switches and the aspects of the signals as +they change, and the train the user is in, in detail. The detail includes what the AI driver would have +done at the same moment, every command the user gave, when the autopilot took over, the signals passed, +the timetable and how the train kept to it at each station, and where it was told to sound the horn. From it another program (the +starter, a viewer) can replay the session on the map, or set the user's driving against the AI's. + +It is written along with the physics logs: `physicslog yes` in `eu07.ini`, not in an editor session. +`blackbox no` turns it off while the physics logs are kept (it is on by default). The file goes next to +them, `physicslog/_.blackbox` (the local time the recording started at). +The `.dat` logs stay as they are. + +The recording starts once the scenery is loaded and ends when the simulator closes. Going into the +editor (F11) ends it too, since the editor may take away the tracks and the vehicles it refers to; no +new one starts when driving goes on after that. + +It is written as the session goes and flushed once a second, so the file can be read while the simulator +still writes it, and after a crash all but the last second is there. A file without the `end` record +was cut short. + +## Layout + +Little-endian throughout. The types: + +| type | bytes | what | +|---------|-------|---------------------------------------------------------------------------| +| `u8` | 1 | unsigned | +| `i8` | 1 | signed | +| `u16` | 2 | unsigned | +| `u32` | 4 | unsigned | +| `f32` | 4 | IEEE 754 single | +| `f64` | 8 | IEEE 754 double | +| `vec3` | 12 | `f32 x, f32 y, f32 z` | +| `dvec3` | 24 | `f64 x, f64 y, f64 z` | +| `text` | 1+ | UTF-8, after its length in bytes in 7 bit groups, low first, the high bit set on all but the last | + +This is what .NET `BinaryReader` reads: `ReadByte`, `ReadSByte`, `ReadUInt16`, `ReadUInt32`, +`ReadSingle`, `ReadDouble` and `ReadString`. + +Positions are in the frame of the scenery: metres, `y` up, the same as in the `.scn` files. + +The file starts with a header of 12 bytes: + +| offset | type | value | +|--------|----------|-------------------------------| +| 0 | 8 bytes | `EU07BBX` and a zero byte | +| 8 | `u16` | version of the format, now 1 | +| 10 | `u16` | reserved, 0 | + +Then come the records, to the end of the file. Each record is: + +| type | what | +|-------|---------------------------------------| +| `u8` | the kind of the record (below) | +| `u32` | length of the content, in bytes | +| | the content | + +A reader skips a record of a kind it doesn't know, and the content past the fields it knows (later +versions may add fields at the end of a record). A record whose length runs past the end of the file is +still being written: stop there and read on from it later. + +`t`, the time of a record, is an `f64` of seconds of simulation since the recording started. It doesn't +run while the simulation is paused. The `clock` records give the time of day it corresponds to. + +Vehicles are given numbers from 1 up, in the `vehicle` records; 0 means no vehicle. Tracks, signals, +stop points and horn points are given numbers from 0 up, in the order of their `track`, `signal`, +`stop point` and `horn point` records. + +## Records + +### 0x01 info + +Once, first. + +| type | field | +|--------|--------------------------------------------------------------------| +| `text` | scenario file, as the simulator was given it (the starter gives `$.scn`) | +| `text` | local time the recording started at, `YYYY-MM-DDTHH:MM:SS` | +| `u16` | simulation year at the start | +| `u16` | simulation day of the year, 1..366 | +| `f64` | simulation second of the day at the start | +| `text` | the vehicle the user was to start in, `ghostview` for none | +| `text` | version of the simulator | +| `f32` | seconds between the `cab` records (0.1) | +| `f32` | seconds between the `vehicles` records (1) | +| `f32` | seconds between the `vehicles` records which list all the vehicles (60) | + +### 0x10 track + +The map: one for each rail track, two for a switch, one for each of its ways. Written at the start. + +| type | field | +|--------|-----------------------------------------------------------| +| `u32` | number of the track | +| `text` | name, may be empty | +| `u8` | kind: 0 a plain track, 1 a way of a switch | +| `u8` | the switch position this way is taken in (0 for a plain track) | +| `u8` | category: 1 rail | +| `u32` | count of the points | +| `vec3` | the points, along the track, as many as counted | + +### 0x11 signal + +The map: one for each signal. Written at the start. + +| type | field | +|---------|----------------------------------------------------| +| `u32` | number of the signal | +| `text` | name (that of its memory cell, without `_sem_mem`) | +| `dvec3` | position | + +### 0x14 stop point + +The map: one for each stop point of a passenger train (W4), a `putvalues` event with the text +`PassengerStopPoint:` (or a memory cell with that text). Written at the start. + +| type | field | +|---------|-------------------------------------------------------------------------------| +| `u32` | number of the stop point | +| `text` | name of the station as the scenery gives it; what follows a `#` tells the stop points of one station apart | +| `dvec3` | position | +| `f32` | where the train is to stop, the first value of the cell: below 0 its head that many metres before the W4, above 0 the middle of the train that many metres before it | +| `f32` | the second value of the cell: above 0 the length of the platform [m], below 0 the stop is for trains shorter than that (W32), 0 not given | +| `f32` | heading of the trains it is for [rad, from +z towards +x], as the track it is read off gives it: the driver reads the events of the second end of a track going from its first end to the second; NaN on no track, or on tracks of both ways | + +### 0x15 horn point + +The map: one for each event telling the trains to sound the horn (W6), a `putvalues` event with the text +`Warning_signal`. Written at the start. + +| type | field | +|---------|-------------------------------------------| +| `u32` | number of the horn point | +| `text` | name of the event | +| `dvec3` | position | +| `f32` | how long the horn is to sound [s] | +| `f32` | the tone, bits: 1 low, 2 high | + +### 0x12 vehicle + +Every vehicle present at the start, and each one which shows up later, before it appears in any other +record. + +| type | field | +|--------|----------------------------------------------------------| +| `u32` | number of the vehicle | +| `text` | name | +| `text` | type | +| `text` | train number of its timetable, if it leads a train with one | +| `f32` | length [m] | +| `f32` | width [m] | +| `u8` | category: 1 rail, 2 road | + +### 0x13 vehicle removed + +| type | field | +|-------|-----------------------| +| `f64` | t | +| `u32` | number of the vehicle | + +### 0x20 cab + +The vehicle the user is in, every 0.1 s, also while the user looks around outside it. 190 bytes. + +| type | field | +|---------|------------------------------------------------------------------------| +| `f64` | t | +| `u32` | number of the vehicle | +| `dvec3` | position | +| `f32` | speed [km/h], positive along the front of the vehicle | +| `f32` | acceleration along the track [m/s²] | +| `f32` | odometer [km] | +| `u8` | driver: 0 none, 1 the user, 2 the AI (the autopilot) | +| `u8` | flags, below | +| `i8` | master controller position | +| `i8` | secondary controller (field shunting) position | +| `i8` | reverser: -1, 0, 1 | +| `f32` | driver's brake valve position | +| `f32` | local brake position, 0..1 | +| `f32` | brake cylinder pressure [bar] | +| `f32` | brake pipe pressure [bar] | +| `f32` | motor current [A] | +| `f32` | AI: the speed it would hold here [km/h] | +| `f32` | AI: the next speed limit it sees [km/h], -1 none | +| `f32` | AI: distance to that limit [m], -1 none | +| `f32` | AI: the acceleration it would go for [m/s²], below 0 it would brake | +| `f32` | AI: braking distance at this speed [m] | +| `f32` | top speed of the timetable [km/h], -1 none | +| `f32` | top speed of the track [km/h], -1 none | +| `u32` | what is switched on, below | +| `f32` | main reservoir pressure [bar] | +| `f32` | voltage given to the motors [V] | +| `f32` | revolutions of the engine or the motors [rpm] | +| `u16` | lights lit at the head of the train, below | +| `u16` | lights lit at its tail | +| `i8` | the cab the user is in: 1 the front one, -1 the rear one, 0 none | +| `u8` | the train protection, below | +| `f32` | acceleration across the track [m/s²], in the curves | +| `u8` | what the driver of the train goes by: 1 a train run, 2 shunting, 0 anything else or no driver | +| `i16` | the light pattern the timetable or the scenery asks for at the head, bits as the lights; -1 the usual one | +| `i16` | the same at the tail | +| `u16` | the lamps the vehicle at the head has at its outer end, bits as the lights | +| `u16` | the same of the vehicle at the tail | +| `u8` | what drives the vehicle whose motors the cab drives, below | +| `u8` | the setting of the brakes: 1 G, 2 P, 4 R, 8 Mg, as bits | +| `u8` | the radio channel | +| `f32` | what the driver's brake valve shows on its gauge [bar]: the equalizing (control) reservoir of most valves, the time reservoir of the FV4a, empty but after an overcharge | +| `f32` | main reservoir pipe along the train [bar] | +| `f32` | oil pressure of the diesel engine [MPa] | +| `f32` | cooling water temperature of the diesel engine [°C] | +| `f32` | its oil temperature [°C] | +| `f32` | its temperature [°C] | +| `f32` | the speed the cruise control is set to [km/h], 0 none | +| `f32` | power of the engine [kW] | +| `i8` | hand brake position, 0 released | +| `u8` | the mode of the brakes: 1 PS, 2 PN, 4 EP, 8 MED, as bits | +| `u8` | the horn's tones sounding: 1 low, 2 high, 4 a third, 0 silent | +| `f32` | the integrated driving and braking handle of a unit, -1 full braking .. 1 full power | +| `f32` | the clutch of a diesel's gearbox, 0 open .. 1 closed | +| `f32` | the filling of a diesel's hydraulic torque converter, 0..1 | +| `f32` | the highest brake cylinder pressure of the vehicles of the train [bar] | + +The lights the train is to show follow from the movement and the two patterns after it: in a train run the head the pattern asked for, +the usual one Pc 1 (both headlights and the upper one; Pc 2 is the left red marker with the right and the +upper headlight), and the tail the pattern asked for, the usual one Pc 5 (both red markers); shunting +Tb 1, one headlight at each end. A pattern is checked against the lamps the vehicle has, as the driver +checks it: one without the upper headlight shows Pc 1 with the two others, one with auxiliary lamps may +show them instead of the headlights, and one with no lamps at all the rear-end signals; the tail shows +Pc 5 with the red markers or the rear-end signals it has. A recording made before the movement ends at +the acceleration across, one made before the lamps at the light pattern of the tail, and one made before +the engine at the lamps of the tail. The temperatures and the oil pressure mean something for a diesel +engine only. + +What drives the vehicle: 0 nothing of its own (a draisine, a car), 1 electric series motors, 2 electric +induction motors, 3 a diesel engine with a mechanical or a hydraulic transmission, 4 a diesel-electric one, +5 steam. + +The flags: + +| bit | meaning | +|------|-----------------------------------------------------------------| +| 0x01 | the user is in the cab | +| 0x02 | the AI fields hold what the AI would do: the consist is started | +| 0x04 | the alerter or the cab signalling blinks | +| 0x08 | the alerter or the cab signalling beeps | +| 0x10 | the wheels slip | +| 0x20 | radio-stop | +| 0x40 | the alarm chain was pulled | + +What is switched on: the radio, the horn, the doors and the spring brake are those of the +vehicle the user is in; the battery is on if it is in either; the rest, and the pressures, the voltage and the revolutions, of the vehicle +whose motors the cab drives (the motor car of a unit). + +| bit | meaning | +|--------|--------------------------------------| +| 0x0001 | battery | +| 0x0002 | main breaker closed | +| 0x0004 | front pantograph raised | +| 0x0008 | rear pantograph raised | +| 0x0010 | converter | +| 0x0020 | compressor | +| 0x0040 | line contactors: the motors are on | +| 0x0080 | overload relay tripped | +| 0x0100 | sanding | +| 0x0200 | horn | +| 0x0400 | left doors open | +| 0x0800 | right doors open | +| 0x1000 | radio | +| 0x2000 | train heating | +| 0x4000 | spring brake applied | +| 0x8000 | shunting mode | +| 0x0001'0000 | departure signal sounding | +| 0x0002'0000 | left doors permitted to open | +| 0x0004'0000 | right doors permitted to open | +| 0x0008'0000 | the steps come out with the doors | +| 0x0010'0000 | the door lock is switched on: the doors lock above a speed | +| 0x0020'0000 | the doors are locked | +| 0x0040'0000 | the brake releaser works | +| 0x0080'0000 | fuel pump | +| 0x0100'0000 | oil pump | +| 0x0200'0000 | the fans of the traction motors | +| 0x0400'0000 | the diesel engine is too hot | +| 0x0800'0000 | the cruise control holds a speed | + +The doors, their permits and steps, the departure signal and the releaser are of the vehicle the user is +in; the pumps, the fans, the engine's heat and the cruise control of the one whose motors the cab drives. + +The lights: 0x001 left headlight, 0x002 left red marker, 0x004 upper headlight, 0x010 right +headlight, 0x020 right red marker, 0x040 rear-end signals, 0x080 left and 0x100 right auxiliary +light, 0x200 left and 0x400 right high beam. Those of the head are at the outer end of the first vehicle +of the train, those of the tail at the outer end of the last one, whichever vehicle the user is in. + +The train protection, of the vehicle whose motors the cab drives; the alerter (CA) and the cab signalling +(SHP) apart: + +| bit | meaning | +|------|-------------------------------------------------------------------------| +| 0x01 | the alerter lamp blinks: the driver is to acknowledge | +| 0x02 | the cab signalling lamp blinks: an SHP magnet was passed | +| 0x04 | the alerter beeps | +| 0x08 | the cab signalling beeps | +| 0x10 | not acknowledged in time: the train protection brakes the train | + +The AI fields are what the AI driver works out from the place and the speed the train has at the time. +It works them out as it goes, also while the user drives, but doesn't act on them then. It is not a +separate run of the AI. They are refreshed every half a second or so, and only once the consist is +started (flag 0x02); before that the speed held, the acceleration and the braking distance are 0, the +next limit, its distance and the top speeds of the timetable and the track -1. + +### 0x21 vehicles + +Every second: the vehicles which moved more than 0.1 m, changed speed or got another driver since they +were last listed. Every 60 s all of them (a keyframe), so a reader can start at any keyframe. + +| type | field | +|-------|-------------------------------------------| +| `f64` | t | +| `u8` | 1 if a keyframe, else 0 | +| `u32` | count of the vehicles | +| | as many times, 25 bytes: | +| `u32` | number of the vehicle | +| `vec3`| position | +| `f32` | heading [rad] of its front, from +z towards +x: the front points to (sin, cos) in (x, z) | +| `f32` | speed [km/h], positive along its front | +| `u8` | driver: 0 none, 1 a user, 2 the AI | + +### 0x22 switch + +The position of a switch: all of them at the start, then as they change (checked every second). + +| type | field | +|-------|---------------------------------------------------------------| +| `f64` | t | +| `u32` | number of the track | +| `u8` | position: the way taken is the `track` record with this branch | + +### 0x23 signal aspect + +What a signal tells: all of them at the start, then as they change (checked every second). The values +are those of its memory cell. + +| type | field | +|--------|----------------------------------------------------------------| +| `f64` | t | +| `u32` | number of the signal | +| `f32` | speed at the signal [km/h]: 0 stop, -1 no limit | +| `f32` | speed at the next signal [km/h] | +| `text` | command of the memory cell, e.g. `SetVelocity`, `ShuntVelocity` | + +### 0x24 timetable + +The timetable of the train the user is in: once the user is in a train which has one, and again whenever +it changes, the user in another train or the train given its next timetable (one recording may hold any +number of them). It is written before the first station record (`0x35`) under it, even when the train +comes to that station at the moment it gets the timetable. The times are minutes of the day (10:05 is 605), +-1 none. + +| type | field | +|--------|---------------------------------------------------------| +| `f64` | t | +| `u32` | number of the vehicle the user was in | +| `text` | train number | +| `text` | category | +| `text` | from | +| `text` | to | +| `f32` | top speed of the train [km/h] | +| `u32` | count of the rows, the stations | +| | as many times: | +| `text` | station | +| `f32` | kilometre | +| `f32` | top speed before the station [km/h], -1 none | +| `f32` | arrival | +| `f32` | departure | +| `u8` | 1 a technical stop, else 0 | + +The rows are numbered from 1, as the `station` records give them. + +### 0x25 camera + +Where the user looks from: at the start, then when it changed, looked at twice a second. From the cab and +in the outside views the camera goes with the train, whose way is written anyway (0x20), so only going to +another mode, view or vehicle counts; the free and the debug camera count as changed as well when they +moved more than 0.5 m or turned more than 0.05 rad. + +| type | field | +|---------|------------------------------------------------------------------------------| +| `f64` | t | +| `u8` | mode: 0 from the cab, 1 an outside view of the user's train, 2 the free camera, 3 the debug camera | +| `u8` | the outside view: 0 the front of the train, 1 its rear, 2 a bogie, 3 drive-by; 0 in the other modes | +| `u32` | number of the vehicle the camera goes with, 0 for none | +| `dvec3` | position | +| `f32` | heading [rad] of where it looks, as that of a vehicle: towards (sin, cos) in (x, z) | +| `f32` | pitch [rad], up from the horizon | + +### 0x30 command + +Every command the user gave: each press and release of a control of the cab, of the keyboard, mouse or a +device. Held controls repeat their command while held; only the press and the release are written, the +`cab` records show the effect. Also the commands to the whole simulation, like the time jumps. + +| type | field | +|--------|-------------------------------------------------------------| +| `f64` | t | +| `u32` | number of the vehicle, 0 for a command to the simulation | +| `u8` | 1 pressed, 0 released | +| `f64` | first parameter | +| `f64` | second parameter | +| `text` | name of the command, as in `eu07_input-keyboard.ini` | + +### 0x31 driver change + +The user or the AI takes over a vehicle: the autopilot switched on (Shift+Q) or off (Q), and entering a +vehicle the AI was driving. + +| type | field | +|-------|--------------------------------| +| `f64` | t | +| `u32` | number of the vehicle | +| `u8` | driver now: 1 the user, 2 the AI | + +### 0x32 signal passed + +A train, any, passed a signal. It is noticed when the AI driver updates its table, up to half a second +later, so the train is somewhat past the signal by then: the position is that of the signal and the +overrun tells how far past it the train had got. + +| type | field | +|---------|---------------------------------------------------------------| +| `f64` | t | +| `u32` | number of the vehicle leading the train | +| `u32` | number of the signal, `0xFFFFFFFF` if it isn't on the map | +| `text` | name of the event telling the aspect | +| `dvec3` | position of the signal | +| `f32` | the aspect when passed: speed at the signal [km/h], 0 stop, -1 no limit | +| `f32` | speed at the next signal [km/h], as the signal shows it when the pass is noticed | +| `f32` | speed of the train [km/h] | +| `f32` | overrun [m] | +| `u8` | flags: 0x01 a shunting signal, 0x02 passed at stop, 0x04 the stop of an automatic block signal; a shunting signal binds only shunting, so passed at stop by a train it has 0x01 alone | + +### 0x33 user vehicle + +The vehicle the user is in changed, the user got out of the cab or back into it, or went to the other cab. + +| type | field | +|-------|---------------------------------------------| +| `f64` | t | +| `u32` | number of the vehicle, 0 for none | +| `u8` | 1 in the cab, 0 outside it | +| `i8` | the cab: 1 the front one, -1 the rear one, 0 none | + +### 0x34 clock + +Every second, and so after a time jump too. + +| type | field | +|-------|--------------------------------| +| `f64` | t | +| `u16` | year | +| `u16` | day of the year, 1..366 | +| `f64` | second of the day | + +### 0x35 station + +A train, any, at a station of its timetable, as its driver (the AI, or the AI watching the user drive) +sees it at the stop point (W4). + +| type | field | +|---------|--------------------------------------------------------------------------| +| `f64` | t | +| `u32` | number of the vehicle leading the train | +| `u8` | what happened, below | +| `u16` | row of the timetable, from 1 | +| `text` | station | +| `dvec3` | position of the stop point | +| `f32` | distance of the head of the train before the stop point [m], below 0 past it | +| `f32` | where the head was to stop: that many metres before the stop point | +| `f32` | length of the train [m] | +| `f32` | the second value of the stop point (see `stop point`): the platform | +| `f32` | arrival of the timetable, in minutes of the day, -1 none | +| `f32` | departure of the timetable | + +What happened: + +| value | meaning | +|-------|-------------------------------------------------------------------------------------------| +| 1 | the train stopped at the stop point of a stop of its timetable | +| 2 | it passed a station where the timetable has no stop | +| 3 | it left the stop point of a stop behind before it was time to go: without stopping, or leaving before the departure; only for a user's train | +| 4 | it was still at the stop point when it was time to go: it may leave | + +How far the distance is from the place the head was to stop at tells how well the train was stopped. + +### 0x36 horn request + +A train, any, was told to sound the horn (it passed a horn point W6). Whether the user did is in the +`cab` records (the horn among what is switched on) and the `command` records. + +| type | field | +|---------|-------------------------------------------------| +| `f64` | t | +| `u32` | number of the vehicle leading the train | +| `f32` | how long the horn is to sound [s] | +| `f32` | the tone, bits: 1 low, 2 high | +| `dvec3` | position of the horn point, or of the train's head if not known | + +### 0x37 coupling + +A coupler of a vehicle, any: at the start those coupled, then each change, looked at every second. A +change is two vehicles coupled, uncoupled, or the hoses and the cables between them connected or not. Both +vehicles of a coupling see it; the one with the lower number tells it. + +| type | field | +|---------|------------------------------------------------------------------------| +| `f64` | t | +| `u32` | number of the vehicle | +| `u8` | its coupler: 0 the front one, 1 the rear one | +| `u32` | number of the vehicle coupled to it now, 0 for none | +| `u8` | the coupler of that vehicle | +| `u8` | what is coupled now, below; 0 nothing | +| `u8` | what was coupled before | +| `u32` | number of the vehicle coupled before, 0 for none | +| `dvec3` | position of the coupler | + +What is coupled: 0x01 the coupler, 0x02 the brake pipe, 0x04 the control (multiple unit), 0x08 the high +voltage, 0x10 the gangway, 0x20 the main reservoir pipe, 0x40 the train heating, 0x80 a coupling not to be +undone in shunting (between the cars of a unit). + +### 0x3F end + +Last, when the simulator closes. + +| type | field | +|-------|-------| +| `f64` | t | + +## What it doesn't hold + +- The kilometrage of the lines: it is known only in an editor session. +- Roads and rivers on the map, only the rail tracks. +- A run of the AI of its own: the AI fields tell what the AI would do from where the user's train is. + +## Reading it + +A minimal reader in C#: + +```csharp +using var reader = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); +if (new string(reader.ReadChars(7)) != "EU07BBX" || reader.ReadByte() != 0) + throw new InvalidDataException("not a black box"); +ushort version = reader.ReadUInt16(); +reader.ReadUInt16(); + +var stream = reader.BaseStream; +while (stream.Length - stream.Position >= 5) +{ + long start = stream.Position; + byte kind = reader.ReadByte(); + uint length = reader.ReadUInt32(); + if (stream.Length - stream.Position < length) + { + stream.Position = start; // still being written, read on from here later + break; + } + long next = stream.Position + length; + switch (kind) + { + case 0x20: + double t = reader.ReadDouble(); + uint vehicle = reader.ReadUInt32(); + double x = reader.ReadDouble(), y = reader.ReadDouble(), z = reader.ReadDouble(); + float speed = reader.ReadSingle(); + // ... + break; + // ... + } + stream.Position = next; // past what wasn't read, and past fields added later +} +``` diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp new file mode 100644 index 000000000..d886bddf0 --- /dev/null +++ b/simulation/blackboxrecorder.cpp @@ -0,0 +1,754 @@ +/* +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/utilities.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +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 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(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(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(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 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 points_of(TSegment const &Segment) { + auto const steps{std::max(1, static_cast(Segment.RaSegCount() * 0.5))}; + std::vector points; + points.reserve(steps + 1); + for (int i = 0; i <= steps; ++i) + points.emplace_back(Segment.FastGetPoint(static_cast(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::max()}; + for (int i = 0; i <= steps; ++i) { + auto const distance{glm::distance(Segment.FastGetPoint(static_cast(i) / steps), Location)}; + if (distance < best) { + best = distance; + nearest = i; + } + } + auto const from{std::min(nearest, steps - 1)}; + auto way{Segment.FastGetPoint(static_cast(from + 1) / steps) - Segment.FastGetPoint(static_cast(from) / steps)}; + if (false == Forward) + way = -way; + return static_cast(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(std::atan2(front.x, front.z)); + state.speed_kmh = static_cast(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; + if (false == WriteLogFlag || false == Global.BlackBox || Global.editor_session) + return; + open(); + } + if (m_writer == nullptr) + return; + + std::scoped_lock lock{m_mutex}; + m_time += Deltatime; + 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; + 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(m_filebuffer.size())); + m_writer = std::make_unique(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(time.wYear), static_cast(simulation::Time.year_day()), + second_of_day(), text_of(Global.local_start_vehicle), text_of(Global.asVersion), static_cast(cabinterval), static_cast(lookinterval), + static_cast(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> blackbox_recorder::stop_point_headings() { + std::vector> 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(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::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())) > 0.5f) + return std::numeric_limits::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(event)}; + if (values == nullptr) + continue; + auto const &text{values->input_text()}; + if (text.starts_with(stoppoint)) + m_writer->stop_point(stoppoints++, text_of(std::string_view{text}.substr(stoppoint.size())), values->input_location(), + static_cast(values->input_value(1)), static_cast(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(values->input_value(1)), + static_cast(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(cell->Value1()), + static_cast(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(occupied->V * 3.6); + sample.acceleration = static_cast(controlling->AccS); + sample.lateral_acceleration = static_cast(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(owner->light_hint(end::front)); + sample.light_hint_tail = static_cast(owner->light_hint(end::rear)); + } + sample.odometer_km = static_cast(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(controlling->MainCtrlPos); + sample.scnd_controller = static_cast(controlling->ScndCtrlPos); + sample.reverser = static_cast(controlling->DirActive); + sample.train_brake = static_cast(occupied->fBrakeCtrlPos); + sample.local_brake = static_cast(occupied->LocalBrakePosA); + sample.brake_cylinder = static_cast(occupied->BrakePress); + sample.brake_pipe = static_cast(occupied->PipePress); + sample.motor_current = static_cast(controlling->Im); + if (owner != nullptr && owner->is_active()) + sample.ai = owner->ai_reference(); + sample.devices = devices_of(*occupied, *controlling); + sample.main_reservoir = static_cast(controlling->Compressor); + sample.voltage = static_cast(controlling->EngineVoltage); + sample.engine_rpm = static_cast(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((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(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(occupied->BrakeDelayFlag); + // the const one: with no driver the first channel + sample.radio_channel = static_cast(std::clamp(std::as_const(*train).RadioChannel(), 0, 255)); + sample.driver_valve = occupied->Handle != nullptr ? static_cast(occupied->Handle->GetCP()) : 0.0f; + sample.feed_pipe = static_cast(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(controlling->SpeedCtrlUnit.DesiredVelocity) : 0.0f; + sample.engine_power = static_cast(controlling->EnginePower); + sample.hand_brake = static_cast(std::clamp(occupied->ManualBrakePos, -128, 127)); + sample.brake_mode = static_cast(occupied->BrakeOpModeFlag); + sample.horn = static_cast(occupied->WarningSignal); + sample.integrated_controller = static_cast(controlling->eimic); + sample.clutch = static_cast(controlling->dizel_engagestate); + sample.converter_fill = static_cast(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(brakes); + m_writer->cab(sample); +} + +void blackbox_recorder::look_around(bool const Keyframe) { + auto const &time{simulation::Time.data()}; + m_writer->clock(m_time, static_cast(time.wYear), static_cast(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(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(); + 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; + 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())); + } +} + +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(side); + change.other = known_id(now.other); + change.other_side = static_cast(now.other_side); + change.flags = static_cast(now.flags); + change.previous_flags = static_cast(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(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())) > 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 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(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(entry.cell->Value1())}; + auto const speed2{static_cast(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(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(timetable.TTVmax); + auto const minutes{[](int const Hour, float const Minute) { return Hour >= 0 ? static_cast(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(mover->Dim.L), static_cast(mover->Dim.W), + static_cast(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_at(glm::dvec3 const &Location) const { + auto const found{std::ranges::find_if(m_signals, [&Location](signal_entry const &Signal) { return glm::distance(Signal.location, Location) < 0.01; })}; + if (found == m_signals.end()) + return blackbox::no_signal; + return static_cast(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(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_at(location), text_of(Event.name()), location, static_cast(Speed1), static_cast(Speed2), + static_cast(Trainspeed), static_cast(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(Duration), static_cast(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 diff --git a/simulation/blackboxrecorder.h b/simulation/blackboxrecorder.h new file mode 100644 index 000000000..677c680b0 --- /dev/null +++ b/simulation/blackboxrecorder.h @@ -0,0 +1,139 @@ +/* +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 "vehicle/blackbox.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class TDynamicObject; +class TTrack; +class TMemCell; +class basic_event; +enum class user_command; + +namespace simulation { + +// the black box of the session (see blackbox_doc.md): written along with the physics logs, in driver mode, +// from the moment the scenario is loaded until the simulator closes or the editor is entered +class blackbox_recorder { +public: + // starts the recording once the scenario is ready, if it is to be made, then records what is due. + // Deltatime: simulation time passed since the last call + void update(double Deltatime); + // writes the end of the recording and closes the file. a recording closed isn't started again + void close(); + + // what happens between the looks, from wherever it happens; the cabs may run on threads of their own. + // nothing is written unless the recording runs + void command(TDynamicObject const *Vehicle, user_command Command, int Action, double Param1, double Param2); + void driver_changed(TDynamicObject const *Vehicle, bool Ai); + // Event: the one telling the aspect of the signal + void signal_passed(TDynamicObject const *Vehicle, basic_event const &Event, double Speed1, double Speed2, double Trainspeed, double Overrun, std::uint8_t Flags); + void vehicle_removed(TDynamicObject const *Vehicle); + // a train at a station of its timetable; the time and the vehicle are filled in here + void station(TDynamicObject const *Vehicle, blackbox::station_event Station); + // a train told to sound the horn (W6), at Position + void horn_requested(TDynamicObject const *Vehicle, double Duration, double Tone, glm::dvec3 const &Position); + // where the user looks from, every frame; written when it changes, at most twice a second. Owner: the vehicle the + // camera goes with; the time and its number are filled in here + void camera(blackbox::camera_state Camera, TDynamicObject const *Owner); + +private: + struct coupler_entry { + TDynamicObject const *other{nullptr}; + int other_side{0}; + int flags{0}; + }; + struct vehicle_entry { + std::uint32_t id{blackbox::no_vehicle}; + blackbox::vehicle_state last; // as listed last + bool listed{false}; + std::array couplers; // as seen last + bool couplers_known{false}; + }; + struct switch_entry { + TTrack *track{nullptr}; + std::uint32_t index{0}; + int state{-1}; + }; + struct signal_entry { + TMemCell const *cell{nullptr}; + glm::dvec3 location{0.0}; + float speed1{0.0f}; + float speed2{0.0f}; + std::string command; + bool known{false}; + }; + + void open(); + void write_map(); + static std::vector> stop_point_headings(); + void write_cab(); + // the clock, the user's vehicle, the vehicles, the switches and the signals + void look_around(bool Keyframe); + void look_at_vehicles(bool Keyframe); + void look_at_switches(); + void look_at_signals(); + // the couplers of every vehicle against the last look: those coupled at the first look, then the changes + void look_at_couplers(); + void write_camera(); + // the timetable of the train the user is in, written again when it is another one; none isn't written + void look_at_timetable(TDynamicObject const &Vehicle, std::uint32_t Id); + // number of the vehicle, given (and the vehicle written) on first sight + std::uint32_t vehicle_id(TDynamicObject const *Vehicle); + // the number of a vehicle still on the scenery, no_vehicle for none or one taken off it + std::uint32_t known_id(TDynamicObject const *Vehicle); + std::uint32_t signal_at(glm::dvec3 const &Location) const; + + std::mutex m_mutex; + std::array m_filebuffer{}; + std::ofstream m_file; + std::unique_ptr m_writer; + bool m_started{false}; // tried to start, made or not + double m_time{0.0}; + double m_cabdue{0.0}; + double m_lookdue{0.0}; + double m_keyframedue{0.0}; + std::unordered_map m_vehicles; + std::uint32_t m_nextvehicle{1}; + std::vector m_switches; + std::vector m_signals; + std::uint32_t m_uservehicle{blackbox::no_vehicle}; + bool m_userincab{false}; + std::int8_t m_usercab{0}; + bool m_userknown{false}; + std::string m_timetabletrain; + TDynamicObject const *m_timetablevehicle{nullptr}; // the user's vehicle at the last look at the timetable + int m_timetablestations{0}; + blackbox::camera_state m_camera; // as given last + TDynamicObject const *m_cameraowner{nullptr}; + blackbox::camera_state m_camerawritten; // as written last + bool m_cameraknown{false}; + bool m_camerawrote{false}; + double m_cameradue{0.0}; + // how long the looks around take, told in the log when the recording ends + std::chrono::steady_clock::duration m_looktime{}; + std::chrono::steady_clock::duration m_looklongest{}; + int m_looks{0}; +}; + +extern blackbox_recorder Blackbox; + +} // namespace simulation diff --git a/simulation/simulation.cpp b/simulation/simulation.cpp index 31058900c..87cce81b6 100644 --- a/simulation/simulation.cpp +++ b/simulation/simulation.cpp @@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/. #include "simulation/simulationtime.h" #include "simulation/simulationenvironment.h" #include "simulation/simulationtest.h" +#include "simulation/blackboxrecorder.h" #include "utilities/Globals.h" #include "world/Event.h" @@ -201,6 +202,7 @@ state_manager::update_scripting_interface() { void state_manager::process_commands() { command_data commanddata; while( Commands.pop( commanddata, (uint32_t)command_target::simulation )) { + Blackbox.command( nullptr, commanddata.command, commanddata.action, commanddata.param1, commanddata.param2 ); if (commanddata.command == user_command::consistreleaser) { TDynamicObject *found_vehicle = simulation::Vehicles.find(commanddata.payload); TDynamicObject *vehicle = found_vehicle; diff --git a/tests/unit/test_blackbox.cpp b/tests/unit/test_blackbox.cpp new file mode 100644 index 000000000..b11253b49 --- /dev/null +++ b/tests/unit/test_blackbox.cpp @@ -0,0 +1,488 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle/blackbox.h" + +#include +#include +#include + +// the black box is read by other programs (the starter, a viewer), from the layout in blackbox_doc.md alone. +// these read what the writer wrote the way such a reader would, so a change of the layout shows up here + +namespace { + +// reads the numbers the way a .NET BinaryReader does: little-endian, texts after their 7 bit length +class reader { +public: + explicit reader(std::string_view Data) : m_data{Data} {} + + template + Type_ get() { + REQUIRE(m_position + sizeof(Type_) <= m_data.size()); + Type_ value; + std::memcpy(&value, m_data.data() + m_position, sizeof(Type_)); + m_position += sizeof(Type_); + return value; + } + + std::string text() { + std::uint32_t length{0}; + int shift{0}; + std::uint8_t part; + do { + part = get(); + length |= static_cast(part & 0x7f) << shift; + shift += 7; + } while ((part & 0x80) != 0); + REQUIRE(m_position + length <= m_data.size()); + std::string value{m_data.substr(m_position, length)}; + m_position += length; + return value; + } + + std::size_t position() const { + return m_position; } + bool done() const { + return m_position == m_data.size(); } + +private: + std::string_view m_data; + std::size_t m_position{0}; +}; + +struct record_view { + blackbox::record type; + std::string payload; +}; + +// splits the file into its records, checking the header and that the lengths add up to the whole file +std::vector records(std::string const &File) { + reader file{File}; + REQUIRE(File.size() >= blackbox::header_size); + CHECK(std::string_view{File.data(), 8} == std::string_view{blackbox::magic.data(), 8}); + file.get>(); + CHECK(file.get() == blackbox::format_version); + CHECK(file.get() == 0); + std::vector result; + while (false == file.done()) { + auto const type{file.get()}; + auto const length{file.get()}; + REQUIRE(file.position() + length <= File.size()); + result.push_back({static_cast(type), File.substr(file.position(), length)}); + for (std::uint32_t i = 0; i < length; ++i) + file.get(); + } + return result; +} + +blackbox::cab_sample sample() { + blackbox::cab_sample s; + s.time = 12.5; + s.vehicle = 3; + s.position = {-1234.5, 87.25, 40321.125}; + s.speed_kmh = -42.5f; + s.acceleration = 0.25f; + s.odometer_km = 17.75f; + s.driving = blackbox::driver::human; + s.flags = blackbox::cab_flags::in_cab | blackbox::cab_flags::ai_valid; + s.main_controller = 7; + s.scnd_controller = 2; + s.reverser = -1; + s.train_brake = 1.5f; + s.local_brake = 0.5f; + s.brake_cylinder = 3.75f; + s.brake_pipe = 4.5f; + s.motor_current = 640.0f; + s.ai = {60.0f, 40.0f, 812.0f, -0.35f, 455.0f, 100.0f, 80.0f}; + s.devices = blackbox::cab_devices::battery | blackbox::cab_devices::pantograph_rear | blackbox::cab_devices::shunt_mode; + s.main_reservoir = 8.25f; + s.voltage = 3000.0f; + s.engine_rpm = 1250.0f; + s.lights_head = 0x15; + s.lights_tail = 0x22; + s.cab = -1; + s.security = blackbox::security_flags::vigilance | blackbox::security_flags::braking; + s.lateral_acceleration = -0.75f; + s.movement = blackbox::movements::shunting; + s.light_hint_head = -1; + s.light_hint_tail = 0x22; + s.equipped_head = 0x1B5; + s.equipped_tail = 0x22; + s.engine = blackbox::engines::diesel_electric; + s.brake_delay = 2; + s.radio_channel = 3; + s.driver_valve = 4.75f; + s.feed_pipe = 8.5f; + s.oil_pressure = 0.5f; + s.water_temperature = 78.5f; + s.oil_temperature = 70.25f; + s.engine_temperature = 81.0f; + s.speed_control_kmh = 90.0f; + s.engine_power = 1250.5f; + s.hand_brake = 5; + s.brake_mode = 4; + s.horn = 2; + s.integrated_controller = -0.5f; + s.clutch = 0.75f; + s.converter_fill = 0.25f; + s.consist_brake_cylinder = 3.5f; + return s; +} + +} // namespace + +TEST_CASE("blackbox: the file starts with the magic and the version, then come records which say how long they are") { + std::ostringstream file; + blackbox::writer out{file}; + out.header(); + out.info({"$td.scn", "2026-11-08T15:32:58", 2026, 312, 8 * 3600.0, "ET22-1", "26.1", 0.1f, 1.0f, 60.0f}); + out.track({0, "tor1", blackbox::track_kinds::normal, 0, 1, {{0.f, 0.f, 0.f}, {10.f, 0.5f, 100.f}}}); + out.signal(0, "sem_a", {10.0, 0.0, 100.0}); + out.vehicle({1, "et22-1", "ET22", "12345", 19.24f, 3.0f, 1}); + out.cab(sample()); + out.vehicles(13.0, true, {{1, {1.f, 2.f, 3.f}, 0.5f, 60.f, blackbox::driver::ai}}); + out.switch_state(13.0, 0, 1); + out.signal_aspect(13.0, 0, 0.0f, 40.0f, "SetVelocity"); + out.command({13.5, 1, true, 1.0, 0.0, "mastercontrollerincrease"}); + out.driver_change(14.0, 1, blackbox::driver::ai); + out.signal_passed({14.5, 1, 0, "sem_a_sem", {10.0, 0.0, 100.0}, 0.0f, 40.0f, 35.0f, 2.5f, blackbox::passing_flags::at_stop}); + out.user_vehicle(15.0, 1, false, -1); + out.clock(15.0, 2026, 312, 8 * 3600.0 + 15.0); + out.vehicle_removed(16.0, 1); + out.end(16.0); + + auto const file_bytes{file.str()}; + CHECK(file_bytes.size() == out.size()); + auto const all{records(file_bytes)}; + + using blackbox::record; + std::vector const expected{record::info, record::track, record::signal, record::vehicle, record::cab, record::vehicles, record::switch_state, record::signal_aspect, + record::command, record::driver_change, record::signal_passed, record::user_vehicle, record::clock, record::vehicle_removed, record::end}; + REQUIRE(all.size() == expected.size()); + for (std::size_t i = 0; i < all.size(); ++i) + CHECK(all[i].type == expected[i]); + + SUBCASE("the info") { + reader info{all[0].payload}; + CHECK(info.text() == "$td.scn"); + CHECK(info.text() == "2026-11-08T15:32:58"); + CHECK(info.get() == 2026); + CHECK(info.get() == 312); + CHECK(info.get() == 8 * 3600.0); + CHECK(info.text() == "ET22-1"); + CHECK(info.text() == "26.1"); + CHECK(info.get() == 0.1f); + CHECK(info.get() == 1.0f); + CHECK(info.get() == 60.0f); + CHECK(info.done()); + } + SUBCASE("a track: its points after their count") { + reader track{all[1].payload}; + CHECK(track.get() == 0); + CHECK(track.text() == "tor1"); + CHECK(track.get() == blackbox::track_kinds::normal); + CHECK(track.get() == 0); + CHECK(track.get() == 1); + CHECK(track.get() == 2); + for (int i = 0; i < 3; ++i) + CHECK(track.get() == 0.f); + CHECK(track.get() == 10.f); + CHECK(track.get() == 0.5f); + CHECK(track.get() == 100.f); + CHECK(track.done()); + } + SUBCASE("a passed signal") { + reader passed{all[10].payload}; + CHECK(passed.get() == 14.5); + CHECK(passed.get() == 1); + CHECK(passed.get() == 0); + CHECK(passed.text() == "sem_a_sem"); + CHECK(passed.get() == 10.0); + CHECK(passed.get() == 0.0); + CHECK(passed.get() == 100.0); + CHECK(passed.get() == 0.0f); + CHECK(passed.get() == 40.0f); + CHECK(passed.get() == 35.0f); + CHECK(passed.get() == 2.5f); + CHECK(passed.get() == blackbox::passing_flags::at_stop); + CHECK(passed.done()); + } +} + +TEST_CASE("blackbox: a sample of the user's train has the documented size and order") { + std::ostringstream file; + blackbox::writer out{file}; + out.header(); + out.cab(sample()); + auto const all{records(file.str())}; + REQUIRE(all.size() == 1); + REQUIRE(all[0].payload.size() == blackbox::cab_sample_size); + + reader cab{all[0].payload}; + CHECK(cab.get() == 12.5); + CHECK(cab.get() == 3); + CHECK(cab.get() == -1234.5); + CHECK(cab.get() == 87.25); + CHECK(cab.get() == 40321.125); + CHECK(cab.get() == -42.5f); + CHECK(cab.get() == 0.25f); + CHECK(cab.get() == 17.75f); + CHECK(cab.get() == static_cast(blackbox::driver::human)); + CHECK(cab.get() == (blackbox::cab_flags::in_cab | blackbox::cab_flags::ai_valid)); + CHECK(cab.get() == 7); + CHECK(cab.get() == 2); + CHECK(cab.get() == -1); + CHECK(cab.get() == 1.5f); + CHECK(cab.get() == 0.5f); + CHECK(cab.get() == 3.75f); + CHECK(cab.get() == 4.5f); + CHECK(cab.get() == 640.0f); + // what the AI would do + CHECK(cab.get() == 60.0f); + CHECK(cab.get() == 40.0f); + CHECK(cab.get() == 812.0f); + CHECK(cab.get() == -0.35f); + CHECK(cab.get() == 455.0f); + CHECK(cab.get() == 100.0f); + CHECK(cab.get() == 80.0f); + // what is switched on + CHECK(cab.get() == 0x8009); + CHECK(cab.get() == 8.25f); + CHECK(cab.get() == 3000.0f); + CHECK(cab.get() == 1250.0f); + CHECK(cab.get() == 0x15); + CHECK(cab.get() == 0x22); + // the cab, the train protection and the lateral acceleration + CHECK(cab.get() == -1); + CHECK(cab.get() == 0x11); + CHECK(cab.get() == -0.75f); + // what the driver goes by, and the lights asked for at the head and the tail + CHECK(cab.get() == blackbox::movements::shunting); + CHECK(cab.get() == -1); + CHECK(cab.get() == 0x22); + // the lamps there are at the head and the tail + CHECK(cab.get() == 0x1B5); + CHECK(cab.get() == 0x22); + // the engine, the brakes' setting, the radio, the pressures, the oil and the temperatures, the cruise control, the power + CHECK(cab.get() == blackbox::engines::diesel_electric); + CHECK(cab.get() == 2); + CHECK(cab.get() == 3); + CHECK(cab.get() == 4.75f); + CHECK(cab.get() == 8.5f); + CHECK(cab.get() == 0.5f); + CHECK(cab.get() == 78.5f); + CHECK(cab.get() == 70.25f); + CHECK(cab.get() == 81.0f); + CHECK(cab.get() == 90.0f); + CHECK(cab.get() == 1250.5f); + // the hand brake, the mode of the brakes, the horn, the integrated controller, the clutch, the torque converter + CHECK(cab.get() == 5); + CHECK(cab.get() == 4); + CHECK(cab.get() == 2); + CHECK(cab.get() == -0.5f); + CHECK(cab.get() == 0.75f); + CHECK(cab.get() == 0.25f); + // the highest brake cylinder pressure of the train + CHECK(cab.get() == 3.5f); + CHECK(cab.done()); +} + +TEST_CASE("blackbox: the stations, the stop points and the horn points are written field by field") { + std::ostringstream file; + blackbox::writer out{file}; + out.header(); + out.stop_point(2, "Gdynia Gl.", {100.0, 1.5, -200.0}, -15.0f, 250.0f, 1.5f); + out.horn_point(0, "w6_most", {10.0, 0.0, 20.0}, 2.0f, 1.0f); + out.timetable({30.0, 4, "61110", "MOJ", "Gdynia", "Hel", 120.0f, {{"Gdynia", 0.0f, -1.0f, -1.0f, 10 * 60 + 5.0f, false}, {"Reda", 21.5f, 100.0f, 10 * 60 + 22.0f, 10 * 60 + 23.0f, true}}}); + out.station({40.5, 4, blackbox::station_kinds::stopped, 2, "Reda", {100.0, 1.5, -200.0}, 3.25f, 15.0f, 66.5f, 250.0f, 10 * 60 + 22.0f, 10 * 60 + 23.0f}); + out.horn_request(41.0, 4, 2.0f, 1.0f, {10.0, 0.0, 20.0}); + out.user_vehicle(42.0, 5, true, 1); + auto const all{records(file.str())}; + using blackbox::record; + REQUIRE(all.size() == 6); + CHECK(all[0].type == record::stop_point); + CHECK(all[1].type == record::horn_point); + CHECK(all[2].type == record::timetable); + CHECK(all[3].type == record::station); + CHECK(all[4].type == record::horn_request); + CHECK(all[5].type == record::user_vehicle); + + SUBCASE("a stop point") { + reader point{all[0].payload}; + CHECK(point.get() == 2); + CHECK(point.text() == "Gdynia Gl."); + CHECK(point.get() == 100.0); + CHECK(point.get() == 1.5); + CHECK(point.get() == -200.0); + CHECK(point.get() == -15.0f); + CHECK(point.get() == 250.0f); + CHECK(point.get() == 1.5f); + CHECK(point.done()); + } + SUBCASE("a horn point") { + reader point{all[1].payload}; + CHECK(point.get() == 0); + CHECK(point.text() == "w6_most"); + CHECK(point.get() == 10.0); + CHECK(point.get() == 0.0); + CHECK(point.get() == 20.0); + CHECK(point.get() == 2.0f); + CHECK(point.get() == 1.0f); + CHECK(point.done()); + } + SUBCASE("a timetable: its rows after their count") { + reader timetable{all[2].payload}; + CHECK(timetable.get() == 30.0); + CHECK(timetable.get() == 4); + CHECK(timetable.text() == "61110"); + CHECK(timetable.text() == "MOJ"); + CHECK(timetable.text() == "Gdynia"); + CHECK(timetable.text() == "Hel"); + CHECK(timetable.get() == 120.0f); + CHECK(timetable.get() == 2); + CHECK(timetable.text() == "Gdynia"); + CHECK(timetable.get() == 0.0f); + CHECK(timetable.get() == -1.0f); + CHECK(timetable.get() == -1.0f); + CHECK(timetable.get() == 605.0f); + CHECK(timetable.get() == 0); + CHECK(timetable.text() == "Reda"); + CHECK(timetable.get() == 21.5f); + CHECK(timetable.get() == 100.0f); + CHECK(timetable.get() == 622.0f); + CHECK(timetable.get() == 623.0f); + CHECK(timetable.get() == 1); + CHECK(timetable.done()); + } + SUBCASE("a train at a station") { + reader station{all[3].payload}; + CHECK(station.get() == 40.5); + CHECK(station.get() == 4); + CHECK(station.get() == blackbox::station_kinds::stopped); + CHECK(station.get() == 2); + CHECK(station.text() == "Reda"); + CHECK(station.get() == 100.0); + CHECK(station.get() == 1.5); + CHECK(station.get() == -200.0); + CHECK(station.get() == 3.25f); + CHECK(station.get() == 15.0f); + CHECK(station.get() == 66.5f); + CHECK(station.get() == 250.0f); + CHECK(station.get() == 622.0f); + CHECK(station.get() == 623.0f); + CHECK(station.done()); + } + SUBCASE("a horn asked for") { + reader horn{all[4].payload}; + CHECK(horn.get() == 41.0); + CHECK(horn.get() == 4); + CHECK(horn.get() == 2.0f); + CHECK(horn.get() == 1.0f); + CHECK(horn.get() == 10.0); + CHECK(horn.get() == 0.0); + CHECK(horn.get() == 20.0); + CHECK(horn.done()); + } + SUBCASE("the user's vehicle, with the cab") { + reader user{all[5].payload}; + CHECK(user.get() == 42.0); + CHECK(user.get() == 5); + CHECK(user.get() == 1); + CHECK(user.get() == 1); + CHECK(user.done()); + } +} + +TEST_CASE("blackbox: the camera and the couplings are written field by field") { + std::ostringstream file; + blackbox::writer out{file}; + out.header(); + out.camera({12.5, blackbox::camera_modes::external, 2, 7, {1.0, 2.0, 3.0}, 1.25f, -0.5f}); + out.coupling({13.0, 3, 1, 4, 0, 0x03, 0x01, 0, {5.0, 0.5, -6.0}}); + auto const all{records(file.str())}; + using blackbox::record; + REQUIRE(all.size() == 2); + CHECK(all[0].type == record::camera); + CHECK(all[1].type == record::coupling); + + SUBCASE("the camera") { + reader camera{all[0].payload}; + CHECK(camera.get() == 12.5); + CHECK(camera.get() == blackbox::camera_modes::external); + CHECK(camera.get() == 2); + CHECK(camera.get() == 7); + CHECK(camera.get() == 1.0); + CHECK(camera.get() == 2.0); + CHECK(camera.get() == 3.0); + CHECK(camera.get() == 1.25f); + CHECK(camera.get() == -0.5f); + CHECK(camera.done()); + } + SUBCASE("a coupling") { + reader coupling{all[1].payload}; + CHECK(coupling.get() == 13.0); + CHECK(coupling.get() == 3); + CHECK(coupling.get() == 1); + CHECK(coupling.get() == 4); + CHECK(coupling.get() == 0); + CHECK(coupling.get() == 0x03); + CHECK(coupling.get() == 0x01); + CHECK(coupling.get() == 0); + CHECK(coupling.get() == 5.0); + CHECK(coupling.get() == 0.5); + CHECK(coupling.get() == -6.0); + CHECK(coupling.done()); + } +} + +TEST_CASE("blackbox: a look at the vehicles takes 25 bytes a vehicle") { + std::ostringstream file; + blackbox::writer out{file}; + out.header(); + out.vehicles(3.0, false, {{1, {1.f, 2.f, 3.f}, 0.5f, 60.f, blackbox::driver::ai}, {2, {4.f, 5.f, 6.f}, -0.5f, -20.f, blackbox::driver::none}, {7, {}, 0.f, 0.f, blackbox::driver::human}}); + auto const all{records(file.str())}; + REQUIRE(all.size() == 1); + CHECK(all[0].payload.size() == 8 + 1 + 4 + 3 * blackbox::vehicle_state_size); + + reader look{all[0].payload}; + CHECK(look.get() == 3.0); + CHECK(look.get() == 0); + CHECK(look.get() == 3); + CHECK(look.get() == 1); + CHECK(look.get() == 1.f); + CHECK(look.get() == 2.f); + CHECK(look.get() == 3.f); + CHECK(look.get() == 0.5f); + CHECK(look.get() == 60.f); + CHECK(look.get() == static_cast(blackbox::driver::ai)); + CHECK(look.get() == 2); +} + +TEST_CASE("blackbox: texts are UTF-8 after their length in 7 bit groups, low first") { + std::ostringstream file; + blackbox::writer out{file}; + out.header(); + std::string const long_name(200, 'x'); + std::string const polish{"\xc5\xbc\xc3\xb3\xc5\x82w"}; // żółw, 7 bytes + out.signal(0, long_name, {}); + out.signal(1, polish, {}); + out.signal(2, "", {}); + auto const all{records(file.str())}; + REQUIRE(all.size() == 3); + + // 200 = 0b1'1001000: 0xc8 (low 7 bits and the more flag), then 0x01 + CHECK(static_cast(all[0].payload[4]) == 0xc8); + CHECK(static_cast(all[0].payload[5]) == 0x01); + reader first{all[0].payload}; + first.get(); + CHECK(first.text() == long_name); + + CHECK(static_cast(all[1].payload[4]) == 7); + reader second{all[1].payload}; + second.get(); + CHECK(second.text() == polish); + + CHECK(static_cast(all[2].payload[4]) == 0); + CHECK(all[2].payload.size() == 4 + 1 + 24); +} diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 5fbd4d4d0..ca3b7507f 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -538,6 +538,12 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& return true; } + if (token == "blackbox") + { + ParseOne(Parser, BlackBox); + return true; + } + if (token == "fullphysics") { ParseOne(Parser, FullPhysics); @@ -1682,6 +1688,7 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "sound.volume.ambient", EnvironmentAmbientVolume ); export_as_text( Output, "sound.asyncload", AudioAsyncLoad ); export_as_text( Output, "physicslog", WriteLogFlag ); + export_as_text( Output, "blackbox", BlackBox ); export_as_text( Output, "fullphysics", FullPhysics ); export_as_text( Output, "debuglog", iWriteLogEnabled ); export_as_text( Output, "multiplelogs", MultipleLogs ); diff --git a/utilities/Globals.h b/utilities/Globals.h index dad8eb292..2fff6e484 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -112,6 +112,7 @@ struct global_settings { bool priorityLoadText3D{false}; // ladowanie T3D priorytetowo int iWriteLogEnabled{ 3 }; // maska bitowa: 1-zapis do pliku, 2-okienko, 4-nazwy torów bool MultipleLogs{ false }; + bool BlackBox{ true }; // with the physics logs on, the black box of the session is written as well (blackbox_doc.md) bool ShowSystemConsole{ false }; // toggle visibility of the OS console window with log output unsigned int DisabledLogTypes{ 0 }; bool ParserLogIncludes{ true }; diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 27b742b06..dda2d8875 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -25,6 +25,7 @@ http://mozilla.org/MPL/2.0/. #include "world/Event.h" #include "world/MemCell.h" #include "simulation/simulation.h" +#include "simulation/blackboxrecorder.h" #include "simulation/simulationtime.h" #include "simulation/simulationtest.h" #include "world/Track.h" @@ -810,7 +811,7 @@ void TController::TableCheck(double fDistance) break; } // a switch is a track, the update of a signal never ends the loop above - CheckSignalPassedAtDanger( sSpeedTable[ i ], passedbefore ); + CheckSignalPassed( sSpeedTable[ i ], passedbefore ); if (sSpeedTable[i].iFlags & spTrack) // jeśli odcinek { if( sSpeedTable[ i ].fDist + sSpeedTable[ i ].trTrack->Length() < -fLength ) // a skład wyjechał całą długością poza @@ -837,7 +838,7 @@ void TController::TableCheck(double fDistance) auto const lastpassedbefore { TestFlag( sSpeedTable[ iLast ].iFlags, spElapsed ) }; sSpeedTable[iLast].Update(); // aktualizacja ostatniego // a signal at stop often is the last, the scan doesn't go past it - CheckSignalPassedAtDanger( sSpeedTable[ iLast ], lastpassedbefore ); + CheckSignalPassed( sSpeedTable[ iLast ], lastpassedbefore ); // WriteLog("TableCheck: Upate last track. Dist=" + AnsiString(sSpeedTable[iLast].fDist)); if( sSpeedTable[ iLast ].fDist < fDistance ) { TableTraceRoute( fDistance, pVehicles[ end::rear ] ); // doskanowanie dalszego odcinka @@ -860,16 +861,53 @@ bool is_signal_at_stop( TSpeedPos const &Point ) && Point.evEvent != nullptr; } +// the position is a signal, a main or a shunting one: the aspect comes from its memory cell. putvalues events +// set speed limits by the track, and the flags they get don't make them signals +bool is_signal( TSpeedPos const &Point ) +{ + return TestFlag( Point.iFlags, spEvent ) + && ( TestFlag( Point.iFlags, spSemaphor ) || TestFlag( Point.iFlags, spShuntSemaphor ) ) + && Point.evEvent != nullptr + && typeid( *Point.evEvent ) == typeid( getvalues_event ); +} + +std::uint8_t passing_flags_of( TSpeedPos const &Point, double const Speedatsignal, bool const Trainmovement ) +{ + std::uint8_t flags { 0 }; + auto const shunting { TestFlag( Point.iFlags, spShuntSemaphor ) }; + if( shunting ) { + flags |= blackbox::passing_flags::shunt; + } + // the speeds of the aspects are whole km/h, -1 for no limit; a shunting signal binds only the shunting + if( std::abs( Speedatsignal ) < 0.5 && ( false == shunting || false == Trainmovement ) ) { + flags |= ( TestFlag( Point.iFlags, spStopOnSBL ) ? blackbox::passing_flags::automatic_block : blackbox::passing_flags::at_stop ); + } + return flags; +} + } // namespace -void TController::CheckSignalPassedAtDanger( TSpeedPos const &Point, bool const Passedbefore ) const +void TController::CheckSignalPassed( TSpeedPos const &Point, bool const Passedbefore ) const { - if( false == Passedbefore - && TestFlag( Point.iFlags, spElapsed ) - && is_signal_at_stop( Point ) - && ( mvOccupied->CategoryFlag & 1 ) != 0 ) { // railway only + if( Passedbefore + || false == TestFlag( Point.iFlags, spElapsed ) + || ( mvOccupied->CategoryFlag & 1 ) == 0 ) { // railway only + return; + } + if( is_signal_at_stop( Point ) ) { simulation::test().signal_passed_at_danger( OwnerName(), Point.evEvent->m_name ); } + if( is_signal( Point ) ) { + // the aspect the train had of it: the table keeps it once the signal is behind, while the memory cell + // may already show stop for the train itself, past the signal on the track it guards. the stop of an + // automatic block signal is kept as the speed allowed after it, with a flag of its own + auto const speedatsignal { TestFlag( Point.iFlags, spStopOnSBL ) ? 0.0 : Point.fVelNext }; + simulation::Blackbox.signal_passed( + pVehicles[ end::front ], *Point.evEvent, + speedatsignal, Point.evEvent->input_value( 2 ), + mvOccupied->Vel, -Point.fDist, + passing_flags_of( Point, speedatsignal, ( OrderCurrentGet() & ( Shunt | Loose_shunt ) ) == 0 ) ); + } } auto const passengerstopmaxdistance { 400.0 }; // maximum allowed distance between passenger stop point and consist head @@ -1196,6 +1234,7 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub // przy jakim dystansie (stanie licznika) ma przesunąć na następny postój fLastStopExpDist = mvOccupied->DistCounter + 0.250 + 0.001 * fLength; TrainParams.UpdateMTable( simulation::Time, asNextStop ); + record_station( Point, blackbox::station_kinds::passed ); UpdateDelayFlag(); TrainParams.StationIndexInc(); // przejście do następnej asNextStop = TrainParams.NextStop(); // pobranie kolejnego miejsca zatrzymania @@ -1246,6 +1285,7 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub // for human-driven vehicles discard the stop point if they leave it far enough behind if( false == AIControllFlag && Point.fDist < -1 * std::max(fLength + 100, 250.0) - Point.fMoved ) { + record_station( Point, blackbox::station_kinds::missed ); Point.iFlags = 0; // nie liczy się już zupełnie (nie wyśle SetVelocity) Point.fVelNext = -1; // można jechać za W4 if( Point.fDist <= 0.0 && eSignNext == Point.evEvent ) { @@ -1286,6 +1326,7 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub } if (TrainParams.UpdateMTable( simulation::Time, asNextStop) ) { // to się wykona tylko raz po zatrzymaniu na W4 + record_station( Point, blackbox::station_kinds::stopped ); if( TrainParams.StationIndex < TrainParams.StationCount ) { // jeśli są dalsze stacje, bez trąbienia przed odjazdem // also ignore any horn cue that may be potentially set below 1 km/h and before the actual full stop @@ -1352,6 +1393,7 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub // z dalszą akcją czekamy do godziny odjazdu // cargo trains and passenger trains at maintenance stop don't need to wait IsAtPassengerStop = false; + record_station( Point, blackbox::station_kinds::ready ); // przy jakim dystansie (stanie licznika) ma przesunąć na następny postój fLastStopExpDist = mvOccupied->DistCounter + 0.050 + 0.001 * fLength; TrainParams.StationIndexInc(); // przejście do następnej @@ -2881,6 +2923,98 @@ TBrakeSystem TController::consist_brake_system() const { return isepcapable ? TBrakeSystem::ElectroPneumatic : TBrakeSystem::Pneumatic; } +std::pair TController::next_speed_limit() const { + + auto const speedlimit { static_cast( VelDesired ) }; + auto nextspeedlimit { speedlimit }; + auto nextspeedlimitdistance { std::numeric_limits::max() }; + if( speedlimit == 0 ) { + // if we aren't allowed to move then any next speed limit is irrelevant + return { nextspeedlimit, nextspeedlimitdistance }; + } + // nie przekraczać rozkladowej + auto const schedulespeedlimit { ( + ( OrderCurrentGet() & ( Obey_train | Bank ) ) != 0 && TrainParams.TTVmax > 0.0 ? static_cast( TrainParams.TTVmax ) : + ( OrderCurrentGet() & ( Obey_train | Bank ) ) == 0 ? static_cast( fShuntVelocity ) : + -1 ) }; + // first take note of any speed change which should occur after passing potential current speed limit + if( VelLimitLastDist.second > 0 ) { + nextspeedlimit = min_speed( schedulespeedlimit, static_cast( VelLimitLastDist.first ) ); + nextspeedlimitdistance = VelLimitLastDist.second; + } + // then take into account speed change ahead, compare it with speed after potentially clearing last limit + // lower of these two takes priority; otherwise limit lasts at least until potential last limit is cleared + auto const noactivespeedlimit { VelLimitLastDist.second < 0 }; + if( auto const speedatproximitydistance { min_speed( schedulespeedlimit, static_cast( VelNext ) ) }; speedatproximitydistance == nextspeedlimit ) { + if( noactivespeedlimit ) { + nextspeedlimit = speedatproximitydistance; + nextspeedlimitdistance = ActualProximityDist; + } + } + else if( speedatproximitydistance < nextspeedlimit ) { + // if the speed limit ahead is more strict than our current limit, it's important enough to report + if( speedatproximitydistance < VelDesired ) { + nextspeedlimit = speedatproximitydistance; + nextspeedlimitdistance = ActualProximityDist; + } + // otherwise report it only if it's located after our current (lower) limit ends + else if( ActualProximityDist > nextspeedlimitdistance ) { + nextspeedlimit = speedatproximitydistance; + nextspeedlimitdistance = ActualProximityDist; + } + } + else if( noactivespeedlimit ) { // implicit proximity > last, report only if last limit isn't present + nextspeedlimit = speedatproximitydistance; + nextspeedlimitdistance = ActualProximityDist; + } + // HACK: if our current speed limit extends beyond our scan range don't display potentially misleading information about its length + if( nextspeedlimitdistance >= EU07_AI_SPEEDLIMITEXTENDSBEYONDSCANRANGE ) { + nextspeedlimit = speedlimit; + } + // HACK: hide next speed limit if the 'limit' is a vehicle in front of us + else if( ActualProximityDist == std::abs( TrackObstacle() ) ) { + nextspeedlimit = speedlimit; + } + return { nextspeedlimit, nextspeedlimitdistance }; +} + +blackbox::ai_reference TController::ai_reference() const { + + blackbox::ai_reference ai; + ai.target_kmh = static_cast( VelDesired ); + if( auto const [ limit, distance ] { next_speed_limit() }; limit != static_cast( VelDesired ) ) { + ai.next_limit_kmh = static_cast( limit ); + ai.next_limit_distance = static_cast( distance ); + } + ai.acceleration = static_cast( AccDesired ); + ai.braking_distance = static_cast( fBrakeDist ); + ai.timetable_vmax_kmh = ( TrainParams.TTVmax > 0.0 ? static_cast( TrainParams.TTVmax ) : -1.0f ); + ai.track_vmax_kmh = static_cast( mvOccupied->RunningTrack.Velmax ); + return ai; +} + +void TController::record_station( TSpeedPos const &Point, std::uint8_t const Kind ) const { + + if( Point.evEvent == nullptr ) { return; } + blackbox::station_event entry; + entry.kind = Kind; + auto const row { TrainParams.StationIndex }; + entry.row = static_cast( std::max( 0, row ) ); + entry.station = asNextStop; + entry.position = Point.evEvent->input_location(); + // the stop point may be moved back from the indicator, to where the head of the train is to stop + entry.distance = static_cast( Point.fDist + Point.fMoved ); + entry.stop_at = static_cast( Point.fMoved ); + entry.train_length = static_cast( fLength ); + entry.platform = static_cast( Point.evEvent->input_value( 2 ) ); + if( row >= 0 && row <= TrainParams.StationCount ) { + auto const &line { TrainParams.TimeTable[ row ] }; + entry.arrival = ( line.Ah >= 0 ? static_cast( line.Ah * 60 ) + line.Am : -1.0f ); + entry.departure = ( line.Dh >= 0 ? static_cast( line.Dh * 60 ) + line.Dm : -1.0f ); + } + simulation::Blackbox.station( pVehicles[ end::front ], std::move( entry ) ); +} + int TController::OrderDirectionChange(int newdir, TMoverParameters *Vehicle) { // zmiana kierunku jazdy, niezależnie od kabiny int testd = newdir; @@ -5092,6 +5226,8 @@ bool TController::PutCommand( std::string NewCommand, double NewValue1, double N if( NewValue1 > 0 && NewValue2 > 0 ) { fWarningDuration = NewValue1; // czas trąbienia cue_action( driver_hint::hornon, NewValue2 ); // horn combination flag + simulation::Blackbox.horn_requested( + pVehicles[ end::front ], NewValue1, NewValue2, NewLocation != nullptr ? *NewLocation : pVehicles[ end::front ]->GetPosition() ); } return true; } @@ -5922,6 +6058,10 @@ void TController::TakeControl( bool const Aidriver, bool const Forcevehiclecheck { // przejęcie kontroli przez AI albo oddanie if (AIControllFlag == Aidriver && !Forcevehiclecheck) return; // już jest jak ma być + if( AIControllFlag != Aidriver ) { + // the autopilot switched on or off + simulation::Blackbox.driver_changed( pVehicle, Aidriver ); + } if (Aidriver) //żeby nie wykonywać dwa razy { // teraz AI prowadzi AIControllFlag = AIdriver; diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 063c844f9..69f5cffc0 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/. #include "world/mtable.h" #include "utilities/translation.h" #include "application/driverhints.h" +#include "vehicle/blackbox.h" auto const EU07_AI_ACCELERATION = 0.05; auto const EU07_AI_NOACCELERATION = -0.05; @@ -269,7 +270,18 @@ public: return mvControlling->EngineType == TEngineType::DieselElectric || mvControlling->EngineType == TEngineType::DieselEngine; } TBrakeSystem consist_brake_system() const; + // the next change of the speed limit ahead, as the driving aid tells it: the speed and the distance to it. + // the speed is the present limit (VelDesired) when nothing worth telling changes within the scan range + std::pair next_speed_limit() const; + // what the AI would do at this place and speed, worked out also while a human drives, for the black box + blackbox::ai_reference ai_reference() const; + // the light pattern the timetable or the scenery asks for at an end of the train, -1 the usual one + int light_hint( int const End ) const { return m_lighthints[ End ]; } + // the order the driver goes by now: a train run, shunting and the like + TOrders order_current() const { return OrderCurrentGet(); } private: + // tells the black box of the train at the station of its timetable it makes for, at the stop point given + void record_station( TSpeedPos const &Point, std::uint8_t Kind ) const; void Activation(); // umieszczenie obsady w odpowiednim członie void ControllingSet(); // znajduje człon do sterowania void SetDriverPsyche(); @@ -550,8 +562,8 @@ private: void TableTraceRoute( double fDistance, TDynamicObject *pVehicle ); void TableCheck( double fDistance ); // called after TSpeedPos::Update() of a position of the speed table, with its spElapsed flag from before it: - // reports the head of the train going past it, if it's a signal showing stop - void CheckSignalPassedAtDanger( TSpeedPos const &Point, bool Passedbefore ) const; + // reports the head of the train going past it, if it's a signal: to the black box, and to the test mode if it showed stop + void CheckSignalPassed( TSpeedPos const &Point, bool Passedbefore ) const; TCommandType TableUpdate( double &fVelDes, double &fDist, double &fNext, double &fAcc ); bool TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, double const Signaldistance ); bool TableUpdateEvent( double &Velocity, TCommandType &Command, TSpeedPos &Point, double &Signaldistance, int const Pointindex ); diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 1de9f0781..613ba408b 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/lightsprogrammer.h" #include "simulation/simulation.h" +#include "simulation/blackboxrecorder.h" #include "rendering/lightarray.h" #include "vehicle/Camera.h" #include "vehicle/Train.h" @@ -9186,6 +9187,7 @@ vehicle_table::erase_disabled() { simulation::Trains.purge(vehicle->name()); // remove potential entries in the light array simulation::Lights.remove( vehicle ); + simulation::Blackbox.vehicle_removed( vehicle ); /* // finally get rid of the vehicle and its record themselves // BUG: deleting the vehicle leaves dangling pointers in event->Activator and potentially elsewhere diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index eef343feb..3fdc3f6e9 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -21,6 +21,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Globals.h" #include "simulation/simulation.h" +#include "simulation/blackboxrecorder.h" #include "world/Event.h" #include "simulation/simulationtime.h" #include "vehicle/Camera.h" @@ -8806,6 +8807,7 @@ bool TTrain::Update(double const Deltatime) command_data commanddata; while (simulation::Commands.pop(commanddata, static_cast(command_target::vehicle) | id())) { + simulation::Blackbox.command(DynamicObject, commanddata.command, commanddata.action, commanddata.param1, commanddata.param2); auto lookup = m_commandhandlers.find(commanddata.command); if (lookup != m_commandhandlers.end()) diff --git a/vehicle/blackbox.cpp b/vehicle/blackbox.cpp new file mode 100644 index 000000000..8612df1bd --- /dev/null +++ b/vehicle/blackbox.cpp @@ -0,0 +1,398 @@ +/* +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/blackbox.h" + +#include +#include +#include + +namespace blackbox { + +// the file is little-endian, written the way the numbers lie in memory +static_assert(std::endian::native == std::endian::little, "the black box writer assumes a little-endian machine"); + +template +void writer::payload::raw(Type_ const Value) { + std::array bytes; + std::memcpy(bytes.data(), &Value, sizeof(Type_)); + m_bytes.append(bytes.data(), bytes.size()); +} + +void writer::payload::u8(std::uint8_t const Value) { + raw(Value); } + +void writer::payload::i8(std::int8_t const Value) { + raw(Value); } + +void writer::payload::u16(std::uint16_t const Value) { + raw(Value); } + +void writer::payload::u32(std::uint32_t const Value) { + raw(Value); } + +void writer::payload::f32(float const Value) { + raw(Value); } + +void writer::payload::f64(double const Value) { + raw(Value); } + +void writer::payload::vec3(glm::vec3 const &Value) { + f32(Value.x); + f32(Value.y); + f32(Value.z); +} + +void writer::payload::dvec3(glm::dvec3 const &Value) { + f64(Value.x); + f64(Value.y); + f64(Value.z); +} + +void writer::payload::text(std::string_view const Value) { + auto length{static_cast(Value.size())}; + while (length >= 0x80) { + u8(static_cast(length | 0x80)); + length >>= 7; + } + u8(static_cast(length)); + m_bytes.append(Value); +} + +writer::writer(std::ostream &Output) : m_output{Output} {} + +writer::payload &writer::begin() { + m_payload.clear(); + return m_payload; +} + +void writer::emit(record const Type) { + auto const &bytes{m_payload.bytes()}; + std::array head; + head[0] = static_cast(Type); + auto const length{static_cast(bytes.size())}; + std::memcpy(head.data() + 1, &length, sizeof(length)); + m_output.write(head.data(), static_cast(head.size())); + m_output.write(bytes.data(), static_cast(bytes.size())); + m_size += head.size() + bytes.size(); +} + +void writer::header() { + std::array head{}; + std::memcpy(head.data(), magic.data(), magic.size()); + std::memcpy(head.data() + magic.size(), &format_version, sizeof(format_version)); + // two bytes reserved, zero + m_output.write(head.data(), static_cast(head.size())); + m_size += head.size(); +} + +void writer::info(info_data const &Info) { + auto &out{begin()}; + out.text(Info.scenery); + out.text(Info.started); + out.u16(Info.year); + out.u16(Info.year_day); + out.f64(Info.second_of_day); + out.text(Info.start_vehicle); + out.text(Info.simulator_version); + out.f32(Info.cab_interval); + out.f32(Info.vehicles_interval); + out.f32(Info.keyframe_interval); + emit(record::info); +} + +void writer::track(track_data const &Track) { + auto &out{begin()}; + out.u32(Track.index); + out.text(Track.name); + out.u8(Track.kind); + out.u8(Track.branch); + out.u8(Track.category); + out.u32(static_cast(Track.points.size())); + for (auto const &point : Track.points) + out.vec3(point); + emit(record::track); +} + +void writer::signal(std::uint32_t const Index, std::string_view const Name, glm::dvec3 const &Position) { + auto &out{begin()}; + out.u32(Index); + out.text(Name); + out.dvec3(Position); + emit(record::signal); +} + +void writer::vehicle(vehicle_data const &Vehicle) { + auto &out{begin()}; + out.u32(Vehicle.id); + out.text(Vehicle.name); + out.text(Vehicle.type); + out.text(Vehicle.train); + out.f32(Vehicle.length); + out.f32(Vehicle.width); + out.u8(Vehicle.category); + emit(record::vehicle); +} + +void writer::vehicle_removed(double const Time, std::uint32_t const Id) { + auto &out{begin()}; + out.f64(Time); + out.u32(Id); + emit(record::vehicle_removed); +} + +void writer::cab(cab_sample const &Sample) { + auto &out{begin()}; + out.f64(Sample.time); + out.u32(Sample.vehicle); + out.dvec3(Sample.position); + out.f32(Sample.speed_kmh); + out.f32(Sample.acceleration); + out.f32(Sample.odometer_km); + out.u8(static_cast(Sample.driving)); + out.u8(Sample.flags); + out.i8(Sample.main_controller); + out.i8(Sample.scnd_controller); + out.i8(Sample.reverser); + out.f32(Sample.train_brake); + out.f32(Sample.local_brake); + out.f32(Sample.brake_cylinder); + out.f32(Sample.brake_pipe); + out.f32(Sample.motor_current); + out.f32(Sample.ai.target_kmh); + out.f32(Sample.ai.next_limit_kmh); + out.f32(Sample.ai.next_limit_distance); + out.f32(Sample.ai.acceleration); + out.f32(Sample.ai.braking_distance); + out.f32(Sample.ai.timetable_vmax_kmh); + out.f32(Sample.ai.track_vmax_kmh); + out.u32(Sample.devices); + out.f32(Sample.main_reservoir); + out.f32(Sample.voltage); + out.f32(Sample.engine_rpm); + out.u16(Sample.lights_head); + out.u16(Sample.lights_tail); + out.i8(Sample.cab); + out.u8(Sample.security); + out.f32(Sample.lateral_acceleration); + out.u8(Sample.movement); + out.u16(static_cast(Sample.light_hint_head)); + out.u16(static_cast(Sample.light_hint_tail)); + out.u16(Sample.equipped_head); + out.u16(Sample.equipped_tail); + out.u8(Sample.engine); + out.u8(Sample.brake_delay); + out.u8(Sample.radio_channel); + out.f32(Sample.driver_valve); + out.f32(Sample.feed_pipe); + out.f32(Sample.oil_pressure); + out.f32(Sample.water_temperature); + out.f32(Sample.oil_temperature); + out.f32(Sample.engine_temperature); + out.f32(Sample.speed_control_kmh); + out.f32(Sample.engine_power); + out.i8(Sample.hand_brake); + out.u8(Sample.brake_mode); + out.u8(Sample.horn); + out.f32(Sample.integrated_controller); + out.f32(Sample.clutch); + out.f32(Sample.converter_fill); + out.f32(Sample.consist_brake_cylinder); + emit(record::cab); +} + +void writer::vehicles(double const Time, bool const Keyframe, std::vector const &States) { + auto &out{begin()}; + out.f64(Time); + out.u8(Keyframe ? 1 : 0); + out.u32(static_cast(States.size())); + for (auto const &state : States) { + out.u32(state.id); + out.vec3(state.position); + out.f32(state.heading); + out.f32(state.speed_kmh); + out.u8(static_cast(state.driving)); + } + emit(record::vehicles); +} + +void writer::switch_state(double const Time, std::uint32_t const Track, std::uint8_t const State) { + auto &out{begin()}; + out.f64(Time); + out.u32(Track); + out.u8(State); + emit(record::switch_state); +} + +void writer::signal_aspect(double const Time, std::uint32_t const Signal, float const Speed1, float const Speed2, std::string_view const Command) { + auto &out{begin()}; + out.f64(Time); + out.u32(Signal); + out.f32(Speed1); + out.f32(Speed2); + out.text(Command); + emit(record::signal_aspect); +} + +void writer::command(command_entry const &Command) { + auto &out{begin()}; + out.f64(Command.time); + out.u32(Command.vehicle); + out.u8(Command.pressed ? 1 : 0); + out.f64(Command.param1); + out.f64(Command.param2); + out.text(Command.name); + emit(record::command); +} + +void writer::driver_change(double const Time, std::uint32_t const Vehicle, driver const Driving) { + auto &out{begin()}; + out.f64(Time); + out.u32(Vehicle); + out.u8(static_cast(Driving)); + emit(record::driver_change); +} + +void writer::signal_passed(signal_passing const &Passing) { + auto &out{begin()}; + out.f64(Passing.time); + out.u32(Passing.vehicle); + out.u32(Passing.signal); + out.text(Passing.event); + out.dvec3(Passing.position); + out.f32(Passing.speed1); + out.f32(Passing.speed2); + out.f32(Passing.speed_kmh); + out.f32(Passing.overrun); + out.u8(Passing.flags); + emit(record::signal_passed); +} + +void writer::user_vehicle(double const Time, std::uint32_t const Vehicle, bool const In_cab, std::int8_t const Cab) { + auto &out{begin()}; + out.f64(Time); + out.u32(Vehicle); + out.u8(In_cab ? 1 : 0); + out.i8(Cab); + emit(record::user_vehicle); +} + +void writer::stop_point(std::uint32_t const Index, std::string_view const Station, glm::dvec3 const &Position, float const Stop_at, float const Platform, + float const Heading) { + auto &out{begin()}; + out.u32(Index); + out.text(Station); + out.dvec3(Position); + out.f32(Stop_at); + out.f32(Platform); + out.f32(Heading); + emit(record::stop_point); +} + +void writer::horn_point(std::uint32_t const Index, std::string_view const Name, glm::dvec3 const &Position, float const Duration, float const Tone) { + auto &out{begin()}; + out.u32(Index); + out.text(Name); + out.dvec3(Position); + out.f32(Duration); + out.f32(Tone); + emit(record::horn_point); +} + +void writer::timetable(timetable_data const &Timetable) { + auto &out{begin()}; + out.f64(Timetable.time); + out.u32(Timetable.vehicle); + out.text(Timetable.train); + out.text(Timetable.category); + out.text(Timetable.from); + out.text(Timetable.to); + out.f32(Timetable.vmax); + out.u32(static_cast(Timetable.rows.size())); + for (auto const &row : Timetable.rows) { + out.text(row.station); + out.f32(row.km); + out.f32(row.vmax); + out.f32(row.arrival); + out.f32(row.departure); + out.u8(row.maintenance ? 1 : 0); + } + emit(record::timetable); +} + +void writer::station(station_event const &Station) { + auto &out{begin()}; + out.f64(Station.time); + out.u32(Station.vehicle); + out.u8(Station.kind); + out.u16(Station.row); + out.text(Station.station); + out.dvec3(Station.position); + out.f32(Station.distance); + out.f32(Station.stop_at); + out.f32(Station.train_length); + out.f32(Station.platform); + out.f32(Station.arrival); + out.f32(Station.departure); + emit(record::station); +} + +void writer::horn_request(double const Time, std::uint32_t const Vehicle, float const Duration, float const Tone, glm::dvec3 const &Position) { + auto &out{begin()}; + out.f64(Time); + out.u32(Vehicle); + out.f32(Duration); + out.f32(Tone); + out.dvec3(Position); + emit(record::horn_request); +} + +void writer::camera(camera_state const &Camera) { + auto &out{begin()}; + out.f64(Camera.time); + out.u8(Camera.mode); + out.u8(Camera.view); + out.u32(Camera.vehicle); + out.dvec3(Camera.position); + out.f32(Camera.heading); + out.f32(Camera.pitch); + emit(record::camera); +} + +void writer::coupling(coupling_change const &Change) { + auto &out{begin()}; + out.f64(Change.time); + out.u32(Change.vehicle); + out.u8(Change.side); + out.u32(Change.other); + out.u8(Change.other_side); + out.u8(Change.flags); + out.u8(Change.previous_flags); + out.u32(Change.previous_other); + out.dvec3(Change.position); + emit(record::coupling); +} + +void writer::clock(double const Time, std::uint16_t const Year, std::uint16_t const Year_day, double const Second_of_day) { + auto &out{begin()}; + out.f64(Time); + out.u16(Year); + out.u16(Year_day); + out.f64(Second_of_day); + emit(record::clock); +} + +void writer::end(double const Time) { + auto &out{begin()}; + out.f64(Time); + emit(record::end); +} + +} // namespace blackbox diff --git a/vehicle/blackbox.h b/vehicle/blackbox.h new file mode 100644 index 000000000..473cf677c --- /dev/null +++ b/vehicle/blackbox.h @@ -0,0 +1,429 @@ +/* +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 +#include +#include +#include +#include +#include +#include + +#include + +// 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 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, + 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 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] +}; + +// 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 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 + 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 +}; + +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{190}; +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 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 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 + 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