From fdc2c3adcd6fe71eaba71fce21dd33ce812860ff Mon Sep 17 00:00:00 2001
From: Monster Iestyn <iestynjealous@ntlworld.com>
Date: Sat, 10 Oct 2015 21:21:16 +0100
Subject: [PATCH] Fix up lib_finetangent so tan() returns values starting from
 "0" in Lua (finetangent itself hasn't been touched)

Also fixed how the function went out of the array's bounds for ANGLE_180 and above (or negative angles)
---
 src/lua_mathlib.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/lua_mathlib.c b/src/lua_mathlib.c
index 8ca2e17af8..f4b5ca5fe9 100644
--- a/src/lua_mathlib.c
+++ b/src/lua_mathlib.c
@@ -77,7 +77,9 @@ static int lib_finecosine(lua_State *L)
 
 static int lib_finetangent(lua_State *L)
 {
-	lua_pushfixed(L, FINETANGENT((luaL_checkangle(L, 1)>>ANGLETOFINESHIFT) & FINEMASK));
+	// HACK: add ANGLE_90 to make tan() in Lua start at 0 like it should
+	// use & 4095 instead of & FINEMASK (8191), so it doesn't go out of the array's bounds
+	lua_pushfixed(L, FINETANGENT(((luaL_checkangle(L, 1)+ANGLE_90)>>ANGLETOFINESHIFT) & 4095));
 	return 1;
 }
 
-- 
GitLab