18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 08:55:22 +01:00
Files
maszyna/physics/components/bufferstops.cpp
T
jerrrrycho 885c28c1ef 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.
2026-12-02 17:04:29 +01:00

200 lines
7.5 KiB
C++

/*
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/.
*/
// the ends of a vehicle against the buffer stops at the free ends of the tracks (physics.bufferstops), see
// physics/bufferstop.h
#include "stdafx.h"
#include "physics/vehicle.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include <algorithm>
#include <cmath>
#include <format>
#include <string>
namespace
{
// the vehicle went on, off the stop, by more than this: its next touch is another hit [m]
constexpr double apart_margin{0.1};
// physics.collisions monitor: the hit is followed for this long at most, in steps of this length [s]
constexpr double monitor_time{20.0};
constexpr double monitor_step{0.01};
std::string_view outcome_words(bufferstop::outcome const Outcome)
{
switch (Outcome)
{
case bufferstop::outcome::rode_up: return "rode up";
case bufferstop::outcome::none: return "held";
default: return bufferstop::outcome_name(Outcome);
}
}
// what the log tells of the stop after the hit
std::string stop_report(bufferstop::installation const &Stop, double const VehicleCrush)
{
return std::format("stop deformed {:.2f} m, peak {:.0f} kN, slid {:.2f} m, absorber {:.2f} m, end crushed {:.2f} m", Stop.now.crush, Stop.now.peak_force / 1000.0, Stop.now.slid, Stop.now.absorber,
VehicleCrush);
}
} // namespace
void physics::Vehicle::buffer_stop_contact(double const dt)
{
if (Global.buffer_stops == global_settings::bufferstop_mode::off || Global.collisions == global_settings::collision_model::old || false == TestFlag(CategoryFlag, 1))
{
return;
}
for (auto const end : VehicleEnds)
{
auto &contact{TrackEnds[end]};
if (contact.stop == nullptr || DamageFlag.HasAll(VehicleDamage::derailed))
{
continue;
}
auto &installation{*contact.stop};
// the gap now: the one found at the start of the frame, and the move of the vehicle since, as for the gap to a
// neighbour (coupler_gap_change())
auto const gap{contact.gap + (end == VehicleEnd::front ? -moveLength : moveLength)};
if (gap > apart_margin)
{
// off the stop: the next touch is another hit
contact.touching = false;
contact.reported = false;
contact.worst = bufferstop::outcome::none;
contact.vehicle_crush = 0.0;
}
if (gap >= 0.0 || installation.now.spent())
{
// apart, the stop as it stands: the absorber extended again, the buffers unloaded
if (false == installation.now.spent())
{
installation.now.absorber = 0.0;
installation.now.elastic.reset();
installation.now.absorbed = 0.0;
installation.now.sliding = false;
}
continue;
}
auto const direction{end == VehicleEnd::front ? 1.0 : -1.0};
auto const closing{direction * travelSpeed};
auto const &coupler{Couplers[end]};
auto const kind{collision_kind()};
auto const curve{crush_curve_at(end)};
bufferstop::contact_input input;
input.closing = closing;
input.penetration = -gap;
input.mass = totalMass;
input.vehicle_end = {curve, coupler.crush};
input.buffers = coupler.physics.buffers;
// an automatic coupler (or its adapter) has no buffers at the side: its draft gear takes in what its springs do
auto const central{coupler.type() == TCouplerType::Automatic || coupler.has_adapter()};
input.vehicle_capacity = central ? 0.0 : collision::buffer_capacity(coupler.collision, kind);
input.vehicle_height = buffer_height_at(end);
input.anticlimber = collision::anticlimber_of(coupler.collision, kind);
input.zone = curve.densify;
input.stiffness = std::max(coupler.physics.link_stiffness, 1.0);
input.dt = dt;
auto const describe = [&]() { return std::format("{} hit the buffer stop of {} ({})", Name, installation.track, bufferstop::kind_name(installation.stop.type)); };
if (Global.collisions == global_settings::collision_model::monitor)
{
// the legacy behaviour goes on, the vehicle runs off the end; the log tells what the whole hit would be
if (contact.reported || closing <= 0.0)
{
continue;
}
contact.reported = true;
auto state{installation.now};
auto would{bufferstop::outcome::none};
auto vehiclecrush{0.0};
input.dt = 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);
vehiclecrush += step.vehicle_crush;
input.closing = step.velocity;
input.penetration += step.travel;
input.vehicle_end.crush += step.vehicle_crush;
if (state.spent() || step.climbed || (step.touched && step.velocity <= 0.0))
{
break;
}
}
auto copy{installation};
copy.now = state;
WriteLog(std::format("bufferstop monitor: {} at {:.0f} km/h: would {}, {}", describe(), closing * 3.6, outcome_words(would), stop_report(copy, vehiclecrush)));
continue;
}
auto const result{bufferstop::contact(installation.stop, installation.now, input)};
if (false == result.touched)
{
continue;
}
if (false == contact.touching)
{
// a new hit
contact.touching = true;
contact.reported = false;
contact.speed = closing * 3.6;
contact.worst = bufferstop::outcome::none;
contact.vehicle_crush = 0.0;
}
contact.worst = bufferstop::worse(contact.worst, result.result);
// the hit changes the velocity alone, as a collision of a pair does (CollisionDetect())
travelSpeed = direction * result.velocity;
if (result.vehicle_crush > 0.0)
{
// the end of the vehicle crushed for good, the body shorter by it off the track; crushed past the draw gear,
// its coupler is gone
auto &thiscoupler{Couplers[end]};
auto const before{thiscoupler.crush};
thiscoupler.crush += result.vehicle_crush;
contact.vehicle_crush += result.vehicle_crush;
if (before < collision::crush_mark && thiscoupler.crush >= collision::crush_mark)
{
ErrorLog(std::format("Bad driving: {} at {:.0f} km/h, the {} end of {} crushed by {:.2f} m", describe(), contact.speed, end == VehicleEnd::front ? "front" : "rear", Name, thiscoupler.crush));
}
OffTrack.crushed(Couplers[VehicleEnd::front].crush, Couplers[VehicleEnd::rear].crush);
if (thiscoupler.crush >= collision::draw_gear_crush && false == thiscoupler.broken && thiscoupler.CouplingFlag == Coupling::none)
{
damage_coupler(end);
}
}
if (result.climbed)
{
// it rode up onto the stop: off the rails, with what the stop left it of its speed
derail_in_collision(COLLISION_OVERRIDE, std::format("{} at {:.0f} km/h and rode up onto it, its buffers at {:.0f} mm, the beam at {:.0f} mm; {}", describe(), contact.speed,
input.vehicle_height * 1000.0, installation.stop.height * 1000.0, stop_report(installation, contact.vehicle_crush)));
travelSpeed = direction * result.residual;
}
// the hit is over once the vehicle stopped or bounced off, or went on past the stop: the log and the scenery are
// told of it once
if (false == contact.reported && (result.velocity <= 0.0 || result.climbed || installation.now.spent()))
{
contact.reported = true;
auto const text{std::format("bufferstop: {} at {:.0f} km/h: {}, {}", describe(), contact.speed, outcome_words(contact.worst), stop_report(installation, contact.vehicle_crush))};
if (contact.worst == bufferstop::outcome::held)
{
WriteLog(text);
}
else
{
ErrorLog(text);
}
contact.pending = bufferstop::notice{Name, installation.track, contact.worst, contact.speed};
}
}
}