18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

fix: SonarQube findings of the buffer stops

- The notice of a finished hit is taken out of the contact with std::exchange before it goes to the scenery, so
  what the scenery does with it can't reach the one held in the contact.
- The loops count whole steps (an int, the number of steps taken with ceil) instead of adding a floating step.
- The local final of bufferstop::contact() is ending (final is a contextual keyword).

Unit tests 1294/1294.
This commit is contained in:
jerrrrycho
2026-12-02 17:04:29 +01:00
parent 9ecbd110da
commit 885c28c1ef
4 changed files with 22 additions and 15 deletions
+8 -8
View File
@@ -357,7 +357,7 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input
auto penetration{Input.penetration};
auto vehiclecrush{Input.vehicle_end.crush};
auto time{0.0};
auto final{outcome::none};
auto ending{outcome::none};
while (Input.dt - time > 1e-12) {
auto const step{std::min(length, Input.dt - time)};
auto deflection{penetration - (now.absorber + now.slid + now.crush + vehiclecrush)};
@@ -409,7 +409,7 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input
if (now.slid >= Stop.runout - 1e-9) {
// slid off the end of its run-out: it goes on with the vehicle, holding nothing
now.overrun = true;
final = outcome::overrun;
ending = outcome::overrun;
}
}
}
@@ -453,11 +453,11 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input
result.climbed = true;
result.work += climbed.work;
velocity = climbed.velocity;
final = climbed.result;
ending = climbed.result;
}
else if (now.crush >= Stop.demolish) {
now.demolished = true;
final = outcome::demolished;
ending = outcome::demolished;
}
break;
}
@@ -483,12 +483,12 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input
result.climbed = true;
result.work += climbed.work;
velocity = climbed.velocity;
final = climbed.result;
ending = climbed.result;
break;
}
if (now.crush >= Stop.demolish) {
now.demolished = true;
final = outcome::demolished;
ending = outcome::demolished;
break;
}
}
@@ -501,8 +501,8 @@ contact_result contact(definition const &Stop, state &Stop_state, contact_input
result.impulse = Input.mass * (Input.closing - velocity);
result.vehicle_crush = vehiclecrush - Input.vehicle_end.crush;
now.peak_force = std::max(now.peak_force, result.force);
if (final != outcome::none) {
result.result = final;
if (ending != outcome::none) {
result.result = ending;
}
else if (result.touched) {
result.result = now.crush >= collision::crush_mark ? outcome::damaged : outcome::held;
+3 -1
View File
@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include <algorithm>
#include <cmath>
#include <format>
#include <string>
@@ -116,7 +117,8 @@ void physics::Vehicle::buffer_stop_contact(double const dt)
auto would{bufferstop::outcome::none};
auto vehiclecrush{0.0};
input.dt = monitor_step;
for (auto time = 0.0; time < monitor_time; time += monitor_step)
auto const steps{static_cast<int>(std::ceil(monitor_time / monitor_step - 1e-9))};
for (auto index{0}; index < steps; ++index)
{
auto const step{bufferstop::contact(installation.stop, state, input)};
would = bufferstop::worse(would, step.result);
+6 -3
View File
@@ -60,7 +60,8 @@ run_result run(bs::definition const &Stop, bs::contact_input Input, double const
Input.closing = Speed;
Input.penetration = -0.01;
auto touched{false};
for (auto time = 0.0; time < 120.0; time += Input.dt) {
auto const steps{static_cast<int>(std::ceil(120.0 / Input.dt))};
for (auto index{0}; index < steps; ++index) {
auto const step{bs::contact(Stop, result.stop, Input)};
result.worst = bs::worse(result.worst, step.result);
result.peak = std::max(result.peak, step.force);
@@ -87,7 +88,8 @@ run_result run(bs::definition const &Stop, bs::contact_input Input, double const
// the lowest speed [km/h], in steps of a quarter, the vehicle rides up the stop at
std::optional<double> ride_up_speed(bs::definition const &Stop, bs::contact_input const &Input) {
for (auto speed = 4.0; speed <= 20.0; speed += 0.25) {
for (auto quarter{16}; quarter <= 80; ++quarter) {
auto const speed{0.25 * quarter};
if (run(Stop, Input, speed * kmh).climbed) {
return speed;
}
@@ -153,7 +155,8 @@ TEST_CASE("bufferstop: the brake elements of a sliding stop hold less the longer
// over the 8 m of its run-out the 1848 kJ of the calculation of Rawie 10 EB
auto work{0.0};
constexpr double step{0.001};
for (auto slid = 0.5 * step; slid < 8.0; slid += step) {
for (auto index{0}; index < 8000; ++index) {
auto const slid{(index + 0.5) * step};
work += bs::friction_force(stop, slid) * step;
}
CHECK(work == doctest::Approx(1848.0e3).epsilon(0.001));
+5 -3
View File
@@ -21,6 +21,8 @@ http://mozilla.org/MPL/2.0/.
#include <algorithm>
#include <cmath>
#include <memory>
#include <optional>
#include <utility>
namespace {
@@ -37,9 +39,9 @@ void TDynamicObject::update_track_ends() {
for (auto const end : VehicleEnds) {
auto &contact{physics->TrackEnds[end]};
// the scenery is told of the hit of the frame before, on the main thread
if (contact.pending) {
simulation::State.report_buffer_stop(*this, *contact.pending);
contact.pending.reset();
// taken out first: what the scenery does with it can't reach the one held here
if (auto const notice{std::exchange(contact.pending, std::nullopt)}; notice.has_value()) {
simulation::State.report_buffer_stop(*this, *notice);
}
}
auto const active{simulation::Paths.buffer_stops() > 0 && Global.buffer_stops != global_settings::bufferstop_mode::off &&