From c5474436af67408342e8dce0ec996d62c9a4c21c Mon Sep 17 00:00:00 2001
From: Sally Coolatta <tehrealsalt@gmail.com>
Date: Sat, 7 Nov 2020 19:47:50 -0500
Subject: [PATCH] Use FixedHypot over P_AproxDistance

Not convinced that the small speed benefit from P_AproxDistance is worth the "aproximate"[sic] results it gives. Let's instead try a define to replace it with FixedHypot. In Lua, the function gives a deprecated warning.

Inspired by the hyperwall fix for vanilla, except for everything. From little testing, actively improves waypoint checks, bumping, speed checks, wall collisions, Jawz targetting, Lightning Shield attacks, so on.

The only way I see this as a potential downgrade is A_Look (and related functions) getting slower, which are barely used in Kart.
---
 src/lua_baselib.c |  3 ++-
 src/p_maputl.c    | 13 -------------
 src/p_maputl.h    |  2 +-
 3 files changed, 3 insertions(+), 15 deletions(-)

diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index 515f6f0ba6..7b2e42bd58 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -427,7 +427,8 @@ static int lib_pAproxDistance(lua_State *L)
 	fixed_t dx = luaL_checkfixed(L, 1);
 	fixed_t dy = luaL_checkfixed(L, 2);
 	//HUDSAFE
-	lua_pushfixed(L, P_AproxDistance(dx, dy));
+	LUA_Deprecated(L, "P_AproxDistance", "FixedHypot");
+	lua_pushfixed(L, FixedHypot(dx, dy));
 	return 1;
 }
 
diff --git a/src/p_maputl.c b/src/p_maputl.c
index 90718a41cb..83905a4183 100644
--- a/src/p_maputl.c
+++ b/src/p_maputl.c
@@ -24,19 +24,6 @@
 #include "p_slopes.h"
 #include "z_zone.h"
 
-//
-// P_AproxDistance
-// Gives an estimation of distance (not exact)
-//
-fixed_t P_AproxDistance(fixed_t dx, fixed_t dy)
-{
-	dx = abs(dx);
-	dy = abs(dy);
-	if (dx < dy)
-		return dx + dy - (dx>>1);
-	return dx + dy - (dy>>1);
-}
-
 //
 // P_ClosestPointOnLine
 // Finds the closest point on a given line to the supplied point
diff --git a/src/p_maputl.h b/src/p_maputl.h
index 08b606833c..df90ab4b47 100644
--- a/src/p_maputl.h
+++ b/src/p_maputl.h
@@ -41,7 +41,7 @@ typedef boolean (*traverser_t)(intercept_t *in);
 boolean P_PathTraverse(fixed_t px1, fixed_t py1, fixed_t px2, fixed_t py2,
 	INT32 pflags, traverser_t ptrav);
 
-FUNCMATH fixed_t P_AproxDistance(fixed_t dx, fixed_t dy);
+#define P_AproxDistance(dx, dy) FixedHypot(dx, dy)
 void P_ClosestPointOnLine(fixed_t x, fixed_t y, line_t *line, vertex_t *result);
 void P_ClosestPointOnLine3D(const vector3_t *p, const vector3_t *line, vector3_t *result);
 INT32 P_PointOnLineSide(fixed_t x, fixed_t y, line_t *line);
-- 
GitLab