diff --git a/tests/unit/test_derail_motion.cpp b/tests/unit/test_derail_motion.cpp index 6fff98507..9b5418f7a 100644 --- a/tests/unit/test_derail_motion.cpp +++ b/tests/unit/test_derail_motion.cpp @@ -517,7 +517,7 @@ std::vector run_train(std::vector> const constexpr double stiffness{2e6}; auto const damping{0.3 * 2.0 * std::sqrt(stiffness * Shape.mass)}; constexpr double slack{0.05}; - for (double time = 0.0; time < 30.0; time += Dt) { + for (int step = 0; step * Dt < 30.0; ++step) { std::vector> ends(train.size()); for (std::size_t index = 0; index + 1 < train.size(); ++index) { auto &ahead{train[index]}; @@ -902,7 +902,7 @@ hit_outcome run_into(double const Speed, double const Angle, double const Across standing.front = glm::dvec3{std::sin(Angle), 0.0, std::cos(Angle)}; second.body.begin(standing, coach(), ballast); hit_outcome result; - for (double time = 0.0; time < 30.0; time += Dt) { + for (int step = 0; step * Dt < 30.0; ++step) { result.most = std::max(result.most, touch(first, second)); slide_on(first, Dt); slide_on(second, Dt); @@ -1032,9 +1032,9 @@ TEST_CASE("derail motion: two bodies folded past a right angle stay apart") { second.body.begin(other, coach(), ballast); second.speed = 5.0; auto most{0.0}; - for (double time = 0.0; time < 20.0; time += 0.01) { + for (int step = 0; step < 2000; ++step) { // 20 s in steps of 0.01 s auto const depth{touch(first, second)}; - if (time > 0.5) { + if (step > 50) { // after half a second most = std::max(most, depth); } slide_on(first, 0.01); diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 385db88fa..63dc54718 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -3698,23 +3698,34 @@ bool TDynamicObject::Update(double dt, double dt1) return true; // Ra: pauza // physics.derailment new: all of the update of a vehicle off the track, timed; a warning once a second at most when it // takes long, to find what makes a derailment slow - struct off_track_watch { - TDynamicObject &vehicle; - double frame; - off_track_clock::time_point started { off_track_clock::now() }; - ~off_track_watch() { - auto const took { seconds_since( started ) }; - vehicle.m_offtrackupdatedue -= frame; - if( took > off_track_slow && vehicle.m_offtrackupdatedue <= 0.0 ) { - vehicle.m_offtrackupdatedue = 1.0; - ErrorLog( std::format( "derailment: warning: the update of {} off the track took {:.0f} ms in a frame of {:.3f} s", vehicle.MoverParameters->Name, took * 1000.0, frame ) ); + // (no vehicle when it is on the track: nothing is timed then) + class off_track_watch { + public: + off_track_watch( TDynamicObject *Vehicle, double const Frame ) : + vehicle { Vehicle }, frame { Frame } { + if( vehicle != nullptr ) { + started = off_track_clock::now(); } } + off_track_watch( off_track_watch const & ) = delete; + off_track_watch &operator=( off_track_watch const & ) = delete; + ~off_track_watch() { + if( vehicle == nullptr ) { + return; + } + auto const took { seconds_since( started ) }; + vehicle->m_offtrackupdatedue -= frame; + if( took > off_track_slow && vehicle->m_offtrackupdatedue <= 0.0 ) { + vehicle->m_offtrackupdatedue = 1.0; + ErrorLog( std::format( "derailment: warning: the update of {} off the track took {:.0f} ms in a frame of {:.3f} s", vehicle->MoverParameters->Name, took * 1000.0, frame ) ); + } + } + private: + TDynamicObject *vehicle; + double frame; + off_track_clock::time_point started {}; }; - std::optional watch; - if( MoverParameters->OffTrack.active() ) { - watch.emplace( *this, dt1 ); - } + off_track_watch const watch { MoverParameters->OffTrack.active() ? this : nullptr, dt1 }; if (!MoverParameters->PhysicActivation && !MechInside) { // to drugie, bo będąc w maszynowym blokuje się fizyka // physics.derailment new: a vehicle off the track stopped along its heading still settles: it slides down a diff --git a/vehicle/derailmotion.cpp b/vehicle/derailmotion.cpp index 9ef439931..9e870ed05 100644 --- a/vehicle/derailmotion.cpp +++ b/vehicle/derailmotion.cpp @@ -105,7 +105,7 @@ double departure_angle(cause const Cause, double const Speed, double const Accel scatter draw_scatter(std::uint32_t const Seed) { // the raw numbers of the engine are the same with every library, the distributions of aren't - std::mt19937 engine{Seed}; + std::mt19937 engine{Seed}; // NOSONAR: seeded on purpose, so that a derailment can be repeated; nothing secret is drawn auto const uniform = [&engine](double const Low, double const High) { return Low + (High - Low) * ((static_cast(engine()) + 0.5) / 4294967296.0); }; scatter result; result.departure = uniform(-0.01, 0.01);