mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 07:07:05 +01:00
d0387753f8
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.
672 lines
32 KiB
C++
672 lines
32 KiB
C++
#include "stdafx.h"
|
|
#include "doctest.h"
|
|
#include "vehicle.h"
|
|
#include "utilities/Globals.h"
|
|
|
|
using test::definition;
|
|
using test::vehicle;
|
|
|
|
namespace {
|
|
|
|
// the wagon from data/wagon.json with automatic couplers at both ends, which couple the given connections
|
|
definition automatic_definition(CouplingSet const Flag = Coupling::coupler | Coupling::brakeHose) {
|
|
return definition::load("wagon").set("BuffCoupl", "CType", "Automatic").set("BuffCoupl", "AutomaticFlag", Flag);
|
|
}
|
|
|
|
// the wagon from data/wagon.json with automatic couplers at both ends, running at the given speed [km/h]
|
|
vehicle automatic_wagon(double Velocity, std::string const &Name) {
|
|
return vehicle{automatic_definition(), Velocity, Name};
|
|
}
|
|
|
|
// the same wagon, twice as heavy
|
|
vehicle heavy_automatic_wagon(double Velocity, std::string const &Name) {
|
|
return vehicle{automatic_definition().set("Param", "M", 2 * 42200), Velocity, Name};
|
|
}
|
|
|
|
bool derailed(vehicle &Vehicle) {
|
|
return Vehicle->DamageFlag.HasAll(VehicleDamage::derailed);
|
|
}
|
|
|
|
// runs the pair for Seconds, returns whether the vehicles were coupled at any point
|
|
bool couple_during(test::pair &Pair, double Seconds) {
|
|
auto coupled{false};
|
|
for (auto i{std::lround(Seconds / 0.01)}; i > 0; --i) {
|
|
Pair.step();
|
|
coupled = coupled || Pair.coupled();
|
|
}
|
|
return coupled;
|
|
}
|
|
|
|
// sets the crash damage setting for the test, restores it after
|
|
class crash_damage_setting {
|
|
public:
|
|
explicit crash_damage_setting(bool Enabled) { Global.crash_damage = Enabled; }
|
|
~crash_damage_setting() { Global.crash_damage = m_previous; }
|
|
crash_damage_setting(crash_damage_setting const &) = delete;
|
|
crash_damage_setting &operator=(crash_damage_setting const &) = delete;
|
|
|
|
private:
|
|
bool m_previous{Global.crash_damage};
|
|
};
|
|
|
|
// the legacy collisions of physics.collisions old for the test, restored after it: the rule of the safe closing speed
|
|
// weighed by the ratio of the masses (the crush of the ends of the new model is in test_collisions.cpp)
|
|
class legacy_collisions {
|
|
public:
|
|
legacy_collisions() { Global.collisions = global_settings::collision_model::old; }
|
|
~legacy_collisions() { Global.collisions = m_previous; }
|
|
legacy_collisions(legacy_collisions const &) = delete;
|
|
legacy_collisions &operator=(legacy_collisions const &) = delete;
|
|
|
|
private:
|
|
global_settings::collision_model m_previous{Global.collisions};
|
|
};
|
|
|
|
} // namespace
|
|
|
|
// issue #86: automatic couplers used to catch at any closing speed, so the vehicles never collided nor derailed
|
|
|
|
TEST_CASE("couplers: automatic couplers catch a standing wagon hit at a walking pace") {
|
|
crash_damage_setting const crashdamage{true};
|
|
auto moving{automatic_wagon(10.0, "moving")};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
REQUIRE(moving->Couplers[VehicleEnd::front].type() == TCouplerType::Automatic);
|
|
REQUIRE(moving.speed() == doctest::Approx(10.0));
|
|
|
|
// the front of the moving wagon runs into the rear of the standing one
|
|
CHECK(moving->CollisionVelocity(VehicleEnd::front, standing.physics(), VehicleEnd::rear) == doctest::Approx(10.0));
|
|
CHECK(moving->AutomaticCouplingSafe(VehicleEnd::front, standing.physics(), VehicleEnd::rear));
|
|
CHECK(standing->AutomaticCouplingSafe(VehicleEnd::rear, moving.physics(), VehicleEnd::front));
|
|
}
|
|
|
|
TEST_CASE("couplers: automatic couplers don't catch a wagon hit too fast, the wagons collide instead") {
|
|
crash_damage_setting const crashdamage{true};
|
|
auto moving{automatic_wagon(30.0, "moving")};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
REQUIRE(moving.speed() == doctest::Approx(30.0));
|
|
|
|
CHECK(moving->CollisionVelocity(VehicleEnd::front, standing.physics(), VehicleEnd::rear) > physics::Vehicle::SafeCollisionVelocity);
|
|
CHECK_FALSE(moving->AutomaticCouplingSafe(VehicleEnd::front, standing.physics(), VehicleEnd::rear));
|
|
CHECK_FALSE(standing->AutomaticCouplingSafe(VehicleEnd::rear, moving.physics(), VehicleEnd::front));
|
|
}
|
|
|
|
TEST_CASE("couplers: automatic couplers catch a wagon running ahead only a little slower") {
|
|
crash_damage_setting const crashdamage{true};
|
|
auto faster{automatic_wagon(60.0, "faster")};
|
|
auto slower{automatic_wagon(50.0, "slower")};
|
|
|
|
CHECK(faster->CollisionVelocity(VehicleEnd::front, slower.physics(), VehicleEnd::rear) == doctest::Approx(10.0));
|
|
CHECK(slower->CollisionVelocity(VehicleEnd::rear, faster.physics(), VehicleEnd::front) == doctest::Approx(10.0));
|
|
CHECK(faster->AutomaticCouplingSafe(VehicleEnd::front, slower.physics(), VehicleEnd::rear));
|
|
}
|
|
|
|
TEST_CASE("couplers: without crash damage automatic couplers catch at any speed") {
|
|
crash_damage_setting const crashdamage{false};
|
|
auto moving{automatic_wagon(30.0, "moving")};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
|
|
CHECK(moving->AutomaticCouplingSafe(VehicleEnd::front, standing.physics(), VehicleEnd::rear));
|
|
}
|
|
|
|
// the closing speed used to treat the speed of each vehicle as a vector turned by half its heading, so for vehicles
|
|
// facing each other it came out as the root of the sum of the squares of their speeds, rather than the sum
|
|
|
|
TEST_CASE("couplers: the closing speed of a rear-end collision is the difference of the speeds") {
|
|
auto faster{automatic_wagon(40.0, "faster")};
|
|
auto slower{automatic_wagon(25.0, "slower")};
|
|
|
|
// running forwards, the front of the faster wagon hits the rear of the slower one
|
|
CHECK(faster->CollisionVelocity(VehicleEnd::front, slower.physics(), VehicleEnd::rear) == doctest::Approx(15.0));
|
|
CHECK(slower->CollisionVelocity(VehicleEnd::rear, faster.physics(), VehicleEnd::front) == doctest::Approx(15.0));
|
|
// the wagons running ahead of each other are parting
|
|
CHECK(slower->CollisionVelocity(VehicleEnd::front, faster.physics(), VehicleEnd::rear) == doctest::Approx(-15.0));
|
|
|
|
// backing up, the rear of the faster wagon hits the front of the slower one
|
|
auto backingfaster{automatic_wagon(-40.0, "backing faster")};
|
|
auto backingslower{automatic_wagon(-25.0, "backing slower")};
|
|
CHECK(backingfaster->CollisionVelocity(VehicleEnd::rear, backingslower.physics(), VehicleEnd::front) == doctest::Approx(15.0));
|
|
CHECK(backingslower->CollisionVelocity(VehicleEnd::front, backingfaster.physics(), VehicleEnd::rear) == doctest::Approx(15.0));
|
|
}
|
|
|
|
TEST_CASE("couplers: the closing speed of a head-on collision is the sum of the speeds") {
|
|
crash_damage_setting const crashdamage{true};
|
|
auto first{automatic_wagon(10.0, "first")};
|
|
auto second{automatic_wagon(10.0, "second")};
|
|
|
|
// both wagons run forwards, their fronts meet
|
|
CHECK(first->CollisionVelocity(VehicleEnd::front, second.physics(), VehicleEnd::front) == doctest::Approx(20.0));
|
|
CHECK(second->CollisionVelocity(VehicleEnd::front, first.physics(), VehicleEnd::front) == doctest::Approx(20.0));
|
|
// 20 km/h is above the limit, the couplers don't catch (the old formula gave 14.1 km/h and coupled them)
|
|
CHECK(first->CollisionVelocity(VehicleEnd::front, second.physics(), VehicleEnd::front) > physics::Vehicle::SafeCollisionVelocity);
|
|
CHECK_FALSE(first->AutomaticCouplingSafe(VehicleEnd::front, second.physics(), VehicleEnd::front));
|
|
CHECK_FALSE(second->AutomaticCouplingSafe(VehicleEnd::front, first.physics(), VehicleEnd::front));
|
|
|
|
// both wagons back up, their rears meet
|
|
auto firstbacking{automatic_wagon(-10.0, "first backing")};
|
|
auto secondbacking{automatic_wagon(-10.0, "second backing")};
|
|
CHECK(firstbacking->CollisionVelocity(VehicleEnd::rear, secondbacking.physics(), VehicleEnd::rear) == doctest::Approx(20.0));
|
|
|
|
// running back to back, the wagons part
|
|
CHECK(first->CollisionVelocity(VehicleEnd::rear, second.physics(), VehicleEnd::rear) == doctest::Approx(-20.0));
|
|
CHECK(first->AutomaticCouplingSafe(VehicleEnd::rear, second.physics(), VehicleEnd::rear));
|
|
|
|
// slow enough, the couplers catch
|
|
auto slowfirst{automatic_wagon(5.0, "slow first")};
|
|
auto slowsecond{automatic_wagon(5.0, "slow second")};
|
|
CHECK(slowfirst->CollisionVelocity(VehicleEnd::front, slowsecond.physics(), VehicleEnd::front) == doctest::Approx(10.0));
|
|
CHECK(slowfirst->AutomaticCouplingSafe(VehicleEnd::front, slowsecond.physics(), VehicleEnd::front));
|
|
}
|
|
|
|
TEST_CASE("couplers: the closing speed of a hit on a standing vehicle is the speed of the moving one, whichever way it faces") {
|
|
auto moving{automatic_wagon(20.0, "moving")};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
|
|
// the standing wagon faces away from the moving one
|
|
CHECK(moving->CollisionVelocity(VehicleEnd::front, standing.physics(), VehicleEnd::rear) == doctest::Approx(20.0));
|
|
CHECK(standing->CollisionVelocity(VehicleEnd::rear, moving.physics(), VehicleEnd::front) == doctest::Approx(20.0));
|
|
// the standing wagon faces the moving one
|
|
CHECK(moving->CollisionVelocity(VehicleEnd::front, standing.physics(), VehicleEnd::front) == doctest::Approx(20.0));
|
|
CHECK(standing->CollisionVelocity(VehicleEnd::front, moving.physics(), VehicleEnd::front) == doctest::Approx(20.0));
|
|
// the moving wagon backs up into the standing one
|
|
auto backing{automatic_wagon(-20.0, "backing")};
|
|
CHECK(backing->CollisionVelocity(VehicleEnd::rear, standing.physics(), VehicleEnd::front) == doctest::Approx(20.0));
|
|
CHECK(backing->CollisionVelocity(VehicleEnd::rear, standing.physics(), VehicleEnd::rear) == doctest::Approx(20.0));
|
|
// the moving wagon runs away from the standing one
|
|
CHECK(moving->CollisionVelocity(VehicleEnd::rear, standing.physics(), VehicleEnd::front) == doctest::Approx(-20.0));
|
|
}
|
|
|
|
// the coupling as the simulation runs it: two vehicles on a track, closing in on each other until the couplers (or
|
|
// buffers) meet, the coupler forces and the collision computed by the physics, step by step
|
|
|
|
TEST_CASE("couplers: automatic couplers catch right up to the safe closing speed") {
|
|
crash_damage_setting const crashdamage{true};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
|
|
auto below{automatic_wagon(14.9, "below")};
|
|
CHECK(below->CollisionVelocity(VehicleEnd::front, standing.physics(), VehicleEnd::rear) == doctest::Approx(14.9));
|
|
CHECK(below->AutomaticCouplingSafe(VehicleEnd::front, standing.physics(), VehicleEnd::rear));
|
|
|
|
auto above{automatic_wagon(15.1, "above")};
|
|
CHECK(above->CollisionVelocity(VehicleEnd::front, standing.physics(), VehicleEnd::rear) == doctest::Approx(15.1));
|
|
CHECK_FALSE(above->AutomaticCouplingSafe(VehicleEnd::front, standing.physics(), VehicleEnd::rear));
|
|
auto faster{automatic_wagon(15.5, "faster")};
|
|
CHECK_FALSE(faster->AutomaticCouplingSafe(VehicleEnd::front, standing.physics(), VehicleEnd::rear));
|
|
}
|
|
|
|
TEST_CASE("couplers: a wagon rolling slowly into a standing one couples with it") {
|
|
crash_damage_setting const crashdamage{true};
|
|
auto moving{automatic_wagon(5.0, "moving")};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
|
|
|
|
pair.run(3.0);
|
|
|
|
REQUIRE(pair.coupled());
|
|
CHECK(moving->Couplers[VehicleEnd::front].CouplingFlag == (Coupling::coupler | Coupling::brakeHose));
|
|
CHECK(standing->Couplers[VehicleEnd::rear].CouplingFlag == (Coupling::coupler | Coupling::brakeHose));
|
|
CHECK(moving->Couplers[VehicleEnd::front].Connected == &standing.physics());
|
|
CHECK_FALSE(derailed(moving));
|
|
CHECK_FALSE(derailed(standing));
|
|
// coupled, they run on together
|
|
pair.run(2.0);
|
|
CHECK(moving.speed() == doctest::Approx(standing.speed()).epsilon(0.05));
|
|
CHECK(pair.gap() < 0.2);
|
|
}
|
|
|
|
TEST_CASE("couplers: a wagon hitting a standing one too fast doesn't couple, nor once the hit has slowed it down") {
|
|
crash_damage_setting const crashdamage{true};
|
|
legacy_collisions const legacy;
|
|
auto moving{automatic_wagon(30.0, "moving")};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
|
|
|
|
auto touched{false};
|
|
auto parted{false};
|
|
auto coupled{false};
|
|
auto locked{true};
|
|
for (int i = 0; i < 1000; ++i) {
|
|
pair.step();
|
|
// as far as the couplers know: the distance between them at the start of the step
|
|
auto const distance{moving->Neighbours[VehicleEnd::front].distance};
|
|
touched = touched || distance < 0.0;
|
|
parted = parted || (touched && distance > 0.05);
|
|
coupled = coupled || pair.coupled();
|
|
if (touched && false == parted) {
|
|
locked = locked && false == moving->Couplers[VehicleEnd::front].AutomaticCouplingAllowed && false == standing->Couplers[VehicleEnd::rear].AutomaticCouplingAllowed;
|
|
}
|
|
}
|
|
|
|
REQUIRE(touched);
|
|
CHECK_FALSE(coupled);
|
|
// the hit derailed and stopped them, the couplers next to each other, the automatic coupling locked all along
|
|
CHECK_FALSE(parted);
|
|
CHECK(locked);
|
|
CHECK(derailed(moving));
|
|
CHECK(derailed(standing));
|
|
CHECK(std::abs(moving.speed()) < 3.0);
|
|
CHECK(std::abs(standing.speed()) < 3.0);
|
|
CHECK_FALSE(couple_during(pair, 5.0));
|
|
}
|
|
|
|
TEST_CASE("couplers: the automatic coupling locked by a hit is unlocked once the vehicles part") {
|
|
crash_damage_setting const crashdamage{true};
|
|
auto moving{automatic_wagon(3.0, "moving")};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
// as a hit too fast or manual uncoupling leaves them
|
|
moving->Couplers[VehicleEnd::front].AutomaticCouplingAllowed = false;
|
|
standing->Couplers[VehicleEnd::rear].AutomaticCouplingAllowed = false;
|
|
|
|
SUBCASE("parted by more than 5 cm, they couple on the next approach") {
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 0.5};
|
|
pair.step();
|
|
CHECK(moving->Couplers[VehicleEnd::front].AutomaticCouplingAllowed);
|
|
CHECK(couple_during(pair, 3.0));
|
|
}
|
|
SUBCASE("kept together, they don't") {
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 0.0};
|
|
auto coupled{false};
|
|
for (int i = 0; i < 300; ++i) {
|
|
pair.step();
|
|
if (pair.gap() > 0.05) {
|
|
break; // parted
|
|
}
|
|
coupled = coupled || pair.coupled();
|
|
}
|
|
CHECK_FALSE(coupled);
|
|
}
|
|
}
|
|
|
|
// the couplers refuse a hit by the closing speed when they meet, the derail check (CollisionDetect()) takes the one
|
|
// left when the buffers are pressed in all the way, after their springs have slowed the hit down: for the wagons here
|
|
// by about 7 km/h. so a hit a little over the safe closing speed only bounces the vehicles apart
|
|
|
|
TEST_CASE("couplers: a hit a little too fast for the couplers bounces the vehicles apart, uncoupled and on the rails") {
|
|
crash_damage_setting const crashdamage{true};
|
|
legacy_collisions const legacy;
|
|
auto moving{automatic_wagon(20.0, "moving")};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
|
|
|
|
CHECK_FALSE(couple_during(pair, 3.0));
|
|
CHECK_FALSE(derailed(moving));
|
|
CHECK_FALSE(derailed(standing));
|
|
CHECK(pair.gap() > 0.5);
|
|
CHECK(standing.speed() > moving.speed());
|
|
}
|
|
|
|
TEST_CASE("couplers: a hit too fast for the couplers derails the vehicles over the limit adjusted for their mass ratio") {
|
|
crash_damage_setting const crashdamage{true};
|
|
legacy_collisions const legacy;
|
|
|
|
SUBCASE("equal masses: both derail") {
|
|
auto moving{automatic_wagon(30.0, "moving")};
|
|
auto standing{automatic_wagon(0.0, "standing")};
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
|
|
CHECK_FALSE(couple_during(pair, 5.0));
|
|
CHECK(derailed(moving));
|
|
CHECK(derailed(standing));
|
|
}
|
|
SUBCASE("half the mass: between the safe closing speed and twice that, only the lighter one derails") {
|
|
auto light{automatic_wagon(27.0, "light")};
|
|
auto heavy{heavy_automatic_wagon(0.0, "heavy")};
|
|
test::pair pair{light, VehicleEnd::front, heavy, VehicleEnd::rear, 1.0};
|
|
CHECK_FALSE(couple_during(pair, 5.0));
|
|
CHECK(derailed(light));
|
|
CHECK_FALSE(derailed(heavy));
|
|
}
|
|
SUBCASE("half the mass: above twice the safe closing speed both derail") {
|
|
auto light{automatic_wagon(45.0, "light")};
|
|
auto heavy{heavy_automatic_wagon(0.0, "heavy")};
|
|
test::pair pair{light, VehicleEnd::front, heavy, VehicleEnd::rear, 1.0};
|
|
CHECK_FALSE(couple_during(pair, 5.0));
|
|
CHECK(derailed(light));
|
|
CHECK(derailed(heavy));
|
|
}
|
|
SUBCASE("half the mass: below the safe closing speed the couplers catch, nothing derails") {
|
|
auto light{automatic_wagon(12.0, "light")};
|
|
auto heavy{heavy_automatic_wagon(0.0, "heavy")};
|
|
test::pair pair{light, VehicleEnd::front, heavy, VehicleEnd::rear, 1.0};
|
|
CHECK(couple_during(pair, 5.0));
|
|
CHECK_FALSE(derailed(light));
|
|
CHECK_FALSE(derailed(heavy));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("couplers: wagons running head-on into each other couple only up to the safe closing speed") {
|
|
crash_damage_setting const crashdamage{true};
|
|
legacy_collisions const legacy;
|
|
|
|
SUBCASE("7 + 7 km/h, the couplers catch") {
|
|
auto first{automatic_wagon(7.0, "first")};
|
|
auto second{automatic_wagon(7.0, "second")};
|
|
test::pair pair{first, VehicleEnd::front, second, VehicleEnd::front, 1.0};
|
|
CHECK(couple_during(pair, 3.0));
|
|
CHECK_FALSE(derailed(first));
|
|
CHECK_FALSE(derailed(second));
|
|
}
|
|
SUBCASE("8 + 8 km/h, the wagons bounce apart") {
|
|
auto first{automatic_wagon(8.0, "first")};
|
|
auto second{automatic_wagon(8.0, "second")};
|
|
test::pair pair{first, VehicleEnd::front, second, VehicleEnd::front, 1.0};
|
|
CHECK_FALSE(couple_during(pair, 3.0));
|
|
CHECK_FALSE(derailed(first));
|
|
CHECK_FALSE(derailed(second));
|
|
// both backwards, away from each other
|
|
CHECK(first.speed() < 0.0);
|
|
CHECK(second.speed() < 0.0);
|
|
}
|
|
SUBCASE("backing up, 8 + 8 km/h, the wagons bounce apart") {
|
|
auto first{automatic_wagon(-8.0, "first")};
|
|
auto second{automatic_wagon(-8.0, "second")};
|
|
test::pair pair{first, VehicleEnd::rear, second, VehicleEnd::rear, 1.0};
|
|
CHECK_FALSE(couple_during(pair, 3.0));
|
|
CHECK(first.speed() > 0.0);
|
|
CHECK(second.speed() > 0.0);
|
|
}
|
|
SUBCASE("15 + 15 km/h, both derail") {
|
|
auto first{automatic_wagon(15.0, "first")};
|
|
auto second{automatic_wagon(15.0, "second")};
|
|
test::pair pair{first, VehicleEnd::front, second, VehicleEnd::front, 1.0};
|
|
CHECK_FALSE(couple_during(pair, 5.0));
|
|
CHECK(derailed(first));
|
|
CHECK(derailed(second));
|
|
}
|
|
}
|
|
|
|
// the collision used to take the speed of a vehicle facing the other way as if it faced the same way, so a wagon
|
|
// pushed away by the buffers of the vehicle hitting it head-on counted as running into it: the hit slowed it down,
|
|
// and the hitter bounced back as from a wall
|
|
|
|
TEST_CASE("couplers: a wagon hit head-on runs off the same as one hit from behind") {
|
|
// without automatic couplers or crash damage, the collision alone
|
|
crash_damage_setting const crashdamage{false};
|
|
auto speeds_after_hit = [](VehicleEnd StruckEnd) {
|
|
auto moving{vehicle{definition::load("wagon"), 20.0, "moving"}};
|
|
auto struck{vehicle{definition::load("wagon"), 0.0, "struck"}};
|
|
test::pair pair{moving, VehicleEnd::front, struck, StruckEnd, 0.5};
|
|
auto touched{false};
|
|
for (int i = 0; i < 300; ++i) {
|
|
pair.step();
|
|
touched = touched || pair.gap() < 0.0;
|
|
if (touched && pair.gap() > 0.3) {
|
|
break; // bounced off
|
|
}
|
|
}
|
|
REQUIRE(touched);
|
|
REQUIRE(pair.gap() > 0.3);
|
|
REQUIRE_FALSE(pair.coupled());
|
|
return std::make_pair(moving.speed(), struck.speed());
|
|
};
|
|
|
|
auto const [hitterfrombehind, struckfrombehind]{speeds_after_hit(VehicleEnd::rear)};
|
|
auto const [hitterheadon, struckheadon]{speeds_after_hit(VehicleEnd::front)};
|
|
|
|
// hit from behind, the struck wagon runs off forwards, faster than the hitter
|
|
CHECK(struckfrombehind > 10.0);
|
|
CHECK(hitterfrombehind < struckfrombehind);
|
|
// hit head-on, it runs off backwards, as fast
|
|
CHECK(struckheadon == doctest::Approx(-struckfrombehind).epsilon(0.02));
|
|
CHECK(hitterheadon == doctest::Approx(hitterfrombehind).epsilon(0.05));
|
|
}
|
|
|
|
// the other conditions of automatic coupling
|
|
|
|
TEST_CASE("couplers: an automatic coupler doesn't catch a coupler of another type") {
|
|
crash_damage_setting const crashdamage{true};
|
|
auto moving{automatic_wagon(5.0, "moving")};
|
|
auto standing{vehicle{definition::load("wagon"), 0.0, "standing"}};
|
|
REQUIRE(standing->Couplers[VehicleEnd::rear].type() == TCouplerType::Screw);
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
|
|
|
|
CHECK_FALSE(couple_during(pair, 3.0));
|
|
}
|
|
|
|
TEST_CASE("couplers: automatic couplers of different control systems couple without the control connection") {
|
|
crash_damage_setting const crashdamage{true};
|
|
auto const flag{Coupling::coupler | Coupling::brakeHose | Coupling::control};
|
|
auto moving{vehicle{automatic_definition(flag).set("BuffCoupl", "ControlType", "first"), 5.0, "moving"}};
|
|
|
|
SUBCASE("the same control system") {
|
|
auto standing{vehicle{automatic_definition(flag).set("BuffCoupl", "ControlType", "first"), 0.0, "standing"}};
|
|
REQUIRE(standing->Couplers[VehicleEnd::rear].control_type == "first");
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
|
|
REQUIRE(couple_during(pair, 3.0));
|
|
CHECK(pair.coupling() == flag);
|
|
}
|
|
SUBCASE("a different one") {
|
|
auto standing{vehicle{automatic_definition(flag).set("BuffCoupl", "ControlType", "second"), 0.0, "standing"}};
|
|
REQUIRE(standing->Couplers[VehicleEnd::rear].control_type == "second");
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
|
|
REQUIRE(couple_during(pair, 3.0));
|
|
CHECK(pair.coupling() == (Coupling::coupler | Coupling::brakeHose));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("couplers: the coupler force of vehicles facing each other lands on the couplers holding them") {
|
|
// the force a vehicle takes from its coupler, the other one takes back on its own coupler of the pair, whichever
|
|
// way the vehicles face; it used to land on the far end of the other vehicle when they faced each other.
|
|
// CForce runs along the axis of its own vehicle, so the two forces of the pair are opposite along the track: of
|
|
// opposite signs for vehicles facing the same way, of the same sign for vehicles facing each other
|
|
auto first{automatic_wagon(5.0, "first")};
|
|
auto second{automatic_wagon(0.0, "second")};
|
|
|
|
SUBCASE("facing each other") {
|
|
test::pair pair{first, VehicleEnd::front, second, VehicleEnd::front, 1.0};
|
|
pair.run(5.0);
|
|
REQUIRE(pair.coupled());
|
|
|
|
pair.step();
|
|
REQUIRE(second->Couplers[VehicleEnd::front].CForce != 0.0);
|
|
CHECK(first->Couplers[VehicleEnd::front].CForce == doctest::Approx(second->Couplers[VehicleEnd::front].CForce));
|
|
CHECK(first->Couplers[VehicleEnd::rear].CForce == 0.0); // nothing there
|
|
CHECK(second->Couplers[VehicleEnd::rear].CForce == 0.0);
|
|
}
|
|
SUBCASE("facing the same way") {
|
|
test::pair pair{first, VehicleEnd::front, second, VehicleEnd::rear, 1.0};
|
|
pair.run(5.0);
|
|
REQUIRE(pair.coupled());
|
|
|
|
pair.step();
|
|
REQUIRE(second->Couplers[VehicleEnd::rear].CForce != 0.0);
|
|
CHECK(first->Couplers[VehicleEnd::front].CForce == doctest::Approx(-second->Couplers[VehicleEnd::rear].CForce));
|
|
CHECK(first->Couplers[VehicleEnd::rear].CForce == 0.0);
|
|
CHECK(second->Couplers[VehicleEnd::front].CForce == 0.0);
|
|
}
|
|
}
|
|
|
|
// eu07/maszyna#553: articulated wagons (T3000e, N-234 and the like) set up with articulated couplers barely moved in a
|
|
// freight train. the joint took the spring force up to 5 cm only, 225 kN, so it overstretched under the train and the
|
|
// sections collided at every step; and each collision scaled the velocities of both vehicles down, rather than only
|
|
// the velocity of one relative to the other, braking the whole train
|
|
|
|
namespace {
|
|
|
|
// the wagon from data/wagon.json, loaded, with articulated couplers at both ends
|
|
vehicle articulated_wagon(double Velocity, std::string const &Name) {
|
|
return vehicle{definition::load("wagon").set("Param", "M", 60000).set("BuffCoupl", "CType", "Articulated"), Velocity, Name};
|
|
}
|
|
|
|
// couples the facing ends of the two vehicles for good, as the scenery does for an articulated set
|
|
void join(vehicle &First, VehicleEnd FirstEnd, vehicle &Second, VehicleEnd SecondEnd) {
|
|
REQUIRE(First->Attach(FirstEnd, SecondEnd, &Second.physics(), Coupling::coupler, true, false));
|
|
REQUIRE(Second->Attach(SecondEnd, FirstEnd, &First.physics(), Coupling::coupler, true, false));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("couplers: an articulated joint takes its full tensile force within its travel") {
|
|
auto wagon{articulated_wagon(0.0, "wagon")};
|
|
auto const &joint{wagon->Couplers[VehicleEnd::front]};
|
|
REQUIRE(joint.type() == TCouplerType::Articulated);
|
|
|
|
CHECK(joint.SpringKC * joint.DmaxC >= doctest::Approx(joint.FmaxC));
|
|
CHECK(joint.SpringKB * joint.DmaxB >= doctest::Approx(joint.FmaxC));
|
|
// and no less than it had before
|
|
CHECK(joint.DmaxC >= 0.05);
|
|
CHECK(joint.DmaxB >= 0.05);
|
|
}
|
|
|
|
TEST_CASE("couplers: an articulated set keeps its momentum when one section jerks the other") {
|
|
// the front section moving, the rear one barely (so that its brakes are released), joined: the jerk overstretches the joint, the sections collide
|
|
// and run on together at half the speed, as the momentum they have between them dictates
|
|
auto front_section{articulated_wagon(20.0, "front")};
|
|
auto rear_section{articulated_wagon(0.1, "rear")};
|
|
join(rear_section, VehicleEnd::front, front_section, VehicleEnd::rear);
|
|
test::pair pair{rear_section, VehicleEnd::front, front_section, VehicleEnd::rear, 0.0};
|
|
REQUIRE(pair.coupled());
|
|
// brakes released, as a locomotive keeps them
|
|
front_section.hold_brake_pipe(5.0);
|
|
rear_section.hold_brake_pipe(5.0);
|
|
|
|
pair.run(3.0, test::track{0.15, 0.0});
|
|
|
|
CHECK(front_section.speed() == doctest::Approx(rear_section.speed()).epsilon(0.1));
|
|
// rolling resistance takes a little in the 3 s, the collisions used to take most of it
|
|
CHECK(0.5 * (front_section.speed() + rear_section.speed()) > 9.0);
|
|
CHECK(0.5 * (front_section.speed() + rear_section.speed()) < 10.05 + 1e-6);
|
|
CHECK(pair.coupled());
|
|
}
|
|
|
|
TEST_CASE("couplers: a collision of coupled vehicles keeps the momentum of the pair") {
|
|
// the same jerk through screw couplers, which collide when stretched beyond their travel as well
|
|
auto front_wagon{vehicle{definition::load("wagon"), 30.0, "front"}};
|
|
auto rear_wagon{vehicle{definition::load("wagon"), 0.1, "rear"}};
|
|
join(rear_wagon, VehicleEnd::front, front_wagon, VehicleEnd::rear);
|
|
test::pair pair{rear_wagon, VehicleEnd::front, front_wagon, VehicleEnd::rear, 0.0};
|
|
REQUIRE(pair.coupled());
|
|
front_wagon.hold_brake_pipe(5.0);
|
|
rear_wagon.hold_brake_pipe(5.0);
|
|
|
|
pair.run(3.0, test::track{0.15, 0.0});
|
|
|
|
CHECK(0.5 * (front_wagon.speed() + rear_wagon.speed()) > 13.5);
|
|
CHECK(0.5 * (front_wagon.speed() + rear_wagon.speed()) < 15.05 + 1e-6);
|
|
}
|
|
|
|
// eu07/maszyna#520: an electric and a diesel locomotive got their control coupled (multiple-unit control) when the
|
|
// scenario or the Starter asked for it, or when the couplers let it through, as a Vectron and an SM42 did
|
|
|
|
namespace {
|
|
|
|
// the given setting of the control coupling check for the test, restored after it
|
|
class control_coupling_check_setting {
|
|
public:
|
|
explicit control_coupling_check_setting(bool Enabled) { Global.control_coupling_engine_check = Enabled; }
|
|
~control_coupling_check_setting() { Global.control_coupling_engine_check = m_previous; }
|
|
control_coupling_check_setting(control_coupling_check_setting const &) = delete;
|
|
control_coupling_check_setting &operator=(control_coupling_check_setting const &) = delete;
|
|
|
|
private:
|
|
bool m_previous{Global.control_coupling_engine_check};
|
|
};
|
|
|
|
constexpr CouplingSet scenario_coupling{Coupling::coupler | Coupling::brakeHose | Coupling::mainHose | Coupling::control};
|
|
|
|
// couples the rear of the first vehicle to the front of the second one the way the scenery does (forced, from the
|
|
// first one only), returns the coupling the pair got
|
|
CouplingSet scenario_couple(vehicle &First, vehicle &Second, CouplingSet const couplings = scenario_coupling) {
|
|
REQUIRE(First->Attach(VehicleEnd::rear, VehicleEnd::front, &Second.physics(), couplings, true, false));
|
|
CHECK(Second->Couplers[VehicleEnd::front].CouplingFlag == First->Couplers[VehicleEnd::rear].CouplingFlag);
|
|
return First->Couplers[VehicleEnd::rear].CouplingFlag;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("couplers: an electric and a diesel locomotive don't share their control") {
|
|
control_coupling_check_setting const check{true};
|
|
// the E6ACT names no control system (ControlType), as the Vectron doesn't; the SM42 names its own, the SM03 none
|
|
auto electric{vehicle{definition::fiz("e6act_v2/e6acta"), 0.0, "electric"}};
|
|
REQUIRE(electric->EngineType == TEngineType::ElectricInductionMotor);
|
|
|
|
SUBCASE("a diesel-electric one with its own control system") {
|
|
auto diesel{vehicle{definition::fiz("sm42_v1/6d1"), 0.0, "diesel"}};
|
|
REQUIRE(diesel->EngineType == TEngineType::DieselElectric);
|
|
CHECK_FALSE(electric->ControlCouplingAllowed(VehicleEnd::rear, diesel.physics(), VehicleEnd::front));
|
|
CHECK(scenario_couple(electric, diesel) == scenario_coupling.Without(Coupling::control));
|
|
}
|
|
SUBCASE("a diesel-mechanical one without a control system named, either") {
|
|
auto diesel{vehicle{definition::fiz("sm03_v1/sm03"), 0.0, "diesel"}};
|
|
REQUIRE(diesel->EngineType == TEngineType::DieselEngine);
|
|
CHECK_FALSE(diesel->ControlCouplingAllowed(VehicleEnd::front, electric.physics(), VehicleEnd::rear));
|
|
CHECK(scenario_couple(diesel, electric) == scenario_coupling.Without(Coupling::control));
|
|
}
|
|
SUBCASE("unless both name the same control system") {
|
|
auto diesel{vehicle{definition::fiz("sm03_v1/sm03").set("BuffCoupl", "ControlType", "shared"), 0.0, "diesel"}};
|
|
auto shared{vehicle{definition::fiz("e6act_v2/e6acta").set("BuffCoupl", "ControlType", "shared"), 0.0, "electric"}};
|
|
CHECK(scenario_couple(shared, diesel) == scenario_coupling);
|
|
}
|
|
SUBCASE("unless the vehicles make one unit, coupled for good") {
|
|
auto diesel{vehicle{definition::fiz("sm03_v1/sm03"), 0.0, "diesel"}};
|
|
auto const forgood{scenario_coupling | Coupling::permanent};
|
|
CHECK(scenario_couple(electric, diesel, forgood) == forgood);
|
|
}
|
|
SUBCASE("unless the check is turned off") {
|
|
control_coupling_check_setting const off{false};
|
|
auto diesel{vehicle{definition::fiz("sm42_v1/6d1"), 0.0, "diesel"}};
|
|
CHECK(electric->ControlCouplingAllowed(VehicleEnd::rear, diesel.physics(), VehicleEnd::front));
|
|
CHECK(scenario_couple(electric, diesel) == scenario_coupling);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("couplers: locomotives of the same traction and the cars keep their control coupling") {
|
|
control_coupling_check_setting const check{true};
|
|
|
|
SUBCASE("two locomotives or motor cars of the same traction") {
|
|
// two electric ones, an electric one of each motor type, two diesel ones, a diesel-mechanical and a
|
|
// diesel-electric one, an electric multiple unit's control trailer and its motor car
|
|
std::initializer_list<std::pair<char const *, char const *>> const pairs{
|
|
{"ep09_v2/104e", "et22_v3/201e-w"},
|
|
{"e6act_v2/e6acta", "eu04_v1/eu04-01"},
|
|
{"sm03_v1/sm03", "wmb10_v1/wmb10"},
|
|
{"sm03_v1/sm03", "st44_v2/m62-1151"},
|
|
{"en57_v1/5bgb", "en57_v1/6bai"}};
|
|
for (auto const &paths : pairs) {
|
|
CAPTURE(paths.first);
|
|
CAPTURE(paths.second);
|
|
auto first{vehicle{definition::fiz(paths.first), 0.0, "first"}};
|
|
auto second{vehicle{definition::fiz(paths.second), 0.0, "second"}};
|
|
CHECK(scenario_couple(first, second) == scenario_coupling);
|
|
}
|
|
}
|
|
SUBCASE("a diesel locomotive and a car") {
|
|
auto diesel{vehicle{definition::fiz("sm42_v1/6d1"), 0.0, "diesel"}};
|
|
auto car{vehicle{definition::load("wagon"), 0.0, "car"}};
|
|
CHECK(scenario_couple(diesel, car) == scenario_coupling);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("couplers: automatic couplers of an electric and a diesel locomotive couple without the control connection") {
|
|
crash_damage_setting const crashdamage{true};
|
|
control_coupling_check_setting const check{true};
|
|
auto const flag{Coupling::coupler | Coupling::brakeHose | Coupling::control};
|
|
auto automatic = [flag](std::string const &Path) {
|
|
return definition::fiz(Path).set("BuffCoupl", "CType", "Automatic").set("BuffCoupl", "AutomaticFlag", flag);
|
|
};
|
|
auto moving{vehicle{automatic("e6act_v2/e6acta"), 5.0, "moving"}};
|
|
|
|
SUBCASE("a diesel one") {
|
|
auto standing{vehicle{automatic("sm03_v1/sm03"), 0.0, "standing"}};
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
|
|
REQUIRE(couple_during(pair, 3.0));
|
|
CHECK(pair.coupling() == (Coupling::coupler | Coupling::brakeHose));
|
|
}
|
|
SUBCASE("an electric one") {
|
|
auto standing{vehicle{automatic("ep09_v2/104e"), 0.0, "standing"}};
|
|
test::pair pair{moving, VehicleEnd::front, standing, VehicleEnd::rear, 1.0};
|
|
REQUIRE(couple_during(pair, 3.0));
|
|
CHECK(pair.coupling() == flag);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("couplers: the AI and the player get the coupling the vehicles can take") {
|
|
control_coupling_check_setting const check{true};
|
|
auto electric{vehicle{definition::fiz("e6act_v2/e6acta"), 0.0, "electric"}};
|
|
auto diesel{vehicle{definition::fiz("sm42_v1/6d1"), 0.0, "diesel"}};
|
|
auto other{vehicle{definition::fiz("ep09_v2/104e"), 0.0, "other"}};
|
|
|
|
CHECK(electric->CompatibleCoupling(VehicleEnd::rear, diesel.physics(), VehicleEnd::front, scenario_coupling) == scenario_coupling.Without(Coupling::control));
|
|
CHECK(electric->CompatibleCoupling(VehicleEnd::rear, other.physics(), VehicleEnd::front, scenario_coupling) == scenario_coupling);
|
|
// nothing to take away without the control connection asked for
|
|
CHECK(electric->CompatibleCoupling(VehicleEnd::rear, diesel.physics(), VehicleEnd::front, Coupling::coupler) == Coupling::coupler);
|
|
}
|