From a81d1495412c1b4467a64433cf8a519bc5650ad3 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Mon, 30 Nov 2026 00:15:50 +0100 Subject: [PATCH] feat: a memory cell of its own for the derailment of a vehicle report_derailment() writes the memory cell at once and only queues the events, which run later in Events.update(). A collision derails both vehicles in one frame, so when :derailed ran, __simulation.derailment already held B. The scenery can now declare the memory cell :derailed, written the same way and with the same three fields as __simulation.derailment, for the events of that one vehicle to read. Documented that __simulation.derailment keeps only the last derailment of a frame; eu07_td_off_track_end_scenery_told checks the cell of the locomotive now. --- physics/derailment.cpp | 4 +++ physics/derailment.h | 5 ++- simulation/simulation.cpp | 31 ++++++++++--------- simulation/simulation.h | 9 +++--- tests/scenarios/README.md | 14 ++++++--- .../eu07_td_off_track_end_scenery_told.json | 8 +++-- tests/unit/test_derailment.cpp | 4 +++ 7 files changed, 50 insertions(+), 25 deletions(-) diff --git a/physics/derailment.cpp b/physics/derailment.cpp index fc784b362..89ad5a0a3 100644 --- a/physics/derailment.cpp +++ b/physics/derailment.cpp @@ -306,6 +306,10 @@ std::string cell_text(notice const &Notice) { return std::format("{} {} {}", Notice.vehicle, reason_name(Notice.why), Notice.track.empty() ? std::string_view{"none"} : std::string_view{Notice.track}); } +std::string derailed_name(std::string_view const Vehicle) { + return std::string{Vehicle} + ":derailed"; +} + std::string describe(notice const &Notice) { return std::format("{} derailed ({}) {} at {:.0f} km/h", Notice.vehicle, reason_name(Notice.why), Notice.track.empty() ? std::string{"on a track without a name"} : "on track " + Notice.track, std::abs(Notice.velocity)); diff --git a/physics/derailment.h b/physics/derailment.h index 1e2851e0e..d8937754e 100644 --- a/physics/derailment.h +++ b/physics/derailment.h @@ -259,8 +259,11 @@ struct notice { std::string track; // name of the track it derailed on, empty for a track without one double velocity{0.0}; // [km/h] as it derailed }; -// the text of the memory cell __simulation.derailment: " ", "none" for a track without a name +// the text of the memory cell __simulation.derailment, and of :derailed: " ", "none" +// for a track without a name std::string cell_text(notice const &Notice); +// the name of the memory cell and of the event of the derailment of that one vehicle: ":derailed" +std::string derailed_name(std::string_view Vehicle); // for the log and the verdict of a test: " derailed () on track at km/h" std::string describe(notice const &Notice); diff --git a/simulation/simulation.cpp b/simulation/simulation.cpp index 52e1eb5d9..25aa8d9b5 100644 --- a/simulation/simulation.cpp +++ b/simulation/simulation.cpp @@ -132,20 +132,23 @@ void state_manager::report_derailment( TDynamicObject const &Vehicle, derailment::notice const &Notice ) { WriteLog( "derailment: " + derailment::describe( Notice ) ); - auto const name { std::string( derailment_name ) }; - if( auto *cell { Memory.find( name ) } ) { - cell->UpdateValues( - derailment::cell_text( Notice ), - Notice.velocity, - static_cast( static_cast( Notice.why ) ), - basic_event::flags::text | basic_event::flags::value1 | basic_event::flags::value2 ); - } - if( auto *event { Events.FindEvent( name ) } ) { - Events.AddToQuery( event, &Vehicle ); - } - if( auto *event { Events.FindEvent( Vehicle.name() + ":derailed" ) } ) { - Events.AddToQuery( event, &Vehicle ); - } + auto const text { derailment::cell_text( Notice ) }; + // the cell of the name gets the notice at once, its event runs later (Events.update()): each with a cell of its own + // for the events of one vehicle, which the derailment of another one in the same frame doesn't overwrite + auto const tell { [ & ]( std::string const &Name ) { + if( auto *cell { Memory.find( Name ) } ) { + cell->UpdateValues( + text, + Notice.velocity, + static_cast( static_cast( Notice.why ) ), + basic_event::flags::text | basic_event::flags::value1 | basic_event::flags::value2 ); + } + if( auto *launched { Events.FindEvent( Name ) } ) { + Events.AddToQuery( launched, &Vehicle ); + } + } }; + tell( std::string( derailment_name ) ); + tell( derailment::derailed_name( Vehicle.name() ) ); Blackbox.derailed( &Vehicle, Notice ); test().vehicle_derailed( Vehicle, Notice ); } diff --git a/simulation/simulation.h b/simulation/simulation.h index 9bf9cd057..6fd044f67 100644 --- a/simulation/simulation.h +++ b/simulation/simulation.h @@ -73,10 +73,11 @@ public: void export_as_text( std::string const &Scenariofile ) const; // the vehicle derailed: the log, the black box, the test mode, and the scenery which asked for it. the scenery's - // memory cell __simulation.derailment gets the notice (derailment::cell_text(), value 1 the speed [km/h], value 2 - // the code of the cause), then its events __simulation.derailment and :derailed run, the vehicle their - // activator. a scenery declares those it wants: nothing is made at the start, where a node made would take a - // draw of the random numbers and change every run + // memory cells __simulation.derailment and :derailed get the notice (derailment::cell_text(), value 1 the + // speed [km/h], value 2 the code of the cause) at once, then its events of those names run, the vehicle their + // activator. __simulation.derailment holds the last derailment of a frame, its event runs once, for the first: + // an event of one vehicle reads the cell of that vehicle. a scenery declares those it wants: nothing is made at the + // start, where a node made would take a draw of the random numbers and change every run void report_derailment( TDynamicObject const &Vehicle, derailment::notice const &Notice ); // name of the memory cell and of the event of a derailment of any vehicle diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 7d9ec9e52..a62ca84c8 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -132,16 +132,22 @@ it needs, nothing is made for it otherwise: ``` node -1 0 __simulation.derailment memcell 0 0 0 none 0 0 none endmemcell +node -1 0 ep07-424:derailed memcell 0 0 0 none 0 0 none endmemcell event __simulation.derailment multiple 0 none stop_the_line endevent -event ep07-424:derailed UpdateValues 0 __test.result derailed 0 * endevent +event ep07-424:derailed multiple 0 ep07-424:derailed send_the_crane condition memcompare * * 2 endevent ``` - the memory cell `__simulation.derailment` gets, before the events run: the text ` ` (`none` for a track without a name), value 1 the speed [km/h] as it derailed (in a collision: after the hit), value 2 the code of the cause. A `memcompare` can test the vehicle by the start of the text (`ep07-424*`) and the cause by value 2; +- the memory cell `:derailed`, if the scenery declares it, gets the same of that one vehicle, the same way (above, `send_the_crane` runs if the EP07 overturned: code 2); - the event `__simulation.derailment` runs for a derailment of any vehicle, `:derailed` for that vehicle - (like `:busy`); the vehicle is their activator (`whois`). Two vehicles derailing in the same frame run - the first one once, the cell holds the second. + (like `:busy`); the vehicle is their activator (`whois`). + +The cells are written at once and the events run later, in the update of the events. Two vehicles derailing in the +same frame (a collision derails both at once) run the event `__simulation.derailment` once, for the first one, and the +cell `__simulation.derailment` holds the last one: it keeps only the last derailment of a frame. An event of one +vehicle reads the cell of that vehicle, `:derailed`, not `__simulation.derailment`. | code | cause | | | --- | --- | --- | @@ -441,7 +447,7 @@ on at 1:5). `eu07_td_off_track_end_scenery_told` is `eu07_td_off_track_end` with the memory cell and the events of a derailment declared in `vars.track_nodes` (see *A derailment, for the scenery*): the event `:derailed` of the locomotive -checks the cell and marks a cell of the case, which the log shows. `eu07_td_buckle_derails_ends_test` is `eu07_td_buckle_derails` with +checks the cell `:derailed` of the locomotive and marks a cell of the case, which the log shows with both cells. `eu07_td_buckle_derails_ends_test` is `eu07_td_buckle_derails` with `test.fail_on_derailment yes`: the derailment of the EU07 under test ends the run with `derailment`. The runner's summary tells the derailment of the train under test after the case, from the verdict line. diff --git a/tests/scenarios/cases/eu07_td_off_track_end_scenery_told.json b/tests/scenarios/cases/eu07_td_off_track_end_scenery_told.json index 58e804567..ded101583 100644 --- a/tests/scenarios/cases/eu07_td_off_track_end_scenery_told.json +++ b/tests/scenarios/cases/eu07_td_off_track_end_scenery_told.json @@ -1,5 +1,5 @@ { - "description": "The scenery is told of a derailment: eu07_td_off_track_end (an EU07 with three coaches and no driver runs off the end of autotest_stub at 60 km/h) with the memory cell __simulation.derailment, the event of the same name (logvalues of the cell) and the event :derailed of the locomotive declared in the fragment. The cell holds the vehicle, the cause, the track and the speed and the code of the cause when the events run; the event of the locomotive checks the cell (its name, end of track: code 3) and marks a cell of the case. test.fail_on_derailment yes doesn't end the test: the derailed train isn't the one under test (the EU07 standing on track 1), so the run ends on the timeout as eu07_td_off_track_end does.", + "description": "The scenery is told of a derailment: eu07_td_off_track_end (an EU07 with three coaches and no driver runs off the end of autotest_stub at 60 km/h) with the memory cell __simulation.derailment, the event of the same name (logvalues of the cell) and the memory cell and the event :derailed of the locomotive declared in the fragment. The cells hold the vehicle, the cause, the track and the speed and the code of the cause when the events run; the event of the locomotive checks its own cell (its name, end of track: code 3), logs it and marks a cell of the case. test.fail_on_derailment yes doesn't end the test: the derailed train isn't the one under test (the EU07 standing on track 1), so the run ends on the timeout as eu07_td_off_track_end does.", "base_scenery": "td.scn", "time": "10:34", "fragment": "td_off_track.scn", @@ -7,7 +7,7 @@ "track": "autotest_stub", "offset": "100", "velocity": "60", - "track_nodes": "node -1 0 autotest_stub track normal 150.0 1.435 0.15 25.0 20 0 flat vis\n rail_screw_used1 6 1435mm/tpbps-new2 0.2 0.5 1.1\n-150.0 0.2 600.0 0.0 //point 1\n0.0 0.0 0.0 //control vector 1\n0.0 0.0 0.0 //control vector 2\n-150.0 0.2 750.0 0.0 //point 2\n0\nendtrack\nnode -1 0 __simulation.derailment memcell 0 0 0 none 0 0 none endmemcell\nnode -1 0 autotest_seen memcell 0 0 0 none 0 0 none endmemcell\nevent __simulation.derailment logvalues 0 __simulation.derailment endevent\nevent {{vehicle}}_off:derailed multiple 0 __simulation.derailment autotest_derail_seen autotest_derail_log condition memcompare {{vehicle}}_off* * 3 endevent\nevent autotest_derail_seen UpdateValues 0 autotest_seen end_of_track_seen 1 * endevent\nevent autotest_derail_log logvalues 0 autotest_seen endevent", + "track_nodes": "node -1 0 autotest_stub track normal 150.0 1.435 0.15 25.0 20 0 flat vis\n rail_screw_used1 6 1435mm/tpbps-new2 0.2 0.5 1.1\n-150.0 0.2 600.0 0.0 //point 1\n0.0 0.0 0.0 //control vector 1\n0.0 0.0 0.0 //control vector 2\n-150.0 0.2 750.0 0.0 //point 2\n0\nendtrack\nnode -1 0 __simulation.derailment memcell 0 0 0 none 0 0 none endmemcell\nnode -1 0 {{vehicle}}_off:derailed memcell 0 0 0 none 0 0 none endmemcell\nnode -1 0 autotest_seen memcell 0 0 0 none 0 0 none endmemcell\nevent __simulation.derailment logvalues 0 __simulation.derailment endevent\nevent {{vehicle}}_off:derailed multiple 0 {{vehicle}}_off:derailed autotest_derail_seen autotest_derail_log autotest_derail_own_log condition memcompare {{vehicle}}_off* * 3 endevent\nevent autotest_derail_seen UpdateValues 0 autotest_seen end_of_track_seen 1 * endevent\nevent autotest_derail_log logvalues 0 autotest_seen endevent\nevent autotest_derail_own_log logvalues 0 {{vehicle}}_off:derailed endevent", "consist": [ "node -1 0 {{vehicle}}_off dynamic pkp\\4e_v1 4e-004 4e 0 nobody 3 0 enddynamic", "node -1 0 {{vehicle}}_off_w1 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608068-6 BDHPUMN_TAPICERKA_1 0 nobody 115 0 enddynamic", @@ -34,6 +34,10 @@ "type": "log_contains", "pattern": "Memcell __simulation\\.derailment: \\[autotest_\\S+_off end_of_track autotest_stub\\] \\[(59|60)\\.[0-9]+\\] \\[3\\.00\\]" }, + { + "type": "log_contains", + "pattern": "Memcell autotest_\\S+_off:derailed: \\[autotest_\\S+_off end_of_track autotest_stub\\] \\[(59|60)\\.[0-9]+\\] \\[3\\.00\\]" + }, { "type": "log_contains", "pattern": "Memcell autotest_seen: \\[end_of_track_seen\\] \\[1\\.00\\]" diff --git a/tests/unit/test_derailment.cpp b/tests/unit/test_derailment.cpp index c44aacaf4..9d68ffabd 100644 --- a/tests/unit/test_derailment.cpp +++ b/tests/unit/test_derailment.cpp @@ -376,6 +376,10 @@ TEST_CASE("derailment: what the scenery and the test mode are told") { notice.track.clear(); CHECK(dr::cell_text(notice) == "ep07-424 overturned none"); } + SUBCASE("the memory cell and the event of one vehicle: :derailed") { + CHECK(dr::derailed_name("ep07-424") == "ep07-424:derailed"); + CHECK(dr::derailed_name("autotest_x_w1") == "autotest_x_w1:derailed"); + } SUBCASE("the log and the verdict of a test") { dr::notice notice{"autotest_x", dr::reason::buckle, "tdo_n222", 46.4}; CHECK(dr::describe(notice) == "autotest_x derailed (buckle) on track tdo_n222 at 46 km/h");