Skip to content

Commit

Permalink
Merge pull request #3 from LunarClient/add-missing-math-functions
Browse files Browse the repository at this point in the history
Add missing math functions
  • Loading branch information
bernie-g authored Nov 2, 2023
2 parents bcb0d5d + e9f65b2 commit f7f9fec
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/eliotlash/molang/CompileConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class CompileConstants {

public CompileConstants() {
this.registerConstant("PI", Math.PI);
// Bedrock Parity
this.registerConstant("math.pi", Math.PI);
this.registerConstant("E", Math.E);
}

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/eliotlash/molang/functions/classic/Acos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.eliotlash.molang.functions.classic;

import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.functions.Function;
import com.eliotlash.molang.variables.ExecutionContext;

/**
* Arc cosine function
*/
public class Acos extends Function {

public Acos(String name) {
super(name);
}

@Override
public int getRequiredArguments() {
return 1;
}

@Override
public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double a = this.evaluateArgument(arguments, ctx, 0);
if (Math.abs(a) > 1) {
return 0;
}
return Math.acos(a);
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/eliotlash/molang/functions/classic/Asin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.eliotlash.molang.functions.classic;

import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.functions.Function;
import com.eliotlash.molang.variables.ExecutionContext;

/**
* Arc sine function
*/
public class Asin extends Function {

public Asin(String name) {
super(name);
}

@Override
public int getRequiredArguments() {
return 1;
}

@Override
public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double a = this.evaluateArgument(arguments, ctx, 0);
if (Math.abs(a) > 1) {
return 0;
}
return Math.asin(a);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/eliotlash/molang/functions/classic/Atan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.eliotlash.molang.functions.classic;

import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.functions.Function;
import com.eliotlash.molang.variables.ExecutionContext;

/**
* Arc tangent function
*/
public class Atan extends Function {

public Atan(String name) {
super(name);
}

@Override
public int getRequiredArguments() {
return 1;
}

@Override
public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double a = this.evaluateArgument(arguments, ctx, 0);
if (Math.abs(a) > 1) {
return 0;
}
return Math.atan(a); }
}
25 changes: 25 additions & 0 deletions src/main/java/com/eliotlash/molang/functions/classic/Atan2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.eliotlash.molang.functions.classic;

import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.functions.Function;
import com.eliotlash.molang.variables.ExecutionContext;

/**
* Arc tangent function
*/
public class Atan2 extends Function {

public Atan2(String name) {
super(name);
}

@Override
public int getRequiredArguments() {
return 2;
}

@Override
public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
return Math.atan2(this.evaluateArgument(arguments, ctx, 0), this.evaluateArgument(arguments, ctx, 1));
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/eliotlash/molang/functions/utility/DiceRoll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.eliotlash.molang.functions.utility;

import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.functions.Function;
import com.eliotlash.molang.variables.ExecutionContext;

public class DiceRoll extends Function {
public java.util.Random random;


public DiceRoll(String name){
super(name);
this.random = new java.util.Random();
}

@Override
public int getRequiredArguments() {
return 3;
}

public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double returnValue = 0;
double rollCount = this.evaluateArgument(arguments, ctx, 0);
double min = this.evaluateArgument(arguments, ctx, 1);
double max = this.evaluateArgument(arguments, ctx, 2);

for (int i = 0; i < rollCount; i++) {
returnValue += this.random.nextDouble() * (max - min) + min;
}

return returnValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.eliotlash.molang.functions.utility;

import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.functions.Function;
import com.eliotlash.molang.variables.ExecutionContext;

public class DiceRollInteger extends Function {
public java.util.Random random;


public DiceRollInteger(String name){
super(name);
this.random = new java.util.Random();
}

@Override
public int getRequiredArguments() {
return 3;
}

public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double returnValue = 0;
double rollCount = this.evaluateArgument(arguments, ctx, 0);
double min = this.evaluateArgument(arguments, ctx, 1);
double max = this.evaluateArgument(arguments, ctx, 2);

for (int i = 0; i < rollCount; i++) {
returnValue += Math.round(this.random.nextDouble() * (max - min) + min);
}

return returnValue;
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/eliotlash/molang/functions/utility/MinAngle.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.eliotlash.molang.functions.utility;

import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.functions.Function;
import com.eliotlash.molang.variables.ExecutionContext;

public class RandomInteger extends Function {
public java.util.Random random;


public RandomInteger(String name){
super(name);
this.random = new java.util.Random();
}

public double _evaluate(Expr[] arguments, ExecutionContext ctx) {
double random = 0;

if (arguments.length >= 3) {
this.random.setSeed((long) this.evaluateArgument(arguments, ctx, 2));
random = this.random.nextInt();
} else {
random = Math.round(Math.random());
}

if (arguments.length >= 2) {
double a = this.evaluateArgument(arguments, ctx, 0);
double b = this.evaluateArgument(arguments, ctx, 1);

double min = Math.min(a, b);
double max = Math.max(a, b);

random = random * (max - min) + min;
} else if (arguments.length >= 1) {
random = random * this.evaluateArgument(arguments, ctx, 0);
}

return random;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import com.eliotlash.molang.functions.rounding.Floor;
import com.eliotlash.molang.functions.rounding.Round;
import com.eliotlash.molang.functions.rounding.Trunc;
import com.eliotlash.molang.functions.utility.Lerp;
import com.eliotlash.molang.functions.utility.LerpRotate;
import com.eliotlash.molang.functions.utility.*;
import com.eliotlash.molang.functions.utility.Random;
import com.eliotlash.molang.utils.MolangUtils;
import it.unimi.dsi.fastutil.Pair;
Expand Down Expand Up @@ -44,6 +43,10 @@ public ExecutionContext(Evaluator evaluator) {
registerFunction("math", new Cos("cosradians"));
registerFunction("math", new SinDegrees("sin"));
registerFunction("math", new Sin("sinradians"));
registerFunction("math", new Asin("asin"));
registerFunction("math", new Acos("acos"));
registerFunction("math", new Atan("atan"));
registerFunction("math", new Atan2("atan2"));
registerFunction("math", new Exp("exp"));
registerFunction("math", new Ln("ln"));
registerFunction("math", new Mod("mod"));
Expand All @@ -58,7 +61,11 @@ 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"));
registerFunction("math", new DiceRollInteger("dice_roll_integer"));
}

public Evaluator getEvaluator() {
Expand Down

0 comments on commit f7f9fec

Please sign in to comment.