mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 06:35:21 +01:00
ee93aa11c8
A rail vehicle running onto a switch from its frog end by the way the switch is not set to pushes its blades over. Before, the switch was just thrown to that way and nothing remembered it. Now the switch is marked as run through, the log tells it once per run-through, and the scenery can react through the memory cell and event __simulation.runthrough. With physics.derailment new the damaged switch stays out of use for the run: switch events no longer throw it, a vehicle running onto it from its points pushes the loose blades over under its first axle and runs forked (the #496 limits derail it, cause split_switch), and a run-through above 60 km/h derails the vehicle at the switch (cause run_through). In monitor mode the run does not change; the log tells what new would do. Every switch is still taken as trailable, as before.
116 lines
3.5 KiB
C++
116 lines
3.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/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "world/switchfork.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <format>
|
|
#include <numbers>
|
|
|
|
namespace switch_fork {
|
|
|
|
int leaving_path(int const Path, int const Set) {
|
|
return (Path == 0 || Path == 1) && Path != (Set & 1) ? Path : -1;
|
|
}
|
|
|
|
void path_memory::record(void const *Switch, int const Path) {
|
|
// the older record of the switch goes, the ones after it move up
|
|
auto const end{m_entries.begin() + m_count};
|
|
auto const found{std::find_if(m_entries.begin(), end, [Switch](auto const &Entry) { return Entry.first == Switch; })};
|
|
if (found != end) {
|
|
std::rotate(found, found + 1, end);
|
|
--m_count;
|
|
}
|
|
if (m_count == size) {
|
|
// the oldest goes
|
|
std::rotate(m_entries.begin(), m_entries.begin() + 1, m_entries.end());
|
|
--m_count;
|
|
}
|
|
m_entries[m_count++] = {Switch, Path};
|
|
}
|
|
|
|
std::optional<int> path_memory::path_of(void const *Switch) const {
|
|
for (auto i = m_count; i > 0; --i) {
|
|
if (m_entries[i - 1].first == Switch) {
|
|
return m_entries[i - 1].second;
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
bool forked(int const Path, std::optional<int> const Ahead) {
|
|
return Ahead.has_value() && *Ahead >= 0 && Path >= 0 && *Ahead != Path;
|
|
}
|
|
|
|
double bogie_limit(double const Pivots, double const Radius) {
|
|
if (Radius <= 0.0) {
|
|
return 0.0;
|
|
}
|
|
return std::asin(std::clamp(std::abs(Pivots) / (2.0 * Radius), 0.0, 1.0));
|
|
}
|
|
|
|
double turn(double const TrackHeading, double const BodyHeading) {
|
|
// the difference of the lines, into (-pi/2, pi/2]
|
|
auto angle{std::remainder(TrackHeading - BodyHeading, std::numbers::pi)};
|
|
if (angle <= -0.5 * std::numbers::pi) {
|
|
angle += std::numbers::pi;
|
|
}
|
|
return angle;
|
|
}
|
|
|
|
double heading(double const X, double const Z) {
|
|
return std::atan2(-X, Z);
|
|
}
|
|
|
|
double end_offset(double const X, double const Z, double const Heading, double const OtherX, double const OtherZ, double const OtherHeading) {
|
|
// the mean of the two lines: the other heading brought within a quarter turn of the first one
|
|
auto const mean{Heading + 0.5 * turn(OtherHeading, Heading)};
|
|
// across it: the unit vector of the heading turned by a quarter, (-sin h, cos h) to (cos h, sin h)
|
|
auto const acrossx{std::cos(mean)};
|
|
auto const acrossz{std::sin(mean)};
|
|
return std::abs((OtherX - X) * acrossx + (OtherZ - Z) * acrossz);
|
|
}
|
|
|
|
state bogies(double const Front, double const Rear, double const Pivots, double const Radius) {
|
|
return {std::max(std::abs(Front), std::abs(Rear)), bogie_limit(Pivots, Radius)};
|
|
}
|
|
|
|
state coupling(double const Offset) {
|
|
return {std::abs(Offset), buffer_head_width};
|
|
}
|
|
|
|
bool runs_through(int const Path, int const Set) {
|
|
return (Path == 0 || Path == 1) && Path != (Set & 1);
|
|
}
|
|
|
|
int pushed_over(int const Set) {
|
|
return 1 - (Set & 1);
|
|
}
|
|
|
|
bool run_through_derails(double const Speed) {
|
|
return std::abs(Speed) > run_through_speed;
|
|
}
|
|
|
|
run_through_rules rules_of(run_through_model const Model) {
|
|
switch (Model) {
|
|
case run_through_model::physical: return {true, true, false};
|
|
case run_through_model::monitor: return {false, false, true};
|
|
case run_through_model::legacy:
|
|
default: return {};
|
|
}
|
|
}
|
|
|
|
std::string run_through_text(std::string_view const Switch, std::string_view const Vehicle, double const Speed) {
|
|
return std::format("switch {} run through by {} at {:.0f} km/h", Switch.empty() ? std::string_view{"(unnamed)"} : Switch, Vehicle, std::abs(Speed));
|
|
}
|
|
|
|
} // namespace switch_fork
|