diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 0e11916f0..f57e358ee 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2014,6 +2014,9 @@ class TMoverParameters glm::vec3 Front{}; std::string Name; /*nazwa wlasna*/ TCoupling Couplers[2]; // urzadzenia zderzno-sprzegowe, polaczenia miedzy wagonami + // the physical coupler model covers the vehicle: it's in the player's train or in one the player's train closes in on + // (vehicle_table::update_coupler_physics()); with couplerphysics all in eu07.ini every vehicle is covered + bool CouplerPhysics{false}; std::array Neighbours; // potential collision sources bool Power110vIsAvailable = false; // cached availability of 110v power bool Power24vIsAvailable = false; // cached availability of 110v power @@ -2540,6 +2543,9 @@ class TMoverParameters double BrakeForceP(double press, double velocity) const; double BrakeForce(const TTrackParam &Track); double CouplerForce(int const End, double dt); + // whether the coupling at the end is computed by the physical model: couplerphysics set in eu07.ini, and this + // vehicle or the one at the end covered by it (CouplerPhysics), or every vehicle with couplerphysics all + bool coupler_physics_at(int const End) const; // the force of the coupling at the end by the physical model (couplerphysics in eu07.ini), see couplerphysics.h double coupler_force_physical(int const End, double const dt); // with the physical model: whether the vehicles at the end hit the stop of their springs at the last update and diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index b835d63ef..016db1b1b 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -1337,10 +1337,14 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) auto const otherend{Neighbours[End].vehicle_end}; auto const &othercoupler{othervehicle->Couplers[otherend]}; - if (Global.coupler_physics && false == coupler_stop_hit(End, *othervehicle, otherend)) + auto const physical{coupler_physics_at(End)}; + if (physical && false == coupler_stop_hit(End, *othervehicle, otherend)) { return; } + // the share of the energy of the relative motion the hit takes: in the physical model the springs gone solid give + // for good, rather than spring back as the legacy model has it + auto const loss{physical ? coupler_physics::solid_hit_loss(thiscoupler.physics, othercoupler.physics) : (thiscoupler.beta + othercoupler.beta) / 2.0}; // the velocities along this vehicle's axis: the other vehicle's one is turned around when the vehicles face each // other (touch with the same ends), as in CouplerForce() @@ -1355,12 +1359,12 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) { case 0: { - CCF = ComputeCollision(velocity, othervehiclevelocity, TotalMass, othervehicle->TotalMass, (thiscoupler.beta + othercoupler.beta) / 2.0, VirtualCoupling) / dt; + CCF = ComputeCollision(velocity, othervehiclevelocity, TotalMass, othervehicle->TotalMass, loss, VirtualCoupling) / dt; break; // yB: ej ej ej, a po } case 1: { - CCF = ComputeCollision(othervehiclevelocity, velocity, othervehicle->TotalMass, TotalMass, (thiscoupler.beta + othercoupler.beta) / 2.0, VirtualCoupling) / dt; + CCF = ComputeCollision(othervehiclevelocity, velocity, othervehicle->TotalMass, TotalMass, loss, VirtualCoupling) / dt; break; } default: @@ -1372,7 +1376,7 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) if (Global.crash_damage) { // the physical model breaks the coupling and crushes the buffers by the force of the hit (coupler_stop_hit()) - if (false == Global.coupler_physics && -thiscoupler.Dist >= thiscoupler.DmaxB && FuzzyLogic(std::abs(CCF), 5.0 * (thiscoupler.FmaxC + 1.0), p_coupldmg)) + if (false == physical && -thiscoupler.Dist >= thiscoupler.DmaxB && FuzzyLogic(std::abs(CCF), 5.0 * (thiscoupler.FmaxC + 1.0), p_coupldmg)) { // small chance to smash the coupler if it's hit with excessive force damage_coupler(End); @@ -1404,7 +1408,7 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) // adjust velocity and acceleration of affected vehicles. the physical model changes the velocities alone: the // integration of the next step (ComputeMovement()) would carry a quarter of an acceleration spike over into the // velocity once more, and the vehicles would bounce off the stop faster than they hit it - auto const accelerationspike{Global.coupler_physics ? 0.0 : 1.0}; + auto const accelerationspike{physical ? 0.0 : 1.0}; if (false == TestFlag(DamageFlag, dtrain_out)) { auto const accelerationchange{(velocity - V) / dt}; @@ -1451,7 +1455,7 @@ void TMoverParameters::damage_coupler(int const End) othercoupler.CouplingFlag = coupling::faux; // the coupling broke between the two, either end can be repaired othercoupler.broken = true; - if (hosetorn && Global.coupler_physics) + if (hosetorn && coupler_physics_at(End)) { // the brake pipe of the other part of the train vents through the torn hose as well thiscoupler.Connected->AlarmChainFlag = true; @@ -5515,7 +5519,7 @@ double TMoverParameters::Adhesive(double staticfriction) const // ************************************************************************************************* double TMoverParameters::CouplerForce(int const End, double dt) { - if (Global.coupler_physics) + if (coupler_physics_at(End)) { return coupler_force_physical(End, dt); } @@ -5649,6 +5653,21 @@ double TMoverParameters::CouplerForce(int const End, double dt) return CF; } +bool TMoverParameters::coupler_physics_at(int const End) const +{ + if (false == Global.coupler_physics) + { + return false; + } + if (Global.coupler_physics_all || CouplerPhysics) + { + return true; + } + // either vehicle of the pair covered takes the pair in, so both of them compute it the same way + auto const *other{Neighbours[End].vehicle}; + return other != nullptr && other->MoverParameters->CouplerPhysics; +} + void TMoverParameters::automatic_coupling(int const End, TMoverParameters &Other, int const OtherEnd) { auto &thiscoupler{Couplers[End]}; @@ -11194,6 +11213,7 @@ double default_slack(TCouplerType const Type) // UnloadB, UnloadC [-] force of the unloading curve of the buffers and of the draw gear to that of the loading one // FyieldC [kN] the force past which the link stretches for good; DbreakC [m] the elongation at which it breaks // kL [kN/m] stiffness of the steel parts past the travel of the springs; FcrushB [kN] the buffers give under it +// RestitutionSolid [-] coefficient of restitution of a hit on the springs gone solid coupler_physics::end_parameters coupler_physics_parameters(TCoupling const &Coupler, std::string const &Line) { coupler_physics::end_parameters parameters; @@ -11217,6 +11237,7 @@ coupler_physics::end_parameters coupler_physics_parameters(TCoupling const &Coup // the steel parts give a few millimetres under the rated force parameters.link_stiffness = optional_kilo_value(Line, "kL", ultimate / 0.003); parameters.crush_force = optional_kilo_value(Line, "FcrushB", 2.5 * std::max(Coupler.FmaxB, Coupler.FmaxC)); + parameters.solid_restitution = std::clamp(optional_value(Line, "RestitutionSolid", parameters.solid_restitution), 0.05, 0.95); return parameters; } diff --git a/McZapkie/couplerphysics.cpp b/McZapkie/couplerphysics.cpp index ab253e7c0..f6a226b55 100644 --- a/McZapkie/couplerphysics.cpp +++ b/McZapkie/couplerphysics.cpp @@ -211,6 +211,12 @@ link_strength weaker_link(end_parameters const &First, end_parameters const &Sec return link; } +double solid_hit_loss(end_parameters const &First, end_parameters const &Second) { + // the collision needs some bounce left (ComputeCollision() asserts it) + auto const restitution{std::clamp(0.5 * (First.solid_restitution + Second.solid_restitution), 0.05, 0.95)}; + return 1.0 - restitution * restitution; +} + void pair_state::couple() { draw_gear.reset(); elongation = 0.0; diff --git a/McZapkie/couplerphysics.h b/McZapkie/couplerphysics.h index 98eaf8b3c..47789aba2 100644 --- a/McZapkie/couplerphysics.h +++ b/McZapkie/couplerphysics.h @@ -79,9 +79,16 @@ struct end_parameters { double damping{0.0}; // viscous damping of the devices, while pressed [N s/m] double link_stiffness{1.0}; // of the steel parts of the end, past the travel of the springs [N/m] double crush_force{std::numeric_limits::max()}; // a hit on the buffers past their travel with this force crushes them [N] + // coefficient of restitution of a hit on the springs gone solid: the steel of the housings and the underframe gives + // for good rather than springing back, so the vehicles share their momentum rather than pass it on + double solid_restitution{0.2}; link_strength link; }; +// the share of the energy of the relative motion a hit on the solid springs of the two ends takes, as the legacy +// collision (ComputeCollision()) takes it: 1 - e^2 +double solid_hit_loss(end_parameters const &First, end_parameters const &Second); + // the weaker of the links of the two ends, which gives first; the same whichever end asks link_strength weaker_link(end_parameters const &First, end_parameters const &Second); diff --git a/application/driveruipanels.cpp b/application/driveruipanels.cpp index c86d017c6..d83911e7b 100644 --- a/application/driveruipanels.cpp +++ b/application/driveruipanels.cpp @@ -1555,6 +1555,12 @@ std::string_view coupler_type_name( TCouplerType const Type ) { } } +// the couplerphysics setting of eu07.ini +std::string_view coupler_physics_setting() { + if( Global.coupler_physics_all ) { return "all"; } + return Global.coupler_physics ? "yes" : "no"; +} + std::string_view coupler_stop_name( int const Stop ) { if( Stop > 0 ) { return "draw gear solid"; } if( Stop < 0 ) { return "buffers solid"; } @@ -1599,6 +1605,7 @@ bool edit_link( coupler_physics::end_parameters &Parameters ) { } changed = edit_value( "Link stiffness [kN/m]", Parameters.link_stiffness, 1000.0, 1000.0, 2000000.0, "%.0f" ) || changed; changed = edit_value( "Buffers crush force [kN]", Parameters.crush_force, 1000.0, 100.0, 10000.0, "%.0f" ) || changed; + changed = edit_value( "Restitution of a solid hit", Parameters.solid_restitution, 1.0, 0.05, 0.95, "%.2f" ) || changed; ImGui::PopID(); return changed; } @@ -1637,7 +1644,12 @@ debug_panel::update_section_coupler( std::vector &Output ) { std::format( "{}, {} end, {} coupler{}", vehicle->name(), nearerfront ? "front" : "rear", coupler_type_name( coupler.type() ), coupler.has_adapter() ? " (adapter)" : "" ), Global.UITextColor ); Output.emplace_back( - std::format( "Model: {}, crash damage {}", Global.coupler_physics ? "physical" : "legacy", Global.crash_damage ? "on" : "off" ), + std::format( + "Model: {} (couplerphysics {}, vehicle {}covered), crash damage {}", + mover.coupler_physics_at( m_coupler.end ) ? "physical" : "legacy", + coupler_physics_setting(), + mover.CouplerPhysics ? "" : "not ", + Global.crash_damage ? "on" : "off" ), Global.UITextColor ); if( neighbour.vehicle != nullptr ) { Output.emplace_back( @@ -1668,6 +1680,7 @@ debug_panel::render_section_coupler() { ImGui::Separator(); ImGui::Checkbox( "Physical coupler model", &Global.coupler_physics ); + ImGui::Checkbox( "Physical model for every train", &Global.coupler_physics_all ); ImGui::Checkbox( "Crash damage", &Global.crash_damage ); ImGui::Checkbox( "Apply to the other end of the pair too", &m_couplerbothends ); diff --git a/tests/unit/test_coupler_physics.cpp b/tests/unit/test_coupler_physics.cpp index 29a9438fa..6785a2cb3 100644 --- a/tests/unit/test_coupler_physics.cpp +++ b/tests/unit/test_coupler_physics.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include "doctest.h" #include "vehicle.h" +#include "consist.h" #include "McZapkie/couplerphysics.h" #include "utilities/Globals.h" @@ -308,6 +309,7 @@ TEST_CASE("coupler physics: the play of the coupling and the parameters of the m TEST_CASE("coupler physics: a coupling with play pulls only once the play is taken up") { flag_setting const physics{Global.coupler_physics, true}; + flag_setting const everywhere{Global.coupler_physics_all, true}; 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")}; @@ -336,6 +338,7 @@ TEST_CASE("coupler physics: a coupling with play pulls only once the play is tak TEST_CASE("coupler physics: buffers hit at a walking pace give back less than they took") { flag_setting const physics{Global.coupler_physics, true}; + flag_setting const everywhere{Global.coupler_physics_all, true}; flag_setting const crashdamage{Global.crash_damage, true}; auto moving{vehicle{definition::load("wagon"), 5.0, "moving"}}; // barely moving, so that its brakes are released @@ -371,6 +374,7 @@ TEST_CASE("coupler physics: buffers hit at a walking pace give back less than th TEST_CASE("coupler physics: coupled vehicles at rest stay at rest") { flag_setting const physics{Global.coupler_physics, true}; + 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); @@ -384,6 +388,7 @@ TEST_CASE("coupler physics: coupled vehicles at rest stay at rest") { TEST_CASE("coupler physics: a jerk on a coupling with play breaks it, the brake hoses torn off") { flag_setting const physics{Global.coupler_physics, true}; + 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); @@ -415,6 +420,7 @@ TEST_CASE("coupler physics: a jerk on a coupling with play breaks it, the brake TEST_CASE("coupler physics: a gentle pull on a coupling with play doesn't hurt it") { flag_setting const physics{Global.coupler_physics, true}; + flag_setting const everywhere{Global.coupler_physics_all, true}; 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")}; @@ -431,6 +437,7 @@ TEST_CASE("coupler physics: a gentle pull on a coupling with play doesn't hurt i TEST_CASE("coupler physics: automatic couplers still catch a wagon rolling into a standing one") { flag_setting const physics{Global.coupler_physics, true}; + flag_setting const everywhere{Global.coupler_physics_all, true}; flag_setting const crashdamage{Global.crash_damage, true}; auto automatic{definition::load("wagon").set("BuffCoupl", "CType", "Automatic").set("BuffCoupl", "AutomaticFlag", coupling::coupler | coupling::brakehose)}; auto moving{vehicle{automatic, 5.0, "moving"}}; @@ -495,3 +502,77 @@ TEST_CASE("couplers: a broken coupler is repaired alone, with the one of the veh CHECK(second->Couplers[end::rear].CouplingFlag == coupling::faux); CHECK(second->Attach(end::rear, end::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") { + flag_setting const physics{Global.coupler_physics, true}; + 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}; + + 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)); + // the state of the physical model untouched + CHECK(moving->Couplers[end::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); + } + 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)); + } +} + +TEST_CASE("coupler physics: a hit on a braked train runs through it as a wave, weaker with each coupling") { + flag_setting const physics{Global.coupler_physics, true}; + flag_setting const everywhere{Global.coupler_physics_all, true}; + flag_setting const crashdamage{Global.crash_damage, true}; + constexpr std::size_t wagons{8}; + test::consist train; + for (std::size_t i = 0; i < wagons; ++i) { + train.add(definition::load("wagon")); + } + // a wagon rolling at 15 km/h into the rear of the standing train, its brakes applied + train.add(definition::load("wagon"), 0, coupling::faux, 15.0, 0.3); + + std::vector peak(wagons, 0.0); + std::vector started(wagons, -1.0); + for (int step = 0; step < 300; ++step) { + train.step(); + for (std::size_t i = 0; i < wagons; ++i) { + peak[i] = std::max(peak[i], train[i].speed()); + if (started[i] < 0.0 && train[i].speed() > 0.5) { + started[i] = step * 0.01; + } + } + } + for (std::size_t i = 0; i < wagons; ++i) { + CAPTURE(i); + CAPTURE(peak[i]); + REQUIRE(started[i] >= 0.0); + CHECK_FALSE(TestFlag(train[i]->DamageFlag, dtrain_out)); + } + // the wagon hit first takes less than the hit, the one at the far end the least + CHECK(peak[wagons - 1] < 12.0); + CHECK(peak[0] < 0.25 * 15.0); + for (std::size_t i = 0; i + 1 < wagons; ++i) { + CAPTURE(i); + CHECK(peak[i] < peak[i + 1] + 0.3); + } + // the wave takes time to run through the train, the far end starts last + CHECK(started[0] - started[wagons - 1] > 0.5); + // and the brakes have stopped it all again + for (std::size_t i = 0; i < wagons; ++i) { + CHECK(std::abs(train[i].speed()) < 0.5); + } +} diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 828737ed0..5fbd4d4d0 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -821,7 +821,11 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& if (token == "couplerphysics") { - ParseOne(Parser, coupler_physics, 1); + // yes: the player's train and the trains it closes in on, all: every train + std::string scope; + ParseOne(Parser, scope, 1); + coupler_physics_all = scope == "all"; + coupler_physics = coupler_physics_all || scope == "yes" || scope == "true" || scope == "1"; return true; } @@ -1713,7 +1717,11 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "scenario.weather.temperature", AirTemperature ); export_as_text( Output, "ai.trainman", AITrainman ); export_as_text( Output, "controlcoupling.enginecheck", control_coupling_engine_check ); - export_as_text( Output, "couplerphysics", coupler_physics ); + std::string couplerphysics { coupler_physics ? "yes" : "no" }; + if( coupler_physics_all ) { + couplerphysics = "all"; + } + export_as_text( Output, "couplerphysics", couplerphysics ); export_as_text( Output, "scalespeculars", ScaleSpecularValues ); export_as_text( Output, "gfxrenderer", GfxRenderer ); export_as_text( Output, "shadows", RenderShadows ); diff --git a/utilities/Globals.h b/utilities/Globals.h index 783a0c769..dad8eb292 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -276,8 +276,11 @@ struct global_settings { bool render_cab = true; bool crash_damage = true; // the physical model of the couplings (McZapkie/couplerphysics.h): progressive buffers and draw gear with hysteresis, - // the play of the coupling, its yield and breaking; the legacy model of TMoverParameters::CouplerForce() without it + // the play of the coupling, its yield and breaking; the legacy model of TMoverParameters::CouplerForce() without it. + // it covers the player's train and the trains it closes in on (vehicle_table::update_coupler_physics()), or every + // train with coupler_physics_all bool coupler_physics = false; + bool coupler_physics_all = false; bool control_coupling_engine_check = true; // no multiple-unit control between electric and diesel traction vehicles (eu07/maszyna#520) bool gui_defaultwindows = true; bool gui_showtranscripts = true; diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 192d73f22..1de9f0781 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -22,6 +22,10 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/Camera.h" #include "vehicle/Train.h" #include "vehicle/Driver.h" + +#include +#include +#include #include "utilities/Globals.h" #include "utilities/Timer.h" #include "utilities/Logs.h" @@ -8876,6 +8880,112 @@ TDynamicObject::powertrain_sounds::render( TMoverParameters const &Vehicle, doub } +namespace { + +// a train on the same track this close to the end of the player's train is taken into the physical coupler model [m] +constexpr double coupler_physics_reach { 200.0 }; +// and let go of once none of its vehicles is this close to the player's train [m]; farther than the reach, so that +// the trains don't switch to and fro between the models +constexpr double coupler_physics_release { 500.0 }; + +// the vehicle and those coupled with it, one after the other +std::vector +coupled_train( TDynamicObject *Vehicle ) { + + std::vector train { Vehicle }; + for( int direction = end::front; direction <= end::rear; ++direction ) { + auto *vehicle { Vehicle }; + auto side { direction }; + // a train of more vehicles than are on the tracks would be one coupled into a ring + while( train.size() < 10000 ) { + auto const &mover { *vehicle->MoverParameters }; + auto *next { mover.Neighbours[ side ].vehicle }; + if( next == nullptr + || ( mover.Couplers[ side ].CouplingFlag & coupling::coupler ) == 0 + || std::ranges::find( train, next ) != train.end() ) { + break; + } + train.emplace_back( next ); + // onwards from the end of the next vehicle away from this one + side = 1 - mover.Neighbours[ side ].vehicle_end; + vehicle = next; + } + } + return train; +} + +// the trains on the same track the player's train is closing in on: those found at its free ends, near enough +std::vector +trains_closing_in( std::vector const &Train ) { + + std::vector trains; + for( auto *vehicle : Train ) { + for( int side = end::front; side <= end::rear; ++side ) { + auto const &mover { *vehicle->MoverParameters }; + auto const &neighbour { mover.Neighbours[ side ] }; + if( neighbour.vehicle == nullptr + || ( mover.Couplers[ side ].CouplingFlag & coupling::coupler ) != 0 + || neighbour.distance > coupler_physics_reach + || std::ranges::find( Train, neighbour.vehicle ) != Train.end() ) { + continue; + } + std::ranges::copy( coupled_train( neighbour.vehicle ), std::back_inserter( trains ) ); + } + } + return trains; +} + +// whether any vehicle of the train is within the distance of any vehicle of the other one +bool +trains_within( std::vector const &Train, std::vector const &Other, double const Distance ) { + + return std::ranges::any_of( Train, [&]( TDynamicObject const *Vehicle ) { + return std::ranges::any_of( Other, [&]( TDynamicObject const *Othervehicle ) { + return glm::distance( Vehicle->GetPosition(), Othervehicle->GetPosition() ) <= Distance; } ); } ); +} + +} // namespace + +// the physical coupler model covers the player's train and the trains it closes in on, on the same track; the others +// keep the legacy model, as the AI drives them in it +void +vehicle_table::update_coupler_physics() const { + + if( false == Global.coupler_physics + || true == Global.coupler_physics_all ) { + return; + } + auto *player { simulation::Train != nullptr ? simulation::Train->Dynamic() : nullptr }; + std::vector train; + if( player != nullptr ) { + train = coupled_train( player ); + } + std::unordered_set covered { train.begin(), train.end() }; + for( auto *vehicle : trains_closing_in( train ) ) { + covered.insert( vehicle ); + } + + std::unordered_set checked; + for( auto *vehicle : m_items ) { + if( covered.contains( vehicle ) ) { + vehicle->MoverParameters->CouplerPhysics = true; + continue; + } + if( false == vehicle->MoverParameters->CouplerPhysics + || checked.contains( vehicle ) ) { + continue; + } + // a train taken in before stays in the model until it's far from the player's one, all of it alike + auto const other { coupled_train( vehicle ) }; + checked.insert( other.begin(), other.end() ); + if( false == trains_within( other, train, coupler_physics_release ) ) { + for( auto *othervehicle : other ) { + othervehicle->MoverParameters->CouplerPhysics = false; + } + } + } +} + // legacy method, calculates changes in simulation state over specified time void vehicle_table::update( double Deltatime, int Iterationcount ) { @@ -8892,6 +9002,7 @@ vehicle_table::update( double Deltatime, int Iterationcount ) { vehicle->MoverParameters->ComputeConstans(); vehicle->update_neighbours(); } + update_coupler_physics(); if( Iterationcount > 1 ) { // ABu: ponizsze wykonujemy tylko jesli wiecej niz jedna iteracja for( int iteration = 0; iteration < Iterationcount - 1; ++iteration ) { diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index 7475a22d7..47202be25 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -941,6 +941,10 @@ private: // maintenance; removes from tracks consists with vehicles marked as disabled bool erase_disabled(); + // marks the vehicles the physical coupler model covers (couplerphysics yes in eu07.ini): the player's train and + // the trains it closes in on + void + update_coupler_physics() const; };