From 18bc7e06bfe58a4efff593dc3245a27289605ed3 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Mon, 2 Nov 2026 16:43:16 +0100 Subject: [PATCH] AI braking: move the uphill power-off check into its own function The condition added for holding the speed uphill near a stop pushed the cognitive complexity of control_tractive_force() to 26, over the 25 SonarCloud allows. It now lives in is_too_fast_uphill(); the behaviour is unchanged. --- vehicle/Driver.cpp | 13 ++++++++----- vehicle/Driver.h | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 6d6c1532e..2311ffacf 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -8110,11 +8110,7 @@ void TController::control_tractive_force() { cue_action( driver_hint::mastercontrollersetzerospeed ); } else if( velocity > VelDesired + SpeedCtrlMargin - || ( fAccGravity < -0.01 ? - AccDesired < 0.0 - // holding the speed uphill on the way to a stop asks for no speeding up - || ( VelNext == 0.0 && ActualProximityDist <= fMaxProximityDist && AbsAccS > AccDesired + 0.05 ) : - AbsAccS > AccDesired + 10.05 ) + || ( fAccGravity < -0.01 ? is_too_fast_uphill() : AbsAccS > AccDesired + 10.05 ) || IsAnyCouplerStretched ) { // jak za bardzo przyspiesza albo prędkość przekroczona // dodany wyjatek na "pelna w przod" @@ -8126,6 +8122,13 @@ void TController::control_tractive_force() { SpeedSet(); // ciągla regulacja prędkości } +bool TController::is_too_fast_uphill() const { + + if( AccDesired < 0.0 ) { return true; } + // holding the speed uphill on the way to a stop asks for no speeding up + return ( VelNext == 0.0 && ActualProximityDist <= fMaxProximityDist && AbsAccS > AccDesired + 0.05 ); +} + void TController::increase_tractive_force() { if( fActionTime < 0.0 ) { return; } // gdy jest nakaz poczekać z jazdą, to nie ruszać diff --git a/vehicle/Driver.h b/vehicle/Driver.h index af97b4a1e..11a01517f 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -358,6 +358,7 @@ private: void control_motor_connectors(); void control_tractive_force(); void increase_tractive_force(); + bool is_too_fast_uphill() const; void control_braking_force(); // how far short of a stop the AI aims: in train running the middle of the 8-15 m window before a signal, // otherwise (a W4 stop point, shunting, coupling) half the minimum proximity distance, as before