18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 04:47:06 +01:00
Files
maszyna/tests/unit/consist.cpp
T
Hirek fbb9dc1e89 refactor(physics): the ends and the sides of a vehicle as scoped enums
end and side were unscoped enums: front, rear, left and right sat in the
global namespace (the test PCH had to leave mover.h out for it), and an end
went into any int, a side into an end, without a word from the compiler. They
become enum class VehicleEnd and VehicleSide (physics/vehicletypes.h); the
names follow the rule for types, and leave "end" and "side" to the many
locals of that name. The numbers stay: front 0, rear 1, right 0, left 1.

- ByEnd<T> / BySide<T>: two values indexed by the enum, and by a number
  where a loop or a format counts them. The arrays of the ends and the sides
  are these now: Couplers, Neighbours, Pantographs, MotorBlowers, Lights,
  iLights, Doors.instances, pVehicles, IsAnyDoor*, m_lighthints, vCoulpler,
  m_couplersounds, m_mirrormotion, iInventory, and the voltages of the
  couplers
- VehicleEnds / VehicleSides to go through them, Opposite() in place of
  1 - end, NumberOf() where a number is needed
- the couplers and the collisions take the ends they work on as VehicleEnd:
  CouplerForce, CouplerVelocityDifference, CollisionDetect, crush_hit,
  CompatibleCoupling and the rest of their family, DirF, DirPatch, and of
  the vehicle couple, uncouple, the adapters, off_track_gap, has_signal_on;
  Attach, Dettach and DettachStatus have overloads by the ends
- neighbour_data::FacingEnd() and TCoupling::ConnectedEnd() give the end of
  the other vehicle as VehicleEnd; the fields stay numbers, as they hold -1
  and 2 for "none" in places

The walks along the consist (FirstFind, Neighbour, the AI's coupling and
uncoupling) still carry the ends as numbers; where one of them meets a typed
end, NumberOf() and EndOf() say so, and keep the number as it was.

Kept as it was, and worth a look: TDynamicObject::SetLights() switches the
headlights A/B off by CabActive with the cases end::front and end::rear, that
is CabActive 0 and 1, while CabActive is 1 or -1 for the cabs; the cases are
NumberOf(VehicleEnd::front / rear) now, the same numbers.

No change in behaviour: 1144/1144 tests, golden traces exact against staging
62f7a29b. betterRenderer/, not built, has had only end:: and side:: renamed.
2026-11-28 18:32:58 +01:00

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 &mover{added->mover()};
std::vector<std::byte> object(sizeof(TDynamicObject));
reinterpret_cast<TDynamicObject *>(object.data())->MoverParameters = &mover; // NOSONAR see m_objects
auto position{0.0};
if (false == m_vehicles.empty()) {
auto &last{m_vehicles.back()->mover()};
position = m_positions.back() - 0.5 * (last.Dim.L + mover.Dim.L) - Gap;
mover.Loc = TLocation{position, 0.0, 0.0};
if (false == couplings.Empty()) {
last.Attach(VehicleEnd::rear, VehicleEnd::front, &mover, couplings, true, false);
mover.Attach(VehicleEnd::front, VehicleEnd::rear, &last, couplings, true, false);
}
}
else {
mover.Loc = 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 &mover{m_vehicles[i]->mover()};
for (auto const end : VehicleEnds) {
auto &neighbour{mover.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 &othermover{m_vehicles[other]->mover()};
auto const otherend{Opposite(end)};
auto const distance{TMoverParameters::CouplerDist(&mover, &othermover) - mover.Couplers[end].adapter_length -
othermover.Couplers[otherend].adapter_length};
// a vehicle not coupled is a collision source when it's within the range the game scans
if (mover.Couplers[end].Connected == nullptr && distance > std::max(10.0, std::abs(mover.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->mover().PipePress * vehicle->mover().Dim.L;
length += vehicle->mover().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<TMoverParameters *> movers;
for (auto const &vehicle : m_vehicles) {
movers.push_back(&vehicle->mover());
}
TMoverParameters::UpdatePipeCouplers(movers, 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 TMoverParameters::CouplerDist(&m_vehicles[Index]->mover(), &m_vehicles[Index + 1]->mover());
}
} // namespace test