
MathOps
FILE LOCATION: My Personal Repo (until one-click submission
META SCRIPT: This script is a meta-script, and part of the ZeroFrame Meta Toolbox. It can be used on its own, or in conjunction with ZeroFrame and the other meta-toolbox scripts, to modify other scripts' API commands.
ABSTRACT: MathOps gives you a way to use inline math equations in command lines intended for other scripts, independent of whether that script's developer built a way to parse equations. You can use standard math operations (*, /, -, +, %), as well as functions, and you can nest the operations using parentheses for ordered evaluation. All of this happens before the intended-recipient script sees the message.
Introduction
Using the MATH tag, you can drop real-time, inline math calculations into your macro command to have the value rendered before the message is handed off to the intended script.
Syntax Highlights
{& math 2*2} => simple multiplication
{& math @{selected|STR}*2} => Roll20 resolves attribute calls before the API sees the message
{& math $[[0]]/2 } => MathOps will expand inline rolls and roll markers, replacing with 0 if not found
{& math sin($[[1d360]]) } => using the sin() function to get the sine of a random angle
{& math trunc(1+(@{selected|Hits}/2)) } => nested operations and trunc() function (round toward 0)
Using MathOps
The MATH tag is denoted by the {& math ... } tag, where the equation to evaluate follows the tag name:
{& math (2+3)/4}
The above would output 1.25. When MathOps handled the message, 1.25 would be left behind in place of the larger syntax structure.
You can use numbers, parentheses, known constants, mule variables, inline rolls or roll markers, and math functions as operands in + , - , * , / , and % (modulo) operations (exponentiation is handled in a function):
{& math round(sin(90) * pi, 2) / randb(0,4) }
The above rounds (to 2 decimal places), the sine of 90 multiplied by pi, then divides that by a random number between 0 and 4.
Available Functions and Constants
Most of the functions and constants in the javascript Math library are available to you. The Math tag will utilize these for their value (constants) or output (functions).
Functions
FUNCTION | DESCRIPTION | EXAMPLE USAGE -----------+------------------------------------------+-----------------------
abs | Absolute value | abs(x)
acos | ArcCosine | acos(x)
acosh | Hyberbolic ArcCosine | acosh(x)
asin | ArcSine | asin(x)
asinh | Hyperbolic ArcSine | asinh(x)
atan | ArcTangent | atan(x)
atanh | Hyperbolic ArcTangent | atanh(x)
atantwo | ArcTangent of quotient of 2 args | atantwo(x)
cbrt | Cube root | cbrt(x)
ceiling | Ceiling | ceiling(x)
cos | Cosine | cos(x)
cosh | Hyperbolic Cosine | cosh(x)
exp | Euler's constant raised to power | exp(x)
expmone | Subtracting 1 from exp() | expmone(x)
floor | Floor | floor(x)
hypot | Hypotenuse | hypot(x, y, ...)
log | Natural Logarithm | log(x)
logonep | Natural logarithm of 1 + x | logonep(x)
logten | Base-10 logarithm | logten(x)
logtwo | Base-2 logarithm | logtwo(x)
min | Minimum of... | min(x, y, z, ...) max | Maximum of... | max(x, y, z, ...) pow | Power/Exponentiation | pow(x, y)
rand | Random | rand()
randb | Random between (lower inclusive) | randb(x, y)
randib | Random integer between (lower inclusive) | randib(x, y)
randa | Random among... | randa(x, y, z, ...)
round | Round x to y digits | round(x, y)
sin | Sine | sin(x)
sinh | Hyberbolic Sine | sinh(x)
sqrt | Square Root | sqrt(x)
tan | Tangent | tan(x)
tanh | Hyperbolic Tangent | tanh(x)
trunc | Truncate (round toward 0) | trunc(x)
Constants
CONSTANT | DESCRIPTION -----------+-----------------------
e | Euler's Constant
pi | Pi
lntwo | Natural Log of 2
lnten | Natural Log of 10 logtwoe | Base-2 Log of E logtene | Base-10 Log of E
Interaction with other Scripts in the Meta-Toolbox
If you have other scripts from the Meta-Toolbox installed, MathOps gets a few extra interactions.
Muler - if you have Muler installed and you load a Mule, the variables within it are available directly, without having to use a get. to retrieve it.
Fetch - if you have Fetch installed, you can retrieve most token or sheet items, and default them to an appropriate value if nothing is found. Just make sure that Fetch runs before MathOps (see the ZeroFrame thread for instructions on ordering the loop process).