18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 22:07:55 +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
+51 -51
View File
@@ -268,7 +268,7 @@ TEST_CASE("coupler physics: a jerk on the solid draw gear breaks the coupling by
namespace {
// couples the facing ends of the two vehicles, as the scenery does
void join(vehicle &First, int FirstEnd, vehicle &Second, int SecondEnd, CouplingSet const couplings = Coupling::coupler) {
void join(vehicle &First, VehicleEnd FirstEnd, vehicle &Second, VehicleEnd SecondEnd, CouplingSet const couplings = Coupling::coupler) {
REQUIRE(First->Attach(FirstEnd, SecondEnd, &Second.mover(), couplings, true, false));
REQUIRE(Second->Attach(SecondEnd, FirstEnd, &First.mover(), couplings, true, false));
}
@@ -286,9 +286,9 @@ bool coupler_damaged(vehicle &Vehicle) {
TEST_CASE("coupler physics: the play of the coupling and the parameters of the model come from the vehicle definition") {
auto loose{loose_wagon(0.05, 0.0, "loose")};
auto const &physics{loose->Couplers[end::front].physics};
auto const &physics{loose->Couplers[VehicleEnd::front].physics};
CHECK(physics.slack == doctest::Approx(0.05));
CHECK(loose->Couplers[end::rear].physics.slack == doctest::Approx(0.05));
CHECK(loose->Couplers[VehicleEnd::rear].physics.slack == doctest::Approx(0.05));
CHECK(physics.buffers.stiffness == doctest::Approx(9200e3));
CHECK(physics.buffers.travel == doctest::Approx(0.11));
CHECK(physics.draw_gear.end_force() == doctest::Approx(450e3));
@@ -300,11 +300,11 @@ TEST_CASE("coupler physics: the play of the coupling and the parameters of the m
CHECK(physics.link.breakable);
auto tight{vehicle{definition::load("wagon"), 0.0, "tight"}};
CHECK(tight->Couplers[end::front].physics.slack == 0.0);
CHECK(tight->Couplers[VehicleEnd::front].physics.slack == 0.0);
auto automatic{vehicle{definition::load("wagon").set("BuffCoupl", "CType", "Automatic"), 0.0, "automatic"}};
CHECK(automatic->Couplers[end::front].physics.slack == doctest::Approx(0.005));
CHECK(automatic->Couplers[VehicleEnd::front].physics.slack == doctest::Approx(0.005));
auto articulated{vehicle{definition::load("wagon").set("BuffCoupl", "CType", "Articulated"), 0.0, "articulated"}};
CHECK_FALSE(articulated->Couplers[end::front].physics.link.breakable);
CHECK_FALSE(articulated->Couplers[VehicleEnd::front].physics.link.breakable);
}
TEST_CASE("coupler physics: a coupling with play pulls only once the play is taken up") {
@@ -313,15 +313,15 @@ TEST_CASE("coupler physics: a coupling with play pulls only once the play is tak
flag_setting const crashdamage{Global.crash_damage, true};
auto front{loose_wagon(0.05, 3.0, "front")};
auto rear{loose_wagon(0.05, 0.1, "rear")};
join(rear, end::front, front, end::rear);
test::pair pair{rear, end::front, front, end::rear, 0.0};
join(rear, VehicleEnd::front, front, VehicleEnd::rear);
test::pair pair{rear, VehicleEnd::front, front, VehicleEnd::rear, 0.0};
front.hold_brake_pipe(5.0);
rear.hold_brake_pipe(5.0);
auto pulledwithinplay{false};
for (int i = 0; i < 300 && pair.gap() < 0.09; ++i) {
pair.step();
pulledwithinplay = pulledwithinplay || rear->Couplers[end::front].CForce != 0.0;
pulledwithinplay = pulledwithinplay || rear->Couplers[VehicleEnd::front].CForce != 0.0;
}
REQUIRE(pair.gap() >= 0.09);
CHECK_FALSE(pulledwithinplay);
@@ -343,7 +343,7 @@ TEST_CASE("coupler physics: buffers hit at a walking pace give back less than th
auto moving{vehicle{definition::load("wagon"), 5.0, "moving"}};
// barely moving, so that its brakes are released
auto standing{vehicle{definition::load("wagon"), 0.1, "standing"}};
test::pair pair{moving, end::front, standing, end::rear, 0.3};
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 0.3};
moving.hold_brake_pipe(5.0);
standing.hold_brake_pipe(5.0);
@@ -352,7 +352,7 @@ TEST_CASE("coupler physics: buffers hit at a walking pace give back less than th
for (int i = 0; i < 500; ++i) {
pair.step();
touched = touched || pair.gap() < 0.0;
solid = solid || moving->Couplers[end::front].physics_state.stop != 0;
solid = solid || moving->Couplers[VehicleEnd::front].physics_state.stop != 0;
if (touched && pair.gap() > 0.3) {
break; // parted
}
@@ -377,8 +377,8 @@ TEST_CASE("coupler physics: coupled vehicles at rest stay at rest") {
flag_setting const everywhere{Global.coupler_physics_all, true};
auto first{vehicle{definition::load("wagon"), 0.0, "first"}};
auto second{vehicle{definition::load("wagon"), 0.0, "second"}};
join(first, end::rear, second, end::front);
test::pair pair{first, end::rear, second, end::front, 0.0};
join(first, VehicleEnd::rear, second, VehicleEnd::front);
test::pair pair{first, VehicleEnd::rear, second, VehicleEnd::front, 0.0};
pair.run(5.0);
CHECK(std::abs(first.speed()) < 0.01);
@@ -391,8 +391,8 @@ TEST_CASE("coupler physics: a jerk on a coupling with play breaks it, the brake
flag_setting const everywhere{Global.coupler_physics_all, true};
auto front{loose_wagon(0.05, 20.0, "front")};
auto rear{loose_wagon(0.05, 0.1, "rear")};
join(rear, end::front, front, end::rear, Coupling::coupler | Coupling::brakeHose);
test::pair pair{rear, end::front, front, end::rear, 0.0};
join(rear, VehicleEnd::front, front, VehicleEnd::rear, Coupling::coupler | Coupling::brakeHose);
test::pair pair{rear, VehicleEnd::front, front, VehicleEnd::rear, 0.0};
front.hold_brake_pipe(5.0);
rear.hold_brake_pipe(5.0);
@@ -424,15 +424,15 @@ TEST_CASE("coupler physics: a gentle pull on a coupling with play doesn't hurt i
flag_setting const crashdamage{Global.crash_damage, true};
auto front{loose_wagon(0.05, 3.0, "front")};
auto rear{loose_wagon(0.05, 0.1, "rear")};
join(rear, end::front, front, end::rear);
test::pair pair{rear, end::front, front, end::rear, 0.0};
join(rear, VehicleEnd::front, front, VehicleEnd::rear);
test::pair pair{rear, VehicleEnd::front, front, VehicleEnd::rear, 0.0};
front.hold_brake_pipe(5.0);
rear.hold_brake_pipe(5.0);
pair.run(3.0);
CHECK(pair.coupled());
CHECK_FALSE(coupler_damaged(rear));
CHECK(rear->Couplers[end::front].physics_state.elongation == 0.0);
CHECK(rear->Couplers[VehicleEnd::front].physics_state.elongation == 0.0);
}
TEST_CASE("coupler physics: automatic couplers still catch a wagon rolling into a standing one") {
@@ -442,7 +442,7 @@ TEST_CASE("coupler physics: automatic couplers still catch a wagon rolling into
auto automatic{definition::load("wagon").set("BuffCoupl", "CType", "Automatic").set("BuffCoupl", "AutomaticFlag", Coupling::coupler | Coupling::brakeHose)};
auto moving{vehicle{automatic, 5.0, "moving"}};
auto standing{vehicle{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};
pair.run(3.0);
REQUIRE(pair.coupled());
@@ -456,51 +456,51 @@ TEST_CASE("couplers: a broken coupling of vehicles facing each other uncouples t
auto first{vehicle{definition::load("wagon"), 0.0, "first"}};
auto second{vehicle{definition::load("wagon"), 0.0, "second"}};
auto third{vehicle{definition::load("wagon"), 0.0, "third"}};
join(first, end::front, second, end::front);
join(second, end::rear, third, end::front);
join(first, VehicleEnd::front, second, VehicleEnd::front);
join(second, VehicleEnd::rear, third, VehicleEnd::front);
first->damage_coupler(end::front);
CHECK(first->Couplers[end::front].CouplingFlag == Coupling::none);
CHECK(second->Couplers[end::front].CouplingFlag == Coupling::none);
first->damage_coupler(VehicleEnd::front);
CHECK(first->Couplers[VehicleEnd::front].CouplingFlag == Coupling::none);
CHECK(second->Couplers[VehicleEnd::front].CouplingFlag == Coupling::none);
// the other coupling of the second vehicle stays
CHECK(second->Couplers[end::rear].CouplingFlag == Coupling::coupler);
CHECK(second->Couplers[VehicleEnd::rear].CouplingFlag == Coupling::coupler);
}
TEST_CASE("couplers: a broken coupler is repaired alone, with the one of the vehicle it was coupled with") {
auto first{vehicle{definition::load("wagon"), 0.0, "first"}};
auto second{vehicle{definition::load("wagon"), 0.0, "second"}};
auto third{vehicle{definition::load("wagon"), 0.0, "third"}};
join(first, end::rear, second, end::front, Coupling::coupler | Coupling::brakeHose);
join(second, end::rear, third, end::front, Coupling::coupler | Coupling::brakeHose);
join(first, VehicleEnd::rear, second, VehicleEnd::front, Coupling::coupler | Coupling::brakeHose);
join(second, VehicleEnd::rear, third, VehicleEnd::front, Coupling::coupler | Coupling::brakeHose);
// both couplings of the second wagon break
second->damage_coupler(end::front);
second->damage_coupler(end::rear);
REQUIRE(second->Couplers[end::front].broken);
REQUIRE(first->Couplers[end::rear].broken);
REQUIRE(third->Couplers[end::front].broken);
second->damage_coupler(VehicleEnd::front);
second->damage_coupler(VehicleEnd::rear);
REQUIRE(second->Couplers[VehicleEnd::front].broken);
REQUIRE(first->Couplers[VehicleEnd::rear].broken);
REQUIRE(third->Couplers[VehicleEnd::front].broken);
REQUIRE(second->DamageFlag.HasAll(VehicleDamage::coupling));
// nothing to repair at the far end of the first wagon
CHECK_FALSE(first->repair_coupler(end::front));
CHECK_FALSE(first->repair_coupler(VehicleEnd::front));
// repaired from the first wagon: the coupling between the first two, the other one stays broken
CHECK(first->repair_coupler(end::rear));
CHECK_FALSE(first->Couplers[end::rear].broken);
CHECK_FALSE(second->Couplers[end::front].broken);
CHECK(second->Couplers[end::rear].broken);
CHECK(third->Couplers[end::front].broken);
CHECK(first->repair_coupler(VehicleEnd::rear));
CHECK_FALSE(first->Couplers[VehicleEnd::rear].broken);
CHECK_FALSE(second->Couplers[VehicleEnd::front].broken);
CHECK(second->Couplers[VehicleEnd::rear].broken);
CHECK(third->Couplers[VehicleEnd::front].broken);
// the second wagon keeps its damage, and its emergency braking, while a coupler of it is broken
CHECK(second->DamageFlag.HasAll(VehicleDamage::coupling));
CHECK(second->AlarmChainFlag);
CHECK(third->repair_coupler(end::front));
CHECK_FALSE(second->Couplers[end::rear].broken);
CHECK(third->repair_coupler(VehicleEnd::front));
CHECK_FALSE(second->Couplers[VehicleEnd::rear].broken);
CHECK_FALSE(second->DamageFlag.HasAll(VehicleDamage::coupling));
CHECK_FALSE(second->AlarmChainFlag);
// repaired, not coupled: the vehicles are coupled again as usual
CHECK(second->Couplers[end::rear].CouplingFlag == Coupling::none);
CHECK(second->Attach(end::rear, end::front, &third.mover(), Coupling::coupler, true, false));
CHECK(second->Couplers[VehicleEnd::rear].CouplingFlag == Coupling::none);
CHECK(second->Attach(VehicleEnd::rear, VehicleEnd::front, &third.mover(), Coupling::coupler, true, false));
}
TEST_CASE("coupler physics: with couplerphysics yes, only the pairs with a vehicle covered take the physical model") {
@@ -508,28 +508,28 @@ TEST_CASE("coupler physics: with couplerphysics yes, only the pairs with a vehic
flag_setting const everywhere{Global.coupler_physics_all, false};
auto moving{vehicle{definition::load("wagon"), 5.0, "moving"}};
auto standing{vehicle{definition::load("wagon"), 0.1, "standing"}};
test::pair pair{moving, end::front, standing, end::rear, 0.1};
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 0.1};
SUBCASE("neither covered: the legacy model") {
pair.run(1.0);
CHECK_FALSE(moving->coupler_physics_at(end::front));
CHECK_FALSE(standing->coupler_physics_at(end::rear));
CHECK_FALSE(moving->coupler_physics_at(VehicleEnd::front));
CHECK_FALSE(standing->coupler_physics_at(VehicleEnd::rear));
// the state of the physical model untouched
CHECK(moving->Couplers[end::front].physics_state.gap == 0.0);
CHECK(moving->Couplers[VehicleEnd::front].physics_state.gap == 0.0);
}
SUBCASE("one covered: both compute the pair physically") {
// as the player's train closing in on the other one
moving->CouplerPhysics = true;
pair.run(1.0);
CHECK(moving->coupler_physics_at(end::front));
CHECK(standing->coupler_physics_at(end::rear));
CHECK(moving->Couplers[end::front].physics_state.gap != 0.0);
CHECK(standing->Couplers[end::rear].physics_state.gap == moving->Couplers[end::front].physics_state.gap);
CHECK(moving->coupler_physics_at(VehicleEnd::front));
CHECK(standing->coupler_physics_at(VehicleEnd::rear));
CHECK(moving->Couplers[VehicleEnd::front].physics_state.gap != 0.0);
CHECK(standing->Couplers[VehicleEnd::rear].physics_state.gap == moving->Couplers[VehicleEnd::front].physics_state.gap);
}
SUBCASE("the setting off: the legacy model, covered or not") {
flag_setting const off{Global.coupler_physics, false};
moving->CouplerPhysics = true;
CHECK_FALSE(moving->coupler_physics_at(end::front));
CHECK_FALSE(moving->coupler_physics_at(VehicleEnd::front));
}
}