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
+41 -41
View File
@@ -75,14 +75,14 @@ std::vector<test::channel> state() {
{"SpeedCtrlParking", [](auto const &M) { return flag(M.SpeedCtrlUnit.Parking); }},
{"SpringBrakeActivate", [](auto const &M) { return flag(M.SpringBrake.Activate); }},
};
for (int end : {end::front, end::rear}) {
auto const name{std::string{end == end::front ? "Front" : "Rear"}};
for (auto const end : VehicleEnds) {
auto const name{std::string{end == VehicleEnd::front ? "Front" : "Rear"}};
result.emplace_back("MotorBlowers" + name + "Enabled", [end](auto const &M) { return flag(M.MotorBlowers[end].is_enabled); });
result.emplace_back("MotorBlowers" + name + "Disabled", [end](auto const &M) { return flag(M.MotorBlowers[end].is_disabled); });
result.emplace_back("Pant" + name + "Valve", [end](auto const &M) { return flag(M.Pantographs[end].valve.is_enabled) + 2 * flag(M.Pantographs[end].valve.is_disabled); });
}
for (int side : {side::right, side::left}) {
auto const name{std::string{side == side::right ? "Right" : "Left"}};
for (auto const side : VehicleSides) {
auto const name{std::string{side == VehicleSide::right ? "Right" : "Left"}};
result.emplace_back(name + "DoorPermit", [side](auto const &M) { return flag(M.Doors.instances[side].open_permit); });
result.emplace_back(name + "DoorRemoteOpen", [side](auto const &M) { return flag(M.Doors.instances[side].remote_open); });
result.emplace_back(name + "DoorRemoteClose", [side](auto const &M) { return flag(M.Doors.instances[side].remote_close); });
@@ -285,16 +285,16 @@ std::vector<action> const actions{
{"OilPumpSwitch on", [](auto &M) { return M.OilPumpSwitch(true, range_t::consist); }},
{"OilPumpSwitchOff on", [](auto &M) { return M.OilPumpSwitchOff(true, range_t::unit); }},
{"OilPumpSwitchOff off", [](auto &M) { return M.OilPumpSwitchOff(false, range_t::consist); }},
{"MotorBlowersSwitch front on", [](auto &M) { return M.MotorBlowersSwitch(true, end::front, range_t::consist); }},
{"MotorBlowersSwitch rear on", [](auto &M) { return M.MotorBlowersSwitch(true, end::rear, range_t::unit); }},
{"MotorBlowersSwitchOff front on", [](auto &M) { return M.MotorBlowersSwitchOff(true, end::front, range_t::consist); }},
{"MotorBlowersSwitchOff rear on", [](auto &M) { return M.MotorBlowersSwitchOff(true, end::rear, range_t::unit); }},
{"MotorBlowersSwitchOff front off", [](auto &M) { return M.MotorBlowersSwitchOff(false, end::front, range_t::consist); }},
{"MotorBlowersSwitchOff rear off", [](auto &M) { return M.MotorBlowersSwitchOff(false, end::rear, range_t::consist); }},
{"MotorBlowersSwitch front on", [](auto &M) { return M.MotorBlowersSwitch(true, VehicleEnd::front, range_t::consist); }},
{"MotorBlowersSwitch rear on", [](auto &M) { return M.MotorBlowersSwitch(true, VehicleEnd::rear, range_t::unit); }},
{"MotorBlowersSwitchOff front on", [](auto &M) { return M.MotorBlowersSwitchOff(true, VehicleEnd::front, range_t::consist); }},
{"MotorBlowersSwitchOff rear on", [](auto &M) { return M.MotorBlowersSwitchOff(true, VehicleEnd::rear, range_t::unit); }},
{"MotorBlowersSwitchOff front off", [](auto &M) { return M.MotorBlowersSwitchOff(false, VehicleEnd::front, range_t::consist); }},
{"MotorBlowersSwitchOff rear off", [](auto &M) { return M.MotorBlowersSwitchOff(false, VehicleEnd::rear, range_t::consist); }},
{"OperatePantographsValve enable", [](auto &M) { return M.OperatePantographsValve(operation_t::enable, range_t::consist); }},
{"OperatePantographValve front enable", [](auto &M) { return M.OperatePantographValve(end::front, operation_t::enable, range_t::consist); }},
{"OperatePantographValve rear enable_on", [](auto &M) { return M.OperatePantographValve(end::rear, operation_t::enable_on, range_t::unit); }},
{"OperatePantographValve rear enable_off", [](auto &M) { return M.OperatePantographValve(end::rear, operation_t::enable_off, range_t::unit); }},
{"OperatePantographValve front enable", [](auto &M) { return M.OperatePantographValve(VehicleEnd::front, operation_t::enable, range_t::consist); }},
{"OperatePantographValve rear enable_on", [](auto &M) { return M.OperatePantographValve(VehicleEnd::rear, operation_t::enable_on, range_t::unit); }},
{"OperatePantographValve rear enable_off", [](auto &M) { return M.OperatePantographValve(VehicleEnd::rear, operation_t::enable_off, range_t::unit); }},
{"DirectionForward", [](auto &M) { return M.DirectionForward(); }},
{"ConverterSwitch on", [](auto &M) { return M.ConverterSwitch(true, range_t::consist); }},
{"CompressorSwitch on", [](auto &M) { return M.CompressorSwitch(true, range_t::consist); }},
@@ -330,30 +330,30 @@ std::vector<action> const actions{
{"SandboxAuto off", [](auto &M) { return M.SandboxAuto(false, range_t::consist); }},
{"SandboxManual off", [](auto &M) { return M.SandboxManual(false, range_t::consist); }},
{"Sandbox off", [](auto &M) { return M.Sandbox(false, range_t::consist); }},
{"PermitDoors right", [](auto &M) { return M.PermitDoors(side::right, true, range_t::consist); }},
{"PermitDoors left", [](auto &M) { return M.PermitDoors(side::left, true, range_t::unit); }},
{"PermitDoors right", [](auto &M) { return M.PermitDoors(VehicleSide::right, true, range_t::consist); }},
{"PermitDoors left", [](auto &M) { return M.PermitDoors(VehicleSide::left, true, range_t::unit); }},
{"ChangeDoorPermitPreset +1", [](auto &M) { return M.ChangeDoorPermitPreset(1, range_t::consist); }},
{"ChangeDoorPermitPreset -1", [](auto &M) { return M.ChangeDoorPermitPreset(-1, range_t::consist); }},
{"PermitDoorStep off", [](auto &M) { return M.PermitDoorStep(false, range_t::consist); }},
{"PermitDoorStep on", [](auto &M) { return M.PermitDoorStep(true, range_t::unit); }},
{"LockDoors off", [](auto &M) { return M.LockDoors(false, range_t::consist); }},
{"OperateDoors right open local", [](auto &M) { return M.OperateDoors(side::right, true, range_t::local); }},
{"OperateDoors left open", [](auto &M) { return M.OperateDoors(side::left, true, range_t::consist); }},
{"OperateDoors right close", [](auto &M) { return M.OperateDoors(side::right, false, range_t::unit); }},
{"OperateDoors right open local", [](auto &M) { return M.OperateDoors(VehicleSide::right, true, range_t::local); }},
{"OperateDoors left open", [](auto &M) { return M.OperateDoors(VehicleSide::left, true, range_t::consist); }},
{"OperateDoors right close", [](auto &M) { return M.OperateDoors(VehicleSide::right, false, range_t::unit); }},
{"ChangeDoorControlMode remote", [](auto &M) { return M.ChangeDoorControlMode(true, range_t::consist); }},
{"ChangeDoorControlMode local", [](auto &M) { return M.ChangeDoorControlMode(false, range_t::unit); }},
{"LockDoors on", [](auto &M) { return M.LockDoors(true, range_t::unit); }},
{"signal_departure on", [](auto &M) { return M.signal_departure(true, range_t::consist); }},
{"signal_departure off", [](auto &M) { return M.signal_departure(false, range_t::unit); }},
{"PermitDoors right off", [](auto &M) { return M.PermitDoors(side::right, false, range_t::consist); }},
{"PermitDoors right off", [](auto &M) { return M.PermitDoors(VehicleSide::right, false, range_t::consist); }},
{"HeatingSwitch off", [](auto &M) { return M.HeatingSwitch(false, range_t::unit); }},
{"MainSwitch off", [](auto &M) { return M.MainSwitch(false, range_t::consist); }},
{"DropAllPantographs", [](auto &M) { return M.DropAllPantographs(true, range_t::consist); }},
{"DropAllPantographs released", [](auto &M) { return M.DropAllPantographs(false, range_t::unit); }},
{"OperatePantographsValve disable", [](auto &M) { return M.OperatePantographsValve(operation_t::disable, range_t::unit); }},
{"OperatePantographValve front disable_on", [](auto &M) { return M.OperatePantographValve(end::front, operation_t::disable_on, range_t::consist); }},
{"OperatePantographValve front disable_off", [](auto &M) { return M.OperatePantographValve(end::front, operation_t::disable_off, range_t::consist); }},
{"OperatePantographValve front none", [](auto &M) { return M.OperatePantographValve(end::front, operation_t::none, range_t::local); }},
{"OperatePantographValve front disable_on", [](auto &M) { return M.OperatePantographValve(VehicleEnd::front, operation_t::disable_on, range_t::consist); }},
{"OperatePantographValve front disable_off", [](auto &M) { return M.OperatePantographValve(VehicleEnd::front, operation_t::disable_off, range_t::consist); }},
{"OperatePantographValve front none", [](auto &M) { return M.OperatePantographValve(VehicleEnd::front, operation_t::none, range_t::local); }},
{"CompressorSwitch off", [](auto &M) { return M.CompressorSwitch(false, range_t::unit); }},
{"ConverterSwitch off", [](auto &M) { return M.ConverterSwitch(false, range_t::unit); }},
{"WaterCircuitsLinkSwitch off", [](auto &M) { return M.WaterCircuitsLinkSwitch(false, range_t::unit); }},
@@ -363,8 +363,8 @@ std::vector<action> const actions{
{"WaterPumpBreakerSwitch off", [](auto &M) { return M.WaterPumpBreakerSwitch(false, range_t::unit); }},
{"FuelPumpSwitch off", [](auto &M) { return M.FuelPumpSwitch(false, range_t::unit); }},
{"OilPumpSwitch off", [](auto &M) { return M.OilPumpSwitch(false, range_t::unit); }},
{"MotorBlowersSwitch front off", [](auto &M) { return M.MotorBlowersSwitch(false, end::front, range_t::unit); }},
{"MotorBlowersSwitch rear off", [](auto &M) { return M.MotorBlowersSwitch(false, end::rear, range_t::consist); }},
{"MotorBlowersSwitch front off", [](auto &M) { return M.MotorBlowersSwitch(false, VehicleEnd::front, range_t::unit); }},
{"MotorBlowersSwitch rear off", [](auto &M) { return M.MotorBlowersSwitch(false, VehicleEnd::rear, range_t::consist); }},
{"CompartmentLightsSwitch off", [](auto &M) { return M.CompartmentLightsSwitch(false, range_t::unit); }},
{"BatterySwitch off", [](auto &M) { return M.BatterySwitch(false, range_t::unit); }},
{"CabDeactivisation", [](auto &M) { return M.CabDeactivisation(); }},
@@ -378,7 +378,7 @@ TEST_CASE("commands: every switch of the cab, sent to the next vehicle") {
test::reseed_random();
vehicle driven{definition::fiz(path), 0.0, "driven", 1};
vehicle next{definition::fiz(path), 0.0, "next"};
driven->Attach(end::rear, end::front, &next.mover(), test::consist::full_coupling, true, false);
driven->Attach(VehicleEnd::rear, VehicleEnd::front, &next.mover(), test::consist::full_coupling, true, false);
auto done{false};
auto channels{state()};
for (auto const &channel : state()) {
@@ -408,8 +408,8 @@ TEST_CASE("commands: every switch of the cab, worked on a pair standing under th
auto done{false};
auto channels{state()};
channels.insert(channels.end(), {
{"PantFrontActive", [](auto const &M) { return flag(M.Pantographs[end::front].is_active); }},
{"PantRearActive", [](auto const &M) { return flag(M.Pantographs[end::rear].is_active); }},
{"PantFrontActive", [](auto const &M) { return flag(M.Pantographs[VehicleEnd::front].is_active); }},
{"PantRearActive", [](auto const &M) { return flag(M.Pantographs[VehicleEnd::rear].is_active); }},
{"PantPress", [](auto const &M) { return M.PantPress; }},
{"GroundRelay", [](auto const &M) { return flag(M.GroundRelay); }},
{"ConverterFlag", [](auto const &M) { return flag(M.ConverterFlag); }},
@@ -449,8 +449,8 @@ struct train {
vehicle last{definition::fiz("4e_v1/4e"), 0.0, "last"};
explicit train(CouplingSet const couplings) {
head->Attach(end::rear, end::rear, &middle.mover(), couplings, true, false);
middle->Attach(end::front, end::front, &last.mover(), couplings, true, false);
head->Attach(VehicleEnd::rear, VehicleEnd::rear, &middle.mover(), couplings, true, false);
middle->Attach(VehicleEnd::front, VehicleEnd::front, &last.mover(), couplings, true, false);
}
};
@@ -468,21 +468,21 @@ TEST_CASE("commands: travel along the train, turned round for a vehicle facing b
CHECK(consist.last->CommandIn.Command == VehicleCommand::none);
SUBCASE("door permits: the right side of the sender is the left one of a vehicle facing backwards") {
consist.head->PermitDoors(side::right, true, range_t::consist);
CHECK(consist.head->Doors.instances[side::right].open_permit);
CHECK(consist.middle->Doors.instances[side::left].open_permit);
CHECK_FALSE(consist.middle->Doors.instances[side::right].open_permit);
CHECK(consist.last->Doors.instances[side::right].open_permit);
consist.head->PermitDoors(VehicleSide::right, true, range_t::consist);
CHECK(consist.head->Doors.instances[VehicleSide::right].open_permit);
CHECK(consist.middle->Doors.instances[VehicleSide::left].open_permit);
CHECK_FALSE(consist.middle->Doors.instances[VehicleSide::right].open_permit);
CHECK(consist.last->Doors.instances[VehicleSide::right].open_permit);
// and revoked the same way
consist.head->PermitDoors(side::right, false, range_t::consist);
CHECK_FALSE(consist.middle->Doors.instances[side::left].open_permit);
CHECK_FALSE(consist.last->Doors.instances[side::right].open_permit);
consist.head->PermitDoors(VehicleSide::right, false, range_t::consist);
CHECK_FALSE(consist.middle->Doors.instances[VehicleSide::left].open_permit);
CHECK_FALSE(consist.last->Doors.instances[VehicleSide::right].open_permit);
}
SUBCASE("a pantograph valve: the vehicles past one coupled by its control cable take the other pantograph") {
consist.head->OperatePantographValve(end::front, operation_t::enable, range_t::consist);
CHECK(consist.head->Pantographs[end::front].valve.is_enabled);
CHECK(consist.middle->Pantographs[end::front].valve.is_enabled);
CHECK(consist.last->Pantographs[end::rear].valve.is_enabled);
consist.head->OperatePantographValve(VehicleEnd::front, operation_t::enable, range_t::consist);
CHECK(consist.head->Pantographs[VehicleEnd::front].valve.is_enabled);
CHECK(consist.middle->Pantographs[VehicleEnd::front].valve.is_enabled);
CHECK(consist.last->Pantographs[VehicleEnd::rear].valve.is_enabled);
}
SUBCASE("sent backwards, it goes no further than the sender: nothing is coupled at its front") {
consist.head->RunCommand(VehicleCommand::doorLock, 0, -1, Coupling::control);