Add-ons / Dice Roller & Fairmath Functions

This is a bunch of functions and methods for performing some useful number- or math-based operations.

Author ChapelR
Website https://twinelab.net/custom-macros-for-sugarcube-2/#/operations-api
Story format SugarCube 2
Last checked Tue Apr 13 2021
License Unlicense
Download dice-fair.zip

Index

Overview

This is a bunch of functions and methods for performing some useful number- or math-based operations.

JavaScript API

dice()

dice()

Syntax: dice(diceNotation) or dice(numberOfDice, sidesOfDice)

A function for rolling dice. The dice to be rolled can be provided as a string of dice notation (i.e. 1d6 + 10, 4d4-1, 1d20), or as sperate arguments (3d6 becomes dice(3, 6), for example). This funciton is global, but if it's unavailable, it can be found on the setup object under setup.dice.roll().

Arguments:

Usage:

/% each <<set>> macro below rolls three six-sided dice and adds to (3d6+2) %/

<<set _roll to dice('3d6+2')>>
<<set _roll to dice('3d6') + 2>>
<<set _roll to dice(3, 6) + 2>>

Number.prototype.dice()

Number.prototype.dice()

Syntax: <numberOfDice>.dice(sidesOfDice) or <numberOfDice>.d(sidesOfDice)

Similar to the dice() function, this is another way to roll dice, and exists on the Number prototype, meaning it can be called directly on numbers. <number>.d() and <number>.dice() do the same thing and can be used interchangeably.

Arguments:

Usage:

/% each <<set>> macro below rolls three six-sided dice and adds two (3d6+2) %/
<<set _sides to 6, _num to 3>>

<<set _roll to (3).d(6) + 2>>
<<set _roll to _num.d(6) + 2>>
<<set _roll to (3).dice(_sides) + 2>>
<<set _roll to _num.dice(_sides) + 2>>

Number.prototype.fairmath()

Number.prototype.fairmath()

Syntax: <value>.fairmath(change) or <value>.fm(change)

Read more on what fairmath is here. Basically, a number is changed in a percentile way, and the lower its value, the greater the effect of adding to it, and vice versa, etc.

Arguments:

Usage:

<<set $stat to 90>>
<<set $otherStat to 50>>
<<set $otherOtherStat to 10>>

<<set $stat to $stat.fm(20)>>
<<set $otherStat to $otherStat.fairmath(20)>>
<<set $otherOtherStat to $otherOtherStat.fairmath(-20)>>

<<= $stat>> /% 92 %/
<<= $otherStat>> /% 60 %/
<<= $otherOtherStat>> /% 8 %/

Number.prototype.between()

Number.prototype.between()

Syntax: <value>.between(a, b) or Math.between(value, a, b)

Returns true if the value is between the numbers a and b, inclusive.

Arguments:

Usage:

<<if $stat.between(50, 70)>>
    Your stat is average.
<</if>>

<<if Math.between($grade, 80, 89)>>
    You got a B in the class.
<</if>>

Math.fairmath()

Math.fairmath()

Syntax: Math.farimath(value, change) or Math.fm(value, change)

The same as <number>.fairmath() above, but on the Math object, which you may or may not prefer.

Arguments:

Usage:

<<set $stat to 90>>
<<set $otherStat to 50>>
<<set $otherStat to 10>>

<<set $stat to Math.fm($stat, -20)>>
<<set $otherStat to Math.fairmath($otherStat, -20)>>
<<set $otherOtherStat to Math.fairmath($otherOtherStat, 20)>>

<<= $stat>> /% 72 %/
<<= $otherStat>> /% 40 %/
<<= $otherOtherStat>> /% 28 %/

Usage notes

Configuration options

There are some configuration options you can mess with if you know what you're doing. Take a look at the unminified script for more.

Live demo

Demo Twee code:

:: Start
<<link "Roll 3d6...">><<set _roll to dice('3d6')>><<replace #diceroll>>You rolled _roll!<</replace>><</link>> @@#diceroll;@@