18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 10:05:23 +01:00
Files
maszyna/world/bufferstops.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

182 lines
6.0 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 at the free ends of the rail tracks (physics.bufferstops, physics/bufferstop.h): those the scenery
// marks on its tracks, and with auto those its models of the stops give
#include "stdafx.h"
#include "world/bufferstops.h"
#include <algorithm>
#include <cmath>
#include <format>
#include <memory>
#include <optional>
#include <vector>
#include "world/Track.h"
#include "physics/bufferstop.h"
#include "model/AnimModel.h"
#include "model/Model3d.h"
#include "simulation/simulation.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
namespace bufferstops {
glm::dvec3 end_point(TTrack const &Track, int const End) {
auto const &segment{Track.CurrentSegment()};
return End == 0 ? segment->FastGetPoint_0() : segment->FastGetPoint_1();
}
glm::dvec3 outward(TTrack const &Track, int const End) {
// from a point a metre inside the track, or half of it for a shorter one, to the end
auto const length{Track.Length()};
auto const back{std::min(1.0, 0.5 * length)};
glm::dvec3 inner;
glm::vec3 rotation;
Track.CurrentSegment()->RaPositionGet(End == 0 ? back : length - back, inner, rotation);
auto direction{end_point(Track, End) - inner};
direction.y = 0.0;
auto const size{glm::length(direction)};
return size > 1e-6 ? direction / size : glm::dvec3{0.0, 0.0, 1.0};
}
glm::dvec3 face(TTrack const &Track, int const End, double const Setback) {
return end_point(Track, End) - Setback * outward(Track, End);
}
} // namespace bufferstops
namespace {
// a stop stands only at the free end of a plain rail track: not on a switch or a road, nor at the piece the vehicles
// running off an end make past it (auto_null)
bool takes_stop(TTrack const &Track) {
return Track.eType == tt_Normal && TestFlag(Track.iCategoryFlag, 1) && false == TestFlag(Track.iCategoryFlag, 0x80);
}
bool is_free(TTrack const &Track, int const End) {
return (End == 0 ? Track.CurrentPrev() : Track.CurrentNext()) == nullptr;
}
// a model must stand about level with the end: not on a bridge over it, nor under it [m]
constexpr double model_height_reach{3.0};
} // namespace
void path_table::InitBufferStops() {
m_bufferstopcount = 0;
if (Global.buffer_stops == global_settings::bufferstop_mode::off) {
return;
}
auto const install = [this](TTrack &Track, int const End, bufferstop::definition const &Stop, std::string const &Source) {
auto stop{std::make_shared<bufferstop::installation>()};
stop->stop = Stop;
stop->track = Track.name();
stop->end = End;
stop->source = Source;
Track.bufferstop(End, std::move(stop));
++m_bufferstopcount;
WriteLog(std::format("bufferstop: {} end {}: {}, by {}, its face {:.2f} m before the end", Track.name(), End + 1, bufferstop::kind_name(Stop.type), Source, Stop.setback));
};
// the free ends without the attribute, for the models
struct free_end {
TTrack *track{nullptr};
int end{0};
glm::dvec3 point;
glm::dvec3 outward;
};
std::vector<free_end> ends;
auto const automatic{Global.buffer_stops == global_settings::bufferstop_mode::automatic};
for (auto *track : m_items) {
if (track == nullptr) {
continue;
}
for (int end = 0; end < 2; ++end) {
auto const &attribute{track->m_bufferstopattributes[end]};
if (attribute.empty()) {
if (automatic && takes_stop(*track) && is_free(*track, end)) {
ends.push_back({track, end, bufferstops::end_point(*track, end), bufferstops::outward(*track, end)});
}
continue;
}
auto const stop{bufferstop::read_attribute(attribute)};
if (false == stop.has_value()) {
ErrorLog(std::format("Bad track: bufferstop{} \"{}\" of track \"{}\" isn't a buffer stop, ignored", end + 1, attribute, track->name()));
continue;
}
if (stop->type == bufferstop::kind::none) {
// opted out of the models
continue;
}
if (false == takes_stop(*track) || false == is_free(*track, end)) {
ErrorLog(std::format("Bad track: bufferstop{} of track \"{}\" at a joined end or not on a plain rail track, ignored", end + 1, track->name()));
continue;
}
install(*track, end, *stop, "the attribute");
}
}
if (ends.empty()) {
return;
}
// the models of the stops: each counts for the nearest free end it stands at, the nearest model for an end
auto const rules{bufferstop::default_rules()};
struct pick {
bufferstop::model_rule rule;
bufferstop::placement placement;
std::string model;
};
std::vector<std::optional<pick>> picks(ends.size());
for (auto const *instance : simulation::Instances.sequence()) {
if (instance == nullptr || instance->Model() == nullptr) {
continue;
}
auto const &file{instance->Model()->NameGet()};
auto const rule{bufferstop::match(file, rules)};
if (false == rule.has_value()) {
continue;
}
auto const &location{instance->location()};
std::optional<std::size_t> nearest;
bufferstop::placement placement;
for (std::size_t index = 0; index < ends.size(); ++index) {
auto const &end{ends[index]};
if (std::abs(location.y - end.point.y) > model_height_reach) {
continue;
}
auto const here{bufferstop::place(end.point.x, end.point.z, end.outward.x, end.outward.z, location.x, location.z)};
if (bufferstop::at_end(here) && (false == nearest.has_value() || here.distance < placement.distance)) {
nearest = index;
placement = here;
}
}
if (false == nearest.has_value()) {
continue;
}
auto &chosen{picks[*nearest]};
if (false == chosen.has_value() || placement.distance < chosen->placement.distance) {
chosen = pick{*rule, placement, file};
}
}
for (std::size_t index = 0; index < ends.size(); ++index) {
if (false == picks[index].has_value()) {
continue;
}
auto const &chosen{*picks[index]};
auto stop{bufferstop::preset(chosen.rule.type)};
stop.setback = bufferstop::model_setback(chosen.placement, chosen.rule);
install(*ends[index].track, ends[index].end, stop, std::format("model {} at {:.2f} m", chosen.model, chosen.placement.distance));
}
}