mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 12:57:06 +01:00
f65411a668
The speed along the couplers, the location, the offsets from the track axis,
the accelerations, the distance counter and the distance moved since the last
check, the flat of the wheels and the truck hunting, the distance of the
derailment check, whether the vehicle is inside a consist and in the coupler
model, the analog demand of the integrated controller and the flow of the pipe
stabiliser are private to physics::Vehicle. TDynamicObject places and moves the
vehicle, the scenery puts road vehicles on their way and the contacts push them
through the operations (SetV(), SetLoc(), SetOffsetTrackH(), ...), and the
jolts of the track are ShakeVertically() and ShakeHorizontally(), the same
additions as before.
No change in behaviour: the golden traces recorded on dfb43c4f match bit for bit.
118 lines
4.2 KiB
C++
118 lines
4.2 KiB
C++
#include "stdafx.h"
|
|
#include "consist.h"
|
|
|
|
#include "vehicle/DynObj.h"
|
|
|
|
namespace test {
|
|
|
|
consist::consist() = default;
|
|
consist::~consist() = default;
|
|
|
|
vehicle &consist::add(definition const &Definition, int Cab, CouplingSet const couplings, double Velocity, double Gap) {
|
|
auto added{std::make_unique<vehicle>(Definition, Velocity, std::format("test{}", m_vehicles.size()), Cab)};
|
|
auto &physics{added->physics()};
|
|
|
|
std::vector<std::byte> object(sizeof(TDynamicObject));
|
|
reinterpret_cast<TDynamicObject *>(object.data())->physics = &physics; // NOSONAR see m_objects
|
|
|
|
auto position{0.0};
|
|
if (false == m_vehicles.empty()) {
|
|
auto &last{m_vehicles.back()->physics()};
|
|
position = m_positions.back() - 0.5 * (last.Dim.L + physics.Dim.L) - Gap;
|
|
physics.SetLoc(TLocation{position, 0.0, 0.0});
|
|
if (false == couplings.Empty()) {
|
|
last.Attach(VehicleEnd::rear, VehicleEnd::front, &physics, couplings, true, false);
|
|
physics.Attach(VehicleEnd::front, VehicleEnd::rear, &last, couplings, true, false);
|
|
}
|
|
}
|
|
else {
|
|
physics.SetLoc(TLocation{position, 0.0, 0.0});
|
|
}
|
|
m_vehicles.push_back(std::move(added));
|
|
m_positions.push_back(position);
|
|
m_objects.push_back(std::move(object));
|
|
update_neighbours();
|
|
return *m_vehicles.back();
|
|
}
|
|
|
|
void consist::update_neighbours() {
|
|
// TDynamicObject::update_neighbours(), for vehicles coupled in a line
|
|
for (std::size_t i = 0; i < m_vehicles.size(); ++i) {
|
|
auto &physics{m_vehicles[i]->physics()};
|
|
for (auto const end : VehicleEnds) {
|
|
auto &neighbour{physics.Neighbours[end]};
|
|
auto const other{end == VehicleEnd::front ? static_cast<std::ptrdiff_t>(i) - 1 : static_cast<std::ptrdiff_t>(i) + 1};
|
|
if (other < 0 || other >= static_cast<std::ptrdiff_t>(m_vehicles.size())) {
|
|
neighbour = neighbour_data();
|
|
continue;
|
|
}
|
|
auto const &otherPhysics{m_vehicles[other]->physics()};
|
|
auto const otherend{Opposite(end)};
|
|
auto const distance{physics::Vehicle::CouplerDist(&physics, &otherPhysics) - physics.Couplers[end].adapter_length -
|
|
otherPhysics.Couplers[otherend].adapter_length};
|
|
// a vehicle not coupled is a collision source when it's within the range the game scans
|
|
if (physics.Couplers[end].Connected == nullptr && distance > std::max(10.0, std::abs(physics.V())) + 40.0) {
|
|
neighbour = neighbour_data();
|
|
continue;
|
|
}
|
|
neighbour.vehicle = reinterpret_cast<TDynamicObject *>(m_objects[other].data()); // NOSONAR see m_objects
|
|
neighbour.vehicle_end = NumberOf(otherend);
|
|
neighbour.distance = static_cast<float>(distance);
|
|
}
|
|
}
|
|
}
|
|
|
|
void consist::step(track const &Track, double Dt) {
|
|
for (auto const &vehicle : m_vehicles) {
|
|
vehicle->begin_step();
|
|
}
|
|
update_neighbours();
|
|
for (int iteration = 1; iteration < Track.iterations; ++iteration) {
|
|
for (auto const &vehicle : m_vehicles) {
|
|
vehicle->forces(Dt);
|
|
}
|
|
for (std::size_t i = 0; i < m_vehicles.size(); ++i) {
|
|
m_positions[i] += m_vehicles[i]->fast_move(Track, Dt, TLocation{m_positions[i], 0.0, 0.0});
|
|
}
|
|
update_brake_pipes(Dt);
|
|
}
|
|
for (auto const &vehicle : m_vehicles) {
|
|
vehicle->forces(Dt);
|
|
}
|
|
// the average pressure in the train's brake pipe weighed by the vehicles' lengths, TDynamicObject::GetEPP(),
|
|
// over the whole train (the game's stops at a vehicle without the brake hose coupled)
|
|
auto pipe{0.0};
|
|
auto length{0.0};
|
|
for (auto const &vehicle : m_vehicles) {
|
|
pipe += vehicle->physics().brakePipe.Pressure() * vehicle->physics().Dim.L;
|
|
length += vehicle->physics().Dim.L;
|
|
}
|
|
pipe /= length;
|
|
for (std::size_t i = 0; i < m_vehicles.size(); ++i) {
|
|
m_positions[i] += m_vehicles[i]->move(Track, Dt, TLocation{m_positions[i], 0.0, 0.0}, pipe);
|
|
}
|
|
update_brake_pipes(Dt);
|
|
}
|
|
|
|
void consist::update_brake_pipes(double Dt) const {
|
|
// vehicle_table::update_brake_pipes()
|
|
std::vector<physics::Vehicle *> vehicles;
|
|
for (auto const &vehicle : m_vehicles) {
|
|
vehicles.push_back(&vehicle->physics());
|
|
}
|
|
physics::Vehicle::UpdatePipeCouplers(vehicles, Dt);
|
|
}
|
|
|
|
void consist::run(double Seconds, track const &Track, double Dt) {
|
|
auto const steps{std::lround(Seconds / (Dt * Track.iterations))};
|
|
for (long i = 0; i < steps; ++i) {
|
|
step(Track, Dt);
|
|
}
|
|
}
|
|
|
|
double consist::gap(std::size_t Index) const {
|
|
return physics::Vehicle::CouplerDist(&m_vehicles[Index]->physics(), &m_vehicles[Index + 1]->physics());
|
|
}
|
|
|
|
} // namespace test
|