18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 19:57:06 +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
+36 -36
View File
@@ -116,7 +116,7 @@ bool derailed(TMoverParameters const &Vehicle) {
}
double crush_of(TMoverParameters const &Vehicle) {
return std::max(Vehicle.Couplers[end::front].crush, Vehicle.Couplers[end::rear].crush);
return std::max(Vehicle.Couplers[VehicleEnd::front].crush, Vehicle.Couplers[VehicleEnd::rear].crush);
}
// a mixed freight train as the PoC has it: every third wagon from the given one an empty two-axle 3WL (10.5 t), the
@@ -172,7 +172,7 @@ crash_outcome freight_crash(double const Speed, double const Radius, double cons
for (std::size_t i = 0; i < train.size(); ++i) {
auto const &vehicle{train[i].mover()};
auto const name{i < standing ? std::format("standing {}", standing - 1 - i) : i == standing ? std::string{"locomotive"} : std::format("running {}", i - standing - 1)};
for (auto const end : {end::front, end::rear}) {
for (auto const end : VehicleEnds) {
if (vehicle.Couplers[end].crush >= cl::crush_mark) {
++result.crushed;
}
@@ -190,7 +190,7 @@ crash_outcome freight_crash(double const Speed, double const Radius, double cons
}
}
for (std::size_t i = 0; i + 1 < train.size(); ++i) {
result.broken += train[i]->Couplers[end::rear].broken;
result.broken += train[i]->Couplers[VehicleEnd::rear].broken;
}
return result;
}
@@ -204,16 +204,16 @@ double locomotive_into_wagon(double const Speed, double const Dt) {
// behind the wagon, running forwards into its rear end
auto &locomotive{train.add(definition::fiz("4e_v1/4e"), 0, Coupling::none, Speed, 0.5)};
train.run(3.0, {}, Dt);
return wagon->Couplers[end::rear].crush + locomotive->Couplers[end::front].crush;
return wagon->Couplers[VehicleEnd::rear].crush + locomotive->Couplers[VehicleEnd::front].crush;
}
// the energy the crush of both ends of the pair took, by their curves [J]
double crush_energy(TMoverParameters const &First, int const FirstEnd, TMoverParameters const &Second, int const SecondEnd) {
double crush_energy(TMoverParameters const &First, VehicleEnd const FirstEnd, TMoverParameters const &Second, VehicleEnd const SecondEnd) {
return First.crush_curve_at(FirstEnd).energy(0.0, First.Couplers[FirstEnd].crush) + Second.crush_curve_at(SecondEnd).energy(0.0, Second.Couplers[SecondEnd].crush);
}
// what the buffers of EN 15551 of the two ends take in [J]
double capacity_of(TMoverParameters const &First, int const FirstEnd, TMoverParameters const &Second, int const SecondEnd) {
double capacity_of(TMoverParameters const &First, VehicleEnd const FirstEnd, TMoverParameters const &Second, VehicleEnd const SecondEnd) {
return cl::buffer_capacity(First.Couplers[FirstEnd].collision, First.collision_kind()) + cl::buffer_capacity(Second.Couplers[SecondEnd].collision, Second.collision_kind());
}
@@ -238,9 +238,9 @@ contact_outcome contact(definition const &Front, definition const &Back, double
auto &back{train.add(Back, 0, Coupling::none, Speed, Gap)};
train.run(Seconds, {}, Dt);
contact_outcome result;
result.crush = front->Couplers[end::rear].crush + back->Couplers[end::front].crush;
result.energy = crush_energy(front.mover(), end::rear, back.mover(), end::front);
result.available = std::max(0.0, relative_energy(front.mover(), back.mover(), Speed * kmh) - capacity_of(front.mover(), end::rear, back.mover(), end::front));
result.crush = front->Couplers[VehicleEnd::rear].crush + back->Couplers[VehicleEnd::front].crush;
result.energy = crush_energy(front.mover(), VehicleEnd::rear, back.mover(), VehicleEnd::front);
result.available = std::max(0.0, relative_energy(front.mover(), back.mover(), Speed * kmh) - capacity_of(front.mover(), VehicleEnd::rear, back.mover(), VehicleEnd::front));
result.derailed = derailed(front.mover()) || derailed(back.mover());
return result;
}
@@ -251,7 +251,7 @@ definition coach(double const Mass) {
}
// Y / Q of the leading wheel of the vehicle at the end pressed in by its coupling, as check_pushed_off() takes it
double pushed_ratio_of(TMoverParameters const &Vehicle, int const End, double const OtherLength, double const Radius) {
double pushed_ratio_of(TMoverParameters const &Vehicle, VehicleEnd const End, double const OtherLength, double const Radius) {
cl::pushed_vehicle pushed;
pushed.mass = Vehicle.TotalMass - Vehicle.Mred;
pushed.axles = Vehicle.NAxles > 0 ? Vehicle.NAxles : 4;
@@ -259,7 +259,7 @@ double pushed_ratio_of(TMoverParameters const &Vehicle, int const End, double co
pushed.pivots = Vehicle.BDist;
pushed.axle_base = Vehicle.ADist > 0.0 ? Vehicle.ADist : Vehicle.BDist;
cl::pushed_end end;
end.force = std::max(0.0, (End == end::front ? -1.0 : 1.0) * Vehicle.Couplers[End].CForce);
end.force = std::max(0.0, (End == VehicleEnd::front ? -1.0 : 1.0) * Vehicle.Couplers[End].CForce);
end.other_length = OtherLength;
end.crush = Vehicle.Couplers[End].crush;
return cl::pushed_ratio(pushed, end, Radius);
@@ -591,8 +591,8 @@ TEST_CASE("collisions: a locomotive at its starting effort against a braked trai
auto pushed{0.0};
auto worst{0.0};
auto const watch = [&]() {
pushed = std::max(pushed, -locomotive->Couplers[end::front].CForce);
worst = std::max(worst, pushed_ratio_of(empty.mover(), end::rear, locomotive->Dim.L, line.radius));
pushed = std::max(pushed, -locomotive->Couplers[VehicleEnd::front].CForce);
worst = std::max(worst, pushed_ratio_of(empty.mover(), VehicleEnd::rear, locomotive->Dim.L, line.radius));
};
// the line contactors close at the first position, once their delay and the pressure switch let them
locomotive->IncMainCtrl(1);
@@ -631,8 +631,8 @@ TEST_CASE("collisions: a locomotive running onto an empty wagon at 9 km/h doesn'
auto over{0}; // steps past the climbing limit
for (int step = 0; step < 500; ++step) {
train.step(curve);
pushed = std::max(pushed, empty->Couplers[end::rear].CForce);
auto const ratio{pushed_ratio_of(empty.mover(), end::rear, locomotive->Dim.L, curve.radius)};
pushed = std::max(pushed, empty->Couplers[VehicleEnd::rear].CForce);
auto const ratio{pushed_ratio_of(empty.mover(), VehicleEnd::rear, locomotive->Dim.L, curve.radius)};
worst = std::max(worst, ratio);
over += ratio >= derailment::climbing_limit;
}
@@ -684,7 +684,7 @@ TEST_CASE("collisions: a repaired vehicle has its ends rebuilt and is back on th
auto &wagon{add_loaded(train, Coupling::none, 0.0, 0.0)};
auto &locomotive{train.add(definition::fiz("4e_v1/4e"), 0, Coupling::none, 30.0, 0.5)};
train.run(3.0);
REQUIRE(wagon->Couplers[end::rear].crush + locomotive->Couplers[end::front].crush > cl::override_crush);
REQUIRE(wagon->Couplers[VehicleEnd::rear].crush + locomotive->Couplers[VehicleEnd::front].crush > cl::override_crush);
REQUIRE((derailed(wagon.mover()) || derailed(locomotive.mover())));
for (auto *vehicle : {&wagon, &locomotive}) {
auto &mover{vehicle->mover()};
@@ -708,12 +708,12 @@ TEST_CASE("collisions: a repaired vehicle has its ends rebuilt and is back on th
TEST_CASE("collisions: the repair of a coupler torn off by the crush rebuilds its end") {
vehicle wagon{definition::fiz("430w_v2/430w")};
auto &coupler{wagon->Couplers[end::rear]};
auto &coupler{wagon->Couplers[VehicleEnd::rear]};
coupler.crush = 0.3;
coupler.broken = true;
coupler.hit_force = 2.0e6;
wagon->DamageFlag.Set(VehicleDamage::coupling);
REQUIRE(wagon->repair_coupler(end::rear));
REQUIRE(wagon->repair_coupler(VehicleEnd::rear));
CHECK(coupler.crush == 0.0);
CHECK(coupler.hit_force == 0.0);
CHECK_FALSE(coupler.broken);
@@ -768,7 +768,7 @@ TEST_CASE("collisions: the legacy rule with physics.collisions old, the crush wi
train.run(10.0);
CHECK(derailed(train[0].mover()) == (physical ? frontout_new : frontout_old));
CHECK(derailed(train[1].mover()) == (physical ? backout_new : backout_old));
auto const crush{train[0]->Couplers[end::rear].crush + train[1]->Couplers[end::front].crush};
auto const crush{train[0]->Couplers[VehicleEnd::rear].crush + train[1]->Couplers[VehicleEnd::front].crush};
if (physical) {
MESSAGE(name, ": crush ", crush, " m");
CHECK(crush > 0.05);
@@ -808,7 +808,7 @@ TEST_CASE("collisions: the model of the collisions goes apart from the one of th
train.add(definition::fiz("4e_v1/4e"), 0, Coupling::none, 25.0, 20.0);
train.run(10.0);
Global.derailment = previous;
auto const crush{train[0]->Couplers[end::rear].crush + train[1]->Couplers[end::front].crush};
auto const crush{train[0]->Couplers[VehicleEnd::rear].crush + train[1]->Couplers[VehicleEnd::front].crush};
CHECK((crush > 0.05) == (collisions == global_settings::collision_model::physical));
}
}
@@ -849,24 +849,24 @@ TEST_CASE("collisions: automatic couplers hit too fast don't catch, the ends cru
CAPTURE(speed);
vehicle moving{automatic, speed, "moving"};
vehicle standing{automatic, 0.0, "standing"};
test::pair pair{moving, end::front, standing, end::rear, 1.0};
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
auto coupled{false};
auto fast{false}; // coupled while closing in faster than the couplers catch
for (int i = 0; i < 1000; ++i) {
auto const closing{moving->CollisionVelocity(end::front, standing.mover(), end::rear)};
auto const closing{moving->CollisionVelocity(VehicleEnd::front, standing.mover(), VehicleEnd::rear)};
pair.step();
fast = fast || (pair.coupled() && false == coupled && closing > TMoverParameters::SafeCollisionVelocity);
coupled = coupled || pair.coupled();
}
auto const crush{moving->Couplers[end::front].crush + standing->Couplers[end::rear].crush};
auto const crush{moving->Couplers[VehicleEnd::front].crush + standing->Couplers[VehicleEnd::rear].crush};
MESSAGE(speed, " km/h: crush ", crush, " m, derailed ", derailed(moving.mover()), " ", derailed(standing.mover()), ", coupled later ", coupled);
CHECK_FALSE(fast);
CHECK(crush > 0.0);
// crushed past the draw gear, the coupler is gone; short of it, the wagons may roll together again slowly
// after the hit, and couple then
if (std::max(moving->Couplers[end::front].crush, standing->Couplers[end::rear].crush) >= cl::draw_gear_crush) {
if (std::max(moving->Couplers[VehicleEnd::front].crush, standing->Couplers[VehicleEnd::rear].crush) >= cl::draw_gear_crush) {
CHECK_FALSE(coupled);
CHECK((moving->Couplers[end::front].broken || standing->Couplers[end::rear].broken));
CHECK((moving->Couplers[VehicleEnd::front].broken || standing->Couplers[VehicleEnd::rear].broken));
}
// the two alike: neither climbs onto the other until the crush is deep enough for both to
CHECK(derailed(moving.mover()) == derailed(standing.mover()));
@@ -886,7 +886,7 @@ TEST_CASE("collisions: road vehicles keep the legacy rule") {
road.run(5.0);
CHECK(derailed(road[0].mover()));
CHECK(derailed(road[1].mover()));
CHECK(road[0]->Couplers[end::rear].crush == 0.0);
CHECK(road[0]->Couplers[VehicleEnd::rear].crush == 0.0);
CHECK(road[1]->CollisionDerail == NONE);
CHECK(road[0]->crush_reports + road[1]->crush_reports == 0);
}
@@ -954,7 +954,7 @@ TEST_CASE("collisions: identical units built to EN 15227 at 36 km/h") {
double inner;
int derailed;
bool higher;
} result{train[1]->Couplers[end::rear].crush + train[2]->Couplers[end::front].crush, std::max(crush_of(train[0].mover()), crush_of(train[3].mover())), 0,
} result{train[1]->Couplers[VehicleEnd::rear].crush + train[2]->Couplers[VehicleEnd::front].crush, std::max(crush_of(train[0].mover()), crush_of(train[3].mover())), 0,
derailed(train[2].mover())};
for (std::size_t i = 0; i < train.size(); ++i) {
result.derailed += derailed(train[i].mover());
@@ -997,9 +997,9 @@ TEST_CASE("collisions: the buffers of EN 15551 of an end take in their energy, o
CHECK(cl::buffer_capacity(category_c, cl::vehicle_kind::wagon) == doctest::Approx(140.0e3));
// the key of the .fiz
vehicle wagon{definition::load("wagon").set("BuffCoupl", "BufferCategory", "B")};
REQUIRE(wagon->Couplers[end::front].collision.buffers.has_value());
CHECK(*wagon->Couplers[end::front].collision.buffers == cl::buffer_category::B);
CHECK(cl::buffer_capacity(wagon->Couplers[end::rear].collision, wagon->collision_kind()) == doctest::Approx(100.0e3));
REQUIRE(wagon->Couplers[VehicleEnd::front].collision.buffers.has_value());
CHECK(*wagon->Couplers[VehicleEnd::front].collision.buffers == cl::buffer_category::B);
CHECK(cl::buffer_capacity(wagon->Couplers[VehicleEnd::rear].collision, wagon->collision_kind()) == doctest::Approx(100.0e3));
}
TEST_CASE("collisions: the budget of a contact, by what the hit brings to the buffers") {
@@ -1207,7 +1207,7 @@ TEST_CASE("collisions: two coaches (36 t and 41.6 t) at 8 km/h don't crush, nor
REQUIRE(derailed(wall.mover()));
auto &running{train.add(heavy, 0, Coupling::none, 5.0, gap)};
train.run(5.0);
CHECK(wall->Couplers[end::rear].crush + running->Couplers[end::front].crush == 0.0);
CHECK(wall->Couplers[VehicleEnd::rear].crush + running->Couplers[VehicleEnd::front].crush == 0.0);
CHECK(running->CollisionDerail == NONE);
}
}
@@ -1242,7 +1242,7 @@ TEST_CASE("collisions: a locomotive pushing a standing braked train at full powe
auto pushed{0.0};
auto seconds{0}; // pushing with 100 kN or more
auto const push = [&]() {
auto const force{std::max(0.0, -locomotive->Couplers[end::front].CForce)};
auto const force{std::max(0.0, -locomotive->Couplers[VehicleEnd::front].CForce)};
pushed = std::max(pushed, force);
seconds += force >= 100.0e3;
};
@@ -1328,12 +1328,12 @@ train_damage damage_of(consist &Train) {
for (std::size_t i = 0; i < Train.size(); ++i) {
auto const &vehicle{Train[i].mover()};
result.crush = std::max(result.crush, crush_of(vehicle));
for (auto const end : {end::front, end::rear}) {
for (auto const end : VehicleEnds) {
result.energy += vehicle.crush_curve_at(end).energy(0.0, vehicle.Couplers[end].crush);
}
result.derailed += vehicle.CollisionDerail != NONE;
if (i + 1 < Train.size()) {
result.broken += vehicle.Couplers[end::rear].broken;
result.broken += vehicle.Couplers[VehicleEnd::rear].broken;
}
if (crush_of(vehicle) > 0.0 || vehicle.CollisionDerail != NONE) {
result.list += std::format("{} crushed {:.2f} m{}; ", i, crush_of(vehicle), vehicle.CollisionDerail != NONE ? ", derailed" : "");
@@ -1487,12 +1487,12 @@ TEST_CASE("collisions: a pair parting at the start of the step isn't crushed (l0
for (auto *vehicle : {&front, &back}) {
vehicle->forces(dt);
}
REQUIRE((front->Couplers[end::rear].closing < 0.0) == parting);
REQUIRE((front->Couplers[VehicleEnd::rear].closing < 0.0) == parting);
back->V = 25.0 * kmh;
for (auto *vehicle : {&front, &back}) {
vehicle->move({}, dt, (*vehicle)->Loc, 5.0);
}
auto const crush{front->Couplers[end::rear].crush + back->Couplers[end::front].crush};
auto const crush{front->Couplers[VehicleEnd::rear].crush + back->Couplers[VehicleEnd::front].crush};
if (parting) {
CHECK(crush == 0.0);
// the legacy collision pushed them apart