18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 00:45:29 +01:00

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.
This commit is contained in:
2026-11-28 18:32:58 +01:00
parent 4679201cb9
commit fbb9dc1e89
96 changed files with 1395 additions and 1304 deletions
+17 -17
View File
@@ -68,7 +68,7 @@ bool TMoverParameters::Attach(int ConnectNo, int ConnectToNr, TMoverParameters *
// no multiple-unit control between an electric and a diesel locomotive, whoever asks for it: the scenario, the AI
// or the player (eu07/maszyna#520)
if (auto const compatible{CompatibleCoupling(ConnectNo, *ConnectTo, otherend, CouplingType)}; compatible != CouplingType)
if (auto const compatible{CompatibleCoupling(static_cast<VehicleEnd>(ConnectNo), *ConnectTo, static_cast<VehicleEnd>(otherend), CouplingType)}; compatible != CouplingType)
{
WriteLog(std::format("Vehicles \"{}\" and \"{}\" can't share their control, coupled without the control connection", Name, ConnectTo->Name));
CouplingType = compatible;
@@ -183,21 +183,21 @@ bool TMoverParameters::Dettach(int ConnectNo)
// *************************************************************************************************
// prędkość zbliżania się stykających się końców pojazdów przy zderzeniu, w km/h
// *************************************************************************************************
double TMoverParameters::CollisionVelocity(int const End, TMoverParameters const &Other, int const OtherEnd) const
double TMoverParameters::CollisionVelocity(VehicleEnd const End, TMoverParameters const &Other, VehicleEnd const OtherEnd) const
{
auto const velocitydifference{CouplerVelocityDifference(End, Other, OtherEnd)};
// the front end closes in on the other vehicle when this one moves forward faster, the rear end when it's slower
return (End == end::front ? velocitydifference : -velocitydifference) * 3.6; // m/s -> km/h
return (End == VehicleEnd::front ? velocitydifference : -velocitydifference) * 3.6; // m/s -> km/h
}
double TMoverParameters::CouplerVelocityDifference(int const End, TMoverParameters const &Other, int const OtherEnd) const
double TMoverParameters::CouplerVelocityDifference(VehicleEnd const End, TMoverParameters const &Other, VehicleEnd const OtherEnd) const
{
// V runs along the vehicle's own axis, towards its front; the other vehicle's velocity is turned around when the
// vehicles face each other (touch with the same ends)
return V - Other.V * DirPatch(End, OtherEnd);
}
bool TMoverParameters::AutomaticCouplingSafe(int const End, TMoverParameters const &Other, int const OtherEnd) const
bool TMoverParameters::AutomaticCouplingSafe(VehicleEnd const End, TMoverParameters const &Other, VehicleEnd const OtherEnd) const
{
return false == Global.crash_damage || CollisionVelocity(End, Other, OtherEnd) <= SafeCollisionVelocity;
}
@@ -230,7 +230,7 @@ traction_kind traction_of(TEngineType const Engine)
} // namespace
bool TMoverParameters::ControlCouplingAllowed(int const End, TMoverParameters const &Other, int const OtherEnd) const
bool TMoverParameters::ControlCouplingAllowed(VehicleEnd const End, TMoverParameters const &Other, VehicleEnd const OtherEnd) const
{
if (false == Global.control_coupling_engine_check)
{
@@ -246,7 +246,7 @@ bool TMoverParameters::ControlCouplingAllowed(int const End, TMoverParameters co
return traction == traction_kind::none || othertraction == traction_kind::none || traction == othertraction;
}
CouplingSet TMoverParameters::CompatibleCoupling(int const End, TMoverParameters const &Other, int const OtherEnd, CouplingSet const CouplingType) const
CouplingSet TMoverParameters::CompatibleCoupling(VehicleEnd const End, TMoverParameters const &Other, VehicleEnd const OtherEnd, CouplingSet const CouplingType) const
{
if (false == CouplingType.HasAny(Coupling::control) || CouplingType.HasAny(Coupling::permanent) || ControlCouplingAllowed(End, Other, OtherEnd))
{
@@ -259,7 +259,7 @@ CouplingSet TMoverParameters::CompatibleCoupling(int const End, TMoverParameters
// Q: 20160713
// Obliczanie sił dzialających na sprzęgach
// *************************************************************************************************
double TMoverParameters::CouplerForce(int const End, double dt)
double TMoverParameters::CouplerForce(VehicleEnd const End, double dt)
{
if (coupler_physics_at(End))
{
@@ -268,12 +268,12 @@ double TMoverParameters::CouplerForce(int const End, double dt)
auto &thiscoupler{Couplers[End]};
auto *othervehicle{Neighbours[End].vehicle->MoverParameters};
auto const otherend{Neighbours[End].vehicle_end};
auto const otherend{Neighbours[End].FacingEnd()};
auto const &othercoupler{othervehicle->Couplers[otherend]};
auto const othervehiclemove{(othervehicle->dMoveLen * DirPatch(End, otherend))};
auto const initialdistance{Neighbours[End].distance}; // odległość od sprzęgu sąsiada
auto const distancedelta{(End == end::front ? othervehiclemove - dMoveLen : dMoveLen - othervehiclemove)};
auto const distancedelta{(End == VehicleEnd::front ? othervehiclemove - dMoveLen : dMoveLen - othervehiclemove)};
// the ends crushed for good (physics.collisions new, see collision.h) let the buffers meet that much closer
auto const crushed{thiscoupler.crush + othercoupler.crush};
@@ -385,7 +385,7 @@ double TMoverParameters::CouplerForce(int const End, double dt)
}
// a coupler stretched with more than its strength for over a second breaks, the more likely the longer, with crash damage on
void TMoverParameters::CouplerStretchCheck(int const End, double const CF, double dt)
void TMoverParameters::CouplerStretchCheck(VehicleEnd const End, double const CF, double dt)
{
auto &thiscoupler{Couplers[End]};
if (std::abs(CF) > thiscoupler.FmaxC)
@@ -405,7 +405,7 @@ void TMoverParameters::CouplerStretchCheck(int const End, double const CF, doubl
}
}
bool TMoverParameters::coupler_physics_at(int const End) const
bool TMoverParameters::coupler_physics_at(VehicleEnd const End) const
{
if (false == Global.coupler_physics)
{
@@ -420,7 +420,7 @@ bool TMoverParameters::coupler_physics_at(int const End) const
return other != nullptr && other->MoverParameters->CouplerPhysics;
}
void TMoverParameters::automatic_coupling(int const End, TMoverParameters &Other, int const OtherEnd)
void TMoverParameters::automatic_coupling(VehicleEnd const End, TMoverParameters &Other, VehicleEnd const OtherEnd)
{
auto &thiscoupler{Couplers[End]};
auto &othercoupler{Other.Couplers[OtherEnd]};
@@ -490,17 +490,17 @@ int coupler_sounds(coupler_sound_input const &Input)
} // namespace
double TMoverParameters::coupler_force_physical(int const End, double const dt)
double TMoverParameters::coupler_force_physical(VehicleEnd const End, double const dt)
{
auto &thiscoupler{Couplers[End]};
auto *othervehicle{Neighbours[End].vehicle->MoverParameters};
auto const otherend{Neighbours[End].vehicle_end};
auto const otherend{Neighbours[End].FacingEnd()};
auto &othercoupler{othervehicle->Couplers[otherend]};
auto &state{thiscoupler.physics_state};
// the gap as it is now: the distance found at the start of the frame, and the movement of both vehicles since
auto const othervehiclemove{othervehicle->dMoveLen * DirPatch(End, otherend)};
auto const gap{Neighbours[End].distance + (End == end::front ? othervehiclemove - dMoveLen : dMoveLen - othervehiclemove)};
auto const gap{Neighbours[End].distance + (End == VehicleEnd::front ? othervehiclemove - dMoveLen : dMoveLen - othervehiclemove)};
// the gap opens up when the front end falls behind the vehicle ahead of it, or the rear end runs ahead of the one behind
auto const gaprate{CouplerVelocityDifference(End, *othervehicle, otherend) * DirF(End)};
auto const coupled{thiscoupler.CouplingFlag != Coupling::none};
@@ -547,7 +547,7 @@ double TMoverParameters::coupler_force_physical(int const End, double const dt)
return force;
}
bool TMoverParameters::coupler_stop_hit(int const End, TMoverParameters &Other, int const OtherEnd)
bool TMoverParameters::coupler_stop_hit(VehicleEnd const End, TMoverParameters &Other, VehicleEnd const OtherEnd)
{
auto &thiscoupler{Couplers[End]};
auto &othercoupler{Other.Couplers[OtherEnd]};