From c7c13e33727eb12118623e169a5f86a9d9746aea Mon Sep 17 00:00:00 2001
From: Lactozilla <jp6781615@gmail.com>
Date: Tue, 30 Jan 2024 14:29:38 -0300
Subject: [PATCH] Recalculate slope vectors at render time

---
 src/p_saveg.c  |  2 ++
 src/p_slopes.c | 28 +++++++++++++---------------
 src/p_slopes.h |  2 +-
 src/r_defs.h   |  2 ++
 src/r_fps.c    |  2 +-
 src/r_plane.c  | 13 ++++++++++++-
 6 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/src/p_saveg.c b/src/p_saveg.c
index 8e2517473..6c6548c56 100644
--- a/src/p_saveg.c
+++ b/src/p_saveg.c
@@ -3221,6 +3221,8 @@ static thinker_t* LoadMobjThinker(actionf_p1 thinker)
 		slope->normal.x = READFIXED(save_p);
 		slope->normal.y = READFIXED(save_p);
 		slope->normal.z = READFIXED(save_p);
+
+		slope->moved = true;
 	}
 	if (diff2 & MD2_DRAWONLYFORPLAYER)
 		mobj->drawonlyforplayer = &players[READUINT8(save_p)];
diff --git a/src/p_slopes.c b/src/p_slopes.c
index 72e643c8a..e75d36ede 100644
--- a/src/p_slopes.c
+++ b/src/p_slopes.c
@@ -29,13 +29,14 @@ pslope_t *slopelist = NULL;
 UINT16 slopecount = 0;
 
 // Calculate line normal
-void P_CalculateSlopeNormal(pslope_t *slope) {
+void P_CalculateSlopeNormal(pslope_t *slope)
+{
 	slope->normal.z = FINECOSINE(slope->zangle>>ANGLETOFINESHIFT);
 	slope->normal.x = FixedMul(FINESINE(slope->zangle>>ANGLETOFINESHIFT), slope->d.x);
 	slope->normal.y = FixedMul(FINESINE(slope->zangle>>ANGLETOFINESHIFT), slope->d.y);
 }
 
-static void CalculateVectors(pslope_t *slope, dvector3_t *dnormal)
+static void CalculateNormalDir(pslope_t *slope, dvector3_t *dnormal)
 {
 	double hyp = hypot(dnormal->x, dnormal->y);
 
@@ -52,17 +53,14 @@ static void CalculateVectors(pslope_t *slope, dvector3_t *dnormal)
 	}
 }
 
-void P_RecalculateSlopeVectors(pslope_t *slope)
+void P_CalculateSlopeVectors(pslope_t *slope)
 {
 	dvector3_t dnormal;
 
-	dnormal.x = FixedToDouble(slope->normal.x);
-	dnormal.y = FixedToDouble(slope->normal.y);
-	dnormal.z = FixedToDouble(slope->normal.z);
-
+	DVector3_Load(&dnormal, FixedToDouble(slope->normal.x), FixedToDouble(slope->normal.y), FixedToDouble(slope->normal.z));
 	DVector3_Load(&slope->dorigin, FixedToDouble(slope->o.x), FixedToDouble(slope->o.y), FixedToDouble(slope->o.z));
 
-	CalculateVectors(slope, &dnormal);
+	CalculateNormalDir(slope, &dnormal);
 }
 
 /// Setup slope via 3 vertexes.
@@ -121,7 +119,7 @@ static void ReconfigureViaVertexes (pslope_t *slope, const vector3_t v1, const v
 		slope->zangle = InvAngle(R_PointToAngle2(0, 0, FRACUNIT, slope->zdelta));
 	}
 
-	P_RecalculateSlopeVectors(slope);
+	P_CalculateSlopeVectors(slope);
 }
 
 /// Setup slope via constants.
@@ -173,7 +171,7 @@ static void ReconfigureViaConstants (pslope_t *slope, const double pa, const dou
 
 	DVector3_Load(&slope->dorigin, 0, 0, d_o);
 
-	CalculateVectors(slope, &dnormal);
+	CalculateNormalDir(slope, &dnormal);
 }
 
 /// Recalculate dynamic slopes.
@@ -212,8 +210,8 @@ void T_DynamicSlopeLine (dynlineplanethink_t* th)
 	if (slope->zdelta != FixedDiv(zdelta, th->extent)) {
 		slope->zdelta = FixedDiv(zdelta, th->extent);
 		slope->zangle = R_PointToAngle2(0, 0, th->extent, -zdelta);
+		slope->moved = true;
 		P_CalculateSlopeNormal(slope);
-		P_RecalculateSlopeVectors(slope);
 	}
 }
 
@@ -444,7 +442,7 @@ static void line_SpawnViaLine(const int linenum, const boolean spawnthinker)
 			fslope->xydirection = R_PointToAngle2(origin.x, origin.y, point.x, point.y);
 
 			P_CalculateSlopeNormal(fslope);
-			P_RecalculateSlopeVectors(fslope);
+			P_CalculateSlopeVectors(fslope);
 
 			if (spawnthinker && (flags & SL_DYNAMIC))
 				P_AddDynLineSlopeThinker(fslope, DP_FRONTFLOOR, line, extent);
@@ -462,7 +460,7 @@ static void line_SpawnViaLine(const int linenum, const boolean spawnthinker)
 			cslope->xydirection = R_PointToAngle2(origin.x, origin.y, point.x, point.y);
 
 			P_CalculateSlopeNormal(cslope);
-			P_RecalculateSlopeVectors(cslope);
+			P_CalculateSlopeVectors(cslope);
 
 			if (spawnthinker && (flags & SL_DYNAMIC))
 				P_AddDynLineSlopeThinker(cslope, DP_FRONTCEIL, line, extent);
@@ -503,7 +501,7 @@ static void line_SpawnViaLine(const int linenum, const boolean spawnthinker)
 			fslope->xydirection = R_PointToAngle2(origin.x, origin.y, point.x, point.y);
 
 			P_CalculateSlopeNormal(fslope);
-			P_RecalculateSlopeVectors(fslope);
+			P_CalculateSlopeVectors(fslope);
 
 			if (spawnthinker && (flags & SL_DYNAMIC))
 				P_AddDynLineSlopeThinker(fslope, DP_BACKFLOOR, line, extent);
@@ -521,7 +519,7 @@ static void line_SpawnViaLine(const int linenum, const boolean spawnthinker)
 			cslope->xydirection = R_PointToAngle2(origin.x, origin.y, point.x, point.y);
 
 			P_CalculateSlopeNormal(cslope);
-			P_RecalculateSlopeVectors(cslope);
+			P_CalculateSlopeVectors(cslope);
 
 			if (spawnthinker && (flags & SL_DYNAMIC))
 				P_AddDynLineSlopeThinker(cslope, DP_BACKCEIL, line, extent);
diff --git a/src/p_slopes.h b/src/p_slopes.h
index 1a7e604b2..fdc07f67e 100644
--- a/src/p_slopes.h
+++ b/src/p_slopes.h
@@ -51,7 +51,7 @@ typedef enum
 void P_LinkSlopeThinkers (void);
 
 void P_CalculateSlopeNormal(pslope_t *slope);
-void P_RecalculateSlopeVectors(pslope_t *slope);
+void P_CalculateSlopeVectors(pslope_t *slope);
 void P_InitSlopes(void);
 void P_SpawnSlopes(const boolean fromsave);
 
diff --git a/src/r_defs.h b/src/r_defs.h
index 4e03afd81..6e0375e61 100644
--- a/src/r_defs.h
+++ b/src/r_defs.h
@@ -349,6 +349,8 @@ typedef struct pslope_s
 
 	double dzdelta;
 
+	boolean moved : 1;
+
 	UINT8 flags; // Slope options
 } pslope_t;
 
diff --git a/src/r_fps.c b/src/r_fps.c
index dd43ca8d4..284d1005c 100644
--- a/src/r_fps.c
+++ b/src/r_fps.c
@@ -631,7 +631,7 @@ void R_ApplyLevelInterpolators(fixed_t frac)
 			R_LerpVector3(&interp->dynslope.oldo, &interp->dynslope.bako, frac, &interp->dynslope.slope->o);
 			R_LerpVector2(&interp->dynslope.oldd, &interp->dynslope.bakd, frac, &interp->dynslope.slope->d);
 			interp->dynslope.slope->zdelta = R_LerpFixed(interp->dynslope.oldzdelta, interp->dynslope.bakzdelta, frac);
-			P_RecalculateSlopeVectors(interp->dynslope.slope);
+			interp->dynslope.slope->moved = true;
 			break;
 		}
 	}
diff --git a/src/r_plane.c b/src/r_plane.c
index 1295b63ba..11aa6c941 100644
--- a/src/r_plane.c
+++ b/src/r_plane.c
@@ -699,6 +699,12 @@ void R_SetSlopePlane(pslope_t *slope, fixed_t xpos, fixed_t ypos, fixed_t zpos,
 	double height, z_at_xy;
 	float ang;
 
+	if (slope->moved)
+	{
+		P_CalculateSlopeVectors(slope);
+		slope->moved = false;
+	}
+
 	R_SetSlopePlaneOrigin(slope, xpos, ypos, zpos, xoff, yoff, angle);
 	height = R_GetSlopeZAt(slope, xpos, ypos);
 	zeroheight = height - FixedToDouble(zpos);
@@ -735,9 +741,14 @@ void R_SetSlopePlane(pslope_t *slope, fixed_t xpos, fixed_t ypos, fixed_t zpos,
 void R_SetScaledSlopePlane(pslope_t *slope, fixed_t xpos, fixed_t ypos, fixed_t zpos, fixed_t xs, fixed_t ys, fixed_t xoff, fixed_t yoff, angle_t angle, angle_t plangle)
 {
 	double height, z_at_xy;
-
 	float ang;
 
+	if (slope->moved)
+	{
+		P_CalculateSlopeVectors(slope);
+		slope->moved = false;
+	}
+
 	R_SetSlopePlaneOrigin(slope, xpos, ypos, zpos, xoff, yoff, angle);
 	height = R_GetSlopeZAt(slope, xpos, ypos);
 	zeroheight = height - FixedToDouble(zpos);
-- 
GitLab