18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 05:02:45 +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
+19 -19
View File
@@ -31,7 +31,7 @@ http://mozilla.org/MPL/2.0/.
#include "physics/components/drive.h"
int DirPatch(int Coupler1, int Coupler2)
int DirPatch(VehicleEnd const Coupler1, VehicleEnd const Coupler2)
{ // poprawka dla liczenia sil przy ustawieniu przeciwnym obiektow
return Coupler1 != Coupler2 ? 1 : -1;
}
@@ -51,13 +51,13 @@ bool legacy_derailed_slides(TMoverParameters const &Vehicle)
} // namespace
int DirF(int CouplerN)
int DirF(VehicleEnd CouplerN)
{
switch (CouplerN)
{
case 0:
case VehicleEnd::front:
return -1;
case 1:
case VehicleEnd::rear:
return 1;
default:
return 0;
@@ -173,7 +173,7 @@ void TMoverParameters::change_train_velocity(double const Change)
for (std::size_t i = 0; i < train.size(); ++i)
{
auto const [vehicle, direction] = train[i];
for (int end = end::front; end <= end::rear; ++end)
for (auto const end : VehicleEnds)
{
auto const &coupler{vehicle->Couplers[end]};
auto *other{coupler.Connected};
@@ -181,7 +181,7 @@ void TMoverParameters::change_train_velocity(double const Change)
{
continue;
}
auto const otherdirection{direction * DirPatch(end, coupler.ConnectedNr)};
auto const otherdirection{direction * DirPatch(end, coupler.ConnectedEnd())};
other->V += Change * otherdirection;
train.emplace_back(other, otherdirection);
}
@@ -308,7 +308,7 @@ double TMoverParameters::ComputeMovement(double dt, double dt1, const TTrackShap
}
derailment_distance = 0.0;
// ale jesli jest kolizja (zas. zach. pedu) to...}
for (int b = 0; b < 2; b++)
for (auto const b : VehicleEnds)
if (Couplers[b].CheckCollision)
CollisionDetect(b, dt); // zmienia niejawnie AccS, V !!!
check_pushed_off(dt, std::abs(dL));
@@ -386,7 +386,7 @@ double TMoverParameters::FastComputeMovement(double dt, const TTrackShape & /*Sh
// the criteria of derailment go by the distance, checked in the full step which ends the frame
derailment_distance += std::abs(dL);
// ale jesli jest kolizja (zas. zach. pedu) to...
for (int b = 0; b < 2; b++)
for (auto const b : VehicleEnds)
if (Couplers[b].CheckCollision)
CollisionDetect(b, dt); // zmienia niejawnie AccS, V !!!
check_pushed_off(dt, std::abs(dL));
@@ -501,13 +501,13 @@ void TMoverParameters::compute_movement_(double const Deltatime)
UpdateBatteryVoltage(Deltatime);
UpdateScndPipePressure(Deltatime); // druga rurka, youBy
if ((DCEMUED_CC & 1) != 0 && Couplers[end::front].CouplingFlag.HasAny(Coupling::control))
if ((DCEMUED_CC & 1) != 0 && Couplers[VehicleEnd::front].CouplingFlag.HasAny(Coupling::control))
{
DynamicBrakeEMUStatus &= Couplers[end::front].Connected->DynamicBrakeEMUStatus;
DynamicBrakeEMUStatus &= Couplers[VehicleEnd::front].Connected->DynamicBrakeEMUStatus;
}
if ((DCEMUED_CC & 2) != 0 && Couplers[end::rear].CouplingFlag.HasAny(Coupling::control))
if ((DCEMUED_CC & 2) != 0 && Couplers[VehicleEnd::rear].CouplingFlag.HasAny(Coupling::control))
{
DynamicBrakeEMUStatus &= Couplers[end::rear].Connected->DynamicBrakeEMUStatus;
DynamicBrakeEMUStatus &= Couplers[VehicleEnd::rear].Connected->DynamicBrakeEMUStatus;
}
if (BrakeSlippingTimer > 0.8 && ASBType != 128)
@@ -721,7 +721,7 @@ void TMoverParameters::ComputeConstans(void)
// drag calculation
{
// NOTE: draft effect of previous vehicle is simplified and doesn't have much to do with reality
auto const *previousvehicle{Couplers[V >= 0.0 ? end::front : end::rear].Connected};
auto const *previousvehicle{Couplers[V >= 0.0 ? VehicleEnd::front : VehicleEnd::rear].Connected};
auto dragarea{Dim.W * Dim.H};
if (previousvehicle)
{
@@ -797,11 +797,11 @@ void TMoverParameters::ComputeTotalForce(double dt)
auto const vehicleisactive{CabActive != 0 || Vel > 0.0001 || accelerating() || LastSwitchingTime < 5 || is_multiple_unit()};
auto const movingvehicleahead{Neighbours[end::front].vehicle != nullptr &&
(Neighbours[end::front].vehicle->MoverParameters->Vel > 0.0001 || Neighbours[end::front].vehicle->MoverParameters->accelerating())};
auto const movingvehicleahead{Neighbours[VehicleEnd::front].vehicle != nullptr &&
(Neighbours[VehicleEnd::front].vehicle->MoverParameters->Vel > 0.0001 || Neighbours[VehicleEnd::front].vehicle->MoverParameters->accelerating())};
auto const movingvehiclebehind{Neighbours[end::rear].vehicle != nullptr &&
(Neighbours[end::rear].vehicle->MoverParameters->Vel > 0.0001 || Neighbours[end::rear].vehicle->MoverParameters->accelerating())};
auto const movingvehiclebehind{Neighbours[VehicleEnd::rear].vehicle != nullptr &&
(Neighbours[VehicleEnd::rear].vehicle->MoverParameters->Vel > 0.0001 || Neighbours[VehicleEnd::rear].vehicle->MoverParameters->accelerating())};
auto const calculatephysics{vehicleisactive || movingvehicleahead || movingvehiclebehind};
@@ -914,7 +914,7 @@ void TMoverParameters::ComputeTotalForce(double dt)
}
nrot_eps = (abs(nrot) - old_nrot) / dt;
// doliczenie sił z innych pojazdów
for (int end = end::front; end <= end::rear; ++end)
for (auto const end : VehicleEnds)
{
// a new step: the hit of the pair not taken by either vehicle yet
Couplers[end].hit_handled = false;
@@ -924,7 +924,7 @@ void TMoverParameters::ComputeTotalForce(double dt)
if (Neighbours[end].vehicle != nullptr)
{
// the velocities before the step, the forces of all the vehicles come before their movement
Couplers[end].closing = -CouplerVelocityDifference(end, *Neighbours[end].vehicle->MoverParameters, Neighbours[end].vehicle_end) * DirF(end);
Couplers[end].closing = -CouplerVelocityDifference(end, *Neighbours[end].vehicle->MoverParameters, Neighbours[end].FacingEnd()) * DirF(end);
Couplers[end].CForce = CouplerForce(end, dt);
FTrain += Couplers[end].CForce;
// what the buffers of the pair take in, for the budget of the crush (physics.collisions new and monitor)