From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: bl4ckscor3 <bl4ckscor33@gmail.com>
Date: Tue, 2 Feb 2021 21:38:12 -0500
Subject: [PATCH] (AI-Improvements) Optimized tan for ControllerLook

Code originally licenced under MIT from: https://github.com/BuiltBrokenModding/AI-Improvements/tree/1.16

diff --git a/src/main/java/com/builtbroken/ai/improvements/FastTrig.java b/src/main/java/com/builtbroken/ai/improvements/FastTrig.java
new file mode 100644
index 0000000000000000000000000000000000000000..82150126c0d1608de0af9a8582571628efe53c2d
--- /dev/null
+++ b/src/main/java/com/builtbroken/ai/improvements/FastTrig.java
@@ -0,0 +1,59 @@
+package com.builtbroken.ai.improvements;
+
+public class FastTrig {
+    private static final int ATAN2_BITS = 8;
+
+    private static final int ATAN2_BITS2 = ATAN2_BITS << 1;
+    private static final int ATAN2_MASK = ~(-1 << ATAN2_BITS2);
+    private static final int ATAN2_COUNT = ATAN2_MASK + 1;
+    private static final int ATAN2_DIM = (int) Math.sqrt(ATAN2_COUNT);
+
+    private static final float INV_ATAN2_DIM_MINUS_1 = 1.0f / (ATAN2_DIM - 1);
+
+    private static final float[] atan2 = new float[ATAN2_COUNT];
+    static {
+        for (int i = 0; i < ATAN2_DIM; i++) {
+            for (int j = 0; j < ATAN2_DIM; j++) {
+                float x0 = (float) i / ATAN2_DIM;
+                float y0 = (float) j / ATAN2_DIM;
+
+                atan2[j * ATAN2_DIM + i] = (float) Math.atan2(y0, x0);
+            }
+        }
+    }
+
+
+    public static final float atan2(double y, double x) {
+        float add, mul;
+
+        if (x < 0.0f) {
+            if (y < 0.0f) {
+                x = -x;
+                y = -y;
+
+                mul = 1.0f;
+            } else {
+                x = -x;
+                mul = -1.0f;
+            }
+
+            add = -3.141592653f;
+        } else {
+            if (y < 0.0f) {
+                y = -y;
+                mul = -1.0f;
+            } else {
+                mul = 1.0f;
+            }
+
+            add = 0.0f;
+        }
+
+        double invDiv = 1.0f / (((x < y) ? y : x) * INV_ATAN2_DIM_MINUS_1);
+
+        int xi = (int) (x * invDiv);
+        int yi = (int) (y * invDiv);
+
+        return (atan2[yi * ATAN2_DIM + xi] + add) * mul;
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/net/minecraft/server/ControllerLook.java b/src/main/java/net/minecraft/server/ControllerLook.java
index 31bad166359c2f2fa0182005c6ae4b26ab54ca4f..66601cf3c30ae468fda61099f5eaac627fde5974 100644
--- a/src/main/java/net/minecraft/server/ControllerLook.java
+++ b/src/main/java/net/minecraft/server/ControllerLook.java
@@ -80,14 +80,14 @@ public class ControllerLook {
         double d2 = this.g - this.a.locZ();
         double d3 = (double) MathHelper.sqrt(d0 * d0 + d2 * d2);
 
-        return (float) (-(MathHelper.d(d1, d3) * 57.2957763671875D));
+        return (float) (-(com.builtbroken.ai.improvements.FastTrig.atan2(d1, d3) * 57.2957763671875D)); // Yatopia - Optimized tan implementation
     }
 
     protected float h() {
         double d0 = this.e - this.a.locX();
         double d1 = this.g - this.a.locZ();
 
-        return (float) (MathHelper.d(d1, d0) * 57.2957763671875D) - 90.0F;
+        return (float) (com.builtbroken.ai.improvements.FastTrig.atan2(d1, d0) * 57.2957763671875D) - 90.0F; // Yatopia - Optimized tan implementation
     }
 
     protected float a(float f, float f1, float f2) {
