From 12c5bb35e712fa2a778a6826d951568f03600a01 Mon Sep 17 00:00:00 2001 From: Hirek193 Date: Sun, 29 Nov 2026 08:36:30 +0100 Subject: [PATCH] refactor(vehicle): the helpers of the split files out of the global namespace The split of Driver.cpp, DynObj.cpp and Train.cpp took five helpers out of their anonymous namespaces. outside() is used in offtrack.cpp alone and goes back into its anonymous namespace. The four shared by the parts of a class go into a namespace of its module: vehicle_ai (brake_response_horizon, uphill_stopping_distance), vehicle_dynamic (seconds_since) and vehicle_cab (is_hold_coil_lever). The rename of mover to physics also hit comments where the word meant something else: "prime mover" and "move the master controller" are back. --- physics/components/fizsections.cpp | 2 +- physics/vehicle.h | 4 ++-- vehicle/Train.h | 2 +- vehicle/ai/controls.cpp | 2 +- vehicle/ai/force.cpp | 6 +++--- vehicle/ai/internal.h | 5 +++++ vehicle/ai/speed.cpp | 16 ++++++++-------- vehicle/cab/internal.h | 6 ++++++ vehicle/cab/powercommands.cpp | 8 ++++---- vehicle/cab/train.cpp | 2 +- vehicle/cab/update.cpp | 2 +- vehicle/dynamic/internal.h | 7 +++++-- vehicle/dynamic/offtrack.cpp | 10 +++++----- vehicle/dynamic/update.cpp | 2 +- vehicle/dynamic/vehicletable.cpp | 2 +- 15 files changed, 45 insertions(+), 31 deletions(-) diff --git a/physics/components/fizsections.cpp b/physics/components/fizsections.cpp index d32c1bda1..f75fb011f 100644 --- a/physics/components/fizsections.cpp +++ b/physics/components/fizsections.cpp @@ -1694,7 +1694,7 @@ void physics::Vehicle::LoadFIZ_PowerParamsDecode(TPowerParameters &Powerparamete } case TPowerSource::Generator: { - // prime physics for the generator + // prime mover for the generator auto &generatorparameters{Powerparameters.EngineGenerator}; if (auto const enginetype{LoadFIZ_EngineDecode(extract_value(Prefix + "GeneratorEngine", Line))}; enginetype == TEngineType::Main) diff --git a/physics/vehicle.h b/physics/vehicle.h index f10c8a038..d106a243c 100644 --- a/physics/vehicle.h +++ b/physics/vehicle.h @@ -212,8 +212,8 @@ enum class TPowerSource struct engine_generator { // ld inputs - /// Pointer to the prime-physics revolutions [rev/s]. - double *engine_revolutions; // revs per second of the prime physics + /// Pointer to the prime-mover revolutions [rev/s]. + double *engine_revolutions; // revs per second of the prime mover // config /// Minimum working rev rate [rev/s]. double revolutions_min; // min working revolutions rate, in revs per second diff --git a/vehicle/Train.h b/vehicle/Train.h index ce5ea2def..078a06064 100644 --- a/vehicle/Train.h +++ b/vehicle/Train.h @@ -248,7 +248,7 @@ class TTrain { return ggHornButton.SubModel != nullptr && ggHornLowButton.SubModel == nullptr && ggHornHighButton.SubModel == nullptr; } // locates nearest vehicle belonging to the consist TDynamicObject *find_nearest_consist_vehicle(bool freefly, glm::vec3 pos) const; - // physics master controller to specified position + // move the master controller to specified position void set_master_controller( double const Position ); // moves train brake lever to specified position, potentially emits switch sound if conditions are met void set_train_brake( double const Position ); diff --git a/vehicle/ai/controls.cpp b/vehicle/ai/controls.cpp index 0d5552e7c..37e7d314e 100644 --- a/vehicle/ai/controls.cpp +++ b/vehicle/ai/controls.cpp @@ -107,7 +107,7 @@ TController::increase_train_brake() { occupied->brakes.delays[ 3 ] ) // a train falling further behind needs no full fill time to be seen not to get enough || ( ( fShortfall > 0.02 ) - && ( fBrakeHeldTime > brake_response_horizon( occupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) ) { + && ( fBrakeHeldTime > vehicle_ai::brake_response_horizon( occupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) ) { // the table says the present position is enough, but the brake has had its full fill // time since the handle last moved and the train is still not slowing down as asked: // believe the train rather than the table diff --git a/vehicle/ai/force.cpp b/vehicle/ai/force.cpp index e97905230..69967d99e 100644 --- a/vehicle/ai/force.cpp +++ b/vehicle/ai/force.cpp @@ -421,7 +421,7 @@ TController::uphill_brake_committed( double const Velocity ) const { ( BrakeSystem == TBrakeSystem::Pneumatic ) && ( BrakeCtrlPosition > 0.0 ) && ( fAccGravity < EU07_AI_UPHILLGRAVITY ) - && ( Velocity / 3.6 / -fAccGravity < brake_response_horizon( occupied, BrakeSystem, -1.0, fBrake_a0[ 0 ] ) ) ); + && ( Velocity / 3.6 / -fAccGravity < vehicle_ai::brake_response_horizon( occupied, BrakeSystem, -1.0, fBrake_a0[ 0 ] ) ) ); } void TController::control_braking_force() { @@ -512,7 +512,7 @@ void TController::control_braking_force() { && ( BrakeCtrlPosition > 0.0 ) && ( fShortfall > 0.02 ) && ( iShortfallGrowth >= 2 ) - && ( fBrakeHeldTime > brake_response_horizon( occupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) { + && ( fBrakeHeldTime > vehicle_ai::brake_response_horizon( occupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) { // the train has been falling further behind the demand for a while at a handle left alone for longer than // the brake takes to respond: a small step now, while it still is small, rather than the deep one the // margin above would allow only once the train is far behind @@ -544,7 +544,7 @@ void TController::control_braking_force() { && ( ( uphill_stop_hold() && ( std::abs( velocity ) < 3.0 ) // a train that has slowed down short of where it is to stop and is to pull up further does not get it && ( VelDesired <= std::abs( velocity ) + 0.5 ) - && ( ActualProximityDist - uphill_stopping_distance( std::abs( velocity ), fAccGravity ) <= stopping_margin() ) ) + && ( ActualProximityDist - vehicle_ai::uphill_stopping_distance( std::abs( velocity ), fAccGravity ) <= stopping_margin() ) ) || ( ( BrakeSystem == TBrakeSystem::Pneumatic ) && ( fAccGravity < EU07_AI_UPHILLGRAVITY ) && ( velocity < -0.1 ) && ( AccDesired <= 0.0 ) ) ) ) { cue_action( driver_hint::independentbrakeapply ); } diff --git a/vehicle/ai/internal.h b/vehicle/ai/internal.h index f155cf57d..adda3a7ea 100644 --- a/vehicle/ai/internal.h +++ b/vehicle/ai/internal.h @@ -108,8 +108,13 @@ const double EasyReactionTime = 0.5; //[s] przebłyski świadomości dla zwykłe const double PrepareTime = 2.0; //[s] przebłyski świadomości przy odpalaniu +// what the split parts of TController share, kept out of the global namespace +namespace vehicle_ai { + double brake_response_horizon( physics::Vehicle const *Vehicle, TBrakeSystem const Brakesystem, double const Acceleration, double const Brakea0 ); double uphill_stopping_distance( double const Velocity, double const Gravity ); +} // namespace vehicle_ai + diff --git a/vehicle/ai/speed.cpp b/vehicle/ai/speed.cpp index 864f67341..55b8db7f0 100644 --- a/vehicle/ai/speed.cpp +++ b/vehicle/ai/speed.cpp @@ -170,7 +170,7 @@ TController::hold_train_coming_to_stand_uphill() { || ( ( BrakeSystem == TBrakeSystem::Pneumatic ) // not for a train that is short of where it is to stop and is to pull up further && ( AccDesired <= 0.0 ) - && ( vel / 3.6 / -fAccGravity <= 0.5 * brake_response_horizon( occupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) { + && ( vel / 3.6 / -fAccGravity <= 0.5 * vehicle_ai::brake_response_horizon( occupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) { // the gradient alone already slows the train down nearly this much; ask for enough on top of it that // the brake is applied for sure, not held back by the margin against small corrections AccDesired = std::min( AccDesired, fAccGravity - std::max( 0.15, fBrake_a0[ 0 ] ) - brake_increase_margin() ); @@ -441,7 +441,7 @@ TController::adjust_desired_speed_for_limits() { // its pneumatic brake takes to apply (while speeding up) or to release (while slowing down), which in G // is tens of seconds; never shorter than the look-ahead the AI has always used double -brake_response_horizon( physics::Vehicle const *Vehicle, TBrakeSystem const Brakesystem, double const Acceleration, double const Brakea0 ) { +vehicle_ai::brake_response_horizon( physics::Vehicle const *Vehicle, TBrakeSystem const Brakesystem, double const Acceleration, double const Brakea0 ) { auto horizon { ( 1.0 - Brakea0 ) * 30.0 / 3.6 }; if( Brakesystem == TBrakeSystem::Pneumatic ) { @@ -455,7 +455,7 @@ brake_response_horizon( physics::Vehicle const *Vehicle, TBrakeSystem const Brak // distance [m] in which the gradient alone, with no brake and no power, brings a train rolling uphill at Velocity [km/h] to a stand double -uphill_stopping_distance( double const Velocity, double const Gravity ) { +vehicle_ai::uphill_stopping_distance( double const Velocity, double const Gravity ) { return ( Velocity / 3.6 ) * ( Velocity / 3.6 ) / ( 2.0 * std::max( 0.01, -Gravity ) ); } @@ -509,8 +509,8 @@ TController::adjust_desired_speed_for_braking_curve( double const Vel ) { ( occupied->brakes.localKind == TLocalBrake::PneumaticBrake || occupied->brakes.localKind == TLocalBrake::HydraulicBrake ) ? occupied->brakes.independentPosition : 0.0 ), - brake_response_horizon( occupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ), - brake_response_horizon( occupied, BrakeSystem, 1.0, fillbrakea0 ) }; + vehicle_ai::brake_response_horizon( occupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ), + vehicle_ai::brake_response_horizon( occupied, BrakeSystem, 1.0, fillbrakea0 ) }; auto const deceleration { braking_curve::demand( approach, BrakeSystem == TBrakeSystem::Pneumatic ? &braketable : nullptr ) }; AccDesired = std::min( AccDesired, deceleration ); // najpierw hamuje mocniej, potem zluzuje if( Vel <= VelNext ) { @@ -523,7 +523,7 @@ TController::adjust_desired_speed_for_braking_curve( double const Vel ) { else if( ( fAccGravity < -0.025 ) && ( OrderCurrentGet().HasAny(DriverOrder::obeyTrain) ) && ( false == uphill_brake_committed( Vel ) ) - && ( ActualProximityDist - uphill_stopping_distance( Vel, fAccGravity ) > brakingpointoffset ) ) { + && ( ActualProximityDist - vehicle_ai::uphill_stopping_distance( Vel, fAccGravity ) > brakingpointoffset ) ) { // uphill the gradient alone slows the train down more than it is asked to; // keep it rolling at the present speed until that changes AccDesired = std::min( 0.0, AccPreferred ); @@ -569,7 +569,7 @@ TController::adjust_desired_speed_for_final_approach( double const Vel ) { if( ( fAccGravity < -0.025 ) && ( AccDesired >= fAccGravity - 0.001 ) // no brake needed at all && ( false == uphill_brake_committed( Vel ) ) - && ( ActualProximityDist - uphill_stopping_distance( Vel, fAccGravity ) > stopping_margin() ) ) { + && ( ActualProximityDist - vehicle_ai::uphill_stopping_distance( Vel, fAccGravity ) > stopping_margin() ) ) { // uphill the gradient alone would bring the train to a stand short of the // middle of the stopping window; keep it rolling at the present speed until that changes, // but no faster than the gradient alone can take off by the middle of the window @@ -709,7 +709,7 @@ TController::adjust_desired_speed_for_current_speed() { // look ahead for as long as the brake takes to respond, since that is how long the // present acceleration will last whatever is done with the handle now; a pneumatic // brake applies and releases on its own time scale, which in G is tens of seconds - auto const horizon { brake_response_horizon( occupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ) }; + auto const horizon { vehicle_ai::brake_response_horizon( occupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ) }; auto const speedestimate = vel + 3.6 * horizon * AbsAccS; // already below a lower limit ahead, the train has no business speeding up towards it auto holdlimit { VelDesired }; diff --git a/vehicle/cab/internal.h b/vehicle/cab/internal.h index 770c556bc..cc4bc2f2c 100644 --- a/vehicle/cab/internal.h +++ b/vehicle/cab/internal.h @@ -57,5 +57,11 @@ extern user_command command; auto const EU07_CONTROLLER_BASERETURNDELAY{0.5f}; +// what the split parts of TTrain share, kept out of the global namespace +namespace vehicle_cab +{ + bool is_hold_coil_lever(TGauge const &Button); +} // namespace vehicle_cab + diff --git a/vehicle/cab/powercommands.cpp b/vehicle/cab/powercommands.cpp index 6d15e634d..f9d17ecf7 100644 --- a/vehicle/cab/powercommands.cpp +++ b/vehicle/cab/powercommands.cpp @@ -48,7 +48,7 @@ http://mozilla.org/MPL/2.0/. // whether the cab's main_off_bt is the bistable lever of the line breaker's hold coil (eu07/maszyna#364) -bool is_hold_coil_lever(TGauge const &Button) +bool vehicle_cab::is_hold_coil_lever(TGauge const &Button) { return Button.SubModel != nullptr && hold_coil_lever::is_bistable(Button.type_declared(), Button.type() == TGaugeType::toggle); } @@ -66,7 +66,7 @@ void TTrain::OnCommand_linebreakertoggle(TTrain *Train, command_data const &Comm { case linebreaker::toggle::close: // ...to close the circuit - if (is_hold_coil_lever(Train->ggMainOffButton) && hold_coil_lever::cuts_off(Train->ggMainOffButton.GetDesiredValue())) + if (vehicle_cab::is_hold_coil_lever(Train->ggMainOffButton) && hold_coil_lever::cuts_off(Train->ggMainOffButton.GetDesiredValue())) { // the one key works both controls: the hold coil lever goes back on first, or the line breaker couldn't close Train->ggMainOffButton.UpdateValue(0.0, Train->dsbSwitch); @@ -112,7 +112,7 @@ void TTrain::OnCommand_linebreakeropen(TTrain *Train, command_data const &Comman if (Command.action == GLFW_PRESS) { // visual feedback - if (is_hold_coil_lever(Train->ggMainOffButton)) + if (vehicle_cab::is_hold_coil_lever(Train->ggMainOffButton)) { // the bistable lever goes over to its other position and stays there auto const position{hold_coil_lever::operated(Train->ggMainOffButton.GetDesiredValue())}; @@ -173,7 +173,7 @@ void TTrain::OnCommand_linebreakeropen(TTrain *Train, command_data const &Comman { Train->ggMainOnButton.UpdateValue(0.0, Train->dsbSwitch); } - if (Train->ggMainOffButton.SubModel != nullptr && false == is_hold_coil_lever(Train->ggMainOffButton)) + if (Train->ggMainOffButton.SubModel != nullptr && false == vehicle_cab::is_hold_coil_lever(Train->ggMainOffButton)) { // (the bistable hold coil lever stays where it was put) Train->ggMainOffButton.UpdateValue(0.0, Train->dsbSwitch); diff --git a/vehicle/cab/train.cpp b/vehicle/cab/train.cpp index 0c6ae06b9..99987edb7 100644 --- a/vehicle/cab/train.cpp +++ b/vehicle/cab/train.cpp @@ -443,7 +443,7 @@ bool TTrain::is_eztoer() const controlled->DirActive != 0; // od yB } -// physics master controller to specified position +// move the master controller to specified position void TTrain::set_master_controller(double const Position) { diff --git a/vehicle/cab/update.cpp b/vehicle/cab/update.cpp index cf33a899a..8a56054b5 100644 --- a/vehicle/cab/update.cpp +++ b/vehicle/cab/update.cpp @@ -151,7 +151,7 @@ void TTrain::updateLineBreakerState(double const Deltatime) // because the state of the line breaker isn't changed to match, we need to do it here manually m_linebreakerstate = linebreaker::state::open; } - if (is_hold_coil_lever(ggMainOffButton) && hold_coil_lever::cuts_off(ggMainOffButton.GetDesiredValue())) + if (vehicle_cab::is_hold_coil_lever(ggMainOffButton) && hold_coil_lever::cuts_off(ggMainOffButton.GetDesiredValue())) { if (true == controlled->lineBreaker.closed) { diff --git a/vehicle/dynamic/internal.h b/vehicle/dynamic/internal.h index 27b6e80ae..99eb1e40c 100644 --- a/vehicle/dynamic/internal.h +++ b/vehicle/dynamic/internal.h @@ -67,8 +67,6 @@ http://mozilla.org/MPL/2.0/. TSubModel * GetSubmodelFromName( TModel3d * const Model, std::string const Name ); -bool outside( glm::dvec3 const &Centre, double const Radius, glm::dvec2 const &Min, glm::dvec2 const &Max ); - // the time spent off the track in a frame which makes it worth a warning [s] constexpr double off_track_slow { 0.02 }; @@ -76,5 +74,10 @@ constexpr double off_track_slow { 0.02 }; using off_track_clock = std::chrono::steady_clock; +// what the split parts of TDynamicObject share, kept out of the global namespace +namespace vehicle_dynamic { + double seconds_since( off_track_clock::time_point const Start ); +} // namespace vehicle_dynamic + diff --git a/vehicle/dynamic/offtrack.cpp b/vehicle/dynamic/offtrack.cpp index 40bb90392..e12ccb0c2 100644 --- a/vehicle/dynamic/offtrack.cpp +++ b/vehicle/dynamic/offtrack.cpp @@ -63,11 +63,11 @@ namespace using ground_triangles = std::vector; -} // namespace - bool outside( glm::dvec3 const &Centre, double const Radius, glm::dvec2 const &Min, glm::dvec2 const &Max ) { return Centre.x + Radius < Min.x || Centre.x - Radius > Max.x || Centre.z + Radius < Min.y || Centre.z - Radius > Max.y; } + +} // namespace namespace { @@ -168,7 +168,7 @@ double compression_of( physics::Vehicle const &Vehicle, VehicleEnd const End ) { } // namespace -double seconds_since( off_track_clock::time_point const Start ) { +double vehicle_dynamic::seconds_since( off_track_clock::time_point const Start ) { return std::chrono::duration( off_track_clock::now() - Start ).count(); } namespace @@ -297,7 +297,7 @@ void TDynamicObject::gather_off_track_terrain() { } } m_offtracksite.terrain( m_offtrackterrain.get() ); - auto const took { seconds_since( started ) }; + auto const took { vehicle_dynamic::seconds_since( started ) }; if( took > off_track_slow ) { ErrorLog( std::format( "derailment: warning: gathering the terrain of {:.0f} m around {} took {:.0f} ms ({} triangles)", reach, physics->Name, took * 1000.0, triangles.size() ) ); } @@ -420,7 +420,7 @@ void TDynamicObject::move_off_track( double const Distance ) { pull_neighbours( Distance ); // what it cost: a frame of the vehicle off the track should take well under a millisecond - auto const took { seconds_since( started ) }; + auto const took { vehicle_dynamic::seconds_since( started ) }; m_offtrackslowdue -= time; if( took > off_track_slow && m_offtrackslowdue <= 0.0 ) { m_offtrackslowdue = 1.0; diff --git a/vehicle/dynamic/update.cpp b/vehicle/dynamic/update.cpp index 4c70f7979..b59ab45aa 100644 --- a/vehicle/dynamic/update.cpp +++ b/vehicle/dynamic/update.cpp @@ -79,7 +79,7 @@ bool TDynamicObject::Update(double dt, double dt1) if( vehicle == nullptr ) { return; } - auto const took { seconds_since( started ) }; + auto const took { vehicle_dynamic::seconds_since( started ) }; vehicle->m_offtrackupdatedue -= frame; if( took > off_track_slow && vehicle->m_offtrackupdatedue <= 0.0 ) { vehicle->m_offtrackupdatedue = 1.0; diff --git a/vehicle/dynamic/vehicletable.cpp b/vehicle/dynamic/vehicletable.cpp index 447579c9d..1b2ffe229 100644 --- a/vehicle/dynamic/vehicletable.cpp +++ b/vehicle/dynamic/vehicletable.cpp @@ -530,7 +530,7 @@ vehicle_table::update_off_track_contacts( double const Deltatime ) { if( false == resting ) { m_overlaplogged = false; } - auto const took { seconds_since( started ) }; + auto const took { vehicle_dynamic::seconds_since( started ) }; if( took > off_track_slow ) { ErrorLog( std::format( "derailment: warning: the contacts of {} vehicles off the track took {:.0f} ms", offtrack.size(), took * 1000.0 ) ); }