From c9abb0273ee4c3b7feb6b083104a884fd8023741 Mon Sep 17 00:00:00 2001 From: kirillsaint Date: Mon, 21 Aug 2023 15:29:49 +0600 Subject: [PATCH] Better Math --- .../silentclient/client/math/JavaMath.java | 12 ++++++ .../silentclient/client/math/LibGDXMath.java | 29 +++++++++++++++ .../silentclient/client/math/RandomMath.java | 12 ++++++ .../client/math/RivensFullMath.java | 27 ++++++++++++++ .../client/math/RivensHalfMath.java | 33 +++++++++++++++++ .../silentclient/client/math/RivensMath.java | 37 +++++++++++++++++++ .../silentclient/client/math/TaylorMath.java | 33 +++++++++++++++++ .../client/mixin/mixins/MathHelperMixin.java | 27 ++++++++++++++ src/main/resources/mixins.SilentClient.json | 3 +- 9 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/silentclient/client/math/JavaMath.java create mode 100644 src/main/java/net/silentclient/client/math/LibGDXMath.java create mode 100644 src/main/java/net/silentclient/client/math/RandomMath.java create mode 100644 src/main/java/net/silentclient/client/math/RivensFullMath.java create mode 100644 src/main/java/net/silentclient/client/math/RivensHalfMath.java create mode 100644 src/main/java/net/silentclient/client/math/RivensMath.java create mode 100644 src/main/java/net/silentclient/client/math/TaylorMath.java create mode 100644 src/main/java/net/silentclient/client/mixin/mixins/MathHelperMixin.java diff --git a/src/main/java/net/silentclient/client/math/JavaMath.java b/src/main/java/net/silentclient/client/math/JavaMath.java new file mode 100644 index 0000000..14b2a2b --- /dev/null +++ b/src/main/java/net/silentclient/client/math/JavaMath.java @@ -0,0 +1,12 @@ +package net.silentclient.client.math; + +public class JavaMath { + + public static float sin(float radians) { + return (float) Math.sin((double) radians); + } + + public static float cos(float radians) { + return (float) Math.cos((double) radians); + } +} diff --git a/src/main/java/net/silentclient/client/math/LibGDXMath.java b/src/main/java/net/silentclient/client/math/LibGDXMath.java new file mode 100644 index 0000000..db7167f --- /dev/null +++ b/src/main/java/net/silentclient/client/math/LibGDXMath.java @@ -0,0 +1,29 @@ +package net.silentclient.client.math; + +public class LibGDXMath { + + public static final float BF_PI = 3.1415927F; + public static final float BF_degreesToRadians = 0.017453292F; + private static final float[] BF_table = new float[16384]; + + public static float sin(float radians) { + return LibGDXMath.BF_table[(int) (radians * 2607.5945F) & 16383]; + } + + public static float cos(float radians) { + return LibGDXMath.BF_table[(int) ((radians + 1.5707964F) * 2607.5945F) & 16383]; + } + + static { + int i; + + for (i = 0; i < 16384; ++i) { + LibGDXMath.BF_table[i] = (float) Math.sin((double) (((float) i + 0.5F) / 16384.0F * 6.2831855F)); + } + + for (i = 0; i < 360; i += 90) { + LibGDXMath.BF_table[(int) ((float) i * 45.511112F) & 16383] = (float) Math.sin((double) ((float) i * 0.017453292F)); + } + + } +} diff --git a/src/main/java/net/silentclient/client/math/RandomMath.java b/src/main/java/net/silentclient/client/math/RandomMath.java new file mode 100644 index 0000000..8cc2fc1 --- /dev/null +++ b/src/main/java/net/silentclient/client/math/RandomMath.java @@ -0,0 +1,12 @@ +package net.silentclient.client.math; + +public class RandomMath { + + public static float sin(float radians) { + return (float) Math.random() * radians; + } + + public static float cos(float radians) { + return (float) Math.random() * radians; + } +} diff --git a/src/main/java/net/silentclient/client/math/RivensFullMath.java b/src/main/java/net/silentclient/client/math/RivensFullMath.java new file mode 100644 index 0000000..5b60fd4 --- /dev/null +++ b/src/main/java/net/silentclient/client/math/RivensFullMath.java @@ -0,0 +1,27 @@ +package net.silentclient.client.math; + +public class RivensFullMath { + + private static final float BF_SIN_TO_COS = 1.5707964F; + private static final int BF_SIN_BITS = 12; + private static final int BF_SIN_MASK = ~(-1 << RivensFullMath.BF_SIN_BITS); + private static final int BF_SIN_COUNT = RivensFullMath.BF_SIN_MASK + 1; + private static final float BF_radFull = 6.2831855F; + private static final float BF_radToIndex = (float) RivensFullMath.BF_SIN_COUNT / RivensFullMath.BF_radFull; + private static final float[] BF_sinFull = new float[RivensFullMath.BF_SIN_COUNT]; + + public static float sin(float rad) { + return RivensFullMath.BF_sinFull[(int) (rad * RivensFullMath.BF_radToIndex) & RivensFullMath.BF_SIN_MASK]; + } + + public static float cos(float rad) { + return sin(rad + RivensFullMath.BF_SIN_TO_COS); + } + + static { + for (int i = 0; i < RivensFullMath.BF_SIN_COUNT; ++i) { + RivensFullMath.BF_sinFull[i] = (float) Math.sin(((double) i + (double) Math.min(1, i % (RivensFullMath.BF_SIN_COUNT / 4)) * 0.5D) / (double) RivensFullMath.BF_SIN_COUNT * (double) RivensFullMath.BF_radFull); + } + + } +} diff --git a/src/main/java/net/silentclient/client/math/RivensHalfMath.java b/src/main/java/net/silentclient/client/math/RivensHalfMath.java new file mode 100644 index 0000000..aa84682 --- /dev/null +++ b/src/main/java/net/silentclient/client/math/RivensHalfMath.java @@ -0,0 +1,33 @@ +package net.silentclient.client.math; + +public class RivensHalfMath { + + private static final float BF_SIN_TO_COS = 1.5707964F; + private static final int BF_SIN_BITS = 12; + private static final int BF_SIN_MASK = ~(-1 << RivensHalfMath.BF_SIN_BITS); + private static final int BF_SIN_MASK2 = RivensHalfMath.BF_SIN_MASK >> 1; + private static final int BF_SIN_COUNT = RivensHalfMath.BF_SIN_MASK + 1; + private static final int BF_SIN_COUNT2 = RivensHalfMath.BF_SIN_MASK2 + 1; + private static final float BF_radFull = 6.2831855F; + private static final float BF_radToIndex = (float) RivensHalfMath.BF_SIN_COUNT / RivensHalfMath.BF_radFull; + private static final float[] BF_sinHalf = new float[RivensHalfMath.BF_SIN_COUNT2]; + + public static float sin(float rad) { + int index1 = (int) (rad * RivensHalfMath.BF_radToIndex) & RivensHalfMath.BF_SIN_MASK; + int index2 = index1 & RivensHalfMath.BF_SIN_MASK2; + int mul = index1 == index2 ? 1 : -1; + + return RivensHalfMath.BF_sinHalf[index2] * (float) mul; + } + + public static float cos(float rad) { + return sin(rad + RivensHalfMath.BF_SIN_TO_COS); + } + + static { + for (int i = 0; i < RivensHalfMath.BF_SIN_COUNT2; ++i) { + RivensHalfMath.BF_sinHalf[i] = (float) Math.sin(((double) i + (double) Math.min(1, i % (RivensHalfMath.BF_SIN_COUNT / 4)) * 0.5D) / (double) RivensHalfMath.BF_SIN_COUNT * (double) RivensHalfMath.BF_radFull); + } + + } +} diff --git a/src/main/java/net/silentclient/client/math/RivensMath.java b/src/main/java/net/silentclient/client/math/RivensMath.java new file mode 100644 index 0000000..35aa121 --- /dev/null +++ b/src/main/java/net/silentclient/client/math/RivensMath.java @@ -0,0 +1,37 @@ +package net.silentclient.client.math; + +public class RivensMath { + + private static final int BF_SIN_BITS = 12; + private static final int BF_SIN_MASK = ~(-1 << RivensMath.BF_SIN_BITS); + private static final int BF_SIN_COUNT = RivensMath.BF_SIN_MASK + 1; + private static final float BF_radFull = 6.2831855F; + private static final float BF_radToIndex = (float) RivensMath.BF_SIN_COUNT / RivensMath.BF_radFull; + private static final float BF_degFull = 360.0F; + private static final float BF_degToIndex = (float) RivensMath.BF_SIN_COUNT / RivensMath.BF_degFull; + private static final float[] BF_sin = new float[RivensMath.BF_SIN_COUNT]; + private static final float[] BF_cos = new float[RivensMath.BF_SIN_COUNT]; + + public static float sin(float rad) { + return RivensMath.BF_sin[(int) (rad * RivensMath.BF_radToIndex) & RivensMath.BF_SIN_MASK]; + } + + public static float cos(float rad) { + return RivensMath.BF_cos[(int) (rad * RivensMath.BF_radToIndex) & RivensMath.BF_SIN_MASK]; + } + + static { + int i; + + for (i = 0; i < RivensMath.BF_SIN_COUNT; ++i) { + RivensMath.BF_sin[i] = (float) Math.sin((double) (((float) i + 0.5F) / (float) RivensMath.BF_SIN_COUNT * RivensMath.BF_radFull)); + RivensMath.BF_cos[i] = (float) Math.cos((double) (((float) i + 0.5F) / (float) RivensMath.BF_SIN_COUNT * RivensMath.BF_radFull)); + } + + for (i = 0; i < 360; i += 90) { + RivensMath.BF_sin[(int) ((float) i * RivensMath.BF_degToIndex) & RivensMath.BF_SIN_MASK] = (float) Math.sin((double) i * 3.141592653589793D / 180.0D); + RivensMath.BF_cos[(int) ((float) i * RivensMath.BF_degToIndex) & RivensMath.BF_SIN_MASK] = (float) Math.cos((double) i * 3.141592653589793D / 180.0D); + } + + } +} diff --git a/src/main/java/net/silentclient/client/math/TaylorMath.java b/src/main/java/net/silentclient/client/math/TaylorMath.java new file mode 100644 index 0000000..682ca29 --- /dev/null +++ b/src/main/java/net/silentclient/client/math/TaylorMath.java @@ -0,0 +1,33 @@ +package net.silentclient.client.math; + +public class TaylorMath { + + private static final float BF_SIN_TO_COS = 1.5707964F; + + public static float sin(float rad) { + double x = (double) rad; + double x2 = x * x; + double x3 = x2 * x; + double x5 = x2 * x3; + double x7 = x2 * x5; + double x9 = x2 * x7; + double x11 = x2 * x9; + double x13 = x2 * x11; + double x15 = x2 * x13; + double x17 = x2 * x15; + double val = x - x3 * 0.16666666666666666D; + + val += x5 * 0.008333333333333333D; + val -= x7 * 1.984126984126984E-4D; + val += x9 * 2.7557319223985893E-6D; + val -= x11 * 2.505210838544172E-8D; + val += x13 * 1.6059043836821613E-10D; + val -= x15 * 7.647163731819816E-13D; + val += x17 * 2.8114572543455206E-15D; + return (float) val; + } + + public static float cos(float rad) { + return sin(rad + TaylorMath.BF_SIN_TO_COS); + } +} diff --git a/src/main/java/net/silentclient/client/mixin/mixins/MathHelperMixin.java b/src/main/java/net/silentclient/client/mixin/mixins/MathHelperMixin.java new file mode 100644 index 0000000..b47a09f --- /dev/null +++ b/src/main/java/net/silentclient/client/mixin/mixins/MathHelperMixin.java @@ -0,0 +1,27 @@ +package net.silentclient.client.mixin.mixins; + +import net.minecraft.util.MathHelper; +import net.silentclient.client.math.RivensHalfMath; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +@Mixin(MathHelper.class) +public class MathHelperMixin { + /** + * @author kirillsaint + * @reason custom math (faster) + */ + @Overwrite + public static float sin(float param) { + return RivensHalfMath.sin(param); + } + + /** + * @author kirillsaint + * @reason custom math (faster) + */ + @Overwrite + public static float cos(float param) { + return RivensHalfMath.cos(param); + } +} diff --git a/src/main/resources/mixins.SilentClient.json b/src/main/resources/mixins.SilentClient.json index a230184..efda61a 100644 --- a/src/main/resources/mixins.SilentClient.json +++ b/src/main/resources/mixins.SilentClient.json @@ -114,6 +114,7 @@ "accessors.optifine.CustomColorsAccessor", "accessors.optifine.ConfigAccessor", "mixins.NetworkPlayerInfoMixin", - "accessors.NetworkPlayerInfoAccessor" + "accessors.NetworkPlayerInfoAccessor", + "mixins.MathHelperMixin" ] } \ No newline at end of file