18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 10:05:23 +01:00
Files
maszyna/vehicle/dynamic/bufferstop.cpp
T
jerrrrycho 7578f2c3a0 feat(physics): buffer stops at the free ends of the tracks
A free end of a rail track marked bufferstop1 / bufferstop2 <kind> in the
scenery is a buffer stop (physics/bufferstop.h): rigid, buffered, hydraulic
or friction. With physics.collisions new a vehicle running into it meets the
stages of the stop in series: the buffers of both, the absorber or the brake
elements of a sliding stop, then the frame, whose hit goes through
collision::hit() and whose ride-up goes through collision::override_of(),
the stop an immovable body. It is held, the frame deforms, it rides up
(Enfield 2021), demolishes the stop (Kirkby 2021) or overruns a sliding one.
The log tells each hit once, and the memory cells __simulation.bufferstop
and <track>:bufferstop and their events tell the scenery.

physics.bufferstops marked (the default) takes the marked ends only, auto
also the ends with a model of a stop within 3 m, off none. Every other free
end keeps the derailment off the end of the track, and so does every end
with physics.collisions old; monitor logs what the stop would have done.
2026-12-02 12:33:36 +01:00

121 lines
3.8 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 buffer stops ahead of the ends of the vehicle (physics.bufferstops, physics/bufferstop.h)
#include "stdafx.h"
#include "vehicle/DynObj.h"
#include "physics/vehicle.h"
#include "physics/bufferstop.h"
#include "simulation/simulation.h"
#include "utilities/Globals.h"
#include "world/Track.h"
#include "world/bufferstops.h"
#include <algorithm>
#include <cmath>
#include <memory>
namespace {
struct found_stop {
TTrack const *track{nullptr};
int end{0};
std::shared_ptr<bufferstop::installation> stop;
};
} // namespace
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();
}
}
auto const active{simulation::Paths.buffer_stops() > 0 && Global.buffer_stops != global_settings::bufferstop_mode::off &&
Global.collisions != global_settings::collision_model::old && TestFlag(physics->CategoryFlag, 1) &&
false == physics->DamageFlag.HasAll(VehicleDamage::derailed) && RaTrackGet() != nullptr};
for (auto const end : VehicleEnds) {
auto &contact{physics->TrackEnds[end]};
auto const previous{contact.stop};
contact.stop = nullptr;
contact.gap = 10000.0;
auto const &coupler{physics->Couplers[end]};
if (false == active || coupler.Connected != nullptr) {
// coupled, the vehicle there stands between this end and any stop
continue;
}
// the route of the end along the tracks from the active axle, as find_vehicle() takes it, up to the free end with a
// stop. the range of the scan of the neighbours
auto const range{std::max(10.0, std::abs(physics->V())) + 40.0};
auto direction{end == VehicleEnd::front ? 1 : -1};
auto const *track{RaTrackGet()};
if (RaDirectionGet() < 0) {
direction = -direction;
}
auto distance{direction > 0 ? track->Length() - RaTranslationGet() : RaTranslationGet()};
found_stop found;
while (track != nullptr) {
auto const trackend{direction > 0 ? 1 : 0};
if (auto const &stop{track->bufferstop(trackend)}; stop != nullptr) {
found = {track, trackend, stop};
break;
}
if (distance > range) {
break;
}
TTrack const *next{nullptr};
if (direction > 0) {
if (track->iNextDirection) {
direction = -direction;
}
next = track->CurrentNext();
}
else {
if (false == track->iPrevDirection) {
direction = -direction;
}
next = track->CurrentPrev();
}
if (next == nullptr || TestFlag(next->iCategoryFlag, 0x80)) {
// a free end without a stop, or the piece past it made for a vehicle that ran off it
break;
}
track = next;
distance += track->Length();
}
if (found.stop == nullptr || found.stop->now.spent()) {
// none, or a wreck: the end is a free one
continue;
}
// the gap from the buffer face of the end to the face of the stop, along the track at the end
auto const outward{bufferstops::outward(*found.track, found.end)};
auto const face{bufferstops::face(*found.track, found.end, found.stop->stop.setback)};
auto const gap{glm::dot(face - vCoulpler[end], outward) - coupler.adapter_length};
auto const &neighbour{physics->Neighbours[end]};
if (neighbour.vehicle != nullptr && neighbour.distance < gap) {
// a vehicle in between is the obstacle
continue;
}
if (previous != found.stop) {
// another stop: no hit under way with it
contact.touching = false;
contact.reported = false;
contact.worst = bufferstop::outcome::none;
contact.vehicle_crush = 0.0;
}
contact.stop = found.stop;
contact.gap = gap;
}
}