/* 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 #include #include #include #include namespace { struct found_stop { TTrack const *track{nullptr}; int end{0}; std::shared_ptr 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 // 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 && 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; } }