diff --git a/CLAUDE.md b/CLAUDE.md index c1709be8d..283058cbe 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -27,11 +27,16 @@ Where no GCC is at hand, read the new code for what MSVC lets through and GCC do The same behaviour needs portable code. Don't rely on the order arguments are evaluated in, on the signedness of `char`, on the size of `wchar_t`, or on `long` being 32 bits. -## SonarCloud: every remark on the changed code gets fixed +## SonarCloud: the PR passes the quality gate -SonarCloud (`sonar.yml`, `sonar-project.properties`) analyses each PR. A PR counts as finished only when every remark Sonar raises on -the code it changes is fixed. That means the code smells too, not just the bugs, the vulnerabilities and the quality gate. The analysis -runs on the GCC build, so a PR that fails to build with GCC gets no analysis at all. +SonarCloud (`sonar.yml`, `sonar-project.properties`) analyses each PR. A PR has to pass Sonar's quality gate. The gate rates the new +code: reliability, security and maintainability have to be A, duplication is at most 3% and the security hotspots are reviewed. +Any bug or vulnerability on the new code lowers its rating below A, so each one gets fixed (or gets a NOSONAR when it is a false +positive). Code smells count only together, through the maintainability rating, so a single one rarely fails the gate, but many in a +large PR can. A hotspot is reviewed by marking it in SonarCloud as safe or fixed, which is the user's step there, not a reason to +rewrite the code. A remark that doesn't fail the gate doesn't have to be fixed. Leave large restructurings that only answer such a +remark (splitting a class or a file, rewriting a working algorithm) out of the PR. The analysis runs on the GCC build, so a PR that +fails to build with GCC gets no analysis at all, and so it doesn't pass the gate. Fixing a remark must not change what the code does: @@ -41,7 +46,7 @@ Fixing a remark must not change what the code does: - `// NOSONAR: ` is only for a real false positive, or where a fix would change behaviour. Use it rarely, always with the reason. -Write new code so that it doesn't raise the rules that keep coming up: +To keep the gate passing, write new code so that it doesn't raise the rules that keep coming up: - **S134** (nesting deeper than 3 `if` / `for` / `while` / `switch` / `try`) and **S3776** (cognitive complexity over 25): - use early `continue` / `return`; diff --git a/CMakeLists.txt b/CMakeLists.txt index 05e24b42d..7137ab39a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -160,6 +160,7 @@ set(SOURCES "world/station.cpp" "application/application.cpp" "simulation/simulationtime.cpp" +"simulation/simulationtest.cpp" "scene/sceneeditor.cpp" "rendering/screenshot.cpp" "input/driverkeyboardinput.cpp" @@ -453,7 +454,7 @@ endif() add_library(eu07_settings INTERFACE) # the parts of the simulation the game shares with the unit tests: the vehicle physics, the vehicle -# definition parser and the helpers they use. built once, and tested as the game uses them +# definition parser, the helpers they use and the rules of the test mode. built once, and tested as the game uses them set(CORE_SOURCES "McZapkie/friction.cpp" "McZapkie/hamulce.cpp" @@ -462,9 +463,11 @@ set(CORE_SOURCES "utilities/parser.cpp" "utilities/utilities.cpp" "scripting/ladderlogic.cpp" +"simulation/simulationtestrules.cpp" "vehicle/enginestart.cpp" "vehicle/handover.cpp" "vehicle/horn.cpp" +"vehicle/linebreaker.cpp" "vehicle/smoke.cpp" ) add_library(eu07_core STATIC ${CORE_SOURCES}) @@ -739,4 +742,4 @@ if(WITH_TESTS) # Visual Studio builds rerun them with the next change of the game) set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY LINK_DEPENDS $ ${UNIT_TEST_DATA} "${CMAKE_CURRENT_SOURCE_DIR}/tests/unit/run_tests.cmake") -endif() \ No newline at end of file +endif() diff --git a/EU07.cpp b/EU07.cpp index e03618297..20f24aa5a 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -19,8 +19,14 @@ Stele, firleju, szociu, hunter, ZiomalCl, OLI_EU and others #include "stdafx.h" #include "application/application.h" +#include "simulation/simulationtest.h" +#include "utilities/Globals.h" #include "utilities/Logs.h" #include +#include +#include +#include +#include #ifdef WITHDUMPGEN #ifdef _WIN32 #include @@ -70,7 +76,9 @@ LONG WINAPI CrashHandler(EXCEPTION_POINTERS *ExceptionInfo) MINIDUMP_TYPE dumpType = MINIDUMP_TYPE(MiniDumpWithFullMemory | MiniDumpWithHandleData | MiniDumpWithThreadInfo | MiniDumpWithUnloadedModules | MiniDumpWithIndirectlyReferencedMemory | MiniDumpWithFullMemoryInfo | MiniDumpWithTokenInformation); - MessageBoxA(nullptr, "Simulator crash occured :(\n", "Simulator crashed :(", MB_ICONERROR); + // in test mode nobody is there to close the box, and the run would wait for it until killed + if (false == Global.test.enabled) + MessageBoxA(nullptr, "Simulator crash occured :(\n", "Simulator crashed :(", MB_ICONERROR); MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, dumpType, &dumpInfo, nullptr, nullptr); CloseHandle(hFile); @@ -81,6 +89,55 @@ LONG WINAPI CrashHandler(EXCEPTION_POINTERS *ExceptionInfo) #endif +namespace +{ + +// the log is written by a thread of its own, which the closing of the application stops and waits for. on the ways +// out that skip the closing (a failed start, an exception) what was logged last would be lost with the process +void finish_log() +{ + StopLogService(); + if (auto const logservice{Global.threads.find("LogService")}; + logservice != Global.threads.end() && logservice->second.joinable() && logservice->second.get_id() != std::this_thread::get_id()) + logservice->second.join(); +} + +// ends a test run with its verdict on the standard output and in the exit code. a test the simulator didn't end +// (a failed start, an exception) ends with the error and Reason +[[noreturn]] void end_test_run(std::string_view const Reason) +{ + simulation::test().conclude(simulation::test_result::error, Reason); + finish_log(); + fflush(stdout); + fflush(stderr); + // skip destructors, there are ordering errors which causes segfaults + std::_Exit(simulation::test().exit_code()); +} + +// a test run (--test): whatever stops it, it ends with its verdict. any other run never gets here and ends as +// it always has +[[noreturn]] void run_test(int argc, char *argv[]) +{ + // for what doesn't reach the catch below: an exception leaving a noexcept function, or on Linux one leaving + // a thread (on Windows the handler is the main thread's only) + std::set_terminate([] { end_test_run(simulation::describe_exception(std::current_exception())); }); + try + { + if (Application.init(argc, argv) == 0) + { + Application.run(); + Application.exit(); + } + } + catch (...) + { + end_test_run(simulation::describe_exception(std::current_exception())); + } + end_test_run("the simulator stopped before the test ended"); +} + +} // namespace + int main(int argc, char *argv[]) { #ifdef WITHDUMPGEN @@ -130,6 +187,11 @@ int main(int argc, char *argv[]) fflush(stderr); std::_Exit(result); } + // unattended test run of a scenery, which says its result in the exit code (eu07/maszyna#541) + else if (simulation::parse_test_command_line(std::vector(argv, argv + argc)).enabled) + { + run_test(argc, argv); + } else { try diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 1d9ca414b..074cffe79 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -261,6 +261,22 @@ enum class range_t /// Affects the entire consist. consist }; +/// State of the door closure signalling (SOD) of a vehicle or a consist, ordered from the least to the most +/// severe: the state of a consist is the most severe state of its vehicles. +enum class door_closure_t +{ + /// No doors worked by the crew: the vehicle doesn't take part in the signalling. + none, + /// All doors closed and locked: they can't be opened. + locked, + /// All doors closed, but released: a permit is given, or no lock holds them shut. + released, + /// A door is open, or not fully closed yet. + open +}; +/// Whether the door closure signalling lamp is lit: steadily for locked doors, in the lit phase of the +/// blinking for released ones, never with a door open or with no doors signalled. +bool door_closure_lamp(door_closure_t const State, bool const BlinkLit); /// Possible settings for an enable/disable input pair (mutually exclusive). enum class operation_t { @@ -1439,6 +1455,7 @@ class TMoverParameters float auto_velocity{-1.f}; // automatic door closure velocity threshold bool auto_include_remote{false}; // automatic door closure applies also to remote control bool permit_needed{false}; + bool closure_signal{false}; // the door closure lamp (SOD) of the cab signals the doors of the whole consist in three states std::vector permit_presets; // permit presets selectable with preset switch float voltage{0.f}; // power type required for door movement // ld inputs @@ -2602,6 +2619,10 @@ class TMoverParameters bool LockDoors(bool const State, range_t const Notify = range_t::consist); bool signal_departure(bool const State, range_t const Notify = range_t::consist); // toggles departure warning void update_doors(double const Deltatime); // door controller update + door_closure_t door_closure() const; // state of the door closure signalling of this vehicle + // whether the door closure lamp in the cab of this vehicle is lit: with DoorClosureSignal, for the given state of the + // consist; without it, for the lock of the vehicle's own doors, as the lamp always worked + bool door_closure_lamp_lit(door_closure_t const Consist, bool const BlinkLit) const; /* funkcje dla samochodow*/ bool ChangeOffsetH(double DeltaOffset); diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index a09e15192..fac42ed7a 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -8706,6 +8706,48 @@ bool TMoverParameters::signal_departure(bool const State, range_t const Notify) return true; } +// state of the door closure signalling (SOD) of this vehicle: open while a door isn't fully closed, released while +// the closed doors can be opened (a permit given, or, for doors that need none, no lock engaged), locked otherwise. +// doors the crew doesn't close (autonomous) aren't signalled, the same as for the door lights of the consist +door_closure_t TMoverParameters::door_closure() const +{ + if (Doors.range == 0.f || Doors.close_control == control_t::autonomous) + { + return door_closure_t::none; + } + if (std::ranges::any_of(Doors.instances, [](basic_door const &Door) { return false == Door.is_closed; })) + { + return door_closure_t::open; + } + if (Doors.is_locked) + { + return door_closure_t::locked; + } + if (Doors.permit_needed && std::ranges::none_of(Doors.instances, [](basic_door const &Door) { return Door.open_permit; })) + { + return door_closure_t::locked; + } + return door_closure_t::released; +} + +bool TMoverParameters::door_closure_lamp_lit(door_closure_t const Consist, bool const BlinkLit) const +{ + return Doors.closure_signal ? door_closure_lamp(Consist, BlinkLit) : Doors.is_locked; +} + +bool door_closure_lamp(door_closure_t const State, bool const BlinkLit) +{ + switch (State) + { + case door_closure_t::locked: + return true; + case door_closure_t::released: + return BlinkLit; + default: + return false; + } +} + // automatic door controller update void TMoverParameters::update_doors(double const Deltatime) { @@ -10583,6 +10625,8 @@ void TMoverParameters::LoadFIZ_Doors(std::string const &line) extract_value(Doors.auto_include_remote, "DoorAutoCloseRemote", line, ""); // operation permit extract_value(Doors.permit_needed, "DoorNeedPermit", line, ""); + // the door closure lamp (SOD) of the cab signals the doors of the whole consist + extract_value(Doors.closure_signal, "DoorClosureSignal", line, ""); { auto permitpresets = Split(extract_value("DoorPermitList", line), '|'); for (auto const &permit : permitpresets) diff --git a/application/application.cpp b/application/application.cpp index 9b7b82853..75ecde778 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -30,8 +30,10 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Timer.h" #include "utilities/dictionary.h" #include "version_info.h" +#include "simulation/simulationtest.h" #include #include +#include #include "utilities/translation.h" #if WITH_DISCORD_RPC @@ -320,6 +322,9 @@ int eu07_application::init(int Argc, char *Argv[]) { int result{0}; + // a test run is known before anything is logged: whoever started it reads its result on the standard output, + // which the log isn't written to then (eu07/maszyna#541) + Global.test.enabled = simulation::parse_test_command_line(std::vector(Argv, Argv + Argc)).enabled; // start logging service std::thread sLoggingService(LogService); Global.threads.emplace("LogService", std::move(sLoggingService)); @@ -378,7 +383,7 @@ int eu07_application::init(int Argc, char *Argv[]) return result; } - if (crashreport_is_pending()) + if (crashreport_is_pending() && false == Global.test.enabled) { // run crashgui as early as possible if ((result = run_crashgui()) != 0) { @@ -428,8 +433,12 @@ int eu07_application::init(int Argc, char *Argv[]) #if WITH_DISCORD_RPC // Run DiscordRPC service - std::thread sDiscordRPC(&eu07_application::DiscordRPCService, this); - Global.threads.emplace("DiscordRPC", std::move(sDiscordRPC)); + // not for a test run: it would show the test scenery as the player's presence, and hold up the closing + if (false == Global.test.enabled) + { + std::thread sDiscordRPC(&eu07_application::DiscordRPCService, this); + Global.threads.emplace("DiscordRPC", std::move(sDiscordRPC)); + } #endif if (!init_network()) @@ -951,14 +960,17 @@ GLFWwindow *eu07_application::window(int const Windowindex, bool visible, int wi } // for index -1 create a new child window - auto const *vmode{glfwGetVideoMode(monitor ? monitor : glfwGetPrimaryMonitor())}; - - // match requested video mode to current to allow for - // fullwindow creation when resolution is the same - glfwWindowHint(GLFW_RED_BITS, vmode->redBits); - glfwWindowHint(GLFW_GREEN_BITS, vmode->greenBits); - glfwWindowHint(GLFW_BLUE_BITS, vmode->blueBits); - glfwWindowHint(GLFW_REFRESH_RATE, vmode->refreshRate); + auto *videomonitor{monitor ? monitor : glfwGetPrimaryMonitor()}; + // a machine without a display (e.g. a test runner) has no monitor to match + if (auto const *vmode{videomonitor ? glfwGetVideoMode(videomonitor) : nullptr}) + { + // match requested video mode to current to allow for + // fullwindow creation when resolution is the same + glfwWindowHint(GLFW_RED_BITS, vmode->redBits); + glfwWindowHint(GLFW_GREEN_BITS, vmode->greenBits); + glfwWindowHint(GLFW_BLUE_BITS, vmode->blueBits); + glfwWindowHint(GLFW_REFRESH_RATE, vmode->refreshRate); + } glfwWindowHint(GLFW_VISIBLE, visible); @@ -1038,6 +1050,13 @@ void eu07_application::init_debug() void eu07_application::init_console() { #ifdef _WIN32 + if (Global.test.enabled) + { + // a test is started by whoever waits for its result, often reading its output: no console of its own + // (showsystemconsole) pops up and takes stdout away from them, and a console it was started from isn't hidden + return; + } + HWND consoleWnd = ::GetConsoleWindow(); const bool hadConsole = consoleWnd != nullptr; @@ -1138,9 +1157,30 @@ int eu07_application::init_settings(int Argc, char *Argv[]) { Global.asVersion = VERSION_INFO; + // a test run is known before the rest of the command line is read (Global.test.enabled, see init()), so a + // command line it can't take ends with the error code of the test, not with 0, which would pass it + auto const testcommandline{simulation::parse_test_command_line(std::vector(Argv, Argv + Argc))}; + if (false == testcommandline.error.empty()) + { + simulation::test().conclude(simulation::test_result::error, testcommandline.error); + return -1; + } + if (testcommandline.timeout.has_value()) + { + Global.test.timeout = *testcommandline.timeout; + Global.test.timeout_from_command_line = true; + } + if (testcommandline.wall_timeout.has_value()) + { + Global.test.wall_timeout = testcommandline.wall_timeout; + Global.test.wall_timeout_from_command_line = true; + } + fs::path iniPath = user_config_path("eu07.ini"); - if (!iniPath.empty() && fs::exists(iniPath)) + // a test reads the settings of the game folder only (and those given with -ini), not the player's own + // (%APPDATA%/MaSzyna, ~/.config/MaSzyna), so it runs the same for whoever runs it + if (false == Global.test.enabled && !iniPath.empty() && fs::exists(iniPath)) { Global.LoadIniFile(iniPath.string().c_str()); } @@ -1195,18 +1235,51 @@ int eu07_application::init_settings(int Argc, char *Argv[]) WriteLog(std::format("seed given on the command line: {} -> {}", Argv[i], Global.random_seed)); } } + else if (token == "--test") + { + // integration test: the scenery runs unattended and the exit code is the result (eu07/maszyna#541), + // taken from the command line above + } + else if (token == "--test-timeout" || token == "--test-wall-timeout") + { + // taken from the command line above, with its value + ++i; + } + else if (token == "-ini") + { + // extra settings file read on top of eu07.ini, so a run can override a few keys + // (window size, physicslog...) without touching the user's own config + if (i + 1 < Argc) + { + Global.LoadIniFile(Argv[++i]); + } + } else { - std::cout << "usage: " << std::string(Argv[0]) << " [-s sceneryfilepath]" + // a test's standard output carries its result only + (Global.test.enabled ? std::cerr : std::cout) << "usage: " << std::string(Argv[0]) << " [-s sceneryfilepath]" << " [-v vehiclename]" << " [-edit [sceneryfilepath]]" - << " [-seed number|word]" << std::endl; + << " [-seed number|word]" + << " [-ini settingsfile]" + << " [--test [--test-timeout seconds] [--test-wall-timeout seconds]]" << std::endl; + simulation::test().conclude(simulation::test_result::error, std::format("unknown command line argument {}", token)); return -1; } } if (editwithoutscenery && false == sceneryfromcommandline) Global.SceneryFile.clear(); + if (Global.test.enabled) + { + if (false == sceneryfromcommandline || Global.SceneryFile.empty()) + { + simulation::test().conclude(simulation::test_result::error, "--test needs a scenery given with -s"); + return -1; + } + simulation::apply_test_mode_settings(); + } + return 0; } @@ -1280,6 +1353,12 @@ int eu07_application::init_glfw() { glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); } + else if (Global.test.enabled) + { + // the null renderer draws nothing, but parts of the ui make textures with gl calls of their own. + // any context will do for them, so none is asked for in particular: a machine without a graphics + // driver (a test runner) has only the basic one of the system + } else { if (!Global.LegacyRenderer) @@ -1331,7 +1410,8 @@ int eu07_application::init_glfw() Global.bFullScreen = true; } - auto *mainwindow = window(-1, true, Global.window_size.x, Global.window_size.y, Global.bFullScreen ? monitor : nullptr, true, false); + // test mode keeps its window hidden: glfw, the ui and the input still need one, but nothing is shown in it + auto *mainwindow = window(-1, false == Global.test.enabled, Global.window_size.x, Global.window_size.y, Global.bFullScreen ? monitor : nullptr, true, false); if (mainwindow == nullptr) { @@ -1355,7 +1435,9 @@ int eu07_application::init_glfw() #ifdef _WIN32 // setup wrapper for base glfw window proc, to handle copydata messages Hwnd = glfwGetWin32Window(mainwindow); - BaseWindowProc = (WNDPROC)::SetWindowLongPtr(Hwnd, GWLP_WNDPROC, (LONG_PTR)WndProc); + // commands sent by other programs are input from outside, which a test doesn't take + if (false == Global.test.enabled) + BaseWindowProc = (WNDPROC)::SetWindowLongPtr(Hwnd, GWLP_WNDPROC, (LONG_PTR)WndProc); // switch off the topmost flag ::SetWindowPos(Hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); #endif @@ -1369,13 +1451,18 @@ void eu07_application::init_callbacks() auto *window{m_windows.front()}; glfwSetWindowSizeCallback(window, window_resize_callback); glfwSetFramebufferSizeCallback(window, framebuffer_resize_callback); + glfwSetWindowFocusCallback(window, focus_callback); + glfwSetCursorEnterCallback(window, cursor_enter_callback); + if (Global.test.enabled) + { + // a test takes no input: keys and clicks sent to its hidden window would change the run + return; + } glfwSetCursorPosCallback(window, cursor_pos_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetKeyCallback(window, key_callback); glfwSetScrollCallback(window, scroll_callback); glfwSetCharCallback(window, char_callback); - glfwSetWindowFocusCallback(window, focus_callback); - glfwSetCursorEnterCallback(window, cursor_enter_callback); } int eu07_application::init_ogl() @@ -1418,6 +1505,11 @@ int eu07_application::init_gfx() // default render path GfxRenderer = gfx_renderer_factory::get_instance()->create("modern"); } + else if (Global.GfxRenderer == "null") + { + // draws nothing and loads no textures (test mode) + GfxRenderer = gfx_renderer_factory::get_instance()->create("null"); + } else if (Global.GfxRenderer == "experimental") { GfxRenderer = gfx_renderer_factory::get_instance()->create(Global.GfxRenderer); diff --git a/application/drivermode.cpp b/application/drivermode.cpp index b55d09c2b..deb906a4a 100644 --- a/application/drivermode.cpp +++ b/application/drivermode.cpp @@ -17,6 +17,7 @@ http://mozilla.org/MPL/2.0/. #include "simulation/simulation.h" #include "simulation/simulationtime.h" #include "simulation/simulationenvironment.h" +#include "simulation/simulationtest.h" #include "scene/scene.h" #include "rendering/lightarray.h" #include "rendering/particles.h" @@ -305,6 +306,11 @@ bool driver_mode::update() simulation::Events.update(); simulation::Region->update_events(); simulation::Lights.update(); + + if (Global.test.enabled && simulation::is_ready) + { + simulation::test().update(deltatime); + } } // render time routines follow: @@ -478,6 +484,13 @@ void driver_mode::enter() Timer::ResetTimers(); + if (Global.test.enabled) + { + // the test runs as fast as it can compute, in steps of fixed length, so that runs on different machines agree + Timer::set_fixed_step(simulation::test_manager::frame_step); + simulation::test().start(); + } + set_picking(!Global.captureonstart); m_editorpending = Global.editor_session; diff --git a/application/scenarioloadermode.cpp b/application/scenarioloadermode.cpp index f8f06ac93..20fa46d3d 100644 --- a/application/scenarioloadermode.cpp +++ b/application/scenarioloadermode.cpp @@ -14,6 +14,7 @@ http://mozilla.org/MPL/2.0/. #include "simulation/simulation.h" #include "simulation/simulationtime.h" #include "simulation/simulationenvironment.h" +#include "simulation/simulationtest.h" #include "application/application.h" #include "application/scenarioloaderuilayer.h" #include "rendering/renderer.h" @@ -58,6 +59,10 @@ bool scenarioloader_mode::update() { catch (std::runtime_error const &e) { ErrorLog(std::format("Bad init: scenario \"{}\" couldn't be opened ({})", Global.SceneryFile, e.what())); state.reset(); + if (Global.test.enabled) { + simulation::test().finish(simulation::test_result::error, "scenario couldn't be opened"); + return true; + } Global.SceneryFile.clear(); Application.pop_mode(); Application.push_mode(eu07_application::mode::launcher); @@ -73,6 +78,11 @@ bool scenarioloader_mode::update() { // back to the launcher as when the scenario can't be opened, not on to the driver mode with the scenery half loaded ErrorLog( "Bad init: scenario loading failed" ); state.reset(); + if( Global.test.enabled ) { + // there's nothing to test; the exit code says the run went wrong + simulation::test().finish( simulation::test_result::error, "scenario loading failed" ); + return true; + } Global.SceneryFile.clear(); Application.pop_mode(); Application.push_mode(eu07_application::mode::launcher); diff --git a/application/uilayer.cpp b/application/uilayer.cpp index 5443d158c..855bfbadc 100644 --- a/application/uilayer.cpp +++ b/application/uilayer.cpp @@ -288,6 +288,11 @@ bool ui_layer::init(GLFWwindow *Window) IMGUI_CHECKVERSION(); ImGui::CreateContext(); m_imguiio = &ImGui::GetIO(); + if (Global.test.enabled) + { + // a test leaves the player's window layout alone (imgui.ini in the game folder), runs in parallel included + m_imguiio->IniFilename = nullptr; + } ImGui::GetCurrentContext()->ConfigNavWindowingKeyNext = ImGuiKey_None; ImGui::GetCurrentContext()->ConfigNavWindowingKeyPrev = ImGuiKey_None; // imgui.ini is read with the first frame, which may be of another mode than the editor diff --git a/ci_shadervalidator/Globals.h b/ci_shadervalidator/Globals.h index 16f85e802..e8f548dc3 100644 --- a/ci_shadervalidator/Globals.h +++ b/ci_shadervalidator/Globals.h @@ -8,6 +8,7 @@ struct _global bool gfx_extraeffects; bool gfx_shadergamma; bool gfx_usegles; + int iMultisampling; }; extern _global Global; diff --git a/ci_shadervalidator/main.cpp b/ci_shadervalidator/main.cpp index 7a4b53aa2..65ad54b2a 100644 --- a/ci_shadervalidator/main.cpp +++ b/ci_shadervalidator/main.cpp @@ -46,6 +46,8 @@ bool check_shader_allopts(std::string filename) Global.gfx_skippipeline = false; Global.gfx_extraeffects = true; Global.gfx_shadergamma = true; + // the default of the simulator, which compiles the alpha to coverage branches of the shaders + Global.iMultisampling = 2; if (!check_shader(filename)) suc = false; @@ -56,6 +58,7 @@ bool check_shader_allopts(std::string filename) Global.gfx_skippipeline = true; Global.gfx_extraeffects = false; Global.gfx_shadergamma = false; + Global.iMultisampling = 0; if (!check_shader(filename)) suc = false; diff --git a/gl/glsl_common.cpp b/gl/glsl_common.cpp index 26c310000..e621fdc6b 100644 --- a/gl/glsl_common.cpp +++ b/gl/glsl_common.cpp @@ -11,6 +11,7 @@ void gl::glsl_common_setup() "#define MOTIONBLUR_ENABLED " + std::to_string((int)Global.gfx_postfx_motionblur_enabled) + "\n" + "#define POSTFX_ENABLED " + std::to_string((int)!Global.gfx_skippipeline) + "\n" + "#define EXTRAEFFECTS_ENABLED " + std::to_string((int)Global.gfx_extraeffects) + "\n" + + "#define ALPHA_TO_COVERAGE_ENABLED " + std::to_string((int)(Global.iMultisampling > 0)) + "\n" + "#define MAX_LIGHTS " + std::to_string(MAX_LIGHTS) + "U\n" + "#define MAX_CASCADES " + std::to_string(MAX_CASCADES) + "U\n" + "#define MAX_PARAMS " + std::to_string(MAX_PARAMS) + "U\n" + @@ -64,6 +65,7 @@ void gl::glsl_common_setup() float fog_density; float alpha_mult; float shadow_tone; + float alpha_to_coverage; }; layout (std140) uniform scene_ubo @@ -110,5 +112,41 @@ void gl::glsl_common_setup() } #endif + #ifdef STAGE_FRAGMENT + // alpha tested surfaces (positive opacity, the opaque pass) get the coverage of their alpha + // test edge from GL_SAMPLE_ALPHA_TO_COVERAGE, which the renderer turns on for them with + // multisampling; otherwise the alpha test keeps or drops whole pixels and its edges stay + // jagged whatever the multisampling. the alpha is sharpened to go from 0 to 1 over about + // a pixel around the test threshold, so the edge keeps the place and sharpness the alpha + // test gave it. computed before any discard, which leaves the derivatives undefined. + // alpha_to_coverage of model_ubo tells whether alpha to coverage is on for this draw; where + // it isn't (reflections, previews) the shaders keep the plain alpha test + float alpha_coverage(float alpha) + { + #if ALPHA_TO_COVERAGE_ENABLED + if (opacity > 0.0 && alpha_to_coverage > 0.0) + { + // a flat alpha has no edge in the pixel and gets the plain test, so an alpha equal + // to the threshold stays fully covered + float fw = fwidth(alpha); + return (fw > 0.0 ? clamp((alpha - opacity) / fw + 0.5, 0.0, 1.0) : step(opacity, alpha)); + } + #endif + return alpha; + } + + // true for the fragments the alpha test drops: below the opacity threshold in the opaque + // pass, at or above it (negative opacity) in the translucent pass. with alpha to coverage + // only the fragments which cover no sample at all + bool alpha_test_fail(float alpha, float coverage) + { + #if ALPHA_TO_COVERAGE_ENABLED + if (opacity > 0.0 && alpha_to_coverage > 0.0) + return coverage <= 0.0; + #endif + return (opacity >= 0.0 ? (alpha < opacity) : (alpha >= -opacity)); + } + #endif + )STRING"; } diff --git a/gl/shader.cpp b/gl/shader.cpp index 9160a2c97..31a3e6a87 100644 --- a/gl/shader.cpp +++ b/gl/shader.cpp @@ -175,6 +175,8 @@ std::pair gl::shader::process_source(const std::string &fil str += "vec4 FBOUT(vec4 x) { return " + (Global.gfx_shadergamma ? std::string("vec4(pow(x.rgb, vec3(1.0 / 2.2)), x.a)") : std::string("x")) + "; }\n"; str += read_file(basedir + filename); + // checked before the includes are expanded, since glsl_common defines the function + alpha_to_coverage = ( str.find( "alpha_test_fail(" ) != std::string::npos ); expand_includes(str, basedir); parse_texture_entries(str); @@ -381,6 +383,7 @@ void gl::program::attach(const gl::shader &s) texture_conf.try_emplace(texturename, std::move(textureentry)); for (auto [paramname, paramentry] : s.param_conf) param_conf.try_emplace(paramname, std::move(paramentry)); + alpha_to_coverage = alpha_to_coverage || s.alpha_to_coverage; glAttachShader(*this, *s); } diff --git a/gl/shader.h b/gl/shader.h index f1735a0e9..e46c14273 100644 --- a/gl/shader.h +++ b/gl/shader.h @@ -63,6 +63,9 @@ namespace gl std::unordered_map texture_conf; std::unordered_map param_conf; std::string name; + // the shader does its alpha test with alpha_test_fail() of glsl_common, so its alpha + // output can drive GL_SAMPLE_ALPHA_TO_COVERAGE in the opaque pass + bool alpha_to_coverage{ false }; #ifndef SHADERVALIDATOR_STANDALONE private: @@ -96,6 +99,7 @@ namespace gl std::unordered_map texture_conf; std::unordered_map param_conf; + bool alpha_to_coverage{ false }; private: void init(); diff --git a/gl/ubo.h b/gl/ubo.h index 9d2c96912..0e5901cb3 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -76,7 +76,11 @@ namespace gl float fog_density; float alpha_mult; float shadow_tone; - UBS_PAD(12); + // 1.0 while GL_SAMPLE_ALPHA_TO_COVERAGE is on, so the alpha tested shaders write the + // coverage of their test edge; 0.0 keeps their plain alpha test. set together with + // the GL state by opengl33_renderer::setup_alpha_to_coverage() + float alpha_to_coverage; + UBS_PAD(8); void set_modelview(const glm::mat4 &mv) { diff --git a/rendering/nullrenderer.cpp b/rendering/nullrenderer.cpp index 332d357c7..bc466e977 100644 --- a/rendering/nullrenderer.cpp +++ b/rendering/nullrenderer.cpp @@ -1,6 +1,26 @@ #include "stdafx.h" #include "rendering/nullrenderer.h" +#include "application/application.h" + +void null_imgui_renderer::BeginFrame() +{ + auto &io { ImGui::GetIO() }; + if( false == io.Fonts->IsBuilt() ) { + unsigned char *pixels { nullptr }; + int width { 0 }; + int height { 0 }; + io.Fonts->GetTexDataAsAlpha8( &pixels, &width, &height ); + } +} + +bool null_renderer::Render() +{ + // the ui frame begun by the application has to be ended, or imgui won't start the next one + Application.render_ui(); + return true; +} + std::unique_ptr null_renderer::create_func() { return std::make_unique(); diff --git a/rendering/nullrenderer.h b/rendering/nullrenderer.h index 0b21580b9..7ecb57bf8 100644 --- a/rendering/nullrenderer.h +++ b/rendering/nullrenderer.h @@ -35,7 +35,18 @@ private: release_() override { /* null implementation, nothing to do */ } }; +// user interface backend which draws nothing: imgui still builds its frames, so the ui code runs as usual +class null_imgui_renderer final : public imgui_renderer { +public: + bool Init() override { return true; } + void Shutdown() override { /* null implementation, nothing to do */ } + // imgui wants its font atlas built before a frame starts; that's done on the cpu, no texture is made of it + void BeginFrame() override; + void Render() override { /* null implementation, nothing to do */ } +}; + // bare-bones render controller, in lack of anything better yet +// it's the renderer of the test mode (--test): models are loaded for the physics, textures aren't class null_renderer : public gfx_renderer { public: @@ -49,7 +60,7 @@ public: Init( GLFWwindow *Window ) override { return true; } // main draw call. returns false on error bool - Render() override { return true; } + Render() override; void SwapBuffers() override { /* null implementation, nothing to do */ } inline @@ -105,20 +116,22 @@ public: void Bind_Material( material_handle const Material, TSubModel const *sm = nullptr, lighting_data const *lighting = nullptr ) override { /* null implementation, nothing to do */ } IMaterial const * - Material( material_handle const Material ) const override { return m_materials.at(Material - 1).get(); } + Material( material_handle const Material ) const override { + return ( Material > 0 && static_cast( Material ) <= m_materials.size() ? m_materials[ Material - 1 ].get() : IMaterial::null_material() ); } // shader methods auto Fetch_Shader( std::string const &name ) -> std::shared_ptr override { throw std::runtime_error("not impl"); } // texture methods + // no textures are loaded; whoever asks gets the null texture texture_handle - Fetch_Texture( std::string const &Filename, bool const Loadnow = true, GLint format_hint = GL_SRGB_ALPHA ) override { throw std::runtime_error("not impl"); } + Fetch_Texture( std::string const &Filename, bool const Loadnow = true, GLint format_hint = GL_SRGB_ALPHA ) override { return null_handle; } void Bind_Texture( texture_handle const Texture ) override { /* null implementation, nothing to do */ } void Bind_Texture( std::size_t const Unit, texture_handle const Texture ) override { /* null implementation, nothing to do */ } - opengl_texture & - Texture( texture_handle const Texture ) override { throw std::runtime_error("not impl"); } - opengl_texture const & - Texture( texture_handle const Texture ) const override { throw std::runtime_error("not impl"); } + ITexture & + Texture( texture_handle const Texture ) override { return *ITexture::null_texture(); } + ITexture const & + Texture( texture_handle const Texture ) const override { return *ITexture::null_texture(); } // utility methods void Pick_Control_Callback( std::function Callback ) override { /* null implementation, nothing to do */ } @@ -152,11 +165,12 @@ public: imgui_renderer *GetImguiRenderer() override { - return nullptr; + return &m_imgui; } private: std::string empty_str; + null_imgui_renderer m_imgui; gfx::geometrybank_manager m_geometry; std::vector> m_materials; }; diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index 932ac31f2..39c0d865c 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -1820,6 +1820,22 @@ void opengl33_renderer::setup_matrices() OpenGLMatrices.load_matrix(glm::mat4{glm::mat3{m_renderpass.pass_camera.modelview()}}); } +void opengl33_renderer::setup_alpha_to_coverage(bool const Enable) +{ + // the shaders keep the plain alpha test unless told the coverage of their alpha is used; + // uploaded with the rest of the model block at the next draw + model_ubs.alpha_to_coverage = (Enable ? 1.f : 0.f); + + if (Enable == m_alphatocoverageenabled) + return; + + if (Enable) + glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE); + else + glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); + m_alphatocoverageenabled = Enable; +} + void opengl33_renderer::setup_drawing(bool const Alpha) { if (Alpha) @@ -1834,6 +1850,8 @@ void opengl33_renderer::setup_drawing(bool const Alpha) glDepthMask(GL_TRUE); m_blendingenabled = false; } + // turned on again per material, by the ones which need it + setup_alpha_to_coverage(false); switch (m_renderpass.draw_mode) { @@ -2374,6 +2392,18 @@ void opengl33_renderer::Bind_Material( material_handle const Material, TSubModel else model_ubs.alpha_mult = 1.0f; + // the alpha test drops or keeps whole pixels, so multisampling alone leaves its edges (e.g. sides + // of the trackbed textures) jagged. the alpha tested materials of the opaque colour pass get them + // antialiased with alpha to coverage instead; their shaders give the coverage as the alpha. + // the model previews draw in colour mode too, but to a single-sampled framebuffer + setup_alpha_to_coverage( + Global.iMultisampling > 0 + && false == m_blendingenabled + && m_renderpass.draw_mode == rendermode::color + && false == m_previewing + && model_ubs.opacity > 0.f + && material.shader->alpha_to_coverage ); + // materials which declare texture variants provide a different texture set per model instance, // picked from the seed of the rendered instance and the variant group of the submodel auto const &materialtextures{ diff --git a/rendering/opengl33renderer.h b/rendering/opengl33renderer.h index 1b4897386..0e84761e1 100644 --- a/rendering/opengl33renderer.h +++ b/rendering/opengl33renderer.h @@ -277,6 +277,8 @@ class opengl33_renderer : public gfx_renderer { void setup_pass(viewport_config &Viewport, renderpass_config &Config, rendermode const Mode, float const Znear = 0.f, float const Zfar = 1.f, bool const Ignoredebug = false); void setup_matrices(); void setup_drawing(bool const Alpha = false); + // alpha to coverage for the alpha tested materials of the opaque colour pass, when multisampled + void setup_alpha_to_coverage(bool const Enable); void setup_shadow_unbind_map(); void setup_shadow_bind_map(); void setup_shadow_color( glm::vec4 const &Shadowcolor ); @@ -560,6 +562,7 @@ class opengl33_renderer : public gfx_renderer { material_handle m_invalid_material; bool m_blendingenabled; + bool m_alphatocoverageenabled{ false }; bool m_widelines_supported; diff --git a/shaders/mat_default.frag b/shaders/mat_default.frag index bc48d939c..5459f1d21 100644 --- a/shaders/mat_default.frag +++ b/shaders/mat_default.frag @@ -29,7 +29,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -42,7 +43,7 @@ void main() glossiness = abs(param[1].w); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, 0.0, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); /* float distance = dot(f_pos.xyz, f_pos.xyz); if( distance <= cascade_end.x ) { color.r += 0.25; } diff --git a/shaders/mat_default_detail.frag b/shaders/mat_default_detail.frag index 63d15c7ce..0f8ad7d5a 100644 --- a/shaders/mat_default_detail.frag +++ b/shaders/mat_default_detail.frag @@ -35,7 +35,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -52,7 +53,7 @@ void main() glossiness = abs(param[1].w); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_default_specgloss.frag b/shaders/mat_default_specgloss.frag index 254a0cc7f..52d94aec0 100644 --- a/shaders/mat_default_specgloss.frag +++ b/shaders/mat_default_specgloss.frag @@ -32,7 +32,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -46,7 +47,7 @@ void main() metalic = texture(specgloss, f_coord).b; fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); /* float distance = dot(f_pos.xyz, f_pos.xyz); if( distance <= cascade_end.x ) { color.r += 0.25; } diff --git a/shaders/mat_detail_normalmap.frag b/shaders/mat_detail_normalmap.frag index 85f552899..debfe3bab 100644 --- a/shaders/mat_detail_normalmap.frag +++ b/shaders/mat_detail_normalmap.frag @@ -39,7 +39,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; @@ -64,7 +65,7 @@ void main() float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_detail_normalmap_specgloss.frag b/shaders/mat_detail_normalmap_specgloss.frag index a3799ce1e..0fa9ea2ee 100644 --- a/shaders/mat_detail_normalmap_specgloss.frag +++ b/shaders/mat_detail_normalmap_specgloss.frag @@ -42,7 +42,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -72,7 +73,7 @@ void main() fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_detail_parallax.frag b/shaders/mat_detail_parallax.frag index 75919b13f..28de25095 100644 --- a/shaders/mat_detail_parallax.frag +++ b/shaders/mat_detail_parallax.frag @@ -49,7 +49,8 @@ void main() vec4 detailnormal_map = texture(detailnormalmap, f_coord_p * param[2].x); vec4 tex_color = texture(diffuse, f_coord_p); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -72,7 +73,7 @@ void main() fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_detail_parallax_specgloss.frag b/shaders/mat_detail_parallax_specgloss.frag index fcc33ac34..8d1b86457 100644 --- a/shaders/mat_detail_parallax_specgloss.frag +++ b/shaders/mat_detail_parallax_specgloss.frag @@ -55,7 +55,8 @@ void main() vec4 tex_color = texture(diffuse, f_coord_p); vec4 specgloss_map = texture(specgloss, f_coord_p); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; @@ -78,7 +79,7 @@ void main() fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_normalmap.frag b/shaders/mat_normalmap.frag index 3ef505eba..9757324d8 100644 --- a/shaders/mat_normalmap.frag +++ b/shaders/mat_normalmap.frag @@ -34,7 +34,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -51,7 +52,7 @@ void main() fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_normalmap_specgloss.frag b/shaders/mat_normalmap_specgloss.frag index a4b184627..3f043b51a 100644 --- a/shaders/mat_normalmap_specgloss.frag +++ b/shaders/mat_normalmap_specgloss.frag @@ -38,7 +38,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -57,7 +58,7 @@ void main() fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_parallax.frag b/shaders/mat_parallax.frag index 7adac0549..49e8eb2a0 100644 --- a/shaders/mat_parallax.frag +++ b/shaders/mat_parallax.frag @@ -42,7 +42,8 @@ void main() vec4 tex_color = texture(diffuse, f_coord_p); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -59,7 +60,7 @@ void main() fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_parallax_specgloss.frag b/shaders/mat_parallax_specgloss.frag index 8b590fae1..04349ef2c 100644 --- a/shaders/mat_parallax_specgloss.frag +++ b/shaders/mat_parallax_specgloss.frag @@ -46,7 +46,8 @@ void main() vec4 tex_color = texture(diffuse, f_coord_p); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -65,7 +66,7 @@ void main() fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_reflmap.frag b/shaders/mat_reflmap.frag index bd4c4ae79..1ffdd822f 100644 --- a/shaders/mat_reflmap.frag +++ b/shaders/mat_reflmap.frag @@ -32,7 +32,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -49,7 +50,7 @@ void main() if(alphatestfail) fragcolor.r = 1.0; - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; diff --git a/shaders/mat_reflmap_specgloss.frag b/shaders/mat_reflmap_specgloss.frag index 4c89083c7..71ebbac24 100644 --- a/shaders/mat_reflmap_specgloss.frag +++ b/shaders/mat_reflmap_specgloss.frag @@ -36,7 +36,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -54,7 +55,7 @@ void main() if(alphatestfail) fragcolor.r = 1.0; - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; diff --git a/shaders/mat_shadowlessnormalmap.frag b/shaders/mat_shadowlessnormalmap.frag index cb2c040e9..ebf135ba1 100644 --- a/shaders/mat_shadowlessnormalmap.frag +++ b/shaders/mat_shadowlessnormalmap.frag @@ -34,7 +34,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -51,7 +52,7 @@ void main() glossiness = abs(param[1].w); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, 1.0); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_shadowlessnormalmap_specgloss.frag b/shaders/mat_shadowlessnormalmap_specgloss.frag index adc473ff6..2bd6d215f 100644 --- a/shaders/mat_shadowlessnormalmap_specgloss.frag +++ b/shaders/mat_shadowlessnormalmap_specgloss.frag @@ -38,7 +38,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -56,7 +57,7 @@ void main() metalic = texture(specgloss, f_coord).b; fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, 1.0); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_sunlessnormalmap.frag b/shaders/mat_sunlessnormalmap.frag index 0b3fc7f67..a5e97d30c 100644 --- a/shaders/mat_sunlessnormalmap.frag +++ b/shaders/mat_sunlessnormalmap.frag @@ -99,7 +99,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -115,7 +116,7 @@ void main() float specularity = (tex_color.r + tex_color.g + tex_color.b) * 0.5; fragcolor = apply_lights_sunless(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/shaders/mat_sunlessnormalmap_specgloss.frag b/shaders/mat_sunlessnormalmap_specgloss.frag index 21cc9f6e9..4e2abe1d9 100644 --- a/shaders/mat_sunlessnormalmap_specgloss.frag +++ b/shaders/mat_sunlessnormalmap_specgloss.frag @@ -113,7 +113,8 @@ void main() { vec4 tex_color = texture(diffuse, f_coord); - bool alphatestfail = ( opacity >= 0.0 ? (tex_color.a < opacity) : (tex_color.a >= -opacity) ); + float alphacoverage = alpha_coverage(tex_color.a); + bool alphatestfail = alpha_test_fail(tex_color.a, alphacoverage); if(alphatestfail) discard; // if (tex_color.a < opacity) @@ -131,7 +132,7 @@ void main() metalic = texture(specgloss, f_coord).b; fragcolor = apply_lights_sunless(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); - vec4 color = vec4(apply_fog(fragcolor), tex_color.a * alpha_mult); + vec4 color = vec4(apply_fog(fragcolor), alphacoverage * alpha_mult); #if POSTFX_ENABLED out_color = color; #else diff --git a/simulation/simulation.cpp b/simulation/simulation.cpp index ea2177170..d10d555a3 100644 --- a/simulation/simulation.cpp +++ b/simulation/simulation.cpp @@ -11,6 +11,7 @@ http://mozilla.org/MPL/2.0/. #include "simulation/simulation.h" #include "simulation/simulationtime.h" #include "simulation/simulationenvironment.h" +#include "simulation/simulationtest.h" #include "utilities/Globals.h" #include "world/Event.h" @@ -109,6 +110,8 @@ state_manager::init_scripting_interface() { simulation::Memory.insert( memorycell ); simulation::Region->insert( memorycell ); } + // the cell a test scenery reports its result to + simulation::test().init_result_cell(); } // legacy method, calculates changes in simulation state over specified time diff --git a/simulation/simulationstateserializer.cpp b/simulation/simulationstateserializer.cpp index 664f3b955..244700070 100644 --- a/simulation/simulationstateserializer.cpp +++ b/simulation/simulationstateserializer.cpp @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "simulation/simulationtime.h" #include "simulation/simulationsounds.h" #include "simulation/simulationenvironment.h" +#include "simulation/simulationtest.h" #include "scene/scenenodegroups.h" #include "scene/scenelayers.h" #include "scene/sceneterrain.h" @@ -363,6 +364,10 @@ state_serializer::deserialize_config( cParser &Input, scene::scratch_data &Scrat // config parameters (re)definition Global.ConfigParse( Input ); + if( Global.test.enabled ) { + // a scenery doesn't get to turn on what a test keeps off + simulation::apply_test_mode_settings(); + } } void diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp new file mode 100644 index 000000000..89a8a1046 --- /dev/null +++ b/simulation/simulationtest.cpp @@ -0,0 +1,270 @@ +/* +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/simulationtest.h" + +#include "application/application.h" +#include "scene/scene.h" +#include "simulation/simulation.h" +#include "utilities/Globals.h" +#include "utilities/Logs.h" +#include "utilities/utilities.h" +#include "vehicle/DynObj.h" +#include "world/Event.h" +#include "world/MemCell.h" +#ifdef _WIN32 +#include "Console.h" +#endif + +#include +#include +#include +#include +#include +#include +#include + +namespace { + +bool is_moving(TDynamicObject const *Vehicle) +{ + return std::abs(Vehicle->GetVelocity()) > 0.01; +} + +// [s] of real time since the start of the simulator +double elapsed_wall_time() +{ + return std::chrono::duration(std::chrono::steady_clock::now() - Global.startTimestamp).count(); +} + +} // namespace + +namespace simulation { + +test_manager &test() +{ + static test_manager instance; + return instance; +} + +void apply_test_mode_settings() +{ + // nobody watches nor listens: no player vehicle, nothing drawn, no sound, no window in the way + Global.editor_session = false; + Global.local_start_vehicle = "ghostview"; + Global.GfxRenderer = "null"; + Global.NvRenderer = false; + Global.bSoundEnabled = false; + Global.python_enabled = false; + Global.bInactivePause = false; + Global.bFullScreen = false; + Global.fullscreen_windowed = false; + Global.VSync = false; + Global.iMultisampling = 0; + Global.gfx_usegles = false; + Global.minframetime = std::chrono::duration(0.0f); + Global.extra_viewports.clear(); + Global.headtrack_conf.joy.clear(); + // a test runs on its own: no network, and no input from outside (eu07/maszyna#541) + Global.network_servers.clear(); + Global.network_client.reset(); + Global.InputGamepad = false; + Global.InputMouse = false; + Global.uart_conf.enable = false; + Global.motiontelemetry_conf.enable = false; +#ifdef WITH_ZMQ + Global.zmq_address.clear(); +#endif + // nor anything to the outside: no keyboard leds, LPT or PoKeys desk (feedbackmode), no messages to a + // multiplayer server (multiplayer, which also holds some events back for it), no program run at the end + Global.iFeedbackMode = 0; +#ifdef _WIN32 + Console::ModeSet(Global.iFeedbackMode, Global.iFeedbackPort); +#endif + Global.iMultiplayer = 0; + Global.exec_on_exit.clear(); + // and the same way every time, unless given another seed (-seed, or randomseed in the settings) + if (!Global.random_seed) + Global.random_seed = seed_of("test"); + // the real time limit, which the scenery may set too + test().watch_wall_clock(wall_clock_limit(Global.test.wall_timeout, Global.test.timeout)); +} + +void test_manager::init_result_cell() const +{ + auto cell { std::make_unique(scene::node_data{0, -1, std::string(result_cell), "memcell"}) }; + // neither of the reported values, so that a cell nobody wrote to says nothing + cell->UpdateValues("", -1.0, 0.0, basic_event::flags::value1 | basic_event::flags::value2); + simulation::Region->insert(cell.get()); + // the table owns its cells + simulation::Memory.insert(cell.release()); +} + +void test_manager::start() +{ + if (m_started) + return; + m_started = true; + { + std::scoped_lock lock { m_lock }; + m_progress = test_progress {}; + } + if (simulation::Vehicles.sequence().empty()) + { + // most likely the scenery wasn't found, or it isn't a test scenery + finish(test_result::error, "the scenery has no vehicles"); + return; + } + for (auto const &name : Global.test.vehicles) + { + if (simulation::Vehicles.find(name) == nullptr) + { + finish(test_result::error, std::format("the scenery has no vehicle {} (test.vehicle)", name)); + return; + } + } + // a pause set by the scenery or the settings would never be lifted, nobody is there to press the key + Global.iPause = 0; + auto tested { std::string("every vehicle") }; + if (false == Global.test.vehicles.empty()) + { + tested = "the consists of"; + for (auto const &name : Global.test.vehicles) + tested += " " + name; + } + auto const walllimit { m_walllimit.load() }; + WriteLog(std::format("test: started, testing {}, timeout {:.0f} s, real time limit {}, standstill {:.0f} s{}{}{}", tested, Global.test.timeout, + walllimit > 0.0 ? std::format("{:.0f} s", walllimit) : std::string("none"), Global.test.standstill, + Global.test.pass_on_standstill ? ", standstill passes" : "", + Global.test.fail_if_stop_overran ? "" : ", signals passed at danger allowed", + Global.test.no_movement_before_pass ? "" : ", pass without waiting for a stop")); +} + +void test_manager::update(double const Deltatime) +{ + if (false == m_started || m_finished) + return; + + test_frame frame { Deltatime, std::nullopt, {}, tested_trains_at_rest() }; + if (auto const *cell = simulation::Memory.find(std::string(result_cell))) + { + frame.reported = cell->Value1(); + frame.reason = cell->Text(); + } + + auto const limits { test_limits { Global.test.timeout, Global.test.standstill, Global.test.pass_on_standstill, Global.test.no_movement_before_pass } }; + std::optional verdict; + auto passrequested { false }; + { + std::scoped_lock lock { m_lock }; + auto const requestedbefore { m_progress.passrequested }; + verdict = evaluate_frame(m_progress, frame, limits); + passrequested = m_progress.passrequested && false == requestedbefore; + } + if (verdict.has_value()) + { + finish(verdict->result, verdict->reason); + return; + } + if (passrequested) + { + WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for the tested trains to stop", m_progress.time, + frame.reason.empty() ? std::string() : std::format(" ({})", frame.reason))); + } +} + +bool test_manager::tested_trains_at_rest() const +{ + if (Global.test.vehicles.empty()) + return std::ranges::none_of(simulation::Vehicles.sequence(), is_moving); + // the consists as they are now: a train the tested one couples to joins it, one it leaves behind doesn't + return std::ranges::none_of(Global.test.vehicles, [](std::string const &Name) { + auto *vehicle { simulation::Vehicles.find(Name) }; + return vehicle != nullptr && vehicle->find_vehicle(coupling::coupler, is_moving) != nullptr; + }); +} + +void test_manager::signal_passed_at_danger(std::string_view const Vehicle, std::string_view const Signal) +{ + auto const reason { std::format("{} passed signal {} at danger", Vehicle, Signal) }; + if (false == Global.test.enabled) + { + WriteLog(reason, logtype::generic); + return; + } + if (false == Global.test.fail_if_stop_overran) + { + WriteLog(std::format("test: {} at {:.1f} s, allowed by test.fail_if_stop_overran", reason, m_progress.time)); + return; + } + finish(test_result::signal_passed_at_danger, reason); +} + +void test_manager::finish(test_result const Result, std::string_view const Reason) +{ + if (conclude(Result, Reason)) + Application.queue_quit(true); +} + +bool test_manager::conclude(test_result const Result, std::string_view const Reason) +{ + std::scoped_lock lock { m_lock }; + if (m_finished || false == Global.test.enabled) + return false; + m_result = Result; + m_finished = true; + + auto const line { std::format("test: {} (exit code {}) after {:.1f} s of simulation time: {}", to_string(Result), static_cast(Result), m_progress.time, Reason) }; + if (Result == test_result::pass) + WriteLog(line); + else + ErrorLog(line); + // the process has no console of its own; whoever started it with a redirected output gets the verdict there too + std::fprintf(stdout, "%s\n", line.c_str()); + std::fflush(stdout); + return true; +} + +int test_manager::exit_code() const +{ + std::scoped_lock lock { m_lock }; + return static_cast(m_finished ? m_result : test_result::error); +} + +void test_manager::watch_wall_clock(double const Limit) +{ + m_walllimit = Limit; + if (Limit <= 0.0 || m_watchdog.joinable()) + return; + m_watchdog = std::jthread([this](std::stop_token const &Stop) { + while (false == Stop.stop_requested()) + { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + check_wall_clock(elapsed_wall_time()); + } + }); +} + +void test_manager::check_wall_clock(double const Elapsed) +{ + auto const limit { m_walllimit.load() }; + if (limit <= 0.0 || Elapsed < limit) + return; + // the run hangs, or is too slow: it ends here, whatever the main thread is doing. a run whose test has ended + // but which doesn't close ends with its result + if (false == conclude(test_result::timeout, std::format("still running after {:.0f} s of real time (test.wall_timeout)", limit))) + ErrorLog(std::format("test: the simulator was still running {:.0f} s of real time after the start, ended", limit)); + // what was logged last gets written, unless the log is held up by a hung thread + StopLogService(); + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + std::_Exit(exit_code()); +} + +} // namespace simulation diff --git a/simulation/simulationtest.h b/simulation/simulationtest.h new file mode 100644 index 000000000..784cd39e8 --- /dev/null +++ b/simulation/simulationtest.h @@ -0,0 +1,76 @@ +/* +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 "simulation/simulationtestrules.h" + +#include +#include +#include +#include + +// integration test mode (eu07 --test -s , eu07/maszyna#541): the scenery runs unattended in a tight +// fixed-step loop and the simulator quits with an exit code saying how the test went +namespace simulation { + +class test_manager { + +public: + // name of the memory cell a scenery reports its result to + static constexpr std::string_view result_cell { "__test.result" }; + // fixed simulation time step of one frame in test mode [s]: 3 physics steps of 10 ms + static constexpr double frame_step { 0.03 }; + + // creates the result cell; called with the other scripting interface cells, before the scenery is read + void init_result_cell() const; + // the simulation is ready: start the clocks + void start(); + // checks the end conditions after a simulation frame of Deltatime seconds + void update(double Deltatime); + // reported by a driver whose train went past a signal showing stop + void signal_passed_at_danger(std::string_view Vehicle, std::string_view Signal); + // ends the test with given result and closes the application; the first call wins + void finish(test_result Result, std::string_view Reason); + // ends the test with given result, without closing the application: for the ways out that close it anyway + // (a bad command line, a failed start, an exception). returns false if the test had already ended + bool conclude(test_result Result, std::string_view Reason); + // (re)sets the real time limit of the run [s] since the start of the simulator, 0 = none. the first limit + // starts the watchdog, which ends a run still going at the limit with the timeout code, whatever it's doing + void watch_wall_clock(double Limit); + + bool finished() const { return m_finished; } + // exit code of the process: the result, or error while the test hasn't finished + int exit_code() const; + +private: + // the watchdog's check, at Elapsed seconds of real time since the start of the simulator + void check_wall_clock(double Elapsed); + // every vehicle of the tested trains stands: the consists of test.vehicle, or every vehicle without it + bool tested_trains_at_rest() const; + + // the watchdog may end the test from its own thread + mutable std::mutex m_lock; + bool m_started { false }; + std::atomic m_finished { false }; + test_result m_result { test_result::error }; + test_progress m_progress; + std::atomic m_walllimit { 0.0 }; // [s] since the start of the simulator, 0 = none + std::jthread m_watchdog; +}; + +// the test of the run +test_manager &test(); + +// the settings of the test mode, over whatever the settings files and the scenery say: applied once the command +// line is read, and again after each config block of the scenery, which could turn the sound, the network or the +// program run at the end back on +void apply_test_mode_settings(); + +} // namespace simulation diff --git a/simulation/simulationtestrules.cpp b/simulation/simulationtestrules.cpp new file mode 100644 index 000000000..fcedaed7d --- /dev/null +++ b/simulation/simulationtestrules.cpp @@ -0,0 +1,162 @@ +/* +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/simulationtestrules.h" + +#include +#include +#include + +namespace { + +// the cell holds what the scenery wrote, so the values are whole numbers; the margin only keeps a stray fraction out +bool holds(double const Value, double const Expected) +{ + return std::abs(Value - Expected) < 1e-6; +} + +// the text of the result cell, as said after the result +std::string reason_suffix(std::string_view const Reason) +{ + return Reason.empty() ? std::string() : std::format(" ({})", Reason); +} + +// options of the simulator which take a value, see eu07_application::init_settings() +bool takes_value(std::string_view const Option) +{ + return Option == "-s" || Option == "-v" || Option == "-seed" || Option == "-ini"; +} + +// a number of seconds given to Option, at Index of Arguments; the index is moved past the value +std::optional option_seconds(std::span const Arguments, std::size_t &Index) +{ + if (Index + 1 >= Arguments.size()) + return std::nullopt; + ++Index; + return simulation::parse_seconds(Arguments[Index]); +} + +} // namespace + +namespace simulation { + +std::string_view to_string(test_result const Result) +{ + using enum test_result; + switch (Result) + { + case pass: return "pass"; + case fail: return "fail"; + case signal_passed_at_danger: return "signal passed at danger"; + case timeout: return "timeout"; + case standstill: return "standstill"; + case error: return "error"; + } + return "unknown"; +} + +std::optional parse_seconds(std::string_view const Text) +{ + auto const *const last{Text.data() + Text.size()}; + double value{0.0}; + if (auto const [parsed, status]{std::from_chars(Text.data(), last, value)}; status != std::errc{} || parsed != last || false == std::isfinite(value) || value < 0.0) + return std::nullopt; + return value; +} + +test_command_line parse_test_command_line(std::span const Arguments) +{ + test_command_line result; + for (std::size_t i = 1; i < Arguments.size(); ++i) + { + std::string_view const option{Arguments[i]}; + if (option == "--test") + { + result.enabled = true; + } + else if (option == "--test-timeout" || option == "--test-wall-timeout") + { + auto const seconds{option_seconds(Arguments, i)}; + if (false == seconds.has_value()) + { + // a typo would otherwise turn the limit off + if (result.error.empty()) + result.error = std::format("{} needs a number of seconds", option); + continue; + } + if (option == "--test-timeout") + result.timeout = seconds; + else + result.wall_timeout = seconds; + } + else if (takes_value(option)) + { + ++i; + } + } + return result; +} + +std::string describe_exception(std::exception_ptr const &Exception) +{ + if (nullptr == Exception) + return "the simulator was terminated"; + try + { + std::rethrow_exception(Exception); + } + catch (std::exception const &Error) + { + return std::format("critical error: {}", Error.what()); + } + catch (...) + { + return "critical error: an exception of unknown type"; + } +} + +double wall_clock_limit(std::optional const Given, double const Simulationtimeout) +{ + if (Given.has_value()) + return *Given; + return Simulationtimeout > 0.0 ? Simulationtimeout + wall_timeout_loading_allowance : 0.0; +} + +std::optional evaluate_frame(test_progress &Progress, test_frame const &Frame, test_limits const &Limits) +{ + using enum test_result; + + Progress.time += Frame.deltatime; + + if (Frame.reported.has_value()) + { + auto const value{*Frame.reported}; + if (holds(value, 0.0)) + return test_verdict{fail, std::format("__test.result set to 0{}", reason_suffix(Frame.reason))}; + if (holds(value, 1.0)) + { + if (false == Limits.no_movement_before_pass) + return test_verdict{pass, std::format("__test.result set to 1{}", reason_suffix(Frame.reason))}; + Progress.passrequested = true; + } + } + + Progress.resttime = Frame.atrest ? Progress.resttime + Frame.deltatime : 0.0; + + if (Progress.passrequested && Progress.resttime >= pass_settle_time) + return test_verdict{pass, "__test.result set to 1 and the tested trains stopped"}; + if (Limits.standstill > 0.0 && Progress.resttime >= Limits.standstill) + return test_verdict{Limits.pass_on_standstill ? pass : standstill, std::format("the tested trains stood still for {:.0f} s", Limits.standstill)}; + if (Limits.timeout > 0.0 && Progress.time >= Limits.timeout) + return test_verdict{timeout, std::format("no result after {:.0f} s", Limits.timeout)}; + return std::nullopt; +} + +} // namespace simulation diff --git a/simulation/simulationtestrules.h b/simulation/simulationtestrules.h new file mode 100644 index 000000000..684b90cde --- /dev/null +++ b/simulation/simulationtestrules.h @@ -0,0 +1,98 @@ +/* +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 + +// the rules of the test mode (eu07 --test, eu07/maszyna#541) which need nothing of the running simulation: the exit +// codes, the command line, the end conditions and the real time limit. kept apart so the unit tests can check them +namespace simulation { + +// exit codes of a test run; public, scripts and CI read them. none of them is a code a process commonly ends with +// when it goes wrong on its own: 1 (killed with TerminateProcess, or a plain failure of most programs), 3 (abort() +// on Windows), 126-127 (a shell that can't run the program) or 128 + a signal number (killed, on Linux) +enum class test_result : int { + pass = 0, // __test.result set to 1, then the tested trains came to rest (or test.pass_on_standstill) + fail = 10, // __test.result set to 0 + signal_passed_at_danger = 11, // a train went past a stop signal (SetVelocity 0 0), unless test.fail_if_stop_overran no + timeout = 12, // test.timeout seconds of simulation time, or test.wall_timeout seconds of real time went by + standstill = 13, // the tested trains stood still for test.standstill seconds without a result + error = 14, // the run could not start (a bad command line, the scenery failed to load) or the simulator threw +}; + +std::string_view to_string(test_result Result); + +// how a test ended, and why +struct test_verdict { + test_result result{test_result::error}; + std::string reason; +}; + +// the test mode options of the command line +struct test_command_line { + bool enabled{false}; // --test + std::optional timeout; // --test-timeout + std::optional wall_timeout; // --test-wall-timeout + std::string error; // what's wrong with them; empty if nothing +}; + +// seconds given on the command line: a number, not negative. nothing for anything else +std::optional parse_seconds(std::string_view Text); + +// picks the test mode options out of a command line (the first argument is the program). the values of the other +// options are skipped, so a scenery named like an option isn't taken for one; options it doesn't know are left alone +test_command_line parse_test_command_line(std::span Arguments); + +// what ended a test run that the simulator didn't end itself: the exception caught (or being handled when the +// program was terminated), or nothing +std::string describe_exception(std::exception_ptr const &Exception); + +// time allowed for loading the scenery, on top of the simulation time, when the real time limit isn't given [s] +inline constexpr double wall_timeout_loading_allowance{600.0}; + +// the real time limit of a test run [s], 0 = none: the one given, else the simulation time limit plus time for the +// loading, so a simulation that runs at least as fast as real time is never cut short. no simulation time limit +// (test.timeout 0) means none in real time either, unless given +double wall_clock_limit(std::optional Given, double Simulationtimeout); + +// the limits and switches the end conditions go by +struct test_limits { + double timeout{3600.0}; // [s] of simulation time; 0 = never + double standstill{300.0}; // [s] of the tested trains standing without a result; 0 = never + bool pass_on_standstill{false}; + bool no_movement_before_pass{true}; +}; + +// what a frame of the simulation brought +struct test_frame { + double deltatime{0.0}; // [s] of simulation time + std::optional reported; // value of the result cell, if there is one + std::string_view reason; // text of the result cell + bool atrest{false}; // every vehicle of the tested trains stands (test.vehicle; every vehicle without it) +}; + +// the state of a test between frames +struct test_progress { + double time{0.0}; // simulation time since the start [s] + double resttime{0.0}; // how long the tested trains have been standing [s] + bool passrequested{false}; // __test.result was set to 1, the pass waits for the tested trains to stop +}; + +// how long the tested trains have to stand before a requested pass is granted [s] +inline constexpr double pass_settle_time{1.0}; + +// checks the end conditions after a frame and moves the progress on. returns the verdict once the test is over +std::optional evaluate_frame(test_progress &Progress, test_frame const &Frame, test_limits const &Limits); + +} // namespace simulation diff --git a/stdafx.h b/stdafx.h index f2b377196..164edc240 100644 --- a/stdafx.h +++ b/stdafx.h @@ -91,7 +91,7 @@ #if defined(__aarch64__) || defined(__arm64__) #define GLM_FORCE_NEON -#else +#elif defined(__AVX2__) // set by /arch:AVX2 or -mavx2 (ENABLE_AVX2); without it GLM falls back to SSE2 #define GLM_FORCE_AVX2 #endif #define GLM_FORCE_SWIZZLE diff --git a/tests/scenarios/.gitignore b/tests/scenarios/.gitignore new file mode 100644 index 000000000..ea93687fe --- /dev/null +++ b/tests/scenarios/.gitignore @@ -0,0 +1,2 @@ +out/ +__pycache__/ diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md new file mode 100644 index 000000000..0a11c6a0a --- /dev/null +++ b/tests/scenarios/README.md @@ -0,0 +1,249 @@ +# Scenario tests + +End-to-end checks that run the real simulator on a scenery and look at how the run ended +and what a vehicle did: did it start, did it keep to the speed limit, did it brake and stop, +did it pass a signal at stop. + +Scenery and vehicle data are not in this repo, so these run against a local game install +only, not in CI. + +## The simulator's test mode + +The runner drives the simulator's test mode (eu07/maszyna#541), which works without the +runner too and is meant for scenery authors as well: + +``` +eu07 --test -s [--test-timeout ] [--test-wall-timeout ] [-seed ] [-ini ] +``` + +With `--test` the simulator: + +- needs `-s`; `-v` is ignored and every train is driven by AI (ghostview), +- reads the settings of the game folder's `eu07.ini` and of the files given with `-ini` (in that + order), not the player's own `eu07.ini` in `%APPDATA%\MaSzyna` (`~/.config/MaSzyna` on Linux), + so a test runs the same for whoever runs it, +- draws nothing: the null renderer loads the 3D models (the physics needs some of their + data) but no textures, and the window stays hidden, +- has no sound, no network, no python, no pause (not even one set by the scenery), and + no crash dialog, +- takes no input from outside: keys and mouse sent to its window, gamepad, UART (a cab + desk), ZeroMQ and commands sent by other programs (`WM_COPYDATA`) are all ignored, and + motion telemetry is off, +- gives nothing to the outside either: no keyboard leds, LPT or PoKeys (`feedbackmode`), no + messages to a multiplayer server (`multiplayer`), no program run at the end (`execonexit`), + no console window of its own (`showsystemconsole`), no Discord presence, and the game's `imgui.ini` + stays as it is, +- keeps all of the above whatever the `config` block of the scenery says: the settings of the + test mode are applied again after it, +- runs the simulation as fast as it can in fixed steps of 0.03 s of simulation time per + frame (three 10 ms physics steps), whatever the real time, the time speed or the machine, +- seeds its random numbers with a fixed seed unless `-seed` or `randomseed` gives one, so two + runs of a scenery come out the same (the physicslogs of two runs are byte for byte equal), +- quits when the test ends, and says how it ended in the exit code and in a `test: ...` + line on stdout and in the log. A command line it can't take, a start that fails, or an + exception thrown by the simulator (of any type, on Linux too) ends the same way, with the + `error` code. That line is all there is on stdout: the log goes to + the log file only (`debuglog` doesn't print it on the console in test mode). + +On Windows the simulator is a windowed program, which the `cmd` prompt and PowerShell don't wait +for. In a `.bat` file `eu07 --test ... > result.txt` waits and sets `%errorlevel%`; at the prompt +`start /wait eu07 --test ...` waits too but doesn't pass the output on. In PowerShell use +`(Start-Process eu07 '--test ...' -Wait -PassThru -RedirectStandardOutput result.txt).ExitCode`. +Scripts (Python's `subprocess`, bash) wait for it as for any other program. + +A scenery reports its result to the memory cell `__test.result`, which the simulator creates +(in every run, so a test scenery loads normally too). Write value 1 to it for a pass and 0 +for a failure, with an `UpdateValues` event; other values are ignored, and the text, if +given, goes to the log as the reason: + +``` +event test_pass UpdateValues 0 __test.result reached_the_stop 1 * endevent +event test_fail UpdateValues 0 __test.result wrong_track 0 * endevent +``` + +(`PutValues` isn't the event for it: it hands a command to a vehicle, it doesn't write a cell.) +A good place for the pass event is `event0` of a track the train should stop on: it runs +while a train stands there, and the length of the track is the margin of the stop. + +The test ends with the first of these: + +| exit code | result | when | +| --- | --- | --- | +| 0 | pass | `__test.result` was set to 1 and then the tested trains stood still for 1 s, so a train still moving towards a signal at stop has to stop first (with `test.no_movement_before_pass no` the pass ends the test at once). Also the standstill end with `test.pass_on_standstill yes` | +| 10 | fail | `__test.result` was set to 0; ends at once | +| 11 | signal passed at danger | a train went past a signal showing stop (`SetVelocity 0 0`; `SetVelocity 0 x` on automatic block, which may be passed after a stop, doesn't count); ends at once, unless `test.fail_if_stop_overran no` | +| 12 | timeout | `test.timeout` seconds of simulation time went by (default 3600), or `test.wall_timeout` seconds of real time since the start (see below) | +| 13 | standstill | the tested trains stood still for `test.standstill` seconds (default 300) and nothing reported a result. Trains waiting at a station or for a signal count as standing, so the limit has to be longer than such waits | +| 14 | error | the run could not start: no `-s`, the scenery failed to load or has no vehicles, or the simulator threw | + +A crash ends with the system's code instead (on Windows e.g. `0xC0000005`, -1073741819). The +codes of the test mode keep clear of the ones a process gets when it goes wrong on its own: 1 +(killed with `TerminateProcess`, or a plain failure of most programs), 3 (`abort()` on Windows), +126-127 (a shell that can't run the program) and 128 + a signal number (killed, on Linux). + +The limits go in a `config` block of the scenery: + +``` +config +test.timeout 600 +test.wall_timeout 900 +test.standstill 120 +test.pass_on_standstill no +test.fail_if_stop_overran yes +test.no_movement_before_pass yes +test.vehicle ep07-424 +endconfig +``` + +| key | default | meaning | +| --- | --- | --- | +| `test.timeout` | 3600 | seconds of simulation time before the test ends as `timeout`; 0 = never | +| `test.wall_timeout` | `test.timeout` + 600 | seconds of real time since the start before the test ends as `timeout`; 0 = never | +| `test.standstill` | 300 | seconds with the tested trains still and no result before the test ends as `standstill`; 0 = never | +| `test.pass_on_standstill` | no | the standstill end passes the test instead | +| `test.fail_if_stop_overran` | yes | a signal passed at danger ends the test; with `no` it's only logged | +| `test.no_movement_before_pass` | yes | a pass waits until the tested trains stand still; with `no` it ends the test at once | +| `test.vehicle` | none | a vehicle of a train under test, by its name in the scenery (once for each train under test). The tested trains are the consists of these vehicles as they are at the moment (a train the tested one couples to joins it), and only they count for the pass and the standstill: other trains, AI traffic, may run on. A name the scenery doesn't have ends the test with `error`. Not given, every vehicle of the scenery counts. A signal passed at danger counts for every train either way | + +`--test-timeout` and `--test-wall-timeout` on the command line win over `test.timeout` and +`test.wall_timeout` of the scenery and the settings files. + +`test.wall_timeout` is a watchdog in real time, for a run that hangs or crawls: when the +simulator still runs that many seconds after its start, loading included, it says `test: timeout` +and quits with the timeout code, whatever it is doing at the moment, with no dialog. Not given, +it is `test.timeout` + 600 s: ten minutes for the loading, and a simulation that runs at least as +fast as real time is never cut short by it. With `test.timeout 0` there is no watchdog unless +`test.wall_timeout` is given; `test.wall_timeout 0` turns it off. A test that has ended but whose +simulator doesn't close is ended by the watchdog too, with its own result. + +A signal is passed at danger when the head of a train goes past a signal which its driver +has in its speed table, showing stop. So it's the driver's view that counts: a train without +a driver isn't watched, and neither is a signal the driver never scanned (a train placed at +100 km/h 150 m before a signal at stop got past it before its AI first looked ahead; at +60 km/h it was caught). Outside the test mode the same is written to the log, nothing more. + +## Running + +``` +python tests/scenarios/run.py --game-dir "C:/Steam/steamapps/common/MaSzyna" -j 4 +``` + +Python 3.10+, no extra packages. Options: + +- `EU07_GAME_DIR` can replace `--game-dir`. +- `--exe path/to/eu07.exe` picks the build to test; the default is the newest + `cmake-build-*/bin/Release/eu07*.exe`. The build needs the `--test` and `-ini` command + line options. +- name one or more cases to run only those, e.g. `run.py ... eu07_td_timetable`. +- `-j N` runs N cases at once. Each case prints its results when it is done. All 17 cases + take about 30 s with `-j 4`, loading included. +- `--watch` runs a case the old way, in a visible window in real time (or faster with + `--timespeed N`), closed after the case's `duration`; the exit code isn't checked then. For + watching what a case does. + +Exit code is 0 when every case passed. Logs of each run (`log.txt`, `errors.txt`, +`stdout.txt`, the physicslogs of the vehicle and of other trains of the case) are moved to +`tests/scenarios/out//`. + +## How it works + +For each case the runner: + +1. writes `scenery/autotest_.scn` into the game dir: the base scenery with its own + trainsets removed, the start time changed if the case says so, the case's track events + added, and the case's `.scn` fragment added: its trainsets after `FirstInit`, everything + else (the case's `test_config`, event launchers standing in for keys the scenery expects a + player to press, extra events) before it, +2. starts the simulator with `--test -s autotest_.scn -ini out//autotest.ini + --test-wall-timeout `, plus `--test-timeout ` when the case gives a + `duration`; the extra ini turns on + `physicslog` and writes the logs to `logs/log (autotest_) .txt` rather than the + shared `log.txt` (`multiplelogs`), +3. waits until the simulator quits by itself. It gives up early when the log reports + `Bad vehicle` / `Bad trainset`, or when the scenario has loaded and no physicslog appeared. + After the case's `timeout` of wall-clock time the simulator ends itself with the timeout code; + one that doesn't is killed 30 s later, +4. compares the result from the exit code with the case's `expect`, then runs the checks on + the physicslog. + +The lead vehicle is named `autotest_`, so every case has its own physicslog and log +files and cases can run in parallel. The `autotest_*` sceneries (and the simulator's `.sbt` +geometry cache of them) stay in the game's `scenery/` dir; the cache is dropped when the base +scenery is newer, and after a run cut short (`wall_timeout`, `error`, a crash, or killed by run.py), +which could have been writing it. + +## Writing a case + +`cases/.json`: + +| key | meaning | +| --- | --- | +| `base_scenery` | scenery file in the game's `scenery/` dir | +| `strip_trainsets` | drop the base scenery's trainsets (default `true`) | +| `time` | replace the scenery start time, `HH:MM` | +| `track_friction` | set the friction of every track of the base scenery (default in the sceneries is 0.15), e.g. for slippery rail | +| `track_events` | lines added to tracks of the base scenery, by track name, e.g. `{ "tor1k": "event0 test_pass" }`; only tracks in the scenery file itself, not in its includes | +| `test_config` | lines of a `config` block added to the scenery, e.g. `["test.timeout 90"]`; the block always has `test.vehicle` with the case's lead vehicle, so only its train counts for the pass and the standstill | +| `fragment` | `.scn` file in `cases/` added to the scenery: trainsets after `FirstInit` (before it they derail on unlinked track), the rest before it (launchers read after it never fire) | +| `vars` | values for `{{name}}` in the fragment; a list becomes one line per item | +| `expect` | the result the test mode should end with: `pass`, `fail`, `signal_passed_at_danger`, `timeout`, `standstill` or `error`, or `wall_timeout` for the timeout of the real time watchdog (the timeout code with a verdict saying it was the real time; a case expecting `timeout` fails on it). Default `timeout` for a case with `duration`, else `pass` | +| `duration` | seconds of simulation to record: the test timeout of the run (`--test-timeout`) | +| `timeout` | wall-clock seconds before giving up, loading included: the simulator's `--test-wall-timeout` (default 600) | +| `fail_on_errors` | fail when `errors.txt` is not empty (default `false`) | +| `checks` | list of checks, below | + +`{{vehicle}}` is always set, also inside `vars` values: the node name of the lead vehicle, +the one whose physicslog is checked, and the train under test (`test.vehicle`). Other vehicles +of the consist take names made from it, e.g. `{{vehicle}}_s`; another train with a driver is named +`_{{vehicle}}`, so its physicslog is moved out of the game dir too. The td.scn cases share two fragments, `td_timetable.scn` (route set, +train runs to Przystanek) and `td_red_signal.scn` (no route, exit signal at stop), and differ +only in `vars.consist`. Przystanek is a technical stop in that timetable (`pt`), where the AI +doesn't wait for the departure time: it sets off as soon as it counts the train as standing at +the W4, which is below 1 km/h, so the train stands there for a few seconds at most, or doesn't +quite stop (since the AI braking of #264 the SN61 turns back at 0.4 km/h). The +timetable cases therefore check that the train comes down to 1 km/h, with no hold, 1380-1420 m +from the start (each of the seven trains stops at 1397-1401 m). `td_red_signal_moving.scn` is the red signal one with the train +starting at `vars.velocity` km/h; with `track_friction` 0.03 it locks the wheels when braking +and slides past the signal, so those cases set `test.fail_if_stop_overran no`. +`td_red_signal_pass.scn` adds the `test_pass` event, which `test_mode_pass` puts as `event0` +on `tor1k`, the track in front of the signal. + +The `test_mode_*` cases show the ends of the test mode: `test_mode_pass` (exit 0, the pass +event), `test_mode_signal_passed_at_danger` (exit 11), `test_mode_timeout` (exit 12, from +`test.timeout`), `test_mode_standstill` (exit 13, from `test.standstill`) and +`test_mode_wall_timeout` (exit 12 from the real time watchdog, which ends it while it loads). +`test_mode_standstill_traffic` (fragment `td_traffic.scn`) has a train under test standing on +track 2 of Stacja1 and AI traffic, `traffic_{{vehicle}}`, leaving track 1 on the timetable: the +standstill of 100 s ends the test although the traffic runs on (counting every vehicle, it runs +into the test timeout). +`test_mode_pass_while_moving` reports the pass from `event2` of `tor1k` (the direction the train runs), as the train runs onto +it, with `test.no_movement_before_pass no`, so the test ends with the train still moving. + +Checks (speeds in km/h, times in seconds of physicslog time, distances in metres). Most use +the physicslog Velocity column, which is the wheel speed: it stays at 0 while the wheels are +locked in a skid. The ones marked *vehicle speed* use the vehicle speed column at the end of +the row instead, logged by builds from this branch on. + +- `starts` `min_speed`, `within`: reaches `min_speed` within `within` seconds. +- `max_speed` `limit`, `tolerance`: never goes faster than `limit + tolerance`. +- `brakes` `min_pressure`: after moving off, brake cylinder pressure reaches `min_pressure` bar + (physicslog pressures are in bar whatever its header says; the brake pipe runs at 5.0). +- `stops` `moving_speed`, `stop_speed`, `hold`, optional `min_distance`, `max_distance`: after + reaching `moving_speed`, stays at or below `stop_speed` for `hold` seconds; with the distances, + that stop has to be between them (metres travelled, as in `distance`). +- `stays_stopped` `moving_speed`, `stop_speed`: after moving off and stopping, never goes + above `stop_speed` again. +- `distance` `min`, `max`: total distance travelled. The physicslog has no position, so the + distance comes from integrating the speed; on td.scn every vehicle's first stop after + Stacja1 came out at 1400 m (+-4), and the red signal stop at 140 m. +- `slips` `within`: the lead vehicle's wheels slip or lock at some point (within `within` + seconds if given), so a slippery rail case does what it's meant to. +- `slip_clears` `moving_speed`, `stop_speed`, `within` (*vehicle speed*): once stopped after + moving off, the wheels stop slipping within `within` seconds. +- `holds_still` `moving_speed`, `stop_speed`, `max_distance` (*vehicle speed*): once stopped + after moving off, moves no more than `max_distance` metres, e.g. no creeping on locked wheels. +- `ends_moving` `min_speed`: the last row of the physicslog is at `min_speed` or faster, e.g. + the test ended without waiting for the train to stop. + +New check types go in `CHECKS` in `run.py`; each gets the physicslog rows and the check's +parameters and returns `(passed, detail)`. diff --git a/tests/scenarios/cases/en57_td_timetable.json b/tests/scenarios/cases/en57_td_timetable.json new file mode 100644 index 000000000..d4c4f26fa --- /dev/null +++ b/tests/scenarios/cases/en57_td_timetable.json @@ -0,0 +1,21 @@ +{ + "description": "EN57 EMU (three cars) on td.scn, AI driver on the td/rozklad timetable: starts from Stacja1, keeps to the speed limit, brakes to a stop at Przystanek.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_timetable.scn", + "vars": { + "consist": [ + "node -1 0 {{vehicle}} dynamic pkp\\en57_v2 EN57-1000ra EN57RA_GSO_WKM1_KM_NS_N 0 headdriver 255 0 enddynamic", + "node -1 0 {{vehicle}}_s dynamic pkp\\en57_v2 EN57-1000s EN57S_WKM1_KM_NS_S 0 nobody 255 0 enddynamic", + "node -1 0 {{vehicle}}_rb dynamic pkp\\en57_v2 EN57-1000rb EN57RB_GSO_WKM1_KM_NS 0 nobody 0 0 enddynamic" + ] + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 20, "within": 150 }, + { "type": "max_speed", "limit": 90, "tolerance": 3 }, + { "type": "brakes", "min_pressure": 0.5 }, + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } + ] +} diff --git a/tests/scenarios/cases/ep09_td_timetable.json b/tests/scenarios/cases/ep09_td_timetable.json new file mode 100644 index 000000000..851b4dbfe --- /dev/null +++ b/tests/scenarios/cases/ep09_td_timetable.json @@ -0,0 +1,17 @@ +{ + "description": "EP09 on td.scn, AI driver on the td/rozklad timetable: starts from Stacja1, keeps to the speed limit, brakes to a stop at Przystanek.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_timetable.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\ep09_v1 104e-005 104e_3 0 headdriver 0 0 enddynamic" + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 20, "within": 150 }, + { "type": "max_speed", "limit": 90, "tolerance": 3 }, + { "type": "brakes", "min_pressure": 0.5 }, + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } + ] +} diff --git a/tests/scenarios/cases/et22_td_timetable.json b/tests/scenarios/cases/et22_td_timetable.json new file mode 100644 index 000000000..47622c622 --- /dev/null +++ b/tests/scenarios/cases/et22_td_timetable.json @@ -0,0 +1,17 @@ +{ + "description": "ET22 on td.scn, AI driver on the td/rozklad timetable: starts from Stacja1, keeps to the speed limit, brakes to a stop at Przystanek.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_timetable.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\et22_v3 201E-1150 201E 0 headdriver 0 0 enddynamic" + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 20, "within": 150 }, + { "type": "max_speed", "limit": 90, "tolerance": 3 }, + { "type": "brakes", "min_pressure": 0.5 }, + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } + ] +} diff --git a/tests/scenarios/cases/eu07_slippery_push_red_signal.json b/tests/scenarios/cases/eu07_slippery_push_red_signal.json new file mode 100644 index 000000000..64d6ba884 --- /dev/null +++ b/tests/scenarios/cases/eu07_slippery_push_red_signal.json @@ -0,0 +1,39 @@ +{ + "description": "EU07 pushing three coaches on td.scn with slippery rail and no route set, starting at 30 km/h: brakes for the exit signal at stop and skids. Pushed by the train it doesn't roll back at the stop, so it used to stay skidding on locked wheels with the brakes capped by the sliding adhesion and creep (eu07/maszyna#479).", + "base_scenery": "td.scn", + "time": "10:34", + "track_friction": 0.03, + "fragment": "td_red_signal_moving.scn", + "test_config": [ + "test.fail_if_stop_overran no" + ], + "vars": { + "velocity": "30", + "consist": [ + "node -1 0 {{vehicle}}_w1 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608068-6 BDHPUMN_TAPICERKA_1 0 nobody 115 0 enddynamic", + "node -1 0 {{vehicle}}_w2 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608011-6 BDHPUMN_NG_1 0 nobody 115 0 enddynamic", + "node -1 0 {{vehicle}}_w3 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608068-6 BDHPUMN_TAPICERKA_1 0 nobody 115 0 enddynamic", + "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + ] + }, + "duration": 240, + "timeout": 900, + "checks": [ + { + "type": "slips", + "within": 60 + }, + { + "type": "slip_clears", + "moving_speed": 5, + "stop_speed": 0.05, + "within": 5 + }, + { + "type": "holds_still", + "moving_speed": 5, + "stop_speed": 0.05, + "max_distance": 0.01 + } + ] +} diff --git a/tests/scenarios/cases/eu07_slippery_red_signal.json b/tests/scenarios/cases/eu07_slippery_red_signal.json new file mode 100644 index 000000000..c2f60f22d --- /dev/null +++ b/tests/scenarios/cases/eu07_slippery_red_signal.json @@ -0,0 +1,39 @@ +{ + "description": "EU07 with three coaches on td.scn with slippery rail and no route set, starting at 30 km/h: brakes for the exit signal at stop and skids, then the skid ends and the train holds still.", + "base_scenery": "td.scn", + "time": "10:34", + "track_friction": 0.03, + "fragment": "td_red_signal_moving.scn", + "test_config": [ + "test.fail_if_stop_overran no" + ], + "vars": { + "velocity": "30", + "consist": [ + "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic", + "node -1 0 {{vehicle}}_w1 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608068-6 BDHPUMN_TAPICERKA_1 0 nobody 115 0 enddynamic", + "node -1 0 {{vehicle}}_w2 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608011-6 BDHPUMN_NG_1 0 nobody 115 0 enddynamic", + "node -1 0 {{vehicle}}_w3 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608068-6 BDHPUMN_TAPICERKA_1 0 nobody 115 0 enddynamic" + ] + }, + "duration": 240, + "timeout": 900, + "checks": [ + { + "type": "slips", + "within": 60 + }, + { + "type": "slip_clears", + "moving_speed": 5, + "stop_speed": 0.05, + "within": 5 + }, + { + "type": "holds_still", + "moving_speed": 5, + "stop_speed": 0.05, + "max_distance": 0.01 + } + ] +} diff --git a/tests/scenarios/cases/eu07_td_red_signal.json b/tests/scenarios/cases/eu07_td_red_signal.json new file mode 100644 index 000000000..b02acdc81 --- /dev/null +++ b/tests/scenarios/cases/eu07_td_red_signal.json @@ -0,0 +1,18 @@ +{ + "description": "EU07 on td.scn with no route set: pulls off, stops before the exit signal stacja1_j at stop, and stays there.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_red_signal.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 5, "within": 150 }, + { "type": "max_speed", "limit": 40 }, + { "type": "stops", "moving_speed": 5, "stop_speed": 0.5, "hold": 5 }, + { "type": "stays_stopped", "moving_speed": 5, "stop_speed": 0.5 }, + { "type": "distance", "max": 150 } + ] +} diff --git a/tests/scenarios/cases/eu07_td_timetable.json b/tests/scenarios/cases/eu07_td_timetable.json new file mode 100644 index 000000000..3774242cd --- /dev/null +++ b/tests/scenarios/cases/eu07_td_timetable.json @@ -0,0 +1,17 @@ +{ + "description": "EU07 on td.scn, AI driver on the td/rozklad timetable: starts from Stacja1, keeps to the speed limit, brakes to a stop at Przystanek.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_timetable.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 20, "within": 150 }, + { "type": "max_speed", "limit": 90, "tolerance": 3 }, + { "type": "brakes", "min_pressure": 0.5 }, + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } + ] +} diff --git a/tests/scenarios/cases/sm42_td_timetable.json b/tests/scenarios/cases/sm42_td_timetable.json new file mode 100644 index 000000000..994f94c0e --- /dev/null +++ b/tests/scenarios/cases/sm42_td_timetable.json @@ -0,0 +1,17 @@ +{ + "description": "SM42 diesel shunter on td.scn, AI driver on the td/rozklad timetable: starts from Stacja1, keeps to the speed limit, brakes to a stop at Przystanek.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_timetable.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\sm42_v1 6D-468 6D 0 headdriver 0 0 enddynamic" + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 20, "within": 150 }, + { "type": "max_speed", "limit": 90, "tolerance": 3 }, + { "type": "brakes", "min_pressure": 0.5 }, + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } + ] +} diff --git a/tests/scenarios/cases/sn61_td_timetable.json b/tests/scenarios/cases/sn61_td_timetable.json new file mode 100644 index 000000000..1a03c31f8 --- /dev/null +++ b/tests/scenarios/cases/sn61_td_timetable.json @@ -0,0 +1,17 @@ +{ + "description": "SN61 diesel railcar on td.scn, AI driver on the td/rozklad timetable: starts from Stacja1, keeps to the speed limit, brakes to a stop at Przystanek.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_timetable.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\sn61_v2 SN61-13 SN61_v1 0 headdriver 0 0 enddynamic" + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 20, "within": 150 }, + { "type": "max_speed", "limit": 90, "tolerance": 3 }, + { "type": "brakes", "min_pressure": 0.5 }, + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } + ] +} diff --git a/tests/scenarios/cases/td_red_signal.scn b/tests/scenarios/cases/td_red_signal.scn new file mode 100644 index 000000000..981707085 --- /dev/null +++ b/tests/scenarios/cases/td_red_signal.scn @@ -0,0 +1,6 @@ +// added by tests/scenarios/run.py to td.scn, whose own trainsets were removed. +// same start as td_timetable.scn, but nobody sets the route: the exit signal stacja1_j, +// about 150 m ahead of the train, stays at stop. +trainset td/rozklad tdo_n1734 0.0 0.1 +{{consist}} +endtrainset diff --git a/tests/scenarios/cases/td_red_signal_moving.scn b/tests/scenarios/cases/td_red_signal_moving.scn new file mode 100644 index 000000000..648e5d90a --- /dev/null +++ b/tests/scenarios/cases/td_red_signal_moving.scn @@ -0,0 +1,6 @@ +// added by tests/scenarios/run.py to td.scn, whose own trainsets were removed. +// like td_red_signal.scn (no route, the exit signal stacja1_j about 150 m ahead stays at stop), +// but the train starts already moving at {{velocity}} km/h, so it has to brake right away. +trainset td/rozklad tdo_n1734 0.0 {{velocity}} +{{consist}} +endtrainset diff --git a/tests/scenarios/cases/td_red_signal_pass.scn b/tests/scenarios/cases/td_red_signal_pass.scn new file mode 100644 index 000000000..724050322 --- /dev/null +++ b/tests/scenarios/cases/td_red_signal_pass.scn @@ -0,0 +1,8 @@ +// added by tests/scenarios/run.py to td.scn, whose own trainsets were removed. +// like td_red_signal.scn (no route, the exit signal stacja1_j about 150 m ahead stays at stop), +// with the test mode's pass event: the case puts it as event0 on tor1k, the track in front of +// the signal, so it runs once the train stands there. the test passes when every vehicle stopped. +event test_pass UpdateValues 0 __test.result stopped_before_stacja1_j 1 * endevent +trainset td/rozklad tdo_n1734 0.0 0.1 +{{consist}} +endtrainset diff --git a/tests/scenarios/cases/td_timetable.scn b/tests/scenarios/cases/td_timetable.scn new file mode 100644 index 000000000..e67dcf9ad --- /dev/null +++ b/tests/scenarios/cases/td_timetable.scn @@ -0,0 +1,12 @@ +// added by tests/scenarios/run.py to td.scn, whose own trainsets were removed: +// the trainset goes after FirstInit, the launcher before it. +// the case's consist stands where td.scn keeps its timetable train (track 1 of Stacja1) +// and gets the same timetable: Stacja1 10:35 -> Przystanek -> Stacja2. +trainset td/rozklad tdo_n1734 0.0 0.1 +{{consist}} +endtrainset + +// td.scn sets routes from the keyboard (shift+1..4 run events keyctrl01..04). keyctrl04 is the +// way out of track 1: switch zwr02 and the exit signal stacja1_j at 40. +// a launcher with a positive time fires once at that hour (HHMM), radius -1 means anywhere. +node -1 0 autotest_route eventlauncher 0 0 0 -1 none 1034 keyctrl04 end diff --git a/tests/scenarios/cases/td_traffic.scn b/tests/scenarios/cases/td_traffic.scn new file mode 100644 index 000000000..49720a0b5 --- /dev/null +++ b/tests/scenarios/cases/td_traffic.scn @@ -0,0 +1,13 @@ +// added by tests/scenarios/run.py to td.scn, whose own trainsets were removed: two trains at Stacja1. +// the train under test (test.vehicle) stands on track 2, where its driver has nothing to do; the other one, +// AI traffic, has the td/rozklad timetable on track 1 and leaves for Przystanek at 10:35. the standstill of +// the test waits for the train under test only, so the traffic running on doesn't hold it up. +trainset none tor2k 30.0 0.0 +node -1 0 {{vehicle}} dynamic pkp\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic +endtrainset +trainset td/rozklad tdo_n1734 0.0 0.1 +node -1 0 traffic_{{vehicle}} dynamic pkp\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic +endtrainset + +// the way out of track 1 for the traffic, see td_timetable.scn +node -1 0 autotest_route eventlauncher 0 0 0 -1 none 1034 keyctrl04 end diff --git a/tests/scenarios/cases/test_mode_pass.json b/tests/scenarios/cases/test_mode_pass.json new file mode 100644 index 000000000..686b7d160 --- /dev/null +++ b/tests/scenarios/cases/test_mode_pass.json @@ -0,0 +1,31 @@ +{ + "description": "Test mode, pass: EU07 on td.scn with no route set stops before the exit signal stacja1_j; event0 of the track there reports the pass, and the test ends as passed once the train stands.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_red_signal_pass.scn", + "track_events": { + "tor1k": "event0 test_pass" + }, + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "expect": "pass", + "timeout": 600, + "checks": [ + { + "type": "starts", + "min_speed": 5, + "within": 150 + }, + { + "type": "stops", + "moving_speed": 5, + "stop_speed": 0.5, + "hold": 0.5 + }, + { + "type": "distance", + "max": 150 + } + ] +} diff --git a/tests/scenarios/cases/test_mode_pass_while_moving.json b/tests/scenarios/cases/test_mode_pass_while_moving.json new file mode 100644 index 000000000..cdf798f16 --- /dev/null +++ b/tests/scenarios/cases/test_mode_pass_while_moving.json @@ -0,0 +1,28 @@ +{ + "description": "Test mode, pass without waiting for a stop: like test_mode_pass, but event2 of the track in front of the exit signal reports the pass as the train runs onto it, and with test.no_movement_before_pass no the test ends at once, the train still moving.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_red_signal_pass.scn", + "track_events": { + "tor1k": "event2 test_pass" + }, + "test_config": [ + "test.no_movement_before_pass no" + ], + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "expect": "pass", + "timeout": 600, + "checks": [ + { + "type": "starts", + "min_speed": 5, + "within": 150 + }, + { + "type": "ends_moving", + "min_speed": 3 + } + ] +} diff --git a/tests/scenarios/cases/test_mode_signal_passed_at_danger.json b/tests/scenarios/cases/test_mode_signal_passed_at_danger.json new file mode 100644 index 000000000..338e7e6ea --- /dev/null +++ b/tests/scenarios/cases/test_mode_signal_passed_at_danger.json @@ -0,0 +1,22 @@ +{ + "description": "Test mode, signal passed at danger: EU07 on td.scn with no route set starts at 60 km/h about 150 m before the exit signal stacja1_j at stop. Its AI driver is still preparing the engine and doesn't brake, and the run ends when the train passes the signal.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_red_signal_moving.scn", + "vars": { + "velocity": "60", + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "expect": "signal_passed_at_danger", + "timeout": 600, + "checks": [ + { + "type": "max_speed", + "limit": 61 + }, + { + "type": "distance", + "min": 140 + } + ] +} diff --git a/tests/scenarios/cases/test_mode_standstill.json b/tests/scenarios/cases/test_mode_standstill.json new file mode 100644 index 000000000..4dd891cf0 --- /dev/null +++ b/tests/scenarios/cases/test_mode_standstill.json @@ -0,0 +1,26 @@ +{ + "description": "Test mode, standstill: EU07 on td.scn with no route set stops before the exit signal stacja1_j at stop and stays; nothing reports a result, so the test ends once the train under test stood still for test.standstill 120 seconds (the train waits about 70 s before it pulls off, so less would end it before the start).", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_red_signal.scn", + "test_config": [ + "test.standstill 120" + ], + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "expect": "standstill", + "timeout": 600, + "checks": [ + { + "type": "stops", + "moving_speed": 5, + "stop_speed": 0.5, + "hold": 5 + }, + { + "type": "distance", + "max": 150 + } + ] +} diff --git a/tests/scenarios/cases/test_mode_standstill_traffic.json b/tests/scenarios/cases/test_mode_standstill_traffic.json new file mode 100644 index 000000000..8b545e2a5 --- /dev/null +++ b/tests/scenarios/cases/test_mode_standstill_traffic.json @@ -0,0 +1,18 @@ +{ + "description": "Test mode, standstill with traffic: an EU07 stands on track 2 of Stacja1 on td.scn while another EU07, AI traffic, leaves track 1 on the td/rozklad timetable at 10:35 and runs on. Only the train under test (test.vehicle, the case's vehicle) counts, so the test ends on test.standstill 100 although the traffic is moving; counting every vehicle, it would run into the 300 s test timeout.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_traffic.scn", + "test_config": [ + "test.standstill 100" + ], + "expect": "standstill", + "duration": 300, + "timeout": 600, + "checks": [ + { + "type": "max_speed", + "limit": 0.5 + } + ] +} diff --git a/tests/scenarios/cases/test_mode_timeout.json b/tests/scenarios/cases/test_mode_timeout.json new file mode 100644 index 000000000..46899e89c --- /dev/null +++ b/tests/scenarios/cases/test_mode_timeout.json @@ -0,0 +1,21 @@ +{ + "description": "Test mode, timeout: EU07 on td.scn with no route set pulls off towards the exit signal stacja1_j at stop; nothing reports a result and test.timeout 90 from the scenery's config block runs out first.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_red_signal.scn", + "test_config": [ + "test.timeout 90" + ], + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "expect": "timeout", + "timeout": 600, + "checks": [ + { + "type": "starts", + "min_speed": 5, + "within": 90 + } + ] +} diff --git a/tests/scenarios/cases/test_mode_wall_timeout.json b/tests/scenarios/cases/test_mode_wall_timeout.json new file mode 100644 index 000000000..078ccca93 --- /dev/null +++ b/tests/scenarios/cases/test_mode_wall_timeout.json @@ -0,0 +1,11 @@ +{ + "description": "Test mode, real time limit: EU07 on td.scn, with a limit of 2 s of real time (the case's timeout, which the runner passes as --test-wall-timeout). The scenery takes longer to load, so the simulator's watchdog ends the run while it loads, with the timeout code and a verdict saying it was the real time.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_red_signal.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "expect": "wall_timeout", + "timeout": 2 +} diff --git a/tests/scenarios/cases/vectron_td_timetable.json b/tests/scenarios/cases/vectron_td_timetable.json new file mode 100644 index 000000000..04656a00f --- /dev/null +++ b/tests/scenarios/cases/vectron_td_timetable.json @@ -0,0 +1,17 @@ +{ + "description": "Vectron 1293-107 on td.scn, AI driver on the td/rozklad timetable: starts from Stacja1, keeps to the speed limit, brakes to a stop at Przystanek.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_timetable.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp\\vectron_v1 1293-107 wariant_7w 0 headdriver 0 0 enddynamic" + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 20, "within": 150 }, + { "type": "max_speed", "limit": 90, "tolerance": 3 }, + { "type": "brakes", "min_pressure": 0.5 }, + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } + ] +} diff --git a/tests/scenarios/run.py b/tests/scenarios/run.py new file mode 100644 index 000000000..18b3e3f2e --- /dev/null +++ b/tests/scenarios/run.py @@ -0,0 +1,640 @@ +#!/usr/bin/env python3 +"""Scenario tests: run the real simulator on a scenery and check what a vehicle did. + +A case (cases/.json) names a base scenery from the game install, a fragment +added to it (usually a trainset with an AI driver), the result the simulator's test +mode should end with, and checks on the vehicle's physics log. The runner: + + 1. builds scenery/autotest_.scn in the game dir: the base scenery with its own + trainsets removed, optionally a different start time, and the case fragment added, + 2. starts eu07 in test mode (--test: every train driven by AI, nothing drawn, fixed + time steps as fast as it can) with that scenery and an extra settings file (-ini) + that turns on physicslog, + 3. waits until the simulator quits by itself and reads the test result from its exit + code: pass, fail, signal passed at danger, timeout (after `duration` seconds of + simulation, if the case gives one), standstill or error, + 4. compares the result with the one the case expects, evaluates the checks on the + physicslog and prints PASS/FAIL per check. + +Scenery and vehicle data are not part of this repo, so this runs against a local game +install only (--game-dir or EU07_GAME_DIR), not in CI. See README.md. + +Usage: + python tests/scenarios/run.py --game-dir "E:/.../MaSzyna" -j 4 # all cases + python tests/scenarios/run.py --game-dir ... eu07_td_timetable # chosen cases +""" + +from __future__ import annotations + +import argparse +import json +import os +import re +import shutil +import subprocess +import sys +import time +from concurrent.futures import ThreadPoolExecutor, as_completed +from dataclasses import dataclass, field +from pathlib import Path + +HERE = Path(__file__).resolve().parent +REPO = HERE.parents[1] +CASES_DIR = HERE / "cases" +OUT_DIR = HERE / "out" + +# physicslog columns, see TController::PhysicsLog() in vehicle/Driver.cpp. +# Velocity is in km/h and pressures in bar, despite the header saying m/s and MPa. +COL_TIME = 0 +COL_VELOCITY = 1 # wheel speed: stays at 0 while the wheels are locked in a skid +COL_BRAKE_PRESS = 8 +COL_COMMAND = 16 # text, read as None +COL_WHEELSLIP = 20 +COL_VEHICLE_VELOCITY = 25 # signed vehicle speed, km/h; only in builds which log it + +# exit codes of the simulator's test mode (eu07 --test), see simulation/simulationtestrules.h +EXIT_RESULTS = { + 0: "pass", + 10: "fail", + 11: "signal_passed_at_danger", + 12: "timeout", + 13: "standstill", + 14: "error", +} +# the timeout of the simulator's real time watchdog (test.wall_timeout) has the timeout code too; told +# apart by its verdict, so a run that hung doesn't pass a case that expects the simulation time to run out +WALL_TIMEOUT = "wall_timeout" +RESULTS = (*EXIT_RESULTS.values(), WALL_TIMEOUT) + +# settings layered over the game's eu07.ini for a test run (test mode doesn't read the player's own +# in %APPDATA%); the window ones only matter +# with --watch, test mode keeps its window hidden and turns the sound off by itself +TEST_INI = """\ +fullscreen no +fullscreenwindowed no +width 1280 +height 720 +inactivepause no +soundenabled no +physicslog yes +multiplelogs yes +timespeed {timespeed} +""" +# multiplelogs: log and errors go to logs/log () .txt instead of the shared +# log.txt, so runs of different cases can go in parallel + + +# ---------------------------------------------------------------- scenery + + +def split_trainsets(text: str) -> tuple[str, str]: + """Splits text into (everything else, the trainset ... endtrainset blocks).""" + rest, trainsets = [], [] + inside = False + for line in text.splitlines(keepends=True): + tokens = line.split("//", 1)[0].split() + if not inside and tokens and tokens[0].lower() == "trainset": + inside = True + if inside: + trainsets.append(line) + if any(t.lower() == "endtrainset" for t in tokens): + inside = False + continue + rest.append(line) + return "".join(rest), "".join(trainsets) + + +def set_start_time(text: str, hhmm: str) -> str: + return re.sub(r"(?im)^(\s*time\s+)\d{1,2}:\d{2}", lambda m: m.group(1) + hhmm, text, count=1) + + +def add_track_events(text: str, events: dict) -> str: + """Adds lines (e.g. "event0 test_pass") to tracks named in events, before their endtrack.""" + for track, lines in events.items(): + lines = [lines] if isinstance(lines, str) else lines + m = re.search(rf"(?im)^\s*node\s+\S+\s+\S+\s+{re.escape(track)}\s+track\b.*?^(\s*endtrack\b)", text, re.S) + if not m: + sys.exit(f"track_events: no track {track} in the base scenery itself (not in its includes)") + text = text[: m.start(1)] + "".join(line + "\n" for line in lines) + text[m.start(1):] + return text + + +def set_track_friction(text: str, friction: float) -> str: + """Sets the friction of every track: node track ...""" + return re.sub(r"(?im)^(\s*node\s+\S+\s+\S+\s+\S+\s+track\s+\S+\s+\S+\s+\S+\s+)[-\d.]+", + lambda m: m.group(1) + f"{friction}", text) + + +def fill_vars(fragment: str, values: dict, keep=frozenset()) -> str: + """Replaces {{name}} with the case's value; a list value becomes one line per item.""" + def value(m): + if m.group(1) in keep: + return m.group(0) + v = values.get(m.group(1)) + if v is None: + sys.exit(f"fragment uses {{{{{m.group(1)}}}}} but the case sets no such var") + return "\n".join(v) if isinstance(v, list) else str(v) + return re.sub(r"\{\{(\w+)\}\}", value, fragment) + + +def scenery_cache(game_dir: Path, case_name: str) -> Path: + """The simulator's geometry cache of the case scenery.""" + return game_dir / "scenery" / f"autotest_{case_name}.sbt" + + +def build_scenery(case: dict, game_dir: Path, case_name: str, vehicle: str) -> str: + """Writes the case scenery into the game's scenery dir and returns its file name.""" + scenery_dir = game_dir / "scenery" + # sceneries are cp1250; latin-1 round-trips every byte, so nothing gets mangled + text = (scenery_dir / case["base_scenery"]).read_bytes().decode("latin-1") + if case.get("strip_trainsets", True): + text, _ = split_trainsets(text) + if "time" in case: + text = set_start_time(text, case["time"]) + if "track_friction" in case: + text = set_track_friction(text, case["track_friction"]) + if "track_events" in case: + text = add_track_events(text, case["track_events"]) + fragment = (CASES_DIR / case["fragment"]).read_bytes().decode("latin-1") + # vars first: {{vehicle}} inside a var's value gets filled in the second pass + fragment = fill_vars(fragment, case.get("vars", {}), keep={"vehicle"}) + fragment = fill_vars(fragment, {"vehicle": vehicle}) + # FirstInit binds event launchers to their events and links the tracks. so launchers and + # events of the case go in before it (read after it they never fire), trainsets after it + # (placed before it they stand on unlinked track and derail at once) + before, trainsets = split_trainsets(fragment) + # settings of the test mode, read like any other config block: the case's vehicle is the train under test, + # whose consist the pass and the standstill wait for, and the case's own (test.timeout and so on) + config = [f"test.vehicle {vehicle}", *case.get("test_config", [])] + before = "config\n" + "".join(line + "\n" for line in config) + "endconfig\n" + before + header = f"\n// ---- autotest: {case_name} ----\n" + firstinit = re.search(r"(?im)^\s*firstinit\b", text) + if firstinit: + text = text[: firstinit.start()] + header + before + "\n" + text[firstinit.start():] + else: + text += header + before + "\n" + text += header + trainsets + "\n" + name = f"autotest_{case_name}.scn" + (scenery_dir / name).write_bytes(text.encode("latin-1")) + # the simulator caches the scenery's static geometry in .sbt and reads it instead of + # the terrain in the .scn (tracks, events and trainsets always come from the .scn), so + # drop the cache once the base scenery has changed + cache = scenery_cache(game_dir, case_name) + if cache.exists() and cache.stat().st_mtime < (scenery_dir / case["base_scenery"]).stat().st_mtime: + cache.unlink() + return name + + +# ---------------------------------------------------------------- run + + +def read_physicslog(path: Path) -> list[list[float]]: + """Returns rows of columns, None for the text ones; skips the header and partial lines.""" + rows = [] + try: + lines = path.read_text(errors="replace").splitlines() + except FileNotFoundError: + return rows + for line in lines[1:]: + parts = line.split() + if len(parts) <= COL_BRAKE_PRESS: + continue # line still being written + try: + row = [float(p) for p in parts[: COL_BRAKE_PRESS + 1]] + except ValueError: + continue + for p in parts[COL_BRAKE_PRESS + 1:]: + try: + row.append(float(p)) + except ValueError: + row.append(None) + rows.append(row) + return rows + + +def find_logs(game_dir: Path, scenery: str, since: float) -> dict[str, Path]: + """The log.txt / errors.txt of one run; with multiplelogs each scenery gets its own files.""" + stem = scenery.rsplit(".", 1)[0] + found = {} + for kind in ("log", "errors"): + files = [p for p in (game_dir / "logs").glob(f"{kind} ({stem}) *.txt") if p.stat().st_mtime >= since] + if files: + found[kind] = max(files, key=lambda p: p.stat().st_mtime) + return found + + +def read_text(path: Path | None) -> str: + try: + return path.read_text(errors="replace") if path else "" + except FileNotFoundError: + return "" + + +# load problems that leave the case without its vehicle; no point waiting for the timeout +FATAL_LOG_LINES = ("Bad vehicle:", "Bad trainset:") +# wall seconds from start to the first physicslog row before giving up. "Scenario loading +# time" in the log is not the end of loading (the trainsets after FirstInit still follow), +# and with several simulators loading at once the first rows took well over 15 s +STARTUP_TIMEOUT = 120.0 +# wall seconds past a case's timeout before the runner kills a simulator that didn't end itself +KILL_GRACE = 30.0 + + +@dataclass +class RunResult: + rows: list[list[float]] = field(default_factory=list) + exit_code: int | None = None # None: still running when we closed it (the normal end with --watch) + verdict: str = "" # the simulator's own line about the test result + failure: str = "" # why the run was cut short + wall_seconds: float = 0.0 + logs: dict[str, Path] = field(default_factory=dict) + + +def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog: Path, + duration: float | None, timeout: float, watch: bool, say) -> RunResult: + """Runs the simulator on the scenery. In test mode it quits by itself and the exit code + is the result; with watch it runs in a window and gets closed after `duration`.""" + physicslog.unlink(missing_ok=True) + if watch: + cmd = [str(exe), "-s", scenery, "-v", "ghostview", "-ini", str(ini)] + else: + # the simulator's own watchdog ends a run that takes too long, with the timeout code and its verdict + cmd = [str(exe), "--test", "-s", scenery, "-ini", str(ini), "--test-wall-timeout", f"{timeout:g}"] + if duration is not None: + cmd += ["--test-timeout", f"{duration:g}"] + say(" $ " + " ".join(f'"{c}"' if " " in c else c for c in cmd), progress=True) + since = time.time() - 1 + started = time.monotonic() + # the test result line goes to stdout; a file, not a pipe, so a chatty run can't block on it + stdout = (ini.parent / "stdout.txt").open("w+b") + proc = subprocess.Popen(cmd, cwd=game_dir, stdout=stdout, stderr=subprocess.STDOUT) + result = RunResult() + last_report = 0.0 + try: + while True: + time.sleep(0.25) # at timespeed 20 that is 5 s of simulation + wall = time.monotonic() - started + result.rows = read_physicslog(physicslog) + sim_t = result.rows[-1][COL_TIME] if result.rows else 0.0 + if wall - last_report >= 15: + v = result.rows[-1][COL_VELOCITY] if result.rows else 0.0 + say(f" ... {wall:5.0f}s wall, sim {sim_t:6.1f}s, v={v:6.1f} km/h", progress=True) + last_report = wall + if proc.poll() is not None: + result.exit_code = proc.returncode + break + if watch and sim_t >= duration: + break + # in test mode a little later than the simulator's own limit, for a run that can't even end itself + if wall > timeout + (0 if watch else KILL_GRACE): + result.failure = f"timed out after {wall:.0f}s before {duration}s of simulation" + break + if not result.rows: + result.logs = find_logs(game_dir, scenery, since) + fatal = [l.strip() for l in read_text(result.logs.get("errors")).splitlines() + if any(k in l for k in FATAL_LOG_LINES)] + if fatal: + result.failure = "vehicle failed to load: " + " | ".join(fatal) + break + if wall > STARTUP_TIMEOUT: + result.failure = f"no physicslog rows after {STARTUP_TIMEOUT:.0f}s" + break + finally: + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=10) + except subprocess.TimeoutExpired: + proc.kill() + result.wall_seconds = time.monotonic() - started + stdout.seek(0) + result.verdict = next((line.strip() for line in stdout.read().decode(errors="replace").splitlines() + if line.startswith("test:")), "") + stdout.close() + result.rows = read_physicslog(physicslog) + result.logs = find_logs(game_dir, scenery, since) + return result + + +# ---------------------------------------------------------------- checks + + +def check_starts(rows, min_speed, within): + for r in rows: + if r[COL_TIME] > within: + break + if r[COL_VELOCITY] >= min_speed: + return True, f"reached {min_speed} km/h at t={r[COL_TIME]:.1f}s" + top = max((r[COL_VELOCITY] for r in rows if r[COL_TIME] <= within), default=0.0) + return False, f"top speed in the first {within}s was {top:.1f} km/h, wanted {min_speed}" + + +def check_max_speed(rows, limit, tolerance=0.0): + worst = max(rows, key=lambda r: r[COL_VELOCITY]) + v, t = worst[COL_VELOCITY], worst[COL_TIME] + ok = v <= limit + tolerance + return ok, f"top speed {v:.1f} km/h at t={t:.1f}s (limit {limit} +{tolerance})" + + +def _first_moving(rows, moving_speed): + return next((i for i, r in enumerate(rows) if r[COL_VELOCITY] >= moving_speed), None) + + +def _distances(rows): + """Metres travelled up to each row, from the speed; the physicslog has no position.""" + out, d = [0.0], 0.0 + for a, b in zip(rows, rows[1:]): + d += (a[COL_VELOCITY] + b[COL_VELOCITY]) / 2 / 3.6 * (b[COL_TIME] - a[COL_TIME]) + out.append(d) + return out + + +def check_brakes(rows, min_pressure, moving_speed=20): + start = _first_moving(rows, moving_speed) + if start is None: + return False, "never moved, so never braked" + for r in rows[start:]: + if r[COL_BRAKE_PRESS] >= min_pressure: + return True, f"brake cylinder {r[COL_BRAKE_PRESS]:.2f} bar at t={r[COL_TIME]:.1f}s, v={r[COL_VELOCITY]:.1f} km/h" + return False, f"brake cylinder pressure never reached {min_pressure} bar after moving off" + + +def check_stops(rows, moving_speed, stop_speed, hold, min_distance=None, max_distance=None): + start = _first_moving(rows, moving_speed) + if start is None: + return False, f"never reached {moving_speed} km/h" + dist = _distances(rows) + stopped_at = None + for i in range(start, len(rows)): + r = rows[i] + if r[COL_VELOCITY] <= stop_speed: + if stopped_at is None: + stopped_at = i + if r[COL_TIME] - rows[stopped_at][COL_TIME] >= hold: + where = dist[stopped_at] + detail = f"came to a stop at t={rows[stopped_at][COL_TIME]:.1f}s after {where:.0f} m" + if (min_distance is not None and where < min_distance) or (max_distance is not None and where > max_distance): + return False, f"{detail}, wanted between {min_distance} and {max_distance} m" + return True, detail + else: + stopped_at = None + return False, f"did not stay below {stop_speed} km/h for {hold}s after moving off" + + +def check_stays_stopped(rows, moving_speed, stop_speed): + """Once stopped after moving off, never moves again (e.g. waiting at a red signal).""" + start = _first_moving(rows, moving_speed) + if start is None: + return False, f"never reached {moving_speed} km/h" + stop = next((i for i in range(start, len(rows)) if rows[i][COL_VELOCITY] <= stop_speed), None) + if stop is None: + return False, f"never came down to {stop_speed} km/h" + moved = next((r for r in rows[stop:] if r[COL_VELOCITY] > stop_speed), None) + if moved is not None: + return False, f"stopped at t={rows[stop][COL_TIME]:.1f}s but moved again at t={moved[COL_TIME]:.1f}s, v={moved[COL_VELOCITY]:.1f} km/h" + return True, f"stopped at t={rows[stop][COL_TIME]:.1f}s and stayed until t={rows[-1][COL_TIME]:.1f}s" + + +def check_distance(rows, max=None, min=None): + """Total distance travelled, in metres.""" + d = _distances(rows)[-1] + ok = (max is None or d <= max) and (min is None or d >= min) + bounds = " ".join(s for s in (f">= {min}" if min is not None else "", f"<= {max}" if max is not None else "") if s) + return ok, f"travelled {d:.0f} m (wanted {bounds})" + + +def check_slips(rows, within=None): + """The lead vehicle's wheels slip (or lock in a skid) at some point, e.g. to make sure a slippery rail case does what it's meant to.""" + for r in rows: + if within is not None and r[COL_TIME] > within: + break + if len(r) > COL_WHEELSLIP and r[COL_WHEELSLIP]: + return True, f"wheels slipping at t={r[COL_TIME]:.1f}s, v={r[COL_VELOCITY]:.1f} km/h" + return False, "the wheels never slipped" + + +def check_holds_still(rows, moving_speed, stop_speed, max_distance): + """Once stopped after moving off, the vehicle doesn't creep. Uses the vehicle speed rather than the + wheel speed, so it also catches a vehicle sliding on locked wheels.""" + if not rows or len(rows[0]) <= COL_VEHICLE_VELOCITY or rows[0][COL_VEHICLE_VELOCITY] is None: + return False, "the physicslog has no vehicle speed column, the build is too old" + speed = [abs(r[COL_VEHICLE_VELOCITY]) for r in rows] + start = next((i for i, v in enumerate(speed) if v >= moving_speed), None) + if start is None: + return False, f"never reached {moving_speed} km/h" + stop = next((i for i in range(start, len(rows)) if speed[i] <= stop_speed), None) + if stop is None: + return False, f"never came down to {stop_speed} km/h" + d, top, top_t = 0.0, 0.0, rows[stop][COL_TIME] + for i in range(stop + 1, len(rows)): + d += (speed[i - 1] + speed[i]) / 2 / 3.6 * (rows[i][COL_TIME] - rows[i - 1][COL_TIME]) + if speed[i] > top: + top, top_t = speed[i], rows[i][COL_TIME] + held = rows[-1][COL_TIME] - rows[stop][COL_TIME] + detail = f"stopped at t={rows[stop][COL_TIME]:.1f}s, then moved {d:.3f} m in {held:.0f}s, top {top:.4f} km/h at t={top_t:.1f}s" + return d <= max_distance, detail + + +def check_ends_moving(rows, min_speed): + """The run ended with the vehicle still moving, e.g. a pass that didn't wait for the trains to stop.""" + if not rows: + return False, "the physicslog is empty" + last = rows[-1] + ok = last[COL_VELOCITY] >= min_speed + return ok, f"last row t={last[COL_TIME]:.1f}s, v={last[COL_VELOCITY]:.1f} km/h (wanted >= {min_speed})" + + +def check_slip_clears(rows, moving_speed, stop_speed, within): + """Once the vehicle has stopped after moving off, its wheels stop slipping within `within` seconds + (a vehicle left skidding on locked wheels keeps its brakes capped by the sliding adhesion).""" + if not rows or len(rows[0]) <= COL_VEHICLE_VELOCITY or rows[0][COL_VEHICLE_VELOCITY] is None: + return False, "the physicslog has no vehicle speed column, the build is too old" + start = next((i for i, r in enumerate(rows) if abs(r[COL_VEHICLE_VELOCITY]) >= moving_speed), None) + if start is None: + return False, f"never reached {moving_speed} km/h" + stop = next((i for i in range(start, len(rows)) if abs(rows[i][COL_VEHICLE_VELOCITY]) <= stop_speed), None) + if stop is None: + return False, f"never came down to {stop_speed} km/h" + deadline = rows[stop][COL_TIME] + within + late = [r for r in rows[stop:] if r[COL_TIME] > deadline and r[COL_WHEELSLIP]] + if late: + return False, f"stopped at t={rows[stop][COL_TIME]:.1f}s, wheels still slipping at t={late[0][COL_TIME]:.1f}s ({len(late)} rows after the {within}s grace)" + return True, f"stopped at t={rows[stop][COL_TIME]:.1f}s, no slipping after t={deadline:.1f}s" + + +CHECKS = { + "slips": check_slips, + "slip_clears": check_slip_clears, + "holds_still": check_holds_still, + "starts": check_starts, + "max_speed": check_max_speed, + "brakes": check_brakes, + "stops": check_stops, + "stays_stopped": check_stays_stopped, + "distance": check_distance, + "ends_moving": check_ends_moving, +} + + +# ---------------------------------------------------------------- main + + +def find_exe(explicit: str | None) -> Path: + if explicit: + return Path(explicit).resolve() + candidates = sorted(REPO.glob("cmake-build-*/bin/Release/eu07*.exe"), key=lambda p: p.stat().st_mtime) + if not candidates: + sys.exit("no eu07 executable found under cmake-build-*/bin/Release, pass --exe") + return candidates[-1] + + +def count_lines(path: Path) -> int: + try: + return sum(1 for line in path.read_text(errors="replace").splitlines() if line.strip()) + except FileNotFoundError: + return 0 + + +def describe_exit(code: int | None) -> str: + if code is None: + return "none, still running" + name = EXIT_RESULTS.get(code) + return f"{code} ({name})" if name else f"{code} (0x{code & 0xFFFFFFFF:08X}, a crash?)" + + +def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, watch: bool, say) -> bool: + case = json.loads((CASES_DIR / f"{name}.json").read_text(encoding="utf-8")) + say(f"\n=== {name}: {case.get('description', '')}") + out = OUT_DIR / name + shutil.rmtree(out, ignore_errors=True) + out.mkdir(parents=True) + + # the lead vehicle is named after the case, so parallel runs never share a physicslog + vehicle = f"autotest_{name}" + scenery = build_scenery(case, game_dir, name, vehicle) + ini = out / "autotest.ini" + ini.write_text(TEST_INI.format(timespeed=timespeed), encoding="ascii") + physicslog = game_dir / "physicslog" / f"{vehicle}.dat" + + duration = float(case["duration"]) if "duration" in case else None + # a case that only records `duration` seconds ends on the test timeout; one that says + # how the test ends (a pass event, a signal passed at danger...) expects that result + expect = case.get("expect", "timeout" if duration is not None else "pass") + if expect not in RESULTS: + sys.exit(f"{name}: expect must be one of {', '.join(RESULTS)}") + if watch and duration is None: + sys.exit(f"{name}: --watch needs a case with duration") + result = run_simulator(exe, game_dir, scenery, ini, physicslog, + duration, float(case.get("timeout", 600)), watch, say) + + # the logs are ours alone (named after the case scenery), move them out of the game dir + for kind, path in result.logs.items(): + shutil.move(path, out / f"{kind}.txt") + if physicslog.exists(): + shutil.move(physicslog, out / physicslog.name) + # other trains of the case with a driver, named _, log too + for other in physicslog.parent.glob(f"*_{vehicle}.dat"): + shutil.move(other, out / other.name) + + ok = True + sim_t = result.rows[-1][COL_TIME] if result.rows else 0.0 + say(f" ran {result.wall_seconds:.0f}s wall, {sim_t:.1f}s of physicslog, results in {out.relative_to(REPO)}") + if watch: + if result.exit_code is not None: + say(f" FAIL simulator exited on its own with code {result.exit_code}") + ok = False + else: + if result.verdict: + say(f" {result.verdict}") + got = EXIT_RESULTS.get(result.exit_code) if result.exit_code is not None else None + if got == "timeout" and "of real time" in result.verdict: + got = WALL_TIMEOUT + passed = got == expect + watchdog = ", from the real time watchdog" if got == WALL_TIMEOUT else "" + say(f" {'PASS' if passed else 'FAIL'} exit code {describe_exit(result.exit_code)}{watchdog}, expected {expect}") + ok &= passed + # a run cut short (the real time watchdog, an error, a crash, or closed by us) could have been writing + # the scenery's geometry cache; the simulator checks only the header of it, so the next runs would read + # a truncated one. it is written again on the next run + if result.failure or (not watch and got in (None, "error", WALL_TIMEOUT)): + scenery_cache(game_dir, name).unlink(missing_ok=True) + if result.failure: + say(f" FAIL {result.failure}") + ok = False + if not result.rows and case.get("checks"): + if not result.failure: + say(f" FAIL no physicslog for '{vehicle}'") + return False + + for check in case.get("checks", []): + params = {k: v for k, v in check.items() if k != "type"} + passed, detail = CHECKS[check["type"]](result.rows, **params) + say(f" {'PASS' if passed else 'FAIL'} {check['type']}: {detail}") + ok &= passed + + errors = count_lines(out / "errors.txt") + if errors: + say(f" note errors.txt has {errors} line(s)") + if case.get("fail_on_errors") and errors: + ok = False + return ok + + +def main() -> int: + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("cases", nargs="*", help="case names from cases/ (default: all)") + ap.add_argument("--game-dir", default=os.environ.get("EU07_GAME_DIR"), + help="game install with scenery/ and dynamic/ (or EU07_GAME_DIR)") + ap.add_argument("--exe", help="simulator executable (default: newest cmake-build-*/bin/Release/eu07*.exe)") + ap.add_argument("--watch", action="store_true", + help="run in a visible window in real time (see --timespeed) instead of the test mode, " + "closed after the case's duration; the exit code isn't checked") + ap.add_argument("--timespeed", type=float, default=1.0, + help="with --watch: simulation time multiplier, the simulator offers 1, 5, 20 and 60 (default 1)") + ap.add_argument("-j", "--jobs", type=int, default=1, + help="cases run at once, each in a simulator of its own (default 1)") + args = ap.parse_args() + + if not args.game_dir: + sys.exit("pass --game-dir or set EU07_GAME_DIR") + game_dir = Path(args.game_dir).resolve() + exe = find_exe(args.exe) + print(f"exe: {exe}\ngame: {game_dir}") + + names = args.cases or sorted(p.stem for p in CASES_DIR.glob("*.json")) + missing = [n for n in names if not (CASES_DIR / f"{n}.json").exists()] + if missing: + sys.exit("no such case: " + ", ".join(missing)) + + started = time.monotonic() + results = {} + if args.jobs <= 1: + def say(line, progress=False): + print(line, flush=True) + for n in names: + results[n] = run_case(n, exe, game_dir, args.timespeed, args.watch, say) + else: + # each case prints its block when it is done, so parallel output does not interleave + def job(n): + lines = [] + def say(line, progress=False): + if not progress: + lines.append(line) + ok = run_case(n, exe, game_dir, args.timespeed, args.watch, say) + return n, ok, lines + with ThreadPoolExecutor(max_workers=args.jobs) as pool: + for n in names: + print(f"started {n}", flush=True) + for fut in as_completed([pool.submit(job, n) for n in names]): + n, ok, lines = fut.result() + print("\n".join(lines), flush=True) + results[n] = ok + + print(f"\n=== summary ({time.monotonic() - started:.0f}s)") + for n in names: + print(f" {'PASS' if results[n] else 'FAIL'} {n}") + return 0 if all(results.values()) else 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/unit/test_compressor.cpp b/tests/unit/test_compressor.cpp index c5aa2fff7..4f55ce0b8 100644 --- a/tests/unit/test_compressor.cpp +++ b/tests/unit/test_compressor.cpp @@ -113,3 +113,93 @@ TEST_CASE("compressor: without a pressure switch the air over the cut-out pressu REQUIRE(vehicle->CompressorFlag); // nothing stops it CHECK(vehicle->CompressedVolume < 8.5 * 0.81); // a fifth of the air let out } + +TEST_CASE("compressor: an engine stopped before the delay passed counts the whole delay again") { + auto vehicle{compressor_vehicle(30.0)}; + vehicle->Mains = true; // engine started + run_compressor(vehicle, 20.0); + REQUIRE_FALSE(vehicle->CompressorFlag); + + vehicle->Mains = false; // engine stopped, 10 s short of the delay + run_compressor(vehicle, 1.0); + vehicle->Mains = true; + run_compressor(vehicle, 15.0); // 36 s since the first start, but only 15 s since this one + CHECK_FALSE(vehicle->CompressorFlag); + + run_compressor(vehicle, 15.1); + CHECK(vehicle->CompressorFlag); +} + +TEST_CASE("compressor: the start delay holds back a compressor switched on by hand too") { + auto vehicle{compressor_vehicle(30.0)}; + vehicle->CompressorStart = start_t::manual; + vehicle->CompressorAllow = true; // switch in the cabinet on before the engine start + vehicle->Mains = true; + + run_compressor(vehicle, 29.9); + CHECK_FALSE(vehicle->CompressorFlag); + run_compressor(vehicle, 0.2); + CHECK(vehicle->CompressorFlag); +} + +TEST_CASE("compressor: the start delay is read from the brake section") { + auto const delay{[](std::string const &Section) { + return vehicle{definition::load("wagon").set(Section, "CompressorStartDelay", 30)}->CompressorStartDelay; + }}; + CHECK(delay("Brake") == doctest::Approx(30.0)); + CHECK(delay("Cntrl") == doctest::Approx(0.0)); // the key isn't taken from the control section +} + +namespace { + +// 401Da: the compressor runs from the 110 V of the auxiliary generator driven by the engine, here the converter +// started with the main circuit, which gets up to voltage the converter start delay after the engine start +definition generator_vehicle(double const ConverterDelay, double const CompressorDelay) { + return definition::load("cab_vehicle") + .set("Param", "PWR", 370) + .set("Light", "Light", "Generator") + .set("Light", "LMaxVoltage", 110) + .set("Brake", "Vv", 0.6) + .set("Brake", "MinCP", 6.5) + .set("Brake", "MaxCP", 8.0) + .set("Brake", "CompressorSpeed", 0.003) + .set("Brake", "CompressorPower", "Converter") + .set("Brake", "CompressorStartDelay", CompressorDelay) + .set("Cntrl", "CompressorStart", "Automatic") + .set("Cntrl", "ConverterStart", "Automatic") + .set("Cntrl", "ConverterStartDelay", ConverterDelay); +} + +} // namespace + +TEST_CASE("compressor: fed from the converter it waits for the converter start delay") { + vehicle loco{generator_vehicle(30.0, 0.0)}; + REQUIRE(loco->CompressorPower == 2); + REQUIRE(loco->ConverterStartDelay == doctest::Approx(30.0)); + + loco.run(1.0); // engine off + CHECK_FALSE(loco->Power110vIsAvailable); + + loco->Mains = true; // engine started + loco.run(25.0); + CHECK_FALSE(loco->Power110vIsAvailable); + CHECK_FALSE(loco->CompressorFlag); + + loco.run(10.0); + CHECK(loco->Power110vIsAvailable); + CHECK(loco->CompressorFlag); +} + +TEST_CASE("compressor: fed from the converter the start delay counts from the converter voltage") { + vehicle loco{generator_vehicle(20.0, 10.0)}; + REQUIRE(loco->CompressorStartDelay == doctest::Approx(10.0)); + + loco.run(1.0); // engine off + loco->Mains = true; // engine started + loco.run(25.0); + REQUIRE(loco->Power110vIsAvailable); // the converter is up + CHECK_FALSE(loco->CompressorFlag); // but the compressor waits for its own delay as well + + loco.run(10.0); + CHECK(loco->CompressorFlag); +} diff --git a/tests/unit/test_door_closure.cpp b/tests/unit/test_door_closure.cpp new file mode 100644 index 000000000..c92d8fef8 --- /dev/null +++ b/tests/unit/test_door_closure.cpp @@ -0,0 +1,158 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scenario.h" +#include "vehicle.h" + +// the door closure signalling (SOD, the i-door_blocked lamp of a cab): the state of each vehicle's doors and of the +// whole train, the most severe of its vehicles, as the driver gathers it for the lamp (eu07/maszyna#288) + +using test::scenario; +using test::definition; +using test::vehicle; +using namespace test::channels; + +namespace { + +// the state of the whole train, the way TController::determine_consist_state() gathers it +door_closure_t consist_closure(scenario &Scenario) { + auto closure{door_closure_t::none}; + for (std::size_t i = 0; i < Scenario.train().size(); ++i) { + closure = std::max(closure, Scenario.vehicle(i).door_closure()); + } + return closure; +} + +} // namespace + +TEST_CASE("door closure: the lamp is lit for locked doors, blinks for released ones and is dark for open ones") { + for (auto const lit : {true, false}) { + CAPTURE(lit); + CHECK(door_closure_lamp(door_closure_t::locked, lit)); + CHECK(door_closure_lamp(door_closure_t::released, lit) == lit); + CHECK_FALSE(door_closure_lamp(door_closure_t::open, lit)); + CHECK_FALSE(door_closure_lamp(door_closure_t::none, lit)); + } + // a consist takes the most severe state of its vehicles + CHECK(std::max(door_closure_t::none, door_closure_t::locked) == door_closure_t::locked); + CHECK(std::max(door_closure_t::locked, door_closure_t::released) == door_closure_t::released); + CHECK(std::max(door_closure_t::released, door_closure_t::open) == door_closure_t::open); +} + +TEST_CASE("door closure: a unit with permits, locked, released by the permit and open, in any of its cars") { + scenario scenario{"door closure/elf", motion() + doors()}; + scenario.add("elf_v1/21we-a", 1).add("elf_v1/21we-b").add("elf_v1/21we-c"); + scenario.start_up(); + auto &cab{scenario.cab()}; + auto &last{scenario.vehicle(scenario.train().size() - 1)}; + // closed with no permit: no door can be opened + CHECK(cab.door_closure() == door_closure_t::locked); + CHECK(last.door_closure() == door_closure_t::locked); + CHECK(consist_closure(scenario) == door_closure_t::locked); + + scenario.act("permit left"); + cab.PermitDoors(side::left, true); + scenario.wait(2.0); + CHECK(last.door_closure() == door_closure_t::released); + CHECK(consist_closure(scenario) == door_closure_t::released); + + scenario.act("the last car opens its own door"); + last.OperateDoors(side::left, true, range_t::local); + scenario.wait(5.0); + // the cab's own doors stay closed, the train is open all the same + CHECK(cab.door_closure() == door_closure_t::released); + CHECK(last.door_closure() == door_closure_t::open); + CHECK(consist_closure(scenario) == door_closure_t::open); + + scenario.act("close, the steps stay out under the permit"); + last.OperateDoors(side::left, false, range_t::local); + cab.OperateDoors(side::left, false); + scenario.wait(10.0); + CHECK(last.Doors.instances[side::left].position <= 0.f); + // a step out isn't a closed door + CHECK(consist_closure(scenario) == door_closure_t::open); + + scenario.act("revoke the permit"); + cab.PermitDoors(side::left, false); + scenario.wait(10.0); + CHECK(consist_closure(scenario) == door_closure_t::locked); +} + +TEST_CASE("door closure: the doors of a unit without permits are released at a standstill") { + scenario scenario{"door closure/en57", motion() + doors()}; + scenario.add("en57_v1/5bgaiii_led_spot", 1).add("en57_v1/6ba_840").add("en57_v1/5bgb"); + scenario.start_up(); + auto &cab{scenario.cab()}; + auto &middle{scenario.vehicle(1)}; + REQUIRE_FALSE(cab.Doors.permit_needed); + REQUIRE(middle.Doors.permit_needed); + // the door lock engages only once the train runs (DoorLockSpeed), till then the end cars' doors can be opened + CHECK(cab.door_closure() == door_closure_t::released); + CHECK(middle.door_closure() == door_closure_t::locked); + CHECK(consist_closure(scenario) == door_closure_t::released); + scenario.act("open right"); + cab.OperateDoors(side::right, true); + scenario.wait(10.0); + CHECK(consist_closure(scenario) == door_closure_t::open); + scenario.act("close right"); + cab.OperateDoors(side::right, false); + scenario.wait(10.0); + CHECK(consist_closure(scenario) == door_closure_t::released); +} + +TEST_CASE("door closure: a locomotive has no doors to signal, its coach does") { + scenario scenario{"door closure/eu07 with a coach", motion() + doors()}; + scenario.add("4e_v1/4e", 1).add("bdhpumn_v3/bdhpumn"); + scenario.start_up(); + CHECK(scenario.cab().door_closure() == door_closure_t::none); + scenario.act("open right"); + scenario.cab().PermitDoors(side::right, true); + scenario.cab().OperateDoors(side::right, true); + scenario.wait(10.0); + CHECK(scenario.vehicle(1).door_closure() == door_closure_t::open); + CHECK(consist_closure(scenario) == door_closure_t::open); +} + +TEST_CASE("door closure: the signalling is switched on by DoorClosureSignal in the Doors section, off without it") { + SUBCASE("without the key") { + vehicle wagon{definition::load("wagon")}; + CHECK_FALSE(wagon->Doors.closure_signal); + } + SUBCASE("with the key") { + vehicle wagon{definition::load("wagon").set("Doors", "DoorClosureSignal", "Yes")}; + CHECK(wagon->Doors.closure_signal); + } +} + +TEST_CASE("door closure: without DoorClosureSignal the lamp shows the lock of the vehicle's own doors, as before") { + vehicle wagon{definition::load("wagon")}; + REQUIRE_FALSE(wagon->Doors.closure_signal); + for (auto const locked : {true, false}) { + wagon->Doors.is_locked = locked; + for (auto const consist : {door_closure_t::none, door_closure_t::locked, door_closure_t::released, door_closure_t::open}) { + for (auto const lit : {true, false}) { + CAPTURE(locked); + CAPTURE(static_cast(consist)); + CAPTURE(lit); + // no blinking at a standstill with the doors closed, whatever the consist does + CHECK(wagon->door_closure_lamp_lit(consist, lit) == locked); + } + } + } +} + +TEST_CASE("door closure: with DoorClosureSignal the lamp shows the consist in three states") { + vehicle wagon{definition::load("wagon").set("Doors", "DoorClosureSignal", "Yes")}; + REQUIRE(wagon->Doors.closure_signal); + for (auto const locked : {true, false}) { + // the vehicle's own lock doesn't matter, the consist does + wagon->Doors.is_locked = locked; + for (auto const lit : {true, false}) { + CAPTURE(locked); + CAPTURE(lit); + CHECK(wagon->door_closure_lamp_lit(door_closure_t::locked, lit)); + CHECK(wagon->door_closure_lamp_lit(door_closure_t::released, lit) == lit); + CHECK_FALSE(wagon->door_closure_lamp_lit(door_closure_t::open, lit)); + CHECK_FALSE(wagon->door_closure_lamp_lit(door_closure_t::none, lit)); + } + } +} diff --git a/tests/unit/test_linebreaker.cpp b/tests/unit/test_linebreaker.cpp new file mode 100644 index 000000000..126e3307c --- /dev/null +++ b/tests/unit/test_linebreaker.cpp @@ -0,0 +1,114 @@ +#include "stdafx.h" +#include "doctest.h" +#include "consist.h" +#include "drive.h" + +#include "vehicle/linebreaker.h" + +// the line breaker (WS) controls of the cab (eu07/maszyna#394) + +using test::consist; +using test::definition; + +namespace { + +// the line breaker lever of the EP05 (main_sw), a single two-state lever, the way TTrain works it: the state +// the cab keeps of the line breaker follows the vehicle, the breaker closes while the lever is on and can close +struct lever { + TMoverParameters &mover; + int breaker{0}; // as TTrain::m_linebreakerstate: 0 open, 1 closed + bool on{false}; + + void press() { + switch (linebreaker::pressed(breaker, true, on)) { + case linebreaker::toggle::close: + on = true; + break; + case linebreaker::toggle::open: + on = false; + if (breaker == 1 && mover.MainSwitch(false)) { + breaker = 0; + } + break; + default: + break; + } + } + // TTrain::Update() + void update() { + if (breaker == 0 && mover.Mains) { + breaker = 1; + } + if (breaker == 1 && false == mover.Mains) { + breaker = 0; + } + if (on && breaker == 0 && mover.MainSwitchCheck()) { + breaker = mover.MainSwitch(true) ? 1 : 0; + } + } +}; + +} // namespace + +TEST_CASE("line breaker: a press of the toggle closes an open breaker and opens a closed one") { + using enum linebreaker::toggle; + for (auto const latching : {false, true}) { + CHECK(linebreaker::pressed(0, latching, false) == close); + CHECK(linebreaker::pressed(1, latching, true) == open); + CHECK(linebreaker::pressed(1, latching, false) == open); + // freshly closed, the cab finishes closing it first + CHECK(linebreaker::pressed(2, latching, true) == none); + } + // a lever left on over an open breaker goes off; push buttons have no position to keep + CHECK(linebreaker::pressed(0, true, true) == open); + CHECK(linebreaker::pressed(0, false, true) == close); +} + +TEST_CASE("line breaker: the EP05's lever goes off with the converter overload relay tripped (eu07/maszyna#394)") { + consist train; + auto &ep05{train.add(definition::fiz("ep05_v2/ep05"), 1).mover()}; + test::track const line{0.15, 0.0, test::catenary_3kv}; + auto const driver{test::driving(train)}; + REQUIRE(test::start_up(driver, line)); + REQUIRE(ep05.Mains); + REQUIRE(ep05.ConverterFlag); + lever ws{ep05, 1, true}; + + // the converter overload relay knocks the line breaker out, as the cab does it when the converter is + // switched on together with the compressor; the lever stays on + ep05.ConvOvldFlag = true; + ep05.MainSwitch(false, range_t::local); + ws.update(); + REQUIRE_FALSE(ep05.Mains); + REQUIRE(ws.breaker == 0); + CHECK(ws.on); + // with the relay tripped the breaker doesn't close + test::drive(driver, line, 5.0); + CHECK_FALSE(ep05.MainSwitchCheck()); + + // nothing holds the lever: it goes off, and on again, though the breaker stays open + ws.press(); + CHECK_FALSE(ws.on); + ws.press(); + CHECK(ws.on); + ws.update(); + CHECK_FALSE(ep05.Mains); + ws.press(); + CHECK_FALSE(ws.on); + + // the relay reset, with the converter switched off, the lever put on closes the breaker again + ep05.ConverterSwitch(false, range_t::local); + test::drive(driver, line, 1.0); + CHECK(ep05.RelayReset(relay_t::primaryconverteroverload, range_t::local)); + CHECK_FALSE(ep05.ConvOvldFlag); + ws.update(); + CHECK_FALSE(ep05.Mains); // the lever is off + ws.press(); + ws.update(); + CHECK(ep05.Mains); + CHECK(ws.breaker == 1); + // and the next press opens it + ws.press(); + CHECK_FALSE(ws.on); + CHECK_FALSE(ep05.Mains); +} diff --git a/tests/unit/test_oil_pump.cpp b/tests/unit/test_oil_pump.cpp new file mode 100644 index 000000000..a5f623233 --- /dev/null +++ b/tests/unit/test_oil_pump.cpp @@ -0,0 +1,55 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +using test::definition; +using test::vehicle; + +// the oil pump lamp of the cab (i-oilpump) shows OilPump.is_active: the pre-lubrication pump of 401Da, which works +// before the engine start and stops once the engine runs and makes the oil pressure itself (eu07/maszyna#546) +// NOTE: the test vehicle has no diesel engine, so the pump check is called the way dizel_Update() calls it + +TEST_CASE("oil pump: the pre-lubrication pump works before the engine start and stops once the engine runs") { + vehicle loco{definition::load("cab_vehicle").set("Cntrl", "OilStart", "Manual")}; + REQUIRE(loco->OilPump.start_type == start_t::manual); + loco->Power24vIsAvailable = true; // battery on + + loco->OilPumpCheck(0.01); + CHECK_FALSE(loco->OilPump.is_active); // not switched on yet + + loco->OilPump.is_enabled = true; // pump switch on: pre-lubrication + loco->OilPumpCheck(0.01); + CHECK(loco->OilPump.is_active); + + loco->Mains = true; // engine started + loco->OilPumpCheck(0.01); + CHECK_FALSE(loco->OilPump.is_active); +} + +TEST_CASE("oil pump: the pump started with the engine start procedure stops once the engine runs") { + vehicle loco{definition::load("cab_vehicle").set("Cntrl", "OilStart", "Automatic")}; + REQUIRE(loco->OilPump.start_type == start_t::automatic); + loco->Power24vIsAvailable = true; + + loco->dizel_startup = true; // starter button pressed + loco->OilPumpCheck(0.01); + CHECK(loco->OilPump.is_active); + + loco->dizel_startup = false; // start procedure done, the pump keeps working until the engine runs + loco->OilPumpCheck(0.01); + CHECK(loco->OilPump.is_active); + + loco->Mains = true; + loco->OilPumpCheck(0.01); + CHECK_FALSE(loco->OilPump.is_active); +} + +TEST_CASE("oil pump: without power the pump and its lamp stay off") { + vehicle loco{definition::load("cab_vehicle")}; + loco->Power24vIsAvailable = false; + loco->Power110vIsAvailable = false; + + loco->OilPump.is_enabled = true; + loco->OilPumpCheck(0.01); + CHECK_FALSE(loco->OilPump.is_active); +} diff --git a/tests/unit/test_test_mode.cpp b/tests/unit/test_test_mode.cpp new file mode 100644 index 000000000..eba641096 --- /dev/null +++ b/tests/unit/test_test_mode.cpp @@ -0,0 +1,268 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "simulation/simulationtestrules.h" + +#include +#include +#include +#include +#include + +// the test mode of the simulator (eu07 --test, eu07/maszyna#541): its exit codes, command line and end conditions + +namespace { + +using simulation::test_result; + +simulation::test_command_line parse(std::vector Arguments) +{ + Arguments.insert(Arguments.begin(), "eu07"); + return simulation::parse_test_command_line(Arguments); +} + +// a frame of the default 0.03 s +simulation::test_frame frame(bool const Atrest, std::optional const Reported = std::nullopt, std::string_view const Reason = {}) +{ + return simulation::test_frame{0.03, Reported, Reason, Atrest}; +} + +// runs frames until the test ends or Seconds of simulation time go by +std::optional run(simulation::test_progress &Progress, simulation::test_limits const &Limits, double const Seconds, + simulation::test_frame const &Frame) +{ + while (Progress.time < Seconds) + { + if (auto verdict{simulation::evaluate_frame(Progress, Frame, Limits)}) + return verdict; + } + return std::nullopt; +} + +} // namespace + +TEST_CASE("test mode: the exit codes keep clear of the ones of a killed or aborted process") +{ + std::array const results{test_result::pass, test_result::fail, test_result::signal_passed_at_danger, test_result::timeout, test_result::standstill, test_result::error}; + for (auto const result : results) + { + auto const code{static_cast(result)}; + CAPTURE(code); + CHECK(code != 1); // TerminateProcess, most programs' failure + CHECK(code != 3); // abort() on Windows + CHECK(code < 126); // shells, signals on Linux + CHECK(simulation::to_string(result) != "unknown"); + } + // the codes are public: scripts and CI read them + CHECK(static_cast(test_result::pass) == 0); + CHECK(static_cast(test_result::fail) == 10); + CHECK(static_cast(test_result::signal_passed_at_danger) == 11); + CHECK(static_cast(test_result::timeout) == 12); + CHECK(static_cast(test_result::standstill) == 13); + CHECK(static_cast(test_result::error) == 14); +} + +TEST_CASE("test mode: seconds on the command line are a number, not negative") +{ + CHECK(simulation::parse_seconds("240").value_or(-1.0) == doctest::Approx(240.0)); + CHECK(simulation::parse_seconds("0.5").value_or(-1.0) == doctest::Approx(0.5)); + CHECK(simulation::parse_seconds("0").value_or(-1.0) == doctest::Approx(0.0)); + CHECK_FALSE(simulation::parse_seconds("").has_value()); + CHECK_FALSE(simulation::parse_seconds("-1").has_value()); + CHECK_FALSE(simulation::parse_seconds("60s").has_value()); + CHECK_FALSE(simulation::parse_seconds("abc").has_value()); + CHECK_FALSE(simulation::parse_seconds("inf").has_value()); + CHECK_FALSE(simulation::parse_seconds("nan").has_value()); +} + +TEST_CASE("test mode: the command line") +{ + SUBCASE("without --test there is no test") + { + auto const commandline{parse({"-s", "td.scn", "-v", "ep07-424"})}; + CHECK_FALSE(commandline.enabled); + CHECK(commandline.error.empty()); + } + SUBCASE("--test and its limits, in any order") + { + auto const commandline{parse({"--test-wall-timeout", "900", "-s", "td.scn", "--test", "--test-timeout", "240"})}; + CHECK(commandline.enabled); + CHECK(commandline.timeout.value_or(-1.0) == doctest::Approx(240.0)); + CHECK(commandline.wall_timeout.value_or(-1.0) == doctest::Approx(900.0)); + CHECK(commandline.error.empty()); + } + SUBCASE("a limit not given stays unset") + { + auto const commandline{parse({"--test", "-s", "td.scn"})}; + CHECK_FALSE(commandline.timeout.has_value()); + CHECK_FALSE(commandline.wall_timeout.has_value()); + } + SUBCASE("a limit which isn't a number is an error, not a limit turned off") + { + auto const commandline{parse({"--test", "-s", "td.scn", "--test-timeout", "4m"})}; + CHECK(commandline.enabled); + CHECK_FALSE(commandline.timeout.has_value()); + CHECK(commandline.error == "--test-timeout needs a number of seconds"); + } + SUBCASE("a limit without its value is an error") + { + auto const commandline{parse({"--test", "-s", "td.scn", "--test-wall-timeout"})}; + CHECK(commandline.error == "--test-wall-timeout needs a number of seconds"); + } + SUBCASE("the first error is the one told") + { + auto const commandline{parse({"--test", "--test-timeout", "x", "--test-wall-timeout", "y"})}; + CHECK(commandline.error == "--test-timeout needs a number of seconds"); + } + SUBCASE("values of other options aren't taken for options") + { + auto const commandline{parse({"-s", "--test", "-ini", "--test-timeout", "-v", "--test-wall-timeout", "-seed", "--test"})}; + CHECK_FALSE(commandline.enabled); + CHECK(commandline.error.empty()); + } + SUBCASE("-edit takes no option for its scenery, which is optional") + { + // a scenery after -edit or none: the option after it is read either way + CHECK(parse({"-edit", "--test", "-s", "td.scn"}).enabled); + CHECK(parse({"-edit", "td.scn", "--test"}).enabled); + // -edit leaves -s alone, which takes --test for its scenery, as the simulator reads it + CHECK_FALSE(parse({"-edit", "-s", "--test"}).enabled); + } +} + +TEST_CASE("test mode: the real time limit") +{ + SUBCASE("given, it is the limit, 0 turns it off") + { + CHECK(simulation::wall_clock_limit(120.0, 3600.0) == doctest::Approx(120.0)); + CHECK(simulation::wall_clock_limit(0.0, 3600.0) == doctest::Approx(0.0)); + } + SUBCASE("not given, it is the simulation time limit and time for the loading") + { + CHECK(simulation::wall_clock_limit(std::nullopt, 240.0) == doctest::Approx(240.0 + simulation::wall_timeout_loading_allowance)); + CHECK(simulation::wall_clock_limit(std::nullopt, 3600.0) == doctest::Approx(3600.0 + simulation::wall_timeout_loading_allowance)); + } + SUBCASE("no simulation time limit, no real time limit unless given") + { + CHECK(simulation::wall_clock_limit(std::nullopt, 0.0) == doctest::Approx(0.0)); + CHECK(simulation::wall_clock_limit(300.0, 0.0) == doctest::Approx(300.0)); + } +} + +TEST_CASE("test mode: the end conditions") +{ + simulation::test_limits limits; + simulation::test_progress progress; + + SUBCASE("nothing happens, nothing ends") + { + CHECK_FALSE(simulation::evaluate_frame(progress, frame(false), limits).has_value()); + CHECK(progress.time == doctest::Approx(0.03)); + CHECK(progress.resttime == doctest::Approx(0.0)); + } + SUBCASE("a cell nobody wrote to says nothing") + { + CHECK_FALSE(simulation::evaluate_frame(progress, frame(false, -1.0), limits).has_value()); + CHECK_FALSE(simulation::evaluate_frame(progress, frame(false, 2.0), limits).has_value()); + CHECK_FALSE(progress.passrequested); + } + SUBCASE("0 fails at once, with the text of the cell") + { + auto const verdict{simulation::evaluate_frame(progress, frame(false, 0.0, "wrong_track"), limits)}; + REQUIRE(verdict.has_value()); + CHECK(verdict->result == test_result::fail); + CHECK(verdict->reason == "__test.result set to 0 (wrong_track)"); + } + SUBCASE("1 passes once the tested trains have stood for a second") + { + CHECK_FALSE(simulation::evaluate_frame(progress, frame(false, 1.0), limits).has_value()); + CHECK(progress.passrequested); + // still moving: the pass waits + CHECK_FALSE(run(progress, limits, 30.0, frame(false, 1.0)).has_value()); + auto const stoppedat{progress.time}; + auto const verdict{run(progress, limits, 60.0, frame(true, 1.0))}; + REQUIRE(verdict.has_value()); + CHECK(verdict->result == test_result::pass); + CHECK(progress.time - stoppedat == doctest::Approx(simulation::pass_settle_time).epsilon(0.05)); + } + SUBCASE("the pass stays requested when the cell changes back") + { + CHECK_FALSE(simulation::evaluate_frame(progress, frame(false, 1.0), limits).has_value()); + auto const verdict{run(progress, limits, 60.0, frame(true, -1.0))}; + REQUIRE(verdict.has_value()); + CHECK(verdict->result == test_result::pass); + } + SUBCASE("1 with test.no_movement_before_pass no passes at once") + { + limits.no_movement_before_pass = false; + auto const verdict{simulation::evaluate_frame(progress, frame(false, 1.0, "reached_the_stop"), limits)}; + REQUIRE(verdict.has_value()); + CHECK(verdict->result == test_result::pass); + CHECK(verdict->reason == "__test.result set to 1 (reached_the_stop)"); + } + SUBCASE("standing still without a result ends after test.standstill") + { + limits.standstill = 120.0; + auto const verdict{run(progress, limits, 1000.0, frame(true))}; + REQUIRE(verdict.has_value()); + CHECK(verdict->result == test_result::standstill); + CHECK(progress.time == doctest::Approx(120.0).epsilon(0.001)); + } + SUBCASE("a move starts the standstill over") + { + limits.standstill = 120.0; + CHECK_FALSE(run(progress, limits, 100.0, frame(true)).has_value()); + CHECK_FALSE(simulation::evaluate_frame(progress, frame(false), limits).has_value()); + CHECK(progress.resttime == doctest::Approx(0.0)); + CHECK_FALSE(run(progress, limits, 200.0, frame(true)).has_value()); + } + SUBCASE("test.pass_on_standstill makes the standstill a pass") + { + limits.standstill = 10.0; + limits.pass_on_standstill = true; + auto const verdict{run(progress, limits, 1000.0, frame(true))}; + REQUIRE(verdict.has_value()); + CHECK(verdict->result == test_result::pass); + } + SUBCASE("test.standstill 0 never ends the test") + { + limits.standstill = 0.0; + limits.timeout = 0.0; + CHECK_FALSE(run(progress, limits, 5000.0, frame(true)).has_value()); + } + SUBCASE("no result by test.timeout") + { + limits.timeout = 240.0; + auto const verdict{run(progress, limits, 1000.0, frame(false))}; + REQUIRE(verdict.has_value()); + CHECK(verdict->result == test_result::timeout); + CHECK(verdict->reason == "no result after 240 s"); + CHECK(progress.time == doctest::Approx(240.0).epsilon(0.001)); + } + SUBCASE("a result in the last frame wins over the timeout") + { + limits.timeout = 0.03; + auto const verdict{simulation::evaluate_frame(progress, frame(false, 0.0), limits)}; + REQUIRE(verdict.has_value()); + CHECK(verdict->result == test_result::fail); + } +} + +TEST_CASE("test mode: what ended a run the simulator didn't end") +{ + SUBCASE("an exception says what it was") + { + CHECK(simulation::describe_exception(std::make_exception_ptr(std::runtime_error("no scenery"))) == "critical error: no scenery"); + // e.g. std::stof on a token of a scenery which isn't a number + CHECK(simulation::describe_exception(std::make_exception_ptr(std::invalid_argument("stof"))) == "critical error: stof"); + CHECK(simulation::describe_exception(std::make_exception_ptr(std::bad_alloc())).starts_with("critical error: ")); + } + SUBCASE("something thrown which isn't a std::exception") + { + CHECK(simulation::describe_exception(std::make_exception_ptr(42)) == "critical error: an exception of unknown type"); + } + SUBCASE("terminated without an exception") + { + CHECK(simulation::describe_exception(nullptr) == "the simulator was terminated"); + } +} diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index b4f2e14be..8957a4a0e 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -456,6 +456,66 @@ bool global_settings::ConfigParseInput(cParser& Parser, const std::string& token return false; } +bool global_settings::ConfigParseTest(cParser& Parser, std::string_view const Token) +{ + // integration test limits, usually given in the config block of a test scenery + if (Token == "test.timeout") + { + double value{ test.timeout }; + ParseOne(Parser, value); + if (false == test.timeout_from_command_line) + test.timeout = std::max(0.0, value); + return true; + } + + if (Token == "test.wall_timeout") + { + double value{ 0.0 }; + ParseOne(Parser, value); + if (false == test.wall_timeout_from_command_line) + test.wall_timeout = std::max(0.0, value); + return true; + } + + if (Token == "test.vehicle") + { + // a vehicle of a train under test, by its name in the scenery; the pass and the standstill wait for its + // consist only, not for other trains (eu07/maszyna#541). given more than once, for each of them + std::string name; + ParseOne(Parser, name, 1, true); + if (false == name.empty() && std::ranges::find(test.vehicles, name) == test.vehicles.end()) + test.vehicles.emplace_back(name); + return true; + } + + if (Token == "test.standstill") + { + ParseOne(Parser, test.standstill); + test.standstill = std::max(0.0, test.standstill); + return true; + } + + if (Token == "test.pass_on_standstill") + { + ParseOne(Parser, test.pass_on_standstill); + return true; + } + + if (Token == "test.fail_if_stop_overran") + { + ParseOne(Parser, test.fail_if_stop_overran); + return true; + } + + if (Token == "test.no_movement_before_pass") + { + ParseOne(Parser, test.no_movement_before_pass); + return true; + } + + return false; +} + bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& token) { if (token == "async.trainThreads") @@ -1316,6 +1376,9 @@ void global_settings::ConfigParse(cParser& Parser) if (ConfigParseDebug(Parser, token)) continue; + if (ConfigParseTest(Parser, token)) + continue; + // WriteLog(std::format("Unknown config token: {}", token), logtype::warning, false); } while (true); @@ -1742,6 +1805,17 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "gfx.shadowmap.enabled", gfx_shadowmap_enabled ); // TODO: export fpslimit export_as_text( Output, "randomseed", Global.random_seed ); + export_as_text( Output, "test.timeout", test.timeout ); + if( test.wall_timeout.has_value() ) { + export_as_text( Output, "test.wall_timeout", *test.wall_timeout ); + } + export_as_text( Output, "test.standstill", test.standstill ); + export_as_text( Output, "test.pass_on_standstill", test.pass_on_standstill ); + export_as_text( Output, "test.fail_if_stop_overran", test.fail_if_stop_overran ); + export_as_text( Output, "test.no_movement_before_pass", test.no_movement_before_pass ); + for( auto const &vehicle : test.vehicles ) { + export_as_text( Output, "test.vehicle", vehicle ); + } export_as_text( Output, "gfx.envmap.enabled", gfx_envmap_enabled ); export_as_text( Output, "gfx.postfx.motionblur.enabled", gfx_postfx_motionblur_enabled ); export_as_text( Output, "gfx.postfx.motionblur.shutter", gfx_postfx_motionblur_shutter ); diff --git a/utilities/Globals.h b/utilities/Globals.h index ad216cd96..50e42085b 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/utilities.h" #include "utilities/motiontelemetry.h" #include +#include #ifdef WITH_UART #include "utilities/uart.h" @@ -86,6 +87,20 @@ struct global_settings { std::string local_start_vehicle{ "EU07-424" }; std::string editor_enter_vehicle; // vehicle placed in the editor, to be entered when the driving goes on bool editor_session{ false }; // scenery was opened for editing (-edit): loader keeps track of scenery layers and hands over to the editor + // integration test mode (--test, eu07/maszyna#541); the limits can be set in the scenery's config block (test.*) + struct test_settings { + bool enabled{ false }; + double timeout{ 3600.0 }; // [s] of simulation time; no result by then fails the test + double standstill{ 300.0 }; // [s] the tested trains standing still this long ends the test; 0 = never + bool pass_on_standstill{ false }; // the standstill end passes the test instead of failing it + bool fail_if_stop_overran{ true }; // a train passing a signal at stop ends the test as failed; with false it's only logged + bool no_movement_before_pass{ true }; // a pass waits until the tested trains stand still; with false it ends the test at once + std::vector vehicles; // test.vehicle: the trains under test are the consists of these; none = every vehicle + bool timeout_from_command_line{ false }; // --test-timeout given, the scenery doesn't override it + std::optional wall_timeout; // [s] of real time since the start; none = test.timeout + time for the loading, 0 = no limit + bool wall_timeout_from_command_line{ false }; // --test-wall-timeout given, the scenery doesn't override it + }; + test_settings test; bool editor_tracks{ false }; // F3 track workspace: draw and pick tracks without the scenery draw-range cap int iConvertModels{ 0 }; // tworzenie plików binarnych int iConvertIndexRange{ 1000 }; // range of duplicate vertex scan @@ -377,6 +392,7 @@ struct global_settings { bool ConfigParseNetwork(cParser& Parser, const std::string& token); bool ConfigParseHardware(cParser& Parser, const std::string& token); bool ConfigParseDebug(cParser& Parser, const std::string& token); + bool ConfigParseTest(cParser& Parser, std::string_view Token); bool ConfigParse_gfx( cParser &parser, std::string_view const Token ); // sends basic content of the class in legacy (text) format to provided stream void diff --git a/utilities/Logs.cpp b/utilities/Logs.cpp index 38094d72e..e0949aba6 100644 --- a/utilities/Logs.cpp +++ b/utilities/Logs.cpp @@ -125,8 +125,8 @@ void LogService() if (log_scrollback.size() > 200) log_scrollback.pop_front(); - // log to console - if (Global.iWriteLogEnabled & 2) + // log to console; not in test mode, where the standard output carries the result of the test only + if ((Global.iWriteLogEnabled & 2) && false == Global.test.enabled) { if (isError) printf("\033[1;37;41m%s\033[0m\n", msg.c_str()); diff --git a/utilities/Timer.cpp b/utilities/Timer.cpp index 0b877f2b4..eacd2ba0a 100644 --- a/utilities/Timer.cpp +++ b/utilities/Timer.cpp @@ -27,6 +27,17 @@ double fSoundTimer{ 0.0 }; double fSinceStart{ 0.0 }; double override_delta = -1.0f; +namespace { + +// when set (test mode), every frame lasts this long, see set_fixed_step() +double &fixed_step() +{ + static double step{0.0}; + return step; +} + +} // namespace + double GetTime() { return fSimulationTime; @@ -53,6 +64,11 @@ void set_delta_override(double t) override_delta = t; } +void set_fixed_step(double t) +{ + fixed_step() = t; +} + void ResetTimers() { UpdateTimers( Global.iPause != 0 ); @@ -76,10 +92,16 @@ void UpdateTimers(bool pause) fr = 1000000000; #endif DeltaRenderTime = double(count - oldCount) / double(fr); + auto const fixedstep{fixed_step()}; + if (fixedstep > 0.0) + { + // the frame lasts exactly as long as said, however long it took to compute; the time speed doesn't apply + DeltaRenderTime = fixedstep; + } fRenderTime += DeltaRenderTime; if (!pause) { - DeltaTime = Global.fTimeSpeed * DeltaRenderTime; + DeltaTime = (fixedstep > 0.0 ? 1.0 : Global.fTimeSpeed) * DeltaRenderTime; fSoundTimer += DeltaTime; if (fSoundTimer > 0.1) fSoundTimer = 0.0; diff --git a/utilities/Timer.h b/utilities/Timer.h index 32e170a46..15fdd4f6b 100644 --- a/utilities/Timer.h +++ b/utilities/Timer.h @@ -19,6 +19,9 @@ double GetDeltaRenderTime(); void set_delta_override(double v); +// every frame advances the clocks by this many seconds, whatever the real time; 0 = real time (test mode) +void set_fixed_step(double v); + void ResetTimers(); void UpdateTimers(bool pause); diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 9f83b6d53..709cb0e43 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -26,6 +26,7 @@ http://mozilla.org/MPL/2.0/. #include "world/MemCell.h" #include "simulation/simulation.h" #include "simulation/simulationtime.h" +#include "simulation/simulationtest.h" #include "world/Track.h" #include "world/station.h" #include "input/keyboardinput.h" @@ -767,6 +768,7 @@ void TController::TableCheck(double fDistance) { // aktualizacja rekordów z wyjątkiem ostatniego if (sSpeedTable[i].iFlags & spEnabled) // jeśli pozycja istotna { + auto const passedbefore { TestFlag( sSpeedTable[ i ].iFlags, spElapsed ) }; if (sSpeedTable[i].Update()) { if( Global.iWriteLogEnabled & 8 ) { @@ -793,6 +795,8 @@ void TController::TableCheck(double fDistance) // nie kontynuujemy pętli, trzeba doskanować ciąg dalszy break; } + // a switch is a track, the update of a signal never ends the loop above + CheckSignalPassedAtDanger( 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 @@ -816,7 +820,10 @@ 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 ); // WriteLog("TableCheck: Upate last track. Dist=" + AnsiString(sSpeedTable[iLast].fDist)); if( sSpeedTable[ iLast ].fDist < fDistance ) { TableTraceRoute( fDistance, pVehicles[ end::rear ] ); // doskanowanie dalszego odcinka @@ -827,8 +834,71 @@ void TController::TableCheck(double fDistance) } }; +namespace { + +// the position is a signal showing stop (SetVelocity 0 0). SetVelocity 0 x on automatic block isn't: it may be passed after a stop +bool is_signal_at_stop( TSpeedPos const &Point ) +{ + return TestFlag( Point.iFlags, spEvent ) + && TestFlag( Point.iFlags, spSemaphor ) + && false == TestFlag( Point.iFlags, spStopOnSBL ) + && std::abs( Point.fVelNext ) <= 0.0 + && Point.evEvent != nullptr; +} + +} // namespace + +void TController::CheckSignalPassedAtDanger( 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 + simulation::test().signal_passed_at_danger( OwnerName(), Point.evEvent->m_name ); + } +} + auto const passengerstopmaxdistance { 400.0 }; // maximum allowed distance between passenger stop point and consist head +// the margins of the final approach of a train [m], closer than the first of which it stops at once and within the +// second of which it makes its final approach: at a passenger stop, or at a signal and anything else it stops at +std::pair +TController::train_proximity_margins( bool const Passengerstop ) const { + + auto minproximity { 0.0 }; + auto maxproximity { 0.0 }; + if( Passengerstop ) { + // a stop point keeps the margins its W4 parameters (head or middle of the consist, + // platform length) were set up against + minproximity = std::clamp( 5 + iVehicles, 10, 15 ) + ( IsCargoTrain ? 10.0 : 0.0 ); + maxproximity = std::clamp( 10 + iVehicles, 15, 40 ); + } + else { + // a train stops 8-15 m short of the signal whatever its length; closer than the lower + // edge it stops at once, the braking aims at the middle of the window, 3.5 m further out + minproximity = 8.0; + // the final approach begins far enough out to slow from the coasting speed down to the window, + // and at the same distance whether the train is moving or standing, so it does not flip + // between creeping closer and holding still when it comes to a stand short of the window + maxproximity = 50.0; + } + if( IsCargoTrain ) { + // start the final approach earlier for cargo trains, their brakes react more slowly + maxproximity += 10.0; + } + if( ( Global.FrictionWeatherFactor < 1.f ) + && ( iVehicles > 1 ) ) { + // take into account weather conditions + minproximity += 5.0; + maxproximity += 5.0; + } + if( mvOccupied->Vel < 0.1 ) { + // jak stanie za daleko, to niech nie dociąga paru metrów + maxproximity = std::max( maxproximity, 50.0 ); + } + return { minproximity, maxproximity }; +} + TCommandType TController::TableUpdate(double &fVelDes, double &fDist, double &fNext, double &fAcc) { // ustalenie parametrów, zwraca typ komendy, jeśli sygnał podaje prędkość do jazdy // fVelDes - prędkość zadana @@ -843,6 +913,7 @@ TCommandType TController::TableUpdate(double &fVelDes, double &fDist, double &fN auto speedlimitiscontinuous { true }; // stays true if potential active speed limit is unbroken to the last (relevant) point in scan table eSignNext = nullptr; IsAtPassengerStop = false; + bStopTargetIsPassengerStop = false; IsScheduledPassengerStopVisible = false; mvOccupied->SecuritySystem.set_cabsignal_lock(false); // te flagi są ustawiane tutaj, w razie potrzeby @@ -962,6 +1033,7 @@ TCommandType TController::TableUpdate(double &fVelDes, double &fDist, double &fN fAcc = a; // zalecane przyspieszenie (nie musi być uwzględniane przez AI) fNext = v; // istotna jest prędkość na końcu tego odcinka fDist = d; // dlugość odcinka + bStopTargetIsPassengerStop = ( v == 0.0 ) && ( ( point.iFlags & spPassengerStopPoint ) != 0 ); } /* else if ((fAcc > 0) && (v >= 0) && (v > fNext)) { @@ -976,6 +1048,7 @@ TCommandType TController::TableUpdate(double &fVelDes, double &fDist, double &fN fAcc = a; fNext = v; fDist = d; + bStopTargetIsPassengerStop = ( v == 0.0 ) && ( ( point.iFlags & spPassengerStopPoint ) != 0 ); } // potentially update our current speed limit if( v < VelLimitLastDist.first @@ -1152,14 +1225,20 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub return true; } + // the margins of a passenger stop, whether the train is already making for it or not: the present ones + // depend on this very flag, and a check against them would turn it on and off every other scan + auto const [ stopminproximity, stopmaxproximity ] { + is_train() ? + train_proximity_margins( true ) : + std::pair{ fMinProximityDist, fMaxProximityDist } }; IsAtPassengerStop = Point.fDist <= passengerstopmaxdistance // Ra 2F1I: odległość plus długość pociągu musi być mniejsza od długości // peronu, chyba że pociąg jest dłuższy, to wtedy minimalna. // jeśli długość peronu ((sSpeedTable[i].evEvent->ValueGet(2)) nie podana, // przyjąć odległość fMinProximityDist && ((iDrivigFlags & moveStopCloser) != 0 ? - Point.fDist + fLength + (Point.fMoved - fMinProximityDist * 0.5f) <= std::max(std::abs(Point.evEvent->input_value(2)), - 2.0 * fMaxProximityDist + fLength) : // fmaxproximitydist typically equals ~50 m + Point.fDist + fLength + (Point.fMoved - stopminproximity * 0.5f) <= std::max(std::abs(Point.evEvent->input_value(2)), + 2.0 * stopmaxproximity + fLength) : Point.fDist < Signaldistance); if( !eSignNext ) { @@ -1880,7 +1959,8 @@ TController::TController(bool AI, TDynamicObject *NewControll, bool InitPsyche, << "BrakePress[MPa] PipePress[MPa] MotorCurrent[A] " << "MCP SCP BCP LBP Direction Command CVal1 CVal2 " << "Security Wheelslip " - << "EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg]" + << "EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg] " + << "VehicleVelocity[km/h]" << "\r\n"; LogFile << std::fixed << std::setprecision( 4 ); #endif @@ -4837,7 +4917,9 @@ void TController::PhysicsLog() << mvControlling->dizel_heat.Ts << " " << mvControlling->dizel_heat.To << " " << mvControlling->dizel_heat.temperatura1 << " " - << mvControlling->dizel_heat.temperatura2 + << mvControlling->dizel_heat.temperatura2 << " " + // vehicle speed [km/h], the second column is the wheel speed, which stays at 0 while the wheels are locked in a skid + << mvOccupied->V * 3.6 << "\r\n"; #endif #if LOGPRESS == 1 @@ -5937,6 +6019,7 @@ TController::determine_consist_state() { IsAnyDoorOnlyOpen[ side::right ] = IsAnyDoorOnlyOpen[ side::left ] = false; IsAnyDoorOpen[ side::right ] = IsAnyDoorOpen[ side::left ] = false; IsAnyDoorPermitActive[ side::right ] = IsAnyDoorPermitActive[ side::left ] = false; + auto doorclosure { door_closure_t::none }; ConsistShade = 0.0; auto *p { pVehicles[ end::front ] }; // pojazd na czole składu double dy; // składowa styczna grawitacji, w przedziale <0,1> @@ -5998,12 +6081,14 @@ TController::determine_consist_state() { IsAnyDoorPermitActive[ side::right ] |= rightdoor.open_permit; IsAnyDoorPermitActive[ side::left ] |= leftdoor.open_permit; } + doorclosure = std::max( doorclosure, vehicle->door_closure() ); } // measure lighting level // TBD: apply weight (multiplier) to partially lit vehicles? ConsistShade += p->fShade > 0.0 ? p->fShade : 1.0; p = p->Next(); // pojazd podłączony z tyłu (patrząc od czoła) } + DoorClosure = doorclosure; // calculate average amount of received sunlight ConsistShade /= iVehicles; @@ -6677,47 +6762,9 @@ TController::determine_proximity_ranges() { // na jaka odleglosc i z jaka predkoscia ma podjechac do przeszkody // jeśli pociąg if( is_train() ) { - if( IsAtPassengerStop ) { - // a stop point keeps the margins its W4 parameters (head or middle of the consist, - // platform length) were set up against - fMinProximityDist = std::clamp( 5 + iVehicles, 10, 15 ); - fMaxProximityDist = std::clamp( 10 + iVehicles, 15, 40 ); - if( IsCargoTrain ) { - fMinProximityDist += 10.0; - } - } - else { - // a train stops 8-15 m short of the signal whatever its length; closer than the lower - // edge it stops at once, the braking aims at the middle of the window, 3.5 m further out - fMinProximityDist = 8.0; - // the final approach begins far enough out to slow from the coasting speed down to the window, - // and at the same distance whether the train is moving or standing, so it does not flip - // between creeping closer and holding still when it comes to a stand short of the window - fMaxProximityDist = 50.0; - } - - if( IsCargoTrain ) { - // start the final approach earlier for cargo trains, their brakes react more slowly - fMaxProximityDist += 10.0; -/* - if( IsHeavyCargoTrain ) { - // cargo trains with high braking threshold may require even larger safety margin - fMaxProximityDist += 20.0; - } -*/ - } - - if (Global.FrictionWeatherFactor < 1.f - && iVehicles > 1 ) { - // take into account weather conditions - fMinProximityDist += 5.0; - fMaxProximityDist += 5.0; - } - - if( mvOccupied->Vel < 0.1 ) { - // jak stanie za daleko, to niech nie dociąga paru metrów - fMaxProximityDist = std::max( fMaxProximityDist, 50.0 ); - } + auto const [ minproximity, maxproximity ] { train_proximity_margins( IsAtPassengerStop ) }; + fMinProximityDist = minproximity; + fMaxProximityDist = maxproximity; if( iDrivigFlags & moveLate ) { // jeśli spóźniony, to gna @@ -7370,6 +7417,10 @@ TController::adjust_desired_speed_for_obstacles() { if( k - vel < 5 ) { // porównanie modułów prędkości [km/h] // zatroszczyć się trzeba, jeśli tamten nie jedzie znacząco szybciej + if( Obstacle.distance < ActualProximityDist ) { + // the vehicle ahead, not the stop found by the scan, is what the train is now to stop short of + bStopTargetIsPassengerStop = false; + } ActualProximityDist = std::min( ActualProximityDist, Obstacle.distance ); @@ -7713,9 +7764,14 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { auto room { brakingroom }; if( ( AbsAccS <= 0.0 ) && ( BrakeCtrlPosition <= 0.0 ) ) { // only while the brake is still to be applied: once it acts, the room it needs to respond is behind + // making for a passenger stop only the time the brake takes to fill: the look-ahead the horizon + // keeps as its floor was set for the stops at a signal, and a vehicle that brakes with its + // independent brake, which leaves the train brake handle where it is, kept braking earlier + // and earlier the whole way in, to crawl the last tens of metres to the stop point + auto const brakea0 { bStopTargetIsPassengerStop ? 1.0 : fBrake_a0[ 0 ] }; room = std::max( 0.5 * brakingroom, - brakingroom - vel / 3.6 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ); + brakingroom - vel / 3.6 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, brakea0 ) ); } deceleration = shaped_braking_demand( velocity, VelNext, room + 0.1 / 25.92, fAccGravity, diff --git a/vehicle/Driver.h b/vehicle/Driver.h index f1975fca6..38b4c9e28 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -312,6 +312,7 @@ private: return mvOccupied->Vel * sign( iDirection * mvOccupied->V ); } double brake_increase_margin() const; + std::pair train_proximity_margins( bool const Passengerstop ) const; void update_timers( double const dt ); void update_logs( double const dt ); @@ -403,6 +404,7 @@ private: double VelDesired = 0.0; // predkość, z jaką ma jechać, wynikająca z analizy tableki; <=VelSignal double fAccDesiredAv = 0.0; // uśrednione przyspieszenie z kolejnych przebłysków świadomości, żeby ograniczyć migotanie double fBrakeHeldTime = 0.0; // czas od ostatniego przestawienia kranu hamulca zasadniczego + bool bStopTargetIsPassengerStop = false; // the point the train is to stop at, found by the last scan of the route, is a passenger stop double fShortfall = 0.0; // how far the train fell short of the braking demand at the last check [m/s2] double fShortfallTime = 0.0; // when that was checked int iShortfallGrowth = 0; // how many checks in a row the shortfall has grown @@ -533,6 +535,9 @@ private: bool TableNotFound( basic_event const *Event, double const Distance ) const; 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; 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 ); @@ -632,6 +637,7 @@ private: bool IsAnyDoorOpen[ 2 ]; // state of door in the consist bool IsAnyDoorOnlyOpen[ 2 ]; // state of door in the consist regardless of step bool IsAnyDoorPermitActive[ 2 ]; // state of door permit in the consist + door_closure_t DoorClosure{ door_closure_t::none }; // state of the door closure signalling (SOD) of the consist bool IsAnyLineBreakerOpen{ false }; // state of line breaker in all powered vehicles under control bool IsAnyConverterOverloadRelayOpen{ false }; // state of converter overload relays in all vehicles under control bool IsAnyMotorOverloadRelayOpen{ false }; // state of motor overload relays in all vehicles under control diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index d5f60e332..2da371e86 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -557,6 +557,33 @@ void TDynamicObject::UpdateAxle(TAnim *pAnim) pAnim->smAnimated->future_transform = glm::rotate((float)glm::radians(m_future_wheels_angle[wheel_id]), glm::vec3(1.0f, 0.0f, 0.0f)); }; +// angle of a slotted lever for given angle of the wheel, which drives the coupling rod with the pin +// all points of the coupling rod move along circles of crank radius, the lever follows the pin sliding in its slot +double TDynamicObject::slottedlever_angle( slottedlever_data const &Lever, double const Wheelangle ) { + // crank turned around X axis, the same way as the wheel + auto const wheelangle { glm::radians( Wheelangle ) }; + auto const crank { glm::dvec2( + Lever.crank.x * std::cos( wheelangle ) - Lever.crank.y * std::sin( wheelangle ), + Lever.crank.x * std::sin( wheelangle ) + Lever.crank.y * std::cos( wheelangle ) ) }; + auto const pin { Lever.pin - Lever.crank + crank }; + // angle relative to the pose of the lever in the model, which matches wheel angle 0 + return glm::degrees( std::atan2( pin.y, pin.x ) - std::atan2( Lever.pin.y, Lever.pin.x ) ); +} + +void TDynamicObject::update_slottedlevers() { + + for( auto const &lever : m_slottedlevers ) { + auto const wheelid { ( + ( lever.axle < iAnimType[ ANIM_WHEELS ] ) && ( lever.axle < static_cast( pAnimations.size() ) ) ? + pAnimations[ lever.axle ].dWheelAngle : + 1 ) }; // driving wheels and coupling rods by default + auto const angle { slottedlever_angle( lever, dWheelAngle[ wheelid ] ) }; + auto const futureangle { slottedlever_angle( lever, dWheelAngle[ wheelid ] + m_future_wheels_angle[ wheelid ] ) }; + lever.submodel->SetRotate( float3( 1, 0, 0 ), angle ); + lever.submodel->future_transform = glm::rotate( (float)glm::radians( std::remainder( futureangle - angle, 360.0 ) ), glm::vec3( 1.0f, 0.0f, 0.0f ) ); + } +} + // animacja drzwi - przesuw void TDynamicObject::UpdateDoorTranslate(TAnim *pAnim) { if( pAnim->smAnimated == nullptr ) { return; } @@ -841,6 +868,7 @@ void TDynamicObject::ABuLittleUpdate(double ObjSqrDist) animation.yUpdate( &animation ); } } + update_slottedlevers(); if( mdModel != nullptr && ObjSqrDist < 50 * 50 ) { @@ -5739,6 +5767,35 @@ void TDynamicObject::LoadMMediaFile( std::string const &TypeName, std::string co } } + else if( token == "animslottedlever:" ) { + // lever with a slot, turned by a pin on the coupling rod (e.g. speedometer drive) + // parameters: submodel name, number of axle (from animwheelprefix) driving the coupling rod, crank radius [m], + // pin position relative to the lever axis [m] and crank angle [deg] for wheel angle 0, in local (Y, Z) plane of the lever + // angle is measured from Y axis towards Z axis, i.e. in the direction of rotation around X axis + slottedlever_data lever; + int axle { 1 }; + double crankradius { 0.0 }; + double crankangle { 0.0 }; + parser.getTokens( 6, false ); + parser + >> asAnimName + >> axle + >> crankradius + >> lever.pin.x + >> lever.pin.y + >> crankangle; + lever.submodel = GetSubmodelFromName( mdModel, asAnimName ); + if( lever.submodel != nullptr ) { + lever.submodel->WillBeAnimated(); + lever.axle = std::max( 0, axle - 1 ); + lever.crank = crankradius * glm::dvec2( std::cos( glm::radians( crankangle ) ), std::sin( glm::radians( crankangle ) ) ); + m_slottedlevers.emplace_back( lever ); + } + else { + ErrorLog( "Bad model: " + asFileName + " - missed submodel " + asAnimName, logtype::model ); + } + } + else if( token == "animdoorprefix:" ) { // nazwa animowanych drzwi int i, j; diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index 577f0f020..9df2d93b6 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -316,6 +316,16 @@ private: TSubModel *sm_summer_variant {}; // submodel letniego wariantu TSubModel *sm_autumn_variant {}; // submodel jesiennego wariantu double fWahaczeAmp; + // levers with a slot, turned by a pin on the coupling rod sliding in the slot (e.g. speedometer drive of 401Da) + struct slottedlever_data { + TSubModel *submodel { nullptr }; // lever, turned around its local X axis like the wheels + int axle { 0 }; // index of the axle animation, whose crank drives the coupling rod + glm::dvec2 pin { 0.0 }; // pin position relative to the lever axis for wheel angle 0, in local (Y, Z) plane [m] + glm::dvec2 crank { 0.0 }; // crank vector for wheel angle 0, in local (Y, Z) plane [m] + }; + std::vector m_slottedlevers; + static double slottedlever_angle( slottedlever_data const &Lever, double const Wheelangle ); + void update_slottedlevers(); // Winger 160204 - pantografy double pantspeedfactor; // animacje typu przesuw diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 8fa708c29..3620257f1 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "vehicle/Train.h" #include "vehicle/horn.h" +#include "vehicle/linebreaker.h" #include "utilities/Globals.h" #include "simulation/simulation.h" @@ -3658,16 +3659,22 @@ void TTrain::OnCommand_linebreakertoggle(TTrain *Train, command_data const &Comm if (Command.action == GLFW_PRESS) { // press or hold... - if (Train->m_linebreakerstate == 0) + // a single two-state lever stays where it was put, also when the line breaker gets knocked out + auto const latchinglever{Train->ggMainOnButton.SubModel == nullptr && Train->ggMainOffButton.SubModel == nullptr && Train->ggMainButton.SubModel != nullptr && + Train->ggMainButton.type() == TGaugeType::toggle}; + switch (linebreaker::pressed(Train->m_linebreakerstate, latchinglever, Train->ggMainButton.GetDesiredValue() > 0.5)) { + case linebreaker::toggle::close: // ...to close the circuit // NOTE: bit of a dirty shortcut here OnCommand_linebreakerclose(Train, Command); - } - else if (Train->m_linebreakerstate == 1) - { - // ...to open the circuit + break; + case linebreaker::toggle::open: + // ...to open the circuit, or to put the lever off if the circuit was already opened OnCommand_linebreakeropen(Train, Command); + break; + default: + break; } } else if (Command.action == GLFW_RELEASE) @@ -9201,6 +9208,7 @@ bool TTrain::Update(double const Deltatime) btLampkaSprezarka.Turn(mvControlled->CompressorFlag); // mutopsitka dziala btLampkaSprezarkaOff.Turn(!mvControlled->CompressorFlag); btLampkaFuelPumpOff.Turn(!mvControlled->FuelPump.is_active); + btLampkaOilPump.Turn(mvControlled->OilPump.is_active); // pre-lubrication pump, goes off once the engine is started // boczniki // Ra: w SU45 boczniki wchodzą na ScndCtrlPos, a nie na ScndCtrlActualPos @@ -9272,7 +9280,17 @@ bool TTrain::Update(double const Deltatime) btLampkaDoorLeft.Turn(DynamicObject->Mechanik->IsAnyDoorOpen[cab_to_end() == end::front ? side::left : side::right]); btLampkaDoorRight.Turn(DynamicObject->Mechanik->IsAnyDoorOpen[cab_to_end() == end::front ? side::right : side::left]); } - btLampkaBlokadaDrzwi.Turn(mvOccupied->Doors.is_locked); + if (DynamicObject->Mechanik != nullptr) + { + // with DoorClosureSignal in the definition of the cab's vehicle, the door closure signalling (SOD) of the whole + // consist: lit with all doors closed and locked, blinking with them closed but released, dark with a door open. + // without it, the lock of the vehicle's own doors, as before + btLampkaBlokadaDrzwi.Turn(mvOccupied->door_closure_lamp_lit(DynamicObject->Mechanik->DoorClosure, simulation::Time.data().wSecond % 2 < 1)); + } + else + { + btLampkaBlokadaDrzwi.Turn(mvOccupied->Doors.is_locked); + } btLampkaDoorLockOff.Turn(false == mvOccupied->Doors.lock_enabled); btLampkaDepartureSignal.Turn(mvControlled->DepartureSignal); btLampkaNapNastHam.Turn(mvControlled->DirActive != 0 && mvOccupied->EpFuse); // napiecie na nastawniku hamulcowym @@ -9360,6 +9378,7 @@ bool TTrain::Update(double const Deltatime) btLampkaSprezarka.Turn(false); btLampkaSprezarkaOff.Turn(false); btLampkaFuelPumpOff.Turn(false); + btLampkaOilPump.Turn(false); btLampkaBezoporowa.Turn(false); btLampkaHamowanie1zes.Turn(false); btLampkaHamienie.Turn(false); @@ -11406,6 +11425,7 @@ void TTrain::clear_cab_controls() btLampkaSprezarkaOff.Clear(); btLampkaSprezarkaBOff.Clear(); btLampkaFuelPumpOff.Clear(); + btLampkaOilPump.Clear(); btLampkaNapNastHam.Clear(); btLampkaOporyB.Clear(); btLampkaStycznB.Clear(); @@ -11844,6 +11864,7 @@ bool TTrain::initialize_button(cParser &Parser, std::string const &Label, int co {"i-compressoroff:", btLampkaSprezarkaOff}, {"i-compressorboff:", btLampkaSprezarkaBOff}, {"i-fuelpumpoff:", btLampkaFuelPumpOff}, + {"i-oilpump:", btLampkaOilPump}, {"i-voltbrake:", btLampkaNapNastHam}, {"i-resistorsb:", btLampkaOporyB}, {"i-contactorsb:", btLampkaStycznB}, diff --git a/vehicle/Train.h b/vehicle/Train.h index a834d8ff0..8e57a1021 100644 --- a/vehicle/Train.h +++ b/vehicle/Train.h @@ -777,6 +777,7 @@ public: // reszta może by?publiczna TButton btLampkaSprezarkaOff; TButton btLampkaSprezarkaBOff; TButton btLampkaFuelPumpOff; + TButton btLampkaOilPump; TButton btLampkaBocznik1; TButton btLampkaBocznik2; TButton btLampkaBocznik3; diff --git a/vehicle/linebreaker.cpp b/vehicle/linebreaker.cpp new file mode 100644 index 000000000..97eae0325 --- /dev/null +++ b/vehicle/linebreaker.cpp @@ -0,0 +1,26 @@ +/* +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/linebreaker.h" + +namespace linebreaker { + +toggle pressed(int const Breakerstate, bool const Latchinglever, bool const Leveron) { + if (Breakerstate == 1) { + return toggle::open; + } + if (Breakerstate != 0) { + return toggle::none; + } + // the line breaker is open; a lever left on goes off first + return Latchinglever && Leveron ? toggle::open : toggle::close; +} + +} // namespace linebreaker diff --git a/vehicle/linebreaker.h b/vehicle/linebreaker.h new file mode 100644 index 000000000..370e992cc --- /dev/null +++ b/vehicle/linebreaker.h @@ -0,0 +1,28 @@ +/* +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 + +// the line breaker (main switch, WS) controls of the cab +namespace linebreaker { + +enum class toggle : int { + none, // the press does nothing + close, // the control goes to its on position, and the line breaker closes if it can + open // the control goes to its off position, and the line breaker opens +}; + +// what a press of the line breaker toggle does. Breakerstate is the state the cab keeps of the line +// breaker (0: open, 1: closed, 2: freshly closed). Latchinglever is a single two-state switch which +// stays where it was put, Leveron that it stands in its on position. such a lever doesn't follow the +// line breaker: knocked out (by the converter overload relay, say) the breaker opens with the lever +// left on, and the lever has to go off before it can be put on again (eu07/maszyna#394) +toggle pressed(int Breakerstate, bool Latchinglever, bool Leveron); + +} // namespace linebreaker