From 0996e87f7991490d70fba9e5b525e7c7404ce637 Mon Sep 17 00:00:00 2001
From: Mitchell Richters <mjr4077au@gmail.com>
Date: Sun, 12 Apr 2020 07:34:14 +1000
Subject: [PATCH] SW: Smooth out 180 degree turn landing and replace some
 fix16_min/max with fix16_clamp.

---
 source/sw/src/draw.cpp | 10 +++-------
 source/sw/src/game.cpp |  7 +++----
 2 files changed, 6 insertions(+), 11 deletions(-)

diff --git a/source/sw/src/draw.cpp b/source/sw/src/draw.cpp
index d2edbfcaf..7188eb063 100644
--- a/source/sw/src/draw.cpp
+++ b/source/sw/src/draw.cpp
@@ -1642,9 +1642,7 @@ void CameraView(PLAYERp pp, int *tx, int *ty, int *tz, short *tsectnum, fix16_t
                         zvect = 0;
 
                     // new horiz to player
-                    *tq16horiz = fix16_from_int(100 - (zvect/256));
-                    *tq16horiz = fix16_max(*tq16horiz, fix16_from_int(PLAYER_HORIZ_MIN));
-                    *tq16horiz = fix16_min(*tq16horiz, fix16_from_int(PLAYER_HORIZ_MAX));
+                    *tq16horiz = fix16_clamp(fix16_from_int(100 - (zvect/256)), fix16_from_int(PLAYER_HORIZ_MIN), fix16_from_int(PLAYER_HORIZ_MAX));
 
                     //DSPRINTF(ds,"xvect %d,yvect %d,zvect %d,tq16horiz %d",xvect,yvect,zvect,*tq16horiz);
                     MONO_PRINT(ds);
@@ -2104,10 +2102,8 @@ drawscreen(PLAYERp pp)
         tz += camerapp->bob_z;
 
         // recoil only when not in camera
-        //tq16horiz = tq16horiz + fix16_from_int(camerapp->recoil_horizoff);
-        tq16horiz = tq16horiz + fix16_from_int(pp->recoil_horizoff);
-        tq16horiz = fix16_max(tq16horiz, fix16_from_int(PLAYER_HORIZ_MIN));
-        tq16horiz = fix16_min(tq16horiz, fix16_from_int(PLAYER_HORIZ_MAX));
+        //tq16horiz = fix16_clamp(fix16_sadd(tq16horiz, fix16_from_int(camerapp->recoil_horizoff)), fix16_from_int(PLAYER_HORIZ_MIN), fix16_from_int(PLAYER_HORIZ_MAX));
+        tq16horiz = fix16_clamp(fix16_sadd(tq16horiz, fix16_from_int(pp->recoil_horizoff)), fix16_from_int(PLAYER_HORIZ_MIN), fix16_from_int(PLAYER_HORIZ_MAX));
     }
 
     if (r_usenewaspect)
diff --git a/source/sw/src/game.cpp b/source/sw/src/game.cpp
index 22ad0f0d9..97467bd80 100644
--- a/source/sw/src/game.cpp
+++ b/source/sw/src/game.cpp
@@ -3309,7 +3309,7 @@ void getinput(int const playerNum)
                         // the rest will follow
                         delta_q16ang = GetDeltaAngleQ16(pp->turn180_target, pp->q16ang);
 
-                        pp->q16ang = fix16_sadd(pp->q16ang, fix16_max(fix16_one,fix16_from_float(scaleAdjustmentToInterval(fix16_to_int(fix16_sdiv(fix16_abs(delta_q16ang), fix16_from_int(TURN_SHIFT))))))) & 0x7FFFFFF;
+                        pp->q16ang = fix16_sadd(pp->q16ang, fix16_max(fix16_one, fix16_from_float(scaleAdjustmentToInterval(fix16_to_int(fix16_sdiv(fix16_abs(delta_q16ang), fix16_from_int(TURN_SHIFT))))))) & 0x7FFFFFF;
 
                         SET(pp->Flags, PF_TURN_180);
                     }
@@ -3337,7 +3337,7 @@ void getinput(int const playerNum)
                 // get new delta to see how close we are
                 delta_q16ang = GetDeltaAngleQ16(pp->turn180_target, pp->q16ang);
 
-                if (fix16_abs(delta_q16ang) < fix16_from_int(3 * TURN_SHIFT))
+                if (fix16_abs(delta_q16ang) < (fix16_one << 1))
                 {
                     pp->q16ang = pp->turn180_target;
                     RESET(pp->Flags, PF_TURN_180);
@@ -3491,8 +3491,7 @@ void getinput(int const playerNum)
         }
 
         // bound the base
-        pp->q16horizbase = fix16_max(pp->q16horizbase, fix16_from_int(PLAYER_HORIZ_MIN));
-        pp->q16horizbase = fix16_min(pp->q16horizbase, fix16_from_int(PLAYER_HORIZ_MAX));
+        pp->q16horizbase = fix16_clamp(pp->q16horizbase, fix16_from_int(PLAYER_HORIZ_MIN), fix16_from_int(PLAYER_HORIZ_MAX));
 
         // bound adjust q16horizoff
         if (pp->q16horizbase + pp->q16horizoff < fix16_from_int(PLAYER_HORIZ_MIN))