18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 04:47:06 +01:00
Files
maszyna/tests/unit/test_engine_start.cpp
T
Hirek d0387753f8 refactor(physics): TMoverParameters becomes physics::Vehicle, the mover is gone
The physics of a vehicle loses the name it had from the Pascal of 2001. Done by
tools/rename/mover_to_physics.py, committed with it:

  physics/mover.h                  physics/vehicle.h
  TMoverParameters                 physics::Vehicle (in namespace physics)
  TDynamicObject::MoverParameters  physics
  mvOccupied, mvControlled         occupied, controlled (TTrain, TController)
  mvSecond, mvThird                secondUnit, thirdUnit
  mvPantographUnit                 pantographUnit
  mover, Mover, movers             physics, Physics, vehicles (locals, params)
  m_mover, othermover, currentMover, from_movers, renderMoverEventSounds
                                   m_physics, otherPhysics, currentPhysics,
                                   from_vehicles, renderPhysicsEventSounds

The code and the comments are renamed, the string literals are not: no text
the game shows, logs or looks up has changed (none had the names). The movers
of the editor, the tracks a slide moves, keep their name.

By hand: two member functions of TDynamicObject (gather_off_track_terrain,
begin_off_track) had a local mover aliasing their own MoverParameters, which
the rename made physics { *physics }; the alias is gone, they use physics->.
No other function has a renamed local meeting a renamed member (checked over
all the functions of the code before the rename).

For the branches made before it: run the script on the branch (python
tools/rename/mover_to_physics.py, from the root of the repository) before
merging it with this one, and the names don't conflict. It can be run again;
the second time it changes nothing.

The class is the same as it was; breaking it into its parts (braking,
electrics, controls, load) and making its data private are the next stages.

No change in behaviour: 1149/1149 tests, golden traces exact against staging
62f7a29b.
2026-11-28 23:56:46 +01:00

299 lines
11 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "consist.h"
#include "vehicle/enginestart.h"
// starting the engines of the vehicles in multiple from the cab (eu07/maszyna#80): the cab's own switch (main_*) works
// all of them unless Engine:JointEngineStart=No keeps it to its own vehicle, and the switches mainb_* and mainc_* start
// the second and the third vehicle alone. the cab (TTrain) passes the vehicle's MainSwitchRange() to its MainSwitch(),
// finds the vehicles with engine(), and runs remote_switch for the switches of the others
using test::consist;
using test::definition;
namespace {
// the SN61 railcar, whose cab has a set of switches for each of up to three railcars in multiple
definition sn61(bool const Joint = true) {
auto railcar{definition::fiz("sn61_v2/sn61")};
if (false == Joint) {
railcar.set("Engine", "JointEngineStart", "No");
}
return railcar;
}
// the battery, the cab and the pumps of every railcar of the train switched on from the first one's cab, the oil
// pressure built up, and the controller at the first position giving the engines fuel, which a mechanical diesel
// starts at: everything but the engines ready to start
void prepare(consist &Train) {
auto &cab{Train[0].physics()};
cab.BatterySwitch(true);
cab.CabActivisation();
cab.OilPumpSwitch(true);
cab.FuelPumpSwitch(true);
while (cab.RList[cab.MainCtrlPos].R == 0.0 && cab.IncMainCtrl(1)) {
// up to the first position with fuel
}
Train.run(20.0);
}
bool running(physics::Vehicle const &Vehicle) {
return Vehicle.Mains && Vehicle.enrot > 0.5 * Vehicle.diesel.minRevolutions;
}
// the time step of the train in the loops below
auto const dt{0.01};
// the count of the steps of dt making up Seconds
int steps(double const Seconds) {
return static_cast<int>(std::lround(Seconds / dt));
}
// the switch of Engine held closed for Seconds, then let go, in steps of the train
void hold(consist &Train, engine_start::remote_switch &Switch, physics::Vehicle *Engine, double const Seconds, bool const Closesonrelease = false) {
for (auto step = 0; step < steps(Seconds); ++step) {
Switch.update(Engine, true, false, dt, Closesonrelease);
Train.step({}, dt);
}
Switch.update(Engine, false, false, dt, Closesonrelease);
}
} // namespace
TEST_CASE("engine start: the joint start is the default, and JointEngineStart=No keeps the cab's switch to its vehicle") {
consist train;
auto const &joint{train.add(sn61(), 1).physics()};
auto const &separate{train.add(sn61(false)).physics()};
CHECK(joint.JointEngineStart);
CHECK_FALSE(separate.JointEngineStart);
CHECK(joint.MainSwitchRange() == range_t::consist);
CHECK(separate.MainSwitchRange() == range_t::local);
}
TEST_CASE("engine start: the vehicles in multiple, counted from the cab's own one, those behind the cab first") {
consist train;
auto &first{train.add(sn61(), 1).physics()};
auto &second{train.add(sn61()).physics()};
auto &third{train.add(sn61()).physics()};
CHECK(engine_start::engine(first, first, 1) == &first);
CHECK(engine_start::engine(first, first, 2) == &second);
CHECK(engine_start::engine(first, first, 3) == &third);
CHECK(engine_start::engine(first, first, 4) == nullptr);
CHECK(engine_start::engine(first, first, 0) == nullptr);
// from the rear cab of the first railcar the others are in front of the cab, in the same order
first.CabOccupied = -1;
CHECK(engine_start::engine(first, first, 2) == &second);
CHECK(engine_start::engine(first, first, 3) == &third);
// from the middle one, the one behind its cab comes first
second.CabOccupied = 1;
CHECK(engine_start::engine(second, second, 2) == &third);
CHECK(engine_start::engine(second, second, 3) == &first);
second.CabOccupied = -1;
CHECK(engine_start::engine(second, second, 2) == &first);
CHECK(engine_start::engine(second, second, 3) == &third);
}
TEST_CASE("engine start: only the vehicles with an engine and under control through the cable count") {
consist train;
auto &first{train.add(sn61(), 1).physics()};
// a vehicle without an engine between the railcars is passed over
auto &trailer{train.add(definition::fiz("ed72_v1/5bs-ra")).physics()};
auto &second{train.add(sn61()).physics()};
REQUIRE(trailer.Power < 0.01);
CHECK(engine_start::engine(first, first, 2) == &second);
// the vehicle with the cab doesn't drive itself: its engine is in the vehicle it controls, which isn't counted twice
trailer.CabOccupied = 1;
CHECK(engine_start::engine(trailer, first, 1) == &first);
CHECK(engine_start::engine(trailer, first, 2) == &second);
CHECK(engine_start::engine(trailer, first, 3) == nullptr);
// coupled without the control cable, the other railcar isn't under the cab's control
consist apart;
auto &alone{apart.add(sn61(), 1).physics()};
apart.add(sn61(), 0, Coupling::coupler | Coupling::brakeHose);
CHECK(engine_start::engine(alone, alone, 2) == nullptr);
}
TEST_CASE("engine start: by default the cab's switch starts and stops the engines of all the vehicles in multiple") {
consist train;
auto &first{train.add(sn61(), 1).physics()};
auto const &second{train.add(sn61()).physics()};
prepare(train);
REQUIRE_FALSE(first.Mains);
REQUIRE_FALSE(second.Mains);
first.MainSwitch(true, first.MainSwitchRange());
train.run(15.0);
CHECK(running(first));
CHECK(running(second));
first.MainSwitch(false, first.MainSwitchRange());
train.run(5.0);
CHECK_FALSE(first.Mains);
CHECK_FALSE(second.Mains);
}
TEST_CASE("engine start: with JointEngineStart=No each engine has its own switch") {
consist train;
auto &first{train.add(sn61(false), 1).physics()};
auto &second{train.add(sn61(false)).physics()};
auto &third{train.add(sn61(false)).physics()};
prepare(train);
// the cab's own switch starts its own engine only
first.MainSwitch(true, first.MainSwitchRange());
train.run(15.0);
REQUIRE(running(first));
CHECK_FALSE(second.Mains);
CHECK_FALSE(third.Mains);
// the second one's switch, mainb_*, starts the second engine
engine_start::remote_switch switchb;
engine_start::remote_switch switchc;
auto *engineb{engine_start::engine(first, first, 2)};
auto *enginec{engine_start::engine(first, first, 3)};
REQUIRE(engineb == &second);
REQUIRE(enginec == &third);
hold(train, switchb, engineb, 2.0); // past the SN61's delay of 1.5 s
train.run(15.0);
CHECK(running(second));
CHECK_FALSE(third.Mains);
// and the third one's, mainc_*, the third
hold(train, switchc, enginec, 2.0);
train.run(15.0);
CHECK(running(third));
// the switches open their own vehicles' circuits only
CHECK(engine_start::remote_switch::open(second));
train.run(5.0);
CHECK_FALSE(second.Mains);
CHECK(running(first));
CHECK(running(third));
first.MainSwitch(false, first.MainSwitchRange());
train.run(5.0);
CHECK_FALSE(first.Mains);
CHECK(running(third));
CHECK_FALSE(engine_start::remote_switch::open(second)); // already stopped
}
TEST_CASE("engine start: the switch of another vehicle is held for the vehicle's delay, with the vehicle ready") {
consist train;
train.add(sn61(false).set("Cntrl", "IniCDelay", "2.0"), 1);
auto &second{train.add(sn61(false).set("Cntrl", "IniCDelay", "2.0")).physics()};
REQUIRE(second.InitialCtrlDelay == doctest::Approx(2.0));
engine_start::remote_switch switchb;
// without the battery the switch does nothing, however long it's held
hold(train, switchb, &second, 5.0);
train.run(5.0);
CHECK_FALSE(second.Mains);
CHECK_FALSE(second.diesel.startupRequested);
prepare(train);
// let go too early
hold(train, switchb, &second, 1.0);
CHECK_FALSE(engine_start::remote_switch::is_running(second));
// the switch opening the circuit held along, the closing one does nothing
for (auto step = 0; step < steps(3.0); ++step) {
switchb.update(&second, true, true, dt, false);
train.step({}, dt);
}
CHECK_FALSE(engine_start::remote_switch::is_running(second));
// held long enough
hold(train, switchb, &second, 2.5);
CHECK(engine_start::remote_switch::is_running(second));
train.run(15.0);
CHECK(running(second));
}
TEST_CASE("engine start: a switch closing on release, as an electric locomotive's impulse switch, waits to be let go") {
consist train;
train.add(sn61(false), 1);
auto &second{train.add(sn61(false).set("Cntrl", "IniCDelay", "1.0")).physics()};
prepare(train);
engine_start::remote_switch switchb;
// held past the delay, the circuit stays open while the switch is held
for (auto step = 0; step < steps(2.0); ++step) {
switchb.update(&second, true, false, dt, true);
train.step({}, dt);
}
CHECK_FALSE(engine_start::remote_switch::is_running(second));
// and closes when let go
switchb.update(&second, false, false, dt, true);
CHECK(engine_start::remote_switch::is_running(second));
train.run(15.0);
REQUIRE(running(second));
// let go before the delay, it doesn't
CHECK(engine_start::remote_switch::open(second));
train.run(5.0);
REQUIRE_FALSE(engine_start::remote_switch::is_running(second));
hold(train, switchb, &second, 0.5, true);
CHECK_FALSE(engine_start::remote_switch::is_running(second));
}
TEST_CASE("engine start: an engine stopping on its own stops the others only when they started together") {
for (auto const joint : {true, false}) {
CAPTURE(joint);
consist train;
auto &first{train.add(sn61(joint), 1).physics()};
auto const &second{train.add(sn61(joint)).physics()};
prepare(train);
engine_start::remote_switch switchb;
first.MainSwitch(true, first.MainSwitchRange());
hold(train, switchb, engine_start::engine(first, first, 2), 2.0);
train.run(15.0);
REQUIRE(running(first));
REQUIRE(running(second));
// the first one's fuel cut off, its engine stops
first.FuelPump.is_disabled = true;
train.run(15.0);
CHECK_FALSE(first.Mains);
CHECK(first.enrot == 0.0);
// started on their own, the others keep running
CHECK(running(second) == (false == joint));
}
}
// a stopped engine stays stopped on its own: only an engine stopping while it ran tells the others, as a joint start's
// engines stop together; one stopped from another vehicle's switch, or not started at all, doesn't stop those running,
// whatever the flags of the vehicles in a mixed train
TEST_CASE("engine start: an engine stopped from its own switch doesn't stop the others when it has spun down") {
consist train;
// the middle railcar keeps the joint start, as a .fiz without the key does. what a vehicle without the cab relays
// goes on away from the cab, so it's the third one that would stop
auto &first{train.add(sn61(false), 1).physics()};
auto &second{train.add(sn61()).physics()};
auto &third{train.add(sn61(false)).physics()};
prepare(train);
engine_start::remote_switch switchb;
engine_start::remote_switch switchc;
first.MainSwitch(true, first.MainSwitchRange());
hold(train, switchb, engine_start::engine(first, first, 2), 2.0);
hold(train, switchc, engine_start::engine(first, first, 3), 2.0);
train.run(15.0);
REQUIRE(running(first));
REQUIRE(running(second));
REQUIRE(running(third));
// mainb_off
CHECK(engine_start::remote_switch::open(second));
train.run(30.0);
CHECK_FALSE(second.Mains);
CHECK(second.enrot == 0.0);
CHECK(running(first));
CHECK(running(third));
}
TEST_CASE("engine start: the stopped engine of the cab's vehicle doesn't stop the one started from its switch") {
consist train;
// the cab's railcar keeps the joint start, its engine stopped
auto &first{train.add(sn61(), 1).physics()};
auto &second{train.add(sn61(false)).physics()};
prepare(train);
REQUIRE_FALSE(first.Mains);
// mainb_on
engine_start::remote_switch switchb;
hold(train, switchb, engine_start::engine(first, first, 2), 2.0);
train.run(15.0);
CHECK(running(second));
CHECK_FALSE(first.Mains);
CHECK(first.enrot == 0.0);
}