From 44166ae9e6a8280d01a4fdf80b1dd0926aceb705 Mon Sep 17 00:00:00 2001
From: Mitchell Richters <mjr4077au@gmail.com>
Date: Tue, 15 Nov 2022 17:56:20 +1100
Subject: [PATCH] - Duke: Wrap calls that add to
 `player_struct::PlayerNowPosition.Z`.

---
 source/games/duke/src/actors.cpp   | 16 ++++++++--------
 source/games/duke/src/player.cpp   |  4 ++--
 source/games/duke/src/player_d.cpp | 16 ++++++++--------
 source/games/duke/src/player_r.cpp | 12 ++++++------
 source/games/duke/src/sectors.cpp  |  2 +-
 source/games/duke/src/types.h      |  4 ++++
 6 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/source/games/duke/src/actors.cpp b/source/games/duke/src/actors.cpp
index fabe74ca4..ae88ea9c6 100644
--- a/source/games/duke/src/actors.cpp
+++ b/source/games/duke/src/actors.cpp
@@ -1862,7 +1862,7 @@ void handle_se00(DDukeActor* actor)
 			{
 				ps[p].angle.addadjustment(ang_amount * direction);
 
-				ps[p].PlayerNowPosition.Z += zchange;
+				ps[p].posZadd(zchange);
 
 				auto result = rotatepoint(Owner->spr.pos, ps[p].posXY(), ang_amount * direction);
 
@@ -2997,7 +2997,7 @@ void handle_se17(DDukeActor* actor)
 		if (act1->spr.statnum == STAT_PLAYER && act1->GetOwner())
 		{
 			int p = act1->spr.yint;
-			ps[p].PlayerNowPosition.Z += q;
+			ps[p].posZadd(q);
 			ps[p].truefz += q;
 			ps[p].truecz += q;
 		}
@@ -3048,7 +3048,7 @@ void handle_se17(DDukeActor* actor)
 
 				ps[p].PlayerOldPosition -= ps[p].PlayerNowPosition;
 				ps[p].posXY() += act2->spr.pos.XY() - actor->spr.pos.XY();
-				ps[p].PlayerNowPosition.Z += act2->sector()->floorz - sc->floorz;
+				ps[p].posZadd(act2->sector()->floorz - sc->floorz);
 				ps[p].PlayerOldPosition += ps[p].PlayerNowPosition;
 
 				if (q > 0) ps[p].backupz();
@@ -3121,7 +3121,7 @@ void handle_se18(DDukeActor *actor, bool morecheck)
 					{
 						if (a2->isPlayer() && a2->GetOwner())
 						{
-							if (ps[a2->PlayerIndex()].on_ground == 1) ps[a2->PlayerIndex()].PlayerNowPosition.Z += extra;
+							if (ps[a2->PlayerIndex()].on_ground == 1) ps[a2->PlayerIndex()].posZadd(extra);
 						}
 						if (a2->vel.Z == 0 && a2->spr.statnum != STAT_EFFECTOR && a2->spr.statnum != STAT_PROJECTILE)
 						{
@@ -3459,7 +3459,7 @@ void handle_se26(DDukeActor* actor)
 		{
 			ps[p].fric.X += vect.X;
 			ps[p].fric.Y += vect.Y;
-			ps[p].PlayerNowPosition.Z += zvel;
+			ps[p].posZadd(zvel);
 		}
 
 	movesector(actor, actor->temp_data[1], nullAngle);
@@ -3869,7 +3869,7 @@ void handle_se31(DDukeActor* actor, bool choosedir)
 					{
 						if (a2->isPlayer() && a2->GetOwner())
 							if (ps[a2->PlayerIndex()].on_ground == 1)
-								ps[a2->PlayerIndex()].PlayerNowPosition.Z +=l;
+								ps[a2->PlayerIndex()].posZadd(l);
 						if (a2->vel.Z == 0 && a2->spr.statnum != STAT_EFFECTOR && (!choosedir || a2->spr.statnum != STAT_PROJECTILE))
 						{
 							a2->spr.pos.Z += l;
@@ -3898,7 +3898,7 @@ void handle_se31(DDukeActor* actor, bool choosedir)
 					{
 						if (a2->isPlayer() && a2->GetOwner())
 							if (ps[a2->PlayerIndex()].on_ground == 1)
-								ps[a2->PlayerIndex()].PlayerNowPosition.Z += l;
+								ps[a2->PlayerIndex()].posZadd(l);
 						if (a2->vel.Z == 0 && a2->spr.statnum != STAT_EFFECTOR && (!choosedir || a2->spr.statnum != STAT_PROJECTILE))
 						{
 							a2->spr.pos.Z += l;
@@ -3929,7 +3929,7 @@ void handle_se31(DDukeActor* actor, bool choosedir)
 				{
 					if (a2->isPlayer() && a2->GetOwner())
 						if (ps[a2->PlayerIndex()].on_ground == 1)
-							ps[a2->PlayerIndex()].PlayerNowPosition.Z += l;
+							ps[a2->PlayerIndex()].posZadd(l);
 					if (a2->vel.Z == 0 && a2->spr.statnum != STAT_EFFECTOR && (!choosedir || a2->spr.statnum != STAT_PROJECTILE))
 					{
 						a2->spr.pos.Z += l;
diff --git a/source/games/duke/src/player.cpp b/source/games/duke/src/player.cpp
index 90a554429..5d53ce36b 100644
--- a/source/games/duke/src/player.cpp
+++ b/source/games/duke/src/player.cpp
@@ -609,7 +609,7 @@ void playerisdead(int snum, int psectlotag, double floorz, double ceilingz)
 		if (p->on_warping_sector == 0)
 		{
 			if (abs(p->PlayerNowPosition.Z - floorz) > (gs.playerheight * 0.5))
-				p->PlayerNowPosition.Z += 348/ 256.;
+				p->posZadd(348/ 256.);
 		}
 		else
 		{
@@ -706,7 +706,7 @@ void playerCrouch(int snum)
 	OnEvent(EVENT_CROUCH, snum, p->GetActor(), -1);
 	if (GetGameVarID(g_iReturnVarID, p->GetActor(), snum).value() == 0)
 	{
-		p->PlayerNowPosition.Z += 8 + 3;
+		p->posZadd(8 + 3);
 		p->crack_time = CRACK_TIME;
 	}
 }
diff --git a/source/games/duke/src/player_d.cpp b/source/games/duke/src/player_d.cpp
index 8a265cce7..634149974 100644
--- a/source/games/duke/src/player_d.cpp
+++ b/source/games/duke/src/player_d.cpp
@@ -1717,7 +1717,7 @@ static void operateJetpack(int snum, ESyncBits actions, int psectlotag, double f
 		OnEvent(EVENT_SOARDOWN, snum, p->GetActor(), -1);
 		if (GetGameVarID(g_iReturnVarID, p->GetActor(), snum).value() == 0)
 		{
-			p->PlayerNowPosition.Z += dist;
+			p->posZadd(dist);
 			p->crack_time = CRACK_TIME;
 		}
 	}
@@ -1730,7 +1730,7 @@ static void operateJetpack(int snum, ESyncBits actions, int psectlotag, double f
 		p->scuba_on = 0;
 
 	if (p->PlayerNowPosition.Z > floorz - k)
-		p->PlayerNowPosition.Z += ((floorz - k) - p->PlayerNowPosition.Z) * 0.5;
+		p->posZadd(((floorz - k) - p->PlayerNowPosition.Z) * 0.5);
 	if (p->PlayerNowPosition.Z < pact->ceilingz + 18)
 		p->posZset(pact->ceilingz + 18);
 
@@ -1850,13 +1850,13 @@ static void movement(int snum, ESyncBits actions, sectortype* psect, double floo
 
 			double k = (floorz - i - p->PlayerNowPosition.Z) * 0.5;
 			if (abs(k) < 1) k = 0;
-			p->PlayerNowPosition.Z += k;
+			p->posZadd(k);
 			p->vel.Z -= 3;
 			if (p->vel.Z < 0) p->vel.Z = 0;
 		}
 		else if (p->jumping_counter == 0)
 		{
-			p->PlayerNowPosition.Z += ((floorz - i * 0.5) - p->PlayerNowPosition.Z) * 0.5; //Smooth on the water
+			p->posZadd(((floorz - i * 0.5) - p->PlayerNowPosition.Z) * 0.5); //Smooth on the water
 			if (p->on_warping_sector == 0 && p->PlayerNowPosition.Z > floorz - 16)
 			{
 				p->posZset(floorz - 16);
@@ -1910,7 +1910,7 @@ static void movement(int snum, ESyncBits actions, sectortype* psect, double floo
 		}
 	}
 
-	p->PlayerNowPosition.Z += p->vel.Z ;
+	p->posZadd(p->vel.Z );
 
 	if (p->PlayerNowPosition.Z < ceilingz + 4)
 	{
@@ -1977,10 +1977,10 @@ static void underwater(int snum, ESyncBits actions, double floorz, double ceilin
 	if (p->vel.Z > 8)
 		p->vel.Z *= 0.5;
 
-	p->PlayerNowPosition.Z += p->vel.Z ;
+	p->posZadd(p->vel.Z );
 
 	if (p->PlayerNowPosition.Z > floorz - 15)
-		p->PlayerNowPosition.Z += (((floorz - 15) - p->PlayerNowPosition.Z) * 0.5);
+		p->posZadd((((floorz - 15) - p->PlayerNowPosition.Z) * 0.5));
 
 	if (p->PlayerNowPosition.Z < ceilingz + 4)
 	{
@@ -3041,7 +3041,7 @@ HORIZONLY:
 		clipmove(p->PlayerNowPosition, &p->cursector, p->vel, 10.25, 4., iif, CLIPMASK0, clip);
 
 	if (p->jetpack_on == 0 && psectlotag != 2 && psectlotag != 1 && shrunk)
-		p->PlayerNowPosition.Z += 32;
+		p->posZadd(32);
 
 	if (clip.type != kHitNone)
 		checkplayerhurt_d(p, clip);
diff --git a/source/games/duke/src/player_r.cpp b/source/games/duke/src/player_r.cpp
index cc440bc3f..c2699772f 100644
--- a/source/games/duke/src/player_r.cpp
+++ b/source/games/duke/src/player_r.cpp
@@ -2177,13 +2177,13 @@ static void movement(int snum, ESyncBits actions, sectortype* psect, double floo
 
 			double k = (floorz - i - p->PlayerNowPosition.Z) * 0.5;
 			if (abs(k) < 1) k = 0;
-			p->PlayerNowPosition.Z += k;
+			p->posZadd(k);
 			p->vel.Z -= 3;
 			if (p->vel.Z < 0) p->vel.Z = 0;
 		}
 		else if (p->jumping_counter == 0)
 		{
-			p->PlayerNowPosition.Z += ((floorz - i * 0.5) - p->PlayerNowPosition.Z) * 0.5; //Smooth on the water
+			p->posZadd(((floorz - i * 0.5) - p->PlayerNowPosition.Z) * 0.5); //Smooth on the water
 			if (p->on_warping_sector == 0 && p->PlayerNowPosition.Z > floorz - 16)
 			{
 				p->posZset(floorz - 16);
@@ -2233,7 +2233,7 @@ static void movement(int snum, ESyncBits actions, sectortype* psect, double floo
 		}
 	}
 
-	p->PlayerNowPosition.Z += p->vel.Z ;
+	p->posZadd(p->vel.Z );
 
 	if (p->PlayerNowPosition.Z < ceilingz + 4)
 	{
@@ -2296,10 +2296,10 @@ static void underwater(int snum, ESyncBits actions, double floorz, double ceilin
 	if (p->vel.Z > 8)
 		p->vel.Z *= 0.5;
 
-	p->PlayerNowPosition.Z += p->vel.Z ;
+	p->posZadd(p->vel.Z );
 
 	if (p->PlayerNowPosition.Z > floorz - 15)
-		p->PlayerNowPosition.Z += (((floorz - 15) - p->PlayerNowPosition.Z) * 0.5);
+		p->posZadd((((floorz - 15) - p->PlayerNowPosition.Z) * 0.5));
 
 	if (p->PlayerNowPosition.Z < ceilingz + 4)
 	{
@@ -3745,7 +3745,7 @@ HORIZONLY:
 		clipmove(p->PlayerNowPosition, &p->cursector, p->vel, 10.25, 4., iif, CLIPMASK0, clip);
 
 	if (p->jetpack_on == 0 && psectlotag != 2 && psectlotag != 1 && shrunk)
-		p->PlayerNowPosition.Z += 32;
+		p->posZadd(32);
 
 	if (clip.type != kHitNone)
 		checkplayerhurt_r(p, clip);
diff --git a/source/games/duke/src/sectors.cpp b/source/games/duke/src/sectors.cpp
index d59c29839..18bc7d02d 100644
--- a/source/games/duke/src/sectors.cpp
+++ b/source/games/duke/src/sectors.cpp
@@ -383,7 +383,7 @@ void doanimations(void)
 					if ((dasectp->floorz - ps[p].PlayerNowPosition.Z) < 64)
 						if (ps[p].GetActor()->GetOwner() != nullptr)
 						{
-							ps[p].PlayerNowPosition.Z += v;
+							ps[p].posZadd(v);
 							ps[p].vel.Z = 0;
 						}
 
diff --git a/source/games/duke/src/types.h b/source/games/duke/src/types.h
index 629fb8449..2ecc6cd2f 100644
--- a/source/games/duke/src/types.h
+++ b/source/games/duke/src/types.h
@@ -375,6 +375,10 @@ struct player_struct
 	{
 		PlayerNowPosition.Z = val;
 	}
+	void posZadd(const double val)
+	{
+		PlayerNowPosition.Z += val;
+	}
 };
 
 struct Cycler