18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 08:17:09 +01:00
Files
maszyna/tests/unit/test_loads.cpp
T
Hirek fbb9dc1e89 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.
2026-11-28 18:32:58 +01:00

117 lines
5.0 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "vehicle.h"
// characterisation of the loads of vehicles besides the loading by commands (test_commands.cpp): the load
// assigned to a vehicle (AssignLoad), with the pantographs of an electric vehicle set up by the "pantstate"
// load of a scenario, and the steps of loading and unloading (LoadingDone)
using test::definition;
using test::vehicle;
TEST_CASE("loads: the pantographs of an electric vehicle set up by the amount of its pantstate load") {
// bit 0 raises the front pantograph, bit 1 the rear one, bit 2 turns the vehicle round in the train first
// (DoubleTr), so the bits name the pantographs the other way round
struct setup {
int amount;
bool frontraised;
bool rearraised;
};
for (auto const &[amount, frontraised, rearraised] : {setup{0, false, false}, setup{1, true, false}, setup{2, false, true}, setup{3, true, true}, setup{5, false, true},
setup{6, true, false}}) {
CAPTURE(amount);
vehicle et42{definition::fiz("et42_v2/112e-a")};
CHECK(et42->AssignLoad("pantstate", static_cast<float>(amount)));
CHECK(et42->DoubleTr == ((amount & 4) != 0 ? -1 : 1));
CHECK(et42->Pantographs[VehicleEnd::front].valve.is_enabled == frontraised);
CHECK(et42->Pantographs[VehicleEnd::rear].valve.is_enabled == rearraised);
// the load itself stays as it was
CHECK(et42->LoadAmount == doctest::Approx(0.0));
}
// a vehicle without a current collector takes no such load
vehicle wagon{definition::fiz("3wl_v2/3wl")};
CHECK_FALSE(wagon->AssignLoad("pantstate", 3.f));
}
TEST_CASE("loads: a load assigned, of one kind at a time, up to the vehicle's capacity, and emptied") {
vehicle wagon{definition::fiz("3wl_v2/3wl")};
auto const empty{wagon->TotalMass};
REQUIRE(wagon->AssignLoad("coal", 10.f));
CHECK(wagon->LoadTypeChange);
CHECK(wagon->LoadType.name == "coal");
CHECK(wagon->TotalMass > empty);
// more of the same: the amount set, not added, and no more than the vehicle takes
wagon->LoadTypeChange = false;
REQUIRE(wagon->AssignLoad("coal", 1000.f));
CHECK_FALSE(wagon->LoadTypeChange);
CHECK(wagon->LoadAmount == doctest::Approx(wagon->MaxLoad));
// another kind can't be added to a load, nor can an unknown one be loaded
CHECK_FALSE(wagon->AssignLoad("ruda", 5.f));
CHECK(wagon->LoadType.name == "coal");
// emptied, with no name
REQUIRE(wagon->AssignLoad(""));
CHECK(wagon->LoadTypeChange);
CHECK(wagon->LoadAmount == doctest::Approx(0.0));
CHECK(wagon->LoadType.name.empty());
CHECK_FALSE(wagon->AssignLoad("unknown", 5.f));
CHECK(wagon->AssignLoad("ruda", 5.f));
}
TEST_CASE("loads: the steps of loading and unloading") {
// most of the game's wagons may be overloaded (OverLoadFactor), the 3WL not
vehicle wagon{definition::fiz("3wl_v2/3wl").set("Load", "OverLoadFactor", 1.5)};
REQUIRE(wagon->AssignLoad("coal", 10.f));
auto const capacity{wagon->MaxLoad * (1.0 + wagon->OverLoadFactor)};
SUBCASE("no speed of the change: done at once") {
CHECK(wagon->LoadingDone(0.0, "coal"));
CHECK(wagon->LoadStatus == 4);
}
SUBCASE("no load, or another one, named: done when it was") {
wagon->LoadStatus = 1;
CHECK_FALSE(wagon->LoadingDone(1.0, ""));
CHECK_FALSE(wagon->LoadingDone(1.0, "ruda"));
wagon->LoadStatus = 4;
CHECK(wagon->LoadingDone(1.0, ""));
}
SUBCASE("unloading, step by step, what was asked or all of it, the name going with the last of it") {
wagon->CommandIn.Value1 = 4.0; // asked to unload
wagon->LastLoadChangeTime = 2.0; // at 1 a second
CHECK_FALSE(wagon->LoadingDone(-1.0, "coal"));
CHECK(wagon->LoadStatus == 2);
CHECK(wagon->LoadAmount == doctest::Approx(8.0));
CHECK(wagon->LastLoadChangeTime == doctest::Approx(0.0));
// nothing done since the last step
CHECK_FALSE(wagon->LoadingDone(-1.0, "coal"));
CHECK(wagon->LoadAmount == doctest::Approx(8.0));
wagon->LastLoadChangeTime = 2.0;
CHECK(wagon->LoadingDone(-1.0, "coal"));
CHECK(wagon->LoadAmount == doctest::Approx(6.0));
wagon->CommandIn.Value1 = 100.0;
wagon->LastLoadChangeTime = 10.0;
CHECK(wagon->LoadingDone(-1.0, "coal"));
CHECK(wagon->LoadAmount == doctest::Approx(0.0));
CHECK(wagon->LoadType.name.empty());
}
SUBCASE("loading, step by step, up to what was asked or what the vehicle takes, slower when overloaded") {
wagon->CommandIn.Value1 = 5.0;
wagon->LastLoadChangeTime = 2.0;
CHECK_FALSE(wagon->LoadingDone(1.0, "coal"));
CHECK(wagon->LoadStatus == 1);
CHECK(wagon->LoadAmount == doctest::Approx(12.0));
wagon->LastLoadChangeTime = 3.0;
CHECK(wagon->LoadingDone(1.0, "coal"));
CHECK(wagon->LoadAmount == doctest::Approx(15.0));
wagon->CommandIn.Value1 = 1000.0;
wagon->LastLoadChangeTime = 1000.0;
CHECK(wagon->LoadingDone(1.0, "coal"));
CHECK(wagon->LoadAmount == doctest::Approx(capacity));
CHECK(capacity == doctest::Approx(2.5 * wagon->MaxLoad));
// over the vehicle's own capacity, the change goes at half the speed
wagon->CommandIn.Value1 = 1000.0;
wagon->LastLoadChangeTime = 4.0;
CHECK_FALSE(wagon->LoadingDone(-1.0, "coal"));
CHECK(wagon->LoadAmount == doctest::Approx(capacity - 2.0));
}
}