From 4575f61cc8d8919571a72c9023b7cd584c81cba1 Mon Sep 17 00:00:00 2001 From: Piotr Warszawski Date: Fri, 4 Sep 2026 15:52:38 +0200 Subject: [PATCH 01/90] whois: add lookahead mode for the timetable entry after the next stop Dispatcher scripts that route trains at a station based on where they go afterwards (e.g. a big terminal throat with several outbound directions) have no way to read that: whois +40 only exposes the current/next timetable entry (NextStop()/IsStop()), which while the train is at that very station is that station itself. Adds flags::whois_ahead (bit 6, previously unused) plus TTrainParameters::StationNameAhead(Offset)/StationIsStopAhead(Offset), returning the raw TimeTable[] entry Offset positions past StationIndex (name and whether it's a booked stop, "none"/false past the end of the timetable). The new whois branch calls both with Offset=1, mirroring the existing +40 branch's UpdateValues()/WriteLog() shape. Logic-verified with an isolated harness mirroring the touched types (TTrainParameters/TController/TMemCell shapes and the exact function bodies); not yet build-tested against the full engine locally - relying on CI for that. --- world/Event.cpp | 31 ++++++++++++++++++++++++++++++- world/Event.h | 1 + world/mtable.cpp | 16 ++++++++++++++++ world/mtable.h | 4 ++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/world/Event.cpp b/world/Event.cpp index eb679e720..e47dbea23 100644 --- a/world/Event.cpp +++ b/world/Event.cpp @@ -1018,13 +1018,42 @@ whois_event::run_() { auto *targetcell { static_cast( std::get( target ) ) }; if( targetcell == nullptr ) { continue; } // event effect code + // +64: station after the next one, unused, stop there or passthrough // +40: next station name, unused, stop at next station (duplicate of +0 2nd numeric value) // +32: vehicle name // +24: vehicle type, consist brake level, obstacle distance // +16: load type, load amount, max load amount // +8: destination, direction, engine power // +0: train name, station count, stop on next station - if( m_input.flags & flags::whois_name ) { + if( m_input.flags & flags::whois_ahead ) { + // +64 + // the timetable entry one past the next stop, and whether the train stops there + auto const *owner { ( + m_activator->Mechanik != nullptr && m_activator->Mechanik->primary() ? + m_activator->Mechanik : + m_activator->ctOwner ) }; + auto const aheadname { ( + owner != nullptr ? + owner->TrainTimetable().StationNameAhead( 1 ) : + "none" ) }; + auto const aheadstop { ( + owner != nullptr && owner->TrainTimetable().StationIsStopAhead( 1 ) ? + 1 : + 0 ) }; + + targetcell->UpdateValues( + aheadname, // station after the next one + 0, // unused + aheadstop, // stop there or passthrough + m_input.flags & ( flags::text | flags::value1 | flags::value2 ) ); + + WriteLog( + "Type: WhoIs (" + std::to_string( m_input.flags ) + ") - " + + "[station after next: " + aheadname + "], " + + "[X], " + + "[stop there: " + ( aheadstop != 0 ? "yes" : "no" ) + "]" ); + } + else if( m_input.flags & flags::whois_name ) { // +32 or +40 // next station name if( m_input.flags & flags::mode_alt ) { diff --git a/world/Event.h b/world/Event.h index 52ee3468d..55be65de1 100644 --- a/world/Event.h +++ b/world/Event.h @@ -37,6 +37,7 @@ public: mode_alt = 1 << 3, whois_load = 1 << 4, whois_name = 1 << 5, + whois_ahead = 1 << 6, // condition values track_busy = 1 << 3, track_free = 1 << 4, diff --git a/world/mtable.cpp b/world/mtable.cpp index cec54f870..75e6c8ad0 100644 --- a/world/mtable.cpp +++ b/world/mtable.cpp @@ -77,6 +77,22 @@ bool TTrainParameters::IsLastStop() const { return StationIndex >= StationCount; } +std::string TTrainParameters::StationNameAhead( int Offset ) const +{ // nazwa wpisu rozkladu Offset pozycji za aktualnym (bez pomijania przelotow) + int const idx { StationIndex + Offset }; + if( ( idx < 1 ) || ( idx > StationCount ) ) + return "none"; + return TimeTable[idx].StationName; +} + +bool TTrainParameters::StationIsStopAhead( int Offset ) const +{ // czy wpis Offset pozycji za aktualnym to zaplanowany postoj + int const idx { StationIndex + Offset }; + if( ( idx < 1 ) || ( idx > StationCount ) ) + return false; + return TimeTable[idx].Ah >= 0; //-1 to brak postoju +} + bool TTrainParameters::IsMaintenance() const { if( StationIndex <= StationCount ) diff --git a/world/mtable.h b/world/mtable.h index c58d120ff..701b0c6ca 100644 --- a/world/mtable.h +++ b/world/mtable.h @@ -70,6 +70,10 @@ class TTrainParameters std::string ShowRelation() const; double WatchMTable(double DistCounter); std::string NextStop() const; + // nazwa wpisu rozkladu Offset pozycji za StationIndex (Offset>=1); "none" poza koncem rozkladu + std::string StationNameAhead( int Offset ) const; + // true, gdy ten wpis to zaplanowany postoj (ma godzine przyjazdu), false dla przelotu / poza koncem + bool StationIsStopAhead( int Offset ) const; sound_source next_stop_sound() const; sound_source last_stop_sound() const; bool IsStop() const; From 5705062cd29826b9bb1386eb2ccfa5be258a3204 Mon Sep 17 00:00:00 2001 From: Piotr Warszawski Date: Fri, 4 Sep 2026 20:28:28 +0200 Subject: [PATCH 02/90] whois: select the lookahead mode via +48 instead of a new bit Per review: +48 (whois_name | whois_load, mode_alt clear) was an unused combination of the existing mode bits - use that instead of claiming a new bit 6. +56 (same, with mode_alt set) stays free for a future mode. No new flags::* entry needed; mtable.h/.cpp unchanged. --- world/Event.cpp | 8 +++++--- world/Event.h | 1 - 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/world/Event.cpp b/world/Event.cpp index e47dbea23..ebfc73958 100644 --- a/world/Event.cpp +++ b/world/Event.cpp @@ -1018,15 +1018,17 @@ whois_event::run_() { auto *targetcell { static_cast( std::get( target ) ) }; if( targetcell == nullptr ) { continue; } // event effect code - // +64: station after the next one, unused, stop there or passthrough + // +48: station after the next one, unused, stop there or passthrough // +40: next station name, unused, stop at next station (duplicate of +0 2nd numeric value) // +32: vehicle name // +24: vehicle type, consist brake level, obstacle distance // +16: load type, load amount, max load amount // +8: destination, direction, engine power // +0: train name, station count, stop on next station - if( m_input.flags & flags::whois_ahead ) { - // +64 + if( ( m_input.flags & ( flags::whois_name | flags::whois_load | flags::mode_alt ) ) + == ( flags::whois_name | flags::whois_load ) ) { + // +48 (the whois_name | whois_load combination was unused; +56, same with mode_alt + // set, stays free for a future mode) // the timetable entry one past the next stop, and whether the train stops there auto const *owner { ( m_activator->Mechanik != nullptr && m_activator->Mechanik->primary() ? diff --git a/world/Event.h b/world/Event.h index 55be65de1..52ee3468d 100644 --- a/world/Event.h +++ b/world/Event.h @@ -37,7 +37,6 @@ public: mode_alt = 1 << 3, whois_load = 1 << 4, whois_name = 1 << 5, - whois_ahead = 1 << 6, // condition values track_busy = 1 << 3, track_free = 1 << 4, From 93463a3850e50f8818d12c042a6ef7e3639b84f3 Mon Sep 17 00:00:00 2001 From: Piotr Warszawski Date: Fri, 4 Sep 2026 21:24:01 +0200 Subject: [PATCH 03/90] whois: consolidate the two lookahead accessors into one Per review: StationNameAhead()/StationIsStopAhead() duplicated the same bounds check for no reason. Replaced with a single TimeTableEntryAhead(Offset) returning the TMTableLine itself; the whois branch reads .StationName/.Ah off it directly. --- world/Event.cpp | 12 +++++------- world/mtable.cpp | 17 +++++------------ world/mtable.h | 7 +++---- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/world/Event.cpp b/world/Event.cpp index ebfc73958..319ad0591 100644 --- a/world/Event.cpp +++ b/world/Event.cpp @@ -1034,14 +1034,12 @@ whois_event::run_() { m_activator->Mechanik != nullptr && m_activator->Mechanik->primary() ? m_activator->Mechanik : m_activator->ctOwner ) }; - auto const aheadname { ( + auto const *entry { ( owner != nullptr ? - owner->TrainTimetable().StationNameAhead( 1 ) : - "none" ) }; - auto const aheadstop { ( - owner != nullptr && owner->TrainTimetable().StationIsStopAhead( 1 ) ? - 1 : - 0 ) }; + &owner->TrainTimetable().TimeTableEntryAhead( 1 ) : + nullptr ) }; + auto const aheadname { ( entry != nullptr ? entry->StationName : "none" ) }; + auto const aheadstop { ( entry != nullptr && entry->Ah >= 0 ? 1 : 0 ) }; targetcell->UpdateValues( aheadname, // station after the next one diff --git a/world/mtable.cpp b/world/mtable.cpp index 75e6c8ad0..dd84b6c23 100644 --- a/world/mtable.cpp +++ b/world/mtable.cpp @@ -77,20 +77,13 @@ bool TTrainParameters::IsLastStop() const { return StationIndex >= StationCount; } -std::string TTrainParameters::StationNameAhead( int Offset ) const -{ // nazwa wpisu rozkladu Offset pozycji za aktualnym (bez pomijania przelotow) +TMTableLine const &TTrainParameters::TimeTableEntryAhead( int Offset ) const +{ // wpis rozkladu Offset pozycji za aktualnym (bez pomijania przelotow); + // poza zakresem 1..StationCount zwraca techniczny wpis zerowy int const idx { StationIndex + Offset }; if( ( idx < 1 ) || ( idx > StationCount ) ) - return "none"; - return TimeTable[idx].StationName; -} - -bool TTrainParameters::StationIsStopAhead( int Offset ) const -{ // czy wpis Offset pozycji za aktualnym to zaplanowany postoj - int const idx { StationIndex + Offset }; - if( ( idx < 1 ) || ( idx > StationCount ) ) - return false; - return TimeTable[idx].Ah >= 0; //-1 to brak postoju + return TimeTable[0]; + return TimeTable[idx]; } diff --git a/world/mtable.h b/world/mtable.h index 701b0c6ca..2d20a27bb 100644 --- a/world/mtable.h +++ b/world/mtable.h @@ -70,10 +70,9 @@ class TTrainParameters std::string ShowRelation() const; double WatchMTable(double DistCounter); std::string NextStop() const; - // nazwa wpisu rozkladu Offset pozycji za StationIndex (Offset>=1); "none" poza koncem rozkladu - std::string StationNameAhead( int Offset ) const; - // true, gdy ten wpis to zaplanowany postoj (ma godzine przyjazdu), false dla przelotu / poza koncem - bool StationIsStopAhead( int Offset ) const; + // wpis rozkladu Offset pozycji za StationIndex (Offset>=1); poza zakresem 1..StationCount + // zwraca techniczny wpis zerowy (TimeTable[0], StationName=="nowhere", Ah==-1) + TMTableLine const &TimeTableEntryAhead( int Offset ) const; sound_source next_stop_sound() const; sound_source last_stop_sound() const; bool IsStop() const; From 36d4f16c01abd9404c7e3b59547260f33191b0ba Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sun, 27 Sep 2026 17:50:07 +0200 Subject: [PATCH 04/90] Brake force table: take the rapid step threshold from the distributor TMoverParameters::BrakeForceR() halves the force of a vehicle set to R below 40 km/h. That is one approximation for every distributor, but a NESt3/ESt4 distributor with a rapid relay engages the step above 70 km/h and keeps it down to 50 km/h (TNESt3::GetPF, RapidStatus), and below that its cylinder pressure is divided by the relay's own multiplier (TRapid, RapidMult). For a vehicle braking from speed the full force therefore lasts down to 50 km/h, not 40. The AI builds its consist brake table (fBrake_a0/a1) from BrakeForceR(), so between 40 and 50 km/h it expected twice the force the wagons actually deliver. Add TBrake::GetRapidRatio(), which keeps the old approximation as the default, and override it in TNESt3 with the relay's threshold and multiplier. The brake itself is unchanged; only the prediction is. --- McZapkie/Mover.cpp | 4 ++-- McZapkie/Oerlikon_ESt.cpp | 11 +++++++++++ McZapkie/Oerlikon_ESt.h | 2 ++ McZapkie/hamulce.h | 5 +++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 83c918e24..a58878ab4 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -5166,8 +5166,8 @@ double TMoverParameters::BrakeForceR(double ratio, double velocity) press = MaxBrakePress[3]; if (DynamicBrakeType == dbrake_automatic) ratio = ratio + (1.5 - ratio) * std::min(1.0, Vel * 0.02); - if (BrakeDelayFlag & bdelay_R && BrakeMethod % 128 != bp_Cosid && BrakeMethod % 128 != bp_D1 && BrakeMethod % 128 != bp_D2 && Power < 1 && velocity < 40) - ratio = ratio / 2; + if (BrakeDelayFlag & bdelay_R && BrakeMethod % 128 != bp_Cosid && BrakeMethod % 128 != bp_D1 && BrakeMethod % 128 != bp_D2 && Power < 1) + ratio *= Hamulec->GetRapidRatio(velocity); if (TrainType == dt_DMU && velocity < 30.0) { ratio -= 0.3; diff --git a/McZapkie/Oerlikon_ESt.cpp b/McZapkie/Oerlikon_ESt.cpp index 9559d3052..d8d5b3da8 100644 --- a/McZapkie/Oerlikon_ESt.cpp +++ b/McZapkie/Oerlikon_ESt.cpp @@ -324,6 +324,17 @@ void TNESt3::Init( double const PP, double const HPP, double const LPP, double c RapidStaly = false; } +double TNESt3::GetRapidRatio(double const Vel) const +{ + // ESt3 has no rapid relay, and with disc brakes the rapid step stays engaged at all speeds + if( ( PrzekRapid == nullptr ) || ( true == RapidStaly ) ) { + return 1.0; + } + // a braking vehicle comes from above, so the step is still engaged down to the speed + // at which GetPF() drops it, not the one at which it engages + return ( Vel < 50.0 ? 1.0 / PrzekRapid->GetRapidMult() : 1.0 ); +} + double TNESt3::GetCRP() { return CntrlRes->P(); diff --git a/McZapkie/Oerlikon_ESt.h b/McZapkie/Oerlikon_ESt.h index c12691851..180c47206 100644 --- a/McZapkie/Oerlikon_ESt.h +++ b/McZapkie/Oerlikon_ESt.h @@ -117,6 +117,7 @@ class TRapid : public TPrzekladnik { public: void SetRapidParams(double mult, double size); void SetRapidStatus(bool rs); + double GetRapidMult() const { return RapidMult; } void Update(double dt) /*override*/; inline TRapid() : TPrzekladnik() @@ -213,6 +214,7 @@ class TNESt3 : public TBrake { virtual double GetPF( double const PP, double const dt, double const Vel ) /*override*/; // przeplyw miedzy komora wstepna i PG void EStParams(double i_crc); // parametry charakterystyczne dla ESt virtual double GetCRP() /*override*/; + double GetRapidRatio(double const Vel) const override; void CheckState(double const BCP, double &dV1); // glowny przyrzad rozrzadczy void CheckReleaser(double const dt); // odluzniacz double CVs(double const BP); // napelniacz sterujacego diff --git a/McZapkie/hamulce.h b/McZapkie/hamulce.h index a05a36359..11b8bdfb3 100644 --- a/McZapkie/hamulce.h +++ b/McZapkie/hamulce.h @@ -442,6 +442,11 @@ class TBrake /// Sets the rapid step ratio. Default no-op; overridden where supported. /// Rapid ratio. virtual void SetRM(double const RMR) {}; // ustalenie przelozenia rapida + /// Share of the full cylinder pressure the distributor delivers at the given speed + /// while the vehicle is slowing down through it, for a vehicle set to R. Used only to predict + /// the brake force; the default is the approximation BrakeForceR() has always used. + /// Vehicle velocity [km/h]. + virtual double GetRapidRatio(double const Vel) const { return Vel < 40.0 ? 0.5 : 1.0; } /// Sets the velocity threshold for the rapid step. /// Velocity threshold (same unit as Vel passed to GetPF). virtual void SetRV(double const RVR) From 92bd7ef827196e0ae7f3dfa6e5cbabf93eb87390 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Mon, 28 Sep 2026 15:45:10 +0200 Subject: [PATCH 05/90] AI braking: hold the speed on a descent Two changes that only work together: the first on its own lets a heavy freight train brake less on a descent than it needs (in testing an ET42 with 30 loaded wagons overran the stop signal by 16-48 m at 20 per mille), the second makes the hold on a descent what it should be. The comparison that decides whether the AI may skip its reaction time between two brake steps stays on the brake-only demand, as before: it is about how much brake is asked for, and a heavy train in G approaching a stop on a descent needs its brake built up at once. Compared in the resultant frame, that train built up its brake one step per reaction time and overran the signal (16 copies of the ET42 train, x20: 22 of 32 overruns with the comparison moved, 0 of 48 with it kept). 1. Compare the measured deceleration in the same frame. Going downhill, adjust_desired_speed_for_current_speed() subtracts the gravity component from AccDesired, so that IncBrake() sizes the handle position for what the brake alone has to deliver. control_braking_force() then compares that brake-only value with AbsAccS, which is the resultant acceleration and already contains the gravity. On a 20 per mille descent the two differ by about 0.2 m/s2, so the AI asked for more braking than the target needed and then released, and repeated that. Keep the subtracted part in fAccDesiredGravity and add it back wherever AccDesired is compared with AbsAccS. It is zero everywhere else (flat, uphill, braking test), so the behaviour there does not change. 2. Hold the speed with one continuous law. On a descent the AI looks ahead to decide whether it will exceed the target speed. The look-ahead was (1 - a0) * 30 / 3.6 s, a few seconds, while a pneumatic brake in G takes tens of seconds to apply or to release; the AI therefore acted on an estimate the train had already left behind. The demand then jumped between the braking threshold above the target and a lerp towards full acceleration below it, and with the brake that slow this turned into long alternating applications and releases. - look ahead for half the brake application time while the train is speeding up and half the release time while it is slowing down (brake_response_horizon), never less than before; - use one law on both sides of the target, proportional to the predicted difference, and hold a band just below the limit that widens with the gradient (pneumatic up to 3 km/h, EP/electric up to 1.5 km/h); - once below a lower limit ahead, hold that limit instead of speeding up towards the line speed and braking again. --- vehicle/Driver.cpp | 101 ++++++++++++++++++++++++++++++++++----------- vehicle/Driver.h | 1 + 2 files changed, 77 insertions(+), 25 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 9a6de8596..5b61b6dd1 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -7483,6 +7483,25 @@ TController::adjust_desired_speed_for_limits() { } } +// how long the present acceleration of a consist lasts whatever is done with the handle now: half the time +// its pneumatic brake takes to apply (while speeding up) or to release (while slowing down), which in G +// is tens of seconds; never shorter than the look-ahead the AI has always used +static double +brake_response_horizon( TMoverParameters const *Vehicle, TBrakeSystem const Brakesystem, double const Acceleration, double const Brakea0 ) { + + auto horizon { ( 1.0 - Brakea0 ) * 30.0 / 3.6 }; + if( Brakesystem == TBrakeSystem::Pneumatic ) { + auto const isrelease { Acceleration < 0.0 }; + horizon = std::max( + horizon, + 0.5 * ( + Vehicle->BrakeDelayFlag > bdelay_G ? + Vehicle->BrakeDelay[ isrelease ? 0 : 1 ] : + Vehicle->BrakeDelay[ isrelease ? 2 : 3 ] ) ); + } + return horizon; +} + void TController::adjust_desired_speed_for_target_speed( double const Range ) { // ustalanie zadanego przyspieszenia @@ -7496,6 +7515,7 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { // gdy jedzie wolniej niż potrzeba, albo nie ma przeszkód na drodze // normalna jazda AccDesired = VelDesired != 0.0 ? AccPreferred : -0.01; + fAccDesiredGravity = 0.0; auto const vel { DirectionalVel() }; @@ -7654,35 +7674,57 @@ TController::adjust_desired_speed_for_current_speed() { if( fAccGravity > 0.025 ) { // going sharply downhill we may need to start braking sooner than usual // try to estimate increase of current velocity before engaged brakes start working - auto const speedestimate = vel + ( 1.0 - fBrake_a0[ 0 ] ) * 30.0 * AbsAccS; - if( speedestimate > VelDesired ) { - // jesli jedzie za szybko do AKTUALNEGO - if( VelDesired == 0.0 ) { - // jesli stoj, to hamuj, ale i tak juz za pozno :) - AccDesired = std::min( AccDesired, -0.85 ); // hamuj solidnie - } - else { - // if it looks like we'll exceed maximum speed start thinking about slight slowing down - AccDesired = std::min( AccDesired, -0.05 + fAccThreshold ); - // HACK: for cargo trains with high braking threshold ensure we cross that threshold - if( true == IsCargoTrain - && fBrake_a0[0] > 0.2 ) { - AccDesired -= std::clamp( fBrake_a0[ 0 ] - 0.2, 0.0, 0.15 ); - } - } + // look ahead for as long as the brake takes to respond, since that is how long the + // present acceleration will last whatever is done with the handle now; a pneumatic + // brake applies and releases on its own time scale, which in G is tens of seconds + auto const horizon { brake_response_horizon( mvOccupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ) }; + auto const speedestimate = vel + 3.6 * horizon * AbsAccS; + // already below a lower limit ahead, the train has no business speeding up towards it + auto holdlimit { VelDesired }; + if( ( VelNext > 0.0 ) + && ( VelNext < holdlimit ) + && ( vel <= VelNext + fVelPlus ) + && ( ActualProximityDist < std::max( 2.0 * fBrakeDist, 500.0 ) ) ) { + holdlimit = VelNext; + } + // hold a band just below the limit; the steeper the descent the harder the train pulls + // away once the brake eases, so the band widens with the gradient (about 3 km/h at 20 per mille) + // an electro-pneumatic or electric brake responds at once and needs far less room + auto const holdspeed { holdlimit - ( + BrakeSystem == TBrakeSystem::Pneumatic ? + std::clamp( fAccGravity * 15.0, 1.0, 3.0 ) : + std::clamp( fAccGravity * 7.5, 1.0, 1.5 ) ) }; + if( ( speedestimate > VelDesired ) + && ( VelDesired == 0.0 ) ) { + // jesli stoj, to hamuj, ale i tak juz za pozno :) + AccDesired = std::min( AccDesired, -0.85 ); // hamuj solidnie } else { - // stop accelerating when close enough to target speed + // one continuous law on both sides of the target, proportional to the predicted + // difference: a demand that jumps to the braking threshold above the target and to + // full acceleration below it alternates, with a brake that responds in seconds, + // between a hard application and a full release AccDesired = std::min( AccDesired, // but don't override decceleration for VelNext - std::lerp( // ease off as you close to the target velocity - EU07_AI_NOACCELERATION, AccPreferred, - std::clamp( VelDesired - speedestimate, 0.0, fVelMinus ) / fVelMinus ) ); + std::clamp( + EU07_AI_NOACCELERATION - 0.04 * ( speedestimate - holdspeed ), + -0.05 + fAccThreshold, + std::max( EU07_AI_NOACCELERATION, AccPreferred ) ) ); + // HACK: for cargo trains with high braking threshold ensure we cross that threshold + if( true == IsCargoTrain + && fBrake_a0[0] > 0.2 + && speedestimate > holdlimit ) { + AccDesired -= std::clamp( fBrake_a0[ 0 ] - 0.2, 0.0, 0.15 ); + } } // final tweaks if( vel > EU07_AI_NOMOVEMENT ) { // going downhill also take into account impact of gravity AccDesired -= fAccGravity; + // from here on AccDesired is what the brake alone has to deliver, which is what + // IncBrake() sizes the handle position for; AbsAccS already contains the gravity, + // so the comparisons with it have to add this part back + fAccDesiredGravity = fAccGravity; // HACK: if the max allowed speed was exceeded something went wrong; brake harder AccDesired -= 0.15 * std::clamp( vel - VelDesired, 0.0, 5.0 ); } @@ -7773,6 +7815,7 @@ TController::adjust_desired_speed_for_braking_test() { } case 1: { AccDesired = EU07_AI_BRAKINGTESTACCELERATION; + fAccDesiredGravity = 0.0; VelDesired = DBT_VelocityBrake; if( ElapsedTime - DBT_BrakingTime > 1 ) { ForcePNBrake = true; @@ -7783,6 +7826,7 @@ TController::adjust_desired_speed_for_braking_test() { } case 2: { AccDesired = EU07_AI_BRAKINGTESTACCELERATION; + fAccDesiredGravity = 0.0; VelDesired = DBT_VelocityBrake; if( ElapsedTime - DBT_BrakingTime > 2 ) { DBT_BrakingTime = ElapsedTime; @@ -7794,6 +7838,7 @@ TController::adjust_desired_speed_for_braking_test() { } case 3: { AccDesired = safe_clamp( -AbsAccS, fAccThreshold * 1.01, fAccThreshold * 1.21 ); + fAccDesiredGravity = 0.0; VelDesired = DBT_VelocityBrake; if( vel <= DBT_VelocityRelease ) { DynamicBrakeTest = 4; @@ -7980,6 +8025,10 @@ void TController::control_braking_force() { } } + // AbsAccS is the resultant acceleration, gravity included, so it is compared with the + // resultant the AI asks for, not with the brake-only demand AccDesired holds on a descent + auto const accdesiredresultant { AccDesired + fAccDesiredGravity }; + if( is_emu() && !ForcePNBrake ) { // właściwie, to warunek powinien być na działający EP // Ra: to dobrze hamuje EP w EZT @@ -7991,13 +8040,13 @@ void TController::control_braking_force() { auto const accmargin = AccMax > 1.1 * AccDesired && fAccGravity < 0.025 ? 0.05 : 0.0; if( AccDesired < accthreshold // jeśli hamować - u góry ustawia się hamowanie na fAccThreshold - && ( AbsAccS > AccDesired + accmargin + && ( AbsAccS > accdesiredresultant + accmargin || BrakeCtrlPosition < 0 ) ) { // hamować bardziej, gdy aktualne opóźnienie hamowania mniejsze niż (AccDesired) cue_action( driver_hint::brakingforceincrease ); } else if( OrderCurrentGet() != Disconnect ) { // przy odłączaniu nie zwalniamy tu hamulca - if( AbsAccS < AccDesired - 0.05 ) { + if( AbsAccS < accdesiredresultant - 0.05 ) { // jeśli opóźnienie większe od wymaganego (z histerezą) luzowanie, gdy za dużo // TBD: check if the condition isn't redundant with the DecBrake() code if( BrakeCtrlPosition >= 0 ) { @@ -8013,10 +8062,12 @@ void TController::control_braking_force() { } // type & dt_ezt else { // a stara wersja w miarę dobrze działa na składy wagonowe - if( ( AccDesired < fAccGravity - 0.1 && AbsAccS > AccDesired + fBrake_a1[0] ) // regular braking + if( ( accdesiredresultant < fAccGravity - 0.1 && AbsAccS > accdesiredresultant + fBrake_a1[0] ) // regular braking || ( fAccGravity < -0.05 && velocity < -0.1 ) ) { // also brake if uphill and slipping back // u góry ustawia się hamowanie na fAccThreshold if( fBrakeTime < 0.0 + // the size of the brake demand decides whether to skip the reaction time, so the brake-only part + // (on a descent a heavy train needs its brake built up at once, the gravity does not help it slow) || AccDesired < fAccGravity - 0.5 || BrakeCtrlPosition <= 0 ) { // jeśli upłynął czas reakcji hamulca, chyba że nagłe albo luzował @@ -8033,8 +8084,8 @@ void TController::control_braking_force() { * 0.5 ); // Ra: tymczasowo, bo przeżyna S1 } } - if ( AccDesired < fAccGravity - 0.05 - && AccDesired - fBrake_a1[0] * 0.51 - AbsAccS > 0.05 ) { + if ( accdesiredresultant < fAccGravity - 0.05 + && accdesiredresultant - fBrake_a1[0] * 0.51 - AbsAccS > 0.05 ) { // jak hamuje, to nie tykaj kranu za często // yB: luzuje hamulec dopiero przy różnicy opóźnień rzędu 0.2 if( OrderCurrentGet() != Disconnect ) { // przy odłączaniu nie zwalniamy tu hamulca diff --git a/vehicle/Driver.h b/vehicle/Driver.h index e7c4fac00..c493afc8a 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -387,6 +387,7 @@ private: double AccDesired = 0.0; // przyspieszenie, jakie ma utrzymywać (<0:nie przyspieszaj,<-0.1:hamuj) 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 fAccDesiredGravity = 0.0; // część AccDesired odjęta na spadku jako kompensacja pochylenia; AbsAccS zawiera ją już sam double VelforDriver = -1.0; // prędkość, używana przy zmianie kierunku (ograniczenie przy nieznajmości szlaku?) double VelSignal = 0.0; // ograniczenie prędkości z kompilacji znaków i sygnałów // normalnie na początku ma stać, no chyba że jedzie double VelLimit = -1.0; // predkość zadawana przez event jednokierunkowego ograniczenia prędkości // -1: brak ograniczenia prędkości From ce87aa0e090d4b5fba7042588df6e5e71895d5a1 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Mon, 28 Sep 2026 15:45:10 +0200 Subject: [PATCH 06/90] AI braking: plan approaches from the consist brake, stop 8-15 m short The new braking curve below plans the stop at the signal itself unless the stopping point moves with it (in testing, trains stopped 0-3 m past the signal), so both go in one change. 1. The braking curve. The braking curve towards a lower speed or a stop assumed a constant deceleration from the present speed over the whole distance. A pneumatic brake does not work like that: - it acts on the state the train is in once it has applied; while the train is still speeding up (downhill), plan from the speed it will have reached by then, over the distance left by then; - cast iron blocks grip harder as the train slows, so a constant deceleration plan asks for too much at the start and then releases; follow the shape of the consist brake table (fBrake_a0/a1) over speed instead (shaped_braking_demand), over the distance left once the brake has had its fill time; - no power while the train is still above the speed it is braking for. IncBrake() only adds braking when the brake table says the present position is not enough. When the table overestimates the consist (see the previous rapid step fix, or a descent), the train keeps slowing too little with the handle held. After the full fill time with the handle unchanged, believe the train rather than the table (fBrakeHeldTime). When the AI asks for no braking and the train is still slowing down more than that, release. With the gentler plan above the demand eases off early, and without this a heavy freight train in G came to a stand a few hundred metres before a W4 with its brake still on (seen in testing). 2. The stopping window. In train running the final approach to a stop signal began at fMaxProximityDist (15-40 m, cargo 25-50 m), aimed at half of fMinProximityDist (10-15 m, cargo 20-25 m), and set VelDesired = 0, which asks for hard braking whatever the distance left. Where a train came to a stand depended on how it entered that phase: on the test sceneries anywhere from 1 to 45 m short of the signal on the flat and 25-42 m uphill for heavy freight. For train running only (a W4 stop point, shunting and coupling keep the old margins and the old final approach): - stop 8-15 m short of the signal whatever the length of the train, aim at the middle of that window (stopping_margin) both with the braking curve and in the final approach, and ask there only for what the distance left takes, following the consist brake table; - start the final approach 50 m out (cargo 60 m) whether the train is moving or standing; it used to be 15-40 m while moving and 50 m once stopped, and with the gentler final approach above a train that came to a stand between the two released to creep closer and, uphill, rolled back and tried again; - uphill, while the gradient alone would bring the train to a stand short of the window, keep it rolling at no more than the speed the gradient can take off by then, with power if needed; - uphill, put the train brake on as the train comes to a stand, keep it on while it stands, and apply it for real if the train rolls back. This is done at the very end of pick_optimal_speed(): the adjustments made before it assume a train moving forward and can raise the demand back to "no braking" (with the rule placed earlier, EMUs rolled back up to 90 m in testing). The EMU branch of control_braking_force() gets the "slipping back" condition the wagon branch has always had. 3. Known limitation. Heavy trains of wagons with the ESt3AL2 distributor (413S, 601W and alike) respond more slowly than the AI expects: with the present brake model the distributor fills the AL2 relay chamber, whose capacity is set to 2 in TNESt3::SetSize (Przekladniki[i]->CreateCap(i)), through orifices sized for about 1 l, so these wagons need 34 s to reach 95 % cylinder pressure in G against 21 s for a plain ESt3. On a 20 per mille descent an ET22 with 20 loaded 413S wagons overran the stop signal by up to 6.5 m in 6 of 24 runs at x20 with this change. With the relay chamber at 1 l and the release orifices at about 80 % of their diameter, as the author of the brake model suggested, none of them did (0 of 16). --- vehicle/Driver.cpp | 217 ++++++++++++++++++++++++++++++++++++++++----- vehicle/Driver.h | 7 ++ 2 files changed, 200 insertions(+), 24 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 5b61b6dd1..09b607b69 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -3122,6 +3122,17 @@ bool TController::IncBrake() } } } + else if( BrakeCtrlPosition > 0.0 + && AccDesired < 0.0 // only while braking is asked for, not when called for a train slipping back while starting + && fBrakeHeldTime > ( + mvOccupied->BrakeDelayFlag > bdelay_G ? + mvOccupied->BrakeDelay[ 1 ] : + mvOccupied->BrakeDelay[ 3 ] ) ) { + // the table says the present position is enough, but the brake has had its full fill + // time since the handle last moved and the train is still not slowing down as asked: + // believe the train rather than the table + OK = BrakeLevelAdd( BrakingLevelIncrease ); + } else OK = false; } @@ -3781,6 +3792,7 @@ void TController::BrakeLevelSet(double b) if (BrakeCtrlPosition == b) return; // nie przeliczać, jak nie ma zmiany BrakeCtrlPosition = std::clamp(b, (double)gbh_MIN, (double)gbh_MAX); + fBrakeHeldTime = 0.0; } bool TController::BrakeLevelAdd(double b) @@ -5911,6 +5923,7 @@ TController::update_timers( double dt ) { ElapsedTime += dt; WaitingTime += dt; fBrakeTime -= dt; // wpisana wartość jest zmniejszana do 0, gdy ujemna należy zmienić nastawę hamulca + fBrakeHeldTime += dt; if( mvOccupied->fBrakeCtrlPos != mvOccupied->Handle->GetPos( bh_FS ) ) { // brake charging timeout starts after charging ends BrakeChargingCooldown += dt; @@ -6659,12 +6672,27 @@ TController::determine_proximity_ranges() { // na jaka odleglosc i z jaka predkoscia ma podjechac do przeszkody // jeśli pociąg if( is_train() ) { - fMinProximityDist = std::clamp( 5 + iVehicles, 10, 15 ); - fMaxProximityDist = std::clamp( 10 + iVehicles, 15, 40 ); + if( true == 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 ) { - // increase distances for cargo trains to take into account slower reaction to brakes - fMinProximityDist += 10.0; + // start the final approach earlier for cargo trains, their brakes react more slowly fMaxProximityDist += 10.0; /* if( IsHeavyCargoTrain ) { @@ -6683,7 +6711,7 @@ TController::determine_proximity_ranges() { if( mvOccupied->Vel < 0.1 ) { // jak stanie za daleko, to niech nie dociąga paru metrów - fMaxProximityDist = 50.0; + fMaxProximityDist = std::max( fMaxProximityDist, 50.0 ); } if( iDrivigFlags & moveLate ) { @@ -7314,6 +7342,26 @@ TController::pick_optimal_speed( double const Range ) { is_car() ? -2.0 : -0.9, is_car() ? 2.0 : 0.9 ); + if( ( true == is_train() ) + && ( ( OrderCurrentGet() & Obey_train ) != 0 ) + && ( VelNext == 0.0 ) + && ( ActualProximityDist <= fMaxProximityDist ) + && ( fAccGravity < -0.025 ) ) { + // uphill, the gradient alone brings a slow train to a stand within metres, far sooner than a released + // brake applies; so the train brake goes on while the train is coming to a stand, stays on while it + // stands, and is applied for real if the train has started to roll back. done last, so that none of + // the adjustments above, made for a train moving forward, can undo it + auto const vel { DirectionalVel() }; + if( std::abs( vel ) < EU07_AI_NOMOVEMENT ) { + VelDesired = 0.0; + AccDesired = std::min( AccDesired, EU07_AI_NOACCELERATION ); + } + else if( ( vel < 0.0 ) + || ( ( vel < 5.0 ) && ( AccDesired < 0.0 ) ) ) { + AccDesired = std::min( AccDesired, fAccGravity - 0.15 ); + } + } + // if the route ahead is blocked we might need to head the other way check_route_behind( 1000 ); // NOTE: legacy scan range value } @@ -7502,6 +7550,55 @@ brake_response_horizon( TMoverParameters const *Vehicle, TBrakeSystem const Brak return horizon; } +// distance [m] in which the gradient alone, with no brake and no power, brings a train rolling uphill at Velocity [km/h] to a stand +static double +uphill_stopping_distance( double const Velocity, double const Gravity ) { + return ( Velocity / 3.6 ) * ( Velocity / 3.6 ) / ( 2.0 * std::max( 0.01, -Gravity ) ); +} + +// speed [km/h] from which the gradient alone brings a train rolling uphill to a stand in Distance [m] +static double +uphill_coasting_speed( double const Distance, double const Gravity ) { + return 3.6 * std::sqrt( 2.0 * std::max( 0.01, -Gravity ) * std::max( 0.0, Distance ) ); +} + +// resultant acceleration [m/s2] to ask for now so that a steady application of the pneumatic brake takes the train +// from Velocity down to Target [km/h] over Distance [m], with the brake force following the speed the way the +// consist brake table says it does (cast iron blocks grip harder as the train slows) +static double +shaped_braking_demand( + double const Velocity, double const Target, double const Distance, double const Gravity, + double const Vmax, double const *Brakea0, double const *Brakea1, int const Tablesize ) { + + auto const fullbraking = [&]( double const Kmh ) { + auto const index { std::clamp( static_cast( Tablesize * Kmh / std::max( 1.0, Vmax ) ), 1, Tablesize ) }; + return std::max( 0.01, Brakea0[ index ] + 12.0 * Brakea1[ index ] ); }; + auto const travel = [&]( double const Ratio ) { + auto const steps { 24 }; + auto const step { ( Velocity - Target ) / steps }; + auto distance { 0.0 }; + for( auto i { 0 }; i < steps; ++i ) { + auto const kmh { Target + ( i + 0.5 ) * step }; + auto const deceleration { Ratio * fullbraking( kmh ) - Gravity }; + if( deceleration <= 0.001 ) { + return std::numeric_limits::max(); + } + distance += ( kmh / 3.6 ) * ( step / 3.6 ) / deceleration; + } + return distance; }; + + auto low { 0.0 }; + auto high { 3.0 }; + if( travel( high ) > Distance ) { + return Gravity - high * fullbraking( Velocity ); + } + for( auto i { 0 }; i < 30; ++i ) { + auto const middle { 0.5 * ( low + high ) }; + ( travel( middle ) > Distance ? low : high ) = middle; + } + return Gravity - high * fullbraking( Velocity ); +} + void TController::adjust_desired_speed_for_target_speed( double const Range ) { // ustalanie zadanego przyspieszenia @@ -7552,9 +7649,33 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { } else { // hamowanie tak, aby stanąć - VelDesired = VelNext; - AccDesired = ( VelNext * VelNext - vel * vel ) / ( 25.92 * ( ActualProximityDist + 0.1 - 0.5*fMinProximityDist ) ); - AccDesired = std::min( AccDesired, fAccThreshold ); + if( ( OrderCurrentGet() & Obey_train ) == 0 ) { + VelDesired = VelNext; + AccDesired = ( VelNext * VelNext - vel * vel ) / ( 25.92 * ( ActualProximityDist + 0.1 - 0.5*fMinProximityDist ) ); + AccDesired = std::min( AccDesired, fAccThreshold ); + } + else { + // a train keeps its desired speed until the lower edge of the stopping window (a desired + // speed of 0 here asks for hard braking whatever the distance left), aims at the middle + // of the window, and asks only for what that takes + auto const stoppingroom { std::max( 0.5, ActualProximityDist - stopping_margin() ) }; + AccDesired = ( + BrakeSystem == TBrakeSystem::Pneumatic ? + // cast iron blocks grip harder as the train comes to a stand + shaped_braking_demand( + vel, VelNext, stoppingroom, fAccGravity, + mvOccupied->Vmax, fBrake_a0, fBrake_a1, BrakeAccTableSize ) : + ( VelNext * VelNext - vel * vel ) / ( 25.92 * stoppingroom ) ); + if( ( fAccGravity < -0.025 ) + && ( AccDesired >= fAccGravity - 0.001 ) // no brake needed at all + && ( ActualProximityDist - uphill_stopping_distance( vel, fAccGravity ) > stopping_margin() ) ) { + // uphill the gradient alone would bring the train to a stand short of the + // middle of the stopping window; keep it rolling at the present speed until that changes, + // but no faster than the gradient alone can take off by the middle of the window + AccDesired = std::min( 0.0, AccPreferred ); + VelDesired = min_speed( VelDesired, uphill_coasting_speed( ActualProximityDist - stopping_margin(), fAccGravity ) ); + } + } } } else { @@ -7578,17 +7699,57 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { if( brakingdistance + std::max(slowdowndistance, fMaxProximityDist) >= ActualProximityDist - fMaxProximityDist ) { // don't slow down prematurely; as long as we have room to come to a full stop at a safe distance, we're good // ensure some minimal coasting speed, otherwise a vehicle entering this zone at very low speed will be crawling forever - auto const brakingpointoffset = VelNext * braking_distance_multiplier( VelNext ); - AccDesired = std::min( - AccDesired, - ( VelNext * VelNext - vel * vel ) - / ( 25.92 - * std::max( - ActualProximityDist - brakingpointoffset, - std::min( - ActualProximityDist, - brakingpointoffset ) ) - + 0.1 ) ); // najpierw hamuje mocniej, potem zluzuje + auto const brakingpointoffset = ( + ( VelNext == 0.0 ) && is_train() && ( OrderCurrentGet() & Obey_train ) ? + stopping_margin() : // the same stopping point as the final approach + VelNext * braking_distance_multiplier( VelNext ) ); + // a pneumatic brake acts on the state the train will be in once it has responded; while the + // train is still speeding up, plan the deceleration from there, so more is asked for at once + auto velocity { vel }; + auto distance { ActualProximityDist }; + if( ( BrakeSystem == TBrakeSystem::Pneumatic ) + && ( AbsAccS > 0.0 ) ) { + auto const horizon { brake_response_horizon( mvOccupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ) }; + velocity = vel + 3.6 * horizon * AbsAccS; + distance = std::max( 0.5 * ActualProximityDist, ActualProximityDist - ( vel + velocity ) * 0.5 / 3.6 * horizon ); + } + auto const brakingroom { + std::max( + distance - brakingpointoffset, + std::min( + distance, + brakingpointoffset ) ) }; + auto deceleration { ( VelNext * VelNext - velocity * velocity ) / ( 25.92 * brakingroom + 0.1 ) }; + if( ( BrakeSystem == TBrakeSystem::Pneumatic ) && ( velocity > VelNext ) ) { + // follow the shape of the consist brake force over speed rather than assume a constant + // deceleration, over the distance left once the brake has had time to fill (while the + // train is still speeding up, the prediction above has taken that off already) + auto room { brakingroom }; + if( ( AbsAccS <= 0.0 ) + && ( ( VelNext > 0.0 ) || ( BrakeCtrlPosition <= 0.0 ) ) ) { // for a stop, only while the brake is still to be applied + room = std::max( + 0.5 * brakingroom, + brakingroom - vel / 3.6 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ); + } + deceleration = shaped_braking_demand( + velocity, VelNext, room + 0.1 / 25.92, fAccGravity, + mvOccupied->Vmax, fBrake_a0, fBrake_a1, BrakeAccTableSize ); + } + AccDesired = std::min( AccDesired, deceleration ); // najpierw hamuje mocniej, potem zluzuje + if( vel > VelNext ) { + if( deceleration < fAccGravity - 0.001 ) { + // no power while still above the speed being braked for + AccDesired = std::min( AccDesired, EU07_AI_NOACCELERATION ); + } + else if( ( fAccGravity < -0.025 ) + && ( ( OrderCurrentGet() & Obey_train ) != 0 ) + && ( ActualProximityDist - uphill_stopping_distance( vel, fAccGravity ) > brakingpointoffset ) ) { + // uphill the gradient alone slows the train down more than it is asked to; + // keep it rolling at the present speed until that changes + AccDesired = std::min( 0.0, AccPreferred ); + VelDesired = min_speed( VelDesired, uphill_coasting_speed( ActualProximityDist - brakingpointoffset, fAccGravity ) ); + } + } } } } @@ -7959,7 +8120,13 @@ void TController::control_tractive_force() { if( ActualProximityDist > (is_car() ? fMinProximityDist : // cars are allowed to move within min proximity distance fMaxProximityDist) ? // other vehicle types keep wider margin true : - velocity + 1.0 < VelNext ) { + ( velocity + 1.0 < VelNext ) + // uphill, a train told to hold its speed on the way to a stop needs the power to do it + || ( ( VelNext == 0.0 ) + && ( ( OrderCurrentGet() & Obey_train ) != 0 ) + && ( AccDesired >= 0.0 ) + && ( fAccGravity < -0.025 ) + && ( ActualProximityDist > fMinProximityDist ) ) ) { // ...to można przyspieszyć increase_tractive_force(); } @@ -8039,9 +8206,10 @@ void TController::control_braking_force() { auto const AccMax{ std::min(fBrake_a0[0] + 12 * fBrake_a1[0], mvOccupied->MED_amax) }; auto const accmargin = AccMax > 1.1 * AccDesired && fAccGravity < 0.025 ? 0.05 : 0.0; - if( AccDesired < accthreshold // jeśli hamować - u góry ustawia się hamowanie na fAccThreshold - && ( AbsAccS > accdesiredresultant + accmargin - || BrakeCtrlPosition < 0 ) ) { + if( ( AccDesired < accthreshold // jeśli hamować - u góry ustawia się hamowanie na fAccThreshold + && ( AbsAccS > accdesiredresultant + accmargin + || BrakeCtrlPosition < 0 ) ) + || ( fAccGravity < -0.05 && velocity < -0.1 && AccDesired < 0.0 ) ) { // also brake if uphill and slipping back // hamować bardziej, gdy aktualne opóźnienie hamowania mniejsze niż (AccDesired) cue_action( driver_hint::brakingforceincrease ); } @@ -8084,7 +8252,8 @@ void TController::control_braking_force() { * 0.5 ); // Ra: tymczasowo, bo przeżyna S1 } } - if ( accdesiredresultant < fAccGravity - 0.05 + if ( ( accdesiredresultant < fAccGravity - 0.05 + || AccDesired >= EU07_AI_NOACCELERATION ) // no braking asked for, yet the train is slowing down more than that && accdesiredresultant - fBrake_a1[0] * 0.51 - AbsAccS > 0.05 ) { // jak hamuje, to nie tykaj kranu za często // yB: luzuje hamulec dopiero przy różnicy opóźnień rzędu 0.2 diff --git a/vehicle/Driver.h b/vehicle/Driver.h index c493afc8a..0e65a4b94 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -353,6 +353,12 @@ private: void control_tractive_force(); void increase_tractive_force(); void control_braking_force(); + // how far short of a stop the AI aims: in train running the middle of the 8-15 m window before a signal, + // otherwise (a W4 stop point, shunting, coupling) half the minimum proximity distance, as before + double stopping_margin() const { + return ( ( OrderCurrentGet() & Obey_train ) != 0 ) && ( false == IsAtPassengerStop ) ? + fMinProximityDist + 3.5 : + 0.5 * fMinProximityDist; } void apply_independent_brake_only(); void check_route_ahead( double const Range ); void check_route_behind( double const Range ); @@ -387,6 +393,7 @@ private: double AccDesired = 0.0; // przyspieszenie, jakie ma utrzymywać (<0:nie przyspieszaj,<-0.1:hamuj) 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 double fAccDesiredGravity = 0.0; // część AccDesired odjęta na spadku jako kompensacja pochylenia; AbsAccS zawiera ją już sam double VelforDriver = -1.0; // prędkość, używana przy zmianie kierunku (ograniczenie przy nieznajmości szlaku?) double VelSignal = 0.0; // ograniczenie prędkości z kompilacji znaków i sygnałów // normalnie na początku ma stać, no chyba że jedzie From 4f2dfd93186f218f20c499ef20572b9fa5b30b83 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Tue, 29 Sep 2026 09:47:51 +0200 Subject: [PATCH 07/90] Drop redundant comparisons with true/false In the conditions this PR adds; no change in behaviour. --- McZapkie/Oerlikon_ESt.cpp | 2 +- vehicle/Driver.cpp | 6 +++--- vehicle/Driver.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/McZapkie/Oerlikon_ESt.cpp b/McZapkie/Oerlikon_ESt.cpp index d8d5b3da8..8705000a4 100644 --- a/McZapkie/Oerlikon_ESt.cpp +++ b/McZapkie/Oerlikon_ESt.cpp @@ -327,7 +327,7 @@ void TNESt3::Init( double const PP, double const HPP, double const LPP, double c double TNESt3::GetRapidRatio(double const Vel) const { // ESt3 has no rapid relay, and with disc brakes the rapid step stays engaged at all speeds - if( ( PrzekRapid == nullptr ) || ( true == RapidStaly ) ) { + if( ( PrzekRapid == nullptr ) || RapidStaly ) { return 1.0; } // a braking vehicle comes from above, so the step is still engaged down to the speed diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 09b607b69..d75500dbe 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -6672,7 +6672,7 @@ TController::determine_proximity_ranges() { // na jaka odleglosc i z jaka predkoscia ma podjechac do przeszkody // jeśli pociąg if( is_train() ) { - if( true == IsAtPassengerStop ) { + 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 ); @@ -7342,7 +7342,7 @@ TController::pick_optimal_speed( double const Range ) { is_car() ? -2.0 : -0.9, is_car() ? 2.0 : 0.9 ); - if( ( true == is_train() ) + if( is_train() && ( ( OrderCurrentGet() & Obey_train ) != 0 ) && ( VelNext == 0.0 ) && ( ActualProximityDist <= fMaxProximityDist ) @@ -7872,7 +7872,7 @@ TController::adjust_desired_speed_for_current_speed() { -0.05 + fAccThreshold, std::max( EU07_AI_NOACCELERATION, AccPreferred ) ) ); // HACK: for cargo trains with high braking threshold ensure we cross that threshold - if( true == IsCargoTrain + if( IsCargoTrain && fBrake_a0[0] > 0.2 && speedestimate > holdlimit ) { AccDesired -= std::clamp( fBrake_a0[ 0 ] - 0.2, 0.0, 0.15 ); diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 0e65a4b94..fefa950ca 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -356,7 +356,7 @@ private: // how far short of a stop the AI aims: in train running the middle of the 8-15 m window before a signal, // otherwise (a W4 stop point, shunting, coupling) half the minimum proximity distance, as before double stopping_margin() const { - return ( ( OrderCurrentGet() & Obey_train ) != 0 ) && ( false == IsAtPassengerStop ) ? + return ( ( OrderCurrentGet() & Obey_train ) != 0 ) && !IsAtPassengerStop ? fMinProximityDist + 3.5 : 0.5 * fMinProximityDist; } void apply_independent_brake_only(); From 504d69b26a1be53aa4e6121685eda395ff10e47c Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 10 Oct 2026 23:39:19 +0200 Subject: [PATCH 08/90] AI braking: keep braking for a lower speed inside the proximity range Inside the proximity range only a stop had a final approach of its own; for a lower speed ahead that branch set nothing, so AccDesired stayed at the preferred acceleration. With the range at 50 m, which this PR uses so that trains stop 8-15 m short of a signal, the AI stopped braking within the last 50 m before a speed restriction and even took power: EMUs reached a 20 km/h restriction at 25 km/h. On master the range is 15-40 m, which kept this mostly out of sight. A lower speed ahead now keeps the braking curve right up to where it begins. Stops are unchanged. Measured in real time on current master (with #199), with a 20 km/h restriction one coach long, two runs of 32 consists each: over 22 km/h in the restriction on the flat 9 on master, 15 before this change, 1 after (a 36WEa at 22.1 km/h); on the 20 per mille descent 15, 0 and 0. Stops 8-15 m short of the signal stay as they were (flat 57 and 55 of 64, descent 44 and 42), no overruns. --- vehicle/Driver.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index c0e78b7cd..a5272b392 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -7518,7 +7518,10 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { } else if (ActualProximityDist > fMinProximityDist) { // jedzie szybciej, niż trzeba na końcu ActualProximityDist, ale jeszcze jest daleko - if (ActualProximityDist < fMaxProximityDist) { + // only a stop has a final approach of its own inside the proximity range; a lower speed ahead + // keeps the braking curve below right up to where it begins, or the train would stop braking + // (and even take power) that far out + if( ( VelNext == 0.0 ) && ( ActualProximityDist < fMaxProximityDist ) ) { // jak minął już maksymalny dystans po prostu hamuj (niski stopień) // ma stanąć, a jest w drodze hamowania albo ma jechać /* From 86069c5eaba8700ff7f8b38bf4ee562421e85889 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Tue, 7 Jul 2026 01:45:05 +0200 Subject: [PATCH 09/90] utilities: XYZtoRGB: Use sRGB colorspace It used Adobe RGB matrix depite of nothing else using Adobe RGB in the pipeline. --- utilities/color.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utilities/color.h b/utilities/color.h index be853a3d4..bbf17e096 100644 --- a/utilities/color.h +++ b/utilities/color.h @@ -15,9 +15,9 @@ glm::vec3 XYZtoRGB( glm::vec3 const &XYZ ) { // M^-1 for Adobe RGB from http://www.brucelindbloom.com/Eqn_RGB_XYZ_Matrix.html - float const mi[ 3 ][ 3 ] = { 2.041369f, -0.969266f, 0.0134474f, -0.5649464f, 1.8760108f, -0.1183897f, -0.3446944f, 0.041556f, 1.0154096f }; +// float const mi[ 3 ][ 3 ] = { 2.041369f, -0.969266f, 0.0134474f, -0.5649464f, 1.8760108f, -0.1183897f, -0.3446944f, 0.041556f, 1.0154096f }; // m^-1 for sRGB: -// float const mi[ 3 ][ 3 ] = { 3.240479f, -0.969256f, 0.055648f, -1.53715f, 1.875991f, -0.204043f, -0.49853f, 0.041556f, 1.057311f }; + float const mi[ 3 ][ 3 ] = { 3.240479f, -0.969256f, 0.055648f, -1.53715f, 1.875991f, -0.204043f, -0.49853f, 0.041556f, 1.057311f }; return glm::vec3{ XYZ.x*mi[ 0 ][ 0 ] + XYZ.y*mi[ 1 ][ 0 ] + XYZ.z*mi[ 2 ][ 0 ], From 842b768803c0499048360680ae5f6d6437c98609 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Tue, 7 Jul 2026 01:45:59 +0200 Subject: [PATCH 10/90] simulationenvironment: Adjust turbidity values Turbidity 3 breaks the model at night. Adjust the values to have them work well all the time and to maximize variation between cloud states. --- simulation/simulationenvironment.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/simulation/simulationenvironment.cpp b/simulation/simulationenvironment.cpp index 14ad2353c..0c1ec44b4 100644 --- a/simulation/simulationenvironment.cpp +++ b/simulation/simulationenvironment.cpp @@ -68,15 +68,15 @@ world_environment::compute_weather() const { Global.AirTemperature > 1 ? "rain:" : "snow:"; - Global.fTurbidity = Global.Overcast <= 0.10 ? 3 : + Global.fTurbidity = Global.Overcast <= 0.10 ? 4 : Global.Overcast <= 0.20 ? 4 : Global.Overcast <= 0.30 ? 5 : Global.Overcast <= 0.40 ? 5 : - Global.Overcast <= 0.50 ? 5 : - Global.Overcast <= 0.60 ? 5 : - Global.Overcast <= 0.70 ? 6 : - Global.Overcast <= 0.80 ? 7 : - Global.Overcast <= 0.90 ? 8 : + Global.Overcast <= 0.50 ? 6 : + Global.Overcast <= 0.60 ? 6 : + Global.Overcast <= 0.70 ? 7 : + Global.Overcast <= 0.80 ? 8 : + Global.Overcast <= 0.90 ? 9 : 9; } From ba405858e16a88ec5c0c9acb4dc0086dad13bfdd Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Tue, 27 Oct 2026 01:00:52 +0100 Subject: [PATCH 11/90] opengl33renderer: Fix up the sizes of Sun and Moon Although the used value makes some physical sense, it fails to take FoV effect into account, leading to both Sun and Moon becoming comically tiny. Revert the sizes to the ones used by the legacy renderer, which makes them appear much more plausible. --- rendering/opengl33renderer.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index d20454c35..9d4318981 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -2028,12 +2028,12 @@ bool opengl33_renderer::Render(world_environment *Environment) glm::vec4 color(suncolor.x, suncolor.y, suncolor.z, std::clamp(1.5f - Global.Overcast, 0.f, 1.f) * fogfactor); auto const sunvector = Environment->m_sun.getDirection(); - /*float const size = std::lerp( // TODO: expose distance/scale factor from the moon object + float const size = std::lerp( // TODO: expose distance/scale factor from the moon object 0.0325f, 0.0275f, - std::clamp( Environment->m_sun.getAngle(), 0.f, 90.f ) / 90.f );*/ + std::clamp( Environment->m_sun.getAngle(), 0.f, 90.f ) / 90.f ); model_ubs.param[0] = color; - model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(sunvector, 1.0f)), 0.00463f /* size */); + model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(sunvector, 1.0f)), size); model_ubs.param[2] = glm::vec4(0.0f, 1.0f, 1.0f, 0.0f); model_ubo->update(model_ubs); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); @@ -2104,14 +2104,13 @@ bool opengl33_renderer::Render(world_environment *Environment) moonu = 0.0f; } - /* float const size = std::lerp( // TODO: expose distance/scale factor from the moon object 0.0160f, 0.0135f, - std::clamp( Environment->m_moon.getAngle(), 0.f, 90.f ) / 90.f );*/ + std::clamp( Environment->m_moon.getAngle(), 0.f, 90.f ) / 90.f ); model_ubs.param[0] = color; - model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(moonvector, 1.0f)), 0.00451f /* size */); + model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(moonvector, 1.0f)), size); model_ubs.param[2] = glm::vec4(moonu, moonv, 0.333f, 0.0f); model_ubo->update(model_ubs); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); From 5fa03304eff805ca5bbe4dce8dfeebdd40c0250a Mon Sep 17 00:00:00 2001 From: maj00r Date: Sat, 31 Oct 2026 00:45:46 +0100 Subject: [PATCH 12/90] fix(ui): keep panels in the main window when resizing them at the screen edge Pulling a panel's grip into the corner of the screen pushed it a couple of pixels past the main window, and ImGui gave it an OS window of its own. Now a panel only comes out when dragged with the cursor outside the main window. A panel that stays in the main window needs its grips in reach, so a resize stops at the edge, and a panel left past the edge or too big for the window is moved back and shrunk to fit. This is a new io.ConfigViewportsNoAutoDetach option in our copy of ImGui, so it has to be carried over when ImGui gets updated. ImGui itself has no option for this, as of 1.93.1. --- application/uilayer.cpp | 2 ++ imgui/imgui.cpp | 58 +++++++++++++++++++++++++++++++++++++++-- imgui/imgui.h | 1 + 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/application/uilayer.cpp b/application/uilayer.cpp index 2dd771bdd..893b84e92 100644 --- a/application/uilayer.cpp +++ b/application/uilayer.cpp @@ -403,6 +403,8 @@ bool ui_layer::init(GLFWwindow *Window) { m_imguiio->ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; m_imguiio->ConfigViewportsNoTaskBarIcon = true; + // only dragging a panel with the cursor past the main window takes it out, not resizing it or placing it at the edge + m_imguiio->ConfigViewportsNoAutoDetach = true; auto &platformio{ImGui::GetPlatformIO()}; imgui_create_window = platformio.Platform_CreateWindow; platformio.Platform_CreateWindow = create_viewport_window; diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index 573efe244..a9026b408 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1294,6 +1294,7 @@ static void WindowSelectViewport(ImGuiWindow* window); static void WindowSyncOwnedViewport(ImGuiWindow* window, ImGuiWindow* parent_window_in_stack); static bool UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImGuiViewportP* host_viewport); static bool UpdateTryMergeWindowIntoHostViewports(ImGuiWindow* window); +static bool WindowStaysInHostViewport(ImGuiWindow* window, ImGuiViewportP* viewport); // MaSzyna addition static bool GetWindowAlwaysWantOwnViewport(ImGuiWindow* window); static int FindPlatformMonitorForPos(const ImVec2& pos); static int FindPlatformMonitorForRect(const ImRect& r); @@ -1489,6 +1490,7 @@ ImGuiIO::ImGuiIO() ConfigViewportsNoTaskBarIcon = false; ConfigViewportsNoDecoration = true; ConfigViewportsNoDefaultParent = false; + ConfigViewportsNoAutoDetach = false; // Miscellaneous options MouseDrawCursor = false; @@ -6845,6 +6847,11 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si ImVec2 pos_target(FLT_MAX, FLT_MAX); ImVec2 size_target(FLT_MAX, FLT_MAX); + // MaSzyna addition: a resize stops at the edge of the host viewport, so the grips and borders can still be reached + ImRect resize_limit_rect(-FLT_MAX, -FLT_MAX, +FLT_MAX, +FLT_MAX); + if (!window->ViewportOwned && WindowStaysInHostViewport(window, window->Viewport)) + resize_limit_rect = window->Viewport->GetWorkRect(); + // Clip mouse interaction rectangles within the viewport rectangle (in practice the narrowing is going to happen most of the time). // - Not narrowing would mostly benefit the situation where OS windows _without_ decoration have a threshold for hovering when outside their limits. // This is however not the case with current backends under Win32, but a custom borderless window implementation would benefit from it. @@ -6892,6 +6899,7 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si ImVec2 clamp_max = ImVec2(def.CornerPosN.x == 0.0f ? clamp_rect.Max.x : +FLT_MAX, def.CornerPosN.y == 0.0f ? clamp_rect.Max.y : +FLT_MAX); ImVec2 corner_target = g.IO.MousePos - g.ActiveIdClickOffset + ImLerp(def.InnerDir * grip_hover_outer_size, def.InnerDir * -grip_hover_inner_size, def.CornerPosN); // Corner of the window corresponding to our corner grip corner_target = ImClamp(corner_target, clamp_min, clamp_max); + corner_target = ImClamp(corner_target, resize_limit_rect.Min, resize_limit_rect.Max); CalcResizePosSizeFromAnyCorner(window, corner_target, def.CornerPosN, &pos_target, &size_target); } @@ -6971,6 +6979,7 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si ImVec2 clamp_min(border_n == ImGuiDir_Right ? clamp_rect.Min.x : -FLT_MAX, border_n == ImGuiDir_Down || (border_n == ImGuiDir_Up && window_move_from_title_bar) ? clamp_rect.Min.y : -FLT_MAX); ImVec2 clamp_max(border_n == ImGuiDir_Left ? clamp_rect.Max.x : +FLT_MAX, border_n == ImGuiDir_Up ? clamp_rect.Max.y : +FLT_MAX); border_target = ImClamp(border_target, clamp_min, clamp_max); + border_target[axis] = ImClamp(border_target[axis], resize_limit_rect.Min[axis], resize_limit_rect.Max[axis]); if (flags & ImGuiWindowFlags_ChildWindow) // Clamp resizing of childs within parent { ImGuiWindow* parent_window = window->ParentWindow; @@ -7056,6 +7065,30 @@ static inline void ClampWindowPos(ImGuiWindow* window, const ImRect& visibility_ window->Pos = ImClamp(window->Pos, visibility_rect.Min - size_for_clamping, visibility_rect.Max); } +// MaSzyna addition: see io.ConfigViewportsNoAutoDetach +static inline bool IsWindowBeingMoved(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + return g.MovingWindow != NULL && g.MovingWindow->RootWindowDockTree == window->RootWindowDockTree; +} + +// MaSzyna addition: a window left past the edge of its host viewport would have its grips and borders out of reach +static inline void KeepWindowInsideRect(ImGuiWindow* window, const ImRect& rect) +{ + if (rect.GetWidth() <= 0.0f || rect.GetHeight() <= 0.0f) + return; + + if (!(window->Flags & (ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize))) + { + const ImVec2 size_full = ImMin(window->SizeFull, rect.GetSize()); + if (size_full.x != window->SizeFull.x || size_full.y != window->SizeFull.y) + ImGui::MarkIniSettingsDirty(window); + window->SizeFull = size_full; + window->Size = ImMin(window->Size, rect.GetSize()); + } + window->Pos = ImMax(ImMin(window->Pos, rect.Max - window->Size), rect.Min); +} + static void RenderWindowOuterSingleBorder(ImGuiWindow* window, int border_n, ImU32 border_col, float border_size) { const ImGuiResizeBorderDef& def = resize_border_def[border_n]; @@ -7828,7 +7861,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Late create viewport if we don't fit within our current host viewport. if (window->ViewportAllowPlatformMonitorExtend >= 0 && !window->ViewportOwned && !(window->Viewport->Flags & ImGuiViewportFlags_IsMinimized)) - if (!window->Viewport->GetMainRect().Contains(window->Rect())) + if (!window->Viewport->GetMainRect().Contains(window->Rect()) && !WindowStaysInHostViewport(window, window->Viewport)) { // This is based on the assumption that the DPI will be known ahead (same as the DPI of the selection done in UpdateSelectWindowViewport) //ImGuiViewport* old_viewport = window->Viewport; @@ -7878,6 +7911,11 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) ClampWindowPos(window, visibility_rect); } } + + // MaSzyna addition: the host window of a floating dock node has its position set through the api, it's free to move all the same + if (!window->ViewportOwned && !IsWindowBeingMoved(window) && (!window_pos_set_by_api || (flags & ImGuiWindowFlags_DockNodeHost)) && WindowStaysInHostViewport(window, window->Viewport)) + KeepWindowInsideRect(window, viewport_work_rect); + window->Pos = ImTrunc(window->Pos); // Lock window rounding for the frame (so that altering them doesn't cause inconsistencies) @@ -15780,6 +15818,22 @@ static bool ImGui::GetWindowAlwaysWantOwnViewport(ImGuiWindow* window) return false; } +// MaSzyna addition: see io.ConfigViewportsNoAutoDetach +static bool ImGui::WindowStaysInHostViewport(ImGuiWindow* window, ImGuiViewportP* viewport) +{ + ImGuiContext& g = *GImGui; + if (!g.IO.ConfigViewportsNoAutoDetach) + return false; + + const ImGuiWindowFlags not_affected_flags = ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip; + if (window->Flags & not_affected_flags) + return false; + + if (!IsWindowBeingMoved(window) || !IsMousePosValid()) + return true; + return viewport->GetMainRect().Contains(g.IO.MousePos); +} + static bool ImGui::UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImGuiViewportP* viewport) { ImGuiContext& g = *GImGui; @@ -16323,7 +16377,7 @@ static void ImGui::WindowSelectViewport(ImGuiWindow* window) // Fallback: merge in default viewport if z-order matches, otherwise create a new viewport if (window->Viewport == NULL) if (!UpdateTryMergeWindowIntoHostViewport(window, main_viewport)) - window->Viewport = AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_None); + window->Viewport = WindowStaysInHostViewport(window, main_viewport) ? main_viewport : AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_None); // Mark window as allowed to protrude outside of its viewport and into the current monitor if (!lock_viewport) diff --git a/imgui/imgui.h b/imgui/imgui.h index bafe97ff8..6bbc42dec 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -2360,6 +2360,7 @@ struct ImGuiIO bool ConfigViewportsNoTaskBarIcon; // = false // Disable default OS task bar icon flag for secondary viewports. When a viewport doesn't want a task bar icon, ImGuiViewportFlags_NoTaskBarIcon will be set on it. bool ConfigViewportsNoDecoration; // = true // Disable default OS window decoration flag for secondary viewports. When a viewport doesn't want window decorations, ImGuiViewportFlags_NoDecoration will be set on it. Enabling decoration can create subsequent issues at OS levels (e.g. minimum window size). bool ConfigViewportsNoDefaultParent; // = false // Disable default OS parenting to main viewport for secondary viewports. By default, viewports are marked with ParentViewportId = , expecting the platform backend to setup a parent/child relationship between the OS windows (some backend may ignore this). Set to true if you want the default to be 0, then all viewports will be top-level OS windows. + bool ConfigViewportsNoAutoDetach; // = false // MaSzyna addition: a floating window stays inside its host viewport: a resize stops at its edge, and a window left past it, or too big for it, is moved and shrunk to fit. It gets a viewport of its own only when dragged with the mouse cursor past the host viewport. Popups and tooltips are not affected. // Miscellaneous options // (you can visualize and interact with all options in 'Demo->Configuration') From 9364b03e1ad9d9711942864c4d5ab17a00f487d3 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sat, 31 Oct 2026 20:17:47 +0100 Subject: [PATCH 13/90] fix(track): what Sonar reports on the unevenness The blockers are in the EP brake of a vehicle, older than this work: its arrays made with new[] are vectors now, which also removes the deletes. - The caches of the unevenness of a track are no longer mutable: what fills them isn't const any more. - TSegment gets back to RaPositionGet in one piece, with what the path has to do with the others in one struct. - Less nesting in the group of switches and in the fields of the editor, nested conditionals written out, and the attribute is read from a string_view. A test no longer counts in floating point. No change in behaviour. --- application/editormodetrack.cpp | 33 ++++----- tests/unit/test_track_uneven.cpp | 33 ++++++--- vehicle/DynObj.cpp | 73 +++++++++---------- vehicle/DynObj.h | 2 +- world/Segment.cpp | 77 +++++++++----------- world/Segment.h | 29 ++++---- world/Track.h | 21 +++--- world/TrackUneven.cpp | 117 ++++++++++++++++++------------- world/TrackUneven.h | 45 +++++++----- 9 files changed, 223 insertions(+), 207 deletions(-) diff --git a/application/editormodetrack.cpp b/application/editormodetrack.cpp index 49059c213..0ae732d86 100644 --- a/application/editormodetrack.cpp +++ b/application/editormodetrack.cpp @@ -1166,26 +1166,23 @@ void editor_mode::render_path_parameters(TTrack &Track) track->m_uneven.value = std::clamp(track->m_uneven.value, 0.f, track_uneven::most); item_tooltip("Peak deviation of a rail on a 10 m chord. Id-14 limits: 6 mm at 160 km/h, 10 at 120, 18 at 80, 35 at 40.\nWith a sound distance set, the rails also dip at the joints. Joined switches use the lowest of their values"); finish_edit(rebuild::parameters); - struct component - { - char const *label; - float *value; - bool shown; - }; - auto const plain{track->eType == tt_Normal}; - for (auto const &part : {component{"Up and down (mm)", &track->m_uneven.up, true}, - component{"Sideways (mm)", &track->m_uneven.side, true}, - component{"Cross level (mm)", &track->m_uneven.cross, plain}, - component{"Diverging path (mm)", &track->m_uneven.branch, !plain}, - component{"Diverging, up and down (mm)", &track->m_uneven.branchup, !plain}, - component{"Diverging, sideways (mm)", &track->m_uneven.branchside, !plain}}) - { - if (false == part.shown) - continue; - if (ImGui::InputFloat(STR_C(part.label), part.value, 0.0f, 0.0f, "%.1f")) - *part.value = *part.value < 0.f ? -1.f : std::min(*part.value, track_uneven::most); + auto const component = [&](char const *Label, float &Value) { + if (ImGui::InputFloat(STR_C(Label), &Value, 0.0f, 0.0f, "%.1f")) + Value = Value < 0.f ? -1.f : std::min(Value, track_uneven::most); item_tooltip("Set apart from the unevenness, mm. Negative: the unevenness"); finish_edit(rebuild::parameters); + }; + component("Up and down (mm)", track->m_uneven.up); + component("Sideways (mm)", track->m_uneven.side); + if (track->eType == tt_Normal) + { + component("Cross level (mm)", track->m_uneven.cross); + } + else + { + component("Diverging path (mm)", track->m_uneven.branch); + component("Diverging, up and down (mm)", track->m_uneven.branchup); + component("Diverging, sideways (mm)", track->m_uneven.branchside); } } if (track->eType == tt_Normal && (track->iCategoryFlag & 15) == 1) diff --git a/tests/unit/test_track_uneven.cpp b/tests/unit/test_track_uneven.cpp index 65b7da6e1..06f4663b0 100644 --- a/tests/unit/test_track_uneven.cpp +++ b/tests/unit/test_track_uneven.cpp @@ -2,6 +2,8 @@ #include "doctest.h" #include "world/TrackUneven.h" +#include +#include #include #include @@ -12,19 +14,20 @@ double deviation(std::vector const &Values) auto mean{0.0}; for (auto const value : Values) mean += value; - mean /= Values.size(); + auto const count{static_cast(Values.size())}; + mean /= count; auto sum{0.0}; for (auto const value : Values) sum += (value - mean) * (value - mean); - return std::sqrt(sum / Values.size()); + return std::sqrt(sum / count); } double peak(std::vector Values) { for (auto &value : Values) value = std::abs(value); - std::sort(Values.begin(), Values.end()); - return Values[static_cast(0.999 * Values.size())]; + std::ranges::sort(Values); + return Values[Values.size() * 999 / 1000]; } void add_chord(std::vector &Output, std::vector const &Line, int const Half) @@ -44,14 +47,22 @@ TEST_CASE("uneven track: measures as the Id-14 limits for its value") int const steps{4000}; int const base{10}; // 5 m int const lines{36}; - double vertical{0.0}, horizontal{0.0}, twist{0.0}, wander{0.0}, level{0.0}, j{0.0}, away{0.0}; + double vertical{0.0}; + double horizontal{0.0}; + double twist{0.0}; + double wander{0.0}; + double level{0.0}; + double j{0.0}; + double away{0.0}; for (int line = 0; line < lines; ++line) { auto const heading{glm::pi() * (line + 0.37) / lines}; glm::dvec3 const start{7919.0 * std::sin(line * 1.7), 0.0, 6577.0 * std::cos(line * 2.3)}; glm::dvec3 const along{std::cos(heading), 0.0, std::sin(heading)}; glm::dvec3 const across{-along.z, 0.0, along.x}; - std::vector up[2], side[2], middle; + std::array, 2> up; + std::array, 2> side; + std::vector middle; for (int i = 0; i < steps; ++i) { auto const point{start + along * (i * step)}; @@ -64,7 +75,11 @@ TEST_CASE("uneven track: measures as the Id-14 limits for its value") } middle.push_back(track_uneven::field(point).y * amplitude); } - std::vector verticals, horizontals, twists, gauges, levels; + std::vector verticals; + std::vector horizontals; + std::vector twists; + std::vector gauges; + std::vector levels; for (int rail = 0; rail < 2; ++rail) { add_chord(verticals, up[rail], base); @@ -269,9 +284,9 @@ TEST_CASE("uneven track: the value along a track meets its neighbours exactly") CHECK(track_uneven::along(18.0, 35.0, 35.0, 0.25, 0.5) == doctest::Approx(35.0).epsilon(0.01)); // no jump anywhere auto last{track_uneven::along(18.0, 35.0, 0.0, 0.0, length)}; - for (auto distance{0.25}; distance <= length; distance += 0.25) + for (int step = 1; step <= 2000; ++step) { - auto const value{track_uneven::along(18.0, 35.0, 0.0, distance, length)}; + auto const value{track_uneven::along(18.0, 35.0, 0.0, step * 0.25, length)}; REQUIRE(std::abs(value - last) < 0.5); last = value; } diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index ac87f395c..305e12020 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -3382,14 +3382,12 @@ bool TDynamicObject::Update(double dt, double dt1) FzadED = 0; //pętla bezpieczeństwa - bez ED auto const FzadPN = Fzad - FrED * MoverParameters->MED_FrED_factor; //np = 0; - // BUG: likely memory leak, allocation per inner loop, deleted only once outside - // TODO: sort this shit out - bool* PrzekrF = new bool[np]; + std::vector PrzekrF( np ); float nPrzekrF = 0; bool test = true; - float* FzED = new float[np]; - float* FzEP = new float[np]; - float* FmaxEP = new float[np]; + std::vector FzED( np ); + std::vector FzEP( np ); + std::vector FmaxEP( np ); // 3. ustaw pojazdom sile hamowania ED // - proporcjonalnie do mozliwosci @@ -3548,11 +3546,6 @@ bool TDynamicObject::Update(double dt, double dt1) } - delete[] PrzekrF; - delete[] FzED; - delete[] FzEP; - delete[] FmaxEP; - MED_oldFED = FzadED; } @@ -3661,33 +3654,32 @@ bool TDynamicObject::Update(double dt, double dt1) 1 : ctOwner->Vehicle()->DirectionGet() == DirectionGet() ? 1 : -1 ) }; for( auto &axle : m_axlesounds ) { - if( axle_passed_joint( axle, dDOMoveLen, directioninconsist ) ) { - if( MoverParameters->Vel > 0.1 ) { - // NOTE: for combined clatter sound we supply 1/100th of actual value, as the sound module converts does the opposite, converting received (typically) 0-1 values to 0-100 range - auto const frequency = true == axle.clatter.is_combined() ? MoverParameters->Vel * 0.01 : 1.0; - axle.clatter - .pitch( frequency ) - .gain( volume ) - .play(); - // crude bump simulation, drop down on even axles, move back up on - // the odd ones - // MoverParameters->AccVert += (MoverParameters->Vel*0.1f) * - double const headroom = std::clamp( - MoverParameters->Vmax - ( MoverParameters->Vel + MoverParameters->Vmax * 0.32f ), - 0.0, MoverParameters->Vmax ); - double const jolt = headroom * .05f * ( MyTrack->iDamageFlag * 0.25f ); - if (MyTrack->eType == tt_Normal) - { - MoverParameters->AccVert += std::clamp( jolt, 0.0, 4.0 ); - } - else if (MyTrack->eType == tt_Switch) - { - double const accHorizontal = std::clamp( jolt, 0.0, 1.0 ) * ((axleindex % 2) != 0 ? 1 : -1); - MoverParameters->AccS += accHorizontal; - MoverParameters->AccN += accHorizontal; - MoverParameters->AccVert += std::clamp( jolt, 0.0, 2.0 ); - } - } + if( axle_passed_joint( axle, dDOMoveLen, directioninconsist ) + && MoverParameters->Vel > 0.1 ) { + // NOTE: for combined clatter sound we supply 1/100th of actual value, as the sound module converts does the opposite, converting received (typically) 0-1 values to 0-100 range + auto const frequency = true == axle.clatter.is_combined() ? MoverParameters->Vel * 0.01 : 1.0; + axle.clatter + .pitch( frequency ) + .gain( volume ) + .play(); + // crude bump simulation, drop down on even axles, move back up on + // the odd ones + // MoverParameters->AccVert += (MoverParameters->Vel*0.1f) * + double const headroom = std::clamp( + MoverParameters->Vmax - ( MoverParameters->Vel + MoverParameters->Vmax * 0.32f ), + 0.0, MoverParameters->Vmax ); + double const jolt = headroom * .05f * ( MyTrack->iDamageFlag * 0.25f ); + if (MyTrack->eType == tt_Normal) + { + MoverParameters->AccVert += std::clamp( jolt, 0.0, 4.0 ); + } + else if (MyTrack->eType == tt_Switch) + { + double const accHorizontal = std::clamp( jolt, 0.0, 1.0 ) * ((axleindex % 2) != 0 ? 1 : -1); + MoverParameters->AccS += accHorizontal; + MoverParameters->AccN += accHorizontal; + MoverParameters->AccVert += std::clamp( jolt, 0.0, 2.0 ); + } } ++axleindex; } @@ -8072,12 +8064,11 @@ TDynamicObject::update_shake( double const Timedelta ) { } bool -TDynamicObject::axle_passed_joint( axle_sounds &Axle, double const Move, int const Direction ) { +TDynamicObject::axle_passed_joint( axle_sounds &Axle, double const Move, int const Direction ) const { // by the outer axle on its side of the vehicle: the two can be on different tracks auto const &outer { Axle.offset >= 0.0 ? Axle0 : Axle1 }; - auto const railphase { outer.RailPhase( Axle.offset - std::copysign( 0.5 * fAxleDist, Axle.offset ) ) }; - if( railphase >= 0.0 ) { + if( auto const railphase { outer.RailPhase( Axle.offset - std::copysign( 0.5 * fAxleDist, Axle.offset ) ) }; railphase >= 0.0 ) { // the rails under the axle, which aren't always those under the track the vehicle is on auto const passed { Axle.atjoints && std::abs( railphase - Axle.distance ) > 0.5 * outer.GetTrack()->fSoundDistance }; Axle.distance = railphase; diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index b903f0874..4fee9778e 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -405,7 +405,7 @@ private: bool atjoints { false }; // distance comes from TTrack::rail_phase }; // whether the axle has just passed a rail joint. On a track with dipping joints it goes by them, elsewhere the axle counts a rail off itself - bool axle_passed_joint( axle_sounds &Axle, double const Move, int const Direction ); + bool axle_passed_joint( axle_sounds &Axle, double const Move, int const Direction ) const; struct powertrain_sounds { sound_source inverter { sound_placement::engine }; diff --git a/world/Segment.cpp b/world/Segment.cpp index 9c0e1077e..1aff4ae42 100644 --- a/world/Segment.cpp +++ b/world/Segment.cpp @@ -69,10 +69,8 @@ bool TSegment::Init(glm::dvec3 &NewPoint1, glm::dvec3 NewCPointOut, glm::dvec3 N // poprawienie przechyłki fRoll1 = static_cast(glm::radians(fNewRoll1)); // Ra: przeliczone jest bardziej przydatne do obliczeń fRoll2 = static_cast(glm::radians(fNewRoll2)); - m_designrolls[ 0 ] = fRoll1; - m_designrolls[ 1 ] = fRoll2; - m_joints[ 0 ].reset(); - m_joints[ 1 ].reset(); + m_setting.designrolls = { fRoll1, fRoll2 }; + m_setting.joints = {}; bCurve = bIsCurve; if (Global.bRollFix) { // Ra: poprawianie przechyłki @@ -122,7 +120,7 @@ bool TSegment::Init(glm::dvec3 &NewPoint1, glm::dvec3 NewCPointOut, glm::dvec3 N fStoop = std::atan2(Point2.y - Point1.y, fLength); // pochylenie toru prostego, żeby nie liczyć wielokrotnie fStep = fNewStep; - if( m_uneven && pOwner->eType != tt_Switch ) { + if( m_setting.uneven && pOwner->eType != tt_Switch ) { // dense enough for the waves; a switch keeps its division, the blades depend on it fStep = std::min( fStep, 2.0 ); } @@ -373,21 +371,6 @@ Math3D::vector3 TSegment::GetPoint(double const fDistance) const }; */ -void TSegment::RaPositionGet(double const fDistance, glm::dvec3 &position, glm::vec3 &rotation) const { - design_position( fDistance, position, rotation ); - if( false == m_uneven ) { - return; - } - // direction and slope stay as designed: the physics takes the curve radius and the grade from them - auto const gauge { std::max( std::abs( pOwner->fTrackWidth ), 0.1f ) }; - glm::dvec3 const across { -std::cos( rotation.z ), 0.0, -std::sin( rotation.z ) }; - auto const amount { pOwner->uneven_at( fDistance, m_path ) }; - rotation.x += static_cast( track_uneven::stray_per_mm * ( - amount.up * track_uneven::roll( position, across, gauge ) - + amount.cross * track_uneven::level( position, across ) / gauge ) ); - position += track_uneven::across_only( stray( position, amount, fDistance ), across ); -} - glm::dvec3 TSegment::stray( glm::dvec3 const &Point, track_uneven::amounts const &Amount, double const Distance ) const { auto const field { track_uneven::field( Point ) }; glm::dvec3 result { field.x * Amount.side, field.y * Amount.up, field.z * Amount.side }; @@ -397,30 +380,25 @@ glm::dvec3 TSegment::stray( glm::dvec3 const &Point, track_uneven::amounts const } glm::dvec3 TSegment::uneven_bend( double const Distance ) const { - if( false == m_uneven ) { + if( false == m_setting.uneven ) { return glm::dvec3 { 0.0 }; } // over about a bogie auto const reach { 1.5 }; - auto const strayed = [&]( double const At ) { - return stray( point_extended( At ), pOwner->uneven_at( At, m_path ), At ); }; + auto const strayed = [ this ]( double const At ) { + // past the ends of the path straight on: the polynomial would run off on a short curved one + auto const on { std::clamp( At, 0.0, fLength ) }; + auto const t { FastGetTFromS( on ) }; + auto point { FastGetPoint( t ) }; + if( auto const direction { FastGetDirection( t, 0.1 / fLength ) }; on != At && glm::length( direction ) > 0.0 ) { + point += direction * ( ( At - on ) / glm::length( direction ) ); + } + return stray( point, pOwner->uneven_at( At, m_setting.path ), At ); }; return ( strayed( Distance + reach ) - 2.0 * strayed( Distance ) + strayed( Distance - reach ) ) / ( reach * reach ); } -glm::dvec3 TSegment::point_extended( double const Distance ) const { - auto const on { std::clamp( Distance, 0.0, fLength ) }; - auto const t { FastGetTFromS( on ) }; - auto const point { FastGetPoint( t ) }; - if( on == Distance ) { - return point; - } - auto const direction { FastGetDirection( t, 0.1 / fLength ) }; - auto const size { glm::length( direction ) }; - return size > 0.0 ? point + direction * ( ( Distance - on ) / size ) : point; -} - void TSegment::settle_vertex( gfx::basic_vertex &Vertex, glm::dvec3 const &Origin, glm::vec3 const &Position, glm::vec3 const &Parallel, double const Distance ) const { - if( false == m_uneven ) { + if( false == m_setting.uneven ) { return; } // the rails settling apart turn about the middle of the track, what's outside them follows the rail; the foot of the ballast stays @@ -430,12 +408,12 @@ void TSegment::settle_vertex( gfx::basic_vertex &Vertex, glm::dvec3 const &Origi if( share <= 0.0 ) { return; } - auto const amount { pOwner->uneven_at( Distance, m_path ) }; + auto const amount { pOwner->uneven_at( Distance, m_setting.path ) }; Vertex.position.y -= static_cast( share * track_uneven::stray_per_mm * amount.cross * track_uneven::level( Origin + glm::dvec3 { Position }, glm::dvec3 { Parallel } ) * across / gauge ); Vertex.position += glm::vec3 { share * stray( Origin + glm::dvec3 { Vertex.position }, amount, Distance ) }; } -void TSegment::design_position(double const fDistance, glm::dvec3 &position, glm::vec3 &rotation) const { +void TSegment::RaPositionGet(double const fDistance, glm::dvec3 &position, glm::vec3 &rotation) const { if (bCurve) { // można by wprowadzić uproszczony wzór dla okręgów płaskich auto const t = FastGetTFromS(fDistance); // aproksymacja dystansu na krzywej Beziera na parametr (t) @@ -458,6 +436,17 @@ void TSegment::design_position(double const fDistance, glm::dvec3 &position, glm rotation.y = static_cast(fStoop); // pochylenie toru prostego rotation.z = static_cast(fDirection); // kierunek toru w planie } + if( false == m_setting.uneven ) { + return; + } + // direction and slope stay as designed: the physics takes the curve radius and the grade from them + auto const gauge { std::max( std::abs( pOwner->fTrackWidth ), 0.1f ) }; + glm::dvec3 const across { -std::cos( rotation.z ), 0.0, -std::sin( rotation.z ) }; + auto const amount { pOwner->uneven_at( fDistance, m_setting.path ) }; + rotation.x += static_cast( track_uneven::stray_per_mm * ( + amount.up * track_uneven::roll( position, across, gauge ) + + amount.cross * track_uneven::level( position, across ) / gauge ) ); + position += track_uneven::across_only( stray( position, amount, fDistance ), across ); }; void TSegment::plan_frame( double const Distance, glm::dvec3 &Point, glm::dvec3 &Across ) const { @@ -523,9 +512,9 @@ bool TSegment::RenderLoft( gfx::vertex_array &Output, glm::dvec3 const &Origin, parallel1 = glm::vec3{ glm::dvec3{ FastGetPoint_1() - FastGetPoint_0() } }; } parallel1 = glm::normalize( parallel1 ); - if( iSkip == 0 && m_joints[ 0 ] ) { - pos1 = glm::vec3{ m_joints[ 0 ]->first - Origin }; - parallel1 = glm::vec3{ m_joints[ 0 ]->second }; + if( iSkip == 0 && m_setting.joints[ 0 ] ) { + pos1 = glm::vec3{ m_setting.joints[ 0 ]->first - Origin }; + parallel1 = glm::vec3{ m_setting.joints[ 0 ]->second }; } if( iEnd == 0 ) iEnd = iSegCount; @@ -572,9 +561,9 @@ bool TSegment::RenderLoft( gfx::vertex_array &Output, glm::dvec3 const &Origin, parallel2 = glm::vec3{ glm::dvec3{ FastGetPoint_1() - FastGetPoint_0() } }; } parallel2 = glm::normalize( parallel2 ); - if( i == iSegCount && m_joints[ 1 ] ) { - pos2 = glm::vec3{ m_joints[ 1 ]->first - Origin }; - parallel2 = glm::vec3{ m_joints[ 1 ]->second }; + if( i == iSegCount && m_setting.joints[ 1 ] ) { + pos2 = glm::vec3{ m_setting.joints[ 1 ]->first - Origin }; + parallel2 = glm::vec3{ m_setting.joints[ 1 ]->second }; } // TODO: refactor the loop, there's no need to calculate starting points for each segment when we can copy the end points of the previous one diff --git a/world/Segment.h b/world/Segment.h index 75e070152..099480e36 100644 --- a/world/Segment.h +++ b/world/Segment.h @@ -9,7 +9,9 @@ http://mozilla.org/MPL/2.0/. #pragma once +#include #include +#include #include "utilities/Classes.h" #include "rendering/geometrybank.h" @@ -50,11 +52,14 @@ class TSegment glm::dvec3 Point2; float fRoll1 { 0.f }; float fRoll2 { 0.f }; // przechyłka na końcach - float m_designrolls[ 2 ] { 0.f, 0.f }; // as entered; the two above can differ at a joint - bool m_uneven { false }; // see TrackUneven.h - int m_path { 0 }; // which path of a switch - // where the path meets the next one: the middle of their ends and their common vector across - std::optional> m_joints[ 2 ]; + // how the path sits among the others + struct setting { + std::array designrolls { 0.f, 0.f }; // as entered; fRoll1 and fRoll2 can differ at a joint + bool uneven { false }; // see TrackUneven.h + int path { 0 }; // which path of a switch + // where the path meets the next one: the middle of their ends and their common vector across + std::array>, 2> joints; + } m_setting; double fLength { -1.0 }; // długość policzona std::vector fTsBuffer; // wartości parametru krzywej t dla równych odcinków s std::vector fTsSlope; // nachylenie dt/du (= dt/ds * fStep) w tych samych punktach @@ -79,9 +84,6 @@ class TSegment void settle_vertex( gfx::basic_vertex &Vertex, glm::dvec3 const &Origin, glm::vec3 const &Position, glm::vec3 const &Parallel, double const Distance ) const; // displacement of a point by the unevenness, at a distance along the path glm::dvec3 stray( glm::dvec3 const &Point, track_uneven::amounts const &Amount, double const Distance ) const; - // a point of the path at a distance, past its ends straight on: the polynomial would run off on a short curved path - glm::dvec3 point_extended( double const Distance ) const; - void design_position( double const fDistance, glm::dvec3 &position, glm::vec3 &rotation ) const; public: bool bCurve = false; @@ -125,20 +127,17 @@ public: // call before Init, it sets the density of the geometry void set_uneven( bool const Uneven, int const Path = 0 ) { - m_uneven = Uneven; - m_path = Path; } - glm::dvec3 - end_point( int const End ) const { - return End == 0 ? Point1 : Point2; } + m_setting.uneven = Uneven; + m_setting.path = Path; } // a point of the path and the unit vector across it in the plan void plan_frame( double const Distance, glm::dvec3 &Point, glm::dvec3 &Across ) const; float design_roll( int const End ) const { - return m_designrolls[ End ]; } + return m_setting.designrolls[ End ]; } void set_joint( int const End, std::optional> const &Joint, float const Roll ) { - m_joints[ End ] = Joint; + m_setting.joints[ End ] = Joint; ( End == 0 ? fRoll1 : fRoll2 ) = Roll; } // second derivative of the displacement along the path; times speed squared: acceleration of an axle glm::dvec3 diff --git a/world/Track.h b/world/Track.h index f5bb8c1b6..72cbd8847 100644 --- a/world/Track.h +++ b/world/Track.h @@ -349,15 +349,15 @@ public: track_uneven::amounts uneven_declared( int const Path = 0 ) const; bool is_uneven() const; // the unevenness at a distance along a path, meeting the tracks joined at its ends - track_uneven::amounts uneven_at( double const Distance, int const Path = 0 ) const; + track_uneven::amounts uneven_at( double const Distance, int const Path = 0 ); // how much of the displacement a point of the profile at the depth takes double uneven_bed_share( double const Depth ) const; // closes the gaps and steps of cant between the ends of joined paths void share_joints(); double joint_depth() const; // mm - double joint_dip( double const Distance ) const; // m + double joint_dip( double const Distance ); // m // distance from the rail joint behind; negative: the track has no joints that dip - double rail_phase( double const Distance ) const; + double rail_phase( double const Distance ); void ConnectionsLog(); bool DoubleSlip() const; static void fetch_default_profiles(); @@ -395,8 +395,8 @@ private: double way { 1.0 }; // direction of the path along the run int stamp { -1 }; }; - void settle_rail_run() const; - mutable rail_run m_railrun; + void settle_rail_run(); + rail_run m_railrun; // the value along each path of a switch: the lowest of that path in the switches joined, 0 next to a track with none. // And at the ends of the paths, by what meets there: the other path at the common end, the path of a switch joined struct switch_stray { @@ -404,15 +404,16 @@ private: std::array, 2> ends; int stamp { -1 }; }; - mutable switch_stray m_switchstray; - switch_stray const &switch_uneven() const; - void settle_switch_stray() const; + switch_stray m_switchstray; + switch_stray const &switch_uneven(); + void settle_switch_stray(); + void settle_switch_ends(); // the lowest value of the paths ending at the point track_uneven::amounts switch_paths_at( glm::dvec3 const &Point ) const; // what a plain track comes to at its end, where it meets the other one - track_uneven::amounts uneven_met( TTrack const *Other, glm::dvec3 const &Point ) const; + track_uneven::amounts uneven_met( TTrack *Other, glm::dvec3 const &Point ); // the value of a switch where a track joined to it meets it - track_uneven::amounts switch_uneven_at( glm::dvec3 const &Point ) const; + track_uneven::amounts switch_uneven_at( glm::dvec3 const &Point ); static int s_layoutstamp; // bumped when tracks are joined or changed, invalidates the two above void create_road_profile( gfx::vertex_array &Output, bool const Forcetransition = false ); void create_road_side_profile( gfx::vertex_array &Right, gfx::vertex_array &Left, gfx::vertex_array const &Road, bool const Forcetransition = false ); diff --git a/world/TrackUneven.cpp b/world/TrackUneven.cpp index 152449ad2..78505ac3a 100644 --- a/world/TrackUneven.cpp +++ b/world/TrackUneven.cpp @@ -24,42 +24,55 @@ bool is_switch( TTrack const *Track ) { return Track != nullptr && Track->eType == tt_Switch && Track->SwitchExtension; } -std::array joined_to_switch( TTrack const &Switch ) { +glm::dvec3 end_point( TSegment const &Segment, int const End ) { + + return End == 0 ? Segment.FastGetPoint_0() : Segment.FastGetPoint_1(); +} + +std::array joined_to_switch( TTrack const &Switch ) { auto const &extension { *Switch.SwitchExtension }; return { extension.pPrevs[ 0 ], extension.pPrevs[ 1 ], extension.pNexts[ 0 ], extension.pNexts[ 1 ] }; } // the switch and the switches joined directly to it, and to those -std::vector switch_group( TTrack const &Switch ) { +std::vector switch_group( TTrack &Switch ) { - std::vector group { &Switch }; - for( std::size_t index = 0; index < group.size(); ++index ) { - for( auto const *other : joined_to_switch( *group[ index ] ) ) { - if( is_switch( other ) && std::find( group.begin(), group.end(), other ) == group.end() ) { + std::vector group { &Switch }; + std::vector pending { &Switch }; + while( false == pending.empty() ) { + auto const *track { pending.back() }; + pending.pop_back(); + for( auto *other : joined_to_switch( *track ) ) { + if( is_switch( other ) && std::ranges::find( group, other ) == group.end() ) { group.push_back( other ); + pending.push_back( other ); } } } return group; } +// a plain track with none of a component keeps the switch to its design in it, any other comes to the value of the switch +void keep_to_plain_track( std::array &Values, TTrack const &Plain ) { + + auto const theirs { Plain.uneven_declared() }; + for( auto &path : Values ) { + if( theirs.up <= 0.0 ) { path.up = 0.0; } + if( theirs.side <= 0.0 ) { path.side = 0.0; } + } +} + // the value along each path: the lowest of that path in the group, 0 in a component that a plain track joined has none of -std::array group_path_values( std::vector const &Group ) { +std::array group_path_values( std::vector const &Group ) { std::array values { Group.front()->uneven_declared( 0 ), Group.front()->uneven_declared( 1 ) }; for( auto const *track : Group ) { values[ 0 ] = track_uneven::least( values[ 0 ], track->uneven_declared( 0 ) ); values[ 1 ] = track_uneven::least( values[ 1 ], track->uneven_declared( 1 ) ); for( auto const *other : joined_to_switch( *track ) ) { - if( other == nullptr || is_switch( other ) ) { - continue; - } - // any other comes to the value of the switch - auto const theirs { other->uneven_declared() }; - for( auto &path : values ) { - if( theirs.up <= 0.0 ) { path.up = 0.0; } - if( theirs.side <= 0.0 ) { path.side = 0.0; } + if( other != nullptr && false == is_switch( other ) ) { + keep_to_plain_track( values, *other ); } } } @@ -74,7 +87,7 @@ track_uneven::amounts least_at_ends( TTrack const &Switch, glm::dvec3 const &Poi track_uneven::amounts result { track_uneven::most, track_uneven::most, track_uneven::most }; for( int path = 0; path < 2; ++path ) { for( int end = 0; end < 2; ++end ) { - if( glm::length( Switch.path_segment( path )->end_point( end ) - Point ) < switch_end_reach ) { + if( glm::length( end_point( *Switch.path_segment( path ), end ) - Point ) < switch_end_reach ) { result = track_uneven::least( result, Value( path, end ) ); found = true; } @@ -165,16 +178,16 @@ double TTrack::uneven_bed_share( double const Depth ) const { return track_uneven::bed_share( Depth, std::abs( track_rail_profile( m_profile1.second ).front().position.y ), fTexHeight1 ); } -track_uneven::amounts TTrack::uneven_at( double const Distance, int const Path ) const { +track_uneven::amounts TTrack::uneven_at( double const Distance, int const Path ) { if( eType == tt_Switch ) { auto const &stray { switch_uneven() }; return track_uneven::along( stray.paths[ Path ], stray.ends[ Path ][ 0 ], stray.ends[ Path ][ 1 ], Distance, path_segment( Path )->GetLength() ); } - return track_uneven::along( uneven_declared(), uneven_met( trPrev, Segment->end_point( 0 ) ), uneven_met( trNext, Segment->end_point( 1 ) ), Distance, Length() ); + return track_uneven::along( uneven_declared(), uneven_met( trPrev, end_point( *Segment, 0 ) ), uneven_met( trNext, end_point( *Segment, 1 ) ), Distance, Length() ); } -track_uneven::amounts TTrack::uneven_met( TTrack const *Other, glm::dvec3 const &Point ) const { +track_uneven::amounts TTrack::uneven_met( TTrack *Other, glm::dvec3 const &Point ) { if( Other == nullptr ) { return {}; @@ -182,7 +195,7 @@ track_uneven::amounts TTrack::uneven_met( TTrack const *Other, glm::dvec3 const return Other->eType == tt_Switch ? Other->switch_uneven_at( Point ) : track_uneven::least( uneven_declared(), Other->uneven_declared() ); } -TTrack::switch_stray const &TTrack::switch_uneven() const { +TTrack::switch_stray const &TTrack::switch_uneven() { if( m_switchstray.stamp != s_layoutstamp ) { settle_switch_stray(); @@ -190,26 +203,30 @@ TTrack::switch_stray const &TTrack::switch_uneven() const { return m_switchstray; } -void TTrack::settle_switch_stray() const { +void TTrack::settle_switch_stray() { auto const group { switch_group( *this ) }; auto const values { group_path_values( group ) }; - for( auto const *track : group ) { + for( auto *track : group ) { track->m_switchstray.paths = values; track->m_switchstray.stamp = s_layoutstamp; } - // at the ends of the paths, with the paths known in all of the group - for( auto const *track : group ) { - for( int path = 0; path < 2; ++path ) { - for( int end = 0; end < 2; ++end ) { - auto const point { track->path_segment( path )->end_point( end ) }; - auto value { track->switch_paths_at( point ) }; - auto const *other { track->path_neighbour( path, end ) }; - if( is_switch( other ) ) { - value = track_uneven::least( value, other->switch_paths_at( point ) ); - } - track->m_switchstray.ends[ path ][ end ] = value; + // with the paths known in all of the group + for( auto *track : group ) { + track->settle_switch_ends(); + } +} + +void TTrack::settle_switch_ends() { + + for( int path = 0; path < 2; ++path ) { + for( int end = 0; end < 2; ++end ) { + auto const point { end_point( *path_segment( path ), end ) }; + auto value { switch_paths_at( point ) }; + if( auto const *other { path_neighbour( path, end ) }; is_switch( other ) ) { + value = track_uneven::least( value, other->switch_paths_at( point ) ); } + m_switchstray.ends[ path ][ end ] = value; } } } @@ -217,13 +234,13 @@ void TTrack::settle_switch_stray() const { track_uneven::amounts TTrack::switch_paths_at( glm::dvec3 const &Point ) const { auto const &paths { m_switchstray.paths }; - return least_at_ends( *this, Point, track_uneven::least( paths[ 0 ], paths[ 1 ] ), [ & ]( int const Path, int ) { return paths[ Path ]; } ); + return least_at_ends( *this, Point, track_uneven::least( paths[ 0 ], paths[ 1 ] ), [ &paths ]( int const Path, int ) { return paths[ Path ]; } ); } -track_uneven::amounts TTrack::switch_uneven_at( glm::dvec3 const &Point ) const { +track_uneven::amounts TTrack::switch_uneven_at( glm::dvec3 const &Point ) { auto const &stray { switch_uneven() }; - return least_at_ends( *this, Point, track_uneven::least( stray.paths[ 0 ], stray.paths[ 1 ] ), [ & ]( int const Path, int const End ) { return stray.ends[ Path ][ End ]; } ); + return least_at_ends( *this, Point, track_uneven::least( stray.paths[ 0 ], stray.paths[ 1 ] ), [ &stray ]( int const Path, int const End ) { return stray.ends[ Path ][ End ]; } ); } double TTrack::joint_depth() const { @@ -234,7 +251,7 @@ double TTrack::joint_depth() const { return ( m_uneven.dip >= 0.f ? m_uneven.dip : track_uneven::dip_share * uneven_declared().up ); } -double TTrack::joint_dip( double const Distance ) const { +double TTrack::joint_dip( double const Distance ) { auto const phase { rail_phase( Distance ) }; if( phase < 0.0 ) { @@ -245,12 +262,12 @@ double TTrack::joint_dip( double const Distance ) const { return 0.0; } auto const depth { joint_depth() }; - auto const atend = [&]( TTrack const *Other ) { + auto const atend = [ this, depth ]( TTrack const *Other ) { return ( Other == nullptr || Other->fSoundDistance != fSoundDistance ? 0.0 : std::min( depth, Other->joint_depth() ) ); }; return 0.001 * track_uneven::dip( fromjoint ) * track_uneven::along( depth, atend( trPrev ), atend( trNext ), Distance, Length() ); } -double TTrack::rail_phase( double const Distance ) const { +double TTrack::rail_phase( double const Distance ) { if( joint_depth() <= 0.0 || fSoundDistance <= 2.0 * track_uneven::dip_reach ) { return -1.0; @@ -261,22 +278,22 @@ double TTrack::rail_phase( double const Distance ) const { return track_uneven::rail_phase( m_railrun.origin + m_railrun.way * Distance, fSoundDistance ); } -void TTrack::settle_rail_run() const { +void TTrack::settle_rail_run() { // the next track of the run past an end of a track, and which of its ends is joined there - auto const onward = []( TTrack const &Track, int const End, int &Joined ) -> TTrack const * { - auto const *other { End == 0 ? Track.trPrev : Track.trNext }; + auto const onward = []( TTrack const &Track, int const End, int &Joined ) -> TTrack * { + auto *other { End == 0 ? Track.trPrev : Track.trNext }; if( other == nullptr || other == &Track || other->joint_depth() <= 0.0 || other->fSoundDistance != Track.fSoundDistance ) { return nullptr; } Joined = ( End == 0 ? Track.iPrevDirection : Track.iNextDirection ) & 1; return other; }; // walks from this track to an end of the run - auto const walk = [&]( int &End ) { - auto const *track { this }; + auto const walk = [ this, &onward ]( int &End ) { + auto *track { this }; int joined { 0 }; for( int step = 0; step < 1000000; ++step ) { - auto const *next { onward( *track, End, joined ) }; + auto *next { onward( *track, End, joined ) }; if( next == nullptr || next == this ) { break; } @@ -285,15 +302,14 @@ void TTrack::settle_rail_run() const { } return track; }; auto const place = []( TTrack const &Track, int const End ) { - auto const point { Track.Segment->end_point( End ) }; + auto const point { end_point( *Track.Segment, End ) }; return std::make_pair( point.x, point.z ); }; // start at the end with the lesser coordinates, to get the same joints from any track of the run int end { 0 }; int otherend { 1 }; - auto const *track { walk( end ) }; - auto const *other { walk( otherend ) }; - if( place( *other, otherend ) < place( *track, end ) ) { + auto *track { walk( end ) }; + if( auto *other { walk( otherend ) }; place( *other, otherend ) < place( *track, end ) ) { track = other; end = otherend; } @@ -327,7 +343,8 @@ void TTrack::share_joints() { } } if( eType != tt_Switch && Segment && ( iCategoryFlag & 1 ) ) { - float roll1, roll2; + float roll1 { 0.f }; + float roll2 { 0.f }; Segment->GetRolls( roll1, roll2 ); if( roll1 != 0.f || roll2 != 0.f ) { iTrapezoid |= 1; diff --git a/world/TrackUneven.h b/world/TrackUneven.h index 624e6db16..2c32ad660 100644 --- a/world/TrackUneven.h +++ b/world/TrackUneven.h @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include #include @@ -51,18 +52,20 @@ struct wave { // the numbers of an entry like "uneven 14;35", negative where left out template -std::array read_fields( std::string const &Text ) { +std::array read_fields( std::string_view const Text ) { std::array result; result.fill( -1.f ); - std::size_t start { 0 }; - for( std::size_t index = 0; index < Count && start < Text.size(); ++index ) { - auto const end { std::min( Text.find( ';', start ), Text.size() ) }; - auto value { 0.f }; - if( end > start && std::from_chars( Text.data() + start, Text.data() + end, value ).ec == std::errc {} ) { - result[ index ] = std::clamp( value, 0.f, most ); + auto rest { Text }; + for( auto &field : result ) { + auto const end { std::min( rest.find( ';' ), rest.size() ) }; + if( auto value { 0.f }; end > 0 && std::from_chars( rest.data(), rest.data() + end, value ).ec == std::errc {} ) { + field = std::clamp( value, 0.f, most ); } - start = end + 1; + if( end == rest.size() ) { + break; + } + rest.remove_prefix( end + 1 ); } return result; } @@ -100,7 +103,7 @@ struct attribute { }; // a plain track: value;up;side;cross;dip. A switch: p1-p2;p3-p4;up of p1-p2;side of p1-p2;up of p3-p4;side of p3-p4 -inline attribute read_attribute( std::string const &Text, bool const Switch ) { +inline attribute read_attribute( std::string_view const Text, bool const Switch ) { auto const fields { read_fields<6>( Text ) }; attribute result; @@ -136,15 +139,15 @@ inline std::string write_attribute( attribute const &Values, bool const Switch ) // what a path comes to from the values: what's not given takes the first value, p3-p4 those of p1-p2. Cross level on plain tracks only inline amounts declared( attribute const &Values, bool const Plain, int const Path ) { + auto const given = []( float const Part, double const Otherwise ) { + return Part >= 0.f ? static_cast( Part ) : Otherwise; }; amounts result { - Values.up >= 0.f ? Values.up : Values.value, - Values.side >= 0.f ? Values.side : Values.value, - false == Plain ? 0.f : Values.cross >= 0.f ? Values.cross : Values.value }; + given( Values.up, Values.value ), + given( Values.side, Values.value ), + Plain ? given( Values.cross, Values.value ) : 0.0 }; if( Path == 1 ) { - auto const branch = [ &Values ]( float const Part, double const Straight ) -> double { - return Part >= 0.f ? Part : Values.branch >= 0.f ? Values.branch : Straight; }; - result.up = branch( Values.branchup, result.up ); - result.side = branch( Values.branchside, result.side ); + result.up = given( Values.branchup, given( Values.branch, result.up ) ); + result.side = given( Values.branchside, given( Values.branch, result.side ) ); } return result; } @@ -160,8 +163,9 @@ wave_set make_waves( double const Turn, std::array const & wave_set result {}; for( std::size_t i = 0; i < Count; ++i ) { auto const number { glm::two_pi() / Lengths[ i ] }; - auto const heading { Turn + golden * i }; - result[ i ] = { number * std::cos( heading ), number * std::sin( heading ), golden * ( i + 1 ) * ( 1.0 + 3.0 * Turn ), Shares[ i ] }; + auto const place { static_cast( i ) }; + auto const heading { Turn + golden * place }; + result[ i ] = { number * std::cos( heading ), number * std::sin( heading ), golden * ( place + 1.0 ) * ( 1.0 + 3.0 * Turn ), Shares[ i ] }; } return result; } @@ -246,7 +250,10 @@ inline glm::dvec3 across_of( glm::dvec3 const &Direction, glm::dvec3 const &Chor // the cant where two paths meet, theirs turned to the way of this one: a switch takes that of a plain track, alike ones meet halfway inline float joint_roll( float const Own, float const Their, bool const Switch, bool const Theirswitch ) { - return Switch == Theirswitch ? 0.5f * ( Own + Their ) : Switch ? Their : Own; + if( Switch == Theirswitch ) { + return 0.5f * ( Own + Their ); + } + return Switch ? Their : Own; } // each component the same way From 32fadd634d259595a961556fd5e6349895569dec Mon Sep 17 00:00:00 2001 From: maj00r Date: Sat, 31 Oct 2026 20:17:47 +0100 Subject: [PATCH 14/90] refactor(tools): the grading tool in smaller functions As Sonar asks, and a regular expression with no backtracking. The results stay the same byte for byte on baltyk_skm1, baltyk_cargo, td and lk357. --- tools/track_uneven_grade.py | 348 +++++++++++++++++++++--------------- 1 file changed, 208 insertions(+), 140 deletions(-) diff --git a/tools/track_uneven_grade.py b/tools/track_uneven_grade.py index d643f94f8..1994559d0 100644 --- a/tools/track_uneven_grade.py +++ b/tools/track_uneven_grade.py @@ -43,7 +43,7 @@ STRAY_PER_MM, DIP_SHARE, DIP_REACH = 0.00145, 0.4, 3.0 GOLDEN = math.pi * (3.0 - math.sqrt(5.0)) UP_WAVES = [(2 * math.pi / length * math.cos(GOLDEN * i), 2 * math.pi / length * math.sin(GOLDEN * i), GOLDEN * (i + 1), share) for i, (length, share) in enumerate(zip((8.3, 11.9, 16.7, 23.9, 37.1, 47.3, 61.9, 83.3), (.14, .19, .22, .22, .23, .23, .23, .23)))] TOKEN = re.compile(r"//[^\n]*|\S+") -NUMBER = re.compile(r"^[-+]?\d*\.?\d+([eE][-+]?\d+)?$") +NUMBER = re.compile(r"^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$") def limit(speed): @@ -79,38 +79,9 @@ class Scenery: def read(self, path, offset=(0.0, 0.0, 0.0)): path = path.resolve() - if path in self.seen: - return - self.seen.add(path) - text = path.read_bytes().decode("latin-1") - self.texts[path] = text - matches = [m for m in TOKEN.finditer(text) if not m.group(0).startswith("//")] - words = [m.group(0) for m in matches] - lower = [w.lower() for w in words] - origins = [offset] - i = 0 - while i < len(words): - word = lower[i] - if word == "include" and i + 1 < len(words): - end = i + 1 - while end < len(words) and lower[end] != "end": - end += 1 - self.include(words[i + 1], words[i + 2:end], origins[-1]) - i = end - elif word == "origin" and i + 3 < len(words) and all(NUMBER.match(w) for w in words[i + 1:i + 4]): - origins.append(tuple(o + float(w) for o, w in zip(origins[-1], words[i + 1:i + 4]))) - i += 3 - elif word == "endorigin" and len(origins) > 1: - origins.pop() - elif word == "rotate" and i + 3 < len(words) and any(w.strip("0.-") for w in words[i + 1:i + 4]): - self.notes["rotate"] += 1 - elif word == "node" and i + 4 < len(words): - kind = lower[i + 4] - if kind == "track": - i = self.track(path, matches, words, lower, i, origins[-1]) - elif kind == "model" and i + 9 < len(words) and GRASS.search(lower[i + 9]) and all(NUMBER.match(w) for w in words[i + 5:i + 8]): - self.plant(float(words[i + 5]) + origins[-1][0], float(words[i + 7]) + origins[-1][2]) - i += 1 + if path not in self.seen: + self.seen.add(path) + FileReader(self, path, offset).run() def include(self, name, params, origin): path = self.root / name.replace("\\", "/") @@ -118,21 +89,90 @@ class Scenery: numbers = [float(w) for w in params if NUMBER.match(w)] if GRASS.search(name.lower()) and len(numbers) >= 3: self.plant(numbers[0] + origin[0], numbers[2] + origin[2]) - return - if not path.is_file(): + elif not path.is_file(): self.notes["brak pliku " + name] += 1 - return - if params: + elif not params: + self.read(path, origin) + elif path.stat().st_size < 4000000 and re.search(rb"\btrack\s+(normal|switch)\b", path.read_bytes(), re.I): # the same file can come many times with other parameters: its tracks can't be told apart in one copy - if path.stat().st_size < 4000000 and re.search(rb"\btrack\s+(normal|switch)\b", path.read_bytes(), re.I): - self.notes["pliki z parametrami, tory pominiete"] += 1 - return - self.read(path, origin) + self.notes["pliki z parametrami, tory pominiete"] += 1 def plant(self, x, z): self.plants[(round(x / 10), round(z / 10))].append((x, z)) - def track(self, path, matches, words, lower, i, origin): + +class FileReader: + """Goes through the words of one file. Each handler takes the place of its word and gives the place of the last word it used.""" + + def __init__(self, scenery, path, offset): + self.scenery, self.path = scenery, path + text = path.read_bytes().decode("latin-1") + scenery.texts[path] = text + self.matches = [m for m in TOKEN.finditer(text) if not m.group(0).startswith("//")] + self.words = [m.group(0) for m in self.matches] + self.lower = [w.lower() for w in self.words] + self.origins = [offset] + self.handlers = {"include": self.include, "origin": self.origin, "endorigin": self.endorigin, "rotate": self.rotate, "node": self.node} + + def run(self): + i = 0 + while i < len(self.words): + handler = self.handlers.get(self.lower[i]) + i = (handler(i) if handler else i) + 1 + + def include(self, i): + if i + 1 >= len(self.words): + return i + end = i + 1 + while end < len(self.words) and self.lower[end] != "end": + end += 1 + self.scenery.include(self.words[i + 1], self.words[i + 2:end], self.origins[-1]) + return end + + def origin(self, i): + values = self.words[i + 1:i + 4] + if len(values) < 3 or not all(NUMBER.match(w) for w in values): + return i + self.origins.append(tuple(o + float(w) for o, w in zip(self.origins[-1], values))) + return i + 3 + + def endorigin(self, i): + if len(self.origins) > 1: + self.origins.pop() + return i + + def rotate(self, i): + if i + 3 < len(self.words) and any(w.strip("0.-") for w in self.words[i + 1:i + 4]): + self.scenery.notes["rotate"] += 1 + return i + + def node(self, i): + if i + 4 >= len(self.words): + return i + kind = self.lower[i + 4] + if kind == "track": + return self.track(i) + if kind == "model" and i + 9 < len(self.words) and GRASS.search(self.lower[i + 9]) and all(NUMBER.match(w) for w in self.words[i + 5:i + 8]): + self.scenery.plant(float(self.words[i + 5]) + self.origins[-1][0], float(self.words[i + 7]) + self.origins[-1][2]) + return i + + def paths_from(self, cursor, count): + paths = [] + origin = self.origins[-1] + for _ in range(count): + values = self.words[cursor:cursor + 15] + if len(values) < 15 or not all(NUMBER.match(v) for v in values): + return None + n = [float(v) for v in values] + part = {"p1": [n[j] + origin[j] for j in range(3)], "c1": n[4:7], "c2": n[7:10], "p2": [n[10 + j] + origin[j] for j in range(3)], "radius": abs(n[14])} + part["line"] = line_of(part) + part["length"] = sum(math.dist(a, b) for a, b in zip(part["line"], part["line"][1:])) + paths.append(part) + cursor += 15 + return paths + + def track(self, i): + words, lower = self.words, self.lower at = i + 4 end = at while end < len(words) and lower[end] != "endtrack": @@ -142,33 +182,30 @@ class Scenery: kind = lower[at + 1] if kind not in ("normal", "switch", "road"): return end - cursor = at + 10 - rail = ballast = None - if lower[at + 9] == "vis": - rail, ballast = lower[cursor], lower[cursor + 2] - cursor += 6 - paths = [] - for _ in range(2 if kind == "switch" else 1): - values = words[cursor:cursor + 15] - if len(values) < 15 or not all(NUMBER.match(v) for v in values): - self.notes["tory z parametrami, pominiete"] += 1 - return end - n = [float(v) for v in values] - part = dict(p1=[n[j] + origin[j] for j in range(3)], c1=n[4:7], c2=n[7:10], p2=[n[10 + j] + origin[j] for j in range(3)], radius=abs(n[14])) - part["line"] = line_of(part) - part["length"] = sum(math.dist(a, b) for a, b in zip(part["line"], part["line"][1:])) - paths.append(part) - cursor += 15 - if kind == "road": - self.roads.append(paths[0]) - return end - block = lower[at:end] - taken = any(w in block for w in ("uneven", "unevenup", "unevenside", "unevencross", "jointdip")) - velocity = float(words[at + block.index("velocity") + 1]) if "velocity" in block and NUMBER.match(words[at + block.index("velocity") + 1]) else 0.0 - sound = float(words[at + 5]) if NUMBER.match(words[at + 5]) else -1.0 - self.tracks.append(dict(name=words[i + 3], kind=kind, rail=rail, ballast=ballast, sound=sound, paths=paths, speed=velocity if velocity > 0 else None, source="wpis" if velocity > 0 else None, file=path, offset=matches[end].start(), taken=taken)) + visible = lower[at + 9] == "vis" + paths = self.paths_from(at + (16 if visible else 10), 2 if kind == "switch" else 1) + if paths is None: + self.scenery.notes["tory z parametrami, pominiete"] += 1 + elif kind == "road": + self.scenery.roads.append(paths[0]) + else: + self.add_track(i, at, end, kind, visible, paths) return end + def add_track(self, i, at, end, kind, visible, paths): + words = self.words + block = self.lower[at:end] + velocity = 0.0 + if "velocity" in block and NUMBER.match(words[at + block.index("velocity") + 1]): + velocity = float(words[at + block.index("velocity") + 1]) + self.scenery.tracks.append({ + "name": words[i + 3], "kind": kind, + "rail": self.lower[at + 10] if visible else None, "ballast": self.lower[at + 12] if visible else None, + "sound": float(words[at + 5]) if NUMBER.match(words[at + 5]) else -1.0, + "paths": paths, "speed": velocity if velocity > 0 else None, "source": "wpis" if velocity > 0 else None, + "file": self.path, "offset": self.matches[end].start(), + "taken": any(w in block for w in ("uneven", "unevenup", "unevenside", "unevencross", "jointdip"))}) + def bezier(part, t): a = [part["p1"][i] + part["c1"][i] for i in range(3)] @@ -220,50 +257,67 @@ class Joints: return found +def plain_neighbours(t, joints): + return [other for key in ("p1", "p2") for other, _, _ in joints.at(t["paths"][0][key]) if other is not t and other["kind"] == "normal"] + + +def plain_past_switches(t, point, joints): + # the plain tracks reached from a point of a track along the straight paths of switches + points, seen, found = [point], set(), [] + for _ in range(6): + onward = [] + for reached in points: + for other, part, side in joints.at(reached): + if other is t or id(part) in seen: + continue + seen.add(id(part)) + if other["kind"] != "switch": + found.append(other) + elif other["paths"].index(part) != other["bent"]: + onward.append(part["p2" if side == "p1" else "p1"]) + points = onward + return found + + +def short_pieces_past_switches(t, joints): + return [o for key in ("p1", "p2") for o in plain_past_switches(t, t["paths"][0][key], joints) if o["paths"][0]["length"] < PIECE] + + +def spread_speed(tracks, joints, reached): + heads = collections.deque(t for t in tracks if t["kind"] == "normal" and t["speed"]) + while heads: + t = heads.popleft() + for other in reached(t, joints): + if other["speed"] is None: + other["speed"], other["source"] = t["speed"], "sasiad" + heads.append(other) + + def settle_speeds(tracks, joints): # from the nearest track on the stretch between switches, a short piece also along the straight path of a switch - heads = collections.deque(t for t in tracks if t["kind"] == "normal" and t["speed"]) - while heads: - t = heads.popleft() - for key in ("p1", "p2"): - for other, _, _ in joints.at(t["paths"][0][key]): - if other is not t and other["kind"] == "normal" and other["speed"] is None: - other["speed"], other["source"] = t["speed"], "sasiad" - heads.append(other) - heads = collections.deque(t for t in tracks if t["kind"] == "normal" and t["speed"]) - while heads: - t = heads.popleft() - for key in ("p1", "p2"): - points, seen = [t["paths"][0][key]], set() - for _ in range(6): - onward = [] - for point in points: - for other, part, side in joints.at(point): - if other is t or id(part) in seen: - continue - seen.add(id(part)) - if other["kind"] == "switch": - if other["paths"].index(part) != other["bent"]: - onward.append(part["p2" if side == "p1" else "p1"]) - elif other["speed"] is None and other["paths"][0]["length"] < PIECE: - other["speed"], other["source"] = t["speed"], "sasiad" - heads.append(other) - points = onward + spread_speed(tracks, joints, plain_neighbours) + spread_speed(tracks, joints, short_pieces_past_switches) for t in tracks: if t["speed"] is None: t["speed"], t["source"] = NO_SPEED, "brak" +def plain_ends(tracks): + for t in tracks: + if t["kind"] == "normal": + for part in t["paths"]: + yield part["p1"] + yield part["p2"] + + def distances(tracks, joints, station): best, heap = {}, [] - for t in tracks: - for part in t["paths"]: - for key in ("p1", "p2"): - gap = math.dist((part[key][0], part[key][2]), station) - node = (round(part[key][0], 1), round(part[key][2], 1)) - if gap < 300 and t["kind"] == "normal" and gap < best.get(node, math.inf): - best[node] = gap - heapq.heappush(heap, (gap, node, part[key])) + for point in plain_ends(tracks): + gap = math.dist((point[0], point[2]), station) + node = (round(point[0], 1), round(point[2], 1)) + if gap < 300 and gap < best.get(node, math.inf): + best[node] = gap + heapq.heappush(heap, (gap, node, point)) while heap: distance, node, point = heapq.heappop(heap) if distance > best.get(node, math.inf): @@ -293,30 +347,34 @@ def grass_on(part, plants): return len(near) * 100.0 / max(part["length"], 5.0) +def cells_under(a, b): + for x in range(math.floor(min(a[0], b[0]) / 50), math.floor(max(a[0], b[0]) / 50) + 1): + for z in range(math.floor(min(a[2], b[2]) / 50), math.floor(max(a[2], b[2]) / 50) + 1): + yield x, z + + def road_cells(roads): cells = collections.defaultdict(list) for road in roads: for a, b in zip(road["line"], road["line"][1:]): - for x in range(math.floor(min(a[0], b[0]) / 50), math.floor(max(a[0], b[0]) / 50) + 1): - for z in range(math.floor(min(a[2], b[2]) / 50), math.floor(max(a[2], b[2]) / 50) + 1): - cells[(x, z)].append((a, b)) + for cell in cells_under(a, b): + cells[cell].append((a, b)) return cells +def cross_in_plan(a, b, c, d): + rx, rz, sx, sz = b[0] - a[0], b[2] - a[2], d[0] - c[0], d[2] - c[2] + k = rx * sz - rz * sx + if abs(k) < 1e-9: + return False + t = ((c[0] - a[0]) * sz - (c[2] - a[2]) * sx) / k + u = ((c[0] - a[0]) * rz - (c[2] - a[2]) * rx) / k + return 0 <= t <= 1 and 0 <= u <= 1 + + def hits_road(part, cells): - for a, b in zip(part["line"], part["line"][1:]): - for x in range(math.floor(min(a[0], b[0]) / 50), math.floor(max(a[0], b[0]) / 50) + 1): - for z in range(math.floor(min(a[2], b[2]) / 50), math.floor(max(a[2], b[2]) / 50) + 1): - for c, d in cells.get((x, z), ()): - rx, rz, sx, sz = b[0] - a[0], b[2] - a[2], d[0] - c[0], d[2] - c[2] - k = rx * sz - rz * sx - if abs(k) < 1e-9: - continue - t = ((c[0] - a[0]) * sz - (c[2] - a[2]) * sx) / k - u = ((c[0] - a[0]) * rz - (c[2] - a[2]) * rx) / k - if 0 <= t <= 1 and 0 <= u <= 1: - return True - return False + line = part["line"] + return any(cross_in_plan(a, b, c, d) for a, b in zip(line, line[1:]) for cell in cells_under(a, b) for c, d in cells.get(cell, ())) def heights_per_mm(t): @@ -423,17 +481,19 @@ def fit_under_pantograph(normal): return low +def joint_curve_changes(t, part, key, joints): + own = end_curve(part, key) + for other, theirs, side in joints.at(part[key]): + if other is not t and other["kind"] == "normal": + their = end_curve(theirs, side) + if abs(own - (-their if side == key else their)) > CURVE_STEP: + return True + return False + + def curve_changes(t, joints): part = t["paths"][0] - if abs(end_curve(part, "p1") - end_curve(part, "p2")) > CURVE_STEP: - return True - for key in ("p1", "p2"): - for other, theirs, side in joints.at(part[key]): - if other is not t and other["kind"] == "normal": - their = end_curve(theirs, side) - if abs(end_curve(part, key) - (-their if side == key else their)) > CURVE_STEP: - return True - return False + return abs(end_curve(part, "p1") - end_curve(part, "p2")) > CURVE_STEP or any(joint_curve_changes(t, part, key, joints) for key in ("p1", "p2")) def move_near_limit(normal, joints, share): @@ -458,24 +518,32 @@ def move_near_limit(normal, joints, share): return chosen +def value_from_plain(t, index, part, joints, product): + found = [o for key in ("p1", "p2") for o, theirs, _ in joints.at(part[key]) if o["kind"] == "normal" and theirs["length"] >= SHORT] + if not found: + return None + speed = max(o["speed"] for o in found) + if index == t["bent"]: + speed = min(speed, DIVERGING) + return min(limit(speed) * max(o["state"] for o in found), SWITCH_MOST, product / speed) + + +def value_from_switches(t, part, joints): + found = [] + for key in ("p1", "p2"): + for other, theirs, _ in joints.at(part[key]): + if other["kind"] == "switch" and other is not t: + found += [other["values"][i] for i, q in enumerate(other["paths"]) if q is theirs and other["values"][i] is not None] + return max(found) if found else None + + def grade_switches(switches, joints, product): # each path by the plain tracks it leads to, the worst of them; those only among switches from their neighbours for t in switches: - t["values"] = [None] * len(t["paths"]) - for index, part in enumerate(t["paths"]): - found = [o for key in ("p1", "p2") for o, theirs, _ in joints.at(part[key]) if o["kind"] == "normal" and theirs["length"] >= SHORT] - if found: - speed = max(o["speed"] for o in found) - if index == t["bent"]: - speed = min(speed, DIVERGING) - t["values"][index] = min(limit(speed) * max(o["state"] for o in found), SWITCH_MOST, product / speed) + t["values"] = [value_from_plain(t, index, part, joints, product) for index, part in enumerate(t["paths"])] for _ in range(5): for t in switches: - for index, part in enumerate(t["paths"]): - if t["values"][index] is None: - found = [o["values"][i] for key in ("p1", "p2") for o, theirs, _ in joints.at(part[key]) if o["kind"] == "switch" and o is not t for i, q in enumerate(o["paths"]) if q is theirs and o["values"][i] is not None] - if found: - t["values"][index] = max(found) + t["values"] = [value_from_switches(t, part, joints) if value is None else value for value, part in zip(t["values"], t["paths"])] for t in switches: t["values"] = [min(SWITCH_MOST, product / NO_SPEED) if v is None else v for v in t["values"]] From dbc173ebeaf0d25c839e0e3df8bbe8dce76ef775 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sat, 31 Oct 2026 20:17:47 +0100 Subject: [PATCH 15/90] docs(track): what of the cab shake goes off on an uneven track A table of all the sources of the shake, and what happens to each of them. --- track_uneven_doc.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/track_uneven_doc.md b/track_uneven_doc.md index c7446e34d..1c3163c50 100644 --- a/track_uneven_doc.md +++ b/track_uneven_doc.md @@ -69,7 +69,19 @@ The rails, the top of the ballast, `sleepermodel`, the axles, the roll of the bo Models from includes (e.g. switch drives and timbers), the terrain and the catenary don't follow it, so at large values a rail shifts away from such a model by a few centimetres. -On a track with unevenness the track itself shakes the cab. The sway calculated from the lateral acceleration, that is from the curve radius, and the random jolts at higher speed are off. On tracks with no unevenness they work as before. +On a track with unevenness the track itself shakes the cab, in place of two of the sources of shake that were there before. It goes by the track under the vehicle, and a track with only dips at its joints counts too. On tracks with no unevenness everything works as before. + +| source of the cab shake | on a track with unevenness | +|--------------------------------------------------------------------|----------------------------| +| side sway from the lateral acceleration: curve radius and cant, jolts sideways on a damaged switch | off | +| random jolts over 25 km/h | off | +| the track itself, from the bend under the axles | added | +| vertical jolts at the joints of a damaged track | stays | +| fore and aft, when accelerating and braking | stays | +| diesel engine vibration | stays | +| hunting (`huntingspring`), set by the vehicle | stays | + +This is about the cab only. The physics, derailment included, and the noise of the wheels still use the lateral acceleration. A pantograph rises at the rate set by the pressure, about 0.14 m/s, and a gap to the wire over 1 cm cuts the voltage and trips the main breaker. Unevenness adds to this at high speed. On the tracks of Bałtyk a simulation gives a gap of 0.6 cm when the vertical value times the speed in km/h is 925, for example 8 mm at 120 km/h, 13 mm at 70 km/h and 23 mm at 40 km/h. The values in the Id-14 table are higher than that. The wire alone can do it too: at `tor0290` on Bałtyk it rises by about 1%, so a pantograph keeps up to about 46 km/h there, and above that the breaker trips on a straight track as well. From d995674bee8b45465fd98489a1559bf4d515765a Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 1 Nov 2026 09:12:36 +0100 Subject: [PATCH 16/90] =?UTF-8?q?fix(physics):=20automatic=20couplers=20do?= =?UTF-8?q?n't=20catch=20above=20the=20safe=20collision=E2=80=A6=20(#217)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: automatic coupler crashes, horns, compressor start delay, radio and cab lever sounds fix(cab): the line contactors lamp goes out with the pressure switch (eu07/maszyna#470) The lamp of the open line contactors, which tells the start can be made, went out at 1 bar in the brake cylinders, while the pressure switch cuts the start off only at 2 bar (or below 3.6 bar in the main pipe), so with the brakes partly applied the start could be made with the lamp out. With a pressure switch fitted the lamp now follows it; without one it goes out at 1 bar as before. The same holds for the lamp of the second unit, which used to keep its last state between 1 bar and the cut-off. The condition is TMoverParameters::LineContactorsLampLit(). Tests: tests/unit/test_line_contactors.cpp. refactor: Sonar findings of PR 217 - horn_supply::pneumatic shadowed the pneumatic enumerator of the sound flags (cpp:S1117), it's horn_supply::air; the supplies read with using enum (S6177), - CouplerForce(): the automatic coupling refused or caught without another level of nested ifs (S134), the same outcome: too fast, the coupling is locked; caught, coupled and locked; otherwise left to try again, - CompressorCheck(): the power the compressor doesn't draw yet noted without a TODO (S1135), - tests: the previous crash damage setting in an in-class initializer (S3230), the loops which stop early with break rather than in their conditions (S886), the stand-ins of test::pair in a std::vector rather than an array (S5945), units in the comments of the example without /* */ (S1103). test: the cases the changes of the branch left without a test - Cntrl.: BDelay1 to BDelay4 reach the brake delays (now through std::format), the default start types of the ground and converter overload relays of a multiple unit and of other vehicles (out of the nested conditionals), - the compressor: fed from the low voltage it waits from the low voltage coming on; the pressure switch stops it above the cut-out pressure, without one the excess air is vented (the vented fraction rewritten), - the horns: a vehicle without any air sounds as before, the threshold pressure, an electric horn on 110 V, - the positions of a cab control below zero, and a control without position sounds. refactor(cab): the kind of the train heating switch as a flag Read once with the vehicle definition, rather than compared as text in each of the switch commands. feat(cab): train heating switch springing back (MaSzyna-EU07/maszyna#85) TrainHeating=[toggle]|impulse in the Switches: section of the vehicle definition makes the train heating switch an unstable one: it goes up to switch the heating on, down to switch it off, and back to the middle once let go, instead of staying where it was put. The position of such a switch isn't set from the heating state when the cab is entered. Tests: tests/unit/test_switches.cpp. feat(sound): mirror sounds (MaSzyna-EU07/maszyna#84, eu07/maszyna#322) The sound definitions of a vehicle (.mmd) take mirroropen: and mirrorclose:, played as a mirror sets off unfolding or folding, and the locations section mirrors: ... end, the offsets along the vehicle of the mirror pairs, as for the doors (negative ahead of the centre), each pair at the sides of the vehicle. As the animation, only the mirrors at the occupied cab sound. The start of a move is mirror_moves::motion in vehicle/mirrors.h. Tests: tests/unit/test_mirrors.cpp. feat(cab): joint oil and fuel pump switch (MaSzyna-EU07/maszyna#81) A cab control oilfuelpump_sw: switches both the oil and the fuel pump at once, with the new command oilfuelpumptoggle (no key by default). As a two-state switch it turns both pumps off when both are on and both on otherwise; as an impulse switch it's held to start them, like the separate pump switches. Its position follows the pumps when the cab is entered. refactor(editor): text fields filled without strncpy (MaSzyna-EU07/maszyna#112) The last three strncpy calls Sonar flags (cpp:S5816), in the road and track editors, copy the text into the ImGui field buffers with std::string::copy and terminate it right after; the text gets cut to the buffer as before. fix: automatic coupler crashes, horns, compressor start delay, radio and cab lever sounds - automatic couplers don't catch above the safe collision speed (#86): with crash damage on they couple at a closing speed of 15 km/h at most, a faster hit locks the coupling until the vehicles part and the collision runs its course. On the way: CollisionVelocity() read a head-on 10 + 10 km/h as 14.1 km/h (quaternions turn by half the angle), it's now taken from the touching ends; CollisionDetect() took the other vehicle's speed the wrong way round for vehicles facing each other. Head-on hits now derail sooner, at the intended limit, - a single horn lever can't sound both tones at once (eu07/maszyna#392), - a skid ends once the vehicle stops on locked wheels (eu07/maszyna#479), - electronic destination signs are blanked at the end of the timetable (eu07/maszyna#446), - horns need the air or the voltage to sound (eu07/maszyna#290, #76): HornType=[Pneumatic]|Electric and Horn3Type in Cntrl.; a pneumatic horn needs 0.2 MPa in the main reservoir (or the main pipe without one), an electric one the low voltage; the lever stays where it was put, - compressor start delay (#67): CompressorStartDelay [s] in Brake., counted from the compressor getting its power, after the engine starts say; none by default, - the channel and volume buttons of a radio without power do nothing (eu07/maszyna#429); the power check of the radio is TMoverParameters::RadioPowered(), - a lever moved smoothly sounds the positions it passes (#83). Sonar findings in the functions changed: nesting, nested conditionals, commented-out code, std::format, references to const. Unit tests: test::pair for two vehicles running into each other, test_couplers, test_horn, test_wheelslip, test_compressor, test_radio, test_gauge_positions. * fix: review findings of PR 217 - the pressures of the mover are in bar (MinCP=7.0 in the vehicle definitions), so the 0.2 the pneumatic horns needed was 0.2 bar; it's 2 bar now, and the tests take their pressures in bar too, - the lamps of the ground relay and of the motor overload relays follow the pressure switch as the line contactors one does, TMoverParameters::MainCircuitLampsEnabled(), so the lamps of one cab go out together, - the joint oil and fuel pump switch leaves out a pump started automatically, which ignores the switching: with one, it couldn't turn the other off, and showed off with both running (OilFuelPumpsEnabled()), - the kind of the train heating switch comes from the occupied vehicle on entering the cab too, as in the switch commands; in a multiple unit these are different vehicles, - CouplerForce() put the force taken back by the other vehicle on its far coupler when the vehicles faced each other (Couplers[1 - End] rather than the coupler it's coupled with), - the velocity difference along the coupler axis computed once, CouplerVelocityDifference(), for CouplerForce() and CollisionVelocity(), - the attachcoupler flag added without the SetFlag() call with 0, the index of the mirror moves noted as by side. Tests: the coupler force of vehicles facing each other, the joint switch with an automatic pump, the lamps of the main circuit, the horn and compressor pressures in bar. * fix: coupler force of vehicles facing each other taken back along the other vehicle's axis CForce runs along the axis of its own vehicle, so the force the other vehicle takes back on its coupler of the pair is turned around once more when the vehicles face each other (-CF * DirPatch()), as that vehicle computes it itself. Tests: the coupler forces of the pair facing each other and facing the same way. --- CMakeLists.txt | 1 + McZapkie/MOVER.h | 50 +++ McZapkie/Mover.cpp | 199 +++++++----- application/editormoderoad.cpp | 4 +- application/editormodetrack.cpp | 7 +- input/command.cpp | 2 + input/command.h | 1 + input/drivermouseinput.cpp | 3 + tests/unit/README.md | 6 +- tests/unit/test_cntrl.cpp | 39 +++ tests/unit/test_compressor.cpp | 115 +++++++ tests/unit/test_couplers.cpp | 458 ++++++++++++++++++++++++++++ tests/unit/test_gauge_positions.cpp | 58 ++++ tests/unit/test_horn.cpp | 121 ++++++++ tests/unit/test_line_contactors.cpp | 59 ++++ tests/unit/test_mirrors.cpp | 32 ++ tests/unit/test_pumps.cpp | 52 ++++ tests/unit/test_radio.cpp | 28 ++ tests/unit/test_switches.cpp | 16 + tests/unit/test_wheelslip.cpp | 62 ++++ tests/unit/vehicle.cpp | 104 ++++++- tests/unit/vehicle.h | 51 ++++ utilities/translation.cpp | 1 + vehicle/Driver.cpp | 27 +- vehicle/DynObj.cpp | 91 +++++- vehicle/DynObj.h | 16 +- vehicle/Gauge.cpp | 42 ++- vehicle/Train.cpp | 185 ++++++----- vehicle/Train.h | 7 +- vehicle/gaugepositions.h | 35 +++ vehicle/horn.cpp | 24 ++ vehicle/horn.h | 24 ++ vehicle/mirrors.h | 44 +++ vr/openvr_imp.cpp | 3 + 34 files changed, 1765 insertions(+), 202 deletions(-) create mode 100644 tests/unit/test_cntrl.cpp create mode 100644 tests/unit/test_compressor.cpp create mode 100644 tests/unit/test_couplers.cpp create mode 100644 tests/unit/test_gauge_positions.cpp create mode 100644 tests/unit/test_horn.cpp create mode 100644 tests/unit/test_line_contactors.cpp create mode 100644 tests/unit/test_mirrors.cpp create mode 100644 tests/unit/test_pumps.cpp create mode 100644 tests/unit/test_radio.cpp create mode 100644 tests/unit/test_switches.cpp create mode 100644 tests/unit/test_wheelslip.cpp create mode 100644 vehicle/gaugepositions.h create mode 100644 vehicle/horn.cpp create mode 100644 vehicle/horn.h create mode 100644 vehicle/mirrors.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 477f584e9..dfe58b66d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -462,6 +462,7 @@ set(CORE_SOURCES "utilities/utilities.cpp" "scripting/ladderlogic.cpp" "vehicle/handover.cpp" +"vehicle/horn.cpp" ) add_library(eu07_core STATIC ${CORE_SOURCES}) target_link_libraries(eu07_core PUBLIC eu07_settings) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 29b3e75cd..bb36694bb 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -676,6 +676,13 @@ struct TTractionParam }; /*powyzsze parametry zwiazane sa z torem po ktorym aktualnie pojazd jedzie*/ +// what a horn needs to sound: air from the main reservoir, or the low voltage +enum class horn_supply +{ + air, + electric +}; + /// High-level brake-system family. enum class TBrakeSystem { @@ -1710,6 +1717,10 @@ class TMoverParameters float DistanceCounterDoublePressPeriod = {1.f}; // czas w jakim nalezy podwojnie wcisnac przycisk, aby rozpoczac pomiar odleglosci TSecuritySystem SecuritySystem; int EmergencyBrakeWarningSignal{0}; // combined with basic WarningSignal when manual emergency brake is active + horn_supply HornSupply{horn_supply::air}; // of the low and high tone horns (WarningSignal 1 and 2) + horn_supply Horn3Supply{horn_supply::air}; // of the third horn (WarningSignal 4) + // main reservoir pressure [bar, as MinCP and MaxCP] below which a pneumatic horn stays silent + static constexpr double HornMinPressure{2.0}; TUniversalCtrlTable UniCtrlList; /*lista pozycji uniwersalnego nastawnika*/ int UniCtrlListSize = 0; /*wielkosc listy pozycji uniwersalnego nastawnika*/ bool UniCtrlIntegratedBrakePNCtrl = false; /*zintegrowany nastawnik JH obsluguje hamulec PN*/ @@ -2001,6 +2012,8 @@ class TMoverParameters bool CompressorAllow = false; /*! zezwolenie na uruchomienie sprezarki NBMX*/ bool CompressorAllowLocal{true}; // local device state override (most units don't have this fitted so it's set to true not to intefere) bool CompressorGovernorLock{false}; // indicates whether compressor pressure switch was activated due to reaching cut-out pressure + double CompressorStartDelay{0.0}; // time [s] the compressor waits after getting its power before it can start, after the engine starts say + double CompressorPowerTime{0.0}; // time [s] since the compressor got its power bool CompressorTankValve{false}; // indicates excessive pressure is vented from compressor tank directly and instantly start_t CompressorStart{start_t::manual}; // whether the compressor is started manually, or another way start_t PantographCompressorStart{start_t::manual}; @@ -2228,6 +2241,7 @@ class TMoverParameters // TODO: move these switch types where they belong, cabin definition std::string PantSwitchType; std::string ConvSwitchType; + bool HeatingSwitchImpulse{false}; // the train heating switch springs back to its middle position std::string StLinSwitchType; bool Heating = false; // ogrzewanie 'Winger 020304 @@ -2428,6 +2442,42 @@ class TMoverParameters double BrakeForce(const TTrackParam &Track); double CouplerForce(int const End, double dt); void CollisionDetect(int const End, double const dt); + // closing speed of a collision [km/h] above which the vehicles involved may derail, adjusted for their mass ratio + static constexpr double SafeCollisionVelocity{15.0}; + // closing speed [km/h] of the End of this vehicle and the OtherEnd of the other vehicle it touches, along the coupler + // axis: the sum of the speeds in a head-on collision, the difference in a rear-end one, negative when they part + double CollisionVelocity(int const End, TMoverParameters const &Other, int const OtherEnd) const; + // velocity [m/s] of this vehicle relative to the other vehicle it touches with its End and the OtherEnd of the + // other one, along this vehicle's axis + double CouplerVelocityDifference(int const End, TMoverParameters const &Other, int const OtherEnd) const; + // whether the automatic couplers at the End of this vehicle and the OtherEnd of the other one can catch each other on + // contact, rather than the vehicles colliding (with crash damage enabled, only at a closing speed which doesn't derail them) + bool AutomaticCouplingSafe(int const End, TMoverParameters const &Other, int const OtherEnd) const; + // the horns of the Signal (WarningSignal bits) which have the air or the voltage they need to sound + int SoundingHorns(int const Signal) const; + // whether the signal lamps of the main circuit (line contactors, ground relay, motor overload relays) can light: + // the pressure switch, when fitted, cuts the start off and puts them out, otherwise they go out once the brakes are applied + bool MainCircuitLampsEnabled() const + { + return false == ControlPressureSwitch && (HasControlPressureSwitch || BrakePress < 1.0); + } + // whether the lamp of the open line contactors is lit, telling the start can be made + bool LineContactorsLampLit() const + { + return false == StLinFlag && MainCircuitLampsEnabled(); + } + // whether the pumps a joint oil and fuel pump switch works, those not started automatically, are all switched on + bool OilFuelPumpsEnabled() const + { + auto const fuelmanual{FuelPump.start_type != start_t::automatic}; + auto const oilmanual{OilPump.start_type != start_t::automatic}; + return (fuelmanual || oilmanual) && (false == fuelmanual || FuelPump.is_enabled) && (false == oilmanual || OilPump.is_enabled); + } + // whether the radio is switched on and has the low voltage to work + bool RadioPowered() const + { + return Radio && (Power24vIsAvailable || Power110vIsAvailable); + } /*obrot kol uwzgledniajacy poslizg*/ double ComputeRotatingWheel(double WForce, double dt, double n) const; diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 3b5b62153..cb17e30ad 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -16,6 +16,8 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Logs.h" #include "utilities/parser.h" #include "simulation/simulation.h" + +#include //--------------------------------------------------------------------------- // Ra: tu należy przenosić funcje z mover.pas, które nie są z niego wywoływane. @@ -1176,6 +1178,56 @@ double TMoverParameters::EngineMaxRPM() const std::numeric_limits::max(); // shouldn't ever get here but, eh } +// ************************************************************************************************* +// prędkość zbliżania się stykających się końców pojazdów przy zderzeniu, w km/h +// ************************************************************************************************* +double TMoverParameters::CollisionVelocity(int const End, TMoverParameters const &Other, int const OtherEnd) const +{ + auto const velocitydifference{CouplerVelocityDifference(End, Other, OtherEnd)}; + // the front end closes in on the other vehicle when this one moves forward faster, the rear end when it's slower + return (End == end::front ? velocitydifference : -velocitydifference) * 3.6; // m/s -> km/h +} + +double TMoverParameters::CouplerVelocityDifference(int const End, TMoverParameters const &Other, int const OtherEnd) const +{ + // V runs along the vehicle's own axis, towards its front; the other vehicle's velocity is turned around when the + // vehicles face each other (touch with the same ends) + return V - Other.V * DirPatch(End, OtherEnd); +} + +bool TMoverParameters::AutomaticCouplingSafe(int const End, TMoverParameters const &Other, int const OtherEnd) const +{ + return false == Global.crash_damage || CollisionVelocity(End, Other, OtherEnd) <= SafeCollisionVelocity; +} + +int TMoverParameters::SoundingHorns(int const Signal) const +{ + auto const supplied{[this](horn_supply const Supply) { + if (Supply == horn_supply::electric) + { + return Power24vIsAvailable || Power110vIsAvailable; + } + if (VeselVolume > 0.0) + { + return Compressor >= HornMinPressure; + } + // a vehicle without a main reservoir of its own takes the air from the main pipe, a vehicle without either + // (a road vehicle, say) has no air to check + auto const mainpipe{((Couplers[end::front].AllowedFlag | Couplers[end::rear].AllowedFlag) & coupling::mainhose) != 0}; + return false == mainpipe || ScndPipePress >= HornMinPressure; + }}; + auto horns{0}; + if (supplied(HornSupply)) + { + horns |= Signal & (1 | 2); + } + if (supplied(Horn3Supply)) + { + horns |= Signal & 4; + } + return horns; +} + // ************************************************************************************************* // Q: 20160716 // Wykrywanie kolizji @@ -1192,8 +1244,11 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) auto const otherend{Neighbours[End].vehicle_end}; auto const &othercoupler{othervehicle->Couplers[otherend]}; + // the velocities along this vehicle's axis: the other vehicle's one is turned around when the vehicles face each + // other (touch with the same ends), as in CouplerForce() + auto const otherdirection{DirPatch(End, otherend)}; auto velocity{V}; - auto othervehiclevelocity{othervehicle->V}; + auto othervehiclevelocity{othervehicle->V * otherdirection}; // calculate collision force and new velocities for involved vehicles auto const VirtualCoupling{(thiscoupler.CouplingFlag == coupling::faux)}; auto CCF{0.0}; @@ -1224,28 +1279,25 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) damage_coupler(End); } - if (thiscoupler.CouplingFlag == coupling::faux || true == TestFlag(othervehicle->DamageFlag, dtrain_out)) - { // HACK: limit excessive speed derailment checks to vehicles which aren't part of the same consist - auto const safevelocitylimit{15.0}; - auto const velocitydifference{glm::length(glm::angleAxis(Rot.Rz, glm::dvec3{0, 1, 0}) * V - glm::angleAxis(othervehicle->Rot.Rz, glm::dvec3{0, 1, 0}) * othervehicle->V) * - 3.6}; // m/s -> km/h + // HACK: limit excessive speed derailment checks to vehicles which aren't part of the same consist + auto const outsideconsist{thiscoupler.CouplingFlag == coupling::faux || true == TestFlag(othervehicle->DamageFlag, dtrain_out)}; + auto const velocitydifference{outsideconsist ? CollisionVelocity(End, *othervehicle, otherend) : 0.0}; - if (velocitydifference > safevelocitylimit) + if (velocitydifference > SafeCollisionVelocity) + { + // HACK: crude estimation for potential derail, will take place with velocity difference > 15 km/h adjusted for vehicle mass ratio + if (false == TestFlag(DamageFlag, dtrain_out) || false == TestFlag(othervehicle->DamageFlag, dtrain_out)) { - // HACK: crude estimation for potential derail, will take place with velocity difference > 15 km/h adjusted for vehicle mass ratio - if (false == TestFlag(DamageFlag, dtrain_out) || false == TestFlag(othervehicle->DamageFlag, dtrain_out)) - { - WriteLog("Bad driving: " + Name + " and " + othervehicle->Name + " collided with velocity " + to_string(velocitydifference, 0) + " km/h"); - } + WriteLog("Bad driving: " + Name + " and " + othervehicle->Name + " collided with velocity " + to_string(velocitydifference, 0) + " km/h"); + } - if (velocitydifference > safevelocitylimit * (TotalMass / othervehicle->TotalMass)) - { - Derail(COLLISION); - } - if (velocitydifference > safevelocitylimit * (othervehicle->TotalMass / TotalMass)) - { - othervehicle->Derail(COLLISION); - } + if (velocitydifference > SafeCollisionVelocity * (TotalMass / othervehicle->TotalMass)) + { + Derail(COLLISION); + } + if (velocitydifference > SafeCollisionVelocity * (othervehicle->TotalMass / TotalMass)) + { + othervehicle->Derail(COLLISION); } } } @@ -1254,21 +1306,15 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) if (false == TestFlag(DamageFlag, dtrain_out)) { auto const accelerationchange{(velocity - V) / dt}; - // if( accelerationchange / AccS < 1.0 ) { - // HACK: prevent excessive vehicle pinball cases AccS += accelerationchange; - // AccS = std::clamp( AccS, -2.0, 2.0 ); V = velocity; - // } } if (false == TestFlag(othervehicle->DamageFlag, dtrain_out)) { - auto const othervehicleaccelerationchange{(othervehiclevelocity - othervehicle->V) / dt}; - // if( othervehicleaccelerationchange / othervehicle->AccS < 1.0 ) { - // HACK: prevent excessive vehicle pinball cases + auto const othervehiclenewvelocity{othervehiclevelocity * otherdirection}; // back along the other vehicle's axis + auto const othervehicleaccelerationchange{(othervehiclenewvelocity - othervehicle->V) / dt}; othervehicle->AccS += othervehicleaccelerationchange; - othervehicle->V = othervehiclevelocity; - // } + othervehicle->V = othervehiclenewvelocity; } } @@ -4332,7 +4378,10 @@ void TMoverParameters::CompressorCheck(double dt) } } - auto const compressorpower{(CompressorPower == 0 ? Mains : CompressorPower == 3 ? Mains : Power110vIsAvailable)}; + // the main circuit, also for a compressor driven by the engine, otherwise the low voltage + auto const compressorpower{CompressorPower == 0 || CompressorPower == 3 ? Mains : Power110vIsAvailable}; + CompressorPowerTime = compressorpower ? CompressorPowerTime + dt : 0.0; + auto const compressorready{compressorpower && CompressorPowerTime >= CompressorStartDelay}; // TBD: split CompressorAllow into separate enable/disable flags, inherit compressor from basic_device auto const compressorenable{CompressorAllowLocal && (CompressorStart == start_t::automatic || CompressorAllow)}; auto const compressordisable{false == compressorenable}; @@ -4354,7 +4403,7 @@ void TMoverParameters::CompressorCheck(double dt) auto const compressorflag{CompressorFlag}; CompressorFlag = - compressorpower && false == compressordisable && (false == governorlock || CompressorPower == 3) && (CompressorFlag || (compressorenable && LastSwitchingTime > CtrlDelay)); + compressorready && false == compressordisable && (false == governorlock || CompressorPower == 3) && (CompressorFlag || (compressorenable && LastSwitchingTime > CtrlDelay)); if (CompressorFlag && CompressorFlag != compressorflag) { @@ -4384,30 +4433,23 @@ void TMoverParameters::CompressorCheck(double dt) { // vent some air out if there's no governor lock to stop the compressor from exceeding acceptable pressure level SetFlag(SoundFlag, sound::relay | sound::loud); - CompressedVolume *= false == governorlockispresent ? 0.80 : // arbitrary amount - CompressorTankValve ? MinCompressorF / MaxCompressorF : // drop to mincompressor level - 0.999; // HACK: drop a tiny bit so the sound doesn't trigger repeatedly + auto ventedfraction{0.999}; // HACK: drop a tiny bit so the sound doesn't trigger repeatedly + if (false == governorlockispresent) + { + ventedfraction = 0.80; // arbitrary amount + } + else if (CompressorTankValve) + { + ventedfraction = MinCompressorF / MaxCompressorF; // drop to mincompressor level + } + CompressedVolume *= ventedfraction; if (false == governorlockispresent || CompressorTankValve) { CompressorGovernorLock = false; } } - // tymczasowo tylko obciążenie sprężarki, tak z 5A na sprężarkę - // TODO: draw power from proper high- or low voltage circuit - if (CompressorPower == 3) - { - // diesel-powered compressor doesn't draw power - } - else - { - // TODO: drain power from 110v circuit - /* - if( compressorowner != nullptr ) { - compressorowner->TotalCurrent += 0.0015 * compressorowner->PantographVoltage; - } - */ - } + // a compressor not driven by the engine doesn't draw power (some 5 A) from the high- or low voltage circuit yet } // ************************************************************************************************* @@ -5008,7 +5050,7 @@ void TMoverParameters::ComputeTotalForce(double dt) } else { - EngineVoltage *= DirAbsolute; // DirActive*CabActive; + EngineVoltage *= DirAbsolute; // the reverser of the active cab, relative to the couplers } } // bo nie dzialalo else @@ -5032,6 +5074,14 @@ void TMoverParameters::ComputeTotalForce(double dt) { SlippingWheels = true; } + if (true == SlippingWheels && Vel < 0.1 // the same crude trap: (nearly) stopped vehicle... + && std::abs(nrot * M_PI * WheelDiameter - V) < 0.1 / 3.6) // ...with wheels turning at its speed, i.e. locked wheels at the end of a skid + { + // locked wheels never pass the rotation speed test below once the vehicle stops, and a skidding vehicle + // keeps its braking force capped by the sliding adhesion, so the rest of the consist could push it around + SlippingWheels = false; + nrot = V / M_PI / WheelDiameter; + } double temp_nrot = nrot; if (true == SlippingWheels) { @@ -5286,7 +5336,7 @@ double TMoverParameters::CouplerForce(int const End, double dt) auto const newdistance{initialdistance + 10.0 * distancedelta}; - auto const dV{V - othervehicle->V * DirPatch(End, otherend)}; + auto const dV{CouplerVelocityDifference(End, *othervehicle, otherend)}; auto const absdV{std::abs(dV)}; // potentially generate sounds on clash or stretch @@ -5399,25 +5449,23 @@ double TMoverParameters::CouplerForce(int const End, double dt) couplingtype &= ~coupling::control; } - if (Attach(End, otherend, othervehicle, couplingtype)) - { - // HACK: we're reusing sound enum to mark whether vehicle was connected to another - SetFlag(AIFlag, sound::attachcoupler); - thiscoupler.AutomaticCouplingAllowed = false; - othercoupler.AutomaticCouplingAllowed = false; - } - /* - coupler.CouplingFlag = ( coupler.AutomaticCouplingFlag & othercoupler.AutomaticCouplingFlag ); - SetFlag( coupler.sounds, sound::attachcoupler ); - */ + // too fast for the couplers to catch, the vehicles collide instead (and may derail, see CollisionDetect()) + auto const safe{AutomaticCouplingSafe(End, *othervehicle, otherend)}; + auto const caught{safe && Attach(End, otherend, othervehicle, couplingtype)}; + // HACK: we're reusing sound enum to mark whether vehicle was connected to another + AIFlag |= (caught ? sound::attachcoupler : 0); + // coupled, or locked until the vehicles part, so they don't couple as soon as the collision slows them down + thiscoupler.AutomaticCouplingAllowed = safe && false == caught; + othercoupler.AutomaticCouplingAllowed = safe && false == caught; } } } if (thiscoupler.CouplingFlag != coupling::faux) { - // uzgadnianie prawa Newtona - othervehicle->Couplers[1 - End].CForce = -CF; + // uzgadnianie prawa Newtona, on the coupler of the other vehicle this one is coupled with, along that vehicle's + // axis: turned around once more when the vehicles face each other + othervehicle->Couplers[otherend].CForce = -CF * DirPatch(End, otherend); } return CF; @@ -10468,6 +10516,7 @@ void TMoverParameters::LoadFIZ_Brake(std::string const &line) auto lookup = compressorpowers.find(extract_value("CompressorPower", line)); CompressorPower = lookup != compressorpowers.end() ? lookup->second : 1; } + extract_value(CompressorStartDelay, "CompressorStartDelay", line, ""); if (true == extract_value(AirLeakRate, "AirLeakRate", line, "")) { @@ -10684,7 +10733,7 @@ void TMoverParameters::LoadFIZ_Cntrl(std::string const &line) for (int idx = 0; idx < 4; ++idx) { - extract_value(BrakeDelay[idx], "BDelay" + std::to_string(idx + 1), line, ""); + extract_value(BrakeDelay[idx], std::format("BDelay{}", idx + 1), line, ""); } // brakedelays, brakedelayflag { @@ -10780,6 +10829,16 @@ void TMoverParameters::LoadFIZ_Cntrl(std::string const &line) // maksymalna predkosc dostepna na tarczce predkosciomierza extract_value(maxTachoSpeed, "MaxTachoSpeed", line, ""); + // horns + { + using enum horn_supply; + std::map> const hornsupplies{{"Pneumatic", air}, {"Electric", electric}}; + auto lookup = hornsupplies.find(extract_value("HornType", line)); + HornSupply = lookup != hornsupplies.end() ? lookup->second : air; + lookup = hornsupplies.find(extract_value("Horn3Type", line)); + Horn3Supply = lookup != hornsupplies.end() ? lookup->second : HornSupply; + } + // dynamicbrake { std::map> dynamicbrakes{{"Passive", dbrake_passive}, {"Switch", dbrake_switch}, {"Reversal", dbrake_reversal}, {"Automatic", dbrake_automatic}}; @@ -10939,14 +10998,15 @@ void TMoverParameters::LoadFIZ_Cntrl(std::string const &line) // ground relay { auto lookup = starts.find(extract_value("GroundRelayStart", line)); - GroundRelayStart = lookup != starts.end() ? lookup->second : TrainType == dt_EZT ? start_t::automatic : start_t::manual; + auto const defaultstart{TrainType == dt_EZT ? start_t::automatic : start_t::manual}; + GroundRelayStart = lookup != starts.end() ? lookup->second : defaultstart; } // converter overload relay { auto lookup = starts.find(extract_value("ConverterOverloadRelayStart", line)); - ConverterOverloadRelayStart = lookup != starts.end() ? lookup->second : - TrainType == dt_EZT ? start_t::converter : // relay activates when converter is switched off - start_t::manual; + // the relay of a multiple unit activates when the converter is switched off + auto const defaultstart{TrainType == dt_EZT ? start_t::converter : start_t::manual}; + ConverterOverloadRelayStart = lookup != starts.end() ? lookup->second : defaultstart; } } @@ -11339,6 +11399,7 @@ void TMoverParameters::LoadFIZ_Switches(std::string const &Input) extract_value(PantSwitchType, "Pantograph", Input, ""); extract_value(ConvSwitchType, "Converter", Input, ""); extract_value(StLinSwitchType, "MotorConnectors", Input, ""); + HeatingSwitchImpulse = ToLower(extract_value("TrainHeating", Input)) == "impulse"; // because people can't make up their minds whether it's "impulse" or "Impulse"... PantSwitchType = ToLower(PantSwitchType); ConvSwitchType = ToLower(ConvSwitchType); diff --git a/application/editormoderoad.cpp b/application/editormoderoad.cpp index a07da795f..9b5dc64ac 100644 --- a/application/editormoderoad.cpp +++ b/application/editormoderoad.cpp @@ -120,8 +120,8 @@ bool is_curved(segment_data const &Path) void copy_text(char *Buffer, std::size_t const Size, std::string const &Text) { - std::strncpy(Buffer, Text.c_str(), Size - 1); - Buffer[Size - 1] = 0; + // as much of the text as fits, always terminated + Buffer[Text.copy(Buffer, Size - 1)] = 0; } std::string lower_case(std::string Text) diff --git a/application/editormodetrack.cpp b/application/editormodetrack.cpp index 0b264b784..8dfd831f9 100644 --- a/application/editormodetrack.cpp +++ b/application/editormodetrack.cpp @@ -1188,8 +1188,8 @@ void editor_mode::render_path_parameters(TTrack &Track) if (m_track_material_edited[i] != nullptr) continue; auto const name = materials[i] ? editor_track::material_name(*materials[i]) : std::string{}; - std::strncpy(m_track_materials[i].data(), name.c_str(), m_track_materials[i].size() - 1); - m_track_materials[i].back() = '\0'; + auto &buffer{m_track_materials[i]}; + buffer[name.copy(buffer.data(), buffer.size() - 1)] = '\0'; } } for (int i = 0; i < 3; ++i) @@ -1238,8 +1238,7 @@ void editor_mode::render_path_circuits(TTrack &Track) { auto *track{&Track}; auto const copy = [](std::array &Buffer, std::string const &Text) { - std::strncpy(Buffer.data(), Text.c_str(), Buffer.size() - 1); - Buffer.back() = '\0'; + Buffer[Text.copy(Buffer.data(), Buffer.size() - 1)] = '\0'; }; auto const isolated_text = [&]() { std::string text; diff --git a/input/command.cpp b/input/command.cpp index d7966f37d..0ae67cfa5 100644 --- a/input/command.cpp +++ b/input/command.cpp @@ -405,6 +405,7 @@ commanddescription_sequence Commands_descriptions = { {"wiperswitchincrease", command_target::vehicle, command_mode::oneoff}, {"wiperswitchdecrease", command_target::vehicle, command_mode::oneoff}, {"lightsset", command_target::vehicle, command_mode::oneoff}, + {"oilfuelpumptoggle", command_target::vehicle, command_mode::oneoff}, }; // Maps of command and coresponding strings @@ -789,6 +790,7 @@ std::unordered_map const commandMap = { {"wiperswitchincrease", user_command::wiperswitchincrease}, {"wiperswitchdecrease", user_command::wiperswitchdecrease}, {"lightsset", user_command::lightsset}, + {"oilfuelpumptoggle", user_command::oilfuelpumptoggle}, {"none", user_command::none}}; } // namespace simulation diff --git a/input/command.h b/input/command.h index e03622a9f..ddacef4a0 100644 --- a/input/command.h +++ b/input/command.h @@ -403,6 +403,7 @@ enum class user_command wiperswitchincrease, wiperswitchdecrease, lightsset, + oilfuelpumptoggle, none = -1 }; diff --git a/input/drivermouseinput.cpp b/input/drivermouseinput.cpp index ab3d0aaf8..22562b856 100644 --- a/input/drivermouseinput.cpp +++ b/input/drivermouseinput.cpp @@ -576,6 +576,9 @@ drivermouse_input::default_bindings() { { "oilpump_sw:", { oilpumptoggle, none } }, + { "oilfuelpump_sw:", { + oilfuelpumptoggle, + none } }, { "motorblowersfront_sw:", { motorblowerstogglefront, none } }, diff --git a/tests/unit/README.md b/tests/unit/README.md index bca46e83b..97cc94910 100644 --- a/tests/unit/README.md +++ b/tests/unit/README.md @@ -56,8 +56,10 @@ TEST_CASE("brakes: service braking stops a heavier wagon too") { functions on the vehicle state the class calls, and the test calls them the same way: see `vehicle/handover.h`, which `TController::TakeControl()` uses, and `test_handover.cpp`. - `vehicle` creates the vehicle the way `TDynamicObject::Init()` does and steps it the way the - simulation loop does, on a straight track of the given friction and gradient. Coupled vehicles - aren't supported yet. + simulation loop does, on a straight track of the given friction and gradient. `pair` puts two of + them on that track facing each other with the given ends and runs them together, as the + simulation runs the vehicles of a scene: the coupler and buffer forces, collisions and automatic + coupling come from the physics (`test_couplers.cpp`). Longer consists aren't supported yet. - Check behaviour (speeds, pressures, states the player sees) rather than internal flags, and make the test check its own preconditions (`REQUIRE` that the braking skidded, before checking what a skid leads to). diff --git a/tests/unit/test_cntrl.cpp b/tests/unit/test_cntrl.cpp new file mode 100644 index 000000000..0dd3ddc20 --- /dev/null +++ b/tests/unit/test_cntrl.cpp @@ -0,0 +1,39 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +using test::definition; +using test::vehicle; + +// the Cntrl. section of the vehicle definition, as read by TMoverParameters::LoadFIZ_Cntrl() + +TEST_CASE("cntrl: the brake delays of the settings come from BDelay1 to BDelay4") { + vehicle wagon{definition::load("wagon").set("Cntrl", "BDelay1", 10.0).set("Cntrl", "BDelay2", 20.0).set("Cntrl", "BDelay3", 30.0).set("Cntrl", "BDelay4", 40.0)}; + CHECK(wagon->BrakeDelay[0] == doctest::Approx(10.0)); + // the vehicle varies the others a little, by 0.83 to 0.9 (CheckLocomotiveParameters()) + for (auto setting{1}; setting < 4; ++setting) { + CAPTURE(setting); + auto const given{10.0 * (setting + 1)}; + CHECK(wagon->BrakeDelay[setting] >= 0.83 * given); + CHECK(wagon->BrakeDelay[setting] <= 0.9 * given); + } +} + +TEST_CASE("cntrl: the relays of a multiple unit start on their own, of other vehicles by hand") { + SUBCASE("multiple unit") { + vehicle unit{definition::load("wagon").set("Param", "Type", "ezt")}; + REQUIRE(unit->TrainType == dt_EZT); + CHECK(unit->GroundRelayStart == start_t::automatic); + CHECK(unit->ConverterOverloadRelayStart == start_t::converter); // activates when the converter is switched off + } + SUBCASE("other vehicle") { + vehicle wagon{definition::load("wagon")}; + CHECK(wagon->GroundRelayStart == start_t::manual); + CHECK(wagon->ConverterOverloadRelayStart == start_t::manual); + } + SUBCASE("given in the definition") { + vehicle unit{definition::load("wagon").set("Param", "Type", "ezt").set("Cntrl", "GroundRelayStart", "Manual").set("Cntrl", "ConverterOverloadRelayStart", "Automatic")}; + CHECK(unit->GroundRelayStart == start_t::manual); + CHECK(unit->ConverterOverloadRelayStart == start_t::automatic); + } +} diff --git a/tests/unit/test_compressor.cpp b/tests/unit/test_compressor.cpp new file mode 100644 index 000000000..c5aa2fff7 --- /dev/null +++ b/tests/unit/test_compressor.cpp @@ -0,0 +1,115 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +using test::definition; +using test::vehicle; + +// the compressor waiting a while after getting its power before it starts (CompressorStartDelay, +// MaSzyna-EU07/maszyna#67): in a diesel it starts some time after the engine, rather than with it + +namespace { + +double const step{0.01}; + +// a compressor which starts on its own, fed from the main circuit unless given another Power, its pressure +// switch between MinCP and MaxCP, and an empty main reservoir for it to fill +vehicle compressor_vehicle(double const Delay, std::string const &Power = "Main", double const MinCP = 6.5) { + auto definition{definition::load("wagon")}; + definition.set("Brake", "Vv", 1.0).set("Brake", "MinCP", MinCP).set("Brake", "MaxCP", 8.0); + definition.set("Brake", "CompressorSpeed", 0.01); + if (false == Power.empty()) { + definition.set("Brake", "CompressorPower", Power); + } + definition.set("Cntrl", "CompressorStart", "Automatic"); + if (Delay > 0.0) { + definition.set("Brake", "CompressorStartDelay", Delay); + } + vehicle vehicle{definition}; + vehicle->CompressedVolume = 0.0; + vehicle->LastSwitchingTime = 10.0; // nothing was switched lately + return vehicle; +} + +// runs the compressor for Seconds, the main circuit as it is, the time since the last switching going on as in +// ComputeTotalForce() +void run_compressor(vehicle &Vehicle, double const Seconds) { + for (auto i{std::lround(Seconds / step)}; i > 0; --i) { + Vehicle->LastSwitchingTime += step; + Vehicle->CompressorCheck(step); + } +} + +} // namespace + +TEST_CASE("compressor: without a start delay the compressor starts with its power") { + auto vehicle{compressor_vehicle(0.0)}; + run_compressor(vehicle, 1.0); + REQUIRE_FALSE(vehicle->CompressorFlag); // no power, no compressor + + vehicle->Mains = true; + run_compressor(vehicle, step); + CHECK(vehicle->CompressorFlag); +} + +TEST_CASE("compressor: the compressor starts the delay after getting its power") { + auto vehicle{compressor_vehicle(5.0)}; + vehicle->Mains = true; + + run_compressor(vehicle, 4.9); + CHECK_FALSE(vehicle->CompressorFlag); + CHECK(vehicle->Compressor == doctest::Approx(0.0)); + + run_compressor(vehicle, 0.2); + CHECK(vehicle->CompressorFlag); + run_compressor(vehicle, 10.0); + CHECK(vehicle->Compressor > 0.0); // and fills the reservoir +} + +TEST_CASE("compressor: the delay starts over once the power is gone") { + auto vehicle{compressor_vehicle(5.0)}; + vehicle->Mains = true; + run_compressor(vehicle, 10.0); + REQUIRE(vehicle->CompressorFlag); + + vehicle->Mains = false; // the engine stopped + run_compressor(vehicle, 1.0); + CHECK_FALSE(vehicle->CompressorFlag); + + vehicle->Mains = true; // and started again + run_compressor(vehicle, 4.9); + CHECK_FALSE(vehicle->CompressorFlag); + run_compressor(vehicle, 0.2); + CHECK(vehicle->CompressorFlag); +} + +TEST_CASE("compressor: a compressor fed from the low voltage waits from the low voltage coming on") { + auto vehicle{compressor_vehicle(5.0, "")}; // the default supply: the converter, the 110 V + vehicle->Mains = true; // which alone doesn't feed it + run_compressor(vehicle, 10.0); + REQUIRE_FALSE(vehicle->CompressorFlag); + + vehicle->Power110vIsAvailable = true; + run_compressor(vehicle, 4.9); + CHECK_FALSE(vehicle->CompressorFlag); + run_compressor(vehicle, 0.2); + CHECK(vehicle->CompressorFlag); +} + +TEST_CASE("compressor: the pressure switch stops the compressor above the cut-out pressure") { + auto vehicle{compressor_vehicle(0.0)}; + vehicle->Mains = true; + vehicle->CompressedVolume = 8.5; // over MaxCP, in the reservoir of 1 m3 + run_compressor(vehicle, step); + CHECK(vehicle->CompressorGovernorLock); + CHECK_FALSE(vehicle->CompressorFlag); +} + +TEST_CASE("compressor: without a pressure switch the air over the cut-out pressure is vented") { + auto vehicle{compressor_vehicle(0.0, "Main", 8.0)}; // MinCP the same as MaxCP: no pressure switch + vehicle->Mains = true; + vehicle->CompressedVolume = 8.5; + run_compressor(vehicle, step); + REQUIRE(vehicle->CompressorFlag); // nothing stops it + CHECK(vehicle->CompressedVolume < 8.5 * 0.81); // a fifth of the air let out +} diff --git a/tests/unit/test_couplers.cpp b/tests/unit/test_couplers.cpp new file mode 100644 index 000000000..8787a419b --- /dev/null +++ b/tests/unit/test_couplers.cpp @@ -0,0 +1,458 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" +#include "utilities/Globals.h" + +using test::definition; +using test::vehicle; + +namespace { + +// the wagon from data/wagon.json with automatic couplers at both ends, which couple the given connections +definition automatic_definition(int Flag = coupling::coupler | coupling::brakehose) { + return definition::load("wagon").set("BuffCoupl", "CType", "Automatic").set("BuffCoupl", "AutomaticFlag", Flag); +} + +// the wagon from data/wagon.json with automatic couplers at both ends, running at the given speed [km/h] +vehicle automatic_wagon(double Velocity, std::string const &Name) { + return vehicle{automatic_definition(), Velocity, Name}; +} + +// the same wagon, twice as heavy +vehicle heavy_automatic_wagon(double Velocity, std::string const &Name) { + return vehicle{automatic_definition().set("Param", "M", 2 * 42200), Velocity, Name}; +} + +bool derailed(vehicle &Vehicle) { + return TestFlag(Vehicle->DamageFlag, dtrain_out); +} + +// runs the pair for Seconds, returns whether the vehicles were coupled at any point +bool couple_during(test::pair &Pair, double Seconds) { + auto coupled{false}; + for (auto i{std::lround(Seconds / 0.01)}; i > 0; --i) { + Pair.step(); + coupled = coupled || Pair.coupled(); + } + return coupled; +} + +// sets the crash damage setting for the test, restores it after +class crash_damage_setting { +public: + explicit crash_damage_setting(bool Enabled) { Global.crash_damage = Enabled; } + ~crash_damage_setting() { Global.crash_damage = m_previous; } + crash_damage_setting(crash_damage_setting const &) = delete; + crash_damage_setting &operator=(crash_damage_setting const &) = delete; + +private: + bool m_previous{Global.crash_damage}; +}; + +} // namespace + +// issue #86: automatic couplers used to catch at any closing speed, so the vehicles never collided nor derailed + +TEST_CASE("couplers: automatic couplers catch a standing wagon hit at a walking pace") { + crash_damage_setting const crashdamage{true}; + auto moving{automatic_wagon(10.0, "moving")}; + auto standing{automatic_wagon(0.0, "standing")}; + REQUIRE(moving->Couplers[end::front].type() == TCouplerType::Automatic); + REQUIRE(moving.speed() == doctest::Approx(10.0)); + + // the front of the moving wagon runs into the rear of the standing one + CHECK(moving->CollisionVelocity(end::front, standing.mover(), end::rear) == doctest::Approx(10.0)); + CHECK(moving->AutomaticCouplingSafe(end::front, standing.mover(), end::rear)); + CHECK(standing->AutomaticCouplingSafe(end::rear, moving.mover(), end::front)); +} + +TEST_CASE("couplers: automatic couplers don't catch a wagon hit too fast, the wagons collide instead") { + crash_damage_setting const crashdamage{true}; + auto moving{automatic_wagon(30.0, "moving")}; + auto standing{automatic_wagon(0.0, "standing")}; + REQUIRE(moving.speed() == doctest::Approx(30.0)); + + CHECK(moving->CollisionVelocity(end::front, standing.mover(), end::rear) > TMoverParameters::SafeCollisionVelocity); + CHECK_FALSE(moving->AutomaticCouplingSafe(end::front, standing.mover(), end::rear)); + CHECK_FALSE(standing->AutomaticCouplingSafe(end::rear, moving.mover(), end::front)); +} + +TEST_CASE("couplers: automatic couplers catch a wagon running ahead only a little slower") { + crash_damage_setting const crashdamage{true}; + auto faster{automatic_wagon(60.0, "faster")}; + auto slower{automatic_wagon(50.0, "slower")}; + + CHECK(faster->CollisionVelocity(end::front, slower.mover(), end::rear) == doctest::Approx(10.0)); + CHECK(slower->CollisionVelocity(end::rear, faster.mover(), end::front) == doctest::Approx(10.0)); + CHECK(faster->AutomaticCouplingSafe(end::front, slower.mover(), end::rear)); +} + +TEST_CASE("couplers: without crash damage automatic couplers catch at any speed") { + crash_damage_setting const crashdamage{false}; + auto moving{automatic_wagon(30.0, "moving")}; + auto standing{automatic_wagon(0.0, "standing")}; + + CHECK(moving->AutomaticCouplingSafe(end::front, standing.mover(), end::rear)); +} + +// the closing speed used to treat the speed of each vehicle as a vector turned by half its heading, so for vehicles +// facing each other it came out as the root of the sum of the squares of their speeds, rather than the sum + +TEST_CASE("couplers: the closing speed of a rear-end collision is the difference of the speeds") { + auto faster{automatic_wagon(40.0, "faster")}; + auto slower{automatic_wagon(25.0, "slower")}; + + // running forwards, the front of the faster wagon hits the rear of the slower one + CHECK(faster->CollisionVelocity(end::front, slower.mover(), end::rear) == doctest::Approx(15.0)); + CHECK(slower->CollisionVelocity(end::rear, faster.mover(), end::front) == doctest::Approx(15.0)); + // the wagons running ahead of each other are parting + CHECK(slower->CollisionVelocity(end::front, faster.mover(), end::rear) == doctest::Approx(-15.0)); + + // backing up, the rear of the faster wagon hits the front of the slower one + auto backingfaster{automatic_wagon(-40.0, "backing faster")}; + auto backingslower{automatic_wagon(-25.0, "backing slower")}; + CHECK(backingfaster->CollisionVelocity(end::rear, backingslower.mover(), end::front) == doctest::Approx(15.0)); + CHECK(backingslower->CollisionVelocity(end::front, backingfaster.mover(), end::rear) == doctest::Approx(15.0)); +} + +TEST_CASE("couplers: the closing speed of a head-on collision is the sum of the speeds") { + crash_damage_setting const crashdamage{true}; + auto first{automatic_wagon(10.0, "first")}; + auto second{automatic_wagon(10.0, "second")}; + + // both wagons run forwards, their fronts meet + CHECK(first->CollisionVelocity(end::front, second.mover(), end::front) == doctest::Approx(20.0)); + CHECK(second->CollisionVelocity(end::front, first.mover(), end::front) == doctest::Approx(20.0)); + // 20 km/h is above the limit, the couplers don't catch (the old formula gave 14.1 km/h and coupled them) + CHECK(first->CollisionVelocity(end::front, second.mover(), end::front) > TMoverParameters::SafeCollisionVelocity); + CHECK_FALSE(first->AutomaticCouplingSafe(end::front, second.mover(), end::front)); + CHECK_FALSE(second->AutomaticCouplingSafe(end::front, first.mover(), end::front)); + + // both wagons back up, their rears meet + auto firstbacking{automatic_wagon(-10.0, "first backing")}; + auto secondbacking{automatic_wagon(-10.0, "second backing")}; + CHECK(firstbacking->CollisionVelocity(end::rear, secondbacking.mover(), end::rear) == doctest::Approx(20.0)); + + // running back to back, the wagons part + CHECK(first->CollisionVelocity(end::rear, second.mover(), end::rear) == doctest::Approx(-20.0)); + CHECK(first->AutomaticCouplingSafe(end::rear, second.mover(), end::rear)); + + // slow enough, the couplers catch + auto slowfirst{automatic_wagon(5.0, "slow first")}; + auto slowsecond{automatic_wagon(5.0, "slow second")}; + CHECK(slowfirst->CollisionVelocity(end::front, slowsecond.mover(), end::front) == doctest::Approx(10.0)); + CHECK(slowfirst->AutomaticCouplingSafe(end::front, slowsecond.mover(), end::front)); +} + +TEST_CASE("couplers: the closing speed of a hit on a standing vehicle is the speed of the moving one, whichever way it faces") { + auto moving{automatic_wagon(20.0, "moving")}; + auto standing{automatic_wagon(0.0, "standing")}; + + // the standing wagon faces away from the moving one + CHECK(moving->CollisionVelocity(end::front, standing.mover(), end::rear) == doctest::Approx(20.0)); + CHECK(standing->CollisionVelocity(end::rear, moving.mover(), end::front) == doctest::Approx(20.0)); + // the standing wagon faces the moving one + CHECK(moving->CollisionVelocity(end::front, standing.mover(), end::front) == doctest::Approx(20.0)); + CHECK(standing->CollisionVelocity(end::front, moving.mover(), end::front) == doctest::Approx(20.0)); + // the moving wagon backs up into the standing one + auto backing{automatic_wagon(-20.0, "backing")}; + CHECK(backing->CollisionVelocity(end::rear, standing.mover(), end::front) == doctest::Approx(20.0)); + CHECK(backing->CollisionVelocity(end::rear, standing.mover(), end::rear) == doctest::Approx(20.0)); + // the moving wagon runs away from the standing one + CHECK(moving->CollisionVelocity(end::rear, standing.mover(), end::front) == doctest::Approx(-20.0)); +} + +// the coupling as the simulation runs it: two vehicles on a track, closing in on each other until the couplers (or +// buffers) meet, the coupler forces and the collision computed by the physics, step by step + +TEST_CASE("couplers: automatic couplers catch right up to the safe closing speed") { + crash_damage_setting const crashdamage{true}; + auto standing{automatic_wagon(0.0, "standing")}; + + auto below{automatic_wagon(14.9, "below")}; + CHECK(below->CollisionVelocity(end::front, standing.mover(), end::rear) == doctest::Approx(14.9)); + CHECK(below->AutomaticCouplingSafe(end::front, standing.mover(), end::rear)); + + auto above{automatic_wagon(15.1, "above")}; + CHECK(above->CollisionVelocity(end::front, standing.mover(), end::rear) == doctest::Approx(15.1)); + CHECK_FALSE(above->AutomaticCouplingSafe(end::front, standing.mover(), end::rear)); + auto faster{automatic_wagon(15.5, "faster")}; + CHECK_FALSE(faster->AutomaticCouplingSafe(end::front, standing.mover(), end::rear)); +} + +TEST_CASE("couplers: a wagon rolling slowly into a standing one couples with it") { + crash_damage_setting const crashdamage{true}; + auto moving{automatic_wagon(5.0, "moving")}; + auto standing{automatic_wagon(0.0, "standing")}; + test::pair pair{moving, end::front, standing, end::rear, 1.0}; + + pair.run(3.0); + + REQUIRE(pair.coupled()); + CHECK(moving->Couplers[end::front].CouplingFlag == (coupling::coupler | coupling::brakehose)); + CHECK(standing->Couplers[end::rear].CouplingFlag == (coupling::coupler | coupling::brakehose)); + CHECK(moving->Couplers[end::front].Connected == &standing.mover()); + CHECK_FALSE(derailed(moving)); + CHECK_FALSE(derailed(standing)); + // coupled, they run on together + pair.run(2.0); + CHECK(moving.speed() == doctest::Approx(standing.speed()).epsilon(0.05)); + CHECK(pair.gap() < 0.2); +} + +TEST_CASE("couplers: a wagon hitting a standing one too fast doesn't couple, nor once the hit has slowed it down") { + crash_damage_setting const crashdamage{true}; + auto moving{automatic_wagon(30.0, "moving")}; + auto standing{automatic_wagon(0.0, "standing")}; + test::pair pair{moving, end::front, standing, end::rear, 1.0}; + + auto touched{false}; + auto parted{false}; + auto coupled{false}; + auto locked{true}; + for (int i = 0; i < 1000; ++i) { + pair.step(); + // as far as the couplers know: the distance between them at the start of the step + auto const distance{moving->Neighbours[end::front].distance}; + touched = touched || distance < 0.0; + parted = parted || (touched && distance > 0.05); + coupled = coupled || pair.coupled(); + if (touched && false == parted) { + locked = locked && false == moving->Couplers[end::front].AutomaticCouplingAllowed && false == standing->Couplers[end::rear].AutomaticCouplingAllowed; + } + } + + REQUIRE(touched); + CHECK_FALSE(coupled); + // the hit derailed and stopped them, the couplers next to each other, the automatic coupling locked all along + CHECK_FALSE(parted); + CHECK(locked); + CHECK(derailed(moving)); + CHECK(derailed(standing)); + CHECK(std::abs(moving.speed()) < 3.0); + CHECK(std::abs(standing.speed()) < 3.0); + CHECK_FALSE(couple_during(pair, 5.0)); +} + +TEST_CASE("couplers: the automatic coupling locked by a hit is unlocked once the vehicles part") { + crash_damage_setting const crashdamage{true}; + auto moving{automatic_wagon(3.0, "moving")}; + auto standing{automatic_wagon(0.0, "standing")}; + // as a hit too fast or manual uncoupling leaves them + moving->Couplers[end::front].AutomaticCouplingAllowed = false; + standing->Couplers[end::rear].AutomaticCouplingAllowed = false; + + SUBCASE("parted by more than 5 cm, they couple on the next approach") { + test::pair pair{moving, end::front, standing, end::rear, 0.5}; + pair.step(); + CHECK(moving->Couplers[end::front].AutomaticCouplingAllowed); + CHECK(couple_during(pair, 3.0)); + } + SUBCASE("kept together, they don't") { + test::pair pair{moving, end::front, standing, end::rear, 0.0}; + auto coupled{false}; + for (int i = 0; i < 300; ++i) { + pair.step(); + if (pair.gap() > 0.05) { + break; // parted + } + coupled = coupled || pair.coupled(); + } + CHECK_FALSE(coupled); + } +} + +// the couplers refuse a hit by the closing speed when they meet, the derail check (CollisionDetect()) takes the one +// left when the buffers are pressed in all the way, after their springs have slowed the hit down: for the wagons here +// by about 7 km/h. so a hit a little over the safe closing speed only bounces the vehicles apart + +TEST_CASE("couplers: a hit a little too fast for the couplers bounces the vehicles apart, uncoupled and on the rails") { + crash_damage_setting const crashdamage{true}; + auto moving{automatic_wagon(20.0, "moving")}; + auto standing{automatic_wagon(0.0, "standing")}; + test::pair pair{moving, end::front, standing, end::rear, 1.0}; + + CHECK_FALSE(couple_during(pair, 3.0)); + CHECK_FALSE(derailed(moving)); + CHECK_FALSE(derailed(standing)); + CHECK(pair.gap() > 0.5); + CHECK(standing.speed() > moving.speed()); +} + +TEST_CASE("couplers: a hit too fast for the couplers derails the vehicles over the limit adjusted for their mass ratio") { + crash_damage_setting const crashdamage{true}; + + SUBCASE("equal masses: both derail") { + auto moving{automatic_wagon(30.0, "moving")}; + auto standing{automatic_wagon(0.0, "standing")}; + test::pair pair{moving, end::front, standing, end::rear, 1.0}; + CHECK_FALSE(couple_during(pair, 5.0)); + CHECK(derailed(moving)); + CHECK(derailed(standing)); + } + SUBCASE("half the mass: between the safe closing speed and twice that, only the lighter one derails") { + auto light{automatic_wagon(27.0, "light")}; + auto heavy{heavy_automatic_wagon(0.0, "heavy")}; + test::pair pair{light, end::front, heavy, end::rear, 1.0}; + CHECK_FALSE(couple_during(pair, 5.0)); + CHECK(derailed(light)); + CHECK_FALSE(derailed(heavy)); + } + SUBCASE("half the mass: above twice the safe closing speed both derail") { + auto light{automatic_wagon(45.0, "light")}; + auto heavy{heavy_automatic_wagon(0.0, "heavy")}; + test::pair pair{light, end::front, heavy, end::rear, 1.0}; + CHECK_FALSE(couple_during(pair, 5.0)); + CHECK(derailed(light)); + CHECK(derailed(heavy)); + } + SUBCASE("half the mass: below the safe closing speed the couplers catch, nothing derails") { + auto light{automatic_wagon(12.0, "light")}; + auto heavy{heavy_automatic_wagon(0.0, "heavy")}; + test::pair pair{light, end::front, heavy, end::rear, 1.0}; + CHECK(couple_during(pair, 5.0)); + CHECK_FALSE(derailed(light)); + CHECK_FALSE(derailed(heavy)); + } +} + +TEST_CASE("couplers: wagons running head-on into each other couple only up to the safe closing speed") { + crash_damage_setting const crashdamage{true}; + + SUBCASE("7 + 7 km/h, the couplers catch") { + auto first{automatic_wagon(7.0, "first")}; + auto second{automatic_wagon(7.0, "second")}; + test::pair pair{first, end::front, second, end::front, 1.0}; + CHECK(couple_during(pair, 3.0)); + CHECK_FALSE(derailed(first)); + CHECK_FALSE(derailed(second)); + } + SUBCASE("8 + 8 km/h, the wagons bounce apart") { + auto first{automatic_wagon(8.0, "first")}; + auto second{automatic_wagon(8.0, "second")}; + test::pair pair{first, end::front, second, end::front, 1.0}; + CHECK_FALSE(couple_during(pair, 3.0)); + CHECK_FALSE(derailed(first)); + CHECK_FALSE(derailed(second)); + // both backwards, away from each other + CHECK(first.speed() < 0.0); + CHECK(second.speed() < 0.0); + } + SUBCASE("backing up, 8 + 8 km/h, the wagons bounce apart") { + auto first{automatic_wagon(-8.0, "first")}; + auto second{automatic_wagon(-8.0, "second")}; + test::pair pair{first, end::rear, second, end::rear, 1.0}; + CHECK_FALSE(couple_during(pair, 3.0)); + CHECK(first.speed() > 0.0); + CHECK(second.speed() > 0.0); + } + SUBCASE("15 + 15 km/h, both derail") { + auto first{automatic_wagon(15.0, "first")}; + auto second{automatic_wagon(15.0, "second")}; + test::pair pair{first, end::front, second, end::front, 1.0}; + CHECK_FALSE(couple_during(pair, 5.0)); + CHECK(derailed(first)); + CHECK(derailed(second)); + } +} + +// the collision used to take the speed of a vehicle facing the other way as if it faced the same way, so a wagon +// pushed away by the buffers of the vehicle hitting it head-on counted as running into it: the hit slowed it down, +// and the hitter bounced back as from a wall + +TEST_CASE("couplers: a wagon hit head-on runs off the same as one hit from behind") { + // without automatic couplers or crash damage, the collision alone + crash_damage_setting const crashdamage{false}; + auto speeds_after_hit = [](int StruckEnd) { + auto moving{vehicle{definition::load("wagon"), 20.0, "moving"}}; + auto struck{vehicle{definition::load("wagon"), 0.0, "struck"}}; + test::pair pair{moving, end::front, struck, StruckEnd, 0.5}; + auto touched{false}; + for (int i = 0; i < 300; ++i) { + pair.step(); + touched = touched || pair.gap() < 0.0; + if (touched && pair.gap() > 0.3) { + break; // bounced off + } + } + REQUIRE(touched); + REQUIRE(pair.gap() > 0.3); + REQUIRE_FALSE(pair.coupled()); + return std::make_pair(moving.speed(), struck.speed()); + }; + + auto const [hitterfrombehind, struckfrombehind]{speeds_after_hit(end::rear)}; + auto const [hitterheadon, struckheadon]{speeds_after_hit(end::front)}; + + // hit from behind, the struck wagon runs off forwards, faster than the hitter + CHECK(struckfrombehind > 10.0); + CHECK(hitterfrombehind < struckfrombehind); + // hit head-on, it runs off backwards, as fast + CHECK(struckheadon == doctest::Approx(-struckfrombehind).epsilon(0.02)); + CHECK(hitterheadon == doctest::Approx(hitterfrombehind).epsilon(0.05)); +} + +// the other conditions of automatic coupling + +TEST_CASE("couplers: an automatic coupler doesn't catch a coupler of another type") { + crash_damage_setting const crashdamage{true}; + auto moving{automatic_wagon(5.0, "moving")}; + auto standing{vehicle{definition::load("wagon"), 0.0, "standing"}}; + REQUIRE(standing->Couplers[end::rear].type() == TCouplerType::Screw); + test::pair pair{moving, end::front, standing, end::rear, 1.0}; + + CHECK_FALSE(couple_during(pair, 3.0)); +} + +TEST_CASE("couplers: automatic couplers of different control systems couple without the control connection") { + crash_damage_setting const crashdamage{true}; + auto const flag{coupling::coupler | coupling::brakehose | coupling::control}; + auto moving{vehicle{automatic_definition(flag).set("BuffCoupl", "ControlType", "first"), 5.0, "moving"}}; + + SUBCASE("the same control system") { + auto standing{vehicle{automatic_definition(flag).set("BuffCoupl", "ControlType", "first"), 0.0, "standing"}}; + REQUIRE(standing->Couplers[end::rear].control_type == "first"); + test::pair pair{moving, end::front, standing, end::rear, 1.0}; + REQUIRE(couple_during(pair, 3.0)); + CHECK(pair.coupling() == flag); + } + SUBCASE("a different one") { + auto standing{vehicle{automatic_definition(flag).set("BuffCoupl", "ControlType", "second"), 0.0, "standing"}}; + REQUIRE(standing->Couplers[end::rear].control_type == "second"); + test::pair pair{moving, end::front, standing, end::rear, 1.0}; + REQUIRE(couple_during(pair, 3.0)); + CHECK(pair.coupling() == (coupling::coupler | coupling::brakehose)); + } +} + +TEST_CASE("couplers: the coupler force of vehicles facing each other lands on the couplers holding them") { + // the force a vehicle takes from its coupler, the other one takes back on its own coupler of the pair, whichever + // way the vehicles face; it used to land on the far end of the other vehicle when they faced each other. + // CForce runs along the axis of its own vehicle, so the two forces of the pair are opposite along the track: of + // opposite signs for vehicles facing the same way, of the same sign for vehicles facing each other + auto first{automatic_wagon(5.0, "first")}; + auto second{automatic_wagon(0.0, "second")}; + + SUBCASE("facing each other") { + test::pair pair{first, end::front, second, end::front, 1.0}; + pair.run(5.0); + REQUIRE(pair.coupled()); + + pair.step(); + REQUIRE(second->Couplers[end::front].CForce != 0.0); + CHECK(first->Couplers[end::front].CForce == doctest::Approx(second->Couplers[end::front].CForce)); + CHECK(first->Couplers[end::rear].CForce == 0.0); // nothing there + CHECK(second->Couplers[end::rear].CForce == 0.0); + } + SUBCASE("facing the same way") { + test::pair pair{first, end::front, second, end::rear, 1.0}; + pair.run(5.0); + REQUIRE(pair.coupled()); + + pair.step(); + REQUIRE(second->Couplers[end::rear].CForce != 0.0); + CHECK(first->Couplers[end::front].CForce == doctest::Approx(-second->Couplers[end::rear].CForce)); + CHECK(first->Couplers[end::rear].CForce == 0.0); + CHECK(second->Couplers[end::front].CForce == 0.0); + } +} diff --git a/tests/unit/test_gauge_positions.cpp b/tests/unit/test_gauge_positions.cpp new file mode 100644 index 000000000..791234e9d --- /dev/null +++ b/tests/unit/test_gauge_positions.cpp @@ -0,0 +1,58 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle/gaugepositions.h" + +#include + +// the sounds of the positions of a cab control (MaSzyna-EU07/maszyna#83): a brake lever moved smoothly (Num3/Num9) +// passes its positions instead of stopping at them, and sounds each one it reaches, as it does when put there + +namespace { + +// a brake lever with sounds at positions 0, 2 and 5; values in hundredths of a position +std::map lever_sounds() { + return {{0, "release"}, {2, "running"}, {5, "service"}}; +} + +std::string reached(int const Previous, int const Desired) { + auto positions{lever_sounds()}; + auto const position{gauge_positions::last_reached(positions, Previous, Desired)}; + return position != positions.end() ? position->second : ""; +} + +} // namespace + +TEST_CASE("gauge positions: a lever put at a position sounds it") { + CHECK(reached(100, 200) == "running"); + CHECK(reached(600, 500) == "service"); +} + +TEST_CASE("gauge positions: a lever moved smoothly past a position sounds it (MaSzyna-EU07/maszyna#83)") { + CHECK(reached(190, 205) == "running"); // moving up + CHECK(reached(510, 495) == "service"); // moving down +} + +TEST_CASE("gauge positions: of the positions passed in one go the last one reached sounds") { + CHECK(reached(50, 520) == "service"); // up past 2 and 5 + CHECK(reached(520, 10) == "running"); // down past 5 and 2, short of 0 + CHECK(reached(520, 0) == "release"); +} + +TEST_CASE("gauge positions: moving between the positions or off one sounds nothing") { + CHECK(reached(210, 290) == ""); + CHECK(reached(200, 230) == ""); // leaving a position isn't reaching it + CHECK(reached(200, 170) == ""); +} + +TEST_CASE("gauge positions: positions below zero count the same way") { + // a brake lever with the release position at -1 + std::map positions{{-1, "release"}, {0, "running"}}; + CHECK(gauge_positions::last_reached(positions, 50, -120)->second == "release"); // down past 0 and -1 + CHECK(gauge_positions::last_reached(positions, -150, -90)->second == "release"); // up past -1 +} + +TEST_CASE("gauge positions: a control without position sounds reaches none") { + std::map positions; + CHECK(gauge_positions::last_reached(positions, 0, 500) == positions.end()); +} diff --git a/tests/unit/test_horn.cpp b/tests/unit/test_horn.cpp new file mode 100644 index 000000000..18e5c5322 --- /dev/null +++ b/tests/unit/test_horn.cpp @@ -0,0 +1,121 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle.h" + +#include "vehicle/horn.h" + +// the horn keys of the cab: low tone (A) and high tone (shift+A), held to sound + +TEST_CASE("horn: a single lever for both tones sounds one tone at a time (eu07/maszyna#392)") { + auto signal{0}; + signal = horn::pressed(signal, horn::low, true); + REQUIRE(signal == horn::low); + + // the lever moved over to the high tone while the low tone key is still held + signal = horn::pressed(signal, horn::high, true); + CHECK(signal == horn::high); + + // letting go of the low tone key leaves the high tone sounding + signal = horn::released(signal, horn::low); + CHECK(signal == horn::high); + + signal = horn::released(signal, horn::high); + CHECK(signal == 0); +} + +TEST_CASE("horn: separate buttons for the tones can sound both at once") { + auto signal{0}; + signal = horn::pressed(signal, horn::low, false); + signal = horn::pressed(signal, horn::high, false); + CHECK(signal == (horn::low | horn::high)); + + signal = horn::released(signal, horn::low); + CHECK(signal == horn::high); +} + +TEST_CASE("horn: releasing a tone leaves the other bits of the signal alone") { + auto const third{4}; // the third horn, operated separately + CHECK(horn::released(horn::low | third, horn::low) == third); + CHECK(horn::pressed(third, horn::high, true) == (horn::high | third)); +} + +// what the horns need to sound (eu07/maszyna#290): a pneumatic horn the air of the main reservoir, an electric one +// the low voltage. the horn lever stays where the driver put it, only the horn is silent +namespace { + +test::vehicle horn_vehicle(test::definition Definition) { + // a main reservoir of its own + return test::vehicle{Definition.set("Brake", "Vv", 1.0).set("Brake", "MinCP", 6.5).set("Brake", "MaxCP", 8.0)}; +} + +int const allhorns{horn::low | horn::high | 4}; + +} // namespace + +TEST_CASE("horn: a pneumatic horn sounds on the air of the main reservoir only") { + auto vehicle{horn_vehicle(test::definition::load("wagon"))}; + REQUIRE(vehicle->HornSupply == horn_supply::air); // the default + + vehicle->Compressor = 6.5; + CHECK(vehicle->SoundingHorns(allhorns) == allhorns); + vehicle->Compressor = 0.0; // the reservoir is empty + CHECK(vehicle->SoundingHorns(allhorns) == 0); +} + +TEST_CASE("horn: a vehicle without a main reservoir takes the horn air from the main pipe") { + test::vehicle vehicle{test::definition::load("wagon")}; + REQUIRE(vehicle->VeselVolume == 0.0); + + vehicle->ScndPipePress = 5.0; + CHECK(vehicle->SoundingHorns(horn::low) == horn::low); + vehicle->ScndPipePress = 0.0; + CHECK(vehicle->SoundingHorns(horn::low) == 0); +} + +TEST_CASE("horn: an electric horn sounds on the low voltage, whatever the air") { + auto vehicle{horn_vehicle(test::definition::load("wagon").set("Cntrl", "HornType", "Electric"))}; + REQUIRE(vehicle->HornSupply == horn_supply::electric); + REQUIRE(vehicle->Horn3Supply == horn_supply::electric); // the third horn follows the others unless told otherwise + + vehicle->Compressor = 0.0; + vehicle->Power24vIsAvailable = true; + CHECK(vehicle->SoundingHorns(allhorns) == allhorns); + vehicle->Power24vIsAvailable = false; + CHECK(vehicle->SoundingHorns(allhorns) == 0); +} + +TEST_CASE("horn: the third horn can have a supply of its own, an electric horn of a diesel say") { + auto vehicle{horn_vehicle(test::definition::load("wagon").set("Cntrl", "Horn3Type", "Electric"))}; + REQUIRE(vehicle->HornSupply == horn_supply::air); + REQUIRE(vehicle->Horn3Supply == horn_supply::electric); + + vehicle->Compressor = 0.0; + vehicle->Power24vIsAvailable = true; + CHECK(vehicle->SoundingHorns(allhorns) == 4); + vehicle->Compressor = 6.5; + vehicle->Power24vIsAvailable = false; + CHECK(vehicle->SoundingHorns(allhorns) == (horn::low | horn::high)); +} + +TEST_CASE("horn: a vehicle without any air, a road vehicle say, sounds its pneumatic horn as before") { + // no main reservoir and no main pipe connection at either end + test::vehicle vehicle{test::definition::load("wagon").set("BuffCoupl", "AllowedFlag", 3)}; + REQUIRE(vehicle->VeselVolume == 0.0); + vehicle->ScndPipePress = 0.0; + CHECK(vehicle->SoundingHorns(allhorns) == allhorns); +} + +TEST_CASE("horn: a pneumatic horn sounds from the threshold pressure up") { + auto vehicle{horn_vehicle(test::definition::load("wagon"))}; + vehicle->Compressor = TMoverParameters::HornMinPressure; + CHECK(vehicle->SoundingHorns(horn::low) == horn::low); + vehicle->Compressor = TMoverParameters::HornMinPressure - 0.01; + CHECK(vehicle->SoundingHorns(horn::low) == 0); +} + +TEST_CASE("horn: an electric horn sounds on 110 V too") { + auto vehicle{horn_vehicle(test::definition::load("wagon").set("Cntrl", "HornType", "Electric"))}; + vehicle->Power110vIsAvailable = true; + CHECK(vehicle->SoundingHorns(horn::high) == horn::high); +} diff --git a/tests/unit/test_line_contactors.cpp b/tests/unit/test_line_contactors.cpp new file mode 100644 index 000000000..79c419edf --- /dev/null +++ b/tests/unit/test_line_contactors.cpp @@ -0,0 +1,59 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +// the lamp of the open line contactors (eu07/maszyna#470): lit, it tells the start can be made, so it should go out +// where the start is cut off, by the pressure switch at 2 bar in the brake cylinders, not at 1 bar already + +namespace { + +test::vehicle locomotive(bool const Pressureswitch) { + test::vehicle vehicle{test::definition::load("wagon").set("Engine", "PressureSwitch", Pressureswitch)}; + vehicle->StLinFlag = false; // the line contactors open + vehicle->PipePress = 5.0; + return vehicle; +} + +} // namespace + +TEST_CASE("line contactors: with a pressure switch the lamp goes out with the start cut off") { + auto vehicle{locomotive(true)}; + REQUIRE(vehicle->HasControlPressureSwitch); + + vehicle->BrakePress = 1.5; // braked, the start can still be made + vehicle->MotorConnectorsCheck(); // updates the pressure switch + REQUIRE_FALSE(vehicle->ControlPressureSwitch); + CHECK(vehicle->LineContactorsLampLit()); + + vehicle->BrakePress = 2.5; // the pressure switch cuts the start off + vehicle->MotorConnectorsCheck(); + REQUIRE(vehicle->ControlPressureSwitch); + CHECK_FALSE(vehicle->LineContactorsLampLit()); +} + +TEST_CASE("line contactors: without a pressure switch the lamp goes out with the brakes applied, as before") { + auto vehicle{locomotive(false)}; + vehicle->BrakePress = 0.5; + vehicle->MotorConnectorsCheck(); + CHECK(vehicle->LineContactorsLampLit()); + vehicle->BrakePress = 1.5; + vehicle->MotorConnectorsCheck(); + CHECK_FALSE(vehicle->LineContactorsLampLit()); +} + +TEST_CASE("line contactors: closed contactors put the lamp out") { + auto vehicle{locomotive(true)}; + vehicle->StLinFlag = true; + CHECK_FALSE(vehicle->LineContactorsLampLit()); +} + +TEST_CASE("line contactors: the ground relay and overload lamps follow the same rule") { + // all the lamps of the main circuit go out together, with the pressure switch where it's fitted + auto vehicle{locomotive(true)}; + vehicle->BrakePress = 1.5; + vehicle->MotorConnectorsCheck(); + CHECK(vehicle->MainCircuitLampsEnabled()); + vehicle->BrakePress = 2.5; + vehicle->MotorConnectorsCheck(); + CHECK_FALSE(vehicle->MainCircuitLampsEnabled()); +} diff --git a/tests/unit/test_mirrors.cpp b/tests/unit/test_mirrors.cpp new file mode 100644 index 000000000..7408eeaa5 --- /dev/null +++ b/tests/unit/test_mirrors.cpp @@ -0,0 +1,32 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle/mirrors.h" + +// the sounds of the mirrors (MaSzyna-EU07/maszyna#84, eu07/maszyna#322): a mirror sounds as it sets off, unfolding +// or folding, and not again while it keeps moving the same way + +using mirror_moves::event; + +TEST_CASE("mirrors: a mirror sounds once as it sets off") { + mirror_moves::motion mirror; + CHECK(mirror.update(0.0) == event::none); // standing folded + CHECK(mirror.update(0.01) == event::unfolding); + CHECK(mirror.update(0.01) == event::none); // still unfolding + CHECK(mirror.update(0.0) == event::none); // unfolded + CHECK(mirror.update(-0.01) == event::folding); + CHECK(mirror.update(-0.01) == event::none); +} + +TEST_CASE("mirrors: a mirror turned back halfway sounds the other way") { + mirror_moves::motion mirror; + REQUIRE(mirror.update(0.01) == event::unfolding); + CHECK(mirror.update(-0.01) == event::folding); // the vehicle got too fast while it was unfolding +} + +TEST_CASE("mirrors: a mirror setting off again the same way sounds again") { + mirror_moves::motion mirror; + REQUIRE(mirror.update(0.01) == event::unfolding); + REQUIRE(mirror.update(0.0) == event::none); // stopped, the doors lost their permit say + CHECK(mirror.update(0.01) == event::unfolding); +} diff --git a/tests/unit/test_pumps.cpp b/tests/unit/test_pumps.cpp new file mode 100644 index 000000000..2245e3eb8 --- /dev/null +++ b/tests/unit/test_pumps.cpp @@ -0,0 +1,52 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +// the joint oil and fuel pump switch (MaSzyna-EU07/maszyna#81) works the pumps which aren't started automatically + +namespace { + +test::vehicle pumps_vehicle(std::string const &Fuelstart, std::string const &Oilstart) { + return test::vehicle{test::definition::load("wagon").set("Cntrl", "FuelStart", Fuelstart).set("Cntrl", "OilStart", Oilstart)}; +} + +// what the two-state joint switch does when pressed, as TTrain::OnCommand_oilfuelpumptoggle() +void press_joint_switch(test::vehicle &Vehicle) { + auto const enable{false == Vehicle->OilFuelPumpsEnabled()}; + Vehicle->FuelPumpSwitch(enable, range_t::local); + Vehicle->FuelPumpSwitchOff(false == enable, range_t::local); + Vehicle->OilPumpSwitch(enable, range_t::local); + Vehicle->OilPumpSwitchOff(false == enable, range_t::local); +} + +} // namespace + +TEST_CASE("pumps: the joint switch turns both manual pumps on, then off") { + auto vehicle{pumps_vehicle("Manual", "Manual")}; + REQUIRE_FALSE(vehicle->OilFuelPumpsEnabled()); + press_joint_switch(vehicle); + CHECK(vehicle->FuelPump.is_enabled); + CHECK(vehicle->OilPump.is_enabled); + CHECK(vehicle->OilFuelPumpsEnabled()); + press_joint_switch(vehicle); + CHECK_FALSE(vehicle->FuelPump.is_enabled); + CHECK_FALSE(vehicle->OilPump.is_enabled); +} + +TEST_CASE("pumps: with one pump started automatically the joint switch still turns the other one off") { + auto vehicle{pumps_vehicle("Manual", "Automatic")}; + press_joint_switch(vehicle); + REQUIRE(vehicle->FuelPump.is_enabled); + CHECK(vehicle->OilFuelPumpsEnabled()); // the switch shows on + press_joint_switch(vehicle); + CHECK_FALSE(vehicle->FuelPump.is_enabled); +} + +TEST_CASE("pumps: with one manual pump on and the other off the joint switch turns both on") { + auto vehicle{pumps_vehicle("Manual", "Manual")}; + vehicle->FuelPumpSwitch(true, range_t::local); + REQUIRE_FALSE(vehicle->OilFuelPumpsEnabled()); + press_joint_switch(vehicle); + CHECK(vehicle->FuelPump.is_enabled); + CHECK(vehicle->OilPump.is_enabled); +} diff --git a/tests/unit/test_radio.cpp b/tests/unit/test_radio.cpp new file mode 100644 index 000000000..33a12f72d --- /dev/null +++ b/tests/unit/test_radio.cpp @@ -0,0 +1,28 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +// the radio works only switched on and with the low voltage (eu07/maszyna#429): its channel and volume buttons, +// the calls and the radio-stop all ask RadioPowered() + +TEST_CASE("radio: a radio switched on with the low voltage works") { + test::vehicle vehicle{test::definition::load("wagon")}; + vehicle->Radio = true; + SUBCASE("24 V") { vehicle->Power24vIsAvailable = true; } + SUBCASE("110 V") { vehicle->Power110vIsAvailable = true; } + CHECK(vehicle->RadioPowered()); +} + +TEST_CASE("radio: a radio switched off or without the low voltage doesn't") { + test::vehicle vehicle{test::definition::load("wagon")}; + SUBCASE("switched off") { + vehicle->Radio = false; + vehicle->Power24vIsAvailable = true; + } + SUBCASE("batteries off") { + vehicle->Radio = true; + vehicle->Power24vIsAvailable = false; + vehicle->Power110vIsAvailable = false; + } + CHECK_FALSE(vehicle->RadioPowered()); +} diff --git a/tests/unit/test_switches.cpp b/tests/unit/test_switches.cpp new file mode 100644 index 000000000..72b1e47ce --- /dev/null +++ b/tests/unit/test_switches.cpp @@ -0,0 +1,16 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +// the kinds of the cab switches, given in the Switches: section of the vehicle definition + +TEST_CASE("switches: the train heating switch can spring back (MaSzyna-EU07/maszyna#85)") { + SUBCASE("impulse") { + test::vehicle vehicle{test::definition::load("cab_vehicle").set("Switches", "TrainHeating", "Impulse")}; + CHECK(vehicle->HeatingSwitchImpulse); // whichever the case it's written in + } + SUBCASE("a plain switch by default") { + test::vehicle vehicle{test::definition::load("cab_vehicle")}; + CHECK_FALSE(vehicle->HeatingSwitchImpulse); + } +} diff --git a/tests/unit/test_wheelslip.cpp b/tests/unit/test_wheelslip.cpp new file mode 100644 index 000000000..9b39e82c6 --- /dev/null +++ b/tests/unit/test_wheelslip.cpp @@ -0,0 +1,62 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +using test::definition; +using test::track; +using test::vehicle; + +namespace { + +struct braking_result { + bool skidded{false}; // the wheels locked while braking + bool stopped{false}; // came (almost) to a stop on the braking rail +}; + +// brakes the wagon from 40 km/h on the Braking rail until it's (nearly) stopped, then has it stand on the +// Standing one for 30 seconds +braking_result brake_to_stop_then_stand(vehicle &Coach, track const &Braking, track const &Standing) { + braking_result result; + Coach.hold_brake_pipe(5.0); + Coach.run(5.0, Braking); + Coach.hold_brake_pipe(3.5); // service braking + // a skidding wagon brakes on the sliding adhesion only, give it time + for (auto step = 0; step < 60000; ++step) { // 600 s in 0.01 s steps + result.skidded |= Coach->SlippingWheels; + if (auto const speed{std::abs(Coach.speed())}; speed > 0.0 && speed < 0.1) { + // almost stopped; a skidding wagon is still skidding at this point + result.stopped = true; + break; + } + Coach.step(Braking); + } + Coach.run(30.0, Standing); + return result; +} + +} // namespace + +// the engine doesn't let a stopped, braked vehicle slip (the Vel > 0.1 trap in TMoverParameters::ComputeTotalForce), +// so a wagon which stopped in a skid should be no different from one which stopped without it. it used to stay +// skidding instead, with its brakes capped by the sliding adhesion, and slid or crept away (eu07/maszyna#479) +TEST_CASE("wheelslip: a wagon which stopped in a skid holds like one which stopped without it") { + track const dry{0.15, 0.0}; + track const slippery{0.02, 0.0}; + // held only by the brakes: the sliding adhesion on this rail is too low for the slope + track const slipperyslope{0.02, 0.08}; + + vehicle normal{definition::load("wagon"), 40.0}; + auto const normalbraking{brake_to_stop_then_stand(normal, dry, slipperyslope)}; + vehicle skidding{definition::load("wagon"), 40.0}; + auto const skiddingbraking{brake_to_stop_then_stand(skidding, slippery, slipperyslope)}; + + // the test means something only when the braking went as planned + REQUIRE(normalbraking.stopped); + REQUIRE_FALSE(normalbraking.skidded); + REQUIRE(skiddingbraking.stopped); + REQUIRE(skiddingbraking.skidded); + + CHECK(std::abs(normal.speed()) < 0.1); + CHECK(std::abs(skidding.speed()) < 0.1); + CHECK_FALSE(skidding->SlippingWheels); +} diff --git a/tests/unit/vehicle.cpp b/tests/unit/vehicle.cpp index 135c5c2fb..c0930d134 100644 --- a/tests/unit/vehicle.cpp +++ b/tests/unit/vehicle.cpp @@ -1,5 +1,6 @@ #include "stdafx.h" #include "vehicle.h" +#include "vehicle/DynObj.h" #include #include @@ -159,6 +160,18 @@ vehicle::vehicle(definition const &Definition, double Velocity, std::string cons } void vehicle::step(track const &Track, double Dt) { + compute_forces(Dt); + compute_movement(Track, Dt, TLocation{0.0, 0.0, 0.0}); +} + +void vehicle::compute_forces(double Dt) { + if (m_pipepressure >= 0.0) { + m_mover->Pipe->CreatePress(m_pipepressure); + } + m_mover->ComputeTotalForce(Dt); +} + +double vehicle::compute_movement(track const &Track, double Dt, TLocation const &Location) { TTrackShape shape; shape.Len = 1000.0; shape.dHtrack = Track.gradient; @@ -167,13 +180,8 @@ void vehicle::step(track const &Track, double Dt) { param.friction = Track.friction; param.CategoryFlag = 1; TTractionParam const traction; - TLocation const location{0.0, 0.0, 0.0}; TRotation const rotation{0.0, 0.0, 0.0}; - if (m_pipepressure >= 0.0) { - m_mover->Pipe->CreatePress(m_pipepressure); - } - m_mover->ComputeTotalForce(Dt); - m_mover->ComputeMovement(Dt, Dt, shape, param, traction, location, rotation); + return m_mover->ComputeMovement(Dt, Dt, shape, param, traction, Location, rotation); } void vehicle::run(double Seconds, track const &Track, double Dt) { @@ -184,4 +192,88 @@ void vehicle::run(double Seconds, track const &Track, double Dt) { } } +// the physics reaches the vehicle next to it through TDynamicObject::MoverParameters only (neighbour_data). the +// tests can't make a TDynamicObject, whose constructor and members come with the rest of the engine, so the +// neighbour is a zeroed block of its size with just that pointer set: enough for the physics, which reads nothing +// else of it, and never used as a whole object +static_assert(alignof(TDynamicObject) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__); + +namespace { + +std::vector make_standin(TMoverParameters &Mover) { + std::vector standin(sizeof(TDynamicObject)); // zeroed + reinterpret_cast(standin.data())->MoverParameters = &Mover; // NOSONAR see above + return standin; +} + +TDynamicObject *as_neighbour(std::byte *Standin) { + return reinterpret_cast(Standin); // NOSONAR see above +} + +} // namespace + +pair::pair(vehicle &First, int FirstEnd, vehicle &Second, int SecondEnd, double Gap) { + auto const &first{First.mover()}; + auto const &second{Second.mover()}; + // the first vehicle in the middle, its FirstEnd towards the second one + m_vehicles[0] = {&First, FirstEnd, 0.0, FirstEnd == end::front ? 1.0 : -1.0, make_standin(First.mover())}; + m_vehicles[1] = {&Second, SecondEnd, 0.5 * first.Dim.L + Gap + 0.5 * second.Dim.L, SecondEnd == end::front ? -1.0 : 1.0, make_standin(Second.mover())}; +} + +pair::~pair() { + for (auto const &placed : m_vehicles) { + // the vehicles outlive the pair, they mustn't point at its stand-ins + for (auto &neighbour : placed.owner->mover().Neighbours) { + neighbour = neighbour_data(); + } + } +} + +void pair::find_neighbours() { + // as TDynamicObject::update_neighbours() + for (auto const &placed : m_vehicles) { + placed.owner->mover().Loc = TLocation{placed.position, 0.0, 0.0}; + } + for (int i = 0; i < 2; ++i) { + auto const &self{m_vehicles[i]}; + auto &other{m_vehicles[1 - i]}; // its stand-in is what this one points at + auto &mover{self.owner->mover()}; + auto const &othermover{other.owner->mover()}; + auto &neighbour{mover.Neighbours[self.end]}; + neighbour.vehicle = as_neighbour(other.standin.data()); + neighbour.vehicle_end = other.end; + neighbour.distance = static_cast(TMoverParameters::CouplerDist(&mover, &othermover) - mover.Couplers[self.end].adapter_length - + othermover.Couplers[other.end].adapter_length); + mover.Neighbours[1 - self.end] = neighbour_data(); + } +} + +void pair::step(track const &Track, double Dt) { + find_neighbours(); + for (auto const &placed : m_vehicles) { + placed.owner->compute_forces(Dt); + } + for (auto &placed : m_vehicles) { + placed.position += placed.heading * placed.owner->compute_movement(Track, Dt, TLocation{placed.position, 0.0, 0.0}); + } +} + +void pair::run(double Seconds, track const &Track, double Dt) { + // whole steps, as in vehicle::run() + auto const steps{std::lround(Seconds / Dt)}; + for (long i = 0; i < steps; ++i) { + step(Track, Dt); + } +} + +double pair::gap() const { + auto const &first{m_vehicles[0].owner->mover()}; + auto const &second{m_vehicles[1].owner->mover()}; + return std::abs(m_vehicles[1].position - m_vehicles[0].position) - 0.5 * (first.Dim.L + second.Dim.L); +} + +int pair::coupling() const { + return m_vehicles[0].owner->mover().Couplers[m_vehicles[0].end].CouplingFlag; +} + } // namespace test diff --git a/tests/unit/vehicle.h b/tests/unit/vehicle.h index ad036839e..616e75d11 100644 --- a/tests/unit/vehicle.h +++ b/tests/unit/vehicle.h @@ -1,8 +1,10 @@ #pragma once +#include #include #include #include +#include #include @@ -64,8 +66,57 @@ public: double speed() const { return m_mover->V * 3.6; } private: + friend class pair; + + // the two halves of a step: the forces, taken by all the vehicles in the scene first, then the movement, + // the vehicle placed at Location. returns the distance travelled [m], positive forwards + void compute_forces(double Dt); + double compute_movement(track const &Track, double Dt, TLocation const &Location); + std::unique_ptr m_mover; double m_pipepressure{-1.0}; }; +// two vehicles on a straight track, the FirstEnd of the first one facing the SecondEnd of the second one Gap metres +// away (from coupler to coupler), with nothing else around. a step runs them the way the simulation runs the +// vehicles of a scene (vehicle_table::update()): each one finds its neighbour, then both take the forces, the +// coupler and buffer ones among them (CouplerForce()), then both move and handle the collisions (CollisionDetect()). +// they couple and part on their own, as the couplers let them; the speed of each runs along its own axis +// test::pair pair{moving, end::front, standing, end::rear, 1.0}; // 1 m apart +// pair.run(5.0); // for 5 s +// CHECK(pair.coupled()); +class pair { +public: + pair(vehicle &First, int FirstEnd, vehicle &Second, int SecondEnd, double Gap); + ~pair(); + pair(pair const &) = delete; + pair &operator=(pair const &) = delete; + // the vehicles point at the stand-ins of the pair, which stay where they are + pair(pair &&) = delete; + pair &operator=(pair &&) = delete; + + // advances both vehicles by one step of Dt + void step(track const &Track = {}, double Dt = 0.01); + // advances both vehicles by Seconds in steps of Dt + void run(double Seconds, track const &Track = {}, double Dt = 0.01); + // distance between the facing couplers [m], negative while the buffers are pressed in + double gap() const; + // type of the coupling between the vehicles, coupling::faux when they aren't coupled + int coupling() const; + bool coupled() const { return coupling() != coupling::faux; } + +private: + struct placement { + vehicle *owner{nullptr}; + int end{0}; // the end facing the other vehicle + double position{0.0}; // of the middle of the vehicle along the track [m] + double heading{1.0}; // +1 when the vehicle runs forwards towards the growing position, -1 otherwise + std::vector standin; // what the other vehicle sees as this one, see vehicle.cpp + }; + + void find_neighbours(); + + std::array m_vehicles; +}; + } // namespace test diff --git a/utilities/translation.cpp b/utilities/translation.cpp index b1db12275..c0dbeb1a0 100644 --- a/utilities/translation.cpp +++ b/utilities/translation.cpp @@ -218,6 +218,7 @@ std::string locale::label_cab_control(std::string const &Label) { "watercircuitslink_sw:", STRN("water circuits link") }, { "fuelpump_sw:", STRN("fuel pump") }, { "oilpump_sw:", STRN("oil pump") }, + { "oilfuelpump_sw:", STRN("oil and fuel pumps") }, { "motorblowersfront_sw:", STRN("motor blowers A") }, { "motorblowersrear_sw:", STRN("motor blowers B") }, { "motorblowersalloff_sw:", STRN("all motor blowers") }, diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 5beac4c1e..08f0c0d24 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -30,6 +30,8 @@ http://mozilla.org/MPL/2.0/. #include "world/station.h" #include "input/keyboardinput.h" +#include + #define LOGVELOCITY 0 #define LOGORDERS 1 #define LOGSTOPS 1 @@ -1093,10 +1095,9 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub if (Point.fDist < passengerstopmaxdistance * 0.5 ) { // zaliczamy posterunek w pewnej odległości przed (choć W4 nie zasłania już semafora) #if LOGSTOPS - WriteLog( - pVehicle->asName + " as " + TrainParams.TrainName - + ": at " + std::to_string(simulation::Time.data().wHour) + ":" + std::to_string(simulation::Time.data().wMinute) - + " passed " + asNextStop); // informacja + WriteLog(std::format( + "{} as {}: at {}:{} passed {}", + pVehicle->asName, TrainParams.TrainName, simulation::Time.data().wHour, simulation::Time.data().wMinute, asNextStop)); // informacja #endif // przy jakim dystansie (stanie licznika) ma przesunąć na następny postój fLastStopExpDist = mvOccupied->DistCounter + 0.250 + 0.001 * fLength; @@ -1253,10 +1254,9 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub TrainParams.StationIndexInc(); // przejście do następnej asNextStop = TrainParams.NextStop(); // pobranie kolejnego miejsca zatrzymania #if LOGSTOPS - WriteLog( - pVehicle->asName + " as " + TrainParams.TrainName - + ": at " + std::to_string(simulation::Time.data().wHour) + ":" + std::to_string(simulation::Time.data().wMinute) - + " next " + asNextStop); // informacja + WriteLog(std::format( + "{} as {}: at {}:{} next {}", + pVehicle->asName, TrainParams.TrainName, simulation::Time.data().wHour, simulation::Time.data().wMinute, asNextStop)); // informacja #endif // update consist weight, brake settings and ai braking tables // NOTE: this calculation is expected to run after completing loading/unloading @@ -1297,13 +1297,16 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub else { // jeśli dojechaliśmy do końca rozkładu #if LOGSTOPS - WriteLog( - pVehicle->asName + " as " + TrainParams.TrainName - + ": at " + std::to_string(simulation::Time.data().wHour) + ":" + std::to_string(simulation::Time.data().wMinute) - + " end of route."); // informacja + WriteLog(std::format( + "{} as {}: at {}:{} end of route.", + pVehicle->asName, TrainParams.TrainName, simulation::Time.data().wHour, simulation::Time.data().wMinute)); // informacja #endif asNextStop = TrainParams.NextStop(); // informacja o końcu trasy TrainParams.NewName("none"); // czyszczenie nieaktualnego rozkładu + // koniec kursu, gasimy wyświetlacze relacji (tablice blaszane zostają) + for( auto *vehicle { pVehicles[ end::front ] }; vehicle != nullptr; vehicle = vehicle->Next() ) { + vehicle->DestinationClear(); + } // ma nie podjeżdżać pod W4 i ma je pomijać iDrivigFlags &= ~moveStopCloser; if( false == TestFlag( iDrivigFlags, movePushPull ) ) { diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 32ba307f7..c6793e454 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -4150,6 +4150,8 @@ bool TDynamicObject::Update(double dt, double dt1) // mirrors + auto const mirrorleft { dMirrorMoveL }; + auto const mirrorright { dMirrorMoveR }; if( MoverParameters->Vel > MoverParameters->MirrorVelClose || MoverParameters->CabActive == 0 && activation::mirrors || MoverParameters->MirrorForbidden ) { @@ -4180,6 +4182,8 @@ bool TDynamicObject::Update(double dt, double dt1) dMirrorMoveR + 1.0 * dt1 ); } } + play_mirror_sounds( side::left, m_mirrormotion[ side::left ].update( dMirrorMoveL - mirrorleft ) ); + play_mirror_sounds( side::right, m_mirrormotion[ side::right ].update( dMirrorMoveR - mirrorright ) ); // compartment lights // if the vehicle has a controller, we base the light state on state of the controller otherwise we check the vehicle itself @@ -4825,12 +4829,13 @@ void TDynamicObject::RenderSounds() { // horns { - // for moving vehicle combine regular horn activation flag with emergency brake horn activation flag, if the brake is active - auto const warningsignal { ( - MoverParameters->Vel > 0.5 && MoverParameters->AlarmChainFlag ? + // for moving vehicle combine regular horn activation flag with emergency brake horn activation flag, if the brake is active; + // the horns without the air or the voltage they need stay silent + auto const warningsignal { MoverParameters->SoundingHorns( + ( MoverParameters->Vel > 0.5 && MoverParameters->AlarmChainFlag ? MoverParameters->EmergencyBrakeWarningSignal : 0 ) - | MoverParameters->WarningSignal }; + | MoverParameters->WarningSignal ) }; if( TestFlag( warningsignal, 1 ) ) { sHorn1.play( sound_flags::exclusive | sound_flags::looping ); @@ -6363,6 +6368,19 @@ void TDynamicObject::LoadMMediaFile( std::string const &TypeName, std::string co } } + else if( token == "mirroropen:" || token == "mirrorclose:" ) { + sound_source soundtemplate { sound_placement::general }; + soundtemplate.deserialize( parser, sound_type::single ); + soundtemplate.owner( this ); + for( auto &mirror : m_mirrorsounds ) { + // apply configuration to all defined mirrors, but preserve their individual offsets + auto &sound { token == "mirroropen:" ? mirror.unfold : mirror.fold }; + auto const mirroroffset { sound.offset() }; + sound = soundtemplate; + sound.offset( mirroroffset ); + } + } + else if( token == "doorstepclose:" ) { sound_source soundtemplate { sound_placement::general }; soundtemplate.deserialize( parser, sound_type::single ); @@ -6576,6 +6594,27 @@ void TDynamicObject::LoadMMediaFile( std::string const &TypeName, std::string co } } + else if( token == "mirrors:" ) { + // a list of offsets along vehicle's z-axis of the mirror pairs, one at each cab; followed with "end" + while( (token = parser.getToken()) != "" + && token != "end" ) { + // vehicle faces +Z in 'its' space, as for the doors negative value means ahead of centre + auto const offset { std::atof( token.c_str() ) * -1.f }; + mirror_sounds mirror; + mirror.end = offset >= 0.f ? end::front : end::rear; + for( auto const mirrorside : { side::left, side::right } ) { + auto const location { glm::vec3 { + MoverParameters->Dim.W * ( mirrorside == side::left ? 0.5f : -0.5f ), + MoverParameters->Dim.H * 0.5f, + offset } }; + mirror.placement = mirrorside; + mirror.unfold.offset( location ); + mirror.fold.offset( location ); + m_mirrorsounds.emplace_back( mirror ); + } + } + } + else if( token == "tractionmotors:" ) { // a list of offsets along vehicle's z-axis; followed with "end" while( (token = parser.getToken()) != "" @@ -7794,7 +7833,29 @@ int TDynamicObject::RouteWish(TTrack *tr) return Mechanik ? Mechanik->CrossRoute(tr) : 0; // wg AI albo prosto }; -void TDynamicObject::DestinationSet(std::string to, std::string numer) { +void TDynamicObject::play_mirror_sounds( side const Side, mirror_moves::event const Event ) { + + if( Event == mirror_moves::event::none ) { return; } + // as UpdateMirror(), only the mirrors of the occupied cab move + if( MoverParameters->CabOccupied == 0 ) { return; } + auto const cabend { MoverParameters->CabOccupied > 0 ? end::front : end::rear }; + for( auto &mirror : m_mirrorsounds ) { + if( mirror.placement != Side || mirror.end != cabend ) { continue; } + auto &sound { Event == mirror_moves::event::unfolding ? mirror.unfold : mirror.fold }; + auto &other { Event == mirror_moves::event::unfolding ? mirror.fold : mirror.unfold }; + other.stop(); + sound.play( sound_flags::exclusive ); + } +} + +void TDynamicObject::DestinationClear() { + + if( false == DestinationSign.has_light ) { return; } // physical destination signs remain up until manually changed + // update_destinations() shows the blank sign in place of the missing destination + DestinationSign.destination = null_handle; +} + +void TDynamicObject::DestinationSet(std::string const &to, std::string const &numer) { // ustawienie stacji docelowej oraz wymiennej tekstury 4, jeśli istnieje plik // w zasadzie, to każdy wagon mógłby mieć inną stację docelową // zwłaszcza w towarowych, pod kątem zautomatyzowania maewrów albo pracy górki @@ -7823,6 +7884,15 @@ void TDynamicObject::DestinationSet(std::string to, std::string numer) { if( DestinationSign.script.empty() ) { return; } // no script so no way to make the texture if( numer == "none" ) { return; } // blank or incomplete/malformed timetable, don't bother + auto const trainname { [this]() -> std::string { + if( ctOwner != nullptr ) { return ctOwner->TrainName(); } + if( Mechanik != nullptr ) { return Mechanik->TrainName(); } + return "none"; } }; // shouldn't get here but, eh + auto const instance { [this]() -> std::string { + if( DestinationSign.instancing == "name" ) { return MoverParameters->Name; } + if( DestinationSign.instancing == "type" ) { return MoverParameters->TypeName; } + return "none"; } }; + std::string signrequest { "make:" + DestinationSign.script @@ -7834,17 +7904,10 @@ void TDynamicObject::DestinationSet(std::string to, std::string numer) { ctOwner->Vehicle()->MoverParameters->Name ) // owned vehicle, safer to point to owner as carriages can have identical names // basic instancing string // NOTE: underscore doesn't have any magic meaning for the time being, it's just less likely to conflict with regular dictionary keys - + "&_id1=" + ( - ctOwner != nullptr ? ctOwner->TrainName() : - Mechanik != nullptr ? Mechanik->TrainName() : - "none" ) }; // shouldn't get here but, eh + + "&_id1=" + trainname() }; // TBD, TODO: replace instancing with support for variables in extra parameters string? if( false == DestinationSign.instancing.empty() ) { - signrequest += - "&_id2=" + ( - DestinationSign.instancing == "name" ? MoverParameters->Name : - DestinationSign.instancing == "type" ? MoverParameters->TypeName : - "none" ); + signrequest += "&_id2=" + instance(); } // optionl extra parameters if( false == DestinationSign.parameters.empty() ) { diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index 36335a320..2d9636b2f 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -21,6 +21,7 @@ http://mozilla.org/MPL/2.0/. #include "interfaces/ITexture.h" #include "audio/sound.h" #include "world/Spring.h" +#include "vehicle/mirrors.h" #include @@ -393,6 +394,13 @@ private: side placement {}; }; + struct mirror_sounds { + sound_source unfold { sound_placement::general }; + sound_source fold { sound_placement::general }; + side placement {}; + int end {}; // of the vehicle, the mirror moves with the cab at this end occupied + }; + struct exchange_sounds { sound_source loading { sound_placement::general }; sound_source unloading { sound_placement::general }; @@ -567,6 +575,8 @@ private: std::vector m_pantographsounds; // typically 2 but can be less (or more?) std::vector m_doorsounds; // can expect symmetrical arrangement, but don't count on it bool m_doorlocks { false }; // sound helper, current state of door locks + std::vector m_mirrorsounds; + std::array m_mirrormotion; // indexed by side: right, left sound_source sHorn1 { sound_placement::external, 5 * EU07_SOUND_RUNNINGNOISECUTOFFRANGE }; sound_source sHorn2 { sound_placement::external, 5 * EU07_SOUND_RUNNINGNOISECUTOFFRANGE }; sound_source sHorn3 { sound_placement::external, 5 * EU07_SOUND_RUNNINGNOISECUTOFFRANGE }; @@ -703,6 +713,8 @@ private: void Move(double fDistance); void FastMove(double fDistance); void RenderSounds(); + // plays the sounds of the mirrors on the Side, of the occupied cab, starting the Event + void play_mirror_sounds( side const Side, mirror_moves::event const Event ); inline glm::dvec3 GetPosition() const { return vPosition; }; // converts location from vehicle coordinates frame to world frame @@ -815,7 +827,9 @@ private: void ParamSet(int what, int into); // zapytanie do AI, po którym segmencie skrzyżowania jechać int RouteWish(TTrack *tr); - void DestinationSet(std::string to, std::string numer); + void DestinationSet(std::string const &to, std::string const &numer); + // blanks electronic destination sign at the end of the service; physical signs stay until changed + void DestinationClear(); material_handle DestinationFind( std::string Destination ); void OverheadTrack(float o); glm::dvec3 get_future_movement() const; diff --git a/vehicle/Gauge.cpp b/vehicle/Gauge.cpp index d7a41062e..af8e9a060 100644 --- a/vehicle/Gauge.cpp +++ b/vehicle/Gauge.cpp @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "vehicle/Gauge.h" +#include "vehicle/gaugepositions.h" #include "utilities/parser.h" #include "model/Model3d.h" #include "vehicle/DynObj.h" @@ -302,42 +303,37 @@ bool TGauge::UpdateValue( float fNewDesired ) { auto const desiredtimes100 = static_cast( std::round( 100.0 * fNewDesired ) ); - if( desiredtimes100 == static_cast( std::round( 100.0 * m_targetvalue ) ) ) { + auto const previoustimes100 = static_cast( std::round( 100.0 * m_targetvalue ) ); + if( desiredtimes100 == previoustimes100 ) { return true; } m_targetvalue = fNewDesired; - // if there's any sound associated with new requested value, play it - // check value-specific table first... - if( auto const fullinteger { desiredtimes100 % 100 == 0 }; fullinteger ) { - // filter out values other than full integers - auto const lookup = m_soundfxvalues.find( desiredtimes100 / 100 ); - if( lookup != m_soundfxvalues.end() ) { - lookup->second.play(); - return true; - } - } - else { + if( desiredtimes100 % 100 != 0 ) { // toggle the control to continous range/exclusive sound mode from now on m_soundtype = sound_flags::exclusive; } + // if there's any sound associated with a position reached on the way to the new requested value, play it + // check value-specific table first... + if( auto const reached = gauge_positions::last_reached( m_soundfxvalues, previoustimes100, desiredtimes100 ); + reached != m_soundfxvalues.end() ) { + reached->second.play(); + return true; + } // ...and if there isn't any, fall back on the basic set... // HACK: crude way to discern controls with continuous and quantized value range - if( auto const currentvalue = GetValue(); currentvalue < fNewDesired ) { + auto const currentvalue = GetValue(); + if( currentvalue < fNewDesired && false == m_soundfxincrease.empty() ) { // shift up - if( false == m_soundfxincrease.empty() ) { - m_soundfxincrease.play( m_soundtype ); - return true; - } + m_soundfxincrease.play( m_soundtype ); + return true; } - else if( currentvalue > fNewDesired ) { + if( currentvalue > fNewDesired && false == m_soundfxdecrease.empty() ) { // shift down - if( false == m_soundfxdecrease.empty() ) { - m_soundfxdecrease.play( m_soundtype ); - return true; - } + m_soundfxdecrease.play( m_soundtype ); + return true; } return false; // no suitable sound was found -}; +} void TGauge::PutValue(float fNewDesired) { // McZapkie-281102: natychmiastowe wpisanie wartosci diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index f0b2367f7..201b8972f 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -14,6 +14,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "vehicle/Train.h" +#include "vehicle/horn.h" #include "utilities/Globals.h" #include "simulation/simulation.h" @@ -340,6 +341,7 @@ TTrain::commandhandler_map const TTrain::m_commandhandlers = { {user_command::fuelpumpenable, &TTrain::OnCommand_fuelpumpenable}, {user_command::fuelpumpdisable, &TTrain::OnCommand_fuelpumpdisable}, {user_command::oilpumptoggle, &TTrain::OnCommand_oilpumptoggle}, + {user_command::oilfuelpumptoggle, &TTrain::OnCommand_oilfuelpumptoggle}, {user_command::oilpumpenable, &TTrain::OnCommand_oilpumpenable}, {user_command::oilpumpdisable, &TTrain::OnCommand_oilpumpdisable}, {user_command::waterheaterbreakertoggle, &TTrain::OnCommand_waterheaterbreakertoggle}, @@ -3995,6 +3997,37 @@ void TTrain::OnCommand_oilpumpdisable(TTrain *Train, command_data const &Command } } +void TTrain::OnCommand_oilfuelpumptoggle(TTrain *Train, command_data const &Command) +{ + if (Command.action == GLFW_REPEAT) + { + return; + } + + auto *vehicle{Train->mvControlled}; + if (Train->ggOilFuelPumpButton.type() == TGaugeType::push) + { + // impulse switch, as the separate ones: held to start both pumps + auto const pressed{Command.action == GLFW_PRESS}; + Train->ggOilFuelPumpButton.UpdateValue(pressed ? 1.0 : 0.0, Train->dsbSwitch); + vehicle->FuelPumpSwitch(pressed); + vehicle->OilPumpSwitch(pressed); + return; + } + // two-state switch, only cares about press events + if (Command.action != GLFW_PRESS) + { + return; + } + // both pumps go off if both were on, otherwise both go on; a pump started automatically doesn't take part + auto const enable{false == vehicle->OilFuelPumpsEnabled()}; + Train->ggOilFuelPumpButton.UpdateValue(enable ? 1.0 : 0.0, Train->dsbSwitch); + vehicle->FuelPumpSwitch(enable); + vehicle->FuelPumpSwitchOff(false == enable); + vehicle->OilPumpSwitch(enable); + vehicle->OilPumpSwitchOff(false == enable); +} + void TTrain::OnCommand_waterheaterbreakertoggle(TTrain *Train, command_data const &Command) { @@ -6605,7 +6638,7 @@ void TTrain::OnCommand_heatingtoggle(TTrain *Train, command_data const &Command) OnCommand_heatingdisable(Train, Command); } } - else if (Command.action == GLFW_RELEASE && Train->ggTrainHeatingButton.type() == TGaugeType::push) + else if (Command.action == GLFW_RELEASE && (Train->ggTrainHeatingButton.type() == TGaugeType::push || Train->mvOccupied->HeatingSwitchImpulse)) { // impulse switch // visual feedback @@ -6623,6 +6656,11 @@ void TTrain::OnCommand_heatingenable(TTrain *Train, command_data const &Command) // visual feedback Train->ggTrainHeatingButton.UpdateValue(1.0, Train->dsbSwitch); } + else if (Command.action == GLFW_RELEASE && Train->mvOccupied->HeatingSwitchImpulse) + { + // the switch springs back to the middle + Train->ggTrainHeatingButton.UpdateValue(0.0, Train->dsbSwitch); + } } void TTrain::OnCommand_heatingdisable(TTrain *Train, command_data const &Command) @@ -6632,8 +6670,21 @@ void TTrain::OnCommand_heatingdisable(TTrain *Train, command_data const &Command { Train->mvOccupied->HeatingSwitch(false); - // visual feedback - Train->ggTrainHeatingButton.UpdateValue(Train->ggTrainHeatingButton.type() == TGaugeType::push ? 1.0 : 0.0, Train->dsbSwitch); + // visual feedback: an impulse switch is pushed the other way, down + auto position{0.0}; + if (Train->mvOccupied->HeatingSwitchImpulse) + { + position = -1.0; + } + else if (Train->ggTrainHeatingButton.type() == TGaugeType::push) + { + position = 1.0; + } + Train->ggTrainHeatingButton.UpdateValue(position, Train->dsbSwitch); + } + else if (Command.action == GLFW_RELEASE && Train->mvOccupied->HeatingSwitchImpulse) + { + Train->ggTrainHeatingButton.UpdateValue(0.0, Train->dsbSwitch); } } @@ -7841,16 +7892,10 @@ void TTrain::OnCommand_hornlowactivate(TTrain *Train, command_data const &Comman if (Command.action == GLFW_PRESS) { // only need to react to press, sound will continue until stopped - if (false == TestFlag(Train->mvOccupied->WarningSignal, 1)) + if (false == TestFlag(Train->mvOccupied->WarningSignal, horn::low)) { // turn on - Train->mvOccupied->WarningSignal |= 1; - /* - if( true == TestFlag( Train->mvOccupied->WarningSignal, 2 ) ) { - // low and high horn are treated as mutually exclusive - Train->mvControlled->WarningSignal &= ~2; - } - */ + Train->mvOccupied->WarningSignal = horn::pressed(Train->mvOccupied->WarningSignal, horn::low, Train->has_single_horn_lever()); // visual feedback Train->ggHornButton.UpdateValue(-1.0); Train->ggHornLowButton.UpdateValue(1.0); @@ -7859,13 +7904,13 @@ void TTrain::OnCommand_hornlowactivate(TTrain *Train, command_data const &Comman else if (Command.action == GLFW_RELEASE) { // turn off - /* - // NOTE: we turn off both low and high horn, due to unreliability of release event when shift key is involved - Train->mvOccupied->WarningSignal &= ~( 1 | 2 ); - */ - Train->mvOccupied->WarningSignal &= ~1; + Train->mvOccupied->WarningSignal = horn::released(Train->mvOccupied->WarningSignal, horn::low); // visual feedback - Train->ggHornButton.UpdateValue(0.0); + if (false == (Train->has_single_horn_lever() && TestFlag(Train->mvOccupied->WarningSignal, horn::high))) + { + // leave the single lever alone if it was moved to the other tone in the meantime + Train->ggHornButton.UpdateValue(0.0); + } Train->ggHornLowButton.UpdateValue(0.0); } } @@ -7885,16 +7930,10 @@ void TTrain::OnCommand_hornhighactivate(TTrain *Train, command_data const &Comma if (Command.action == GLFW_PRESS) { // only need to react to press, sound will continue until stopped - if (false == TestFlag(Train->mvOccupied->WarningSignal, 2)) + if (false == TestFlag(Train->mvOccupied->WarningSignal, horn::high)) { // turn on - Train->mvOccupied->WarningSignal |= 2; - /* - if( true == TestFlag( Train->mvOccupied->WarningSignal, 1 ) ) { - // low and high horn are treated as mutually exclusive - Train->mvControlled->WarningSignal &= ~1; - } - */ + Train->mvOccupied->WarningSignal = horn::pressed(Train->mvOccupied->WarningSignal, horn::high, Train->has_single_horn_lever()); // visual feedback Train->ggHornButton.UpdateValue(1.0); Train->ggHornHighButton.UpdateValue(1.0); @@ -7903,13 +7942,13 @@ void TTrain::OnCommand_hornhighactivate(TTrain *Train, command_data const &Comma else if (Command.action == GLFW_RELEASE) { // turn off - /* - // NOTE: we turn off both low and high horn, due to unreliability of release event when shift key is involved - Train->mvOccupied->WarningSignal &= ~( 1 | 2 ); - */ - Train->mvOccupied->WarningSignal &= ~2; + Train->mvOccupied->WarningSignal = horn::released(Train->mvOccupied->WarningSignal, horn::high); // visual feedback - Train->ggHornButton.UpdateValue(0.0); + if (false == (Train->has_single_horn_lever() && TestFlag(Train->mvOccupied->WarningSignal, horn::low))) + { + // leave the single lever alone if it was moved to the other tone in the meantime + Train->ggHornButton.UpdateValue(0.0); + } Train->ggHornHighButton.UpdateValue(0.0); } } @@ -7953,12 +7992,7 @@ void TTrain::OnCommand_radiotoggle(TTrain *Train, command_data const &Command) return; } - // NOTE: we ignore the lack of 3d model to allow system reset after receiving radio-stop signal - /* - if( false == Train->m_controlmapper.contains( "radio_sw:" ) ) { - return; - } - */ + // NOTE: works without the switch in the 3d model too, to allow system reset after receiving radio-stop signal // only reacting to press, so the sound can loop uninterrupted if (false == Train->mvOccupied->Radio) { @@ -8003,9 +8037,13 @@ void TTrain::OnCommand_radiochannelincrease(TTrain *Train, command_data const &C if (Command.action == GLFW_PRESS) { - command_data newCommand = Command; - newCommand.param1 = Train->RadioChannel() + 1; - OnCommand_radiochannelset(Train, newCommand); + // the channel buttons of a radio switched off or without power do nothing + if (Train->mvOccupied->RadioPowered()) + { + command_data newCommand = Command; + newCommand.param1 = Train->RadioChannel() + 1; + OnCommand_radiochannelset(Train, newCommand); + } Train->ggRadioChannelNext.UpdateValue(1.0); } @@ -8021,9 +8059,12 @@ void TTrain::OnCommand_radiochanneldecrease(TTrain *Train, command_data const &C if (Command.action == GLFW_PRESS) { - command_data newCommand = Command; - newCommand.param1 = Train->RadioChannel() - 1; - OnCommand_radiochannelset(Train, newCommand); + if (Train->mvOccupied->RadioPowered()) + { + command_data newCommand = Command; + newCommand.param1 = Train->RadioChannel() - 1; + OnCommand_radiochannelset(Train, newCommand); + } Train->ggRadioChannelPrevious.UpdateValue(1.0); } @@ -8049,7 +8090,7 @@ void TTrain::OnCommand_radiostopsend(TTrain *Train, command_data const &Command) if (Command.action == GLFW_PRESS) { - if (true == Train->mvOccupied->Radio && (Train->mvOccupied->Power24vIsAvailable || Train->mvOccupied->Power110vIsAvailable)) + if (Train->mvOccupied->RadioPowered()) { simulation::Region->RadioStop(Train->Dynamic()->GetPosition()); } @@ -8067,7 +8108,7 @@ void TTrain::OnCommand_radiostopenable(TTrain *Train, command_data const &Comman { if (Command.action == GLFW_PRESS && Train->ggRadioStop.GetValue() == 0) { - if (true == Train->mvOccupied->Radio && (Train->mvOccupied->Power24vIsAvailable || Train->mvOccupied->Power110vIsAvailable)) + if (Train->mvOccupied->RadioPowered()) { simulation::Region->RadioStop(Train->Dynamic()->GetPosition()); } @@ -8090,7 +8131,7 @@ void TTrain::OnCommand_radiostoptest(TTrain *Train, command_data const &Command) if (Command.action == GLFW_PRESS) { - if (Train->RadioChannel() == 10 && true == Train->mvOccupied->Radio && (Train->mvOccupied->Power24vIsAvailable || Train->mvOccupied->Power110vIsAvailable)) + if (Train->RadioChannel() == 10 && Train->mvOccupied->RadioPowered()) { Train->Dynamic()->RadioStop(); } @@ -8108,7 +8149,7 @@ void TTrain::OnCommand_radiocall1send(TTrain *Train, command_data const &Command { if (Command.action == GLFW_PRESS) { - if (Train->RadioChannel() != 10 && true == Train->mvOccupied->Radio && (Train->mvOccupied->Power24vIsAvailable || Train->mvOccupied->Power110vIsAvailable)) + if (Train->RadioChannel() != 10 && Train->mvOccupied->RadioPowered()) { simulation::Events.queue_receivers(radio_message::call1, Train->Dynamic()->GetPosition()); } @@ -8127,7 +8168,7 @@ void TTrain::OnCommand_radiocall3send(TTrain *Train, command_data const &Command if (Command.action == GLFW_PRESS) { - if (Train->RadioChannel() != 10 && true == Train->mvOccupied->Radio && (Train->mvOccupied->Power24vIsAvailable || Train->mvOccupied->Power110vIsAvailable)) + if (Train->RadioChannel() != 10 && Train->mvOccupied->RadioPowered()) { simulation::Events.queue_receivers(radio_message::call3, Train->Dynamic()->GetPosition()); } @@ -8146,9 +8187,13 @@ void TTrain::OnCommand_radiovolumeincrease(TTrain *Train, command_data const &Co if (Command.action == GLFW_PRESS) { - command_data newCommand = Command; - newCommand.param1 = Train->m_radiovolume + 0.125; - OnCommand_radiovolumeset(Train, newCommand); + // so do the volume buttons + if (Train->mvOccupied->RadioPowered()) + { + command_data newCommand = Command; + newCommand.param1 = Train->m_radiovolume + 0.125; + OnCommand_radiovolumeset(Train, newCommand); + } Train->ggRadioVolumeNext.UpdateValue(1.0); } else if (Command.action == GLFW_RELEASE) @@ -8163,9 +8208,12 @@ void TTrain::OnCommand_radiovolumedecrease(TTrain *Train, command_data const &Co if (Command.action == GLFW_PRESS) { - command_data newCommand = Command; - newCommand.param1 = Train->m_radiovolume - 0.125; - OnCommand_radiovolumeset(Train, newCommand); + if (Train->mvOccupied->RadioPowered()) + { + command_data newCommand = Command; + newCommand.param1 = Train->m_radiovolume - 0.125; + OnCommand_radiovolumeset(Train, newCommand); + } Train->ggRadioVolumePrevious.UpdateValue(1.0); } else if (Command.action == GLFW_RELEASE) @@ -8931,9 +8979,9 @@ bool TTrain::Update(double const Deltatime) btLampkaOpory.Turn(mvControlled->StLinFlag && mvControlled->ResistorsFlagCheck()); btLampkaBezoporowa.Turn(mvControlled->ResistorsFlagCheck() || mvControlled->MainCtrlActualPos == 0); // do EU04 - btLampkaStyczn.Turn(!mvControlled->StLinFlag && !mvControlled->ControlPressureSwitch && mvControlled->BrakePress < 1.0); // mozna prowadzic rozruch - btLampkaPrzekRozn.Turn(!mvControlled->GroundRelay && !mvControlled->ControlPressureSwitch && mvControlled->BrakePress < 1.0); // relay is off and needs a reset - btLampkaNadmSil.Turn(mvControlled->FuseFlagCheck() && !mvControlled->ControlPressureSwitch && mvControlled->BrakePress < 1.0); // relay is off and needs a reset + btLampkaStyczn.Turn(mvControlled->LineContactorsLampLit()); // mozna prowadzic rozruch + btLampkaPrzekRozn.Turn(!mvControlled->GroundRelay && mvControlled->MainCircuitLampsEnabled()); // relay is off and needs a reset + btLampkaNadmSil.Turn(mvControlled->FuseFlagCheck() && mvControlled->MainCircuitLampsEnabled()); // relay is off and needs a reset btLampkaUkrotnienie.Turn(TestFlag(mvControlled->Couplers[mvControlled->CabOccupied == 1 ? rear : front].CouplingFlag, control)); btLampkaHamPosp.Turn(TestFlag(mvOccupied->Hamulec->GetBrakeStatus(), 1)); // lampka drugiego stopnia hamowania // TODO: youBy wyciągnąć flagę wysokiego stopnia @@ -9180,14 +9228,7 @@ bool TTrain::Update(double const Deltatime) btLampkaOporyB.Turn(mover->ResistorsFlagCheck()); btLampkaBezoporowaB.Turn(true == mover->ResistorsFlagCheck() || mover->MainCtrlActualPos == 0); // do EU04 - if (mover->StLinFlag || mover->ControlPressureSwitch) - { - btLampkaStycznB.Turn(false); - } - else if (mover->BrakePress < 1.0) - { - btLampkaStycznB.Turn(true); // mozna prowadzic rozruch - } + btLampkaStycznB.Turn(mover->LineContactorsLampLit()); // mozna prowadzic rozruch // hunter-271211: sygnalizacja poslizgu w pierwszym pojezdzie, gdy wystapi w drugim if (mover->SlippingWheels) { @@ -9583,6 +9624,7 @@ bool TTrain::Update(double const Deltatime) ggWaterCircuitsLinkButton.Update(); ggFuelPumpButton.Update(); ggOilPumpButton.Update(); + ggOilFuelPumpButton.Update(); ggMotorBlowersFrontButton.Update(); ggMotorBlowersRearButton.Update(); ggMotorBlowersAllOffButton.Update(); @@ -10106,7 +10148,7 @@ void TTrain::update_sounds_radio() std::erase_if(m_radiomessages, [](auto const &source) { return false == source.second->is_playing(); }); } // adjust audibility of remaining messages based on current radio conditions - auto const radioenabled{true == mvOccupied->Radio && (mvOccupied->Power24vIsAvailable || mvOccupied->Power110vIsAvailable)}; + auto const radioenabled{mvOccupied->RadioPowered()}; for (auto const &[messagechannel, messagesound] : m_radiomessages) { auto const volume{true == radioenabled && Dynamic()->Mechanik != nullptr && messagechannel == RadioChannel() ? m_radiovolume : 0.0}; @@ -10893,7 +10935,7 @@ void TTrain::radio_message(sound_source *Message, int const Channel) m_radiomessages.emplace_back(Channel, std::make_shared(m_radiosound)); // assign sound to the template and play it auto &message = *m_radiomessages.back().second.get(); - auto const radioenabled{true == mvOccupied->Radio && (mvOccupied->Power24vIsAvailable || mvOccupied->Power110vIsAvailable)}; + auto const radioenabled{mvOccupied->RadioPowered()}; auto const volume{true == radioenabled && Dynamic()->Mechanik != nullptr && Channel == RadioChannel() ? 1.0 : 0.0}; message.copy_sounds(*Message).gain(static_cast(volume)).play(); } @@ -11081,6 +11123,7 @@ void TTrain::clear_cab_controls() ggWaterCircuitsLinkButton.Clear(); ggFuelPumpButton.Clear(); ggOilPumpButton.Clear(); + ggOilFuelPumpButton.Clear(); ggMotorBlowersFrontButton.Clear(); ggMotorBlowersRearButton.Clear(); ggMotorBlowersAllOffButton.Clear(); @@ -11362,7 +11405,7 @@ void TTrain::set_cab_controls(int const Cab) ggDoorStepButton.PutValue(mvOccupied->Doors.step_enabled ? 1.f : 0.f); } // heating - if (false == ggTrainHeatingButton.is_push()) + if (false == ggTrainHeatingButton.is_push() && false == mvOccupied->HeatingSwitchImpulse) { ggTrainHeatingButton.PutValue(mvControlled->Heating ? 1.f : 0.f); } @@ -11415,6 +11458,11 @@ void TTrain::set_cab_controls(int const Cab) { ggOilPumpButton.PutValue(mvControlled->OilPump.is_enabled ? 1.f : 0.f); } + // joint oil and fuel pump + if (ggOilFuelPumpButton.type() != TGaugeType::push) + { + ggOilFuelPumpButton.PutValue(mvControlled->OilFuelPumpsEnabled() ? 1.f : 0.f); + } // traction motor fans if (ggMotorBlowersFrontButton.type() != TGaugeType::push) { @@ -11742,6 +11790,7 @@ bool TTrain::initialize_gauge(cParser &Parser, std::string const &Label, int con {"watercircuitslink_sw:", ggWaterCircuitsLinkButton}, {"fuelpump_sw:", ggFuelPumpButton}, {"oilpump_sw:", ggOilPumpButton}, + {"oilfuelpump_sw:", ggOilFuelPumpButton}, {"oilpressb:", ggOilPressB}, {"motorblowersfront_sw:", ggMotorBlowersFrontButton}, {"motorblowersrear_sw:", ggMotorBlowersRearButton}, diff --git a/vehicle/Train.h b/vehicle/Train.h index 4fc11dc2c..009b349b4 100644 --- a/vehicle/Train.h +++ b/vehicle/Train.h @@ -195,6 +195,9 @@ class TTrain { bool initialize_button(cParser &Parser, std::string const &Label, int const Cabindex); // helper, returns true for EMU with oerlikon brake bool is_eztoer() const; + // helper, returns true if both horn tones are operated with the same lever, without separate low and high tone buttons + bool has_single_horn_lever() const { + return ggHornButton.SubModel != nullptr && ggHornLowButton.SubModel == nullptr && ggHornHighButton.SubModel == nullptr; } // locates nearest vehicle belonging to the consist TDynamicObject *find_nearest_consist_vehicle(bool freefly, glm::vec3 pos) const; // mover master controller to specified position @@ -339,6 +342,7 @@ class TTrain { static void OnCommand_fuelpumpenable( TTrain *Train, command_data const &Command ); static void OnCommand_fuelpumpdisable( TTrain *Train, command_data const &Command ); static void OnCommand_oilpumptoggle( TTrain *Train, command_data const &Command ); + static void OnCommand_oilfuelpumptoggle( TTrain *Train, command_data const &Command ); static void OnCommand_oilpumpenable( TTrain *Train, command_data const &Command ); static void OnCommand_oilpumpdisable( TTrain *Train, command_data const &Command ); static void OnCommand_waterheaterbreakertoggle( TTrain *Train, command_data const &Command ); @@ -692,7 +696,8 @@ public: // reszta może by?publiczna TGauge ggWaterHeaterButton; // water heater switch TGauge ggWaterCircuitsLinkButton; TGauge ggFuelPumpButton; // fuel pump switch - TGauge ggOilPumpButton; // fuel pump switch + TGauge ggOilPumpButton; // oil pump switch + TGauge ggOilFuelPumpButton; // joint oil and fuel pump switch TGauge ggMotorBlowersFrontButton; // front traction motor fan switch TGauge ggMotorBlowersRearButton; // rear traction motor fan switch TGauge ggMotorBlowersAllOffButton; // motor fans shutdown switch diff --git a/vehicle/gaugepositions.h b/vehicle/gaugepositions.h new file mode 100644 index 000000000..fc82ed44b --- /dev/null +++ b/vehicle/gaugepositions.h @@ -0,0 +1,35 @@ +/* +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 + +// positions of a cab control which have sounds of their own +namespace gauge_positions { + +// the entry of the last position of Positions the control reached on its way from the Previous to the Desired value, +// both in hundredths of a position, or Positions.end() if it reached none: a lever moved smoothly passes the +// positions rather than stops at them, and should sound each one it gets to +template +typename std::map::iterator last_reached(std::map &Positions, int const Previous, int const Desired) { + auto reached{Positions.end()}; + for (auto position{Positions.begin()}; position != Positions.end(); ++position) { + auto const at{position->first * 100}; + if (Desired > Previous && at > Previous && at <= Desired) { + reached = position; // moving up, the last one in the ascending order is the one reached last + } + else if (Desired < Previous && at < Previous && at >= Desired) { + return position; // moving down, the first one is + } + } + return reached; +} + +} // namespace gauge_positions diff --git a/vehicle/horn.cpp b/vehicle/horn.cpp new file mode 100644 index 000000000..006274bb9 --- /dev/null +++ b/vehicle/horn.cpp @@ -0,0 +1,24 @@ +/* +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/horn.h" + +namespace horn { + +int pressed(int const Signal, int const Tone, bool const Singlelever) { + auto const other{Tone == low ? high : low}; + return (Signal | Tone) & (Singlelever ? ~other : ~0); +} + +int released(int const Signal, int const Tone) { + return Signal & ~Tone; +} + +} // namespace horn diff --git a/vehicle/horn.h b/vehicle/horn.h new file mode 100644 index 000000000..615db2f1b --- /dev/null +++ b/vehicle/horn.h @@ -0,0 +1,24 @@ +/* +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 horn controls of the cab, on the WarningSignal bits of the vehicle (1: low tone, 2: high tone) +namespace horn { + +int const low{1}; +int const high{2}; + +// the signal after the key or button of the Tone is pressed. a single lever for both tones, without +// separate buttons for them, can't sound both tones at once +int pressed(int Signal, int Tone, bool Singlelever); +// the signal after the key or button of the Tone is released +int released(int Signal, int Tone); + +} // namespace horn diff --git a/vehicle/mirrors.h b/vehicle/mirrors.h new file mode 100644 index 000000000..cf3735541 --- /dev/null +++ b/vehicle/mirrors.h @@ -0,0 +1,44 @@ +/* +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 mirrors of a vehicle, unfolded on the side of the open doors and folded at speed +namespace mirror_moves { + +enum class event { + none, + unfolding, + folding +}; + +// the move of a mirror from one update to the next. a mirror sounds as it sets off, unfolding or folding, not on +// every update it keeps moving the same way +struct motion { + int direction{0}; // of the last update: +1 unfolding, -1 folding, 0 standing + + // the event the Change of the mirror position (0: folded, 1: unfolded) since the last update starts, if any + event update(double const Change) { + auto now{0}; + if (Change > 0.0) { + now = 1; + } + else if (Change < 0.0) { + now = -1; + } + auto const setsoff{now != 0 && now != direction}; + direction = now; + if (false == setsoff) { + return event::none; + } + return now > 0 ? event::unfolding : event::folding; + } +}; + +} // namespace mirror_moves diff --git a/vr/openvr_imp.cpp b/vr/openvr_imp.cpp index 088fd5b58..15467d006 100644 --- a/vr/openvr_imp.cpp +++ b/vr/openvr_imp.cpp @@ -474,6 +474,9 @@ std::unordered_map vr_openvr::m_buttonb { "oilpump_sw:", { user_command::oilpumptoggle, user_command::none } }, + { "oilfuelpump_sw:", { + user_command::oilfuelpumptoggle, + user_command::none } }, { "motorblowersfront_sw:", { user_command::motorblowerstogglefront, user_command::none } }, From 823807201c7b1c8512fff2c45aaf23dbf062ab9e Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sun, 1 Nov 2026 13:40:57 +0100 Subject: [PATCH 17/90] Fix driver UI "mode windows" menu (#226) Make the keyboard shortcuts match the actually used ones and check the correct panels for is_open. --- application/driveruilayer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/driveruilayer.cpp b/application/driveruilayer.cpp index 569ade337..0b7b0dc46 100644 --- a/application/driveruilayer.cpp +++ b/application/driveruilayer.cpp @@ -74,9 +74,9 @@ void driver_ui::render_menu_contents() if (ImGui::BeginMenu(STR_C("Mode windows"))) { ImGui::MenuItem(m_aidpanel.title.c_str(), "F1", &m_aidpanel.is_open); - ImGui::MenuItem(m_scenariopanel.title.c_str(), "F1", &m_aidpanel.is_open); ImGui::MenuItem(STR_C("Timetable"), "F2", &m_timetablepanel.is_open); - ImGui::MenuItem(m_debugpanel.name().c_str(), "F12", &m_debugpanel.is_open); + ImGui::MenuItem(m_scenariopanel.title.c_str(), "F3", &m_scenariopanel.is_open); + ImGui::MenuItem(m_debugpanel.name().c_str(), "Shift+F12", &m_debugpanel.is_open); ImGui::MenuItem(m_mappanel.name().c_str(), "Tab", &m_mappanel.is_open); ImGui::MenuItem(m_vehiclelist.name().c_str(), nullptr, &m_vehiclelist.is_open); ImGui::MenuItem(m_trainingcardpanel.name().c_str(), nullptr, &m_trainingcardpanel.is_open); From a68c535be9e3338306881576be4b115f5f87b0a6 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 1 Nov 2026 20:12:33 +0100 Subject: [PATCH 18/90] fix(track): the cab keeps the shake a track doesn't replace, and the editor redraws all that an edit changes The side sway from the lateral acceleration now goes off only on a track with a sideways value, and the random jolts on one with a vertical or a sideways value. Dips at the rail joints and cross level alone replace neither. On a switch it goes by the path set and what its joined tracks allow. An edit in the editor also rebuilds the other switches of a joined group with the tracks they meet, and the tracks of a rail run whose joints moved. Removing a track does the same. While dragging, the tracks further off wait until the drag ends. The clatter of a bogie's axles was right: the followers stand at the pivots and the offsets are the places of the wheel sets. The misleading name goes. Also what Sonar had left: a parameter as a span, a const method, a struct and its member apart, commented out code, and a function of the grading tool split, with the same output. --- application/editormode.h | 1 + application/editormodetrack.cpp | 6 ++++-- editor/editorTrack.cpp | 22 +++++++++++++++---- editor/editorTrack.hpp | 5 ++++- tests/unit/test_track_uneven.cpp | 22 +++++++++++++++++++ tools/track_uneven_grade.py | 25 ++++++++++++++------- track_uneven_doc.md | 6 +++--- vehicle/DynObj.cpp | 18 ++++++++-------- world/Segment.h | 3 ++- world/Track.cpp | 5 ++++- world/Track.h | 11 ++++++++-- world/TrackUneven.cpp | 37 ++++++++++++++++++++++++++++++-- world/TrackUneven.h | 12 +++++++++++ 13 files changed, 140 insertions(+), 33 deletions(-) diff --git a/application/editormode.h b/application/editormode.h index c7dbd3911..4b395ae68 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -639,6 +639,7 @@ class editor_mode : public application_mode, private editor_track::observer glm::mat4 m_track_gizmo{1.0f}; bool m_track_gizmo_using{false}; bool m_track_dirty{false}; + bool m_track_drag_on_the_way{false}; // committed, but not as the end of the drag std::chrono::steady_clock::time_point m_track_last_commit; editor_track::state m_track_field_before; std::array, 3> m_track_materials{}; diff --git a/application/editormodetrack.cpp b/application/editormodetrack.cpp index 0ae732d86..2523929d8 100644 --- a/application/editormodetrack.cpp +++ b/application/editormodetrack.cpp @@ -442,12 +442,14 @@ void editor_mode::render_track_gizmo() void editor_mode::commit_track_drag(bool const Force) { - if (false == m_track_dirty || m_track_drag.empty()) + // on letting go the rest of what follows the dragged tracks is yet to come, even with all of the drag committed + if ((false == m_track_dirty && false == (Force && m_track_drag_on_the_way)) || m_track_drag.empty()) return; auto const now = std::chrono::steady_clock::now(); if (false == Force && now - m_track_last_commit < std::chrono::milliseconds(100)) return; - editor_track::commit(m_track_drag); + editor_track::commit(m_track_drag, false == Force); + m_track_drag_on_the_way = false == Force; m_track_dirty = false; m_track_last_commit = now; } diff --git a/editor/editorTrack.cpp b/editor/editorTrack.cpp index b70501747..8e6339913 100644 --- a/editor/editorTrack.cpp +++ b/editor/editorTrack.cpp @@ -426,7 +426,7 @@ bool editor_track::can_edit_geometry(TTrack const &Track, std::string &Reason) return true; } -void editor_track::commit(std::vector const &Tracks) +void editor_track::commit(std::vector const &Tracks, bool const Dragging) { if (Tracks.empty()) return; @@ -457,6 +457,8 @@ void editor_track::commit(std::vector const &Tracks) add_unique(affected, neighbour); for (auto *track : affected) update_transition(*track); + if (false == Dragging) + add_followers(affected); for (auto *track : affected) rebuild_geometry(*track); for (auto *track : Tracks) @@ -478,6 +480,7 @@ void editor_track::commit_parameters(TTrack &Track) add_unique(affected, neighbour); for (auto *track : affected) update_transition(*track); + add_followers(affected); for (auto *track : affected) rebuild_geometry(*track); Track.mark_dirty(); @@ -2819,14 +2822,25 @@ void editor_track::retire(TTrack &Track) simulation::Region->erase_and_unregister(&Track); Track.m_editorremoved = true; for (auto *neighbour : adjoining) - { update_transition(*neighbour); - rebuild_geometry(*neighbour); - } + auto affected{adjoining}; + add_followers(affected); + for (auto *track : affected) + rebuild_geometry(*track); if (s_observer != nullptr) s_observer->track_retired(Track); } +void editor_track::add_followers(std::vector &Tracks) +{ + ++TTrack::s_layoutstamp; + for (auto *track : TTrack::switch_followers(Tracks)) + add_unique(Tracks, track); + for (auto *track : simulation::Paths.sequence()) + if (false == track->m_editorremoved && track->rail_joints_moved()) + add_unique(Tracks, track); +} + void editor_track::rebuild_geometry(TTrack &Track) { auto §ion{simulation::Region->section(Track.location())}; diff --git a/editor/editorTrack.hpp b/editor/editorTrack.hpp index 14f9e9a40..efb5fdac9 100644 --- a/editor/editorTrack.hpp +++ b/editor/editorTrack.hpp @@ -93,7 +93,8 @@ class editor_track static bool can_edit_geometry(TTrack const &Track, std::string &Reason); static bool is_supported(TTrack const &Track); - static void commit(std::vector const &Tracks); + // Dragging: on the way, what follows them from further off waits for the commit at the end of it + static void commit(std::vector const &Tracks, bool const Dragging = false); static void commit_parameters(TTrack &Track); static glm::dvec3 point_position(TTrack const &Track, point_ref const &Point); @@ -350,6 +351,8 @@ class editor_track static void bind_switch_path(TTrack &Switch, int const Path); static void store_switch_path(TTrack &Switch, int const Path); static void rebuild_geometry(TTrack &Track); + // adds the tracks whose shape goes by a change of the given from further off: by the switches they meet, and by rail joints running on through them + static void add_followers(std::vector &Tracks); static TTrack *clone(TTrack const &Template); static TTrack *load_text(std::string const &Text, TTrack const *Template, std::string const &Name); static void move_straight(straight const &Line, glm::dvec3 const &Start, glm::dvec3 const &End, std::function const &Member); diff --git a/tests/unit/test_track_uneven.cpp b/tests/unit/test_track_uneven.cpp index 06f4663b0..b55de1644 100644 --- a/tests/unit/test_track_uneven.cpp +++ b/tests/unit/test_track_uneven.cpp @@ -38,6 +38,28 @@ void add_chord(std::vector &Output, std::vector const &Line, int } // namespace +TEST_CASE("uneven track: the cab keeps what the ride on the track doesn't stand in for") +{ + auto const replaced = [](double const Up, double const Side, double const Cross) { + return track_uneven::replaced_shake({Up, Side, Cross}); + }; + // all of it, as "uneven 18" + CHECK(replaced(18.0, 18.0, 18.0).sway); + CHECK(replaced(18.0, 18.0, 18.0).jolts); + // nothing at all, as on a track with dips at its joints alone + CHECK_FALSE(replaced(0.0, 0.0, 0.0).sway); + CHECK_FALSE(replaced(0.0, 0.0, 0.0).jolts); + // up and down only: there is nothing to sway with + CHECK_FALSE(replaced(18.0, 0.0, 0.0).sway); + CHECK(replaced(18.0, 0.0, 0.0).jolts); + // sideways only + CHECK(replaced(0.0, 18.0, 0.0).sway); + CHECK(replaced(0.0, 18.0, 0.0).jolts); + // cross level only turns the body + CHECK_FALSE(replaced(0.0, 0.0, 18.0).sway); + CHECK_FALSE(replaced(0.0, 0.0, 18.0).jolts); +} + TEST_CASE("uneven track: measures as the Id-14 limits for its value") { // appendix 1, class C at 80 km/h: 18 mm vertically, 17 mm sideways, 16 mm of twist on 5 m, gauge +10/-8 mm, 20 mm of cross level, J = 5.3 diff --git a/tools/track_uneven_grade.py b/tools/track_uneven_grade.py index 1994559d0..81df2ce2a 100644 --- a/tools/track_uneven_grade.py +++ b/tools/track_uneven_grade.py @@ -261,20 +261,29 @@ def plain_neighbours(t, joints): return [other for key in ("p1", "p2") for other, _, _ in joints.at(t["paths"][0][key]) if other is not t and other["kind"] == "normal"] +def joined_at(t, point, joints, seen): + # the plain tracks joined at a point, and the far ends of the straight paths of the switches joined there + plain, further = [], [] + for other, part, side in joints.at(point): + if other is t or id(part) in seen: + continue + seen.add(id(part)) + if other["kind"] != "switch": + plain.append(other) + elif other["paths"].index(part) != other["bent"]: + further.append(part["p2" if side == "p1" else "p1"]) + return plain, further + + def plain_past_switches(t, point, joints): # the plain tracks reached from a point of a track along the straight paths of switches points, seen, found = [point], set(), [] for _ in range(6): onward = [] for reached in points: - for other, part, side in joints.at(reached): - if other is t or id(part) in seen: - continue - seen.add(id(part)) - if other["kind"] != "switch": - found.append(other) - elif other["paths"].index(part) != other["bent"]: - onward.append(part["p2" if side == "p1" else "p1"]) + plain, further = joined_at(t, reached, joints, seen) + found += plain + onward += further points = onward return found diff --git a/track_uneven_doc.md b/track_uneven_doc.md index 1c3163c50..e4822b971 100644 --- a/track_uneven_doc.md +++ b/track_uneven_doc.md @@ -69,12 +69,12 @@ The rails, the top of the ballast, `sleepermodel`, the axles, the roll of the bo Models from includes (e.g. switch drives and timbers), the terrain and the catenary don't follow it, so at large values a rail shifts away from such a model by a few centimetres. -On a track with unevenness the track itself shakes the cab, in place of two of the sources of shake that were there before. It goes by the track under the vehicle, and a track with only dips at its joints counts too. On tracks with no unevenness everything works as before. +On a track with unevenness the track itself shakes the cab, in place of two of the sources of shake that were there before, as far as it has something to put in their place. It goes by the track under the vehicle, and on a switch by the path set and what its joined tracks allow. Dips at the joints and cross level replace nothing, as they don't shake the cab all the way along or sideways. On tracks with no unevenness everything works as before. | source of the cab shake | on a track with unevenness | |--------------------------------------------------------------------|----------------------------| -| side sway from the lateral acceleration: curve radius and cant, jolts sideways on a damaged switch | off | -| random jolts over 25 km/h | off | +| side sway from the lateral acceleration: curve radius and cant, jolts sideways on a damaged switch | off with a sideways value | +| random jolts over 25 km/h | off with a vertical or a sideways value | | the track itself, from the bend under the axles | added | | vertical jolts at the joints of a damaged track | stays | | fore and aft, when accelerating and braking | stays | diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 305e12020..c4838395f 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -3381,7 +3381,6 @@ bool TDynamicObject::Update(double dt, double dt1) && MoverParameters->eimc[eimc_p_abed] < 0.001) FzadED = 0; //pętla bezpieczeństwa - bez ED auto const FzadPN = Fzad - FrED * MoverParameters->MED_FrED_factor; - //np = 0; std::vector PrzekrF( np ); float nPrzekrF = 0; bool test = true; @@ -8019,12 +8018,12 @@ TDynamicObject::update_shake( double const Timedelta ) { shakevector *= 0; auto const iVel { std::min( GetVelocity(), 150.0 ) }; - // an uneven track shakes the cab by itself, in place of the sway drawn from the curve and the jolts drawn at random - auto const uneven { MyTrack != nullptr && MyTrack->is_uneven() }; + // an uneven track shakes the cab by itself, in place of the sway drawn from the curve and the jolts drawn at random, as far as it does + auto const replaced { track_uneven::replaced_shake( MyTrack != nullptr ? MyTrack->uneven_underway() : track_uneven::amounts {} ) }; if( iVel > 0.5 ) { // acceleration-driven base shake shakevector += glm::dvec3( - uneven ? 0.0 : -MoverParameters->AccN * Timedelta * 5.0 * Global.ShakingMultiplierRL, // highlight side sway + replaced.sway ? 0.0 : -MoverParameters->AccN * Timedelta * 5.0 * Global.ShakingMultiplierRL, // highlight side sway -MoverParameters->AccVert * Timedelta * Global.ShakingMultiplierUD, -MoverParameters->AccSVBased * Timedelta * 1.5 * Global.ShakingMultiplierBF); // accent acceleration/deceleration // unevenness of the track under the outer axles @@ -8037,7 +8036,7 @@ TDynamicObject::update_shake( double const Timedelta ) { auto shake { 1.25 * ShakeSpring.ComputateForces( shakevector, ShakeState.offset ) }; - if( false == uneven && LocalRandom( iVel ) > 25.0 ) { + if( false == replaced.jolts && LocalRandom( iVel ) > 25.0 ) { // extra shake at increased velocity shake += ShakeSpring.ComputateForces( glm::dvec3( @@ -8066,11 +8065,12 @@ TDynamicObject::update_shake( double const Timedelta ) { bool TDynamicObject::axle_passed_joint( axle_sounds &Axle, double const Move, int const Direction ) const { - // by the outer axle on its side of the vehicle: the two can be on different tracks - auto const &outer { Axle.offset >= 0.0 ? Axle0 : Axle1 }; - if( auto const railphase { outer.RailPhase( Axle.offset - std::copysign( 0.5 * fAxleDist, Axle.offset ) ) }; railphase >= 0.0 ) { + // from the follower on its side of the vehicle, as the two can be on different tracks. They stand fAxleDist apart, at the bogie pivots, and + // the offset of the axle is the place of its wheel set, so the two of a bogie come out half of ADist to either side of the pivot + auto const &follower { Axle.offset >= 0.0 ? Axle0 : Axle1 }; + if( auto const railphase { follower.RailPhase( Axle.offset - std::copysign( 0.5 * fAxleDist, Axle.offset ) ) }; railphase >= 0.0 ) { // the rails under the axle, which aren't always those under the track the vehicle is on - auto const passed { Axle.atjoints && std::abs( railphase - Axle.distance ) > 0.5 * outer.GetTrack()->fSoundDistance }; + auto const passed { Axle.atjoints && std::abs( railphase - Axle.distance ) > 0.5 * follower.GetTrack()->fSoundDistance }; Axle.distance = railphase; Axle.atjoints = true; return passed; diff --git a/world/Segment.h b/world/Segment.h index 099480e36..05522e602 100644 --- a/world/Segment.h +++ b/world/Segment.h @@ -59,7 +59,8 @@ class TSegment int path { 0 }; // which path of a switch // where the path meets the next one: the middle of their ends and their common vector across std::array>, 2> joints; - } m_setting; + }; + setting m_setting; double fLength { -1.0 }; // długość policzona std::vector fTsBuffer; // wartości parametru krzywej t dla równych odcinków s std::vector fTsSlope; // nachylenie dt/du (= dt/ds * fStep) w tych samych punktach diff --git a/world/Track.cpp b/world/Track.cpp index 49138b4eb..c9c7ae36d 100644 --- a/world/Track.cpp +++ b/world/Track.cpp @@ -1375,7 +1375,7 @@ glm::vec3 TTrack::get_nearest_point(const glm::dvec3 &point) const // wypełnianie tablic VBO void TTrack::rebuild_geometry( gfx::geometrybank_handle const &Bank ) { - ++s_layoutstamp; + m_drawnrun = {}; Geometry1.clear(); Geometry2.clear(); @@ -1392,6 +1392,9 @@ void TTrack::rebuild_geometry( gfx::geometrybank_handle const &Bank ) { void TTrack::create_geometry( gfx::geometrybank_handle const &Bank ) { gfx::userdata_array empty_userdata; share_joints(); + if( rail_phase( 0.0 ) >= 0.0 ) { + m_drawnrun = m_railrun; + } // bake per-instance sleeper transforms now that the owning cell has assigned m_origin. // safe to call here even if the track has no sleepermodel (early-outs internally). if( m_sleeper_enabled && m_sleeper_local_transforms.empty() ) { diff --git a/world/Track.h b/world/Track.h index 72cbd8847..dc6be7412 100644 --- a/world/Track.h +++ b/world/Track.h @@ -348,6 +348,8 @@ public: // the unevenness the track declares, on a path of a switch track_uneven::amounts uneven_declared( int const Path = 0 ) const; bool is_uneven() const; + // the unevenness along what the vehicles take now: the path set on a switch, with what the switches joined and the tracks by them allow + track_uneven::amounts uneven_underway(); // the unevenness at a distance along a path, meeting the tracks joined at its ends track_uneven::amounts uneven_at( double const Distance, int const Path = 0 ); // how much of the displacement a point of the profile at the depth takes @@ -358,6 +360,10 @@ public: double joint_dip( double const Distance ); // m // distance from the rail joint behind; negative: the track has no joints that dip double rail_phase( double const Distance ); + // whether the rail joints are elsewhere than the geometry of the track has them + bool rail_joints_moved(); + // the uneven tracks whose values go by the switches among the given: all the switches joined to those, and the tracks they meet + static std::vector switch_followers( std::vector const &Tracks ); void ConnectionsLog(); bool DoubleSlip() const; static void fetch_default_profiles(); @@ -397,6 +403,7 @@ private: }; void settle_rail_run(); rail_run m_railrun; + rail_run m_drawnrun; // as the geometry has it; stamp -1: it has no joints that dip // the value along each path of a switch: the lowest of that path in the switches joined, 0 next to a track with none. // And at the ends of the paths, by what meets there: the other path at the common end, the path of a switch joined struct switch_stray { @@ -411,10 +418,10 @@ private: // the lowest value of the paths ending at the point track_uneven::amounts switch_paths_at( glm::dvec3 const &Point ) const; // what a plain track comes to at its end, where it meets the other one - track_uneven::amounts uneven_met( TTrack *Other, glm::dvec3 const &Point ); + track_uneven::amounts uneven_met( TTrack *Other, glm::dvec3 const &Point ) const; // the value of a switch where a track joined to it meets it track_uneven::amounts switch_uneven_at( glm::dvec3 const &Point ); - static int s_layoutstamp; // bumped when tracks are joined or changed, invalidates the two above + static int s_layoutstamp; // bumped when tracks are joined or changed, invalidates the two above. Once per change: a run is walked again after each void create_road_profile( gfx::vertex_array &Output, bool const Forcetransition = false ); void create_road_side_profile( gfx::vertex_array &Right, gfx::vertex_array &Left, gfx::vertex_array const &Road, bool const Forcetransition = false ); /// diff --git a/world/TrackUneven.cpp b/world/TrackUneven.cpp index 78505ac3a..10bf48d62 100644 --- a/world/TrackUneven.cpp +++ b/world/TrackUneven.cpp @@ -10,6 +10,8 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "world/Track.h" +#include + // The part of TTrack that goes with the unevenness of a track and with what it meets at its ends: joined tracks and switches. // The rules themselves are in TrackUneven.h, here they get applied to the tracks. @@ -64,7 +66,7 @@ void keep_to_plain_track( std::array &Values, TTrack c } // the value along each path: the lowest of that path in the group, 0 in a component that a plain track joined has none of -std::array group_path_values( std::vector const &Group ) { +std::array group_path_values( std::span const Group ) { std::array values { Group.front()->uneven_declared( 0 ), Group.front()->uneven_declared( 1 ) }; for( auto const *track : Group ) { @@ -173,6 +175,11 @@ bool TTrack::is_uneven() const { return any( uneven_declared( 0 ) ) || any( uneven_declared( 1 ) ) || joint_depth() > 0.0; } +track_uneven::amounts TTrack::uneven_underway() { + + return is_switch( this ) ? switch_uneven().paths[ SwitchExtension->CurrentIndex ] : uneven_declared(); +} + double TTrack::uneven_bed_share( double const Depth ) const { return track_uneven::bed_share( Depth, std::abs( track_rail_profile( m_profile1.second ).front().position.y ), fTexHeight1 ); @@ -187,7 +194,7 @@ track_uneven::amounts TTrack::uneven_at( double const Distance, int const Path ) return track_uneven::along( uneven_declared(), uneven_met( trPrev, end_point( *Segment, 0 ) ), uneven_met( trNext, end_point( *Segment, 1 ) ), Distance, Length() ); } -track_uneven::amounts TTrack::uneven_met( TTrack *Other, glm::dvec3 const &Point ) { +track_uneven::amounts TTrack::uneven_met( TTrack *Other, glm::dvec3 const &Point ) const { if( Other == nullptr ) { return {}; @@ -278,6 +285,32 @@ double TTrack::rail_phase( double const Distance ) { return track_uneven::rail_phase( m_railrun.origin + m_railrun.way * Distance, fSoundDistance ); } +bool TTrack::rail_joints_moved() { + + return m_drawnrun.stamp != -1 + && rail_phase( 0.0 ) >= 0.0 + && ( m_railrun.origin != m_drawnrun.origin || m_railrun.way != m_drawnrun.way ); +} + +std::vector TTrack::switch_followers( std::vector const &Tracks ) { + + std::vector followers; + auto const follow = [ &followers ]( TTrack *Track ) { + if( Track != nullptr && Track->is_uneven() && std::ranges::find( followers, Track ) == followers.end() ) { + followers.push_back( Track ); + } }; + for( auto *track : Tracks ) { + if( false == is_switch( track ) ) { + continue; + } + for( auto *member : switch_group( *track ) ) { + follow( member ); + std::ranges::for_each( joined_to_switch( *member ), follow ); + } + } + return followers; +} + void TTrack::settle_rail_run() { // the next track of the run past an end of a track, and which of its ends is joined there diff --git a/world/TrackUneven.h b/world/TrackUneven.h index 2c32ad660..8e0f07750 100644 --- a/world/TrackUneven.h +++ b/world/TrackUneven.h @@ -43,6 +43,18 @@ inline amounts least( amounts const &Left, amounts const &Right ) { return { std::min( Left.up, Right.up ), std::min( Left.side, Right.side ), std::min( Left.cross, Right.cross ) }; } +// which of the other sources of the cab shake the ride on a track stands in for +struct shake_sources { + bool sway { false }; // from the lateral acceleration + bool jolts { false }; // drawn at random +}; + +// sideways only with something going sideways. Cross level turns the body, and the dips at the joints come and go: neither makes up for any of them +inline shake_sources replaced_shake( amounts const &Amount ) { + + return { Amount.side > 0.0, Amount.up > 0.0 || Amount.side > 0.0 }; +} + struct wave { double x; double z; From 6b82f1471aaf38b7417f28c018aa3e1e958416fc Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Mon, 2 Nov 2026 12:36:11 +0100 Subject: [PATCH 19/90] AI braking: take power off when holding the speed uphill near a stop Within fMaxProximityDist of a stop, going uphill, the AI holds its speed with AccDesired = min(0, AccPreferred). control_tractive_force only cut the power uphill when AccDesired < 0, so an AccDesired of exactly 0 kept the controller where it was. A consist that came into that range under power kept full power and crept up to the signal. Holding the speed uphill on the way to a stop now asks for no speeding up: power comes off once the train is faster than AccDesired allows. With the current brake valves this rarely shows: on a 10 per mille climb, four real-time runs of 32 consists each, the E186 with 25 empty 408W drew power in 0-14 samples of the last 50 m before and 0-8 after, no overruns either way. It surfaced once the AI predicted the ESt3 brake closer to what the valve delivers, where the same train crept to 12 km/h from 40 m out and overran the signal; with this change it stopped short in all runs. --- vehicle/Driver.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 2c59ddcda..436cdb7f6 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -8099,7 +8099,9 @@ void TController::control_tractive_force() { } else if( velocity > VelDesired + SpeedCtrlMargin || ( fAccGravity < -0.01 ? - AccDesired < 0.0 : + AccDesired < 0.0 + // holding the speed uphill on the way to a stop asks for no speeding up + || ( VelNext == 0.0 && ActualProximityDist <= fMaxProximityDist && AbsAccS > AccDesired + 0.05 ) : AbsAccS > AccDesired + 10.05 ) || IsAnyCouplerStretched ) { // jak za bardzo przyspiesza albo prędkość przekroczona From 1f3cf4fe56ee33fe38accd7903eaee401cd2d2a7 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Mon, 2 Nov 2026 16:02:06 +0100 Subject: [PATCH 20/90] AI braking: don't let the running brake test override a stop ahead adjust_desired_speed_for_braking_test runs after the target speed was worked out and overwrites AccDesired. In its third phase it holds the deceleration near fAccThreshold until the speed has dropped by 8 km/h. On a 20 per mille descent that application barely holds the speed, so the phase can last minutes, and a stop signal coming up meanwhile is ignored. In one real-time run an E186 with 19 container wagons started the test 1290 m before a stop signal, lost 7 km/h by the signal and overran it by 274 m after the emergency application. While the test runs the AI now uses whichever of the two asks for the harder braking: the test may brake harder than the route needs, never softer. Measured in real time, four runs of 32 consists each on a 20 per mille descent, on the flat and on the descent with half the rail adhesion: the test no longer runs within 600 m of a stop signal (before: down to 414 m on this branch, and 14 m with the overrun above in a build with other brake changes), no overruns, stops 8-15 m short of the signal 86, 117 and 83 of 128 (before 90, 117 and 78). --- vehicle/Driver.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 436cdb7f6..6d6c1532e 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -7927,6 +7927,10 @@ TController::adjust_desired_speed_for_braking_test() { if( false == AIControllFlag ) { return; } // TBD: add notification about braking test and enable it for human driver as well? auto const vel { DirectionalVel() }; + // what the route ahead asks for; the test may brake harder than that, never softer + auto const routeaccdesired { AccDesired }; + auto const routeaccdesiredgravity { fAccDesiredGravity }; + auto const routeveldesired { VelDesired }; switch( DynamicBrakeTest ) { case 0: { @@ -7993,6 +7997,14 @@ TController::adjust_desired_speed_for_braking_test() { break; } } + // a stop or a restriction coming up while the test runs takes precedence + if( ( DynamicBrakeTest >= 1 ) + && ( DynamicBrakeTest <= 3 ) + && ( routeaccdesired < AccDesired ) ) { + AccDesired = routeaccdesired; + fAccDesiredGravity = routeaccdesiredgravity; + VelDesired = std::min( VelDesired, routeveldesired ); + } } void From 18bc7e06bfe58a4efff593dc3245a27289605ed3 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Mon, 2 Nov 2026 16:43:16 +0100 Subject: [PATCH 21/90] AI braking: move the uphill power-off check into its own function The condition added for holding the speed uphill near a stop pushed the cognitive complexity of control_tractive_force() to 26, over the 25 SonarCloud allows. It now lives in is_too_fast_uphill(); the behaviour is unchanged. --- vehicle/Driver.cpp | 13 ++++++++----- vehicle/Driver.h | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 6d6c1532e..2311ffacf 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -8110,11 +8110,7 @@ void TController::control_tractive_force() { cue_action( driver_hint::mastercontrollersetzerospeed ); } else if( velocity > VelDesired + SpeedCtrlMargin - || ( fAccGravity < -0.01 ? - AccDesired < 0.0 - // holding the speed uphill on the way to a stop asks for no speeding up - || ( VelNext == 0.0 && ActualProximityDist <= fMaxProximityDist && AbsAccS > AccDesired + 0.05 ) : - AbsAccS > AccDesired + 10.05 ) + || ( fAccGravity < -0.01 ? is_too_fast_uphill() : AbsAccS > AccDesired + 10.05 ) || IsAnyCouplerStretched ) { // jak za bardzo przyspiesza albo prędkość przekroczona // dodany wyjatek na "pelna w przod" @@ -8126,6 +8122,13 @@ void TController::control_tractive_force() { SpeedSet(); // ciągla regulacja prędkości } +bool TController::is_too_fast_uphill() const { + + if( AccDesired < 0.0 ) { return true; } + // holding the speed uphill on the way to a stop asks for no speeding up + return ( VelNext == 0.0 && ActualProximityDist <= fMaxProximityDist && AbsAccS > AccDesired + 0.05 ); +} + void TController::increase_tractive_force() { if( fActionTime < 0.0 ) { return; } // gdy jest nakaz poczekać z jazdą, to nie ruszać diff --git a/vehicle/Driver.h b/vehicle/Driver.h index af97b4a1e..11a01517f 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -358,6 +358,7 @@ private: void control_motor_connectors(); void control_tractive_force(); void increase_tractive_force(); + bool is_too_fast_uphill() const; void control_braking_force(); // how far short of a stop the AI aims: in train running the middle of the 8-15 m window before a signal, // otherwise (a W4 stop point, shunting, coupling) half the minimum proximity distance, as before From bb43f559e8ede590142d5383e4686e8d3d563f6c Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Mon, 2 Nov 2026 17:01:21 +0100 Subject: [PATCH 22/90] ESt3: spring preload, minimum stage, releaser time and AL2 timing Corrections to the TNESt3 distributor, checked against the ESt3 data and acceptance requirements (orange book tables 3-5, 3-7 and 3-9) and discussed with youBy: - The main control device settles at (control reservoir - pipe - 0.21) times the ratio instead of using a 0.05 bar offset. The 0.21 bar is the spring preload; without it the first stage came 0.2 bar of pipe drop too early. BVM is derived from the same offset, so a full service drop still gives MaxBP. - When the valve brakes it gives at least the minimum stage of 0.28 bar (acceptance tests 12 and 22 require 0.25-0.30 bar); when the target drops to zero the cylinder vents completely. - The automatic releaser vents the 15 l control reservoir from 5.0 to 0.4 bar in about 23 s (test 27: 23 +/- 3 s) instead of 5.6 s. - 12" cylinder fill nozzle dTN is 1.62, not 1.65 (typo). - With the AL2 relay the impulse chamber is 1 l and the cylinder nozzles are scaled so that the fill and release times meet table 3-9; before, the AL2 timings had been set by eye. On a valve test bench running the acceptance procedures, the number of results out of tolerance drops from 77 to 40. The remaining ones need parts that are not modelled yet (accelerator venting, ZP/ZS filling, intermediate chamber). --- McZapkie/Oerlikon_ESt.cpp | 44 +++++++++++++++++++++++++++++---------- McZapkie/Oerlikon_ESt.h | 1 + 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/McZapkie/Oerlikon_ESt.cpp b/McZapkie/Oerlikon_ESt.cpp index eb3a9ad4b..f37dc69f9 100644 --- a/McZapkie/Oerlikon_ESt.cpp +++ b/McZapkie/Oerlikon_ESt.cpp @@ -16,6 +16,11 @@ Copyright (C) 2007-2014 Maciej Cierniak #include "Oerlikon_ESt.h" #include "utilities/utilities.h" +// wstepny nacisk sprezyn na glowny przyrzad rozrzadczy, wyrazony jako roznica cisnien ZS - PG [bar] +static double const dPSprezyny = 0.21; +// najmniejszy stopien hamowania w cylindrze [bar] (tablica 3-9, proby 12 i 22: 0,25-0,30) +static double const pStopnia = 0.28; + double d2A( double const d ) { return d * d * 0.7854 / 1000.0; @@ -218,10 +223,11 @@ double TNESt3::GetPF( double const PP, double const dt, double const Vel ) // pr CheckReleaser(dt); // luzowanie + double const cel{ CelBCP( CVP, VVP ) }; if ((BrakeStatus & b_hld) == b_off) dV = PF(0.0, BCP, Nozzles[dTO] * nastG + (1.0 - nastG) * Nozzles[dOO]) - * dt * (0.1 + 4.9 * std::min(0.2, BCP - ((CVP - 0.05 - VVP) * BVM + 0.1))); + * dt * (0.1 + 4.9 * std::min(0.2, BCP - (cel > 0.0 ? cel + 0.1 : 0.0))); // bez stopnia luzuje do zera else dV = 0.0; // BrakeCyl.Flow(-dV); @@ -229,7 +235,7 @@ double TNESt3::GetPF( double const PP, double const dt, double const Vel ) // pr if ( (BrakeStatus & b_on) == b_on && Przekladniki[1]->P() * HBG300 < MaxBP ) dV = PF( BVP, BCP, Nozzles[dTN] * (nastG + 2.0 * (BCP < Podskok ? 1.0 : 0.0)) + Nozzles[dON] * (1.0 - nastG) ) - * dt * (0.1 + 4.9 * std::min( 0.2, (CVP - 0.05 - VVP) * BVM - BCP ) ); + * dt * (0.1 + 4.9 * std::min( 0.2, cel - BCP ) ); else dV = 0; // BrakeCyl.Flow(-dV); @@ -313,7 +319,7 @@ void TNESt3::Init( double const PP, double const HPP, double const LPP, double c Miedzypoj->CreateCap(5.0); Miedzypoj->CreatePress(PP); - BVM = 1.0 / (HPP - 0.05 - LPP) * MaxBP; + BVM = 1.0 / (HPP - dPSprezyny - LPP) * MaxBP; BrakeDelayFlag = BDF; @@ -345,6 +351,13 @@ double TNESt3::GetCRP() // return Miedzypoj.P; } +double TNESt3::CelBCP( double const CVP, double const VVP ) const // docelowe cisnienie w cylindrze +{ + double const cel{ ( CVP - dPSprezyny - VVP ) * BVM }; + // gdy zawor hamuje, nie daje mniej niz najmniejszy stopien + return ( cel > 0.0 ? std::max( cel, pStopnia ) : cel ); +} + void TNESt3::CheckState(double const BCP, double & /*dV1*/) // glowny przyrzad rozrzadczy { double const VVP{ ValveRes->P() }; @@ -355,14 +368,14 @@ void TNESt3::CheckState(double const BCP, double & /*dV1*/) // glowny przyrzad r Przys_blok = false; // sprawdzanie stanu - // if ((BrakeStatus and 1)=1)and(BCP>0.25)then - if (VVP + 0.01 + BCP / BVM < CVP - 0.05 && Przys_blok) + double const cel{ CelBCP( CVP, VVP ) }; + if (BCP < cel - 0.01 * BVM && Przys_blok) BrakeStatus |= b_on | b_hld; // hamowanie stopniowe; - else if (VVP - 0.01 + (BCP - 0.1) / BVM > CVP - 0.05) + else if (BCP - 0.1 > cel + 0.01 * BVM) BrakeStatus &= ~( b_on | b_hld ); // luzowanie; - else if (VVP + BCP / BVM > CVP - 0.05) + else if (BCP > cel) BrakeStatus &= ~b_on; // zatrzymanie napelaniania; - else if (VVP + (BCP - 0.1) / BVM < CVP - 0.05 && BCP > 0.25) // zatrzymanie luzowania + else if (BCP - 0.1 < cel && BCP > 0.25) // zatrzymanie luzowania BrakeStatus |= b_hld; if( ( BrakeStatus & b_hld ) == 0 ) @@ -391,7 +404,7 @@ void TNESt3::CheckReleaser(double const dt) // odluzniacz // odluzniacz automatyczny if ((BrakeStatus & b_rls) == b_rls) { - CntrlRes->Flow(PF(CVP, 0, 0.02) * dt); + CntrlRes->Flow(PF(CVP, 0, 0.0049) * dt); // ZS 15 l z 5,0 do 0,4 bar w 23 s (proba odbioru nr 27) if (CVP < VVP + 0.3 || false == autom) BrakeStatus &= ~b_rls; } @@ -560,7 +573,7 @@ void TNESt3::SetSize( int const size, std::string const ¶ms ) // ustawianie { // dON,dOO,dTN,dTO,dP,dS Nozzles[dON] = 3.7; Nozzles[dOO] = 2.50; - Nozzles[dTN] = 1.65; + Nozzles[dTN] = 1.62; Nozzles[dTO] = 1.39; Nozzles[dP] = 2.65; Nozzles[dPd] = 1.80; @@ -649,6 +662,14 @@ void TNESt3::SetSize( int const size, std::string const ¶ms ) // ustawianie Nozzles[dSd] = 1.1; Nozzles[dSm] = 0.9; + if( PrzekCiagly != nullptr ) { + // dysze cylindra skalibrowane na komore impulsowa przekladnika AL2 (1 l) wg czasow z tablicy 3-9 + Nozzles[dON] *= 0.914; + Nozzles[dTN] *= 0.885; + Nozzles[dOO] *= 0.82; + Nozzles[dTO] *= 0.8; + } + // przeliczanie z mm^2 na l/m for (auto &nozzle : Nozzles) { @@ -657,7 +678,8 @@ void TNESt3::SetSize( int const size, std::string const ¶ms ) // ustawianie for (int i = 1; i < 4; ++i) { Przekladniki[i]->BrakeRes = BrakeRes; - Przekladniki[i]->CreateCap(i); + // komora impulsowa przekladnika AL2 ma 1 l + Przekladniki[i]->CreateCap( ( Przekladniki[i] == PrzekCiagly ) ? 1.0 : i ); Przekladniki[i]->CreatePress(BrakeCyl->P()); if (i < 3) Przekladniki[i]->Next = Przekladniki[i + 1]; diff --git a/McZapkie/Oerlikon_ESt.h b/McZapkie/Oerlikon_ESt.h index 9cdffe7bc..01aefb7f2 100644 --- a/McZapkie/Oerlikon_ESt.h +++ b/McZapkie/Oerlikon_ESt.h @@ -196,6 +196,7 @@ class TNESt3 : public TBrake { void EStParams(double i_crc) const; // parametry charakterystyczne dla ESt double GetCRP() override; double GetRapidRatio(double const Vel) const override; + double CelBCP(double const CVP, double const VVP) const; // docelowe cisnienie w cylindrze void CheckState(double const BCP, double &dV1); // glowny przyrzad rozrzadczy void CheckReleaser(double const dt); // odluzniacz double CVs(double const BP) const; // napelniacz sterujacego From 6374e2d32e7ef00bb938d7b79763b1aae1d57779 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Mon, 2 Nov 2026 17:01:32 +0100 Subject: [PATCH 23/90] AI braking: predict the ESt3 brake force from the distributor characteristic --- McZapkie/Mover.cpp | 1 + McZapkie/Oerlikon_ESt.cpp | 11 +++++++++++ McZapkie/Oerlikon_ESt.h | 1 + McZapkie/hamulce.h | 4 ++++ 4 files changed, 17 insertions(+) diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 429c2df17..2b2275112 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -5091,6 +5091,7 @@ void TMoverParameters::ComputeTotalForce(double dt) double TMoverParameters::BrakeForceR(double ratio, double velocity) { double press = 0; + ratio = Hamulec->GetBrakeRatio(ratio); if (MBPM > 2) { press = MaxBrakePress[1] + (MaxBrakePress[3] - MaxBrakePress[1]) * std::min(1.0, (TotalMass - Mass) / (MBPM - Mass)); diff --git a/McZapkie/Oerlikon_ESt.cpp b/McZapkie/Oerlikon_ESt.cpp index f37dc69f9..1e7a5be66 100644 --- a/McZapkie/Oerlikon_ESt.cpp +++ b/McZapkie/Oerlikon_ESt.cpp @@ -351,6 +351,17 @@ double TNESt3::GetCRP() // return Miedzypoj.P; } +double TNESt3::GetBrakeRatio( double const Ratio ) const // udzial cisnienia w cylindrze dla udzialu spadku w PG +{ + if( MaxBP <= 0.0 || BVM <= 0.0 ) { + return Ratio; + } + // pelny spadek sluzbowy w PG (HPP - LPP), odtworzony z przelozenia + double const spadek{ MaxBP / BVM + dPSprezyny }; + double const cel{ ( Ratio * spadek - dPSprezyny ) * BVM }; + return ( cel > 0.0 ? std::max( cel, pStopnia ) / MaxBP : 0.0 ); +} + double TNESt3::CelBCP( double const CVP, double const VVP ) const // docelowe cisnienie w cylindrze { double const cel{ ( CVP - dPSprezyny - VVP ) * BVM }; diff --git a/McZapkie/Oerlikon_ESt.h b/McZapkie/Oerlikon_ESt.h index 01aefb7f2..02d65a285 100644 --- a/McZapkie/Oerlikon_ESt.h +++ b/McZapkie/Oerlikon_ESt.h @@ -197,6 +197,7 @@ class TNESt3 : public TBrake { double GetCRP() override; double GetRapidRatio(double const Vel) const override; double CelBCP(double const CVP, double const VVP) const; // docelowe cisnienie w cylindrze + double GetBrakeRatio(double const Ratio) const override; // udzial cisnienia w cylindrze dla udzialu spadku w PG void CheckState(double const BCP, double &dV1); // glowny przyrzad rozrzadczy void CheckReleaser(double const dt); // odluzniacz double CVs(double const BP) const; // napelniacz sterujacego diff --git a/McZapkie/hamulce.h b/McZapkie/hamulce.h index 5b5f8933c..37b9b6a4d 100644 --- a/McZapkie/hamulce.h +++ b/McZapkie/hamulce.h @@ -421,6 +421,10 @@ class TBrake double GetVRP() const; // cisnienie komory wstepnej rozdzielacza /// Returns control reservoir (ZS) pressure [bar]; defaults to the auxiliary reservoir for valves without a dedicated ZS. virtual double GetCRP(); // cisnienie zbiornika sterujacego + /// Share of the full cylinder pressure the distributor settles at for the given share + /// of the full service pipe drop. Used only to predict the brake force; the default is proportional. + /// Share of the full service brake pipe drop. + virtual double GetBrakeRatio(double const Ratio) const { return Ratio; } /// /// Sets the brake delay (G/P/R/M) selector if requested mode is supported. /// From b34238f33fd49a82e9bc68380959335c890343ab Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 3 Nov 2026 21:45:57 +0100 Subject: [PATCH 24/90] fix(app): the log written to the end of the closing, each step of it with its time; the Discord thread idle outside the driver mode The log service stopped with applicationQuitOrder, so whatever was logged while the simulator closed was lost; it now runs until StopLogService() and writes out what came before. The closing logs each of its steps with the time since it began, to tell where a closing that hangs stops. Outside the driver mode the Discord thread spun a core in its loop, it now sleeps a second between the checks. --- application/application.cpp | 16 ++++++++++++++++ nodebank_previews_standalone.cpp | 4 ++-- utilities/Logs.cpp | 11 ++++++++++- utilities/Logs.h | 2 ++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/application/application.cpp b/application/application.cpp index 119cb021e..56a83c29c 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -270,7 +270,11 @@ void eu07_application::DiscordRPCService() } if (currentMode != mode::driver) + { + // the editor and the other modes: nothing to tell, without spinning a core on it + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); continue; + } // Discord RPC updater if (simulation::is_ready) @@ -690,19 +694,29 @@ void eu07_application::release_python_lock() void eu07_application::exit() { Global.applicationQuitOrder = true; + // each step of the closing in the log, to tell from it where a closing that hangs stops + auto const closing{std::chrono::steady_clock::now()}; + auto const step = [&](char const *What) { + WriteLog(std::string{"Closing: "} + What + " at " + std::to_string(std::chrono::duration_cast(std::chrono::steady_clock::now() - closing).count()) + " ms"); + }; + step("the modes"); for (auto &mode : m_modes) mode.reset(); + step("the renderer"); GfxRenderer->Shutdown(); m_network.reset(); // SafeDelete( simulation::Train ); + step("the scenery"); SafeDelete(simulation::Region); + step("the ui and the python workers"); ui_layer::shutdown(); // the python workers detach their GL context on the way out, so they have to be done before the windows they use are destroyed m_taskqueue.exit(); + step("the windows"); for (auto *window : m_windows) { glfwDestroyWindow(window); @@ -713,6 +727,8 @@ void eu07_application::exit() if (!Global.exec_on_exit.empty()) system(Global.exec_on_exit.c_str()); + step("done"); + StopLogService(); auto it = Global.threads.find("LogService"); if (it != Global.threads.end()) { diff --git a/nodebank_previews_standalone.cpp b/nodebank_previews_standalone.cpp index 39884fd38..d94eb13ed 100644 --- a/nodebank_previews_standalone.cpp +++ b/nodebank_previews_standalone.cpp @@ -290,9 +290,9 @@ int generate_nodebank_previews(int Argc, char *Argv[]) std::thread logservice(LogService); auto const finish = [&](int const Result) { - // give the log service time to write out what's left - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + // the log service writes out what's left before it stops Global.applicationQuitOrder = true; + StopLogService(); logservice.join(); return Result; }; diff --git a/utilities/Logs.cpp b/utilities/Logs.cpp index 38f8062b9..18020d6f1 100644 --- a/utilities/Logs.cpp +++ b/utilities/Logs.cpp @@ -73,12 +73,21 @@ std::deque ErrorStack; // lock for log stacks std::mutex logMutex; +// the log is written through the closing of the simulator, to the end +std::atomic logStop{false}; + +void StopLogService() +{ + logStop = true; +} void LogService() { - while (!Global.applicationQuitOrder) + // one more pass after the stop, so what came just before it gets written too + for (bool last{false}; false == last;) { + last = logStop; { // --- Obsługa InfoStack --- while (!InfoStack.empty()) diff --git a/utilities/Logs.h b/utilities/Logs.h index 93276d651..63727de9c 100644 --- a/utilities/Logs.h +++ b/utilities/Logs.h @@ -23,7 +23,9 @@ enum class logtype : unsigned int { traction = 1 << 9, powergrid = 1 << 10, }; +// writes the log until StopLogService(), and all of it that came before void LogService(); +void StopLogService(); void WriteLog( const char *str, logtype const Type = logtype::generic, bool isError = false ); void Error( const std::string &asMessage, bool box = false ); void Error( const char* &asMessage, bool box = false ); From e75d8496797732cb2463956f3986f022529bdb6b Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 3 Nov 2026 21:45:57 +0100 Subject: [PATCH 25/90] fix(app): a scenery file named in another case than given is found, a scenario that can't be opened goes back to the launcher The command line gives the name of the scenery lowercased, and the sceneries made on Windows name their files and includes in any case: on a case-sensitive filesystem they weren't found. FileCased() finds the file whose name differs only in case; the parser and the scenario loader use it. A scenario that can't be opened logs the reason and goes back to the launcher instead of ending with an exception. --- application/scenarioloadermode.cpp | 16 +++++++++++++++- utilities/parser.cpp | 9 +++++++++ utilities/utilities.cpp | 14 ++++++++++++++ utilities/utilities.h | 3 +++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/application/scenarioloadermode.cpp b/application/scenarioloadermode.cpp index dbdce6068..7afe0a442 100644 --- a/application/scenarioloadermode.cpp +++ b/application/scenarioloadermode.cpp @@ -41,11 +41,25 @@ bool scenarioloader_mode::update() { WriteLog("using simulation seed: " + std::to_string(Global.random_seed), logtype::generic); WriteLog("using simulation starting timestamp: " + std::to_string(Global.starting_timestamp), logtype::generic); + // the command line gives the name lowercased, the file may be named in another case, as a scenery made on Windows; + // the scenery is saved back to its own file then + if (auto const cased{FileCased(Global.asCurrentSceneryPath + Global.SceneryFile)}; cased.size() > Global.asCurrentSceneryPath.size()) + Global.SceneryFile = cased.substr(Global.asCurrentSceneryPath.size()); Application.set_title( Global.AppName + " (" + Global.SceneryFile + ")" ); WriteLog( "\nLoading scenario \"" + Global.SceneryFile + "\"..." ); timestart = std::chrono::system_clock::now(); - state = simulation::State.deserialize_begin(Global.SceneryFile); + try { + state = simulation::State.deserialize_begin(Global.SceneryFile); + } + catch (std::exception const &e) { + ErrorLog("Bad init: scenario \"" + Global.SceneryFile + "\" couldn't be opened (" + e.what() + ")"); + state.reset(); + Global.SceneryFile.clear(); + Application.pop_mode(); + Application.push_mode(eu07_application::mode::launcher); + return true; + } } try { diff --git a/utilities/parser.cpp b/utilities/parser.cpp index b64aee39b..ee5a9ce5b 100644 --- a/utilities/parser.cpp +++ b/utilities/parser.cpp @@ -63,6 +63,15 @@ cParser::cParser(std::string const &Stream, buffertype const Type, std::string P // the file is read in one go, tokens are then extracted straight from the memory std::ifstream file(Path, std::ios_base::binary); if (file.fail()) + { + // named in another case than it's on the disk + if (auto const cased{FileCased(Path)}; false == cased.empty()) + { + file.clear(); + file.open(cased, std::ios_base::binary); + } + } + if (file.fail()) { mFailBit = true; } diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index 02608262b..0c18e0027 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -471,6 +471,20 @@ bool FileExists(std::string const &Filename) return std::filesystem::exists(Filename); } +std::string FileCased(std::string const &Filename) +{ + std::error_code error; + if (std::filesystem::exists(Filename, error)) + return Filename; + std::filesystem::path const path{Filename}; + auto const directory{path.has_parent_path() ? path.parent_path() : std::filesystem::path{"."}}; + auto const wanted{ToLower(path.filename().string())}; + for (std::filesystem::directory_iterator entry{directory, error}, end; entry != end; entry.increment(error)) + if (ToLower(entry->path().filename().string()) == wanted) + return path.has_parent_path() ? (directory / entry->path().filename()).string() : entry->path().filename().string(); + return {}; +} + std::pair FileExists(std::vector const &Names, std::vector const &Extensions) { diff --git a/utilities/utilities.h b/utilities/utilities.h index 5392ba6c5..d98b7bc9a 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -217,6 +217,9 @@ template bool extract_value(Type_ &Variable, std::string const template <> bool extract_value(bool &Variable, std::string const &Key, std::string const &Input, std::string const &Default); bool FileExists(std::string const &Filename); +// the file as it's named on the disk when only the case of its name differs, as in the sceneries made on Windows; empty when +// there's none +std::string FileCased(std::string const &Filename); std::pair FileExists(std::vector const &Names, std::vector const &Extensions); From 0d69059448b84d9b545ce0a4812c823fc7f60ec9 Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 3 Nov 2026 21:45:58 +0100 Subject: [PATCH 26/90] fix(launcher): the scans of the sceneries and the vehicles go past folders they can't read, a vehicle dropped without its skin set isn't added A folder without the permission to read it or a name that can't be converted ended the scan with an exception. Ctrl+Delete with no trainset selected, a dropped skin whose vehicle is gone, and a description with a % in it no longer crash the launcher. --- launcher/scenery_list.cpp | 28 ++++++++++++++++------------ launcher/scenery_scanner.cpp | 16 ++++++++++++---- launcher/textures_scanner.cpp | 9 +++++---- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/launcher/scenery_list.cpp b/launcher/scenery_list.cpp index d95c962b5..195b0e2c3 100644 --- a/launcher/scenery_list.cpp +++ b/launcher/scenery_list.cpp @@ -23,7 +23,7 @@ bool ui::scenerylist_panel::on_key(int key, int action) if (it == keyboard.keytonamemap.end()) return false; - if (Global.ctrlState && it->second == "delete") + if (Global.ctrlState && it->second == "delete" && selected_trainset != nullptr) purge_selected_trainset(); return true; @@ -117,7 +117,7 @@ void ui::scenerylist_panel::draw_launch_box() { ImGui::NextColumn(); - ImGui::TextWrapped(selected_trainset->description.c_str()); + ImGui::TextWrapped("%s", selected_trainset->description.c_str()); ImGui::Checkbox(STR_C("Trainset turned around"), &selected_trainset->reversed); if (ImGui::IsItemHovered()) @@ -283,7 +283,8 @@ void ui::scenerylist_panel::draw_trainset(trainset_desc &trainset) draw_droptarget(trainset, position++); ImGui::SameLine(15.0f); - for (auto &dyn_desc : trainset.vehicles) { + for (std::size_t index = 0; index < trainset.vehicles.size(); ++index) { + auto &dyn_desc = trainset.vehicles[index]; deferred_image *mini = nullptr; if (dyn_desc.skin && dyn_desc.skin->mini.get() != deferred_image::invalid_id) @@ -345,16 +346,19 @@ void ui::scenerylist_panel::draw_droptarget(trainset_desc &trainset, int positio if (payload) { skin_set *ptr = *reinterpret_cast(payload->Data); std::shared_ptr skin; - for (auto &s : ptr->vehicle.lock()->matching_skinsets) - if (s.get() == ptr) - skin = s; + if (auto const vehicle = ptr->vehicle.lock()) + for (auto &s : vehicle->matching_skinsets) + if (s.get() == ptr) + skin = s; - trainset.vehicles.emplace(trainset.vehicles.begin() + position); - dynamic_desc &desc = trainset.vehicles[position]; + if (skin) { + trainset.vehicles.emplace(trainset.vehicles.begin() + position); + dynamic_desc &desc = trainset.vehicles[position]; - desc.name = skin->skin + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0)); - desc.vehicle = skin->vehicle.lock(); - desc.skin = skin; + desc.name = skin->skin + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0)); + desc.vehicle = skin->vehicle.lock(); + desc.skin = skin; + } } payload = ImGui::AcceptDragDropPayload("vehicle_set"); @@ -368,7 +372,7 @@ void ui::scenerylist_panel::draw_droptarget(trainset_desc &trainset, int positio if (moved->position < position) offset = -1; } else { - desc_copy.name = desc_copy.skin->skin + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0)); + desc_copy.name = (desc_copy.skin ? desc_copy.skin->skin : desc_copy.name) + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0)); } trainset.vehicles.insert(trainset.vehicles.begin() + position + offset, desc_copy); diff --git a/launcher/scenery_scanner.cpp b/launcher/scenery_scanner.cpp index bcd18ac90..5f6a6d2f9 100644 --- a/launcher/scenery_scanner.cpp +++ b/launcher/scenery_scanner.cpp @@ -10,13 +10,21 @@ scenery_scanner::scenery_scanner(ui::vehicles_bank &bank) void scenery_scanner::scan() { - for (auto &f : std::filesystem::directory_iterator("scenery")) { - std::filesystem::path path(std::filesystem::relative(f.path(), "scenery/")); + std::error_code error; + for (std::filesystem::directory_iterator f{"scenery", std::filesystem::directory_options::skip_permission_denied, error}, end; f != end; f.increment(error)) { + std::filesystem::path path(std::filesystem::relative(f->path(), "scenery/", error)); + std::string name; + try { + name = path.string(); + } + catch (std::exception const &) { + continue; + } - if (*path.filename().string().begin() == '$') + if (name.empty() || path.filename().string().front() == '$') continue; - if (path.string().ends_with(".scn")) + if (name.ends_with(".scn")) scan_scn(path); } diff --git a/launcher/textures_scanner.cpp b/launcher/textures_scanner.cpp index 524a23ca6..741f978ca 100644 --- a/launcher/textures_scanner.cpp +++ b/launcher/textures_scanner.cpp @@ -4,12 +4,13 @@ void ui::vehicles_bank::scan_textures() { - for (auto &f : std::filesystem::recursive_directory_iterator("dynamic")) { - if (f.path().filename() != "textures.txt") + std::error_code error; + for (std::filesystem::recursive_directory_iterator f{"dynamic", std::filesystem::directory_options::skip_permission_denied, error}, end; f != end; f.increment(error)) { + if (f->path().filename() != "textures.txt") continue; - std::fstream stream(f.path(), std::ios_base::binary | std::ios_base::in); - ctx_path = std::filesystem::relative(f.path().parent_path(), "dynamic/"); + std::fstream stream(f->path(), std::ios_base::binary | std::ios_base::in); + ctx_path = std::filesystem::relative(f->path().parent_path(), "dynamic/", error); std::string line; while (std::getline(stream, line)) From e230d7b902831b9e6fdac4eebffd73087fb4b6cd Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 3 Nov 2026 21:45:58 +0100 Subject: [PATCH 27/90] fix: a deleted model is no longer the picked node, an orthophoto tile isn't swept as a grey stub, the ED brake arrays freed Deleting a model in the editor left the renderer holding it as the picked node, the next pick used freed memory: the renderer forgets it now (Forget_Node). A texture filled straight from memory (internalsrc:) can't be read again, released by the garbage sweep it came back grey; it stays now, and the units bound with the old name are rebound. The arrays of the ED brake force sharing are vectors, they leaked once per update. The scene hierarchy and the editor menu items go through the translation. Python caches ignored. --- .gitignore | 2 ++ application/uilayer.cpp | 10 +++++----- model/Texture.cpp | 10 ++++++++++ rendering/opengl33renderer.cpp | 7 +++++++ rendering/opengl33renderer.h | 2 ++ rendering/openglrenderer.cpp | 9 +++++++++ rendering/openglrenderer.h | 2 ++ rendering/renderer.h | 2 ++ simulation/simulation.cpp | 4 ++++ vehicle/DynObj.cpp | 16 ++++------------ 10 files changed, 47 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 5186ceaee..70a6d1c68 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ vcpkg.json # Local Python (setup.bat / CMake) for embedding tools/python314/ +__pycache__/ +*.pyc diff --git a/application/uilayer.cpp b/application/uilayer.cpp index 2dd771bdd..d8417e4fe 100644 --- a/application/uilayer.cpp +++ b/application/uilayer.cpp @@ -567,7 +567,7 @@ void ui_layer::render_hierarchy(){ // fitted to the contents when it first appears; no auto resize, which a docked window can't do ImGui::SetNextWindowSize(ImVec2(0, 0), ImGuiCond_FirstUseEver); ImGui::Begin((std::string(STR_C("Scene Hierarchy")) + "###scenehierarchy").c_str(), &m_editor_hierarchy); - ImGui::Text("Registered nodes: %zu", scene::Hierarchy.size()); + ImGui::Text(STR_C("Registered nodes: %zu"), scene::Hierarchy.size()); ImGui::BeginChild("hierarchy_list", ImVec2(500, 300), true); for (auto &entry : scene::Hierarchy) @@ -598,7 +598,7 @@ void ui_layer::render_hierarchy(){ if (ImGui::IsItemHovered()) { - ImGui::SetTooltip("UUID: %s\nNode type: %s", uuid.c_str(), typeid(*node).name()); + ImGui::SetTooltip(STR_C("UUID: %s\nNode type: %s"),uuid.c_str(), typeid(*node).name()); } } } @@ -708,14 +708,14 @@ void ui_layer::render_menu_contents() GfxRenderer->Debug_Ui_State(ret); } if(EditorModeFlag){ - ImGui::MenuItem("Hierarchy", nullptr, &m_editor_hierarchy); + ImGui::MenuItem(STR_C("Scene Hierarchy"), nullptr, &m_editor_hierarchy); bool change_history_enabled = editor_mode::change_history(); - if (ImGui::MenuItem("Change History", nullptr, &change_history_enabled)) + if (ImGui::MenuItem(STR_C("Change History"), nullptr, &change_history_enabled)) { editor_mode::set_change_history(change_history_enabled); } bool settings_open = editor_mode::settings_open(); - if (ImGui::MenuItem("Editor Settings", nullptr, &settings_open)) + if (ImGui::MenuItem(STR_C("Editor Settings"), nullptr, &settings_open)) { editor_mode::set_settings_open(settings_open); } diff --git a/model/Texture.cpp b/model/Texture.cpp index 136846a33..4a7b24792 100644 --- a/model/Texture.cpp +++ b/model/Texture.cpp @@ -706,6 +706,16 @@ void opengl_texture::update_from_memory(size_t width, size_t height, const uint8 if (glGenerateMipmap) glGenerateMipmap(target); glFlush(); + // the content given straight to the gpu can't be read again: released by the garbage sweep, the texture would come back as + // the grey stub + if (type == "internalsrc:") + { + is_static = true; + is_ready = true; + } + // the name may be a new one, and the unit active took the texture past the unit cache + for (auto &unit : units) + unit = -1; } void diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index d20454c35..a5d1f3894 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -5410,6 +5410,13 @@ void opengl33_renderer::Pick_Node_Callback(std::functionis_busy()) diff --git a/rendering/opengl33renderer.h b/rendering/opengl33renderer.h index 64574420f..0f17ede7f 100644 --- a/rendering/opengl33renderer.h +++ b/rendering/opengl33renderer.h @@ -108,6 +108,8 @@ class opengl33_renderer : public gfx_renderer { Pick_Control() const override { return m_pickcontrolitem; } scene::basic_node const * Pick_Node() const override { return m_picksceneryitem; } + void + Forget_Node( scene::basic_node const *Node ) override; glm::dvec3 Mouse_Position() const override { return m_worldmousecoordinates; } bool diff --git a/rendering/openglrenderer.cpp b/rendering/openglrenderer.cpp index 59152c64d..864d84141 100644 --- a/rendering/openglrenderer.cpp +++ b/rendering/openglrenderer.cpp @@ -1888,6 +1888,15 @@ opengl_renderer::Pick_Node_Callback( std::function m_node_pick_requests.emplace_back( Callback ); } +void +opengl_renderer::Forget_Node( scene::basic_node const *Node ) { + + if( m_picksceneryitem == Node ) { + m_picksceneryitem = nullptr; + } + std::replace_if( m_picksceneryitems.begin(), m_picksceneryitems.end(), [Node]( scene::basic_node const *Item ) { return Item == Node; }, nullptr ); +} + void opengl_renderer::Render( scene::basic_region *Region ) { diff --git a/rendering/openglrenderer.h b/rendering/openglrenderer.h index 78fffa40b..ada032808 100644 --- a/rendering/openglrenderer.h +++ b/rendering/openglrenderer.h @@ -103,6 +103,8 @@ public: Pick_Control() const override { return m_pickcontrolitem; } scene::basic_node const * Pick_Node() const override { return m_picksceneryitem; } + void + Forget_Node( scene::basic_node const *Node ) override; glm::dvec3 Mouse_Position() const override { return m_worldmousecoordinates; } glm::mat4 diff --git a/rendering/renderer.h b/rendering/renderer.h index 0b9eb60f4..e9dc8a47c 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -90,6 +90,8 @@ public: virtual void Pick_Node_Callback( std::function Callback ) = 0; virtual auto Pick_Control() const -> TSubModel const * = 0; virtual auto Pick_Node() const -> scene::basic_node const * = 0; + // a node deleted from the scene is no longer given as picked + virtual void Forget_Node( scene::basic_node const *Node ) {} virtual auto Mouse_Position() const -> glm::dvec3 = 0; virtual auto Mouse_Hit() const -> bool { return true; } diff --git a/simulation/simulation.cpp b/simulation/simulation.cpp index 70f9e3074..ea2177170 100644 --- a/simulation/simulation.cpp +++ b/simulation/simulation.cpp @@ -30,6 +30,7 @@ http://mozilla.org/MPL/2.0/. #include "scene/scenelayers.h" #include "vehicle/Train.h" #include "application/application.h" +#include "rendering/renderer.h" #include "utilities/Logs.h" #include "vehicle/Driver.h" @@ -561,6 +562,9 @@ std::pair state_manager::rebuild_include(scene::instance_handle const } void state_manager::delete_model(TAnimModel *model) { + if (GfxRenderer) { + GfxRenderer->Forget_Node(model); + } scene::Layers.count(model->layer(), scene::layer_item::model, -1); scene::Layers.forget(model); Region->erase(model); diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index c6793e454..2309c433b 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -3381,15 +3381,12 @@ bool TDynamicObject::Update(double dt, double dt1) && MoverParameters->eimc[eimc_p_abed] < 0.001) FzadED = 0; //pętla bezpieczeństwa - bez ED auto const FzadPN = Fzad - FrED * MoverParameters->MED_FrED_factor; - //np = 0; - // BUG: likely memory leak, allocation per inner loop, deleted only once outside - // TODO: sort this shit out - bool* PrzekrF = new bool[np]; + std::vector PrzekrF( np ); float nPrzekrF = 0; bool test = true; - float* FzED = new float[np]; - float* FzEP = new float[np]; - float* FmaxEP = new float[np]; + std::vector FzED( np ); + std::vector FzEP( np ); + std::vector FmaxEP( np ); // 3. ustaw pojazdom sile hamowania ED // - proporcjonalnie do mozliwosci @@ -3548,11 +3545,6 @@ bool TDynamicObject::Update(double dt, double dt1) } - delete[] PrzekrF; - delete[] FzED; - delete[] FzEP; - delete[] FmaxEP; - MED_oldFED = FzadED; } From a24118baf7630eb0ab545c947d7874135a96356d Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 3 Nov 2026 21:50:31 +0100 Subject: [PATCH 28/90] feat(render): a geometry chunk emptied in place, the kind of its primitive told; a chunk gaining or losing its userdata gets a new buffer Erase() empties a chunk and keeps its handle to be filled again, for geometry made anew as it changes. Primitive() tells whether a chunk holds triangles or strips, -1 from a renderer which doesn't know. The VAO bank reused the buffer of a chunk of the same length even when the userdata came or went, the layout of the buffer differing then. --- rendering/geometrybank.cpp | 36 ++++++++++++++++++++++++++++++ rendering/geometrybank.h | 6 +++++ rendering/opengl33geometrybank.cpp | 3 ++- rendering/opengl33renderer.cpp | 24 +++++++++++++++++++- rendering/opengl33renderer.h | 7 ++++++ rendering/openglrenderer.cpp | 5 +++++ rendering/openglrenderer.h | 1 + rendering/renderer.h | 8 +++++++ 8 files changed, 88 insertions(+), 2 deletions(-) diff --git a/rendering/geometrybank.cpp b/rendering/geometrybank.cpp index f2db8b8bf..2a1ff5ee6 100644 --- a/rendering/geometrybank.cpp +++ b/rendering/geometrybank.cpp @@ -318,6 +318,20 @@ geometry_bank::append( gfx::vertex_array &Vertices, userdata_array &Userdata, gf return replace( Vertices, Userdata, Geometry, gfx::geometry_bank::chunk( Geometry ).vertices.size() ); } +void +geometry_bank::erase( gfx::geometry_handle const &Geometry ) { + + if( ( Geometry.chunk == 0 ) || ( Geometry.chunk > m_chunks.size() ) ) { return; } + + auto &chunk = gfx::geometry_bank::chunk( Geometry ); + if( chunk.vertices.empty() && chunk.indices.empty() ) { return; } + + gfx::vertex_array().swap( chunk.vertices ); + gfx::index_array().swap( chunk.indices ); + gfx::userdata_array().swap( chunk.userdata ); + replace_( Geometry ); +} + // draws geometry stored in specified chunk std::size_t geometry_bank::draw( gfx::geometry_handle const &Geometry, gfx::stream_units const &Units, unsigned int const Streams ) { @@ -360,6 +374,13 @@ geometry_bank::vertices( gfx::geometry_handle const &Geometry ) const { return geometry_bank::chunk( Geometry ).vertices; } +// kind of geometry of specified chunk +unsigned int +geometry_bank::primitive( gfx::geometry_handle const &Geometry ) const { + + return geometry_bank::chunk( Geometry ).type; +} + // provides direct access to vertex user data of specfied chunk userdata_array const & geometry_bank::userdata( gfx::geometry_handle const &Geometry ) const { @@ -418,6 +439,14 @@ geometrybank_manager::append( gfx::vertex_array &Vertices, userdata_array &Userd return bank( Geometry ).first->append( Vertices, Userdata, Geometry ); } + +void +geometrybank_manager::erase( gfx::geometry_handle const &Geometry ) { + + if( ( Geometry.bank == 0 ) || ( Geometry.bank > m_geometrybanks.size() ) ) { return; } + + bank( Geometry ).first->erase( Geometry ); +} // draws geometry stored in specified chunk void geometrybank_manager::draw( gfx::geometry_handle const &Geometry, unsigned int const Streams ) { @@ -483,6 +512,13 @@ geometrybank_manager::vertices( gfx::geometry_handle const &Geometry ) const { return bank( Geometry ).first->vertices( Geometry ); } +// kind of geometry of specified chunk +unsigned int +geometrybank_manager::primitive( gfx::geometry_handle const &Geometry ) const { + + return bank( Geometry ).first->primitive( Geometry ); +} + // provides direct access to vertex user data of specfied chunk gfx::userdata_array const & geometrybank_manager::userdata( gfx::geometry_handle const &Geometry ) const { diff --git a/rendering/geometrybank.h b/rendering/geometrybank.h index 20baf4ed2..537c79ba2 100644 --- a/rendering/geometrybank.h +++ b/rendering/geometrybank.h @@ -117,6 +117,7 @@ public: auto replace( gfx::vertex_array &Vertices, gfx::userdata_array& Userdata, gfx::geometry_handle const &Geometry, std::size_t const Offset = 0 ) -> bool; // adds supplied vertex data at the end of specified chunk auto append( gfx::vertex_array &Vertices, gfx::userdata_array& Userdata, gfx::geometry_handle const &Geometry ) -> bool; + void erase( gfx::geometry_handle const &Geometry ); // draws geometry stored in specified chunk auto draw( gfx::geometry_handle const &Geometry, gfx::stream_units const &Units, unsigned int const Streams = basic_streams ) -> std::size_t; // draws geometry stored in specified chunk N times via glDrawElementsInstanced* @@ -138,6 +139,8 @@ public: auto vertices( gfx::geometry_handle const &Geometry ) const -> gfx::vertex_array const &; // provides direct access to vertex user data of specfied chunk auto userdata( gfx::geometry_handle const &Geometry ) const -> gfx::userdata_array const &; + // kind of geometry of specified chunk, GL_TRIANGLES, GL_TRIANGLE_STRIP... + auto primitive( gfx::geometry_handle const &Geometry ) const -> unsigned int; protected: // types: @@ -220,6 +223,7 @@ public: auto replace( gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, std::size_t const Offset = 0 ) -> bool; // adds supplied vertex data at the end of specified chunk auto append( gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry ) -> bool; + void erase( gfx::geometry_handle const &Geometry ); // draws geometry stored in specified chunk void draw( gfx::geometry_handle const &Geometry, unsigned int const Streams = basic_streams ); // draws geometry stored in specified chunk InstanceCount times via GPU instancing. @@ -238,6 +242,8 @@ public: auto vertices( gfx::geometry_handle const &Geometry ) const -> gfx::vertex_array const &; // provides direct access to vertex data of specfied chunk auto userdata( gfx::geometry_handle const &Geometry ) const -> gfx::userdata_array const &; + // kind of geometry of specified chunk + auto primitive( gfx::geometry_handle const &Geometry ) const -> unsigned int; // sets target texture unit for the texture data stream auto units() -> gfx::stream_units & { return m_units; } // provides access to primitives count diff --git a/rendering/opengl33geometrybank.cpp b/rendering/opengl33geometrybank.cpp index 4713ec69b..978a1b75d 100644 --- a/rendering/opengl33geometrybank.cpp +++ b/rendering/opengl33geometrybank.cpp @@ -33,7 +33,8 @@ opengl33_vaogeometrybank::replace_( gfx::geometry_handle const &Geometry ) { auto &chunkrecord = m_chunkrecords[ Geometry.chunk - 1 ]; chunkrecord.is_good = false; // if the overall length of the chunk didn't change we can get away with reusing the old buffer... - if( geometry_bank::chunk( Geometry ).vertices.size() != chunkrecord.vertex_count ) { + if( ( geometry_bank::chunk( Geometry ).vertices.size() != chunkrecord.vertex_count ) + || ( geometry_bank::chunk( Geometry ).userdata.empty() == chunkrecord.has_userdata ) ) { // ...but otherwise we'll need to allocate a new one // TBD: we could keep and reuse the old buffer also if the new chunk is smaller than the old one, // but it'd require some extra tracking and work to keep all chunks up to date; also wasting vram; may be not worth it? diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index d20454c35..97b2fa8d2 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -2201,8 +2201,13 @@ bool opengl33_renderer::Append(gfx::vertex_array &Vertices, gfx::userdata_array return m_geometry.append(Vertices, Userdata, Geometry); } +void opengl33_renderer::Erase(gfx::geometry_handle const &Geometry) +{ + m_geometry.erase(Geometry); +} + // provides direct access to index data of specfied chunk -gfx::index_array const & opengl33_renderer::Indices(gfx::geometry_handle const &Geometry) const +gfx::index_array const & opengl33_renderer::Indices(gfx::geometry_handle const &Geometry) const { return m_geometry.indices(Geometry); } @@ -2218,6 +2223,11 @@ gfx::userdata_array const &opengl33_renderer::UserData(const gfx::geometry_handl return m_geometry.userdata(Geometry); } +int opengl33_renderer::Primitive(gfx::geometry_handle const &Geometry) const +{ + return static_cast(m_geometry.primitive(Geometry)); +} + // material methods material_handle opengl33_renderer::Fetch_Material(std::string const &Filename, bool const Loadnow) { @@ -2225,6 +2235,18 @@ material_handle opengl33_renderer::Fetch_Material(std::string const &Filename, b return m_materials.create(Filename, Loadnow); } +material_handle opengl33_renderer::Matte_Material(material_handle const Material) +{ + + return m_materials.create_matte(Material); +} + +material_handle opengl33_renderer::Tinted_Material(material_handle const Material, glm::vec3 const &Colour) +{ + + return m_materials.create_tinted(Material, Colour); +} + std::shared_ptr opengl33_renderer::Fetch_Shader(const std::string &name) { auto it = m_shaders.find(name); diff --git a/rendering/opengl33renderer.h b/rendering/opengl33renderer.h index 64574420f..b04153586 100644 --- a/rendering/opengl33renderer.h +++ b/rendering/opengl33renderer.h @@ -66,6 +66,7 @@ class opengl33_renderer : public gfx_renderer { bool Replace(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type, const std::size_t Offset = 0) override; // adds supplied vertex data at the end of specified chunk bool Append(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type) override; + void Erase( gfx::geometry_handle const &Geometry ) override; // provides direct access to index data of specfied chunk gfx::index_array const & Indices( gfx::geometry_handle const &Geometry ) const override; @@ -75,9 +76,15 @@ class opengl33_renderer : public gfx_renderer { // provides direct access to vertex data of specfied chunk gfx::userdata_array const & UserData( gfx::geometry_handle const &Geometry ) const override; + int + Primitive( gfx::geometry_handle const &Geometry ) const override; // material methods material_handle Fetch_Material( std::string const &Filename, bool const Loadnow = true ) override; + material_handle + Matte_Material( material_handle const Material ) override; + material_handle + Tinted_Material( material_handle const Material, glm::vec3 const &Colour ) override; void Bind_Material( material_handle const Material, TSubModel const *sm = nullptr, lighting_data const *lighting = nullptr ) override; IMaterial const * diff --git a/rendering/openglrenderer.cpp b/rendering/openglrenderer.cpp index 59152c64d..51ac1e715 100644 --- a/rendering/openglrenderer.cpp +++ b/rendering/openglrenderer.cpp @@ -1758,6 +1758,11 @@ bool opengl_renderer::Append(gfx::vertex_array &Vertices, gfx::userdata_array &U return m_geometry.append( Vertices, Userdata, Geometry ); } +void opengl_renderer::Erase( gfx::geometry_handle const &Geometry ) +{ + m_geometry.erase( Geometry ); +} + // provides direct access to vertex data of specfied chunk gfx::index_array const & opengl_renderer::Indices( gfx::geometry_handle const &Geometry ) const { diff --git a/rendering/openglrenderer.h b/rendering/openglrenderer.h index 78fffa40b..da3faad06 100644 --- a/rendering/openglrenderer.h +++ b/rendering/openglrenderer.h @@ -65,6 +65,7 @@ public: bool Replace(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type, const std::size_t Offset = 0) override; // adds supplied vertex data at the end of specified chunk bool Append(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type) override; + void Erase( gfx::geometry_handle const &Geometry ) override; // provides direct access to index data of specfied chunk gfx::index_array const & Indices( gfx::geometry_handle const &Geometry ) const override; diff --git a/rendering/renderer.h b/rendering/renderer.h index 0b9eb60f4..4dabc204a 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -59,16 +59,24 @@ public: virtual auto Replace(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type, const std::size_t Offset = 0) -> bool = 0; // adds supplied vertex data at the end of specified chunk virtual auto Append(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type) -> bool = 0; + // empties specified chunk, its handle stays valid to be filled again + virtual void Erase( gfx::geometry_handle const &Geometry ) {} // provides direct access to index data of specfied chunk virtual auto Indices( gfx::geometry_handle const &Geometry ) const->gfx::index_array const & = 0; // provides direct access to vertex data of specfied chunk virtual auto Vertices( gfx::geometry_handle const &Geometry ) const ->gfx::vertex_array const & = 0; // provides direct access to vertex user data of specfied chunk virtual auto UserData( gfx::geometry_handle const &Geometry ) const ->gfx::userdata_array const & = 0; + // kind of geometry of specified chunk, -1 when the renderer doesn't tell + virtual auto Primitive( gfx::geometry_handle const &Geometry ) const -> int { return -1; } // material methods virtual auto Fetch_Material( std::string const &Filename, bool const Loadnow = true ) -> material_handle = 0; virtual void Bind_Material( material_handle const Material, TSubModel const *sm = nullptr, lighting_data const *lighting = nullptr ) = 0; virtual auto Material( material_handle const Material ) const -> IMaterial const * = 0; + // the same material with its highlights damped, for faces which aren't polished like the surfaces they are cut from. backends which can't make one give back the material itself + virtual auto Matte_Material( material_handle const Material ) -> material_handle { return Material; } + // the same material with its texture tinted by the colour, as far as the userdata of the vertices says. backends which can't make one give back the material itself + virtual auto Tinted_Material( material_handle const Material, glm::vec3 const &Colour ) -> material_handle { return Material; } // heightmap terrain: material of a chunk blending the materials of up to 8 layers by their weights. // Weights: (Samples + 1)^2 values per layer, planar, rows of growing z, the weights of a sample adding up to 255; null with a single layer. // Placement: corner of the chunk (x, z) in the texture coordinates of its mesh, and the size of the chunk. From 89c4555e1e9b410c149331cb485c5baf48f1285d Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 3 Nov 2026 21:50:32 +0100 Subject: [PATCH 29/90] feat(render): matte and tinted copies of a material, the steel weathered or tinted as far as the userdata of its vertices says Matte_Material() gives a copy of a material with its glossiness damped and its weathering on: the faces cut out of a polished surface, as the sides of a worn rail head, darken and lose the shine by the userdata of their vertices. Tinted_Material() gives a copy whose texture is tinted to a colour as far instead. Both are made once per material. The weathering and the tint colour have a param slot of their own, param[3]: the shaders of the materials use all four components of param[2] among them (detail, parallax, water, the terrain placement), and light_common.glsl is included in all of them. MAX_PARAMS grows from 3 to 4, the model UBO by 16 bytes. Neither copy is made when another param of the shader lies on the components it would set. --- gl/ubo.h | 2 +- model/material.cpp | 117 ++++++++++++++++++++++++++++++++++++++ model/material.h | 7 +++ shaders/light_common.glsl | 18 ++++++ 4 files changed, 143 insertions(+), 1 deletion(-) diff --git a/gl/ubo.h b/gl/ubo.h index d5335b21d..9d2c96912 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -62,7 +62,7 @@ namespace gl static_assert(sizeof(scene_ubs) == 400, "bad size of ubs"); - const size_t MAX_PARAMS = 3; + const size_t MAX_PARAMS = 4; struct model_ubs { diff --git a/model/material.cpp b/model/material.cpp index dd7598ab1..ddc521e59 100644 --- a/model/material.cpp +++ b/model/material.cpp @@ -662,6 +662,123 @@ material_manager::create_from_text( std::string const &Name, std::string const & return materialhandle; } +namespace { + +opengl_material +variant_of( opengl_material const &Source, std::string const &Suffix ) { + + opengl_material variant; + variant.textures = Source.textures; + variant.texture_variants = Source.texture_variants; + variant.params = Source.params; + variant.params_state = Source.params_state; + variant.shader = Source.shader; + variant.opacity = Source.opacity; + variant.selfillum = Source.selfillum; + variant.shadow_rank = Source.shadow_rank; + variant.name = Source.name + Suffix; + variant.size = Source.size; + variant.is_good = Source.is_good; + variant.path = Source.path; + return variant; +} + +// whether another param of the shader lies on a component of the named one, which a value set for that one would change too +bool +shares_slot( std::unordered_map const &Conf, std::string const &Name ) { + + auto const own { Conf.find( Name ) }; + if( own == Conf.end() ) { + return false; + } + return std::any_of( + std::begin( Conf ), std::end( Conf ), + [&]( auto const &Entry ) { + return Entry.first != Name + && Entry.second.location == own->second.location + && Entry.second.offset < own->second.offset + own->second.size + && own->second.offset < Entry.second.offset + Entry.second.size; } ); +} + +} // namespace + +material_handle +material_manager::create_matte( material_handle const Material ) { + + if( Material == null_handle || Material >= m_materials.size() ) { + return Material; + } + auto const lookup { m_mattes.find( Material ) }; + if( lookup != m_mattes.end() ) { + return lookup->second; + } + auto matte { variant_of( m_materials[ Material ], ":matte" ) }; + matte.glossiness = 0.1f; + if( matte.shader ) { + for( auto const &[ key, value ] : std::vector>{ { "weathering", glm::vec4{ 1.f } } } ) { + auto const entry { matte.shader->param_conf.find( key ) }; + if( entry == matte.shader->param_conf.end() + || shares_slot( matte.shader->param_conf, key ) ) { + continue; + } + for( std::size_t i = 0; i < entry->second.size; ++i ) { + matte.params[ entry->second.location ][ entry->second.offset + i ] = value[ static_cast( i ) ]; + } + matte.params_state.erase( + std::remove_if( + std::begin( matte.params_state ), std::end( matte.params_state ), + [&]( gl::shader::param_entry const &State ) { + return State.location == entry->second.location && State.offset == entry->second.offset; } ), + std::end( matte.params_state ) ); + } + } + auto const mattehandle { static_cast( m_materials.size() ) }; + m_materials.emplace_back( std::move( matte ) ); + m_mattes.emplace( Material, mattehandle ); + return mattehandle; +} + +material_handle +material_manager::create_tinted( material_handle const Material, glm::vec3 const &Colour ) { + + if( Material == null_handle || Material >= m_materials.size() || !m_materials[ Material ].shader ) { + return Material; + } + auto const key { std::make_tuple( Material, Colour.r, Colour.g, Colour.b ) }; + if( auto const lookup { m_tints.find( key ) }; lookup != m_tints.end() ) { + return lookup->second; + } + // the colour in the rgb of the slot of the weathering, which tells the shader to tint by its sign; nothing else in that slot + auto const &conf { m_materials[ Material ].shader->param_conf }; + auto const weathering { conf.find( "weathering" ) }; + auto const tint { conf.find( "tint" ) }; + if( weathering == conf.end() + || tint == conf.end() + || weathering->second.offset != 3 + || tint->second.location != weathering->second.location + || tint->second.offset != 0 + || std::any_of( + std::begin( conf ), std::end( conf ), + [&]( auto const &Entry ) { + return Entry.first != weathering->first && Entry.first != tint->first && Entry.second.location == weathering->second.location; } ) ) { + return Material; + } + auto tinted { variant_of( m_materials[ Material ], ":tinted" ) }; + tinted.glossiness = m_materials[ Material ].glossiness; + auto const location { weathering->second.location }; + tinted.params[ location ] = glm::vec4{ Colour, -1.f }; + tinted.params_state.erase( + std::remove_if( + std::begin( tinted.params_state ), std::end( tinted.params_state ), + [&]( gl::shader::param_entry const &State ) { + return State.location == location; } ), + std::end( tinted.params_state ) ); + auto const tintedhandle { static_cast( m_materials.size() ) }; + m_materials.emplace_back( std::move( tinted ) ); + m_tints.emplace( key, tintedhandle ); + return tintedhandle; +} + void material_manager::on_weather_change() { diff --git a/model/material.h b/model/material.h index f329a3256..05a68880f 100644 --- a/model/material.h +++ b/model/material.h @@ -142,6 +142,11 @@ public: // material described by provided text (the content of a .mat file), not kept in a file. returns: handle to the material or null_handle material_handle create_from_text( std::string const &Name, std::string const &Definition ); + // copy of provided material with its specular highlights and reflections damped. returns: handle to the copy, made once per material + material_handle + create_matte( material_handle const Material ); + material_handle + create_tinted( material_handle const Material, glm::vec3 const &Colour ); opengl_material const & material( material_handle const Material ) const { return m_materials[ Material ]; } opengl_material & @@ -170,6 +175,8 @@ private: // members: material_sequence m_materials; index_map m_materialmappings; + std::unordered_map m_mattes; + std::map, material_handle> m_tints; }; //--------------------------------------------------------------------------- diff --git a/shaders/light_common.glsl b/shaders/light_common.glsl index 0801b09dc..6261a090d 100644 --- a/shaders/light_common.glsl +++ b/shaders/light_common.glsl @@ -9,6 +9,11 @@ uniform sampler2D headlightmap; float glossiness = 1.0; float metalic = 0.0; +in vec4 UserData; +// a slot of its own, the shaders of the materials using all of param[2]: how weathered the steel is as far as the userdata of +// its vertices says; below 0: the texture tinted to the colour of tint as far instead +#param (weathering, 3, 3, 1, zero) +#param (tint, 3, 0, 3, one) // --------------------------------------------------------------------- // Lighting balance tunables - tweak these to control overall scene @@ -285,6 +290,19 @@ vec3 apply_lights(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float refl { // Diffuse colour is applied once, to the albedo; the light terms below are not scaled by it again texturecolor *= material_diffuse(); + float weathered = (param[3].w > 0.0 ? clamp(param[3].w * UserData.x, 0.0, 1.0) : 0.0); + float tinted = (param[3].w < 0.0 ? clamp(-param[3].w * UserData.x, 0.0, 1.0) : 0.0); + // a tint only scales the colour of the texture, the steel keeps its own polish + float polished = weathered * smoothstep(0.03, 0.12, dot(texturecolor, vec3(0.2126, 0.7152, 0.0722))); + float weatheredluma = min(dot(texturecolor, vec3(0.2126, 0.7152, 0.0722)), 0.05); + vec3 unused = texturecolor * (weatheredluma / max(dot(texturecolor, vec3(0.2126, 0.7152, 0.0722)), 1e-4)); + unused = mix(unused, vec3(1.0, 0.82, 0.74) * (weatheredluma / 0.8525), 0.5); + texturecolor = mix(texturecolor, unused, smoothstep(0.0, 0.4, weathered)); + texturecolor = (tinted > 0.0 ? texturecolor * mix(vec3(1.0), param[3].rgb, tinted) : texturecolor); + specularity = mix(specularity, min(specularity, 0.03), smoothstep(0.0, 0.4, polished)); + metalic = mix(metalic, 0.0, polished); + glossiness = mix(glossiness, min(glossiness, 0.06 * abs(param[1].w)), polished); + reflectivity = mix(reflectivity, min(reflectivity, 0.08 * param[1].z), smoothstep(0.0, 0.4, polished)); // Scale ambient before it gets tinted by the texture. // Sun, headlights and emission are added afterwards so they are NOT // attenuated by AMBIENT_SCALE - this only dims the indirect term. From ed0ff36a6994243d939160807c651376adecc7d2 Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 3 Nov 2026 21:53:52 +0100 Subject: [PATCH 30/90] fix(app): a scenario failing as it loads goes back to the launcher too, the files of other cases looked for only off Windows A scenario failing past its opening popped the loader mode and went on to pop it again and push the driver mode with the scenery half loaded; it goes back to the launcher as one that can't be opened. FileCased() goes through the folder only where the file system tells the case apart: on Windows a missing file was looked for in its whole folder for nothing, and a name outside the code page threw from the parser. The Discord thread sleeps a quarter of a second outside the driver mode, not to hold the closing up. --- application/application.cpp | 2 +- application/scenarioloadermode.cpp | 5 +++++ application/uilayer.cpp | 2 +- utilities/utilities.cpp | 7 +++++++ utilities/utilities.h | 2 +- 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/application/application.cpp b/application/application.cpp index 56a83c29c..33f5c8a0e 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -272,7 +272,7 @@ void eu07_application::DiscordRPCService() if (currentMode != mode::driver) { // the editor and the other modes: nothing to tell, without spinning a core on it - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::this_thread::sleep_for(std::chrono::milliseconds(250)); continue; } diff --git a/application/scenarioloadermode.cpp b/application/scenarioloadermode.cpp index 7afe0a442..eaa034f7e 100644 --- a/application/scenarioloadermode.cpp +++ b/application/scenarioloadermode.cpp @@ -67,8 +67,13 @@ bool scenarioloader_mode::update() { return true; } catch (invalid_scenery_exception &e) { + // 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(); + Global.SceneryFile.clear(); Application.pop_mode(); + Application.push_mode(eu07_application::mode::launcher); + return true; } // the loading state holds the scenario file open, which on some systems keeps the file from being replaced // when the scenery is saved in the editor. it'd also get in the way of another load diff --git a/application/uilayer.cpp b/application/uilayer.cpp index d8417e4fe..a188d4f0b 100644 --- a/application/uilayer.cpp +++ b/application/uilayer.cpp @@ -598,7 +598,7 @@ void ui_layer::render_hierarchy(){ if (ImGui::IsItemHovered()) { - ImGui::SetTooltip(STR_C("UUID: %s\nNode type: %s"),uuid.c_str(), typeid(*node).name()); + ImGui::SetTooltip(STR_C("UUID: %s\nNode type: %s"), uuid.c_str(), typeid(*node).name()); } } } diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index 0c18e0027..b25c6b31d 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -476,6 +476,12 @@ std::string FileCased(std::string const &Filename) std::error_code error; if (std::filesystem::exists(Filename, error)) return Filename; +#ifdef _WIN32 + // the names there are found in any case already: no folder to go through for every file missing, nor names outside of the + // code page to throw on + return {}; +#else + // only the name of the file itself, a folder named in another case isn't found std::filesystem::path const path{Filename}; auto const directory{path.has_parent_path() ? path.parent_path() : std::filesystem::path{"."}}; auto const wanted{ToLower(path.filename().string())}; @@ -483,6 +489,7 @@ std::string FileCased(std::string const &Filename) if (ToLower(entry->path().filename().string()) == wanted) return path.has_parent_path() ? (directory / entry->path().filename()).string() : entry->path().filename().string(); return {}; +#endif } std::pair FileExists(std::vector const &Names, std::vector const &Extensions) diff --git a/utilities/utilities.h b/utilities/utilities.h index d98b7bc9a..f16431dde 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -218,7 +218,7 @@ template <> bool extract_value(bool &Variable, std::string const &Key, std::stri bool FileExists(std::string const &Filename); // the file as it's named on the disk when only the case of its name differs, as in the sceneries made on Windows; empty when -// there's none +// there's none. the folders have to be named in their case; on Windows, finding the names in any case itself, the file given or none std::string FileCased(std::string const &Filename); std::pair FileExists(std::vector const &Names, std::vector const &Extensions); From 48af69cbf19e475d4472d6347e64f29b770a1ddf Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 3 Nov 2026 22:00:48 +0100 Subject: [PATCH 31/90] fix(render): the matte and tinted copies of a material made again with its season and weather, the primitive of a handle to no chunk told as none The copies don't update themselves, their names aren't files to read again: after a change of the weather or the season the material got its new textures and its copies kept the old ones. They are made again from it then, under their handles. primitive() of an empty handle or one past the banks read out of the bank list; it gives no_primitive there, -1 from the renderer. --- model/material.cpp | 131 ++++++++++++++++++++++----------- model/material.h | 5 ++ rendering/geometrybank.cpp | 4 + rendering/geometrybank.h | 3 +- rendering/opengl33renderer.cpp | 3 +- 5 files changed, 102 insertions(+), 44 deletions(-) diff --git a/model/material.cpp b/model/material.cpp index ddc521e59..2b92986d0 100644 --- a/model/material.cpp +++ b/model/material.cpp @@ -676,6 +676,7 @@ variant_of( opengl_material const &Source, std::string const &Suffix ) { variant.opacity = Source.opacity; variant.selfillum = Source.selfillum; variant.shadow_rank = Source.shadow_rank; + variant.glossiness = Source.glossiness; variant.name = Source.name + Suffix; variant.size = Source.size; variant.is_good = Source.is_good; @@ -700,6 +701,65 @@ shares_slot( std::unordered_map const &Con && own->second.offset < Entry.second.offset + Entry.second.size; } ); } +// the highlights of a copy of a material damped and its weathering on +void +make_matte( opengl_material &Variant ) { + + Variant.glossiness = 0.1f; + if( !Variant.shader ) { + return; + } + for( auto const &[ key, value ] : std::vector>{ { "weathering", glm::vec4{ 1.f } } } ) { + auto const entry { Variant.shader->param_conf.find( key ) }; + if( entry == Variant.shader->param_conf.end() + || shares_slot( Variant.shader->param_conf, key ) ) { + continue; + } + for( std::size_t i = 0; i < entry->second.size; ++i ) { + Variant.params[ entry->second.location ][ entry->second.offset + i ] = value[ static_cast( i ) ]; + } + Variant.params_state.erase( + std::remove_if( + std::begin( Variant.params_state ), std::end( Variant.params_state ), + [&]( gl::shader::param_entry const &State ) { + return State.location == entry->second.location && State.offset == entry->second.offset; } ), + std::end( Variant.params_state ) ); + } +} + +// the colour in the rgb of the slot of the weathering of a copy of a material, which tells the shader to tint by its sign. returns: +// whether the shader can tint it, the weathering and the tint alone in that slot +bool +make_tinted( opengl_material &Variant, glm::vec3 const &Colour ) { + + if( !Variant.shader ) { + return false; + } + auto const &conf { Variant.shader->param_conf }; + auto const weathering { conf.find( "weathering" ) }; + auto const tint { conf.find( "tint" ) }; + if( weathering == conf.end() + || tint == conf.end() + || weathering->second.offset != 3 + || tint->second.location != weathering->second.location + || tint->second.offset != 0 + || std::any_of( + std::begin( conf ), std::end( conf ), + [&]( auto const &Entry ) { + return Entry.first != weathering->first && Entry.first != tint->first && Entry.second.location == weathering->second.location; } ) ) { + return false; + } + auto const location { weathering->second.location }; + Variant.params[ location ] = glm::vec4{ Colour, -1.f }; + Variant.params_state.erase( + std::remove_if( + std::begin( Variant.params_state ), std::end( Variant.params_state ), + [&]( gl::shader::param_entry const &State ) { + return State.location == location; } ), + std::end( Variant.params_state ) ); + return true; +} + } // namespace material_handle @@ -713,25 +773,7 @@ material_manager::create_matte( material_handle const Material ) { return lookup->second; } auto matte { variant_of( m_materials[ Material ], ":matte" ) }; - matte.glossiness = 0.1f; - if( matte.shader ) { - for( auto const &[ key, value ] : std::vector>{ { "weathering", glm::vec4{ 1.f } } } ) { - auto const entry { matte.shader->param_conf.find( key ) }; - if( entry == matte.shader->param_conf.end() - || shares_slot( matte.shader->param_conf, key ) ) { - continue; - } - for( std::size_t i = 0; i < entry->second.size; ++i ) { - matte.params[ entry->second.location ][ entry->second.offset + i ] = value[ static_cast( i ) ]; - } - matte.params_state.erase( - std::remove_if( - std::begin( matte.params_state ), std::end( matte.params_state ), - [&]( gl::shader::param_entry const &State ) { - return State.location == entry->second.location && State.offset == entry->second.offset; } ), - std::end( matte.params_state ) ); - } - } + make_matte( matte ); auto const mattehandle { static_cast( m_materials.size() ) }; m_materials.emplace_back( std::move( matte ) ); m_mattes.emplace( Material, mattehandle ); @@ -748,43 +790,47 @@ material_manager::create_tinted( material_handle const Material, glm::vec3 const if( auto const lookup { m_tints.find( key ) }; lookup != m_tints.end() ) { return lookup->second; } - // the colour in the rgb of the slot of the weathering, which tells the shader to tint by its sign; nothing else in that slot - auto const &conf { m_materials[ Material ].shader->param_conf }; - auto const weathering { conf.find( "weathering" ) }; - auto const tint { conf.find( "tint" ) }; - if( weathering == conf.end() - || tint == conf.end() - || weathering->second.offset != 3 - || tint->second.location != weathering->second.location - || tint->second.offset != 0 - || std::any_of( - std::begin( conf ), std::end( conf ), - [&]( auto const &Entry ) { - return Entry.first != weathering->first && Entry.first != tint->first && Entry.second.location == weathering->second.location; } ) ) { + auto tinted { variant_of( m_materials[ Material ], ":tinted" ) }; + if( false == make_tinted( tinted, Colour ) ) { return Material; } - auto tinted { variant_of( m_materials[ Material ], ":tinted" ) }; - tinted.glossiness = m_materials[ Material ].glossiness; - auto const location { weathering->second.location }; - tinted.params[ location ] = glm::vec4{ Colour, -1.f }; - tinted.params_state.erase( - std::remove_if( - std::begin( tinted.params_state ), std::end( tinted.params_state ), - [&]( gl::shader::param_entry const &State ) { - return State.location == location; } ), - std::end( tinted.params_state ) ); auto const tintedhandle { static_cast( m_materials.size() ) }; m_materials.emplace_back( std::move( tinted ) ); m_tints.emplace( key, tintedhandle ); return tintedhandle; } +void +material_manager::update_variants( bool const Weather ) { + + // the copies don't update themselves, their names aren't files to read again + auto const changed = [&]( material_handle const Source ) { + auto const &source { m_materials[ Source ] }; + return Weather ? source.update_on_weather_change : source.update_on_season_change; }; + for( auto const &[ source, matte ] : m_mattes ) { + if( changed( source ) ) { + auto variant { variant_of( m_materials[ source ], ":matte" ) }; + make_matte( variant ); + m_materials[ matte ] = std::move( variant ); + } + } + for( auto const &[ key, tinted ] : m_tints ) { + auto const source { std::get<0>( key ) }; + if( changed( source ) ) { + auto variant { variant_of( m_materials[ source ], ":tinted" ) }; + make_tinted( variant, glm::vec3 { std::get<1>( key ), std::get<2>( key ), std::get<3>( key ) } ); + m_materials[ tinted ] = std::move( variant ); + } + } +} + void material_manager::on_weather_change() { for( auto &material : m_materials ) { if( material.update_on_weather_change ) { material.update(); } } + update_variants( true ); } void @@ -793,6 +839,7 @@ material_manager::on_season_change() { for( auto &material : m_materials ) { if( material.update_on_season_change ) { material.update(); } } + update_variants( false ); } // checks whether specified material is in the material bank. returns handle to the material, or a null handle diff --git a/model/material.h b/model/material.h index 05a68880f..82d14557c 100644 --- a/model/material.h +++ b/model/material.h @@ -145,6 +145,8 @@ public: // copy of provided material with its specular highlights and reflections damped. returns: handle to the copy, made once per material material_handle create_matte( material_handle const Material ); + // copy of provided material with its texture tinted to the colour as far as the userdata of the vertices says. returns: handle to + // the copy, made once per material and colour; the material itself when its shader can't tint it material_handle create_tinted( material_handle const Material, glm::vec3 const &Colour ); opengl_material const & @@ -166,6 +168,9 @@ private: // checks whether specified texture is in the texture bank. returns texture id, or npos. material_handle find_in_databank( std::string const &Materialname ) const; + // the matte and tinted copies of the materials changed with the weather or the season made again from them, under their handles + void + update_variants( bool const Weather ); public: // checks whether specified file exists. returns name of the located file, or empty string. static std::pair diff --git a/rendering/geometrybank.cpp b/rendering/geometrybank.cpp index 2a1ff5ee6..aa7c1f8b5 100644 --- a/rendering/geometrybank.cpp +++ b/rendering/geometrybank.cpp @@ -378,6 +378,8 @@ geometry_bank::vertices( gfx::geometry_handle const &Geometry ) const { unsigned int geometry_bank::primitive( gfx::geometry_handle const &Geometry ) const { + if( ( Geometry.chunk == 0 ) || ( Geometry.chunk > m_chunks.size() ) ) { return no_primitive; } + return geometry_bank::chunk( Geometry ).type; } @@ -516,6 +518,8 @@ geometrybank_manager::vertices( gfx::geometry_handle const &Geometry ) const { unsigned int geometrybank_manager::primitive( gfx::geometry_handle const &Geometry ) const { + if( ( Geometry.bank == 0 ) || ( Geometry.bank > m_geometrybanks.size() ) ) { return geometry_bank::no_primitive; } + return bank( Geometry ).first->primitive( Geometry ); } diff --git a/rendering/geometrybank.h b/rendering/geometrybank.h index 537c79ba2..c01e46fe3 100644 --- a/rendering/geometrybank.h +++ b/rendering/geometrybank.h @@ -139,8 +139,9 @@ public: auto vertices( gfx::geometry_handle const &Geometry ) const -> gfx::vertex_array const &; // provides direct access to vertex user data of specfied chunk auto userdata( gfx::geometry_handle const &Geometry ) const -> gfx::userdata_array const &; - // kind of geometry of specified chunk, GL_TRIANGLES, GL_TRIANGLE_STRIP... + // kind of geometry of specified chunk, GL_TRIANGLES, GL_TRIANGLE_STRIP...; no_primitive for a handle to no chunk auto primitive( gfx::geometry_handle const &Geometry ) const -> unsigned int; + static constexpr unsigned int no_primitive { ~0u }; protected: // types: diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index 97b2fa8d2..9b42bb280 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -2225,7 +2225,8 @@ gfx::userdata_array const &opengl33_renderer::UserData(const gfx::geometry_handl int opengl33_renderer::Primitive(gfx::geometry_handle const &Geometry) const { - return static_cast(m_geometry.primitive(Geometry)); + auto const primitive{m_geometry.primitive(Geometry)}; + return primitive == gfx::geometry_bank::no_primitive ? -1 : static_cast(primitive); } // material methods From 85233160069bd0d5b79dcb34800363431eebb61e Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 3 Nov 2026 23:03:52 +0100 Subject: [PATCH 32/90] fix(track): a tributary loads as a river of its first path keeping the second, a track of an unknown type is left out, where both made the simulator fall over as it loaded On master neither got a segment: update_location() dereferenced the missing one of a track of an unknown type, and a tributary went to Switch(0), which logged "Cannot switch normal track", with no segment under it either. A tributary is now a river of its first path. Its second path is kept in m_paths, isn't drawn nor run on, and is written back with the type "tributary". A track of an unknown type is logged and left out by the loader; as it is no node, its text stays in the file when the editor saves the scenery. --- simulation/simulationstateserializer.cpp | 6 ++++++ world/Track.cpp | 18 ++++++++++++++++-- world/Track.h | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/simulation/simulationstateserializer.cpp b/simulation/simulationstateserializer.cpp index 1ff771d1d..78955c828 100644 --- a/simulation/simulationstateserializer.cpp +++ b/simulation/simulationstateserializer.cpp @@ -543,6 +543,12 @@ state_serializer::deserialize_node( cParser &Input, scene::scratch_data &Scratch else if( nodedata.type == "track" ) { auto *path { deserialize_path( Input, Scratchpad, nodedata ) }; + if( path->eType == tt_Unknown ) { + // with no path made it can't be joined nor drawn, and nothing could run on it + ErrorLog( "Bad scenario: track \"" + path->name() + "\" of an unknown type defined in file \"" + Input.Name() + "\" (line " + std::to_string( inputline ) + ") left out" ); + delete path; + return; + } // duplicates of named tracks are currently experimentally allowed if( false == simulation::Paths.insert( path ) ) { ErrorLog( "Bad scenario: duplicate track name \"" + path->name() + "\" defined in file \"" + Input.Name() + "\" (line " + std::to_string( inputline ) + ")" ); diff --git a/world/Track.cpp b/world/Track.cpp index f1a29798c..14998ce7c 100644 --- a/world/Track.cpp +++ b/world/Track.cpp @@ -427,11 +427,15 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin) } else if (str == "tributary") { - eType = tt_Tributary; + // dopływ: no path of it was ever made, the simulator falling over it as it loaded. a river of its first path now, the + // second one kept and written back + ErrorLog("Bad track: tributary \"" + m_name + "\" in file \"" + parser->Name() + "\" (line " + std::to_string(parser->Line()) + ") is drawn and run on as a river of its first path"); + eType = tt_Normal; iCategoryFlag = 4; + m_tributary = true; } else - eType = tt_Unknown; + eType = tt_Unknown; // left out by the loader if (Global.iWriteLogEnabled & 4) WriteLog(str); parser->getTokens(4); @@ -517,6 +521,11 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin) break; } } + if( m_tributary ) { + // the second path of a tributary, kept to be written back + m_paths.emplace_back(); + m_paths.back().deserialize( *parser, pOrigin ); + } init_segments( true ); @@ -681,6 +690,10 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin) m_name = m_name.substr(0, i - 1); // usunięcie z nazwy } + if( CurrentSegment() == nullptr ) { + // of an unknown type, no path of it is made + return; + } update_location(); // sleeper transforms are baked later in create_geometry(), once the owning cell has // assigned this track its m_origin (otherwise the local-space matrices would be relative @@ -2314,6 +2327,7 @@ TTrack::export_as_text_( std::ostream &Output ) const { // type Output << ( eType == tt_Normal ? ( + m_tributary ? "tributary" : iCategoryFlag == 1 ? "normal" : iCategoryFlag == 2 ? "road" : iCategoryFlag == 4 ? "river" : diff --git a/world/Track.h b/world/Track.h index 72a6b7ff4..5952da6a7 100644 --- a/world/Track.h +++ b/world/Track.h @@ -187,6 +187,7 @@ public: std::vector m_paths; // source data for owned paths bool m_editorremoved { false }; + bool m_tributary { false }; // loaded as a tributary: a river of its first path, the second one kept to be written back scene::basic_node *m_road { nullptr }; // road or road junction which generated this path. such path is one-way, and isn't saved nor edited on its own std::array m_routeweights { 0.f, 0.f, 0.f }; // odds of each route through a crossroads being picked by a driver, by the number of the route. all zero: even odds int iterate_stamp = 0; From 09b1c2de10463f69331841a81cc18999fd7846a2 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Thu, 5 Nov 2026 03:12:05 +0100 Subject: [PATCH 33/90] fix(utilities): win1250_to_utf8 turns the polish letters into utf-8, not into themselves (#252) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit utilities.cpp starts with a byte order mark, so the compiler reads it as utf-8. The game's targets don't get /utf-8, though: set_target_properties(... COMPILE_OPTIONS ...) in the MSVC block of CMakeLists.txt replaces the options add_compile_options gave them. So the string literals went into the program in the code page of the system, windows-1250 on a polish one, and the map of win1250_to_utf8 turned each letter into itself. What read windows-1250 through it got no utf-8: the names, descriptions and categories of the sceneries and the names of the textures in the launcher, and the descriptions of the templates in the editor. The editor writes such a description back through utf8_to_win1250, which makes a question mark of each byte that doesn't start a utf-8 sequence, so the letters it had read unchanged didn't come back. The utf-8 sequences are written as bytes now, the way utf8_to_win1250 has them, so they don't depend on the options. The options stay as they are: with /utf-8 other literals of the files with a byte order mark would change, the "±" of the table of Bezogonkow would take two bytes and move the letters after it. The unit tests check the letters and that the text given is valid utf-8. Co-authored-by: maj00r --- tests/unit/test_win1250.cpp | 26 ++++++++++++++++++++++++++ utilities/utilities.cpp | 7 +++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/unit/test_win1250.cpp diff --git a/tests/unit/test_win1250.cpp b/tests/unit/test_win1250.cpp new file mode 100644 index 000000000..156c46ae7 --- /dev/null +++ b/tests/unit/test_win1250.cpp @@ -0,0 +1,26 @@ +#include "stdafx.h" +#include "doctest.h" +#include "utilities/utilities.h" + +// the polish letters, in windows-1250 and in utf-8; written as bytes, the build reads the sources in any code page +namespace +{ +std::string const kWin1250{"\xA5\xC6\xCA\xA3\xD1\xD3\x8C\x8F\xAF\xB9\xE6\xEA\xB3\xF1\xF3\x9C\x9F\xBF"}; +std::string const kUtf8{"\xC4\x84\xC4\x86\xC4\x98\xC5\x81\xC5\x83\xC3\x93\xC5\x9A\xC5\xB9\xC5\xBB" + "\xC4\x85\xC4\x87\xC4\x99\xC5\x82\xC5\x84\xC3\xB3\xC5\x9B\xC5\xBA\xC5\xBC"}; +} // namespace + +TEST_CASE("win1250: the polish letters of a scenery file read as utf-8") { + CHECK(win1250_to_utf8("Bia\xB3" "a Podlaska") == "Bia\xC5\x82" "a Podlaska"); + CHECK(win1250_to_utf8(kWin1250) == kUtf8); + // the rest of the text stays as it is + CHECK(win1250_to_utf8("tor 1, km 0.178") == "tor 1, km 0.178"); +} + +TEST_CASE("win1250: what win1250_to_utf8 gives is valid utf-8") { + std::u32string decoded; + CHECK_FALSE(utf8_to_utf32(kWin1250, decoded)); + REQUIRE(utf8_to_utf32(win1250_to_utf8(kWin1250), decoded)); + CHECK(decoded.size() == kWin1250.size()); + CHECK(decoded[12] == U'\x0142'); +} diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index 02608262b..e84307e39 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -317,8 +317,11 @@ void win1250_to_ascii(std::string &Input) std::string win1250_to_utf8(const std::string &Input) { - std::unordered_map const charmap{{165, "Ą"}, {198, "Ć"}, {202, "Ę"}, {163, "Ł"}, {209, "Ń"}, {211, "Ó"}, {140, "Ś"}, {143, "Ź"}, {175, "Ż"}, - {185, "ą"}, {230, "ć"}, {234, "ę"}, {179, "ł"}, {241, "ń"}, {243, "ó"}, {156, "ś"}, {159, "ź"}, {191, "ż"}}; + // the utf-8 sequences written as bytes: the file has a byte order mark and the build doesn't give the compiler /utf-8, so letters + // written as letters would end up in the program in windows-1250, the same as the input + static std::unordered_map const charmap{{'\xA5', "\xC4\x84"}, {'\xC6', "\xC4\x86"}, {'\xCA', "\xC4\x98"}, {'\xA3', "\xC5\x81"}, {'\xD1', "\xC5\x83"}, {'\xD3', "\xC3\x93"}, + {'\x8C', "\xC5\x9A"}, {'\x8F', "\xC5\xB9"}, {'\xAF', "\xC5\xBB"}, {'\xB9', "\xC4\x85"}, {'\xE6', "\xC4\x87"}, {'\xEA', "\xC4\x99"}, + {'\xB3', "\xC5\x82"}, {'\xF1', "\xC5\x84"}, {'\xF3', "\xC3\xB3"}, {'\x9C', "\xC5\x9B"}, {'\x9F', "\xC5\xBA"}, {'\xBF', "\xC5\xBC"}}; std::string output; std::unordered_map::const_iterator lookup; for (auto &input : Input) From fc22645d7c5a1fa7c008436b48541296bbbec040 Mon Sep 17 00:00:00 2001 From: pawelw Date: Thu, 5 Nov 2026 10:25:26 +0100 Subject: [PATCH 34/90] fix(physics): articulated couplers stall freight trains (eu07/maszyna#553) (#255) * fix(physics): articulated couplers stall freight trains (eu07/maszyna#553) Articulated wagons (T3000e, N-230, N-234) set up with CType=Articulated barely moved in a freight train and jerked. - The articulated joint took the spring force up to DmaxC = 0.05 m only, 225 kN, well short of its FmaxC of 850 kN and of what a loaded section in a freight train takes. It overstretched and the sections collided at every physics step. Dmax is now sized so the spring carries FmaxC (0.19 m stretched, 0.09 m pressed in), never below the old 0.05 m. - ComputeCollision() scaled the velocities of both vehicles by sqrt(1 - beta), so each collision braked the pair as a whole: two coupled vehicles in a sustained "collision" lost a third of their speed per step. The restitution now applies to the velocities relative to the centre of mass only, and the pair keeps its momentum. Tests: the articulated joint carries FmaxC within its travel; a jerked articulated set and a jerked screw-coupled pair keep their momentum. On the code before the fix both pairs stop dead within 3 s. * style(physics): English-only comment in ComputeCollision() The comment above the collision velocities started in Polish and went on in English. * style(physics): SonarQube findings in the #553 fix std::lerp for the collision velocities (cpp:S6179) and test locals renamed so they no longer shadow the end and coupling enumerators (cpp:S1117). --- McZapkie/Mover.cpp | 17 ++++++--- tests/unit/test_couplers.cpp | 69 ++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index cb17e30ad..954e277ba 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -17,6 +17,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/parser.h" #include "simulation/simulation.h" +#include #include //--------------------------------------------------------------------------- @@ -51,8 +52,13 @@ double ComputeCollision(double &v1, double &v2, double m1, double m2, double bet double sum = m1 + m2; double w1 = (m2 * v2 * 2.0 + v1 * (m1 - m2)) / sum; double w2 = (m1 * v1 * 2.0 + v2 * (m2 - m1)) / sum; - v1 = w1 * std::sqrt(1.0 - beta); // niejawna zmiana predkosci wskutek zderzenia - v2 = w2 * std::sqrt(1.0 - beta); + // the collision changes the velocities directly: the bounce loses energy in the velocities relative to the + // centre of mass only, the pair keeps its momentum. scaling the velocities themselves braked the vehicles + // down with every step their couplers spent stretched or pressed in all the way (eu07/maszyna#553) + auto const centre{(m1 * v1 + m2 * v2) / sum}; + auto const restitution{std::sqrt(1.0 - beta)}; + v1 = std::lerp(centre, w1, restitution); + v2 = std::lerp(centre, w2, restitution); return m1 * (w2 - w1) * (1 - beta); } } @@ -10695,12 +10701,15 @@ void TMoverParameters::LoadFIZ_BuffCoupl(std::string const &line, int const Inde coupler->beta = 0.55; */ thiscoupler->SpringKC = 4500 * 1000; - thiscoupler->DmaxC = 0.05; thiscoupler->FmaxC = 850 * 1000; thiscoupler->SpringKB = 9200 * 1000; - thiscoupler->DmaxB = 0.05; thiscoupler->FmaxB = 320 * 1000; thiscoupler->beta = 0.55; + // the spring force is taken up to Dmax only, past it the vehicles collide (CollisionDetect()); so the joint + // carries the full FmaxC either way before it gives. with 0.05 m it carried 225 kN, short of what a loaded + // articulated wagon in a freight train takes, which then collided at every step (eu07/maszyna#553) + thiscoupler->DmaxC = std::max(0.05, thiscoupler->FmaxC / thiscoupler->SpringKC); + thiscoupler->DmaxB = std::max(0.05, thiscoupler->FmaxC / thiscoupler->SpringKB); } if (Index == 0) diff --git a/tests/unit/test_couplers.cpp b/tests/unit/test_couplers.cpp index 8787a419b..2a0ff6e50 100644 --- a/tests/unit/test_couplers.cpp +++ b/tests/unit/test_couplers.cpp @@ -456,3 +456,72 @@ TEST_CASE("couplers: the coupler force of vehicles facing each other lands on th CHECK(second->Couplers[end::front].CForce == 0.0); } } + +// eu07/maszyna#553: articulated wagons (T3000e, N-234 and the like) set up with articulated couplers barely moved in a +// freight train. the joint took the spring force up to 5 cm only, 225 kN, so it overstretched under the train and the +// sections collided at every step; and each collision scaled the velocities of both vehicles down, rather than only +// the velocity of one relative to the other, braking the whole train + +namespace { + +// the wagon from data/wagon.json, loaded, with articulated couplers at both ends +vehicle articulated_wagon(double Velocity, std::string const &Name) { + return vehicle{definition::load("wagon").set("Param", "M", 60000).set("BuffCoupl", "CType", "Articulated"), Velocity, Name}; +} + +// couples the facing ends of the two vehicles for good, as the scenery does for an articulated set +void join(vehicle &First, int FirstEnd, vehicle &Second, int SecondEnd) { + REQUIRE(First->Attach(FirstEnd, SecondEnd, &Second.mover(), coupling::coupler, true, false)); + REQUIRE(Second->Attach(SecondEnd, FirstEnd, &First.mover(), coupling::coupler, true, false)); +} + +} // namespace + +TEST_CASE("couplers: an articulated joint takes its full tensile force within its travel") { + auto wagon{articulated_wagon(0.0, "wagon")}; + auto const &joint{wagon->Couplers[end::front]}; + REQUIRE(joint.type() == TCouplerType::Articulated); + + CHECK(joint.SpringKC * joint.DmaxC >= doctest::Approx(joint.FmaxC)); + CHECK(joint.SpringKB * joint.DmaxB >= doctest::Approx(joint.FmaxC)); + // and no less than it had before + CHECK(joint.DmaxC >= 0.05); + CHECK(joint.DmaxB >= 0.05); +} + +TEST_CASE("couplers: an articulated set keeps its momentum when one section jerks the other") { + // the front section moving, the rear one barely (so that its brakes are released), joined: the jerk overstretches the joint, the sections collide + // and run on together at half the speed, as the momentum they have between them dictates + auto front_section{articulated_wagon(20.0, "front")}; + auto rear_section{articulated_wagon(0.1, "rear")}; + join(rear_section, end::front, front_section, end::rear); + test::pair pair{rear_section, end::front, front_section, end::rear, 0.0}; + REQUIRE(pair.coupled()); + // brakes released, as a locomotive keeps them + front_section.hold_brake_pipe(5.0); + rear_section.hold_brake_pipe(5.0); + + pair.run(3.0, test::track{0.15, 0.0}); + + CHECK(front_section.speed() == doctest::Approx(rear_section.speed()).epsilon(0.1)); + // rolling resistance takes a little in the 3 s, the collisions used to take most of it + CHECK(0.5 * (front_section.speed() + rear_section.speed()) > 9.0); + CHECK(0.5 * (front_section.speed() + rear_section.speed()) < 10.05 + 1e-6); + CHECK(pair.coupled()); +} + +TEST_CASE("couplers: a collision of coupled vehicles keeps the momentum of the pair") { + // the same jerk through screw couplers, which collide when stretched beyond their travel as well + auto front_wagon{vehicle{definition::load("wagon"), 30.0, "front"}}; + auto rear_wagon{vehicle{definition::load("wagon"), 0.1, "rear"}}; + join(rear_wagon, end::front, front_wagon, end::rear); + test::pair pair{rear_wagon, end::front, front_wagon, end::rear, 0.0}; + REQUIRE(pair.coupled()); + front_wagon.hold_brake_pipe(5.0); + rear_wagon.hold_brake_pipe(5.0); + + pair.run(3.0, test::track{0.15, 0.0}); + + CHECK(0.5 * (front_wagon.speed() + rear_wagon.speed()) > 13.5); + CHECK(0.5 * (front_wagon.speed() + rear_wagon.speed()) < 15.05 + 1e-6); +} From a6778c9fe24f8bee37ce045cf99e797a943da0d0 Mon Sep 17 00:00:00 2001 From: maj00r Date: Fri, 6 Nov 2026 19:54:54 +0100 Subject: [PATCH 35/90] refactor: the Sonar remarks on the small fixes taken care of - the log stops through a flag kept in a function, not a global one - the messages are put together with std::format - a vehicle dropped on a trainset in the launcher goes in once the trainset is drawn, so the vehicles are gone through with a plain loop again; the drop split into a new vehicle and a moved one - the scenario that can't be opened is caught as the runtime error it is, a file name outside of the code page as the system error it is - a track of an unknown type is left out through the pointer owning it - the keyword of a track type written out by a function of its own - the picked node forgotten with ranges::replace_if, the empty Forget_Node of the base renderer told why it is empty - FileCased takes the path it works on --- application/application.cpp | 4 +- application/scenarioloadermode.cpp | 7 +- launcher/scenery_list.cpp | 103 +++++++++++++++-------- launcher/scenery_list.h | 4 + launcher/scenery_scanner.cpp | 5 +- rendering/opengl33renderer.cpp | 2 +- rendering/openglrenderer.cpp | 2 +- rendering/renderer.h | 4 +- simulation/simulationstateserializer.cpp | 12 +-- utilities/Logs.cpp | 15 +++- utilities/utilities.cpp | 11 ++- utilities/utilities.h | 2 +- world/Track.cpp | 42 ++++++--- world/Track.h | 2 + 14 files changed, 140 insertions(+), 75 deletions(-) diff --git a/application/application.cpp b/application/application.cpp index 33f5c8a0e..9b7b82853 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -696,8 +696,8 @@ void eu07_application::exit() Global.applicationQuitOrder = true; // each step of the closing in the log, to tell from it where a closing that hangs stops auto const closing{std::chrono::steady_clock::now()}; - auto const step = [&](char const *What) { - WriteLog(std::string{"Closing: "} + What + " at " + std::to_string(std::chrono::duration_cast(std::chrono::steady_clock::now() - closing).count()) + " ms"); + auto const step = [closing](char const *What) { + WriteLog(std::format("Closing: {} at {} ms", What, std::chrono::duration_cast(std::chrono::steady_clock::now() - closing).count())); }; step("the modes"); for (auto &mode : m_modes) diff --git a/application/scenarioloadermode.cpp b/application/scenarioloadermode.cpp index eaa034f7e..f8f06ac93 100644 --- a/application/scenarioloadermode.cpp +++ b/application/scenarioloadermode.cpp @@ -21,6 +21,8 @@ http://mozilla.org/MPL/2.0/. #include "utilities/translation.h" #include "audio/audiorenderer.h" +#include + scenarioloader_mode::scenarioloader_mode() { m_userinterface = std::make_shared(); } @@ -52,8 +54,9 @@ bool scenarioloader_mode::update() { try { state = simulation::State.deserialize_begin(Global.SceneryFile); } - catch (std::exception const &e) { - ErrorLog("Bad init: scenario \"" + Global.SceneryFile + "\" couldn't be opened (" + e.what() + ")"); + // the file it can't read, as the parser and the scenery opening it report it + catch (std::runtime_error const &e) { + ErrorLog(std::format("Bad init: scenario \"{}\" couldn't be opened ({})", Global.SceneryFile, e.what())); state.reset(); Global.SceneryFile.clear(); Application.pop_mode(); diff --git a/launcher/scenery_list.cpp b/launcher/scenery_list.cpp index 195b0e2c3..6a57db54b 100644 --- a/launcher/scenery_list.cpp +++ b/launcher/scenery_list.cpp @@ -8,6 +8,28 @@ #include "utilities/translation.h" #include +#include + +namespace +{ + +// the skin of the bank of vehicles as it's shared, the one a skin dragged out of the bank points at; none for one gone since +std::shared_ptr shared_skin(ui::skin_set const *Skin) +{ + auto const vehicle{Skin->vehicle.lock()}; + if (vehicle == nullptr) + return {}; + auto const found{std::ranges::find_if(vehicle->matching_skinsets, [Skin](std::shared_ptr const &Set) { return Set.get() == Skin; })}; + return found != vehicle->matching_skinsets.end() ? *found : nullptr; +} + +// the name of a vehicle put in a trainset, with a number of its own +std::string numbered(std::string const &Name) +{ + return std::format("{}_{}", Name, static_cast(LocalRandom(0.0, 10000000.0))); +} + +} // namespace ui::scenerylist_panel::scenerylist_panel(scenery_scanner &scanner) : ui_panel(STR("Scenario list"), false), scanner(scanner), placeholder_mini("textures/mini/other") @@ -283,8 +305,8 @@ void ui::scenerylist_panel::draw_trainset(trainset_desc &trainset) draw_droptarget(trainset, position++); ImGui::SameLine(15.0f); - for (std::size_t index = 0; index < trainset.vehicles.size(); ++index) { - auto &dyn_desc = trainset.vehicles[index]; + // a vehicle dropped between them goes in once they're all drawn, so they're gone through unchanged + for (auto &dyn_desc : trainset.vehicles) { deferred_image *mini = nullptr; if (dyn_desc.skin && dyn_desc.skin->mini.get() != deferred_image::invalid_id) @@ -333,6 +355,10 @@ void ui::scenerylist_panel::draw_trainset(trainset_desc &trainset) ImGui::PopID(); } + if (m_drop) { + m_drop(); + m_drop = nullptr; + } ImGui::NewLine(); ImGui::PopID(); @@ -341,44 +367,47 @@ void ui::scenerylist_panel::draw_trainset(trainset_desc &trainset) void ui::scenerylist_panel::draw_droptarget(trainset_desc &trainset, int position) { ImGui::Dummy(ImVec2(15, 30)); - if (ImGui::BeginDragDropTarget()) { - const ImGuiPayload *payload = ImGui::AcceptDragDropPayload("vehicle_pure"); - if (payload) { - skin_set *ptr = *reinterpret_cast(payload->Data); - std::shared_ptr skin; - if (auto const vehicle = ptr->vehicle.lock()) - for (auto &s : vehicle->matching_skinsets) - if (s.get() == ptr) - skin = s; + if (false == ImGui::BeginDragDropTarget()) + return; + if (auto const *payload{ImGui::AcceptDragDropPayload("vehicle_pure")}) + drop_new_vehicle(trainset, position, *static_cast(payload->Data)); + if (auto const *payload{ImGui::AcceptDragDropPayload("vehicle_set")}) + drop_moved_vehicle(trainset, position, *static_cast(payload->Data)); + ImGui::EndDragDropTarget(); +} - if (skin) { - trainset.vehicles.emplace(trainset.vehicles.begin() + position); - dynamic_desc &desc = trainset.vehicles[position]; +// a vehicle of the bank dropped on a gap of a trainset, put in there as a new one of it +void ui::scenerylist_panel::drop_new_vehicle(trainset_desc &trainset, int const position, skin_set const *Skin) +{ + auto skin{shared_skin(Skin)}; + if (skin == nullptr) + return; + m_drop = [&trainset, position, skin] { + auto &desc{*trainset.vehicles.emplace(trainset.vehicles.begin() + position)}; + desc.name = numbered(skin->skin); + desc.vehicle = skin->vehicle.lock(); + desc.skin = skin; + }; +} - desc.name = skin->skin + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0)); - desc.vehicle = skin->vehicle.lock(); - desc.skin = skin; - } +// a vehicle of a trainset dropped on a gap of it or of another one: moved there within it, from another one copied in with a +// name of its own +void ui::scenerylist_panel::drop_moved_vehicle(trainset_desc &trainset, int const position, vehicle_moved const &Moved) +{ + auto copy{Moved.dynamic}; + auto &source{Moved.source}; + auto const from{Moved.position}; + if (&trainset != &source) + copy.name = numbered(copy.skin ? copy.skin->skin : copy.name); + m_drop = [&trainset, &source, from, position, copy] { + auto offset{0}; + if (&trainset == &source) { + trainset.vehicles.erase(trainset.vehicles.begin() + from); + if (from < position) + offset = -1; } - - payload = ImGui::AcceptDragDropPayload("vehicle_set"); - if (payload) { - vehicle_moved *moved = reinterpret_cast(payload->Data); - - dynamic_desc desc_copy = moved->dynamic; - int offset = 0; - if (&trainset == &moved->source) { - trainset.vehicles.erase(trainset.vehicles.begin() + moved->position); - if (moved->position < position) - offset = -1; - } else { - desc_copy.name = (desc_copy.skin ? desc_copy.skin->skin : desc_copy.name) + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0)); - } - - trainset.vehicles.insert(trainset.vehicles.begin() + position + offset, desc_copy); - } - ImGui::EndDragDropTarget(); - } + trainset.vehicles.insert(trainset.vehicles.begin() + position + offset, copy); + }; } void ui::scenerylist_panel::purge_selected_trainset() diff --git a/launcher/scenery_list.h b/launcher/scenery_list.h index 957f1b5ee..0bc91c452 100644 --- a/launcher/scenery_list.h +++ b/launcher/scenery_list.h @@ -54,6 +54,8 @@ private: scenery_desc *selected_scenery = nullptr; trainset_desc *selected_trainset = nullptr; deferred_image placeholder_mini; + // a vehicle dropped on a gap between the vehicles of a trainset: put in there once they're all drawn, not while they are + std::function m_drop; void draw_scenery_list(); void draw_scenery_info(); @@ -67,6 +69,8 @@ private: void open_link(const std::string &link); void draw_trainset(trainset_desc &trainset); void draw_droptarget(trainset_desc &trainset, int position); + void drop_new_vehicle(trainset_desc &trainset, int position, skin_set const *Skin); + void drop_moved_vehicle(trainset_desc &trainset, int position, vehicle_moved const &Moved); void purge_selected_trainset(); }; } // namespace ui diff --git a/launcher/scenery_scanner.cpp b/launcher/scenery_scanner.cpp index 5f6a6d2f9..6e258425e 100644 --- a/launcher/scenery_scanner.cpp +++ b/launcher/scenery_scanner.cpp @@ -2,6 +2,8 @@ #include "scenery_scanner.h" #include "utilities/Logs.h" +#include + scenery_scanner::scenery_scanner(ui::vehicles_bank &bank) : bank(bank) { @@ -17,7 +19,8 @@ void scenery_scanner::scan() try { name = path.string(); } - catch (std::exception const &) { + catch (std::system_error const &) { + // a name outside of the code page, which the path can't give as a string on Windows continue; } diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index a5d1f3894..a8a1b2139 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -5414,7 +5414,7 @@ void opengl33_renderer::Forget_Node(scene::basic_node const *Node) { if (m_picksceneryitem == Node) m_picksceneryitem = nullptr; - std::replace_if(m_picksceneryitems.begin(), m_picksceneryitems.end(), [Node](scene::basic_node const *Item) { return Item == Node; }, nullptr); + std::ranges::replace_if(m_picksceneryitems, [Node](scene::basic_node const *Item) { return Item == Node; }, nullptr); } glm::dvec3 opengl33_renderer::Update_Mouse_Position() diff --git a/rendering/openglrenderer.cpp b/rendering/openglrenderer.cpp index 864d84141..6313aa55f 100644 --- a/rendering/openglrenderer.cpp +++ b/rendering/openglrenderer.cpp @@ -1894,7 +1894,7 @@ opengl_renderer::Forget_Node( scene::basic_node const *Node ) { if( m_picksceneryitem == Node ) { m_picksceneryitem = nullptr; } - std::replace_if( m_picksceneryitems.begin(), m_picksceneryitems.end(), [Node]( scene::basic_node const *Item ) { return Item == Node; }, nullptr ); + std::ranges::replace_if( m_picksceneryitems, [Node]( scene::basic_node const *Item ) { return Item == Node; }, nullptr ); } void diff --git a/rendering/renderer.h b/rendering/renderer.h index e9dc8a47c..5850e1754 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -91,7 +91,9 @@ public: virtual auto Pick_Control() const -> TSubModel const * = 0; virtual auto Pick_Node() const -> scene::basic_node const * = 0; // a node deleted from the scene is no longer given as picked - virtual void Forget_Node( scene::basic_node const *Node ) {} + virtual void Forget_Node( scene::basic_node const * /*Node*/ ) { + // a renderer which keeps no node picked has none to forget + } virtual auto Mouse_Position() const -> glm::dvec3 = 0; virtual auto Mouse_Hit() const -> bool { return true; } diff --git a/simulation/simulationstateserializer.cpp b/simulation/simulationstateserializer.cpp index 78955c828..664f3b955 100644 --- a/simulation/simulationstateserializer.cpp +++ b/simulation/simulationstateserializer.cpp @@ -36,6 +36,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/utilities.h" #include "editor/editorTerrainStreamer.hpp" +#include namespace simulation { @@ -542,13 +543,14 @@ state_serializer::deserialize_node( cParser &Input, scene::scratch_data &Scratch } else if( nodedata.type == "track" ) { - auto *path { deserialize_path( Input, Scratchpad, nodedata ) }; - if( path->eType == tt_Unknown ) { - // with no path made it can't be joined nor drawn, and nothing could run on it - ErrorLog( "Bad scenario: track \"" + path->name() + "\" of an unknown type defined in file \"" + Input.Name() + "\" (line " + std::to_string( inputline ) + ") left out" ); - delete path; + std::unique_ptr made { deserialize_path( Input, Scratchpad, nodedata ) }; + if( made->eType == tt_Unknown ) { + // with no path made it can't be joined nor drawn, and nothing could run on it: it goes with its holder + ErrorLog( std::format( "Bad scenario: track \"{}\" of an unknown type defined in file \"{}\" (line {}) left out", made->name(), Input.Name(), inputline ) ); return; } + // from here on the scene owns it + auto *path { made.release() }; // duplicates of named tracks are currently experimentally allowed if( false == simulation::Paths.insert( path ) ) { ErrorLog( "Bad scenario: duplicate track name \"" + path->name() + "\" defined in file \"" + Input.Name() + "\" (line " + std::to_string( inputline ) + ")" ); diff --git a/utilities/Logs.cpp b/utilities/Logs.cpp index 18020d6f1..38094d72e 100644 --- a/utilities/Logs.cpp +++ b/utilities/Logs.cpp @@ -73,12 +73,19 @@ std::deque ErrorStack; // lock for log stacks std::mutex logMutex; -// the log is written through the closing of the simulator, to the end -std::atomic logStop{false}; +namespace +{ +// the log is written through the closing of the simulator, to the end: set once the closing has nothing more to tell +std::atomic &log_stopped() +{ + static std::atomic stopped{false}; + return stopped; +} +} // namespace void StopLogService() { - logStop = true; + log_stopped() = true; } @@ -87,7 +94,7 @@ void LogService() // one more pass after the stop, so what came just before it gets written too for (bool last{false}; false == last;) { - last = logStop; + last = log_stopped(); { // --- Obsługa InfoStack --- while (!InfoStack.empty()) diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index b25c6b31d..cf9003f4d 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -471,23 +471,22 @@ bool FileExists(std::string const &Filename) return std::filesystem::exists(Filename); } -std::string FileCased(std::string const &Filename) +std::string FileCased(std::filesystem::path const &Filename) { std::error_code error; if (std::filesystem::exists(Filename, error)) - return Filename; + return Filename.string(); #ifdef _WIN32 // the names there are found in any case already: no folder to go through for every file missing, nor names outside of the // code page to throw on return {}; #else // only the name of the file itself, a folder named in another case isn't found - std::filesystem::path const path{Filename}; - auto const directory{path.has_parent_path() ? path.parent_path() : std::filesystem::path{"."}}; - auto const wanted{ToLower(path.filename().string())}; + auto const directory{Filename.has_parent_path() ? Filename.parent_path() : std::filesystem::path{"."}}; + auto const wanted{ToLower(Filename.filename().string())}; for (std::filesystem::directory_iterator entry{directory, error}, end; entry != end; entry.increment(error)) if (ToLower(entry->path().filename().string()) == wanted) - return path.has_parent_path() ? (directory / entry->path().filename()).string() : entry->path().filename().string(); + return Filename.has_parent_path() ? (directory / entry->path().filename()).string() : entry->path().filename().string(); return {}; #endif } diff --git a/utilities/utilities.h b/utilities/utilities.h index f16431dde..cc4056cae 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -219,7 +219,7 @@ template <> bool extract_value(bool &Variable, std::string const &Key, std::stri bool FileExists(std::string const &Filename); // the file as it's named on the disk when only the case of its name differs, as in the sceneries made on Windows; empty when // there's none. the folders have to be named in their case; on Windows, finding the names in any case itself, the file given or none -std::string FileCased(std::string const &Filename); +std::string FileCased(std::filesystem::path const &Filename); std::pair FileExists(std::vector const &Names, std::vector const &Extensions); diff --git a/world/Track.cpp b/world/Track.cpp index 14998ce7c..c6339a424 100644 --- a/world/Track.cpp +++ b/world/Track.cpp @@ -29,6 +29,8 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Logs.h" #include "rendering/renderer.h" +#include + // 101206 Ra: trapezoidalne drogi i tory // 110720 Ra: rozprucie zwrotnicy i odcinki izolowane @@ -429,7 +431,7 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin) { // dopływ: no path of it was ever made, the simulator falling over it as it loaded. a river of its first path now, the // second one kept and written back - ErrorLog("Bad track: tributary \"" + m_name + "\" in file \"" + parser->Name() + "\" (line " + std::to_string(parser->Line()) + ") is drawn and run on as a river of its first path"); + ErrorLog(std::format("Bad track: tributary \"{}\" in file \"{}\" (line {}) is drawn and run on as a river of its first path", m_name, parser->Name(), parser->Line())); eType = tt_Normal; iCategoryFlag = 4; m_tributary = true; @@ -2319,25 +2321,37 @@ TTrack::deserialize_( std::istream &Input ) { // TODO: implement } +// the keyword the track is written with, by its type and for a plain one by what runs on it +char const * +TTrack::type_keyword() const { + + if( eType == tt_Normal && m_tributary ) { + return "tributary"; + } + if( eType == tt_Normal ) { + switch( iCategoryFlag ) { + case 1: { return "normal"; } + case 2: { return "road"; } + case 4: { return "river"; } + default: { return "none"; } + } + } + switch( eType ) { + case tt_Switch: { return "switch"; } + case tt_Cross: { return "cross"; } + case tt_Table: { return "turn"; } + case tt_Tributary: { return "tributary"; } + default: { return "none"; } + } +} + // export() subclass details, sends basic content of the class in legacy (text) format to provided stream void TTrack::export_as_text_( std::ostream &Output ) const { // header Output << "track "; // type - Output << ( - eType == tt_Normal ? ( - m_tributary ? "tributary" : - iCategoryFlag == 1 ? "normal" : - iCategoryFlag == 2 ? "road" : - iCategoryFlag == 4 ? "river" : - "none" ) : - eType == tt_Switch ? "switch" : - eType == tt_Cross ? "cross" : - eType == tt_Table ? "turn" : - eType == tt_Tributary ? "tributary" : - "none" ) - << ' '; + Output << type_keyword() << ' '; // basic attributes Output << Length() << ' ' diff --git a/world/Track.h b/world/Track.h index 5952da6a7..70431072a 100644 --- a/world/Track.h +++ b/world/Track.h @@ -358,6 +358,8 @@ private: void deserialize_( std::istream &Input ); // export() subclass details, sends basic content of the class in legacy (text) format to provided stream void export_as_text_( std::ostream &Output ) const; + // the keyword the track is written with, by its type and for a plain one by what runs on it + char const *type_keyword() const; // locates specified profile in the profile database, potentially loading it from a file static std::pair fetch_track_rail_profile( std::string const &Profile ); // loads content of specified file and converts it into a vertex array From d232cd0a6937dda6bb482fac2a9b23e10bdd7fb7 Mon Sep 17 00:00:00 2001 From: maj00r Date: Fri, 6 Nov 2026 19:58:24 +0100 Subject: [PATCH 36/90] refactor: the Sonar remarks on the material copies taken care of - the params of a shader named by the type the shader keeps them in - ranges::any_of and erase_if, with explicit captures - the weathering set component by component with an index of the type of the vector - the lookups declared in the ifs that use them - the empty Erase of the base renderer told why it is empty --- model/material.cpp | 51 ++++++++++++++++++++++---------------------- rendering/renderer.h | 4 +++- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/model/material.cpp b/model/material.cpp index 2b92986d0..4e96ddd8c 100644 --- a/model/material.cpp +++ b/model/material.cpp @@ -684,17 +684,20 @@ variant_of( opengl_material const &Source, std::string const &Suffix ) { return variant; } +// the params of a shader, as the shader keeps them +using param_confs = decltype( gl::shader::param_conf ); + // whether another param of the shader lies on a component of the named one, which a value set for that one would change too bool -shares_slot( std::unordered_map const &Conf, std::string const &Name ) { +shares_slot( param_confs const &Conf, std::string const &Name ) { auto const own { Conf.find( Name ) }; if( own == Conf.end() ) { return false; } - return std::any_of( - std::begin( Conf ), std::end( Conf ), - [&]( auto const &Entry ) { + return std::ranges::any_of( + Conf, + [ &Name, &own ]( auto const &Entry ) { return Entry.first != Name && Entry.second.location == own->second.location && Entry.second.offset < own->second.offset + own->second.size @@ -715,15 +718,16 @@ make_matte( opengl_material &Variant ) { || shares_slot( Variant.shader->param_conf, key ) ) { continue; } - for( std::size_t i = 0; i < entry->second.size; ++i ) { - Variant.params[ entry->second.location ][ entry->second.offset + i ] = value[ static_cast( i ) ]; + auto &slot { Variant.params[ entry->second.location ] }; + auto const offset { static_cast( entry->second.offset ) }; + auto const size { static_cast( entry->second.size ) }; + for( glm::length_t i = 0; i < size; ++i ) { + slot[ offset + i ] = value[ i ]; } - Variant.params_state.erase( - std::remove_if( - std::begin( Variant.params_state ), std::end( Variant.params_state ), - [&]( gl::shader::param_entry const &State ) { - return State.location == entry->second.location && State.offset == entry->second.offset; } ), - std::end( Variant.params_state ) ); + std::erase_if( + Variant.params_state, + [ &entry ]( gl::shader::param_entry const &State ) { + return State.location == entry->second.location && State.offset == entry->second.offset; } ); } } @@ -737,26 +741,24 @@ make_tinted( opengl_material &Variant, glm::vec3 const &Colour ) { } auto const &conf { Variant.shader->param_conf }; auto const weathering { conf.find( "weathering" ) }; - auto const tint { conf.find( "tint" ) }; - if( weathering == conf.end() + if( auto const tint { conf.find( "tint" ) }; + weathering == conf.end() || tint == conf.end() || weathering->second.offset != 3 || tint->second.location != weathering->second.location || tint->second.offset != 0 - || std::any_of( - std::begin( conf ), std::end( conf ), - [&]( auto const &Entry ) { + || std::ranges::any_of( + conf, + [ &weathering, &tint ]( auto const &Entry ) { return Entry.first != weathering->first && Entry.first != tint->first && Entry.second.location == weathering->second.location; } ) ) { return false; } auto const location { weathering->second.location }; Variant.params[ location ] = glm::vec4{ Colour, -1.f }; - Variant.params_state.erase( - std::remove_if( - std::begin( Variant.params_state ), std::end( Variant.params_state ), - [&]( gl::shader::param_entry const &State ) { - return State.location == location; } ), - std::end( Variant.params_state ) ); + std::erase_if( + Variant.params_state, + [ location ]( gl::shader::param_entry const &State ) { + return State.location == location; } ); return true; } @@ -768,8 +770,7 @@ material_manager::create_matte( material_handle const Material ) { if( Material == null_handle || Material >= m_materials.size() ) { return Material; } - auto const lookup { m_mattes.find( Material ) }; - if( lookup != m_mattes.end() ) { + if( auto const lookup { m_mattes.find( Material ) }; lookup != m_mattes.end() ) { return lookup->second; } auto matte { variant_of( m_materials[ Material ], ":matte" ) }; diff --git a/rendering/renderer.h b/rendering/renderer.h index 4dabc204a..1d60a1d95 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -60,7 +60,9 @@ public: // adds supplied vertex data at the end of specified chunk virtual auto Append(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type) -> bool = 0; // empties specified chunk, its handle stays valid to be filled again - virtual void Erase( gfx::geometry_handle const &Geometry ) {} + virtual void Erase( gfx::geometry_handle const & /*Geometry*/ ) { + // a renderer which keeps no chunks of its own has none to empty + } // provides direct access to index data of specfied chunk virtual auto Indices( gfx::geometry_handle const &Geometry ) const->gfx::index_array const & = 0; // provides direct access to vertex data of specfied chunk From e0cef91049c3089c0ac0a2bb93a78d2dc53595ba Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 04:48:15 +0100 Subject: [PATCH 37/90] fix(physics): running backwards, the camshaft goes back down from the last series position (eu07/maszyna#393) (#256) Running backwards, AutoRelayCheck() keeps the camshaft from joining the motors in more branches than BackwardsBranchesAllowed (the series connection alone, by default). It did that by returning before any move of the camshaft, so once it reached the last series position (22 of 43 in the Skoda 181 and 182, the next one joins the motors in two branches) it stayed there whatever the driver did with the master controller: with the controller back at zero the line contactors stayed closed and the motors kept pulling, until the line contactors were opened by hand. Now the limit only keeps the camshaft from going further up; it goes back down as usual. Tests: tests/unit/test_camshaft.cpp, on a locomotive made after the Skoda 181 (its RList); the test definitions take the RList and MotorParamTable0 tables. --- McZapkie/Mover.cpp | 11 ++- tests/unit/test_camshaft.cpp | 135 +++++++++++++++++++++++++++++++++++ tests/unit/vehicle.cpp | 2 + 3 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 tests/unit/test_camshaft.cpp diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 954e277ba..6b7bdcec5 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -6904,13 +6904,12 @@ bool TMoverParameters::AutoRelayCheck(void) } else { // zmieniaj mainctrlactualpos - if (DirActive < 0 && TrainType != dt_PseudoDiesel && RList[MainCtrlActualPos + 1].Bn > BackwardsBranchesAllowed) - { - return false; // nie poprawiamy przy konwersji - // return ARC;// bbylo exit; //Ra: to powoduje, że EN57 nie wyłącza się przy IminLo - } + // running backwards, the motors don't get joined in more branches than allowed (the series connection + // alone, usually); the camshaft still goes back down from there (eu07/maszyna#393: it stayed where it + // was, the line contactors closed, whatever the driver did with the master controller) + auto const backwardslimit{DirActive < 0 && TrainType != dt_PseudoDiesel && RList[MainCtrlActualPos + 1].Bn > BackwardsBranchesAllowed}; // main bez samoczynnego rozruchu - if (MainCtrlActualPos < std::size(RList) - 1 // crude guard against running out of current fixed table + if (false == backwardslimit && MainCtrlActualPos < std::size(RList) - 1 // crude guard against running out of current fixed table && (RList[MainCtrlActualPos].Relay < MainCtrlPos || (RList[MainCtrlActualPos + 1].Relay == MainCtrlPos && MainCtrlActualPos < RlistSize) || (TrainType == dt_ET22 && DelayCtrlFlag))) { diff --git a/tests/unit/test_camshaft.cpp b/tests/unit/test_camshaft.cpp new file mode 100644 index 000000000..940616a62 --- /dev/null +++ b/tests/unit/test_camshaft.cpp @@ -0,0 +1,135 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +using test::definition; +using test::vehicle; + +// eu07/maszyna#393: running backwards, a camshaft which reached the last position of the series connection (the +// next one already joins the motors in more branches than BackwardsBranchesAllowed) stayed there whatever the driver +// did with the master controller, with the line contactors closed and the motors pulling, until the line contactors +// were opened by hand + +namespace { + +// the starting resistances of the Skoda 181 (cd/181_v2): 6 motors in series up to position 22, then two branches of +// 3 up to 34, then three branches of 2 up to 43; columns: position, resistance, branches, motors in a branch, auto +nlohmann::ordered_json skoda_rlist() { + nlohmann::ordered_json rows{nlohmann::ordered_json::array({0, 0, 0, 0, 0}), nlohmann::ordered_json::array({1, 999999, 1, 6, 0})}; + auto const series{std::vector{15.87, 12.32, 10.03, 8.54, 6.98, 5.84, 5.35, 4.87, 3.88, 2.92, 2.68, 2.43, 2.19, 1.95, 1.70, 1.46, 1.22, 0.97, 0.49, 0.24, 0.00}}; + auto const seriesparallel{std::vector{4.50, 3.63, 3.11, 2.42, 2.08, 1.73, 1.38, 1.04, 0.69, 0.35, 0.17, 0.00}}; + auto const parallel{std::vector{2.78, 2.29, 1.80, 1.41, 1.02, 0.73, 0.44, 0.24, 0.00}}; + auto position{2}; + for (auto const resistance : series) { + rows.push_back(nlohmann::ordered_json::array({position++, resistance, 1, 6, 0})); + } + for (auto const resistance : seriesparallel) { + rows.push_back(nlohmann::ordered_json::array({position++, resistance, 2, 3, 0})); + } + for (auto const resistance : parallel) { + rows.push_back(nlohmann::ordered_json::array({position++, resistance, 3, 2, 0})); + } + return rows; +} + +// an electric locomotive with a camshaft, made after the Skoda 181 +definition skoda() { + auto result{definition::load("wagon") + .set("Param", "M", 124200) + .set("Param", "Vmax", 90) + .set("Dimensions", "L", 18.8) + .set("Wheels", "D", 1.25) + .set("Wheels", "Axle", "Co'Co'") + .set("Engine", "EngineType", "ElectricSeriesMotor") + .set("Engine", "Volt", 967) + .set("Engine", "Trans", "21:89") + .set("Engine", "WindingRes", 0.084) + .set("Engine", "nmax", 1870) + .set("Circuit", "CircuitRes", 0.01) + .set("Circuit", "ImaxLo", 550) + .set("Circuit", "ImaxHi", 550) + .set("Circuit", "IminLo", 307) + .set("Circuit", "IminHi", 332) + .set("Cntrl", "MCPN", 43) + .set("Cntrl", "SCPN", 3) + .set("Cntrl", "CoupledCtrl", "No") + .set("Cntrl", "IniCDelay", 0.2) + .set("Cntrl", "SCDelay", 0.18) + .set("Cntrl", "SCDDelay", 0.18) + .set("Cntrl", "Camshaft", "Yes") + .set("MotorParamTable0", "rows", + nlohmann::ordered_json::array({nlohmann::ordered_json::array({0, 33.75, 57, 0.4, 225.70, 57, 0.4}), nlohmann::ordered_json::array({1, 31.66, 69, 0.4, 217.10, 69, 0.4}), + nlohmann::ordered_json::array({2, 30.52, 90, 0.4, 212.80, 90, 0.4}), nlohmann::ordered_json::array({3, 28.74, 107, 0.4, 199.55, 107, 0.4})})) + .set("RList", "Size", 43) + .set("RList", "rows", skoda_rlist())}; + return result; +} + +// the locomotive running with the line contactors closed and the camshaft at Position, the master controller moved +// to Controller, in the given direction; the time since the last step of the camshaft long enough for the next one +void running(TMoverParameters &Locomotive, int const Direction, int const Position, int const Controller) { + Locomotive.CabActive = 1; + Locomotive.DirActive = Direction; + Locomotive.Mains = true; + Locomotive.PipePress = 5.0; // bar, the pressure switch lets the line contactors close + Locomotive.BrakePress = 0.0; + Locomotive.StLinFlag = true; + Locomotive.MainCtrlActualPos = Position; + Locomotive.MainCtrlPos = Controller; + Locomotive.LastRelayTime = 10.0; +} + +// steps the camshaft as the physics does, each step once the delay since the previous one passed; returns where it +// ended up +int run_camshaft(TMoverParameters &Locomotive, int const Steps) { + for (int step = 0; step < Steps; ++step) { + Locomotive.LastRelayTime = 10.0; + Locomotive.AutoRelayCheck(); + } + return Locomotive.MainCtrlActualPos; +} + +} // namespace + +TEST_CASE("camshaft: running backwards, the camshaft goes back down from the last series position (eu07/maszyna#393)") { + vehicle locomotive{skoda()}; + auto &mover{locomotive.mover()}; + REQUIRE(mover.HasCamshaft); + REQUIRE(mover.RlistSize == 43); + REQUIRE(mover.RList[22].Bn == 1); + REQUIRE(mover.RList[23].Bn == 2); + + SUBCASE("the controller turned back a few positions") { + running(mover, -1, 22, 15); + CHECK(run_camshaft(mover, 1) == 21); // it used to stay at 22 + CHECK(run_camshaft(mover, 10) == 15); + } + SUBCASE("the controller turned back to zero: the camshaft runs down and the line contactors open") { + running(mover, -1, 22, 0); + run_camshaft(mover, 30); + CHECK(mover.MainCtrlActualPos == 0); + CHECK_FALSE(mover.StLinFlag); // the motors don't pull any more + } +} + +TEST_CASE("camshaft: running backwards, the camshaft still doesn't go past the series connection") { + vehicle locomotive{skoda()}; + auto &mover{locomotive.mover()}; + running(mover, -1, 20, 43); + CHECK(run_camshaft(mover, 10) == 22); + + SUBCASE("unless more branches are allowed backwards") { + mover.BackwardsBranchesAllowed = 2; + CHECK(run_camshaft(mover, 20) == 34); + } +} + +TEST_CASE("camshaft: running forwards, the camshaft goes through all the connections and back") { + vehicle locomotive{skoda()}; + auto &mover{locomotive.mover()}; + running(mover, 1, 20, 43); + CHECK(run_camshaft(mover, 30) == 43); + + mover.MainCtrlPos = 10; + CHECK(run_camshaft(mover, 40) == 10); +} diff --git a/tests/unit/vehicle.cpp b/tests/unit/vehicle.cpp index c0930d134..914a31079 100644 --- a/tests/unit/vehicle.cpp +++ b/tests/unit/vehicle.cpp @@ -45,6 +45,8 @@ std::map> const sections{ {"DimmerList", {':', "endDimmerList"}}, {"WiperList", {':', "endwl"}}, {"CompressorList", {':', "endCL"}}, + {"RList", {':', "END-RL"}}, + {"MotorParamTable0", {':', "END-MPT"}}, }; section_format const &format_of(std::string const &Section) { From 28cf5fe276ccc61087da9d1800b9c5738094af6f Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Thu, 15 Oct 2026 22:26:13 +0200 Subject: [PATCH 38/90] fix(gfx): a vehicle taken off the tracks stops smoking (#90) The smoke sources of a vehicle stay in the particle manager when the vehicle is taken off the tracks: one gone through a portal, or a road vehicle waiting in the pool of its spawn point. They kept spawning particles where the vehicle had vanished, for good. A source of a disabled vehicle spawns no more particles now, the ones it let out fade away as usual, and a pooled road vehicle puffs again once it's back on the road. Checked on td.scn with a portal added at the dead end of tdo_n585 and an SM42 driven into it by the AI: on master its smoke source kept about 100 particles where it vanished for the rest of the run (over 200 s); with the fix they were gone within a second. --- rendering/particles.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/rendering/particles.cpp b/rendering/particles.cpp index 5f7158e0c..6719eb6df 100644 --- a/rendering/particles.cpp +++ b/rendering/particles.cpp @@ -162,8 +162,15 @@ smoke_source::update( double const Timedelta, bool const Onlydespawn ) { glm::dvec3{ std::numeric_limits::max() }, glm::dvec3{ std::numeric_limits::lowest() } }; + // a vehicle taken off the tracks (gone through a portal, or waiting in the pool of the road traffic) puffs no more, + // the particles it already let out fade away as usual + auto const nospawn { + false == Global.Smoke + || true == Onlydespawn + || ( m_ownertype == owner_type::vehicle && false == m_owner.vehicle->bEnabled ) }; + m_spawncount = ( - ( ( false == Global.Smoke ) || ( true == Onlydespawn ) ) ? + ( true == nospawn ) ? 0.f : std::min( static_cast(m_spawncount + ( m_spawnrate * Timedelta * Global.SmokeFidelity )), @@ -179,7 +186,7 @@ smoke_source::update( double const Timedelta, bool const Onlydespawn ) { if (m_owner.vehicle->MoverParameters->dizel_spinup == true) { m_spawncount = - ((false == Global.Smoke) || (true == Onlydespawn)) ? + (true == nospawn) ? 0.f : std::min( // m_spawncount + ( m_spawnrate * Timedelta * Global.SmokeFidelity ), @@ -191,7 +198,7 @@ smoke_source::update( double const Timedelta, bool const Onlydespawn ) { m_owner.vehicle->MoverParameters->Im == 0) { m_spawncount = - ((false == Global.Smoke) || (true == Onlydespawn)) ? + (true == nospawn) ? 0.f : std::min( // m_spawncount + ( m_spawnrate * Timedelta * Global.SmokeFidelity ), @@ -201,7 +208,7 @@ smoke_source::update( double const Timedelta, bool const Onlydespawn ) { { m_spawncount = - ((false == Global.Smoke) || (true == Onlydespawn)) ? + (true == nospawn) ? 0.f : std::min( // m_spawncount + ( m_spawnrate * Timedelta * Global.SmokeFidelity ), From f94b3080629523d991683fabff7f6ec3584534d8 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Thu, 15 Oct 2026 22:26:13 +0200 Subject: [PATCH 39/90] fix(sound): check for the player train in the outer noise test of a vehicle The test whether a vehicle belongs to the player train dereferenced simulation::Train without a null check (commented out long ago). The driver mode switches to the free camera in the same frame the player train goes through a portal, before the sounds are updated, so it isn't known to crash, but nothing else guards it. --- vehicle/DynObj.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index c6793e454..6f151f0e8 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -4863,7 +4863,7 @@ void TDynamicObject::RenderSounds() { #endif && ( // compound test whether the vehicle belongs to user-driven consist (as these don't emit outer noise in cab view) FreeFlyModeFlag ? true : // in external view all vehicles emit outer noise - // Global.pWorld->train() == nullptr ? true : // (can skip this check, with no player train the external view is a given) + simulation::Train == nullptr ? true : // no player train, e.g. it went through a portal and the view isn't switched yet ctOwner == nullptr ? true : // standalone vehicle, can't be part of user-driven train ctOwner != simulation::Train->Dynamic()->ctOwner ? true : // confirmed isn't a part of the user-driven train Global.CabWindowOpen ? true : // sticking head out we get to hear outer noise From 4489dd7469e16437a125d1bc399d7127f54237dc Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 11:38:49 +0100 Subject: [PATCH 40/90] test(gfx): unit tests of the smoke of a vehicle taken off the tracks (#90) The spawn count of the smoke source of a vehicle moves out of smoke_source::update() into smoke::vehicle_spawn_count() (vehicle/smoke.h), a free function on the mover state, which joins the core library so the unit tests can call it the way the source does. The rates are the same as before, the steady rate of the source still counted in before the share of the engine. Tests: tests/unit/test_smoke.cpp. A vehicle taken off the tracks spawns no smoke and drops the pending particles, with the engine starting, idling or under load; a road vehicle back from the pool puffs again; the engine adds to the steady rate; the pending count stays within the room of the source. Two of the four fail without the fix. Checked again on td.scn with the SM42 driven into a portal: the smoke source keeps puffing on the run and drops to 0 particles once the vehicle is taken off the tracks. --- CMakeLists.txt | 1 + rendering/particles.cpp | 75 +++++++++++----------------------- tests/unit/test_smoke.cpp | 85 +++++++++++++++++++++++++++++++++++++++ vehicle/smoke.cpp | 46 +++++++++++++++++++++ vehicle/smoke.h | 30 ++++++++++++++ 5 files changed, 186 insertions(+), 51 deletions(-) create mode 100644 tests/unit/test_smoke.cpp create mode 100644 vehicle/smoke.cpp create mode 100644 vehicle/smoke.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dfe58b66d..9d19e844c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -463,6 +463,7 @@ set(CORE_SOURCES "scripting/ladderlogic.cpp" "vehicle/handover.cpp" "vehicle/horn.cpp" +"vehicle/smoke.cpp" ) add_library(eu07_core STATIC ${CORE_SOURCES}) target_link_libraries(eu07_core PUBLIC eu07_settings) diff --git a/rendering/particles.cpp b/rendering/particles.cpp index 6719eb6df..8ccb728dd 100644 --- a/rendering/particles.cpp +++ b/rendering/particles.cpp @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "model/AnimModel.h" #include "simulation/simulationenvironment.h" #include "utilities/Logs.h" +#include "vehicle/smoke.h" void @@ -162,60 +163,32 @@ smoke_source::update( double const Timedelta, bool const Onlydespawn ) { glm::dvec3{ std::numeric_limits::max() }, glm::dvec3{ std::numeric_limits::lowest() } }; - // a vehicle taken off the tracks (gone through a portal, or waiting in the pool of the road traffic) puffs no more, - // the particles it already let out fade away as usual - auto const nospawn { - false == Global.Smoke - || true == Onlydespawn - || ( m_ownertype == owner_type::vehicle && false == m_owner.vehicle->bEnabled ) }; + auto const spawning { ( true == Global.Smoke ) && ( false == Onlydespawn ) }; - m_spawncount = ( - ( true == nospawn ) ? - 0.f : - std::min( - static_cast(m_spawncount + ( m_spawnrate * Timedelta * Global.SmokeFidelity )), - static_cast(m_max_particles) ) ); - // consider special spawn rate cases if( m_ownertype == owner_type::vehicle ) { - if (Global.AirTemperature <= 5.f || m_owner.vehicle->MoverParameters->dizel_heat.Ts < 45.f) - { - m_emitter.color = glm::vec3{128, 128, 128}; - } - - if (m_owner.vehicle->MoverParameters->dizel_spinup == true) - { - m_spawncount = - (true == nospawn) ? - 0.f : - std::min( - // m_spawncount + ( m_spawnrate * Timedelta * Global.SmokeFidelity ), - static_cast(m_spawncount +(m_spawnrate * Timedelta * Global.SmokeFidelity * (((m_owner.vehicle->MoverParameters->enrot) / 4 ) * 0.01))), static_cast(m_max_particles)); - } - else - { - if (m_owner.vehicle->MoverParameters->DirAbsolute == 0 || - m_owner.vehicle->MoverParameters->Im == 0) - { - m_spawncount = - (true == nospawn) ? - 0.f : - std::min( - // m_spawncount + ( m_spawnrate * Timedelta * Global.SmokeFidelity ), - static_cast(m_spawncount + (m_spawnrate * Timedelta * Global.SmokeFidelity * ((((m_owner.vehicle->MoverParameters->DElist[m_owner.vehicle->MoverParameters->MainCtrlPosNo].RPM -m_owner.vehicle->MoverParameters->enrot) /60) *0.02) *(m_owner.vehicle->MoverParameters->EnginePower * 0.005)))), static_cast(m_max_particles)); - } - else - { - - m_spawncount = - (true == nospawn) ? - 0.f : - std::min( - // m_spawncount + ( m_spawnrate * Timedelta * Global.SmokeFidelity ), - static_cast(m_spawncount + (m_spawnrate * Timedelta * Global.SmokeFidelity * ((((m_owner.vehicle->MoverParameters->DElist[m_owner.vehicle->MoverParameters->MainCtrlPosNo].RPM -m_owner.vehicle->MoverParameters->enrot) /60) * (sqrt(m_owner.vehicle->MoverParameters->Im) * 0.01) * 0.02) *(m_owner.vehicle->MoverParameters->EnginePower * 0.005)))), static_cast(m_max_particles)); - } - } - } + auto const &mover { *m_owner.vehicle->MoverParameters }; + if( Global.AirTemperature <= 5.f || mover.dizel_heat.Ts < 45.f ) { + m_emitter.color = glm::vec3{ 128, 128, 128 }; + } + // a vehicle taken off the tracks (gone through a portal, or waiting in the pool of the road traffic) puffs no more, + // the particles it already let out fade away as usual + m_spawncount = ( + ( true == spawning ) ? + smoke::vehicle_spawn_count( + mover, m_owner.vehicle->bEnabled, + { m_spawncount, m_spawnrate, Global.SmokeFidelity, static_cast( m_max_particles ) }, + Timedelta ) : + 0.f ); + } + else { + m_spawncount = ( + ( true == spawning ) ? + std::min( + static_cast(m_spawncount + ( m_spawnrate * Timedelta * Global.SmokeFidelity )), + static_cast(m_max_particles) ) : + 0.f ); + } // update spawned particles for( auto particleiterator { std::begin( m_particles ) }; particleiterator != std::end( m_particles ); ++particleiterator ) { diff --git a/tests/unit/test_smoke.cpp b/tests/unit/test_smoke.cpp new file mode 100644 index 000000000..05fb8ae8b --- /dev/null +++ b/tests/unit/test_smoke.cpp @@ -0,0 +1,85 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle.h" + +#include "vehicle/smoke.h" + +// the smoke of a vehicle's engine: how many particles the smoke source of the vehicle's model spawns in an update + +namespace { + +// a source of 20 particles per second, with room for 100 +smoke::spawn_rate const source{0.f, 20.f, 1.f, 100.f}; +double const update{0.1}; // s + +// a diesel at work, its engine in the state of one of the cases below +test::vehicle diesel() { + test::vehicle vehicle{test::definition::load("wagon")}; + vehicle->MainCtrlPosNo = 1; + vehicle->DElist[1].RPM = 1500.0; + vehicle->enrot = 300.0; + vehicle->EnginePower = 400.0; + return vehicle; +} + +} // namespace + +TEST_CASE("smoke: a vehicle taken off the tracks puffs no more (#90)") { + auto vehicle{diesel()}; + SUBCASE("engine starting") { vehicle->dizel_spinup = true; } + SUBCASE("engine idling") { vehicle->DirAbsolute = 0; } + SUBCASE("engine under load") { + vehicle->DirAbsolute = 1; + vehicle->Im = 400.0; + } + // on the tracks the engine puffs + REQUIRE(smoke::vehicle_spawn_count(vehicle.mover(), true, source, update) >= 1.f); + + // gone through a portal, the pending particles are dropped too + auto pending{source}; + pending.pending = 5.f; + CHECK(smoke::vehicle_spawn_count(vehicle.mover(), false, pending, update) == 0.f); +} + +TEST_CASE("smoke: a road vehicle back on the road from the pool puffs again") { + auto vehicle{diesel()}; + // waiting in the pool, the count stays at zero update after update + auto rate{source}; + for (auto i{0}; i < 10; ++i) { + rate.pending = smoke::vehicle_spawn_count(vehicle.mover(), false, rate, update); + } + REQUIRE(rate.pending == 0.f); + + CHECK(smoke::vehicle_spawn_count(vehicle.mover(), true, rate, update) >= 1.f); +} + +TEST_CASE("smoke: the engine adds to the steady rate of the source") { + auto vehicle{diesel()}; + vehicle->DirAbsolute = 0; + auto const steady{source.rate * update}; // the rate of a source of the scenery + + vehicle->EnginePower = 0.0; + CHECK(smoke::vehicle_spawn_count(vehicle.mover(), true, source, update) == doctest::Approx(steady)); + + vehicle->EnginePower = 400.0; + auto const working{smoke::vehicle_spawn_count(vehicle.mover(), true, source, update)}; + CHECK(working > steady); + + // the faster the starting engine turns, the more it puffs + vehicle->dizel_spinup = true; + vehicle->enrot = 100.0; + auto const slow{smoke::vehicle_spawn_count(vehicle.mover(), true, source, update)}; + vehicle->enrot = 400.0; + CHECK(smoke::vehicle_spawn_count(vehicle.mover(), true, source, update) > slow); +} + +TEST_CASE("smoke: the pending particles never outgrow the room of the source") { + auto vehicle{diesel()}; + vehicle->dizel_spinup = true; + vehicle->enrot = 100000.0; + auto rate{source}; + rate.pending = 99.f; + + CHECK(smoke::vehicle_spawn_count(vehicle.mover(), true, rate, 10.0) == rate.limit); +} diff --git a/vehicle/smoke.cpp b/vehicle/smoke.cpp new file mode 100644 index 000000000..b6e923222 --- /dev/null +++ b/vehicle/smoke.cpp @@ -0,0 +1,46 @@ +/* +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/smoke.h" + +#include "McZapkie/MOVER.h" + +namespace smoke { + +namespace { + +// how hard the engine works, scaling the spawn rate of the source +double engine_load(TMoverParameters const &Mover) { + if (Mover.dizel_spinup) { + // starting up, it puffs harder the faster it turns + return ((Mover.enrot / 4) * 0.01); + } + auto const rpmrise{(Mover.DElist[Mover.MainCtrlPosNo].RPM - Mover.enrot) / 60}; + if (Mover.DirAbsolute == 0 || Mover.Im == 0) { + // idling: speeding up to the revolutions of the master controller + return ((rpmrise * 0.02) * (Mover.EnginePower * 0.005)); + } + // under load: the more current the traction motors draw, the more smoke + return ((rpmrise * (std::sqrt(Mover.Im) * 0.01) * 0.02) * (Mover.EnginePower * 0.005)); +} + +} // namespace + +float vehicle_spawn_count(TMoverParameters const &Mover, bool const Ontracks, spawn_rate const &Rate, double const Timedelta) { + if (false == Ontracks) { + return 0.f; + } + // the steady rate of the source, the way a source of the scenery spawns... + auto const steady{std::min(static_cast(Rate.pending + (Rate.rate * Timedelta * Rate.fidelity)), Rate.limit)}; + // ...and on top of it the share of the engine + return std::min(static_cast(steady + (Rate.rate * Timedelta * Rate.fidelity * engine_load(Mover))), Rate.limit); +} + +} // namespace smoke diff --git a/vehicle/smoke.h b/vehicle/smoke.h new file mode 100644 index 000000000..e9dac2622 --- /dev/null +++ b/vehicle/smoke.h @@ -0,0 +1,30 @@ +/* +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 + +class TMoverParameters; + +// the smoke of a vehicle's engine, let out by the smoke sources of its model (rendering/particles.h) +namespace smoke { + +// what a smoke source spawns, before the engine of its vehicle is taken into account +struct spawn_rate { + float pending{0.f}; // particles left to spawn from the previous updates + float rate{0.f}; // particles per second + float fidelity{1.f}; // multiplier of the smoke settings + float limit{0.f}; // most particles the source can have +}; + +// number of particles the smoke source of a vehicle has to spawn after Timedelta seconds: the steady rate of the +// source, plus a share which grows with the work of its engine. a vehicle taken off the tracks (Ontracks false: gone through a portal, or waiting in the pool of the road +// traffic) puffs no more, the particles it already let out fade away as usual +float vehicle_spawn_count(TMoverParameters const &Mover, bool Ontracks, spawn_rate const &Rate, double Timedelta); + +} // namespace smoke From 088eeaa743d5e88ae39329238bc78e9da144f605 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Sat, 7 Nov 2026 12:32:02 +0100 Subject: [PATCH 41/90] fix(editor): a scenery saved under another name keeps one file of turntable controls, not the old one beside its own (#251) The editor writes the turntable controls on each save to _obrotnice.scm and includes it. A copy of a scenery, saved under another name, still included the file of the scenery it came from. Its save wrote a file of its own and included it too, so each turntable had its events twice. Now the save leaves out of the scenery the other files of turntable controls it includes, told by the line the editor writes at their start. Their files stay on the disk, for the scenery they were made for. A file of turntables made by hand, without that line, stays included. Co-authored-by: maj00r --- application/editormodeturntable.cpp | 32 ++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/application/editormodeturntable.cpp b/application/editormodeturntable.cpp index e6b78d9ef..76b70b575 100644 --- a/application/editormodeturntable.cpp +++ b/application/editormodeturntable.cpp @@ -19,6 +19,7 @@ http://mozilla.org/MPL/2.0/. #include "scene/scenelayers.h" #include "simulation/simulation.h" #include "utilities/Globals.h" +#include "utilities/Logs.h" #include "utilities/translation.h" #include "utilities/utilities.h" #include "world/Track.h" @@ -186,6 +187,20 @@ void label(screen_projection const &Projection, glm::dvec3 const &Point, std::st drawlist->AddText(ImVec2(at.x + 12.f, at.y), IM_COL32(255, 255, 255, 235), Text.c_str()); } +// the first line of the file of the turntable controls, which tells it from the files of the scenery made by hand +char const *const kControlsHeader{"// made by the scenery editor on each save: T turns each turntable to its next position, Shift+T to the previous one"}; + +// true if the file is one of the turntable controls the editor writes +bool controls_file(std::string const &Path) +{ + std::ifstream input{Path, std::ios_base::binary}; + std::string first; + std::getline(input, first); + if (false == first.empty() && first.back() == '\r') + first.pop_back(); + return first == kControlsHeader; +} + } // namespace void editor_mode::scan_turntables() @@ -1143,7 +1158,7 @@ bool editor_mode::turntable_controls(std::vector &Rootstatements) auto const exists{std::filesystem::exists(path, error)}; if (tables == 0 && false == exists) return false; - auto const content{"// made by the scenery editor on each save: T turns each turntable to its next position, Shift+T to the previous one\r\n" + text.str()}; + auto const content{std::string{kControlsHeader} + "\r\n" + text.str()}; std::string current; { std::ifstream input{path, std::ios_base::binary}; @@ -1154,6 +1169,21 @@ bool editor_mode::turntable_controls(std::vector &Rootstatements) std::ofstream output{path, std::ios_base::binary | std::ios_base::trunc}; output << content; } + // the controls written for this scenery under another name, the file it was copied from, give way to these: with both + // included each turntable would have its events twice. their files stay on the disk, for the scenery they were made for + for (std::size_t handle = 1; handle <= scene::Layers.size(); ++handle) + { + auto const layer{static_cast(handle)}; + if (false == scene::Layers.listed(layer) || scene::Layers.layer(layer).removed) + continue; + auto name{scene::Layers.layer(layer).name}; + std::replace(name.begin(), name.end(), '\\', '/'); + auto const lower{ToLower(name)}; + if (lower == ToLower(file) || lower.ends_with("/" + ToLower(file)) || false == lower.ends_with("_obrotnice.scm")) + continue; + if (controls_file(Global.asCurrentSceneryPath + scene::Layers.layer(layer).name) && scene::Layers.remove(layer)) + WriteLog("Editor: " + name + " left out of the scenery, the turntable controls are in " + file + " now"); + } auto included{m_turntable_included}; for (std::size_t handle = 1; handle <= scene::Layers.size() && false == included; ++handle) { From ecdef7f7537bbc9d76a7938fbd8cd74f7b4da5bf Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sat, 7 Nov 2026 12:33:22 +0100 Subject: [PATCH 42/90] opengl*renderer: Show an empty frame early (#233) Gives a better feedback to the user that something has actually launched. --- rendering/opengl33renderer.cpp | 4 ++++ rendering/openglrenderer.cpp | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index 76750472d..5c64988b4 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -104,6 +104,10 @@ bool opengl33_renderer::Init(GLFWwindow *Window) glDebugMessageCallback( ErrorCallback, 0 ); } + glClearColor( 0.f, 0.f, 0.f, 1.f ); + glClear( GL_COLOR_BUFFER_BIT ); + glfwSwapBuffers( Window ); + if (!Init_caps()) return false; diff --git a/rendering/openglrenderer.cpp b/rendering/openglrenderer.cpp index 7516f5a22..ef54f32c9 100644 --- a/rendering/openglrenderer.cpp +++ b/rendering/openglrenderer.cpp @@ -75,6 +75,10 @@ float const EU07_OPACITYDEFAULT { 0.5f }; bool opengl_renderer::Init( GLFWwindow *Window ) { + glClearColor( 0.f, 0.f, 0.f, 1.f ); + glClear( GL_COLOR_BUFFER_BIT ); + glfwSwapBuffers( Window ); + if( false == Init_caps() ) { return false; } m_window = Window; From 605a94cf2b66214ca4ebc24c841952c504a1f973 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sat, 7 Nov 2026 12:33:46 +0100 Subject: [PATCH 43/90] uilayer: Force ui viewports to be disabled under gamescope and Steam Big Picture (#232) --- application/uilayer.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/application/uilayer.cpp b/application/uilayer.cpp index 2b1fa1686..5443d158c 100644 --- a/application/uilayer.cpp +++ b/application/uilayer.cpp @@ -395,10 +395,15 @@ bool ui_layer::init(GLFWwindow *Window) } // panels can be docked together, and dragged out of the simulator window into windows of their own (e.g. on another monitor) - // when both backends can do it. glfw can't place windows under wayland. both flags have to be set before the first frame + // when both backends can do it. glfw can't place windows under wayland. gamescope does not work well with popup windows. + // both flags have to be set before the first frame m_imguiio->ConfigFlags |= ImGuiConfigFlags_DockingEnable; + const char* desktop = std::getenv("XDG_CURRENT_DESKTOP"); + const char* gamepadui = std::getenv("SteamGamepadUI"); + if (!desktop) desktop = ""; + if (!gamepadui) gamepadui = ""; auto const viewportsupport{(m_imguiio->BackendFlags & ImGuiBackendFlags_PlatformHasViewports) && (m_imguiio->BackendFlags & ImGuiBackendFlags_RendererHasViewports) && - glfwGetPlatform() != GLFW_PLATFORM_WAYLAND}; + glfwGetPlatform() != GLFW_PLATFORM_WAYLAND && std::string_view{desktop} != "gamescope" && std::string_view{gamepadui} != "1"}; if (Global.ui_viewports && viewportsupport) { m_imguiio->ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; From 3a29a165a9b79aee5ee7d7f687848da0bc07b33a Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sat, 7 Nov 2026 12:34:01 +0100 Subject: [PATCH 44/90] simulationenvironment: Don't multiply fog color by diffuselevel (#229) It can easily make the fog too dark compared to the environment, producing a "fire in the forest" effect. --- simulation/simulationenvironment.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/simulation/simulationenvironment.cpp b/simulation/simulationenvironment.cpp index 14ad2353c..bdc4b0598 100644 --- a/simulation/simulationenvironment.cpp +++ b/simulation/simulationenvironment.cpp @@ -167,9 +167,8 @@ world_environment::update() { // the impact of sun component is reduced proportionally to overcast level, as overcast increases role of ambient light auto const diffuselevel = std::lerp( keylightintensity, intensity * ( 1.0f - twilightfactor ), 1.0f - std::min( 1.f, Global.Overcast ) * 0.75f ); // ...update light colours and intensity. - keylightcolor = keylightcolor * diffuselevel; - Global.DayLight.diffuse = glm::vec4( keylightcolor, Global.DayLight.diffuse.a ); - Global.DayLight.specular = glm::vec4( keylightcolor * 0.85f, diffuselevel ); + Global.DayLight.diffuse = glm::vec4( keylightcolor * diffuselevel, Global.DayLight.diffuse.a ); + Global.DayLight.specular = glm::vec4( keylightcolor * diffuselevel * 0.85f, diffuselevel ); // tonal impact of skydome color is inversely proportional to how high the sun is above the horizon // (this is pure conjecture, aimed more to 'look right' than be accurate) From 55ba7a9f41e463dcb94f2adaf6859d8563df316c Mon Sep 17 00:00:00 2001 From: pawelw Date: Sat, 7 Nov 2026 12:35:55 +0100 Subject: [PATCH 45/90] Instancing: skip the buckets of a cell lying wholly beyond the range of their instances (#225) * perf(instancing): skip a cell's buckets lying wholly beyond the range of their instances * perf(instancing): go only through the instances drawn one by one in the per-node pass * perf(instancing): leave out of the color pass the cells holding only instances too far to be drawn * refactor(instancing): keep the caches of the instances of a cell without mutable fields, set by the renderer when the cell is queued --- model/AnimModel.cpp | 3 ++ model/AnimModel.h | 3 ++ rendering/opengl33renderer.cpp | 93 +++++++++++++++++++++++++++++++++- rendering/opengl33renderer.h | 6 +++ scene/scene.cpp | 3 ++ scene/scene.h | 13 +++++ 6 files changed, 119 insertions(+), 2 deletions(-) diff --git a/model/AnimModel.cpp b/model/AnimModel.cpp index 770790c1e..cafb3a93e 100644 --- a/model/AnimModel.cpp +++ b/model/AnimModel.cpp @@ -514,6 +514,7 @@ bool submodel_tree_blocks_instancing( TSubModel const *Sub ) { } // anonymous namespace int TAnimModel::s_instanceable_total = 0; +unsigned int TAnimModel::s_instanceable_revoked = 0; int TAnimModel::s_classified_total = 0; int TAnimModel::s_rejected_no_pmodel = 0; int TAnimModel::s_rejected_lights = 0; @@ -551,6 +552,7 @@ std::shared_ptr TAnimModel::AddContainer(std::string const &Name m_animlist.push_back(tmp); // containers are added by events after the instance was loaded and bucketed; // the instanced path skips RaAnimate(), so this instance has to be drawn on its own + if( m_instanceable ) { ++s_instanceable_revoked; } m_instanceable = false; return tmp; } @@ -780,6 +782,7 @@ void TAnimModel::SkinSet( int const Index, material_handle const Material ) { m_materialdata.replacable_skins[ std::clamp( Index, 1, 4 ) ] = Material; // the instance stays in the bucket of its old skin set, where it would get the textures of the batch + if( m_instanceable ) { ++s_instanceable_revoked; } m_instanceable = false; } diff --git a/model/AnimModel.h b/model/AnimModel.h index 0087418df..2cf1003bb 100644 --- a/model/AnimModel.h +++ b/model/AnimModel.h @@ -237,6 +237,9 @@ public: // diagnostic counters (process-wide). Updated inside update_instanceable_flag() // so the renderer can surface load-time classification stats in the debug overlay. static int s_instanceable_total; // count of TAnimModel*'s classified instanceable + // bumped whenever an instance loses its instanceable status after it was put in a scene cell (an event adds + // an animation container, or changes a skin); the cells rebuild their lists of instances drawn one by one then + static unsigned int s_instanceable_revoked; static int s_classified_total; // count of TAnimModel*'s that ran through the classifier static int s_rejected_no_pmodel; static int s_rejected_lights; diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index 5c64988b4..33346fad2 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -2666,6 +2666,12 @@ void opengl33_renderer::Render(section_sequence::iterator First, section_sequenc { if ((true == cell.m_active) && (m_renderpass.pass_camera.visible(cell.m_area))) { + // a cell with nothing but instances too far to be drawn is left out of the color pass altogether + if( ( true == Prepare_Instances( cell ) ) + && ( m_renderpass.draw_mode == rendermode::color ) + && ( true == Cell_Out_Of_Range( cell ) ) ) { + continue; + } // store visible cells with content as well as their current distance, for sorting later m_cellqueue.emplace_back(glm::length2(m_renderpass.pass_camera.position() - cell.m_area.center), &cell); } @@ -2680,6 +2686,7 @@ void opengl33_renderer::Render(section_sequence::iterator First, section_sequenc { if ((true == cell.m_active) && (m_renderpass.pass_camera.visible(cell.m_area))) { + Prepare_Instances( cell ); // store visible cells with content as well as their current distance, for sorting later m_cellqueue.emplace_back(glm::length2(m_renderpass.pass_camera.position() - cell.m_area.center), &cell); } @@ -2871,8 +2878,10 @@ void opengl33_renderer::Render(cell_sequence::iterator First, cell_sequence::ite Queue_Instances( cell ); } } - // remaining (non-instanceable) opaque instance nodes go through the per-node path - for (auto *instance : cell->m_instancesopaque) + // remaining (non-instanceable) opaque instance nodes go through the per-node path. the list of them kept for + // the cell spares going through every instance of it only to skip the instanceable ones - with many of these + // (vegetation) that took more time than drawing everything else + for (auto *instance : ( Instance_Cache_Valid( *cell ) ? cell->m_instancecache.single : cell->m_instancesopaque )) { if( instance->m_instanceable ) { continue; } // already handled via bucket Render(instance); @@ -3178,6 +3187,11 @@ void opengl33_renderer::Queue_Instances( scene::basic_cell const *Cell ) auto const distancelimit { slack * ( reflections ? sq( static_cast( Global.reflectiontune.range_instances ) ) : sq( static_cast( m_renderpass.draw_range ) + 250.0 ) ) }; // up close the rounding of the copies is too large a part of the distance for the slack to cover it auto const distancetested { sq( 64.0 ) }; + // the nearest point of the bounding sphere of the cell, which holds the locations of all its instances. no instance + // of the cell is closer, so where this distance fails the range test below for the largest range in a bucket, + // it fails it for each instance of that bucket, and the bucket can be skipped without going through them + auto const cellnearestdistance { std::max( 0.0, glm::length( Cell->m_area.center - cameraposition ) - Cell->m_area.radius ) }; + auto const cellnearestdistancesquared { cellnearestdistance * cellnearestdistance }; for( auto const &bucket : Cell->m_instancebuckets_opaque ) { auto const &instances { bucket.second.instances }; @@ -3190,6 +3204,11 @@ void opengl33_renderer::Queue_Instances( scene::basic_cell const *Cell ) || ( bucket.second.bounds.size() != instances.size() ) ) { Update_Instance_Bounds( bucket.first.pModel, bucket.second ); } + else if( ( bucket.second.rangeversion == m_instanceboundsversion ) + && ( cellnearestdistancesquared > distancetested ) + && ( cellnearestdistancesquared * distancescale + distanceoffset >= slack * bucket.second.rangesquaredmax ) ) { + continue; + } auto const &bounds { bucket.second.bounds }; std::vector *dest { nullptr }; // looked up when there's something to put in it for( std::size_t idx = 0; idx < bounds.size(); ++idx ) { @@ -3210,6 +3229,76 @@ void opengl33_renderer::Queue_Instances( scene::basic_cell const *Cell ) } } +// brings up to date what the renderer keeps of the instances of the cell: the copies of the bounds of the instances +// and the largest range in each bucket, the largest range in the cell, and the list of its instances drawn one by one. +// rebuilt after the content of the cell changed, or any instance lost its instanceable status (an event added an +// animation container, or changed a skin). returns: true if the cache can be used; it can't in the editor, which +// moves and resizes the instances in place, as with the copies of their bounds +bool opengl33_renderer::Prepare_Instances( scene::basic_cell &Cell ) +{ + if( EditorModeFlag ) { return false; } + if( Instance_Cache_Valid( Cell ) ) { return true; } + + auto &cache { Cell.m_instancecache }; + cache.rangesquaredmax = 0.f; + for( auto &[ key, bucket ] : Cell.m_instancebuckets_opaque ) { + if( ( bucket.boundsversion != m_instanceboundsversion ) + || ( bucket.bounds.size() != bucket.instances.size() ) ) { + Update_Instance_Bounds( key.pModel, bucket ); + } + auto rangesquaredmax { 0.f }; + for( auto const &instancebounds : bucket.bounds ) { + rangesquaredmax = std::max( rangesquaredmax, instancebounds.rangesquaredmax ); + } + bucket.rangesquaredmax = rangesquaredmax; + bucket.rangeversion = m_instanceboundsversion; + cache.rangesquaredmax = std::max( cache.rangesquaredmax, rangesquaredmax ); + } + cache.single.clear(); + for( auto *instance : Cell.m_instancesopaque ) { + if( false == instance->m_instanceable ) { + cache.single.emplace_back( instance ); + } + } + cache.instancedonly = + ( false == Cell.m_instancebuckets_opaque.empty() ) + && Cell.m_paths.empty() + && Cell.m_shapesopaque.empty() + && Cell.m_shapestranslucent.empty() + && Cell.m_lines.empty() + && Cell.m_traction.empty() + && Cell.m_instancetranslucent.empty() + && cache.single.empty(); + cache.boundsversion = m_instanceboundsversion; + cache.revoked = TAnimModel::s_instanceable_revoked; + return true; +} + +bool opengl33_renderer::Instance_Cache_Valid( scene::basic_cell const &Cell ) const +{ + return ( false == EditorModeFlag ) + && ( Cell.m_instancecache.boundsversion == m_instanceboundsversion ) + && ( Cell.m_instancecache.revoked == TAnimModel::s_instanceable_revoked ); +} + +// true if nothing of the cell would be drawn by the color pass: it holds only instances drawn with the instanced path, +// and the nearest point of the cell is past the largest range of them. the test is the one made for each bucket in +// Queue_Instances(), done before the cell is put in the cell queue: a scenery with many small instances of a short +// range has them in nearly every cell up to the draw range, and queueing, sorting and visiting these cells only +// to skip all their buckets took a good part of the frame +bool opengl33_renderer::Cell_Out_Of_Range( scene::basic_cell const &Cell ) const +{ + if( ( false == Instance_Cache_Valid( Cell ) ) || ( false == Cell.m_instancecache.instancedonly ) ) { return false; } + // same measures as in Queue_Instances() for the color pass + auto const distancescale { 1.0 / ( static_cast( Global.ZoomFactor ) * static_cast( Global.ZoomFactor ) * static_cast( Global.fDistanceFactor ) ) }; + auto const slack { 1.001 }; + auto const distancetested { sq( 64.0 ) }; + auto const cellnearestdistance { std::max( 0.0, glm::length( Cell.m_area.center - m_renderpass.pass_camera.position() ) - Cell.m_area.radius ) }; + auto const cellnearestdistancesquared { cellnearestdistance * cellnearestdistance }; + return ( cellnearestdistancesquared > distancetested ) + && ( cellnearestdistancesquared * distancescale >= slack * Cell.m_instancecache.rangesquaredmax ); +} + // makes the copies of the bounds of the instances held by a bucket of a scene cell void opengl33_renderer::Update_Instance_Bounds( TModel3d const *Model, scene::basic_cell::instance_bucket const &Bucket ) { diff --git a/rendering/opengl33renderer.h b/rendering/opengl33renderer.h index d48055f9f..b472849bc 100644 --- a/rendering/opengl33renderer.h +++ b/rendering/opengl33renderer.h @@ -302,6 +302,12 @@ class opengl33_renderer : public gfx_renderer { // adds the bucketed instances of a scene cell to the frame-level buckets drawn by Render_Instanced(), // less the ones the copies of their bounds show to be out of sight in the current render pass void Queue_Instances( scene::basic_cell const *Cell ); + // brings up to date what the renderer keeps of the instances of the cell; returns: true if the cache can be used + bool Prepare_Instances( scene::basic_cell &Cell ); + // true if the cache of the cell is up to date + bool Instance_Cache_Valid( scene::basic_cell const &Cell ) const; + // true if nothing of the cell would be drawn by the color pass from the camera of the pass + bool Cell_Out_Of_Range( scene::basic_cell const &Cell ) const; // makes the copies of the bounds of the instances held by a bucket of a scene cell void Update_Instance_Bounds( TModel3d const *Model, scene::basic_cell::instance_bucket const &Bucket ); bool Render(TDynamicObject *Dynamic); diff --git a/scene/scene.cpp b/scene/scene.cpp index 7b0e29940..312765c4d 100644 --- a/scene/scene.cpp +++ b/scene/scene.cpp @@ -352,6 +352,7 @@ void basic_cell::insert( TAnimModel *Instance ) { m_active = true; + m_instancecache.boundsversion = 0; // the renderer builds it anew auto const flags = Instance->Flags(); auto alpha = @@ -454,6 +455,8 @@ basic_cell::unregister_end( TTrack *Path ) { void basic_cell::erase( TAnimModel *Instance ) { + m_instancecache.boundsversion = 0; // the renderer builds it anew + auto const flags = Instance->Flags(); auto alpha = Instance->Material() != nullptr ? Instance->Material()->textures_alpha : 0x30300030; diff --git a/scene/scene.h b/scene/scene.h index 086e2e830..0f52fcdbd 100644 --- a/scene/scene.h +++ b/scene/scene.h @@ -252,6 +252,18 @@ public: // caches owned by the renderer; emptied whenever the content of the bucket changes mutable std::vector bounds; // copies for the instances, in the same order mutable unsigned int boundsversion { 0 }; + // the largest rangesquaredmax of the copies - past it no instance of the bucket is drawn - and the version + // of the bounds it was taken with. set by the renderer before the cell is drawn, see Prepare_Instances() + float rangesquaredmax { 0.f }; + unsigned int rangeversion { 0 }; + }; + // what the renderer keeps of the instances of the cell, set by it before the cell is drawn + struct instance_cache { + instance_sequence single; // the opaque instances drawn one by one, as they aren't instanceable + float rangesquaredmax { 0.f }; // the largest rangesquaredmax of the instance buckets + bool instancedonly { false }; // the cell holds nothing to draw but instances drawn with the instanced path + unsigned int boundsversion { 0 }; // version of the bounds of the renderer it was built with, 0: not built + unsigned int revoked { 0 }; // TAnimModel::s_instanceable_revoked at the time of the build }; using instance_bucketdata_map = std::unordered_map< instance_bucket_key, instance_bucket, instance_bucket_key_hash >; using sound_sequence = std::vector; @@ -275,6 +287,7 @@ public: // m_instancesopaque for nodes whose TAnimModel::m_instanceable == true. // The renderer uses these to amortise per-model state setup across many instances. instance_bucketdata_map m_instancebuckets_opaque; + instance_cache m_instancecache; traction_sequence m_traction; sound_sequence m_sounds; eventlauncher_sequence m_eventlaunchers; From bb009b049993c84cc4f84c4d7f7c3bf0c42ea742 Mon Sep 17 00:00:00 2001 From: pawelw Date: Sat, 7 Nov 2026 12:36:17 +0100 Subject: [PATCH 46/90] Text displays: draw opaque characters in the opaque pass (#201) * Text displays: accept characters above 127, in windows-1250 or utf-8 A text display (sub-model of type "text") looks up the sub-model of each character in a 256 entry table, and indexed it with a plain char. With char being signed, any character above 127 gave a negative index: reading the text went outside the table, and so did filling the table if a character sub-model had such a name. In effect the displays could only show ASCII, and a text with national characters could crash. The lookup is now keyed by unicode code point. Both the displayed text and the names of the character sub-models are decoded as utf-8 if they are valid utf-8, and as windows-1250 otherwise, so legacy (ansi) sceneries and models work next to utf-8 ones, and the two can be mixed. A short ansi text can be valid utf-8 by accident (two upper case accented letters in a row will do); when both readings are possible the one which finds more of its characters in the model is used. The rest is unchanged: the first character of the text still isn't displayed, the characters are still drawn in the translucent pass only. A missing text pointer no longer gets dereferenced. * Text displays: draw opaque characters in the opaque pass The characters of a text display were only laid out in the translucent pass, so a display had to be made of translucent sub-models to show anything. Those don't write depth: overhead wires and other translucent geometry drawn later showed through the display, and translucent geometry in front of it could end up painted over. The opaque pass now lays the characters out the same way, so a display built from opaque sub-models is drawn with depth like any other opaque geometry (and casts shadows). Translucent characters keep working as before. A model with a text display shows the text of its own instance, so such models are excluded from the GPU-instanced opaque path. * Text displays: prepare the characters once for each text The sequence of character sub-models was cached for the last text only, so displays of several instances drawn in turn decoded their texts again on every draw. It is now kept for each text the display shows; the texts of the instances don't change, so a draw costs a single lookup. Also the comments added in the renderers are now in English. * Text displays: move the text decoding to utilities utf8_to_utf32() and win1250_to_utf32(), with the windows-1250 table, now sit next to win1250_to_utf8(). The existing function maps only the Polish letters and passes other bytes above 127 through, so it can't serve the displays. * Text displays: address the SonarCloud findings std::array instead of the C-style arrays, the utf-8 sequence length worked out in a function of its own instead of nested conditional operators, std::byte for the bit operations, std::ranges::count_if and contains(). The character loop of the renderers moved to Render_Alpha_text(), which takes the nesting back to the limit, and the static pasText is accessed through the class. * Text displays: move the opaque character loop to Render_text() The same as for the translucent pass: the loop sits in a function of its own and the static pasText is accessed through the class, which keeps the nesting within the limit. * Text displays: access the static iAlpha through the class in the opaque pass * Text displays: transparent lookup of the text layouts, init-statement for the lookup result Two SonarCloud findings left in the code of #198: the map of the text layouts gets a transparent hasher with std::equal_to<>, and the result of the lookup is declared in the if statement. --- model/AnimModel.cpp | 2 ++ model/Model3d.cpp | 3 +-- model/Model3d.h | 9 ++++++++- rendering/opengl33renderer.cpp | 29 +++++++++++++++++++++++++++-- rendering/opengl33renderer.h | 1 + rendering/openglrenderer.cpp | 25 +++++++++++++++++++++++-- rendering/openglrenderer.h | 3 +++ 7 files changed, 65 insertions(+), 7 deletions(-) diff --git a/model/AnimModel.cpp b/model/AnimModel.cpp index cafb3a93e..52a879ac2 100644 --- a/model/AnimModel.cpp +++ b/model/AnimModel.cpp @@ -505,6 +505,8 @@ bool anim_type_shared_by_instances( TAnimType a ) { // instanced path (one shared submodel tree across all instances) cannot serve. bool submodel_tree_blocks_instancing( TSubModel const *Sub ) { if( Sub == nullptr ) { return false; } + // a text display shows the text of its instance, a tree shared by all instances can't do that + if( Sub->eType == TP_TEXT ) { return true; } if( anim_type_unsafe_for_instancing( Sub->b_Anim ) ) { return true; } if( ( ( Sub->iFlags & 0x4000 ) != 0 ) && ( false == anim_type_shared_by_instances( Sub->b_Anim ) ) ) { return true; } if( submodel_tree_blocks_instancing( Sub->Child ) ) { return true; } diff --git a/model/Model3d.cpp b/model/Model3d.cpp index 66fc08c94..44f5f6255 100644 --- a/model/Model3d.cpp +++ b/model/Model3d.cpp @@ -898,8 +898,7 @@ std::vector const &TSubModel::text_letters(std::string const &Text) { // the sequence is prepared once for each text the display shows. the texts of the model instances don't change, // so drawing a display afterwards costs a single lookup, however many instances and passes take turns - auto const prepared{m_textlayouts.find(Text)}; - if (prepared != m_textlayouts.end()) + if (auto const prepared{m_textlayouts.find(Text)}; prepared != m_textlayouts.end()) { return prepared->second; } diff --git a/model/Model3d.h b/model/Model3d.h index 27565c206..a9ef8af12 100644 --- a/model/Model3d.h +++ b/model/Model3d.h @@ -15,6 +15,8 @@ http://mozilla.org/MPL/2.0/. #include "model/material.h" #include "gl/query.h" +#include + #define EU07_USE_GEOMETRYINDEXING // Ra: specjalne typy submodeli, poza tym GL_TRIANGLES itp. @@ -159,7 +161,12 @@ public: // chwilowo // text display (TP_TEXT) data, filled on first use std::unordered_map m_letters; // sub-models of the available characters, by unicode code point bool m_lettersready { false }; // whether the character lookup was built yet - std::unordered_map> m_textlayouts; // sub-models of consecutive characters, for each text shown + // transparent hasher, so the text layouts can be looked up with any string type without a temporary std::string + struct text_hash { + using is_transparent = void; + std::size_t operator()( std::string_view const Text ) const noexcept { return std::hash{}( Text ); } + }; + std::unordered_map, text_hash, std::equal_to<>> m_textlayouts; // sub-models of consecutive characters, for each text shown TSubModel *Parent{ nullptr }; // nadrzędny, np. do wymnażania macierzy int iVisible { 1 }; // roboczy stan widoczności float fVisible { 1.f }; // visibility level diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index 33346fad2..c24f750c8 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -4152,6 +4152,23 @@ bool opengl33_renderer::Render(TModel3d *Model, material_data const *Material, f return result; } +// opaque characters of a text display, laid out in a row the same way as the translucent ones +void opengl33_renderer::Render_text(TSubModel *Submodel) +{ + if (TSubModel::pasText == nullptr) + { + return; + } + ::glPushMatrix(); + for (auto *p : Submodel->text_letters(*TSubModel::pasText)) + { + Render(p); + if (p->fMatrix) + ::glMultMatrixf(p->fMatrix->readArray()); // move on to the place of the next character + } + ::glPopMatrix(); +} + void opengl33_renderer::Render(TSubModel *Submodel) { glDebug("Render TSubModel"); @@ -4302,9 +4319,17 @@ void opengl33_renderer::Render(TSubModel *Submodel) } } } - if (Submodel->Child != nullptr) - if (Submodel->iAlpha & Submodel->iFlags & 0x001F0000) + if (Submodel->Child != nullptr && (TSubModel::iAlpha & Submodel->iFlags & 0x001F0000)) + { + if (Submodel->eType == TP_TEXT) + { + Render_text(Submodel); + } + else + { Render(Submodel->Child); + } + } if (Submodel->iFlags & 0xC000) { diff --git a/rendering/opengl33renderer.h b/rendering/opengl33renderer.h index b472849bc..1b4897386 100644 --- a/rendering/opengl33renderer.h +++ b/rendering/opengl33renderer.h @@ -314,6 +314,7 @@ class opengl33_renderer : public gfx_renderer { bool Render(TModel3d *Model, material_data const *Material, float const Squaredistance, glm::dvec3 const &Position, glm::vec3 const &Angle); bool Render(TModel3d *Model, material_data const *Material, float const Squaredistance); void Render(TSubModel *Submodel); + void Render_text(TSubModel *Submodel); // opaque characters of a text display void Render(TTrack *Track); void Render(scene::basic_cell::path_sequence::const_iterator First, scene::basic_cell::path_sequence::const_iterator Last); // renders the per-track sleeper instances (TTrack::m_sleeper_local_transforms) via GPU instancing. diff --git a/rendering/openglrenderer.cpp b/rendering/openglrenderer.cpp index ef54f32c9..180dba771 100644 --- a/rendering/openglrenderer.cpp +++ b/rendering/openglrenderer.cpp @@ -2794,6 +2794,21 @@ opengl_renderer::Render( TModel3d *Model, material_data const *Material, float c return result; } +// opaque characters of a text display, laid out in a row the same way as the translucent ones +void +opengl_renderer::Render_text( TSubModel *Submodel ) { + + if( TSubModel::pasText == nullptr ) { return; } + + ::glPushMatrix(); + for( auto *p : Submodel->text_letters( *TSubModel::pasText ) ) { + Render( p ); + if( p->fMatrix ) + ::glMultMatrixf( p->fMatrix->readArray() ); // move on to the place of the next character + } + ::glPopMatrix(); +} + void opengl_renderer::Render( TSubModel *Submodel ) { @@ -3112,9 +3127,15 @@ opengl_renderer::Render( TSubModel *Submodel ) { } } } - if( Submodel->Child != nullptr ) - if( Submodel->iAlpha & Submodel->iFlags & 0x001F0000 ) + if( Submodel->Child != nullptr + && ( TSubModel::iAlpha & Submodel->iFlags & 0x001F0000 ) ) { + if( Submodel->eType == TP_TEXT ) { + Render_text( Submodel ); + } + else { Render( Submodel->Child ); + } + } if( Submodel->iFlags & 0xC000 ) ::glPopMatrix(); diff --git a/rendering/openglrenderer.h b/rendering/openglrenderer.h index 9779388c7..3a3538a6c 100644 --- a/rendering/openglrenderer.h +++ b/rendering/openglrenderer.h @@ -237,6 +237,9 @@ private: Render( TModel3d *Model, material_data const *Material, float const Squaredistance ); void Render( TSubModel *Submodel ); + // opaque characters of a text display + void + Render_text( TSubModel *Submodel ); void Render( TTrack *Track ); void From 466cbbbfa157525a51738f9d9715225ca066d7ce Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 12:37:12 +0100 Subject: [PATCH 47/90] Test/core unit coverage (#235) * test: real vehicles and golden traces in the unit tests The game's own vehicles (dynamic/ of the release used for the tests) as JSON of what the game's vehicle loader reads of their .fiz files, includes expanded: test_fiz.cpp converts them on demand and checks the loader reads the same sections, values and rows as of the game's file (fiz.h). Chosen to cover every engine type, brake valve, brake handle, local brake, coupler type, door type and brake delay set the game data uses. Golden traces (golden.h) characterise the simulation, compared within a relative tolerance or exactly, re-recorded with EU07_UNIT_TESTS_UPDATE_GOLDEN=1: - the parameters of every real vehicle as loaded, - the brakes of every real vehicle through charge, service steps, release, overcharge and emergency, on every brake delay setting, - every position of every driver's and local brake handle, - every powered vehicle started up the way the AI does it, driven to full power and braked to a stop (drive.h), - the real electric units and a locomotive with wagons driven whole, as trains of coupled vehicles stepped the way the game steps them (consist.h), - scenarios of the controls (scenario.h): doors, reversing and cab change, slip and sand, emergency stops, releasers, the ep and the electrodynamic brake, heating, loading, a collision, and frames of several physics iterations. * test: commands passed between vehicles Every command RunCommand knows run on every real vehicle and recorded, commands travelling through a train with a vehicle facing backwards, a coupling without the control cable, and loading and unloading by commands. * test: every switch of the cab, sent to the next vehicle Each switch sending its state to the train, each way and to each range, with the controllers and direction among them, on every real vehicle driving another of its kind, recorded for both. * test: the switches of the cab worked on a pair under the catenary The same switches with two seconds for the vehicles to act on each, so the pantographs rise, the main switch closes and the auxiliaries start. * test: gauges of trains in multiple and the damage descriptions The ammeters and the brake force the AI expects, recorded on the electric units and locomotives in multiple driven, the damage described on the driver's panel and ammeters showing the current of controlled vehicles. * test: give load commands their place, as the game does The loading test set its commands with SetInternalCommand, which leaves TCommand::Location as it is: uninitialised in a fresh vehicle, so the distance check of Load= and UnLoad= read garbage and the test failed now and then. The game gives them through PutCommand with the place of the platform, and so do the tests now. * test: brake valves and handles no vehicle of the game data has Fitted to a wagon and to an EU07: the valve variants and handles the loader knows that no real vehicle uses, the CV1 ones and the test handle among them, and a name the loader doesn't know. * test: braking from speed with the automatic ED brake, and a draisine Trains with the EP09, the E6ACT and the EW58 braked by ep, light and full service applications from speed, and the draisine pumped from rest and rolling. Only the E6ACT brings its electrodynamic brake in here. * test: the EP09's electrodynamic brake in service braking held to a stop start_up() took a vehicle for ready once its line breaker was closed and its reservoir full, which a train made already moving is at once: the EP09's manually started converter was never switched on, so there was no 110 V and no electrodynamic brake. It now waits for the converter. The EP09 with wagons is braked from 120 km/h by light, medium, high and full service applications held to a stop: the motors brake, the air of its cylinders is let out and given back as the train slows down. The flag of the electrodynamic brake stays on at a standstill below 2.1 bar. C strings given to CAPTURE are shown as text. * test: the converter switched on before a train is ready, the EW58's ep-staged electrodynamic brake start_up() called a train ready as soon as its line breaker closed and the air was there, so the converter of a unit having it in another car than the cab (the EW58's middle car) was never switched on, and its electrodynamic brake, gated by the 110 V, never came in. The converter and the compressor now always get switched on first. The EW58 brakes electrically by the stages of the ep brake, not by the pipe alone. * test: the ET42 and the 214M railbus among the real vehicles The ET42's halves (its own series motor branch: the motors in series at the high current setting) and the 214MA, a diesel railbus with the hydraulic torque converter, the retarder and the controller of an induction motor drive, converted from the game data and recorded in every sweep of the real vehicles. * test: the wagons' hand brakes released with the train's brakes; the 754, SM31 and SU46 release_brakes() released the hand brake of the driver's vehicle only, so every wagon set up braked kept its hand brake on and got dragged along. It now releases the hand and spring brakes of the whole train, as TController::AutoRewident() does when the AI takes it over, and the traces of the trains with wagons are recorded anew. The 754, the SM31 and the SU46 join the real vehicles for the kinds of the diesel-electrics' field shunting relays the others lack (RelayType 0, 1 and 46). * test: traction on a lost and too high catenary voltage, the ET42, the diesel-electrics' shunting relays The line breaker opened by the voltage relays of a series motor locomotive and by the limits of an induction motor one. The ET42 at the high current setting, which keeps its halves' motors in series, and its switched electrodynamic brake, whose current trips the overload relay from the top speed of the low setting. The field shunting of the diesel-electrics through each kind of their relays, at full power and notched back. * test: the EW58's electrodynamic brake stages, the EU47's speed control and braking at the voltage's limit The stages of the EW58's motor brake set by the positions of its ep handle, the first one alone over 100 km/h. The EU47's second controller holding, raising and lowering the speed set, its local brake setting the flag of the electrodynamic brake without the motors braking, and its brake feeding back less current into a catenary over the drive's MaxV. * test: diesel engines too cold and too hot to start, the 214M railbus's controller and retarder The heat of the diesel engines: at -10 degrees the ones with the lowest water temperature set don't start until the heater warms them, at 88 degrees the ones with a second water circuit don't start while it cools, and the SM42 run hot at full power is kept under its highest temperature by its fans and shutters. The 214M railbus's controller from the air brake through the retarder and neutral to power, its torque converter's lock-up and its gearbox. The controller helpers of the traction tests (full power, waiting for the line contactors, and notching back) move to test::scenario. * test: couplers coupled, uncoupled, broken and colliding; loads assigned, loaded and unloaded Coupling by hand touching or apart and of the same kind or not, with the sounds of what changes; uncoupling a screw coupler pressed together, stretched, with the hoses alone or broken; the automatic couplers of electric units coupling on contact, without the control cable between two kinds; a coupler broken with its brake hose; collisions over 15 km/h derailing. The pantographs set up by the pantstate load, a load of one kind at a time up to the capacity, and the steps of loading and unloading. * test: the battery drained and charged, the pantographs' tank and pressure switch, the 214M's speed control The EU07's battery drained without the converter by the lights and the small compressor until it opens the line breaker and nothing starts, charged by the converter, and switched off. The pantographs on their own leaking tank until the pressure switch opens the line breaker, and the small compressor pumping it up and priming the switch. The 214M railbus's speed control set by its buttons, steps and power limit, braking down with the retarder, and standing by. * test: the MHZ handles' buttons, the valves at speed, every brake material, the NESt3's relays The overload and the high pressure buttons of the MHZ handles. The KE and the LSt at speed in the R and P settings, under the anti-slip request and the releaser button. Every material of the brake shoes and disks the loader takes, braking a wagon from speed, the ones no game vehicle has among them. The CV1_R and the Oerlikon valve's relays set by its name (-ED, AL2 without ESt3) fitted to a wagon. * test: the loader's lists and lines: the 218M, SA134 and 22WEc, a compressor list, Pmax by speed, skipped lines The 218M (V2NList), the SA134 (HTCList) and the Elf 22WEc (ffBrakeList) join the real vehicles for the lists the others lack. Definitions made by the tests for what no game file has: a compressor list, whose table of whole numbers reads a factor of 0.9 as 0; the compressor's limits apart for the rear cab; the highest power of an induction motor drive by the speed, extrapolated past the table; and the lines the loader skips or trims (comments, a hash anywhere, leading spaces, carriage returns). * test: what the cab and the AI call on a vehicle: gauges of the train, the split ED brake, spring brake, cut-off The engine rotation of up to three vehicles in multiple and the lamps of the overload relay and the starting resistors read through the control cable; the engine's idle speed and the ET42's second controller shown in its dynamic brake; the controller of the electrodynamic brake apart from the local brake, which no game vehicle has yet; the spring brake shut off and released, the ep brake's fuse and the handle stepped by the AI; damaged motors cut off; the cab activated by itself; a road vehicle moved across a road, for which the tests' track gets a width; and the lamps and the buzzer of the security system left unattended. * test: the EP05 and its camshaft among the real vehicles, the headlights' colour and brightness The EP05, whose camshaft closes the line breaker and the line contactors by the camshaft's own position, converted from the game data and recorded in every sweep of the real vehicles. The Headlights section, which only the 214M's own file has, in a definition made by the test. * test: what the unit tests reach of the simulation core, what's dead or left, and what they found COVERAGE.md: the blocks of Mover.cpp, hamulce.cpp, Oerlikon_ESt.cpp and friction.cpp the tests run, the code nothing in the game runs, the code only data no game vehicle has would run, what's reachable and left, and the behaviour the tests found that may deserve a look. * test: check invariants of the physics on every step of every golden trace Rules any vehicle keeps whatever a test does (no NaN, cylinders and pipes within their limits, no current with the line breaker or contactors open, no force without power, distance as the speed runs, no acceleration over 5 g), checked on every step, with no comparison to a recorded file. The traces where a test or a game vehicle breaks one are excused with the reason; the vehicles' cases are in the findings of COVERAGE.md. * test: key figures of every golden trace, per part of the run Duration, speeds, distance, time to stop, the brake pipe's lowest pressure and time to recover, the peaks of the cylinders, the motor current and the tractive force, for each part between two events, in data/golden/.figures. Compared within 0.5% or the last decimal, so they hold on any compiler, and an intended change of behaviour shows in a few readable lines. Traces sampled by hand keep none. * test: keep the golden samples out of the repository, recorded on the code before a change The samples of the golden traces are 29 MB and only mean something against the code before the change they check, so they're recorded locally before it (EU07_UNIT_TESTS_UPDATE_GOLDEN=1) and compared after it, and ignored by git. The key figures and the invariants stay, and are what the CI checks. Without samples recorded a trace compares only its key figures, but with EU07_UNIT_TESTS_GOLDEN_EXACT=1, the check of a change which shouldn't alter anything, it fails, so the check can't pass by having nothing to compare. * fix: draw the noise of the control pipe pressure in one order on every compiler The order of the two Random(10) calls in 'Random(10) - Random(10)' is unspecified: GCC draws them the other way round than MSVC Release, which mirrored the control pipe pressure around 5 bar and failed 32 unit tests on the Linux CI. The draws are sequenced in the order of MSVC Release, which the golden traces were recorded with. * test: count the start-up rounds with an integer, not a double (SonarCloud S2193) * test: behaviour in CHECKs, the key figures out of the repository The key figures of the golden traces are recorded with their samples on the code before a change, and compared after it, instead of being kept: a trace without them checks the invariants and its test's CHECKs, which is what the CI runs. The scenarios of the controls state what they show in CHECKs, and the runs of every real vehicle what holds for each of them. Writing them showed scenarios which didn't do what they were named for: the cab change stopped in the engine room, the slip runs put the controller to full power at once and never got the line contactors closed, and the train stopped by the alarm chain never got its pipe charged again. They do it now; the rest went to the findings. * test: the SonarCloud findings in the unit tests std::format for the texts put together, emplace_back, const references, string_view parameters, std::array and std::vector for the C arrays, the ranges algorithms, names no longer shadowing the coupler ends, comments in the empty loops; start_up(), trace::check() and the converter of the definitions split up. BrakeForceR() is const, which it always was in effect, so the gauges test needs no const_cast. The golden traces compare bit for bit. --- McZapkie/MOVER.h | 2 +- McZapkie/Mover.cpp | 7 +- tests/unit/CMakeLists.txt | 14 +- tests/unit/COVERAGE.md | 117 ++++ tests/unit/README.md | 118 +++- tests/unit/consist.cpp | 106 +++ tests/unit/consist.h | 57 ++ tests/unit/data/fiz/11xa_v2/93a.json | 14 + tests/unit/data/fiz/14xa_v1/173a.json | 14 + tests/unit/data/fiz/168a_v1/168a.json | 14 + tests/unit/data/fiz/21xm_v1/214ma.json | 78 +++ tests/unit/data/fiz/21xm_v1/218ma_a_kd.json | 84 +++ tests/unit/data/fiz/3wl_v2/3wl.json | 11 + tests/unit/data/fiz/430w_v2/430w.json | 11 + tests/unit/data/fiz/4e_v1/4e.json | 81 +++ tests/unit/data/fiz/README.md | 66 ++ tests/unit/data/fiz/bdhpumn_v3/bdhpumn.json | 14 + tests/unit/data/fiz/bhp_v1/bhp-b.json | 15 + tests/unit/data/fiz/br285_v1/br285.json | 69 ++ tests/unit/data/fiz/cd/754_v1/754.json | 56 ++ tests/unit/data/fiz/cd/810_v2/810.json | 41 ++ tests/unit/data/fiz/dr2_v1/dr2.json | 13 + tests/unit/data/fiz/e186_v2/eu47.json | 61 ++ tests/unit/data/fiz/e6act_v2/e6acta.json | 41 ++ tests/unit/data/fiz/ed72_v1/5bs-ra.json | 25 + tests/unit/data/fiz/ed72_v1/5bs-rb.json | 25 + tests/unit/data/fiz/ed72_v1/6bs-sa.json | 47 ++ tests/unit/data/fiz/ed72_v1/6bs-sb.json | 47 ++ tests/unit/data/fiz/elf_v1/21we-a.json | 52 ++ tests/unit/data/fiz/elf_v1/21we-b.json | 25 + tests/unit/data/fiz/elf_v1/21we-c.json | 52 ++ tests/unit/data/fiz/elf_v1/22wec-a.json | 54 ++ .../data/fiz/en57_v1/5bgaiii_led_spot.json | 26 + tests/unit/data/fiz/en57_v1/5bgb.json | 26 + tests/unit/data/fiz/en57_v1/6ba_840.json | 26 + tests/unit/data/fiz/en57_v1/6bai.json | 25 + tests/unit/data/fiz/en57akl_v1/akl_ra.json | 40 ++ tests/unit/data/fiz/ep05_v2/ep05.json | 84 +++ tests/unit/data/fiz/ep09_v2/104e.json | 76 +++ tests/unit/data/fiz/et22_v3/201e-rn.json | 86 +++ tests/unit/data/fiz/et22_v3/201e-w.json | 86 +++ tests/unit/data/fiz/et42_v2/112e-a.json | 77 +++ tests/unit/data/fiz/et42_v2/112e-b.json | 77 +++ tests/unit/data/fiz/eu04_v1/eu04-01.json | 67 ++ tests/unit/data/fiz/ew58_v1/ew58-d.json | 20 + tests/unit/data/fiz/ew58_v1/ew58-sa.json | 68 ++ tests/unit/data/fiz/ew58_v1/ew58-sb.json | 68 ++ tests/unit/data/fiz/impuls_v1/31we-a_pr.json | 50 ++ tests/unit/data/fiz/impuls_v1/31we-b_pr.json | 20 + tests/unit/data/fiz/impuls_v1/31we-c_pr.json | 20 + tests/unit/data/fiz/impuls_v1/31we-d_pr.json | 50 ++ tests/unit/data/fiz/road/man/nl293.json | 36 + tests/unit/data/fiz/sa134_v1/218mc_a.json | 94 +++ tests/unit/data/fiz/sieciowiec_v1/a.json | 13 + tests/unit/data/fiz/sm03_v1/sm03.json | 41 ++ tests/unit/data/fiz/sm31_v1/411d.json | 43 ++ tests/unit/data/fiz/sm40_v1/sm40.json | 48 ++ tests/unit/data/fiz/sm42_v1/6d1.json | 42 ++ tests/unit/data/fiz/sn61_v2/sn61.json | 42 ++ tests/unit/data/fiz/st44_v2/m62-1151.json | 56 ++ tests/unit/data/fiz/st45_v2/301dd.json | 55 ++ .../unit/data/fiz/static/sug/sug_kielce.json | 70 ++ tests/unit/data/fiz/su45_v2/301d.json | 47 ++ tests/unit/data/fiz/su46_v2/303d.json | 50 ++ tests/unit/data/fiz/tem2_v2/tem2a.json | 39 ++ tests/unit/data/fiz/wm15_v1/wm15.json | 22 + tests/unit/data/fiz/wmb10_v1/wmb10.json | 49 ++ tests/unit/data/fiz/z1_v2/152a.json | 14 + tests/unit/data/golden/.gitignore | 4 + tests/unit/drive.cpp | 246 +++++++ tests/unit/drive.h | 56 ++ tests/unit/figures.cpp | 222 ++++++ tests/unit/figures.h | 70 ++ tests/unit/fiz.cpp | 222 ++++++ tests/unit/fiz.h | 44 ++ tests/unit/golden.cpp | 393 +++++++++++ tests/unit/golden.h | 90 +++ tests/unit/invariants.cpp | 163 +++++ tests/unit/invariants.h | 36 + tests/unit/main.cpp | 12 +- tests/unit/real_vehicles.h | 12 + tests/unit/scenario.cpp | 80 +++ tests/unit/scenario.h | 63 ++ tests/unit/test_auxiliaries.cpp | 112 ++++ tests/unit/test_brake_cycle.cpp | 194 ++++++ tests/unit/test_brake_handles.cpp | 154 +++++ tests/unit/test_cab.cpp | 229 +++++++ tests/unit/test_commands.cpp | 547 +++++++++++++++ tests/unit/test_consist.cpp | 135 ++++ tests/unit/test_controls.cpp | 632 ++++++++++++++++++ tests/unit/test_couplers_trains.cpp | 174 +++++ tests/unit/test_definition.cpp | 106 +++ tests/unit/test_diesel.cpp | 179 +++++ tests/unit/test_figures.cpp | 67 ++ tests/unit/test_fiz.cpp | 183 +++++ tests/unit/test_invariants.cpp | 34 + tests/unit/test_loads.cpp | 116 ++++ tests/unit/test_queries.cpp | 61 ++ tests/unit/test_traction.cpp | 270 ++++++++ tests/unit/vehicle.cpp | 254 ++++--- tests/unit/vehicle.h | 70 +- 101 files changed, 8298 insertions(+), 116 deletions(-) create mode 100644 tests/unit/COVERAGE.md create mode 100644 tests/unit/consist.cpp create mode 100644 tests/unit/consist.h create mode 100644 tests/unit/data/fiz/11xa_v2/93a.json create mode 100644 tests/unit/data/fiz/14xa_v1/173a.json create mode 100644 tests/unit/data/fiz/168a_v1/168a.json create mode 100644 tests/unit/data/fiz/21xm_v1/214ma.json create mode 100644 tests/unit/data/fiz/21xm_v1/218ma_a_kd.json create mode 100644 tests/unit/data/fiz/3wl_v2/3wl.json create mode 100644 tests/unit/data/fiz/430w_v2/430w.json create mode 100644 tests/unit/data/fiz/4e_v1/4e.json create mode 100644 tests/unit/data/fiz/README.md create mode 100644 tests/unit/data/fiz/bdhpumn_v3/bdhpumn.json create mode 100644 tests/unit/data/fiz/bhp_v1/bhp-b.json create mode 100644 tests/unit/data/fiz/br285_v1/br285.json create mode 100644 tests/unit/data/fiz/cd/754_v1/754.json create mode 100644 tests/unit/data/fiz/cd/810_v2/810.json create mode 100644 tests/unit/data/fiz/dr2_v1/dr2.json create mode 100644 tests/unit/data/fiz/e186_v2/eu47.json create mode 100644 tests/unit/data/fiz/e6act_v2/e6acta.json create mode 100644 tests/unit/data/fiz/ed72_v1/5bs-ra.json create mode 100644 tests/unit/data/fiz/ed72_v1/5bs-rb.json create mode 100644 tests/unit/data/fiz/ed72_v1/6bs-sa.json create mode 100644 tests/unit/data/fiz/ed72_v1/6bs-sb.json create mode 100644 tests/unit/data/fiz/elf_v1/21we-a.json create mode 100644 tests/unit/data/fiz/elf_v1/21we-b.json create mode 100644 tests/unit/data/fiz/elf_v1/21we-c.json create mode 100644 tests/unit/data/fiz/elf_v1/22wec-a.json create mode 100644 tests/unit/data/fiz/en57_v1/5bgaiii_led_spot.json create mode 100644 tests/unit/data/fiz/en57_v1/5bgb.json create mode 100644 tests/unit/data/fiz/en57_v1/6ba_840.json create mode 100644 tests/unit/data/fiz/en57_v1/6bai.json create mode 100644 tests/unit/data/fiz/en57akl_v1/akl_ra.json create mode 100644 tests/unit/data/fiz/ep05_v2/ep05.json create mode 100644 tests/unit/data/fiz/ep09_v2/104e.json create mode 100644 tests/unit/data/fiz/et22_v3/201e-rn.json create mode 100644 tests/unit/data/fiz/et22_v3/201e-w.json create mode 100644 tests/unit/data/fiz/et42_v2/112e-a.json create mode 100644 tests/unit/data/fiz/et42_v2/112e-b.json create mode 100644 tests/unit/data/fiz/eu04_v1/eu04-01.json create mode 100644 tests/unit/data/fiz/ew58_v1/ew58-d.json create mode 100644 tests/unit/data/fiz/ew58_v1/ew58-sa.json create mode 100644 tests/unit/data/fiz/ew58_v1/ew58-sb.json create mode 100644 tests/unit/data/fiz/impuls_v1/31we-a_pr.json create mode 100644 tests/unit/data/fiz/impuls_v1/31we-b_pr.json create mode 100644 tests/unit/data/fiz/impuls_v1/31we-c_pr.json create mode 100644 tests/unit/data/fiz/impuls_v1/31we-d_pr.json create mode 100644 tests/unit/data/fiz/road/man/nl293.json create mode 100644 tests/unit/data/fiz/sa134_v1/218mc_a.json create mode 100644 tests/unit/data/fiz/sieciowiec_v1/a.json create mode 100644 tests/unit/data/fiz/sm03_v1/sm03.json create mode 100644 tests/unit/data/fiz/sm31_v1/411d.json create mode 100644 tests/unit/data/fiz/sm40_v1/sm40.json create mode 100644 tests/unit/data/fiz/sm42_v1/6d1.json create mode 100644 tests/unit/data/fiz/sn61_v2/sn61.json create mode 100644 tests/unit/data/fiz/st44_v2/m62-1151.json create mode 100644 tests/unit/data/fiz/st45_v2/301dd.json create mode 100644 tests/unit/data/fiz/static/sug/sug_kielce.json create mode 100644 tests/unit/data/fiz/su45_v2/301d.json create mode 100644 tests/unit/data/fiz/su46_v2/303d.json create mode 100644 tests/unit/data/fiz/tem2_v2/tem2a.json create mode 100644 tests/unit/data/fiz/wm15_v1/wm15.json create mode 100644 tests/unit/data/fiz/wmb10_v1/wmb10.json create mode 100644 tests/unit/data/fiz/z1_v2/152a.json create mode 100644 tests/unit/data/golden/.gitignore create mode 100644 tests/unit/drive.cpp create mode 100644 tests/unit/drive.h create mode 100644 tests/unit/figures.cpp create mode 100644 tests/unit/figures.h create mode 100644 tests/unit/fiz.cpp create mode 100644 tests/unit/fiz.h create mode 100644 tests/unit/golden.cpp create mode 100644 tests/unit/golden.h create mode 100644 tests/unit/invariants.cpp create mode 100644 tests/unit/invariants.h create mode 100644 tests/unit/real_vehicles.h create mode 100644 tests/unit/scenario.cpp create mode 100644 tests/unit/scenario.h create mode 100644 tests/unit/test_auxiliaries.cpp create mode 100644 tests/unit/test_brake_cycle.cpp create mode 100644 tests/unit/test_brake_handles.cpp create mode 100644 tests/unit/test_cab.cpp create mode 100644 tests/unit/test_commands.cpp create mode 100644 tests/unit/test_consist.cpp create mode 100644 tests/unit/test_controls.cpp create mode 100644 tests/unit/test_couplers_trains.cpp create mode 100644 tests/unit/test_diesel.cpp create mode 100644 tests/unit/test_figures.cpp create mode 100644 tests/unit/test_fiz.cpp create mode 100644 tests/unit/test_invariants.cpp create mode 100644 tests/unit/test_loads.cpp create mode 100644 tests/unit/test_queries.cpp create mode 100644 tests/unit/test_traction.cpp diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index bb36694bb..199c65a0c 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2437,7 +2437,7 @@ class TMoverParameters double Adhesive(double staticfriction) const; double TractionForce(double dt); double FrictionForce() const; - double BrakeForceR(double ratio, double velocity); + double BrakeForceR(double ratio, double velocity) const; double BrakeForceP(double press, double velocity) const; double BrakeForce(const TTrackParam &Track); double CouplerForce(int const End, double dt); diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 6b7bdcec5..330e8eb8f 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -5144,7 +5144,7 @@ void TMoverParameters::ComputeTotalForce(double dt) FTotal = FTrain - Sign(V) * FStand; } -double TMoverParameters::BrakeForceR(double ratio, double velocity) +double TMoverParameters::BrakeForceR(double ratio, double velocity) const { double press = 0; if (MBPM > 2) @@ -10482,7 +10482,10 @@ void TMoverParameters::LoadFIZ_Brake(std::string const &line) P2FTrans = 0; } - CntrlPipePress = 5 + 0.001 * (Random(10) - Random(10)); // Ra 2014-07: trochę niedokładności + // Ra 2014-07: trochę niedokładności + // the draws are sequenced: the order of the operands of '-' is unspecified, and GCC and MSVC draw them the other way round + auto const firstdraw{Random(10)}; + CntrlPipePress = 5 + 0.001 * (Random(10) - firstdraw); extract_value(CntrlPipePress, "HiPP", line, ""); HighPipePress = CntrlPipePress; LowPipePress = std::min(HighPipePress, 3.5); diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 7bb624106..17efffa7d 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -6,20 +6,28 @@ set(UNIT_TESTS_NAME eu07_unit_tests) -# a new test_*.cpp file or data/*.json definition is picked up on its own +# a new test_*.cpp file or file in data/ (definitions, real vehicles, golden traces) is picked up on its own file(GLOB UNIT_TEST_CASES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/test_*.cpp") -file(GLOB UNIT_TEST_DATA CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/data/*.json") +file(GLOB_RECURSE UNIT_TEST_DATA CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/data/*") add_executable(${UNIT_TESTS_NAME} main.cpp vehicle.cpp + fiz.cpp + drive.cpp + consist.cpp + scenario.cpp + golden.cpp + invariants.cpp + figures.cpp engine_stubs.cpp ${UNIT_TEST_CASES}) # the core library brings the game's build settings along target_link_libraries(${UNIT_TESTS_NAME} PRIVATE eu07_core) target_include_directories(${UNIT_TESTS_NAME} PRIVATE "${DEPS_DIR}/doctest") -target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_DATA="${CMAKE_CURRENT_SOURCE_DIR}/data") +# the names the tests CAPTURE are often C strings, shown as text rather than pointers +target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_DATA="${CMAKE_CURRENT_SOURCE_DIR}/data" DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING) if (MSVC) target_compile_options(${UNIT_TESTS_NAME} PRIVATE /bigobj) endif() diff --git a/tests/unit/COVERAGE.md b/tests/unit/COVERAGE.md new file mode 100644 index 000000000..aafcc27a6 --- /dev/null +++ b/tests/unit/COVERAGE.md @@ -0,0 +1,117 @@ +# Coverage of the simulation core by the unit tests + +These tests characterise the physics of the vehicles (`McZapkie/Mover.cpp`, `hamulce.cpp`, `Oerlikon_ESt.cpp`, `friction.cpp`). They exist to show that refactors of those files, such as splitting `TractionForce` or `RunCommand`, leave the behaviour as it was. This page lists: + +- what the tests reach; +- what nothing in the game ever runs; +- what only data no game vehicle has would run; +- what is left to test; +- the behaviour the tests found that may deserve a look. + +## How it was measured + +Coverage was measured on Windows with MSVC's SanitizerCoverage (`/fsanitize-coverage=trace-pc-guard`) on the unit test binary. The counts are basic blocks, which comes close to branch coverage: both sides of a condition are blocks of their own. The tool is local and not part of the build, by decision (2026-10). Any block-coverage tool run over `eu07_unit_tests` gives comparable numbers. + +| File | Blocks run, at the start | Now | +|---|---|---| +| `Mover.cpp` | 8.9% | 80.7% (3323 of 4118) | +| `hamulce.cpp` | 30% | 76.3% (649 of 851) | +| `Oerlikon_ESt.cpp` | 58% | 87.4% (118 of 135) | +| `friction.cpp` | 9% | 90.9% (10 of 11) | + +The aim is branch coverage with the rest explained below, not 100%. + +## Code nothing in the game runs + +Nothing creates these objects or calls these functions anywhere in the game, which was checked against the whole source tree. A refactor can delete them, or leave them untested. + +- **Brake valves that never get created:** + - `TESt`, `TESt3` and `TESt3AL2`, including `Init`, `PLC` and `SetLP`. The valve names ESt3, ESt3AL2 and ESt4 all create `TNESt3`. + - `TESt4R::GetPF`, overridden by `TLSt` and never called by it. + - `TESt::CheckState`, `CVs`, `BVs`, `GetPF` and `EStParams`, and `TNESt3::EStParams`. +- **`TFV4a`** (`GetPF`, `Init`): the FV4a handle of the vehicle definitions creates `TFV4aM`. +- **Base implementations always overridden:** `TFricMat::GetFC` (every material, the default P10 too, has its own) and `TPrzekladnik::Update` (every relay has its own). +- **Functions without callers:** + - In `hamulce.cpp`: `PF_old`, `TBrake::GetBCF`, `TDriverHandle::OvrldButton`, `TReservoir::pa` and `TBrakeCyl::pa`. + - In `Mover.cpp`: `BrakeVP`, `PipeRatio`, `RealPipeRatio`, `IncBrakeLevel`, `DecBrakeLevel`, `IncBrakePress`, `DecBrakePress`, `MainCtrlActualPowerPos` and `GetExternalCommand`. +- **Inverter failures** of the induction motor drives (`inverter::Failure_Drive`, `Failure_Const`, `Activate`): nothing ever sets them. The cab only shows them. The branches reading them in `TractionForce` can't be reached. + +## Code only data no game vehicle has would run + +Where it was worth it, a test fits the missing parameter to a real vehicle or builds a definition with it (`definition::set()`). Those cases are marked "tested" below. The rest is reachable only by a vehicle definition the game data doesn't have: + +- **Tested, fitted or handmade:** + - The valves CV1, CV1_R, CV1_L_TR, the W, K and Hik variants, SW, NESt3 and M483 (`brake cycle: the valves the game knows…`). + - An Oerlikon valve named with -ED or AL2 without ESt3 (its relays `TPrzekED` and `TPrzekCiagly`). + - The brake materials FR513, FR510, P10yBg and P10yBgu (`brake cycle: every material…`). + - The handles test, D2, Westinghouse, MHZ_EN57 and St113 fitted to an EU07. + - A `CompressorList`, a `PmaxList` and a `Headlights` section. + - The rear cab's compressor limits (`MinCP_B`, `MaxCP_B`). + - The controller of the electrodynamic brake apart from the local brake (`SplitEDPneumaticBrake`). +- **Not tested:** + - The retarder's clutch (`R_IsClutch`, `R_ClutchSpeed`) and the retarder worked by the local brake (`R_WithIndividual`). + - The automatic relays of type 2 (`RelayType=2`). + - The variant of `CheckLocomotiveParameters` for a definition whose `RList` doesn't match its powered axles. + +## Reachable, not tested yet + +These are run by the game, by vehicles it has or by the cab, the AI or the scenery, but no test reaches them yet. Most of them are the far sides of conditions inside functions the tests do run. + +- **Entry points from outside the core:** + - `TBrake::ForceEmptiness` and its overrides: a scenario's vehicle set up with empty brakes (`DynObj.cpp`). + - `TBrake::GetSoundFlag`: the sounds of the brakes. + - `LoadFIZ` and `reload_FIZ`: the file loading around `LoadFIZ_Text`, which the tests call directly. + - `TSecuritySystem::set_cabsignal_lock`. + - `TMHZ_EN57::GetEP`: an MHZ_EN57 handle in the EP mode of the brake. +- **Inside the functions the tests run** (blocks run of the function's total): + - `TractionForce` (305 of 373): the inverter failures above. The rest are mostly halves of conditions: + - the `SST` table of the series-excited diesel-electrics; + - the speed control's brake intervention with the air under `BrakeInterventionVel`; + - a diesel-electric's generator tripping on overcurrent; + - the `ffBrakeList` of the 22WEc, whose electrodynamic brake no test applies. + - `AutoRelayCheck` (87 of 119): the ET22's group switch under some sequences, the relays at reverse with `BackwardsBranchesAllowed`, and the shunting of the EZTs. + - `update_doors` (134 of 160), `PantographsCheck` (44 of 64) and `PowerCouplersCheck` (70 of 88): door modes and pantograph valve kinds only some game vehicles have, and the 24 V circuit across couplers. + - `RelayReset` (29 of 43) and `MainSwitchCheck` (16 of 27): relays tripping in states the drive cycles don't reach. + - `UpdatePipePressure` (108 of 130): the pressure switch lock of an electric unit's pantographs, which the EW58's automatic valve keeps away. Also the EP mode of the EN57's handle and the `LockPipe` thresholds (`LPOn`, `LPOff`), which no vehicle sets. + - `TCV1L_TR::GetPF` (9 of 20), `TKE::GetPF` (28 of 37) and the MHZ handles: settings (G with the CV1L_TR, the KE's anti-slip pressure `ASBP`) that no real vehicle has together. + +## What the tests found + +The tests record the behaviour as it is. The following points may deserve a look by someone who knows how the real vehicles behave. + +- **`Random(10) - Random(10)` (`Mover.cpp`, the noise of the control pipe pressure `CntrlPipePress`):** the order the two calls were evaluated in isn't specified. MSVC Release, MSVC Debug and GCC gave different traces, a control pipe pressure mirrored around 5 bar, which failed 32 tests on the Linux CI. Fixed: the draws are sequenced in the order of MSVC Release, which the golden traces were recorded with. +- **`TCommand::Location` isn't initialised** in a new vehicle. The game always sets it through `PutCommand()`, but a test calling `RunCommand()` directly got different results run to run. +- **`CompressorList` is `int[4][9]`** (`MOVER.h`), though its factors are fractions: 0.9 is read as 0. No game vehicle has the list yet. +- **A train whose pipe leaks under 2.75 bar before its start-up can't be released:** + - Where this happens: an FV4a handle and an LSt valve, for example 4 minutes standing with wagons, waiting for a hot engine. + - The cause: the pipe gets cut off the main reservoir (`lock_old` in `UpdatePipePressure`). The AI presses its unlock button only for the lock set by `LPOn` and `LPOff` (`control_main_pipe`). + - The same after every emergency stop of such a train, by the alarm chain or the radio-stop: reset, it stays with its pipe empty until the releaser is held (`b_rls`), which the test of the emergency stops does. +- **EW58:** + - Its electrodynamic brake comes in only by the stages of the ep brake (TUHEX), never by the brake pipe alone. + - It needs 110 V, from the converter of the middle car only. +- **ET42:** + - At the high current setting the controller stops at the last series position, and the train crawls. + - From the low setting's top speed (over 100 km/h) the dynamic brake's current trips the overload relay at once. +- **EU47:** its local brake sets the flag of the electrodynamic brake, which keeps the train brake's air out, but the motors don't brake. The local brake's air does it. +- **EP09:** the flag of the automatic electrodynamic brake stays on at a standstill while the cylinders hold less than 2.1 bar, and it flicks on while a high application is released. +- **Diesel engines with a second water circuit (SU45, ST44) started at 88 degrees:** they don't start until that circuit, over its limit (74 or 75 degrees), cools. Off, it cools slowly. +- **Brake cylinders over the vehicle's highest pressure** (found by the invariants, see the README): + - The K valve of the SM03 and the EU04 fills the cylinders to the pressure of the auxiliary reservoir in a full service or emergency application: 4.27 bar of the SM03's MaxBP 3.5, 3.19 bar of the EU04's 2.8. + - The ESt3d_PZZ valve of the SN61 does so in an emergency: 4.6 bar of its MaxBP 3.8. + - The EU07's LSt overshoots a little, to 4.0 bar of its 3.8. +- **Diesel engines driven over their top speed:** a lower gear taken at speed drives the engine through the wheels over its `nmax`, the SM03's by 10%, the WMB10's by 25%. The cutoff (`nmax_cutoff`) only takes the fuel off. +- **The ELF's C car's main reservoir over its governor:** the cars share the supply pipe, the A car's governor stops at 9 bar, the C car's at 8.5, so the C car's reservoir reaches 8.64 bar. +- **Wagons thrown about as a train starts with full physics of the couplers:** the slack taken up throws them at up to 5.7 g, and back within a step. +- **Nothing limits the speed:** a light train at full power runs well past its Vmax (the 754 past 160 km/h, against a Vmax of 100). +- **Vehicles that don't do what they should, as driven by the tests:** + - An EN57 doesn't get ready (main reservoir). + - An EN57AKL alone doesn't charge its pipe. + - The TEM2 barely moves, and the SM31 crawls at 0.3 km/h at full power, its motors taking 444 A. + - The draisine can't start from rest (the pump's force is under the resistance of a standing draisine). + - The Elf and the Impuls, their doors opened with the steps permitted, close the doors as they start off but keep the steps out, which keeps them from moving. + - The ED72 holds an ep application with its handle at running, and with the ep brake switched off too; only the ep release position lets it off. The EW58 releases at running. + - The road vehicle (MAN) is only slowed down by a minute of full service. +- **A train run with five physics steps in a frame:** an EU07 with wagons takes no current, its controller at full power, and rolls at a few km/h. The test steps the vehicles the way `TDynamicObject` does (`FastComputeMovement` for the steps before the last one), so the game may do the same. +- **Game data:** + - The 214M's file has `TurboPos` and `MotorParamTable` twice, and the EN57AL's `Engine` twice. The loader reads each in turn, so the later one overrides what both set. + - A wagon set up braked keeps its hand brake on until someone releases it. The AI does that for the whole train (`AutoRewident`), so a test has to as well. diff --git a/tests/unit/README.md b/tests/unit/README.md index 97cc94910..a10f1798e 100644 --- a/tests/unit/README.md +++ b/tests/unit/README.md @@ -10,6 +10,9 @@ The code under test is `eu07_core`, the library of the simulation parts the game game uses it. The python library gets linked along with the game's settings, but the tests don't use it and run without it. +[COVERAGE.md](COVERAGE.md) says how much of the simulation core the tests reach, what's left and why, and +what they found. + ## Running - **With the build**: building the game (`WITH_TESTS`, on by default) runs them at the end, once the @@ -46,24 +49,127 @@ TEST_CASE("brakes: service braking stops a heavier wagon too") { - `definition::load("")` reads `data/.json`, with the sections and keys of the game's vehicle definitions; `set()` and `remove()` change it for the test. Table sections give their rows as `"rows": [[...], ...]`, keys starting with `_` are comments. A section the loader doesn't - know is an error (the table of them is in `vehicle.cpp`); a misspelt key isn't caught, so check + know is an error (the table of them is in `fiz.cpp`); a misspelt key isn't caught, so check in the test that the value the test is about made it to the vehicle. Keep the definitions physically sensible: some parts need keys others don't (the ESt brake valves need `Brake.Size`), and a test on a half-defined vehicle tests the definition rather than the simulation. +- `definition::fiz("/")` is one of the game's own vehicles, `data/fiz//.json`, + in the same form, converted from the game's `.fiz` with the files it includes (the list and how to + add one: `data/fiz/README.md`). The conversion keeps what the game's loader reads of the file and + checks it reads the same of the converted one (`fiz.h`), so the vehicle is the game's. - The random numbers of the simulation are reseeded before every test case, so a test gives the - same result alone or among the others. + same result alone or among the others. Loading a vehicle draws some too (the scatter of its brake + pipe pressure), so a test making vehicles in a loop calls `test::reseed_random()` before each. - Logic which lives in classes too big to set up in a test (the AI driver, the cab) goes into free functions on the vehicle state the class calls, and the test calls them the same way: see `vehicle/handover.h`, which `TController::TakeControl()` uses, and `test_handover.cpp`. - `vehicle` creates the vehicle the way `TDynamicObject::Init()` does and steps it the way the - simulation loop does, on a straight track of the given friction and gradient. `pair` puts two of - them on that track facing each other with the given ends and runs them together, as the - simulation runs the vehicles of a scene: the coupler and buffer forces, collisions and automatic - coupling come from the physics (`test_couplers.cpp`). Longer consists aren't supported yet. + simulation loop does, on a straight track of the given friction, gradient, catenary voltage and + physics iterations a frame. With a cab (`vehicle{definition, 0.0, "name", 1}`) it has a driver. +- `consist` (`consist.h`) couples vehicles into a train and steps them together, coupler forces, + air, control and power passing between them. The physics reaches a neighbour through its + `TDynamicObject`, for its `MoverParameters` only; the tests have a stand-in of it with just that. +- `drive.h` has the driver's part, made of the AI's steps: `start_up()` (battery, pumps, + pantographs, line breaker, converter, compressor), `drive()` (running, acknowledging the + vigilance), `release_brakes()`, `drive_cycle()` (a whole run from start-up to a stop). +- `scenario` (`scenario.h`) puts a train, its driver and a golden trace for each vehicle together, + for a test of a feature: `act()` marks an event in the traces, `wait()` runs the train. +- `pair` (`vehicle.h`) puts two vehicles on that track facing each other with the given ends and + runs them together, as the simulation runs the vehicles of a scene: the coupler and buffer + forces, collisions and automatic coupling come from the physics (`test_couplers.cpp`). - Check behaviour (speeds, pressures, states the player sees) rather than internal flags, and make the test check its own preconditions (`REQUIRE` that the braking skidded, before checking what a skid leads to). +## Golden traces + +A golden trace pins down how the simulation behaves now, for changes which mustn't alter it, like +restructuring a function: the values of some channels of a vehicle over a run, compared with the +ones recorded from the code before, in `data/golden/.csv`. A difference fails the test, naming +the channel, the time and both rows. + +The samples and their key figures aren't kept in the repository (tens of megabytes, and they only mean +something against the code before the change): record them before the change, and compare after it. + +``` +git switch master +EU07_UNIT_TESTS_UPDATE_GOLDEN=1 eu07_unit_tests # records data/golden/**/*.csv, ignored by git +git switch my-refactor +EU07_UNIT_TESTS_GOLDEN_EXACT=1 eu07_unit_tests # compares every sample, bit for bit +``` + +The recorded files stay through switching branches. Without them, a trace checks the invariants and +its test's own `CHECK`s, which is what the CI does: every test states the behaviour it's about in +`CHECK`s, a scenario of a feature what the feature does (the doors open, the train stops), a run of +every real vehicle what holds for each of them (it brakes harder by every step, it runs off and stops). + +```cpp +test::trace trace{"eu07/notch up", test::channels::motion() + test::channels::traction()}; +eu07.record(trace); // samples every 10th step +eu07->MainSwitch(true); +trace.mark("main switch"); // shows in the event column of the row +eu07.run(30.0); +trace.check(); +``` + +- `EU07_UNIT_TESTS_UPDATE_GOLDEN=1` records the samples and the key figures, for a new test or a + change which alters the behaviour on purpose; the diff of the key figures shows what it altered, + review it like code. +- Without `EU07_UNIT_TESTS_GOLDEN_EXACT=1`, samples recorded are compared within a relative 1e-9, as + GCC and glibc's maths functions can differ from MSVC's in the last bits, and a trace without them + passes. With it they're compared exactly, and a trace without them or its key figures fails, so a + run of the refactoring check can't pass by having nothing to compare. +- Keep the runs short (tens of seconds): the simulation feeds back on itself, and a last bit grows. +- A trace with no assertion about what the behaviour should be is a characterisation, bugs included; + put the behaviour the test is about into `CHECK`s besides. + +### Key figures + +Next to its samples, every trace records the figures a person reads (`figures.h`), for each part of +the run between two events, in `data/golden/.figures`: the duration, the speeds, the distance, +the time to stop, the brake pipe's lowest pressure and the time to get back to 4.9 bar, the peaks of +the brake cylinders, the motor current and the tractive force. + +``` +[brake] +duration = 60.00 s +speed at the start = 118.5 km/h +distance = 944.3 m +stopped after = 45.97 s +brake cylinders peak = 3.997 bar +``` + +They're compared within 0.5% or the last decimal written, loose enough for any compiler, and the +differences are told by part and figure: + +``` +key figures of "traction/4e_v1/4e" differ: + [brake] distance: 944.3 m now, 980.0 m recorded +``` + +`EU07_UNIT_TESTS_UPDATE_GOLDEN=1` records them with the samples. After a change which alters the +behaviour, their comparison says in a few lines what the thousands of changed samples mean. A trace +sampled by hand, at times of its own (the commands and switches tests), records none. + +### Invariants + +Every trace also checks, on every step of its vehicle and not only on the recorded ones, the rules +which hold for any vehicle whatever a test does (`invariants.h`): no NaN, the brake cylinders under +the vehicle's highest pressure, the pipes under the highest governor setting, no motor current with +the line breaker open, no tractive force without power, the distance counted as the speed runs, no +acceleration over 5 g, and a few more. They compare nothing with a recorded file, so they hold on +any compiler, and they still check a behaviour recorded anew on purpose makes sense. A broken rule +fails the test, naming the time, the last event and the values: + +``` +invariant "no motor current with the line breaker open" broken in traction/et42 fuse/0 at 63.200 s, +after "dynamic brake": Mains=0 Im=-412.5 Itot=-412.5 +``` + +Where a test makes a vehicle break a rule (a collision), or a game vehicle breaks one as it is, the +list of excuses in `invariants.cpp` names the trace, the rule and the reason; the second kind goes +into the findings of [COVERAGE.md](COVERAGE.md) too. + ## Code to test, and when the tests fail to link A source joins the tested code by moving from `SOURCES` to `CORE_SOURCES` in the main diff --git a/tests/unit/consist.cpp b/tests/unit/consist.cpp new file mode 100644 index 000000000..a5f10d936 --- /dev/null +++ b/tests/unit/consist.cpp @@ -0,0 +1,106 @@ +#include "stdafx.h" +#include "consist.h" + +#include "vehicle/DynObj.h" + +namespace test { + +consist::consist() = default; +consist::~consist() = default; + +vehicle &consist::add(definition const &Definition, int Cab, int Coupling, double Velocity, double Gap) { + auto added{std::make_unique(Definition, Velocity, std::format("test{}", m_vehicles.size()), Cab)}; + auto &mover{added->mover()}; + + std::vector object(sizeof(TDynamicObject)); + reinterpret_cast(object.data())->MoverParameters = &mover; // NOSONAR see m_objects + + auto position{0.0}; + if (false == m_vehicles.empty()) { + auto &last{m_vehicles.back()->mover()}; + position = m_positions.back() - 0.5 * (last.Dim.L + mover.Dim.L) - Gap; + mover.Loc = TLocation{position, 0.0, 0.0}; + if (Coupling != coupling::faux) { + last.Attach(end::rear, end::front, &mover, Coupling, true, false); + mover.Attach(end::front, end::rear, &last, Coupling, true, false); + } + } + else { + mover.Loc = TLocation{position, 0.0, 0.0}; + } + m_vehicles.push_back(std::move(added)); + m_positions.push_back(position); + m_objects.push_back(std::move(object)); + update_neighbours(); + return *m_vehicles.back(); +} + +void consist::update_neighbours() { + // TDynamicObject::update_neighbours(), for vehicles coupled in a line + for (std::size_t i = 0; i < m_vehicles.size(); ++i) { + auto &mover{m_vehicles[i]->mover()}; + for (int end = end::front; end <= end::rear; ++end) { + auto &neighbour{mover.Neighbours[end]}; + auto const other{end == end::front ? static_cast(i) - 1 : static_cast(i) + 1}; + if (other < 0 || other >= static_cast(m_vehicles.size())) { + neighbour = neighbour_data(); + continue; + } + auto const &othermover{m_vehicles[other]->mover()}; + auto const otherend{end == end::front ? end::rear : end::front}; + auto const distance{TMoverParameters::CouplerDist(&mover, &othermover) - mover.Couplers[end].adapter_length - + othermover.Couplers[otherend].adapter_length}; + // a vehicle not coupled is a collision source when it's within the range the game scans + if (mover.Couplers[end].Connected == nullptr && distance > std::max(10.0, std::abs(mover.V)) + 40.0) { + neighbour = neighbour_data(); + continue; + } + neighbour.vehicle = reinterpret_cast(m_objects[other].data()); // NOSONAR see m_objects + neighbour.vehicle_end = otherend; + neighbour.distance = static_cast(distance); + } + } +} + +void consist::step(track const &Track, double Dt) { + for (auto const &vehicle : m_vehicles) { + vehicle->begin_step(); + } + update_neighbours(); + for (int iteration = 1; iteration < Track.iterations; ++iteration) { + for (auto const &vehicle : m_vehicles) { + vehicle->forces(Dt); + } + for (std::size_t i = 0; i < m_vehicles.size(); ++i) { + m_positions[i] += m_vehicles[i]->fast_move(Track, Dt, TLocation{m_positions[i], 0.0, 0.0}); + } + } + for (auto const &vehicle : m_vehicles) { + vehicle->forces(Dt); + } + // the average pressure in the train's brake pipe weighed by the vehicles' lengths, TDynamicObject::GetEPP(), + // over the whole train (the game's stops at a vehicle without the brake hose coupled) + auto pipe{0.0}; + auto length{0.0}; + for (auto const &vehicle : m_vehicles) { + pipe += vehicle->mover().PipePress * vehicle->mover().Dim.L; + length += vehicle->mover().Dim.L; + } + pipe /= length; + for (std::size_t i = 0; i < m_vehicles.size(); ++i) { + m_positions[i] += m_vehicles[i]->move(Track, Dt, TLocation{m_positions[i], 0.0, 0.0}, pipe); + } +} + +void consist::run(double Seconds, track const &Track, double Dt) { + auto const steps{std::lround(Seconds / (Dt * Track.iterations))}; + for (long i = 0; i < steps; ++i) { + step(Track, Dt); + } +} + +double consist::gap(std::size_t Index) const { + return TMoverParameters::CouplerDist(&m_vehicles[Index]->mover(), &m_vehicles[Index + 1]->mover()); +} + +} // namespace test diff --git a/tests/unit/consist.h b/tests/unit/consist.h new file mode 100644 index 000000000..02001e042 --- /dev/null +++ b/tests/unit/consist.h @@ -0,0 +1,57 @@ +#pragma once + +#include +#include +#include + +#include "vehicle.h" + +class TDynamicObject; + +namespace test { + +// vehicles coupled into a train on a straight track, all facing forward, from the front one back, stepped the +// way vehicle_table::update() steps the vehicles of the game: the constants and neighbours of all of them, +// their forces, then their movement. couplers carry the forces, the air, the control and the power between +// the vehicles as in the game: +// test::consist en57; +// en57.add(definition::fiz("en57_v1/5bgb"), 1); // driven from the front cab +// en57.add(definition::fiz("en57_v1/6ba_840")); +// en57.add(definition::fiz("en57_v1/5bgb")); +class consist { +public: + // everything a coupler can join, as two vehicles of a train coupled by a crew + static constexpr int full_coupling{coupling::coupler | coupling::brakehose | coupling::mainhose | coupling::control | coupling::gangway | coupling::heating | + coupling::permanent | coupling::power110v | coupling::power24v | coupling::highvoltage}; + + consist(); + ~consist(); + consist(consist const &) = delete; + consist &operator=(consist const &) = delete; + + // a vehicle at the back of the train, its front coupler joined to the rear one of the last vehicle with + // the given coupling (coupling flags), Gap [m] between their buffers. Cab as for a vehicle. a vehicle + // with no coupling (coupling::faux) is free on the same track, as one about to be coupled or hit + vehicle &add(definition const &Definition, int Cab = 0, int Coupling = full_coupling, double Velocity = 0.0, double Gap = 0.0); + + vehicle &operator[](std::size_t Index) { return *m_vehicles[Index]; } + std::size_t size() const { return m_vehicles.size(); } + + void step(track const &Track = {}, double Dt = 0.01); + void run(double Seconds, track const &Track = {}, double Dt = 0.01); + + // distance between the couplers of the vehicle and the one behind it [m], negative when the buffers press + double gap(std::size_t Index) const; + +private: + void update_neighbours(); + + std::vector> m_vehicles; + std::vector m_positions; // of the vehicles' middles along the track + // the physics reaches a neighbouring vehicle through its TDynamicObject, for its MoverParameters alone. + // the game's object can't be made without the rest of the game, so it's a stand-in: storage of its size + // with that one member set, never constructed, which only the physics ever looks into + std::vector> m_objects; +}; + +} // namespace test diff --git a/tests/unit/data/fiz/11xa_v2/93a.json b/tests/unit/data/fiz/11xa_v2/93a.json new file mode 100644 index 000000000..63dbb4fbb --- /dev/null +++ b/tests/unit/data/fiz/11xa_v2/93a.json @@ -0,0 +1,14 @@ +{ + "_source": "pkp/11xa_v2/93a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "40000", "Vmax": "120", "HeatingP": "32", "LightP": "0.56"}, + "Load": {"MaxLoad": "8", "LoadQ": "pieces", "LoadAccepted": "Workers", "OverLoadFactor": "1.5", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "24.5", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.50", "Bd": "17.2", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "ESt4", "Size": "200", "NBpA": "4", "MBF": "90", "MaxBP": "4.0", "BCN": "2", "BCR": "0.1778", "BCD": "0.150", "BCM": "8.17", "BCS": "1.4", "BSA": "4.0", "BM": "P10-Bgu", "BVV": "200", "BRE": "0.87"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.11", "FmaxB": "320", "kC": "4500", "DmaxC": "0.1", "FmaxC": "850", "beta": "0.58", "AllowedFlag": "115"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocalBrake": "ManualBrake", "BrakeDelays": "R", "ASB": "Yes", "MCPN": "1", "rows": []}, + "Light": {"Light": "Generator", "LGeneratorEngine": "WheelsDriven", "LMaxVoltage": "24", "AlterLight": "Accu", "AlterLMaxVoltage": "24", "AlterLCap": "495", "AlterLRS": "Generator"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"}, + "Doors": {"OpenCtrl": "Passenger", "CloseCtrl": "Mixed", "OpenSpeed": "70", "CloseSpeed": "400", "DoorMaxShiftL": "80", "DoorMaxShiftR": "80", "DoorOpenMethod": "Fold", "DoorClosureWarning": "No"} +} diff --git a/tests/unit/data/fiz/14xa_v1/173a.json b/tests/unit/data/fiz/14xa_v1/173a.json new file mode 100644 index 000000000..d1243f7ed --- /dev/null +++ b/tests/unit/data/fiz/14xa_v1/173a.json @@ -0,0 +1,14 @@ +{ + "_source": "pkp/14xa_v1/173a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "42000", "Mred": "1600", "Vmax": "160", "HeatingP": "42", "LightP": "0.56"}, + "Load": {"MaxLoad": "30", "LoadQ": "pieces", "LoadAccepted": "Passengers", "OverLoadFactor": "1.5", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "24.5", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.70", "Bd": "17.2", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "ESt4HBG300-s216", "Size": "375", "NBpA": "4", "MBF": "90", "MaxBP": "3.8", "BCN": "2", "BCR": "0.2032", "BCD": "0.150", "BCM": "9.04", "BCS": "1.6", "BSA": "4.0", "BM": "P10-Bgu", "BVV": "375", "BRE": "0.95"}, + "Doors": {"OpenCtrl": "Passenger", "CloseCtrl": "AutomaticCtrl", "OpenSpeed": "0.36", "CloseSpeed": "0.48", "DoorCloseDelay": "2.2", "DoorMaxShiftL": "0.9", "DoorMaxShiftR": "0.9", "DoorOpenMethod": "Plug", "DoorClosureWarning": "yes", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.043", "PlatformMaxShift": "-45", "PlatformSpeed": "4", "PlatformOpenMethod": "Rotate", "DoorStayOpen": "10"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.11", "FmaxB": "320", "kC": "4500", "DmaxC": "0.1", "FmaxC": "850", "beta": "0.58", "AllowedFlag": "115"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocalBrake": "ManualBrake", "BrakeDelays": "R", "ASB": "Yes", "MCPN": "1", "rows": []}, + "Light": {"Light": "Generator", "LGeneratorEngine": "WheelsDriven", "LMaxVoltage": "24", "AlterLight": "Accu", "AlterLMaxVoltage": "24", "AlterLCap": "495", "AlterLRS": "Generator"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"} +} diff --git a/tests/unit/data/fiz/168a_v1/168a.json b/tests/unit/data/fiz/168a_v1/168a.json new file mode 100644 index 000000000..aa391639a --- /dev/null +++ b/tests/unit/data/fiz/168a_v1/168a.json @@ -0,0 +1,14 @@ +{ + "_source": "pkp/168a_v1/168a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "42000", "Mred": "1600", "Vmax": "160", "HeatingP": "20", "LightP": "0.56"}, + "Load": {"MaxLoad": "56", "LoadQ": "pieces", "LoadAccepted": "Passengers,Luggage", "OverLoadFactor": "1.5", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "24.5", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.70", "Bd": "17.2", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "ESt4HBG300", "Size": "200", "NBpA": "4", "MBF": "90", "MaxBP": "3.8", "BCN": "2", "BCR": "0.1778", "BCD": "0.150", "BCM": "10.00", "BCS": "1.4", "BSA": "6.0", "BM": "P10-Bgu", "BVV": "200", "BRE": "0.9"}, + "Doors": {"OpenCtrl": "Passenger", "CloseCtrl": "AutomaticCtrl", "OpenSpeed": "0.44", "CloseSpeed": "0.27", "DoorCloseDelay": "2.25", "DoorMaxShiftL": "0.9", "DoorMaxShiftR": "0.9", "DoorOpenMethod": "Plug", "DoorClosureWarning": "yes", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.043", "PlatformMaxShift": "-45", "PlatformSpeed": "4", "PlatformOpenMethod": "Rotate", "DoorStayOpen": "10"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.11", "FmaxB": "320", "kC": "4500", "DmaxC": "0.1", "FmaxC": "850", "beta": "0.58", "AllowedFlag": "115"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocalBrake": "ManualBrake", "BrakeDelays": "R", "ASB": "Yes", "MCPN": "1", "rows": []}, + "Light": {"Light": "Generator", "LGeneratorEngine": "WheelsDriven", "LMaxVoltage": "24", "AlterLight": "Accu", "AlterLMaxVoltage": "24", "AlterLCap": "495", "AlterLRS": "Generator"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"} +} diff --git a/tests/unit/data/fiz/21xm_v1/214ma.json b/tests/unit/data/fiz/21xm_v1/214ma.json new file mode 100644 index 000000000..10cfcd60c --- /dev/null +++ b/tests/unit/data/fiz/21xm_v1/214ma.json @@ -0,0 +1,78 @@ +{ + "_source": "pkp/21xm_v1/214ma.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "46000", "Vmax": "120", "PWR": "390", "SandCap": "100", "Type": "dmu"}, + "Load": {"MaxLoad": "50", "OverLoadFactor": "2.2", "LoadQ": "pieces", "LoadAccepted": "passengers", "LoadSpeed": "0.5", "UnLoadSpeed": "0.7"}, + "Dimensions": {"L": "24.38", "H": "4.2", "W": "2.9", "Cx": "0.0"}, + "Wheels": {"D": "0.84", "Tw": "1.435", "AIM": "300", "Axle": "B'2'", "Ad": "2", "Bd": "17.23", "Rmin": "75", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "Size": "150", "NBpA": "4", "MBF": "2.74", "MaxBP": "2.9", "LocMaxBP": "3.8", "RV": "-1", "RM": "0.77", "BCN": "8", "BCR": "0.127", "BCD": "0.1", "BCM": "2.15", "HiPP": "5", "LoPP": "3.5", "Vv": "0.125", "MinCP": "7.5", "MaxCP": "8.5", "CompressorSpeed": "0.0138", "CompressorPower": "Main", "BCS": "0.5", "BSA": "0", "BM": "Cosid", "BVV": "88", "BRE": "0.97", "TareMaxBP": "2.2", "LPOn": "3.0", "LPOff": "4.5", "UBB1": "2", "UBB2": "8"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.25", "CloseSpeed": "0.19", "DoorCloseDelay": "0.1", "DoorMaxShiftL": "0.78", "DoorMaxShiftR": "0.78", "DoorOpenMethod": "Plug", "DoorClosureWarning": "No", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.04", "MirrorMaxShift": "75", "DoorStayOpen": "10", "DoorClosexDelay": "1.8", "DoorNeedPermit": "Yes", "DoorPermitList": "1|0|2|3", "DoorPermitListDefault": "2", "DoorAutoCloseRemote": "No"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "ControlType": "214MA"}, + "SpringBrake": {"Volume": "20", "MBF": "5.8", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "0.5", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "3", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Yes", "LocalBrake": "PneumaticBrake", "ManualBrake": "No", "MCPN": "5", "SCPN": "5", "AutoRelay": "Yes", "IniCDelay": "1.0", "SCDelay": "0.3", "BrakeDelays": "PR", "MaxBPMass": "55", "BrakeHandle": "MHZ_K5P", "LocBrakeHandle": "FD1", "WaterStart": "Battery", "OilStart": "Automatic", "FuelStart": "Automatic", "ConverterStart": "Automatic", "ConverterStartDelay": "4", "BrakeOpModes": "PN", "EIMCtrlType": "3", "DirChangeMaxPos": "4", "SBD": "0.47", "AutomaticCabActivation": "No", "InactiveCabFlag": "27", "CompartmentLightsStart": "Manual", "rows": [ + ["-1","0.6","-1","20","Pneumatic"], + ["0","0.5","-1","2","Pneumatic"], + ["1","0.46","-1","2","Pneumatic"], + ["2","0.42","-1","2","Pneumatic"], + ["3","0.38","-1","2","Pneumatic"], + ["4","0.34","-1","2","Pneumatic"], + ["5","0.29","-1","2","Pneumatic"], + ["6","0","-1","40","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "EmergencyBrakeDelay": "2.1", "RadioStop": "Yes", "CabDependent": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "28", "LMaxCurrent": "500"}, + "Clima": {"HMaxVoltage": "3000", "Heating": "Generator", "HGeneratorEngine": "Main", "HGeneratorMinRPM": "696", "HGeneratorMinVoltage": "2400", "HGeneratorMaxRPM": "1500", "HGeneratorMaxVoltage": "3300"}, + "Engine": {"EngineType": "DieselEngine", "Trans": "50:129", "TransEff": "0.91", "nmin": "800", "nmax": "2000", "nmax_cutoff": "1950", "AIM": "65", "EUS": "102.5", "EDS": "102.0", "IsTC": "Yes", "TC_TMMax": "1.82", "TC_CP": "0.75", "TC_LT": "3000", "TC_LR": "1", "TC_ULR": "1", "TC_FRI": "1", "TC_FRD": "2", "TC_TII": "2.93", "TC_TIO": "0", "TC_TOO": "-1.65", "TC_LS": "29.5", "TC_ULS": "40", "IsRetarder": "Yes", "R_Place": "1", "R_TII": "5.0", "R_MT": "1500", "R_MP": "250", "R_FRI": "1", "R_FRD": "1", "R_MinVel": "30", "WaterMaxTemperature": "105", "WaterCoolingTemperature": "90", "WaterFlowTemperature": "35", "WaterAuxCircuit": "Yes"}, + "SpeedControl": {"SpeedCtrl": "Yes", "SpeedButtons": "0|30|40|50|60|70|80|90|100|120", "OverrideManual": "Yes", "InitPwr": "0.3", "MaxPwrVel": "5", "StartVel": "2", "VelStep": "5", "PwrStep": "0.1", "MinPwr": "0.3", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "120", "Offset": "-0.5", "kPpos": "0.1", "kPneg": "0.4", "kIpos": "0.06", "kIneg": "0.06", "BrakeIntervention": "Yes", "PowerUpSpeed": "0.3", "PowerDownSpeed": "0.3"}, + "TurboPos": {"TurboPos": "1"}, + "MotorParamTable": {"minVelfullengage": "3.0", "engageDia": "1", "engageMaxForce": "3000", "engagefriction": "1", "rows": []}, + "MotorParamTable0": {"minVelfullengage": "3.0", "engageDia": "1", "engageMaxForce": "3000", "engagefriction": "1", "rows": [ + ["0","0","0","0","-1","-1","0","1"], + ["1","2.81","-1","-1","38.2","42.4","0.18","1"], + ["2","1.84","17.4","29.9","53.8","59.8","0.13","1"], + ["3","1.36","26.6","45.0","70.8","78.7","0","1"], + ["4","1.00","61.1","61.1","90.4","100.4","0","1"], + ["5","0.80","80.2","80.2","200","200","0","1"] + ]}, + "DList": {"Size": "5", "Mmax": "2450", "nMmax": "21.7", "nmax": "33", "Mnmax": "2160", "nominalfill": "1.0", "Mstand": "250.0", "NomFuelConsRate": "220", "rows": [ + ["0","0.15","0"], + ["1","0.20","2"], + ["2","0.40","2"], + ["3","0.60","2"], + ["4","0.80","2"], + ["5","1.00","2"] + ]}, + "DMList": {"rows": [ + ["0","0"], + ["80","637"], + ["400","796"], + ["800","1592"], + ["1050","2090"], + ["1300","2200"], + ["1550","2124"], + ["1800","2070"], + ["2000","1676"] + ]}, + "UCList": {"Size": "5", "IntegratedBrakePN": "Yes", "IntegratedBrake": "Yes", "rows": [ + ["0","3","0","0","0","0.15","0.15","0","1","0"], + ["1","2","0","0","0","0.15","0.15","1","3","0"], + ["2","1","-1","0","-1","0.15","0.15","3","3","1"], + ["3","1","-1","1","0","0.00","0.15","3","4","1"], + ["4","0","-1","1","0","0.15","0.00","4","5","3"], + ["5","0","0","1","1","0.15","0.15","4","5","3"] + ]}, + "LightsList": {"Size": "5", "Wrap": "no", "Default": "1", "rows": [ + ["0","0"], + ["21","34"], + ["22","34"], + ["34","34"], + ["16","1"] + ]}, + "WiperList": {"Size": "5", "Angle": "75", "Default": "1", "rows": [ + ["3","0.5","0.5","0.0"], + ["0","0.8","0.0","0.5"], + ["3","0.8","6.0","0.5"], + ["3","0.8","3.0","0.0"], + ["3","0.8","1.0","0.0"] + ]} +} diff --git a/tests/unit/data/fiz/21xm_v1/218ma_a_kd.json b/tests/unit/data/fiz/21xm_v1/218ma_a_kd.json new file mode 100644 index 000000000..6d2f9db61 --- /dev/null +++ b/tests/unit/data/fiz/21xm_v1/218ma_a_kd.json @@ -0,0 +1,84 @@ +{ + "_source": "pkp/21xm_v1/218ma_a_kd.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "38500", "Vmax": "120", "PWR": "360", "SandCap": "100", "Type": "dmu"}, + "Load": {"MaxLoad": "50", "OverLoadFactor": "2.2", "LoadQ": "pieces", "LoadAccepted": "passengers", "LoadSpeed": "0.5", "UnLoadSpeed": "0.7"}, + "Dimensions": {"L": "20.75", "H": "4.2", "W": "2.9", "Cx": "0.10"}, + "Wheels": {"D": "0.84", "Tw": "1.435", "AIM": "300", "Axle": "B'1'", "Ad": "2", "Bd": "17.23", "Rmin": "75", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "Size": "150", "NBpA": "4", "MBF": "2.74", "MaxBP": "2.9", "LocMaxBP": "3.8", "RV": "-1", "RM": "0.77", "BCN": "6", "BCR": "0.127", "BCD": "0.1", "BCM": "2.15", "HiPP": "5", "LoPP": "3.5", "Vv": "0.125", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.015", "CompressorPower": "Main", "BCS": "0.5", "BSA": "0", "BM": "Cosid", "BVV": "88", "BRE": "0.97", "TareMaxBP": "2.2", "LPOn": "3.0", "LPOff": "4.5", "UBB1": "2", "UBB2": "8"}, + "SpringBrake": {"Volume": "15", "MBF": "4.35", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "0.5", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.25", "CloseSpeed": "0.19", "DoorCloseDelay": "0.1", "DoorMaxShiftL": "0.78", "DoorMaxShiftR": "0.78", "DoorOpenMethod": "Plug", "DoorClosureWarning": "No", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.04", "MirrorMaxShift": "75", "DoorStayOpen": "10", "DoorNeedPermit": "Yes", "DoorPermitList": "1|0|2|3", "DoorPermitListDefault": "2", "DoorAutoCloseRemote": "No"}, + "BuffCoupl1": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39"}, + "BuffCoupl2": {"CType": "Articulated", "AllowedFlag": "-55"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "3", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Yes", "LocalBrake": "PneumaticBrake", "ManualBrake": "No", "MCPN": "5", "SCPN": "5", "AutoRelay": "Yes", "IniCDelay": "1.0", "SCDelay": "0.3", "BrakeDelays": "PR", "MaxBPMass": "55", "BrakeHandle": "MHZ_K5P", "LocBrakeHandle": "FD1", "WaterStart": "Battery", "OilStart": "Automatic", "FuelStart": "Automatic", "ConverterStart": "Automatic", "ConverterStartDelay": "4", "BrakeOpModes": "PN", "EIMCtrlType": "3", "DirChangeMaxPos": "4", "SBD": "0.47", "AutomaticCabActivation": "No", "InactiveCabFlag": "27", "CompartmentLightsStart": "Manual", "rows": [ + ["-1","0.6","-1","20","Pneumatic"], + ["0","0.5","-1","2","Pneumatic"], + ["1","0.46","-1","2","Pneumatic"], + ["2","0.42","-1","2","Pneumatic"], + ["3","0.38","-1","2","Pneumatic"], + ["4","0.34","-1","2","Pneumatic"], + ["5","0.29","-1","2","Pneumatic"], + ["6","0","-1","40","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "2.1", "RadioStop": "Yes", "CabDependent": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "28", "LMaxCurrent": "500"}, + "Engine": {"EngineType": "DieselEngine", "Trans": "50:129", "TransEff": "0.95", "nmin": "800", "nmax": "1800", "nmax_cutoff": "1850", "nreg_acc": "150", "AIM": "30", "EUS": "102.5", "EDS": "102.0", "IsTC": "Yes", "TC_TMMax": "1.82", "TC_CP": "0.85", "TC_LT": "2500", "TC_LR": "1", "TC_ULR": "1", "TC_FRI": "1", "TC_FRD": "2", "TC_TII": "2.93", "TC_TIO": "0", "TC_TOO": "-1.65", "TC_LS": "22", "TC_ULS": "40", "IsRetarder": "Yes", "R_Place": "1", "R_TII": "5.0", "R_MT": "1500", "R_MP": "250", "R_FRI": "1", "R_FRD": "1", "R_MinVel": "1", "WaterMaxTemperature": "105", "WaterCoolingTemperature": "95", "WaterFlowTemperature": "75", "WaterAuxCircuit": "No", "WaterCoolingFanSpeed": "-100", "NominalCoolingPower": "300"}, + "Clima": {"HMaxVoltage": "3000", "Heating": "Generator", "HGeneratorEngine": "Main", "HGeneratorMinRPM": "696", "HGeneratorMinVoltage": "2400", "HGeneratorMaxRPM": "1500", "HGeneratorMaxVoltage": "3300"}, + "SpeedControl": {"SpeedCtrl": "Yes", "SpeedButtons": "0|30|40|50|60|70|80|90|100|120", "OverrideManual": "Yes", "InitPwr": "0.3", "MaxPwrVel": "5", "StartVel": "2", "VelStep": "5", "PwrStep": "0.1", "MinPwr": "0.3", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "120", "Offset": "-0.5", "kPpos": "0.1", "kPneg": "0.4", "kIpos": "0.06", "kIneg": "0.06", "BrakeIntervention": "Yes", "PowerUpSpeed": "0.17", "PowerDownSpeed": "0.17"}, + "TurboPos": {"TurboPos": "1"}, + "MotorParamTable": {"minVelfullengage": "3.0", "engageDia": "1", "engageMaxForce": "2300", "engagefriction": "1", "rows": []}, + "MotorParamTable0": {"minVelfullengage": "3.0", "engageDia": "1", "engageMaxForce": "2300", "engagefriction": "1", "rows": [ + ["0","0","0","0","-1","-1","0","1"], + ["1","2.81","-1","-1","31.3","33.7","0.17","1"], + ["2","1.84","17.4","29.9","47.9","51.5","0.11","1"], + ["3","1.36","26.6","45.0","64.8","69.7","0","1"], + ["4","1.00","61.1","61.1","84.2","90.8","0","1"], + ["5","0.80","80.2","80.2","200","200","0","1"] + ]}, + "DList": {"Size": "5", "Mmax": "2450", "nMmax": "21.7", "nmax": "30", "Mnmax": "2160", "nominalfill": "1.0", "Mstand": "250.0", "NomFuelConsRate": "220", "rows": [ + ["0","0.15","0"], + ["1","0.20","2"], + ["2","0.40","2"], + ["3","0.60","2"], + ["4","0.80","2"], + ["5","1.00","2"] + ]}, + "DMList": {"rows": [ + ["0","0"], + ["80","637"], + ["400","796"], + ["800","1592"], + ["1050","2090"], + ["1300","2200"], + ["1550","2032"], + ["1800","1910"], + ["2000","1547"] + ]}, + "V2NList": {"rows": [ + ["0","1200"], + ["40","1800"], + ["120","1800"] + ]}, + "UCList": {"Size": "5", "IntegratedBrakePN": "Yes", "IntegratedBrake": "Yes", "rows": [ + ["0","3","0","0","0","0.15","0.15","0","1","0"], + ["1","2","0","0","0","0.15","0.15","1","3","0"], + ["2","1","-1","0","-1","0.15","0.15","3","3","1"], + ["3","1","-1","1","0","0.00","0.15","3","4","1"], + ["4","0","-1","1","0","0.15","0.00","4","5","3"], + ["5","0","0","1","1","0.15","0.15","4","5","3"] + ]}, + "LightsList": {"Size": "5", "Wrap": "no", "Default": "1", "rows": [ + ["0","0"], + ["21","34"], + ["22","34"], + ["34","34"], + ["16","1"] + ]}, + "WiperList": {"Size": "5", "Angle": "75", "Default": "1", "rows": [ + ["3","0.5","0.5","0.0"], + ["0","0.8","0.0","0.5"], + ["3","0.8","6.0","0.5"], + ["3","0.8","3.0","0.0"], + ["3","0.8","1.0","0.0"] + ]} +} diff --git a/tests/unit/data/fiz/3wl_v2/3wl.json b/tests/unit/data/fiz/3wl_v2/3wl.json new file mode 100644 index 000000000..96e22b6ca --- /dev/null +++ b/tests/unit/data/fiz/3wl_v2/3wl.json @@ -0,0 +1,11 @@ +{ + "_source": "pkp/3wl_v2/3wl.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "10500", "Vmax": "100"}, + "Load": {"MaxLoad": "30", "LoadQ": "tonns", "LoadAccepted": "Coal,Sand,sand2,ruda,tluczen,scrap,scrap2", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "9.87", "H": "3", "W": "3", "Cx": "0.1"}, + "Wheels": {"D": "1", "Tw": "1.435", "AIM": "300", "Axle": "1'1'", "Bd": "6", "Rmin": "30", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "ESt3", "Size": "12", "MBF": "72", "MaxBP": "3.8", "BCN": "1", "BCR": "0.1524", "BCD": "0.14", "BCMlo": "3.05", "BCMHi": "10.67", "NBpA": "4", "BM": "P10-Bg", "BCS": "2.7", "BSA": "8.00", "BVV": "50", "BRE": "0.8"}, + "BuffCoupl": {"CType": "Screw", "kB": "9000", "DmaxB": "0.12", "FmaxB": "350", "kC": "4500", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.52", "AllowedFlag": "3"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocalBrake": "ManualBrake", "BrakeDelays": "GP", "rows": []} +} diff --git a/tests/unit/data/fiz/430w_v2/430w.json b/tests/unit/data/fiz/430w_v2/430w.json new file mode 100644 index 000000000..b99807857 --- /dev/null +++ b/tests/unit/data/fiz/430w_v2/430w.json @@ -0,0 +1,11 @@ +{ + "_source": "pkp/430w_v2/430w.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "20500", "Vmax": "100"}, + "Load": {"MaxLoad": "59.5", "LoadQ": "tonns", "LoadAccepted": "coal,piasek,piasek2,ruda,tluczen,zlom,zlom2,siarka", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "14.028", "H": "3.235", "W": "3.04", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "Tw": "1.435", "AIM": "300", "Axle": "2'2'", "Ad": "2", "Bd": "8.5", "Rmin": "75", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "ESt3AL2HBG300", "Size": "100", "MBF": "144", "MaxBP": "3.8", "TareMaxBP": "1.3", "BCN": "1", "BCR": "0.2032", "BCD": "0.145", "BCM": "12.00", "NBpA": "4", "BM": "P10-Bg", "BCS": "2.7", "BSA": "16.00", "BVV": "100", "BRE": "0.9"}, + "BuffCoupl": {"CType": "Screw", "kB": "9000", "DmaxB": "0.12", "FmaxB": "350", "kC": "4500", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.52", "AllowedFlag": "3"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocalBrake": "ManualBrake", "BrakeDelays": "GP", "rows": []} +} diff --git a/tests/unit/data/fiz/4e_v1/4e.json b/tests/unit/data/fiz/4e_v1/4e.json new file mode 100644 index 000000000..6781a9e88 --- /dev/null +++ b/tests/unit/data/fiz/4e_v1/4e.json @@ -0,0 +1,81 @@ +{ + "_source": "pkp/4e_v1/4e.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "80000", "Mred": "16000", "Vmax": "125", "PWR": "2000", "SandCap": "500"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "15.915", "H": "4.348", "W": "2.903", "Cx": "0.38"}, + "Wheels": {"D": "1.250", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "3.05", "Bd": "8.55", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.0", "MaxASBP": "1.1", "RM": "0.635", "BCN": "8", "BCR": "0.1143", "BCD": "0.05", "BCM": "5.18", "Vv": "0.828", "MinCP": "7.0", "MaxCP": "8.7", "BCS": "1", "BSA": "0.5", "BM": "P10-Bgu", "BVV": "278", "CompressorSpeed": "0.0566", "BRE": "0.9"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "103", "ControlType": "4e"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "7", "BDelay3": "39", "BDelay4": "17", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "DynamicBrake": "Passive", "MCPN": "43", "SCPN": "6", "AutoRelay": "No", "IniCDelay": "0.5", "SCDelay": "0.26", "SCDDelay": "0.05", "BrakeDelays": "GPR", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "MotorBlowersStart": "Converter", "rows": [ + ["-1","0.6","-1","12","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.43","-1","9","Pneumatic"], + ["2","0.40","-1","9","Pneumatic"], + ["3","0.37","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.28","-1","9","Pneumatic"], + ["6","0","-1","18","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareMinSpeed": "10", "AwareDelay": "60", "SoundSignalDelay": "3", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "4", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "3600", "MaxCurrent": "1250", "MinH": "0.48", "MaxH": "2.5", "CSW": "1.4", "MinV": "1900", "MinPress": "3.5", "MaxPress": "5.0", "PhysicalLayout": "3"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "18:79", "WindingRes": "0.14", "nmax": "2390", "MotorBlowersSpeed": "-1310"}, + "MotorParamTable0": {"rows": [ + ["0","21.8","105","0.11","140.9","105","0.11"], + ["1","20.22","140","0.07","130.7","140","0.07"], + ["2","21.06","190","0.09","136.2","190","0.09"], + ["3","23.95","160","0.25","154.8","160","0.25"], + ["4","21.73","255","0.16","140.5","255","0.16"], + ["5","24.66","170","0.35","159.4","170","0.35"], + ["6","23","800","0.02","148.7","800","0.02"] + ]}, + "Circuit": {"CircuitRes": "0.5", "ImaxLo": "600", "ImaxHi": "750", "IminLo": "350", "IminHi": "470"}, + "RList": {"Size": "43", "RVent": "Automatic", "RVentnmax": "3300", "RVentCutOff": "0.2", "rows": [ + ["0","0","0","0","0"], + ["1","24.891","1","4","0"], + ["2","16.821","1","4","0"], + ["3","12.301","1","4","0"], + ["4","9.771","1","4","0"], + ["5","8.111","1","4","0"], + ["6","6.915","1","4","0"], + ["7","6.481","1","4","0"], + ["8","6.047","1","4","0"], + ["9","5.629","1","4","0"], + ["10","5.211","1","4","0"], + ["11","4.827","1","4","0"], + ["12","4.443","1","4","0"], + ["13","4.099","1","4","0"], + ["14","3.755","1","4","0"], + ["15","3.442","1","4","0"], + ["16","3.129","1","4","0"], + ["17","2.838","1","4","0"], + ["18","2.547","1","4","0"], + ["19","2.286","1","4","0"], + ["20","2.025","1","4","0"], + ["21","1.780","1","4","0"], + ["22","1.535","1","4","0"], + ["23","1.275","1","4","0"], + ["24","1.095","1","4","0"], + ["25","0.775","1","4","0"], + ["26","0.525","1","4","0"], + ["27","0.275","1","4","0"], + ["28","0.000","1","4","0"], + ["29","5.44","2","2","0"], + ["30","4.41","2","2","0"], + ["31","3.85","2","2","0"], + ["32","3.29","2","2","0"], + ["33","2.82","2","2","0"], + ["34","2.36","2","2","0"], + ["35","1.93","2","2","0"], + ["36","1.56","2","2","0"], + ["37","1.21","2","2","0"], + ["38","0.89","2","2","0"], + ["39","0.61","2","2","0"], + ["40","0.34","2","2","0"], + ["41","0.21","2","2","0"], + ["42","0.10","2","2","0"], + ["43","0.00","2","2","0"] + ]} +} diff --git a/tests/unit/data/fiz/README.md b/tests/unit/data/fiz/README.md new file mode 100644 index 000000000..514fb6287 --- /dev/null +++ b/tests/unit/data/fiz/README.md @@ -0,0 +1,66 @@ +# Real vehicle definitions + +The game's own vehicles (`dynamic/` of the MaSzyna release used for the tests, 2026-10), to test the simulation on the vehicles players drive. Chosen to cover every engine type, brake valve, brake handle, local brake, coupler type, door type and brake delay set the game data uses. `pkp/` is left out of the paths, the other operators keep theirs. + +They aren't the game's files: each `.json` here holds what the game's vehicle loader reads of the `.fiz`, with the files it includes, in the form of the tests' own definitions (`vehicle.h`): the values of every section line, and the rows of the lists. `test_fiz.cpp` converts them, using the game's parser for the includes, and checks the loader reads the same of the converted definition as of the game's file (`fiz.h`): the same sections with the same values, the same rows read by the same lists, in the same order. Comments, and lines the loader skips, are left out. + +Convert them again, from the game data in `/dynamic`, and add vehicles (paths as in the table, without the extension, separated by commas), with: + + EU07_UNIT_TESTS_FIZ_SOURCE=/dynamic EU07_UNIT_TESTS_FIZ_ADD=sm42_v1/6d1 eu07_unit_tests -tc="fiz: conversion*" + +Every `.json` here is a vehicle picked up by `test_fiz.cpp`: record the parameters of an added one with `EU07_UNIT_TESTS_UPDATE_GOLDEN=1`. + +| Path | Vehicle | Engine | Brake valve | Brake handle | Local brake | Coupler | +|---|---|---|---|---|---|---| +| `11xa_v2/93a.fiz` | 11XA | - | ESt4 | - | ManualBrake | Screw | +| `14xa_v1/173a.fiz` | 14XA | - | ESt4HBG300-s216 | - | ManualBrake | Screw | +| `168a_v1/168a.fiz` | 168A | - | ESt4HBG300 | - | ManualBrake | Screw | +| `21xm_v1/218ma_a_kd.fiz` | 218M | DieselEngine | EStED | MHZ_K5P | PneumaticBrake | Screw | +| `21xm_v1/214ma.fiz` | 214M | DieselEngine | EStED | MHZ_K5P | PneumaticBrake | Screw | +| `3wl_v2/3wl.fiz` | 3WL | - | ESt3 | - | ManualBrake | Screw | +| `430w_v2/430w.fiz` | 430W | - | ESt3AL2HBG300 | - | ManualBrake | Screw | +| `4e_v1/4e.fiz` | 4E | ElectricSeriesMotor | LSt | FV4a | PneumaticBrake | Screw | +| `bdhpumn_v3/bdhpumn.fiz` | BDHPUMN | - | KE | - | ManualBrake | Screw | +| `bhp_v1/bhp-b.fiz` | BHP | - | ESt3AL2 | - | ManualBrake | Articulated | +| `br285_v1/br285.fiz` | BR285 | DieselElectric | KE | MHZ_6P | PneumaticBrake | Screw | +| `cd/810_v2/810.fiz` | 810 | DieselEngine | ESt3_PZZ | FV4a | PneumaticBrake | Screw | +| `cd/754_v1/754.fiz` | 754 | DumbDE | LSt | FV4a | PneumaticBrake | Screw | +| `dr2_v1/dr2.fiz` | DR2 | WheelsDriven | - | - | ManualBrake | None | +| `e186_v2/eu47.fiz` | E186 | ElectricInductionMotor | EStED | MHZ_6P | PneumaticBrake | Screw | +| `e6act_v2/e6acta.fiz` | E6ACT | ElectricInductionMotor | EStED | MHZ_K5P | PneumaticBrake | Screw | +| `ed72_v1/5bs-rb.fiz` | ED72 | - | EP2 | FVel6 | ManualBrake | Automatic | +| `elf_v1/22wec-a.fiz` | ELF 22WEc | ElectricInductionMotor | EStED | MHZ_K5P | PneumaticBrake | Automatic | +| `elf_v1/21we-a.fiz` | ELF | ElectricInductionMotor | EStED | MHZ_K5P | PneumaticBrake | Automatic | +| `en57_v1/5bgb.fiz` | EN57 | - | EP2 | FVel6 | ManualBrake | Automatic | +| `en57_v1/6ba_840.fiz` | EN57 | - | EP2 | FVel6 | ManualBrake | Automatic | +| `en57_v1/6bai.fiz` | EN57 | - | K | St113 | ManualBrake | Automatic | +| `en57akl_v1/akl_ra.fiz` | EN57AKL | ElectricInductionMotor | EStED | MHZ_EN57 | PneumaticBrake | Automatic | +| `ep05_v2/ep05.fiz` | EP05 | ElectricSeriesMotor | LSt | FV4a | PneumaticBrake | Screw | +| `ep09_v2/104e.fiz` | EP09 | ElectricSeriesMotor | EStED | FV4a | PneumaticBrake | Screw | +| `et22_v3/201e-rn.fiz` | ET22 | ElectricSeriesMotor | LSt | FV4a | PneumaticBrake | Screw | +| `et22_v3/201e-w.fiz` | ET22 | ElectricSeriesMotor | LSt | FV4a | PneumaticBrake | Screw | +| `et42_v2/112e-a.fiz` | ET42 | ElectricSeriesMotor | LSt | FV4a | PneumaticBrake | Screw | +| `et42_v2/112e-b.fiz` | ET42 | ElectricSeriesMotor | LSt | FV4a | PneumaticBrake | Screw | +| `eu04_v1/eu04-01.fiz` | EU04 | ElectricSeriesMotor | K | Knorr | PneumaticBrake | Screw | +| `ew58_v1/ew58-d.fiz` | EW58 | - | EP1 | - | ManualBrake | Automatic | +| `ew58_v1/ew58-sa.fiz` | EW58 | ElectricSeriesMotor | EP1 | FVE408 | ManualBrake | Automatic | +| `impuls_v1/31we-a_pr.fiz` | IMPULS | ElectricInductionMotor | EStED | MHZ_6P | PneumaticBrake | Automatic | +| `road/man/nl293.fiz` | MAN (road) | DieselEngine | - | - | HydraulicBrake | Bare | +| `sa134_v1/218mc_a.fiz` | SA134 | DieselEngine | EStED | MHZ_K5P | PneumaticBrake | Screw | +| `sieciowiec_v1/a.fiz` | SIECIOWIEC | - | - | - | NoBrake | Screw | +| `sm03_v1/sm03.fiz` | SM03 | DieselEngine | K | Knorr | PneumaticBrake | Screw | +| `sm31_v1/411d.fiz` | SM31 | DumbDE | LSt | FV4a | PneumaticBrake | Screw | +| `sm40_v1/sm40.fiz` | SM40 | DieselElectric | K | Knorr | PneumaticBrake | Screw | +| `sm42_v1/6d1.fiz` | SM42 | DumbDE | LSt | FV4a | PneumaticBrake | Screw | +| `sn61_v2/sn61.fiz` | SN61 | DieselEngine | ESt3d_PZZ | FV4a | PneumaticBrake | Screw | +| `st44_v2/m62-1151.fiz` | ST44 | DieselElectric | LSt | FV4a | PneumaticBrake | Screw | +| `st45_v2/301dd.fiz` | ST45 | DumbDE | SW | MHZ_K8P | PneumaticBrake | Screw | +| `static/sug/sug_kielce.fiz` | SUG | ElectricSeriesMotor | W | Knorr | PneumaticBrake | Screw | +| `su45_v2/301d.fiz` | SU45 | DumbDE | LSt | FV4a | PneumaticBrake | Screw | +| `su46_v2/303d.fiz` | SU46 | DumbDE | LSt | FV4a | PneumaticBrake | Screw | +| `tem2_v2/tem2a.fiz` | TEM2 | DieselElectric | LSt | M394 | PneumaticBrake | Screw | +| `wm15_v1/wm15.fiz` | WM15 | Dumb | LSt | FV4a | PneumaticBrake | Screw | +| `wmb10_v1/wmb10.fiz` | WMB10 | DieselEngine | LSt | FV4a | PneumaticBrake | Screw | +| `z1_v2/152a.fiz` | Z1 | - | KE | - | ManualBrake | Screw | + +The paths are those of the game's `.fiz` files. `elf_v1/inverter.fiz` is a part the Elf units include, not a vehicle. The 218M (V2NList), the SA134 (HTCList) and the 22WEc (ffBrakeList) are there for lists the others lack, the EP05 for its camshaft. The 214MA stands for the 214M railbus, whose own file repeats a section (TurboPos and MotorParamTable twice), which the definitions here can't hold. diff --git a/tests/unit/data/fiz/bdhpumn_v3/bdhpumn.json b/tests/unit/data/fiz/bdhpumn_v3/bdhpumn.json new file mode 100644 index 000000000..80b1ab662 --- /dev/null +++ b/tests/unit/data/fiz/bdhpumn_v3/bdhpumn.json @@ -0,0 +1,14 @@ +{ + "_source": "pkp/bdhpumn_v3/bdhpumn.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "42200", "Vmax": "120", "HeatingP": "40", "LightP": "0.56"}, + "Load": {"MaxLoad": "132", "LoadQ": "pieces", "LoadAccepted": "Passengers", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "26.8", "H": "4.7", "W": "2.88", "Cx": "0.15"}, + "Wheels": {"D": "0.96", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.5", "Bd": "19.5", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "KE", "NBpA": "6", "MBF": "15", "MaxBP": "3.8", "BCN": "12", "BCR": "0.127", "BCD": "0.04", "BCM": "1.5", "BCS": "1.2", "BSA": "0", "BM": "Disk1", "BVV": "125", "TareMaxBP": "2.3"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "Mixed", "OpenSpeed": "0.5", "CloseSpeed": "1.0", "DoorCloseDelay": "0", "DoorMaxShiftL": "0.65", "DoorMaxShiftR": "0.65", "DoorOpenMethod": "Shift", "DoorMaxShiftPlug": "0.028", "DoorStayOpen": "5.0", "DoorClosureWarning": "no", "DoorBlocked": "Yes", "DoorAutoCloseRemote": "No", "DoorNeedPermit": "no"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.11", "FmaxB": "320", "kC": "4500", "DmaxC": "0.1", "FmaxC": "850", "beta": "0.58", "AllowedFlag": "115"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocalBrake": "ManualBrake", "BrakeDelays": "P", "MaxBPMass": "63", "rows": []}, + "Light": {"Light": "Generator", "LGeneratorEngine": "WheelsDriven", "LMaxVoltage": "24", "AlterLight": "Accu", "AlterLMaxVoltage": "24", "AlterLCap": "495", "AlterLRS": "Generator"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000", "AlterHeating": "PowerCable", "AlterHPowerTrans": "SteamPower", "AlterHSteamPress": "0.45"} +} diff --git a/tests/unit/data/fiz/bhp_v1/bhp-b.json b/tests/unit/data/fiz/bhp_v1/bhp-b.json new file mode 100644 index 000000000..031542506 --- /dev/null +++ b/tests/unit/data/fiz/bhp_v1/bhp-b.json @@ -0,0 +1,15 @@ +{ + "_source": "pkp/bhp_v1/bhp-b.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "36000", "Vmax": "120", "HeatingP": "37.5", "LightP": "0.56"}, + "Load": {"LoadAccepted": "Passengers,Luggage", "MaxLoad": "110", "LoadQ": "pieces", "OverLoadFactor": "2", "LoadSpeed": "3", "UnLoadSpeed": "4"}, + "Dimensions": {"L": "19.75", "H": "4.750", "W": "2.720", "Cx": "0.4"}, + "Wheels": {"D": "0.94", "AIM": "300", "Tw": "1.435", "Axle": "2'3'", "Ad": "3.0", "Bd": "17", "Rmin": "80", "BearingType": "Roll"}, + "BuffCoupl1": {"CType": "Articulated", "AllowedFlag": "83", "PowerFlag": "256"}, + "BuffCoupl2": {"CType": "Screw", "kB": "9200", "DmaxB": "0.11", "FmaxB": "320", "kC": "4500", "DmaxC": "0.1", "FmaxC": "850", "beta": "0.58", "AllowedFlag": "67"}, + "Brake": {"BrakeValve": "ESt3AL2", "Size": "100", "MBF": "144", "MaxBP": "3.8", "TareMaxBP": "2.5", "BCN": "2", "BCR": "0.1403", "BCD": "0.14", "BCM": "10.38", "NBpA": "4", "BM": "P10-Bgu", "BCS": "2.7", "BSA": "5.00", "BVV": "100", "BRE": "0.85"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocalBrake": "ManualBrake", "BrakeDelays": "P", "MaxBPMass": "55", "rows": []}, + "Doors": {"OpenCtrl": "Passenger", "CloseCtrl": "Mixed", "OpenSpeed": "0.55", "CloseSpeed": "0.75", "DoorMaxShiftL": "0.65", "DoorMaxShiftR": "0.65", "DoorOpenMethod": "Shift"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000", "AlterHeating": "PowerCable", "AlterHPowerTrans": "SteamPower", "AlterHSteamPress": "0.45"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "WheelsDriven", "LMaxVoltage": "24", "AlterLight": "Accu", "AlterLMaxVoltage": "24", "AlterLCap": "375", "AlterLRS": "Generator"} +} diff --git a/tests/unit/data/fiz/br285_v1/br285.json b/tests/unit/data/fiz/br285_v1/br285.json new file mode 100644 index 000000000..ec7dcbf4d --- /dev/null +++ b/tests/unit/data/fiz/br285_v1/br285.json @@ -0,0 +1,69 @@ +{ + "_source": "pkp/br285_v1/br285.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "82400", "Mred": "12000", "Vmax": "140", "PWR": "2200", "SandCap": "700"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "18.900", "H": "3.95", "W": "2.977", "Cx": "0.1"}, + "Wheels": {"D": "1.250", "AIM": "4000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "1.75", "Bd": "10.3", "Rmin": "80"}, + "SpringBrake": {"Volume": "10", "MBF": "8", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "3.5", "ValveOnArea": "0.0002", "ValveOffArea": "0.0002", "ValvePNBArea": "0.002", "MTC": "4"}, + "BuffCoupl": {"ControlType": "br285", "CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "103"}, + "Brake": {"BrakeValve": "KE", "NBpA": "4", "MBF": "50", "MaxBP": "3.8", "BCN": "8", "RM": "1.4", "BCR": "0.127", "BCD": "0.04", "BCM": "2.7", "BCS": "0.8", "BSA": "0", "BM": "Disk2", "BVV": "125", "Vv": "0.8", "CompressorSpeed": "0.04", "CompressorPower": "Converter", "MinCP": "8", "MaxCP": "10", "UBB3": "8", "LPOn": "3.0", "LPOff": "4.5", "HandlePipeUnlockPos": "2", "EmergencyCutsOffHandle": "Yes"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "3", "BDelay3": "35", "BDelay4": "36", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "6", "SCPN": "4", "AutoRelay": "Yes", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GPR", "BrakeHandle": "MHZ_6P", "LocBrakeHandle": "FD1", "ConverterStart": "Manual", "ConverterStartDelay": "3", "FuelStart": "Automatic", "OilStart": "Automatic", "BrakeOpModes": "PN", "EIMCtrlType": "3", "DirChangeMaxPos": "3", "HAO": "No", "HMO": "Yes", "HGDP1": "2", "rows": [ + ["-1","0.8","-1","20","Pneumatic"], + ["0","0.5","-1","2","Pneumatic"], + ["1","0.46","-1","2","Pneumatic"], + ["2","0.42","-1","2","Pneumatic"], + ["3","0.38","-1","2","Pneumatic"], + ["4","0.34","-1","2","Pneumatic"], + ["5","0.29","-1","2","Pneumatic"], + ["6","0","-1","40","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal,SeparateAcknowledge", "AwareDelay": "30", "SoundSignalDelay": "2.5", "MaxHoldTime": "1.0", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "100"}, + "Clima": {"HMaxVoltage": "3000", "Heating": "Generator", "HGeneratorEngine": "Main", "HGeneratorMinRPM": "600", "HGeneratorMinVoltage": "2400", "HGeneratorMaxRPM": "1860", "HGeneratorMaxVoltage": "3300"}, + "SpeedControl": {"SpeedCtrl": "Yes", "OverrideManual": "No", "InitPwr": "1.0", "MaxPwrVel": "-1", "StartVel": "-1", "VelStep": "5", "PwrStep": "0.01", "MinPwr": "0.0", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "140", "Offset": "0", "kPpos": "0.1", "kPneg": "0.2", "kIpos": "0.03", "kIneg": "0.03", "BrakeIntervention": "No", "SpeedCtrlType": "Time", "SpeedCtrlDelay": "0.5"}, + "Engine": {"EngineType": "DieselElectric", "Trans": "18:64", "Ftmax": "300000", "Vhyp": "24", "Vadd": "5.5", "Cr": "1", "WaterMinTemperature": "40", "WaterFlowTemperature": "70", "WaterCoolingTemperature": "82", "WaterMaxTemperature": "91", "WaterShutters": "Yes", "WaterAuxCircuit": "Yes", "WaterAuxCoolingTemperature": "50", "WaterAuxMaxTemperature": "74", "WaterAuxShutters": "Yes", "OilMinPressure": "0.15", "HeatKFO2": "33"}, + "MotorParamTable": {"rows": [ + ["0","8.35","50","2000","48.2","0","0"] + ]}, + "Switches": {"Converter": "impulse"}, + "WWList": {"Size": "6", "rows": [ + ["600","0","0","0"], + ["800","600","1404","6000"], + ["950","900","1404","6000"], + ["1050","1100","1404","6000"], + ["1250","1500","1404","6000"], + ["1450","1850","1404","6000"], + ["1860","2188","1404","6000"] + ]}, + "LightsList": {"Size": "12", "Wrap": "Yes", "Default": "1", "rows": [ + ["21","34"], + ["34","0"], + ["37","34"], + ["21","34"], + ["38","34"], + ["22","34"], + ["17","17"], + ["17","17"], + ["42","42"], + ["4","4"], + ["17","17"], + ["16","1"] + ]}, + "WiperList": {"Size": "4", "Angle": "70", "rows": [ + ["0","0.8","0.0","0.5"], + ["3","0.8","5.0","0.5"], + ["3","0.8","0.5","0.0"], + ["3","0.6","0.5","0.0"], + ["endL"] + ]}, + "UCList": {"Size": "6", "IntegratedBrakePN": "no", "IntegratedLocBrake": "no", "IntegratedBrake": "no", "rows": [ + ["0","0","-1","0","-1","0.15","0.15","0","1","1"], + ["1","0","-1","0","0","0.00","0.00","1","3","1"], + ["2","0","-1","0","0","0.15","0.00","1","3","1"], + ["3","0","0","0","0","0.00","0.00","1","5","3"], + ["4","0","0","1","0","0.00","0.15","3","5","5"], + ["5","0","0","1","0","0.00","0.00","3","6","5"], + ["6","0","0","1","1","0.15","0.15","5","6","5"] + ]} +} diff --git a/tests/unit/data/fiz/cd/754_v1/754.json b/tests/unit/data/fiz/cd/754_v1/754.json new file mode 100644 index 000000000..4f0a850a9 --- /dev/null +++ b/tests/unit/data/fiz/cd/754_v1/754.json @@ -0,0 +1,56 @@ +{ + "_source": "cd/754_v1/754.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "74500", "Mred": "15000", "Vmax": "100", "PWR": "1460", "SandCap": "400", "HeatingP": "300"}, + "Dimensions": {"L": "16.54", "H": "4.335", "W": "2.900", "Cx": "-0.1"}, + "Wheels": {"D": "1.000", "AIM": "4000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "2.4", "Bd": "9.0", "Rmin": "100"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.1", "LocMaxBP": "4.5", "MaxASBP": "1.5", "RM": "0.69", "BCN": "8", "BCR": "0.114", "BCD": "0.06", "BCM": "8.1", "HiPP": "5", "LoPP": "3.5", "Vv": "0.8", "MinCP": "7.0", "MaxCP": "8.5", "CompressorSpeed": "0.069", "BCS": "1", "BSA": "2", "BM": "P10-Bgu", "CompressorPower": "Converter", "BVV": "240", "BRE": "0.9"}, + "BuffCoupl": {"ControlType": "cd754", "CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "103"}, + "Cntrl": {"BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "6", "BDelay3": "36", "BDelay4": "32", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "14", "SCPN": "3", "AutoRelay": "Yes", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GPR", "rows": [ + ["-1","0.7","-1","15","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.29","-1","9","Pneumatic"], + ["6","0","-1","15","Pneumatic"] + ]}, + "LightsList": {"Size": "8", "Wrap": "No", "Default": "1", "rows": [ + ["0","0"], + ["388","34"], + ["21","34"], + ["1","16"], + ["16","1"], + ["22","34"], + ["38","38"], + ["34","34"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "227.8"}, + "Engine": {"EngineType": "DumbDE", "Trans": "18:62", "Ftmax": "215000", "Flat": "0", "Vhyp": "7.2", "Vadd": "5.5", "Cr": "1.0", "RelayType": "0"}, + "Clima": {"Heating": "Main"}, + "MotorParamTable": {"rows": [ + ["0","20","100","3000","20.8","1340","1713"], + ["1","18.5","110","2700","25","1340","1713"], + ["2","17.2","135","2500","30","1340","1713"], + ["3","14.3","170","2000","48.5","1340","1713"] + ]}, + "WWList": {"Size": "14", "rows": [ + ["500","0","0","0"], + ["500","0","0","0"], + ["550","122","78","273"], + ["550","122","78","273"], + ["600","244","147","547"], + ["650","365","221","820"], + ["700","287","295","1094"], + ["750","609","369","1367"], + ["800","730","442","1641"], + ["850","852","516","1914"], + ["900","973","590","2187"], + ["950","1095","664","2461"], + ["1000","1216","737","2734"], + ["1050","1338","811","3007"], + ["1100","1460","885","3281"] + ]} +} diff --git a/tests/unit/data/fiz/cd/810_v2/810.json b/tests/unit/data/fiz/cd/810_v2/810.json new file mode 100644 index 000000000..89a3c945e --- /dev/null +++ b/tests/unit/data/fiz/cd/810_v2/810.json @@ -0,0 +1,41 @@ +{ + "_source": "cd/810_v2/810.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "Type": "dmu", "M": "20000", "Vmax": "80", "PWR": "155", "SandCap": "25"}, + "Load": {"MaxLoad": "55", "OverLoadFactor": "2.2", "LoadQ": "pieces", "LoadAccepted": "passengers", "LoadSpeed": "0.5", "UnLoadSpeed": "0.7"}, + "Dimensions": {"L": "13.86", "H": "2.8", "W": "3.00", "Cx": "0.38"}, + "Wheels": {"D": "0.84", "Tw": "1.435", "AIM": "300", "Axle": "1'A'", "Bd": "8", "Rmin": "100", "BearingType": "Roll"}, + "Brake": {"BrakeType": "Oerlikon", "BrakeValve": "ESt3_PZZ", "Size": "100", "NBpA": "4", "MBF": "45.0", "MaxBP": "3.8", "BCN": "2", "BCR": "0.127", "BCD": "0.14", "BCM": "3.25", "HiPP": "5.0", "LoPP": "3.5", "Vv": "0.257", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.0136", "BM": "P10-Bg", "BVV": "75", "BRE": "0.87", "AirLeakRate": "1.15", "CompressorPower": "Converter"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "3"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.35", "CloseSpeed": "0.625", "DoorMaxShiftL": "0.9", "DoorMaxShiftR": "0.9", "DoorOpenMethod": "Plug", "DoorClosureWarning": "No", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.02", "DoorNeedPermit": "Yes", "DoorPermitList": "1|0|2", "DoorPermitListDefault": "2", "DoorAutoCloseRemote": "No"}, + "Cntrl": {"BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "8", "SCPN": "2", "AutoRelay": "Yes", "IniCDelay": "0.5", "SCDelay": "0.3", "ManualBrake": "Yes", "WaterStart": "Battery", "OilStart": "Automatic", "FuelStart": "Automatic", "ConverterStart": "Automatic", "ConverterStartDelay": "4", "CompartmentLightsStart": "Manual", "rows": [ + ["-1","1.0","-1","12","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.43","-1","9","Pneumatic"], + ["2","0.4","-1","9","Pneumatic"], + ["3","0.37","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.3","-1","9","Pneumatic"], + ["6","0","-1","16","Pneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"HMaxVoltage": "3000", "Heating": "Generator", "HGeneratorEngine": "Main", "HGeneratorMinRPM": "600", "HGeneratorMinVoltage": "2400", "HGeneratorMaxRPM": "2150", "HGeneratorMaxVoltage": "3300"}, + "Security": {"AwareSystem": "Active", "AwareDelay": "30", "SoundSignalDelay": "0.1", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "5", "RadioStop": "No"}, + "Engine": {"EngineType": "DieselEngine", "Trans": "13:51", "nmin": "600", "nmax": "2150", "nmax_cutoff": "2500", "AIM": "60", "EUS": "2.5", "EDS": "2.5", "IsTC": "Yes", "TC_TMMax": "2.70", "TC_CP": "0.85", "TC_LT": "850", "TC_LR": "1", "TC_ULR": "1", "TC_FRI": "1", "TC_FRD": "1", "TC_TII": "0.60", "TC_TIO": "0", "TC_TOO": "0", "TC_LS": "55", "TC_ULS": "45", "IsRetarder": "No", "WaterMaxTemperature": "105", "WaterCoolingTemperature": "90", "WaterFlowTemperature": "75", "WaterAuxCircuit": "No", "WaterCoolingFanSpeed": "-100", "NominalCoolingPower": "154"}, + "MotorParamTable": {"minVelfullengage": "-0.1", "engageDia": "1", "engageMaxForce": "1900", "engagefriction": "1", "rows": [ + ["0","0","0","-1","1"], + ["1","2.120","-1","33","1"], + ["2","1.000","25","200","1"] + ]}, + "DList": {"Size": "8", "Mmax": "850", "nMmax": "29", "nmax": "41", "Mnmax": "750", "nominalfill": "0.9", "Mstand": "100.0", "rows": [ + ["0","0.1","0"], + ["1","0.2","1"], + ["2","0.3","2"], + ["3","0.4","2"], + ["4","0.5","2"], + ["5","0.6","2"], + ["6","0.7","2"], + ["7","0.8","2"], + ["8","1.0","2"] + ]} +} diff --git a/tests/unit/data/fiz/dr2_v1/dr2.json b/tests/unit/data/fiz/dr2_v1/dr2.json new file mode 100644 index 000000000..f80e6ef75 --- /dev/null +++ b/tests/unit/data/fiz/dr2_v1/dr2.json @@ -0,0 +1,13 @@ +{ + "_source": "pkp/dr2_v1/dr2.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "300", "Vmax": "40", "PWR": "6"}, + "Dimensions": {"L": "1.65", "H": "0.79", "W": "1.47", "Cx": "0.058"}, + "Load": {"MaxLoad": "3", "LoadQ": "pieces", "LoadAccepted": "Passengers", "LoadSpeed": "0.5", "UnLoadSpeed": "0.5"}, + "Wheels": {"D": "0.4", "AIM": "4.8", "Tw": "1.435", "Axle": "1'A", "Bd": "0.86", "Rmin": "20", "BearingType": "Slide"}, + "BuffCoupl": {"CType": "None"}, + "Brake": {"BrakeType": "Individual", "MBF": "0.2"}, + "Cntrl": {"BrakeSystem": "Individual", "LocalBrake": "ManualBrake", "MCPN": "1", "SCDelay": "0.2", "rows": []}, + "Power": {"EnginePower": "Internal", "PowerType": "BioPower"}, + "Engine": {"EngineType": "WheelsDriven", "Trans": "9:2", "Ftmax": "1200"} +} diff --git a/tests/unit/data/fiz/e186_v2/eu47.json b/tests/unit/data/fiz/e186_v2/eu47.json new file mode 100644 index 000000000..c20e0c823 --- /dev/null +++ b/tests/unit/data/fiz/e186_v2/eu47.json @@ -0,0 +1,61 @@ +{ + "_source": "pkp/e186_v2/eu47.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "81000", "Mred": "9000", "Vmax": "160", "PWR": "5600", "SandCap": "760"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "18.900", "H": "3.95", "W": "2.977", "Cx": "0.1"}, + "Wheels": {"D": "1.250", "AIM": "4000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "1.75", "Bd": "10.3", "Rmin": "100"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "4", "MBF": "50", "MaxBP": "3.8", "BCN": "8", "RM": "1.4", "BCR": "0.127", "BCD": "0.04", "BCM": "2.7", "BCS": "0.8", "BSA": "0", "BM": "Disk2", "BVV": "125", "Vv": "0.8", "CompressorSpeed": "0.04", "CompressorPower": "Converter", "MinCP": "8", "MaxCP": "10", "UBB3": "8", "LPOn": "3.0", "LPOff": "4.5", "HandlePipeUnlockPos": "2", "EmergencyCutsOffHandle": "Yes"}, + "SpringBrake": {"Volume": "10", "MBF": "8", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "3.5", "ValveOnArea": "0.0002", "ValveOffArea": "0.0002", "ValvePNBArea": "0.002", "MTC": "4"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "103", "ControlType": "EU47"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BrakeOpModes": "PN", "BCPN": "4", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Automatic", "LocalBrake": "PneumaticBrake", "MCPN": "7", "SCPN": "4", "AutoRelay": "No", "MainInitTime": "10", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GPR", "BrakeHandle": "MHZ_6P", "LocBrakeHandle": "FD1", "ScndS": "Yes", "SBD": "0", "ConverterStart": "Automatic", "ConverterStartDelay": "10", "PantCompressorStart": "Automatic", "PantAutoValve": "Yes", "EIMCtrlType": "1", "HAO": "No", "HMO": "No", "HGDP1": "0.0", "HGDP2": "1", "OMP": "0.4", "DirChangeMaxPos": "3", "LocalBrakeTraxx": "Yes", "PantEPValveStart": "Manual", "PantEPValveSpring": "No", "rows": [ + ["-1","0.8","-1","20","Pneumatic"], + ["0","0.5","-1","2","Pneumatic"], + ["1","0.46","-1","2","Pneumatic"], + ["2","0.42","-1","2","Pneumatic"], + ["3","0.38","-1","2","Pneumatic"], + ["4","0.34","-1","2","Pneumatic"], + ["5","0.29","-1","2","Pneumatic"], + ["6","0","-1","40","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal,SeparateAcknowledge", "AwareDelay": "30", "SoundSignalDelay": "2.5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "5", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "4", "MaxVoltage": "3900", "MaxCurrent": "2000", "MinH": "0.48", "MinV": "1900", "MaxH": "2.5", "CSW": "1.5", "PhysicalLayout": "3"}, + "Engine": {"EngineType": "ElectricInductionMotor", "Trans": "48:251", "dfic": "861", "dfmax": "1.84", "p": "2", "cfu": "43.7", "cim": "13.4", "icif": "0.679", "Uzmax": "2183", "Uzh": "2183", "DU": "20", "I0": "20", "fcfu": "43.7", "F0": "300", "a1": "0.4", "Pmax": "5600", "Fh": "150", "Ph": "2600", "Vh0": "5", "Vh1": "10", "Imax": "1950", "abed": "1", "Flat": "Yes"}, + "Switches": {"Pantograph": "Impulse"}, + "SpeedControl": {"SpeedCtrl": "Yes", "OverrideManual": "No", "InitPwr": "1.0", "MaxPwrVel": "-1", "StartVel": "-1", "VelStep": "5", "PwrStep": "0.01", "MinPwr": "0.0", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "160", "Offset": "0", "kPpos": "0.1", "kPneg": "0.2", "kIpos": "0.03", "kIneg": "0.03", "BrakeIntervention": "No", "SpeedCtrlType": "Time", "SpeedCtrlDelay": "0.5"}, + "Doors": {"DoorPermitList": "0|3", "DoorPermitListDefault": "1"}, + "Circuit": {"ImaxHi": "500"}, + "ffList": {"Size": "7", "rows": [ + ["0","0.4"], + ["6.3","0.4"], + ["7.1","0.6"], + ["25.6","0.6"], + ["26.4","0.7"], + ["29.6","0.7"], + ["30.4","1.0"], + ["60.8","2.0"] + ]}, + "LightsList": {"Size": "12", "Wrap": "Yes", "Default": "1", "rows": [ + ["21","34"], + ["34","0"], + ["37","34"], + ["21","34"], + ["38","34"], + ["22","34"], + ["17","17"], + ["17","17"], + ["42","42"], + ["4","4"], + ["17","17"], + ["16","1"] + ]}, + "WiperList": {"Size": "4", "Angle": "70", "rows": [ + ["0","0.8","0.0","0.5"], + ["3","0.8","5.0","0.5"], + ["3","0.8","0.5","0.0"], + ["3","0.6","0.5","0.0"], + ["endL"] + ]} +} diff --git a/tests/unit/data/fiz/e6act_v2/e6acta.json b/tests/unit/data/fiz/e6act_v2/e6acta.json new file mode 100644 index 000000000..1904bbcf6 --- /dev/null +++ b/tests/unit/data/fiz/e6act_v2/e6acta.json @@ -0,0 +1,41 @@ +{ + "_source": "pkp/e6act_v2/e6acta.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "115000", "Mred": "9000", "Vmax": "120", "PWR": "5004", "SandCap": "700"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "20.33", "H": "4.325", "W": "3.0", "Cx": "0.1"}, + "Wheels": {"D": "1.250", "AIM": "4000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "3.9", "Bd": "14.2", "Rmin": "150", "BearingType": "Roll"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "35"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "4", "MBF": "50", "MaxBP": "4.4", "BCN": "12", "MaxLBP": "4.4", "RM": "1.4", "BCR": "0.127", "BCD": "0.04", "BCM": "2.7", "BCS": "0.8", "BSA": "0", "BRE": "0.97", "BM": "P10-Bgu", "BVV": "100", "Vv": "0.8", "CompressorSpeed": "0.04", "MinCP": "8", "MaxCP": "10", "DynamicBrake": "Automatic", "LPOn": "3.0", "LPOff": "4.5", "UBB1": "8", "UBB2": "1", "HandlePipeUnlockPos": "0", "EmergencyCutsOffHandle": "Yes"}, + "SpringBrake": {"Volume": "10", "MBF": "2.9", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "4.2", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Automatic", "LocalBrake": "PneumaticBrake", "MCPN": "10", "SCPN": "12", "AutoRelay": "No", "MainInitTime": "5", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GP", "BrakeHandle": "MHZ_K5P", "LocBrakeHandle": "FD1", "ConverterStart": "Automatic", "ConverterStartDelay": "10", "PantCompressorStart": "Mixed", "PantAutoValve": "Yes", "LocalBrakeTraxx": "Yes", "HAO": "No", "HMO": "No", "HGDP1": "0.0", "HGDP2": "1", "BrakeOpModes": "PN", "AutomaticCabActivation": "No", "InactivaCabEmergencyBrake": "Yes", "InactiveCabFlag": "15", "rows": [ + ["-1","0.8","-1","20","Pneumatic"], + ["0","0.5","-1","2","Pneumatic"], + ["1","0.46","-1","2","Pneumatic"], + ["2","0.42","-1","2","Pneumatic"], + ["3","0.38","-1","2","Pneumatic"], + ["4","0.34","-1","2","Pneumatic"], + ["5","0.29","-1","2","Pneumatic"], + ["6","0","-1","40","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "2.1", "CabDependent": "Yes", "RadioStop": "Yes"}, + "Circuit": {"ImaxHi": "500"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "4600", "MaxCurrent": "1600", "MinH": "0.8", "MaxH": "1.8", "CSW": "1.5", "MinV": "2000", "InsetV": "2000", "PhysicalLayout": "3"}, + "Engine": {"EngineType": "ElectricInductionMotor", "Trans": "54:253", "dfic": "1021", "dfmax": "0.92", "p": "2", "cfu": "48.9", "cim": "15", "icif": "0.664", "Uzmax": "2150", "Uzh": "2150", "DU": "20", "I0": "20", "fcfu": "48.9", "F0": "420", "a1": "1.53", "Pmax": "5100", "Fh": "200", "Ph": "3000", "Vh0": "5", "Vh1": "10", "Imax": "1870", "abed": "1", "eped": "10"}, + "SpeedControl": {"SpeedCtrl": "Yes", "OverrideManual": "No", "InitPwr": "1.0", "MaxPwrVel": "-1", "StartVel": "-1", "VelStep": "10", "PwrStep": "0.01", "MinPwr": "0.0", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "120", "Offset": "-0.1", "kPpos": "0.2", "kPneg": "0.4", "kIpos": "0.02", "kIneg": "0.00", "BrakeIntervention": "No", "SpeedCtrlDelay": "0.5"}, + "ffList": {"Size": "2", "rows": [ + ["0","0.40"], + ["19.6","0.805"], + ["25.7","1.055"] + ]}, + "LightsList": {"Size": "7", "Wrap": "no", "Default": "4", "rows": [ + ["16","1"], + ["21","34"], + ["22","34"], + ["0","0"], + ["34","34"], + ["17","34"], + ["38","34"] + ]} +} diff --git a/tests/unit/data/fiz/ed72_v1/5bs-ra.json b/tests/unit/data/fiz/ed72_v1/5bs-ra.json new file mode 100644 index 000000000..0f11e0b42 --- /dev/null +++ b/tests/unit/data/fiz/ed72_v1/5bs-ra.json @@ -0,0 +1,25 @@ +{ + "_source": "pkp/ed72_v1/5bs-ra.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "35500", "Vmax": "110", "PWR": "0", "SandCap": "0", "Type": "ezt"}, + "Load": {"MaxLoad": "54", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "1"}, + "Dimensions": {"L": "20.87", "H": "4", "W": "2.9", "Cx": "0.2"}, + "Wheels": {"D": "0.94", "AIM": "2000", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EP2", "NBpA": "4", "MBF": "85.0", "MaxBP": "4", "BCN": "1", "BCR": "0.203", "BCD": "0.15", "BCM": "9.39", "HiPP": "5", "LoPP": "3.5", "Vv": "0.3", "MinCP": "5.8", "MaxCP": "7.2", "CompressorSpeed": "0.013", "TareMaxBP": "2.5", "BVV": "88", "BM": "P10-Bgu", "BCS": "1.5", "BSA": "5", "BRE": "0.9", "CompressorPower": "Coupler2"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.4", "CloseSpeed": "0.4", "DoorCloseDelay": "0.5", "DoorMaxShiftL": "0.625", "DoorMaxShiftR": "0.625", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes", "DoorBlocked": "Yes"}, + "BuffCoupl1": {"ControlType": "ed72", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39"}, + "BuffCoupl2": {"ControlType": "ed72", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-119"}, + "Cntrl": {"BrakeHandle": "FVel6", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "52", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "ManualBrake", "MCPN": "3", "SCPN": "3", "IniCDelay": "0.0", "SCDelay": "0.975", "CoupledCtrl": "Yes", "ScndS": "Yes", "BatteryStart": "Disabled", "ConverterStart": "Disabled", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes", "MagnetLocation": "5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Switches": {"Pantograph": "impulse", "Converter": "impulse"} +} diff --git a/tests/unit/data/fiz/ed72_v1/5bs-rb.json b/tests/unit/data/fiz/ed72_v1/5bs-rb.json new file mode 100644 index 000000000..ccb89b269 --- /dev/null +++ b/tests/unit/data/fiz/ed72_v1/5bs-rb.json @@ -0,0 +1,25 @@ +{ + "_source": "pkp/ed72_v1/5bs-rb.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "35500", "Vmax": "110", "PWR": "0", "SandCap": "0", "Type": "ezt"}, + "Load": {"MaxLoad": "54", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "1"}, + "Dimensions": {"L": "20.87", "H": "4", "W": "2.9", "Cx": "0.2"}, + "Wheels": {"D": "0.94", "AIM": "2000", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EP2", "NBpA": "4", "MBF": "85.0", "MaxBP": "4", "BCN": "1", "BCR": "0.203", "BCD": "0.15", "BCM": "9.39", "HiPP": "5", "LoPP": "3.5", "Vv": "0.3", "MinCP": "5.8", "MaxCP": "7.2", "CompressorSpeed": "0.013", "TareMaxBP": "2.5", "BVV": "88", "BM": "P10-Bgu", "BCS": "1.5", "BSA": "5", "BRE": "0.9", "CompressorPower": "Coupler1"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.315", "CloseSpeed": "0.325", "DoorMaxShiftL": "0.625", "DoorCloseDelay": "1.6", "DoorMaxShiftR": "0.625", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes", "DoorBlocked": "Yes"}, + "BuffCoupl1": {"ControlType": "ed72", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-119"}, + "BuffCoupl2": {"ControlType": "ed72", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39"}, + "Cntrl": {"BrakeHandle": "FVel6", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "52", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "ManualBrake", "MCPN": "3", "SCPN": "3", "IniCDelay": "0.0", "SCDelay": "0.975", "CoupledCtrl": "Yes", "ScndS": "Yes", "ConverterStart": "Disabled", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes", "MagnetLocation": "5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Switches": {"Pantograph": "impulse", "Converter": "impulse"} +} diff --git a/tests/unit/data/fiz/ed72_v1/6bs-sa.json b/tests/unit/data/fiz/ed72_v1/6bs-sa.json new file mode 100644 index 000000000..bd115fe6c --- /dev/null +++ b/tests/unit/data/fiz/ed72_v1/6bs-sa.json @@ -0,0 +1,47 @@ +{ + "_source": "pkp/ed72_v1/6bs-sa.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "55500", "Vmax": "110", "PWR": "608", "SandCap": "500", "Type": "ezt"}, + "Load": {"MaxLoad": "76", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.77", "H": "4.12", "W": "2.96", "Cx": "0.1"}, + "Wheels": {"D": "1.0", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "2.75", "Bd": "15.6", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EP2", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.1", "BCN": "2", "BCR": "0.178", "BCD": "0.15", "BCM": "7.32", "HiPP": "5", "LoPP": "3.5", "Vv": "0.1", "MinCP": "5.8", "MaxCP": "7.2", "CompressorSpeed": "0.0", "TareMaxBP": "2.8", "BVV": "100", "BM": "P10-Bgu", "BCS": "1.5", "BSA": "4", "BRE": "0.9"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.38", "CloseSpeed": "0.475", "DoorCloseDelay": "2.0", "DoorMaxShiftL": "0.625", "DoorMaxShiftR": "0.625", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes", "DoorBlocked": "Yes"}, + "BuffCoupl1": {"ControlType": "ed72", "CType": "Automatic", "kB": "19200", "DmaxB": "0.05", "FmaxB": "740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-119"}, + "BuffCoupl2": {"ControlType": "ed72", "CType": "Automatic", "kB": "19200", "DmaxB": "0.05", "FmaxB": "740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-127"}, + "Cntrl": {"BrakeSystem": "ElectroPneumatic", "MaxBPMass": "75", "BCPN": "1", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "DynamicBrake": "Passive", "MCPN": "3", "SCPN": "3", "AutoRelay": "Yes", "IniCDelay": "0.0", "SCDelay": "0.975", "CoupledCtrl": "Yes", "ScndS": "Yes", "BatteryStart": "Disabled", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","-1","-1","0","ElectroPneumatic"], + ["1","-1","0.35","4","ElectroPneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "1", "MaxVoltage": "3600", "MaxCurrent": "1200", "MinH": "0.38", "MinV": "1900", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "1"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "19:70", "WindingRes": "0.3836", "nmax": "2390"}, + "Switches": {"Pantograph": "Impulse"}, + "MotorParamTable0": {"rows": [ + ["0","21.52","81","-0.02","148","81","-0.02","1"], + ["1","21.68","120","-0.02","148.2","120","-0.02","1"], + ["2","21.17","130","0.01","142.3","130","0.01","1"], + ["3","22.46","70","0.24","150.1","70","0.24","0"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "280", "ImaxHi": "281", "IminLo": "135", "IminHi": "175"}, + "RList": {"Size": "14", "RVent": "No", "rows": [ + ["0","0","0","0","0"], + ["1","17.682","1","4","0"], + ["2","14.182","1","4","1"], + ["2","10.682","1","4","1"], + ["2","8.474","1","4","1"], + ["2","6.266","1","4","1"], + ["2","4.605","1","4","1"], + ["2","2.944","1","4","1"], + ["2","1.472","1","4","1"], + ["2","0","1","4","1"], + ["3","8.841","2","2","1"], + ["3","5.341","2","2","1"], + ["3","3.133","2","2","1"], + ["3","1.472","2","2","1"], + ["3","0","2","2","0"] + ]} +} diff --git a/tests/unit/data/fiz/ed72_v1/6bs-sb.json b/tests/unit/data/fiz/ed72_v1/6bs-sb.json new file mode 100644 index 000000000..2a8cb50c1 --- /dev/null +++ b/tests/unit/data/fiz/ed72_v1/6bs-sb.json @@ -0,0 +1,47 @@ +{ + "_source": "pkp/ed72_v1/6bs-sb.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "55500", "Vmax": "110", "PWR": "608", "SandCap": "500", "Type": "ezt"}, + "Load": {"MaxLoad": "76", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.77", "H": "4.12", "W": "2.96", "Cx": "0.1"}, + "Wheels": {"D": "1.0", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "2.75", "Bd": "15.6", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EP2", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.1", "BCN": "2", "BCR": "0.178", "BCD": "0.15", "BCM": "7.32", "HiPP": "5", "LoPP": "3.5", "Vv": "0.1", "MinCP": "5.8", "MaxCP": "7.2", "CompressorSpeed": "0.0", "TareMaxBP": "2.8", "BVV": "100", "BM": "P10-Bgu", "BCS": "1.5", "BSA": "4", "BRE": "0.9"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.335", "CloseSpeed": "0.34", "DoorCloseDelay": "1.5", "DoorMaxShiftL": "0.625", "DoorMaxShiftR": "0.625", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes", "DoorBlocked": "Yes"}, + "BuffCoupl1": {"ControlType": "ed72", "CType": "Automatic", "kB": "19200", "DmaxB": "0.05", "FmaxB": "740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-127"}, + "BuffCoupl2": {"ControlType": "ed72", "CType": "Automatic", "kB": "19200", "DmaxB": "0.05", "FmaxB": "740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-119"}, + "Cntrl": {"BrakeSystem": "ElectroPneumatic", "MaxBPMass": "75", "BCPN": "1", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "DynamicBrake": "Passive", "MCPN": "3", "SCPN": "3", "AutoRelay": "Yes", "IniCDelay": "0.0", "SCDelay": "0.975", "CoupledCtrl": "Yes", "ScndS": "Yes", "BatteryStart": "Disabled", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","-1","-1","0","ElectroPneumatic"], + ["1","-1","0.35","4","ElectroPneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "3600", "MaxCurrent": "1200", "MinH": "0.38", "MinV": "1900", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "2"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "19:70", "WindingRes": "0.3836", "nmax": "2390"}, + "Switches": {"Pantograph": "Impulse"}, + "MotorParamTable0": {"rows": [ + ["0","21.52","81","-0.02","148","81","-0.02","1"], + ["1","21.68","120","-0.02","148.2","120","-0.02","1"], + ["2","21.17","130","0.01","142.3","130","0.01","1"], + ["3","22.46","70","0.24","150.1","70","0.24","0"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "280", "ImaxHi": "281", "IminLo": "135", "IminHi": "175"}, + "RList": {"Size": "14", "RVent": "No", "rows": [ + ["0","0","0","0","0"], + ["1","17.682","1","4","0"], + ["2","14.182","1","4","1"], + ["2","10.682","1","4","1"], + ["2","8.474","1","4","1"], + ["2","6.266","1","4","1"], + ["2","4.605","1","4","1"], + ["2","2.944","1","4","1"], + ["2","1.472","1","4","1"], + ["2","0","1","4","1"], + ["3","8.841","2","2","1"], + ["3","5.341","2","2","1"], + ["3","3.133","2","2","1"], + ["3","1.472","2","2","1"], + ["3","0","2","2","0"] + ]} +} diff --git a/tests/unit/data/fiz/elf_v1/21we-a.json b/tests/unit/data/fiz/elf_v1/21we-a.json new file mode 100644 index 000000000..19b3938c6 --- /dev/null +++ b/tests/unit/data/fiz/elf_v1/21we-a.json @@ -0,0 +1,52 @@ +{ + "_source": "pkp/elf_v1/21we-a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "28650", "Vmax": "160", "PWR": "800", "SandCap": "500", "Type": "ezt", "HeatingP": "37"}, + "Load": {"MaxLoad": "108", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2.3", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.081", "H": "4.228", "W": "2.96", "Cx": "0.1"}, + "Wheels": {"D": "0.850", "AIM": "8000", "Tw": "1.435", "Axle": "Bo'", "Ad": "2.83", "Bd": "16.3", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.1", "MaxLBP": "4.2", "RM": "0.74", "RV": "-1", "BCN": "4", "BCR": "0.127", "BCD": "0.05", "BCM": "1.9075", "HiPP": "5", "LoPP": "3.5", "Vv": "0.5", "MinCP": "7.7", "MaxCP": "9.0", "CompressorSpeed": "0.025", "BVV": "150", "BM": "Cosid", "BCS": "1", "BSA": "0.00", "BRE": "0.95", "TareMaxBP": "2.6", "CompressorPower": "Converter", "LPOn": "3.0", "LPOff": "4.5", "UBB1": "2", "UBB2": "8"}, + "SpringBrake": {"Volume": "10", "MBF": "2.9", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "4.2", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.24", "CloseSpeed": "0.2", "DoorMaxShiftL": "0.74", "DoorMaxShiftR": "0.74", "DoorOpenMethod": "Plug", "DoorStayOpen": "5.0", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.05", "DoorCloseDelay": "1.8", "PlatformMaxShift": "0.270", "PlatformSpeed": "0.4", "PlatformOpenMethod": "Shift", "MirrorMaxShift": "45", "DoorNeedPermit": "Yes", "DoorPermitList": "1|0|2|3", "DoorPermitListDefault": "2", "DoorAutoCloseRemote": "Yes", "MirrorVelClose": "25"}, + "BuffCoupl1": {"CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39", "ControlType": "en62"}, + "BuffCoupl2": {"CType": "Articulated", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-255"}, + "Cntrl": {"BrakeHandle": "MHZ_K5P", "LocBrakeHandle": "FD1", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "37", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "LocalBrake": "PneumaticBrake", "MCPN": "4", "EIMCtrlType": "2", "SCPN": "1", "MainInitTime": "10", "IniCDelay": "1.0", "SCDelay": "0.2", "CoupledCtrl": "No", "SBD": "0.6", "ASB": "Yes", "CompressorStart": "Automatic", "ConverterStart": "Automatic", "ConverterStartDelay": "10.0", "PantCompressorStart": "Mixed", "PantAutoValve": "Yes", "BrakeDelays": "PR", "BrakeOpModes": "PN", "DirChangeMaxPos": "3", "HAO": "No", "HMO": "Yes", "HGDP1": "0", "ReleaseParkingBySpringBrake": "Yes", "MotorBlowersStart": "Converter", "CompartmentLightsStart": "Manual", "AutomaticCabActivation": "No", "InactivaCabEmergencyBrake": "Yes", "InactiveCabFlag": "3", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Circuit": {"ImaxHi": "500"}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "MaxHoldTime": "0.1", "EmergencyBrakeDelay": "2.1", "CabDependent": "Yes", "RadioStop": "Yes", "EmergencyBrakeWarningSignal": "2", "MagnetLocation": "4.5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main", "HMaxVoltage": "400"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "1", "MaxVoltage": "4000", "MinV": "2000", "MaxCurrent": "1200", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "1"}, + "Blending": {"MED_amax": "1.04", "MED_EPVC": "No", "MED_Ncor": "No", "MED_FirstDelayED": "2", "MED_ScndDelayED": "1"}, + "Engine": {"EngineType": "ElectricInductionMotor", "Trans": "40:218", "dfic": "470", "dfmax": "0.790", "p": "2", "cfu": "33.43", "cim": "10.22", "icif": "0.67", "Uzmax": "2340", "Uzh": "2340", "DU": "20", "I0": "20", "fcfu": "33.5", "F0": "75", "a1": "0.0", "Pmax": "980", "Fh": "48", "Ph": "830", "Vh0": "2", "Vh1": "5", "Imax": "377", "abed": "0", "Flat": "Yes", "edep": "1.0", "MotorBlowersSpeed": "-29", "InvNo": "2", "InvCtrCplFlag": "255"}, + "SpeedControl": {"SpeedCtrl": "Yes", "SpeedButtons": "0|40|50|60|70|80|100|120|140|160", "OverrideManual": "Yes", "InitPwr": "0.3", "MaxPwrVel": "5", "StartVel": "2", "VelStep": "5", "PwrStep": "0.1", "MinPwr": "0.3", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "160", "Offset": "-0.5", "kPpos": "0.1", "kPneg": "0.4", "kIpos": "0.06", "kIneg": "0.06", "BrakeIntervention": "No", "PowerUpSpeed": "0.3", "PowerDownSpeed": "0.3"}, + "AI": {"Pantstate": "1"}, + "ffList": {"Size": "10", "rows": [ + ["0","2.49"], + ["22.1","2.49"], + ["24.1","2.50"], + ["31.3","2.50"], + ["32.3","2.53"], + ["39.3","3.0"], + ["65","5.0"], + ["91","7.0"], + ["124","9.0"], + ["161","11.7"], + ["177","13.0"] + ]}, + "LightsList": {"Size": "5", "Wrap": "no", "Default": "1", "rows": [ + ["0","0"], + ["21","34"], + ["22","34"], + ["34","34"], + ["16","1"] + ]} +} diff --git a/tests/unit/data/fiz/elf_v1/21we-b.json b/tests/unit/data/fiz/elf_v1/21we-b.json new file mode 100644 index 000000000..ecd26847c --- /dev/null +++ b/tests/unit/data/fiz/elf_v1/21we-b.json @@ -0,0 +1,25 @@ +{ + "_source": "pkp/elf_v1/21we-b.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "50000", "Vmax": "160", "PWR": "0", "SandCap": "500", "Type": "ezt", "HeatingP": "37"}, + "Load": {"MaxLoad": "108", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2.3", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "16.5", "H": "4.12", "W": "2.96", "Cx": "0.1"}, + "Wheels": {"D": "0.850", "AIM": "670", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.83", "Bd": "16.3", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "2", "MBF": "85.0", "MaxBP": "3.1", "MaxLBP": "4.2", "RM": "0.74", "RV": "-1", "BCN": "8", "BCR": "0.127", "BCD": "0.05", "BCM": "1.9075", "HiPP": "5", "LoPP": "3.5", "Vv": "0.1", "MinCP": "7.7", "MaxCP": "9.0", "CompressorSpeed": "0.0000001", "BVV": "176", "BM": "Cosid", "BCS": "1", "BSA": "0.00", "BRE": "0.95", "TareMaxBP": "1.9"}, + "SpringBrake": {"Volume": "20", "MBF": "5.8", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "4.2", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.24", "CloseSpeed": "0.2", "DoorMaxShiftL": "0.74", "DoorMaxShiftR": "0.74", "DoorOpenMethod": "Plug", "DoorStayOpen": "5.0", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.05", "DoorCloseDelay": "1.8", "PlatformMaxShift": "0.270", "PlatformSpeed": "0.4", "PlatformOpenMethod": "Shift", "MirrorMaxShift": "45", "DoorNeedPermit": "Yes", "DoorPermitList": "1|0|2|3", "DoorPermitListDefault": "2", "DoorAutoCloseRemote": "Yes"}, + "BuffCoupl1": {"CType": "Articulated", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-255"}, + "BuffCoupl2": {"CType": "Articulated", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-255"}, + "Cntrl": {"BrakeHandle": "MHZ_K5P", "LocBrakeHandle": "FD1", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "74", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "LocalBrake": "PneumaticBrake", "MCPN": "4", "EIMCtrlType": "2", "SCPN": "1", "IniCDelay": "0.0", "SCDelay": "0.8", "CoupledCtrl": "No", "SBD": "0.6", "ASB": "Automatic", "CompressorStart": "Automatic", "ConverterStart": "Automatic", "ConverterStartDelay": "10.15", "BrakeDelays": "PR", "BrakeOpModes": "PN", "ReleaseParkingBySpringBrake": "Yes", "CompartmentLightsStart": "Manual", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "400"} +} diff --git a/tests/unit/data/fiz/elf_v1/21we-c.json b/tests/unit/data/fiz/elf_v1/21we-c.json new file mode 100644 index 000000000..10101e91a --- /dev/null +++ b/tests/unit/data/fiz/elf_v1/21we-c.json @@ -0,0 +1,52 @@ +{ + "_source": "pkp/elf_v1/21we-c.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "28650", "Vmax": "160", "PWR": "800", "SandCap": "500", "Type": "ezt", "HeatingP": "37"}, + "Load": {"MaxLoad": "95", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2.3", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "20.70", "H": "4.228", "W": "2.96", "Cx": "0.1"}, + "Wheels": {"D": "0.850", "AIM": "8000", "Tw": "1.435", "Axle": "Bo'", "Ad": "2.83", "Bd": "16.3", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.1", "MaxLBP": "4.2", "RM": "0.74", "RV": "-1", "BCN": "4", "BCR": "0.127", "BCD": "0.05", "BCM": "1.9075", "HiPP": "5", "LoPP": "3.5", "Vv": "0.5", "MinCP": "7.0", "MaxCP": "8.5", "CompressorSpeed": "0.025", "BVV": "150", "BM": "Cosid", "BCS": "1", "BSA": "0.00", "BRE": "0.95", "TareMaxBP": "2.6", "CompressorPower": "Converter", "LPOn": "3.0", "LPOff": "4.5", "UBB1": "2", "UBB2": "8"}, + "SpringBrake": {"Volume": "10", "MBF": "2.9", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "4.2", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.24", "CloseSpeed": "0.2", "DoorMaxShiftL": "0.74", "DoorMaxShiftR": "0.74", "DoorOpenMethod": "Plug", "DoorStayOpen": "5.0", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.05", "DoorCloseDelay": "1.8", "PlatformMaxShift": "0.270", "PlatformSpeed": "0.4", "PlatformOpenMethod": "Shift", "MirrorMaxShift": "45", "DoorNeedPermit": "Yes", "DoorPermitList": "1|0|2|3", "DoorPermitListDefault": "2", "DoorAutoCloseRemote": "Yes", "MirrorVelClose": "25"}, + "BuffCoupl1": {"CType": "Articulated", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-255"}, + "BuffCoupl2": {"CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39", "ControlType": "en62"}, + "Cntrl": {"BrakeHandle": "MHZ_K5P", "LocBrakeHandle": "FD1", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "37", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "LocalBrake": "PneumaticBrake", "MCPN": "4", "EIMCtrlType": "2", "SCPN": "1", "MainInitTime": "10", "IniCDelay": "1.0", "SCDelay": "0.2", "CoupledCtrl": "No", "SBD": "0.6", "ASB": "Yes", "CompressorStart": "Automatic", "ConverterStart": "Automatic", "ConverterStartDelay": "10.0", "PantCompressorStart": "Mixed", "PantAutoValve": "Yes", "BrakeDelays": "PR", "BrakeOpModes": "PN", "DirChangeMaxPos": "3", "HAO": "No", "HMO": "Yes", "HGDP1": "0", "ReleaseParkingBySpringBrake": "Yes", "MotorBlowersStart": "Converter", "CompartmentLightsStart": "Manual", "AutomaticCabActivation": "No", "InactivaCabEmergencyBrake": "Yes", "InactiveCabFlag": "3", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Circuit": {"ImaxHi": "500"}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "MaxHoldTime": "0.1", "EmergencyBrakeDelay": "2.1", "CabDependent": "Yes", "RadioStop": "Yes", "EmergencyBrakeWarningSignal": "2", "MagnetLocation": "4.5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main", "HMaxVoltage": "400"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "1", "MaxVoltage": "4000", "MinV": "2000", "MaxCurrent": "1200", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "2"}, + "Blending": {"MED_amax": "1.04", "MED_EPVC": "No", "MED_Ncor": "No", "MED_FirstDelayED": "2", "MED_ScndDelayED": "1"}, + "Engine": {"EngineType": "ElectricInductionMotor", "Trans": "40:218", "dfic": "470", "dfmax": "0.790", "p": "2", "cfu": "33.43", "cim": "10.22", "icif": "0.67", "Uzmax": "2340", "Uzh": "2340", "DU": "20", "I0": "20", "fcfu": "33.5", "F0": "75", "a1": "0.0", "Pmax": "980", "Fh": "48", "Ph": "830", "Vh0": "2", "Vh1": "5", "Imax": "377", "abed": "0", "Flat": "Yes", "edep": "1.0", "MotorBlowersSpeed": "-29", "InvNo": "2", "InvCtrCplFlag": "255"}, + "SpeedControl": {"SpeedCtrl": "Yes", "SpeedButtons": "0|40|50|60|70|80|100|120|140|160", "OverrideManual": "Yes", "InitPwr": "0.3", "MaxPwrVel": "5", "StartVel": "2", "VelStep": "5", "PwrStep": "0.1", "MinPwr": "0.3", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "160", "Offset": "-0.5", "kPpos": "0.1", "kPneg": "0.4", "kIpos": "0.06", "kIneg": "0.06", "BrakeIntervention": "No", "PowerUpSpeed": "0.3", "PowerDownSpeed": "0.3"}, + "AI": {"Pantstate": "2"}, + "ffList": {"Size": "10", "rows": [ + ["0","2.49"], + ["22.1","2.49"], + ["24.1","2.50"], + ["31.3","2.50"], + ["32.3","2.53"], + ["39.3","3.0"], + ["65","5.0"], + ["91","7.0"], + ["124","9.0"], + ["161","11.7"], + ["177","13.0"] + ]}, + "LightsList": {"Size": "5", "Wrap": "no", "Default": "1", "rows": [ + ["0","0"], + ["21","34"], + ["22","34"], + ["34","34"], + ["16","1"] + ]} +} diff --git a/tests/unit/data/fiz/elf_v1/22wec-a.json b/tests/unit/data/fiz/elf_v1/22wec-a.json new file mode 100644 index 000000000..6059b098d --- /dev/null +++ b/tests/unit/data/fiz/elf_v1/22wec-a.json @@ -0,0 +1,54 @@ +{ + "_source": "pkp/elf_v1/22wec-a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "29000", "Vmax": "120", "PWR": "800", "SandCap": "500", "Type": "ezt", "HeatingP": "37"}, + "Load": {"MaxLoad": "108", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2.3", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.081", "H": "4.228", "W": "2.96", "Cx": "0.1"}, + "Wheels": {"D": "0.850", "AIM": "8000", "Tw": "1.435", "Axle": "Bo'", "Ad": "2.83", "Bd": "16.3", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.1", "MaxLBP": "4.2", "RM": "0.74", "RV": "-1", "BCN": "4", "BCR": "0.127", "BCD": "0.05", "BCM": "1.9075", "HiPP": "5", "LoPP": "3.5", "Vv": "0.5", "MinCP": "7.7", "MaxCP": "9.0", "CompressorSpeed": "0.025", "BVV": "150", "BM": "Cosid", "BCS": "1", "BSA": "0.00", "BRE": "0.95", "TareMaxBP": "2.6", "CompressorPower": "Converter", "LPOn": "3.0", "LPOff": "4.5", "UBB1": "2", "UBB2": "8"}, + "SpringBrake": {"Volume": "10", "MBF": "2.9", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "4.2", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.24", "CloseSpeed": "0.2", "DoorMaxShiftL": "0.74", "DoorMaxShiftR": "0.74", "DoorOpenMethod": "Plug", "DoorStayOpen": "5.0", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.05", "DoorCloseDelay": "1.8", "PlatformMaxShift": "0.270", "PlatformSpeed": "0.4", "PlatformOpenMethod": "Shift", "MirrorMaxShift": "45", "DoorNeedPermit": "Yes", "DoorPermitList": "1|0|2|3", "DoorPermitListDefault": "2", "DoorAutoCloseRemote": "Yes", "MirrorVelClose": "25"}, + "BuffCoupl1": {"CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39"}, + "BuffCoupl2": {"CType": "Articulated", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-255"}, + "Cntrl": {"BrakeHandle": "MHZ_K5P", "LocBrakeHandle": "FD1", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "35", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "LocalBrake": "PneumaticBrake", "MCPN": "4", "EIMCtrlType": "2", "SCPN": "1", "MainInitTime": "10", "IniCDelay": "1.0", "SCDelay": "0.2", "CoupledCtrl": "No", "SBD": "0.6", "ASB": "Yes", "CompressorStart": "Automatic", "ConverterStart": "Automatic", "ConverterStartDelay": "10.15", "BrakeDelays": "PR", "BrakeOpModes": "PN", "DirChangeMaxPos": "3", "HAO": "No", "HMO": "Yes", "HGDP1": "0", "ReleaseParkingBySpringBrake": "Yes", "MotorBlowersStart": "Converter", "CompartmentLightsStart": "Manual", "PantEPValveSpring": "No", "PantCompressorStart": "Automatic", "PantAutoValve": "Yes", "AutomaticCabActivation": "No", "InactivaCabEmergencyBrake": "Yes", "InactiveCabFlag": "9", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "MaxHoldTime": "0.1", "EmergencyBrakeDelay": "2.1", "CabDependent": "Yes", "RadioStop": "Yes", "EmergencyBrakeWarningSignal": "2", "MagnetLocation": "4.5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main", "HMaxVoltage": "400"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "1", "MaxVoltage": "4000", "MinV": "2000", "MaxCurrent": "1200", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "1"}, + "Switches": {"PantographPresets": "1|3|2"}, + "Circuit": {"ImaxHi": "500"}, + "Blending": {"MED_amax": "1.04", "MED_EPVC": "No", "MED_Ncor": "No", "MED_FirstDelayED": "2", "MED_ScndDelayED": "1"}, + "Engine": {"EngineType": "ElectricInductionMotor", "Trans": "40:218", "dfic": "470", "dfmax": "0.790", "p": "2", "cfu": "33.43", "cim": "10.22", "icif": "0.67", "Uzmax": "2340", "Uzh": "2340", "DU": "20", "I0": "20", "fcfu": "33.5", "F0": "75", "a1": "0.0", "Pmax": "990", "Fh": "48", "Ph": "830", "Vh0": "2", "Vh1": "5", "Imax": "377", "abed": "0", "Flat": "Yes", "edep": "1.0", "MotorBlowersSpeed": "-29", "InvNo": "2", "InvCtrCplFlag": "255"}, + "SpeedControl": {"SpeedCtrl": "Yes", "SpeedButtons": "0|10|20|30|40|50|60|80|100|120", "OverrideManual": "Yes", "InitPwr": "0.3", "MaxPwrVel": "5", "StartVel": "2", "VelStep": "5", "PwrStep": "0.1", "MinPwr": "0.3", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "160", "Offset": "-0.5", "kPpos": "0.1", "kPneg": "0.4", "kIpos": "0.06", "kIneg": "0.06", "BrakeIntervention": "No", "PowerUpSpeed": "0.3", "PowerDownSpeed": "0.3"}, + "AI": {"Pantstate": "1"}, + "ffList": {"Size": "3", "rows": [ + ["0","2.0"], + ["33.7","2.08"], + ["34.0","2.12"], + ["130.4","8.10"] + ]}, + "ffBrakeList": {"Size": "4", "rows": [ + ["0","2.0"], + ["33.7","2.08"], + ["34.8","2.08"], + ["34.8","2.17"], + ["130.4","8.10"] + ]}, + "LightsList": {"Size": "6", "Wrap": "no", "Default": "1", "rows": [ + ["21","34"], + ["22","34"], + ["16","1"], + ["34","34"], + ["17","34"], + ["38","34"] + ]} +} diff --git a/tests/unit/data/fiz/en57_v1/5bgaiii_led_spot.json b/tests/unit/data/fiz/en57_v1/5bgaiii_led_spot.json new file mode 100644 index 000000000..7ca99dcc3 --- /dev/null +++ b/tests/unit/data/fiz/en57_v1/5bgaiii_led_spot.json @@ -0,0 +1,26 @@ +{ + "_source": "pkp/en57_v1/5bgaiii_led_spot.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "34000", "Vmax": "110", "PWR": "0", "SandCap": "500", "Type": "ezt", "HeatingP": "19.6"}, + "Load": {"MaxLoad": "95", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.286", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "AIM": "2000", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "BuffCoupl1": {"ControlType": "en57", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39"}, + "BuffCoupl2": {"ControlType": "en57", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-887", "PowerFlag": "768"}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes", "MagnetLocation": "5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"}, + "Switches": {"Pantograph": "impulse", "Converter": "impulse"}, + "Brake": {"BrakeValve": "EP2", "NBpA": "4", "MBF": "85.0", "MaxBP": "4", "BCN": "1", "BCR": "0.203", "BCD": "0.15", "BCM": "9.39", "HiPP": "5", "LoPP": "3.5", "Vv": "0.3", "MinCP": "5.8", "MaxCP": "7.2", "CompressorSpeed": "0.013", "TareMaxBP": "2.5", "BVV": "88", "BM": "P10-Bgu", "BCS": "1.5", "BSA": "5", "BRE": "0.9", "CompressorPower": "Coupler2"}, + "Cntrl": {"BrakeHandle": "FVel6", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "52", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "ManualBrake", "MCPN": "3", "SCPN": "3", "IniCDelay": "0.0", "SCDelay": "0.975", "CoupledCtrl": "Yes", "CompartmentLightsStart": "Manual", "BatteryStart": "Disabled", "ConverterStart": "Disabled", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.315", "CloseSpeed": "0.315", "DoorCloseDelay": "1.6", "DoorMaxShiftL": "0.625", "DoorMaxShiftR": "0.625", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes", "DoorBlocked": "Yes"} +} diff --git a/tests/unit/data/fiz/en57_v1/5bgb.json b/tests/unit/data/fiz/en57_v1/5bgb.json new file mode 100644 index 000000000..3075c0a6f --- /dev/null +++ b/tests/unit/data/fiz/en57_v1/5bgb.json @@ -0,0 +1,26 @@ +{ + "_source": "pkp/en57_v1/5bgb.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "34000", "Vmax": "110", "PWR": "0", "SandCap": "500", "Type": "ezt", "HeatingP": "19.6"}, + "Load": {"MaxLoad": "90", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.288", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "AIM": "2000", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "BuffCoupl1": {"ControlType": "en57", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-887", "PowerFlag": "768"}, + "BuffCoupl2": {"ControlType": "en57", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39"}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes", "MagnetLocation": "5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"}, + "Switches": {"Pantograph": "impulse", "Converter": "impulse"}, + "Brake": {"BrakeValve": "EP2", "NBpA": "4", "MBF": "85.0", "MaxBP": "4", "BCN": "1", "BCR": "0.203", "BCD": "0.15", "BCM": "9.39", "HiPP": "5", "LoPP": "3.5", "Vv": "0.3", "MinCP": "5.8", "MaxCP": "7.2", "CompressorSpeed": "0.013", "TareMaxBP": "2.5", "BVV": "88", "BM": "P10-Bgu", "BCS": "1.5", "BSA": "5", "BRE": "0.9", "CompressorPower": "Coupler1"}, + "Cntrl": {"BrakeHandle": "FVel6", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "52", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "ManualBrake", "MCPN": "3", "SCPN": "3", "IniCDelay": "0.0", "SCDelay": "0.975", "CoupledCtrl": "Yes", "CompartmentLightsStart": "Manual", "ConverterStart": "Disabled", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.315", "CloseSpeed": "0.315", "DoorCloseDelay": "1.6", "DoorMaxShiftL": "0.625", "DoorMaxShiftR": "0.625", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes", "DoorBlocked": "Yes"} +} diff --git a/tests/unit/data/fiz/en57_v1/6ba_840.json b/tests/unit/data/fiz/en57_v1/6ba_840.json new file mode 100644 index 000000000..aed27f1e7 --- /dev/null +++ b/tests/unit/data/fiz/en57_v1/6ba_840.json @@ -0,0 +1,26 @@ +{ + "_source": "pkp/en57_v1/6ba_840.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "34000", "Vmax": "110", "PWR": "0", "SandCap": "500", "Type": "ezt", "HeatingP": "19.6"}, + "Load": {"MaxLoad": "95", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.286", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "AIM": "2000", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "BuffCoupl1": {"ControlType": "en57", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39"}, + "BuffCoupl2": {"ControlType": "en57", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-887", "PowerFlag": "768"}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes", "MagnetLocation": "5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"}, + "Switches": {"Pantograph": "impulse", "Converter": "impulse"}, + "Brake": {"BrakeValve": "EP2", "NBpA": "4", "MBF": "85.0", "MaxBP": "4", "BCN": "1", "BCR": "0.203", "BCD": "0.15", "BCM": "9.39", "HiPP": "5", "LoPP": "3.5", "Vv": "0.3", "MinCP": "5.8", "MaxCP": "7.2", "CompressorSpeed": "0.013", "TareMaxBP": "2.5", "BVV": "88", "BM": "P10-Bgu", "BCS": "1.5", "BSA": "5", "BRE": "0.9", "CompressorPower": "Coupler2"}, + "Cntrl": {"BrakeHandle": "FVel6", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "52", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "ManualBrake", "MCPN": "3", "SCPN": "3", "IniCDelay": "0.0", "SCDelay": "0.975", "CoupledCtrl": "Yes", "CompartmentLightsStart": "Manual", "BatteryStart": "Disabled", "ConverterStart": "Disabled", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.32", "CloseSpeed": "0.242", "DoorCloseDelay": "3.35", "DoorMaxShiftL": "0.625", "DoorMaxShiftR": "0.625", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorNeedPermit": "Yes", "DoorAutoCloseVel": "10.0"} +} diff --git a/tests/unit/data/fiz/en57_v1/6bai.json b/tests/unit/data/fiz/en57_v1/6bai.json new file mode 100644 index 000000000..13f1132b7 --- /dev/null +++ b/tests/unit/data/fiz/en57_v1/6bai.json @@ -0,0 +1,25 @@ +{ + "_source": "pkp/en57_v1/6bai.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "34000", "Vmax": "110", "PWR": "0", "SandCap": "500", "Type": "ezt", "HeatingP": "19.6"}, + "Load": {"MaxLoad": "80", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.286", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "AIM": "2000", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "K", "NBpA": "4", "MBF": "85.0", "MaxBP": "12", "BCN": "1", "BCR": "0.203", "BCD": "0.15", "BCM": "12.00", "HiPP": "5", "LoPP": "3.5", "Vv": "0.3", "MinCP": "5.8", "MaxCP": "7.2", "CompressorSpeed": "0.013", "TareMaxBP": "2.5", "BVV": "88", "BM": "P10-Bgu", "BCS": "1.5", "BSA": "5", "BRE": "0.9", "CompressorPower": "Coupler2"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.315", "CloseSpeed": "0.315", "DoorCloseDelay": "1.6", "DoorMaxShiftL": "0.625", "DoorMaxShiftR": "0.625", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes", "DoorBlocked": "Yes"}, + "BuffCoupl1": {"CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39"}, + "BuffCoupl2": {"CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-887", "PowerFlag": "768"}, + "Cntrl": {"BrakeHandle": "St113", "BrakeSystem": "ElectroPneumatic", "LocalBrake": "ManualBrake", "MaxBPMass": "52", "BCPN": "5", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "DynamicBrake": "Passive", "MCPN": "3", "SCPN": "3", "IniCDelay": "0.0", "SCDelay": "0.975", "CoupledCtrl": "Yes", "CompartmentLightsStart": "Manual", "BatteryStart": "Disabled", "ConverterStart": "Disabled", "rows": [ + ["-1","0.5","-1","9","Pneumatic"], + ["0","-1","0.0","6","ElectroPneumatic"], + ["1","-1","0.35","4","ElectroPneumatic"], + ["2","-1","-1","0","Pneumatic"], + ["3","-1","-1","3","Pneumatic"], + ["4","0.34","-1","4","Pneumatic"], + ["5","0.0","-1","9","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes", "MagnetLocation": "5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"}, + "Switches": {"Pantograph": "impulse", "Converter": "impulse"} +} diff --git a/tests/unit/data/fiz/en57akl_v1/akl_ra.json b/tests/unit/data/fiz/en57akl_v1/akl_ra.json new file mode 100644 index 000000000..18cc9ed8d --- /dev/null +++ b/tests/unit/data/fiz/en57akl_v1/akl_ra.json @@ -0,0 +1,40 @@ +{ + "_source": "pkp/en57akl_v1/akl_ra.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "38000", "Vmax": "120", "PWR": "0", "SandCap": "500", "Type": "ezt", "HeatingP": "20"}, + "Load": {"MaxLoad": "80", "LoadQ": "pieces", "LoadAccepted": "Passengers", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.286", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "AIM": "2000", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.7", "Bd": "14.9", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.5", "MaxLBP": "3.5", "BCN": "1", "BCR": "0.203", "BCD": "0.105", "BCM": "9.39", "HiPP": "5", "LoPP": "3.5", "Vv": "0.3", "MinCP": "6.2", "MaxCP": "7.2", "CompressorSpeed": "0.01416667", "TareMaxBP": "2.5", "BVV": "107", "BM": "P10-Bgu", "BCS": "1.5", "BSA": "5", "BRE": "0.9", "CompressorPower": "Coupler2", "UBB1": "2", "UBB2": "8", "LPOn": "3.0", "LPOff": "4.5"}, + "SpringBrake": {"Volume": "10", "MBF": "29", "MaxSP": "7.2", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "4.2", "ValveOnArea": "0.0001", "ValveOffArea": "0.0001", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.2", "CloseSpeed": "0.2", "DoorCloseDelay": "2.5", "DoorMaxShiftL": "0.58", "DoorMaxShiftR": "0.58", "DoorOpenMethod": "Plug", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.035", "DoorNeedPermit": "Yes", "DoorStayOpen": "5.0", "DoorAutoCloseVel": "10.0"}, + "BuffCoupl1": {"CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39"}, + "BuffCoupl2": {"CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-119", "PowerFlag": "1792"}, + "Cntrl": {"BrakeHandle": "MHZ_EN57", "LocBrakeHandle": "FD1", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "52", "BCPN": "10", "BDelay1": "15", "BDelay2": "4", "MainInitTime": "10", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "10", "SCPN": "1", "SpeedCtrlATOF": "1", "EIMCtrlAddZeros": "Yes", "IniCDelay": "0.0", "SCDelay": "0.8", "CoupledCtrl": "No", "BrakeOpModes": "PNEPMED", "CompressorStart": "Automatic", "CompartmentLightsStart": "Manual", "ConverterStart": "Disabled", "BatteryStart": "Disabled", "AutomaticCabActivation": "No", "InactivaCabEmergencyBrake": "Yes", "InactiveCabFlag": "15", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Blending": {"MED_Vmax": "95", "MED_Vmin": "1", "MED_Vref": "100", "MED_amax": "0.8", "MED_EPVC": "Yes", "MED_Ncor": "No"}, + "SpeedControl": {"SpeedCtrl": "Yes", "OverrideManual": "No", "InitPwr": "1.0", "MaxPwrVel": "-1", "StartVel": "-1", "VelStep": "10", "PwrStep": "0.00", "MinPwr": "1.0", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "120", "Offset": "-0.1", "kPpos": "0.2", "kPneg": "0.4", "kIpos": "0.00", "kIneg": "0.00", "BrakeIntervention": "No", "SpeedCtrlATOF": "1"}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "2.1", "CabDependent": "Yes", "RadioStop": "Yes", "MagnetLocation": "5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"}, + "Switches": {"Pantograph": "impulse", "Converter": "impulse"}, + "AI": {"Pantstate": "2", "IdlePantUp": "Yes"}, + "Engine": {"EngineType": "ElectricInductionMotor", "Trans": "46:252", "dfic": "377", "dfmax": "0.98", "p": "2", "cfu": "46.8", "cim": "14.2", "icif": "0.671", "Uzmax": "2340", "Uzh": "2340", "DU": "20", "I0": "20", "fcfu": "49", "F0": "130", "a1": "0", "Pmax": "0", "Fh": "130", "Ph": "1000", "Vh0": "5", "Vh1": "10", "Imax": "557", "abed": "0", "edep": "0.95"}, + "LightsList": {"Size": "7", "Wrap": "No", "Default": "1", "rows": [ + ["0","0"], + ["34","34"], + ["38","34"], + ["22","34"], + ["16","1"], + ["1","16"], + ["21","34"] + ]} +} diff --git a/tests/unit/data/fiz/ep05_v2/ep05.json b/tests/unit/data/fiz/ep05_v2/ep05.json new file mode 100644 index 000000000..4340a0b11 --- /dev/null +++ b/tests/unit/data/fiz/ep05_v2/ep05.json @@ -0,0 +1,84 @@ +{ + "_source": "pkp/ep05_v2/ep05.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "82500", "Mred": "12000", "Vmax": "160", "PWR": "2032", "SandCap": "100", "Type": "ep05"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "16.14", "H": "3.987", "W": "2.950", "Cx": "0.2"}, + "Wheels": {"D": "1.250", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "3.05", "Bd": "8.55", "Rmin": "120", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.8", "MaxASBP": "1.3", "RM": "0.56", "BCN": "4", "BCR": "0.1524", "BCD": "0.05", "BCM": "7.13", "Vv": "1.07", "MinCP": "7.5", "MaxCP": "9.0", "BCS": "1.2", "BSA": "0.0", "BM": "P10-Bgu", "BVV": "220", "CompressorSpeed": "0.06", "BRE": "0.85"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "103", "ControlType": "EP05"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Manual", "ManualBrake": "Yes", "LocalBrake": "PneumaticBrake", "DynamicBrake": "Passive", "MCPN": "46", "SCPN": "0", "CoupledCtrl": "Yes", "AutoRelay": "Yes", "SCIM": "1", "IniCDelay": "0.4", "SCDelay": "0.26", "BrakeDelays": "P", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "Camshaft": "Yes", "rows": [ + ["-1","0.60","-1","15","Pneumatic"], + ["0","0.50","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.28","-1","9","Pneumatic"], + ["6","0.00","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "48", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "3600", "MaxCurrent": "1600", "MinH": "0.38", "MinV": "1900", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "3"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "44:77", "WindingRes": "0.126", "nmax": "1270"}, + "Switches": {"PantographPresets": "0|2|3|1"}, + "MotorParamTable0": {"rows": [ + ["0","56.48","80","0.4","370","80","0.445","0"], + ["1","45.15","150","0.3","296","150","0.29","0"], + ["2","41.45","300","0.2","271","300","0.16","0"], + ["3","39.29","400","0.13","257","400","0.13","0"], + ["4","38.36","700","0.04","251","700","0.04","0"], + ["5","33.83","850","0.01","221","850","0.01","0"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "800", "ImaxHi": "800", "IminLo": "350", "IminHi": "450"}, + "RList": {"Size": "46", "RVent": "Automatic", "RVentnmax": "3300", "RVentCutOff": "0.1", "rows": [ + ["0","0","0","0","0","0"], + ["1","100.000","1","4","0","0"], + ["2","14.308","1","4","0","0"], + ["3","10.558","1","4","0","0"], + ["4","8.908","1","4","0","0"], + ["5","7.668","1","4","0","0"], + ["6","6.428","1","4","0","0"], + ["7","5.744","1","4","0","0"], + ["8","5.060","1","4","0","0"], + ["9","4.616","1","4","0","0"], + ["10","4.172","1","4","0","0"], + ["11","3.813","1","4","0","0"], + ["12","3.454","1","4","0","0"], + ["13","3.100","1","4","0","0"], + ["14","2.746","1","4","0","0"], + ["15","2.456","1","4","0","0"], + ["16","2.215","1","4","0","0"], + ["17","1.974","1","4","0","0"], + ["18","1.629","1","4","0","0"], + ["19","1.284","1","4","0","0"], + ["20","1.054","1","4","0","0"], + ["21","0.824","1","4","0","0"], + ["22","0.593","1","4","0","0"], + ["23","0.362","1","4","0","0"], + ["24","0.181","1","4","0","0"], + ["25","0.000","1","4","0","0"], + ["26","0.000","1","4","0","1"], + ["27","0.000","1","4","0","2"], + ["28","0.000","1","4","0","3"], + ["29","0.000","1","4","0","4"], + ["30","0.000","1","4","0","4"], + ["31","0.000","1","4","0","4"], + ["32","0.000","1","4","0","4"], + ["33","2.086","2","2","0","0"], + ["34","1.727","2","2","0","0"], + ["35","1.373","2","2","0","0"], + ["36","1.228","2","2","0","0"], + ["37","0.987","2","2","0","0"], + ["38","0.642","2","2","0","0"], + ["39","0.412","2","2","0","0"], + ["40","0.181","2","2","0","0"], + ["41","0.00","2","2","0","0"], + ["42","0.00","2","2","0","1"], + ["43","0.00","2","2","0","2"], + ["44","0.00","2","2","0","3"], + ["45","0.00","2","2","0","4"], + ["46","0.00","2","2","0","5"] + ]} +} diff --git a/tests/unit/data/fiz/ep09_v2/104e.json b/tests/unit/data/fiz/ep09_v2/104e.json new file mode 100644 index 000000000..9928285cb --- /dev/null +++ b/tests/unit/data/fiz/ep09_v2/104e.json @@ -0,0 +1,76 @@ +{ + "_source": "pkp/ep09_v2/104e.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "10", "UnLoadSpeed": "0.1"}, + "Param": {"Category": "train", "M": "85400", "Mred": "12000", "Vmax": "160", "PWR": "2920", "SandCap": "730"}, + "Dimensions": {"L": "16.74", "H": "4.12", "W": "2.96", "Cx": "0.3"}, + "Wheels": {"D": "1.2", "Tw": "1.435", "AIM": "2000", "Axle": "Bo'Bo'", "Ad": "3.2", "Bd": "7.97", "Rmin": "120", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.6", "MaxASBP": "1.3", "RM": "0.72", "BCN": "8", "BCR": "0.1143", "BCD": "0.05", "BCM": "5.18", "HiPP": "5", "LoPP": "3.5", "Vv": "0.834", "MinCP": "8.5", "MaxCP": "10.0", "CompressorSpeed": "0.058", "BCS": "1", "BSA": "2", "BM": "P10-Bgu", "BVV": "278", "CompressorPower": "Main"}, + "SpringBrake": {"Volume": "10", "MBF": "8", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "3.5", "ValveOnArea": "0.0002", "ValveOffArea": "0.0002", "ValvePNBArea": "0.002", "MTC": "4"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "99"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "27", "BDelay2": "4", "BDelay3": "55", "BDelay4": "25", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "ManualBrake": "No", "DynamicBrake": "Automatic", "DBAM": "1", "MCPN": "31", "SCPN": "5", "AutoRelay": "No", "SCIM": "255", "IniCDelay": "0.5", "SCDelay": "0.25", "SCDDelay": "0.25", "BrakeDelays": "GPR", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "rows": [ + ["-1","0.7","-1","9","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.44","-1","9","Pneumatic"], + ["2","0.40","-1","9","Pneumatic"], + ["3","0.36","-1","9","Pneumatic"], + ["4","0.32","-1","9","Pneumatic"], + ["5","0.28","-1","9","Pneumatic"], + ["6","0","-1","35","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "3", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "4", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "3600", "MaxCurrent": "2000", "MinH": "0.38", "MinV": "1900", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "3"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "38:73", "WindingRes": "0.07", "nmax": "1530"}, + "Switches": {"Pantograph": "Continuos", "RelayResetButton1": "1", "RelayResetButton2": "4"}, + "MotorParamTable0": {"rows": [ + ["0","52.81","78","0.53","356.8","78","0.53"], + ["1","42.73","120","0.42","288.7","120","0.42"], + ["2","38.38","193","0.33","259.3","193","0.33"], + ["3","36.60","320","0.24","247.3","320","0.24"], + ["4","36.66","534","0.16","247.7","534","0.16"], + ["5","40.29","1100","0.06","272.2","1100","0.06"] + ]}, + "Circuit": {"CircuitRes": "0.00", "ImaxLo": "850", "ImaxHi": "850", "IminLo": "520", "IminHi": "650"}, + "RList": {"Size": "31", "RVent": "Automatic", "RVentnmax": "3300", "RVentCutOff": "0.1", "rows": [ + ["0","0","0","0","0","0"], + ["1","10.758","1","4","0","2"], + ["2","10.758","1","4","0","0"], + ["3","7.994","1","4","0","0"], + ["4","6.156","1","4","0","0"], + ["5","4.808","1","4","0","0"], + ["6","4.032","1","4","0","0"], + ["7","3.342","1","4","0","0"], + ["8","3.014","1","4","0","0"], + ["9","2.686","1","4","0","0"], + ["10","2.375","1","4","0","0"], + ["11","2.064","1","4","0","0"], + ["12","1.772","1","4","0","0"], + ["13","1.480","1","4","0","0"], + ["14","1.196","1","4","0","0"], + ["15","0.912","1","4","0","0"], + ["16","0.683","1","4","0","0"], + ["17","0.454","1","4","0","0"], + ["18","0.227","1","4","0","0"], + ["19","0","1","4","0","255"], + ["20","5.379","2","2","0","0"], + ["21","3.997","2","2","0","0"], + ["22","3.078","2","2","0","0"], + ["23","2.404","2","2","0","0"], + ["24","2.016","2","2","0","0"], + ["25","1.671","2","2","0","0"], + ["26","1.343","2","2","0","0"], + ["27","1.032","2","2","0","0"], + ["28","0.740","2","2","0","0"], + ["29","0.456","2","2","0","0"], + ["30","0.227","2","2","0","0"], + ["31","0","2","2","0","255"] + ]}, + "WiperList": {"Size": "4", "Angle": "83", "rows": [ + ["0","1.0","0.0","0.5"], + ["3","1.0","5.0","0.5"], + ["3","1.0","0.0","0.5"], + ["3","0.5","0.0","0.5"] + ]} +} diff --git a/tests/unit/data/fiz/et22_v3/201e-rn.json b/tests/unit/data/fiz/et22_v3/201e-rn.json new file mode 100644 index 000000000..05b2e9c8a --- /dev/null +++ b/tests/unit/data/fiz/et22_v3/201e-rn.json @@ -0,0 +1,86 @@ +{ + "_source": "pkp/et22_v3/201e-rn.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "120000", "Mred": "18000", "Vmax": "125", "PWR": "3120", "SandCap": "700", "Type": "et22"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "19.240", "H": "4.055", "W": "3.005", "Cx": "0.3"}, + "Wheels": {"D": "1.250", "AIM": "2000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "3.05", "Bd": "10.30", "Rmin": "120", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.2", "MaxASBP": "1.8", "BCN": "8", "BCR": "0.1143", "BCD": "0.05", "BCM": "7.75", "HiPP": "5", "LoPP": "3.5", "Vv": "1", "MinCP": "7.5", "MaxCP": "8.5", "CompressorSpeed": "0.0566", "BVV": "250", "RM": "0.677", "BSA": "0", "BCS": "1", "BM": "P10-Bgu", "CompressorPower": "Main", "BRE": "0.75"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "99"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "17", "BDelay2": "7", "BDelay3": "45", "BDelay4": "25", "BrakeDelays": "GPR", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "ManualBrake": "Yes", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "DynamicBrake": "Passive", "MCPN": "48", "SCPN": "6", "AutoRelay": "No", "IniCDelay": "0.5", "SCDelay": "0.26", "SCDDelay": "0.05", "MotorBlowersStart": "Converter", "MotorBlowersSpeed": "-1310", "rows": [ + ["-1","0.6","-1","12","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.43","-1","9","Pneumatic"], + ["2","0.40","-1","9","Pneumatic"], + ["3","0.37","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.28","-1","9","Pneumatic"], + ["6","0","-1","18","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "3600", "MaxCurrent": "1800", "MinH": "0.38", "MinV": "1900", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "3"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "18:79", "WindingRes": "0.14", "nmax": "2390"}, + "MotorParamTable0": {"rows": [ + ["0","21.4","105","0.12","141.5","105","0.12"], + ["1","19.57","140","0.07","129.3","140","0.07"], + ["2","20.42","190","0.09","134.7","190","0.09"], + ["3","22.97","160","0.25","152.1","160","0.25"], + ["4","22.12","255","0.16","145.8","255","0.16"], + ["5","24.18","170","0.35","158.2","170","0.35"], + ["6","22.76","800","0.02","149","800","0.02"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "600", "ImaxHi": "750", "IminLo": "350", "IminHi": "450"}, + "RList": {"Size": "48", "RVent": "Automatic", "RVentnmax": "3300", "RVentCutOff": "0.2", "rows": [ + ["0","0","0","0","0"], + ["1","25.88","1","6","0"], + ["2","15.05","1","6","0"], + ["3","11.02","1","6","0"], + ["4","8.98","1","6","0"], + ["5","7.54","1","6","0"], + ["6","6.28","1","6","0"], + ["7","5.50","1","6","0"], + ["8","5.11","1","6","0"], + ["9","4.67","1","6","0"], + ["10","4.11","1","6","0"], + ["11","3.61","1","6","0"], + ["12","3.06","1","6","0"], + ["13","2.70","1","6","0"], + ["14","2.38","1","6","0"], + ["15","2.05","1","6","0"], + ["16","1.69","1","6","0"], + ["17","1.36","1","6","0"], + ["18","1.00","1","6","0"], + ["19","0.84","1","6","0"], + ["20","0.29","1","6","0"], + ["21","0.00","1","6","0"], + ["22","5.53","2","3","0"], + ["23","4.53","2","3","0"], + ["24","3.58","2","3","0"], + ["25","3.03","2","3","0"], + ["26","2.60","2","3","0"], + ["27","2.26","2","3","0"], + ["28","1.97","2","3","0"], + ["29","1.60","2","3","0"], + ["30","1.46","2","3","0"], + ["31","1.26","2","3","0"], + ["32","1.00","2","3","0"], + ["33","0.61","2","3","0"], + ["34","0.35","2","3","0"], + ["35","0.23","2","3","0"], + ["36","0.00","2","3","0"], + ["37","2.64","3","2","0"], + ["38","2.23","3","2","0"], + ["39","1.78","3","2","0"], + ["40","1.49","3","2","0"], + ["41","1.29","3","2","0"], + ["42","1.11","3","2","0"], + ["43","0.95","3","2","0"], + ["44","0.78","3","2","0"], + ["45","0.62","3","2","0"], + ["46","0.47","3","2","0"], + ["47","0.22","3","2","0"], + ["48","0.00","3","2","0"] + ]} +} diff --git a/tests/unit/data/fiz/et22_v3/201e-w.json b/tests/unit/data/fiz/et22_v3/201e-w.json new file mode 100644 index 000000000..80b9aedda --- /dev/null +++ b/tests/unit/data/fiz/et22_v3/201e-w.json @@ -0,0 +1,86 @@ +{ + "_source": "pkp/et22_v3/201e-w.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "120000", "Mred": "18000", "Vmax": "125", "PWR": "3120", "SandCap": "700", "Type": "et22"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "19.240", "H": "4.055", "W": "3.005", "Cx": "0.3"}, + "Wheels": {"D": "1.250", "AIM": "2000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "3.05", "Bd": "10.30", "Rmin": "120", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.2", "MaxASBP": "1.8", "BCN": "8", "BCR": "0.1143", "BCD": "0.05", "BCM": "7.75", "HiPP": "5", "LoPP": "3.5", "Vv": "1", "MinCP": "7.5", "MaxCP": "8.5", "CompressorSpeed": "0.0566", "BVV": "250", "RM": "0.677", "BSA": "0", "BCS": "1", "BM": "P10-Bgu", "CompressorPower": "Main", "BRE": "0.75"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "99"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "17", "BDelay2": "7", "BDelay3": "45", "BDelay4": "25", "BrakeDelays": "GPR", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "ManualBrake": "Yes", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "DynamicBrake": "Passive", "MCPN": "48", "SCPN": "6", "AutoRelay": "No", "IniCDelay": "0.5", "SCDelay": "0.26", "SCDDelay": "0.05", "MotorBlowersStart": "Converter", "MotorBlowersSpeed": "-1310", "rows": [ + ["-1","0.6","-1","12","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.43","-1","9","Pneumatic"], + ["2","0.40","-1","9","Pneumatic"], + ["3","0.37","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.28","-1","9","Pneumatic"], + ["6","0","-1","18","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "3600", "MaxCurrent": "1800", "MinH": "0.38", "MinV": "1900", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "3"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "18:79", "WindingRes": "0.14", "nmax": "2390"}, + "MotorParamTable0": {"rows": [ + ["0","21.4","105","0.12","141.5","105","0.12"], + ["1","19.57","140","0.07","129.3","140","0.07"], + ["2","20.42","190","0.09","134.7","190","0.09"], + ["3","22.97","160","0.25","152.1","160","0.25"], + ["4","22.12","255","0.16","145.8","255","0.16"], + ["5","24.18","170","0.35","158.2","170","0.35"], + ["6","22.76","800","0.02","149","800","0.02"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "600", "ImaxHi": "750", "IminLo": "350", "IminHi": "450"}, + "RList": {"Size": "48", "RVent": "Automatic", "RVentnmax": "3300", "RVentCutOff": "0.2", "rows": [ + ["0","0","0","0","0"], + ["1","25.88","1","6","0"], + ["2","15.05","1","6","0"], + ["3","11.02","1","6","0"], + ["4","8.98","1","6","0"], + ["5","7.54","1","6","0"], + ["6","6.28","1","6","0"], + ["7","5.50","1","6","0"], + ["8","5.11","1","6","0"], + ["9","4.67","1","6","0"], + ["10","4.11","1","6","0"], + ["11","3.61","1","6","0"], + ["12","3.06","1","6","0"], + ["13","2.70","1","6","0"], + ["14","2.38","1","6","0"], + ["15","2.05","1","6","0"], + ["16","1.69","1","6","0"], + ["17","1.36","1","6","0"], + ["18","1.00","1","6","0"], + ["19","0.84","1","6","0"], + ["20","0.29","1","6","0"], + ["21","0.00","1","6","0"], + ["22","5.53","2","3","0"], + ["23","4.53","2","3","0"], + ["24","3.58","2","3","0"], + ["25","3.03","2","3","0"], + ["26","2.60","2","3","0"], + ["27","2.26","2","3","0"], + ["28","1.97","2","3","0"], + ["29","1.60","2","3","0"], + ["30","1.46","2","3","0"], + ["31","1.26","2","3","0"], + ["32","1.00","2","3","0"], + ["33","0.61","2","3","0"], + ["34","0.35","2","3","0"], + ["35","0.23","2","3","0"], + ["36","0.00","2","3","0"], + ["37","2.64","3","2","0"], + ["38","2.23","3","2","0"], + ["39","1.78","3","2","0"], + ["40","1.49","3","2","0"], + ["41","1.29","3","2","0"], + ["42","1.11","3","2","0"], + ["43","0.95","3","2","0"], + ["44","0.78","3","2","0"], + ["45","0.62","3","2","0"], + ["46","0.47","3","2","0"], + ["47","0.22","3","2","0"], + ["48","0.00","3","2","0"] + ]} +} diff --git a/tests/unit/data/fiz/et42_v2/112e-a.json b/tests/unit/data/fiz/et42_v2/112e-a.json new file mode 100644 index 000000000..f1ed904ff --- /dev/null +++ b/tests/unit/data/fiz/et42_v2/112e-a.json @@ -0,0 +1,77 @@ +{ + "_source": "pkp/et42_v2/112e-a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "82000", "Vmax": "100", "PWR": "2240", "SandCap": "850", "Type": "et42"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "15.43", "H": "4.62", "W": "2.95", "Cx": "0.3"}, + "Wheels": {"D": "1.250", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "3.05", "Bd": "8.55", "Rmin": "125", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.0", "MaxASBP": "1.5", "BCN": "4", "BCR": "0.127", "BCD": "0.12", "BCM": "5.76", "Vv": "1.2", "MinCP": "7.5", "MaxCP": "9.0", "BCS": "1", "BSA": "0.5", "BM": "P10-Bg", "BVV": "300", "CompressorSpeed": "0.0392", "CompressorPower": "Main", "BRE": "0.7"}, + "BuffCoupl1": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "99"}, + "BuffCoupl2": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-127"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BrakeDelays": "GP", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "ManualBrake": "Yes", "DynamicBrake": "Switch", "MCPN": "38", "SCPN": "5", "AutoRelay": "No", "SCIM": "255", "IniCDelay": "0.5", "SCDelay": "0.1", "rows": [ + ["-1","0.60","-1","15","Pneumatic"], + ["0","0.50","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.28","-1","9","Pneumatic"], + ["6","0.00","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "1", "MaxVoltage": "3600", "MaxCurrent": "1300", "MinH": "0.42", "MinV": "1900", "MaxH": "1.77", "CSW": "1.4", "PhysicalLayout": "1"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "25:86", "WindingRes": "0.5", "nmax": "1500"}, + "Switches": {"Pantograph": "Continuos", "MotorConnectors": "Toggle"}, + "MotorParamTable": {"rows": [ + ["0","21.86","205","142","150"], + ["1","21.49","244","139","187"], + ["2","21.48","299","138","234"], + ["3","19.77","320","136","297"], + ["4","23.75","581","126","333"], + ["5","24.47","768","123","412"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "650", "ImaxHi": "651", "IminLo": "415", "IminHi": "415"}, + "RList": {"Size": "38", "RVent": "Automatic", "RVentnmax": "3300", "RVentCutOff": "0.2", "rows": [ + ["0","0","0","0","0","0"], + ["1","10.566","1","4","0","5"], + ["2","10.566","1","4","0","3"], + ["3","10.566","1","4","0","0"], + ["4","9.136","1","4","0","0"], + ["5","7.706","1","4","0","0"], + ["6","6.881","1","4","0","0"], + ["7","6.056","1","4","0","0"], + ["8","5.682","1","4","0","0"], + ["9","5.308","1","4","0","0"], + ["10","4.932","1","4","0","0"], + ["11","4.556","1","4","0","0"], + ["12","4.211","1","4","0","0"], + ["13","3.866","1","4","0","0"], + ["14","3.538","1","4","0","0"], + ["15","3.21","1","4","0","0"], + ["16","2.899","1","4","0","0"], + ["17","2.588","1","4","0","0"], + ["18","2.296","1","4","0","0"], + ["19","2.004","1","4","0","0"], + ["20","1.72","1","4","0","0"], + ["21","1.436","1","4","0","0"], + ["22","1.055","1","4","0","0"], + ["23","0.674","1","4","0","0"], + ["24","0.337","1","4","0","0"], + ["25","0.00","1","4","0","255"], + ["26","5.283","2","2","0","0"], + ["27","4.568","2","2","0","0"], + ["28","3.853","2","2","0","0"], + ["29","3.028","2","2","0","0"], + ["30","2.654","2","2","0","0"], + ["31","2.278","2","2","0","0"], + ["32","1.933","2","2","0","0"], + ["33","1.605","2","2","0","0"], + ["34","1.294","2","2","0","0"], + ["35","1.002","2","2","0","0"], + ["36","0.718","2","2","0","0"], + ["37","0.337","2","2","0","0"], + ["38","0.0","2","2","0","255"] + ]} +} diff --git a/tests/unit/data/fiz/et42_v2/112e-b.json b/tests/unit/data/fiz/et42_v2/112e-b.json new file mode 100644 index 000000000..4c6ec9ede --- /dev/null +++ b/tests/unit/data/fiz/et42_v2/112e-b.json @@ -0,0 +1,77 @@ +{ + "_source": "pkp/et42_v2/112e-b.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "82000", "Vmax": "100", "PWR": "2240", "SandCap": "850", "Type": "et42"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "15.43", "H": "4.62", "W": "2.95", "Cx": "0.3"}, + "Wheels": {"D": "1.250", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "3.05", "Bd": "8.55", "Rmin": "125", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.0", "MaxASBP": "1.5", "BCN": "4", "BCR": "0.127", "BCD": "0.12", "BCM": "5.76", "Vv": "1.2", "MinCP": "7.5", "MaxCP": "9.0", "BCS": "1", "BSA": "0.5", "BM": "P10-Bg", "BVV": "300", "CompressorSpeed": "0.0392", "CompressorPower": "Main", "BRE": "0.7"}, + "BuffCoupl1": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-127"}, + "BuffCoupl2": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "99"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BrakeDelays": "GP", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "ManualBrake": "Yes", "DynamicBrake": "Switch", "MCPN": "38", "SCPN": "5", "AutoRelay": "No", "SCIM": "255", "IniCDelay": "0.5", "SCDelay": "0.1", "rows": [ + ["-1","0.60","-1","15","Pneumatic"], + ["0","0.50","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.28","-1","9","Pneumatic"], + ["6","0.00","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "1", "MaxVoltage": "3600", "MaxCurrent": "1300", "MinH": "0.42", "MinV": "1900", "MaxH": "1.77", "CSW": "1.4", "PhysicalLayout": "1"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "25:86", "WindingRes": "0.5", "nmax": "1500"}, + "Switches": {"Pantograph": "Continuos", "MotorConnectors": "Toggle"}, + "MotorParamTable": {"rows": [ + ["0","21.86","205","142","150"], + ["1","21.49","244","139","187"], + ["2","21.48","299","138","234"], + ["3","19.77","320","136","297"], + ["4","23.75","581","126","333"], + ["5","24.47","768","123","412"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "650", "ImaxHi": "651", "IminLo": "415", "IminHi": "415"}, + "RList": {"Size": "38", "RVent": "Automatic", "RVentnmax": "3300", "RVentCutOff": "0.2", "rows": [ + ["0","0","0","0","0","0"], + ["1","10.566","1","4","0","5"], + ["2","10.566","1","4","0","3"], + ["3","10.566","1","4","0","0"], + ["4","9.136","1","4","0","0"], + ["5","7.706","1","4","0","0"], + ["6","6.881","1","4","0","0"], + ["7","6.056","1","4","0","0"], + ["8","5.682","1","4","0","0"], + ["9","5.308","1","4","0","0"], + ["10","4.932","1","4","0","0"], + ["11","4.556","1","4","0","0"], + ["12","4.211","1","4","0","0"], + ["13","3.866","1","4","0","0"], + ["14","3.538","1","4","0","0"], + ["15","3.21","1","4","0","0"], + ["16","2.899","1","4","0","0"], + ["17","2.588","1","4","0","0"], + ["18","2.296","1","4","0","0"], + ["19","2.004","1","4","0","0"], + ["20","1.72","1","4","0","0"], + ["21","1.436","1","4","0","0"], + ["22","1.055","1","4","0","0"], + ["23","0.674","1","4","0","0"], + ["24","0.337","1","4","0","0"], + ["25","0.00","1","4","0","255"], + ["26","5.283","2","2","0","0"], + ["27","4.568","2","2","0","0"], + ["28","3.853","2","2","0","0"], + ["29","3.028","2","2","0","0"], + ["30","2.654","2","2","0","0"], + ["31","2.278","2","2","0","0"], + ["32","1.933","2","2","0","0"], + ["33","1.605","2","2","0","0"], + ["34","1.294","2","2","0","0"], + ["35","1.002","2","2","0","0"], + ["36","0.718","2","2","0","0"], + ["37","0.337","2","2","0","0"], + ["38","0.0","2","2","0","255"] + ]} +} diff --git a/tests/unit/data/fiz/eu04_v1/eu04-01.json b/tests/unit/data/fiz/eu04_v1/eu04-01.json new file mode 100644 index 000000000..bf5354005 --- /dev/null +++ b/tests/unit/data/fiz/eu04_v1/eu04-01.json @@ -0,0 +1,67 @@ +{ + "_source": "pkp/eu04_v1/eu04-01.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "87200", "Mred": "12000", "Vmax": "110", "PWR": "1680", "SandCap": "500", "type": "EU04"}, + "Dimensions": {"L": "16.238", "H": "4.343", "W": "2.903", "Cx": "0.3"}, + "Wheels": {"D": "1.350", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "3.05", "Bd": "8.55", "Rmin": "140", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "K", "NBpA": "4", "MBF": "85.0", "MaxBP": "2.8", "RM": "0.43", "LocMaxBP": "3.5", "BCN": "8", "BCR": "0.1524", "BCD": "0.12", "BCM": "8.7", "HiPP": "5", "LoPP": "3.5", "Vv": "0.83", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.03", "BCS": "0.8", "BSA": "0", "BM": "P10-Bgu", "BVV": "200", "BRE": "0.8"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "103"}, + "Cntrl": {"BrakeDelays": "GPRR", "BrakeHandle": "Knorr", "LocBrakeHandle": "Knorr", "BrakeSystem": "Pneumatic", "BCPN": "4", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ManualBrake": "Yes", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "DynamicBrake": "Passive", "MCPN": "31", "SCPN": "8", "AutoRelay": "Optional", "IniCDelay": "0.4", "SCDelay": "0.4", "rows": [ + ["-1","0.90","0","15","Pneumatic"], + ["0","0.50","0","9","Pneumatic"], + ["1","-1","-1","0","Pneumatic"], + ["2","-1","-1","5","Pneumatic"], + ["3","0.001","-1","9","Pneumatic"], + ["4","0.00","-1","15","Pneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "3600", "MaxCurrent": "1000", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "3"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "21:81", "WindingRes": "0.1411", "nmax": "2075"}, + "MotorParamTable0": {"rows": [ + ["0","23.9","200","-0.08","155.9","200","-0.08","1"], + ["1","23.28","200","-0.07","151.8","200","-0.07","1"], + ["2","22.45","200","-0.06","146.5","200","-0.06","1"], + ["3","22.18","200","0","144.7","200","0","1"], + ["4","23.09","300","-0.05","150.6","300","-0.05","1"], + ["5","22.89","290","-0.02","149.3","290","-0.02","1"], + ["6","23.59","360","-0.02","153.9","360","-0.02","1"], + ["7","23.95","410","0.01","156.2","410","0.01","1"], + ["8","21.94","410","0.05","143.1","410","0.05","0"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "650", "ImaxHi": "650", "IminLo": "350", "IminHi": "450"}, + "RList": {"Size": "31", "RVent": "Automatic", "RVentnmax": "3300", "RVentCutOff": "50", "rows": [ + ["0","0","0","0","0"], + ["1","16.9","1","4","1"], + ["2","12.4","1","4","1"], + ["3","9.77","1","4","1"], + ["4","8.0","1","4","1"], + ["5","6.4","1","4","1"], + ["6","4.94","1","4","1"], + ["7","4.38","1","4","1"], + ["8","3.9","1","4","1"], + ["9","3.37","1","4","1"], + ["10","2.86","1","4","1"], + ["11","2.5","1","4","1"], + ["12","2.1","1","4","1"], + ["13","1.78","1","4","1"], + ["14","1.49","1","4","1"], + ["15","1.18","1","4","1"], + ["16","0.89","1","4","1"], + ["17","0.66","1","4","1"], + ["18","0.42","1","4","1"], + ["19","0.2","1","4","1"], + ["20","0.0","1","4","1"], + ["21","6.09","2","2","1"], + ["22","3.9","2","2","1"], + ["23","3.1","2","2","1"], + ["24","2.57","2","2","1"], + ["25","2.185","2","2","1"], + ["26","1.58","2","2","1"], + ["27","1.115","2","2","1"], + ["28","0.795","2","2","1"], + ["29","0.5","2","2","1"], + ["30","0.23","2","2","1"], + ["31","0.0","2","2","0"] + ]} +} diff --git a/tests/unit/data/fiz/ew58_v1/ew58-d.json b/tests/unit/data/fiz/ew58_v1/ew58-d.json new file mode 100644 index 000000000..67a381d36 --- /dev/null +++ b/tests/unit/data/fiz/ew58_v1/ew58-d.json @@ -0,0 +1,20 @@ +{ + "_source": "pkp/ew58_v1/ew58-d.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "42200", "Mred": "1800", "Vmax": "120", "PWR": "2", "SandCap": "0", "Type": "ezt", "HeatingP": "18"}, + "Load": {"MaxLoad": "220", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "6", "UnLoadSpeed": "6"}, + "Dimensions": {"L": "21.7", "H": "4.12", "W": "2.96", "Cx": "0.1"}, + "Wheels": {"D": "0.92", "AIM": "2000", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.75", "Bd": "15.6", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EP1", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.8", "BCN": "4", "BCR": "0.1143", "BCD": "0.08", "BCM": "12.156", "HiPP": "5", "LoPP": "3.5", "Vv": "0.3", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.013", "TareMaxBP": "2.4", "BVV": "150", "BM": "P10-Bg", "BCS": "1.5", "BSA": "16.00", "BRE": "0.83"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.35", "CloseSpeed": "0.35", "DoorCloseDelay": "0.5", "DoorMaxShiftL": "0.53", "DoorMaxShiftR": "0.53", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes"}, + "BuffCoupl": {"ControlType": "ew58", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-127"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "MaxBPMass": "68", "BCPN": "1", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "ManualBrake", "DynamicBrake": "Passive", "MCPN": "4", "SCPN": "9", "SCIM": "1", "AutoRelay": "Yes", "IniCDelay": "0.0", "SCDelay": "0.8", "CoupledCtrl": "No", "BrakeDelays": "R", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","-1","-1","0","ElectroPneumatic"], + ["1","-1","0.35","4","ElectroPneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "3600", "MaxCurrent": "1200", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4", "MinPress": "3.5", "PhysicalLayout": "3"}, + "DCEMUED": {"CouplerCheck": "3", "EP_max_Vel": "48"}, + "Switches": {"Pantograph": "Continous"} +} diff --git a/tests/unit/data/fiz/ew58_v1/ew58-sa.json b/tests/unit/data/fiz/ew58_v1/ew58-sa.json new file mode 100644 index 000000000..95cc27446 --- /dev/null +++ b/tests/unit/data/fiz/ew58_v1/ew58-sa.json @@ -0,0 +1,68 @@ +{ + "_source": "pkp/ew58_v1/ew58-sa.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "52400", "Mred": "4980", "Vmax": "120", "PWR": "820", "SandCap": "500", "Type": "ezt", "HeatingP": "21"}, + "Load": {"MaxLoad": "220", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "6", "UnLoadSpeed": "6"}, + "Dimensions": {"L": "21.55", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "1.0", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EP1", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.8", "BCN": "4", "BCR": "0.1143", "BCD": "0.08", "BCM": "12.156", "HiPP": "5", "LoPP": "3.5", "TareMaxBP": "2.9", "BVV": "150", "BM": "P10-Bg", "BCS": "1.5", "BSA": "16.00", "BRE": "0.83"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.35", "CloseSpeed": "0.35", "DoorCloseDelay": "0.5", "DoorMaxShiftL": "0.53", "DoorMaxShiftR": "0.53", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes"}, + "BuffCoupl1": {"ControlType": "ew58", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39"}, + "BuffCoupl2": {"ControlType": "ew58", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-127"}, + "Cntrl": {"BrakeHandle": "FVE408", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "68", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "ManualBrake", "MCPN": "4", "SCPN": "9", "DBAM": "2", "IniCDelay": "0.0", "SCDelay": "0.3", "CoupledCtrl": "No", "SCIM": "1", "AutoRelay": "Yes", "DynamicBrake": "Automatic", "ConverterStart": "Disabled", "BatteryStart": "Disabled", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Security": {"AwareSystem": "CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "0", "MaxVoltage": "3750", "MaxCurrent": "800"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "750", "Trans": "18:69", "WindingRes": "0.1097", "nmax": "2390"}, + "Blending": {"MED_amax": "1.00"}, + "DCEMUED": {"CouplerCheck": "2", "EP_min_Im": "80"}, + "Switches": {"Pantograph": "Continous", "RelayResetButton1": "101"}, + "MotorParamTable0": {"rows": [ + ["0","11.3","90","0.21","79.5","90","0.21","1"], + ["1","12.39","110","0.24","83.9","110","0.24","1"], + ["2","11.88","123","0.23","80.5","123","0.23","1"], + ["3","11.37","135","0.22","77.2","135","0.22","1"], + ["4","11.05","180","0.2","74.6","180","0.2","1"], + ["5","10.73","225","0.18","72.1","225","0.18","1"], + ["6","10.26","250","0.18","68.7","250","0.18","1"], + ["7","9.8","275","0.19","65.4","275","0.19","1"], + ["8","9.34","300","0.19","62","300","0.19","1"], + ["9","8.15","455","0.1","55.5","455","0.1","0"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "580", "ImaxHi": "581", "IminLo": "360", "IminHi": "455", "TUHEX_Sum1": "365", "TUHEX_Sum2": "500", "TUHEX_Sum3": "610", "TUHEX_Diff": "10", "TUHEX_MaxIw": "280", "TUHEX_MinIw": "11", "TUHEX_Stages": "3"}, + "RList": {"Size": "22", "RVent": "No", "DynBrakeRes1": "4.3", "DynBrakeRes2": "1.956", "rows": [ + ["0","0","0","0","0","0"], + ["1","17.001","1","4","0","0"], + ["2","10.677","1","4","1","0"], + ["2","7.573","1","4","1","0"], + ["2","5.505","1","4","1","0"], + ["2","4.477","1","4","1","0"], + ["2","3.589","1","4","1","0"], + ["2","2.821","1","4","1","0"], + ["2","2.157","1","4","1","0"], + ["2","1.585","1","4","1","0"], + ["2","1.116","1","4","1","0"], + ["2","0.688","1","4","1","0"], + ["2","0.320","1","4","1","0"], + ["2","0.000","1","4","1","0"], + ["3","0.000","1","4","1","1"], + ["3","0.000","1","4","1","2"], + ["3","0.000","1","4","1","3"], + ["3","0.000","1","4","1","4"], + ["3","0.000","1","4","1","5"], + ["4","0.000","1","4","1","6"], + ["4","0.000","1","4","1","7"], + ["4","0.000","1","4","1","8"], + ["4","0.000","1","4","1","9"] + ]} +} diff --git a/tests/unit/data/fiz/ew58_v1/ew58-sb.json b/tests/unit/data/fiz/ew58_v1/ew58-sb.json new file mode 100644 index 000000000..f08052daa --- /dev/null +++ b/tests/unit/data/fiz/ew58_v1/ew58-sb.json @@ -0,0 +1,68 @@ +{ + "_source": "pkp/ew58_v1/ew58-sb.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "52400", "Mred": "4980", "Vmax": "120", "PWR": "820", "SandCap": "500", "Type": "ezt", "HeatingP": "21"}, + "Load": {"MaxLoad": "220", "LoadQ": "pieces", "LoadAccepted": "Passengers,Pantstate", "OverLoadFactor": "2", "LoadSpeed": "6", "UnLoadSpeed": "6"}, + "Dimensions": {"L": "21.55", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "1.0", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EP1", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.8", "BCN": "4", "BCR": "0.1143", "BCD": "0.08", "BCM": "12.156", "HiPP": "5", "LoPP": "3.5", "TareMaxBP": "2.5", "BVV": "150", "BM": "P10-Bg", "BCS": "1.5", "BSA": "16.00", "BRE": "0.83"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.35", "CloseSpeed": "0.35", "DoorCloseDelay": "0.5", "DoorMaxShiftL": "0.53", "DoorMaxShiftR": "0.53", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes"}, + "BuffCoupl1": {"CControlType": "ew58", "Type": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "-127"}, + "BuffCoupl2": {"ControlType": "ew58", "CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39"}, + "Cntrl": {"BrakeHandle": "FVE408", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "68", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "ManualBrake", "MCPN": "4", "SCPN": "9", "DBAM": "2", "IniCDelay": "0.0", "SCDelay": "0.3", "CoupledCtrl": "No", "SCIM": "1", "AutoRelay": "Yes", "DynamicBrake": "Automatic", "ConverterStart": "Disabled", "BatteryStart": "Disabled", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Security": {"AwareSystem": "CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Clima": {"Heating": "Main"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "0", "MaxVoltage": "3750", "MaxCurrent": "800"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "750", "Trans": "18:69", "WindingRes": "0.1097", "nmax": "2390"}, + "Blending": {"MED_amax": "1.00"}, + "DCEMUED": {"CouplerCheck": "1", "EP_min_Im": "80"}, + "Switches": {"Pantograph": "Continous", "RelayResetButton1": "101"}, + "MotorParamTable0": {"rows": [ + ["0","11.3","90","0.21","79.5","90","0.21","1"], + ["1","12.39","110","0.24","83.9","110","0.24","1"], + ["2","11.88","123","0.23","80.5","123","0.23","1"], + ["3","11.37","135","0.22","77.2","135","0.22","1"], + ["4","11.05","180","0.2","74.6","180","0.2","1"], + ["5","10.73","225","0.18","72.1","225","0.18","1"], + ["6","10.26","250","0.18","68.7","250","0.18","1"], + ["7","9.8","275","0.19","65.4","275","0.19","1"], + ["8","9.34","300","0.19","62","300","0.19","1"], + ["9","8.15","455","0.1","55.5","455","0.1","0"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "580", "ImaxHi": "581", "IminLo": "360", "IminHi": "455", "TUHEX_Sum1": "365", "TUHEX_Sum2": "500", "TUHEX_Sum3": "610", "TUHEX_Diff": "10", "TUHEX_MaxIw": "280", "TUHEX_MinIw": "11", "TUHEX_Stages": "3"}, + "RList": {"Size": "22", "RVent": "No", "DynBrakeRes1": "4.3", "DynBrakeRes2": "1.956", "rows": [ + ["0","0","0","0","0","0"], + ["1","17.001","1","4","0","0"], + ["2","10.677","1","4","1","0"], + ["2","7.573","1","4","1","0"], + ["2","5.505","1","4","1","0"], + ["2","4.477","1","4","1","0"], + ["2","3.589","1","4","1","0"], + ["2","2.821","1","4","1","0"], + ["2","2.157","1","4","1","0"], + ["2","1.585","1","4","1","0"], + ["2","1.116","1","4","1","0"], + ["2","0.688","1","4","1","0"], + ["2","0.320","1","4","1","0"], + ["2","0.000","1","4","1","0"], + ["3","0.000","1","4","1","1"], + ["3","0.000","1","4","1","2"], + ["3","0.000","1","4","1","3"], + ["3","0.000","1","4","1","4"], + ["3","0.000","1","4","1","5"], + ["4","0.000","1","4","1","6"], + ["4","0.000","1","4","1","7"], + ["4","0.000","1","4","1","8"], + ["4","0.000","1","4","1","9"] + ]} +} diff --git a/tests/unit/data/fiz/impuls_v1/31we-a_pr.json b/tests/unit/data/fiz/impuls_v1/31we-a_pr.json new file mode 100644 index 000000000..368c0f24f --- /dev/null +++ b/tests/unit/data/fiz/impuls_v1/31we-a_pr.json @@ -0,0 +1,50 @@ +{ + "_source": "pkp/impuls_v1/31we-a_pr.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "31800", "Vmax": "160", "PWR": "1000", "SandCap": "200", "Type": "ezt"}, + "Load": {"MaxLoad": "42", "LoadQ": "pieces", "LoadAccepted": "Passengers", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "1"}, + "Dimensions": {"L": "21.06", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.840", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.37", "MaxLBP": "4.37", "BCN": "4", "BCR": "0.127", "BCD": "0.05", "BCM": "1.9075", "HiPP": "5", "LoPP": "3.5", "Vv": "0.125", "MinCP": "7.5", "MaxCP": "9.0", "MinCP_B": "8.5", "MaxCP_B": "10.0", "CompressorSpeed": "0.00625", "BVV": "125", "BM": "Cosid", "BCS": "1", "BSA": "0.00", "BRE": "0.95", "TareMaxBP": "3.85", "CompressorPower": "Converter", "LPOn": "2.8", "LPOff": "4.3", "UBB1": "2", "EmergencyCutsOffHandle": "Yes"}, + "SpringBrake": {"Volume": "10", "MBF": "2.9", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "4.2", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.21", "CloseSpeed": "0.19", "DoorCloseDelay": "2.1", "DoorOpenWithPermit": "1", "DoorMaxShiftL": "0.7", "DoorMaxShiftR": "0.7", "DoorOpenMethod": "Plug", "DoorMaxShiftPlug": "0.028", "DoorStayOpen": "5.0", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorAutoCloseRemote": "Yes", "PlatformMaxShift": "0.270", "PlatformSpeed": "0.4", "PlatformOpenMethod": "Shift", "MirrorMaxShift": "45", "DoorNeedPermit": "Yes", "MirrorVelClose": "20", "DoorsPermitLightBlinking": "2"}, + "BuffCoupl1": {"CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39", "ControlType": "31we_pr"}, + "BuffCoupl2": {"CType": "Articulated", "AllowedFlag": "-255", "ControlType": "impuls"}, + "Cntrl": {"BrakeHandle": "MHZ_6P", "LocBrakeHandle": "FD1", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "37.8", "BCPN": "4", "BDelay1": "15", "BDelay2": "4", "LocalBrake": "PneumaticBrake", "MCPN": "10", "SCPN": "1", "IniCDelay": "0.0", "SCDelay": "0.8", "CoupledCtrl": "No", "EIMCtrlAddZeros": "Yes", "SBD": "0.65", "ASB": "Yes", "ConverterStart": "Automatic", "ConverterStartDelay": "8", "MainInitTime": "2", "BrakeOpModes": "PN", "MotorBlowersStart": "Converter", "EIMCtrlEmergency": "Yes", "PantEPValveStart": "Automatic", "PantEPValveSpring": "No", "SpringBrakeCutsOffDrive": "No", "SpringBrakeDriveEmergencyVel": "2", "ReleaseParkingBySpringBrakeWhenDoorIsOpen": "Yes", "CompartmentLightsStart": "Manual", "HAO": "Yes", "OMP": "0.8", "AutomaticCabActivation": "No", "InactivaCabEmergencyBrake": "Yes", "InactiveCabFlag": "239", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "2.1", "CabDependent": "Yes", "RadioStop": "Yes", "MagnetLocation": "4.5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "24", "LMaxCurrent": "300"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "0", "MaxVoltage": "3900", "MinV": "2100", "MaxCurrent": "1200", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4"}, + "Engine": {"EngineType": "ElectricInductionMotor", "InvNo": "2", "Trans": "60:252", "dfic": "794", "dfmax": "1.500", "p": "2", "cfu": "40.1", "cim": "12.24", "icif": "0.679", "Uzmax": "2200", "Uzh": "1500", "DU": "20", "I0": "10", "fcfu": "47.6", "fcfuh": "31.4", "F0": "100", "a1": "0", "Pmax": "1260", "Fh": "48", "Ph": "800", "Vh0": "5", "Vh1": "15", "Imax": "475", "abed": "0", "edep": "3.0", "eimclf": "Yes", "MotorBlowersSpeed": "-15", "MotorBlowersSustainTime": "300", "MotorBlowersStartVelocity": "0.1"}, + "SpeedControl": {"SpeedCtrl": "Yes", "SpeedButtons": "10|20|40|60|80|100|120|140|160", "OverrideManual": "No", "InitPwr": "1.0", "MaxPwrVel": "-1", "StartVel": "-1", "VelStep": "10", "PwrStep": "0.0", "MinPwr": "0.2", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "160", "Offset": "-0.5", "kPpos": "0.2", "kPneg": "0.4", "kIpos": "0.00", "kIneg": "0.00", "BrakeIntervention": "No", "SpeedCtrlATOF": "2"}, + "AI": {"Pantstate": "2", "LocalBrakeAccFactor": "0.5", "IdlePantUp": "No"}, + "Blending": {"MED_amax": "1.08", "MED_EPVC": "No", "MED_Ncor": "No", "MED_FrEDFactor": "0", "MED_FirstDelayED": "0.5", "MED_ScndDelayED": "0.25"}, + "Switches": {"PantographPresets": "0|1|2|3"}, + "Circuit": {"ImaxHi": "500"}, + "LightsList": {"Size": "8", "Wrap": "No", "Default": "1", "rows": [ + ["0","0"], + ["388","34"], + ["21","34"], + ["1","16"], + ["16","1"], + ["22","34"], + ["38","38"], + ["34","34"] + ]}, + "ffList": {"Size": "5", "rows": [ + ["0","0.5"], + ["24","1.0"], + ["35","1.0"], + ["42","1.3"], + ["42","1.3"], + ["74","2.3"] + ]} +} diff --git a/tests/unit/data/fiz/impuls_v1/31we-b_pr.json b/tests/unit/data/fiz/impuls_v1/31we-b_pr.json new file mode 100644 index 000000000..60138291f --- /dev/null +++ b/tests/unit/data/fiz/impuls_v1/31we-b_pr.json @@ -0,0 +1,20 @@ +{ + "_source": "pkp/impuls_v1/31we-b_pr.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "38700", "Vmax": "160", "PWR": "2", "SandCap": "0", "Type": "ezt"}, + "Load": {"MaxLoad": "48", "LoadQ": "pieces", "LoadAccepted": "Passengers", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "15.92", "H": "4.12", "W": "2.96", "Cx": "0.1"}, + "Wheels": {"D": "0.84", "AIM": "500", "Tw": "1.435", "Axle": "2'1'", "Ad": "2.75", "Bd": "15.92", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "2", "MBF": "85.0", "MaxBP": "4.06", "MaxLBP": "4.06", "BCN": "6", "BCR": "0.127", "BCD": "0.05", "BCM": "1.9075", "HiPP": "5", "LoPP": "3.5", "Vv": "0.01", "MinCP": "10.0", "MaxCP": "12.0", "CompressorSpeed": "0.0000001", "BVV": "187", "BM": "Cosid", "BCS": "1", "BSA": "0.00", "BRE": "0.95", "TareMaxBP": "3.09", "CompressorPower": "Converter"}, + "SpringBrake": {"Volume": "10", "MBF": "2.9", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.8", "PressOn": "4.5", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.21", "CloseSpeed": "0.19", "DoorCloseDelay": "2.1", "DoorOpenWithPermit": "1", "DoorMaxShiftL": "0.7", "DoorMaxShiftR": "0.7", "DoorOpenMethod": "Plug", "DoorMaxShiftPlug": "0.028", "DoorStayOpen": "5.0", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorAutoCloseRemote": "Yes", "PlatformMaxShift": "0.270", "PlatformSpeed": "0.4", "PlatformOpenMethod": "Shift", "MirrorMaxShift": "45", "DoorNeedPermit": "Yes"}, + "BuffCoupl": {"CType": "Articulated", "AllowedFlag": "-255", "ControlType": "impuls"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocBrakeHandle": "FD1", "MaxBPMass": "53", "BCPN": "2", "BDelay1": "15", "BDelay2": "4", "LocalBrake": "PneumaticBrake", "DynamicBrake": "Passive", "MCPN": "10", "SCPN": "0", "AutoRelay": "Yes", "IniCDelay": "0.0", "SCDelay": "0.8", "CoupledCtrl": "Yes", "ASB": "Yes", "CompartmentLightsStart": "Manual", "HAO": "Yes", "OMP": "0.8", "PantEPValveStart": "Automatic", "PantEPValveSpring": "No", "PantCompressorStart": "Automatic", "PantAutoValve": "Yes", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","-1","-1","0","ElectroPneumatic"], + ["1","-1","0.35","4","ElectroPneumatic"], + ["2","-1","-1","0","ElectroPneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "24", "LMaxCurrent": "300"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "1", "MaxVoltage": "3900", "MinV": "2100", "MaxCurrent": "1200", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "1"} +} diff --git a/tests/unit/data/fiz/impuls_v1/31we-c_pr.json b/tests/unit/data/fiz/impuls_v1/31we-c_pr.json new file mode 100644 index 000000000..0272b3e24 --- /dev/null +++ b/tests/unit/data/fiz/impuls_v1/31we-c_pr.json @@ -0,0 +1,20 @@ +{ + "_source": "pkp/impuls_v1/31we-c_pr.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "38700", "Vmax": "160", "PWR": "2", "SandCap": "0", "Type": "ezt"}, + "Load": {"MaxLoad": "46", "LoadQ": "pieces", "LoadAccepted": "Passengers", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "15.92", "H": "4.12", "W": "2.96", "Cx": "0.1"}, + "Wheels": {"D": "0.84", "AIM": "500", "Tw": "1.435", "Axle": "1'2'", "Ad": "2.75", "Bd": "15.92", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "2", "MBF": "85.0", "MaxBP": "4.06", "MaxLBP": "4.06", "BCN": "6", "BCR": "0.127", "BCD": "0.05", "BCM": "1.9075", "HiPP": "5", "LoPP": "3.5", "Vv": "0.01", "MinCP": "10.0", "MaxCP": "12.0", "CompressorSpeed": "0.0000001", "BVV": "187", "BM": "Cosid", "BCS": "1", "BSA": "0.00", "BRE": "0.95", "TareMaxBP": "3.09", "CompressorPower": "Converter"}, + "SpringBrake": {"Volume": "10", "MBF": "2.9", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.8", "PressOn": "4.5", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.21", "CloseSpeed": "0.19", "DoorCloseDelay": "2.1", "DoorOpenWithPermit": "1", "DoorMaxShiftL": "0.7", "DoorMaxShiftR": "0.7", "DoorOpenMethod": "Plug", "DoorMaxShiftPlug": "0.028", "DoorStayOpen": "5.0", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorAutoCloseRemote": "Yes", "PlatformMaxShift": "0.270", "PlatformSpeed": "0.4", "PlatformOpenMethod": "Shift", "MirrorMaxShift": "45", "DoorNeedPermit": "Yes"}, + "BuffCoupl": {"CType": "Articulated", "AllowedFlag": "-255", "ControlType": "impuls"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocBrakeHandle": "FD1", "MaxBPMass": "53", "BCPN": "2", "BDelay1": "15", "BDelay2": "4", "LocalBrake": "PneumaticBrake", "DynamicBrake": "Passive", "MCPN": "10", "SCPN": "0", "AutoRelay": "Yes", "IniCDelay": "0.0", "SCDelay": "0.8", "CoupledCtrl": "Yes", "ASB": "Yes", "CompartmentLightsStart": "Manual", "HAO": "Yes", "OMP": "0.8", "PantEPValveStart": "Automatic", "PantEPValveSpring": "No", "PantCompressorStart": "Automatic", "PantAutoValve": "Yes", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","-1","-1","0","ElectroPneumatic"], + ["1","-1","0.35","4","ElectroPneumatic"], + ["2","-1","-1","0","ElectroPneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "24", "LMaxCurrent": "300"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "1", "MaxVoltage": "3900", "MinV": "2100", "MaxCurrent": "1200", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4", "PhysicalLayout": "2"} +} diff --git a/tests/unit/data/fiz/impuls_v1/31we-d_pr.json b/tests/unit/data/fiz/impuls_v1/31we-d_pr.json new file mode 100644 index 000000000..d78930b9b --- /dev/null +++ b/tests/unit/data/fiz/impuls_v1/31we-d_pr.json @@ -0,0 +1,50 @@ +{ + "_source": "pkp/impuls_v1/31we-d_pr.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "31800", "Vmax": "160", "PWR": "1000", "SandCap": "200", "Type": "ezt"}, + "Load": {"MaxLoad": "54", "LoadQ": "pieces", "LoadAccepted": "Passengers", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "1"}, + "Dimensions": {"L": "21.06", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "0.840", "AIM": "2000", "Tw": "1.435", "Axle": "Bo'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.37", "MaxLBP": "4.37", "BCN": "4", "BCR": "0.127", "BCD": "0.05", "BCM": "1.9075", "HiPP": "5", "LoPP": "3.5", "Vv": "0.125", "MinCP": "8.5", "MaxCP": "10.0", "MinCP_B": "7.5", "MaxCP_B": "9.0", "CompressorSpeed": "0.00625", "BVV": "125", "BM": "Cosid", "BCS": "1", "BSA": "0.00", "BRE": "0.95", "TareMaxBP": "3.85", "CompressorPower": "Converter", "LPOn": "2.8", "LPOff": "4.3", "UBB1": "2", "EmergencyCutsOffHandle": "Yes"}, + "SpringBrake": {"Volume": "10", "MBF": "2.9", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "4.2", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.21", "CloseSpeed": "0.19", "DoorCloseDelay": "2.1", "DoorOpenWithPermit": "1", "DoorMaxShiftL": "0.7", "DoorMaxShiftR": "0.7", "DoorOpenMethod": "Plug", "DoorMaxShiftPlug": "0.028", "DoorStayOpen": "5.0", "DoorClosureWarningAuto": "Yes", "DoorBlocked": "Yes", "DoorAutoCloseRemote": "Yes", "PlatformMaxShift": "0.270", "PlatformSpeed": "0.4", "PlatformOpenMethod": "Shift", "MirrorMaxShift": "45", "DoorNeedPermit": "Yes", "MirrorVelClose": "20", "DoorsPermitLightBlinking": "2"}, + "BuffCoupl1": {"CType": "Articulated", "AllowedFlag": "-255", "ControlType": "impuls"}, + "BuffCoupl2": {"CType": "Automatic", "kB": "4200", "DmaxB": "0.05", "FmaxB": "1740", "kC": "6800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "AutomaticFlag": "39", "ControlType": "31we_pr"}, + "Cntrl": {"BrakeHandle": "MHZ_6P", "LocBrakeHandle": "FD1", "BrakeSystem": "ElectroPneumatic", "MaxBPMass": "37.8", "BCPN": "4", "BDelay1": "15", "BDelay2": "4", "LocalBrake": "PneumaticBrake", "MCPN": "10", "SCPN": "1", "IniCDelay": "0.0", "SCDelay": "0.8", "CoupledCtrl": "No", "EIMCtrlAddZeros": "Yes", "SBD": "0.65", "ASB": "Yes", "ConverterStart": "Automatic", "ConverterStartDelay": "8", "MainInitTime": "2", "BrakeOpModes": "PN", "MotorBlowersStart": "Converter", "EIMCtrlEmergency": "Yes", "PantEPValveStart": "Automatic", "PantEPValveSpring": "No", "SpringBrakeCutsOffDrive": "No", "SpringBrakeDriveEmergencyVel": "2", "ReleaseParkingBySpringBrakeWhenDoorIsOpen": "Yes", "CompartmentLightsStart": "Manual", "HAO": "Yes", "OMP": "0.8", "AutomaticCabActivation": "No", "InactivaCabEmergencyBrake": "Yes", "InactiveCabFlag": "239", "rows": [ + ["-1","0.5","0.0","6","ElectroPneumatic"], + ["0","0.5","-1","4","ElectroPneumatic"], + ["1","0.5","0.35","4","ElectroPneumatic"], + ["2","0.5","0.35","4","Pneumatic"], + ["3","0.5","0.35","4","Pneumatic"], + ["4","-1","0.35","4","Pneumatic"], + ["5","0.34","-1","4","Pneumatic"], + ["6","-1","-1","0","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "2.1", "CabDependent": "Yes", "RadioStop": "Yes", "MagnetLocation": "4.5"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "24", "LMaxCurrent": "300"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "0", "MaxVoltage": "3900", "MinV": "2100", "MaxCurrent": "1200", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4"}, + "Engine": {"EngineType": "ElectricInductionMotor", "InvNo": "2", "Trans": "60:252", "dfic": "794", "dfmax": "1.500", "p": "2", "cfu": "40.1", "cim": "12.24", "icif": "0.679", "Uzmax": "2200", "Uzh": "1500", "DU": "20", "I0": "10", "fcfu": "47.6", "fcfuh": "31.4", "F0": "100", "a1": "0", "Pmax": "1260", "Fh": "48", "Ph": "800", "Vh0": "5", "Vh1": "15", "Imax": "475", "abed": "0", "edep": "3.0", "eimclf": "Yes", "MotorBlowersSpeed": "-15", "MotorBlowersSustainTime": "300", "MotorBlowersStartVelocity": "0.1"}, + "SpeedControl": {"SpeedCtrl": "Yes", "SpeedButtons": "10|20|40|60|80|100|120|140|160", "OverrideManual": "No", "InitPwr": "1.0", "MaxPwrVel": "-1", "StartVel": "-1", "VelStep": "10", "PwrStep": "0.0", "MinPwr": "0.2", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "160", "Offset": "-0.5", "kPpos": "0.2", "kPneg": "0.4", "kIpos": "0.00", "kIneg": "0.00", "BrakeIntervention": "No", "SpeedCtrlATOF": "2"}, + "AI": {"Pantstate": "1", "LocalBrakeAccFactor": "0.5", "IdlePantUp": "No"}, + "Blending": {"MED_amax": "1.08", "MED_EPVC": "No", "MED_Ncor": "No", "MED_FrEDFactor": "0", "MED_FirstDelayED": "0.5", "MED_ScndDelayED": "0.25"}, + "Switches": {"PantographPresets": "0|1|2|3"}, + "Circuit": {"ImaxHi": "500"}, + "LightsList": {"Size": "8", "Wrap": "No", "Default": "1", "rows": [ + ["0","0"], + ["388","34"], + ["21","34"], + ["1","16"], + ["16","1"], + ["22","34"], + ["38","38"], + ["34","34"] + ]}, + "ffList": {"Size": "5", "rows": [ + ["0","0.5"], + ["24","1.0"], + ["35","1.0"], + ["42","1.3"], + ["42","1.3"], + ["74","2.3"] + ]} +} diff --git a/tests/unit/data/fiz/road/man/nl293.json b/tests/unit/data/fiz/road/man/nl293.json new file mode 100644 index 000000000..3d5e91a44 --- /dev/null +++ b/tests/unit/data/fiz/road/man/nl293.json @@ -0,0 +1,36 @@ +{ + "_source": "road/man/nl293.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "road", "M": "12000", "Vmax": "90", "PWR": "206"}, + "Load": {"LoadAccepted": "Passengers,Luggage", "MaxLoad": "44", "LoadQ": "pieces", "OverLoadFactor": "1.2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "10.5", "H": "3", "W": "2.6", "Cx": "0.4"}, + "Wheels": {"D": "1.0", "AIM": "300", "Tw": "2.3", "Axle": "1'A", "Bd": "4.615", "Rmin": "10.5"}, + "Brake": {"BrakeType": "Hydraulic", "NBpA": "2", "MBF": "14.75", "MaxBP": "0.5", "BCN": "4", "BCR": "0.2", "BCD": "0.2", "BCM": "2.0"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "30", "CloseSpeed": "38", "DoorMaxShiftL": "88", "DoorMaxShiftR": "88", "DoorOpenMethod": "Fold", "DoorClosureWarning": "Yes"}, + "BuffCoupl": {"CType": "Bare"}, + "Cntrl": {"BrakeSystem": "Individual", "BCPN": "10", "LocalBrake": "HydraulicBrake", "MCPN": "10", "SCPN": "5", "IniCDelay": "1.0", "SCDelay": "0.25", "AutoRelay": "Yes", "OilStart": "Automatic", "FuelStart": "Automatic", "rows": []}, + "Light": {"Light": "Accu", "LMaxVoltage": "12", "LCap": "340", "LRS": "Generator", "LMaxCurrent": "10"}, + "Power": {"EnginePower": "Internal"}, + "Engine": {"EngineType": "DieselEngine", "Trans": "10:54", "nmin": "600", "nmax": "2000", "nmax_cutoff": "1900", "AIM": "50", "EUS": "2.5", "EDS": "2.5", "IsTC": "Yes", "TC_TMMax": "1.52", "TC_CP": "0.85", "TC_LT": "1700", "TC_LR": "2.0", "TC_ULR": "2.0", "TC_FRI": "1", "TC_FRD": "1", "TC_TII": "1.63", "TC_TIO": "0", "TC_TOO": "0", "TC_LS": "33", "TC_ULS": "25.4", "IsRetarder": "Yes", "R_Place": "2", "R_TII": "2.0", "R_MT": "1000", "R_MP": "100", "R_FRI": "1", "R_FRD": "1", "R_MinVel": "30"}, + "MotorParamTable": {"minVelfullengage": "2.0", "engageDia": "1", "engageMaxForce": "1600", "engagefriction": "1", "rows": [ + ["0","0","0","-1","1"], + ["1","2.81","-1","18.8","1"], + ["2","1.84","15.2","28.6","1"], + ["3","1.36","25.4","38.8","1"], + ["4","1.00","34.6","52.7","1"], + ["5","0.80","43.2","200","1"] + ]}, + "DList": {"Size": "10", "nMmax": "20.83", "Mmax": "1400", "Mnmax": "1050", "nmax": "31.67", "nominalfill": "1", "Mstand": "150", "rows": [ + ["0","0.1","0"], + ["1","0.1","2"], + ["2","0.2","2"], + ["3","0.3","2"], + ["4","0.4","2"], + ["5","0.5","2"], + ["6","0.6","2"], + ["7","0.7","2"], + ["8","0.8","2"], + ["9","0.9","2"], + ["10","1.0","2"] + ]} +} diff --git a/tests/unit/data/fiz/sa134_v1/218mc_a.json b/tests/unit/data/fiz/sa134_v1/218mc_a.json new file mode 100644 index 000000000..6c91065df --- /dev/null +++ b/tests/unit/data/fiz/sa134_v1/218mc_a.json @@ -0,0 +1,94 @@ +{ + "_source": "pkp/sa134_v1/218mc_a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "41000", "Vmax": "120", "PWR": "382", "SandCap": "100", "Type": "dmu"}, + "Load": {"MaxLoad": "50", "OverLoadFactor": "2.2", "LoadQ": "pieces", "LoadAccepted": "passengers", "LoadSpeed": "0.5", "UnLoadSpeed": "0.7"}, + "Dimensions": {"L": "20.75", "H": "4.2", "W": "2.9", "Cx": "0.0"}, + "Wheels": {"D": "0.84", "Tw": "1.435", "AIM": "300", "Axle": "B'1'", "Ad": "2", "Bd": "17.23", "Rmin": "75", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "EStED", "Size": "150", "NBpA": "4", "MBF": "85.0", "MaxBP": "2.9", "LocMaxBP": "3.8", "RV": "-1", "RM": "0.77", "BCN": "6", "BCR": "0.127", "BCD": "0.1", "BCM": "2.15", "HiPP": "5", "LoPP": "3.5", "Vv": "0.50", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.0208", "CompressorPower": "Main", "BCS": "0.5", "BSA": "0", "BM": "Cosid", "BVV": "150", "BRE": "0.97", "TareMaxBP": "2.2", "LPOn": "3.0", "LPOff": "4.5", "UBB1": "2", "UBB2": "8"}, + "SpringBrake": {"Volume": "15", "MBF": "4.35", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "0.5", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "4"}, + "Doors": {"OpenCtrl": "Mixed", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.23", "CloseSpeed": "0.19", "DoorCloseDelay": "2.45", "DoorMaxShiftL": "0.78", "DoorMaxShiftR": "0.78", "DoorOpenMethod": "Plug", "DoorClosureWarning": "No", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.04", "MirrorMaxShift": "75", "DoorStayOpen": "10", "DoorNeedPermit": "Yes", "DoorPermitList": "1|0|2|3", "DoorPermitListDefault": "2", "DoorAutoCloseRemote": "No"}, + "BuffCoupl1": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "ControlType": "218M"}, + "BuffCoupl2": {"CType": "Articulated", "AllowedFlag": "-55", "ControlType": "218M"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "3", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Yes", "LocalBrake": "PneumaticBrake", "ManualBrake": "No", "MCPN": "5", "SCPN": "1", "AutoRelay": "Yes", "IniCDelay": "1.0", "SCDelay": "0.3", "BrakeDelays": "PR", "MaxBPMass": "55", "BrakeHandle": "MHZ_K5P", "LocBrakeHandle": "FD1", "WaterStart": "Battery", "OilStart": "Automatic", "FuelStart": "Automatic", "ConverterStart": "Automatic", "ConverterStartDelay": "4", "BrakeOpModes": "PN", "EIMCtrlType": "3", "DirChangeMaxPos": "4", "SBD": "0.47", "AutomaticCabActivation": "No", "InactiveCabFlag": "27", "CompartmentLightsStart": "Manual", "rows": [ + ["-1","0.6","-1","20","Pneumatic"], + ["0","0.5","-1","2","Pneumatic"], + ["1","0.46","-1","2","Pneumatic"], + ["2","0.42","-1","2","Pneumatic"], + ["3","0.38","-1","2","Pneumatic"], + ["4","0.34","-1","2","Pneumatic"], + ["5","0.29","-1","2","Pneumatic"], + ["6","0","-1","40","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "2.4", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "2.1", "RadioStop": "Yes", "CabDependent": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "24", "LMaxCurrent": "155"}, + "Engine": {"EngineType": "DieselEngine", "Trans": "69:229", "TransEff": "0.90", "nmin": "850", "nmax": "2100", "nmax_cutoff": "2150", "nmin_hdrive": "1000", "nmin_hdrive_factor": "800", "nmin_retarder": "1400", "MaxVelANS": "255", "AIM": "150", "EUS": "2.5", "EDS": "2.5", "IsTC": "Yes", "TC_TMMax": "4.23", "TC_CP": "0.80", "TC_LT": "3000", "TC_LR": "1", "TC_ULR": "1", "TC_FRI": "0.25", "TC_FRD": "1", "TC_TII": "1.97", "TC_TIO": "0", "TC_TOO": "0", "TC_LS": "80", "TC_ULS": "75", "IsRetarder": "Yes", "R_Place": "0", "R_TII": "18.0", "R_MT": "2000", "R_MP": "300", "R_FRI": "1", "R_FRD": "1", "R_MinVel": "16", "R_EngageVel": "95", "WaterMaxTemperature": "105", "WaterCoolingTemperature": "95", "WaterFlowTemperature": "75", "WaterAuxCircuit": "No", "WaterCoolingFanSpeed": "-100", "NominalCoolingPower": "300"}, + "Clima": {"HMaxVoltage": "3000", "Heating": "Generator", "HGeneratorEngine": "Main", "HGeneratorMinRPM": "696", "HGeneratorMinVoltage": "2400", "HGeneratorMaxRPM": "1500", "HGeneratorMaxVoltage": "3300"}, + "SpeedControl": {"SpeedCtrl": "Yes", "SpeedButtons": "0|30|40|50|60|70|80|90|100|120", "OverrideManual": "Yes", "InitPwr": "0.3", "MaxPwrVel": "5", "StartVel": "2", "VelStep": "5", "PwrStep": "0.1", "MinPwr": "0.3", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "120", "Offset": "-0.5", "kPpos": "0.1", "kPneg": "0.4", "kIpos": "0.06", "kIneg": "0.06", "BrakeIntervention": "Yes"}, + "TurboPos": {"TurboPos": "1"}, + "MotorParamTable": {"minVelfullengage": "-0.1", "engageDia": "1", "engageMaxForce": "3300", "engagefriction": "5.0", "rows": [ + ["0","0","0","-1","1"], + ["1","0.72","-1","200","1"] + ]}, + "DList": {"Size": "10", "Mmax": "2750", "nMmax": "18.3", "nmax": "33.3", "Mnmax": "2142", "nominalfill": "1.0", "Mstand": "250.0", "NomFuelConsRate": "220", "rows": [ + ["0","0.15","0"], + ["1","0.10","2"], + ["2","0.20","2"], + ["3","0.30","2"], + ["4","0.40","2"], + ["5","0.50","2"], + ["6","0.60","2"], + ["7","0.70","2"], + ["8","0.80","2"], + ["9","0.90","2"], + ["10","1.00","2"] + ]}, + "DMList": {"rows": [ + ["0","0"], + ["80","591"], + ["400","738"], + ["800","1477"], + ["1300","2400"], + ["1500","2400"], + ["1750","2071"], + ["2000","1824"], + ["2200","1508"] + ]}, + "HTCList": {"rows": [ + ["0.000","4.89"], + ["0.061","4.44"], + ["0.159","3.63"], + ["0.236","3.12"], + ["0.293","2.81"], + ["0.354","2.46"], + ["0.427","2.14"], + ["0.492","1.87"], + ["0.565","1.62"], + ["0.642","1.39"], + ["0.720","1.23"], + ["0.800","1.00"], + ["1.000","0.00"] + ]}, + "UCList": {"Size": "5", "IntegratedBrakePN": "Yes", "IntegratedBrake": "Yes", "rows": [ + ["0","3","0","0","0","0.15","0.15","0","1","0"], + ["1","2","0","0","0","0.15","0.15","1","3","0"], + ["2","1","-1","0","-1","0.15","0.15","3","3","1"], + ["3","1","-1","1","0","0.00","0.15","3","4","1"], + ["4","0","-1","1","0","0.15","0.00","4","5","3"], + ["5","0","0","1","1","0.15","0.15","4","5","3"] + ]}, + "LightsList": {"Size": "5", "Wrap": "no", "Default": "1", "rows": [ + ["0","0"], + ["21","34"], + ["22","34"], + ["34","34"], + ["16","1"] + ]}, + "WiperList": {"Size": "5", "Angle": "-75", "Default": "1", "rows": [ + ["3","0.5","0.5","0.0"], + ["0","0.8","0.0","0.5"], + ["3","0.8","6.0","0.5"], + ["3","0.8","3.0","0.0"], + ["3","0.8","1.0","0.0"] + ]} +} diff --git a/tests/unit/data/fiz/sieciowiec_v1/a.json b/tests/unit/data/fiz/sieciowiec_v1/a.json new file mode 100644 index 000000000..4471406d7 --- /dev/null +++ b/tests/unit/data/fiz/sieciowiec_v1/a.json @@ -0,0 +1,13 @@ +{ + "_source": "pkp/sieciowiec_v1/a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "35500", "Vmax": "80", "HeatingP": "20", "LightP": "0.56"}, + "Load": {"MaxLoad": "80", "LoadQ": "pieces", "LoadAccepted": "Passengers,Luggage", "OverLoadFactor": "2", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "21.13", "H": "4", "W": "2.9", "Cx": "0.1"}, + "Wheels": {"D": "1.00", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.83", "Bd": "13.9", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeType": "None", "NBpA": "0", "MBF": "0", "MaxBP": "0"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.11", "FmaxB": "320", "kC": "4500", "DmaxC": "0.1", "FmaxC": "850", "beta": "0.58", "AllowedFlag": "115"}, + "Cntrl": {"MCPN": "1", "BrakeSystem": "Individual", "LocalBrake": "NoBrake", "rows": []}, + "Light": {"Light": "Generator", "LGeneratorEngine": "WheelsDriven", "LMaxVoltage": "24", "AlterLight": "Accu", "AlterLMaxVoltage": "24", "AlterLCap": "495", "AlterLRS": "Generator"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000", "AlterHeating": "PowerCable", "AlterHPowerTrans": "SteamPower", "AlterHSteamPress": "0.45"} +} diff --git a/tests/unit/data/fiz/sm03_v1/sm03.json b/tests/unit/data/fiz/sm03_v1/sm03.json new file mode 100644 index 000000000..0b241f6b4 --- /dev/null +++ b/tests/unit/data/fiz/sm03_v1/sm03.json @@ -0,0 +1,41 @@ +{ + "_source": "pkp/sm03_v1/sm03.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "23200", "Vmax": "25.68", "PWR": "111.855", "SandCap": "200"}, + "Dimensions": {"L": "6.94", "H": "3.21", "W": "2.97", "Cx": "0.3"}, + "Wheels": {"D": "0.94", "Tw": "1.435", "AIM": "250", "Axle": "B", "Ad": "2.5", "Bd": "2.5", "Rmin": "50", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "K", "NBpA": "2", "MBF": "85.0", "MaxBP": "3.5", "LocMaxBP": "5.0", "BCN": "1", "BCR": "0.114", "BCD": "0.12", "BCM": "10.0", "HiPP": "5", "LoPP": "3.5", "Vv": "0.3", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.0125", "CompressorPower": "Main", "BCS": "0.8", "BSA": "0", "BM": "P10-Bg", "BVV": "40", "BRE": "0.9"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "3"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "4", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "ManualBrake": "Yes", "DynamicBrake": "Passive", "MCPN": "9", "SCPN": "7", "AutoRelay": "No", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "P", "BrakeHandle": "Knorr", "LocBrakeHandle": "Knorr", "FuelStart": "Automatic", "ConverterStart": "Automatic", "rows": [ + ["-1","0.6","0","15","Pneumatic"], + ["0","0.5","0","9","Pneumatic"], + ["1","-1","-1","0","Pneumatic"], + ["2","-1","-1","5","Pneumatic"], + ["3","0.34","-1","9","Pneumatic"], + ["4","0.0","-1","15","Pneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "24", "LMaxCurrent": "100"}, + "Engine": {"EngineType": "DieselEngine", "Trans": "1:1", "nmin": "900", "nmax": "1500", "nmax_cutoff": "1550", "AIM": "100", "OilMinPressure": "0.343", "OilMaxPressure": "0.686", "WaterCoolingTemperature": "80", "WaterShutters": "yes", "WaterAuxCircuit": "no", "HeatKFO": "160", "HeatKFO2": "180", "HeatKFS": "160", "HeatKFE": "7", "HeatKW": "2", "HeatKV": "0.3", "IsTC": "No", "TC_TMMax": "1.00", "TC_CP": "0.9", "TC_LT": "1000", "TC_LR": "1", "TC_ULR": "1", "TC_FRI": "1", "TC_FRD": "1", "TC_TII": "0.8", "TC_TIO": "0", "TC_TOO": "0", "TC_LS": "3.5", "TC_ULS": "3.0", "EUS": "0.35", "EDS": "1.0"}, + "MotorParamTable": {"minVelfullengage": "3.8", "engageDia": "1", "engageMaxForce": "800", "engagefriction": "1", "rows": [ + ["0","0","0","-1"], + ["1","46.88","-1","5.73"], + ["2","0","0","-1"], + ["3","28.65","2.9","9.42"], + ["4","0","0","-1"], + ["5","17.12","4.7","15.69"], + ["6","0","0","-1"], + ["7","10.46","10.6","100"] + ]}, + "DList": {"Size": "9", "Mmax": "784.5", "nMmax": "15", "Mnmax": "686.4", "nmax": "25", "nominalfill": "1", "Mstand": "200.0", "rows": [ + ["0","0.1","0"], + ["1","0.2","2"], + ["2","0.3","2"], + ["3","0.4","2"], + ["4","0.5","2"], + ["5","0.6","2"], + ["6","0.7","2"], + ["7","0.8","2"], + ["8","0.9","2"], + ["9","1.0","2"] + ]} +} diff --git a/tests/unit/data/fiz/sm31_v1/411d.json b/tests/unit/data/fiz/sm31_v1/411d.json new file mode 100644 index 000000000..b08d1c7e5 --- /dev/null +++ b/tests/unit/data/fiz/sm31_v1/411d.json @@ -0,0 +1,43 @@ +{ + "_source": "pkp/sm31_v1/411d.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "116400", "Mred": "15000", "Vmax": "80", "PWR": "880", "SandCap": "400"}, + "Dimensions": {"L": "17.00", "H": "4.292", "W": "2.904", "Cx": "0.3"}, + "Wheels": {"D": "1.100", "AIM": "4000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "2.6", "Bd": "13.1", "Rmin": "80"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.3", "LocMaxBP": "4.3", "MaxASBP": "1.3", "BCN": "12", "BCR": "0.114", "BCD": "0.06", "BCM": "5.84", "HiPP": "5", "LoPP": "3.5", "Vv": "0.8", "MinCP": "7.5", "MaxCP": "8.0", "CompressorSpeed": "0.03", "BCS": "1", "BSA": "2", "BM": "P10-Bgu", "CompressorPower": "Converter", "BVV": "130", "BRE": "0.9"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "35"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "11", "SCPN": "2", "AutoRelay": "Yes", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GP", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "ConverterStart": "Automatic", "ConverterStartDelay": "7", "rows": [ + ["-1","0.60","-1","15","Pneumatic"], + ["0","0.50","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.28","-1","9","Pneumatic"], + ["6","0.00","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "100"}, + "Engine": {"AIM": "1.6", "EngineType": "DumbDE", "Trans": "19:70", "Ftmax": "392000", "Vhyp": "4", "Vadd": "5", "Cr": "1", "RelayType": "1"}, + "TurboPos": {"TurboPos": "5"}, + "MotorParamTable": {"rows": [ + ["0","17.567","64","1500","15","30","0"], + ["1","16.29","123.7","1750","32","43","0"], + ["2","15","183.3","2000","49","200","0"] + ]}, + "Circuit": {"ImaxLo": "2800", "IminLo": "2000"}, + "WWList": {"Size": "11", "rows": [ + ["496","0","0","0"], + ["496","30","870","3000"], + ["496","75","870","3000"], + ["496","105","870","3000"], + ["496","135","870","3000"], + ["568","213","870","3000"], + ["640","305","870","3000"], + ["712","395","870","3000"], + ["784","495","870","3000"], + ["856","595","870","3000"], + ["928","705","870","3000"], + ["1000","800","870","3000"] + ]} +} diff --git a/tests/unit/data/fiz/sm40_v1/sm40.json b/tests/unit/data/fiz/sm40_v1/sm40.json new file mode 100644 index 000000000..e8cec2474 --- /dev/null +++ b/tests/unit/data/fiz/sm40_v1/sm40.json @@ -0,0 +1,48 @@ +{ + "_source": "pkp/sm40_v1/sm40.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "62000", "Mred": "10000", "Vmax": "80", "PWR": "1175", "SandCap": "400"}, + "Dimensions": {"L": "11.30", "H": "4.44", "W": "3.05", "Cx": "0.3"}, + "Wheels": {"D": "1.04", "AIM": "4000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "1.75", "Bd": "10.3", "Rmin": "100"}, + "Brake": {"BrakeValve": "K", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.0", "LocMaxBP": "5.0", "BCN": "4", "BCR": "0.236", "BCD": "0.1", "BCM": "6.54", "HiPP": "5", "LoPP": "3.5", "Vv": "0.50", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.0208", "CompressorPower": "Main", "BCS": "0.8", "BSA": "0", "BM": "P10-Bg", "BVV": "127", "BRE": "0.75"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "850", "beta": "0.5", "AllowedFlag": "39"}, + "Cntrl": {"ControlType": "sm40", "BrakeSystem": "Pneumatic", "BCPN": "4", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "0", "LocalBrake": "PneumaticBrake", "ManualBrake": "Yes", "MCPN": "20", "SCPN": "1", "AutoRelay": "Yes", "IniCDelay": "0.4", "SCDelay": "0.4", "BrakeDelays": "GP", "BrakeHandle": "Knorr", "LocBrakeHandle": "Knorr", "ConverterStart": "Automatic", "rows": [ + ["-1","0.90","0","15","Pneumatic"], + ["0","0.50","0","9","Pneumatic"], + ["1","-1","-1","0","Pneumatic"], + ["2","-1","-1","5","Pneumatic"], + ["3","0.001","-1","9","Pneumatic"], + ["4","0.00","-1","15","Pneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "100"}, + "Engine": {"EngineType": "DieselElectric", "Trans": "17:77", "Ftmax": "184500", "Flat": "1", "Vhyp": "5.5", "Vadd": "5.5", "Cr": "1.0", "RelayType": "41"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"}, + "MotorParamTable": {"rows": [ + ["0","5.84","137","300","91","25","0"], + ["1","3.32","191","400","230","0","2700"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "3350", "ImaxHi": "3350", "IminLo": "400", "IminHi": "400"}, + "WWList": {"Size": "20", "rows": [ + ["470","0","0","0"], + ["900","0","0","0"], + ["900","0","0","0"], + ["800","75","360","2600"], + ["800","100","405","2600"], + ["800","125","450","2600"], + ["800","145","450","2600"], + ["845","180","495","2600"], + ["890","230","540","2600"], + ["930","270","585","2600"], + ["970","315","630","2600"], + ["1005","355","675","2600"], + ["1100","360","410","2600"], + ["1100","365","285","2600"], + ["1100","370","405","2600"], + ["1100","375","440","2600"], + ["1100","380","468","2600"], + ["1100","385","490","2600"], + ["1100","390","502","2600"], + ["1100","400","512","2600"], + ["1100","412","540","2600"] + ]} +} diff --git a/tests/unit/data/fiz/sm42_v1/6d1.json b/tests/unit/data/fiz/sm42_v1/6d1.json new file mode 100644 index 000000000..54bdf251b --- /dev/null +++ b/tests/unit/data/fiz/sm42_v1/6d1.json @@ -0,0 +1,42 @@ +{ + "_source": "pkp/sm42_v1/6d1.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "74000", "Mred": "10000", "Vmax": "90", "PWR": "590", "SandCap": "300"}, + "Dimensions": {"L": "14.24", "H": "4.285", "W": "3.170", "Cx": "0.2"}, + "Wheels": {"D": "1.100", "AIM": "4000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "2.6", "Bd": "7.524", "Rmin": "80"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.0", "MaxASBP": "1.8", "BCN": "4", "BCR": "0.1143", "BCD": "0.05", "BCM": "6.68", "HiPP": "5.0", "LoPP": "3.5", "Vv": "0.8", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.057", "CompressorPower": "Engine", "BVV": "200", "BRE": "0.85"}, + "BuffCoupl": {"ControlType": "sm42", "CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "103"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "3", "BDelay3": "36", "BDelay4": "22", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "11", "SCPN": "1", "AutoRelay": "No", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GP", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "ManualBrake": "Yes", "OilStart": "Mixed", "WaterStart": "Battery", "ConverterStart": "Automatic", "rows": [ + ["-1","0.7","-1","15","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.45","-1","9","Pneumatic"], + ["2","0.41","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.28","-1","9","Pneumatic"], + ["6","0","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active", "AwareMinSpeed": "0.0", "AwareDelay": "60", "SoundSignalDelay": "2.5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "2.5", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "91"}, + "Engine": {"AIM": "1.6", "RPMDecRate": "4.0", "EngineType": "DumbDE", "Trans": "17:75", "Ftmax": "228000", "Vhyp": "7", "Vadd": "5.5", "Cr": "1", "ShuntMode": "1", "WaterCoolingTemperature": "81", "WaterCoolingFanSpeed": "-1200", "WaterMaxTemperature": "85", "OilMinPressure": "0.15", "OilMinTemperature": "25", "OilMaxTemperature": "90", "HeaterMinTemperature": "55", "HeaterMaxTemperature": "60", "HeatKFS": "35", "HeatKFO": "30", "HeatKFE": "7", "HeatKW": "0.011", "HeatKV": "0.3", "HeatKFO2": "100"}, + "TurboPos": {"TurboPos": "5"}, + "MotorParamTable": {"rows": [ + ["0","17.567","64","1500","15","0","0"], + ["1","15","183.3","2000","49","0","0"] + ]}, + "Circuit": {"CircuitRes": "0.1", "ImaxLo": "1900", "ImaxHi": "1900", "IminLo": "400", "IminHi": "400"}, + "WWList": {"Size": "11", "rows": [ + ["496","0","0","0","0","0","0"], + ["496","20","125","600","0","140","180"], + ["496","50","156","846","0","140","180"], + ["496","90","194","1065","0","140","180"], + ["496","135","240","1260","0","140","180"], + ["568","182","293","1428","25","150","200"], + ["640","234","353","1571","50","160","235"], + ["712","286","420","1688","80","200","270"], + ["784","348","494","1780","100","235","305"], + ["856","408","575","1845","130","300","370"], + ["928","474","664","1886","150","360","410"], + ["1000","540","760","1900","170","450","490"] + ]} +} diff --git a/tests/unit/data/fiz/sn61_v2/sn61.json b/tests/unit/data/fiz/sn61_v2/sn61.json new file mode 100644 index 000000000..4237a1336 --- /dev/null +++ b/tests/unit/data/fiz/sn61_v2/sn61.json @@ -0,0 +1,42 @@ +{ + "_source": "pkp/sn61_v2/sn61.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "Type": "sn61", "M": "54810", "Vmax": "100", "PWR": "368", "SandCap": "100"}, + "Load": {"MaxLoad": "50", "LoadQ": "pieces", "LoadAccepted": "passengers", "LoadSpeed": "1", "UnLoadSpeed": "1"}, + "Dimensions": {"L": "24.22", "H": "4.12", "W": "2.96", "Cx": "0.5"}, + "Wheels": {"D": "0.94", "Tw": "1.435", "AIM": "300", "Axle": "B'2'", "Ad": "3.2", "Bd": "18.36", "Rmin": "100", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "ESt3d_PZZ", "Size": "150", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.8", "LocMaxBP": "3.8", "BCN": "3", "BCR": "0.127", "BCD": "0.1", "BCM": "6.54", "HiPP": "5", "LoPP": "3.5", "Vv": "0.50", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.0208", "CompressorPower": "Main", "BCS": "1.5", "BSA": "2", "BM": "P10-Bg", "BVV": "150", "BRE": "0.85"}, + "Doors": {"OpenCtrl": "Passenger", "CloseCtrl": "Passenger", "OpenSpeed": "110", "CloseSpeed": "400", "DoorMaxShiftL": "89", "DoorMaxShiftR": "89", "DoorOpenMethod": "Roll", "DoorClosureWarning": "No"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "ControlType": "sn61"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Manual", "DirChangeMaxPos": "2", "LocalBrake": "PneumaticBrake", "ManualBrake": "Yes", "MCPN": "8", "SCPN": "5", "AutoRelay": "Yes", "IniCDelay": "1.5", "SCDelay": "0.3", "BrakeDelays": "P", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "FuelStart": "Automatic", "WaterStart": "Battery", "OilStart": "Automatic", "ConverterStart": "Automatic", "rows": [ + ["-1","0.6","-1","20","Pneumatic"], + ["0","0.5","-1","2","Pneumatic"], + ["1","0.46","-1","2","Pneumatic"], + ["2","0.42","-1","2","Pneumatic"], + ["3","0.38","-1","2","Pneumatic"], + ["4","0.34","-1","2","Pneumatic"], + ["5","0.29","-1","2","Pneumatic"], + ["6","0","-1","40","Pneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Engine": {"EngineType": "DieselEngine", "Trans": "39:83", "nmin": "660", "nmax": "1350", "nmax_cutoff": "1250", "AIM": "150"}, + "MotorParamTable": {"minVelfullengage": "7.8", "engageDia": "1", "engageMaxForce": "3800", "engagefriction": "1", "rows": [ + ["0","0","0","-1","1"], + ["1","5.056","-1","20.3","1"], + ["2","3.325","14.7","30.8","1"], + ["3","2.259","25.4","45.3","1"], + ["4","1.486","40.9","68.9","1"], + ["5","1.000","62.4","200","1"] + ]}, + "DList": {"Size": "8", "Mmax": "3630", "nMmax": "13.333", "Mnmax": "3100", "nmax": "21", "nominalfill": "0.9", "Mstand": "300.0", "rows": [ + ["0","0","0"], + ["1","0.4","0"], + ["2","0.1","0"], + ["3","0.6","1"], + ["4","0.15","3"], + ["5","0.40","3"], + ["6","0.60","3"], + ["7","0.80","3"], + ["8","1.00","4"] + ]} +} diff --git a/tests/unit/data/fiz/st44_v2/m62-1151.json b/tests/unit/data/fiz/st44_v2/m62-1151.json new file mode 100644 index 000000000..1e75cc9c8 --- /dev/null +++ b/tests/unit/data/fiz/st44_v2/m62-1151.json @@ -0,0 +1,56 @@ +{ + "_source": "pkp/st44_v2/m62-1151.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "116000", "Mred": "20000", "Vmax": "100", "PWR": "1232", "SandCap": "400", "Type": "st44"}, + "Dimensions": {"L": "17.55", "H": "4.62", "W": "2.95", "Cx": "0.1"}, + "Wheels": {"D": "1.05", "AIM": "4000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "2.6", "Bd": "8.6", "Rmin": "80"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "124", "MaxBP": "4.1", "MaxLBP": "3.9", "MaxASBP": "1.8", "BCN": "8", "BCR": "0.1016", "BCD": "0.1", "BCM": "10.4", "HiPP": "5", "LoPP": "3.5", "Vv": "0.888", "MinCP": "7", "MaxCP": "8.5", "CompressorSpeed": "0.077", "BCS": "1", "BSA": "2", "BM": "P10-Bgu", "BVV": "222", "CompressorPower": "Main", "BRE": "0.9", "UBB1": "1", "UBB2": "2"}, + "BuffCoupl": {"ControlType": "kolomna", "CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "15", "SCPN": "2", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GP", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "AutoRelay": "Yes", "ManualBrake": "Yes", "OilStart": "Automatic", "WaterStart": "Battery", "ConverterStart": "Automatic", "rows": [ + ["-1","0.7","-1","15","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.29","-1","9","Pneumatic"], + ["6","0","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "100"}, + "Clima": {"HMaxVoltage": "3000", "Heating": "Generator", "HGeneratorEngine": "Main", "HGeneratorMinRPM": "400", "HGeneratorMinVoltage": "2400", "HGeneratorMaxRPM": "750", "HGeneratorMaxVoltage": "3300"}, + "Engine": {"AIM": "3.3", "EngineType": "DieselElectric", "Trans": "15:68", "Ftmax": "396000", "Flat": "1", "Vhyp": "18", "Vadd": "0", "Cr": "1.107", "WaterFlowTemperature": "80", "WaterCoolingTemperature": "45", "WaterMaxTemperature": "95", "WaterShutters": "Yes", "WaterAuxCircuit": "Yes", "WaterAuxCoolingTemperature": "60", "WaterAuxMaxTemperature": "75", "WaterAuxShutters": "Yes", "OilMinPressure": "0.25", "OilMaxPressure": "0.8", "OilMaxTemperature": "71", "HeatKFO": "40", "HeatKFO2": "190", "HeatKW": "0.2", "WaterCoolingFanSpeed": "1.86"}, + "LightsList": {"Size": "8", "Wrap": "No", "Default": "4", "rows": [ + ["38","38","/","A"], + ["38","38","/","A"], + ["34","34"], + ["0","0"], + ["21","34"], + ["1","16"], + ["16","1"], + ["22","34"] + ]}, + "MotorParamTable": {"rows": [ + ["0","10","179","2000","72","2450","0"], + ["1","8","190","1600","95","2250","3600"], + ["2","7","300","1400","170","0","3450"] + ]}, + "WWList": {"Size": "15", "rows": [ + ["400","0","0","0"], + ["400","40","180","1500"], + ["425","100","240","2000"], + ["450","250","300","2080"], + ["475","450","330","3660"], + ["500","500","360","3830"], + ["525","550","380","4000"], + ["550","600","405","4160"], + ["575","700","430","4330"], + ["600","750","450","4660"], + ["625","800","475","4830"], + ["650","850","500","5160"], + ["675","950","525","5330"], + ["700","1050","550","5500"], + ["725","1150","575","5660"], + ["750","1232","600","6000"] + ]} +} diff --git a/tests/unit/data/fiz/st45_v2/301dd.json b/tests/unit/data/fiz/st45_v2/301dd.json new file mode 100644 index 000000000..bc1924c07 --- /dev/null +++ b/tests/unit/data/fiz/st45_v2/301dd.json @@ -0,0 +1,55 @@ +{ + "_source": "pkp/st45_v2/301dd.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "97000", "Mred": "15000", "Vmax": "120", "PWR": "1330", "SandCap": "400"}, + "Dimensions": {"L": "18.99", "H": "4.243", "W": "3.034", "Cx": "0.3"}, + "Wheels": {"D": "1.100", "AIM": "4000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "1.950", "Bd": "10.3", "Rmin": "100"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.1", "MaxASBP": "1.5", "RM": "0.71", "BCN": "8", "BCR": "0.114", "RV": "50", "BCD": "0.06", "BCM": "8.1", "HiPP": "5", "LoPP": "3.5", "Vv": "0.8", "MinCP": "8.0", "MaxCP": "10.0", "CompressorSpeed": "0.0613", "CompressorPower": "Converter", "BCS": "1", "BSA": "2", "BM": "P10-Bgu", "BVV": "250", "BRE": "0.9", "UBB1": "8", "HandlePipeUnlockPos": "-0.5", "LPOn": "2.75", "LPOff": "4.5"}, + "SpringBrake": {"Volume": "10", "MBF": "8", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "3.5", "ValveOnArea": "0.0002", "ValveOffArea": "0.0002", "ValvePNBArea": "0.002", "MTC": "4"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39", "ControlType": "ST45"}, + "Cntrl": {"BrakeHandle": "MHZ_K8P", "LocBrakeHandle": "FD1", "BrakeSystem": "Pneumatic", "BCPN": "10", "BDelay1": "15", "BDelay2": "6", "BDelay3": "36", "BDelay4": "32", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "4", "SCPN": "3", "AutoRelay": "Yes", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GPR", "ConverterStart": "Automatic", "ConverterStartDelay": "5", "MotorBlowersStart": "Converter", "FuelStart": "Automatic", "OilStart": "Automatic", "WaterStart": "Battery", "EIMCtrlType": "3", "BrakeOpModes": "PN", "DirChangeMaxPos": "2", "rows": [ + ["-1","0.7","-1","15","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.29","-1","9","Pneumatic"], + ["6","0","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "227.8"}, + "Engine": {"AIM": "0.9", "EngineType": "DumbDE", "Trans": "19:64", "Ftmax": "326000", "Flat": "0", "Vhyp": "7.2", "Vadd": "5.5", "Cr": "1.0", "RelayType": "45", "WaterMinTemperature": "40", "WaterFlowTemperature": "70", "WaterCoolingTemperature": "82", "WaterMaxTemperature": "91", "WaterShutters": "Yes", "WaterAuxCircuit": "Yes", "WaterAuxCoolingTemperature": "60", "WaterAuxMaxTemperature": "74", "WaterAuxShutters": "Yes", "OilMinPressure": "0.15", "HeatKFO2": "24", "HeatingRPM": "1096", "MotorBlowersSpeed": "-10", "MotorBlowersSustainTime": "600", "MotorBlowersStartVelocity": "0.1"}, + "SpeedControl": {"SpeedCtrl": "Yes", "SpeedButtons": "0|30|40|50|60|70|80|90|100|120", "OverrideManual": "Yes", "InitPwr": "0.3", "MaxPwrVel": "5", "StartVel": "2", "VelStep": "5", "PwrStep": "0.1", "MinPwr": "0.3", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "120", "Offset": "-0.5", "kPpos": "0.1", "kPneg": "0.4", "kIpos": "0.06", "kIneg": "0.06", "BrakeIntervention": "Yes"}, + "MotorParamTable": {"rows": [ + ["0","20","100","3000","20.8","1340","1713"], + ["1","18.5","110","2700","25","1340","1713"], + ["2","17.2","135","2500","30","1340","1713"], + ["3","14.3","170","2000","48.5","1340","1713"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "3300", "IminLo": "2500"}, + "WWList": {"Size": "4", "rows": [ + ["600","0","0","0"], + ["1365","1131","816","3026"], + ["1410","1196","864","3204"], + ["1455","1264","912","3382"], + ["1500","1320","960","3560"] + ]}, + "LightsList": {"Size": "8", "Wrap": "no", "Default": "4", "rows": [ + ["38","34"], + ["38","0"], + ["34","34"], + ["0","0"], + ["21","34"], + ["22","34"], + ["01","16"], + ["16","01"] + ]}, + "UCList": {"Size": "4", "IntegratedBrakePN": "no", "IntegratedBrake": "no", "rows": [ + ["0","0","0","0","0","0.15","0.15","0","1","0"], + ["1","0","0","0","0","0.15","0.15","3","3","1"], + ["2","0","0","1","0","0.00","0.15","3","4","1"], + ["3","0","0","1","0","0.15","0.00","4","5","3"], + ["4","0","0","1","1","0.15","0.15","4","5","3"] + ]} +} diff --git a/tests/unit/data/fiz/static/sug/sug_kielce.json b/tests/unit/data/fiz/static/sug/sug_kielce.json new file mode 100644 index 000000000..d7474ed0f --- /dev/null +++ b/tests/unit/data/fiz/static/sug/sug_kielce.json @@ -0,0 +1,70 @@ +{ + "_source": "static/sug/sug_kielce.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "120000", "Mred": "18000", "Vmax": "100", "PWR": "1860", "SandCap": "700", "Type": "et22"}, + "Load": {"MaxLoad": "52", "LoadQ": "pieces", "LoadAccepted": "pantstate", "LoadSpeed": "1", "UnLoadSpeed": "0.1"}, + "Dimensions": {"L": "16.820", "H": "4.559", "W": "3.005", "Cx": "0.585"}, + "Wheels": {"D": "1.250", "AIM": "2000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "2.1", "Bd": "8.6", "Rmin": "100", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "W", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.1", "LocMaxBP": "5.0", "BCN": "8", "BCR": "0.114", "BCD": "0.12", "BCM": "7.0", "HiPP": "5", "LoPP": "3.5", "Vv": "0.88", "MinCP": "7.0", "MaxCP": "8.0", "CompressorSpeed": "0.0567", "BCS": "0.8", "BSA": "0", "BM": "P10-Bg", "BVV": "200", "CompressorPower": "Main", "BRE": "0.75"}, + "Doors": {"OpenCtrl": "DriverCtrl", "CloseCtrl": "DriverCtrl", "OpenSpeed": "0.3", "CloseSpeed": "0.1", "DoorMaxShiftL": "0.53", "DoorMaxShiftR": "0.53", "DoorOpenMethod": "Shift", "DoorClosureWarning": "Yes"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "850", "beta": "0.5", "AllowedFlag": "0"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "4", "BDelay1": "15", "BDelay2": "4", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "ManualBrake": "Yes", "DynamicBrake": "Passive", "MCPN": "36", "SCPN": "3", "AutoRelay": "No", "IniCDelay": "0.5", "SCDelay": "0.23", "BrakeDelays": "GP", "BrakeHandle": "Knorr", "LocBrakeHandle": "Knorr", "rows": [ + ["-1","0.90","0","15","Pneumatic"], + ["0","0.50","0","9","Pneumatic"], + ["1","-1","-1","0","Pneumatic"], + ["2","-1","-1","5","Pneumatic"], + ["3","0.001","-1","9","Pneumatic"], + ["4","0.00","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Power": {"EnginePower": "CurrentCollector", "CollectorsNo": "2", "MaxVoltage": "3600", "MaxCurrent": "1500", "MinH": "0.38", "MaxH": "2.4", "CSW": "1.4", "MinV": "1800", "MinPress": "3.5", "MaxPress": "5.0"}, + "Engine": {"EngineType": "ElectricSeriesMotor", "Volt": "1500", "Trans": "24:85", "WindingRes": "0.1782", "nmax": "1500"}, + "Switches": {"Pantograph": "impulse"}, + "MotorParamTable0": {"rows": [ + ["0","24.84","85","0.02","167.8","85","0.02"], + ["1","21.37","180","-0.15","144.4","180","-0.15"], + ["2","20.67","230","-0.15","139.7","230","-0.15"], + ["3","21.69","190","-0.04","145","190","-0.04"] + ]}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "500", "ImaxHi": "500", "IminLo": "250", "IminHi": "250"}, + "RList": {"Size": "36", "RVent": "Automatic", "RVentnmax": "3300", "RVentCutOff": "50", "rows": [ + ["0","0","0","0","0"], + ["1","26.43","1","6","0"], + ["2","18.43","1","6","0"], + ["3","13.49","1","6","0"], + ["4","10.87","1","6","0"], + ["5","9.40","1","6","0"], + ["6","7.97","1","6","0"], + ["7","6.56","1","6","0"], + ["8","5.56","1","6","0"], + ["9","4.59","1","6","0"], + ["10","3.36","1","6","0"], + ["11","2.67","1","6","0"], + ["12","2.04","1","6","0"], + ["13","1.48","1","6","0"], + ["14","0.91","1","6","0"], + ["15","0.37","1","6","0"], + ["16","0.00","1","6","0"], + ["17","3.72","2","3","0"], + ["18","2.68","2","3","0"], + ["19","2.40","2","3","0"], + ["20","1.98","2","3","0"], + ["21","1.88","2","3","0"], + ["22","1.26","2","3","0"], + ["23","0.98","2","3","0"], + ["24","0.72","2","3","0"], + ["25","0.40","2","3","0"], + ["26","0.21","2","3","0"], + ["27","0.00","2","3","0"], + ["28","1.59","3","2","0"], + ["29","1.16","3","2","0"], + ["30","0.86","3","2","0"], + ["31","0.65","3","2","0"], + ["32","0.48","3","2","0"], + ["33","0.40","3","2","0"], + ["34","0.25","3","2","0"], + ["35","0.14","3","2","0"], + ["36","0.00","3","2","0"] + ]} +} diff --git a/tests/unit/data/fiz/su45_v2/301d.json b/tests/unit/data/fiz/su45_v2/301d.json new file mode 100644 index 000000000..e45090fb2 --- /dev/null +++ b/tests/unit/data/fiz/su45_v2/301d.json @@ -0,0 +1,47 @@ +{ + "_source": "pkp/su45_v2/301d.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "102000", "Mred": "15000", "Vmax": "120", "PWR": "1250", "SandCap": "400", "HeatingP": "300"}, + "Dimensions": {"L": "18.99", "H": "4.238", "W": "2.900", "Cx": "0.2"}, + "Wheels": {"D": "1.100", "AIM": "4000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "1.950", "Bd": "10.3", "Rmin": "100"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.1", "LocMaxBP": "4.5", "MaxASBP": "1.5", "RM": "0.69", "BCN": "8", "BCR": "0.114", "BCD": "0.06", "BCM": "8.1", "HiPP": "5", "LoPP": "3.5", "Vv": "0.8", "MinCP": "7.0", "MaxCP": "8.5", "CompressorSpeed": "0.053", "BCS": "1", "BSA": "2", "BM": "P10-Bgu", "CompressorPower": "Engine", "BVV": "240", "BRE": "0.9"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "103", "ControlType": "SU45"}, + "Cntrl": {"BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "6", "BDelay3": "36", "BDelay4": "32", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "14", "SCPN": "3", "AutoRelay": "Yes", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GPR", "MotorBlowersStart": "Manual", "ConverterStart": "Automatic", "rows": [ + ["-1","0.7","-1","15","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.29","-1","9","Pneumatic"], + ["6","0","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "227.8"}, + "TurboPos": {"TurboPos": "4"}, + "Engine": {"AIM": "1.6", "EngineType": "DumbDE", "Trans": "18:62", "Ftmax": "326000", "Flat": "0", "Vhyp": "7.2", "Vadd": "5.5", "Cr": "1.0", "RelayType": "45", "WaterMinTemperature": "40", "WaterFlowTemperature": "70", "WaterCoolingTemperature": "82", "WaterMaxTemperature": "91", "WaterShutters": "Yes", "WaterAuxCircuit": "Yes", "WaterAuxCoolingTemperature": "60", "WaterAuxMaxTemperature": "74", "WaterAuxShutters": "Yes", "OilMinPressure": "0.15", "HeatKFO2": "24", "HeatingRPM": "1096", "MotorBlowersSpeed": "-10"}, + "Clima": {"HMaxVoltage": "3000", "Heating": "Generator", "HGeneratorEngine": "Main", "HGeneratorMinRPM": "696", "HGeneratorMinVoltage": "2400", "HGeneratorMaxRPM": "1500", "HGeneratorMaxVoltage": "3300"}, + "MotorParamTable": {"rows": [ + ["0","20","100","3000","20.8","1340","0"], + ["1","18.5","110","2700","25","1340","40"], + ["2","17.2","135","2500","30","1340","50"], + ["3","14.3","170","2000","48.5","1340","1713"] + ]}, + "WWList": {"Size": "14", "rows": [ + ["696","0","0","0"], + ["696","0","0","0"], + ["629","96","78","273"], + ["763","192","147","547"], + ["830","288","221","820"], + ["897","384","295","1094"], + ["964","480","369","1367"], + ["1031","576","442","1641"], + ["1098","672","516","1914"], + ["1165","768","590","2187"], + ["1232","864","664","2461"], + ["1299","960","737","2734"], + ["1366","1056","811","3007"], + ["1433","1152","885","3281"], + ["1500","1245","960","3560"] + ]} +} diff --git a/tests/unit/data/fiz/su46_v2/303d.json b/tests/unit/data/fiz/su46_v2/303d.json new file mode 100644 index 000000000..57061902f --- /dev/null +++ b/tests/unit/data/fiz/su46_v2/303d.json @@ -0,0 +1,50 @@ +{ + "_source": "pkp/su46_v2/303d.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "105000", "Mred": "15000", "Vmax": "120", "PWR": "1400", "SandCap": "400", "HeatingP": "440"}, + "Dimensions": {"L": "18.99", "H": "4.292", "W": "2.904", "Cx": "0.3"}, + "Wheels": {"D": "1.100", "AIM": "4000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "1.75", "Bd": "10.3", "Rmin": "100"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "4.4", "LocMaxBP": "4.4", "MaxASBP": "1.3", "RM": "0.71", "BCN": "12", "BCR": "0.114", "BCD": "0.06", "BCM": "5.84", "HiPP": "5", "LoPP": "3.5", "Vv": "0.9", "MinCP": "7", "MaxCP": "8.5", "CompressorSpeed": "0.05", "BCS": "1", "BSA": "2", "BM": "P10-Bgu", "CompressorPower": "Converter", "BVV": "200", "BRE": "0.8"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "103", "ControlType": "SU46"}, + "Cntrl": {"BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "6", "BDelay3": "36", "BDelay4": "32", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "15", "SCPN": "4", "AutoRelay": "Yes", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GPR", "ConverterStart": "Automatic", "ConverterStartDelay": "10", "MotorBlowersStart": "Manual", "rows": [ + ["-1","0.7","-1","15","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.29","-1","9","Pneumatic"], + ["6","0","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "100"}, + "Engine": {"AIM": "1.6", "EngineType": "DumbDE", "Trans": "18:62", "Ftmax": "317000", "Flat": "0", "Vhyp": "15.0", "Vadd": "3.8", "Cr": "1.0", "RelayType": "46", "HeatingRPM": "1098", "OilMinPressure": "0.055", "OilMaxPressure": "0.6", "WaterMinTemperature": "40", "WaterMaxTemperature": "92", "WaterCoolingTemperature": "76", "WaterFlowTemperature": "71", "WaterShutters": "Yes", "WaterAuxCircuit": "Yes", "WaterAuxFlowTemperature": "1", "WaterAuxMinTemperature": "35", "WaterAuxMaxTemperature": "71", "WaterAuxCoolingTemperature": "56", "WaterAuxShutters": "Yes", "HeaterMinTemperature": "50", "HeaterMaxTemperature": "85", "HeatKW": "0.068599", "HeatKV": "0.3", "HeatKFE": "1", "HeatKFS": "87.76269", "HeatKFO": "26.72716", "HeatKFO2": "57.72", "WaterCoolingFanSpeed": "0.56", "MotorBlowersSpeed": "-10"}, + "TurboPos": {"TurboPos": "4"}, + "Circuit": {"CircuitRes": "0.01", "ImaxLo": "3300", "IminLo": "2500"}, + "Clima": {"HMaxVoltage": "3000", "Heating": "Generator", "HGeneratorEngine": "Main", "HGeneratorMinRPM": "696", "HGeneratorMinVoltage": "2400", "HGeneratorMaxRPM": "1500", "HGeneratorMaxVoltage": "3300"}, + "MotorParamTable": {"rows": [ + ["0","16.7","100","2000","24.1","1680","0"], + ["1","16.9","155","2250","36.9","40","40"], + ["2","17.1","210","2500","50.8","1680","1680"], + ["3","17.3","265","2800","65.0","50","50"], + ["4","17.5","321","3125","79.9","0","1680"] + ]}, + "WWList": {"Size": "15", "rows": [ + ["696","0","0","0"], + ["696","0","0","0"], + ["696","80","350","1000"], + ["696","80","350","1000"], + ["763","214","455","1400"], + ["830","321","500","1600"], + ["897","428","545","1800"], + ["964","535","590","2000"], + ["1031","642","635","2200"], + ["1098","748","680","2400"], + ["1165","855","725","2600"], + ["1232","962","770","2800"], + ["1299","1069","815","3000"], + ["1366","1176","865","3200"], + ["1433","1283","910","3400"], + ["1500","1390","960","3600"] + ]} +} diff --git a/tests/unit/data/fiz/tem2_v2/tem2a.json b/tests/unit/data/fiz/tem2_v2/tem2a.json new file mode 100644 index 000000000..6a9831b57 --- /dev/null +++ b/tests/unit/data/fiz/tem2_v2/tem2a.json @@ -0,0 +1,39 @@ +{ + "_source": "pkp/tem2_v2/tem2a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "116000", "Mred": "20000", "Vmax": "100", "PWR": "780", "SandCap": "2000"}, + "Dimensions": {"L": "17", "H": "4.62", "W": "2.95", "Cx": "0.1"}, + "Wheels": {"D": "1.05", "AIM": "4000", "Tw": "1.435", "Axle": "Co'Co'", "Ad": "2.1", "Bd": "8.6", "Rmin": "80", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "2", "MBF": "124", "MaxBP": "4.1", "MaxLBP": "3.9", "BCN": "4", "BCR": "0.127", "BCD": "0.1", "BCM": "10.77", "HiPP": "5.0", "LoPP": "3.5", "Vv": "0.888", "MinCP": "7", "MaxCP": "8.5", "CompressorSpeed": "0.077", "CompressorPower": "Main", "BCS": "1", "BSA": "0", "BM": "P10-Bgu", "BVV": "222", "BRE": "0.85"}, + "BuffCoupl1": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "3"}, + "BuffCoupl2": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "23", "ControlType": "TEM2"}, + "Cntrl": {"BrakeHandle": "M394", "LocBrakeHandle": "FD1", "BrakeDelays": "G", "BrakeSystem": "Pneumatic", "BCPN": "5", "BDelay1": "25", "BDelay2": "13", "BDelay3": "25", "BDelay4": "13", "ASB": "None", "LocalBrake": "PneumaticBrake", "MCPN": "8", "SCPN": "2", "AutoRelay": "Yes", "IniCDelay": "0.5", "SCDelay": "0.1", "rows": [ + ["-1","1.0","-1","10","Pneumatic"], + ["0","0.5","-1","4","Pneumatic"], + ["1","-1","-1","0","Pneumatic"], + ["2","-1","-1","2","Pneumatic"], + ["3","0.0","-1","2","Pneumatic"], + ["4","0.0","-1","6","Pneumatic"], + ["5","0.0","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "75", "LMaxCurrent": "75"}, + "TurboPos": {"TurboPos": "3"}, + "Engine": {"AIM": "0.9", "EngineType": "DieselElectric", "Trans": "15:68", "Ftmax": "396000", "Flat": "1", "Vhyp": "15", "Vadd": "0", "Cr": "1.107", "OilMinPressure": "0.16", "OilMaxPressure": "0.3", "WaterCoolingFanSpeed": "1.41", "WaterMaxTemperature": "88", "WaterCoolingTemperature": "76", "WaterShutters": "yes"}, + "MotorParamTable": {"rows": [ + ["0","24","66","2000","14","1000","0"], + ["1","18","70","1600","17","1000","1400"], + ["2","15","106","1400","26","0","1400"] + ]}, + "WWList": {"Size": "8", "rows": [ + ["300","0","0","0"], + ["290","65","870","1900"], + ["270","166","870","1900"], + ["330","268","870","1900"], + ["400","370","870","1900"], + ["480","471","870","1900"], + ["570","573","870","1900"], + ["650","647","870","1900"], + ["750","780","870","1900"] + ]} +} diff --git a/tests/unit/data/fiz/wm15_v1/wm15.json b/tests/unit/data/fiz/wm15_v1/wm15.json new file mode 100644 index 000000000..1b1cdce49 --- /dev/null +++ b/tests/unit/data/fiz/wm15_v1/wm15.json @@ -0,0 +1,22 @@ +{ + "_source": "pkp/wm15_v1/wm15.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "20000", "Vmax": "80", "PWR": "200", "SandCap": "400"}, + "Dimensions": {"L": "11", "H": "4.292", "W": "2.900", "Cx": "0.3"}, + "Wheels": {"D": "1.100", "AIM": "4000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "2.6", "Bd": "10.1", "Rmin": "80"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "3.8", "BCN": "4", "BCR": "0.0625", "BCD": "0.11", "BCM": "5.14", "HiPP": "5.0", "LoPP": "3.5", "Vv": "0.250", "MinCP": "7.5", "MaxCP": "8.5", "CompressorSpeed": "0.03", "BVV": "52", "BRE": "0.8", "BM": "P10-Bg", "BCS": "0.8", "BSA": "0"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "3"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "8", "SCPN": "3", "AutoRelay": "No", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GP", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "rows": [ + ["-1","0.8","-1","20","Pneumatic"], + ["0","0.5","-1","2","Pneumatic"], + ["1","0.46","-1","2","Pneumatic"], + ["2","0.42","-1","2","Pneumatic"], + ["3","0.38","-1","2","Pneumatic"], + ["4","0.34","-1","2","Pneumatic"], + ["5","0.29","-1","2","Pneumatic"], + ["6","0","-1","40","Pneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "155"}, + "Power": {"EnginePower": "Internal"}, + "Engine": {"EngineType": "Dumb", "Ftmax": "250000"} +} diff --git a/tests/unit/data/fiz/wmb10_v1/wmb10.json b/tests/unit/data/fiz/wmb10_v1/wmb10.json new file mode 100644 index 000000000..8dc4272f6 --- /dev/null +++ b/tests/unit/data/fiz/wmb10_v1/wmb10.json @@ -0,0 +1,49 @@ +{ + "_source": "pkp/wmb10_v1/wmb10.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "9800", "Vmax": "70", "PWR": "92", "SandCap": "0", "Mred": "1000"}, + "Dimensions": {"L": "8.21", "H": "2.9", "W": "2.8", "Cx": "0.2"}, + "Load": {"MaxLoad": "6", "LoadQ": "pieces", "LoadAccepted": "Passengers,naprawcze,brygada_naprawcza,konserwacja", "OverLoadFactor": "1", "LoadSpeed": "0.5", "UnLoadSpeed": "0.5"}, + "Wheels": {"D": "0.7", "AIM": "2000", "Tw": "1.435", "Axle": "1'A", "Bd": "5.1", "Rmin": "80", "BearingType": "Roll"}, + "Doors": {"OpenCtrl": "Passenger", "CloseCtrl": "Passenger", "OpenSpeed": "0.2", "CloseSpeed": "0.2", "DoorOpenMethod": "Rotate", "DoorMaxShiftL": "0.625", "DoorMaxShiftR": "0.625"}, + "Brake": {"BrakeValve": "LSt", "Size": "12", "NBpA": "1", "MBF": "11", "MaxBP": "4.0", "BCN": "2", "BCR": "0.1016", "BCD": "0.15", "BCM": "4.1", "BCS": "1.0", "BSA": "1.0", "BM": "Disk2", "BVV": "50", "BRE": "0.9", "MinCP": "6", "MaxCP": "7.3", "Vv": "0.2", "CompressorSpeed": "0.0125", "CompressorPower": "Main"}, + "SpringBrake": {"Volume": "9.4", "MBF": "11", "MaxSP": "5.5", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "4.2", "ValveOnArea": "0.0005", "ValveOffArea": "0.0005", "ValvePNBArea": "0.002", "MTC": "132"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "3"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "6", "BDelay1": "15", "BDelay2": "4", "BDelay3": "35", "BDelay4": "25", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "9", "SCPN": "5", "AutoRelay": "no", "IniCDelay": "0.48", "SCDelay": "0.3", "BrakeDelays": "P", "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "FuelStart": "Automatic", "OilStart": "Automatic", "WaterStart": "Battery", "ConverterStart": "Automatic", "rows": [ + ["-1","0.8","-1","20","Pneumatic"], + ["0","0.5","-1","2","Pneumatic"], + ["1","0.46","-1","2","Pneumatic"], + ["2","0.42","-1","2","Pneumatic"], + ["3","0.38","-1","2","Pneumatic"], + ["4","0.34","-1","2","Pneumatic"], + ["5","0.29","-1","2","Pneumatic"], + ["6","0","-1","40","Pneumatic"] + ]}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "24", "LMaxCurrent": "100"}, + "Engine": {"EngineType": "DieselEngine", "Trans": "10:54", "nmin": "575", "nmax": "2700", "nmax_cutoff": "2600", "AIM": "10", "EUS": "20", "EDS": "20"}, + "MotorParamTable": {"minVelfullengage": "1.62", "engageDia": "1", "engageMaxForce": "384", "engagefriction": "1", "rows": [ + ["0","0","0","-1"], + ["1","6.93","-1","10"], + ["2","4.03","4","17"], + ["3","2.36","9","30"], + ["4","1.40","18","53"], + ["5","1.00","33","100"] + ]}, + "DList": {"Size": "9", "Mmax": "450", "nMmax": "26.66", "Mnmax": "370", "nmax": "43.33", "nominalfill": "1", "Mstand": "130", "rows": [ + ["0","0.4","0"], + ["1","0.46","1"], + ["2","0.52","2"], + ["3","0.58","2"], + ["4","0.64","2"], + ["5","0.70","2"], + ["6","0.76","2"], + ["7","0.82","2"], + ["8","0.88","2"], + ["9","1.0","2"] + ]}, + "LightsList": {"Size": "3", "Wrap": "No", "Default": "2", "rows": [ + ["34","21"], + ["0","0"], + ["21","34"] + ]} +} diff --git a/tests/unit/data/fiz/z1_v2/152a.json b/tests/unit/data/fiz/z1_v2/152a.json new file mode 100644 index 000000000..8b33786a2 --- /dev/null +++ b/tests/unit/data/fiz/z1_v2/152a.json @@ -0,0 +1,14 @@ +{ + "_source": "pkp/z1_v2/152a.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "48000", "Vmax": "200", "HeatingP": "54", "LightP": "0.56"}, + "Load": {"MaxLoad": "80", "LoadQ": "pieces", "LoadAccepted": "Passengers,Luggage", "OverLoadFactor": "1.5", "LoadSpeed": "1", "UnLoadSpeed": "2"}, + "Dimensions": {"L": "26.4", "H": "4", "W": "2.9", "Cx": "0.01"}, + "Wheels": {"D": "0.92", "Tw": "1.435", "Axle": "2'2'", "Ad": "2.70", "Bd": "19", "Rmin": "100", "BearingType": "Roll"}, + "Brake": {"BrakeValve": "KE", "MBF": "11", "MaxBP": "3.8", "BCN": "12", "BCR": "0.127", "BCD": "0.05", "BCM": "1.42", "NBpA": "6", "RM": "1.4", "BM": "Disk1+Mg", "BCS": "1.2", "BSA": "0", "BVV": "130", "TBF": "32"}, + "BuffCoupl": {"CType": "Screw", "kB": "9200", "DmaxB": "0.11", "FmaxB": "320", "kC": "4500", "DmaxC": "0.1", "FmaxC": "850", "beta": "0.58", "AllowedFlag": "115"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "LocalBrake": "ManualBrake", "BrakeDelays": "PR+Mg", "ASB": "Yes", "rows": []}, + "Light": {"Light": "Generator", "LGeneratorEngine": "WheelsDriven", "LMaxVoltage": "24", "AlterLight": "Accu", "AlterLMaxVoltage": "24", "AlterLCap": "495", "AlterLRS": "Generator"}, + "Clima": {"Heating": "PowerCable", "HPowerTrans": "ElectricPower", "HMaxVoltage": "3000"}, + "Doors": {"OpenCtrl": "Passenger", "CloseCtrl": "AutomaticCtrl", "OpenSpeed": "0.5", "CloseSpeed": "0.20", "DoorMaxShiftL": "0.9", "DoorMaxShiftR": "0.9", "DoorOpenMethod": "Plug", "DoorClosureWarning": "yes", "DoorBlocked": "Yes", "DoorMaxShiftPlug": "0.043"} +} diff --git a/tests/unit/data/golden/.gitignore b/tests/unit/data/golden/.gitignore new file mode 100644 index 000000000..b191ca629 --- /dev/null +++ b/tests/unit/data/golden/.gitignore @@ -0,0 +1,4 @@ +# the golden traces are recorded on the code before a change, not kept: the samples and their key figures +# (see tests/unit/README.md) +*.csv +*.figures diff --git a/tests/unit/drive.cpp b/tests/unit/drive.cpp new file mode 100644 index 000000000..0ee5adb2b --- /dev/null +++ b/tests/unit/drive.cpp @@ -0,0 +1,246 @@ +#include "stdafx.h" +#include "drive.h" +#include "consist.h" +#include "golden.h" +#include "doctest.h" + +#include "McZapkie/hamulce.h" + +namespace test { + +void mark(trace *Trace, std::string const &Event) { + if (Trace != nullptr) { + Trace->mark(Event); + } +} + +bool is_powered(TMoverParameters const &Vehicle) { + using enum TEngineType; + auto const type{Vehicle.EngineType}; + return Vehicle.Power > 1.0 && (type == ElectricSeriesMotor || type == ElectricInductionMotor || type == DieselEngine || type == DieselElectric); +} + +driven driving(vehicle &Vehicle) { + return {Vehicle.mover(), Vehicle.mover(), Vehicle.mover(), [&Vehicle](double Seconds, track const &Track) { Vehicle.run(Seconds, Track); }}; +} + +driven driving(consist &Consist) { + // the AI's mvControlling and mvPantographUnit: the first vehicle with an engine, and with pantographs + auto *controlling{&Consist[0].mover()}; + auto *pantographs{&Consist[0].mover()}; + for (auto i = Consist.size(); i-- > 0;) { + auto &mover{Consist[i].mover()}; + if (is_powered(mover)) { + controlling = &mover; + } + if (mover.EnginePowerSource.SourceType == TPowerSource::CurrentCollector && mover.EnginePowerSource.CollectorParameters.CollectorsNo > 0) { + pantographs = &mover; + } + } + return {Consist[0].mover(), *controlling, *pantographs, [&Consist](double Seconds, track const &Track) { Consist.run(Seconds, Track); }}; +} + +namespace { + +void attend(TMoverParameters &Mover) { + if (Mover.SecuritySystem.is_cabsignal_blinking() && Mover.SecuritySystem.has_separate_acknowledge()) { + Mover.SecuritySystem.cabsignal_reset(); + } + if (Mover.SecuritySystem.is_blinking()) { + Mover.SecuritySystemReset(); + } +} + +} // namespace + +void drive(driven Driven, track const &Track, double Seconds) { + auto const seconds{std::lround(Seconds)}; + for (long second = 0; second < seconds; ++second) { + attend(Driven.cab); + Driven.run(1.0, Track); + } +} + +void release_brakes(TMoverParameters &Cab) { + Cab.BrakeLevelSet(Cab.Handle->GetPos(bh_RP)); + Cab.DecLocalBrakeLevel(LocalBrakePosNo); + Cab.DecManualBrakeLevel(ManualBrakePosNo); + Cab.SpringBrakeActivate(false); + // the hand and spring brakes of the rest of the train, as TController::AutoRewident() releases them + std::vector train{&Cab}; + std::vector pending{&Cab}; + while (false == pending.empty()) { + auto const *vehicle{pending.back()}; + pending.pop_back(); + for (auto const &thiscoupler : vehicle->Couplers) { + if (thiscoupler.Connected != nullptr && thiscoupler.CouplingFlag != coupling::faux && std::ranges::find(train, thiscoupler.Connected) == train.end()) { + thiscoupler.Connected->DecManualBrakeLevel(ManualBrakePosNo); + thiscoupler.Connected->SpringBrake.Activate = false; + train.push_back(thiscoupler.Connected); + pending.push_back(thiscoupler.Connected); + } + } + } +} + +bool is_ready(driven const &Driven) { + auto const &mover{Driven.controlling}; + auto const air{mover.VeselVolume <= 0.0 || mover.ScndPipePress >= 4.5}; + // the converter feeds the 110 V circuits (the electrodynamic brake's control among them); the AI switches + // it on once the line breaker is closed, so a train already charged with air isn't ready before it runs + auto const converter{mover.ConverterStart != start_t::manual || mover.ConverterFlag}; + return mover.Mains && air && converter; +} + +namespace { + +// the pumps and the heater of a diesel's water, and the fuel pump +void prepare_diesel(driven const &Driven) { + Driven.cab.OilPumpSwitch(true); + Driven.controlling.WaterPumpBreakerSwitch(true); + Driven.controlling.WaterPumpSwitch(true); + Driven.controlling.WaterHeaterBreakerSwitch(true); + Driven.controlling.WaterHeaterSwitch(true); + Driven.cab.FuelPumpSwitch(true); +} + +// the pantographs' own air topped up by their compressor, and the pantographs raised +void raise_pantographs(driven const &Driven) { + auto &pantographs{Driven.pantographs}; + if (auto const enough{(pantographs.TrainType == dt_EZT ? 2.5 : 3.5) + 0.1}; pantographs.PantPress < enough) { + if (false == pantographs.PantAutoValve) { + pantographs.bPantKurek3 = false; // the pantographs on their own air + } + pantographs.PantCompFlag = true; + } + else if (false == pantographs.bPantKurek3 || pantographs.PantPress <= pantographs.ScndPipePress) { + pantographs.PantCompFlag = false; + } + Driven.cab.OperatePantographsValve(operation_t::enable); + Driven.cab.OperatePantographValve(end::rear, operation_t::enable); +} + +// the line breaker closed, which it does only with the controller at zero, and after the relays are reset +void close_line_breaker(driven const &Driven) { + auto &controlling{Driven.controlling}; + while (controlling.DecMainCtrl(2)) { + // down to zero + } + controlling.DecScndCtrl(2); + Driven.cab.RelayReset(relay_t::maincircuitground | relay_t::tractionnmotoroverload | relay_t::primaryconverteroverload); + if (controlling.EngineType == TEngineType::DieselEngine) { + // a mechanical diesel starts at idle, the first position giving the engine fuel + while (controlling.RList[controlling.MainCtrlPos].Mn == 0 && controlling.IncMainCtrl(1)) { + // up to the first position with fuel + } + } + Driven.cab.MainSwitch(true); +} + +// the converter, the compressor and the motor blowers, once the line breaker is closed +void switch_auxiliaries_on(TMoverParameters &Cab) { + Cab.ConverterSwitch(true); + Cab.CompressorSwitch(true); + for (auto const side : {end::front, end::rear}) { + if (Cab.MotorBlowers[side].speed != 0) { + Cab.MotorBlowersSwitchOff(false, side); + Cab.MotorBlowersSwitch(true, side); + } + } +} + +} // namespace + +bool start_up(driven Driven, track const &Track, double Seconds, trace *Trace) { + auto &cab{Driven.cab}; + auto const &controlling{Driven.controlling}; + auto const &pantographs{Driven.pantographs}; + mark(Trace, "start up"); + auto const diesel{controlling.EngineType == TEngineType::DieselEngine || controlling.EngineType == TEngineType::DieselElectric}; + auto const collector{pantographs.EnginePowerSource.SourceType == TPowerSource::CurrentCollector && + pantographs.EnginePowerSource.CollectorParameters.CollectorsNo > 0}; + // the converter and the compressor get switched on once the line breaker is closed, wherever in the train + // they are; a train already charged with air isn't ready before that + auto auxiliaries{false}; + // one round of the driver's actions a second, as the AI reacts + for (auto second = 0; second < Seconds; ++second) { + cab.BatterySwitch(true); + cab.CabActivisation(); + if (diesel) { + prepare_diesel(Driven); + } + if (collector) { + raise_pantographs(Driven); + } + if (false == collector || std::max(controlling.GetTrainsetHighVoltage(), controlling.PantographVoltage) > controlling.EnginePowerSource.CollectorParameters.MinV) { + // a direction first, as in TController::PrepareDirection(): the relays reset only with one set + for (int i = 0; i < 3 && cab.DirActive <= 0; ++i) { + cab.DirectionForward(); + } + if (false == controlling.Mains) { + close_line_breaker(Driven); + } + else { + switch_auxiliaries_on(cab); + auxiliaries = true; + } + } + drive(Driven, Track, 1.0); + if (auxiliaries && is_ready(Driven)) { + mark(Trace, "ready"); + return true; + } + } + return false; +} + +void drive_cycle(driven Driven, track const &Track, trace &Trace) { + auto &cab{Driven.cab}; + auto &controlling{Driven.controlling}; + if (false == start_up(Driven, Track, 300.0, &Trace)) { + MESSAGE("didn't get ready: Mains " << controlling.Mains << ", main reservoir " << controlling.ScndPipePress); + } + Trace.mark("release brakes"); + release_brakes(cab); + drive(Driven, Track, 30.0); + + Trace.mark("forward"); + for (int i = 0; i < 3 && cab.DirActive <= 0; ++i) { + cab.DirectionForward(); + } + drive(Driven, Track, 2.0); + + Trace.mark("notch up"); + for (int i = 0; i < 60 && false == controlling.IsMainCtrlMaxPowerPos(); ++i) { + controlling.IncMainCtrl(1); + drive(Driven, Track, 2.0); + } + Trace.mark("full power"); + drive(Driven, Track, 30.0); + + Trace.mark("second controller up"); + for (int i = 0; i < 20 && controlling.IncScndCtrl(1); ++i) { + drive(Driven, Track, 2.0); + } + drive(Driven, Track, 20.0); + Trace.mark("second controller down"); + for (int i = 0; i < 20 && controlling.DecScndCtrl(1); ++i) { + drive(Driven, Track, 1.0); + } + + Trace.mark("notch down"); + for (int i = 0; i < 60 && controlling.DecMainCtrl(1); ++i) { + drive(Driven, Track, 1.0); + } + Trace.mark("coast"); + drive(Driven, Track, 10.0); + + Trace.mark("brake"); + cab.BrakeLevelSet(cab.Handle->GetPos(bh_FB)); + drive(Driven, Track, 60.0); + Trace.mark("release"); + cab.BrakeLevelSet(cab.Handle->GetPos(bh_RP)); + drive(Driven, Track, 20.0); +} + +} // namespace test diff --git a/tests/unit/drive.h b/tests/unit/drive.h new file mode 100644 index 000000000..a1aed95be --- /dev/null +++ b/tests/unit/drive.h @@ -0,0 +1,56 @@ +#pragma once + +#include +#include + +#include "vehicle.h" + +namespace test { + +class trace; +class consist; + +// what a driver does with a vehicle, made of the steps the AI takes (TController and the driver hints of +// application/driverhints.cpp), on the vehicle's own controls. the driver sits in a vehicle with a cab +// (Cab != 0): a lone one, or the first one of a consist, and drives the whole of it + +// what the driver drives: the vehicle with the driver in it, the vehicles the driver controls the engine and +// the pantographs of (the AI's mvOccupied, mvControlling, mvPantographUnit; all the same one in a lone +// vehicle or a locomotive), and the way to run the whole of it +struct driven { + TMoverParameters &cab; + TMoverParameters &controlling; + TMoverParameters &pantographs; + std::function run; +}; +driven driving(vehicle &Vehicle); +driven driving(consist &Consist); + +// gets the vehicle ready to run, the way TController::PrepareEngine() does: battery, cab, pumps and heating +// of a diesel engine, pantographs, line breaker, converter, compressor and motor blowers, then waits for the +// air. stops trying after Seconds; returns whether the vehicle got ready +bool start_up(driven Driven, track const &Track, double Seconds = 300.0, trace *Trace = nullptr); + +// runs the vehicle for Seconds with the driver paying attention, as TController::control_security_system() +// does: once a second the vigilance and the cab signalling get acknowledged when they call for it +void drive(driven Driven, track const &Track, double Seconds); + +// releases every brake the driver has a hand on: the train brake to running, the independent brake, the +// hand brake and the spring brake; and the hand and spring brakes of the vehicles coupled to the cab's +void release_brakes(TMoverParameters &Cab); + +// whether the vehicle is ready to run: line breaker closed (or the engine running), air in the main reservoir +bool is_ready(driven const &Driven); + +// whether the vehicle drives itself +bool is_powered(TMoverParameters const &Vehicle); + +// a trace mark, when there's a trace +void mark(trace *Trace, std::string const &Event); + +// a whole run: start up, release the brakes, notch up to full power, the second controller (field weakening, +// shunting) up and down, notch down, coast and brake to a stop. the controllers move on a fixed schedule, +// not the way a driver watching the ammeter would, so the relays may trip on the way +void drive_cycle(driven Driven, track const &Track, trace &Trace); + +} // namespace test diff --git a/tests/unit/figures.cpp b/tests/unit/figures.cpp new file mode 100644 index 000000000..96f5a3d74 --- /dev/null +++ b/tests/unit/figures.cpp @@ -0,0 +1,222 @@ +#include "stdafx.h" +#include "figures.h" + +#include +#include +#include +#include + + +namespace test { + +namespace { + +// the units of the figures, with the decimals they're written with +struct unit { + char const *name; + int decimals; +}; +unit const seconds{"s", 2}; +unit const kmh{"km/h", 1}; +unit const metres{"m", 1}; +unit const bar{"bar", 3}; +unit const amperes{"A", 0}; +unit const kilonewtons{"kN", 1}; +std::array const units{seconds, kmh, metres, bar, amperes, kilonewtons}; + +std::string line(char const *Name, double Value, unit const &Unit) { + return std::format("{} = {:.{}f} {}", Name, Value, Unit.decimals, Unit.name); +} + +// the name, the value and the unit of a line of a figure, or false for the header of a part +bool parse(std::string_view const Line, std::string &Name, double &Value, std::string &Unit) { + auto const equals{Line.find(" = ")}; + if (equals == std::string_view::npos) { + return false; + } + Name = Line.substr(0, equals); + std::string const value{Line.substr(equals + 3)}; + auto const space{value.find(' ')}; + Value = std::strtod(value.substr(0, space).c_str(), nullptr); + Unit = space == std::string::npos ? std::string{} : value.substr(space + 1); + return true; +} + +// a difference of 0.5%, or of the last decimal written +bool close(double Recorded, double Now, std::string_view const Unit) { + auto step{0.0}; + for (auto const &unit : units) { + if (Unit == unit.name) { + step = std::pow(10.0, -unit.decimals); + } + } + return std::abs(Recorded - Now) <= 0.005 * std::max(std::abs(Recorded), std::abs(Now)) + step + 1e-9; +} + +// the drop of the brake pipe which the time to get back counts from, and the pressure it counts to +constexpr double pipe_drop{4.8}; +constexpr double pipe_back{4.9}; + +} // namespace + +void figures::sample(double Time, TMoverParameters const &Vehicle) { + auto &part{m_parts.back()}; + auto const speed{Vehicle.V * 3.6}; + auto const pipe{Vehicle.PipePress}; + if (false == part.sampled) { + part.sampled = true; + // from the last step of the part before, so the parts add up to the run + part.start = m_last_time < 0.0 ? Time : m_last_time; + part.speed_start = m_last_time < 0.0 ? speed : m_last_speed; + part.distance_start = m_last_time < 0.0 ? Vehicle.DistCounter : m_last_distance; + part.pipe_lowest = pipe; + } + part.end = Time; + part.speed_end = speed; + part.speed_peak = std::max(part.speed_peak, std::abs(speed)); + part.distance_end = Vehicle.DistCounter; + if (part.stopped < 0.0 && std::abs(part.speed_start) > 1.0 && std::abs(speed) < 0.05) { + part.stopped = Time - part.start; + } + if (pipe < part.pipe_lowest) { + part.pipe_lowest = pipe; + part.pipe_back = -1.0; + } + part.pipe_end = pipe; + if (part.pipe_back < 0.0 && part.pipe_lowest < pipe_drop && pipe >= pipe_back) { + part.pipe_back = Time - part.start; + } + part.cylinders_peak = std::max(part.cylinders_peak, Vehicle.BrakePress); + part.current_peak = std::max(part.current_peak, std::abs(Vehicle.Im)); + part.force_peak = std::max(part.force_peak, std::abs(Vehicle.Ft) / 1000.0); + + m_last_time = Time; + m_last_speed = speed; + m_last_distance = Vehicle.DistCounter; + // which figures the vehicle has, over the whole run, so each part has the same ones + m_moved |= std::abs(speed) >= 0.05; + m_piped |= pipe >= 0.1; + m_braked |= Vehicle.BrakePress >= 0.005; + m_current |= std::abs(Vehicle.Im) >= 0.5; + m_force |= std::abs(Vehicle.Ft) >= 50.0; +} + +void figures::mark(std::string const &Event) { + if (auto &part{m_parts.back()}; false == part.sampled) { + // nothing ran since the last event: a part of both + part.event = part.event == "start" ? Event : part.event + ", " + Event; + return; + } + m_parts.emplace_back(Event); +} + +std::vector figures::lines() const { + std::vector result; + for (auto const &part : m_parts) { + if (false == part.sampled) { + continue; + } + result.push_back("[" + part.event + "]"); + result.push_back(line("duration", part.end - part.start, seconds)); + if (m_moved) { + result.push_back(line("speed at the start", part.speed_start, kmh)); + result.push_back(line("speed at the end", part.speed_end, kmh)); + result.push_back(line("speed peak", part.speed_peak, kmh)); + result.push_back(line("distance", (part.distance_end - part.distance_start) * 1000.0, metres)); + if (part.stopped >= 0.0) { + result.push_back(line("stopped after", part.stopped, seconds)); + } + } + if (m_piped) { + result.push_back(line("brake pipe lowest", part.pipe_lowest, bar)); + result.push_back(line("brake pipe at the end", part.pipe_end, bar)); + if (part.pipe_back >= 0.0) { + result.push_back(line("brake pipe back at 4.9 bar after", part.pipe_back, seconds)); + } + } + if (m_braked) { + result.push_back(line("brake cylinders peak", part.cylinders_peak, bar)); + } + if (m_current) { + result.push_back(line("motor current peak", part.current_peak, amperes)); + } + if (m_force) { + result.push_back(line("tractive force peak", part.force_peak, kilonewtons)); + } + } + return result; +} + +namespace { + +struct figure { + std::string name; + double value; + std::string unit; + std::string text; // the value and the unit as written +}; + +struct recorded_part { + std::string header; + std::vector
figures; +}; + +std::vector parts_of(std::vector const &Lines) { + std::vector parts; + for (auto const &line : Lines) { + figure figure; + if (false == parse(line, figure.name, figure.value, figure.unit)) { + parts.emplace_back(line); + continue; + } + if (parts.empty()) { + parts.emplace_back(); + } + figure.text = line.substr(line.find(" = ") + 3); + parts.back().figures.push_back(figure); + } + return parts; +} + +figure const *find(std::vector
const &Figures, std::string_view const Name) { + for (auto const &figure : Figures) { + if (figure.name == Name) { + return &figure; + } + } + return nullptr; +} + +} // namespace + +std::vector figures::compare(std::vector const &Recorded, std::vector const &Now) { + std::vector differences; + auto const recorded{parts_of(Recorded)}; + auto const now{parts_of(Now)}; + for (std::size_t i = 0; i < std::max(recorded.size(), now.size()); ++i) { + if (i >= recorded.size() || i >= now.size() || recorded[i].header != now[i].header) { + // the parts can't be paired up from here on + differences.push_back(std::format("the parts differ from part {}: {} now, {} recorded", i + 1, i < now.size() ? now[i].header : std::string{"none"}, + i < recorded.size() ? recorded[i].header : std::string{"none"})); + break; + } + auto const &header{now[i].header}; + for (auto const &figure : recorded[i].figures) { + auto const *current{find(now[i].figures, figure.name)}; + if (current == nullptr) { + differences.push_back(header + " " + figure.name + ": none now, " + figure.text + " recorded"); + } + else if (current->unit != figure.unit || false == close(figure.value, current->value, figure.unit)) { + differences.push_back(header + " " + figure.name + ": " + current->text + " now, " + figure.text + " recorded"); + } + } + for (auto const &figure : now[i].figures) { + if (find(recorded[i].figures, figure.name) == nullptr) { + differences.push_back(header + " " + figure.name + ": " + figure.text + " now, none recorded"); + } + } + } + return differences; +} + +} // namespace test diff --git a/tests/unit/figures.h b/tests/unit/figures.h new file mode 100644 index 000000000..e69e67ef4 --- /dev/null +++ b/tests/unit/figures.h @@ -0,0 +1,70 @@ +#pragma once + +#include +#include + +#include "McZapkie/MOVER.h" + +namespace test { + +// the key figures of a vehicle's run, for each part of it between two events of the test: the speeds, the +// distance, the time to stop, the brake pipe's lowest pressure and the time it took to get back, the brake +// cylinders', the motor current's and the tractive force's peaks. a golden trace keeps them next to its +// samples, in data/golden/.figures, which a person reads: +// +// [emergency] +// duration = 40.00 s +// speed at the start = 80.0 km/h +// speed at the end = 0.0 km/h +// distance = 412.3 m +// stopped after = 38.21 s +// brake cylinders peak = 3.800 bar +// +// a figure the vehicle doesn't have (no speed, no current) is left out of all the parts. they're compared +// within 0.5%, loose enough for any compiler, so a change which alters the behaviour on purpose shows in their +// diff in a few readable lines, where the samples change in thousands +class figures { +public: + // called on every step of the vehicle + void sample(double Time, TMoverParameters const &Vehicle); + // a part of the run starts with the event + void mark(std::string const &Event); + + // the figures as the lines of the file + std::vector lines() const; + // the differences of the lines from the recorded ones, beyond the tolerance, each told in a line + static std::vector compare(std::vector const &Recorded, std::vector const &Now); + +private: + struct part { + std::string event; + bool sampled{false}; + double start{0.0}; + double end{0.0}; + double speed_start{0.0}; + double speed_end{0.0}; + double speed_peak{0.0}; + double distance_start{0.0}; + double distance_end{0.0}; + double stopped{-1.0}; // after the start, if it started moving and stopped + double pipe_lowest{0.0}; + double pipe_end{0.0}; + double pipe_back{-1.0}; // after the start, at 4.9 bar again after a drop under 4.8 + double cylinders_peak{0.0}; + double current_peak{0.0}; + double force_peak{0.0}; + }; + + std::vector m_parts{part{"start"}}; + double m_last_time{-1.0}; + double m_last_speed{0.0}; + double m_last_distance{0.0}; + // the figures the vehicle has, shown in every part + bool m_moved{false}; + bool m_piped{false}; + bool m_braked{false}; + bool m_current{false}; + bool m_force{false}; +}; + +} // namespace test diff --git a/tests/unit/fiz.cpp b/tests/unit/fiz.cpp new file mode 100644 index 000000000..38310db46 --- /dev/null +++ b/tests/unit/fiz.cpp @@ -0,0 +1,222 @@ +#include "stdafx.h" +#include "fiz.h" + +#include +#include +#include + +#include "utilities/parser.h" + +namespace test::fiz { + +namespace { + +// the lists of LoadFIZ_Parse(), as bits of the set of the ones reading a row +constexpr unsigned brake_table{1 << 0}; // startBPT +constexpr unsigned motor_table{1 << 1}; // startMPT +constexpr unsigned motor_table0{1 << 2}; // startMPT0 +constexpr unsigned resistors{1 << 3}; // startRLIST +constexpr unsigned universal{1 << 4}; // startUCLIST +constexpr unsigned diesel{1 << 5}; // startDLIST +constexpr unsigned diesel_momentum{1 << 6}; // startDIZELMOMENTUMLIST +constexpr unsigned diesel_v2n{1 << 7}; // startDIZELV2NMAXLIST +constexpr unsigned hydro{1 << 8}; // startHYDROTCLIST +constexpr unsigned diesel_pmax{1 << 9}; // startPMAXLIST +constexpr unsigned field{1 << 10}; // startFFLIST +constexpr unsigned field_brake{1 << 11}; // startFFEDLIST +constexpr unsigned generator{1 << 12}; // startWWLIST +constexpr unsigned wipers{1 << 13}; // startWiperList +constexpr unsigned dimmer{1 << 14}; // startDimmerList +constexpr unsigned lights{1 << 15}; // startLIGHTSLIST +constexpr unsigned compressors{1 << 16}; // startCOMPRESSORLIST + +// the issection() calls of LoadFIZ_Parse() +std::array const sections{ + section{"Param", '.'}, + section{"Load", ':'}, + section{"Dimensions", ':'}, + section{"Wheels", ':'}, + section{"Brake", ':'}, + section{"Doors", ':'}, + section{"BuffCoupl", '.'}, + section{"BuffCoupl1", '.'}, + section{"BuffCoupl2", '.'}, + section{"TurboPos", ':'}, + section{"Cntrl", '.', brake_table, ""}, + section{"Headlights", ':'}, + section{"Blending", ':'}, + section{"DCEMUED", ':'}, + section{"SpringBrake", ':'}, + section{"Light", ':'}, + section{"Security", ':'}, + section{"Clima", ':'}, + section{"Power", ':'}, + section{"SpeedControl", ':'}, + section{"Engine", ':'}, + section{"Switches", ':'}, + section{"MotorParamTable", ':', motor_table, "END-MPT"}, + section{"MotorParamTable0", ':', motor_table0, "END-MPT"}, + section{"Circuit", ':'}, + section{"AI", ':'}, + section{"RList", ':', resistors, "END-RL"}, + section{"UCList", ':', universal, "END-UCL"}, + section{"DList", ':', diesel, "END-DL"}, + section{"DMList", ':', diesel_momentum, "END-DML"}, + section{"HTCList", ':', hydro, "END-HTCL"}, + section{"PmaxList", ':', diesel_pmax, "END-PML"}, + section{"V2NList", ':', diesel_v2n, "END-V2NL"}, + section{"ffList", ':', field, "endff"}, + section{"ffBrakeList", ':', field_brake, "endff"}, + section{"WWList", ':', generator, "END-WWL"}, + section{"WiperList", ':', wipers, "endwl"}, + section{"DimmerList", ':', dimmer, "endDimmerList"}, + section{"LightsList", ':', lights, "endL"}, + section{"CompressorList", ':', compressors, "endCL"}, +}; + +// the lines closing lists, checked by the loader before the sections in this order, each closing the brake table too +struct closing { + std::string_view line; + unsigned lists; +}; +std::array const closings{ + closing{"END-MPT", motor_table | motor_table0}, + closing{"END-RL", resistors}, + closing{"END-UCL", universal}, + closing{"END-DL", diesel}, + closing{"END-DML", diesel_momentum}, + closing{"END-V2NL", diesel_v2n}, + closing{"END-HTCL", hydro}, + closing{"END-PML", diesel_pmax}, + closing{"endff", field | field_brake}, + closing{"endwl", wipers}, + closing{"endDimmerList", dimmer}, + closing{"END-WWL", generator}, + closing{"endL", lights}, + closing{"endCL", compressors}, +}; + +} // namespace + +std::string value(std::string const &Key, std::string const &Line) { + std::string const input{" " + Line}; + auto const lookup{input.find(" " + Key + "=")}; + if (lookup == std::string::npos) { + return {}; + } + auto const start{input.find_first_not_of(' ', lookup + Key.size() + 2)}; + if (start == std::string::npos) { + return {}; + } + auto const end{input.find(' ', start)}; + return input.substr(start, end == std::string::npos ? std::string::npos : end - start); +} + +std::vector words(std::string_view const Line) { + std::vector result; + std::size_t start{0}; + while ((start = Line.find_first_not_of(" \t;", start)) != std::string_view::npos) { + auto const end{Line.find_first_of(" \t;", start)}; + result.emplace_back(Line.substr(start, end == std::string_view::npos ? std::string_view::npos : end - start)); + start = end; + } + return result; +} + +section const *named(std::string_view Name) { + auto const lookup{std::ranges::find(sections, Name, §ion::name)}; + return lookup == sections.end() ? nullptr : std::to_address(lookup); +} + +section const *opened_by(std::string_view Line) { + auto const lookup{std::ranges::find_if(sections, [&](section const &Section) { return Line.starts_with(Section.name) && Line.size() > Section.name.size() && Line[Section.name.size()] == Section.separator; })}; + return lookup == sections.end() ? nullptr : std::to_address(lookup); +} + +std::vector lines(cParser &Parser) { + std::vector result; + while (Parser.ok()) { + auto line{Parser.getToken(false, "\n\r")}; + if (line.starts_with("//") || line.find('#') != std::string::npos) { + continue; + } + line.erase(0, line.find_first_not_of(' ')); + if (false == line.empty() && line.back() == '\r') { + line.pop_back(); + } + result.push_back(line); + } + return result; +} + +std::vector keys(std::string const &Line) { + std::vector result; + std::string const input{" " + Line}; + for (auto equals{input.find('=')}; equals != std::string::npos; equals = input.find('=', equals + 1)) { + auto const start{input.rfind(' ', equals) + 1}; + auto key{input.substr(start, equals - start)}; + // a=b=c gives a, not a=b + if (false == key.empty() && key.find('=') == std::string::npos && std::ranges::find(result, key) == result.end()) { + result.push_back(std::move(key)); + } + } + return result; +} + +// the values the section gives, as the loader asks for them by their keys +std::string section_values(section const &Section, std::string const &Line) { + auto keys{fiz::keys(Line)}; + std::ranges::sort(keys); + std::string values{Section.name}; + for (auto const &key : keys) { + if (auto const value{fiz::value(key, Line)}; false == value.empty()) { + values += " " + key + "=" + value; + } + } + return values; +} + +// the lists reading a row: the first one open, in the order of the loader's checks, which carries on past the dimmer list +unsigned row_readers(unsigned const Active) { + unsigned readers{0}; + for (unsigned list = brake_table; list <= compressors; list <<= 1) { + if ((Active & list) == 0) { + continue; + } + readers |= list; + if (list != dimmer) { + break; + } + } + return readers; +} + +std::vector reading(std::vector const &Lines) { + std::vector result; + unsigned active{0}; + for (auto const &line : Lines) { + if (line.empty()) { + active &= ~brake_table; + continue; + } + if (auto const close{std::ranges::find_if(closings, [&](closing const &Closing) { return line.starts_with(Closing.line); })}; close != closings.end()) { + active &= ~(close->lists | brake_table); + continue; + } + if (auto const opened{opened_by(line)}; opened != nullptr) { + active = (active & ~brake_table) | opened->list; + result.push_back(section_values(*opened, line)); + continue; + } + if (auto const readers{row_readers(active)}; readers != 0) { + std::string row{std::format("row of {:x}:", readers)}; + for (auto const &word : words(line)) { + row += " " + word; + } + result.push_back(std::move(row)); + } + } + return result; +} + +} // namespace test::fiz diff --git a/tests/unit/fiz.h b/tests/unit/fiz.h new file mode 100644 index 000000000..1e65049fc --- /dev/null +++ b/tests/unit/fiz.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include + +class cParser; + +// what the game's vehicle loader (TMoverParameters::LoadFIZ_Parse) makes of the lines of a vehicle definition, +// for the test definitions to give it the same and for checking they do +namespace test::fiz { + +// a section of the definition, opened by a line starting with its name and separator. a list section is +// followed by rows, read until the line closing them (an empty line for the Cntrl brake table) and as +// long as no other list closes first, see reading() +struct section { + std::string_view name; + char separator; + unsigned list{0}; // the bit of the list its rows go to, none for a section of values only + char const *end{nullptr}; // the line closing the rows +}; + +// the section of the given name, or none for a name the loader doesn't know +section const *named(std::string_view Name); +// the section the line opens, or none +section const *opened_by(std::string_view Line); + +// the lines of a definition as the loader takes them: without comments, leading spaces and line ends +std::vector lines(cParser &Parser); + +// the keys of the values in the line, in the order of their first appearance: the words followed by =, +// which extract_value() finds +std::vector keys(std::string const &Line); + +// the value of the key in the line, as extract_value() gives it, empty for none (where it would throw too) +std::string value(std::string const &Key, std::string const &Line); +// the words of a row, as the readers of the lists get them from cParser +std::vector words(std::string_view Line); + +// what the loader reads from the lines, in order: every section line with the values it gives, every row +// with the lists reading it. two definitions reading the same load the same vehicle +std::vector reading(std::vector const &Lines); + +} // namespace test::fiz diff --git a/tests/unit/golden.cpp b/tests/unit/golden.cpp new file mode 100644 index 000000000..45bd1c532 --- /dev/null +++ b/tests/unit/golden.cpp @@ -0,0 +1,393 @@ +#include "stdafx.h" +#include "golden.h" +#include "doctest.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "McZapkie/hamulce.h" + +namespace test { + +namespace channels { + +std::vector motion() { + return { + {"V", [](auto const &M) { return M.V; }}, + {"DistCounter", [](auto const &M) { return M.DistCounter; }}, + {"AccS", [](auto const &M) { return M.AccS; }}, + {"nrot", [](auto const &M) { return M.nrot; }}, + {"Ft", [](auto const &M) { return M.Ft; }}, + {"Fb", [](auto const &M) { return M.Fb; }}, + {"FStand", [](auto const &M) { return M.FStand; }}, + {"SlippingWheels", [](auto const &M) { return M.SlippingWheels ? 1.0 : 0.0; }}, + }; +} + +std::vector traction() { + return { + {"Mains", [](auto const &M) { return M.Mains ? 1.0 : 0.0; }}, + {"DirActive", [](auto const &M) { return M.DirActive; }}, + {"MainCtrlPos", [](auto const &M) { return M.MainCtrlPos; }}, + {"MainCtrlActualPos", [](auto const &M) { return M.MainCtrlActualPos; }}, + {"ScndCtrlPos", [](auto const &M) { return M.ScndCtrlPos; }}, + {"ScndCtrlActualPos", [](auto const &M) { return M.ScndCtrlActualPos; }}, + {"Im", [](auto const &M) { return M.Im; }}, + {"Itot", [](auto const &M) { return M.Itot; }}, + {"EngineVoltage", [](auto const &M) { return M.EngineVoltage; }}, + {"EnginePower", [](auto const &M) { return M.EnginePower; }}, + {"enrot", [](auto const &M) { return M.enrot; }}, + {"dizel_fill", [](auto const &M) { return M.dizel_fill; }}, + {"eimv_pr", [](auto const &M) { return M.eimv_pr; }}, + {"DynamicBrakeFlag", [](auto const &M) { return M.DynamicBrakeFlag ? 1.0 : 0.0; }}, + }; +} + +std::vector brakes() { + return { + {"BrakeCtrlPos", [](auto const &M) { return M.BrakeCtrlPos; }}, + {"PipePress", [](auto const &M) { return M.PipePress; }}, + {"BrakePress", [](auto const &M) { return M.BrakePress; }}, + {"LocBrakePress", [](auto const &M) { return M.LocBrakePress; }}, + {"BRP", [](auto const &M) { return M.Hamulec->GetBRP(); }}, + {"VRP", [](auto const &M) { return M.Hamulec->GetVRP(); }}, + {"CRP", [](auto const &M) { return M.Hamulec->GetCRP(); }}, + }; +} + +std::vector air() { + return { + {"Compressor", [](auto const &M) { return M.Compressor; }}, + {"CompressorFlag", [](auto const &M) { return M.CompressorFlag ? 1.0 : 0.0; }}, + {"ScndPipePress", [](auto const &M) { return M.ScndPipePress; }}, + }; +} + +std::vector handles() { + std::vector result{ + {"BrakeCtrlPosR", [](auto const &M) { return M.BrakeCtrlPosR; }}, + {"dpMainValve", [](auto const &M) { return M.dpMainValve; }}, + {"HandleCP", [](auto const &M) { return M.Handle->GetCP(); }}, + {"HandleRP", [](auto const &M) { return M.Handle->GetRP(); }}, + {"HandleEP", [](auto const &M) { return M.Handle->GetEP(); }}, + {"LocalBrakePosA", [](auto const &M) { return M.LocalBrakePosA; }}, + {"dpLocalValve", [](auto const &M) { return M.dpLocalValve; }}, + }; + for (int i = 0; i < 5; ++i) { + result.emplace_back(std::format("HandleSound{}", i), [i](auto const &M) { return M.Handle->GetSound(i); }); + } + return result; +} + +std::vector doors() { + std::vector result{ + {"DoorsLocked", [](auto const &M) { return M.Doors.is_locked ? 1.0 : 0.0; }}, + }; + for (int side : {side::right, side::left}) { + auto const name{std::string{side == side::right ? "Right" : "Left"}}; + result.emplace_back(name + "Position", [side](auto const &M) { return M.Doors.instances[side].position; }); + result.emplace_back(name + "Step", [side](auto const &M) { return M.Doors.instances[side].step_position; }); + result.emplace_back(name + "Open", [side](auto const &M) { return M.Doors.instances[side].is_open ? 1.0 : 0.0; }); + result.emplace_back(name + "Closed", [side](auto const &M) { return M.Doors.instances[side].is_closed ? 1.0 : 0.0; }); + result.emplace_back(name + "Permit", [side](auto const &M) { return M.Doors.instances[side].open_permit ? 1.0 : 0.0; }); + } + return result; +} + +std::vector parameters() { + std::vector result{ + {"CategoryFlag", [](auto const &M) { return M.CategoryFlag; }}, + {"TrainType", [](auto const &M) { return M.TrainType; }}, + {"EngineType", [](auto const &M) { return static_cast(M.EngineType); }}, + {"Vmax", [](auto const &M) { return M.Vmax; }}, + {"Mass", [](auto const &M) { return M.Mass; }}, + {"TotalMass", [](auto const &M) { return M.TotalMass; }}, + {"Mred", [](auto const &M) { return M.Mred; }}, + {"Power", [](auto const &M) { return M.Power; }}, + {"HeatingPower", [](auto const &M) { return M.HeatingPower; }}, + {"LightPower", [](auto const &M) { return M.LightPower; }}, + {"BatteryVoltage", [](auto const &M) { return M.BatteryVoltage; }}, + {"Cx", [](auto const &M) { return M.Cx; }}, + {"WheelDiameter", [](auto const &M) { return M.WheelDiameter; }}, + {"TrackW", [](auto const &M) { return M.TrackW; }}, + {"AxleInertialMoment", [](auto const &M) { return M.AxleInertialMoment; }}, + {"NPoweredAxles", [](auto const &M) { return M.NPoweredAxles; }}, + {"NAxles", [](auto const &M) { return M.NAxles; }}, + {"BearingType", [](auto const &M) { return M.BearingType; }}, + {"ADist", [](auto const &M) { return M.ADist; }}, + {"BDist", [](auto const &M) { return M.BDist; }}, + {"NBpA", [](auto const &M) { return M.NBpA; }}, + {"SandCapacity", [](auto const &M) { return M.SandCapacity; }}, + {"BrakeSystem", [](auto const &M) { return static_cast(M.BrakeSystem); }}, + {"BrakeValve", [](auto const &M) { return static_cast(M.BrakeValve); }}, + {"BrakeHandle", [](auto const &M) { return static_cast(M.BrakeHandle); }}, + {"BrakeLocHandle", [](auto const &M) { return static_cast(M.BrakeLocHandle); }}, + {"LocalBrake", [](auto const &M) { return static_cast(M.LocalBrake); }}, + {"ASBType", [](auto const &M) { return M.ASBType; }}, + {"MaxBrakeForce", [](auto const &M) { return M.MaxBrakeForce; }}, + {"P2FTrans", [](auto const &M) { return M.P2FTrans; }}, + {"BrakeMethod", [](auto const &M) { return M.BrakeMethod; }}, + {"HighPipePress", [](auto const &M) { return M.HighPipePress; }}, + {"LowPipePress", [](auto const &M) { return M.LowPipePress; }}, + {"DeltaPipePress", [](auto const &M) { return M.DeltaPipePress; }}, + {"CntrlPipePress", [](auto const &M) { return M.CntrlPipePress; }}, + {"BrakeVolume", [](auto const &M) { return M.BrakeVolume; }}, + {"BrakeVVolume", [](auto const &M) { return M.BrakeVVolume; }}, + {"VeselVolume", [](auto const &M) { return M.VeselVolume; }}, + {"BrakeCylNo", [](auto const &M) { return M.BrakeCylNo; }}, + {"BrakeCylRadius", [](auto const &M) { return M.BrakeCylRadius; }}, + {"BrakeCylDist", [](auto const &M) { return M.BrakeCylDist; }}, + {"BrakeCylSpring", [](auto const &M) { return M.BrakeCylSpring; }}, + {"BrakeSlckAdj", [](auto const &M) { return M.BrakeSlckAdj; }}, + {"BrakeRigEff", [](auto const &M) { return M.BrakeRigEff; }}, + {"RapidMult", [](auto const &M) { return M.RapidMult; }}, + {"RapidVel", [](auto const &M) { return M.RapidVel; }}, + {"BrakeValveSize", [](auto const &M) { return M.BrakeValveSize; }}, + {"MinCompressor", [](auto const &M) { return M.MinCompressor; }}, + {"MaxCompressor", [](auto const &M) { return M.MaxCompressor; }}, + {"CompressorSpeed", [](auto const &M) { return M.CompressorSpeed; }}, + {"EmergencyValveOn", [](auto const &M) { return M.EmergencyValveOn; }}, + {"AirLeakRate", [](auto const &M) { return M.AirLeakRate; }}, + {"BrakeCtrlPosNo", [](auto const &M) { return M.BrakeCtrlPosNo; }}, + {"MainCtrlPosNo", [](auto const &M) { return M.MainCtrlPosNo; }}, + {"ScndCtrlPosNo", [](auto const &M) { return M.ScndCtrlPosNo; }}, + {"LightsPosNo", [](auto const &M) { return M.LightsPosNo; }}, + {"MBrake", [](auto const &M) { return M.MBrake ? 1.0 : 0.0; }}, + {"StopBrakeDecc", [](auto const &M) { return M.StopBrakeDecc; }}, + {"BrakeDelays", [](auto const &M) { return M.BrakeDelays; }}, + {"BrakeOpModes", [](auto const &M) { return M.BrakeOpModes; }}, + {"Imin", [](auto const &M) { return M.Imin; }}, + {"Imax", [](auto const &M) { return M.Imax; }}, + {"EIMCtrlType", [](auto const &M) { return M.EIMCtrlType; }}, + {"Volume", [](auto const &M) { return M.Volume; }}, + {"PantVolume", [](auto const &M) { return M.PantVolume; }}, + }; + for (int i = 0; i < 5; ++i) { + result.emplace_back(std::format("MaxBrakePress{}", i), [i](auto const &M) { return M.MaxBrakePress[i]; }); + } + for (int i = 0; i < 3; ++i) { + result.emplace_back(std::format("BrakeCylMult{}", i), [i](auto const &M) { return M.BrakeCylMult[i]; }); + } + return result; +} + +std::vector operator+(std::vector Left, std::vector const &Right) { + Left.insert(Left.end(), Right.begin(), Right.end()); + return Left; +} + +} // namespace channels + +namespace { + +bool environment(char const *Name) { + auto const *value{std::getenv(Name)}; + return value != nullptr && value[0] != '\0' && value[0] != '0'; +} + +std::string number(double Value) { + // the shortest text which reads back as the same double + std::array text{}; + auto const result{std::to_chars(text.data(), text.data() + text.size(), Value)}; + return {text.data(), result.ptr}; +} + +// a line of the file, whichever line ends the checkout gave it +bool read_line(std::ifstream &File, std::string &Line) { + if (false == static_cast(std::getline(File, Line))) { + return false; + } + if (false == Line.empty() && Line.back() == '\r') { + Line.pop_back(); + } + return true; +} + +std::vector split(std::string const &Line) { + std::vector fields; + std::stringstream stream(Line); + std::string field; + while (std::getline(stream, field, ',')) { + fields.push_back(field); + } + if (false == Line.empty() && Line.back() == ',') { + fields.emplace_back(); + } + return fields; +} + +bool same(std::string const &Recorded, std::string const &Now, bool const Exact) { + if (Recorded == Now) { + return true; + } + if (Exact) { + return false; + } + char *end{nullptr}; + auto const recorded{std::strtod(Recorded.c_str(), &end)}; + auto const now{std::strtod(Now.c_str(), &end)}; + if (std::isnan(recorded) || std::isnan(now)) { + return std::isnan(recorded) && std::isnan(now); + } + return std::abs(recorded - now) <= 1e-9 * std::max(std::abs(recorded), std::abs(now)) + 1e-12; +} + +} // namespace + +trace::trace(std::string Name, std::vector Channels, int Every, bool Figures) + : m_name(std::move(Name)), m_channels(std::move(Channels)), m_every(Every), m_keep_figures(Figures) {} + +void trace::sample(double Time, TMoverParameters const &Vehicle) { + // the invariants on every step, the first time each is broken + for (auto const &invariant : invariants()) { + if (std::ranges::find(m_broken, invariant.name) != m_broken.end()) { + continue; + } + if (false == invariant.holds(Vehicle, Time, m_sampled ? &m_before : nullptr)) { + m_broken.push_back(invariant.name); + m_violations.push_back(std::format("invariant \"{}\" broken in {} at {:.3f} s{}: {}", invariant.name, m_name, Time, + m_last_event.empty() ? std::string{} : std::format(", after \"{}\"", m_last_event), invariant.explain(Vehicle))); + } + } + m_before = {Time, Vehicle.V, Vehicle.DistCounter}; + m_sampled = true; + m_figures.sample(Time, Vehicle); + + if (m_step++ % m_every != 0) { + return; + } + // the time is a label, summed up from the steps: milliseconds are all it needs + auto row{std::format("{:.3f}", Time)}; + for (auto const &channel : m_channels) { + row += "," + number(channel.value(Vehicle)); + } + row += "," + m_event; + m_event.clear(); + m_rows.push_back(row); +} + +void trace::mark(std::string const &Event) { + m_event += (m_event.empty() ? "" : " ") + Event; + m_last_event = Event; + m_figures.mark(Event); +} + +void trace::check_figures(std::filesystem::path const &Path, bool const Exact) const { + // like the samples, the key figures are recorded on the code before a change and compared after it + std::ifstream file(Path, std::ios::binary); + if (false == file.is_open()) { + if (Exact) { + FAIL_CHECK("no key figures " << Path.string() << ", run the tests with EU07_UNIT_TESTS_UPDATE_GOLDEN=1 on the code before the change to record them"); + } + return; + } + std::vector recorded; + std::string line; + while (read_line(file, line)) { + recorded.push_back(line); + } + auto const differences{figures::compare(recorded, m_figures.lines())}; + if (false == differences.empty()) { + std::string text; + for (auto const &difference : differences) { + text += "\n " + difference; + } + FAIL_CHECK("key figures of " << m_name << " differ:" << text); + } +} + +void trace::check() const { + for (std::size_t i = 0; i < m_violations.size(); ++i) { + if (false == excused(m_name, m_broken[i])) { + FAIL_CHECK(m_violations[i]); + } + } + + std::string header{"time"}; + for (auto const &channel : m_channels) { + header += "," + channel.name; + } + header += ",event"; + + // no spaces in the file names: the build depends on the files, and make doesn't take to them + auto name{m_name}; + std::ranges::replace(name, ' ', '_'); + std::filesystem::path const filename{name + ".csv"}; + auto const path{std::filesystem::path{EU07_UNIT_TESTS_DATA} / "golden" / filename}; + auto figurespath{path}; + figurespath.replace_extension(".figures"); + if (environment("EU07_UNIT_TESTS_UPDATE_GOLDEN")) { + record(path, figurespath, header); + return; + } + + // the samples aren't kept in the repository, they're recorded on the code before a change and compared after it + auto const exact{environment("EU07_UNIT_TESTS_GOLDEN_EXACT")}; + if (m_keep_figures) { + check_figures(figurespath, exact); + } + check_samples(path, header, exact); +} + +void trace::record(std::filesystem::path const &Path, std::filesystem::path const &FiguresPath, std::string const &Header) const { + std::filesystem::create_directories(Path.parent_path()); + std::ofstream file(Path, std::ios::binary); + file << Header << "\n"; + for (auto const &row : m_rows) { + file << row << "\n"; + } + if (m_keep_figures) { + std::ofstream figuresfile(FiguresPath, std::ios::binary); + for (auto const &line : m_figures.lines()) { + figuresfile << line << "\n"; + } + } + MESSAGE("recorded the golden trace " << Path.string()); +} + +void trace::check_samples(std::filesystem::path const &path, std::string const &header, bool const exact) const { + std::ifstream file(path, std::ios::binary); + if (false == file.is_open()) { + if (exact) { + FAIL_CHECK("no golden trace " << path.string() << ", run the tests with EU07_UNIT_TESTS_UPDATE_GOLDEN=1 on the code before the change to record it"); + } + return; + } + std::string line; + read_line(file, line); + if (line != header) { + FAIL_CHECK("golden trace " << m_name << " has other channels: " << line << ", the test records " << header); + return; + } + auto const names{split(header)}; + std::size_t row{0}; + while (read_line(file, line)) { + if (row >= m_rows.size()) { + FAIL_CHECK("golden trace " << m_name << " is longer: " << m_rows.size() << " rows now, more recorded"); + return; + } + auto const recorded{split(line)}; + auto const now{split(m_rows[row])}; + for (std::size_t field = 0; field < names.size(); ++field) { + auto const recordedvalue{field < recorded.size() ? recorded[field] : std::string{}}; + auto const nowvalue{field < now.size() ? now[field] : std::string{}}; + if (false == same(recordedvalue, nowvalue, exact || names[field] == "event")) { + FAIL_CHECK("golden trace " << m_name << " differs first at time " << now.front() << " in " << names[field] << ": " << nowvalue + << " now, " << recordedvalue << " recorded\n now: " << m_rows[row] << "\n recorded: " << line); + return; + } + } + ++row; + } + if (row != m_rows.size()) { + FAIL_CHECK("golden trace " << m_name << " is shorter: " << m_rows.size() << " rows now, " << row << " recorded"); + } +} + +} // namespace test diff --git a/tests/unit/golden.h b/tests/unit/golden.h new file mode 100644 index 000000000..cb82ed0da --- /dev/null +++ b/tests/unit/golden.h @@ -0,0 +1,90 @@ +#pragma once + +#include +#include +#include +#include + +#include "McZapkie/MOVER.h" +#include "figures.h" +#include "invariants.h" + +namespace test { + +// one value of the vehicle a trace follows +struct channel { + std::string name; + std::function value; +}; + +// sets of channels for the usual subjects of a test, which can be put together +namespace channels { +std::vector motion(); // speed, distance, forces +std::vector traction(); // controllers, current, voltage, engine +std::vector brakes(); // pressures of the brake pipe, cylinders and reservoirs +std::vector air(); // compressor and main reservoir +std::vector handles(); // the driver's brake handle and the local brake handle, with their sounds +std::vector doors(); // both sides' doors and steps, their permits and the lock +std::vector parameters(); // what the vehicle definition sets, as the loader read it +std::vector operator+(std::vector Left, std::vector const &Right); +} // namespace channels + +// characterisation of the simulation: the values of the channels over a run of a vehicle, compared with the +// ones the code gave when the trace got recorded, in tests/unit/data/golden/.csv (spaces as underscores). a difference fails the +// test, naming the channel and the time it shows up first. the samples are tens of megabytes and only mean +// something against the code before a change, so they stay out of the repository: running the tests with +// EU07_UNIT_TESTS_UPDATE_GOLDEN=1 records them on the code before the change, and after it they're compared. +// a trace with nothing recorded checks only the invariants and its test's own CHECKs, as the CI does. +// +// values are compared within a relative tolerance of 1e-9 (the CI builds with GCC and glibc, whose maths +// functions can differ from MSVC's in the last bits), or exactly with EU07_UNIT_TESTS_GOLDEN_EXACT=1, which is the check to +// run for a change which shouldn't alter anything at all, and which fails a trace with nothing recorded. +// +// besides the samples, a trace records the key figures of the run (figures.h) in .figures, the few +// lines a person reads of what a change altered, and checks the invariants (invariants.h) on every step +// +// test::trace trace{"eu07 notch up", test::channels::motion() + test::channels::traction()}; +// eu07.record(trace); +// ... run the vehicle ... +// trace.check(); +class trace { +public: + // samples every Every-th step of the vehicle: 100, a second at the usual step, is plenty for a run of minutes, + // as a difference stays once the simulation has taken it in + // Figures: whether to keep the key figures, which a trace sampled by hand, at times of its own, has no use for + trace(std::string Name, std::vector Channels, int Every = 10, bool Figures = true); + + // called by the vehicle after every step + void sample(double Time, TMoverParameters const &Vehicle); + // adds an event of the test to the trace, so a difference can be told apart from the moment of the action + void mark(std::string const &Event); + + // compares the trace and its key figures (figures.h) with the recorded ones, or records them, and reports the + // invariants broken + void check() const; + // the invariants broken in the trace (see invariants.h), the first time each, excused or not + std::vector const &violations() const { return m_violations; } + +private: + void check_figures(std::filesystem::path const &Path, bool Exact) const; + // writes the samples and the key figures, as recorded on the code before a change + void record(std::filesystem::path const &Path, std::filesystem::path const &FiguresPath, std::string const &Header) const; + // compares the samples with the recorded ones + void check_samples(std::filesystem::path const &path, std::string const &header, bool exact) const; + + std::string m_name; + std::vector m_channels; + int m_every; + long m_step{0}; + std::vector m_rows; // formatted, as in the file + std::string m_event; // marked since the last row + std::string m_last_event; // the last one marked, for the invariants broken after it + snapshot m_before{}; + bool m_sampled{false}; + std::vector m_broken; // the names of the invariants broken so far + std::vector m_violations; + bool m_keep_figures; + figures m_figures; +}; + +} // namespace test diff --git a/tests/unit/invariants.cpp b/tests/unit/invariants.cpp new file mode 100644 index 000000000..4fafeb04d --- /dev/null +++ b/tests/unit/invariants.cpp @@ -0,0 +1,163 @@ +#include "stdafx.h" +#include "invariants.h" + +#include +#include +#include +#include + +namespace test { + +namespace { + +std::string values(std::initializer_list> Values) { + std::string result; + for (auto const &[name, value] : Values) { + result += std::format("{}{}={:.6g}", result.empty() ? "" : " ", name, value); + } + return result; +} + +bool electric(TMoverParameters const &M) { + return M.EngineType == TEngineType::ElectricSeriesMotor || M.EngineType == TEngineType::ElectricInductionMotor; +} + +double max_brake_press(TMoverParameters const &M) { + auto result{0.0}; + for (auto const press : M.MaxBrakePress) { + result = std::max(result, press); + } + return result; +} + +// the margin over a limit which a step of the simulation may overshoot it by +constexpr double pressure_margin{0.05}; +// the highest acceleration of a vehicle, 5 g, which a collision goes well over +constexpr double highest_acceleration{5.0 * 9.81}; + +} // namespace + +std::vector const &invariants() { + static std::vector const rules{ + {"every value a number", + [](auto const &M, double, auto const *) { + std::initializer_list const values{M.V, M.AccS, M.Ft, M.Fb, M.Im, M.Itot, M.EngineVoltage, + M.enrot, M.PipePress, M.BrakePress, M.LocBrakePress, M.ScndPipePress, M.Compressor, M.BatteryVoltage}; + return std::ranges::all_of(values, [](double const Value) { return std::isfinite(Value); }); + }, + [](auto const &M) { + return values({{"V", M.V}, {"AccS", M.AccS}, {"Ft", M.Ft}, {"Fb", M.Fb}, {"Im", M.Im}, {"Itot", M.Itot}, {"EngineVoltage", M.EngineVoltage}, + {"enrot", M.enrot}, {"PipePress", M.PipePress}, {"BrakePress", M.BrakePress}, {"LocBrakePress", M.LocBrakePress}, + {"ScndPipePress", M.ScndPipePress}, {"Compressor", M.Compressor}, {"BatteryVoltage", M.BatteryVoltage}}); + }}, + {"brake cylinders between empty and the vehicle's highest pressure", + [](auto const &M, double, auto const *) { + // a step overshoots the setting a little: the EU07's LSt fills its cylinders to 4.0 bar of its 3.8 + auto const limit{1.1 * max_brake_press(M) + pressure_margin}; + return M.BrakePress >= -pressure_margin && M.BrakePress <= limit && M.LocBrakePress >= -pressure_margin && M.LocBrakePress <= limit; + }, + [](auto const &M) { return values({{"BrakePress", M.BrakePress}, {"LocBrakePress", M.LocBrakePress}, {"max(MaxBrakePress)", max_brake_press(M)}}); }}, + {"pipes between empty and the highest governor setting", + [](auto const &M, double, auto const *) { + // all the air comes from the main reservoirs, which the game sets up to 10 bar, some (the Impuls) to 12; + // the driver's valve in its filling position joins the brake pipe to them + auto const highest{1.01 * std::max(10.0, M.MaxCompressor) + pressure_margin}; + std::initializer_list const pressures{M.PipePress, M.ScndPipePress, M.Compressor}; + return std::ranges::all_of(pressures, [highest](double const Press) { return Press >= -pressure_margin && Press <= highest; }); + }, + [](auto const &M) { return values({{"PipePress", M.PipePress}, {"ScndPipePress", M.ScndPipePress}, {"Compressor", M.Compressor}}); }}, + {"main reservoir no higher than the compressor's governor", + [](auto const &M, double, auto const *) { return M.MaxCompressor <= 0.0 || M.Compressor <= 1.01 * M.MaxCompressor + pressure_margin; }, + [](auto const &M) { return values({{"Compressor", M.Compressor}, {"MaxCompressor", M.MaxCompressor}}); }}, + {"no motor current with the line breaker open", + [](auto const &M, double, auto const *) { return M.Mains || false == electric(M) || std::abs(M.Im) < 1.0; }, + [](auto const &M) { return values({{"Mains", M.Mains}, {"Im", M.Im}, {"Itot", M.Itot}}); }}, + {"no motor current with the line contactors open, but for the dynamic brake", + [](auto const &M, double, auto const *) { + // the dynamic brake closes its own circuit of the motors and the resistors + return M.StLinFlag || M.DynamicBrakeFlag || M.EngineType != TEngineType::ElectricSeriesMotor || std::abs(M.Im) < 1.0; + }, + [](auto const &M) { + return values({{"StLinFlag", M.StLinFlag}, {"DynamicBrakeFlag", M.DynamicBrakeFlag}, {"Im", M.Im}, {"MainCtrlActualPos", M.MainCtrlActualPos}}); + }}, + {"no tractive force without power", + [](auto const &M, double, auto const *) { return M.Power >= 0.01 || std::abs(M.Ft) < 1.0; }, + [](auto const &M) { return values({{"Power", M.Power}, {"Ft", M.Ft}}); }}, + {"engine fill between none and full", + [](auto const &M, double, auto const *) { return M.dizel_fill >= 0.0 && M.dizel_fill <= 1.0; }, + [](auto const &M) { return values({{"dizel_fill", M.dizel_fill}}); }}, + {"diesel engine turning forward, no faster than its top speed", + [](auto const &M, double, auto const *) { + // the governor lets it overshoot a little (the SN61 by 1%) + return M.EngineType != TEngineType::DieselEngine || (M.enrot >= 0.0 && M.enrot <= 1.05 * M.dizel_nmax); + }, + [](auto const &M) { return values({{"enrot", M.enrot}, {"dizel_nmax", M.dizel_nmax}}); }}, + {"battery no higher than its nominal voltage", + [](auto const &M, double, auto const *) { return M.NominalBatteryVoltage <= 0.0 || M.BatteryVoltage <= 1.05 * M.NominalBatteryVoltage; }, + [](auto const &M) { return values({{"BatteryVoltage", M.BatteryVoltage}, {"NominalBatteryVoltage", M.NominalBatteryVoltage}}); }}, + {"distance counted as the speed runs", + [](auto const &M, double Time, auto const *Before) { + if (Before == nullptr) { + return true; + } + // the speed of the step is between the speeds at its ends, or a bit off it, as the movement is integrated; + // through a standstill, down to none + auto const dt{Time - Before->time}; + auto const covered{(M.DistCounter - Before->DistCounter) * 1000.0}; + auto const lowest{M.V * Before->V > 0.0 ? std::min(std::abs(M.V), std::abs(Before->V)) : 0.0}; + auto const highest{std::max(std::abs(M.V), std::abs(Before->V))}; + return covered >= lowest * dt * 0.9 - 0.01 && covered <= highest * dt * 1.1 + 0.01; + }, + [](auto const &M) { return values({{"V", M.V}, {"DistCounter", M.DistCounter}}); }}, + {"no acceleration over 5 g", + [](auto const &M, double Time, auto const *Before) { + return Before == nullptr || std::abs(M.V - Before->V) <= highest_acceleration * (Time - Before->time) + 0.01; + }, + [](auto const &M) { return values({{"V", M.V}, {"AccS", M.AccS}}); }}, + {"hand brake applied, the wheels braked on the move", + [](auto const &M, double, auto const *) { return false == M.MBrake || M.ManualBrakePos == 0 || std::abs(M.V) < 0.5 || std::abs(M.Fb) > 0.0; }, + [](auto const &M) { return values({{"ManualBrakePos", M.ManualBrakePos}, {"V", M.V}, {"Fb", M.Fb}}); }}, + }; + return rules; +} + +namespace { + +struct excuse { + char const *trace; // a part of the trace's name + char const *invariant; + char const *reason; +}; + +std::vector const excuses{ + // what the tests do + {"couplers/collision/", "no acceleration over 5 g", "vehicles run into each other"}, + {"controls/collision/", "no acceleration over 5 g", "vehicles run into each other"}, + {"couplers/collision/", "distance counted as the speed runs", "the buffers throw the vehicle back within a step"}, + {"switches_live/", "no acceleration over 5 g", "sampled by hand, a second apart in the trace for 2 s run"}, + {"switches_live/", "distance counted as the speed runs", "sampled by hand, a second apart in the trace for 2 s run"}, + {"brake handles/", "main reservoir no higher than the compressor's governor", "the test holds the main reservoir at 8 bar"}, + {"local brake/", "main reservoir no higher than the compressor's governor", "the test holds the main reservoir at 8 bar"}, + {"brake cycle/at speed/", "brake cylinders between empty and the vehicle's highest pressure", "the rapid step of the valves at speed"}, + // the vehicles as they are + {"elf/2", "main reservoir no higher than the compressor's governor", + "the units' reservoirs share the supply pipe: the A car's governor stops at 9 bar, the C car's at 8.5"}, + {"sm03_v1/", "brake cylinders between empty and the vehicle's highest pressure", + "its K valve fills the cylinders to the auxiliary reservoir's pressure (4.27 bar), over MaxBP 3.5"}, + {"eu04_v1/", "brake cylinders between empty and the vehicle's highest pressure", + "its K valve fills the cylinders to the auxiliary reservoir's pressure (3.19 bar), over MaxBP 2.8"}, + {"sn61", "brake cylinders between empty and the vehicle's highest pressure", + "its ESt3d_PZZ valve fills the cylinders to the auxiliary reservoir's pressure in an emergency (4.6 bar), over MaxBP 3.8"}, + {"sm03_v1/", "diesel engine turning forward, no faster than its top speed", "a lower gear at speed drives the engine over its top speed, by 10%"}, + {"wmb10_v1/", "diesel engine turning forward, no faster than its top speed", "a lower gear at speed drives the engine over its top speed, by 25%"}, + {"controls/full physics/", "no acceleration over 5 g", "the couplers' slack taken up as the train starts throws the wagons about at up to 5.7 g"}, + {"controls/full physics/", "distance counted as the speed runs", "the couplers' slack taken up as the train starts throws the wagons back within a step"}, +}; + +} // namespace + +bool excused(std::string_view const Trace, std::string_view const Invariant) { + return std::ranges::any_of(excuses, [&](excuse const &Excuse) { return Excuse.invariant == Invariant && Trace.find(Excuse.trace) != std::string_view::npos; }); +} + +} // namespace test diff --git a/tests/unit/invariants.h b/tests/unit/invariants.h new file mode 100644 index 000000000..491edc84b --- /dev/null +++ b/tests/unit/invariants.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include +#include + +#include "McZapkie/MOVER.h" + +namespace test { + +// what the rules over time need of the step before +struct snapshot { + double time; + double V; + double DistCounter; +}; + +// a rule of physics or of the vehicle's circuits, which holds for every vehicle after every step, whatever +// the test does with it. every golden trace checks them as it samples (see golden.h), on every step, not only +// on the recorded ones, and with no comparison to a recorded file, so they hold on any compiler. a change which +// alters the behaviour on purpose records the traces anew, and the rules still check the new behaviour makes sense. +struct invariant { + std::string name; + // Before is null at the first step of a trace + std::function holds; + // the values the rule looks at, shown when it's broken + std::function explain; +}; + +std::vector const &invariants(); + +// the traces where the game breaks a rule, as it is, with the reason (see COVERAGE.md, what the tests found) +bool excused(std::string_view Trace, std::string_view Invariant); + +} // namespace test diff --git a/tests/unit/main.cpp b/tests/unit/main.cpp index 451dd4c7b..cc801ba18 100644 --- a/tests/unit/main.cpp +++ b/tests/unit/main.cpp @@ -2,7 +2,7 @@ #include "stdafx.h" #include "doctest.h" -#include "utilities/Globals.h" +#include "vehicle.h" namespace { @@ -13,8 +13,8 @@ class deterministic_random : public doctest::IReporter { public: explicit deterministic_random(doctest::ContextOptions const &) {} - void test_case_start(doctest::TestCaseData const &) override { reseed(); } - void test_case_reenter(doctest::TestCaseData const &) override { reseed(); } + void test_case_start(doctest::TestCaseData const &) override { test::reseed_random(); } + void test_case_reenter(doctest::TestCaseData const &) override { test::reseed_random(); } // the rest of doctest's reporter interface: a listener reports nothing, the console reporter does that void report_query(doctest::QueryData const &) override { /* nothing to report */ } @@ -27,12 +27,6 @@ public: void log_assert(doctest::AssertData const &) override { /* nothing to report */ } void log_message(doctest::MessageData const &) override { /* nothing to report */ } void test_case_skipped(doctest::TestCaseData const &) override { /* nothing to report */ } - -private: - static void reseed() { - Global.random_engine.seed(1); - Global.local_random_engine.seed(1); - } }; } // namespace diff --git a/tests/unit/real_vehicles.h b/tests/unit/real_vehicles.h new file mode 100644 index 000000000..0928ca2d5 --- /dev/null +++ b/tests/unit/real_vehicles.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace test { + +// the real vehicle definitions of data/fiz, as paths for definition::fiz(): every .json file there, sorted. +// vehicles the game defines by including others are vehicles of their own too (201e-rn, included by 201e-w) +std::vector const &real_vehicles(); + +} // namespace test diff --git a/tests/unit/scenario.cpp b/tests/unit/scenario.cpp new file mode 100644 index 000000000..b933a0ab5 --- /dev/null +++ b/tests/unit/scenario.cpp @@ -0,0 +1,80 @@ +#include "stdafx.h" +#include "scenario.h" + +#include "McZapkie/hamulce.h" + +namespace test { + +scenario::scenario(std::string Name, std::vector Channels, track const &Track) : m_name(std::move(Name)), m_channels(std::move(Channels)), m_track(Track) { + reseed_random(); +} + +scenario &scenario::add(std::string const &Path, int Cab, int Coupling, double Velocity, double Gap) { + m_train.add(definition::fiz(Path), Cab, Coupling, Velocity, Gap); + return *this; +} + +void scenario::record() { + // on the first action, once the train is complete + if (false == m_traces.empty()) { + return; + } + for (std::size_t i = 0; i < m_train.size(); ++i) { + m_traces.push_back(std::make_unique(std::format("{}/{}", m_name, i), m_channels, 100)); + m_train[i].record(*m_traces.back()); + } +} + +void scenario::act(std::string const &Event) { + record(); + for (auto const &trace : m_traces) { + trace->mark(Event); + } +} + +void scenario::wait(double Seconds) { + record(); + drive(driver(), m_track, Seconds); +} + +bool scenario::start_up() { + record(); + return test::start_up(driver(), m_track, 300.0, m_traces.front().get()); +} + +void scenario::get_going() { + act("get going"); + release_brakes(cab()); + for (int i = 0; i < 3 && cab().DirActive <= 0; ++i) { + cab().DirectionForward(); + } + wait(20.0); +} + +void scenario::full_power(double Seconds) { + auto &controlling{driver().controlling}; + controlling.IncMainCtrl(1); + for (int i = 0; i < 90 && false == controlling.StLinFlag; ++i) { + wait(1.0); + } + for (int i = 0; i < 60 && false == controlling.IsMainCtrlMaxPowerPos(); ++i) { + controlling.IncMainCtrl(1); + wait(1.0); + } + wait(Seconds); +} + +void scenario::notch_back(int Position) { + auto &controlling{driver().controlling}; + while (controlling.MainCtrlPos > Position && controlling.DecMainCtrl(1)) { + wait(1.0); + } +} + +void scenario::check() const { + for (auto const &trace : m_traces) { + trace->check(); + } +} + +} // namespace test diff --git a/tests/unit/scenario.h b/tests/unit/scenario.h new file mode 100644 index 000000000..fff4d9619 --- /dev/null +++ b/tests/unit/scenario.h @@ -0,0 +1,63 @@ +#pragma once + +#include +#include +#include + +#include "consist.h" +#include "drive.h" +#include "golden.h" + +namespace test { + +// a scenario of a test: a train of real vehicles (one vehicle is a train too), a driver in the front one, +// and a golden trace for each vehicle (data/golden//.csv) with the events of +// the scenario in them: +// test::scenario scenario{"doors/elf", test::channels::motion() + test::channels::doors()}; +// scenario.add("elf_v1/21we-a", 1).add("elf_v1/21we-b").add("elf_v1/21we-c"); +// scenario.start_up(); +// scenario.act("open left"); +// scenario.cab().OperateDoors(side::left, true); +// scenario.wait(10.0); +// scenario.check(); +class scenario { +public: + scenario(std::string Name, std::vector Channels, track const &Track = {0.15, 0.0, catenary_3kv}); + + // adds a real vehicle at the back, see consist::add() + scenario &add(std::string const &Path, int Cab = 0, int Coupling = consist::full_coupling, double Velocity = 0.0, double Gap = 0.0); + + consist &train() { return m_train; } + TMoverParameters &vehicle(std::size_t Index) { return m_train[Index].mover(); } + TMoverParameters &cab() { return m_train[0].mover(); } + driven driver() { return driving(m_train); } + track &line() { return m_track; } + + // marks the event in the traces, the action follows it + void act(std::string const &Event); + // runs the train with the driver attending to it + void wait(double Seconds); + // gets the train ready to run, see test::start_up(); returns whether it did + bool start_up(); + // releases the brakes and sets the direction forward, for a train to move off + void get_going(); + // the main controller up to its full power position, a notch a second, and Seconds more there. the line + // contactors close at the first position, after their delay (InitialCtrlDelay) and once the pressure switch + // lets them (the cylinders under 2 bar, the pipe over 3.6), so it waits there for them, as the AI does + void full_power(double Seconds); + // the main controller back to the given position, a notch a second + void notch_back(int Position); + // checks the traces + void check() const; + +private: + void record(); + + std::string m_name; + std::vector m_channels; + track m_track; + consist m_train; + std::vector> m_traces; +}; + +} // namespace test diff --git a/tests/unit/test_auxiliaries.cpp b/tests/unit/test_auxiliaries.cpp new file mode 100644 index 000000000..bb823a4cf --- /dev/null +++ b/tests/unit/test_auxiliaries.cpp @@ -0,0 +1,112 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scenario.h" +#include "drive.h" + +// characterisation of the auxiliary circuits of vehicles: the battery drained and charged, and the air of the +// pantographs kept in their own tank, whose pressure switch opens the line breaker, each in a scenario on real +// vehicles recorded in golden traces (data/golden/auxiliaries/) + +using test::scenario; +using namespace test::channels; + +namespace { + +std::vector auxiliaries() { + return { + {"BatteryVoltage", [](auto const &M) { return M.BatteryVoltage; }}, + {"Battery", [](auto const &M) { return M.Battery ? 1.0 : 0.0; }}, + {"ConverterFlag", [](auto const &M) { return M.ConverterFlag ? 1.0 : 0.0; }}, + {"Power110vIsAvailable", [](auto const &M) { return M.Power110vIsAvailable ? 1.0 : 0.0; }}, + {"PantPress", [](auto const &M) { return M.PantPress; }}, + {"PantVolume", [](auto const &M) { return M.PantVolume; }}, + {"PantCompFlag", [](auto const &M) { return M.PantCompFlag ? 1.0 : 0.0; }}, + {"PantPressSwitchActive", [](auto const &M) { return M.PantPressSwitchActive ? 1.0 : 0.0; }}, + {"PantPressLockActive", [](auto const &M) { return M.PantPressLockActive ? 1.0 : 0.0; }}, + }; +} + +} // namespace + +TEST_CASE("auxiliaries: the battery drained by the lights and the pantographs' compressor, until nothing starts") { + // without the converter the battery falls quickly to 90 V, then slowly as the lights and the small compressor + // draw on it; at 70 V (110 / 1.57) it opens the line breaker, and the locomotive doesn't start again + scenario scenario{"auxiliaries/battery/eu07 drained", traction() + auxiliaries()}; + scenario.add("4e_v1/4e", 1); + REQUIRE(scenario.start_up()); + auto &eu07{scenario.cab()}; + REQUIRE(eu07.BatteryVoltage == doctest::Approx(eu07.NominalBatteryVoltage)); + scenario.act("converter off, lights on, small compressor on"); + eu07.ConverterSwitch(false); + eu07.iLights[end::front] = 1 | 2 | 4; + eu07.iLights[end::rear] = 16; + eu07.PantCompFlag = true; + scenario.wait(30.0); + CHECK(eu07.BatteryVoltage < 0.83 * eu07.NominalBatteryVoltage); + CHECK(eu07.Mains); + scenario.wait(5000.0); + CHECK(eu07.BatteryVoltage < eu07.NominalBatteryVoltage / 1.57); + CHECK_FALSE(eu07.Mains); + scenario.act("start up again"); + CHECK_FALSE(scenario.start_up()); + CHECK(eu07.BatteryVoltage < eu07.NominalBatteryVoltage / 1.57); + scenario.check(); +} + +TEST_CASE("auxiliaries: the battery switched off, and charged by the converter") { + scenario scenario{"auxiliaries/battery/eu07 switched off", traction() + auxiliaries()}; + scenario.add("4e_v1/4e", 1); + REQUIRE(scenario.start_up()); + auto &eu07{scenario.cab()}; + scenario.act("converter off"); + eu07.ConverterSwitch(false); + scenario.wait(10.0); + auto const discharged{eu07.BatteryVoltage}; + CHECK(discharged < 0.83 * eu07.NominalBatteryVoltage); + // the converter charges it back at once + scenario.act("converter on"); + eu07.ConverterSwitch(true); + scenario.wait(10.0); + CHECK(eu07.BatteryVoltage == doctest::Approx(eu07.NominalBatteryVoltage)); + // switched off, the battery falls to 90 V at once, then holds + scenario.act("battery off"); + eu07.MainSwitch(false); + eu07.ConverterSwitch(false); + eu07.BatterySwitch(false); + scenario.wait(10.0); + CHECK(eu07.BatteryVoltage == doctest::Approx(discharged).epsilon(0.01)); + scenario.check(); +} + +TEST_CASE("auxiliaries: the pantographs on their own tank, leaking until the pressure switch opens the line breaker") { + // the pantographs cut off the main reservoir (bPantKurek3 off) are held up by the air of their own small tank, + // which leaks; under MinPress the pressure switch, primed at 4.6 bar, opens the line breaker. the small + // compressor pumps the tank up to 4.8 bar, priming the switch again. (an electric unit's switch turns its + // converter off instead; the EW58's pantographs have an automatic valve to the main reservoir, which keeps + // their tank up) + scenario scenario{"auxiliaries/pantograph tank/eu07", traction() + auxiliaries()}; + scenario.add("4e_v1/4e", 1); + REQUIRE(scenario.start_up()); + auto &eu07{scenario.cab()}; + // started up on the small compressor, the tank gets joined to the main reservoir, which primes the switch + scenario.act("tank joined to the main reservoir"); + eu07.bPantKurek3 = true; + scenario.wait(30.0); + REQUIRE(eu07.PantPressSwitchActive); + scenario.act("tank cut off the main reservoir"); + eu07.bPantKurek3 = false; + eu07.PantCompFlag = false; + scenario.wait(600.0); + CHECK(eu07.PantPress < eu07.EnginePowerSource.CollectorParameters.MinPress); + CHECK_FALSE(eu07.PantPressSwitchActive); + CHECK_FALSE(eu07.Mains); + scenario.act("small compressor on"); + eu07.PantCompFlag = true; + for (int i = 0; i < 60 && eu07.PantCompFlag; ++i) { + scenario.wait(10.0); + } + CHECK_FALSE(eu07.PantCompFlag); + CHECK(eu07.PantPress >= 4.6); // stopped at 4.8, leaking since + CHECK(eu07.PantPressSwitchActive); + scenario.check(); +} diff --git a/tests/unit/test_brake_cycle.cpp b/tests/unit/test_brake_cycle.cpp new file mode 100644 index 000000000..1dc5a84c1 --- /dev/null +++ b/tests/unit/test_brake_cycle.cpp @@ -0,0 +1,194 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" +#include "golden.h" +#include "real_vehicles.h" + +#include "McZapkie/hamulce.h" + +#include +#include + +using test::definition; +using test::vehicle; + +// characterisation of the brakes of every real vehicle: the brake pipe driven through a whole cycle, as the +// locomotive of a train would drive it, and the distributor valve, the cylinders and the reservoirs followed +// in golden traces (data/golden/brake cycle/). pressures in bar, the pipe runs at 5.0 + +namespace { + +struct stage { + char const *name; + double pipe; // held brake pipe pressure + double seconds; +}; + +// charging, the service application in steps, release, the overcharge used to speed the release up and its +// drop back, the emergency application and recharging after it +std::array const cycle{{ + {"charge", 5.0, 90.0}, {"light", 4.6, 20.0}, {"medium", 4.2, 20.0}, {"full service", 3.5, 20.0}, + {"release", 5.0, 40.0}, {"overcharge", 5.4, 15.0}, {"back to 5", 5.0, 45.0}, {"emergency", 0.0, 15.0}, + {"recharge", 5.0, 90.0}, +}}; + +// returns the pressure in the cylinders at the end of each stage, by the name of the stage +std::map> run_cycle(vehicle &Vehicle, test::trace &Trace) { + std::map> cylinders; + for (auto const &stage : cycle) { + Trace.mark(stage.name); + Vehicle.hold_brake_pipe(stage.pipe); + Vehicle.run(stage.seconds); + cylinders[stage.name] = Vehicle->BrakePress; + } + return cylinders; +} + +} // namespace + +TEST_CASE("brake cycle: every real vehicle through charge, service steps, release, overcharge and emergency") { + for (auto const &path : test::real_vehicles()) { + CAPTURE(path); + test::reseed_random(); + vehicle real{definition::fiz(path)}; + test::trace trace{"brake cycle/" + path, test::channels::brakes(), 100}; + real.record(trace); + // a vehicle braked by the pipe: released once charged, braking harder by every step of the service + // application, and by the emergency one. the release isn't checked: some vehicles don't release within + // its 40 s, others keep a little air after the overcharge + if (auto cylinders{run_cycle(real, trace)}; cylinders["full service"] > 0.0) { + CHECK(cylinders["charge"] < 0.1); + CHECK(cylinders["light"] <= cylinders["medium"]); + CHECK(cylinders["medium"] <= cylinders["full service"] + 0.01); + CHECK(cylinders["full service"] > 1.0); + CHECK(cylinders["emergency"] > 0.5); + } + else { + // the draisine, the road vehicle and the catenary car, which have no brake worked by the pipe + CHECK(cylinders["emergency"] == 0.0); + } + trace.check(); + } +} + +TEST_CASE("brake cycle: every brake delay setting of the real vehicles") { + // G, P, R and their mixes change how fast the cylinders fill and empty; every vehicle offering more + // than one setting goes through the service part of the cycle on each + for (auto const &path : test::real_vehicles()) { + CAPTURE(path); + test::reseed_random(); + vehicle probe{definition::fiz(path)}; + for (auto const delay : {bdelay_G, bdelay_P, bdelay_R, bdelay_R + bdelay_M, bdelay_G + bdelay_M}) { + if ((probe->BrakeDelays & delay) != delay || delay == probe->BrakeDelayFlag) { + continue; + } + CAPTURE(delay); + test::reseed_random(); + vehicle real{definition::fiz(path)}; + REQUIRE(real->BrakeDelaySwitch(delay)); + test::trace trace{std::format("brake delays/{} {}", path, delay), test::channels::brakes(), 100}; + real.record(trace); + std::map> cylinders; + for (auto const &stage : {stage{"charge", 5.0, 90.0}, stage{"full service", 3.5, 30.0}, stage{"release", 5.0, 60.0}}) { + trace.mark(stage.name); + real.hold_brake_pipe(stage.pipe); + real.run(stage.seconds); + cylinders[stage.name] = real->BrakePress; + } + // every setting brakes in half a minute and releases in a minute + CHECK(cylinders["full service"] > 1.0); + CHECK(cylinders["release"] < 0.3); + trace.check(); + } + } +} + +TEST_CASE("brake cycle: the valves the game knows but none of its vehicles has, fitted to a wagon") { + // the variants of the Westinghouse and Knorr valves, the Hik ones, the old ESt3 and the CV1 ones, with a name + // the game doesn't know at the end. any other name with ESt in it is an Oerlikon valve (NESt3) whose name sets + // its relays: -ED the electrodynamic brake's one, AL2 the load-weighing one, without ESt3 the rapid one + for (auto const *name : {"W_Lu_L", "W_Lu_XR", "W_Lu_VI", "Kg", "Kp", "Kss", "Kkg", "Kkp", "Kks", "Hikp1", "Hikss", "Hikg1", "SW", "NESt3", "M483", "CV1", "CV1_L_TR", + "CV1_R", "ESt4-ED", "ESt4AL2", "no such valve"}) { + CAPTURE(name); + test::reseed_random(); + vehicle real{definition::fiz("3wl_v2/3wl").set("Brake", "BrakeValve", name)}; + test::trace trace{std::string{"brake cycle/fitted/"} + name, test::channels::brakes(), 100}; + real.record(trace); + run_cycle(real, trace); + trace.check(); + } +} + +TEST_CASE("brake cycle: the rapid setting at speed, the anti-slip brake and the releaser button") { + // R gives the cylinders more air at speed (RM); the anti-slip request the game makes while the wheels slip and + // slow down (3) and the releaser button (ub_Release) let the LSt's cylinders out, the KE's not + struct valve { + char const *name; + char const *path; + int delay; + double service; // in the cylinders, full service over 100 km/h + bool releases; // by the button + }; + for (auto const &[name, path, delay, service, releases] : {valve{"KE", "z1_v2/152a", bdelay_R, 3.78, false}, valve{"KE in P", "z1_v2/152a", bdelay_P, 2.70, false}, + valve{"LSt", "4e_v1/4e", bdelay_R, 5.79, true}, valve{"LSt in P", "4e_v1/4e", bdelay_P, 3.68, true}}) { + CAPTURE(name); + test::reseed_random(); + vehicle real{definition::fiz(path), 160.0}; + real->DecManualBrakeLevel(ManualBrakePosNo); + real->BrakeDelaySwitch(delay); // false when already set + REQUIRE(real->BrakeDelayFlag == delay); + using test::channels::operator+; + test::trace trace{std::string{"brake cycle/at speed/"} + name, test::channels::motion() + test::channels::brakes(), 100}; + real.record(trace); + trace.mark("charge"); + real.hold_brake_pipe(5.0); + real.run(60.0); + trace.mark("full service"); + real.hold_brake_pipe(3.5); + real.run(10.0); + CHECK(real->Vel > 100.0); + CHECK(real->BrakePress == doctest::Approx(service).epsilon(0.01)); + trace.mark("anti-slip"); + real->Hamulec->ASB(3); + real.run(2.0); + real->Hamulec->ASB(0); + real.run(3.0); + trace.mark("releaser button"); + real->Hamulec->SetUniversalFlag(TUniversalBrake::ub_Release); + real.run(3.0); + real->Hamulec->SetUniversalFlag(0); + CHECK((real->BrakePress < 1.0) == releases); + // released for good, the pipe still at full service + real.run(40.0); + CHECK((real->BrakePress < 0.1) == releases); + trace.check(); + } +} + +TEST_CASE("brake cycle: every material of the brake shoes and disks, braking a wagon from speed") { + // the friction of the shoes (BM) against the speed and the pressing force (friction.cpp): the game's wagons + // have P10-Bg, P10-Bgu, Cosid and the disks; FR513, FR510, P10yBg and P10yBgu none of them, nor an unknown one + // (P10, as with no BM). Mg adds the magnetic rail brake under 3 bar in the pipe + for (auto const *material : {"P10-Bg", "P10-Bgu", "FR513", "FR510", "Cosid", "P10yBg", "P10yBgu", "Disk1", "Disk1+Mg", "Disk2", "no such material"}) { + CAPTURE(material); + test::reseed_random(); + vehicle real{definition::fiz("3wl_v2/3wl").set("Brake", "BM", material), 100.0}; + real->DecManualBrakeLevel(ManualBrakePosNo); + using test::channels::operator+; + test::trace trace{std::string{"brake cycle/materials/"} + material, test::channels::motion() + test::channels::brakes(), 100}; + real.record(trace); + trace.mark("charge"); + real.hold_brake_pipe(5.0); + real.run(30.0); + auto const speed{real->Vel}; + trace.mark("full service"); + real.hold_brake_pipe(3.5); + real.run(20.0); + CHECK(real->Vel < speed - 10.0); + CHECK(real->Fb > 0.0); + trace.mark("emergency"); + real.hold_brake_pipe(0.0); + real.run(60.0); + trace.check(); + } +} diff --git a/tests/unit/test_brake_handles.cpp b/tests/unit/test_brake_handles.cpp new file mode 100644 index 000000000..51c25243b --- /dev/null +++ b/tests/unit/test_brake_handles.cpp @@ -0,0 +1,154 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" +#include "golden.h" +#include "real_vehicles.h" +#include "drive.h" + +#include "McZapkie/hamulce.h" + +using test::definition; +using test::vehicle; + +// characterisation of the driver's brake handles and the local brake handles of the real locomotives and +// units: every position of the handle, with the main reservoir kept full, followed in golden traces +// (data/golden/brake handles/). the vehicle runs alone, so the handle drives only its own brake pipe + +namespace { + +std::vector handle_channels() { + using namespace test::channels; + return brakes() + handles() + air(); +} + +// the vehicles with a driver's brake handle +bool has_handle(vehicle &Vehicle) { + return Vehicle->BrakeCtrlPosNo > 1; +} + +void hold(vehicle &Vehicle, test::trace &Trace, std::string const &Event, double Position, double Seconds) { + Trace.mark(Event); + Vehicle->BrakeLevelSet(Position); + Vehicle.run(Seconds); +} + +// every position of the driver's handle, recorded in the trace +void every_position(vehicle &Vehicle, test::trace &Trace) { + Vehicle.hold_main_reservoir(std::max(Vehicle->MaxCompressor, 8.0)); + auto &handle{*Vehicle->Handle}; + Vehicle.record(Trace); + + hold(Vehicle, Trace, "running", handle.GetPos(bh_RP), 90.0); + // every position from the lowest to the highest, emergency included, then back to running + auto const lowest{static_cast(std::ceil(handle.GetPos(bh_MIN)))}; + auto const highest{static_cast(std::floor(handle.GetPos(bh_MAX)))}; + for (auto position = lowest; position <= highest; ++position) { + hold(Vehicle, Trace, std::format("position {}", position), position, 15.0); + hold(Vehicle, Trace, "running", handle.GetPos(bh_RP), 30.0); + } + // filling stroke, neutral and the positions of the ep brake, where the handle has them + hold(Vehicle, Trace, "filling stroke", handle.GetPos(bh_FS), 10.0); + hold(Vehicle, Trace, "neutral", handle.GetPos(bh_NP), 20.0); + hold(Vehicle, Trace, "first step", handle.GetPos(bh_MB), 20.0); + hold(Vehicle, Trace, "full service", handle.GetPos(bh_FB), 20.0); + hold(Vehicle, Trace, "ep release", handle.GetPos(bh_EPR), 10.0); + hold(Vehicle, Trace, "ep hold", handle.GetPos(bh_EPN), 10.0); + hold(Vehicle, Trace, "ep brake", handle.GetPos(bh_EPB), 10.0); + hold(Vehicle, Trace, "running", handle.GetPos(bh_RP), 60.0); +} + +} // namespace + +TEST_CASE("brake handles: every position of the driver's handle of every real vehicle") { + for (auto const &path : test::real_vehicles()) { + CAPTURE(path); + test::reseed_random(); + vehicle real{definition::fiz(path)}; + if (false == has_handle(real)) { + continue; + } + test::trace trace{"brake handles/" + path, handle_channels(), 100}; + every_position(real, trace); + trace.check(); + } +} + +TEST_CASE("brake handles: the handles the game knows but none of its vehicles has, fitted to an EU07") { + // test is a handle for trying things out, D2 and Westinghouse fall back to the plain handle of the base class + for (auto const *name : {"test", "D2", "Westinghouse", "MHZ_EN57", "St113", "no such handle"}) { + CAPTURE(name); + test::reseed_random(); + vehicle real{definition::fiz("4e_v1/4e").set("Cntrl", "BrakeHandle", name)}; + test::trace trace{std::string{"brake handles/fitted/"} + name, handle_channels(), 100}; + every_position(real, trace); + trace.check(); + } +} + +TEST_CASE("brake handles: the local brake handle of every real vehicle") { + for (auto const &path : test::real_vehicles()) { + CAPTURE(path); + test::reseed_random(); + vehicle real{definition::fiz(path)}; + if (false == has_handle(real)) { + continue; + } + real.hold_main_reservoir(std::max(real->MaxCompressor, 8.0)); + test::trace trace{"local brake/" + path, handle_channels(), 100}; + real.record(trace); + real->BrakeLevelSet(real->Handle->GetPos(bh_RP)); + trace.mark("charge"); + real.run(60.0); + for (auto const position : {0.25, 0.5, 1.0, 0.5, 0.0}) { + trace.mark(std::format("local {:f}", position)); + real->LocalBrakePosA = position; + real.run(15.0); + } + trace.check(); + } +} + +TEST_CASE("brake handles: the overload and the high pressure buttons of the MHZ handles") { + // the overload button charges the pipe over its running pressure (TP), which then decays; the high pressure + // button gives the filling stroke its overpressure, where the handle has one only with the button pressed. + // from the cab, with the battery on and the driver attending to the vigilance: the electro-pneumatic handles + // work only so + // the MHZ_EN57, whose unit doesn't charge its pipe alone, fitted to an EU07 + for (auto const &[name, definition] : std::vector>{ + {"eu47", definition::fiz("e186_v2/eu47")}, + {"e6act", definition::fiz("e6act_v2/e6acta")}, + {"eu07 with MHZ_EN57", definition::fiz("4e_v1/4e").set("Cntrl", "BrakeHandle", "MHZ_EN57")}}) { + CAPTURE(name); + test::reseed_random(); + vehicle real{definition, 0.0, "test", 1}; + real->BatterySwitch(true); + real->CabActivisation(); + real.hold_main_reservoir(std::max(real->MaxCompressor, 8.0)); + test::trace trace{"brake handles/buttons/" + name, handle_channels(), 100}; + real.record(trace); + auto &handle{*real->Handle}; + auto const at{[&trace, &real](std::string const &Event, double Position, double Seconds) { + trace.mark(Event); + real->BrakeLevelSet(Position); + test::drive(test::driving(real), {}, Seconds); + }}; + at("running", handle.GetPos(bh_RP), 60.0); + auto const running{real->PipePress}; + handle.SetUniversalFlag(TUniversalBrake::ub_Overload); + at("overload button", handle.GetPos(bh_RP), 30.0); + handle.SetUniversalFlag(0); + auto const overloaded{real->PipePress}; + at("overload decaying", handle.GetPos(bh_RP), 240.0); + auto const decayed{real->PipePress}; + handle.SetUniversalFlag(TUniversalBrake::ub_HighPressure); + at("high pressure button, filling stroke", handle.GetPos(bh_FS), 10.0); + auto const filled{real->PipePress}; + handle.SetUniversalFlag(0); + at("running", handle.GetPos(bh_RP), 120.0); + CHECK(running == doctest::Approx(5.0).epsilon(0.01)); + CHECK(overloaded > running + 0.3); + CHECK(decayed < overloaded); + CHECK(filled >= decayed - 0.05); + trace.check(); + } +} diff --git a/tests/unit/test_cab.cpp b/tests/unit/test_cab.cpp new file mode 100644 index 000000000..4c5ba9f53 --- /dev/null +++ b/tests/unit/test_cab.cpp @@ -0,0 +1,229 @@ +#include "stdafx.h" +#include "doctest.h" +#include "consist.h" +#include "scenario.h" +#include "drive.h" + +#include "McZapkie/hamulce.h" +#include "utilities/utilities.h" + +// characterisation of what the cab (Train.cpp) and the AI (Driver.cpp, driverhints.cpp) call on a vehicle +// besides the controls of test_controls.cpp: the gauges and lamps reading other vehicles of the train, the +// controller of the electrodynamic brake apart from the local brake, the spring brake, the ep brake's fuse, +// cutting off damaged motors, the cab activated by itself, a road vehicle's place across the road, and the +// lamps and the buzzer of the security system + +using test::consist; +using test::definition; +using test::scenario; +using test::vehicle; +using namespace test::channels; + +TEST_CASE("cab: the engine rotation of up to three vehicles in multiple, on the tachometers of the SN61") { + consist train; + auto &first{train.add(definition::fiz("sn61_v2/sn61"), 1).mover()}; + auto &second{train.add(definition::fiz("sn61_v2/sn61")).mover()}; + auto &third{train.add(definition::fiz("sn61_v2/sn61")).mover()}; + first.enrot = -10.0; + second.enrot = 20.0; + third.enrot = 30.0; + CHECK(first.ShowEngineRotation(1) == doctest::Approx(10.0)); + CHECK(first.ShowEngineRotation(2) == doctest::Approx(20.0)); + CHECK(first.ShowEngineRotation(3) == doctest::Approx(30.0)); + CHECK(first.ShowEngineRotation(4) == doctest::Approx(0.0)); + // the others look the same way along their couplers, front first + CHECK(second.ShowEngineRotation(2) == doctest::Approx(10.0)); + CHECK(third.ShowEngineRotation(2) == doctest::Approx(20.0)); + CHECK(third.ShowEngineRotation(3) == doctest::Approx(10.0)); + // no control cable, no reading + consist apart; + auto const &alone{apart.add(definition::fiz("sn61_v2/sn61"), 1).mover()}; + apart.add(definition::fiz("sn61_v2/sn61"), 0, coupling::coupler | coupling::brakehose); + CHECK(alone.ShowEngineRotation(2) == doctest::Approx(0.0)); +} + +TEST_CASE("cab: the lamps of the overload relay and the starting resistors, read through the control cable") { + consist unit; + auto &trailer{unit.add(definition::fiz("ed72_v1/5bs-ra"), 1).mover()}; + auto &motor{unit.add(definition::fiz("ed72_v1/6bs-sa")).mover()}; + REQUIRE(trailer.Power < 0.01); + REQUIRE(motor.Power > 0.01); + CHECK_FALSE(trailer.FuseFlagCheck()); + CHECK_FALSE(trailer.ResistorsFlagCheck()); + motor.FuseFlag = true; + motor.ResistorsFlag = true; + CHECK(trailer.FuseFlagCheck()); + CHECK(trailer.ResistorsFlagCheck()); + CHECK(motor.FuseFlagCheck()); + CHECK(motor.ResistorsFlagCheck()); +} + +TEST_CASE("cab: the engine's idle speed, and the ET42's second controller shown in its dynamic brake") { + vehicle sn61{definition::fiz("sn61_v2/sn61")}; + CHECK(sn61->EngineIdleRPM() == doctest::Approx(sn61->dizel_nmin * 60.0)); + vehicle su45{definition::fiz("su45_v2/301d")}; + CHECK(su45->EngineIdleRPM() == doctest::Approx(su45->DElist[su45->MainCtrlNoPowerPos()].RPM)); + vehicle eu07{definition::fiz("4e_v1/4e")}; + CHECK(eu07->EngineIdleRPM() == std::numeric_limits::max()); + + vehicle et42{definition::fiz("et42_v2/112e-a")}; + CHECK(et42->GetVirtualScndPos() == 0); + et42->DynamicBrakeFlag = true; + CHECK(et42->GetVirtualScndPos() == -1); + et42->ScndCtrlPos = 1; + CHECK(et42->GetVirtualScndPos() == 1); + // with a speed control in place of the second controller, either end of it, for the AI + vehicle railbus{definition::fiz("21xm_v1/214ma")}; + REQUIRE(railbus->SpeedCtrl); + railbus->ScndCtrlPos = 1; + CHECK(railbus->IsScndCtrlNoPowerPos()); + CHECK(railbus->IsScndCtrlMaxPowerPos()); + CHECK(eu07->IsScndCtrlNoPowerPos()); + CHECK_FALSE(eu07->IsScndCtrlMaxPowerPos()); +} + +TEST_CASE("cab: the controller of the electrodynamic brake apart from the local brake") { + // none of the game's vehicles has one yet (SplitEDPneumaticBrake): the electrodynamic brake follows its own + // controller of DBPN positions, the local brake is pneumatic alone + scenario scenario{"cab/split electrodynamic brake", motion() + traction() + brakes()}; + scenario.add("e186_v2/eu47", 1, consist::full_coupling, 100.0).add("bdhpumn_v3/bdhpumn", 0, consist::full_coupling, 100.0); + auto &eu47{scenario.cab()}; + CHECK_FALSE(eu47.IncDynamicBrakeLevel(1.f)); + CHECK(eu47.DynamicBrakeRatio() == doctest::Approx(0.0)); + + scenario.train()[0].mover().SplitEDPneumaticBrake = true; + REQUIRE(scenario.start_up()); + REQUIRE(eu47.DynamicBrakeCtrlPosNo == 10); + CHECK(eu47.DynamicBrakeAvailable()); + scenario.act("dynamic brake up"); + for (int i = 0; i < 5; ++i) { + CHECK(eu47.IncDynamicBrakeLevel(1.f)); + } + CHECK(eu47.DynamicBrakeRatio() == doctest::Approx(0.5)); + scenario.wait(10.0); + CHECK(eu47.DynamicBrakeFlag); + CHECK(eu47.Im < -50.0); + CHECK(eu47.BrakePress < 0.1); + scenario.act("dynamic brake full"); + CHECK(eu47.DynamicBrakeLevelSet(2.0)); + CHECK(eu47.DynamicBrakeRatio() == doctest::Approx(1.0)); + CHECK_FALSE(eu47.IncDynamicBrakeLevel(1.f)); + scenario.wait(10.0); + scenario.act("dynamic brake off"); + for (int i = 0; i < 20 && eu47.DecDynamicBrakeLevel(1.f); ++i) { + // down a notch at a time + } + CHECK(eu47.DynamicBrakeRatio() == doctest::Approx(0.0)); + // the local brake no longer brings the motors in + scenario.act("local brake"); + eu47.IncLocalBrakeLevel(LocalBrakePosNo); + scenario.wait(5.0); + CHECK(eu47.BrakePress > 1.0); + scenario.check(); +} + +TEST_CASE("cab: the spring brake shut off and released by hand, the ep brake's fuse, the train brake handle stepped") { + scenario scenario{"cab/214m spring brake", motion() + brakes()}; + scenario.add("21xm_v1/214ma", 1); + REQUIRE(scenario.start_up()); + auto &railbus{scenario.cab()}; + scenario.act("spring brake on"); + railbus.SpringBrakeActivate(true); + scenario.wait(10.0); + CHECK(railbus.SpringBrake.IsActive); + // released by hand only with its cylinder empty of the air holding it off + auto const pressure{railbus.SpringBrake.Cylinder->P()}; + CHECK(railbus.SpringBrakeRelease() == (pressure < railbus.SpringBrake.MinForcePressure)); + scenario.act("shut off"); + CHECK(railbus.SpringBrakeShutOff(true)); + CHECK_FALSE(railbus.SpringBrakeShutOff(true)); + scenario.wait(5.0); + CHECK(railbus.SpringBrakeShutOff(false)); + scenario.act("spring brake off"); + railbus.SpringBrakeActivate(false); + scenario.wait(10.0); + // the ep brake's fuse + CHECK(railbus.EpFuseSwitch(false)); + CHECK_FALSE(railbus.EpFuseSwitch(false)); + CHECK(railbus.EpFuseSwitch(true)); + // the train brake handle stepped as the AI does, false at its ends + scenario.act("handle stepped"); + railbus.BrakeLevelSet(railbus.Handle->GetPos(bh_RP)); + CHECK(railbus.BrakeLevelAdd(1.0)); + CHECK(railbus.BrakeCtrlPos == static_cast(railbus.Handle->GetPos(bh_RP)) + 1); + CHECK(railbus.BrakeLevelAdd(-1.0)); + CHECK_FALSE(railbus.BrakeLevelAdd(-10.0)); + CHECK_FALSE(railbus.BrakeLevelAdd(100.0)); + scenario.wait(5.0); + scenario.check(); +} + +TEST_CASE("cab: damaged motors cut off in a car without a driver") { + // cutting off takes the damage off the engine and half of the powered axles with it; only in a car nobody + // drives, with its engine damaged + vehicle driven{definition::fiz("ew58_v1/ew58-sa"), 0.0, "test", 1}; + driven->BatterySwitch(true); + REQUIRE(driven->CabActivisation()); + driven->DamageFlag |= dtrain_engine; + CHECK_FALSE(driven->CutOffEngine()); + vehicle car{definition::fiz("ew58_v1/ew58-sa")}; + auto const axles{car->NPoweredAxles}; + CHECK_FALSE(car->CutOffEngine()); + car->DamageFlag |= dtrain_engine; + CHECK(car->CutOffEngine()); + CHECK(car->NPoweredAxles == axles / 2); + CHECK_FALSE(TestFlag(car->DamageFlag, dtrain_engine)); + vehicle wagon{definition::fiz("3wl_v2/3wl")}; + wagon->DamageFlag |= dtrain_engine; + CHECK_FALSE(wagon->CutOffEngine()); +} + +TEST_CASE("cab: activated by itself, where the vehicle allows it") { + for (auto const allowed : {false, true}) { + CAPTURE(allowed); + vehicle eu07{definition::fiz("4e_v1/4e").set("Cntrl", "AutomaticCabActivation", allowed ? "Yes" : "No"), 0.0, "test", 1}; + eu07->BatterySwitch(true); + CHECK(eu07->CabActivisationAuto() == allowed); + CHECK((eu07->CabActive != 0) == allowed); + CHECK(eu07->CabDeactivisationAuto() == allowed); + CHECK(eu07->CabActive == 0); + } +} + +TEST_CASE("cab: a road vehicle moved across its road") { + vehicle bus{definition::fiz("road/man/nl293")}; + bus.step(test::track{0.15, 0.0, 0.0, 2, 1, 7.0}); + REQUIRE(TestFlag(bus->CategoryFlag, 2)); + CHECK(bus->ChangeOffsetH(0.5)); + CHECK(bus->OffsetTrackH == doctest::Approx(0.5)); + // off the edge of the road + CHECK_FALSE(bus->ChangeOffsetH(100.0)); + vehicle eu07{definition::fiz("4e_v1/4e")}; + CHECK_FALSE(eu07->ChangeOffsetH(0.5)); +} + +TEST_CASE("cab: the lamps and the buzzer of the security system left unattended") { + scenario scenario{"cab/security system", motion() + brakes()}; + scenario.add("4e_v1/4e", 1); + REQUIRE(scenario.start_up()); + auto &eu07{scenario.cab()}; + scenario.get_going(); + scenario.full_power(10.0); + auto const &security{eu07.SecuritySystem}; + CHECK_FALSE(security.is_beeping()); + scenario.act("unattended"); + // the train runs with nobody acknowledging the vigilance + bool blinked{false}; + bool beeped{false}; + for (int i = 0; i < 120; ++i) { + scenario.train().run(1.0, scenario.line()); + blinked |= security.is_vigilance_blinking(); + beeped |= security.is_beeping(); + } + CHECK(blinked); + CHECK(beeped); + CHECK_FALSE(security.is_cabsignal_beeping()); + CHECK(eu07.PipePress < 1.0); + CHECK(security.radiostop_available()); + scenario.check(); +} diff --git a/tests/unit/test_commands.cpp b/tests/unit/test_commands.cpp new file mode 100644 index 000000000..6f5730cf7 --- /dev/null +++ b/tests/unit/test_commands.cpp @@ -0,0 +1,547 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" +#include "golden.h" +#include "consist.h" +#include "real_vehicles.h" + +#include +#include + +#include "McZapkie/hamulce.h" + +// characterisation of the commands vehicles pass each other along the couplers (RunCommand): what each of them +// does to every real vehicle, recorded in golden traces (data/golden/commands/), how they travel through a train of +// vehicles coupled either way round, and the loading and unloading driven by them + +using test::definition; +using test::vehicle; + +namespace { + +double flag(bool Value) { + return Value ? 1.0 : 0.0; +} + +// what the commands change +std::vector state() { + std::vector result{ + {"MainCtrlPos", [](auto const &M) { return M.MainCtrlPos; }}, + {"ScndCtrlPos", [](auto const &M) { return M.ScndCtrlPos; }}, + {"LastRelayTime", [](auto const &M) { return M.LastRelayTime; }}, + {"eimic_real", [](auto const &M) { return M.eimic_real; }}, + {"TUHEX_StageActual", [](auto const &M) { return M.TUHEX_StageActual; }}, + {"TUHEX_Active", [](auto const &M) { return flag(M.TUHEX_Active); }}, + {"DynamicBrakeEMUStatus", [](auto const &M) { return flag(M.DynamicBrakeEMUStatus); }}, + {"BrakeReleaser", [](auto const &M) { return flag(M.Hamulec->Releaser()); }}, + {"WaterPumpBreaker", [](auto const &M) { return flag(M.WaterPump.breaker); }}, + {"WaterPumpEnabled", [](auto const &M) { return flag(M.WaterPump.is_enabled); }}, + {"WaterPumpDisabled", [](auto const &M) { return flag(M.WaterPump.is_disabled); }}, + {"WaterHeaterBreaker", [](auto const &M) { return flag(M.WaterHeater.breaker); }}, + {"WaterHeaterEnabled", [](auto const &M) { return flag(M.WaterHeater.is_enabled); }}, + {"WaterCircuitsLink", [](auto const &M) { return flag(M.WaterCircuitsLink); }}, + {"FuelPumpEnabled", [](auto const &M) { return flag(M.FuelPump.is_enabled); }}, + {"FuelPumpDisabled", [](auto const &M) { return flag(M.FuelPump.is_disabled); }}, + {"OilPumpEnabled", [](auto const &M) { return flag(M.OilPump.is_enabled); }}, + {"OilPumpDisabled", [](auto const &M) { return flag(M.OilPump.is_disabled); }}, + {"CompartmentLightsEnabled", [](auto const &M) { return flag(M.CompartmentLights.is_enabled); }}, + {"CompartmentLightsDisabled", [](auto const &M) { return flag(M.CompartmentLights.is_disabled); }}, + {"MainSwitch", [](auto const &M) { return flag(M.Mains); }}, + {"Heating", [](auto const &M) { return flag(M.Heating); }}, + {"DirActive", [](auto const &M) { return M.DirActive; }}, + {"DirAbsolute", [](auto const &M) { return M.DirAbsolute; }}, + {"CabActive", [](auto const &M) { return M.CabActive; }}, + {"CabMaster", [](auto const &M) { return flag(M.CabMaster); }}, + {"AutoRelayFlag", [](auto const &M) { return flag(M.AutoRelayFlag); }}, + {"ConverterAllow", [](auto const &M) { return flag(M.ConverterAllow); }}, + {"Battery", [](auto const &M) { return flag(M.Battery); }}, + {"CompressorAllow", [](auto const &M) { return flag(M.CompressorAllow); }}, + {"CompressorListPos", [](auto const &M) { return M.CompressorListPos; }}, + {"DoorLock", [](auto const &M) { return flag(M.Doors.lock_enabled); }}, + {"DoorStep", [](auto const &M) { return flag(M.Doors.step_enabled); }}, + {"DoorRemoteOnly", [](auto const &M) { return flag(M.Doors.remote_only); }}, + {"DepartureSignal", [](auto const &M) { return flag(M.DepartureSignal); }}, + {"PantsValve", [](auto const &M) { return flag(M.PantsValve.is_enabled) + 2 * flag(M.PantsValve.is_disabled); }}, + {"MaxCurrentHigh", [](auto const &M) { return flag(M.MotorOverloadRelayHighThreshold); }}, + {"Imin", [](auto const &M) { return M.Imin; }}, + {"DamageFlag", [](auto const &M) { return M.DamageFlag; }}, + {"RadioStop", [](auto const &M) { return flag(M.RadioStopFlag); }}, + {"BrakeDelayFlag", [](auto const &M) { return M.BrakeDelayFlag; }}, + {"SandDose", [](auto const &M) { return flag(M.SandDose); }}, + {"CabSignal", [](auto const &M) { return flag(M.SecuritySystem.is_cabsignal_blinking()); }}, + {"LoadAmount", [](auto const &M) { return M.LoadAmount; }}, + {"LoadStatus", [](auto const &M) { return M.LoadStatus; }}, + {"SpeedCtrlValue", [](auto const &M) { return M.SpeedCtrlValue; }}, + {"SpeedCtrlParking", [](auto const &M) { return flag(M.SpeedCtrlUnit.Parking); }}, + {"SpringBrakeActivate", [](auto const &M) { return flag(M.SpringBrake.Activate); }}, + }; + for (int end : {end::front, end::rear}) { + auto const name{std::string{end == end::front ? "Front" : "Rear"}}; + result.emplace_back("MotorBlowers" + name + "Enabled", [end](auto const &M) { return flag(M.MotorBlowers[end].is_enabled); }); + result.emplace_back("MotorBlowers" + name + "Disabled", [end](auto const &M) { return flag(M.MotorBlowers[end].is_disabled); }); + result.emplace_back("Pant" + name + "Valve", [end](auto const &M) { return flag(M.Pantographs[end].valve.is_enabled) + 2 * flag(M.Pantographs[end].valve.is_disabled); }); + } + for (int side : {side::right, side::left}) { + auto const name{std::string{side == side::right ? "Right" : "Left"}}; + result.emplace_back(name + "DoorPermit", [side](auto const &M) { return flag(M.Doors.instances[side].open_permit); }); + result.emplace_back(name + "DoorRemoteOpen", [side](auto const &M) { return flag(M.Doors.instances[side].remote_open); }); + result.emplace_back(name + "DoorRemoteClose", [side](auto const &M) { return flag(M.Doors.instances[side].remote_close); }); + } + return result; +} + +struct command { + std::string name; + double value1; + double value2{1.0}; // the direction it comes from, 1 the front +}; + +// every command RunCommand knows, with values taking each of its ways, and one it doesn't +std::vector const commands{ + {"MainCtrl", 1}, + {"MainCtrl", 2}, + {"MainCtrl", 0}, + {"MainCtrl", 99}, + {"ScndCtrl", 1}, + {"ScndCtrl", 0}, + {"ScndCtrl", 99}, + {"EIMIC", 0.5}, + {"EIMIC", -0.5}, + {"Brake", 1}, + {"Brake", 2}, + {"Brake", 0}, + {"BrakeReleaser", 1}, + {"BrakeReleaser", 0}, + {"WaterPumpBreakerSwitch", 1}, + {"WaterPumpSwitch", 1}, + {"WaterPumpSwitchOff", 1}, + {"WaterPumpSwitchOff", 0}, + {"WaterPumpSwitch", 0}, + {"WaterPumpBreakerSwitch", 0}, + {"WaterHeaterBreakerSwitch", 1}, + {"WaterHeaterSwitch", 1}, + {"WaterHeaterSwitch", 0}, + {"WaterHeaterBreakerSwitch", 0}, + {"WaterCircuitsLinkSwitch", 1}, + {"WaterCircuitsLinkSwitch", 0}, + {"FuelPumpSwitch", 1}, + {"FuelPumpSwitchOff", 1}, + {"FuelPumpSwitchOff", 0}, + {"FuelPumpSwitch", 0}, + {"OilPumpSwitch", 1}, + {"OilPumpSwitchOff", 1}, + {"OilPumpSwitchOff", 0}, + {"OilPumpSwitch", 0}, + {"MotorBlowersFrontSwitch", 1}, + {"MotorBlowersFrontSwitchOff", 1}, + {"MotorBlowersFrontSwitchOff", 0}, + {"MotorBlowersFrontSwitch", 0}, + {"MotorBlowersRearSwitch", 1}, + {"MotorBlowersRearSwitchOff", 1}, + {"MotorBlowersRearSwitchOff", 0}, + {"MotorBlowersRearSwitch", 0}, + {"CompartmentLightsSwitch", 1}, + {"CompartmentLightsSwitchOff", 1}, + {"CompartmentLightsSwitchOff", 0}, + {"CompartmentLightsSwitch", 0}, + {"BatterySwitch", 1}, + {"ConverterSwitch", 1}, + {"CompressorSwitch", 1}, + {"CompressorPreset", 1}, + {"CompressorPreset", 99}, + {"CompressorPreset", -1}, + {"MainSwitch", 1}, + {"HeatingSwitch", 1}, + {"HeatingSwitch", 0}, + {"Direction", 1}, + {"Direction", -1}, + {"Direction", 0}, + {"CabActivisation", 1, 1}, + {"CabActivisation", 1, -1}, + {"CabActivisation", 0, 1}, + {"AutoRelaySwitch", 1}, + {"AutoRelaySwitch", 0}, + {"RelayReset", 1}, + {"DoorPermit", 1, 1}, + {"DoorPermit", 2, 1}, + {"DoorPermit", -3, 1}, + {"DoorPermit", 1, -1}, + {"DoorPermit", 3, -1}, + {"DoorOpen", 1, 1}, + {"DoorOpen", 2, -1}, + {"DoorClose", 3, 1}, + {"DoorOpen", 3, 1}, + {"DoorClose", 1, -1}, + {"DoorLock", 0}, + {"DoorLock", 1}, + {"DoorStep", 0}, + {"DoorStep", 1}, + {"DoorMode", 1}, + {"DoorMode", 0}, + {"DepartureSignal", 1}, + {"DepartureSignal", 0}, + {"PantValve", 1}, + {"PantValve", 0x80 | 1}, + {"PantValve", 0x40 | 1, -1}, + {"PantValve", 0x80 | 0x40 | 2, -1}, + {"PantValve", 3}, + {"PantValve", 4}, + {"PantValve", 0x80 | 5}, + {"PantValve", 0x80 | 6}, + {"PantsValve", 1}, + {"PantsValve", 2}, + {"PantsValve", 3}, + {"PantsValve", 4}, + {"PantsValve", 5}, + {"PantsValve", 6}, + {"PantAllDown", 1}, + {"PantAllDown", 0}, + {"MaxCurrentSwitch", 1}, + {"MaxCurrentSwitch", 0}, + {"MinCurrentSwitch", 1}, + {"MinCurrentSwitch", 0}, + {"SetDamage", 4, 1}, + {"SetDamage", 4, -1}, + {"SetDamage", 4, 0}, + {"Emergency_brake", 1}, + {"Emergency_brake", 0}, + {"BrakeDelay", 1}, + {"BrakeDelay", 2}, + {"BrakeDelay", 4}, + {"BrakeDelay", 8}, + {"BrakeDelay", 5}, + {"Sandbox", 1}, + {"Sandbox", 0}, + {"CabSignal", 0}, + {"Load=coal", 5, 0}, + {"Load=Coal", 5, 20}, + {"UnLoad=coal", 5, 0}, + {"UnLoad=Coal", 5, 0}, + {"SpeedCntrl", 40.4}, + {"SpeedCtrlUnit.Parking", 1}, + {"SpeedCtrlUnit.Parking", 0}, + {"SpringBrakeActivate", 1}, + {"SpringBrakeActivate", 0}, + {"MainSwitch", 0}, + {"CompressorSwitch", 0}, + {"ConverterSwitch", 0}, + {"BatterySwitch", 0}, + {"SpringBrakeActivate", 1}, + {"NoSuchCommand", 1}, +}; + +} // namespace + +TEST_CASE("commands: every command run on every real vehicle") { + for (auto const &path : test::real_vehicles()) { + CAPTURE(path); + test::reseed_random(); + vehicle real{definition::fiz(path), 0.0, "test", 1}; + // loading takes place where the command was given, which comes with the command (PutCommand). a + // vehicle gets none before its first one, TCommand::Location is left uninitialised + real->CommandIn.Location = real->Loc; + auto done{false}; + auto channels{state()}; + channels.emplace_back("Done", [&done](auto const &) { return flag(done); }); + test::trace trace{"commands/" + path, channels, 1, false}; + trace.sample(0.0, real.mover()); + for (std::size_t i = 0; i < commands.size(); ++i) { + auto const &command{commands[i]}; + trace.mark(std::format("{} {} {}", command.name, command.value1, command.value2)); + done = real->RunCommand(command.name, command.value1, command.value2, coupling::control); + trace.sample(static_cast(i + 1), real.mover()); + } + trace.check(); + } +} + +namespace { + +// a switch of the cab, worked on the driven vehicle +struct action { + std::string name; + std::function work; +}; + +// every switch sending its state to the rest of the train, each way and to each range, in the order a crew +// could work them, with the controllers and the direction among them to see what they make of the state +std::vector const actions{ + {"CabActivisation", [](auto &M) { return M.CabActivisation(); }}, + {"BatterySwitch on", [](auto &M) { return M.BatterySwitch(true, range_t::consist); }}, + {"CompartmentLightsSwitch on", [](auto &M) { return M.CompartmentLightsSwitch(true, range_t::consist); }}, + {"CompartmentLightsSwitchOff on", [](auto &M) { return M.CompartmentLightsSwitchOff(true, range_t::consist); }}, + {"CompartmentLightsSwitchOff off", [](auto &M) { return M.CompartmentLightsSwitchOff(false, range_t::unit); }}, + {"WaterPumpBreakerSwitch on", [](auto &M) { return M.WaterPumpBreakerSwitch(true, range_t::consist); }}, + {"WaterPumpSwitch on", [](auto &M) { return M.WaterPumpSwitch(true, range_t::consist); }}, + {"WaterPumpSwitchOff on", [](auto &M) { return M.WaterPumpSwitchOff(true, range_t::unit); }}, + {"WaterPumpSwitchOff off", [](auto &M) { return M.WaterPumpSwitchOff(false, range_t::consist); }}, + {"WaterHeaterBreakerSwitch on", [](auto &M) { return M.WaterHeaterBreakerSwitch(true, range_t::consist); }}, + {"WaterHeaterSwitch on", [](auto &M) { return M.WaterHeaterSwitch(true, range_t::consist); }}, + {"WaterCircuitsLinkSwitch on", [](auto &M) { return M.WaterCircuitsLinkSwitch(true, range_t::consist); }}, + {"FuelPumpSwitch on", [](auto &M) { return M.FuelPumpSwitch(true, range_t::consist); }}, + {"FuelPumpSwitchOff on", [](auto &M) { return M.FuelPumpSwitchOff(true, range_t::unit); }}, + {"FuelPumpSwitchOff off", [](auto &M) { return M.FuelPumpSwitchOff(false, range_t::consist); }}, + {"OilPumpSwitch on", [](auto &M) { return M.OilPumpSwitch(true, range_t::consist); }}, + {"OilPumpSwitchOff on", [](auto &M) { return M.OilPumpSwitchOff(true, range_t::unit); }}, + {"OilPumpSwitchOff off", [](auto &M) { return M.OilPumpSwitchOff(false, range_t::consist); }}, + {"MotorBlowersSwitch front on", [](auto &M) { return M.MotorBlowersSwitch(true, end::front, range_t::consist); }}, + {"MotorBlowersSwitch rear on", [](auto &M) { return M.MotorBlowersSwitch(true, end::rear, range_t::unit); }}, + {"MotorBlowersSwitchOff front on", [](auto &M) { return M.MotorBlowersSwitchOff(true, end::front, range_t::consist); }}, + {"MotorBlowersSwitchOff rear on", [](auto &M) { return M.MotorBlowersSwitchOff(true, end::rear, range_t::unit); }}, + {"MotorBlowersSwitchOff front off", [](auto &M) { return M.MotorBlowersSwitchOff(false, end::front, range_t::consist); }}, + {"MotorBlowersSwitchOff rear off", [](auto &M) { return M.MotorBlowersSwitchOff(false, end::rear, range_t::consist); }}, + {"OperatePantographsValve enable", [](auto &M) { return M.OperatePantographsValve(operation_t::enable, range_t::consist); }}, + {"OperatePantographValve front enable", [](auto &M) { return M.OperatePantographValve(end::front, operation_t::enable, range_t::consist); }}, + {"OperatePantographValve rear enable_on", [](auto &M) { return M.OperatePantographValve(end::rear, operation_t::enable_on, range_t::unit); }}, + {"OperatePantographValve rear enable_off", [](auto &M) { return M.OperatePantographValve(end::rear, operation_t::enable_off, range_t::unit); }}, + {"DirectionForward", [](auto &M) { return M.DirectionForward(); }}, + {"ConverterSwitch on", [](auto &M) { return M.ConverterSwitch(true, range_t::consist); }}, + {"CompressorSwitch on", [](auto &M) { return M.CompressorSwitch(true, range_t::consist); }}, + {"ChangeCompressorPreset 1", [](auto &M) { return M.ChangeCompressorPreset(1, range_t::consist); }}, + {"FuseOn", [](auto &M) { return M.FuseOn(range_t::consist); }}, + {"RelayReset all", [](auto &M) { return M.RelayReset(0xff, range_t::consist); }}, + {"UniversalResetButton 1", [](auto &M) { return M.UniversalResetButton(0, range_t::consist); }}, + {"UniversalResetButton 2", [](auto &M) { return M.UniversalResetButton(1, range_t::unit); }}, + {"MainSwitch on", [](auto &M) { return M.MainSwitch(true, range_t::consist); }}, + {"HeatingSwitch on", [](auto &M) { return M.HeatingSwitch(true, range_t::consist); }}, + {"MaxCurrentSwitch on", [](auto &M) { return M.MaxCurrentSwitch(true, range_t::consist); }}, + {"MinCurrentSwitch on", [](auto &M) { return M.MinCurrentSwitch(true); }}, + {"CurrentSwitch on", [](auto &M) { return M.CurrentSwitch(true); }}, + {"AutoRelaySwitch on", [](auto &M) { return M.AutoRelaySwitch(true); }}, + {"IncMainCtrl 1", [](auto &M) { return M.IncMainCtrl(1); }}, + {"IncMainCtrl 2", [](auto &M) { return M.IncMainCtrl(2); }}, + {"IncScndCtrl 1", [](auto &M) { return M.IncScndCtrl(1); }}, + {"IncScndCtrl 2", [](auto &M) { return M.IncScndCtrl(2); }}, + {"DecScndCtrl 1", [](auto &M) { return M.DecScndCtrl(1); }}, + {"DecScndCtrl 2", [](auto &M) { return M.DecScndCtrl(2); }}, + {"DecMainCtrl 1", [](auto &M) { return M.DecMainCtrl(1); }}, + {"DecMainCtrl 2", [](auto &M) { return M.DecMainCtrl(2); }}, + {"AutoRelaySwitch off", [](auto &M) { return M.AutoRelaySwitch(false); }}, + {"CurrentSwitch off", [](auto &M) { return M.CurrentSwitch(false); }}, + {"MinCurrentSwitch off", [](auto &M) { return M.MinCurrentSwitch(false); }}, + {"MaxCurrentSwitch off", [](auto &M) { return M.MaxCurrentSwitch(false, range_t::unit); }}, + {"DirectionBackward", [](auto &M) { return M.DirectionBackward(); }}, + {"DirectionBackward again", [](auto &M) { return M.DirectionBackward(); }}, + {"DirectionForward", [](auto &M) { return M.DirectionForward(); }}, + {"Sandbox on", [](auto &M) { return M.Sandbox(true, range_t::consist); }}, + {"SandboxManual on", [](auto &M) { return M.SandboxManual(true, range_t::consist); }}, + {"SandboxAuto on", [](auto &M) { return M.SandboxAuto(true, range_t::unit); }}, + {"SandboxAuto off", [](auto &M) { return M.SandboxAuto(false, range_t::consist); }}, + {"SandboxManual off", [](auto &M) { return M.SandboxManual(false, range_t::consist); }}, + {"Sandbox off", [](auto &M) { return M.Sandbox(false, range_t::consist); }}, + {"PermitDoors right", [](auto &M) { return M.PermitDoors(side::right, true, range_t::consist); }}, + {"PermitDoors left", [](auto &M) { return M.PermitDoors(side::left, true, range_t::unit); }}, + {"ChangeDoorPermitPreset +1", [](auto &M) { return M.ChangeDoorPermitPreset(1, range_t::consist); }}, + {"ChangeDoorPermitPreset -1", [](auto &M) { return M.ChangeDoorPermitPreset(-1, range_t::consist); }}, + {"PermitDoorStep off", [](auto &M) { return M.PermitDoorStep(false, range_t::consist); }}, + {"PermitDoorStep on", [](auto &M) { return M.PermitDoorStep(true, range_t::unit); }}, + {"LockDoors off", [](auto &M) { return M.LockDoors(false, range_t::consist); }}, + {"OperateDoors right open local", [](auto &M) { return M.OperateDoors(side::right, true, range_t::local); }}, + {"OperateDoors left open", [](auto &M) { return M.OperateDoors(side::left, true, range_t::consist); }}, + {"OperateDoors right close", [](auto &M) { return M.OperateDoors(side::right, false, range_t::unit); }}, + {"ChangeDoorControlMode remote", [](auto &M) { return M.ChangeDoorControlMode(true, range_t::consist); }}, + {"ChangeDoorControlMode local", [](auto &M) { return M.ChangeDoorControlMode(false, range_t::unit); }}, + {"LockDoors on", [](auto &M) { return M.LockDoors(true, range_t::unit); }}, + {"signal_departure on", [](auto &M) { return M.signal_departure(true, range_t::consist); }}, + {"signal_departure off", [](auto &M) { return M.signal_departure(false, range_t::unit); }}, + {"PermitDoors right off", [](auto &M) { return M.PermitDoors(side::right, false, range_t::consist); }}, + {"HeatingSwitch off", [](auto &M) { return M.HeatingSwitch(false, range_t::unit); }}, + {"MainSwitch off", [](auto &M) { return M.MainSwitch(false, range_t::consist); }}, + {"DropAllPantographs", [](auto &M) { return M.DropAllPantographs(true, range_t::consist); }}, + {"DropAllPantographs released", [](auto &M) { return M.DropAllPantographs(false, range_t::unit); }}, + {"OperatePantographsValve disable", [](auto &M) { return M.OperatePantographsValve(operation_t::disable, range_t::unit); }}, + {"OperatePantographValve front disable_on", [](auto &M) { return M.OperatePantographValve(end::front, operation_t::disable_on, range_t::consist); }}, + {"OperatePantographValve front disable_off", [](auto &M) { return M.OperatePantographValve(end::front, operation_t::disable_off, range_t::consist); }}, + {"OperatePantographValve front none", [](auto &M) { return M.OperatePantographValve(end::front, operation_t::none, range_t::local); }}, + {"CompressorSwitch off", [](auto &M) { return M.CompressorSwitch(false, range_t::unit); }}, + {"ConverterSwitch off", [](auto &M) { return M.ConverterSwitch(false, range_t::unit); }}, + {"WaterCircuitsLinkSwitch off", [](auto &M) { return M.WaterCircuitsLinkSwitch(false, range_t::unit); }}, + {"WaterHeaterSwitch off", [](auto &M) { return M.WaterHeaterSwitch(false, range_t::unit); }}, + {"WaterHeaterBreakerSwitch off", [](auto &M) { return M.WaterHeaterBreakerSwitch(false, range_t::unit); }}, + {"WaterPumpSwitch off", [](auto &M) { return M.WaterPumpSwitch(false, range_t::unit); }}, + {"WaterPumpBreakerSwitch off", [](auto &M) { return M.WaterPumpBreakerSwitch(false, range_t::unit); }}, + {"FuelPumpSwitch off", [](auto &M) { return M.FuelPumpSwitch(false, range_t::unit); }}, + {"OilPumpSwitch off", [](auto &M) { return M.OilPumpSwitch(false, range_t::unit); }}, + {"MotorBlowersSwitch front off", [](auto &M) { return M.MotorBlowersSwitch(false, end::front, range_t::unit); }}, + {"MotorBlowersSwitch rear off", [](auto &M) { return M.MotorBlowersSwitch(false, end::rear, range_t::consist); }}, + {"CompartmentLightsSwitch off", [](auto &M) { return M.CompartmentLightsSwitch(false, range_t::unit); }}, + {"BatterySwitch off", [](auto &M) { return M.BatterySwitch(false, range_t::unit); }}, + {"CabDeactivisation", [](auto &M) { return M.CabDeactivisation(); }}, +}; + +} // namespace + +TEST_CASE("commands: every switch of the cab, sent to the next vehicle") { + for (auto const &path : test::real_vehicles()) { + CAPTURE(path); + test::reseed_random(); + vehicle driven{definition::fiz(path), 0.0, "driven", 1}; + vehicle next{definition::fiz(path), 0.0, "next"}; + driven->Attach(end::rear, end::front, &next.mover(), test::consist::full_coupling, true, false); + auto done{false}; + auto channels{state()}; + for (auto const &channel : state()) { + channels.emplace_back("next." + channel.name, [&next, value = channel.value](auto const &) { return value(next.mover()); }); + } + channels.emplace_back("Done", [&done](auto const &) { return flag(done); }); + test::trace trace{"switches/" + path, channels, 1, false}; + trace.sample(0.0, driven.mover()); + for (std::size_t i = 0; i < actions.size(); ++i) { + trace.mark(actions[i].name); + done = actions[i].work(driven.mover()); + trace.sample(static_cast(i + 1), driven.mover()); + } + trace.check(); + } +} + +TEST_CASE("commands: every switch of the cab, worked on a pair standing under the catenary") { + // the same switches with time for the vehicles to act on them, as a crew works them: the pantographs rise, + // the main switch closes, the converter and the compressor start, and the controllers take power + for (auto const &path : test::real_vehicles()) { + CAPTURE(path); + test::reseed_random(); + test::consist pair; + pair.add(definition::fiz(path), 1); + pair.add(definition::fiz(path)); + auto done{false}; + auto channels{state()}; + channels.insert(channels.end(), { + {"PantFrontActive", [](auto const &M) { return flag(M.Pantographs[end::front].is_active); }}, + {"PantRearActive", [](auto const &M) { return flag(M.Pantographs[end::rear].is_active); }}, + {"PantPress", [](auto const &M) { return M.PantPress; }}, + {"GroundRelay", [](auto const &M) { return flag(M.GroundRelay); }}, + {"ConverterFlag", [](auto const &M) { return flag(M.ConverterFlag); }}, + {"CompressorFlag", [](auto const &M) { return flag(M.CompressorFlag); }}, + {"Compressor", [](auto const &M) { return M.Compressor; }}, + {"MainCtrlActualPos", [](auto const &M) { return M.MainCtrlActualPos; }}, + {"Itot", [](auto const &M) { return M.Itot; }}, + {"FuseFlag", [](auto const &M) { return flag(M.FuseFlag); }}, + {"EngineRotation", [](auto const &M) { return M.enrot; }}, + }); + auto const own{channels.size()}; + for (std::size_t i = 0; i < own; ++i) { + channels.emplace_back("next." + channels[i].name, [&pair, value = channels[i].value](auto const &) { return value(pair[1].mover()); }); + } + channels.emplace_back("Done", [&done](auto const &) { return flag(done); }); + test::trace trace{"switches_live/" + path, channels, 1, false}; + trace.sample(0.0, pair[0].mover()); + test::track const catenary{.voltage = test::catenary_3kv}; + for (std::size_t i = 0; i < actions.size(); ++i) { + trace.mark(actions[i].name); + done = actions[i].work(pair[0].mover()); + pair.run(2.0, catenary); + trace.sample(static_cast(i + 1), pair[0].mover()); + } + trace.check(); + } +} + +namespace { + +// three locomotives coupled with the given coupling, the middle one facing backwards, as a train made up of +// whatever came to hand: the commands of the front one go to the rear coupler of the middle one and from its +// front coupler to the front one of the last, which faces backwards from the middle one, so forwards +struct train { + vehicle head{definition::fiz("4e_v1/4e"), 0.0, "front", 1}; + vehicle middle{definition::fiz("4e_v1/4e"), 0.0, "middle"}; + vehicle last{definition::fiz("4e_v1/4e"), 0.0, "last"}; + + explicit train(int Coupling) { + head->Attach(end::rear, end::rear, &middle.mover(), Coupling, true, false); + middle->Attach(end::front, end::front, &last.mover(), Coupling, true, false); + } +}; + +} // namespace + +TEST_CASE("commands: travel along the train, turned round for a vehicle facing backwards") { + train consist{test::consist::full_coupling}; + // the driver takes the front cab, which the others follow + REQUIRE(consist.head->CabActivisation()); + CHECK(consist.head->CabActive == 1); + CHECK(consist.middle->CabActive == -1); + CHECK(consist.last->CabActive == 1); + // the command is done in each vehicle and cleared + CHECK(consist.middle->CommandIn.Command.empty()); + CHECK(consist.last->CommandIn.Command.empty()); + + SUBCASE("door permits: the right side of the sender is the left one of a vehicle facing backwards") { + consist.head->PermitDoors(side::right, true, range_t::consist); + CHECK(consist.head->Doors.instances[side::right].open_permit); + CHECK(consist.middle->Doors.instances[side::left].open_permit); + CHECK_FALSE(consist.middle->Doors.instances[side::right].open_permit); + CHECK(consist.last->Doors.instances[side::right].open_permit); + // and revoked the same way + consist.head->PermitDoors(side::right, false, range_t::consist); + CHECK_FALSE(consist.middle->Doors.instances[side::left].open_permit); + CHECK_FALSE(consist.last->Doors.instances[side::right].open_permit); + } + SUBCASE("a pantograph valve: the vehicles past one coupled by its control cable take the other pantograph") { + consist.head->OperatePantographValve(end::front, operation_t::enable, range_t::consist); + CHECK(consist.head->Pantographs[end::front].valve.is_enabled); + CHECK(consist.middle->Pantographs[end::front].valve.is_enabled); + CHECK(consist.last->Pantographs[end::rear].valve.is_enabled); + } + SUBCASE("sent backwards, it goes no further than the sender: nothing is coupled at its front") { + consist.head->RunCommand("DoorLock", 0, -1, coupling::control); + CHECK_FALSE(consist.head->Doors.lock_enabled); + CHECK(consist.middle->Doors.lock_enabled); + } + SUBCASE("the same command again isn't taken by the next vehicle a second time") { + CHECK(consist.middle->SetInternalCommand("DoorStep", 0, 1, coupling::control)); + CHECK_FALSE(consist.middle->SetInternalCommand("DoorStep", 0, 1, coupling::control)); + CHECK_FALSE(consist.head->RunInternalCommand()); // nothing to run + } +} + +TEST_CASE("commands: don't pass a coupling without the control cable") { + train consist{coupling::coupler | coupling::brakehose}; + consist.head->RunCommand("DoorLock", 0, 1, coupling::control); + CHECK_FALSE(consist.head->Doors.lock_enabled); + CHECK(consist.middle->Doors.lock_enabled); + // a command of another coupling goes through it + consist.head->RunCommand("DoorStep", 0, 1, coupling::brakehose); + CHECK_FALSE(consist.middle->Doors.step_enabled); +} + +TEST_CASE("commands: a wagon loaded and unloaded by commands, which repeat until done") { + vehicle wagon{definition::fiz("3wl_v2/3wl")}; + REQUIRE(wagon->MaxLoad > 0); + test::trace trace{"commands/loading", state(), 100}; + wagon.record(trace); + + // the game gives the commands of loading through PutCommand, with the place of the platform or the ramp + SUBCASE("loading 12 tonnes of coal") { + trace.mark("load"); + wagon->PutCommand("Load=Coal", 12, 0, wagon->Loc); + wagon.run(30.0); + CHECK(wagon->LoadAmount == doctest::Approx(12.0).epsilon(0.1)); + CHECK(wagon->LoadType.name == "coal"); + CHECK(wagon->CommandIn.Command.empty()); + + trace.mark("unload"); + wagon->PutCommand("UnLoad=Coal", 20, 0, wagon->Loc); + wagon.run(200.0); + CHECK(wagon->LoadAmount == doctest::Approx(0.0)); + CHECK(wagon->LoadType.name.empty()); + trace.check(); + } + SUBCASE("a load the wagon doesn't take stays a command, not done") { + wagon->PutCommand("Load=passengers", 12, 0, wagon->Loc); + wagon.run(10.0); + CHECK(wagon->LoadAmount == doctest::Approx(0.0)); + CHECK_FALSE(wagon->CommandIn.Command.empty()); + } + SUBCASE("away from the place of the command nothing is loaded") { + wagon->PutCommand("Load=Coal", 12, 5.0, TLocation{100.0, 0.0, 0.0}); + wagon.run(10.0); + CHECK(wagon->LoadAmount == doctest::Approx(0.0)); + CHECK(wagon->LastLoadChangeTime == doctest::Approx(0.0)); + } + SUBCASE("unloading an empty wagon isn't done either") { + wagon->PutCommand("UnLoad=Coal", 12, 0, wagon->Loc); + wagon.run(10.0); + CHECK_FALSE(wagon->CommandIn.Command.empty()); + } +} diff --git a/tests/unit/test_consist.cpp b/tests/unit/test_consist.cpp new file mode 100644 index 000000000..8305be171 --- /dev/null +++ b/tests/unit/test_consist.cpp @@ -0,0 +1,135 @@ +#include "stdafx.h" +#include "doctest.h" +#include "consist.h" +#include "drive.h" +#include "golden.h" + +#include "McZapkie/hamulce.h" + +using test::consist; +using test::definition; + +// characterisation of trains of coupled vehicles: the real electric units driven whole, a locomotive with +// wagons, the air, the control and the coupler forces between the vehicles, in golden traces +// (data/golden/consist/), each vehicle of the train in its own + +namespace { + +struct unit { + char const *name; + std::vector cars; // from the front, the first one driven +}; + +std::vector const units{ + {"en57", {"en57_v1/5bgaiii_led_spot", "en57_v1/6ba_840", "en57_v1/5bgb"}}, + {"ed72", {"ed72_v1/5bs-ra", "ed72_v1/6bs-sa", "ed72_v1/6bs-sb", "ed72_v1/5bs-rb"}}, + {"ew58", {"ew58_v1/ew58-sa", "ew58_v1/ew58-d", "ew58_v1/ew58-sb"}}, + {"elf", {"elf_v1/21we-a", "elf_v1/21we-b", "elf_v1/21we-c"}}, + {"impuls", {"impuls_v1/31we-a_pr", "impuls_v1/31we-b_pr", "impuls_v1/31we-c_pr", "impuls_v1/31we-d_pr"}}, +}; + +std::vector car_channels() { + using namespace test::channels; + return motion() + traction() + air() + brakes(); +} + +// a trace for each vehicle of the train, recording it +std::vector> record(consist &Train, std::string const &Name) { + std::vector> traces; + for (std::size_t i = 0; i < Train.size(); ++i) { + traces.push_back(std::make_unique(std::format("consist/{}/{}", Name, i), car_channels(), 100)); + Train[i].record(*traces.back()); + } + return traces; +} + +// the events go to every trace +struct all_traces { + std::vector> &traces; + void mark(std::string const &Event) const { + for (auto const &trace : traces) { + trace->mark(Event); + } + } +}; + +} // namespace + +TEST_CASE("consist: every real electric unit driven whole from its front cab") { + for (auto const &unit : units) { + CAPTURE(unit.name); + test::reseed_random(); + consist train; + for (auto const *car : unit.cars) { + train.add(definition::fiz(car), train.size() == 0 ? 1 : 0); + } + auto traces{record(train, unit.name)}; + // the drive cycle marks the events in the trace it gets; the first car's carries them + test::drive_cycle(test::driving(train), test::track{0.15, 0.0, test::catenary_3kv}, *traces.front()); + // every unit runs off and is braked to a stop, but the EN57, which doesn't get ready (COVERAGE.md) + auto const &leading{train[0].mover()}; + CHECK((leading.DistCounter > 0.2 /* km */) != (std::string{unit.name} == "en57")); + CHECK(leading.Vel < 0.5); + for (auto const &trace : traces) { + trace->check(); + } + } +} + +TEST_CASE("consist: a locomotive with wagons, driven, braked by its handle and pulled apart by the couplers") { + test::reseed_random(); + consist train; + train.add(definition::fiz("4e_v1/4e"), 1); + for (auto const *wagon : {"3wl_v2/3wl", "11xa_v2/93a", "bdhpumn_v3/bdhpumn", "z1_v2/152a"}) { + train.add(definition::fiz(wagon)); + } + auto traces{record(train, "eu07 with wagons")}; + test::drive_cycle(test::driving(train), test::track{0.15, 0.0, test::catenary_3kv}, *traces.front()); + for (auto const &trace : traces) { + trace->check(); + } + // the couplers kept the train together + for (std::size_t i = 0; i + 1 < train.size(); ++i) { + CAPTURE(i); + CHECK(std::abs(train.gap(i)) < 0.5); + } +} + +namespace { + +// what the cab's ammeters show (Train.cpp, the driver's panel) and the brake force the AI expects of the +// vehicle (Driver.cpp), read of the driven vehicle +std::vector gauges() { + std::vector result; + for (int ammeter = 0; ammeter < 4; ++ammeter) { + result.emplace_back(std::format("ShowCurrent{}", ammeter), [ammeter](auto const &M) { return M.ShowCurrent(ammeter); }); + } + result.emplace_back("BrakeForceR0.25", [](auto const &M) { return M.BrakeForceR(0.25, M.Vel); }); + result.emplace_back("BrakeForceR1", [](auto const &M) { return M.BrakeForceR(1.0, M.Vel); }); + result.emplace_back("BrakeForceR1at20", [](auto const &M) { return M.BrakeForceR(1.0, 20.0); }); + using test::channels::operator+; + return result + test::channels::traction(); +} + +} // namespace + +TEST_CASE("consist: the gauges of units and locomotives in multiple, driven") { + auto trains{units}; + trains.push_back({"eu07 pair", {"4e_v1/4e", "4e_v1/4e"}}); + trains.push_back({"et22 pair", {"et22_v3/201e-w", "et22_v3/201e-rn"}}); + trains.push_back({"ep09 pair", {"ep09_v2/104e", "ep09_v2/104e"}}); + trains.push_back({"sn61 three", {"sn61_v2/sn61", "sn61_v2/sn61", "sn61_v2/sn61"}}); + trains.push_back({"su45 pair", {"su45_v2/301d", "su45_v2/301d"}}); + for (auto const &train : trains) { + CAPTURE(train.name); + test::reseed_random(); + consist vehicles; + for (auto const *car : train.cars) { + vehicles.add(definition::fiz(car), vehicles.size() == 0 ? 1 : 0); + } + test::trace trace{std::string{"gauges/"} + train.name, gauges(), 100}; + vehicles[0].record(trace); + test::drive_cycle(test::driving(vehicles), test::track{0.15, 0.0, test::catenary_3kv}, trace); + trace.check(); + } +} diff --git a/tests/unit/test_controls.cpp b/tests/unit/test_controls.cpp new file mode 100644 index 000000000..4e5255ff4 --- /dev/null +++ b/tests/unit/test_controls.cpp @@ -0,0 +1,632 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scenario.h" +#include "drive.h" + +#include "McZapkie/hamulce.h" +#include "utilities/utilities.h" + +// characterisation of the controls a driver and a crew use besides driving and braking: doors, direction and +// cab change, sanding against slip, the emergency brake, releasers, the ep and the dynamic brake, heating, +// loading, collisions and the frame of several physics iterations, each in a scenario on real vehicles, +// recorded in golden traces (data/golden/controls/) + +using test::scenario; +using namespace test::channels; + +namespace { + +void notch_up(scenario &Scenario, int Positions, double Seconds) { + for (int i = 0; i < Positions; ++i) { + Scenario.driver().controlling.IncMainCtrl(1); + Scenario.wait(Seconds); + } +} + +void notch_down(scenario &Scenario) { + while (Scenario.driver().controlling.DecMainCtrl(1)) { + Scenario.wait(0.5); + } + Scenario.driver().controlling.DecScndCtrl(2); +} + +void brake_to_stop(scenario &Scenario, double Seconds = 60.0) { + Scenario.act("brake to stop"); + Scenario.cab().BrakeLevelSet(Scenario.cab().Handle->GetPos(bh_FB)); + Scenario.wait(Seconds); + Scenario.cab().BrakeLevelSet(Scenario.cab().Handle->GetPos(bh_RP)); + Scenario.wait(20.0); +} + +// the pipe charged again after an emergency stop: the releaser held with the handle at running, then let go +void recharge(scenario &Scenario) { + auto &cab{Scenario.cab()}; + Scenario.act("releaser held, the pipe charged"); + cab.BrakeLevelSet(cab.Handle->GetPos(bh_RP)); + cab.BrakeReleaser(1); + Scenario.wait(30.0); + cab.BrakeReleaser(0); + Scenario.wait(30.0); +} + +bool stopped(scenario &Scenario) { + for (std::size_t i = 0; i < Scenario.train().size(); ++i) { + if (Scenario.vehicle(i).Vel > 0.1) { + return false; + } + } + return true; +} + +// the brake cylinders of every vehicle of the train under the pressure given +bool released(scenario &Scenario, double Pressure = 0.1) { + for (std::size_t i = 0; i < Scenario.train().size(); ++i) { + if (Scenario.vehicle(i).BrakePress > Pressure) { + return false; + } + } + return true; +} + +bool open(TMoverParameters const &Vehicle, int Side) { + return Vehicle.Doors.instances[Side].is_open; +} + +// the leaf shut, whatever the step does (closed, is_closed, wants the step folded too) +bool shut(TMoverParameters const &Vehicle, int Side) { + return Vehicle.Doors.instances[Side].position <= 0.f; +} + +bool closed(TMoverParameters const &Vehicle, int Side) { + return Vehicle.Doors.instances[Side].is_closed; +} + +} // namespace + +TEST_CASE("controls: the doors of an electric unit, opened, locked, permitted and closed by driving off") { + for (auto const &[name, cars] : std::vector>>{ + {"elf", {"elf_v1/21we-a", "elf_v1/21we-b", "elf_v1/21we-c"}}, + {"en57", {"en57_v1/5bgaiii_led_spot", "en57_v1/6ba_840", "en57_v1/5bgb"}}, + {"impuls", {"impuls_v1/31we-a_pr", "impuls_v1/31we-b_pr", "impuls_v1/31we-c_pr", "impuls_v1/31we-d_pr"}}}) { + CAPTURE(name); + scenario scenario{"controls/doors/" + name, motion() + doors()}; + for (auto const *car : cars) { + scenario.add(car, scenario.train().size() == 0 ? 1 : 0); + } + // the EN57 doesn't get ready (COVERAGE.md), its doors work all the same + auto const ready{scenario.start_up()}; + CHECK(ready == (name != "en57")); + auto &cab{scenario.cab()}; + auto const &last{scenario.vehicle(scenario.train().size() - 1)}; + scenario.act("permit left"); + cab.PermitDoors(side::left, true); + scenario.wait(2.0); + scenario.act("open left"); + cab.OperateDoors(side::left, true); + scenario.wait(10.0); + CHECK(open(cab, side::left)); + CHECK(open(last, side::left)); + CHECK(closed(cab, side::right)); + scenario.act("close left"); + cab.OperateDoors(side::left, false); + scenario.wait(10.0); + CHECK(shut(cab, side::left)); + CHECK(shut(last, side::left)); + scenario.act("open right without permit"); + cab.OperateDoors(side::right, true); + scenario.wait(5.0); + // the EN57 needs no permit + CHECK(shut(cab, side::right) == cab.Doors.permit_needed); + scenario.act("permit right, open"); + cab.PermitDoors(side::right, true); + cab.OperateDoors(side::right, true); + scenario.wait(10.0); + CHECK(open(cab, side::right)); + CHECK(open(last, side::right)); + scenario.act("lock"); + cab.LockDoors(true); + scenario.wait(5.0); + scenario.act("permit preset, steps off, remote control"); + cab.ChangeDoorPermitPreset(1); + cab.PermitDoorStep(false); + cab.ChangeDoorControlMode(true); + scenario.wait(5.0); + cab.ChangeDoorPermitPreset(-1); + cab.PermitDoorStep(true); + cab.ChangeDoorControlMode(false); + scenario.act("close all, unlock"); + cab.OperateDoors(side::right, false); + cab.OperateDoors(side::left, false); + cab.LockDoors(false); + scenario.wait(10.0); + CHECK(shut(cab, side::left)); + CHECK(shut(cab, side::right)); + scenario.act("open both and drive off"); + cab.PermitDoors(side::left, true); + cab.PermitDoors(side::right, true); + cab.OperateDoors(side::left, true); + cab.OperateDoors(side::right, true); + scenario.wait(5.0); + CHECK_FALSE(shut(cab, side::left)); + CHECK_FALSE(shut(cab, side::right)); + scenario.get_going(); + notch_up(scenario, 4, 2.0); + scenario.wait(20.0); + // the doors of a train ready to run close as the driver starts off. no CHECK of the speed: the Elf and + // the Impuls keep their steps out under the permit, which keeps them from moving (COVERAGE.md) + CHECK(shut(cab, side::left) == ready); + CHECK(shut(cab, side::right) == ready); + CHECK(shut(last, side::left) == ready); + CHECK(shut(last, side::right) == ready); + notch_down(scenario); + brake_to_stop(scenario); + scenario.check(); + } +} + +TEST_CASE("controls: the doors of wagons, worked from the locomotive") { + scenario scenario{"controls/doors/eu07 with wagons", motion() + doors()}; + scenario.add("4e_v1/4e", 1).add("bdhpumn_v3/bdhpumn").add("11xa_v2/93a"); + scenario.start_up(); + auto &cab{scenario.cab()}; + auto &coach{scenario.vehicle(1)}; + auto const &freight{scenario.vehicle(2)}; + // the locomotive works the coach's doors for the consist and for the unit, not with its local controls; the + // freight wagon's doors don't move from the locomotive + for (auto const range : {range_t::consist, range_t::unit, range_t::local}) { + CAPTURE(static_cast(range)); + scenario.act(std::format("open right {}", static_cast(range))); + cab.PermitDoors(side::right, true, range); + cab.OperateDoors(side::right, true, range); + scenario.wait(10.0); + CHECK(open(coach, side::right) == (range != range_t::local)); + CHECK(shut(coach, side::left)); + CHECK(shut(freight, side::right)); + scenario.act("close right"); + cab.OperateDoors(side::right, false, range); + scenario.wait(10.0); + CHECK(shut(coach, side::right)); + } + scenario.act("wagon opens its own"); + coach.OperateDoors(side::left, true, range_t::local); + // a second at a time, the same run as one wait. the coach's doors close by themselves 5 s after they + // opened (DoorStayOpen) + scenario.wait(1.0); + CHECK_FALSE(shut(coach, side::left)); + scenario.wait(9.0); + CHECK(shut(coach, side::left)); + coach.OperateDoors(side::left, false, range_t::local); + scenario.wait(10.0); + CHECK(shut(coach, side::left)); + scenario.check(); +} + +TEST_CASE("controls: reversing and changing the cab of a locomotive") { + scenario scenario{"controls/reverse/eu07", motion() + traction()}; + scenario.add("4e_v1/4e", 1); + scenario.start_up(); + scenario.get_going(); + auto &cab{scenario.cab()}; + // the speed along the vehicle, its front ahead + notch_up(scenario, 10, 1.5); + scenario.wait(10.0); + CHECK(cab.V > 3.0); + notch_down(scenario); + brake_to_stop(scenario); + CHECK(stopped(scenario)); + scenario.act("backward"); + cab.DirectionBackward(); + cab.DirectionBackward(); + scenario.wait(2.0); + CHECK(cab.DirActive < 0); + notch_up(scenario, 10, 1.5); + scenario.wait(10.0); + CHECK(cab.V < -3.0); + notch_down(scenario); + brake_to_stop(scenario); + CHECK(stopped(scenario)); + scenario.act("neutral, change cab"); + cab.DirectionForward(); + scenario.wait(1.0); + cab.CabDeactivisation(); + // through the engine room (cab 0) to the rear cab, as the player walks there + cab.ChangeCab(-1); + cab.ChangeCab(-1); + cab.CabActivisation(); + scenario.wait(5.0); + CHECK(cab.CabActive < 0); + scenario.act("drive from the rear cab"); + scenario.get_going(); + notch_up(scenario, 10, 1.5); + scenario.wait(10.0); + // forward from the rear cab is the vehicle's backward + CHECK(cab.V < -3.0); + notch_down(scenario); + brake_to_stop(scenario); + CHECK(stopped(scenario)); + scenario.check(); +} + +TEST_CASE("controls: wheel slip on a slippery track, sand and the anti-slip brake") { + for (auto const *path : {"4e_v1/4e", "sm42_v1/6d1", "e186_v2/eu47"}) { + CAPTURE(path); + scenario scenario{std::string{"controls/slip/"} + path, motion() + traction() + brakes(), test::track{0.04, 0.0, test::catenary_3kv}}; + scenario.add(path, 1); + scenario.start_up(); + scenario.get_going(); + auto &cab{scenario.cab()}; + // a notch a second: the controller put to full power at once never gets the line contactors closed + scenario.act("full power"); + scenario.full_power(0.0); + auto slipped{cab.SlippingWheels}; + for (int i = 0; i < 10; ++i) { + scenario.wait(1.0); + slipped = slipped || cab.SlippingWheels; + } + // the electric locomotives slip at full power, the SM42, of less power for its weight, doesn't + CHECK(slipped == (std::string{path} != "sm42_v1/6d1")); + scenario.act("sand"); + cab.Sandbox(true); + scenario.wait(10.0); + cab.Sandbox(false); + scenario.act("manual sand"); + cab.SandboxManual(true); + scenario.wait(5.0); + cab.SandboxManual(false); + scenario.act("anti-slip brake"); + cab.AntiSlippingButton(); + cab.AntiSlippingBrake(); + scenario.wait(10.0); + CHECK(cab.Vel > 5.0); + notch_down(scenario); + brake_to_stop(scenario); + CHECK(stopped(scenario)); + scenario.check(); + } +} + +TEST_CASE("controls: emergency stops by the alarm chain and the radio-stop") { + scenario scenario{"controls/emergency/eu07 with wagons", motion() + brakes()}; + scenario.add("4e_v1/4e", 1).add("bdhpumn_v3/bdhpumn").add("3wl_v2/3wl"); + scenario.start_up(); + scenario.get_going(); + notch_up(scenario, 20, 1.0); + scenario.wait(20.0); + notch_down(scenario); + auto &cab{scenario.cab()}; + CHECK(cab.Vel > 30.0); + scenario.act("alarm chain in the wagon"); + scenario.vehicle(1).AlarmChainSwitch(true); + scenario.wait(40.0); + // the pipe emptied, the whole train braked to a stop + CHECK(stopped(scenario)); + CHECK(cab.PipePress < 0.5); + CHECK(scenario.vehicle(2).BrakePress > 1.0); + scenario.act("alarm chain reset"); + scenario.vehicle(1).AlarmChainSwitch(false); + scenario.wait(10.0); + // the FV4a handle stays cut off the emptied pipe of an LSt locomotive (lock_old in UpdatePipePressure) + // until the releaser is held + CHECK(cab.PipePress < 0.5); + recharge(scenario); + CHECK(cab.PipePress > 4.5); + scenario.get_going(); + notch_up(scenario, 20, 1.0); + scenario.wait(20.0); + notch_down(scenario); + CHECK(cab.Vel > 30.0); + scenario.act("radio-stop"); + cab.RadiostopSwitch(true); + scenario.wait(40.0); + CHECK(stopped(scenario)); + CHECK(cab.PipePress < 0.5); + scenario.act("radio-stop reset"); + cab.RadiostopSwitch(false); + scenario.wait(10.0); + recharge(scenario); + CHECK(cab.PipePress > 4.5); + scenario.check(); +} + +TEST_CASE("controls: releasers and the universal brake buttons") { + scenario scenario{"controls/releaser/eu07 with wagons", brakes() + air()}; + scenario.add("4e_v1/4e", 1).add("bdhpumn_v3/bdhpumn").add("3wl_v2/3wl"); + scenario.start_up(); + scenario.get_going(); + auto &cab{scenario.cab()}; + auto &wagon{scenario.vehicle(2)}; + scenario.act("service brake"); + cab.BrakeLevelSet(cab.Handle->GetPos(bh_FB)); + scenario.wait(20.0); + CHECK(cab.BrakePress > 1.0); + CHECK(wagon.BrakePress > 1.0); + cab.BrakeLevelSet(cab.Handle->GetPos(bh_NP)); + auto &coach{scenario.vehicle(1)}; + // the releaser of the cab is passed along the whole train, a wagon's works on the wagon alone + auto const coachbraked{coach.BrakePress}; + auto wagonbraked{wagon.BrakePress}; + scenario.act("releaser of the locomotive"); + cab.BrakeReleaser(1); + scenario.wait(10.0); + cab.BrakeReleaser(0); + CHECK(cab.BrakePress < 0.5); + CHECK(coach.BrakePress < coachbraked - 1.0); + CHECK(wagon.BrakePress < wagonbraked - 0.5); + CHECK(cab.PipePress < 4.0); // the pipe stays where the service application left it + wagonbraked = wagon.BrakePress; + scenario.act("releaser of the wagon"); + wagon.BrakeReleaser(1); + scenario.wait(10.0); + wagon.BrakeReleaser(0); + CHECK(wagon.BrakePress < wagonbraked - 0.5); + for (int button = 0; button < 3; ++button) { + scenario.act(std::format("universal button {}", button)); + cab.UniversalBrakeButton(button, 1); + scenario.wait(5.0); + cab.UniversalBrakeButton(button, 0); + scenario.wait(5.0); + } + scenario.act("running"); + cab.BrakeLevelSet(cab.Handle->GetPos(bh_RP)); + scenario.wait(60.0); + CHECK(cab.PipePress > 4.8); + CHECK(released(scenario)); + scenario.check(); +} + +TEST_CASE("controls: the ep brake of electric units") { + for (auto const &[name, cars] : std::vector>>{ + {"ed72", {"ed72_v1/5bs-ra", "ed72_v1/6bs-sa", "ed72_v1/6bs-sb", "ed72_v1/5bs-rb"}}, + {"ew58", {"ew58_v1/ew58-sa", "ew58_v1/ew58-d", "ew58_v1/ew58-sb"}}}) { + CAPTURE(name); + scenario scenario{"controls/ep brake/" + name, motion() + brakes() + handles()}; + for (auto const *car : cars) { + scenario.add(car, scenario.train().size() == 0 ? 1 : 0); + } + scenario.start_up(); + scenario.get_going(); + auto &cab{scenario.cab()}; + scenario.act("ep on"); + cab.SwitchEPBrake(1); + scenario.wait(5.0); + auto const &last{scenario.vehicle(scenario.train().size() - 1)}; + auto const positions{std::vector{bh_EPB, bh_EPN, bh_EPR, bh_EPB, bh_RP}}; + for (std::size_t i = 0; i < positions.size(); ++i) { + CAPTURE(positions[i]); + scenario.act(std::format("handle {}", positions[i])); + cab.BrakeLevelSet(cab.Handle->GetPos(positions[i])); + scenario.wait(15.0); + if (i == 2) { + CHECK(released(scenario)); + } + if (i == 3) { + // the ep brake works the cylinders of every car by wire, the pipe stays charged (by the first + // application the EW58's pipe is still charging after the start-up) + CHECK(cab.PipePress > 4.8); + CHECK(cab.BrakePress > 1.0); + CHECK(last.BrakePress > 1.0); + } + } + // the EW58 releases at the running position, the ED72 holds the ep application there, and with the ep + // brake switched off as well: only the ep release position lets it off + auto const ed72{name == "ed72"}; + CHECK(released(scenario) != ed72); + scenario.act("ep off"); + cab.SwitchEPBrake(0); + scenario.wait(30.0); + CHECK(released(scenario) != ed72); + scenario.check(); + } +} + +TEST_CASE("controls: the electrodynamic brake of locomotives having one") { + for (auto const *path : {"ep09_v2/104e", "e186_v2/eu47", "e6act_v2/e6acta", "et22_v3/201e-rn"}) { + CAPTURE(path); + scenario scenario{std::string{"controls/dynamic brake/"} + path, motion() + traction() + brakes()}; + scenario.add(path, 1).add("bdhpumn_v3/bdhpumn").add("bdhpumn_v3/bdhpumn"); + scenario.start_up(); + scenario.get_going(); + notch_up(scenario, 60, 1.0); + scenario.wait(30.0); + notch_down(scenario); + auto &cab{scenario.cab()}; + CHECK(cab.Vel > 40.0); + auto const speed{cab.Vel}; + scenario.act("dynamic brake on"); + cab.DynamicBrakeSwitch(true); + scenario.wait(5.0); + scenario.act("brake"); + cab.BrakeLevelSet(cab.Handle->GetPos(bh_FB)); + scenario.wait(40.0); + CHECK(cab.Vel < speed); + scenario.act("dynamic brake off"); + cab.DynamicBrakeSwitch(false); + brake_to_stop(scenario, 30.0); + CHECK(stopped(scenario)); + scenario.check(); + } +} + +TEST_CASE("controls: train heating from the locomotive") { + scenario scenario{"controls/heating/eu07 with wagons", traction() + air()}; + scenario.add("4e_v1/4e", 1).add("bdhpumn_v3/bdhpumn").add("bdhpumn_v3/bdhpumn"); + scenario.start_up(); + auto &cab{scenario.cab()}; + scenario.act("heating on"); + cab.HeatingSwitch(true); + scenario.wait(30.0); + CHECK(cab.Heating); + CHECK(scenario.vehicle(2).Heating); + scenario.act("heating off"); + cab.HeatingSwitch(false); + scenario.wait(10.0); + CHECK_FALSE(cab.Heating); + CHECK_FALSE(scenario.vehicle(2).Heating); + scenario.check(); +} + +TEST_CASE("controls: a freight wagon loaded and braked") { + auto emptymass{0.0}; + for (auto const *load : {"", "coal", "ruda", "unknown"}) { + CAPTURE(load); + scenario scenario{std::string{"controls/load/3wl "} + (*load ? load : "empty"), motion() + brakes()}; + scenario.add("4e_v1/4e", 1).add("3wl_v2/3wl"); + auto &wagon{scenario.vehicle(1)}; + auto const known{std::string{load} == "coal" || std::string{load} == "ruda"}; + CHECK(wagon.AssignLoad(load, 30.f) == (known || std::string{load}.empty())); + wagon.ComputeMass(); + if (*load == '\0') { + emptymass = wagon.TotalMass; + } + else { + // 30 t of a load the wagon takes, none of one it doesn't + CHECK(wagon.TotalMass == doctest::Approx(emptymass + (known ? 30000.0 : 0.0)).epsilon(0.01)); + } + scenario.start_up(); + scenario.get_going(); + notch_up(scenario, 20, 1.0); + scenario.wait(20.0); + notch_down(scenario); + CHECK(scenario.cab().Vel > 20.0); + brake_to_stop(scenario); + CHECK(stopped(scenario)); + scenario.check(); + } +} + +TEST_CASE("controls: a locomotive running into a standing wagon") { + for (auto const speed : {5.0, 15.0, 40.0}) { + CAPTURE(speed); + scenario scenario{std::format("controls/collision/{} kmh", static_cast(speed)), motion()}; + // the wagon stands in front, the locomotive comes from behind it + scenario.add("bdhpumn_v3/bdhpumn").add("4e_v1/4e", 0, coupling::faux, speed, 30.0); + auto const &wagon{scenario.vehicle(0)}; + scenario.wait(30.0); + // slow, the locomotive pushes the wagon off, the buffers only touching, and both roll to a stop; fast, + // the wagon derails where it stands + auto const derailed{speed > 30.0}; + CHECK(TestFlag(wagon.DamageFlag, dtrain_out) == derailed); + CHECK((std::abs(wagon.DistCounter) > 0.0001) != derailed); + CHECK(stopped(scenario)); + scenario.check(); + } +} + +TEST_CASE("controls: a train run with five physics steps in a frame") { + scenario scenario{"controls/full physics/eu07 with wagons", motion() + traction() + brakes(), + test::track{0.15, 0.0, test::catenary_3kv, 0, 5}}; + scenario.add("4e_v1/4e", 1).add("bdhpumn_v3/bdhpumn").add("3wl_v2/3wl"); + scenario.start_up(); + scenario.get_going(); + notch_up(scenario, 25, 1.0); + scenario.wait(30.0); + notch_down(scenario); + // no CHECKs of the run: with five steps a frame the motors take no current and the train rolls at a few + // km/h, which is a finding (COVERAGE.md), not the behaviour to keep + brake_to_stop(scenario); + scenario.check(); +} + +TEST_CASE("controls: the automatic electrodynamic brake of the EP09, in service braking held to a stop") { + // held for longer, a service application of the train brake makes the EP09 brake electrodynamically: the + // pressure its distributor asks for (GetEDBCP) brings the motors in as generators, the air of its own + // cylinders is let out, and given back as the train slows down and the motors' brake force fades + struct application { + char const *name; + double steps; // past the first braking position of the handle + }; + for (auto const &[name, steps] : {application{"light", 0.0}, application{"medium", 1.0}, application{"high", 2.0}, application{"full service", 3.0}}) { + CAPTURE(name); + scenario scenario{std::string{"controls/dynamic brake held/ep09 "} + name, motion() + traction() + brakes()}; + scenario.add("ep09_v2/104e", 1, test::consist::full_coupling, 120.0); + scenario.add("bdhpumn_v3/bdhpumn", 0, test::consist::full_coupling, 120.0); + scenario.add("bdhpumn_v3/bdhpumn", 0, test::consist::full_coupling, 120.0); + REQUIRE(scenario.start_up()); + auto &ep09{scenario.cab()}; + REQUIRE(ep09.Power110vIsAvailable); + scenario.act(std::string{"brake "} + name); + ep09.BrakeLevelSet(ep09.Handle->GetPos(bh_MB) + steps); + scenario.wait(10.0); + CHECK(ep09.DynamicBrakeFlag); + CHECK(ep09.Im < -100.0); + CHECK(ep09.BrakePress < 0.25 * ep09.Hamulec->GetEDBCP()); + scenario.wait(110.0); + // stopped, held by the air alone. the flag of the electrodynamic brake stays on while the cylinders hold + // less than 2.1 bar, though the motors give nothing; a stronger application drops it before the stop + CHECK(ep09.Vel == doctest::Approx(0.0)); + CHECK(ep09.Im == doctest::Approx(0.0)); + CHECK(ep09.DynamicBrakeFlag == (ep09.BrakePress <= 2.1)); + CHECK(ep09.BrakePress == doctest::Approx(ep09.Hamulec->GetEDBCP()).epsilon(0.05)); + scenario.act("release"); + ep09.BrakeLevelSet(ep09.Handle->GetPos(bh_RP)); + scenario.wait(30.0); + scenario.check(); + } +} + +TEST_CASE("controls: the automatic electrodynamic brake under ep and service braking, from speed") { + // ep, light and full service braking of trains running at speed, whose locomotive or unit has the automatic + // electrodynamic brake + struct train { + char const *name; + std::vector cars; // the first one driven + double speed; // km/h at the start + bool epelectric; // whether the ep brake alone brings the motors in + }; + // the EW58's motors brake by the stages of the ep brake (TUHEX), with the 110 V of the converter in its + // middle car; the E6ACT's by its distributor, from a deeper service application + for (auto const &[name, cars, speed, epelectric] : { + train{"e6act with wagons", {"e6act_v2/e6acta", "bdhpumn_v3/bdhpumn", "bdhpumn_v3/bdhpumn"}, 100.0, false}, + train{"ew58", {"ew58_v1/ew58-sa", "ew58_v1/ew58-d", "ew58_v1/ew58-sb"}, 90.0, true}, + }) { + CAPTURE(name); + scenario scenario{std::string{"controls/dynamic brake from speed/"} + name, motion() + traction() + brakes()}; + for (auto const *car : cars) { + scenario.add(car, car == cars.front() ? 1 : 0, test::consist::full_coupling, speed); + } + scenario.start_up(); + auto &cab{scenario.cab()}; + auto const brake{[&scenario, &cab](char const *Event, double Position, double Seconds) { + scenario.act(Event); + cab.BrakeLevelSet(Position); + scenario.wait(Seconds); + }}; + brake("ep brake", cab.Handle->GetPos(bh_EPB), 15.0); + CHECK(cab.DynamicBrakeFlag == epelectric); + CHECK((cab.Im < -100.0) == epelectric); + brake("ep hold", cab.Handle->GetPos(bh_EPN), 5.0); + brake("first step", cab.Handle->GetPos(bh_MB), 20.0); + brake("deeper", cab.Handle->GetPos(bh_MB) + 1.0, 15.0); + // the pipe alone doesn't bring the EW58's motors in + CHECK((cab.Im < -50.0) != epelectric); + brake("release", cab.Handle->GetPos(bh_RP), 20.0); + brake("full service", cab.Handle->GetPos(bh_FB), 40.0); + brake("release", cab.Handle->GetPos(bh_RP), 20.0); + scenario.check(); + } +} + +TEST_CASE("controls: a draisine pumped along by hand") { + // the pumping force, half a stroke forwards and half backwards, is below the resistance of a draisine + // standing, which stays where it is; a rolling one is kept going + for (auto const speed : {0.0, 10.0}) { + CAPTURE(speed); + scenario scenario{std::format("controls/draisine/{}", static_cast(speed)), motion() + traction()}; + scenario.add("dr2_v1/dr2", 1, test::consist::full_coupling, speed); + auto &cab{scenario.cab()}; + cab.CabActivisation(); + scenario.act("hand brake off"); + test::release_brakes(cab); + for (int stroke = 0; stroke < 40; ++stroke) { + scenario.act("stroke"); + cab.IncMainCtrl(1); + scenario.wait(0.5); + cab.DecMainCtrl(1); + scenario.wait(0.5); + } + CHECK((speed > 0.0) == (cab.Vel > 1.0)); + scenario.act("coast"); + scenario.wait(20.0); + scenario.check(); + } +} diff --git a/tests/unit/test_couplers_trains.cpp b/tests/unit/test_couplers_trains.cpp new file mode 100644 index 000000000..926964dbe --- /dev/null +++ b/tests/unit/test_couplers_trains.cpp @@ -0,0 +1,174 @@ +#include "stdafx.h" +#include "doctest.h" +#include "consist.h" +#include "scenario.h" + +#include "utilities/utilities.h" + +// characterisation of the couplers between vehicles: coupling and uncoupling by hand (Attach, DettachStatus, +// Dettach), the automatic couplers coupling on contact, a broken coupler, and collisions which derail, on real +// vehicles, the moving ones recorded in golden traces (data/golden/couplers/) + +using test::consist; +using test::definition; +using test::scenario; +using namespace test::channels; + +TEST_CASE("couplers: coupled by hand, touching or apart, of the same kind or not") { + consist train; + auto &locomotive{train.add(definition::fiz("4e_v1/4e")).mover()}; + // the wagon's buffers touch the locomotive's, the unit's car stands a metre away + auto &wagon{train.add(definition::fiz("3wl_v2/3wl"), 0, coupling::faux).mover()}; + train.step(); + + // nothing to couple to, or nothing to couple + CHECK_FALSE(locomotive.Attach(end::rear, end::front, nullptr, coupling::coupler)); + CHECK_FALSE(locomotive.Attach(end::rear, end::front, &wagon, coupling::faux)); + // the hook first: the coupler alone, which sounds + locomotive.Couplers[end::rear].sounds = 0; + REQUIRE(locomotive.Attach(end::rear, end::front, &wagon, coupling::coupler)); + CHECK(locomotive.Couplers[end::rear].Connected == &wagon); + CHECK(wagon.Couplers[end::front].Connected == &locomotive); + CHECK(wagon.Couplers[end::front].CouplingFlag == coupling::coupler); + CHECK(locomotive.Couplers[end::rear].sounds == sound::attachcoupler); + // then the hoses: only what changes sounds + locomotive.Couplers[end::rear].sounds = 0; + REQUIRE(locomotive.Attach(end::rear, end::front, &wagon, coupling::coupler | coupling::brakehose | coupling::mainhose)); + CHECK(locomotive.Couplers[end::rear].sounds == (sound::attachbrakehose | sound::attachmainhose)); + // and again, quietly, the other side naming the coupler by the one it's connected to (2) + locomotive.Couplers[end::rear].sounds = 0; + REQUIRE(wagon.Attach(end::front, 2, &locomotive, coupling::coupler | coupling::brakehose, false, false)); + CHECK(locomotive.Couplers[end::rear].CouplingFlag == (coupling::coupler | coupling::brakehose)); + CHECK(locomotive.Couplers[end::rear].sounds == 0); + + // a screw coupler with the buffers pressed together comes apart whole + train.step(); + CHECK(locomotive.DettachStatus(end::rear) < 0); + locomotive.Couplers[end::rear].sounds = 0; + CHECK(locomotive.Dettach(end::rear)); + CHECK(locomotive.Couplers[end::rear].Connected == nullptr); + CHECK(wagon.Couplers[end::front].Connected == nullptr); + CHECK(locomotive.Couplers[end::rear].sounds == (sound::detach | sound::attachcoupler | sound::attachbrakehose)); + CHECK(locomotive.DettachStatus(end::rear) == 0); + CHECK(locomotive.Dettach(end::rear)); +} + +TEST_CASE("couplers: a screw coupler uncoupled with the buffers apart, or with the hoses alone, or broken") { + struct state { + char const *name; + double gap; // between the buffers, the coupler stretched + int coupling; + bool damaged; + bool apart; // whether it comes apart whole + }; + for (auto const &[name, gap, couplingtype, damaged, apart] : { + state{"stretched a little", 0.1, coupling::coupler | coupling::brakehose, false, false}, + state{"stretched far", 0.3, coupling::coupler | coupling::brakehose, false, true}, + state{"hoses alone", 0.1, coupling::brakehose, false, true}, + state{"broken", 0.1, coupling::coupler | coupling::brakehose, true, true}, + }) { + CAPTURE(name); + consist train; + auto &locomotive{train.add(definition::fiz("4e_v1/4e")).mover()}; + auto &wagon{train.add(definition::fiz("3wl_v2/3wl"), 0, coupling::faux, 0.0, gap).mover()}; + REQUIRE(locomotive.Attach(end::rear, end::front, &wagon, couplingtype, true)); + if (damaged) { + SetFlag(locomotive.DamageFlag, dtrain_coupling); + } + train.step(); + CHECK((locomotive.DettachStatus(end::rear) < 0) == apart); + CHECK(locomotive.Dettach(end::rear) == apart); + // what's left when it doesn't come apart is the coupler, the hoses taken off + CHECK(locomotive.Couplers[end::rear].CouplingFlag == (apart ? coupling::faux : coupling::coupler)); + CHECK(wagon.Couplers[end::front].CouplingFlag == (apart ? coupling::faux : coupling::coupler)); + } + // couplers of different kinds don't couple, unless forced + consist train; + auto &locomotive{train.add(definition::fiz("4e_v1/4e")).mover()}; + auto &unit{train.add(definition::fiz("en57_v1/5bgb"), 0, coupling::faux).mover()}; + train.step(); + CHECK_FALSE(locomotive.Attach(end::rear, end::front, &unit, coupling::coupler)); + CHECK(locomotive.Attach(end::rear, end::front, &unit, coupling::coupler, true)); +} + +TEST_CASE("couplers: automatic couplers coupling on contact") { + // a car running slowly into another from close by (its brakes are on), the automatic couplers of both ends + // set to couple what their AutomaticFlag says; without one, the hook alone + struct meeting { + char const *name; + char const *standing; // its rear coupler met + char const *running; // its front coupler meets + int coupling; // what they couple: what both flags allow, the control cable only of one kind + }; + for (auto const &[name, standing, running, couplingtype] : { + meeting{"en57 to en57", "en57_v1/5bgb", "en57_v1/5bgaiii_led_spot", 39}, + meeting{"impuls to en57", "en57_v1/5bgb", "impuls_v1/31we-a_pr", 39 & ~coupling::control}, + meeting{"en57 without the flag", "en57_v1/5bgaiii_led_spot", "en57_v1/6ba_840", coupling::coupler}, + }) { + CAPTURE(name); + scenario scenario{std::string{"couplers/automatic/"} + name, motion(), test::track{}}; + scenario.add(standing, 0, consist::full_coupling); + scenario.add(running, 0, coupling::faux, 3.0, 0.2); + scenario.wait(10.0); + auto &first{scenario.vehicle(0)}; + auto &second{scenario.vehicle(1)}; + CHECK(first.Couplers[end::rear].CouplingFlag == couplingtype); + CHECK(second.Couplers[end::front].CouplingFlag == couplingtype); + // once coupled, not again until uncoupled; the vehicle which coupled marks it for the AI + CHECK_FALSE(first.Couplers[end::rear].AutomaticCouplingAllowed); + CHECK_FALSE(second.Couplers[end::front].AutomaticCouplingAllowed); + CHECK((TestFlag(first.AIFlag, sound::attachcoupler) || TestFlag(second.AIFlag, sound::attachcoupler))); + scenario.check(); + } +} + +TEST_CASE("couplers: a coupler broken, with its brake hose torn off") { + consist train; + auto &locomotive{train.add(definition::fiz("4e_v1/4e")).mover()}; + auto &wagon{train.add(definition::fiz("3wl_v2/3wl")).mover()}; + REQUIRE(locomotive.Couplers[end::rear].Connected == &wagon); + locomotive.damage_coupler(end::rear); + CHECK(TestFlag(locomotive.DamageFlag, dtrain_coupling)); + CHECK(locomotive.AlarmChainFlag); + CHECK(locomotive.Couplers[end::rear].CouplingFlag == coupling::faux); + CHECK(wagon.Couplers[end::front].CouplingFlag == coupling::faux); + // the front coupler of the wagon, broken on its own: no hose to tear + wagon.damage_coupler(end::front); + CHECK(TestFlag(wagon.DamageFlag, dtrain_coupling)); + CHECK_FALSE(wagon.AlarmChainFlag); + // articulated couplers don't break + consist articulated; + auto &unit{articulated.add(definition::fiz("impuls_v1/31we-a_pr")).mover()}; + articulated.add(definition::fiz("impuls_v1/31we-b_pr")); + unit.damage_coupler(end::rear); + CHECK_FALSE(TestFlag(unit.DamageFlag, dtrain_coupling)); + CHECK(unit.Couplers[end::rear].CouplingFlag != coupling::faux); +} + +TEST_CASE("couplers: collisions fast enough to derail") { + // a locomotive running into a standing wagon from behind, and a wagon running backwards into a standing + // locomotive; over 15 km/h, weighed by the ratio of the masses, the vehicles derail + struct collision { + char const *name; + char const *frontpath; + double frontspeed; + char const *back; + double backspeed; + bool frontout; + bool backout; + }; + for (auto const &[name, frontpath, frontspeed, back, backspeed, frontout, backout] : { + collision{"locomotive into a wagon at 60", "3wl_v2/3wl", 0.0, "4e_v1/4e", 60.0, true, false}, + collision{"wagon backwards into a locomotive at 60", "3wl_v2/3wl", -60.0, "4e_v1/4e", 0.0, true, false}, + collision{"locomotive into a locomotive at 25", "4e_v1/4e", 0.0, "4e_v1/4e", 25.0, true, true}, + }) { + CAPTURE(name); + scenario scenario{std::string{"couplers/collision/"} + name, motion(), test::track{}}; + scenario.add(frontpath, 0, consist::full_coupling, frontspeed); + scenario.add(back, 0, coupling::faux, backspeed, 20.0); + scenario.wait(10.0); + CHECK(TestFlag(scenario.vehicle(0).DamageFlag, dtrain_out) == frontout); + CHECK(TestFlag(scenario.vehicle(1).DamageFlag, dtrain_out) == backout); + scenario.check(); + } +} diff --git a/tests/unit/test_definition.cpp b/tests/unit/test_definition.cpp index 6b2d446c0..6b0ce22b3 100644 --- a/tests/unit/test_definition.cpp +++ b/tests/unit/test_definition.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include "doctest.h" #include "vehicle.h" +#include "drive.h" using test::definition; using test::vehicle; @@ -43,3 +44,108 @@ TEST_CASE("definition: rows of a table section reach the vehicle") { CHECK(wagon->LightsPos == 1); CHECK(wagon->Lights[end::front][1] == pc1); } + +TEST_CASE("definition: lines the loader skips or trims: comments, a hash anywhere, leading spaces, carriage returns") { + auto text{definition::load("wagon").text()}; + // a commented line and one with a hash set the mass the wagon doesn't get; the rest, indented and ended with + // a carriage return, still load + auto const param{text.find("Param")}; + REQUIRE(param != std::string::npos); + text.insert(param, "// Param: M=11111\nParam: M=22222 # a note\n "); + std::string crlf; + for (auto const c : text) { + if (c == '\n') { + crlf += '\r'; + } + crlf += c; + } + TMoverParameters wagon(0.0, "wagon", "test", 0); + REQUIRE(wagon.LoadFIZ_Text(crlf)); + CHECK(wagon.Mass == doctest::Approx(42200.0)); + CHECK(wagon.Vmax == doctest::Approx(120.0)); + CHECK(wagon.MaxBrakePress[3] == doctest::Approx(3.8)); +} + +TEST_CASE("definition: a compressor list, whose positions allow the compressor and scale its speed and limits") { + // none of the game's vehicles has one. its rows are the positions after the 0th, which changes nothing: whether + // the compressor may run (2 yes, 1 no, 0 as the switch says), its speed, and its lower and upper limits. the + // table holds whole numbers (int CompressorList[4][9]), so a factor of 0.9 is read as 0, and both limits + // drop to nothing + auto eu07{definition::fiz("4e_v1/4e") + .set("CompressorList", "Size", 3) + .set("CompressorList", "Default", 1) + .set("CompressorList", "rows", {{2, 2, 1, 1}, {1, 1, 1, 1}, {2, 1, 0.9, 0.9}})}; + vehicle real{eu07, 0.0, "test", 1}; + CHECK(real->CompressorListPosNo == 3); + CHECK(real->CompressorList[3][3] == 0); + auto driven{test::driving(real)}; + test::track const line{0.15, 0.0, test::catenary_3kv}; + REQUIRE(test::start_up(driven, line)); + CHECK(real->CompressorListPos == 1); + test::drive(driven, line, 60.0); + CHECK(real->CompressorAllow); + CHECK(real->Compressor <= real->MaxCompressor + 0.05); + CHECK(real->Compressor >= real->MinCompressor - 0.05); + // the second position doesn't let it run + CHECK(real->ChangeCompressorPreset(2)); + test::drive(driven, line, 2.0); + CHECK_FALSE(real->CompressorAllow); + CHECK_FALSE(real->CompressorFlag); + // the third lets it, under limits of nothing: with no span between them for the governor, the reservoir over + // the upper limit gets vented while the compressor runs on + CHECK(real->ChangeCompressorPreset(3)); + auto const full{real->Compressor}; + test::drive(driven, line, 10.0); + CHECK(real->CompressorAllow); + CHECK(real->CompressorFlag); + CHECK(real->Compressor < full - 1.0); + // no further than the last position + CHECK_FALSE(real->ChangeCompressorPreset(5)); + CHECK(real->CompressorListPos == 3); +} + +TEST_CASE("definition: the compressor's limits set apart for the rear cab") { + auto eu07{definition::fiz("4e_v1/4e").set("Brake", "MinCP_B", 5.0).set("Brake", "MaxCP_B", 6.0)}; + for (auto const cab : {1, -1}) { + CAPTURE(cab); + vehicle real{eu07, 0.0, "test", cab}; + auto driven{test::driving(real)}; + test::track const line{0.15, 0.0, test::catenary_3kv}; + REQUIRE(test::start_up(driven, line)); + test::drive(driven, line, 120.0); + CHECK(real->CabDependentCompressor); + CHECK(real->MaxCompressor == doctest::Approx(cab > 0 ? 8.7 : 6.0)); + CHECK(real->MinCompressor == doctest::Approx(cab > 0 ? 7.0 : 5.0)); + CHECK(real->Compressor <= real->MaxCompressor + 0.05); + CHECK(real->Compressor >= real->MinCompressor - 0.05); + } +} + +TEST_CASE("definition: the highest power of an induction motor drive by the speed (PmaxList)") { + // the drive's power limit (eimc_p_Pmax) follows the table, between its rows and, by the last two, past its end + auto eu47{definition::fiz("e186_v2/eu47").set("PmaxList", "Size", 3).set("PmaxList", "rows", {{0, 1000}, {50, 3000}, {100, 5600}})}; + auto const table{[](double Speed) { return Speed < 50.0 ? 1000.0 + 40.0 * Speed : 3000.0 + 52.0 * (Speed - 50.0); }}; + for (auto const speed : {25.0, 75.0, 150.0}) { + CAPTURE(speed); + vehicle real{eu47, speed, "test", 1}; + auto driven{test::driving(real)}; + test::track const line{0.0, 0.0, test::catenary_3kv}; + REQUIRE(test::start_up(driven, line)); + CHECK(real->eimc[eimc_p_Pmax] == doctest::Approx(table(real->Vel)).epsilon(0.01)); + } +} + +TEST_CASE("definition: the colour and the brightness of the headlights") { + // only the 214M's own file has the section, which the definitions here can't hold (see data/fiz/README.md) + vehicle railbus{definition::fiz("21xm_v1/214ma") + .set("Headlights", "LampRed", 248) + .set("Headlights", "LampGreen", 221) + .set("Headlights", "LampBlue", 111) + .set("Headlights", "DimmedMultiplier", 0.4) + .set("Headlights", "NormalMultiplier", 0.9)}; + CHECK(railbus->refR == doctest::Approx(248.0)); + CHECK(railbus->refG == doctest::Approx(221.0)); + CHECK(railbus->refB == doctest::Approx(111.0)); + CHECK(railbus->dimMultiplier == doctest::Approx(0.4)); + CHECK(railbus->normMultiplier == doctest::Approx(0.9)); +} diff --git a/tests/unit/test_diesel.cpp b/tests/unit/test_diesel.cpp new file mode 100644 index 000000000..07686dc10 --- /dev/null +++ b/tests/unit/test_diesel.cpp @@ -0,0 +1,179 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scenario.h" +#include "drive.h" + +#include "McZapkie/hamulce.h" + +// characterisation of the diesel engines besides their drive, recorded in golden traces (data/golden/diesel/): +// the heat of the engine, its oil and its water circuits, which keep a too cold or too hot engine from starting + +using test::scenario; +using namespace test::channels; + +namespace { + +std::vector heat() { + return { + {"Ts", [](auto const &M) { return M.dizel_heat.Ts; }}, + {"To", [](auto const &M) { return M.dizel_heat.To; }}, + {"Twy", [](auto const &M) { return M.dizel_heat.Twy; }}, + {"Twy2", [](auto const &M) { return M.dizel_heat.Twy2; }}, + {"PA", [](auto const &M) { return M.dizel_heat.PA ? 1.0 : 0.0; }}, + {"WaterHeater", [](auto const &M) { return M.WaterHeater.is_active ? 1.0 : 0.0; }}, + {"WaterPump", [](auto const &M) { return M.WaterPump.is_active ? 1.0 : 0.0; }}, + {"zaluzje1", [](auto const &M) { return M.dizel_heat.zaluzje1 ? 1.0 : 0.0; }}, + {"zaluzje2", [](auto const &M) { return M.dizel_heat.zaluzje2 ? 1.0 : 0.0; }}, + }; +} + +} // namespace + +TEST_CASE("diesel: an engine too cold to start, warmed by its heater") { + // at -10 degrees an engine with the lowest temperature of its water set (WaterMinTemperature) doesn't start, + // until the heater, which the driver switches on with the pumps, warms the water up; one without starts + struct engine { + char const *path; + bool starts; // cold + }; + for (auto const &[path, starts] : {engine{"su45_v2/301d", false}, engine{"sm42_v1/6d1", false}, engine{"st44_v2/m62-1151", true}}) { + CAPTURE(path); + scenario scenario{std::string{"diesel/cold/"} + path, traction() + heat(), test::track{}}; + scenario.add(path, 1); + auto &locomotive{scenario.cab()}; + locomotive.dizel_HeatSet(-10.f); + CHECK(scenario.start_up() == starts); + CHECK(locomotive.dizel_heat.PA != starts); + CHECK(locomotive.WaterHeater.is_active); + scenario.act("heating"); + scenario.wait(1200.0); + CHECK_FALSE(locomotive.dizel_heat.PA); + CHECK(locomotive.dizel_heat.Twy > 50.f); + CHECK(scenario.start_up()); + scenario.check(); + } +} + +TEST_CASE("diesel: an engine started hot, and run at full power") { + // at 88 degrees the engines with a second water circuit are over its highest temperature + // (WaterAuxMaxTemperature) and don't start while it cools; the SM42 has none, starts and gets cooled by its + // fans and shutters at full power. alone: a train's pipe, leaking under 2.75 bar while the engine + // doesn't start, gets cut off the FV4a handle of an LSt locomotive (lock_old in UpdatePipePressure) + struct engine { + char const *path; + bool starts; + }; + for (auto const &[path, starts] : {engine{"su45_v2/301d", false}, engine{"sm42_v1/6d1", true}, engine{"st44_v2/m62-1151", false}}) { + CAPTURE(path); + scenario scenario{std::string{"diesel/hot/"} + path, traction() + heat(), test::track{}}; + scenario.add(path, 1); + auto &locomotive{scenario.cab()}; + locomotive.dizel_HeatSet(88.f); + CHECK(scenario.start_up() == starts); + CHECK(locomotive.dizel_heat.PA != starts); + if (starts) { + scenario.get_going(); + scenario.full_power(300.0); + CHECK(locomotive.Vel > 50.0); + CHECK(locomotive.dizel_heat.zaluzje1); + CHECK(locomotive.dizel_heat.Twy < locomotive.dizel_heat.water.config.temp_max); + } + scenario.check(); + } +} + +TEST_CASE("diesel: the 214M railbus's torque converter, gears and retarder") { + // the controller of the railbus (EIMCtrlType 3) runs from the air brake (0, 1) through the retarder (2) and + // neutral (3) to holding (4) and raising (5) the power; the torque converter locks up at speed and the + // gearbox shifts by itself + scenario scenario{"diesel/214m", motion() + traction() + brakes(), test::track{}}; + scenario.add("21xm_v1/214ma", 1); + REQUIRE(scenario.start_up()); + auto &railbus{scenario.cab()}; + scenario.get_going(); + scenario.full_power(60.0); + CHECK(railbus.MainCtrlPos == 5); + CHECK(railbus.eimic_real == doctest::Approx(1.0)); + CHECK(railbus.hydro_TC_Lockup); + CHECK(railbus.ScndCtrlActualPos == railbus.ScndCtrlPosNo); + struct position { + double eimic; + bool retarder; + bool air; + }; + auto number{railbus.MainCtrlPos}; + for (auto const &[eimic, retarder, air] : {position{1.0, false, false}, position{0.0, false, false}, position{-1.0, true, false}, position{0.0, false, true}}) { + --number; + CAPTURE(number); + scenario.act(std::format("position {}", number)); + railbus.DecMainCtrl(1); + scenario.wait(8.0); + CHECK(railbus.eimic_real == doctest::Approx(eimic)); + CHECK((railbus.hydro_R_Fill > 0.99) == retarder); + CHECK((railbus.hydro_R_Torque > 1000.0) == retarder); + CHECK((railbus.BrakePress > 2.0) == air); + } + scenario.act("position 0"); + railbus.DecMainCtrl(1); + scenario.wait(30.0); + CHECK(railbus.Vel == doctest::Approx(0.0)); + // the gearbox shifted down as the railbus slowed + CHECK(railbus.ScndCtrlActualPos < railbus.ScndCtrlPosNo); + scenario.check(); +} + +TEST_CASE("diesel: the 214M railbus's speed control, up to the speed of a button and braking down to a lower one") { + // the second controller's first position takes the speed of the moment to hold; the buttons, the steps and + // the power limit change it. set lower than the speed, it brakes with the retarder (and under + // BrakeInterventionVel with the air); with the main controller off its top positions it stands by + scenario scenario{"diesel/214m speed control", motion() + traction() + brakes(), test::track{}}; + scenario.add("21xm_v1/214ma", 1); + REQUIRE(scenario.start_up()); + auto &railbus{scenario.cab()}; + REQUIRE(railbus.SpeedCtrl); + scenario.get_going(); + scenario.full_power(10.0); + scenario.act("speed control on"); + REQUIRE(railbus.IncScndCtrl(1)); + CHECK(railbus.SpeedCtrlUnit.IsActive); + CHECK(railbus.SpeedCtrlValue == doctest::Approx(std::round(railbus.Vel)).epsilon(0.02)); + scenario.act("button 80"); + railbus.SpeedCtrlButton(6); + CHECK(railbus.SpeedCtrlValue == doctest::Approx(80.0)); + scenario.wait(90.0); + CHECK(railbus.Vel == doctest::Approx(80.0).epsilon(0.02)); + CHECK_FALSE(railbus.SpeedCtrlUnit.Standby); + scenario.act("step down, up, power down and up"); + railbus.SpeedCtrlDec(); + CHECK(railbus.SpeedCtrlValue == doctest::Approx(75.0)); + railbus.SpeedCtrlInc(); + railbus.SpeedCtrlInc(); + CHECK(railbus.SpeedCtrlValue == doctest::Approx(85.0)); + CHECK(railbus.SpeedCtrlUnit.DesiredPower == doctest::Approx(1.0)); + CHECK(railbus.SpeedCtrlPowerDec()); + CHECK(railbus.SpeedCtrlUnit.DesiredPower == doctest::Approx(0.9)); + CHECK(railbus.SpeedCtrlPowerInc()); + CHECK(railbus.SpeedCtrlUnit.DesiredPower == doctest::Approx(1.0)); + scenario.wait(20.0); + scenario.act("button 40"); + railbus.SpeedCtrlButton(2); + scenario.wait(5.0); + // the retarder does it at this speed, the air isn't needed + CHECK(railbus.eimic_real == doctest::Approx(-1.0)); + CHECK(railbus.hydro_R_Fill == doctest::Approx(1.0)); + CHECK(railbus.BrakePress < 0.1); + CHECK_FALSE(railbus.SpeedCtrlUnit.BrakeInterventionBraking); + scenario.wait(60.0); + CHECK(railbus.Vel == doctest::Approx(40.0).epsilon(0.05)); + scenario.act("main controller back, standing by"); + railbus.DecMainCtrl(1); + railbus.DecMainCtrl(1); + railbus.DecMainCtrl(1); + scenario.wait(5.0); + CHECK(railbus.SpeedCtrlUnit.Standby); + scenario.act("speed control off"); + railbus.DecScndCtrl(2); + scenario.wait(5.0); + CHECK_FALSE(railbus.SpeedCtrlUnit.IsActive); + scenario.check(); +} diff --git a/tests/unit/test_figures.cpp b/tests/unit/test_figures.cpp new file mode 100644 index 000000000..c8b575d50 --- /dev/null +++ b/tests/unit/test_figures.cpp @@ -0,0 +1,67 @@ +#include "stdafx.h" +#include "doctest.h" + +#include + +#include "figures.h" +#include "vehicle.h" + +// the key figures of the golden traces (figures.h): the parts of a run between the events, and the comparison +// with the recorded ones, which lets the last decimal and half a percent go and tells the rest in readable lines + +using test::definition; +using test::vehicle; + +TEST_CASE("figures: a run split into parts by the events, each from the last step of the part before") { + vehicle eu07{definition::fiz("4e_v1/4e")}; + test::figures figures; + auto &mover{eu07.mover()}; + mover.V = 10.0; + mover.PipePress = 5.0; + figures.sample(0.0, mover); + figures.mark("brake"); + figures.mark("hard"); + for (int i = 1; i <= 10; ++i) { + mover.V = 10.0 - i; + mover.DistCounter += 0.005; + mover.PipePress = 3.5; + mover.BrakePress = 0.4 * i; + figures.sample(i, mover); + } + figures.mark("release"); + mover.PipePress = 4.0; + figures.sample(11.0, mover); + mover.PipePress = 5.0; + figures.sample(12.0, mover); + + auto const lines{figures.lines()}; + auto const has{[&lines](std::string const &Line) { return std::ranges::find(lines, Line) != lines.end(); }}; + CHECK(lines.front() == "[start]"); + // two events with nothing run between them make one part + CHECK(has("[brake, hard]")); + CHECK(has("duration = 10.00 s")); + CHECK(has("speed at the start = 36.0 km/h")); + CHECK(has("distance = 50.0 m")); + CHECK(has("stopped after = 10.00 s")); + CHECK(has("brake pipe lowest = 3.500 bar")); + CHECK(has("brake cylinders peak = 4.000 bar")); + CHECK(has("brake pipe back at 4.9 bar after = 2.00 s")); + // no current nor force anywhere in the run: left out of every part + CHECK_FALSE(has("motor current peak = 0 A")); +} + +TEST_CASE("figures: compared within half a percent or the last decimal, the differences told by part") { + std::vector const recorded{"[brake]", "distance = 412.3 m", "stopped after = 38.21 s", "brake cylinders peak = 3.800 bar"}; + CHECK(test::figures::compare(recorded, recorded).empty()); + CHECK(test::figures::compare(recorded, {"[brake]", "distance = 413.9 m", "stopped after = 38.22 s", "brake cylinders peak = 3.810 bar"}).empty()); + + auto const differences{test::figures::compare(recorded, {"[brake]", "distance = 398.0 m", "brake cylinders peak = 3.800 bar", "motor current peak = 10 A"})}; + REQUIRE(differences.size() == 3); + CHECK(differences[0] == "[brake] distance: 398.0 m now, 412.3 m recorded"); + CHECK(differences[1] == "[brake] stopped after: none now, 38.21 s recorded"); + CHECK(differences[2] == "[brake] motor current peak: 10 A now, none recorded"); + + auto const parts{test::figures::compare(recorded, {"[brake hard]", "distance = 412.3 m"})}; + REQUIRE(parts.size() == 1); + CHECK(parts[0] == "the parts differ from part 1: [brake hard] now, [brake] recorded"); +} diff --git a/tests/unit/test_fiz.cpp b/tests/unit/test_fiz.cpp new file mode 100644 index 000000000..f25632dee --- /dev/null +++ b/tests/unit/test_fiz.cpp @@ -0,0 +1,183 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" +#include "golden.h" +#include "real_vehicles.h" +#include "fiz.h" + +#include +#include +#include + +#include "utilities/parser.h" + +using test::definition; +using test::vehicle; + +// the real vehicle definitions in data/fiz load, as the game loads them, and give the parameters they gave +// when the traces got recorded: the loader (LoadFIZ_Parse and the parsing of every section) stays as it was + +TEST_CASE("fiz: every real vehicle loads and keeps its parameters") { + for (auto const &path : test::real_vehicles()) { + CAPTURE(path); + test::reseed_random(); + vehicle real{definition::fiz(path)}; + test::trace trace{"fiz/" + path, test::channels::parameters(), 1}; + trace.sample(0.0, real.mover()); + // what every vehicle has, the rest of its parameters are in the golden trace + CHECK(real->Mass > 0.0); + CHECK(real->TotalMass >= real->Mass); + CHECK(real->Vmax > 0.0); + CHECK(real->NAxles > 0); + CHECK(real->WheelDiameter > 0.0); + CHECK(real->NPoweredAxles <= real->NAxles); + trace.check(); + } +} + +TEST_CASE("fiz: changes made by a test reach the vehicle") { + auto const eu07{definition::fiz("4e_v1/4e")}; + CHECK(vehicle{eu07}->Mass == doctest::Approx(80000.0)); + CHECK(vehicle{definition{eu07}.set("Param", "M", 90000)}->Mass == doctest::Approx(90000.0)); + // a key the file doesn't have is added to its section + CHECK(vehicle{definition{eu07}.set("Brake", "MaxBP", 3.5)}->MaxBrakePress[3] == doctest::Approx(3.5)); + // removing a key brings its default back + CHECK(vehicle{definition{eu07}.remove("Param", "Vmax")}->Vmax != doctest::Approx(125.0)); +} + +namespace { + +// a section of the game's definition, as data/fiz keeps it +struct converted_section { + std::string name; + nlohmann::ordered_json keys = nlohmann::ordered_json::object(); + nlohmann::ordered_json rows = nlohmann::ordered_json::array(); +}; + +// the sections of the lines of the game's definition: the values of each section line, and the rows of a +// list until the line closing it. lines the loader skips, and the lines closing the lists, are left out +converted_section convert_section(test::fiz::section const &Section, std::string const &Line) { + converted_section converted{std::string{Section.name}}; + for (auto const &key : test::fiz::keys(Line)) { + if (auto const value{test::fiz::value(key, Line)}; false == value.empty()) { + converted.keys[key] = value; + } + } + return converted; +} + +nlohmann::ordered_json convert_row(std::string const &Line) { + auto row = nlohmann::ordered_json::array(); + for (auto const &word : test::fiz::words(Line)) { + row.push_back(word); + } + return row; +} + +std::vector convert(std::vector const &Lines) { + std::vector result; + auto reading_rows{false}; + for (auto const &line : Lines) { + if (auto const section{test::fiz::opened_by(line)}; section != nullptr) { + result.push_back(convert_section(*section, line)); + reading_rows = section->list != 0; + continue; + } + auto const list{result.empty() ? nullptr : test::fiz::named(result.back().name)}; + if (list == nullptr || list->end == nullptr) { + continue; + } + // an empty line closes the brake table of the Cntrl section, the other lists have lines of their own + if (std::string_view const end{list->end}; line.empty() ? end.empty() : (false == end.empty() && line.starts_with(end))) { + reading_rows = false; + continue; + } + if (reading_rows && false == line.empty()) { + result.back().rows.push_back(convert_row(line)); + } + } + return result; +} + +// the JSON of data/fiz: a section a line, a row of a list a line +std::string json_text(std::string const &Source, std::vector const &Sections) { + std::string text{"{\n \"_source\": " + nlohmann::ordered_json(Source).dump() + ",\n"}; + text += R"( "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads")"; + for (auto const §ion : Sections) { + text += ",\n " + nlohmann::ordered_json(section.name).dump() + ": {"; + auto first{true}; + for (auto const &[key, value] : section.keys.items()) { + text += (first ? "" : ", ") + nlohmann::ordered_json(key).dump() + ": " + value.dump(); + first = false; + } + if (test::fiz::named(section.name)->list != 0) { + text += std::string{first ? "" : ", "} + "\"rows\": ["; + for (std::size_t row = 0; row < section.rows.size(); ++row) { + text += (row == 0 ? "\n " : ",\n ") + section.rows[row].dump(); + } + text += section.rows.empty() ? "]" : "\n ]"; + } + text += "}"; + } + return text + "\n}\n"; +} + +std::vector split(std::string_view const List) { + std::vector result; + std::size_t start{0}; + while (start < List.size()) { + auto const end{std::min(List.find(',', start), List.size())}; + if (end > start) { + result.emplace_back(List.substr(start, end - start)); + } + start = end + 1; + } + return result; +} + +} // namespace + +// converts the game's definitions of the real vehicles to data/fiz, from the folder of the game's vehicles given +// in EU07_UNIT_TESTS_FIZ_SOURCE (dynamic/): the vehicles there already, and the ones named in EU07_UNIT_TESTS_FIZ_ADD +// (paths without pkp/ and the extension, separated by commas). the game's parser reads the file with its includes, +// the conversion keeps what the loader reads of it, and checks the loader reads the same of the converted one +TEST_CASE("fiz: conversion of the game's definitions, when asked for") { + auto const *source{std::getenv("EU07_UNIT_TESTS_FIZ_SOURCE")}; + if (source == nullptr) { + return; + } + auto paths{test::real_vehicles()}; + if (auto const *added{std::getenv("EU07_UNIT_TESTS_FIZ_ADD")}; added != nullptr) { + for (auto const &path : split(added)) { + paths.push_back(path); + } + } + auto const root{std::filesystem::path{source}}; + for (auto const &path : paths) { + CAPTURE(path); + // the vehicles of the Polish railways are in pkp/, the others under their operators + auto file{root / (path + ".fiz")}; + if (false == std::filesystem::exists(file)) { + file = root / "pkp" / (path + ".fiz"); + } + REQUIRE(std::filesystem::exists(file)); + cParser parser(file.filename().string(), cParser::buffer_FILE, file.parent_path().generic_string() + "/"); + auto const original{test::fiz::lines(parser)}; + + auto const target{std::filesystem::path{EU07_UNIT_TESTS_DATA} / "fiz" / (path + ".json")}; + std::filesystem::create_directories(target.parent_path()); + { + std::ofstream output(target, std::ios::binary); + output << json_text(std::filesystem::relative(file, root).generic_string(), convert(original)); + } + cParser converted(definition::fiz(path).text(), cParser::buffer_TEXT); + auto const expected{test::fiz::reading(original)}; + auto const actual{test::fiz::reading(test::fiz::lines(converted))}; + auto const mismatch{std::ranges::mismatch(expected, actual)}; + if (mismatch.in1 != expected.end() || mismatch.in2 != actual.end()) { + std::filesystem::remove(target); + FAIL_CHECK("the loader reads the converted definition differently, at " << (mismatch.in1 != expected.end() ? *mismatch.in1 : "the end") << " against " + << (mismatch.in2 != actual.end() ? *mismatch.in2 : "the end")); + } + } +} diff --git a/tests/unit/test_invariants.cpp b/tests/unit/test_invariants.cpp new file mode 100644 index 000000000..36c511863 --- /dev/null +++ b/tests/unit/test_invariants.cpp @@ -0,0 +1,34 @@ +#include "stdafx.h" +#include "doctest.h" +#include "golden.h" +#include "vehicle.h" + +// the invariants every golden trace checks (invariants.h) catch a vehicle in a state the simulation must +// never reach, once each, naming the time and the last event + +using test::definition; +using test::vehicle; + +TEST_CASE("invariants: a vehicle in an impossible state, reported once with its time and the last event") { + vehicle eu07{definition::fiz("4e_v1/4e")}; + test::trace trace{"invariants/self test", test::channels::brakes()}; + trace.sample(0.0, eu07.mover()); + CHECK(trace.violations().empty()); + + trace.mark("overfilled"); + eu07->BrakePress = 50.0; + trace.sample(0.01, eu07.mover()); + trace.sample(0.02, eu07.mover()); + REQUIRE(trace.violations().size() == 1); + auto const &violation{trace.violations().front()}; + CHECK(violation.find("brake cylinders") != std::string::npos); + CHECK(violation.find("at 0.010 s") != std::string::npos); + CHECK(violation.find("after \"overfilled\"") != std::string::npos); + CHECK(violation.find("BrakePress=50") != std::string::npos); + + eu07->BrakePress = 0.0; + eu07->V = std::nan(""); + trace.sample(0.03, eu07.mover()); + CHECK(trace.violations().size() > 1); + CHECK(trace.violations()[1].find("every value a number") != std::string::npos); +} diff --git a/tests/unit/test_loads.cpp b/tests/unit/test_loads.cpp new file mode 100644 index 000000000..b4a9da62a --- /dev/null +++ b/tests/unit/test_loads.cpp @@ -0,0 +1,116 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +// characterisation of the loads of vehicles besides the loading by commands (test_commands.cpp): the load +// assigned to a vehicle (AssignLoad), with the pantographs of an electric vehicle set up by the "pantstate" +// load of a scenario, and the steps of loading and unloading (LoadingDone) + +using test::definition; +using test::vehicle; + +TEST_CASE("loads: the pantographs of an electric vehicle set up by the amount of its pantstate load") { + // bit 0 raises the front pantograph, bit 1 the rear one, bit 2 turns the vehicle round in the train first + // (DoubleTr), so the bits name the pantographs the other way round + struct setup { + int amount; + bool frontraised; + bool rearraised; + }; + for (auto const &[amount, frontraised, rearraised] : {setup{0, false, false}, setup{1, true, false}, setup{2, false, true}, setup{3, true, true}, setup{5, false, true}, + setup{6, true, false}}) { + CAPTURE(amount); + vehicle et42{definition::fiz("et42_v2/112e-a")}; + CHECK(et42->AssignLoad("pantstate", static_cast(amount))); + CHECK(et42->DoubleTr == ((amount & 4) != 0 ? -1 : 1)); + CHECK(et42->Pantographs[end::front].valve.is_enabled == frontraised); + CHECK(et42->Pantographs[end::rear].valve.is_enabled == rearraised); + // the load itself stays as it was + CHECK(et42->LoadAmount == doctest::Approx(0.0)); + } + // a vehicle without a current collector takes no such load + vehicle wagon{definition::fiz("3wl_v2/3wl")}; + CHECK_FALSE(wagon->AssignLoad("pantstate", 3.f)); +} + +TEST_CASE("loads: a load assigned, of one kind at a time, up to the vehicle's capacity, and emptied") { + vehicle wagon{definition::fiz("3wl_v2/3wl")}; + auto const empty{wagon->TotalMass}; + REQUIRE(wagon->AssignLoad("coal", 10.f)); + CHECK(wagon->LoadTypeChange); + CHECK(wagon->LoadType.name == "coal"); + CHECK(wagon->TotalMass > empty); + // more of the same: the amount set, not added, and no more than the vehicle takes + wagon->LoadTypeChange = false; + REQUIRE(wagon->AssignLoad("coal", 1000.f)); + CHECK_FALSE(wagon->LoadTypeChange); + CHECK(wagon->LoadAmount == doctest::Approx(wagon->MaxLoad)); + // another kind can't be added to a load, nor can an unknown one be loaded + CHECK_FALSE(wagon->AssignLoad("ruda", 5.f)); + CHECK(wagon->LoadType.name == "coal"); + // emptied, with no name + REQUIRE(wagon->AssignLoad("")); + CHECK(wagon->LoadTypeChange); + CHECK(wagon->LoadAmount == doctest::Approx(0.0)); + CHECK(wagon->LoadType.name.empty()); + CHECK_FALSE(wagon->AssignLoad("unknown", 5.f)); + CHECK(wagon->AssignLoad("ruda", 5.f)); +} + +TEST_CASE("loads: the steps of loading and unloading") { + // most of the game's wagons may be overloaded (OverLoadFactor), the 3WL not + vehicle wagon{definition::fiz("3wl_v2/3wl").set("Load", "OverLoadFactor", 1.5)}; + REQUIRE(wagon->AssignLoad("coal", 10.f)); + auto const capacity{wagon->MaxLoad * (1.0 + wagon->OverLoadFactor)}; + + SUBCASE("no speed of the change: done at once") { + CHECK(wagon->LoadingDone(0.0, "coal")); + CHECK(wagon->LoadStatus == 4); + } + SUBCASE("no load, or another one, named: done when it was") { + wagon->LoadStatus = 1; + CHECK_FALSE(wagon->LoadingDone(1.0, "")); + CHECK_FALSE(wagon->LoadingDone(1.0, "ruda")); + wagon->LoadStatus = 4; + CHECK(wagon->LoadingDone(1.0, "")); + } + SUBCASE("unloading, step by step, what was asked or all of it, the name going with the last of it") { + wagon->CommandIn.Value1 = 4.0; // asked to unload + wagon->LastLoadChangeTime = 2.0; // at 1 a second + CHECK_FALSE(wagon->LoadingDone(-1.0, "coal")); + CHECK(wagon->LoadStatus == 2); + CHECK(wagon->LoadAmount == doctest::Approx(8.0)); + CHECK(wagon->LastLoadChangeTime == doctest::Approx(0.0)); + // nothing done since the last step + CHECK_FALSE(wagon->LoadingDone(-1.0, "coal")); + CHECK(wagon->LoadAmount == doctest::Approx(8.0)); + wagon->LastLoadChangeTime = 2.0; + CHECK(wagon->LoadingDone(-1.0, "coal")); + CHECK(wagon->LoadAmount == doctest::Approx(6.0)); + wagon->CommandIn.Value1 = 100.0; + wagon->LastLoadChangeTime = 10.0; + CHECK(wagon->LoadingDone(-1.0, "coal")); + CHECK(wagon->LoadAmount == doctest::Approx(0.0)); + CHECK(wagon->LoadType.name.empty()); + } + SUBCASE("loading, step by step, up to what was asked or what the vehicle takes, slower when overloaded") { + wagon->CommandIn.Value1 = 5.0; + wagon->LastLoadChangeTime = 2.0; + CHECK_FALSE(wagon->LoadingDone(1.0, "coal")); + CHECK(wagon->LoadStatus == 1); + CHECK(wagon->LoadAmount == doctest::Approx(12.0)); + wagon->LastLoadChangeTime = 3.0; + CHECK(wagon->LoadingDone(1.0, "coal")); + CHECK(wagon->LoadAmount == doctest::Approx(15.0)); + wagon->CommandIn.Value1 = 1000.0; + wagon->LastLoadChangeTime = 1000.0; + CHECK(wagon->LoadingDone(1.0, "coal")); + CHECK(wagon->LoadAmount == doctest::Approx(capacity)); + CHECK(capacity == doctest::Approx(2.5 * wagon->MaxLoad)); + // over the vehicle's own capacity, the change goes at half the speed + wagon->CommandIn.Value1 = 1000.0; + wagon->LastLoadChangeTime = 4.0; + CHECK_FALSE(wagon->LoadingDone(-1.0, "coal")); + CHECK(wagon->LoadAmount == doctest::Approx(capacity - 2.0)); + } +} diff --git a/tests/unit/test_queries.cpp b/tests/unit/test_queries.cpp new file mode 100644 index 000000000..d42232c2e --- /dev/null +++ b/tests/unit/test_queries.cpp @@ -0,0 +1,61 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +// characterisation of what the game's panels read of a vehicle besides the gauges recorded in the traces: the +// description of its damage on the driver's panel (driveruipanels.cpp) + +using test::definition; +using test::vehicle; + +TEST_CASE("queries: the damage of a vehicle as the driver's panel describes it") { + vehicle locomotive{definition::fiz("4e_v1/4e")}; + vehicle wagon{definition::fiz("3wl_v2/3wl")}; + // a powered locomotive and a wagon read some of the flags differently + locomotive->Power = 2000.0; + REQUIRE(wagon->Power < 0.1); + + struct damage { + int flag; + char const *locomotive; + char const *wagon; + }; + for (auto const &[flag, locomotivetext, wagontext] : { + damage{0, "OK", "OK"}, + damage{dtrain_thinwheel, "Thin wheel", "Load shifted"}, + damage{dtrain_wheelwear, "Wheel wear", "Wheel wear"}, + damage{dtrain_bearing, "Bearing damaged", "Bearing damaged"}, + damage{dtrain_coupling, "Coupler broken", "Coupler broken"}, + damage{dtrain_loaddamage, "Ventilator damaged", "Load damaged"}, + damage{dtrain_loaddestroyed, "Engine damaged", "LOAD DESTROYED"}, + damage{dtrain_axle, "Axle broken", "Axle broken"}, + damage{dtrain_out, "DERAILED", "DERAILED"}, + // the last flag of the list wins + damage{dtrain_bearing | dtrain_axle, "Axle broken", "Axle broken"}, + damage{255, "WRECKED", "WRECKED"}, + }) { + CAPTURE(flag); + locomotive->DamageFlag = flag; + wagon->DamageFlag = flag; + CHECK(locomotive->EngineDescription(0) == locomotivetext); + CHECK(wagon->EngineDescription(0) == wagontext); + } + // flat wheels count as worn ones + wagon->DamageFlag = 0; + wagon->WheelFlat = 6.0; + CHECK(wagon->EngineDescription(0) == "Wheel wear"); + // nothing else is described + CHECK(wagon->EngineDescription(1) == "Invalid qualifier"); +} + +TEST_CASE("queries: the ammeters of a vehicle without power show the current of the ones it controls") { + vehicle unpowered{definition::fiz("en57_v1/6ba_840")}; + vehicle powered{definition::fiz("en57_v1/5bgb")}; + unpowered->Attach(end::rear, end::front, &powered.mover(), coupling::control, true, false); + powered->Power = 600.0; + powered->Itot = 321.7; + unpowered->Power = 0.0; + CHECK(unpowered->ShowCurrent(0) == doctest::Approx(321.0)); // whole amperes + powered->Power = 0.0; + CHECK(unpowered->ShowCurrent(0) == doctest::Approx(0.0)); +} diff --git a/tests/unit/test_traction.cpp b/tests/unit/test_traction.cpp new file mode 100644 index 000000000..6da4ca506 --- /dev/null +++ b/tests/unit/test_traction.cpp @@ -0,0 +1,270 @@ +#include "stdafx.h" + +#include + +#include "doctest.h" +#include "vehicle.h" +#include "drive.h" +#include "golden.h" +#include "real_vehicles.h" +#include "scenario.h" + +#include "McZapkie/hamulce.h" + +using test::definition; +using test::vehicle; + +// characterisation of the drive of every real powered vehicle, driven alone from its front cab under a 3 kV +// catenary through test::drive_cycle(), in golden traces (data/golden/traction/). the cars of the electric +// units which take power or battery from the rest of their unit don't start alone: their traces record +// that, test_consist.cpp drives the whole units + +TEST_CASE("traction: every real powered vehicle started up, driven to full power and braked to a stop") { + for (auto const &path : test::real_vehicles()) { + CAPTURE(path); + test::reseed_random(); + vehicle real{definition::fiz(path), 0.0, "test", 1}; + if (false == test::is_powered(real.mover())) { + continue; + } + using namespace test::channels; + test::trace trace{"traction/" + path, motion() + traction() + air() + brakes(), 100}; + real.record(trace); + test::drive_cycle(test::driving(real), test::track{0.15, 0.0, test::catenary_3kv}, trace); + // every vehicle getting its line breaker closed runs off and is braked to a stop. the SM31 and the TEM2 + // barely move (COVERAGE.md); the cars of a unit taking their power from another car don't start alone + if (real->Mains) { + auto const crawls{path == "sm31_v1/411d" || path == "tem2_v2/tem2a"}; + CHECK((real->DistCounter > 0.2 /* km */) != crawls); + // but the road vehicle, which a minute of full service only slows down + CHECK((real->Vel < 0.5) != (path == "road/man/nl293")); + } + trace.check(); + } +} + +namespace { + +using test::scenario; +using namespace test::channels; + +void stop(scenario &Scenario) { + Scenario.notch_back(0); + Scenario.act("brake"); + Scenario.cab().BrakeLevelSet(Scenario.cab().Handle->GetPos(bh_FB)); + Scenario.wait(60.0); +} + +} // namespace + +TEST_CASE("traction: the line breaker opened by the catenary lost, and by too high a voltage") { + // the voltage relays of the series motors, and the induction motors' own limits: under MinV of the current + // collector, or over MaxV (with the overvoltage protection, OVP) and over MaxV + 200 V for the induction motors + for (auto const &[name, vehicles] : std::vector>>{ + {"eu07", {"4e_v1/4e"}}, {"et42", {"et42_v2/112e-a", "et42_v2/112e-b"}}, {"eu47", {"e186_v2/eu47"}}}) { + CAPTURE(name); + scenario scenario{"traction/catenary/" + name, motion() + traction()}; + for (auto const *path : vehicles) { + scenario.add(path, scenario.train().size() == 0 ? 1 : 0); + } + scenario.add("bdhpumn_v3/bdhpumn"); + REQUIRE(scenario.start_up()); + scenario.get_going(); + auto &controlling{scenario.driver().controlling}; + scenario.full_power(10.0); + REQUIRE(controlling.Mains); + scenario.act("catenary lost"); + scenario.line().voltage = 0.0; + scenario.wait(5.0); + CHECK_FALSE(controlling.Mains); + CHECK(controlling.Im == doctest::Approx(0.0)); + scenario.act("catenary back"); + scenario.line().voltage = test::catenary_3kv; + scenario.notch_back(0); + CHECK(scenario.start_up()); + scenario.full_power(5.0); + scenario.act("overvoltage"); + scenario.line().voltage = controlling.EnginePowerSource.CollectorParameters.MaxV + 300.0; + scenario.wait(5.0); + CHECK(controlling.Mains == (controlling.EngineType == TEngineType::ElectricSeriesMotor && false == controlling.EnginePowerSource.CollectorParameters.OVP)); + scenario.line().voltage = test::catenary_3kv; + stop(scenario); + scenario.check(); + } +} + +TEST_CASE("traction: the ET42 at the high current setting, its halves in series, and its switched electrodynamic brake") { + for (auto const high : {false, true}) { + CAPTURE(high); + scenario scenario{std::string{"traction/et42/"} + (high ? "high current" : "low current"), motion() + traction() + brakes()}; + scenario.add("et42_v2/112e-a", 1).add("et42_v2/112e-b").add("3wl_v2/3wl").add("3wl_v2/3wl").add("3wl_v2/3wl"); + REQUIRE(scenario.start_up()); + auto &et42{scenario.cab()}; + scenario.act("current setting"); + CHECK(et42.CurrentSwitch(high) == high); + // the relay takes the setting at the next step, with the controller at zero + scenario.get_going(); + CHECK((et42.Imax == et42.ImaxHi) == high); + scenario.full_power(60.0); + // at the high current setting the controller stops at the last series position: the halves' motors stay + // in series, at half the voltage each, for a heavy start, and the train crawls + CHECK((et42.RList[et42.MainCtrlPos].Bn > 1) != high); + CHECK(et42.Vel > (high ? 30.0 : 90.0)); + CHECK(et42.Vel < (high ? 50.0 : 110.0)); + scenario.act("controller to zero"); + while (et42.DecMainCtrl(2)) { + // down to zero + } + scenario.wait(2.0); + scenario.act("dynamic brake"); + CHECK(et42.DynamicBrakeSwitch(true)); + scenario.wait(5.0); + scenario.full_power(20.0); + // the controller sets the brake's strength. from the low setting's top speed, over 100 km/h, the motors' + // current goes over 300 A at the first position and the overload relay trips, leaving the train to roll; + // from the high setting's, under 40 km/h, the brake stops the train + CHECK(et42.FuseFlag != high); + CHECK((et42.Vel == doctest::Approx(0.0)) == high); + scenario.act("dynamic brake off"); + scenario.notch_back(0); + et42.DynamicBrakeSwitch(false); + stop(scenario); + scenario.check(); + } +} + +TEST_CASE("traction: the field shunting relays of the diesel-electric locomotives, at full power and notched back") { + // each kind of the automatic relays (RelayType) the game's diesel-electrics have, run with a wagon up to full + // shunting (and past their top speed, which nothing limits) and notched back through the positions where the + // relays let the shunting go: by the current (0), by the speed alone (1, which keeps it at any position), by + // the current at the full position only (41), by the position and the speed (45, 46) + struct locomotive { + char const *path; + int relaytype; + std::array shunting; // after notching back to 11, 10, 7 and 3 + }; + for (auto const &[path, relaytype, shunting] : {locomotive{"cd/754_v1/754", 0, {3, 3, 3, 0}}, locomotive{"sm31_v1/411d", 1, {2, 2, 2, 2}}, + locomotive{"sm40_v1/sm40", 41, {1, 1, 1, 1}}, locomotive{"su45_v2/301d", 45, {3, 2, 0, 0}}, + locomotive{"su46_v2/303d", 46, {4, 2, 0, 0}}}) { + CAPTURE(path); + scenario scenario{std::string{"traction/relays/"} + path, motion() + traction()}; + scenario.add(path, 1).add("bdhpumn_v3/bdhpumn"); + REQUIRE(scenario.start_up()); + auto &controlling{scenario.driver().controlling}; + CHECK(controlling.RelayType == relaytype); + scenario.get_going(); + scenario.full_power(120.0); + CHECK(controlling.ScndCtrlPos == controlling.ScndCtrlPosNo); + auto expected{shunting.begin()}; + for (auto const position : {11, 10, 7, 3}) { + CAPTURE(position); + scenario.act(std::format("notch back to {}", position)); + scenario.notch_back(position); + scenario.wait(15.0); + CHECK(controlling.ScndCtrlPos == *expected++); + } + stop(scenario); + scenario.check(); + } +} + +TEST_CASE("traction: the stages of the EW58's electrodynamic brake, set by the positions of its ep handle") { + // the FVE408's positions 1 to 5 give the ep brake's strength, whose whole part is the stage of the motors' + // brake (TUHEX): 1, 1, 2, 3, 3; over 100 km/h the motors brake at the first stage alone + scenario scenario{"traction/ew58 brake stages", motion() + traction() + brakes()}; + for (auto const *car : {"ew58_v1/ew58-sa", "ew58_v1/ew58-d", "ew58_v1/ew58-sb"}) { + scenario.add(car, scenario.train().size() == 0 ? 1 : 0, test::consist::full_coupling, 115.0); + } + REQUIRE(scenario.start_up()); + auto &ew58{scenario.cab()}; + for (auto const &[position, stage] : {std::pair{1, 1}, std::pair{3, 2}, std::pair{4, 3}, std::pair{2, 1}, std::pair{5, 3}}) { + CAPTURE(position); + scenario.act(std::format("position {}", position)); + ew58.BrakeLevelSet(position); + scenario.wait(6.0); + CHECK(ew58.TUHEX_StageActual == stage); + CHECK(ew58.DynamicBrakeFlag); + CHECK(ew58.Im < 0.0); + } + scenario.act("release"); + ew58.BrakeLevelSet(ew58.Handle->GetPos(bh_RP)); + scenario.wait(10.0); + CHECK(ew58.TUHEX_StageActual == 0); + scenario.check(); +} + +TEST_CASE("traction: the induction motor drive's speed control, its electrodynamic brake by the local brake, and at the voltage's limit") { + scenario scenario{"traction/eu47 drive", motion() + traction() + brakes()}; + scenario.add("e186_v2/eu47", 1).add("bdhpumn_v3/bdhpumn").add("bdhpumn_v3/bdhpumn"); + REQUIRE(scenario.start_up()); + auto &eu47{scenario.cab()}; + scenario.get_going(); + // the second controller of the EU47 sets the speed to hold (SpeedCtrlType=Time): position 2 keeps the speed + // set, 3 raises it by 10 km/h every SpeedCtrlDelay, 4 sets the top speed, 1 lowers it, 0 drops it + REQUIRE(eu47.ScndCtrlPosNo == 4); + REQUIRE(eu47.SpeedCtrlTypeTime); + scenario.full_power(0.0); + scenario.act("speed up"); + for (int i = 0; i < 3; ++i) { + eu47.IncScndCtrl(1); + } + scenario.wait(3.0); + scenario.act("hold"); + eu47.DecScndCtrl(1); + scenario.wait(1.0); + CHECK(eu47.SpeedCtrlValue == doctest::Approx(60.0)); + scenario.wait(60.0); + CHECK(eu47.Vel == doctest::Approx(60.0).epsilon(0.01)); + scenario.act("slow down"); + eu47.DecScndCtrl(1); + scenario.wait(1.5); + eu47.IncScndCtrl(1); + scenario.wait(1.0); + CHECK(eu47.SpeedCtrlValue == doctest::Approx(20.0)); + // a lower speed set only takes the power off, the train rolls on + scenario.wait(40.0); + CHECK(eu47.Vel > 50.0); + scenario.act("top speed"); + eu47.IncScndCtrl(2); + scenario.wait(5.0); + CHECK(eu47.SpeedCtrlValue == doctest::Approx(eu47.Vmax)); + scenario.act("speed control off"); + while (eu47.DecScndCtrl(1)) { + // down to zero + } + scenario.notch_back(0); + scenario.wait(2.0); + CHECK(eu47.SpeedCtrlValue == doctest::Approx(0.0)); + // the local brake sets the flag of the electrodynamic brake, which keeps the train brake's air out of the + // cylinders, but the motors' brake follows the train brake alone: the local brake's air does the braking + scenario.act("local brake"); + eu47.IncLocalBrakeLevel(LocalBrakePosNo); + scenario.wait(5.0); + CHECK(eu47.DynamicBrakeFlag); + CHECK(eu47.Im == doctest::Approx(0.0)); + CHECK(eu47.BrakePress > 2.0); + scenario.act("local brake off"); + eu47.DecLocalBrakeLevel(LocalBrakePosNo); + scenario.wait(10.0); + // braking back into the catenary: at its usual voltage, and at its highest, over the drive's MaxV, where + // the drive lets less of the current go back to it (Vadd) + for (auto const voltage : {test::catenary_3kv, eu47.EnginePowerSource.CollectorParameters.MaxV + 100.0}) { + CAPTURE(voltage); + scenario.line().voltage = voltage; + scenario.act(std::format("service brake at {} V", static_cast(voltage))); + eu47.BrakeLevelSet(eu47.Handle->GetPos(bh_FB)); + scenario.wait(6.0); + CHECK(eu47.DynamicBrakeFlag); + CHECK(eu47.Im < -100.0); + CHECK((eu47.Vadd > 1.0) == (voltage > eu47.EnginePowerSource.CollectorParameters.MaxV)); + eu47.BrakeLevelSet(eu47.Handle->GetPos(bh_RP)); + scenario.wait(6.0); + } + scenario.line().voltage = test::catenary_3kv; + scenario.act("line breaker off"); + eu47.MainSwitch(false); + scenario.wait(5.0); + CHECK(eu47.Im == doctest::Approx(0.0)); + stop(scenario); + scenario.check(); +} diff --git a/tests/unit/vehicle.cpp b/tests/unit/vehicle.cpp index 914a31079..e38999f7d 100644 --- a/tests/unit/vehicle.cpp +++ b/tests/unit/vehicle.cpp @@ -1,60 +1,26 @@ #include "stdafx.h" #include "vehicle.h" +#include "golden.h" +#include "real_vehicles.h" +#include "fiz.h" #include "vehicle/DynObj.h" +#include #include #include +#include "utilities/Globals.h" + namespace test { namespace { -struct section_format { - char separator; // the game's definitions end section names with either '.' or ':' - char const *table_end{nullptr}; // the line closing the rows of a table section -}; - -// the sections TMoverParameters::LoadFIZ() reads, see its issection() calls. a section missing here -// would be silently skipped by the loader, and the test would run on the defaults instead -std::map> const sections{ - {"Param", {'.'}}, - {"Load", {':'}}, - {"Dimensions", {':'}}, - {"Wheels", {':'}}, - {"Brake", {':'}}, - {"Doors", {':'}}, - {"BuffCoupl", {'.'}}, - {"BuffCoupl1", {'.'}}, - {"BuffCoupl2", {'.'}}, - {"TurboPos", {':'}}, - {"Cntrl", {'.'}}, - {"Blending", {':'}}, - {"DCEMUED", {':'}}, - {"SpringBrake", {':'}}, - {"Light", {':'}}, - {"Headlights", {':'}}, - {"Clima", {':'}}, - {"Power", {':'}}, - {"SpeedControl", {':'}}, - {"Engine", {':'}}, - {"Switches", {':'}}, - {"Security", {':'}}, - {"Circuit", {':'}}, - {"AI", {':'}}, - {"LightsList", {':', "endL"}}, - {"DimmerList", {':', "endDimmerList"}}, - {"WiperList", {':', "endwl"}}, - {"CompressorList", {':', "endCL"}}, - {"RList", {':', "END-RL"}}, - {"MotorParamTable0", {':', "END-MPT"}}, -}; - -section_format const &format_of(std::string const &Section) { - auto const lookup{sections.find(Section)}; - if (lookup == sections.end()) { - throw definition_error("unknown vehicle definition section " + Section + ", see tests/unit/vehicle.cpp"); +fiz::section const &format_of(std::string const &Section) { + auto const section{fiz::named(Section)}; + if (section == nullptr) { + throw definition_error("vehicle definition section " + Section + " isn't one the loader reads, see tests/unit/fiz.cpp"); } - return lookup->second; + return *section; } std::string value_text(nlohmann::ordered_json const &Value) { @@ -67,9 +33,9 @@ std::string value_text(nlohmann::ordered_json const &Value) { return Value.dump(); } -// the rows of a table section, each a list of values, and the line closing them -std::string table_text(std::string const &Section, section_format const &Format, nlohmann::ordered_json const &Rows) { - if (Format.table_end == nullptr) { +// the rows of a list section, each a list of values, and the line closing them +std::string table_text(std::string const &Section, fiz::section const &Format, nlohmann::ordered_json const &Rows) { + if (Format.end == nullptr) { throw definition_error("vehicle definition section " + Section + " has no rows"); } std::string text; @@ -79,34 +45,47 @@ std::string table_text(std::string const &Section, section_format const &Format, } text += "\n"; } - return text + Format.table_end + "\n"; + return text + Format.end + "\n"; } -} // namespace - -definition definition::load(std::string const &Name) { - auto const path{std::string{EU07_UNIT_TESTS_DATA} + "/" + Name + ".json"}; - std::ifstream file(path); +nlohmann::ordered_json sections_of(std::string const &Path) { + std::ifstream file(Path); if (false == file.is_open()) { - throw definition_error("no test vehicle definition " + path); + throw definition_error("no test vehicle definition " + Path); } - definition result; // not brace-initialised: json{other} makes an array of other with GCC and Clang (the initializer list constructor wins) auto const json = nlohmann::ordered_json::parse(file, nullptr, true, true); if (false == json.is_object()) { - throw definition_error("test vehicle definition " + path + " isn't a JSON object"); + throw definition_error("test vehicle definition " + Path + " isn't a JSON object"); } + auto result = nlohmann::ordered_json::object(); for (auto const &[section, keys] : json.items()) { // keys starting with _ are comments if (section.starts_with('_')) { continue; } format_of(section); - result.m_sections[section] = keys; + result[section] = keys; } return result; } +} // namespace + +definition definition::load(std::string const &Name) { + definition result; + result.m_sections = sections_of(std::string{EU07_UNIT_TESTS_DATA} + "/" + Name + ".json"); + return result; +} + +definition definition::fiz(std::string const &Path) { + definition result; + result.m_sections = sections_of(std::string{EU07_UNIT_TESTS_DATA} + "/fiz/" + Path + ".json"); + auto const slash{Path.rfind('/')}; + result.m_typename = slash == std::string::npos ? Path : Path.substr(slash + 1); + return result; +} + definition &definition::set(std::string const &Section, std::string const &Key, nlohmann::ordered_json const &Value) { format_of(Section); m_sections[Section][Key] = Value; @@ -143,52 +122,145 @@ std::string definition::text() const { return text; } -vehicle::vehicle(definition const &Definition, double Velocity, std::string const &Name) { - // the same steps as TDynamicObject::Init() - m_mover = std::make_unique(Velocity, Name, Name, 0); +vehicle::vehicle(definition const &Definition, double Velocity, std::string const &Name, int Cab) : m_cab(Cab) { + // the same steps as TDynamicObject::Init(), for a vehicle set the usual way round (iDirection 1) + auto const typename_{Definition.type_name().empty() ? Name : Definition.type_name()}; + m_mover = std::make_unique(Velocity, typename_, Name, Cab); if (false == m_mover->LoadFIZ_Text(Definition.text())) { throw definition_error("the test vehicle definition didn't load"); } m_mover->AssignLoad("", 0.f); m_mover->ComputeMass(); auto const driveractive{Velocity != 0.0}; - if (false == m_mover->CheckLocomotiveParameters(driveractive, Velocity >= 0.0 ? 1 : -1)) { + if (false == m_mover->CheckLocomotiveParameters(driveractive, (Velocity > 0 ? 1 : -1) * Cab)) { throw definition_error("parameters mismatch in the test vehicle definition"); } m_mover->MainCtrlPos = m_mover->MainCtrlNoPowerPos(); m_mover->LocalBrakePosA = 0.0; - m_mover->BrakeCtrlPos = static_cast(std::floor(m_mover->Handle->GetPos(driveractive ? bh_RP : bh_NP))); + if (driveractive) { + m_mover->BrakeCtrlPos = static_cast(std::floor(m_mover->Handle->GetPos(Cab == 0 ? bh_NP : bh_RP))); + // a driver keeps a vehicle on standby on its independent brake + if (Cab != 0 && m_mover->LocalBrake != TLocalBrake::ManualBrake && Velocity < 1.0) { + m_mover->IncLocalBrakeLevel(LocalBrakePosNo); + if (m_mover->EIMCtrlEmergency) { + m_mover->DecLocalBrakeLevel(1); + } + } + } + else { + m_mover->BrakeCtrlPos = static_cast(std::floor(m_mover->Handle->GetPos(bh_NP))); + } m_mover->BrakeLevelSet(m_mover->BrakeCtrlPos); } void vehicle::step(track const &Track, double Dt) { - compute_forces(Dt); - compute_movement(Track, Dt, TLocation{0.0, 0.0, 0.0}); -} - -void vehicle::compute_forces(double Dt) { - if (m_pipepressure >= 0.0) { - m_mover->Pipe->CreatePress(m_pipepressure); + begin_step(); + TLocation const location{0.0, 0.0, 0.0}; + for (int iteration = 1; iteration < Track.iterations; ++iteration) { + forces(Dt); + fast_move(Track, Dt, location); } - m_mover->ComputeTotalForce(Dt); + forces(Dt); + move(Track, Dt, location, m_mover->PipePress); } -double vehicle::compute_movement(track const &Track, double Dt, TLocation const &Location) { +namespace { + +TTrackShape shape_of(track const &Track) { TTrackShape shape; shape.Len = 1000.0; shape.dHtrack = Track.gradient; + return shape; +} + +TTrackParam parameters_of(track const &Track, TMoverParameters const &Mover) { TTrackParam param; - param.Width = 1.435; + param.Width = Track.width; param.friction = Track.friction; - param.CategoryFlag = 1; - TTractionParam const traction; + // the vehicle's own kind of way: a road vehicle on rails derails as soon as it moves + if (Track.category != 0) { + param.CategoryFlag = Track.category; + } + else { + param.CategoryFlag = Mover.CategoryFlag != 0 ? Mover.CategoryFlag : 1; + } + return param; +} + +} // namespace + +double vehicle::fast_move(track const &Track, double Dt, TLocation const &Location) { + // TDynamicObject::FastUpdate(), the iterations of a frame before the last one + auto param{parameters_of(Track, *m_mover)}; + return m_mover->FastComputeMovement(Dt, shape_of(Track), param, Location, TRotation{0.0, 0.0, 0.0}); +} + +void vehicle::begin_step() { + // the conditions the test keeps + if (m_pipepressure >= 0.0) { + m_mover->Pipe->CreatePress(m_pipepressure); + } + if (m_reservoirpressure >= 0.0 && m_mover->VeselVolume > 0.0) { + m_mover->CompressedVolume = m_reservoirpressure * m_mover->VeselVolume; + } + // vehicle_table::update(), before the forces + m_mover->ComputeConstans(); +} + +void vehicle::forces(double Dt) { + // TDynamicObject::UpdateForce() + m_mover->ComputeTotalForce(Dt); +} + +double vehicle::move(track const &Track, double Dt, TLocation const &Location, double Trainpipe) { + auto const shape{shape_of(Track)}; + auto const param{parameters_of(Track, *m_mover)}; TRotation const rotation{0.0, 0.0, 0.0}; - return m_mover->ComputeMovement(Dt, Dt, shape, param, traction, Location, rotation); + + // what TDynamicObject::Update() does before the movement. a raised pantograph touches the wire at once, + // the game animates its rise + auto &mover{*m_mover}; + mover.PantFrontVolt = mover.Pantographs[end::front].is_active ? Track.voltage : 0.0; + mover.PantRearVolt = mover.Pantographs[end::rear].is_active ? Track.voltage : 0.0; + TTractionParam traction; + traction.TractionVoltage = + mover.EnginePowerSource.SourceType == TPowerSource::CurrentCollector ? std::max(std::abs(mover.PantRearVolt), std::abs(mover.PantFrontVolt)) : 0.95 * mover.EnginePowerSource.MaxVoltage; + traction.TractionFreq = 0; + traction.TractionMaxCurrent = 7500; + traction.TractionResistivity = 0.3; + mover.PantographVoltage = traction.TractionVoltage; + if (m_cab != 0) { + using enum TEngineType; + // the driver's own vehicle, which gets the average pressure of the train's brake pipe + mover.EqvtPipePress = Trainpipe; + if (auto const diesel{mover.EngineType == DieselEngine || mover.EngineType == DieselElectric}; diesel && mover.EIMCtrlType > 0) { + mover.CheckEIMIC(Dt); + if (mover.SpeedCtrl) { + mover.CheckSpeedCtrl(Dt); + } + mover.eimic_real = std::min(mover.eimic, mover.eimicSpeedCtrl); + } + if (mover.EngineType == ElectricInductionMotor) { + // the share of the electric and pneumatic brake over the train (TDynamicObject's MED) isn't here + mover.CheckEIMIC(Dt); + mover.CheckSpeedCtrl(Dt); + mover.eimic_real = std::min(mover.eimic, mover.eimicSpeedCtrl); + } + } + + // the whole frame's time, of all its iterations + auto const frame{Dt * Track.iterations}; + auto const distance{m_mover->ComputeMovement(Dt, frame, shape, param, traction, Location, rotation)}; + m_time += frame; + if (m_trace != nullptr) { + m_trace->sample(m_time, *m_mover); + } + return distance; } void vehicle::run(double Seconds, track const &Track, double Dt) { - // whole steps: summing up Dt would make the step count depend on the rounding - auto const steps{std::lround(Seconds / Dt)}; + // whole frames: summing up Dt would make their count depend on the rounding + auto const steps{std::lround(Seconds / (Dt * Track.iterations))}; for (long i = 0; i < steps; ++i) { step(Track, Dt); } @@ -253,10 +325,12 @@ void pair::find_neighbours() { void pair::step(track const &Track, double Dt) { find_neighbours(); for (auto const &placed : m_vehicles) { - placed.owner->compute_forces(Dt); + placed.owner->begin_step(); + placed.owner->forces(Dt); } for (auto &placed : m_vehicles) { - placed.position += placed.heading * placed.owner->compute_movement(Track, Dt, TLocation{placed.position, 0.0, 0.0}); + auto const &mover{placed.owner->mover()}; + placed.position += placed.heading * placed.owner->move(Track, Dt, TLocation{placed.position, 0.0, 0.0}, mover.PipePress); } } @@ -278,4 +352,24 @@ int pair::coupling() const { return m_vehicles[0].owner->mover().Couplers[m_vehicles[0].end].CouplingFlag; } +void reseed_random() { + Global.random_engine.seed(1); + Global.local_random_engine.seed(1); +} + +std::vector const &real_vehicles() { + static std::vector const paths{[] { + std::vector result; + auto const root{std::filesystem::path{EU07_UNIT_TESTS_DATA} / "fiz"}; + for (auto const &entry : std::filesystem::recursive_directory_iterator(root)) { + if (entry.is_regular_file() && entry.path().extension() == ".json") { + result.push_back(std::filesystem::relative(entry.path(), root).replace_extension().generic_string()); + } + } + std::ranges::sort(result); + return result; + }()}; + return paths; +} + } // namespace test diff --git a/tests/unit/vehicle.h b/tests/unit/vehicle.h index 616e75d11..e14f4e464 100644 --- a/tests/unit/vehicle.h +++ b/tests/unit/vehicle.h @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -12,6 +13,10 @@ namespace test { +// reseeds the random number engines of the simulation, as before every test case. a test making several +// vehicles in a loop reseeds before each, so each one is the same whatever comes before it +void reseed_random(); + // thrown for a test vehicle definition which can't be used: missing, malformed, or not taken by the loader class definition_error : public std::runtime_error { public: @@ -21,34 +26,59 @@ public: // definition of a test vehicle: one of the JSON files in tests/unit/data, with the sections and keys // of the game's vehicle definitions, changed by the test where it needs to: // auto heavy = definition::load("wagon").set("Param", "M", 60000).set("Brake", "BrakeValve", "ESt3"); +// or one of the game's own vehicles, converted with the files it includes to tests/unit/data/fiz (see the +// README there), which reads to the game's vehicle loader the same as the game's file: +// auto eu07 = definition::fiz("4e_v1/4e").set("Brake", "MaxBP", 3.8); +// a section of a list keeps its rows under "rows", each a list of values class definition { public: // tests/unit/data/.json static definition load(std::string const &Name); + // tests/unit/data/fiz/.json + static definition fiz(std::string const &Path); // sets the key of the section, adding either one when it's missing definition &set(std::string const &Section, std::string const &Key, nlohmann::ordered_json const &Value); - // drops the whole section, or one key of it + // drops the whole section, with its rows, or one key of it definition &remove(std::string const &Section); definition &remove(std::string const &Section, std::string const &Key); - // the definition in the form the game's vehicle loader reads + // the definition in the form the game's vehicle loader reads, the sections in their order std::string text() const; + // the vehicle type of a real definition, its file name in the game; empty for the others + std::string const &type_name() const { return m_typename; } + private: - nlohmann::ordered_json m_sections; + nlohmann::ordered_json m_sections = nlohmann::ordered_json::object(); + std::string m_typename; }; -// conditions the vehicle runs in: straight track of the given friction and gradient (rise/run) +// conditions the vehicle runs in: straight track of the given friction and gradient (rise/run), under a +// catenary of the given voltage, or without one. the track is of the vehicle's own category (rail, road, +// water), unless the test gives one, and of the standard gauge wide, unless the test gives a road's width. iterations is how many steps of the physics the game makes in a frame: +// the last one moves the vehicle fully, the ones before it (FastComputeMovement) only roughly; the game +// makes as many as it takes to keep them at 0.01 s, in fives with "full physics" set struct track { double friction{0.15}; double gradient{0.0}; + double voltage{0.0}; + int category{0}; + int iterations{1}; + double width{1.435}; }; -// one vehicle created the way TDynamicObject::Init() creates it, from the given definition +// the catenary of the Polish railways [V] +inline constexpr double catenary_3kv{3000.0}; + +class trace; + +// one vehicle created the way TDynamicObject::Init() creates it, from the given definition. Cab is the +// cab with a driver in it, 1 (front) or -1 (rear), as for a "headdriver" or "reardriver" in a scenario; +// 0 for a vehicle nobody drives. the vehicle steps the way TDynamicObject::Update() steps a lone one class vehicle { public: - explicit vehicle(definition const &Definition, double Velocity = 0.0, std::string const &Name = "test"); + explicit vehicle(definition const &Definition, double Velocity = 0.0, std::string const &Name = "test", int Cab = 0); TMoverParameters &mover() { return *m_mover; } TMoverParameters *operator->() { return m_mover.get(); } @@ -56,25 +86,35 @@ public: // keeps the brake pipe at the given pressure [bar], as a locomotive coupled to the vehicle would; // a negative value leaves the pipe alone void hold_brake_pipe(double Pressure) { m_pipepressure = Pressure; } + // keeps the main reservoir at the given pressure [bar], as a running compressor or a coupled vehicle + // feeding it would; a negative value leaves it alone + void hold_main_reservoir(double Pressure) { m_reservoirpressure = Pressure; } - // advances the physics by one step of Dt, the way the simulation loop does + // advances the physics by one frame of Track.iterations steps of Dt, the way the simulation loop does void step(track const &Track = {}, double Dt = 0.01); - // advances the physics by Seconds in steps of Dt + // advances the physics by Seconds in frames of steps of Dt void run(double Seconds, track const &Track = {}, double Dt = 0.01); + // from now on records the vehicle in the trace after every step, see golden.h + void record(trace &Trace) { m_trace = &Trace; } + + // the parts of a step, which a consist makes for all its vehicles in turn, as vehicle_table::update() + // does: the test's conditions and the constants, the forces, the movement (returning the distance) + void begin_step(); + void forces(double Dt); + double fast_move(track const &Track, double Dt, TLocation const &Location); + double move(track const &Track, double Dt, TLocation const &Location, double Trainpipe); + // vehicle speed in kilometres per hour, negative when moving backwards double speed() const { return m_mover->V * 3.6; } private: - friend class pair; - - // the two halves of a step: the forces, taken by all the vehicles in the scene first, then the movement, - // the vehicle placed at Location. returns the distance travelled [m], positive forwards - void compute_forces(double Dt); - double compute_movement(track const &Track, double Dt, TLocation const &Location); - std::unique_ptr m_mover; + int m_cab{0}; double m_pipepressure{-1.0}; + double m_reservoirpressure{-1.0}; + trace *m_trace{nullptr}; + double m_time{0.0}; // of the simulation, since the vehicle got created }; // two vehicles on a straight track, the FirstEnd of the first one facing the SecondEnd of the second one Gap metres From 4a256477e0d5b8be8685d7e9944af530b6584136 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Wed, 28 Oct 2026 03:33:33 +0100 Subject: [PATCH 48/90] Train: Handle 3-way light switches in headlighttoggle actions This lets them be fully operated with mouse and VR pointers. --- vehicle/Train.cpp | 76 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 201b8972f..e2f8fa86e 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -5234,15 +5234,37 @@ void TTrain::OnCommand_headlighttoggleleft(TTrain *Train, command_data const &Co if (Command.action == GLFW_PRESS) { // only reacting to press, so the switch doesn't flip back and forth if key is held down - if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_left) == 0) + + if (Train->ggLeftEndLightButton.SubModel != nullptr || Train->ggLeftLightButton.SubModel == nullptr) { - // turn on - OnCommand_headlightenableleft(Train, Command); - } - else - { - // turn off - OnCommand_headlightdisableleft(Train, Command); + if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_left) == 0) + { + // turn on + OnCommand_headlightenableleft(Train, Command); + } + else + { + // turn off + OnCommand_headlightdisableleft(Train, Command); + } + } else { + // if the light is controlled by 3-way switch, handle both headlight and redmarker + if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_left) == 0 && + (Train->mvOccupied->iLights[vehicleend] & light::redmarker_left) == 0) + { + // turn on + OnCommand_headlightenableleft(Train, Command); + } + else if (Train->mvOccupied->iLights[vehicleend] & light::headlight_left) + { + // switch to red + OnCommand_redmarkerenableleft(Train, Command); + } + else + { + // disable + OnCommand_redmarkerdisableleft(Train, Command); + } } } } @@ -5317,15 +5339,37 @@ void TTrain::OnCommand_headlighttoggleright(TTrain *Train, command_data const &C if (Command.action == GLFW_PRESS) { // only reacting to press, so the switch doesn't flip back and forth if key is held down - if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_right) == 0) + + if (Train->ggRightEndLightButton.SubModel != nullptr || Train->ggRightLightButton.SubModel == nullptr) { - // turn on - OnCommand_headlightenableright(Train, Command); - } - else - { - // turn off - OnCommand_headlightdisableright(Train, Command); + if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_right) == 0) + { + // turn on + OnCommand_headlightenableright(Train, Command); + } + else + { + // turn off + OnCommand_headlightdisableright(Train, Command); + } + } else { + // if the light is controlled by 3-way switch, handle both headlight and redmarker + if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_right) == 0 && + (Train->mvOccupied->iLights[vehicleend] & light::redmarker_right) == 0) + { + // turn on + OnCommand_headlightenableright(Train, Command); + } + else if (Train->mvOccupied->iLights[vehicleend] & light::headlight_right) + { + // switch to red + OnCommand_redmarkerenableright(Train, Command); + } + else + { + // disable + OnCommand_redmarkerdisableright(Train, Command); + } } } } From 7e426192b7962f0e2d0c1610e4639fe746b67fd9 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 10:05:10 +0100 Subject: [PATCH 49/90] fix(cab): a diesel with only the starter button stops its engine with the main switch off command (eu07/maszyna#427) The SM03 had no way to stop its engine. Its fuel pump starts on its own, so the fuel pump switch is ignored; the engine runs the converter feeding the low voltage circuit, so the battery switch doesn't starve the fuel pump either; and its cab has only the starter button, for which TTrain::OnCommand_linebreakeropen() kept a legacy branch which animated the button and returned without opening the circuit. MainSwitch(false) itself stops the engine fine. The command now opens the circuit on diesels whose cab has only the starter button (TMoverParameters::MainCircuitOpensWithStarterOnly()), without animating the button, which held down would start the engine again. Other vehicles with only the button closing the circuit keep ignoring the command. Tests: tests/unit/test_engine_shutdown.cpp, on a shunter made after the SM03. --- McZapkie/MOVER.h | 7 +++ tests/unit/data/diesel_shunter.json | 53 +++++++++++++++++++++ tests/unit/test_engine_shutdown.cpp | 74 +++++++++++++++++++++++++++++ vehicle/Train.cpp | 3 +- 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 tests/unit/data/diesel_shunter.json create mode 100644 tests/unit/test_engine_shutdown.cpp diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 199c65a0c..79a73f1a0 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2466,6 +2466,13 @@ class TMoverParameters { return false == StLinFlag && MainCircuitLampsEnabled(); } + // whether the cab command opening the main circuit opens it where the only cab switch of the circuit is the button + // closing it: on a diesel that's the starter button, and the command is the one way to stop an engine whose fuel pump + // starts on its own (eu07/maszyna#427). the other vehicles with such a button keep ignoring the command + bool MainCircuitOpensWithStarterOnly() const + { + return EngineType == TEngineType::DieselEngine || EngineType == TEngineType::DieselElectric; + } // whether the pumps a joint oil and fuel pump switch works, those not started automatically, are all switched on bool OilFuelPumpsEnabled() const { diff --git a/tests/unit/data/diesel_shunter.json b/tests/unit/data/diesel_shunter.json new file mode 100644 index 000000000..3cbcdb992 --- /dev/null +++ b/tests/unit/data/diesel_shunter.json @@ -0,0 +1,53 @@ +{ + "_comment": "Diesel shunter with a mechanical gearbox, made after the SM03 (Ls150): the fuel pump starts on its own with the engine, a converter run by the engine feeds the low voltage circuit, the only cab control of the engine is the starter button.", + "Param": { "Category": "train", "M": 23200, "Vmax": 25.68, "PWR": 111.855, "SandCap": 200 }, + "Dimensions": { "L": 6.94, "H": 3.21, "W": 2.97, "Cx": 0.3 }, + "Wheels": { "D": 0.94, "Tw": 1.435, "AIM": 250, "Axle": "B", "Ad": 2.5, "Bd": 2.5, "Rmin": 50, "BearingType": "Roll" }, + "Brake": { + "BrakeValve": "K", "NBpA": 2, "MBF": 85.0, "MaxBP": 3.5, "LocMaxBP": 5.0, "BCN": 1, "BCR": 0.114, "BCD": 0.12, "BCM": 10.0, + "HiPP": 5, "LoPP": 3.5, "Vv": 0.3, "MinCP": 7.0, "MaxCP": 8.0, "CompressorSpeed": 0.0125, "CompressorPower": "Main", + "BCS": 0.8, "BSA": 0, "BM": "P10-Bg", "BVV": 40, "BRE": 0.9 + }, + "BuffCoupl": { "CType": "Screw", "kB": 9200, "DmaxB": 0.15, "FmaxB": 740, "kC": 4800, "DmaxC": 0.15, "FmaxC": 780, "beta": 0.58, "AllowedFlag": 3 }, + "Cntrl": { + "BrakeSystem": "Pneumatic", "BDelay1": 15, "BDelay2": 4, "ASB": "Manual", "LocalBrake": "PneumaticBrake", "ManualBrake": "Yes", + "DynamicBrake": "Passive", "MCPN": 9, "SCPN": 7, "AutoRelay": "No", "IniCDelay": 0.5, "SCDelay": 0.1, "BrakeDelays": "P", + "BrakeHandle": "Knorr", "LocBrakeHandle": "Knorr", "FuelStart": "Automatic", "ConverterStart": "Automatic" + }, + "Light": { "Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": 24, "LMaxCurrent": 100 }, + "Engine": { + "EngineType": "DieselEngine", "Trans": "1:1", "nmin": 900, "nmax": 1500, "nmax_cutoff": 1550, "AIM": 100, + "OilMinPressure": 0.343, "OilMaxPressure": 0.686, "WaterCoolingTemperature": 80, "WaterShutters": "yes", "WaterAuxCircuit": "no", + "HeatKFO": 160, "HeatKFO2": 180, "HeatKFS": 160, "HeatKFE": 7, "HeatKW": 2, "HeatKV": 0.3, "IsTC": "No", "EUS": 0.35, "EDS": 1.0 + }, + "_gears": "rows: gear, ratio, engagement speed, top speed; the even ones neutral", + "MotorParamTable": { + "minVelfullengage": 3.8, "engageDia": 1, "engageMaxForce": 800, "engagefriction": 1, + "rows": [ + [0, 0, 0, -1], + [1, 46.88, -1, 5.73], + [2, 0, 0, -1], + [3, 28.65, 2.9, 9.42], + [4, 0, 0, -1], + [5, 17.12, 4.7, 15.69], + [6, 0, 0, -1], + [7, 10.46, 10.6, 100] + ] + }, + "_throttle": "rows: position, fill, speed governor mode; position 0 idles", + "DList": { + "Size": 9, "Mmax": 784.5, "nMmax": 15, "Mnmax": 686.4, "nmax": 25, "nominalfill": 1, "Mstand": 200.0, + "rows": [ + [0, 0.1, 0], + [1, 0.2, 2], + [2, 0.3, 2], + [3, 0.4, 2], + [4, 0.5, 2], + [5, 0.6, 2], + [6, 0.7, 2], + [7, 0.8, 2], + [8, 0.9, 2], + [9, 1.0, 2] + ] + } +} diff --git a/tests/unit/test_engine_shutdown.cpp b/tests/unit/test_engine_shutdown.cpp new file mode 100644 index 000000000..9230330f4 --- /dev/null +++ b/tests/unit/test_engine_shutdown.cpp @@ -0,0 +1,74 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +// stopping the engine of a diesel shunter like the SM03 (eu07/maszyna#427): its fuel pump starts on its own with the +// engine and the engine runs the converter which feeds the low voltage circuit, so neither the fuel pump switch nor the +// battery switch stop it. the cab has only the starter button, and the command opening the main circuit has to stop it + +namespace { + +// the shunter with its engine started from the cab and idling +test::vehicle running_shunter() { + test::vehicle shunter{test::definition::load("diesel_shunter")}; + shunter->CabOccupied = 1; + shunter->CabActivisation(); + shunter->BatterySwitch(true); + shunter->OilPumpSwitch(true); + shunter.run(20.0); // the oil pressure builds up + shunter->MainSwitch(true); + shunter.run(15.0); + return shunter; +} + +} // namespace + +TEST_CASE("engine shutdown: the shunter starts with the starter button") { + auto shunter{running_shunter()}; + REQUIRE(shunter->EngineType == TEngineType::DieselEngine); + REQUIRE(shunter->FuelPump.start_type == start_t::automatic); + CHECK(shunter->Mains); + CHECK(shunter->FuelPump.is_active); + CHECK(shunter->enrot > 0.9 * shunter->dizel_nmin); +} + +TEST_CASE("engine shutdown: neither the fuel pump switch nor the battery switch stop the engine") { + auto shunter{running_shunter()}; + shunter->FuelPumpSwitchOff(true, range_t::local); // the pump started automatically ignores its switch + shunter.run(10.0); + CHECK(shunter->Mains); + shunter->BatterySwitch(false); + shunter.run(10.0); + CHECK(shunter->Power110vIsAvailable); // the converter run by the engine keeps feeding the fuel pump + CHECK(shunter->Mains); + CHECK(shunter->enrot > 0.9 * shunter->dizel_nmin); +} + +TEST_CASE("engine shutdown: the command opening the main circuit stops the engine and it stays stopped") { + auto shunter{running_shunter()}; + REQUIRE(shunter->MainCircuitOpensWithStarterOnly()); // the cab passes the command on with only the starter button + CHECK(shunter->MainSwitch(false)); + shunter.run(20.0); + CHECK_FALSE(shunter->Mains); + CHECK_FALSE(shunter->FuelPump.is_active); + CHECK(shunter->enrot == 0.0); +} + +TEST_CASE("engine shutdown: the engine starts again after it was stopped") { + auto shunter{running_shunter()}; + shunter->MainSwitch(false); + shunter.run(20.0); + REQUIRE(shunter->enrot == 0.0); + shunter->MainSwitch(true); + shunter.run(15.0); + CHECK(shunter->Mains); + CHECK(shunter->enrot > 0.9 * shunter->dizel_nmin); +} + +TEST_CASE("engine shutdown: vehicles without a diesel keep ignoring the command with only the button closing the circuit") { + test::vehicle wagon{test::definition::load("wagon")}; + CHECK_FALSE(wagon->MainCircuitOpensWithStarterOnly()); + test::vehicle electric{test::definition::load("wagon").set("Engine", "EngineType", "ElectricSeriesMotor")}; + REQUIRE(electric->EngineType == TEngineType::ElectricSeriesMotor); + CHECK_FALSE(electric->MainCircuitOpensWithStarterOnly()); +} diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 201b8972f..1b7d06a8f 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -3702,13 +3702,14 @@ void TTrain::OnCommand_linebreakeropen(TTrain *Train, command_data const &Comman { Train->ggMainButton.UpdateValue(0.0, Train->dsbSwitch); } - else if (Train->ggMainOnButton.SubModel != nullptr) + else if (Train->ggMainOnButton.SubModel != nullptr && false == Train->mvControlled->MainCircuitOpensWithStarterOnly()) { // NOTE: legacy behaviour, for vehicles equipped only with impulse close switch // it doesn't make any real sense to animate this one, but some people can't get over how there's no visual reaction to their keypress Train->ggMainOnButton.UpdateValue(1.0, Train->dsbSwitch); return; } + // a diesel with only the starter button gets its engine stopped. the button stays put: held down, it would start the engine again // play sound immediately when the switch is hit, not after release Train->fMainRelayTimer = 0.0f; From d90edb698033c66ccc93e1c447c53269dbeaa9e9 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 14:06:58 +0100 Subject: [PATCH 50/90] fix(cab): on the SU45 and SU46 the open line contactors lamp lights with the contactors opened by the pressure switch (eu07/maszyna#263) On the electric locomotives the lamp of the open line contactors tells the start can be made, so it goes out with the start cut off by the pressure switch. The diesel-electric SU45 and SU46 (the only diesel-electrics with the lamp) use it just to show the contactors are open, but it went out the same way: with too low a pressure in the main pipe the contactors were open and the lamp out. Once the pressure came back with the master controller past the first position, the contactors stayed open, as they should, and the lamp lit up on a power position, which looked like the lamp working the other way round. On the diesel-electrics TMoverParameters::LineContactorsLampLit() now follows the contactors alone; the electric locomotives and the other main circuit lamps are unchanged. Tests: tests/unit/test_line_contactors.cpp, on a locomotive made after the SU46. --- McZapkie/MOVER.h | 8 +++- tests/unit/data/diesel_electric.json | 46 ++++++++++++++++++ tests/unit/test_line_contactors.cpp | 71 ++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 tests/unit/data/diesel_electric.json diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 199c65a0c..a34d537f4 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2461,9 +2461,15 @@ class TMoverParameters { return false == ControlPressureSwitch && (HasControlPressureSwitch || BrakePress < 1.0); } - // whether the lamp of the open line contactors is lit, telling the start can be made + // whether the lamp of the open line contactors is lit. on the electric locomotives it tells the start can be made, so + // it goes out with the start cut off; on the diesel-electric ones (the SU45 and SU46 family) it just tells the + // contactors are open, whatever opened them: the master controller at 0, or the pressure switch (eu07/maszyna#263) bool LineContactorsLampLit() const { + if (EngineType == TEngineType::DieselElectric) + { + return false == StLinFlag; + } return false == StLinFlag && MainCircuitLampsEnabled(); } // whether the pumps a joint oil and fuel pump switch works, those not started automatically, are all switched on diff --git a/tests/unit/data/diesel_electric.json b/tests/unit/data/diesel_electric.json new file mode 100644 index 000000000..bef605880 --- /dev/null +++ b/tests/unit/data/diesel_electric.json @@ -0,0 +1,46 @@ +{ + "_comment": "Diesel-electric locomotive made after the SU46 (303D): the line contactors close on the first power position of the master controller, the pressure switch (on by default) opens them with too low a pressure in the main pipe or the brakes applied.", + "Param": { "Category": "train", "M": 105000, "Mred": 15000, "Vmax": 120, "PWR": 1400, "SandCap": 400 }, + "Dimensions": { "L": 18.99, "H": 4.292, "W": 2.904, "Cx": 0.3 }, + "Wheels": { "D": 1.1, "AIM": 4000, "Tw": 1.435, "Axle": "Co'Co'", "Ad": 1.75, "Bd": 10.3, "Rmin": 100 }, + "Brake": { + "BrakeValve": "LSt", "NBpA": 4, "MBF": 85.0, "MaxBP": 4.4, "LocMaxBP": 4.4, "MaxASBP": 1.3, "RM": 0.71, "BCN": 12, "BCR": 0.114, + "BCD": 0.06, "BCM": 5.84, "HiPP": 5, "LoPP": 3.5, "Vv": 0.9, "MinCP": 7, "MaxCP": 8.5, "CompressorSpeed": 0.05, "BCS": 1, "BSA": 2, + "BM": "P10-Bgu", "CompressorPower": "Converter", "BVV": 200, "BRE": 0.8 + }, + "BuffCoupl": { "CType": "Screw", "kB": 9200, "DmaxB": 0.15, "FmaxB": 740, "kC": 4800, "DmaxC": 0.15, "FmaxC": 780, "beta": 0.58, "AllowedFlag": 103 }, + "Cntrl": { + "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "BrakeSystem": "Pneumatic", "BDelay1": 15, "BDelay2": 6, "BDelay3": 36, "BDelay4": 32, + "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": 15, "SCPN": 4, "AutoRelay": "Yes", "IniCDelay": 0.5, "SCDelay": 0.1, + "BrakeDelays": "GPR", "ConverterStart": "Automatic", "ConverterStartDelay": 10 + }, + "Light": { "Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": 110, "LMaxCurrent": 100 }, + "Engine": { + "AIM": 1.6, "EngineType": "DumbDE", "Trans": "18:62", "Ftmax": 317000, "Flat": 0, "Vhyp": 15.0, "Vadd": 3.8, "Cr": 1.0, "RelayType": 46, + "OilMinPressure": 0.055, "OilMaxPressure": 0.6, "WaterMinTemperature": 40, "WaterMaxTemperature": 92, "WaterCoolingTemperature": 76, + "HeatKW": 0.068599, "HeatKV": 0.3, "HeatKFE": 1, "HeatKFS": 87.76269, "HeatKFO": 26.72716, "HeatKFO2": 57.72 + }, + "Circuit": { "CircuitRes": 0.01, "ImaxLo": 3300, "IminLo": 2500 }, + "_power": "rows: engine speed [rpm], power [kW], generator voltage limit, current limit, one per master controller position", + "WWList": { + "Size": 15, + "rows": [ + [696, 0, 0, 0], + [696, 0, 0, 0], + [696, 80, 350, 1000], + [696, 80, 350, 1000], + [763, 214, 455, 1400], + [830, 321, 500, 1600], + [897, 428, 545, 1800], + [964, 535, 590, 2000], + [1031, 642, 635, 2200], + [1098, 748, 680, 2400], + [1165, 855, 725, 2600], + [1232, 962, 770, 2800], + [1299, 1069, 815, 3000], + [1366, 1176, 865, 3200], + [1433, 1283, 910, 3400], + [1500, 1390, 960, 3600] + ] + } +} diff --git a/tests/unit/test_line_contactors.cpp b/tests/unit/test_line_contactors.cpp index 79c419edf..fd7e24f08 100644 --- a/tests/unit/test_line_contactors.cpp +++ b/tests/unit/test_line_contactors.cpp @@ -57,3 +57,74 @@ TEST_CASE("line contactors: the ground relay and overload lamps follow the same vehicle->MotorConnectorsCheck(); CHECK_FALSE(vehicle->MainCircuitLampsEnabled()); } + +// on the diesel-electric SU45 and SU46 (eu07/maszyna#263) the lamp tells just the line contactors are open: lit with the +// master controller at 0 and with the start cut off by too low a pressure in the main pipe, out on the power positions + +namespace { + +// the diesel-electric locomotive with its engine running and the brakes released, the master controller at 0 +test::vehicle diesel_electric() { + test::vehicle vehicle{test::definition::load("diesel_electric")}; + vehicle->CabOccupied = 1; + vehicle->CabActivisation(); + vehicle->BatterySwitch(true); + vehicle->OilPumpSwitch(true); + vehicle->FuelPumpSwitch(true); + vehicle.run(20.0); // the oil pressure builds up + vehicle.hold_brake_pipe(5.0); // bar + vehicle->MainSwitch(true); + vehicle.run(30.0); + vehicle->DirectionForward(); + vehicle.run(60.0); // the brakes release + return vehicle; +} + +} // namespace + +TEST_CASE("line contactors: on a diesel-electric the lamp is lit at 0 and goes out on a power position") { + auto vehicle{diesel_electric()}; + REQUIRE(vehicle->EngineType == TEngineType::DieselElectric); + REQUIRE(vehicle->Mains); + REQUIRE_FALSE(vehicle->ControlPressureSwitch); + CHECK_FALSE(vehicle->StLinFlag); + CHECK(vehicle->LineContactorsLampLit()); + + vehicle->IncMainCtrl(1); + vehicle.run(3.0); + CHECK(vehicle->StLinFlag); + CHECK_FALSE(vehicle->LineContactorsLampLit()); + + vehicle->DecMainCtrl(1); + vehicle.run(3.0); + CHECK_FALSE(vehicle->StLinFlag); + CHECK(vehicle->LineContactorsLampLit()); +} + +TEST_CASE("line contactors: on a diesel-electric the lamp lights with the contactors opened by too low a main pipe pressure") { + auto vehicle{diesel_electric()}; + vehicle->IncMainCtrl(1); + vehicle.run(1.0); + vehicle->IncMainCtrl(1); + vehicle.run(2.0); + REQUIRE(vehicle->StLinFlag); + + vehicle.hold_brake_pipe(2.5); // bar, below the 3.6 bar of the pressure switch + vehicle.run(10.0); + REQUIRE(vehicle->ControlPressureSwitch); + CHECK_FALSE(vehicle->StLinFlag); + CHECK(vehicle->LineContactorsLampLit()); // it went out here + CHECK_FALSE(vehicle->MainCircuitLampsEnabled()); // the ground relay and overload lamps still go out with the start cut off + + // the pressure back, the contactors stay open on the second position and the lamp stays lit, as it should; + // they close again only on the first one + vehicle.hold_brake_pipe(5.0); + vehicle.run(60.0); + REQUIRE_FALSE(vehicle->ControlPressureSwitch); + CHECK_FALSE(vehicle->StLinFlag); + CHECK(vehicle->LineContactorsLampLit()); + vehicle->DecMainCtrl(1); + vehicle.run(3.0); + CHECK(vehicle->StLinFlag); + CHECK_FALSE(vehicle->LineContactorsLampLit()); +} From 439e892abdaeac36bb76f3df2c8f926f76791477 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 15:35:35 +0100 Subject: [PATCH 51/90] AI: use the locomotive releaser only when starting off The rule "wyluzuj lokomotywe, to szybciej ruszymy" released the locomotive's own brake whenever power was asked for and the locomotive cylinder held more than 0.4 bar. It had no speed condition, so it also fired in motion, at 50-75 km/h, right after an application. On a locomotive with the EStED distributor (E186) this vented the control reservoir; with the pipe not yet recharged the locomotive brake then faded to nothing at the next application. The rule is meant for starting off, so it now applies only below 1 km/h. --- vehicle/Driver.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 2311ffacf..8d2a3fac6 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -8292,8 +8292,11 @@ void TController::control_releaser() { } } // wyluzuj lokomotywę, to szybciej ruszymy + // this rule is for starting off, so only below 1 km/h: at speed, just after an application, it would vent + // the control reservoir of a locomotive that may have to brake again before the pipe has refilled it if( mvOccupied->BrakePress > 0.4 - && mvOccupied->Hamulec->GetCRP() > 4.9 ) { + && mvOccupied->Hamulec->GetCRP() > 4.9 + && mvOccupied->Vel < 1.0 ) { actuate = true; } // keep engine brakes released during coupling/uncoupling From fddeea8b0e48e3bda81c3da5c90cb729eb8d3e31 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 15:35:35 +0100 Subject: [PATCH 52/90] AI: use the locomotive releaser only when starting off The rule "wyluzuj lokomotywe, to szybciej ruszymy" released the locomotive's own brake whenever power was asked for and the locomotive cylinder held more than 0.4 bar. It had no speed condition, so it also fired in motion, at 50-75 km/h, right after an application. On a locomotive with the EStED distributor (E186) this vented the control reservoir; with the pipe not yet recharged the locomotive brake then faded to nothing at the next application. The rule is meant for starting off, so it now applies only below 1 km/h. --- vehicle/Driver.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 08f0c0d24..97ec36470 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -8057,8 +8057,11 @@ void TController::control_releaser() { } } // wyluzuj lokomotywę, to szybciej ruszymy + // this rule is for starting off, so only below 1 km/h: at speed, just after an application, it would vent + // the control reservoir of a locomotive that may have to brake again before the pipe has refilled it if( mvOccupied->BrakePress > 0.4 - && mvOccupied->Hamulec->GetCRP() > 4.9 ) { + && mvOccupied->Hamulec->GetCRP() > 4.9 + && mvOccupied->Vel < 1.0 ) { actuate = true; } // keep engine brakes released during coupling/uncoupling From 939e42b611f3015f3f1bc5c1f0fe3be7b964b916 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Sat, 7 Nov 2026 16:17:57 +0100 Subject: [PATCH 53/90] docs: CLAUDE.md with the team's notes for Claude Code (#258) * docs: CLAUDE.md with the team's notes for Claude Code The rules Claude Code sessions kept relearning, shared with the team: - the code builds and behaves the same with GCC 13 (Linux CI and the Sonar build) as with MSVC, with the MSVC leniencies to watch for; - every SonarCloud remark on the changed code gets fixed without changing behaviour, with the recipes for the rules that keep coming up; - building, the unit tests, scripted editor runs, the game data and the profile folders, and GPU hangs; - the game data in the asset SVN and backward compatibility: existing files keep being read as before, new entries are optional, code falls back without new assets and a change that uses them says so in its commit message, what the editor saves stays readable by the current release, new features can be turned off in eu07.ini and don't change the look of existing data abruptly; StarterNG is the official starter. * docs: CLAUDE.md on the move to OpenGL 4.6 and Vulkan betterRenderer/ is to be removed and the OpenGL 3.3 and legacy renderers are being replaced by feat/gl46-migration (PR #216, a draft with milestone 27.04). Work on that code and on the shaders is kept to fixes and to what a feature on master needs to be drawn, with the PR saying where the branch has the counterpart. --------- Co-authored-by: maj00r --- CLAUDE.md | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 CLAUDE.md diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 000000000..c1709be8d --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,163 @@ +# MaSzyna: notes for Claude Code + +## The code builds and works with GCC, not only MSVC + +The Linux CI (`.github/workflows/build.yml`) builds with GCC 13 on ubuntu-24.04, through the Sonar build-wrapper, and `sonar.yml` +analyses that build once it has passed. A change that compiles with MSVC only fails there, and a failed GCC build also leaves Sonar +without an analysis of the PR. So every change has to compile with GCC, and behave the same with it, as well as with MSVC. The CI +builds with `-DWITH_BETTER_RENDERER=OFF` and Sonar excludes `betterRenderer/`, so that part is neither built on CI nor analysed. + +Where no GCC is at hand, read the new code for what MSVC lets through and GCC doesn't: + +- `constexpr` initialised from a floating or class-type constant declared only `const`: make the source `constexpr` too. +- Standard headers MSVC pulls in through others: include them yourself. For example, `` for `std::views`, ``, + ``, ``, ``, `` and ``. +- `typename` and `this->` where a template needs them. +- A temporary bound to a non-const reference. +- Explicit specialisations inside a class. +- A name a class uses and then declares again in its own body (GCC: "changes meaning"). +- A nested type with default member initialisers used as a default argument inside the class that encloses it. +- A declaration changed in a header (`const`, parameters) without its definition, or a new member function defined but not declared. +- `std::ifstream` / `std::ofstream` opened with a `std::wstring`. +- MSVC-only functions: `_stricmp`, `sprintf_s`, `fopen_s`, `localtime_s` and the like. +- Designated initialisers out of declaration order. +- `emplace_back(a, b, c)` with parenthesised aggregate initialisation is fine on GCC 13, but not for `std::array`. There is no brace + elision in parentheses, so build a named local instead. + +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 (`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. + +Fixing a remark must not change what the code does: + +- Keep the order of side effects, the conditions and the floating-point results as they were. +- Text in `STR(...)`, `STR_C(...)` and `_(...)` is looked up in the translations (`lang/*.po`), so it stays byte for byte the same. + The same goes for what is written to the log. +- `// 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: + +- **S134** (nesting deeper than 3 `if` / `for` / `while` / `switch` / `try`) and **S3776** (cognitive complexity over 25): + - use early `continue` / `return`; + - move loop bodies and branches into named helpers, preferably free functions in the anonymous namespace; + - group the state of a long algorithm in a small struct with methods. + + Every `else if`, every `&&` / `||` sequence and every nested lambda adds to the complexity. +- **S1188**: a lambda over 20 lines becomes a named function. +- **S107**: a function with more than 7 parameters takes a struct. +- **S3608**: lambdas capture explicitly (`[this, &points]`, not `[&]` or `[=]`). The only exception is `[&]` on a lambda passed + straight to a standard algorithm; `[=]` is flagged even there, and so is `[&]` passed to one of our own functions. +- **S6197** / **S5566**: use the `std::ranges::` algorithm that takes the container rather than a begin/end pair or a hand-written + loop. +- **S6165**: use `std::erase_if` rather than erase-remove. +- **S6004**: a variable used only by the condition is declared in the `if (init; condition)`. +- **S3358**: no nested `?:`; use an `if` or a small function. +- **S1121**: no assignment inside an expression, chained ones included (`a = b = c`). +- **S1117**: no declaration shadowing a local, a member or an enumerator. +- **S6003**: `v.emplace_back(a, b, c)` rather than `v.push_back({a, b, c})`. For a `std::array` element, push a named local instead + (see the GCC notes above). +- **S6012**: let class template argument deduction work: `std::vector values{1.0, 2.0}`. +- **S5945**: `std::array` / `std::vector` / `std::string`, not C arrays. +- **S1659**: one declaration per statement. +- **S6009**: `std::string_view` rather than `std::string const &` for a string parameter that is only read. +- **S6185** / **S6494**: `std::format` rather than concatenation or `snprintf`. +- **S924**: at most one `break` per loop. +- **S886**: a `for` whose counter changes in its body becomes a `while`. +- **S5997**: `std::scoped_lock` rather than `std::lock_guard`. +- **S6168**: `std::jthread` rather than `std::thread`. A `std::jthread` joins in its destructor where a `std::thread` would + terminate, so in existing code switch only where the thread is always joined or detached before it is destroyed. +- **S6177**: `using enum` where a function names many enumerators. +- **S5350** / **S995**: `const` on references and pointers that aren't written through. +- **S5817**: `const` on member functions that change nothing. +- **S2193**: no `float` loop counters. +- **S5421**: no non-const globals. +- **S6179**: `std::lerp` rather than a hand-written interpolation, in new code. In existing code only where it gives the very same + result. +- **S6164**: `std::numbers::pi` rather than a literal, in new code. In existing code only where it gives the very same result. + +The project is C++20 (`CMAKE_CXX_STANDARD 20`), so no C++23 library addition is available on any compiler. That rules out +`std::ranges::to`, `views::zip`, `views::enumerate`, `ranges::contains`, `std::print` and `std::expected`. + +## Renderers: the move to OpenGL 4.6 and Vulkan + +The renderers on `master` are to be replaced. `feat/gl46-migration` (PR #216, a draft with milestone 27.04, to be merged after the +26.11 release) replaces the OpenGL 3.3 renderer with an OpenGL 4.6 core one and adds a Vulkan 1.4 renderer beside it. It also +removes the legacy OpenGL 2.1, the OpenGL ES / ANGLE and the NVRHI (`betterRenderer/`) paths. + +- `betterRenderer/` will be removed. Don't put work into it. +- On that branch, of the `rendering/opengl*` files only `openglcamera.*` and `openglmatrixstack.*` stay. The whole + `rendering/opengl33*` family and the rest of the legacy `rendering/opengl*` go, and the new renderers are in + `rendering/opengl46/`, `rendering/vulkan14/` and `rendering/common/`. The shared `rendering/renderer.*`, `geometrybank.*`, + `particles.*` and the like stay. The shaders move from `shaders/` to `shaders/ogl46/` and `shaders/vk14/`. +- So a change on `master` to the code that goes away, or to the shaders, may be pointless. Keep it to fixes and to what a feature + on `master` needs in order to be drawn at all. For such a change, say in the PR where `feat/gl46-migration` has the counterpart, + or that the branch already carries the change. + +## Building, testing and running + +- The unit tests (`tests/unit`) run at the end of the build, which prints `Unit tests: N/N passed` and fails on any failed case. + `-DWITH_TESTS=OFF` builds the game without them. +- A new feature goes on its own branch from `master`, not on whatever branch is checked out. Say which branch the work lands on + before the first edit. A fresh worktree has empty submodules, so it doesn't build until they are filled. +- The editor runs a script when `EU07_EDITOR_SELFTEST=` is set: one command per line, `#` for comments. The commands are in + `application/editormodeselftest*.cpp` (`camera`, `wait`, `select`, `lay`, `screenshot`, `save`, `quit` and many more). + - Run it on a copy of the scenery, never on the original, and delete the copy afterwards. + - Give every run a timeout and keep an eye on its memory. A loop gone wrong can fill the RAM of the whole machine. + - `screenshot` only queues the grab and waits 3 frames; the image is written later on another thread. A click right after it + has been seen to lose the selection, so leave a `wait` of about 15 frames after a `screenshot` before clicking. +- At run time the editor's texts load from `lang/editor_.po` in the game folder (`utilities/translation.cpp`), not from + the build. New strings of a branch show in English until that file has them. Without the `fonts` folder Polish letters show as `?`. +- When a build runs on a copy of the game data made of links to the real folders, turn off whatever writes into the data + (`convertmodels 0` in `eu07.ini`), or the run changes the real game folder through the links. +- A test of code that moves, migrates or deletes files in the user's profile (`%APPDATA%`, `%LOCALAPPDATA%`, caches) gets its own + test folder. Never point it at the real profile. +- A GPU hang (Windows TDR) can freeze the whole machine, not just the game. Don't test a hang hypothesis by running it. Guard the + code against what causes it (degenerate geometry, NaN in the camera or in transforms) and read the `LiveKernelEvent` / watchdog + dumps instead. +- PowerShell variable names are case-insensitive: `$Track` and `$track` are the same variable. + +## Game data and backward compatibility + +The code and the game data live apart. The assets (sceneries, vehicles with their `.fiz` and `.mmd`, models, textures, sounds, +timetables, most of `lang` and the shipped `eu07.ini`) are in a separate SVN repository, `https://svn.eu07.pl/pctga`. It needs an +account; there is no anonymous checkout. Of `lang/`, only `editor_pl.po` is in this repository. Players install and +launch the game through Steam, GOG and the like, and run new builds on the data they already have. + +The official starter is StarterNG (`https://github.com/MaSzyna-EU07/StarterNG`), a separate repository; the older +`MaSzyna-EU07/Starter` is not the one in use. `launcher/` in this repository is the simulator's built-in scenario and vehicle +picker (`launcher_mode`), not StarterNG. + +- Every existing file must still be read the way it was. Don't rename or drop a keyword, change the meaning or the unit of a value, + or make an optional entry required. This holds for every format the game reads: for example `.scn` and its includes, `.fiz`, + `.mmd`, `.e3d` / `.t3d`, `.mat`, the `.txt` timetables and track profiles, `eu07.ini` and the Python screens. +- A bug fix may change what an existing file leads to, such as how a vehicle from its `.fiz` behaves, as long as the PR says so. +- Something new in a format is optional, and without it the code does exactly what it did before. A file that loaded correctly + before doesn't get new warnings or errors in the log. A real fault that is newly detected may be logged. +- A code change must not need a change in the SVN to work. Where it uses new or changed assets, it falls back to the old behaviour + without them, and the PR says what is to be added to the SVN and in what order the two go in. +- A change that uses new or changed assets (and falls back without them, see above) says so plainly in its commit message: which + files, and what is to change in them. It is never left implied. Before such a commit, either make the asset change right away in + the user's SVN working copy, or get the user's agreement to leave it for later. Ask the user where their SVN working copy is; the + installed game folder is not one. Pushing to git and committing to the SVN both need the user's consent. +- A scenery saved by the editor without using a new feature holds nothing the current release can't read. The entries of a new + feature (such as a new kind of node) appear only where that feature was used, and the PR says that the release doesn't read + them. +- Saving patches the scenery files by byte spans: unchanged definitions stay byte for byte, and an edited node is written anew by + its `export_as_text`. A change to saving must not widen what gets rewritten, nor make `export_as_text` drop more of an edited + node (comments, unknown tokens) than it did before. +- A new feature that changes what the simulator does or shows with existing sceneries and vehicles can be turned off in + `eu07.ini`, and turned off, the simulator behaves as it did before the feature. Editor tools and new optional entries of a + format are covered by the points above. The key is parsed in `utilities/Globals.cpp` (`global_settings::ConfigParse*`) and + written by `global_settings::export_as_text`, which puts the settings in the log at start. `eu07.ini` is read from the user's + profile (`user_config_path`), and from the game folder only when the profile has none; test the switch in the file that is + actually read. +- A feature doesn't make existing sceneries and vehicles look suddenly different after an update. A change to how things look is + something the player turns on, or comes in gradually. +- When a format really has to change, read both the old and the new form, and say in the PR what changes for the data and for + the people who make it. From 798fd89dd0fc616967485a06530f9b40a88791e9 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 30 Sep 2026 19:16:46 +0200 Subject: [PATCH 54/90] test: scenario tests that run the simulator and check a vehicle's physicslog tests/scenarios/run.py builds a test scenery from a game install's scenery (own trainsets removed, case fragment added), runs eu07 in ghostview with physicslog on, and checks what the vehicle did: starts, max speed, brakes, stops. First case: EU07 on td.scn driven by AI on the td/rozklad timetable, with the Stacja1 exit route set by an event launcher instead of shift+4. eu07 gets a -ini option: an extra settings file read after eu07.ini, so a test run can switch to a window and turn on physicslog without touching the user's config. Local only: scenery and vehicle data are not in the repo. --- application/application.cpp | 12 +- tests/scenarios/.gitignore | 2 + tests/scenarios/README.md | 66 ++++ tests/scenarios/cases/eu07_td_timetable.json | 15 + tests/scenarios/cases/eu07_td_timetable.scn | 11 + tests/scenarios/run.py | 330 +++++++++++++++++++ 6 files changed, 435 insertions(+), 1 deletion(-) create mode 100644 tests/scenarios/.gitignore create mode 100644 tests/scenarios/README.md create mode 100644 tests/scenarios/cases/eu07_td_timetable.json create mode 100644 tests/scenarios/cases/eu07_td_timetable.scn create mode 100644 tests/scenarios/run.py diff --git a/application/application.cpp b/application/application.cpp index 9b7b82853..ee381f98b 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -1195,12 +1195,22 @@ 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 == "-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]" << " [-v vehiclename]" << " [-edit [sceneryfilepath]]" - << " [-seed number|word]" << std::endl; + << " [-seed number|word]" + << " [-ini settingsfile]" << std::endl; return -1; } } 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..77edb5a93 --- /dev/null +++ b/tests/scenarios/README.md @@ -0,0 +1,66 @@ +# Scenario tests + +End-to-end checks that run the real simulator on a scenery and look at what a vehicle +did: did it start, did it keep to the speed limit, did it brake and stop. + +Scenery and vehicle data are not in this repo, so these run against a local game install +only, not in CI. + +## Running + +``` +python tests/scenarios/run.py --game-dir "E:/Program Files (x86)/Steam/steamapps/common/MaSzyna" +``` + +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 `-ini` command line option. +- name one or more cases to run only those, e.g. `run.py ... eu07_td_timetable`. +- `--timespeed 4` runs the simulation faster than real time. + +Exit code is 0 when every case passed. Logs of each run (`log.txt`, `errors.txt`, +the vehicle's physicslog) land in `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, and the case's `.scn` + fragment added: its trainsets after `FirstInit`, everything else (event launchers standing + in for keys the scenery expects a player to press, extra events) before it, +2. starts the simulator with `-s autotest_.scn -v ghostview -ini out//autotest.ini`; + ghostview leaves every train to the AI, and the extra ini turns on `physicslog`, + runs in a 1280x720 window and does not pause when the window loses focus, +3. waits until `physicslog/.dat` covers `duration` seconds, then closes the simulator, +4. runs the checks on the physicslog. + +## 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` | +| `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) | +| `vehicle` | node name of the vehicle whose physicslog is checked | +| `duration` | seconds of simulation to record | +| `timeout` | wall-clock seconds before giving up, loading included | +| `fail_on_errors` | fail when `errors.txt` is not empty (default `false`) | +| `checks` | list of checks, below | + +Checks (speeds in km/h, times in seconds of physicslog time): + +- `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`: after reaching `moving_speed`, stays at or + below `stop_speed` for `hold` seconds. + +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/eu07_td_timetable.json b/tests/scenarios/cases/eu07_td_timetable.json new file mode 100644 index 000000000..c12b8ae71 --- /dev/null +++ b/tests/scenarios/cases/eu07_td_timetable.json @@ -0,0 +1,15 @@ +{ + "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": "eu07_td_timetable.scn", + "vehicle": "autotest_eu07", + "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": 0.5, "hold": 5 } + ] +} diff --git a/tests/scenarios/cases/eu07_td_timetable.scn b/tests/scenarios/cases/eu07_td_timetable.scn new file mode 100644 index 000000000..c8044e467 --- /dev/null +++ b/tests/scenarios/cases/eu07_td_timetable.scn @@ -0,0 +1,11 @@ +// added by tests/scenarios/run.py to the base scenery, whose own trainsets were removed: +// the trainset goes after FirstInit, the launcher before it. +// the EU07 stands where td.scn keeps its timetable train and gets the same timetable. +trainset td/rozklad tdo_n1734 0.0 0.1 +node -1 0 autotest_eu07 dynamic pkp\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic +endtrainset + +// td.scn sets routes from the keyboard (shift+1..4 run events keyctrl01..04). keyctrl04 is the +// way out of the track the train stands on: 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/run.py b/tests/scenarios/run.py new file mode 100644 index 000000000..ad1f51ee7 --- /dev/null +++ b/tests/scenarios/run.py @@ -0,0 +1,330 @@ +#!/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 +appended to it (usually a trainset with an AI driver), 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 appended, + 2. starts eu07 with that scenery in ghostview (every train driven by AI) and an extra + settings file (-ini) that turns on physicslog and runs in a window, + 3. waits until the vehicle's physicslog covers `duration` seconds of simulation, + then closes the simulator, + 4. evaluates the checks on the log 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. + +Usage: + python tests/scenarios/run.py --game-dir "E:/.../MaSzyna" # 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 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 +COL_BRAKE_PRESS = 8 + +# settings layered over the user's eu07.ini for a test run +TEST_INI = """\ +fullscreen no +fullscreenwindowed no +width 1280 +height 720 +inactivepause no +soundenabled no +physicslog yes +multiplelogs no +timespeed {timespeed} +""" + + +# ---------------------------------------------------------------- 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 build_scenery(case: dict, game_dir: Path, case_name: 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"]) + fragment = (CASES_DIR / case["fragment"]).read_bytes().decode("latin-1") + # 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) + 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")) + return name + + +# ---------------------------------------------------------------- run + + +def read_physicslog(path: Path) -> list[list[float]]: + """Returns rows of numeric columns up to BrakePress; 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: + rows.append([float(p) for p in parts[: COL_BRAKE_PRESS + 1]]) + except ValueError: + continue + return rows + + +@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) + timed_out: bool = False + wall_seconds: float = 0.0 + + +def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog: Path, + duration: float, timeout: float) -> RunResult: + physicslog.unlink(missing_ok=True) + cmd = [str(exe), "-s", scenery, "-v", "ghostview", "-ini", str(ini)] + print(" $", " ".join(f'"{c}"' if " " in c else c for c in cmd)) + started = time.monotonic() + proc = subprocess.Popen(cmd, cwd=game_dir) + result = RunResult() + last_report = 0.0 + try: + while True: + time.sleep(1.0) + result.rows = read_physicslog(physicslog) + sim_t = result.rows[-1][COL_TIME] if result.rows else 0.0 + wall = time.monotonic() - started + if wall - last_report >= 15: + v = result.rows[-1][COL_VELOCITY] if result.rows else 0.0 + print(f" ... {wall:5.0f}s wall, sim {sim_t:6.1f}s, v={v:6.1f} km/h") + last_report = wall + if proc.poll() is not None: + result.exit_code = proc.returncode + break + if sim_t >= duration: + break + if wall > timeout: + result.timed_out = True + 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 + result.rows = read_physicslog(physicslog) + 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 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): + start = _first_moving(rows, moving_speed) + if start is None: + return False, f"never reached {moving_speed} km/h" + stopped_at = None + for r in rows[start:]: + if r[COL_VELOCITY] <= stop_speed: + if stopped_at is None: + stopped_at = r[COL_TIME] + if r[COL_TIME] - stopped_at >= hold: + return True, f"came to a stop at t={stopped_at:.1f}s" + else: + stopped_at = None + return False, f"did not stay below {stop_speed} km/h for {hold}s after moving off" + + +CHECKS = { + "starts": check_starts, + "max_speed": check_max_speed, + "brakes": check_brakes, + "stops": check_stops, +} + + +# ---------------------------------------------------------------- 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 run_case(name: str, exe: Path, game_dir: Path, timespeed: float) -> bool: + case = json.loads((CASES_DIR / f"{name}.json").read_text(encoding="utf-8")) + print(f"\n=== {name}: {case.get('description', '')}") + out = OUT_DIR / name + shutil.rmtree(out, ignore_errors=True) + out.mkdir(parents=True) + + scenery = build_scenery(case, game_dir, name) + ini = out / "autotest.ini" + ini.write_text(TEST_INI.format(timespeed=timespeed), encoding="ascii") + physicslog = game_dir / "physicslog" / f"{case['vehicle']}.dat" + + result = run_simulator(exe, game_dir, scenery, ini, physicslog, + float(case["duration"]), float(case.get("timeout", 600))) + + for f in ("log.txt", "errors.txt"): + if (game_dir / f).exists(): + shutil.copy2(game_dir / f, out / f) + if physicslog.exists(): + shutil.copy2(physicslog, out / physicslog.name) + + ok = True + sim_t = result.rows[-1][COL_TIME] if result.rows else 0.0 + print(f" ran {result.wall_seconds:.0f}s wall, {sim_t:.1f}s of physicslog, results in {out.relative_to(REPO)}") + if result.exit_code is not None: + print(f" FAIL simulator exited on its own with code {result.exit_code} (crash?)") + ok = False + if result.timed_out: + print(f" FAIL timed out before {case['duration']}s of simulation") + ok = False + if not result.rows: + print(f" FAIL no physicslog for '{case['vehicle']}' ({physicslog})") + return False + + for check in case["checks"]: + params = {k: v for k, v in check.items() if k != "type"} + passed, detail = CHECKS[check["type"]](result.rows, **params) + print(f" {'PASS' if passed else 'FAIL'} {check['type']}: {detail}") + ok &= passed + + errors = count_lines(out / "errors.txt") + if errors: + print(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("--timespeed", type=float, default=1.0, help="simulation time multiplier") + 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")) + results = {n: run_case(n, exe, game_dir, args.timespeed) for n in names} + + print("\n=== summary") + for n, ok in results.items(): + print(f" {'PASS' if ok else 'FAIL'} {n}") + return 0 if all(results.values()) else 1 + + +if __name__ == "__main__": + sys.exit(main()) From 6cf8934acb65aec779aea3d0f7a9604f9ef7ec15 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 30 Sep 2026 19:29:10 +0200 Subject: [PATCH 55/90] test: run scenario tests at timespeed 20 by default eu07_td_timetable now takes about 20 s instead of 4 min. Its speed profile at 20 and 60 matched a real-time run within a few km/h and seconds; the physicslog keeps its 0.05 s rows. The runner polls the log every 0.25 s so it does not overshoot the case duration by a minute of simulation. --- tests/scenarios/README.md | 5 ++++- tests/scenarios/run.py | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 77edb5a93..c233154d6 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -18,7 +18,10 @@ Python 3.10+, no extra packages. Options: - `--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 `-ini` command line option. - name one or more cases to run only those, e.g. `run.py ... eu07_td_timetable`. -- `--timespeed 4` runs the simulation faster than real time. +- `--timespeed N` sets the simulation time multiplier, default 20: a 4-minute case runs in + about 20 s. Physics still steps finely (the physicslog keeps its 0.05 s rows) and the speed + profile at 20 and 60 matched a real-time run within a few km/h and seconds. Use + `--timespeed 1` to watch a run. Exit code is 0 when every case passed. Logs of each run (`log.txt`, `errors.txt`, the vehicle's physicslog) land in `tests/scenarios/out//`. diff --git a/tests/scenarios/run.py b/tests/scenarios/run.py index ad1f51ee7..b3736a929 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -149,7 +149,7 @@ def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog last_report = 0.0 try: while True: - time.sleep(1.0) + time.sleep(0.25) # at timespeed 20 that is 5 s of simulation result.rows = read_physicslog(physicslog) sim_t = result.rows[-1][COL_TIME] if result.rows else 0.0 wall = time.monotonic() - started @@ -308,7 +308,10 @@ def main() -> int: 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("--timespeed", type=float, default=1.0, help="simulation time multiplier") + # 20 gave the same speed profile as 1 within a few km/h and seconds on eu07_td_timetable, + # in 20 s instead of 4 min; 1 is for watching a run + ap.add_argument("--timespeed", type=float, default=20.0, + help="simulation time multiplier, the simulator offers 1, 5, 20 and 60 (default 20)") args = ap.parse_args() if not args.game_dir: From cb0a1223835009fa8e82726170d4cc0afe992a41 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 30 Sep 2026 21:55:16 +0200 Subject: [PATCH 56/90] test: red signal case, seven vehicles on the td.scn timetable, parallel runs - eu07_td_red_signal: no route set, the train has to stop before the exit signal and stay there (new checks stays_stopped and distance; distance is integrated from the speed, the physicslog has no position) - en57/ep09/et22/sm42/sn61/vectron/eu07_td_timetable share one fragment template, td_timetable.scn; a case sets vars.consist and the lead vehicle is named {{vehicle}} = autotest_ - -j N runs cases in parallel: each case has its own physicslog and, with multiplelogs, its own log files, which are moved to out// - a run stops at once on Bad vehicle / Bad trainset instead of waiting for the timeout, and fails after 120 s without physicslog rows - the .sbt geometry cache of a test scenery is dropped when the base scenery is newer All 8 cases pass in about a minute with -j 4. --- tests/scenarios/README.md | 35 ++- tests/scenarios/cases/en57_td_timetable.json | 21 ++ tests/scenarios/cases/ep09_td_timetable.json | 17 ++ tests/scenarios/cases/et22_td_timetable.json | 17 ++ tests/scenarios/cases/eu07_td_red_signal.json | 18 ++ tests/scenarios/cases/eu07_td_timetable.json | 6 +- tests/scenarios/cases/sm42_td_timetable.json | 17 ++ tests/scenarios/cases/sn61_td_timetable.json | 17 ++ tests/scenarios/cases/td_red_signal.scn | 6 + ...eu07_td_timetable.scn => td_timetable.scn} | 9 +- .../scenarios/cases/vectron_td_timetable.json | 17 ++ tests/scenarios/run.py | 201 ++++++++++++++---- 12 files changed, 332 insertions(+), 49 deletions(-) create mode 100644 tests/scenarios/cases/en57_td_timetable.json create mode 100644 tests/scenarios/cases/ep09_td_timetable.json create mode 100644 tests/scenarios/cases/et22_td_timetable.json create mode 100644 tests/scenarios/cases/eu07_td_red_signal.json create mode 100644 tests/scenarios/cases/sm42_td_timetable.json create mode 100644 tests/scenarios/cases/sn61_td_timetable.json create mode 100644 tests/scenarios/cases/td_red_signal.scn rename tests/scenarios/cases/{eu07_td_timetable.scn => td_timetable.scn} (51%) create mode 100644 tests/scenarios/cases/vectron_td_timetable.json diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index c233154d6..23f793018 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -9,7 +9,7 @@ only, not in CI. ## Running ``` -python tests/scenarios/run.py --game-dir "E:/Program Files (x86)/Steam/steamapps/common/MaSzyna" +python tests/scenarios/run.py --game-dir "E:/Program Files (x86)/Steam/steamapps/common/MaSzyna" -j 4 ``` Python 3.10+, no extra packages. Options: @@ -18,13 +18,15 @@ Python 3.10+, no extra packages. Options: - `--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 `-ini` command line option. - name one or more cases to run only those, e.g. `run.py ... eu07_td_timetable`. +- `-j N` runs N cases at once, each in its own simulator window. Each case prints its + results when it is done. All 8 cases take about a minute with `-j 4`. - `--timespeed N` sets the simulation time multiplier, default 20: a 4-minute case runs in about 20 s. Physics still steps finely (the physicslog keeps its 0.05 s rows) and the speed profile at 20 and 60 matched a real-time run within a few km/h and seconds. Use `--timespeed 1` to watch a run. Exit code is 0 when every case passed. Logs of each run (`log.txt`, `errors.txt`, -the vehicle's physicslog) land in `tests/scenarios/out//`. +the vehicle's physicslog) are moved to `tests/scenarios/out//`. ## How it works @@ -35,11 +37,19 @@ For each case the runner: fragment added: its trainsets after `FirstInit`, everything else (event launchers standing in for keys the scenery expects a player to press, extra events) before it, 2. starts the simulator with `-s autotest_.scn -v ghostview -ini out//autotest.ini`; - ghostview leaves every train to the AI, and the extra ini turns on `physicslog`, - runs in a 1280x720 window and does not pause when the window loses focus, -3. waits until `physicslog/.dat` covers `duration` seconds, then closes the simulator, + ghostview leaves every train to the AI, and the extra ini turns on `physicslog`, runs in a + 1280x720 window, does not pause when the window loses focus, and writes the logs to + `logs/log (autotest_) .txt` rather than the shared `log.txt` (`multiplelogs`), +3. waits until `physicslog/autotest_.dat` covers `duration` seconds, then closes the + simulator. It gives up early when the log reports `Bad vehicle` / `Bad trainset`, or when + the scenario has loaded and no physicslog appeared, 4. 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. + ## Writing a case `cases/.json`: @@ -50,13 +60,19 @@ For each case the runner: | `strip_trainsets` | drop the base scenery's trainsets (default `true`) | | `time` | replace the scenery start time, `HH:MM` | | `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) | -| `vehicle` | node name of the vehicle whose physicslog is checked | +| `vars` | values for `{{name}}` in the fragment; a list becomes one line per item | | `duration` | seconds of simulation to record | | `timeout` | wall-clock seconds before giving up, loading included | | `fail_on_errors` | fail when `errors.txt` is not empty (default `false`) | | `checks` | list of checks, below | -Checks (speeds in km/h, times in seconds of physicslog time): +`{{vehicle}}` is always set, also inside `vars` values: the node name of the lead vehicle, +the one whose physicslog is checked. Other vehicles of the consist take names made from it, +e.g. `{{vehicle}}_s`. 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`. + +Checks (speeds in km/h, times in seconds of physicslog time, distances in metres): - `starts` `min_speed`, `within`: reaches `min_speed` within `within` seconds. - `max_speed` `limit`, `tolerance`: never goes faster than `limit + tolerance`. @@ -64,6 +80,11 @@ Checks (speeds in km/h, times in seconds of physicslog time): (physicslog pressures are in bar whatever its header says; the brake pipe runs at 5.0). - `stops` `moving_speed`, `stop_speed`, `hold`: after reaching `moving_speed`, stays at or below `stop_speed` for `hold` seconds. +- `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 (+-3), and the red signal stop at 140 m. 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..ddd52af37 --- /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": 0.5, "hold": 5 } + ] +} diff --git a/tests/scenarios/cases/ep09_td_timetable.json b/tests/scenarios/cases/ep09_td_timetable.json new file mode 100644 index 000000000..dd130be3c --- /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": 0.5, "hold": 5 } + ] +} diff --git a/tests/scenarios/cases/et22_td_timetable.json b/tests/scenarios/cases/et22_td_timetable.json new file mode 100644 index 000000000..004f5354d --- /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": 0.5, "hold": 5 } + ] +} 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 index c12b8ae71..2f23019fe 100644 --- a/tests/scenarios/cases/eu07_td_timetable.json +++ b/tests/scenarios/cases/eu07_td_timetable.json @@ -2,8 +2,10 @@ "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": "eu07_td_timetable.scn", - "vehicle": "autotest_eu07", + "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": [ diff --git a/tests/scenarios/cases/sm42_td_timetable.json b/tests/scenarios/cases/sm42_td_timetable.json new file mode 100644 index 000000000..fc89be0fa --- /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": 0.5, "hold": 5 } + ] +} diff --git a/tests/scenarios/cases/sn61_td_timetable.json b/tests/scenarios/cases/sn61_td_timetable.json new file mode 100644 index 000000000..1db425c80 --- /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": 0.5, "hold": 5 } + ] +} 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/eu07_td_timetable.scn b/tests/scenarios/cases/td_timetable.scn similarity index 51% rename from tests/scenarios/cases/eu07_td_timetable.scn rename to tests/scenarios/cases/td_timetable.scn index c8044e467..e67dcf9ad 100644 --- a/tests/scenarios/cases/eu07_td_timetable.scn +++ b/tests/scenarios/cases/td_timetable.scn @@ -1,11 +1,12 @@ -// added by tests/scenarios/run.py to the base scenery, whose own trainsets were removed: +// added by tests/scenarios/run.py to td.scn, whose own trainsets were removed: // the trainset goes after FirstInit, the launcher before it. -// the EU07 stands where td.scn keeps its timetable train and gets the same timetable. +// 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 -node -1 0 autotest_eu07 dynamic pkp\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic +{{consist}} endtrainset // td.scn sets routes from the keyboard (shift+1..4 run events keyctrl01..04). keyctrl04 is the -// way out of the track the train stands on: switch zwr02 and the exit signal stacja1_j at 40. +// 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/vectron_td_timetable.json b/tests/scenarios/cases/vectron_td_timetable.json new file mode 100644 index 000000000..9948a09ef --- /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": 0.5, "hold": 5 } + ] +} diff --git a/tests/scenarios/run.py b/tests/scenarios/run.py index b3736a929..419906700 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -2,11 +2,11 @@ """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 -appended to it (usually a trainset with an AI driver), and checks on the vehicle's +added to it (usually a trainset with an AI driver), 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 appended, + trainsets removed, optionally a different start time, and the case fragment added, 2. starts eu07 with that scenery in ghostview (every train driven by AI) and an extra settings file (-ini) that turns on physicslog and runs in a window, 3. waits until the vehicle's physicslog covers `duration` seconds of simulation, @@ -14,10 +14,10 @@ physics log. The runner: 4. evaluates the checks on the log 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. +install only (--game-dir or EU07_GAME_DIR), not in CI. See README.md. Usage: - python tests/scenarios/run.py --game-dir "E:/.../MaSzyna" # all cases + 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 """ @@ -31,6 +31,7 @@ import shutil import subprocess import sys import time +from concurrent.futures import ThreadPoolExecutor, as_completed from dataclasses import dataclass, field from pathlib import Path @@ -54,9 +55,11 @@ height 720 inactivepause no soundenabled no physicslog yes -multiplelogs no +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 @@ -83,7 +86,19 @@ 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 build_scenery(case: dict, game_dir: Path, case_name: str) -> str: +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 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 @@ -93,6 +108,9 @@ def build_scenery(case: dict, game_dir: Path, case_name: str) -> str: if "time" in case: text = set_start_time(text, case["time"]) 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) @@ -106,6 +124,12 @@ def build_scenery(case: dict, game_dir: Path, case_name: str) -> str: 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_dir / f"autotest_{case_name}.sbt" + if cache.exists() and cache.stat().st_mtime < (scenery_dir / case["base_scenery"]).stat().st_mtime: + cache.unlink() return name @@ -130,19 +154,47 @@ def read_physicslog(path: Path) -> list[list[float]]: 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 + + @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) - timed_out: bool = False + 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, timeout: float) -> RunResult: + duration: float, timeout: float, say) -> RunResult: physicslog.unlink(missing_ok=True) cmd = [str(exe), "-s", scenery, "-v", "ghostview", "-ini", str(ini)] - print(" $", " ".join(f'"{c}"' if " " in c else c for c in cmd)) + say(" $ " + " ".join(f'"{c}"' if " " in c else c for c in cmd), progress=True) + since = time.time() - 1 started = time.monotonic() proc = subprocess.Popen(cmd, cwd=game_dir) result = RunResult() @@ -150,12 +202,12 @@ def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog 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 - wall = time.monotonic() - started if wall - last_report >= 15: v = result.rows[-1][COL_VELOCITY] if result.rows else 0.0 - print(f" ... {wall:5.0f}s wall, sim {sim_t:6.1f}s, v={v:6.1f} km/h") + 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 @@ -163,8 +215,18 @@ def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog if sim_t >= duration: break if wall > timeout: - result.timed_out = True + result.failure = f"timed out after {timeout:.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() @@ -174,6 +236,7 @@ def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog proc.kill() result.wall_seconds = time.monotonic() - started result.rows = read_physicslog(physicslog) + result.logs = find_logs(game_dir, scenery, since) return result @@ -201,6 +264,15 @@ 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: @@ -215,23 +287,49 @@ def check_stops(rows, moving_speed, stop_speed, hold): 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 r in rows[start:]: + for i in range(start, len(rows)): + r = rows[i] if r[COL_VELOCITY] <= stop_speed: if stopped_at is None: - stopped_at = r[COL_TIME] - if r[COL_TIME] - stopped_at >= hold: - return True, f"came to a stop at t={stopped_at:.1f}s" + stopped_at = i + if r[COL_TIME] - rows[stopped_at][COL_TIME] >= hold: + return True, f"came to a stop at t={rows[stopped_at][COL_TIME]:.1f}s after {dist[stopped_at]:.0f} m" 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})" + + CHECKS = { "starts": check_starts, "max_speed": check_max_speed, "brakes": check_brakes, "stops": check_stops, + "stays_stopped": check_stays_stopped, + "distance": check_distance, } @@ -254,49 +352,52 @@ def count_lines(path: Path) -> int: return 0 -def run_case(name: str, exe: Path, game_dir: Path, timespeed: float) -> bool: +def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, say) -> bool: case = json.loads((CASES_DIR / f"{name}.json").read_text(encoding="utf-8")) - print(f"\n=== {name}: {case.get('description', '')}") + say(f"\n=== {name}: {case.get('description', '')}") out = OUT_DIR / name shutil.rmtree(out, ignore_errors=True) out.mkdir(parents=True) - scenery = build_scenery(case, game_dir, name) + # 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"{case['vehicle']}.dat" + physicslog = game_dir / "physicslog" / f"{vehicle}.dat" result = run_simulator(exe, game_dir, scenery, ini, physicslog, - float(case["duration"]), float(case.get("timeout", 600))) + float(case["duration"]), float(case.get("timeout", 600)), say) - for f in ("log.txt", "errors.txt"): - if (game_dir / f).exists(): - shutil.copy2(game_dir / f, out / f) + # 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.copy2(physicslog, out / physicslog.name) + shutil.move(physicslog, out / physicslog.name) ok = True sim_t = result.rows[-1][COL_TIME] if result.rows else 0.0 - print(f" ran {result.wall_seconds:.0f}s wall, {sim_t:.1f}s of physicslog, results in {out.relative_to(REPO)}") + say(f" ran {result.wall_seconds:.0f}s wall, {sim_t:.1f}s of physicslog, results in {out.relative_to(REPO)}") if result.exit_code is not None: - print(f" FAIL simulator exited on its own with code {result.exit_code} (crash?)") + say(f" FAIL simulator exited on its own with code {result.exit_code}") ok = False - if result.timed_out: - print(f" FAIL timed out before {case['duration']}s of simulation") + if result.failure: + say(f" FAIL {result.failure}") ok = False if not result.rows: - print(f" FAIL no physicslog for '{case['vehicle']}' ({physicslog})") + if not result.failure: + say(f" FAIL no physicslog for '{vehicle}'") return False for check in case["checks"]: params = {k: v for k, v in check.items() if k != "type"} passed, detail = CHECKS[check["type"]](result.rows, **params) - print(f" {'PASS' if passed else 'FAIL'} {check['type']}: {detail}") + say(f" {'PASS' if passed else 'FAIL'} {check['type']}: {detail}") ok &= passed errors = count_lines(out / "errors.txt") if errors: - print(f" note errors.txt has {errors} line(s)") + say(f" note errors.txt has {errors} line(s)") if case.get("fail_on_errors") and errors: ok = False return ok @@ -312,6 +413,8 @@ def main() -> int: # in 20 s instead of 4 min; 1 is for watching a run ap.add_argument("--timespeed", type=float, default=20.0, help="simulation time multiplier, the simulator offers 1, 5, 20 and 60 (default 20)") + ap.add_argument("-j", "--jobs", type=int, default=1, + help="cases run at once, each in its own simulator window (default 1)") args = ap.parse_args() if not args.game_dir: @@ -321,11 +424,37 @@ def main() -> int: print(f"exe: {exe}\ngame: {game_dir}") names = args.cases or sorted(p.stem for p in CASES_DIR.glob("*.json")) - results = {n: run_case(n, exe, game_dir, args.timespeed) for n in names} + missing = [n for n in names if not (CASES_DIR / f"{n}.json").exists()] + if missing: + sys.exit("no such case: " + ", ".join(missing)) - print("\n=== summary") - for n, ok in results.items(): - print(f" {'PASS' if ok else 'FAIL'} {n}") + 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, 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, 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 From 219836b4648020de36f1ccf7dc7d7edc71b8e2a6 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 14 Oct 2026 13:55:06 +0200 Subject: [PATCH 57/90] test: track_friction case key for slippery rail scenarios --- tests/scenarios/README.md | 1 + tests/scenarios/run.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 23f793018..463086cc5 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -59,6 +59,7 @@ scenery is newer. | `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 | | `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 | | `duration` | seconds of simulation to record | diff --git a/tests/scenarios/run.py b/tests/scenarios/run.py index 419906700..32be5dfc0 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -86,6 +86,12 @@ 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 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): @@ -107,6 +113,8 @@ def build_scenery(case: dict, game_dir: Path, case_name: str, vehicle: str) -> s 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"]) 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"}) From fe9e2f77c24f71d965fd3d6c11478840c6f659e8 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 14 Oct 2026 14:11:54 +0200 Subject: [PATCH 58/90] feat(log): vehicle speed column in the physicslog The Velocity column is the wheel speed, which stays at 0 while the wheels are locked in a skid, so a vehicle sliding on locked wheels looked as if it stood still. Add the signed vehicle speed in km/h as the last column. --- vehicle/Driver.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 08f0c0d24..c5f5f51e9 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -1880,7 +1880,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 @@ -4817,7 +4818,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 From 6209e165583b841b61d4ec9a99b2d69b2e0db5c2 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 14 Oct 2026 14:11:54 +0200 Subject: [PATCH 59/90] test: slippery rail skid cases, wheelslip and vehicle speed checks - physicslog rows keep all columns (text ones as None), checks can read the wheelslip flag and the new vehicle speed column - slips: the lead vehicle's wheels slip at some point (makes sure a slippery rail case does what it's meant to) - slip_clears: once stopped, the wheels stop slipping within N seconds - holds_still: once stopped, the vehicle doesn't creep, by vehicle speed - td_red_signal_moving.scn: the red signal case starting already moving - eu07_slippery_red_signal / eu07_slippery_push_red_signal: skid on braking for the red signal with track friction 0.03; the pushing one fails without the fix for eu07/maszyna#479 (stays skidding, creeps) - sn61_td_timetable: stop hold 5 -> 2 s, the railcar arrives 5-7 s before its departure time, so 5 s made the case flaky --- tests/scenarios/README.md | 14 +++- .../cases/eu07_slippery_push_red_signal.json | 36 ++++++++++ .../cases/eu07_slippery_red_signal.json | 36 ++++++++++ tests/scenarios/cases/sn61_td_timetable.json | 2 +- .../scenarios/cases/td_red_signal_moving.scn | 6 ++ tests/scenarios/run.py | 68 ++++++++++++++++++- 6 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 tests/scenarios/cases/eu07_slippery_push_red_signal.json create mode 100644 tests/scenarios/cases/eu07_slippery_red_signal.json create mode 100644 tests/scenarios/cases/td_red_signal_moving.scn diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 463086cc5..4600a3577 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -71,9 +71,13 @@ scenery is newer. the one whose physicslog is checked. Other vehicles of the consist take names made from it, e.g. `{{vehicle}}_s`. 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`. +only in `vars.consist`. `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. -Checks (speeds in km/h, times in seconds of physicslog time, distances in metres): +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`. @@ -86,6 +90,12 @@ Checks (speeds in km/h, times in seconds of physicslog time, distances in metres - `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 (+-3), 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. 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/eu07_slippery_push_red_signal.json b/tests/scenarios/cases/eu07_slippery_push_red_signal.json new file mode 100644 index 000000000..f7ae10bb7 --- /dev/null +++ b/tests/scenarios/cases/eu07_slippery_push_red_signal.json @@ -0,0 +1,36 @@ +{ + "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", + "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..40b5944eb --- /dev/null +++ b/tests/scenarios/cases/eu07_slippery_red_signal.json @@ -0,0 +1,36 @@ +{ + "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", + "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/sn61_td_timetable.json b/tests/scenarios/cases/sn61_td_timetable.json index 1db425c80..9fa4b18d8 100644 --- a/tests/scenarios/cases/sn61_td_timetable.json +++ b/tests/scenarios/cases/sn61_td_timetable.json @@ -12,6 +12,6 @@ { "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": 0.5, "hold": 5 } + { "type": "stops", "moving_speed": 20, "stop_speed": 0.5, "hold": 2 } ] } 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/run.py b/tests/scenarios/run.py index 32be5dfc0..2cd754a06 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -43,8 +43,11 @@ 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 +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 # settings layered over the user's eu07.ini for a test run TEST_INI = """\ @@ -145,7 +148,7 @@ def build_scenery(case: dict, game_dir: Path, case_name: str, vehicle: str) -> s def read_physicslog(path: Path) -> list[list[float]]: - """Returns rows of numeric columns up to BrakePress; skips the header and partial lines.""" + """Returns rows of columns, None for the text ones; skips the header and partial lines.""" rows = [] try: lines = path.read_text(errors="replace").splitlines() @@ -156,9 +159,15 @@ def read_physicslog(path: Path) -> list[list[float]]: if len(parts) <= COL_BRAKE_PRESS: continue # line still being written try: - rows.append([float(p) for p in parts[: COL_BRAKE_PRESS + 1]]) + 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 @@ -331,7 +340,60 @@ def check_distance(rows, max=None, min=None): 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_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, From 02e29f69712d1e02e960c14eba32feb1a3245ab3 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 13:17:26 +0100 Subject: [PATCH 60/90] feat(simulation): test mode, --test runs a scenery unattended and exits with the result (eu07/maszyna#541) eu07 --test -s [--test-timeout ] runs the scenery in ghostview with the null renderer (models loaded, no textures), a hidden window, no sound, network or python, and in fixed steps of 0.03 s of simulation time per frame (three 10 ms physics steps) as fast as it can. Without -seed it uses a fixed seed, so two runs give byte-identical physicslogs. The scenery reports its result to the __test.result memory cell, which the simulator always creates: value 1 passes once every vehicle has stood still for 1 s, value 0 fails at once. The run ends with exit code 0 pass, 1 fail, 2 a train passed a signal at stop (SetVelocity 0 0, as seen in its driver's speed table; SetVelocity 0 x on automatic block doesn't count), 3 timeout (test.timeout, default 3600 s of simulation time), 4 standstill (every vehicle still for test.standstill s, default 300, with no result; a pass with test.pass_on_standstill yes), 5 error (no -s, the scenery didn't load or has no vehicles, an exception). The test.* keys go in the scenery's config block; test.fail_if_stop_overran no only logs a signal passed at danger. Outside the test mode a signal passed at danger is logged. The null renderer gets a ui backend which draws nothing, hands out the null texture and material instead of throwing, and ends the ui frame. A GL context is still made for the hidden window, without asking for a version, as parts of the ui create textures with gl calls of their own. No crash dialog or message box in test mode; the window code no longer needs a monitor. --- CMakeLists.txt | 1 + EU07.cpp | 17 +++- application/application.cpp | 82 +++++++++++++--- application/drivermode.cpp | 13 +++ application/scenarioloadermode.cpp | 10 ++ rendering/nullrenderer.cpp | 19 ++++ rendering/nullrenderer.h | 30 ++++-- simulation/simulation.cpp | 3 + simulation/simulationtest.cpp | 149 +++++++++++++++++++++++++++++ simulation/simulationtest.h | 66 +++++++++++++ utilities/Globals.cpp | 37 +++++++ utilities/Globals.h | 10 ++ utilities/Timer.cpp | 13 ++- utilities/Timer.h | 3 + vehicle/Driver.cpp | 29 +++++- vehicle/Driver.h | 2 + 16 files changed, 458 insertions(+), 26 deletions(-) create mode 100644 simulation/simulationtest.cpp create mode 100644 simulation/simulationtest.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f34ad61f..8a4e1d6ea 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" diff --git a/EU07.cpp b/EU07.cpp index e03618297..aac7cae11 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -19,6 +19,8 @@ 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 #ifdef WITHDUMPGEN @@ -148,9 +150,14 @@ int main(int argc, char *argv[]) #ifdef _WIN32 catch (std::runtime_error const &Error) { - std::string msg = "Simulator crash occured :(\n"; - msg += Error.what(); - MessageBoxA(nullptr, msg.c_str(), "Simulator crashed :(", MB_ICONERROR); + ErrorLog("Critical error: " + std::string(Error.what())); + // in test mode nobody is there to close the box, the exit code tells of the error + if (false == Global.test.enabled) + { + std::string msg = "Simulator crash occured :(\n"; + msg += Error.what(); + MessageBoxA(nullptr, msg.c_str(), "Simulator crashed :(", MB_ICONERROR); + } } #endif } @@ -158,5 +165,7 @@ int main(int argc, char *argv[]) fflush(stdout); fflush(stderr); #endif - std::_Exit(0); // skip destructors, there are ordering errors which causes segfaults + // skip destructors, there are ordering errors which causes segfaults + // a test run (--test) says its result in the exit code + std::_Exit(Global.test.enabled ? simulation::Test.exit_code() : 0); } diff --git a/application/application.cpp b/application/application.cpp index ee381f98b..37afff67f 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -378,7 +378,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) { @@ -951,14 +951,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); @@ -1195,6 +1198,19 @@ 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) + Global.test.enabled = true; + } + else if (token == "--test-timeout") + { + if (i + 1 < Argc) + { + Global.test.timeout = std::max(0.0, std::atof(Argv[++i])); + Global.test.timeout_from_command_line = true; + } + } else if (token == "-ini") { // extra settings file read on top of eu07.ini, so a run can override a few keys @@ -1210,13 +1226,45 @@ int eu07_application::init_settings(int Argc, char *Argv[]) << " [-v vehiclename]" << " [-edit [sceneryfilepath]]" << " [-seed number|word]" - << " [-ini settingsfile]" << std::endl; + << " [-ini settingsfile]" + << " [--test [--test-timeout seconds]]" << std::endl; return -1; } } if (editwithoutscenery && false == sceneryfromcommandline) Global.SceneryFile.clear(); + if (Global.test.enabled) + { + if (false == sceneryfromcommandline || Global.SceneryFile.empty()) + { + ErrorLog("test: --test needs a scenery given with -s"); + return -1; + } + // 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 + Global.network_servers.clear(); + Global.network_client.reset(); + // 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"); + } + return 0; } @@ -1290,6 +1338,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) @@ -1341,7 +1395,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) { @@ -1428,6 +1483,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..1b3a44d63 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..b84c1912f 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/rendering/nullrenderer.cpp b/rendering/nullrenderer.cpp index 332d357c7..f193ad3e4 100644 --- a/rendering/nullrenderer.cpp +++ b/rendering/nullrenderer.cpp @@ -1,6 +1,25 @@ #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; + int width, height; + 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..2a7ef0398 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 : 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/simulation/simulation.cpp b/simulation/simulation.cpp index ea2177170..59635c38c 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/simulationtest.cpp b/simulation/simulationtest.cpp new file mode 100644 index 000000000..4dbe4c24f --- /dev/null +++ b/simulation/simulationtest.cpp @@ -0,0 +1,149 @@ +/* +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 "vehicle/DynObj.h" +#include "world/Event.h" +#include "world/MemCell.h" + +namespace simulation { + +test_manager Test; + +std::string_view to_string(test_result const Result) +{ + switch (Result) + { + case test_result::pass: return "pass"; + case test_result::fail: return "fail"; + case test_result::signal_passed_at_danger: return "signal passed at danger"; + case test_result::timeout: return "timeout"; + case test_result::standstill: return "standstill"; + case test_result::error: return "error"; + } + return "unknown"; +} + +void test_manager::init_result_cell() +{ + auto *cell = new TMemCell({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::Memory.insert(cell); + simulation::Region->insert(cell); +} + +void test_manager::start() +{ + if (m_started) + return; + m_started = true; + m_time = 0.0; + m_resttime = 0.0; + 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; + } + // a pause set by the scenery or the settings would never be lifted, nobody is there to press the key + Global.iPause = 0; + WriteLog(std::format("test: started, timeout {:.0f} s, standstill {:.0f} s{}{}", Global.test.timeout, Global.test.standstill, + Global.test.pass_on_standstill ? ", standstill passes" : "", + Global.test.fail_if_stop_overran ? "" : ", signals passed at danger allowed")); +} + +void test_manager::update(double const Deltatime) +{ + if (false == m_started || m_finished) + return; + + m_time += Deltatime; + + if (auto const *cell = simulation::Memory.find(std::string(result_cell))) + { + auto const value { cell->Value1() }; + auto const reason { cell->Text().empty() ? std::string() : " (" + cell->Text() + ")" }; + if (value == 0.0) + { + finish(test_result::fail, "__test.result set to 0" + reason); + return; + } + if (value == 1.0 && false == m_passrequested) + { + m_passrequested = true; + WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for every vehicle to stop", m_time, reason)); + } + } + + auto const atrest = std::none_of(simulation::Vehicles.sequence().begin(), simulation::Vehicles.sequence().end(), + [](TDynamicObject const *Vehicle) { return std::abs(Vehicle->GetVelocity()) > 0.01; }); + m_resttime = atrest ? m_resttime + Deltatime : 0.0; + + if (m_passrequested && m_resttime >= pass_settle_time) + { + finish(test_result::pass, "__test.result set to 1 and every vehicle stopped"); + return; + } + if (Global.test.standstill > 0.0 && m_resttime >= Global.test.standstill) + { + auto const reason { std::format("every vehicle stood still for {:.0f} s", Global.test.standstill) }; + finish(Global.test.pass_on_standstill ? test_result::pass : test_result::standstill, reason); + return; + } + if (Global.test.timeout > 0.0 && m_time >= Global.test.timeout) + { + finish(test_result::timeout, std::format("no result after {:.0f} s", Global.test.timeout)); + return; + } +} + +void test_manager::signal_passed_at_danger(std::string const &Vehicle, std::string const &Signal) +{ + auto const reason { Vehicle + " passed signal " + Signal + " at danger" }; + 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_time)); + return; + } + finish(test_result::signal_passed_at_danger, reason); +} + +void test_manager::finish(test_result const Result, std::string const &Reason) +{ + if (m_finished || false == Global.test.enabled) + return; + m_finished = true; + m_result = Result; + + auto const line { std::format("test: {} (exit code {}) after {:.1f} s of simulation time: {}", to_string(Result), static_cast(Result), m_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); + + Application.queue_quit(true); +} + +} // namespace simulation diff --git a/simulation/simulationtest.h b/simulation/simulationtest.h new file mode 100644 index 000000000..c9bc5437e --- /dev/null +++ b/simulation/simulationtest.h @@ -0,0 +1,66 @@ +/* +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 + +// 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 { + +// exit codes of a test run; public, scripts and CI read them +enum class test_result : int { + pass = 0, // __test.result set to 1, then every vehicle came to rest (or test.pass_on_standstill) + fail = 1, // __test.result set to 0 + signal_passed_at_danger = 2, // a train went past a stop signal (SetVelocity 0 0), unless test.fail_if_stop_overran no + timeout = 3, // test.timeout seconds of simulation time went by without a result + standstill = 4, // every vehicle stood still for test.standstill seconds without a result + error = 5, // the run could not start (no -s, the scenery failed to load) or the simulator threw +}; + +std::string_view to_string(test_result Result); + +class test_manager { + +public: + // name of the memory cell a scenery reports its result to + static constexpr std::string_view result_cell { "__test.result" }; + // how long everything has to stand before a requested pass is granted [s] + static constexpr double pass_settle_time { 1.0 }; + // 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(); + // 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 const &Vehicle, std::string const &Signal); + // ends the test with given result; the first call wins + void finish(test_result Result, std::string const &Reason); + + 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 { return static_cast(m_finished ? m_result : test_result::error); } + +private: + bool m_started { false }; + bool m_finished { false }; + test_result m_result { test_result::error }; + bool m_passrequested { false }; + double m_time { 0.0 }; // simulation time since the start [s] + double m_resttime { 0.0 }; // how long every vehicle has been standing [s] +}; + +extern test_manager Test; + +} // namespace simulation diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index b4f2e14be..e36223174 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -456,6 +456,40 @@ bool global_settings::ConfigParseInput(cParser& Parser, const std::string& token return false; } +bool global_settings::ConfigParseTest(cParser& Parser, const std::string& 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.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; + } + + return false; +} + bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& token) { if (token == "async.trainThreads") @@ -1316,6 +1350,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); diff --git a/utilities/Globals.h b/utilities/Globals.h index ad216cd96..8fa04e9ce 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -86,6 +86,15 @@ 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] every vehicle 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 timeout_from_command_line{ false }; // --test-timeout given, the scenery doesn't override it + } 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 +386,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, const std::string& 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/Timer.cpp b/utilities/Timer.cpp index 0b877f2b4..388330bbe 100644 --- a/utilities/Timer.cpp +++ b/utilities/Timer.cpp @@ -26,6 +26,7 @@ double fRenderTime{ 0.0 }; double fSoundTimer{ 0.0 }; double fSinceStart{ 0.0 }; double override_delta = -1.0f; +double fixed_step = 0.0; double GetTime() { @@ -53,6 +54,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 +82,15 @@ void UpdateTimers(bool pause) fr = 1000000000; #endif DeltaRenderTime = double(count - oldCount) / double(fr); + if (fixed_step > 0.0) + { + // the frame lasts exactly as long as said, however long it took to compute; the time speed doesn't apply + DeltaRenderTime = fixed_step; + } fRenderTime += DeltaRenderTime; if (!pause) { - DeltaTime = Global.fTimeSpeed * DeltaRenderTime; + DeltaTime = (fixed_step > 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 c5f5f51e9..5f1bde20c 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,7 +768,12 @@ void TController::TableCheck(double fDistance) { // aktualizacja rekordów z wyjątkiem ostatniego if (sSpeedTable[i].iFlags & spEnabled) // jeśli pozycja istotna { - if (sSpeedTable[i].Update()) + auto const waspassed { TestFlag( sSpeedTable[ i ].iFlags, spElapsed ) }; + auto const switchchanged { sSpeedTable[ i ].Update() }; + if( false == waspassed ) { + CheckSignalPassedAtDanger( sSpeedTable[ i ] ); + } + if (switchchanged) { if( Global.iWriteLogEnabled & 8 ) { WriteLog( "Speed table for " + OwnerName() + " detected switch change at " + sSpeedTable[ i ].trTrack->name() + " (generating fresh trace)" ); @@ -816,7 +822,12 @@ void TController::TableCheck(double fDistance) } } } - sSpeedTable[iLast].Update(); // aktualizacja ostatniego + // aktualizacja ostatniego; a signal at stop often is the last, the scan doesn't go past it + auto const lastwaspassed { TestFlag( sSpeedTable[ iLast ].iFlags, spElapsed ) }; + sSpeedTable[iLast].Update(); + if( false == lastwaspassed ) { + CheckSignalPassedAtDanger( sSpeedTable[ iLast ] ); + } // WriteLog("TableCheck: Upate last track. Dist=" + AnsiString(sSpeedTable[iLast].fDist)); if( sSpeedTable[ iLast ].fDist < fDistance ) { TableTraceRoute( fDistance, pVehicles[ end::rear ] ); // doskanowanie dalszego odcinka @@ -827,6 +838,20 @@ void TController::TableCheck(double fDistance) } }; +void TController::CheckSignalPassedAtDanger( TSpeedPos const &Point ) const +{ + if( false == TestFlag( Point.iFlags, spElapsed ) + || false == TestFlag( Point.iFlags, spEvent ) + || false == TestFlag( Point.iFlags, spSemaphor ) + || true == TestFlag( Point.iFlags, spStopOnSBL ) // SetVelocity 0 x on automatic block: may go on after a stop + || Point.fVelNext != 0.0 + || Point.evEvent == nullptr + || ( mvOccupied->CategoryFlag & 1 ) == 0 ) { // railway only + return; + } + 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 TCommandType TController::TableUpdate(double &fVelDes, double &fDist, double &fNext, double &fAcc) diff --git a/vehicle/Driver.h b/vehicle/Driver.h index f88e115ac..8c12087f9 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -519,6 +519,8 @@ private: bool TableNotFound( basic_event const *Event, double const Distance ) const; void TableTraceRoute( double fDistance, TDynamicObject *pVehicle ); void TableCheck( double fDistance ); + // reports the train going past a signal showing stop (SetVelocity 0 0); called once the head passed Point + void CheckSignalPassedAtDanger( TSpeedPos const &Point ) 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 ); From 6a538d202b6437e27e189e7c63a0fa413da65167 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 13:17:27 +0100 Subject: [PATCH 61/90] test: scenario tests run in the simulator's test mode and check its exit code run.py starts eu07 with --test (and --test-timeout for cases that record a fixed time) and compares the result from the exit code with the case's expect key, before the physicslog checks. The 14 cases take about 30 s with -j 4 instead of minutes. --watch keeps the old windowed run. New case keys: expect, test_config (lines of a config block), track_events (lines added to a track of the base scenery, e.g. event0 test_pass). New cases show each end of the test mode: test_mode_pass, _signal_passed_at_danger, _timeout and _standstill. The slippery rail cases slide past the red signal, which now ends a test, so they set test.fail_if_stop_overran no. README documents the test mode for scenery authors. --- tests/scenarios/README.md | 130 ++++++++++++++---- .../cases/eu07_slippery_push_red_signal.json | 3 + .../cases/eu07_slippery_red_signal.json | 3 + tests/scenarios/cases/td_red_signal_pass.scn | 8 ++ tests/scenarios/cases/test_mode_pass.json | 31 +++++ .../test_mode_signal_passed_at_danger.json | 22 +++ .../scenarios/cases/test_mode_standstill.json | 26 ++++ tests/scenarios/cases/test_mode_timeout.json | 21 +++ tests/scenarios/run.py | 122 ++++++++++++---- 9 files changed, 315 insertions(+), 51 deletions(-) create mode 100644 tests/scenarios/cases/td_red_signal_pass.scn create mode 100644 tests/scenarios/cases/test_mode_pass.json create mode 100644 tests/scenarios/cases/test_mode_signal_passed_at_danger.json create mode 100644 tests/scenarios/cases/test_mode_standstill.json create mode 100644 tests/scenarios/cases/test_mode_timeout.json diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 4600a3577..1ebd84c75 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -1,49 +1,121 @@ # Scenario tests -End-to-end checks that run the real simulator on a scenery and look at what a vehicle -did: did it start, did it keep to the speed limit, did it brake and stop. +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 ] [-seed ] [-ini ] +``` + +With `--test` the simulator: + +- needs `-s`; `-v` is ignored and every train is driven by AI (ghostview), +- 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, +- 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 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 every vehicle stood still for 1 s, so a train still moving towards a signal at stop has to stop first. Also the standstill end with `test.pass_on_standstill yes` | +| 1 | fail | `__test.result` was set to 0; ends at once | +| 2 | 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` | +| 3 | timeout | `test.timeout` seconds of simulation time went by (default 3600) | +| 4 | standstill | every vehicle 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 | +| 5 | 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 limits go in a `config` block of the scenery: + +``` +config +test.timeout 600 +test.standstill 120 +test.pass_on_standstill no +test.fail_if_stop_overran yes +endconfig +``` + +`--test-timeout` on the command line wins over `test.timeout` of the scenery. + +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 "E:/Program Files (x86)/Steam/steamapps/common/MaSzyna" -j 4 +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 `-ini` command line option. + `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 in its own simulator window. Each case prints its - results when it is done. All 8 cases take about a minute with `-j 4`. -- `--timespeed N` sets the simulation time multiplier, default 20: a 4-minute case runs in - about 20 s. Physics still steps finely (the physicslog keeps its 0.05 s rows) and the speed - profile at 20 and 60 matched a real-time run within a few km/h and seconds. Use - `--timespeed 1` to watch a run. +- `-j N` runs N cases at once. Each case prints its results when it is done. All 14 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`, -the vehicle's physicslog) are moved to `tests/scenarios/out//`. +`stdout.txt`, the vehicle's physicslog) 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, and the case's `.scn` - fragment added: its trainsets after `FirstInit`, everything else (event launchers standing - in for keys the scenery expects a player to press, extra events) before it, -2. starts the simulator with `-s autotest_.scn -v ghostview -ini out//autotest.ini`; - ghostview leaves every train to the AI, and the extra ini turns on `physicslog`, runs in a - 1280x720 window, does not pause when the window loses focus, and writes the logs to - `logs/log (autotest_) .txt` rather than the shared `log.txt` (`multiplelogs`), -3. waits until `physicslog/autotest_.dat` covers `duration` seconds, then closes the - simulator. It gives up early when the log reports `Bad vehicle` / `Bad trainset`, or when - the scenario has loaded and no physicslog appeared, -4. runs the checks on the physicslog. + 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`, + 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`, when the scenario has loaded and no physicslog appeared, or + after the case's `timeout` of wall-clock time, +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` @@ -60,9 +132,12 @@ scenery is newer. | `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"]` | | `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 | -| `duration` | seconds of simulation to record | +| `expect` | the result the test mode should end with: `pass`, `fail`, `signal_passed_at_danger`, `timeout`, `standstill` or `error`. 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 | | `fail_on_errors` | fail when `errors.txt` is not empty (default `false`) | | `checks` | list of checks, below | @@ -72,7 +147,14 @@ the one whose physicslog is checked. Other vehicles of the consist take names ma e.g. `{{vehicle}}_s`. 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`. `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. +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 2), `test_mode_timeout` (exit 3, from +`test.timeout`) and `test_mode_standstill` (exit 4, from `test.standstill`). 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 diff --git a/tests/scenarios/cases/eu07_slippery_push_red_signal.json b/tests/scenarios/cases/eu07_slippery_push_red_signal.json index f7ae10bb7..64d6ba884 100644 --- a/tests/scenarios/cases/eu07_slippery_push_red_signal.json +++ b/tests/scenarios/cases/eu07_slippery_push_red_signal.json @@ -4,6 +4,9 @@ "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": [ diff --git a/tests/scenarios/cases/eu07_slippery_red_signal.json b/tests/scenarios/cases/eu07_slippery_red_signal.json index 40b5944eb..c2f60f22d 100644 --- a/tests/scenarios/cases/eu07_slippery_red_signal.json +++ b/tests/scenarios/cases/eu07_slippery_red_signal.json @@ -4,6 +4,9 @@ "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": [ 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/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_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..0d96585ee --- /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 every vehicle 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_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/run.py b/tests/scenarios/run.py index 2cd754a06..770069d12 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -2,16 +2,19 @@ """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), and checks on the vehicle's -physics log. The runner: +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 with that scenery in ghostview (every train driven by AI) and an extra - settings file (-ini) that turns on physicslog and runs in a window, - 3. waits until the vehicle's physicslog covers `duration` seconds of simulation, - then closes the simulator, - 4. evaluates the checks on the log and prints PASS/FAIL per check. + 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. @@ -49,7 +52,18 @@ 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 -# settings layered over the user's eu07.ini for a test run +# exit codes of the simulator's test mode (eu07 --test), see simulation/simulationtest.h +EXIT_RESULTS = { + 0: "pass", + 1: "fail", + 2: "signal_passed_at_danger", + 3: "timeout", + 4: "standstill", + 5: "error", +} + +# settings layered over the user's eu07.ini for a test run; 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 @@ -89,6 +103,17 @@ 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.]+", @@ -118,6 +143,8 @@ def build_scenery(case: dict, game_dir: Path, case_name: str, vehicle: str) -> s 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"}) @@ -126,6 +153,9 @@ def build_scenery(case: dict, game_dir: Path, case_name: str, vehicle: str) -> s # 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) + if case.get("test_config"): + # settings of the test mode (test.timeout and so on), read like any other config block + before = "config\n" + "".join(line + "\n" for line in case["test_config"]) + "endconfig\n" + before header = f"\n// ---- autotest: {case_name} ----\n" firstinit = re.search(r"(?im)^\s*firstinit\b", text) if firstinit: @@ -200,20 +230,30 @@ STARTUP_TIMEOUT = 120.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) + 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, timeout: float, say) -> RunResult: + 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) - cmd = [str(exe), "-s", scenery, "-v", "ghostview", "-ini", str(ini)] + if watch: + cmd = [str(exe), "-s", scenery, "-v", "ghostview", "-ini", str(ini)] + else: + cmd = [str(exe), "--test", "-s", scenery, "-ini", str(ini)] + 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() - proc = subprocess.Popen(cmd, cwd=game_dir) + # 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: @@ -229,7 +269,7 @@ def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog if proc.poll() is not None: result.exit_code = proc.returncode break - if sim_t >= duration: + if watch and sim_t >= duration: break if wall > timeout: result.failure = f"timed out after {timeout:.0f}s before {duration}s of simulation" @@ -252,6 +292,10 @@ def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog 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 @@ -422,7 +466,14 @@ def count_lines(path: Path) -> int: return 0 -def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, say) -> bool: +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 @@ -436,8 +487,16 @@ def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, say) -> boo 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 EXIT_RESULTS.values(): + sys.exit(f"{name}: expect must be one of {', '.join(EXIT_RESULTS.values())}") + 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, - float(case["duration"]), float(case.get("timeout", 600)), say) + 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(): @@ -448,18 +507,26 @@ def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, say) -> boo 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 result.exit_code is not None: - say(f" FAIL simulator exited on its own with code {result.exit_code}") - ok = False + 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 + passed = got == expect + say(f" {'PASS' if passed else 'FAIL'} exit code {describe_exit(result.exit_code)}, expected {expect}") + ok &= passed if result.failure: say(f" FAIL {result.failure}") ok = False - if not result.rows: + 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["checks"]: + 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}") @@ -479,12 +546,13 @@ def main() -> int: 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)") - # 20 gave the same speed profile as 1 within a few km/h and seconds on eu07_td_timetable, - # in 20 s instead of 4 min; 1 is for watching a run - ap.add_argument("--timespeed", type=float, default=20.0, - help="simulation time multiplier, the simulator offers 1, 5, 20 and 60 (default 20)") + 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 its own simulator window (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: @@ -504,7 +572,7 @@ def main() -> int: def say(line, progress=False): print(line, flush=True) for n in names: - results[n] = run_case(n, exe, game_dir, args.timespeed, say) + 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): @@ -512,7 +580,7 @@ def main() -> int: def say(line, progress=False): if not progress: lines.append(line) - ok = run_case(n, exe, game_dir, args.timespeed, say) + 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: From 441caeed641b1a2257b382916f5dd6431b3369bc Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 17:12:11 +0100 Subject: [PATCH 62/90] refactor(simulation): tidy the test mode code The test manager is reached through simulation::test() instead of a global, the result cell is owned by a unique_ptr until the memory table takes it, the cell value is compared with a margin, strings are built with std::format and the --test settings moved to apply_test_mode_settings(). The driver's signal check is folded into UpdateSpeedPosition(), which updates a speed table entry and reports a signal at stop the head of the train just passed. The test.* keys are listed with the other settings at the start of the log. --- EU07.cpp | 5 +-- application/application.cpp | 55 ++++++++++++++++++------------ application/drivermode.cpp | 4 +-- application/scenarioloadermode.cpp | 4 +-- rendering/nullrenderer.cpp | 5 +-- simulation/simulation.cpp | 2 +- simulation/simulationtest.cpp | 47 ++++++++++++++++++------- simulation/simulationtest.h | 8 +++-- utilities/Globals.cpp | 4 +++ utilities/Timer.cpp | 21 +++++++++--- vehicle/Driver.cpp | 46 +++++++++++++------------ vehicle/Driver.h | 5 +-- 12 files changed, 131 insertions(+), 75 deletions(-) diff --git a/EU07.cpp b/EU07.cpp index aac7cae11..ec3e972e7 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -23,6 +23,7 @@ Stele, firleju, szociu, hunter, ZiomalCl, OLI_EU and others #include "utilities/Globals.h" #include "utilities/Logs.h" #include +#include #ifdef WITHDUMPGEN #ifdef _WIN32 #include @@ -150,7 +151,7 @@ int main(int argc, char *argv[]) #ifdef _WIN32 catch (std::runtime_error const &Error) { - ErrorLog("Critical error: " + std::string(Error.what())); + ErrorLog(std::format("Critical error: {}", Error.what())); // in test mode nobody is there to close the box, the exit code tells of the error if (false == Global.test.enabled) { @@ -167,5 +168,5 @@ int main(int argc, char *argv[]) #endif // skip destructors, there are ordering errors which causes segfaults // a test run (--test) says its result in the exit code - std::_Exit(Global.test.enabled ? simulation::Test.exit_code() : 0); + std::_Exit(Global.test.enabled ? simulation::test().exit_code() : 0); } diff --git a/application/application.cpp b/application/application.cpp index 37afff67f..439c01d97 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -1137,6 +1137,38 @@ void eu07_application::init_files() } namespace fs = std::filesystem; +namespace +{ + +// the settings of the test mode (--test), over whatever the settings files say +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 + Global.network_servers.clear(); + Global.network_client.reset(); + // 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"); +} + +} // namespace + int eu07_application::init_settings(int Argc, char *Argv[]) { Global.asVersion = VERSION_INFO; @@ -1241,28 +1273,7 @@ int eu07_application::init_settings(int Argc, char *Argv[]) ErrorLog("test: --test needs a scenery given with -s"); return -1; } - // 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 - Global.network_servers.clear(); - Global.network_client.reset(); - // 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"); + apply_test_mode_settings(); } return 0; diff --git a/application/drivermode.cpp b/application/drivermode.cpp index 1b3a44d63..deb906a4a 100644 --- a/application/drivermode.cpp +++ b/application/drivermode.cpp @@ -309,7 +309,7 @@ bool driver_mode::update() if (Global.test.enabled && simulation::is_ready) { - simulation::Test.update(deltatime); + simulation::test().update(deltatime); } } @@ -488,7 +488,7 @@ void driver_mode::enter() { // 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(); + simulation::test().start(); } set_picking(!Global.captureonstart); diff --git a/application/scenarioloadermode.cpp b/application/scenarioloadermode.cpp index b84c1912f..20fa46d3d 100644 --- a/application/scenarioloadermode.cpp +++ b/application/scenarioloadermode.cpp @@ -60,7 +60,7 @@ bool scenarioloader_mode::update() { 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"); + simulation::test().finish(simulation::test_result::error, "scenario couldn't be opened"); return true; } Global.SceneryFile.clear(); @@ -80,7 +80,7 @@ bool scenarioloader_mode::update() { 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" ); + simulation::test().finish( simulation::test_result::error, "scenario loading failed" ); return true; } Global.SceneryFile.clear(); diff --git a/rendering/nullrenderer.cpp b/rendering/nullrenderer.cpp index f193ad3e4..bc466e977 100644 --- a/rendering/nullrenderer.cpp +++ b/rendering/nullrenderer.cpp @@ -7,8 +7,9 @@ void null_imgui_renderer::BeginFrame() { auto &io { ImGui::GetIO() }; if( false == io.Fonts->IsBuilt() ) { - unsigned char *pixels; - int width, height; + unsigned char *pixels { nullptr }; + int width { 0 }; + int height { 0 }; io.Fonts->GetTexDataAsAlpha8( &pixels, &width, &height ); } } diff --git a/simulation/simulation.cpp b/simulation/simulation.cpp index 59635c38c..d10d555a3 100644 --- a/simulation/simulation.cpp +++ b/simulation/simulation.cpp @@ -111,7 +111,7 @@ state_manager::init_scripting_interface() { simulation::Region->insert( memorycell ); } // the cell a test scenery reports its result to - simulation::Test.init_result_cell(); + simulation::test().init_result_cell(); } // legacy method, calculates changes in simulation state over specified time diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 4dbe4c24f..439f295fe 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -19,9 +19,29 @@ http://mozilla.org/MPL/2.0/. #include "world/Event.h" #include "world/MemCell.h" +#include +#include +#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; +} + +} // namespace + namespace simulation { -test_manager Test; +test_manager &test() +{ + static test_manager instance; + return instance; +} std::string_view to_string(test_result const Result) { @@ -39,11 +59,12 @@ std::string_view to_string(test_result const Result) void test_manager::init_result_cell() { - auto *cell = new TMemCell({0, -1, std::string(result_cell), "memcell"}); + 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::Memory.insert(cell); - simulation::Region->insert(cell); + simulation::Region->insert(cell.get()); + // the table owns its cells + simulation::Memory.insert(cell.release()); } void test_manager::start() @@ -76,21 +97,21 @@ void test_manager::update(double const Deltatime) if (auto const *cell = simulation::Memory.find(std::string(result_cell))) { auto const value { cell->Value1() }; - auto const reason { cell->Text().empty() ? std::string() : " (" + cell->Text() + ")" }; - if (value == 0.0) + auto const reason { cell->Text().empty() ? std::string() : std::format(" ({})", cell->Text()) }; + if (holds(value, 0.0)) { - finish(test_result::fail, "__test.result set to 0" + reason); + finish(test_result::fail, std::format("__test.result set to 0{}", reason)); return; } - if (value == 1.0 && false == m_passrequested) + if (holds(value, 1.0) && false == m_passrequested) { m_passrequested = true; WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for every vehicle to stop", m_time, reason)); } } - auto const atrest = std::none_of(simulation::Vehicles.sequence().begin(), simulation::Vehicles.sequence().end(), - [](TDynamicObject const *Vehicle) { return std::abs(Vehicle->GetVelocity()) > 0.01; }); + auto const atrest { std::ranges::none_of(simulation::Vehicles.sequence(), + [](TDynamicObject const *Vehicle) { return std::abs(Vehicle->GetVelocity()) > 0.01; }) }; m_resttime = atrest ? m_resttime + Deltatime : 0.0; if (m_passrequested && m_resttime >= pass_settle_time) @@ -111,9 +132,9 @@ void test_manager::update(double const Deltatime) } } -void test_manager::signal_passed_at_danger(std::string const &Vehicle, std::string const &Signal) +void test_manager::signal_passed_at_danger(std::string_view const Vehicle, std::string_view const Signal) { - auto const reason { Vehicle + " passed signal " + Signal + " at danger" }; + auto const reason { std::format("{} passed signal {} at danger", Vehicle, Signal) }; if (false == Global.test.enabled) { WriteLog(reason, logtype::generic); @@ -127,7 +148,7 @@ void test_manager::signal_passed_at_danger(std::string const &Vehicle, std::stri finish(test_result::signal_passed_at_danger, reason); } -void test_manager::finish(test_result const Result, std::string const &Reason) +void test_manager::finish(test_result const Result, std::string_view const Reason) { if (m_finished || false == Global.test.enabled) return; diff --git a/simulation/simulationtest.h b/simulation/simulationtest.h index c9bc5437e..f47f408b7 100644 --- a/simulation/simulationtest.h +++ b/simulation/simulationtest.h @@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/. #pragma once #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 @@ -44,9 +45,9 @@ public: // 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 const &Vehicle, std::string const &Signal); + void signal_passed_at_danger(std::string_view Vehicle, std::string_view Signal); // ends the test with given result; the first call wins - void finish(test_result Result, std::string const &Reason); + void finish(test_result Result, std::string_view Reason); bool finished() const { return m_finished; } // exit code of the process: the result, or error while the test hasn't finished @@ -61,6 +62,7 @@ private: double m_resttime { 0.0 }; // how long every vehicle has been standing [s] }; -extern test_manager Test; +// the test of the run +test_manager &test(); } // namespace simulation diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index e36223174..5cdb3ef25 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -1779,6 +1779,10 @@ 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 ); + 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, "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/Timer.cpp b/utilities/Timer.cpp index 388330bbe..eacd2ba0a 100644 --- a/utilities/Timer.cpp +++ b/utilities/Timer.cpp @@ -26,7 +26,17 @@ double fRenderTime{ 0.0 }; double fSoundTimer{ 0.0 }; double fSinceStart{ 0.0 }; double override_delta = -1.0f; -double fixed_step = 0.0; + +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() { @@ -56,7 +66,7 @@ void set_delta_override(double t) void set_fixed_step(double t) { - fixed_step = t; + fixed_step() = t; } void ResetTimers() @@ -82,15 +92,16 @@ void UpdateTimers(bool pause) fr = 1000000000; #endif DeltaRenderTime = double(count - oldCount) / double(fr); - if (fixed_step > 0.0) + 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 = fixed_step; + DeltaRenderTime = fixedstep; } fRenderTime += DeltaRenderTime; if (!pause) { - DeltaTime = (fixed_step > 0.0 ? 1.0 : Global.fTimeSpeed) * DeltaRenderTime; + DeltaTime = (fixedstep > 0.0 ? 1.0 : Global.fTimeSpeed) * DeltaRenderTime; fSoundTimer += DeltaTime; if (fSoundTimer > 0.1) fSoundTimer = 0.0; diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 5f1bde20c..c0614923c 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -768,12 +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 waspassed { TestFlag( sSpeedTable[ i ].iFlags, spElapsed ) }; - auto const switchchanged { sSpeedTable[ i ].Update() }; - if( false == waspassed ) { - CheckSignalPassedAtDanger( sSpeedTable[ i ] ); - } - if (switchchanged) + if (UpdateSpeedPosition(sSpeedTable[i])) { if( Global.iWriteLogEnabled & 8 ) { WriteLog( "Speed table for " + OwnerName() + " detected switch change at " + sSpeedTable[ i ].trTrack->name() + " (generating fresh trace)" ); @@ -823,11 +818,7 @@ void TController::TableCheck(double fDistance) } } // aktualizacja ostatniego; a signal at stop often is the last, the scan doesn't go past it - auto const lastwaspassed { TestFlag( sSpeedTable[ iLast ].iFlags, spElapsed ) }; - sSpeedTable[iLast].Update(); - if( false == lastwaspassed ) { - CheckSignalPassedAtDanger( sSpeedTable[ iLast ] ); - } + UpdateSpeedPosition(sSpeedTable[iLast]); // WriteLog("TableCheck: Upate last track. Dist=" + AnsiString(sSpeedTable[iLast].fDist)); if( sSpeedTable[ iLast ].fDist < fDistance ) { TableTraceRoute( fDistance, pVehicles[ end::rear ] ); // doskanowanie dalszego odcinka @@ -838,18 +829,31 @@ void TController::TableCheck(double fDistance) } }; -void TController::CheckSignalPassedAtDanger( TSpeedPos const &Point ) const +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 ) { - if( false == TestFlag( Point.iFlags, spElapsed ) - || false == TestFlag( Point.iFlags, spEvent ) - || false == TestFlag( Point.iFlags, spSemaphor ) - || true == TestFlag( Point.iFlags, spStopOnSBL ) // SetVelocity 0 x on automatic block: may go on after a stop - || Point.fVelNext != 0.0 - || Point.evEvent == nullptr - || ( mvOccupied->CategoryFlag & 1 ) == 0 ) { // railway only - return; + return TestFlag( Point.iFlags, spEvent ) + && TestFlag( Point.iFlags, spSemaphor ) + && false == TestFlag( Point.iFlags, spStopOnSBL ) + && std::abs( Point.fVelNext ) <= 0.0 + && Point.evEvent != nullptr; +} + +} // namespace + +bool TController::UpdateSpeedPosition( TSpeedPos &Point ) const +{ + auto const passedbefore { TestFlag( Point.iFlags, spElapsed ) }; + auto const switchchanged { Point.Update() }; + 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 ); } - simulation::Test.signal_passed_at_danger( OwnerName(), Point.evEvent->m_name ); + return switchchanged; } auto const passengerstopmaxdistance { 400.0 }; // maximum allowed distance between passenger stop point and consist head diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 8c12087f9..8f2015f2f 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -519,8 +519,9 @@ private: bool TableNotFound( basic_event const *Event, double const Distance ) const; void TableTraceRoute( double fDistance, TDynamicObject *pVehicle ); void TableCheck( double fDistance ); - // reports the train going past a signal showing stop (SetVelocity 0 0); called once the head passed Point - void CheckSignalPassedAtDanger( TSpeedPos const &Point ) const; + // updates a position of the speed table, returns true when a switch on it changed (TSpeedPos::Update). + // reports the head of the train going past it, if it's a signal showing stop + bool UpdateSpeedPosition( TSpeedPos &Point ) 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 ); From dd019d88ba4f45c711e37abe00782c27207ed5be Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 17:38:37 +0100 Subject: [PATCH 63/90] feat(simulation): the test mode takes no input from outside (eu07/maszyna#541) With --test the gamepad, mouse picking, UART, ZeroMQ and motion telemetry are off, the hidden window gets no key, mouse, scroll or char callbacks, and the window procedure that takes WM_COPYDATA commands from other programs isn't installed, so nothing but the scenery drives a test run. --- application/application.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/application/application.cpp b/application/application.cpp index 439c01d97..eaf60c7cd 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -1159,9 +1159,16 @@ void apply_test_mode_settings() Global.minframetime = std::chrono::duration(0.0f); Global.extra_viewports.clear(); Global.headtrack_conf.joy.clear(); - // a test runs on its own + // 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 // 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"); @@ -1431,7 +1438,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 @@ -1445,13 +1454,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() From 6ccf4b14bf8a02fb7835dd20837db54ed5a5edcb Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 17:38:38 +0100 Subject: [PATCH 64/90] feat(simulation): test.no_movement_before_pass, a pass that doesn't wait for a stop The config key from eu07/maszyna#541. By default (yes) a pass reported to __test.result waits until every vehicle has stood still for 1 s; with no it ends the test at once. The key is listed with the other test.* settings in the log and in the test start line. Scenario tests: new case test_mode_pass_while_moving reports the pass from event2 of tor1k as the train runs onto it and checks the run ended with the train still moving (new check ends_moving). The README lists the test.* keys with their defaults and the input the test mode ignores. --- simulation/simulationtest.cpp | 10 +++++-- tests/scenarios/README.md | 18 +++++++++++- .../cases/test_mode_pass_while_moving.json | 28 +++++++++++++++++++ tests/scenarios/run.py | 10 +++++++ utilities/Globals.cpp | 7 +++++ utilities/Globals.h | 1 + 6 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 tests/scenarios/cases/test_mode_pass_while_moving.json diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 439f295fe..8d57505ff 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -82,9 +82,10 @@ void test_manager::start() } // a pause set by the scenery or the settings would never be lifted, nobody is there to press the key Global.iPause = 0; - WriteLog(std::format("test: started, timeout {:.0f} s, standstill {:.0f} s{}{}", Global.test.timeout, Global.test.standstill, + WriteLog(std::format("test: started, timeout {:.0f} s, standstill {:.0f} s{}{}{}", Global.test.timeout, Global.test.standstill, Global.test.pass_on_standstill ? ", standstill passes" : "", - Global.test.fail_if_stop_overran ? "" : ", signals passed at danger allowed")); + 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) @@ -103,6 +104,11 @@ void test_manager::update(double const Deltatime) finish(test_result::fail, std::format("__test.result set to 0{}", reason)); return; } + if (holds(value, 1.0) && false == Global.test.no_movement_before_pass) + { + finish(test_result::pass, std::format("__test.result set to 1{}", reason)); + return; + } if (holds(value, 1.0) && false == m_passrequested) { m_passrequested = true; diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 1ebd84c75..b57fc4a05 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -23,6 +23,9 @@ With `--test` the simulator: 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, - 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 @@ -48,7 +51,7 @@ The test ends with the first of these: | exit code | result | when | | --- | --- | --- | -| 0 | pass | `__test.result` was set to 1 and then every vehicle stood still for 1 s, so a train still moving towards a signal at stop has to stop first. Also the standstill end with `test.pass_on_standstill yes` | +| 0 | pass | `__test.result` was set to 1 and then every vehicle 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` | | 1 | fail | `__test.result` was set to 0; ends at once | | 2 | 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` | | 3 | timeout | `test.timeout` seconds of simulation time went by (default 3600) | @@ -65,9 +68,18 @@ test.timeout 600 test.standstill 120 test.pass_on_standstill no test.fail_if_stop_overran yes +test.no_movement_before_pass yes endconfig ``` +| key | default | meaning | +| --- | --- | --- | +| `test.timeout` | 3600 | seconds of simulation time before the test ends as `timeout`; 0 = never | +| `test.standstill` | 300 | seconds with every vehicle 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 every vehicle stands still; with `no` it ends the test at once | + `--test-timeout` on the command line wins over `test.timeout` of the scenery. A signal is passed at danger when the head of a train goes past a signal which its driver @@ -155,6 +167,8 @@ 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 2), `test_mode_timeout` (exit 3, from `test.timeout`) and `test_mode_standstill` (exit 4, from `test.standstill`). +`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 @@ -178,6 +192,8 @@ the row instead, logged by builds from this branch on. 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/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/run.py b/tests/scenarios/run.py index 770069d12..4c3383081 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -416,6 +416,15 @@ def check_holds_still(rows, moving_speed, stop_speed, max_distance): 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).""" @@ -444,6 +453,7 @@ CHECKS = { "stops": check_stops, "stays_stopped": check_stays_stopped, "distance": check_distance, + "ends_moving": check_ends_moving, } diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 5cdb3ef25..2516da29e 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -487,6 +487,12 @@ bool global_settings::ConfigParseTest(cParser& Parser, const std::string& token) return true; } + if (token == "test.no_movement_before_pass") + { + ParseOne(Parser, test.no_movement_before_pass); + return true; + } + return false; } @@ -1783,6 +1789,7 @@ global_settings::export_as_text( std::ostream &Output ) const { 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 ); 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 8fa04e9ce..a6a9e1226 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -93,6 +93,7 @@ struct global_settings { double standstill{ 300.0 }; // [s] every vehicle 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 every vehicle stands still; with false it ends the test at once bool timeout_from_command_line{ false }; // --test-timeout given, the scenery doesn't override it } test; bool editor_tracks{ false }; // F3 track workspace: draw and pick tracks without the scenery draw-range cap From 8d1d9eec9b2f2e0ae15dc20f21c943e7f9693c89 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 18:10:47 +0100 Subject: [PATCH 65/90] AI braking: smaller steps, earlier, while approaching a stop or a lower speed Changes to how the AI works the train brake on the way to a lower speed or a stop, so that it uses few, early applications instead of a late deep one: - Approaching a lower speed, the deceleration may fall short of the demand by only half of a step (fBrake_a1) before the brake is applied harder. With a full step the demand crept up meanwhile and a long G train got its next step many seconds too late. - The shortfall is checked every 3 s. If it keeps growing at a handle left alone for longer than the brake takes to respond, the AI adds a small step at once instead of waiting until the margin calls for a deep one. A shortfall that shrinks on its own (brake still filling, blocks gripping harder at lower speed) does not trigger it. - Releasing, a position short of the first one counts as giving the minimum stage of the distributor, not less. - Holding the speed on a descent, the predicted excess speed is taken off over the time the brake takes to respond, not at once, so a slow brake does not get a deep application that then takes as long to release. --- vehicle/Driver.cpp | 69 ++++++++++++++++++++++++++++++++++++++++------ vehicle/Driver.h | 5 ++++ 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 8d2a3fac6..ea78e655c 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2928,6 +2928,9 @@ bool TController::ReleaseEngine() { return OK; } +// defined further down, with the braking demand it is used for +static double brake_response_horizon( TMoverParameters const *Vehicle, TBrakeSystem const Brakesystem, double const Acceleration, double const Brakea0 ); + bool TController::IncBrake() { // zwiększenie hamowania auto OK { false }; @@ -3040,7 +3043,7 @@ bool TController::IncBrake() } double deltaAcc = -AccDesired*BrakeAccFactor() - (fBrake_a0[0] + 4.0 * (/*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition - 1 - pos_corr)*fBrake_a1[0]); - if( deltaAcc > fBrake_a1[0]) + if( deltaAcc > brake_increase_margin() ) { if( /*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition < 0.1 ) { OK = /*mvOccupied->*/BrakeLevelAdd( BrakingInitialLevel ); //GBH @@ -3069,10 +3072,13 @@ bool TController::IncBrake() } else if( BrakeCtrlPosition > 0.0 && AccDesired < 0.0 // only while braking is asked for, not when called for a train slipping back while starting - && fBrakeHeldTime > ( - mvOccupied->BrakeDelayFlag > bdelay_G ? - mvOccupied->BrakeDelay[ 1 ] : - mvOccupied->BrakeDelay[ 3 ] ) ) { + && ( fBrakeHeldTime > ( + mvOccupied->BrakeDelayFlag > bdelay_G ? + mvOccupied->BrakeDelay[ 1 ] : + mvOccupied->BrakeDelay[ 3 ] ) + // a train falling further behind needs no full fill time to be seen not to get enough + || ( ( fShortfall > 0.02 ) + && ( fBrakeHeldTime > brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) ) { // the table says the present position is enough, but the brake has had its full fill // time since the handle last moved and the train is still not slowing down as asked: // believe the train rather than the table @@ -3194,7 +3200,9 @@ bool TController::DecBrake() { if( fBrake_a0[0] != 0.0 || fBrake_a1[0] != 0.0 ) { auto const pos_diff { ( is_dmu() ? 0.25 : 1.0 ) }; - deltaAcc = -AccDesired * BrakeAccFactor() - ( fBrake_a0[ 0 ] + 4 * (/*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition - pos_diff )*fBrake_a1[ 0 ] ); + // a distributor applied at all gives at least its minimum stage, about what the first position does, so + // a position short of the first one brakes no weaker than that + deltaAcc = -AccDesired * BrakeAccFactor() - ( fBrake_a0[ 0 ] + 4 * std::max( 0.0, BrakeCtrlPosition - pos_diff ) * fBrake_a1[ 0 ] ); } if (deltaAcc < 0 && /*GBH mvOccupied->BrakeCtrlPosR*/ BrakeCtrlPosition > 0) { OK = /*mvOccupied->*/ BrakeLevelAdd(-0.25); @@ -7701,7 +7709,7 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { // train is still speeding up, the prediction above has taken that off already) auto room { brakingroom }; if( ( AbsAccS <= 0.0 ) - && ( ( VelNext > 0.0 ) || ( BrakeCtrlPosition <= 0.0 ) ) ) { // for a stop, only while the brake is still to be applied + && ( BrakeCtrlPosition <= 0.0 ) ) { // only while the brake is still to be applied: once it acts, the room it needs to respond is behind room = std::max( 0.5 * brakingroom, brakingroom - vel / 3.6 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ); @@ -7830,6 +7838,10 @@ TController::adjust_desired_speed_for_current_speed() { BrakeSystem == TBrakeSystem::Pneumatic ? std::clamp( fAccGravity * 15.0, 1.0, 3.0 ) : std::clamp( fAccGravity * 7.5, 1.0, 1.5 ) ) }; + // the predicted difference is to be made up over the time the brake takes to respond, not at once: with a + // slow brake, a demand sized to remove it at once comes on as a deep application, which then takes as + // long to let go, and the train ends up well below the speed it was to hold + auto const holdgain { std::min( 0.04, 1.0 / ( 3.6 * std::max( 1.0, horizon ) ) ) }; if( ( speedestimate > VelDesired ) && ( VelDesired == 0.0 ) ) { // jesli stoj, to hamuj, ale i tak juz za pozno :) @@ -7843,7 +7855,7 @@ TController::adjust_desired_speed_for_current_speed() { AccDesired = std::min( AccDesired, // but don't override decceleration for VelNext std::clamp( - EU07_AI_NOACCELERATION - 0.04 * ( speedestimate - holdspeed ), + EU07_AI_NOACCELERATION - holdgain * ( speedestimate - holdspeed ), -0.05 + fAccThreshold, std::max( EU07_AI_NOACCELERATION, AccPreferred ) ) ); // HACK: for cargo trains with high braking threshold ensure we cross that threshold @@ -8151,6 +8163,20 @@ void TController::increase_tractive_force() { cue_action( driver_hint::tractiveforceincrease ); } +// how far the deceleration may fall short of the demand before the brake is applied harder. approaching a lower +// speed, a pneumatic brake given a full step only once that much is missing acts too late: the demand +// creeps up meanwhile, and in a long G train the next step takes many seconds to act, so half a step is enough +double +TController::brake_increase_margin() const { + + if( ( BrakeSystem == TBrakeSystem::Pneumatic ) + && ( VelNext > 0.0 ) + && ( VelNext < DirectionalVel() ) ) { + return 0.5 * fBrake_a1[ 0 ]; + } + return fBrake_a1[ 0 ]; +} + void TController::control_braking_force() { // don't touch the brakes when compressing buffers during coupling/uncoupling @@ -8168,6 +8194,19 @@ void TController::control_braking_force() { // resultant the AI asks for, not with the brake-only demand AccDesired holds on a descent auto const accdesiredresultant { AccDesired + fAccDesiredGravity }; + // every few seconds, note how far the train falls short of the demand and whether that is getting worse: a brake + // still filling, or blocks gripping harder as the train slows down, make up a shortfall on their own, but a + // shortfall that keeps growing at a constant handle means the train is falling further behind its braking curve + if( ElapsedTime - fShortfallTime >= 3.0 ) { + auto const shortfall { AbsAccS - accdesiredresultant }; + iShortfallGrowth = ( + ( ( AccDesired < 0.0 ) && ( shortfall > fShortfall + 0.005 ) ) ? + iShortfallGrowth + 1 : + 0 ); + fShortfall = shortfall; + fShortfallTime = ElapsedTime; + } + if( is_emu() && !ForcePNBrake ) { // właściwie, to warunek powinien być na działający EP // Ra: to dobrze hamuje EP w EZT @@ -8201,7 +8240,7 @@ void TController::control_braking_force() { } // type & dt_ezt else { // a stara wersja w miarę dobrze działa na składy wagonowe - if( (accdesiredresultant < fAccGravity - 0.1 && AbsAccS > accdesiredresultant + fBrake_a1[0] // regular braking + if( (accdesiredresultant < fAccGravity - 0.1 && AbsAccS > accdesiredresultant + brake_increase_margin() // regular braking || fAccGravity < -0.05 && velocity < -0.1) && // also brake if uphill and slipping back (fBrakeTime < 0.0 // the size of the brake demand decides whether to skip the reaction time, so the brake-only part @@ -8214,6 +8253,18 @@ void TController::control_braking_force() { // Ra: ten czas należy zmniejszyć, jeśli czas dojazdu do zatrzymania jest mniejszy (3.0 + 0.5 * ((mvOccupied->BrakeDelayFlag > bdelay_G ? mvOccupied->BrakeDelay[1] : mvOccupied->BrakeDelay[3]) - 3.0)) * 0.5); // Ra: tymczasowo, bo przeżyna S1 } + else if( ( BrakeSystem == TBrakeSystem::Pneumatic ) + && ( AccDesired < 0.0 ) + && ( BrakeCtrlPosition > 0.0 ) + && ( fShortfall > 0.02 ) + && ( iShortfallGrowth >= 2 ) + && ( fBrakeHeldTime > brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) { + // the train has been falling further behind the demand for a while at a handle left alone for longer than + // the brake takes to respond: a small step now, while it still is small, rather than the deep one the + // margin above would allow only once the train is far behind + cue_action( driver_hint::brakingforceincrease ); + iShortfallGrowth = 0; + } if ( (accdesiredresultant < fAccGravity - 0.05 || AccDesired >= EU07_AI_NOACCELERATION) // no braking asked for, yet the train is slowing down more than that && accdesiredresultant - fBrake_a1[0] * 0.51 - AbsAccS > 0.05 && OrderCurrentGet() != Disconnect && VelDesired > 0.0 ) { diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 11a01517f..f1975fca6 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -311,6 +311,8 @@ private: double DirectionalVel() const { return mvOccupied->Vel * sign( iDirection * mvOccupied->V ); } + double brake_increase_margin() const; + void update_timers( double const dt ); void update_logs( double const dt ); void determine_consist_state(); @@ -401,6 +403,9 @@ 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 + 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 double fAccDesiredGravity = 0.0; // część AccDesired odjęta na spadku jako kompensacja pochylenia; AbsAccS zawiera ją już sam double VelforDriver = -1.0; // prędkość, używana przy zmianie kierunku (ograniczenie przy nieznajmości szlaku?) double VelSignal = 0.0; // ograniczenie prędkości z kompilacji znaków i sygnałów // normalnie na początku ma stać, no chyba że jedzie From 39244a88a500d51ada0f8c33ccc3056848466d22 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 21:17:12 +0100 Subject: [PATCH 66/90] tests: wait for the pressure switches before notching up full_power() notched up as soon as the controlling half had closed its line contactors. In the ET42 test the locomotive brake is still at about 2.0 bar then, right at the pressure switch threshold, so whether the second half starts as well depended on thousandths of a bar in its cylinder: a half whose switch is on when the controller leaves zero doesn't close its line contactors until the controller is back at zero. Any change to the air brakes could tip it either way (with the corrected ESt3 the second half stayed off and the train reached 88.7 instead of 106 km/h). The helper now waits, as a driver does, until no pressure switch in the train cuts the start off. --- tests/unit/scenario.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unit/scenario.cpp b/tests/unit/scenario.cpp index b933a0ab5..4af8be18a 100644 --- a/tests/unit/scenario.cpp +++ b/tests/unit/scenario.cpp @@ -5,6 +5,21 @@ namespace test { +namespace { + +// whether the pressure switch of a vehicle in the train still cuts the start off: a half of a locomotive whose +// switch is on when the controller leaves zero doesn't close its line contactors until the controller is back at zero +bool start_cut_off(consist &Train) { + for (std::size_t i = 0; i < Train.size(); ++i) { + if (Train[i].mover().ControlPressureSwitch) { + return true; + } + } + return false; +} + +} // namespace + scenario::scenario(std::string Name, std::vector Channels, track const &Track) : m_name(std::move(Name)), m_channels(std::move(Channels)), m_track(Track) { reseed_random(); } @@ -53,6 +68,11 @@ void scenario::get_going() { void scenario::full_power(double Seconds) { auto &controlling{driver().controlling}; + // as a driver does, wait for the brakes to let go far enough for every pressure switch of the train to allow the + // start; otherwise whether all the halves of a locomotive start hangs on thousandths of a bar of their cylinders + for (int i = 0; i < 120 && start_cut_off(m_train); ++i) { + wait(1.0); + } controlling.IncMainCtrl(1); for (int i = 0; i < 90 && false == controlling.StLinFlag; ++i) { wait(1.0); From 4b769d95c1588e13acd8e9e6fea02efdae2336ca Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 02:05:45 +0100 Subject: [PATCH 67/90] fix: SOD lamp shows the doors of the whole consist in three states (eu07/maszyna#288) The door closure signalling lamp (i-door_blocked) showed only Doors.is_locked of the occupied vehicle, so a door open in another car or a coupled unit didn't show on it. TMoverParameters::door_closure() gives the state of one vehicle (none, locked, released, open), the driver gathers the most severe one of the train in determine_consist_state(), next to IsAnyDoorOpen, and the lamp is lit for locked doors, blinks for closed but released ones (a permit given, or no lock engaged on doors that need no permit) and is dark with a door open. doorclosuresignal in eu07.ini (default yes) switches it; with no the lamp shows is_locked of the occupied vehicle as before. No data change needed. --- McZapkie/MOVER.h | 17 +++++ McZapkie/Mover.cpp | 37 +++++++++++ tests/unit/test_door_closure.cpp | 110 +++++++++++++++++++++++++++++++ utilities/Globals.cpp | 7 ++ utilities/Globals.h | 2 + vehicle/Driver.cpp | 3 + vehicle/Driver.h | 1 + vehicle/Train.cpp | 11 +++- 8 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_door_closure.cpp diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 16bb207fa..45e3305c0 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 { @@ -2593,6 +2609,7 @@ 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 /* funkcje dla samochodow*/ bool ChangeOffsetH(double DeltaOffset); diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 20761bacb..03fa3e6db 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -8699,6 +8699,43 @@ 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 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) { diff --git a/tests/unit/test_door_closure.cpp b/tests/unit/test_door_closure.cpp new file mode 100644 index 000000000..abfa5a7e1 --- /dev/null +++ b/tests/unit/test_door_closure.cpp @@ -0,0 +1,110 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scenario.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 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); +} diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index b4f2e14be..a8aadec22 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -506,6 +506,12 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& return true; } + if (token == "doorclosuresignal") + { + ParseOne(Parser, DoorClosureSignal); + return true; + } + if (token == "movelight") { ParseOne(Parser, fMoveLight, 1, false); @@ -1573,6 +1579,7 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "skyenabled", asSky ); export_as_text( Output, "defaultext", szDefaultExt ); export_as_text( Output, "newaircouplers", bnewAirCouplers ); + export_as_text( Output, "doorclosuresignal", DoorClosureSignal ); export_as_text( Output, "anisotropicfiltering", AnisotropicFiltering ); export_as_text( Output, "usevbo", bUseVBO ); export_as_text( Output, "feedbackmode", iFeedbackMode ); diff --git a/utilities/Globals.h b/utilities/Globals.h index ad216cd96..971f8495a 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -115,6 +115,8 @@ struct global_settings { std::string Period{}; // time of the day, based on sun position bool FullPhysics{ true }; // full calculations performed for each simulation step bool bnewAirCouplers{ true }; + bool DoorClosureSignal{ true }; // door closure signalling lamp (i-door_blocked) shows the doors of the whole consist in three states + float fMoveLight{ 0.f }; // numer dnia w roku albo -1 bool FakeLight{ false }; // toggle between fixed and dynamic daylight double fTimeSpeed{ 1.0 }; // przyspieszenie czasu, zmienna do testów diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 9f83b6d53..778b97004 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -5937,6 +5937,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 +5999,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; diff --git a/vehicle/Driver.h b/vehicle/Driver.h index f1975fca6..d750fc398 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -632,6 +632,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/Train.cpp b/vehicle/Train.cpp index e30635f4f..3a4c42db1 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -9108,7 +9108,16 @@ 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 (Global.DoorClosureSignal && DynamicObject->Mechanik != nullptr) + { + // 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 + btLampkaBlokadaDrzwi.Turn(door_closure_lamp(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 From 547706b54d769865bc9fb8239cdbdbe64f292dc8 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 09:35:59 +0100 Subject: [PATCH 68/90] refactor(simulation): fix the Sonar remarks on the test mode code - TController::TableCheck() calls TSpeedPos::Update() as it did before the test mode: the check for a signal passed at danger is a call of its own after it (CheckSignalPassedAtDanger), so the update lines nest no deeper than on master (S134). A switch is a track, so a signal's update never takes the switch-change branch and the check runs in the same place as before; the physicslogs of all 15 scenario cases are byte for byte the same. - null_imgui_renderer is final (S1235), to_string(test_result) uses `using enum` (S6177), init_result_cell() is const (S5817), ConfigParseTest() takes a std::string_view (S6009) and the test settings struct is declared apart from its member (S3646). --- rendering/nullrenderer.h | 2 +- simulation/simulationtest.cpp | 15 ++++++++------- simulation/simulationtest.h | 2 +- utilities/Globals.cpp | 12 ++++++------ utilities/Globals.h | 5 +++-- vehicle/Driver.cpp | 18 ++++++++++-------- vehicle/Driver.h | 4 ++-- 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/rendering/nullrenderer.h b/rendering/nullrenderer.h index 2a7ef0398..7ecb57bf8 100644 --- a/rendering/nullrenderer.h +++ b/rendering/nullrenderer.h @@ -36,7 +36,7 @@ private: }; // user interface backend which draws nothing: imgui still builds its frames, so the ui code runs as usual -class null_imgui_renderer : public imgui_renderer { +class null_imgui_renderer final : public imgui_renderer { public: bool Init() override { return true; } void Shutdown() override { /* null implementation, nothing to do */ } diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 8d57505ff..45aa513f8 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -45,19 +45,20 @@ test_manager &test() std::string_view to_string(test_result const Result) { + using enum test_result; switch (Result) { - case test_result::pass: return "pass"; - case test_result::fail: return "fail"; - case test_result::signal_passed_at_danger: return "signal passed at danger"; - case test_result::timeout: return "timeout"; - case test_result::standstill: return "standstill"; - case test_result::error: return "error"; + 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"; } -void test_manager::init_result_cell() +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 diff --git a/simulation/simulationtest.h b/simulation/simulationtest.h index f47f408b7..a58e9ae2f 100644 --- a/simulation/simulationtest.h +++ b/simulation/simulationtest.h @@ -39,7 +39,7 @@ public: 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(); + 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 diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 2516da29e..3a9e10145 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -456,10 +456,10 @@ bool global_settings::ConfigParseInput(cParser& Parser, const std::string& token return false; } -bool global_settings::ConfigParseTest(cParser& Parser, const std::string& token) +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") + if (Token == "test.timeout") { double value{ test.timeout }; ParseOne(Parser, value); @@ -468,26 +468,26 @@ bool global_settings::ConfigParseTest(cParser& Parser, const std::string& token) return true; } - if (token == "test.standstill") + if (Token == "test.standstill") { ParseOne(Parser, test.standstill); test.standstill = std::max(0.0, test.standstill); return true; } - if (token == "test.pass_on_standstill") + if (Token == "test.pass_on_standstill") { ParseOne(Parser, test.pass_on_standstill); return true; } - if (token == "test.fail_if_stop_overran") + if (Token == "test.fail_if_stop_overran") { ParseOne(Parser, test.fail_if_stop_overran); return true; } - if (token == "test.no_movement_before_pass") + if (Token == "test.no_movement_before_pass") { ParseOne(Parser, test.no_movement_before_pass); return true; diff --git a/utilities/Globals.h b/utilities/Globals.h index a6a9e1226..11d14e34f 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -95,7 +95,8 @@ struct global_settings { 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 every vehicle stands still; with false it ends the test at once bool timeout_from_command_line{ false }; // --test-timeout given, the scenery doesn't override it - } test; + }; + 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 @@ -387,7 +388,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, 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/vehicle/Driver.cpp b/vehicle/Driver.cpp index c0614923c..fe09fb240 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -768,7 +768,8 @@ void TController::TableCheck(double fDistance) { // aktualizacja rekordów z wyjątkiem ostatniego if (sSpeedTable[i].iFlags & spEnabled) // jeśli pozycja istotna { - if (UpdateSpeedPosition(sSpeedTable[i])) + auto const passedbefore { TestFlag( sSpeedTable[ i ].iFlags, spElapsed ) }; + if (sSpeedTable[i].Update()) { if( Global.iWriteLogEnabled & 8 ) { WriteLog( "Speed table for " + OwnerName() + " detected switch change at " + sSpeedTable[ i ].trTrack->name() + " (generating fresh trace)" ); @@ -794,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 @@ -817,8 +820,10 @@ void TController::TableCheck(double fDistance) } } } - // aktualizacja ostatniego; a signal at stop often is the last, the scan doesn't go past it - UpdateSpeedPosition(sSpeedTable[iLast]); + 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 @@ -843,17 +848,14 @@ bool is_signal_at_stop( TSpeedPos const &Point ) } // namespace -bool TController::UpdateSpeedPosition( TSpeedPos &Point ) const +void TController::CheckSignalPassedAtDanger( TSpeedPos const &Point, bool const Passedbefore ) const { - auto const passedbefore { TestFlag( Point.iFlags, spElapsed ) }; - auto const switchchanged { Point.Update() }; - if( false == passedbefore + 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 ); } - return switchchanged; } auto const passengerstopmaxdistance { 400.0 }; // maximum allowed distance between passenger stop point and consist head diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 8f2015f2f..dfa63c088 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -519,9 +519,9 @@ private: bool TableNotFound( basic_event const *Event, double const Distance ) const; void TableTraceRoute( double fDistance, TDynamicObject *pVehicle ); void TableCheck( double fDistance ); - // updates a position of the speed table, returns true when a switch on it changed (TSpeedPos::Update). + // 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 - bool UpdateSpeedPosition( TSpeedPos &Point ) const; + 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 ); From 4356e7c3fd8beb83c9c22e0ae55b0ac34df7705e Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 09:36:28 +0100 Subject: [PATCH 69/90] fix(simulation): a test run that can't start ends with the error code and says why - `--test` is looked for before the rest of the command line is read. An unknown option before it (e.g. `-s x.scn --test-timout 90 --test`) used to end the run with exit code 0, which a caller reads as a pass. - `--test-timeout` without a value, or with one that isn't a number of seconds, fails the run instead of leaving the default or, through atof, turning the timeout off. - These, a missing -s, a failed start and an exception caught in main() end the test through test_manager::conclude(), which writes the usual `test: error (exit code 5) ... : ` line to stdout and the log. Before, `eu07 --test` alone exited with 5 and nothing anywhere: the error went to the log queue and the process ended before the log thread wrote it. In test mode main() now stops the log thread and waits for it before _Exit(). Outside the test mode nothing changes: conclude() does nothing there. --- EU07.cpp | 21 +++++++++++++++++++++ application/application.cpp | 34 ++++++++++++++++++++++++++++++---- simulation/simulationtest.cpp | 11 ++++++++--- simulation/simulationtest.h | 5 ++++- 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/EU07.cpp b/EU07.cpp index ec3e972e7..790f6ffb9 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -84,6 +84,20 @@ 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.join(); +} + +} // namespace + int main(int argc, char *argv[]) { #ifdef WITHDUMPGEN @@ -153,6 +167,7 @@ int main(int argc, char *argv[]) { ErrorLog(std::format("Critical error: {}", Error.what())); // in test mode nobody is there to close the box, the exit code tells of the error + simulation::test().conclude(simulation::test_result::error, std::format("critical error: {}", Error.what())); if (false == Global.test.enabled) { std::string msg = "Simulator crash occured :(\n"; @@ -166,6 +181,12 @@ int main(int argc, char *argv[]) fflush(stdout); fflush(stderr); #endif + if (Global.test.enabled) + { + // a run that stopped before its test ended (a failed start, an exception) says so like any other end + simulation::test().conclude(simulation::test_result::error, "the simulator stopped before the test ended"); + finish_log(); + } // skip destructors, there are ordering errors which causes segfaults // a test run (--test) says its result in the exit code std::_Exit(Global.test.enabled ? simulation::test().exit_code() : 0); diff --git a/application/application.cpp b/application/application.cpp index eaf60c7cd..f8e20bed4 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -30,8 +30,15 @@ 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 #include +#include +#include +#include #include "utilities/translation.h" #if WITH_DISCORD_RPC @@ -1174,6 +1181,16 @@ void apply_test_mode_settings() Global.random_seed = seed_of("test"); } +// seconds given on the command line: a number, not negative. returns nothing for anything else +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; +} + } // namespace int eu07_application::init_settings(int Argc, char *Argv[]) @@ -1191,6 +1208,10 @@ int eu07_application::init_settings(int Argc, char *Argv[]) Global.LoadIniFile("eu07.ini"); } + // a test run is known before the rest of the command line is read, so a command line it can't take + // ends with the error code of the test, not with 0, which would pass it + Global.test.enabled = std::ranges::any_of(std::span(Argv, static_cast(Argc)), [](char const *Argument) { return std::string_view(Argument) == "--test"; }); + // process command line arguments bool sceneryfromcommandline{false}; bool editwithoutscenery{false}; @@ -1244,11 +1265,15 @@ int eu07_application::init_settings(int Argc, char *Argv[]) } else if (token == "--test-timeout") { - if (i + 1 < Argc) + auto const timeout{i + 1 < Argc ? parse_seconds(Argv[++i]) : std::nullopt}; + if (false == timeout.has_value()) { - Global.test.timeout = std::max(0.0, std::atof(Argv[++i])); - Global.test.timeout_from_command_line = true; + // a typo would otherwise turn the timeout off + simulation::test().conclude(simulation::test_result::error, "--test-timeout needs a number of seconds"); + return -1; } + Global.test.timeout = *timeout; + Global.test.timeout_from_command_line = true; } else if (token == "-ini") { @@ -1267,6 +1292,7 @@ int eu07_application::init_settings(int Argc, char *Argv[]) << " [-seed number|word]" << " [-ini settingsfile]" << " [--test [--test-timeout seconds]]" << std::endl; + simulation::test().conclude(simulation::test_result::error, std::format("unknown command line argument {}", token)); return -1; } } @@ -1277,7 +1303,7 @@ int eu07_application::init_settings(int Argc, char *Argv[]) { if (false == sceneryfromcommandline || Global.SceneryFile.empty()) { - ErrorLog("test: --test needs a scenery given with -s"); + simulation::test().conclude(simulation::test_result::error, "--test needs a scenery given with -s"); return -1; } apply_test_mode_settings(); diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 45aa513f8..214d7e463 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -156,9 +156,15 @@ void test_manager::signal_passed_at_danger(std::string_view const Vehicle, std:: } 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) { if (m_finished || false == Global.test.enabled) - return; + return false; m_finished = true; m_result = Result; @@ -170,8 +176,7 @@ void test_manager::finish(test_result const Result, std::string_view const Reaso // 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); - - Application.queue_quit(true); + return true; } } // namespace simulation diff --git a/simulation/simulationtest.h b/simulation/simulationtest.h index a58e9ae2f..2b5012e9f 100644 --- a/simulation/simulationtest.h +++ b/simulation/simulationtest.h @@ -46,8 +46,11 @@ public: 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; the first call wins + // 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); bool finished() const { return m_finished; } // exit code of the process: the result, or error while the test hasn't finished From ced4e06b35b7945789460efed5af22c205fa0b98 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 09:36:35 +0100 Subject: [PATCH 70/90] fix(simulation): no crash message box in test mode The crash handler of WITHDUMPGEN builds (on by default) shows a message box before it writes the dump. It also gets the C++ exceptions nothing catches, since it replaces the CRT's unhandled exception filter. In test mode nobody is there to close the box, so a crashing test waited for it until whatever started it gave up (run.py after the case's timeout, 600 s by default). The dump is still written, the exit code is the exception's. --- EU07.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/EU07.cpp b/EU07.cpp index 790f6ffb9..0fbabba5b 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -73,7 +73,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); From 93e2b85d08cca5d4d374a3965c747292d59656d9 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 09:36:51 +0100 Subject: [PATCH 71/90] fix(simulation): the test mode gives nothing to the outside and leaves the player's files alone The test mode turned off what comes in from outside, but a few settings of the user's eu07.ini still reached out of a test run: - feedbackmode: 1 and 2 toggle Caps Lock / Scroll Lock by injecting key events into the whole system, 3 and 4 drive an LPT port or a PoKeys desk (and 4 reads the desk's switches); - multiplayer: events are reported to a multiplayer server, and some events are held back for it; - execonexit: a program is run with system() at the end of each run, which also waits for it; - showsystemconsole: a console of the run's own pops up and stdout is reopened on it, so whoever started the test with a redirected output doesn't get the `test:` line (and a console the run was started from would be hidden without it); - imgui.ini in the game folder was written by every run, and by several at once with run.py -j. All of these are off in test mode. Nothing changes outside it. --- application/application.cpp | 18 ++++++++++++++++++ application/uilayer.cpp | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/application/application.cpp b/application/application.cpp index f8e20bed4..d4aed9d8a 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -31,6 +31,9 @@ http://mozilla.org/MPL/2.0/. #include "utilities/dictionary.h" #include "version_info.h" #include "simulation/simulationtest.h" +#ifdef _WIN32 +#include "Console.h" +#endif #include #include #include @@ -1048,6 +1051,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; @@ -1176,6 +1186,14 @@ void apply_test_mode_settings() #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"); 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 From 1b373bcc385e800e3a6717fc37f1fbda75f5de7c Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 09:36:56 +0100 Subject: [PATCH 72/90] docs(test): the test mode README on the new ends, outside settings and Windows shells - what the test mode turns off towards the outside (feedbackmode, multiplayer, execonexit, showsystemconsole, imgui.ini), - a command line it can't take or a failed start ends with `error`, - cmd and PowerShell don't wait for the (windowed) simulator nor show its output: start /wait, Start-Process -Wait, - 15 cases, not 14. --- tests/scenarios/README.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index b57fc4a05..1d63ebefe 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -26,12 +26,21 @@ With `--test` the simulator: - 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`), and the game's `imgui.ini` stays as it is, - 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. + line on stdout and in the log. A command line it can't take, or a start that fails, ends + the same way with the `error` code. + +On Windows the simulator is a windowed program, so `cmd` and PowerShell don't wait for it nor +show its output by themselves: run it with `start /wait eu07 --test ... > result.txt` and read +`%errorlevel%` in `cmd`, or `(Start-Process eu07 '--test ...' -Wait -PassThru -RedirectStandardOutput result.txt).ExitCode` +in PowerShell. 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 @@ -101,7 +110,7 @@ Python 3.10+, no extra packages. Options: `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 14 cases +- `-j N` runs N cases at once. Each case prints its results when it is done. All 15 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 From b2331ad2d185a2b4e52529b1900e90eeeecf4be7 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 09:37:32 +0100 Subject: [PATCH 73/90] docs(test): what waits for a test run in cmd, checked start /wait gives the exit code but not the output; a .bat file waits for the windowed simulator and passes its output on. --- tests/scenarios/README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 1d63ebefe..8ad7e7470 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -37,10 +37,11 @@ With `--test` the simulator: line on stdout and in the log. A command line it can't take, or a start that fails, ends the same way with the `error` code. -On Windows the simulator is a windowed program, so `cmd` and PowerShell don't wait for it nor -show its output by themselves: run it with `start /wait eu07 --test ... > result.txt` and read -`%errorlevel%` in `cmd`, or `(Start-Process eu07 '--test ...' -Wait -PassThru -RedirectStandardOutput result.txt).ExitCode` -in PowerShell. Scripts (Python's `subprocess`, bash) wait for it as for any other program. +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 From 62526e9d6a1b87fcb0f67cb36ba674a91da94f9c Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:16:33 +0100 Subject: [PATCH 74/90] test(scenarios): the timetable cases check where the train stops, not how long Przystanek on td.scn is a technical stop of the td/rozklad timetable (pt): the AI doesn't wait there for the departure time but sets off as soon as it counts the train as standing at the W4, below 1 km/h. How long the speed then stays under 0.5 km/h is only how fast the brake releases. With the AI braking of #264 the trains creep up to the W4 at about 10 km/h on a light application and stood there for 1-5 s; the SN61 turns back at 0.36 km/h without stopping. The hold of 5 s (2 s for the SN61) failed for EP09, SN61 and Vectron and had well under a second to spare for others. The cases now check that the train comes down to 1 km/h, the AI's own standstill at a W4, 1380-1420 m from the start; every vehicle stops at 1397-1401 m, before and after #264. `stops` takes min_distance and max_distance for that. --- tests/scenarios/README.md | 14 ++++++++++---- tests/scenarios/cases/en57_td_timetable.json | 2 +- tests/scenarios/cases/ep09_td_timetable.json | 2 +- tests/scenarios/cases/et22_td_timetable.json | 2 +- tests/scenarios/cases/eu07_td_timetable.json | 2 +- tests/scenarios/cases/sm42_td_timetable.json | 2 +- tests/scenarios/cases/sn61_td_timetable.json | 2 +- tests/scenarios/cases/vectron_td_timetable.json | 2 +- tests/scenarios/run.py | 8 ++++++-- 9 files changed, 23 insertions(+), 13 deletions(-) diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 8ad7e7470..8bd6f1a4a 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -168,7 +168,12 @@ scenery is newer. the one whose physicslog is checked. Other vehicles of the consist take names made from it, e.g. `{{vehicle}}_s`. 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`. `td_red_signal_moving.scn` is the red signal one with the train +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 (every vehicle 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` @@ -189,13 +194,14 @@ the row instead, logged by builds from this branch on. - `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`: after reaching `moving_speed`, stays at or - below `stop_speed` for `hold` seconds. +- `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 (+-3), and the red signal stop at 140 m. + 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 diff --git a/tests/scenarios/cases/en57_td_timetable.json b/tests/scenarios/cases/en57_td_timetable.json index ddd52af37..d4c4f26fa 100644 --- a/tests/scenarios/cases/en57_td_timetable.json +++ b/tests/scenarios/cases/en57_td_timetable.json @@ -16,6 +16,6 @@ { "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": 0.5, "hold": 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 index dd130be3c..851b4dbfe 100644 --- a/tests/scenarios/cases/ep09_td_timetable.json +++ b/tests/scenarios/cases/ep09_td_timetable.json @@ -12,6 +12,6 @@ { "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": 0.5, "hold": 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 index 004f5354d..47622c622 100644 --- a/tests/scenarios/cases/et22_td_timetable.json +++ b/tests/scenarios/cases/et22_td_timetable.json @@ -12,6 +12,6 @@ { "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": 0.5, "hold": 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_td_timetable.json b/tests/scenarios/cases/eu07_td_timetable.json index 2f23019fe..3774242cd 100644 --- a/tests/scenarios/cases/eu07_td_timetable.json +++ b/tests/scenarios/cases/eu07_td_timetable.json @@ -12,6 +12,6 @@ { "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": 0.5, "hold": 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 index fc89be0fa..994f94c0e 100644 --- a/tests/scenarios/cases/sm42_td_timetable.json +++ b/tests/scenarios/cases/sm42_td_timetable.json @@ -12,6 +12,6 @@ { "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": 0.5, "hold": 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 index 9fa4b18d8..1a03c31f8 100644 --- a/tests/scenarios/cases/sn61_td_timetable.json +++ b/tests/scenarios/cases/sn61_td_timetable.json @@ -12,6 +12,6 @@ { "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": 0.5, "hold": 2 } + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } ] } diff --git a/tests/scenarios/cases/vectron_td_timetable.json b/tests/scenarios/cases/vectron_td_timetable.json index 9948a09ef..04656a00f 100644 --- a/tests/scenarios/cases/vectron_td_timetable.json +++ b/tests/scenarios/cases/vectron_td_timetable.json @@ -12,6 +12,6 @@ { "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": 0.5, "hold": 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 index 4c3383081..210d6c013 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -344,7 +344,7 @@ def check_brakes(rows, min_pressure, moving_speed=20): return False, f"brake cylinder pressure never reached {min_pressure} bar after moving off" -def check_stops(rows, moving_speed, stop_speed, hold): +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" @@ -356,7 +356,11 @@ def check_stops(rows, moving_speed, stop_speed, hold): if stopped_at is None: stopped_at = i if r[COL_TIME] - rows[stopped_at][COL_TIME] >= hold: - return True, f"came to a stop at t={rows[stopped_at][COL_TIME]:.1f}s after {dist[stopped_at]:.0f} m" + 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" From 486a714f521ce6d28aca9f9adc475f03817306dc Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:20:52 +0100 Subject: [PATCH 75/90] refactor(simulation): the rules of the test mode in a file of their own, with unit tests The command line options of the test mode and the end conditions checked after each frame are now plain functions in simulation/simulationtestrules.cpp, which needs nothing of the running simulation: parse_test_command_line() and evaluate_frame(). The file is part of the core library, so the unit tests check them (tests/unit/test_test_mode.cpp): the --test-timeout value, values of other options not taken for options, a pass that waits for the stop, the standstill, the timeout. test_manager only gathers what a frame brought and acts on the verdict. No change in behaviour. --- CMakeLists.txt | 3 +- application/application.cpp | 45 +++---- simulation/simulationtest.cpp | 77 +++--------- simulation/simulationtest.h | 21 +--- simulation/simulationtestrules.cpp | 139 ++++++++++++++++++++ simulation/simulationtestrules.h | 82 ++++++++++++ tests/unit/test_test_mode.cpp | 195 +++++++++++++++++++++++++++++ 7 files changed, 452 insertions(+), 110 deletions(-) create mode 100644 simulation/simulationtestrules.cpp create mode 100644 simulation/simulationtestrules.h create mode 100644 tests/unit/test_test_mode.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a4e1d6ea..483dbc146 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -454,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" @@ -463,6 +463,7 @@ set(CORE_SOURCES "utilities/parser.cpp" "utilities/utilities.cpp" "scripting/ladderlogic.cpp" +"simulation/simulationtestrules.cpp" "vehicle/handover.cpp" "vehicle/horn.cpp" "vehicle/smoke.cpp" diff --git a/application/application.cpp b/application/application.cpp index d4aed9d8a..f3c3fe46d 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -34,14 +34,9 @@ http://mozilla.org/MPL/2.0/. #ifdef _WIN32 #include "Console.h" #endif -#include -#include #include -#include #include -#include -#include -#include +#include #include "utilities/translation.h" #if WITH_DISCORD_RPC @@ -1199,16 +1194,6 @@ void apply_test_mode_settings() Global.random_seed = seed_of("test"); } -// seconds given on the command line: a number, not negative. returns nothing for anything else -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; -} - } // namespace int eu07_application::init_settings(int Argc, char *Argv[]) @@ -1228,7 +1213,18 @@ int eu07_application::init_settings(int Argc, char *Argv[]) // a test run is known before the rest of the command line is read, so a command line it can't take // ends with the error code of the test, not with 0, which would pass it - Global.test.enabled = std::ranges::any_of(std::span(Argv, static_cast(Argc)), [](char const *Argument) { return std::string_view(Argument) == "--test"; }); + auto const testcommandline{simulation::parse_test_command_line(std::vector(Argv, Argv + Argc))}; + Global.test.enabled = testcommandline.enabled; + 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; + } // process command line arguments bool sceneryfromcommandline{false}; @@ -1278,20 +1274,13 @@ int eu07_application::init_settings(int Argc, char *Argv[]) } else if (token == "--test") { - // integration test: the scenery runs unattended and the exit code is the result (eu07/maszyna#541) - Global.test.enabled = true; + // 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") { - auto const timeout{i + 1 < Argc ? parse_seconds(Argv[++i]) : std::nullopt}; - if (false == timeout.has_value()) - { - // a typo would otherwise turn the timeout off - simulation::test().conclude(simulation::test_result::error, "--test-timeout needs a number of seconds"); - return -1; - } - Global.test.timeout = *timeout; - Global.test.timeout_from_command_line = true; + // taken from the command line above, with its value + ++i; } else if (token == "-ini") { diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 214d7e463..76aa1be30 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -25,16 +25,6 @@ http://mozilla.org/MPL/2.0/. #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; -} - -} // namespace - namespace simulation { test_manager &test() @@ -43,21 +33,6 @@ test_manager &test() return instance; } -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"; -} - void test_manager::init_result_cell() const { auto cell { std::make_unique(scene::node_data{0, -1, std::string(result_cell), "memcell"}) }; @@ -73,8 +48,7 @@ void test_manager::start() if (m_started) return; m_started = true; - m_time = 0.0; - m_resttime = 0.0; + m_progress = test_progress {}; if (simulation::Vehicles.sequence().empty()) { // most likely the scenery wasn't found, or it isn't a test scenery @@ -94,48 +68,25 @@ void test_manager::update(double const Deltatime) if (false == m_started || m_finished) return; - m_time += Deltatime; - + test_frame frame { Deltatime, std::nullopt, {}, + std::ranges::none_of(simulation::Vehicles.sequence(), [](TDynamicObject const *Vehicle) { return std::abs(Vehicle->GetVelocity()) > 0.01; }) }; if (auto const *cell = simulation::Memory.find(std::string(result_cell))) { - auto const value { cell->Value1() }; - auto const reason { cell->Text().empty() ? std::string() : std::format(" ({})", cell->Text()) }; - if (holds(value, 0.0)) - { - finish(test_result::fail, std::format("__test.result set to 0{}", reason)); - return; - } - if (holds(value, 1.0) && false == Global.test.no_movement_before_pass) - { - finish(test_result::pass, std::format("__test.result set to 1{}", reason)); - return; - } - if (holds(value, 1.0) && false == m_passrequested) - { - m_passrequested = true; - WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for every vehicle to stop", m_time, reason)); - } + frame.reported = cell->Value1(); + frame.reason = cell->Text(); } - auto const atrest { std::ranges::none_of(simulation::Vehicles.sequence(), - [](TDynamicObject const *Vehicle) { return std::abs(Vehicle->GetVelocity()) > 0.01; }) }; - m_resttime = atrest ? m_resttime + Deltatime : 0.0; - - if (m_passrequested && m_resttime >= pass_settle_time) + auto const requestedbefore { m_progress.passrequested }; + auto const verdict { evaluate_frame(m_progress, frame, test_limits { Global.test.timeout, Global.test.standstill, Global.test.pass_on_standstill, Global.test.no_movement_before_pass }) }; + if (verdict.has_value()) { - finish(test_result::pass, "__test.result set to 1 and every vehicle stopped"); + finish(verdict->result, verdict->reason); return; } - if (Global.test.standstill > 0.0 && m_resttime >= Global.test.standstill) + if (m_progress.passrequested && false == requestedbefore) { - auto const reason { std::format("every vehicle stood still for {:.0f} s", Global.test.standstill) }; - finish(Global.test.pass_on_standstill ? test_result::pass : test_result::standstill, reason); - return; - } - if (Global.test.timeout > 0.0 && m_time >= Global.test.timeout) - { - finish(test_result::timeout, std::format("no result after {:.0f} s", Global.test.timeout)); - return; + WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for every vehicle to stop", m_progress.time, + frame.reason.empty() ? std::string() : std::format(" ({})", frame.reason))); } } @@ -149,7 +100,7 @@ void test_manager::signal_passed_at_danger(std::string_view const Vehicle, std:: } if (false == Global.test.fail_if_stop_overran) { - WriteLog(std::format("test: {} at {:.1f} s, allowed by test.fail_if_stop_overran", reason, m_time)); + 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); @@ -168,7 +119,7 @@ bool test_manager::conclude(test_result const Result, std::string_view const Rea m_finished = true; m_result = Result; - auto const line { std::format("test: {} (exit code {}) after {:.1f} s of simulation time: {}", to_string(Result), static_cast(Result), m_time, Reason) }; + 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 diff --git a/simulation/simulationtest.h b/simulation/simulationtest.h index 2b5012e9f..66384b6ed 100644 --- a/simulation/simulationtest.h +++ b/simulation/simulationtest.h @@ -9,32 +9,19 @@ http://mozilla.org/MPL/2.0/. #pragma once -#include +#include "simulation/simulationtestrules.h" + #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 { -// exit codes of a test run; public, scripts and CI read them -enum class test_result : int { - pass = 0, // __test.result set to 1, then every vehicle came to rest (or test.pass_on_standstill) - fail = 1, // __test.result set to 0 - signal_passed_at_danger = 2, // a train went past a stop signal (SetVelocity 0 0), unless test.fail_if_stop_overran no - timeout = 3, // test.timeout seconds of simulation time went by without a result - standstill = 4, // every vehicle stood still for test.standstill seconds without a result - error = 5, // the run could not start (no -s, the scenery failed to load) or the simulator threw -}; - -std::string_view to_string(test_result Result); - class test_manager { public: // name of the memory cell a scenery reports its result to static constexpr std::string_view result_cell { "__test.result" }; - // how long everything has to stand before a requested pass is granted [s] - static constexpr double pass_settle_time { 1.0 }; // fixed simulation time step of one frame in test mode [s]: 3 physics steps of 10 ms static constexpr double frame_step { 0.03 }; @@ -60,9 +47,7 @@ private: bool m_started { false }; bool m_finished { false }; test_result m_result { test_result::error }; - bool m_passrequested { false }; - double m_time { 0.0 }; // simulation time since the start [s] - double m_resttime { 0.0 }; // how long every vehicle has been standing [s] + test_progress m_progress; }; // the test of the run diff --git a/simulation/simulationtestrules.cpp b/simulation/simulationtestrules.cpp new file mode 100644 index 000000000..44f740f13 --- /dev/null +++ b/simulation/simulationtestrules.cpp @@ -0,0 +1,139 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#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") + { + 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; + } + result.timeout = seconds; + } + else if (takes_value(option)) + { + ++i; + } + else if (option == "-edit" && i + 1 < Arguments.size() && Arguments[i + 1][0] != '-') + { + // the scenery to edit is optional + ++i; + } + } + return result; +} + +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 every vehicle stopped"}; + if (Limits.standstill > 0.0 && Progress.resttime >= Limits.standstill) + return test_verdict{Limits.pass_on_standstill ? pass : standstill, std::format("every vehicle 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..5d966c3c2 --- /dev/null +++ b/simulation/simulationtestrules.h @@ -0,0 +1,82 @@ +/* +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 + +// the rules of the test mode (eu07 --test, eu07/maszyna#541) which need nothing of the running simulation: the exit +// codes, the command line and the end conditions. kept apart so the unit tests can check them +namespace simulation { + +// exit codes of a test run; public, scripts and CI read them +enum class test_result : int { + pass = 0, // __test.result set to 1, then every vehicle came to rest (or test.pass_on_standstill) + fail = 1, // __test.result set to 0 + signal_passed_at_danger = 2, // a train went past a stop signal (SetVelocity 0 0), unless test.fail_if_stop_overran no + timeout = 3, // test.timeout seconds of simulation time went by without a result + standstill = 4, // every vehicle stood still for test.standstill seconds without a result + error = 5, // 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::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); + +// 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 every vehicle 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 stands +}; + +// 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 every vehicle has been standing [s] + bool passrequested{false}; // __test.result was set to 1, the pass waits for the vehicles to stop +}; + +// how long everything has 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/tests/unit/test_test_mode.cpp b/tests/unit/test_test_mode.cpp new file mode 100644 index 000000000..fec475c5c --- /dev/null +++ b/tests/unit/test_test_mode.cpp @@ -0,0 +1,195 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "simulation/simulationtestrules.h" + +#include + +// the test mode of the simulator (eu07 --test, eu07/maszyna#541): its 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: seconds on the command line are a number, not negative") +{ + CHECK(simulation::parse_seconds("240") == 240.0); + CHECK(simulation::parse_seconds("0.5") == 0.5); + CHECK(simulation::parse_seconds("0") == 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 limit, in any order") + { + auto const commandline{parse({"-s", "td.scn", "--test-timeout", "240", "--test"})}; + CHECK(commandline.enabled); + CHECK(commandline.timeout == 240.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()); + } + 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-timeout"})}; + 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", "-seed", "--test"})}; + CHECK_FALSE(commandline.enabled); + CHECK(commandline.error.empty()); + } + SUBCASE("the scenery of -edit is optional") + { + CHECK(parse({"-edit", "--test", "-s", "td.scn"}).enabled); + CHECK_FALSE(parse({"-edit", "td.scn"}).enabled); + } +} + +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 == 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 every vehicle has 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 == 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); + } +} From baf6bc2a2080a2c6a691b4e0e119e346a15d6603 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:21:30 +0100 Subject: [PATCH 76/90] feat(simulation): the test mode's exit codes keep clear of 1 and 3 1 is what a process killed with TerminateProcess (and the plain failure of most programs) ends with, 3 what abort() ends with on Windows, so a fail or a timeout couldn't be told from a run that was killed or blew up. The codes other than pass now are 10 fail, 11 signal passed at danger, 12 timeout, 13 standstill and 14 error: none of them a code a process gets on its own, neither the shells' 126-127 nor 128 + a signal number on Linux. pass stays 0. --- simulation/simulationtestrules.h | 14 ++++++++------ tests/scenarios/README.md | 19 +++++++++++-------- tests/scenarios/run.py | 12 ++++++------ tests/unit/test_test_mode.cpp | 24 +++++++++++++++++++++++- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/simulation/simulationtestrules.h b/simulation/simulationtestrules.h index 5d966c3c2..99d35db7b 100644 --- a/simulation/simulationtestrules.h +++ b/simulation/simulationtestrules.h @@ -18,14 +18,16 @@ http://mozilla.org/MPL/2.0/. // codes, the command line and the end conditions. kept apart so the unit tests can check them namespace simulation { -// exit codes of a test run; public, scripts and CI read them +// 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 every vehicle came to rest (or test.pass_on_standstill) - fail = 1, // __test.result set to 0 - signal_passed_at_danger = 2, // a train went past a stop signal (SetVelocity 0 0), unless test.fail_if_stop_overran no - timeout = 3, // test.timeout seconds of simulation time went by without a result - standstill = 4, // every vehicle stood still for test.standstill seconds without a result - error = 5, // the run could not start (a bad command line, the scenery failed to load) or the simulator threw + 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 went by without a result + standstill = 13, // every vehicle 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); diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 8bd6f1a4a..acab07e79 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -62,13 +62,16 @@ The test ends with the first of these: | exit code | result | when | | --- | --- | --- | | 0 | pass | `__test.result` was set to 1 and then every vehicle 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` | -| 1 | fail | `__test.result` was set to 0; ends at once | -| 2 | 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` | -| 3 | timeout | `test.timeout` seconds of simulation time went by (default 3600) | -| 4 | standstill | every vehicle 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 | -| 5 | error | the run could not start: no `-s`, the scenery failed to load or has no vehicles, or the simulator threw | +| 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) | +| 13 | standstill | every vehicle 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). +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: @@ -180,8 +183,8 @@ and slides past the signal, so those cases set `test.fail_if_stop_overran no`. 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 2), `test_mode_timeout` (exit 3, from -`test.timeout`) and `test_mode_standstill` (exit 4, from `test.standstill`). +event), `test_mode_signal_passed_at_danger` (exit 11), `test_mode_timeout` (exit 12, from +`test.timeout`) and `test_mode_standstill` (exit 13, from `test.standstill`). `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. diff --git a/tests/scenarios/run.py b/tests/scenarios/run.py index 210d6c013..886e2e90d 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -52,14 +52,14 @@ 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/simulationtest.h +# exit codes of the simulator's test mode (eu07 --test), see simulation/simulationtestrules.h EXIT_RESULTS = { 0: "pass", - 1: "fail", - 2: "signal_passed_at_danger", - 3: "timeout", - 4: "standstill", - 5: "error", + 10: "fail", + 11: "signal_passed_at_danger", + 12: "timeout", + 13: "standstill", + 14: "error", } # settings layered over the user's eu07.ini for a test run; the window ones only matter diff --git a/tests/unit/test_test_mode.cpp b/tests/unit/test_test_mode.cpp index fec475c5c..609b16ff0 100644 --- a/tests/unit/test_test_mode.cpp +++ b/tests/unit/test_test_mode.cpp @@ -3,9 +3,10 @@ #include "simulation/simulationtestrules.h" +#include #include -// the test mode of the simulator (eu07 --test, eu07/maszyna#541): its command line and end conditions +// the test mode of the simulator (eu07 --test, eu07/maszyna#541): its exit codes, command line and end conditions namespace { @@ -37,6 +38,27 @@ std::optional run(simulation::test_progress &Progress, } // 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") == 240.0); From 7f8892cda6f02facd364d093992c48060bd1db09 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:21:57 +0100 Subject: [PATCH 77/90] fix(simulation): a test doesn't read the player's own eu07.ini The simulator reads eu07.ini from %APPDATA%/MaSzyna (~/.config/MaSzyna on Linux) when there is one, in place of the game folder's. A test run then depended on whose machine it ran on. With --test only the game folder's eu07.ini is read, then the files given with -ini. The test mode options are taken from the command line before the settings for that; --test-timeout still wins over test.timeout of any settings file. --- application/application.cpp | 24 +++++++++++++----------- tests/scenarios/README.md | 3 +++ tests/scenarios/run.py | 3 ++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/application/application.cpp b/application/application.cpp index f3c3fe46d..638eacdce 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -1200,17 +1200,6 @@ int eu07_application::init_settings(int Argc, char *Argv[]) { Global.asVersion = VERSION_INFO; - fs::path iniPath = user_config_path("eu07.ini"); - - if (!iniPath.empty() && fs::exists(iniPath)) - { - Global.LoadIniFile(iniPath.string().c_str()); - } - else - { - Global.LoadIniFile("eu07.ini"); - } - // a test run is known before the rest of the command line is read, 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))}; @@ -1226,6 +1215,19 @@ int eu07_application::init_settings(int Argc, char *Argv[]) Global.test.timeout_from_command_line = true; } + fs::path iniPath = user_config_path("eu07.ini"); + + // 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()); + } + else + { + Global.LoadIniFile("eu07.ini"); + } + // process command line arguments bool sceneryfromcommandline{false}; bool editwithoutscenery{false}; diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index acab07e79..ca45dc34d 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -19,6 +19,9 @@ eu07 --test -s [--test-timeout ] [-seed ] [- 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 diff --git a/tests/scenarios/run.py b/tests/scenarios/run.py index 886e2e90d..9cc6f84a9 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -62,7 +62,8 @@ EXIT_RESULTS = { 14: "error", } -# settings layered over the user's eu07.ini for a test run; the window ones only matter +# 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 From 1626df2bc49945680f596395f06f81256edd2130 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:22:37 +0100 Subject: [PATCH 78/90] fix(simulation): a scenery's config block can't turn back on what a test keeps off The config block of a scenery is read like the settings files, after the test mode settings were applied, so a scenery with soundenabled, multiplayer, execonexit, feedbackmode or the like in it got them back in a test run. The test mode settings now are applied again after each config block of the scenery; apply_test_mode_settings() moved to simulationtest.cpp for that. --- application/application.cpp | 52 +----------------------- simulation/simulationstateserializer.cpp | 5 +++ simulation/simulationtest.cpp | 46 +++++++++++++++++++++ simulation/simulationtest.h | 5 +++ tests/scenarios/README.md | 2 + 5 files changed, 59 insertions(+), 51 deletions(-) diff --git a/application/application.cpp b/application/application.cpp index 638eacdce..d0ef6a01a 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -31,9 +31,6 @@ http://mozilla.org/MPL/2.0/. #include "utilities/dictionary.h" #include "version_info.h" #include "simulation/simulationtest.h" -#ifdef _WIN32 -#include "Console.h" -#endif #include #include #include @@ -1149,53 +1146,6 @@ void eu07_application::init_files() } namespace fs = std::filesystem; -namespace -{ - -// the settings of the test mode (--test), over whatever the settings files say -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"); -} - -} // namespace - int eu07_application::init_settings(int Argc, char *Argv[]) { Global.asVersion = VERSION_INFO; @@ -1315,7 +1265,7 @@ int eu07_application::init_settings(int Argc, char *Argv[]) simulation::test().conclude(simulation::test_result::error, "--test needs a scenery given with -s"); return -1; } - apply_test_mode_settings(); + simulation::apply_test_mode_settings(); } return 0; 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 index 76aa1be30..67e05ad31 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -15,11 +15,16 @@ http://mozilla.org/MPL/2.0/. #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 @@ -33,6 +38,47 @@ test_manager &test() 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"); +} + void test_manager::init_result_cell() const { auto cell { std::make_unique(scene::node_data{0, -1, std::string(result_cell), "memcell"}) }; diff --git a/simulation/simulationtest.h b/simulation/simulationtest.h index 66384b6ed..e67b2adaf 100644 --- a/simulation/simulationtest.h +++ b/simulation/simulationtest.h @@ -53,4 +53,9 @@ private: // 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/tests/scenarios/README.md b/tests/scenarios/README.md index ca45dc34d..006a0fc69 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -32,6 +32,8 @@ With `--test` the simulator: - 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`), 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 From 14d25520f3e056b9279c0458e7a1487bc4fb2bea Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:23:02 +0100 Subject: [PATCH 79/90] fix(simulation): a test's standard output carries its result only The log was printed in colour to stdout too (debuglog bit 2), so a script reading the verdict of a test got the whole log with it. In test mode the log goes to the log file only and stdout has the `test: ...` line alone; the usage text of a bad command line goes to stderr. Whether the run is a test is now known before the log service starts. --- application/application.cpp | 11 +++++++---- tests/scenarios/README.md | 3 ++- utilities/Logs.cpp | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/application/application.cpp b/application/application.cpp index d0ef6a01a..4fe0911ac 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -322,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)); @@ -1150,10 +1153,9 @@ 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, so a command line it can't take - // ends with the error code of the test, not with 0, which would pass it + // 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))}; - Global.test.enabled = testcommandline.enabled; if (false == testcommandline.error.empty()) { simulation::test().conclude(simulation::test_result::error, testcommandline.error); @@ -1245,7 +1247,8 @@ int eu07_application::init_settings(int Argc, char *Argv[]) } 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]" diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 006a0fc69..d3f256782 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -40,7 +40,8 @@ With `--test` the simulator: 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, or a start that fails, ends - the same way with the `error` code. + 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 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()); From 03a417c7b0b4307546bec78a9cf0321abd573f17 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:33:36 +0100 Subject: [PATCH 80/90] feat(simulation): a real time watchdog ends a test run that hangs A test run that hung, in the loading or anywhere else, waited until whoever started it killed it. A watchdog thread now ends a run still going test.wall_timeout seconds of real time after the start of the simulator, loading included: it says `test: timeout ... still running after N s of real time` and quits with the timeout code, whatever the main thread is doing, with no dialog. A test that has ended but whose simulator doesn't close ends with its own result. The limit is test.wall_timeout (settings, the scenery's config block) or --test-wall-timeout, which wins. 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. test.timeout 0 means no watchdog unless the limit is given; test.wall_timeout 0 turns it off. run.py passes the case's timeout as --test-wall-timeout and kills a simulator only 30 s after it. A timeout from the watchdog counts as wall_timeout there, so a run that hung doesn't pass a case expecting the simulation time to run out; test_mode_wall_timeout ends while loading on a limit of 2 s. --- application/application.cpp | 9 ++- simulation/simulationtest.cpp | 73 +++++++++++++++++-- simulation/simulationtest.h | 17 ++++- simulation/simulationtestrules.cpp | 14 +++- simulation/simulationtestrules.h | 13 +++- tests/scenarios/README.md | 36 ++++++--- .../cases/test_mode_wall_timeout.json | 11 +++ tests/scenarios/run.py | 23 ++++-- tests/unit/test_test_mode.cpp | 34 ++++++++- utilities/Globals.cpp | 12 +++ utilities/Globals.h | 3 + 11 files changed, 210 insertions(+), 35 deletions(-) create mode 100644 tests/scenarios/cases/test_mode_wall_timeout.json diff --git a/application/application.cpp b/application/application.cpp index 4fe0911ac..860b8a235 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -1166,6 +1166,11 @@ int eu07_application::init_settings(int Argc, char *Argv[]) 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"); @@ -1231,7 +1236,7 @@ int eu07_application::init_settings(int Argc, char *Argv[]) // 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") + else if (token == "--test-timeout" || token == "--test-wall-timeout") { // taken from the command line above, with its value ++i; @@ -1253,7 +1258,7 @@ int eu07_application::init_settings(int Argc, char *Argv[]) << " [-edit [sceneryfilepath]]" << " [-seed number|word]" << " [-ini settingsfile]" - << " [--test [--test-timeout seconds]]" << std::endl; + << " [--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; } diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 67e05ad31..15cdf0e3f 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -27,9 +27,20 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include #include +namespace { + +// [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() @@ -77,6 +88,8 @@ void apply_test_mode_settings() // 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 @@ -94,7 +107,10 @@ void test_manager::start() if (m_started) return; m_started = true; - m_progress = test_progress {}; + { + 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 @@ -103,7 +119,9 @@ void test_manager::start() } // a pause set by the scenery or the settings would never be lifted, nobody is there to press the key Global.iPause = 0; - WriteLog(std::format("test: started, timeout {:.0f} s, standstill {:.0f} s{}{}{}", Global.test.timeout, Global.test.standstill, + auto const walllimit { m_walllimit.load() }; + WriteLog(std::format("test: started, timeout {:.0f} s, real time limit {}, standstill {:.0f} s{}{}{}", 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")); @@ -122,14 +140,21 @@ void test_manager::update(double const Deltatime) frame.reason = cell->Text(); } - auto const requestedbefore { m_progress.passrequested }; - auto const verdict { evaluate_frame(m_progress, frame, test_limits { Global.test.timeout, Global.test.standstill, Global.test.pass_on_standstill, Global.test.no_movement_before_pass }) }; + 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 (m_progress.passrequested && false == requestedbefore) + if (passrequested) { WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for every vehicle to stop", m_progress.time, frame.reason.empty() ? std::string() : std::format(" ({})", frame.reason))); @@ -160,10 +185,11 @@ void test_manager::finish(test_result const Result, std::string_view const Reaso 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_finished = true; 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) @@ -176,4 +202,39 @@ bool test_manager::conclude(test_result const Result, std::string_view const Rea 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 index e67b2adaf..a59ec87bf 100644 --- a/simulation/simulationtest.h +++ b/simulation/simulationtest.h @@ -11,7 +11,10 @@ http://mozilla.org/MPL/2.0/. #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 @@ -38,16 +41,26 @@ public: // 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 { return static_cast(m_finished ? m_result : test_result::error); } + 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); + + // the watchdog may end the test from its own thread + mutable std::mutex m_lock; bool m_started { false }; - bool m_finished { 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 diff --git a/simulation/simulationtestrules.cpp b/simulation/simulationtestrules.cpp index 44f740f13..eba1d7dd3 100644 --- a/simulation/simulationtestrules.cpp +++ b/simulation/simulationtestrules.cpp @@ -81,7 +81,7 @@ test_command_line parse_test_command_line(std::span const Arg { result.enabled = true; } - else if (option == "--test-timeout") + else if (option == "--test-timeout" || option == "--test-wall-timeout") { auto const seconds{option_seconds(Arguments, i)}; if (false == seconds.has_value()) @@ -91,7 +91,10 @@ test_command_line parse_test_command_line(std::span const Arg result.error = std::format("{} needs a number of seconds", option); continue; } - result.timeout = seconds; + if (option == "--test-timeout") + result.timeout = seconds; + else + result.wall_timeout = seconds; } else if (takes_value(option)) { @@ -106,6 +109,13 @@ test_command_line parse_test_command_line(std::span const Arg return result; } +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; diff --git a/simulation/simulationtestrules.h b/simulation/simulationtestrules.h index 99d35db7b..52adf27a8 100644 --- a/simulation/simulationtestrules.h +++ b/simulation/simulationtestrules.h @@ -15,7 +15,7 @@ http://mozilla.org/MPL/2.0/. #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 and the end conditions. kept apart so the unit tests can check them +// 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 @@ -25,7 +25,7 @@ enum class test_result : int { pass = 0, // __test.result set to 1, then every vehicle 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 went by without a result + timeout = 12, // test.timeout seconds of simulation time, or test.wall_timeout seconds of real time went by standstill = 13, // every vehicle 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 }; @@ -42,6 +42,7 @@ struct test_verdict { 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 }; @@ -52,6 +53,14 @@ std::optional parse_seconds(std::string_view Text); // 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); +// 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 diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index d3f256782..5eed983e1 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -13,7 +13,7 @@ The runner drives the simulator's test mode (eu07/maszyna#541), which works with runner too and is meant for scenery authors as well: ``` -eu07 --test -s [--test-timeout ] [-seed ] [-ini ] +eu07 --test -s [--test-timeout ] [--test-wall-timeout ] [-seed ] [-ini ] ``` With `--test` the simulator: @@ -70,7 +70,7 @@ The test ends with the first of these: | 0 | pass | `__test.result` was set to 1 and then every vehicle 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) | +| 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 | every vehicle 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 | @@ -84,6 +84,7 @@ 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 @@ -94,12 +95,22 @@ 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 every vehicle 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 every vehicle stands still; with `no` it ends the test at once | -`--test-timeout` on the command line wins over `test.timeout` of the scenery. +`--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 @@ -120,7 +131,7 @@ Python 3.10+, no extra packages. Options: `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 15 cases +- `-j N` runs N cases at once. Each case prints its results when it is done. All 16 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 @@ -138,13 +149,15 @@ For each case the runner: 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`, - plus `--test-timeout ` when the case gives a `duration`; the extra ini turns on +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`, when the scenario has loaded and no physicslog appeared, or - after the case's `timeout` of wall-clock time, + `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. @@ -167,9 +180,9 @@ scenery is newer. | `test_config` | lines of a `config` block added to the scenery, e.g. `["test.timeout 90"]` | | `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`. Default `timeout` for a case with `duration`, else `pass` | +| `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 | +| `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 | @@ -190,7 +203,8 @@ 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`) and `test_mode_standstill` (exit 13, from `test.standstill`). +`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_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. 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/run.py b/tests/scenarios/run.py index 9cc6f84a9..62cb7d135 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -61,6 +61,10 @@ EXIT_RESULTS = { 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 @@ -226,6 +230,8 @@ FATAL_LOG_LINES = ("Bad vehicle:", "Bad trainset:") # 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 @@ -246,7 +252,8 @@ def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog if watch: cmd = [str(exe), "-s", scenery, "-v", "ghostview", "-ini", str(ini)] else: - cmd = [str(exe), "--test", "-s", scenery, "-ini", str(ini)] + # 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) @@ -272,8 +279,9 @@ def run_simulator(exe: Path, game_dir: Path, scenery: str, ini: Path, physicslog break if watch and sim_t >= duration: break - if wall > timeout: - result.failure = f"timed out after {timeout:.0f}s before {duration}s of simulation" + # 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) @@ -506,8 +514,8 @@ def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, watch: bool # 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 EXIT_RESULTS.values(): - sys.exit(f"{name}: expect must be one of {', '.join(EXIT_RESULTS.values())}") + 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, @@ -530,8 +538,11 @@ def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, watch: bool 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 - say(f" {'PASS' if passed else 'FAIL'} exit code {describe_exit(result.exit_code)}, expected {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 if result.failure: say(f" FAIL {result.failure}") diff --git a/tests/unit/test_test_mode.cpp b/tests/unit/test_test_mode.cpp index 609b16ff0..55a90387e 100644 --- a/tests/unit/test_test_mode.cpp +++ b/tests/unit/test_test_mode.cpp @@ -80,17 +80,19 @@ TEST_CASE("test mode: the command line") CHECK_FALSE(commandline.enabled); CHECK(commandline.error.empty()); } - SUBCASE("--test and its limit, in any order") + SUBCASE("--test and its limits, in any order") { - auto const commandline{parse({"-s", "td.scn", "--test-timeout", "240", "--test"})}; + auto const commandline{parse({"--test-wall-timeout", "900", "-s", "td.scn", "--test", "--test-timeout", "240"})}; CHECK(commandline.enabled); CHECK(commandline.timeout == 240.0); + CHECK(commandline.wall_timeout == 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") { @@ -101,12 +103,17 @@ TEST_CASE("test mode: the command line") } SUBCASE("a limit without its value is an error") { - auto const commandline{parse({"--test", "-s", "td.scn", "--test-timeout"})}; + 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", "-seed", "--test"})}; + auto const commandline{parse({"-s", "--test", "-ini", "--test-timeout", "-v", "--test-wall-timeout", "-seed", "--test"})}; CHECK_FALSE(commandline.enabled); CHECK(commandline.error.empty()); } @@ -117,6 +124,25 @@ TEST_CASE("test mode: the command line") } } +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) == 120.0); + CHECK(simulation::wall_clock_limit(0.0, 3600.0) == 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) == 240.0 + simulation::wall_timeout_loading_allowance); + CHECK(simulation::wall_clock_limit(std::nullopt, 3600.0) == 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) == 0.0); + CHECK(simulation::wall_clock_limit(300.0, 0.0) == 300.0); + } +} + TEST_CASE("test mode: the end conditions") { simulation::test_limits limits; diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 3a9e10145..217374cb6 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -468,6 +468,15 @@ bool global_settings::ConfigParseTest(cParser& Parser, std::string_view const To 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.standstill") { ParseOne(Parser, test.standstill); @@ -1786,6 +1795,9 @@ global_settings::export_as_text( std::ostream &Output ) const { // 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 ); diff --git a/utilities/Globals.h b/utilities/Globals.h index 11d14e34f..81bc1e475 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" @@ -95,6 +96,8 @@ struct global_settings { 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 every vehicle stands still; with false it ends the test at once 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 From e5aafd154f058ae801093fe2032dbe56f3751e3e Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:39:52 +0100 Subject: [PATCH 81/90] feat(simulation): the pass and the standstill wait for the train under test only Both waited for every vehicle of the scenery to stand still, so on a scenery with AI traffic a test could pass only when the traffic happened to stand too, and the standstill end hardly ever came. A test scenery names the train under test with test.vehicle in its config block (once for each train, if several): the pass and the standstill wait for the consists of these vehicles as they are at the moment, so a train the tested one couples to counts with it. A name the scenery doesn't have ends the test with the error code. Without test.vehicle every vehicle counts, as before. A signal passed at danger counts for every train either way. run.py puts the case's lead vehicle into test.vehicle. test_mode_standstill_traffic has the train under test standing on track 2 of Stacja1 while AI traffic leaves track 1 on the timetable: it ends on its 100 s standstill, where counting every vehicle runs into the 300 s timeout. Physicslogs of the other trains of a case (_) are moved out of the game dir too. --- simulation/simulationtest.cpp | 38 +++++++++++++++++-- simulation/simulationtest.h | 2 + simulation/simulationtestrules.cpp | 4 +- simulation/simulationtestrules.h | 14 +++---- tests/scenarios/README.md | 28 +++++++++----- tests/scenarios/cases/td_traffic.scn | 13 +++++++ .../scenarios/cases/test_mode_standstill.json | 2 +- .../cases/test_mode_standstill_traffic.json | 18 +++++++++ tests/scenarios/run.py | 10 +++-- tests/unit/test_test_mode.cpp | 2 +- utilities/Globals.cpp | 14 +++++++ utilities/Globals.h | 5 ++- 12 files changed, 120 insertions(+), 30 deletions(-) create mode 100644 tests/scenarios/cases/td_traffic.scn create mode 100644 tests/scenarios/cases/test_mode_standstill_traffic.json diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 15cdf0e3f..89a8a1046 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -33,6 +33,11 @@ http://mozilla.org/MPL/2.0/. 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() { @@ -117,10 +122,25 @@ void test_manager::start() 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, timeout {:.0f} s, real time limit {}, standstill {:.0f} s{}{}{}", Global.test.timeout, + 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", @@ -132,8 +152,7 @@ void test_manager::update(double const Deltatime) if (false == m_started || m_finished) return; - test_frame frame { Deltatime, std::nullopt, {}, - std::ranges::none_of(simulation::Vehicles.sequence(), [](TDynamicObject const *Vehicle) { return std::abs(Vehicle->GetVelocity()) > 0.01; }) }; + 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(); @@ -156,11 +175,22 @@ void test_manager::update(double const Deltatime) } if (passrequested) { - WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for every vehicle to stop", m_progress.time, + 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) }; diff --git a/simulation/simulationtest.h b/simulation/simulationtest.h index a59ec87bf..784cd39e8 100644 --- a/simulation/simulationtest.h +++ b/simulation/simulationtest.h @@ -52,6 +52,8 @@ public: 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; diff --git a/simulation/simulationtestrules.cpp b/simulation/simulationtestrules.cpp index eba1d7dd3..cdfea0e3b 100644 --- a/simulation/simulationtestrules.cpp +++ b/simulation/simulationtestrules.cpp @@ -138,9 +138,9 @@ std::optional evaluate_frame(test_progress &Progress, test_frame c 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 every vehicle stopped"}; + 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("every vehicle stood still for {:.0f} s", 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; diff --git a/simulation/simulationtestrules.h b/simulation/simulationtestrules.h index 52adf27a8..153dc8a42 100644 --- a/simulation/simulationtestrules.h +++ b/simulation/simulationtestrules.h @@ -22,11 +22,11 @@ namespace simulation { // 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 every vehicle came to rest (or test.pass_on_standstill) + 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, // every vehicle stood still for test.standstill seconds without a result + 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 }; @@ -64,7 +64,7 @@ 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 every vehicle standing without a result; 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}; }; @@ -74,17 +74,17 @@ 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 stands + 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 every vehicle has been standing [s] - bool passrequested{false}; // __test.result was set to 1, the pass waits for the vehicles to stop + 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 everything has to stand before a requested pass is granted [s] +// 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 diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index 5eed983e1..c1226e637 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -67,11 +67,11 @@ The test ends with the first of these: | exit code | result | when | | --- | --- | --- | -| 0 | pass | `__test.result` was set to 1 and then every vehicle 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` | +| 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 | every vehicle 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 | +| 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 @@ -89,6 +89,7 @@ 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 ``` @@ -96,10 +97,11 @@ endconfig | --- | --- | --- | | `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 every vehicle still and no result before the test ends as `standstill`; 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 every vehicle stands still; with `no` it ends the test at once | +| `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. @@ -131,14 +133,15 @@ Python 3.10+, no extra packages. Options: `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 16 cases +- `-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 vehicle's physicslog) are moved to `tests/scenarios/out//`. +`stdout.txt`, the physicslogs of the vehicle and of other trains of the case) are moved to +`tests/scenarios/out//`. ## How it works @@ -177,7 +180,7 @@ scenery is newer. | `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"]` | +| `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` | @@ -187,15 +190,16 @@ scenery is newer. | `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. Other vehicles of the consist take names made from it, -e.g. `{{vehicle}}_s`. The td.scn cases share two fragments, `td_timetable.scn` (route set, +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 (every vehicle stops at 1397-1401 m). `td_red_signal_moving.scn` is the red signal one with the train +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` @@ -205,6 +209,10 @@ The `test_mode_*` cases show the ends of the test mode: `test_mode_pass` (exit 0 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. 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_standstill.json b/tests/scenarios/cases/test_mode_standstill.json index 0d96585ee..4dd891cf0 100644 --- a/tests/scenarios/cases/test_mode_standstill.json +++ b/tests/scenarios/cases/test_mode_standstill.json @@ -1,5 +1,5 @@ { - "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 every vehicle 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).", + "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", 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/run.py b/tests/scenarios/run.py index 62cb7d135..3757a8249 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -158,9 +158,10 @@ def build_scenery(case: dict, game_dir: Path, case_name: str, vehicle: str) -> s # 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) - if case.get("test_config"): - # settings of the test mode (test.timeout and so on), read like any other config block - before = "config\n" + "".join(line + "\n" for line in case["test_config"]) + "endconfig\n" + before + # 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: @@ -526,6 +527,9 @@ def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, watch: bool 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 diff --git a/tests/unit/test_test_mode.cpp b/tests/unit/test_test_mode.cpp index 55a90387e..27e8c0d96 100644 --- a/tests/unit/test_test_mode.cpp +++ b/tests/unit/test_test_mode.cpp @@ -167,7 +167,7 @@ TEST_CASE("test mode: the end conditions") CHECK(verdict->result == test_result::fail); CHECK(verdict->reason == "__test.result set to 0 (wrong_track)"); } - SUBCASE("1 passes once every vehicle has stood for a second") + 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); diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 217374cb6..8957a4a0e 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -477,6 +477,17 @@ bool global_settings::ConfigParseTest(cParser& Parser, std::string_view const To 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); @@ -1802,6 +1813,9 @@ global_settings::export_as_text( std::ostream &Output ) const { 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 81bc1e475..50e42085b 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -91,10 +91,11 @@ struct global_settings { 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] every vehicle standing still this long ends the test; 0 = never + 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 every vehicle stands still; with false it ends the test at once + 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 From 349ffb1caf0ec40a8c350a29e36ca7cce383e38a Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:40:13 +0100 Subject: [PATCH 82/90] fix(simulation): on Linux an exception ends a test run with its verdict too The runtime_error the simulator throws on a fatal problem was caught on Windows only. On Linux a test run aborted on it, with the code of abort() and no `test:` line. It is caught there too in test mode and ends the test with the error code and the reason; outside a test it is thrown on as before. --- EU07.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/EU07.cpp b/EU07.cpp index 0fbabba5b..4bc453867 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -164,20 +164,25 @@ int main(int argc, char *argv[]) { ErrorLog("Critical error, memory allocation failure: " + std::string(Error.what())); } -#ifdef _WIN32 catch (std::runtime_error const &Error) { +#ifndef _WIN32 + // outside a test the error ends the program as it always did there, uncaught + if (false == Global.test.enabled) + throw; +#endif ErrorLog(std::format("Critical error: {}", Error.what())); // in test mode nobody is there to close the box, the exit code tells of the error simulation::test().conclude(simulation::test_result::error, std::format("critical error: {}", Error.what())); +#ifdef _WIN32 if (false == Global.test.enabled) { std::string msg = "Simulator crash occured :(\n"; msg += Error.what(); MessageBoxA(nullptr, msg.c_str(), "Simulator crashed :(", MB_ICONERROR); } - } #endif + } } #ifndef _WIN32 fflush(stdout); From cea92f017011389d37deecf72926b61bbd291080 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 11:57:03 +0100 Subject: [PATCH 83/90] test(unit): the test mode tests compare seconds with a tolerance Floating point values of the command line, the real time limit and the standstill clock are checked with doctest::Approx rather than ==. --- tests/unit/test_test_mode.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/unit/test_test_mode.cpp b/tests/unit/test_test_mode.cpp index 27e8c0d96..a85d73e52 100644 --- a/tests/unit/test_test_mode.cpp +++ b/tests/unit/test_test_mode.cpp @@ -61,9 +61,9 @@ TEST_CASE("test mode: the exit codes keep clear of the ones of a killed or abort TEST_CASE("test mode: seconds on the command line are a number, not negative") { - CHECK(simulation::parse_seconds("240") == 240.0); - CHECK(simulation::parse_seconds("0.5") == 0.5); - CHECK(simulation::parse_seconds("0") == 0.0); + 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()); @@ -84,8 +84,8 @@ TEST_CASE("test mode: the command line") { auto const commandline{parse({"--test-wall-timeout", "900", "-s", "td.scn", "--test", "--test-timeout", "240"})}; CHECK(commandline.enabled); - CHECK(commandline.timeout == 240.0); - CHECK(commandline.wall_timeout == 900.0); + 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") @@ -128,18 +128,18 @@ 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) == 120.0); - CHECK(simulation::wall_clock_limit(0.0, 3600.0) == 0.0); + 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) == 240.0 + simulation::wall_timeout_loading_allowance); - CHECK(simulation::wall_clock_limit(std::nullopt, 3600.0) == 3600.0 + simulation::wall_timeout_loading_allowance); + 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) == 0.0); - CHECK(simulation::wall_clock_limit(300.0, 0.0) == 300.0); + 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)); } } @@ -152,7 +152,7 @@ TEST_CASE("test mode: the end conditions") { CHECK_FALSE(simulation::evaluate_frame(progress, frame(false), limits).has_value()); CHECK(progress.time == doctest::Approx(0.03)); - CHECK(progress.resttime == 0.0); + CHECK(progress.resttime == doctest::Approx(0.0)); } SUBCASE("a cell nobody wrote to says nothing") { @@ -207,7 +207,7 @@ TEST_CASE("test mode: the end conditions") 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 == 0.0); + 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") From a6f30c3a2a9ec81eccb83c8b07a5ac0cce12bcf5 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Sun, 8 Nov 2026 14:39:19 +0100 Subject: [PATCH 84/90] CLAUDE.md: a PR has to pass the Sonar quality gate, not fix every remark (#296) * docs(claude): a PR has to pass the Sonar quality gate, not fix every remark The section on SonarCloud asked for every remark on the changed code to be fixed, code smells included. It now asks for the quality gate: new code rated A for reliability, security and maintainability, duplication at most 3%, hotspots reviewed. A remark that doesn't fail the gate needn't be fixed, and large restructurings that only answer one stay out of the PR. The rules that keep coming up stay listed, as the way to keep the gate passing. * docs(claude): what the gate's ratings mean for bugs, smells and hotspots Every bug or vulnerability on new code drops its rating below A and fails the gate, code smells only count together through the maintainability rating, and a hotspot is reviewed in SonarCloud rather than by rewriting the code. --------- Co-authored-by: maj00r --- CLAUDE.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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`; From 3487e75f9affe9ba47e86cd219581aafddb86ddb Mon Sep 17 00:00:00 2001 From: pawelw Date: Sun, 8 Nov 2026 14:40:13 +0100 Subject: [PATCH 85/90] AI: the passenger stop flag no longer turns itself on and off (#295) Whether a train is at a passenger stop (IsAtPassengerStop) was checked against the final approach margins fMinProximityDist and fMaxProximityDist, which are themselves set from that flag: 10-15 m and 15-40 m at a stop, 8 m and 50 m otherwise. With the two sets different, the flag switched on and off on every other scan of the route on the way to a W4. Each time the train went from a final approach to a stop 50 m long to one 15 m long and back: braking in one scan, full power below the 10 km/h coasting speed in the next. Close to the stop the braking demand did not hold the train to a standstill, and below 1 km/h the stop counted as done, so an SN61 drove off without ever standing still (#290). The margins are now worked out in train_proximity_margins(), and the check of the stop uses the margins of a passenger stop whatever the flag is at the moment. A vehicle that is not a train keeps the present ones, as before. --- vehicle/Driver.cpp | 93 +++++++++++++++++++++++++--------------------- vehicle/Driver.h | 1 + 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 9f83b6d53..21aed82ab 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -829,6 +829,45 @@ void TController::TableCheck(double fDistance) 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 @@ -1152,14 +1191,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 ) { @@ -6677,47 +6722,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 diff --git a/vehicle/Driver.h b/vehicle/Driver.h index f1975fca6..68964711c 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 ); From 5f46aca43a41bb26d7822eb39756f5c3169b98a6 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 14:48:04 +0100 Subject: [PATCH 86/90] fix: a latching line breaker lever goes off with the breaker knocked out (#277) The EP05's single two-state line breaker lever stayed on once the converter overload relay knocked the breaker out: every press of the toggle tried to close the open breaker, which the tripped relay refused. A press now puts a lever left on over an open breaker off first. (eu07/maszyna#394) --- CMakeLists.txt | 1 + tests/unit/test_linebreaker.cpp | 114 ++++++++++++++++++++++++++++++++ vehicle/Train.cpp | 17 +++-- vehicle/linebreaker.cpp | 26 ++++++++ vehicle/linebreaker.h | 28 ++++++++ 5 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 tests/unit/test_linebreaker.cpp create mode 100644 vehicle/linebreaker.cpp create mode 100644 vehicle/linebreaker.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f34ad61f..a5e467d93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -464,6 +464,7 @@ set(CORE_SOURCES "scripting/ladderlogic.cpp" "vehicle/handover.cpp" "vehicle/horn.cpp" +"vehicle/linebreaker.cpp" "vehicle/smoke.cpp" ) add_library(eu07_core STATIC ${CORE_SOURCES}) 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/vehicle/Train.cpp b/vehicle/Train.cpp index e30635f4f..9fdb97bf9 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" @@ -3652,16 +3653,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) 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 From 22c588d8f99099947b6d3e89e50c4051f5c1ef62 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 14:48:42 +0100 Subject: [PATCH 87/90] feat(cab): 401Da oil pump lamp and slotted speedometer lever (eu07/maszyna#546) (#261) * feat: 401da oil pump lamp and slotted lever animation (eu07/maszyna#546) i-oilpump lights while the pre-lubrication oil pump works, so it goes off once the engine runs. animslottedlever: in the .mmd turns a lever by a pin on the coupling rod sliding in the lever's slot, which the speedometer drive of 401Da needs. The compressor start delay of #546 came with CompressorStartDelay (Brake section) of master. * fix: force GLM AVX2 only when the compiler targets AVX2 ENABLE_AVX2=OFF left GLM_FORCE_AVX2 defined, so MSVC still emitted AVX2 intrinsics and the exe crashed on CPUs without AVX2. * test: cover the compressor start delay and the oil pump lamp The delay of an engine stopped before it passed starts over, a compressor switched on by hand waits too, and the key is read from the Brake section. In the converter-fed set-up of 401Da the compressor waits for ConverterStartDelay and then its own delay. The oil pump (i-oilpump lamp) works before the engine start and stops once the engine runs. --- stdafx.h | 2 +- tests/unit/test_compressor.cpp | 90 ++++++++++++++++++++++++++++++++++ tests/unit/test_oil_pump.cpp | 55 +++++++++++++++++++++ vehicle/DynObj.cpp | 57 +++++++++++++++++++++ vehicle/DynObj.h | 10 ++++ vehicle/Train.cpp | 4 ++ vehicle/Train.h | 1 + 7 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_oil_pump.cpp 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/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_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/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 9fdb97bf9..5f27f2720 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -9044,6 +9044,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 @@ -9203,6 +9204,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); @@ -11236,6 +11238,7 @@ void TTrain::clear_cab_controls() btLampkaSprezarkaOff.Clear(); btLampkaSprezarkaBOff.Clear(); btLampkaFuelPumpOff.Clear(); + btLampkaOilPump.Clear(); btLampkaNapNastHam.Clear(); btLampkaOporyB.Clear(); btLampkaStycznB.Clear(); @@ -11658,6 +11661,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 009b349b4..e664c231a 100644 --- a/vehicle/Train.h +++ b/vehicle/Train.h @@ -760,6 +760,7 @@ public: // reszta może by?publiczna TButton btLampkaSprezarkaOff; TButton btLampkaSprezarkaBOff; TButton btLampkaFuelPumpOff; + TButton btLampkaOilPump; TButton btLampkaBocznik1; TButton btLampkaBocznik2; TButton btLampkaBocznik3; From 8cc8158d2a18aed45fd9a9a98be2f7e8df638fcb Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sun, 8 Nov 2026 16:12:51 +0100 Subject: [PATCH 88/90] AI: making for a passenger stop, leave room only for the brake's own fill time Since #187 the braking curve towards a stop or a lower speed leaves room for the time the brake takes to respond, as long as the train brake handle is still in the running position. The horizon it takes has the AI's old look-ahead, (1 - a0) * 30/3.6 s, about 7-8 s, as its floor; at 60 km/h that is over 100 m, up to half of the braking distance. Stops at signals were tuned with it. A vehicle that brakes with its independent brake (an SN61, a light engine) leaves the train brake handle where it is, so it kept that room the whole way in: it braked early and gently, came down to 10 km/h some 50 m out and crawled the rest, applying power and the brake in turn. Making for a passenger stop (the point the train is to stop at, found by the scan of the route, is a W4), the room now covers only the time the brake takes to fill (half its application time), without the look-ahead floor. Stops at signals and anything else keep the room as it was. When a vehicle ahead is closer than the stop the scan found, the train makes for that vehicle, so the room is the full one again. --- vehicle/Driver.cpp | 14 +++++++++++++- vehicle/Driver.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 21aed82ab..bf0e18318 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -882,6 +882,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 @@ -1001,6 +1002,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)) { @@ -1015,6 +1017,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 @@ -7377,6 +7380,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 ); @@ -7720,9 +7727,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 68964711c..dd5403789 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -404,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 From 57db0087ff43e898bd949525edd704ebf1576a78 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 18:30:22 +0100 Subject: [PATCH 89/90] fix: the SOD lamp of the whole consist is opt-in per vehicle (DoorClosureSignal) The three-state door closure lamp is switched on by DoorClosureSignal=Yes in the Doors: section of the .fiz of the vehicle whose cab shows the lamp (the occupied vehicle), off by default: without it the lamp keeps showing the lock of the vehicle's own doors. The eu07.ini switch doorclosuresignal is gone, the data decides. Sets whose cars lack DoorNeedPermit would blink at every standstill, so only the sets with permits get the key. --- McZapkie/MOVER.h | 4 +++ McZapkie/Mover.cpp | 7 +++++ tests/unit/test_door_closure.cpp | 48 ++++++++++++++++++++++++++++++++ utilities/Globals.cpp | 7 ----- utilities/Globals.h | 2 -- vehicle/Train.cpp | 9 +++--- 6 files changed, 64 insertions(+), 13 deletions(-) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 45e3305c0..a1a102075 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -1455,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 @@ -2610,6 +2611,9 @@ class TMoverParameters 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 03fa3e6db..ce3c08458 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -8723,6 +8723,11 @@ door_closure_t TMoverParameters::door_closure() const 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) @@ -10613,6 +10618,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/tests/unit/test_door_closure.cpp b/tests/unit/test_door_closure.cpp index abfa5a7e1..c92d8fef8 100644 --- a/tests/unit/test_door_closure.cpp +++ b/tests/unit/test_door_closure.cpp @@ -1,11 +1,14 @@ #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 { @@ -108,3 +111,48 @@ TEST_CASE("door closure: a locomotive has no doors to signal, its coach does") { 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/utilities/Globals.cpp b/utilities/Globals.cpp index a8aadec22..b4f2e14be 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -506,12 +506,6 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& return true; } - if (token == "doorclosuresignal") - { - ParseOne(Parser, DoorClosureSignal); - return true; - } - if (token == "movelight") { ParseOne(Parser, fMoveLight, 1, false); @@ -1579,7 +1573,6 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "skyenabled", asSky ); export_as_text( Output, "defaultext", szDefaultExt ); export_as_text( Output, "newaircouplers", bnewAirCouplers ); - export_as_text( Output, "doorclosuresignal", DoorClosureSignal ); export_as_text( Output, "anisotropicfiltering", AnisotropicFiltering ); export_as_text( Output, "usevbo", bUseVBO ); export_as_text( Output, "feedbackmode", iFeedbackMode ); diff --git a/utilities/Globals.h b/utilities/Globals.h index 971f8495a..ad216cd96 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -115,8 +115,6 @@ struct global_settings { std::string Period{}; // time of the day, based on sun position bool FullPhysics{ true }; // full calculations performed for each simulation step bool bnewAirCouplers{ true }; - bool DoorClosureSignal{ true }; // door closure signalling lamp (i-door_blocked) shows the doors of the whole consist in three states - float fMoveLight{ 0.f }; // numer dnia w roku albo -1 bool FakeLight{ false }; // toggle between fixed and dynamic daylight double fTimeSpeed{ 1.0 }; // przyspieszenie czasu, zmienna do testów diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 3a4c42db1..bbcdd1778 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -9108,11 +9108,12 @@ 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]); } - if (Global.DoorClosureSignal && DynamicObject->Mechanik != nullptr) + if (DynamicObject->Mechanik != nullptr) { - // 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 - btLampkaBlokadaDrzwi.Turn(door_closure_lamp(DynamicObject->Mechanik->DoorClosure, simulation::Time.data().wSecond % 2 < 1)); + // 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 { From 8fb596f2186cdbac2ed26d7b25a56f0f57da0ad0 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 19:46:16 +0100 Subject: [PATCH 90/90] fix(simulation): any exception ends a test run with error, other runs as on master main() tells a test run (--test) apart before anything runs. Only a test run catches every exception and installs a std::terminate handler, both ending the run with the error code and a test: line; any other run keeps master's exception handling, so on Linux an uncaught runtime_error aborts at the throw site again. A test run doesn't start the Discord RPC thread: it published the test scenery as the player's presence and held the closing up to 5 s. run.py drops a case scenery's .sbt cache after a run cut short (wall_timeout, error, a crash, killed by run.py), which could have been writing it. The -edit branch of the test command line parser had no effect (a token that isn't an option is ignored either way) and is gone; its test checks what can be seen instead. --- EU07.cpp | 78 +++++++++++++++++++----------- application/application.cpp | 8 ++- simulation/simulationtestrules.cpp | 23 +++++++-- simulation/simulationtestrules.h | 5 ++ tests/scenarios/README.md | 11 +++-- tests/scenarios/run.py | 12 ++++- tests/unit/test_test_mode.cpp | 29 ++++++++++- 7 files changed, 125 insertions(+), 41 deletions(-) diff --git a/EU07.cpp b/EU07.cpp index 4bc453867..20f24aa5a 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -23,7 +23,10 @@ Stele, firleju, szociu, hunter, ZiomalCl, OLI_EU and others #include "utilities/Globals.h" #include "utilities/Logs.h" #include -#include +#include +#include +#include +#include #ifdef WITHDUMPGEN #ifdef _WIN32 #include @@ -94,10 +97,45 @@ namespace void finish_log() { StopLogService(); - if (auto const logservice{Global.threads.find("LogService")}; logservice != Global.threads.end() && logservice->second.joinable()) + 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[]) @@ -149,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 @@ -164,37 +207,18 @@ int main(int argc, char *argv[]) { ErrorLog("Critical error, memory allocation failure: " + std::string(Error.what())); } +#ifdef _WIN32 catch (std::runtime_error const &Error) { -#ifndef _WIN32 - // outside a test the error ends the program as it always did there, uncaught - if (false == Global.test.enabled) - throw; -#endif - ErrorLog(std::format("Critical error: {}", Error.what())); - // in test mode nobody is there to close the box, the exit code tells of the error - simulation::test().conclude(simulation::test_result::error, std::format("critical error: {}", Error.what())); -#ifdef _WIN32 - if (false == Global.test.enabled) - { - std::string msg = "Simulator crash occured :(\n"; - msg += Error.what(); - MessageBoxA(nullptr, msg.c_str(), "Simulator crashed :(", MB_ICONERROR); - } -#endif + std::string msg = "Simulator crash occured :(\n"; + msg += Error.what(); + MessageBoxA(nullptr, msg.c_str(), "Simulator crashed :(", MB_ICONERROR); } +#endif } #ifndef _WIN32 fflush(stdout); fflush(stderr); #endif - if (Global.test.enabled) - { - // a run that stopped before its test ended (a failed start, an exception) says so like any other end - simulation::test().conclude(simulation::test_result::error, "the simulator stopped before the test ended"); - finish_log(); - } - // skip destructors, there are ordering errors which causes segfaults - // a test run (--test) says its result in the exit code - std::_Exit(Global.test.enabled ? simulation::test().exit_code() : 0); + std::_Exit(0); // skip destructors, there are ordering errors which causes segfaults } diff --git a/application/application.cpp b/application/application.cpp index 860b8a235..75ecde778 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -433,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()) diff --git a/simulation/simulationtestrules.cpp b/simulation/simulationtestrules.cpp index cdfea0e3b..fcedaed7d 100644 --- a/simulation/simulationtestrules.cpp +++ b/simulation/simulationtestrules.cpp @@ -100,15 +100,28 @@ test_command_line parse_test_command_line(std::span const Arg { ++i; } - else if (option == "-edit" && i + 1 < Arguments.size() && Arguments[i + 1][0] != '-') - { - // the scenery to edit is optional - ++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()) diff --git a/simulation/simulationtestrules.h b/simulation/simulationtestrules.h index 153dc8a42..684b90cde 100644 --- a/simulation/simulationtestrules.h +++ b/simulation/simulationtestrules.h @@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/. #pragma once +#include #include #include #include @@ -53,6 +54,10 @@ std::optional parse_seconds(std::string_view Text); // 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}; diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index c1226e637..0a11c6a0a 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -31,7 +31,8 @@ With `--test` the simulator: 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`), and the game's `imgui.ini` stays as it is, + 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 @@ -39,8 +40,9 @@ With `--test` the simulator: - 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, or a start that fails, ends - the same way with the `error` code. That line is all there is on stdout: the log goes to + 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 @@ -167,7 +169,8 @@ For each case the runner: 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. +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 diff --git a/tests/scenarios/run.py b/tests/scenarios/run.py index 3757a8249..18b3e3f2e 100644 --- a/tests/scenarios/run.py +++ b/tests/scenarios/run.py @@ -137,6 +137,11 @@ def fill_vars(fragment: str, values: dict, keep=frozenset()) -> str: 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" @@ -174,7 +179,7 @@ def build_scenery(case: dict, game_dir: Path, case_name: str, vehicle: str) -> s # 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_dir / f"autotest_{case_name}.sbt" + 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 @@ -548,6 +553,11 @@ def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, watch: bool 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 diff --git a/tests/unit/test_test_mode.cpp b/tests/unit/test_test_mode.cpp index a85d73e52..eba641096 100644 --- a/tests/unit/test_test_mode.cpp +++ b/tests/unit/test_test_mode.cpp @@ -4,6 +4,9 @@ #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 @@ -117,10 +120,13 @@ TEST_CASE("test mode: the command line") CHECK_FALSE(commandline.enabled); CHECK(commandline.error.empty()); } - SUBCASE("the scenery of -edit is optional") + 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_FALSE(parse({"-edit", "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); } } @@ -241,3 +247,22 @@ TEST_CASE("test mode: the end conditions") 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"); + } +}