16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-21 01:19:18 +02:00

fix: restore min>max tolerance lost in clamp->std::clamp migration

The dumb3d->glm / utilities-simplification refactor replaced the old custom
clamp() (which tolerated Lo>Hi, returning the upper bound) with std::clamp,
where inverted bounds are undefined behaviour. Only a few sites were patched
afterwards, by hand, with std::minmax plumbing (incl. a stray `static` inside
a per-axle loop). Others silently produced wrong results in the AI braking /
acceleration path.

Add a safe_clamp() helper (normalizes inverted bounds) and use it ONLY where
Lo<=Hi cannot be proven at compile time (config min/max pairs, sign-dependent
expressions, container underflow). Sites with constant bounds or [0, x>=0]
keep std::clamp. Remove the manual std::minmax workarounds and rewrite the
damaged-track jolt code value-first.
This commit is contained in:
maj00r
2026-07-02 00:16:03 +02:00
parent 80c3feac03
commit 03bfbb1345
4 changed files with 33 additions and 23 deletions

View File

@@ -3701,7 +3701,7 @@ bool TController::IncSpeedEIM() {
// TBD, TODO: set position based on desired acceleration?
OK = mvControlling->MainCtrlPos < mvControlling->MainCtrlPosNo;
if( OK ) {
mvControlling->MainCtrlPos = std::clamp( mvControlling->MainCtrlPos + 1, 6, mvControlling->MainCtrlPosNo );
mvControlling->MainCtrlPos = safe_clamp( mvControlling->MainCtrlPos + 1, 6, mvControlling->MainCtrlPosNo );
}
*/
break;
@@ -7669,7 +7669,7 @@ TController::adjust_desired_speed_for_current_speed() {
if( iVehicles - ControlledEnginesCount > 0 ) {
MaxAcc *= std::clamp( vel * 0.025, 0.2, 1.0 );
}
AccDesired = std::min(AccDesired, std::clamp(MaxAcc, HeavyCargoTrainAcceleration, AccPreferred));
AccDesired = std::min(AccDesired, safe_clamp(MaxAcc, HeavyCargoTrainAcceleration, AccPreferred));
// TBD: expand this behaviour to all trains with car(s) exceeding certain weight?
/*
if( ( IsPassengerTrain ) && ( iVehicles - ControlledEnginesCount > 0 ) ) {
@@ -7770,8 +7770,7 @@ TController::adjust_desired_speed_for_braking_test() {
break;
}
case 3: {
auto [minV, maxV] = std::minmax(fAccThreshold * 1.01f, fAccThreshold * 1.21f);
AccDesired = std::clamp(-AbsAccS, minV, maxV);
AccDesired = safe_clamp( -AbsAccS, fAccThreshold * 1.01, fAccThreshold * 1.21 );
VelDesired = DBT_VelocityBrake;
if( vel <= DBT_VelocityRelease ) {
DynamicBrakeTest = 4;