Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Advertisement Create a free account

[Meta Script] MathOps - inline math operations and nested formulas

1618937178

Edited 1620074542
timmaugh
Pro
API Scripter
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).
1618937186
timmaugh
Pro
API Scripter
Updates and Releases
1618937193
timmaugh
Pro
API Scripter
Advanced Usage and Tricks
1619100133
David M.
Pro
API Scripter
OK, now I wanna know what game system need to use Euler's constant. [Reads the PHB:] "...then apply a Laplace transform to the resulting probability distribution..." O.o Looks like this has been quite the ambitious project, Tim! I hope to start poking around a little bit this weekend.
1619104790
Ernest P.
Pro
API Scripter
@David M. - Your games don't include compound interest calculations? Don't we all play to that level in Acquisitions Incorporated??? =P
1619108075
timmaugh
Pro
API Scripter
It could happen! =D Really once the apparatus was there to expose the javascript math library, there wasn't a reason to *not* include it... though I'd have to say that would be a preeeeeeeeeeeeeeeeeeeetty niche group of gamers who'd need that particular constant. A throwaway game at an accounting convention? =D I think the only part of the js Math library I didn't expose was around 32-bit integers because... well, we're not dealing in 32 bit data. (and a collective grumble was heard from the National 32bit Data Convention.) Looking forward to any feedback, David. Let me know if I can help at all!
1620419833
timmaugh
Pro
API Scripter
Version 1.0.2 Released May 7, 2021 FIXED/ADDED : Tag buffering with parentheses of brace-enclosed tags, i.e.,  ({& mule... }) Update Notes This fix gives an option if the command line might result in a series of brace characters that would be inadvertently eaten by Roll20 parsers. In that case, you can enclose the syntax token in parentheses to break up that series of characters. This only matters for brace-enclosed tags: {& math sin(60)} or ({& math sin(60)})
1620743442
timmaugh
Pro
API Scripter
MathOps has been submitted to the one-click and should be available from there with the next merge.