forked from fadookie/particleman
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from LunarClient/add-missing-math-functions
Add missing math functions
- Loading branch information
Showing
10 changed files
with
256 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/eliotlash/molang/functions/classic/Acos.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/main/java/com/eliotlash/molang/functions/classic/Asin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/main/java/com/eliotlash/molang/functions/classic/Atan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/main/java/com/eliotlash/molang/functions/classic/Atan2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/main/java/com/eliotlash/molang/functions/utility/DiceRoll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/eliotlash/molang/functions/utility/DiceRollInteger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/main/java/com/eliotlash/molang/functions/utility/MinAngle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/eliotlash/molang/functions/utility/RandomInteger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters