diff --git a/src/main/java/com/eliotlash/molang/functions/classic/Acos.java b/src/main/java/com/eliotlash/molang/functions/classic/Acos.java index ad87dd9..0283862 100644 --- a/src/main/java/com/eliotlash/molang/functions/classic/Acos.java +++ b/src/main/java/com/eliotlash/molang/functions/classic/Acos.java @@ -21,7 +21,7 @@ public int getRequiredArguments() { @Override public double _evaluate(Expr[] arguments, ExecutionContext ctx) { double a = this.evaluateArgument(arguments, ctx, 0); - if (a > 1) { + if (Math.abs(a) > 1) { return 0; } return Math.acos(a); diff --git a/src/main/java/com/eliotlash/molang/functions/classic/Asin.java b/src/main/java/com/eliotlash/molang/functions/classic/Asin.java index 9e781ed..1c88c3f 100644 --- a/src/main/java/com/eliotlash/molang/functions/classic/Asin.java +++ b/src/main/java/com/eliotlash/molang/functions/classic/Asin.java @@ -21,7 +21,7 @@ public int getRequiredArguments() { @Override public double _evaluate(Expr[] arguments, ExecutionContext ctx) { double a = this.evaluateArgument(arguments, ctx, 0); - if (a > 1) { + if (Math.abs(a) > 1) { return 0; } return Math.asin(a); diff --git a/src/main/java/com/eliotlash/molang/functions/classic/Atan.java b/src/main/java/com/eliotlash/molang/functions/classic/Atan.java index 0c2bad8..b65a44d 100644 --- a/src/main/java/com/eliotlash/molang/functions/classic/Atan.java +++ b/src/main/java/com/eliotlash/molang/functions/classic/Atan.java @@ -21,7 +21,7 @@ public int getRequiredArguments() { @Override public double _evaluate(Expr[] arguments, ExecutionContext ctx) { double a = this.evaluateArgument(arguments, ctx, 0); - if (a > 1) { + if (Math.abs(a) > 1) { return 0; } return Math.atan(a); } diff --git a/src/main/java/com/eliotlash/molang/functions/utility/MinAngle.java b/src/main/java/com/eliotlash/molang/functions/utility/MinAngle.java new file mode 100644 index 0000000..4436b31 --- /dev/null +++ b/src/main/java/com/eliotlash/molang/functions/utility/MinAngle.java @@ -0,0 +1,28 @@ +package com.eliotlash.molang.functions.utility; + +import com.eliotlash.molang.ast.Expr; +import com.eliotlash.molang.functions.Function; +import com.eliotlash.molang.utils.Interpolations; +import com.eliotlash.molang.variables.ExecutionContext; + +public class MinAngle extends Function { + + public MinAngle(String name) { + super(name); + } + + @Override + public int getRequiredArguments() { + return 3; + } + + @Override + public double _evaluate(Expr[] arguments, ExecutionContext ctx) { + double result = this.evaluateArgument(arguments, ctx, 0); + // Clamp the result to -360 to 360, then add 360 to make it positive, then mod 360 to get the positive angle + result = ((result % 360) + 360) % 360; + // If the result is greater than 180, subtract 360 to get the negative angle + if (result > 179) result -= 360; + return result; + } +} diff --git a/src/main/java/com/eliotlash/molang/variables/ExecutionContext.java b/src/main/java/com/eliotlash/molang/variables/ExecutionContext.java index 431fd34..4f4148d 100644 --- a/src/main/java/com/eliotlash/molang/variables/ExecutionContext.java +++ b/src/main/java/com/eliotlash/molang/variables/ExecutionContext.java @@ -61,6 +61,7 @@ public ExecutionContext(Evaluator evaluator) { registerFunction("math", new Trunc("trunc")); registerFunction("math", new Lerp("lerp")); registerFunction("math", new LerpRotate("lerprotate")); + registerFunction("math", new MinAngle("min_angle")); registerFunction("math", new Random("random")); registerFunction("math", new RandomInteger("random_integer")); registerFunction("math", new DiceRoll("dice_roll"));