From 47a0b14b43dbe6da4f112b3ad89683ab3a31355a Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Sat, 18 Jul 2020 00:59:10 +0200
Subject: [PATCH] - the main input function is also free of conflicts. That
 leaves 3 functions with roughly 4kb of code that cannot be redone without
 using EDuke code.

---
 source/games/duke/src/input.cpp     |  88 ++++++++++++++++++++--
 source/games/duke/src/zz_player.cpp | 112 ++--------------------------
 2 files changed, 88 insertions(+), 112 deletions(-)

diff --git a/source/games/duke/src/input.cpp b/source/games/duke/src/input.cpp
index 4106f154f..8db31b93b 100644
--- a/source/games/duke/src/input.cpp
+++ b/source/games/duke/src/input.cpp
@@ -42,8 +42,6 @@ BEGIN_DUKE_NS
 static int WeaponToSend;
 static ESyncBits BitsToSend;
 
-extern double elapsedInputTicks;
-
 //---------------------------------------------------------------------------
 //
 // handles UI side input not handled via CCMDs or CVARs.
@@ -709,7 +707,7 @@ enum
 //
 //---------------------------------------------------------------------------
 
-fix16_t GetDeltaQ16Angle(fix16_t ang1, fix16_t ang2)
+static fix16_t GetDeltaQ16Angle(fix16_t ang1, fix16_t ang2)
 {
 	// Look at the smaller angle if > 1024 (180 degrees)
 	if (fix16_abs(ang1 - ang2) > fix16_from_int(1024))
@@ -733,7 +731,7 @@ fix16_t GetDeltaQ16Angle(fix16_t ang1, fix16_t ang2)
 //
 //---------------------------------------------------------------------------
 
-void processInputBits(player_struct *p, ControlInfo &info)
+static void processInputBits(player_struct *p, ControlInfo &info)
 {
 	bool onVehicle = p->OnMotorcycle || p->OnBoat;
 	if (buttonMap.ButtonDown(gamefunc_Fire)) loc.bits |= SKB_FIRE;
@@ -801,7 +799,7 @@ void processInputBits(player_struct *p, ControlInfo &info)
 //
 //---------------------------------------------------------------------------
 
-void processMovement(player_struct *p, input_t &input, ControlInfo &info, double scaleFactor)
+static void processMovement(player_struct *p, input_t &input, ControlInfo &info, double scaleFactor)
 {
 	bool mouseaim = in_mousemode || buttonMap.ButtonDown(gamefunc_Mouse_Aiming);
 
@@ -906,7 +904,7 @@ void processMovement(player_struct *p, input_t &input, ControlInfo &info, double
 //
 //---------------------------------------------------------------------------
 
-int motoApplyTurn(player_struct* p, int turnl, int turnr, int bike_turn, bool goback, double factor)
+static int motoApplyTurn(player_struct* p, int turnl, int turnr, int bike_turn, bool goback, double factor)
 {
 	static int turnheldtime;
 	static int lastcontroltime;
@@ -1060,7 +1058,7 @@ static int boatApplyTurn(player_struct *p, int turnl, int turnr, int bike_turn,
 //
 //---------------------------------------------------------------------------
 
-void processVehicleInput(player_struct *p, ControlInfo& info, input_t& input, double scaleAdjust)
+static void processVehicleInput(player_struct *p, ControlInfo& info, input_t& input, double scaleAdjust)
 {
 	auto turnspeed = info.mousex + scaleAdjust * info.dyaw * (1. / 32); // originally this was 64, not 32. Why the change?
 	int turnl = buttonMap.ButtonDown(gamefunc_Turn_Left) || buttonMap.ButtonDown(gamefunc_Strafe_Left);
@@ -1112,6 +1110,82 @@ void processVehicleInput(player_struct *p, ControlInfo& info, input_t& input, do
 	input.q16avel = fix16_from_dbl(turnvel);
 }
 
+//---------------------------------------------------------------------------
+//
+// main input handler routine
+//
+//---------------------------------------------------------------------------
+void checkCrouchToggle(player_struct* p);
+void FinalizeInput(int playerNum, input_t& input, bool vehicle);
+
+void GetInput()
+{
+	static double lastCheck;
+	double elapsedInputTicks;
+	auto const p = &ps[myconnectindex];
+	updatePauseStatus();
+
+	auto now = I_msTimeF();
+	// do not let this become too large - it would create overflows resulting in undefined behavior. The very first tic must not use the timer difference at all because the timer has not been set yet.
+	// This really needs to have the timer fixed to be robust, doing it ad-hoc here is not really safe.
+	if (lastCheck > 0) elapsedInputTicks = min(now - lastCheck, 10.);
+	else elapsedInputTicks = 1;
+	lastCheck = now;
+
+	if (paused)
+	{
+		loc = {};
+		if (g_gameQuit) loc.bits |= SKB_GAMEQUIT;
+		return;
+	}
+
+	D_ProcessEvents();
+	if (numplayers == 1)
+	{
+		setlocalplayerinput(p);
+	}
+
+	double scaleAdjust = elapsedInputTicks * REALGAMETICSPERSEC / 1000.0;
+	ControlInfo info;
+	CONTROL_GetInput(&info);
+	input_t input{};
+
+	if (isRRRA() && (p->OnMotorcycle || p->OnBoat))
+	{
+		p->crouch_toggle = 0;
+		processInputBits(p, info);
+		processVehicleInput(p, info, input, scaleAdjust);
+		FinalizeInput(myconnectindex, input, true);
+
+		if (!synchronized_input)
+		{
+			p->q16horiz = clamp(p->q16horiz, F16(HORIZ_MIN), F16(HORIZ_MAX));
+			if (sprite[p->i].extra > 0)
+			{
+				apply_seasick(p, scaleAdjust);
+			}
+		}
+	}
+	else
+	{
+		processMovement(p, input, info, scaleAdjust);
+		checkCrouchToggle(p);
+		processInputBits(p, info);
+		FinalizeInput(myconnectindex, input, false);
+
+		if (!synchronized_input)
+		{
+			if (sprite[p->i].extra > 0)
+			{
+				applylook(myconnectindex, scaleAdjust);
+			}
+
+			// Do these in the same order as the old code.
+			calcviewpitch(p, scaleAdjust);
+			sethorizon(myconnectindex, loc.bits, scaleAdjust, true);
+		}
+	}
+}
 
 //---------------------------------------------------------------------------
 //
diff --git a/source/games/duke/src/zz_player.cpp b/source/games/duke/src/zz_player.cpp
index a0bcd50f3..7bdfdb2d1 100644
--- a/source/games/duke/src/zz_player.cpp
+++ b/source/games/duke/src/zz_player.cpp
@@ -27,12 +27,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 BEGIN_DUKE_NS
 
-fix16_t GetDeltaQ16Angle(fix16_t ang1, fix16_t ang2);
-void processInputBits(player_struct *p, ControlInfo& info);
-int motoApplyTurn(player_struct* p, int turnl, int turnr, int bike_turn, bool goback, double factor);
-void processVehicleInput(player_struct* p, ControlInfo& info, input_t& input, double scaleAdjust);
-void processMovement(player_struct* p, input_t& input, ControlInfo& info, double scaleFactor);
-
 
 int32_t PHEIGHT = PHEIGHT_DUKE;
 
@@ -71,23 +65,20 @@ enum inputlock_t
 
 static int P_CheckLockedMovement(int const playerNum)
 {
-    auto& thisPlayer = g_player[playerNum]; 
     auto const pPlayer = &ps[playerNum];
 
-    if (pPlayer->on_crane >= 0)
-        return IL_NOMOVE|IL_NOANGLE;
-
-    if (pPlayer->newowner != -1)
-        return IL_NOANGLE|IL_NOHORIZ;
-
-    if (pPlayer->curr_weapon > 11) return 0;
-
-    if (pPlayer->dead_flag || pPlayer->fist_incs || pPlayer->transporter_hold > 2 || pPlayer->hard_landing || pPlayer->access_incs > 0
+    if (sprite[pPlayer->i].extra <= 0 || (pPlayer->dead_flag && !ud.god) || pPlayer->fist_incs || pPlayer->transporter_hold > 2 || pPlayer->hard_landing || pPlayer->access_incs > 0
         || pPlayer->knee_incs > 0
         || (PWEAPON(playerNum, pPlayer->curr_weapon, WorksLike) == TRIPBOMB_WEAPON && pPlayer->kickback_pic > 1
             && pPlayer->kickback_pic < PWEAPON(playerNum, pPlayer->curr_weapon, FireDelay)))
         return IL_NOTHING;
 
+    if (pPlayer->on_crane >= 0)
+        return IL_NOMOVE | IL_NOANGLE;
+
+    if (pPlayer->newowner != -1)
+        return IL_NOANGLE | IL_NOHORIZ;
+
     if (pPlayer->return_to_center > 0)
         return IL_NOHORIZ;
 
@@ -175,93 +166,4 @@ void FinalizeInput(int playerNum, input_t &input, bool vehicle)
     }
 }
 
-double elapsedInputTicks = -1;
-
-void P_GetInput(int const playerNum)
-{
-    auto const pPlayer = &ps[playerNum];
-    ControlInfo info;
-    double scaleAdjust = elapsedInputTicks * REALGAMETICSPERSEC / 1000.0;
-
-    CONTROL_GetInput(&info);
-
-    input_t input {};
-
-    // here it goes
-    processMovement(pPlayer, input, info, scaleAdjust);
-    checkCrouchToggle(pPlayer);
-    processInputBits(pPlayer, info);
-    FinalizeInput(playerNum, input, false);
-
-    if (!synchronized_input)
-    {
-        // don't adjust rotscrnang and look_ang if dead.
-        if (sprite[pPlayer->i].extra > 0)
-        {
-            applylook(playerNum, scaleAdjust);
-        }
-
-        // Do these in the same order as the old code.
-        calcviewpitch(pPlayer, scaleAdjust);
-        sethorizon(playerNum, loc.bits, scaleAdjust, true);
-    }
-}
-
-
-void P_GetInputVehicle(int playerNum)
-{
-    auto const pPlayer = &ps[playerNum];
-    ControlInfo info;
-    double scaleAdjust = elapsedInputTicks * REALGAMETICSPERSEC / 1000.0;
-
-    CONTROL_GetInput(&info);
-
-    input_t input {};
-
-    pPlayer->crouch_toggle = 0;
-    processInputBits(pPlayer, info);
-    processVehicleInput(pPlayer, info, input, scaleAdjust);
-
-    FinalizeInput(playerNum, input, true);
-
-    // don't adjust rotscrnang and look_ang if dead.
-    if (sprite[pPlayer->i].extra > 0 && !synchronized_input)
-    {
-        apply_seasick(pPlayer, scaleAdjust);
-    }
-}
-
-
-void GetInput()
-{
-    static double lastCheck;
-
-    auto const p = &ps[myconnectindex];
-    updatePauseStatus();
-
-    auto now = I_msTimeF();
-    // do not let this become too large - it would create overflows resulting in undefined behavior. The very first tic must not use the timer difference at all because the timer has not been set yet.
-    // This really needs to have the timer fixed to be robust, doing it ad-hoc here is not really safe.
-    if (elapsedInputTicks >= 0) elapsedInputTicks = min(now - lastCheck, 10.);  
-    else elapsedInputTicks = 1;
-    lastCheck = now;
-
-    if (paused)
-    {
-        loc = {};
-        if (g_gameQuit) loc.bits |= SKB_GAMEQUIT;
-        return;
-    }
-
-    D_ProcessEvents();
-    if (numplayers == 1)
-    {
-        setlocalplayerinput(p);
-    }
-
-    if (isRRRA() && (p->OnMotorcycle || p->OnBoat))
-        P_GetInputVehicle(myconnectindex);
-    else
-        P_GetInput(myconnectindex);
-}
 END_DUKE_NS