Dice library
Dice formulas: the Dice module parses formulas such as 2d6kh1 + strength into a syntax
tree, rolls them and formats the outcome for display.
Formulas are case-insensitive and whitespace between tokens is ignored. They are made of:
- numbers (
3,2.5), parentheses, binary+ - * / %, unary-and^(power); - dice terms
NdS(d20is1d20), fudge dicedF/NdF(-1 to 1) andd%(d100); the count and the sides may also be a parenthesized expression or a{variable}; - modifiers right after a dice term, any number of them in any order:
kh N/k Nkeep the highest,kl Nkeep the lowest,dh Ndrop the highest,dl Ndrop the lowest (N is 1 when left out),!explode on the highest face,!Nexplode on N or more (at most 100 extra dice per term),r N/ro Nreroll once when N or less,rr Nreroll until above N (at most 100 times per die),min N/max Nclamp every die; - functions
floor(x),ceil(x),round(x),abs(x),min(a, b, ...),max(a, b, ...); - variables: bare names such as
strengthor names in braces such as{level}, looked up in lower case. A term rolls at most 1000 dice, a formula at most 10000, and dice have 1 to 10000 sides. Rerolls and explosions happen while rolling, clamps apply to every die, then keep and drop modifiers apply in the order they are written. Division results are not rounded.
Dice.roll('1d20 + 5') -- 1d20 (14) + 5 = 19
Dice.roll('4d6kh3') -- 4d6kh3 (5, 4, 3, [1]) = 12
Dice.roll('2d20kl1 + {stealth}', { stealth = 3 })
Dice.roll('d6! + floor(level / 2)', function(name) return 4 end)
Dice.roll('3dF + 4d10rr1 min 2')
Summary
Functions
Functions
Rolls plain dice without a formula.
Parameters
- sides Number sides of each die
- count Number optional, defaults to
1how many dice to roll
Returns
- Number sum of the dice, 0 when the arguments are invalid
Rolls a parsed formula.
The result has total, formula (as given), rolls (a list of
{ sides, results, kept, dropped, total } per dice term, in evaluation order, with sides
being 'F' for fudge dice), natural (sum of the kept dice of all terms, nil without dice) and
critical ('success' or 'failure' when the formula has a single d20 term that keeps one die
showing 20 or 1).
Parameters
- ast Table syntax tree from Dice.parse
- variables Table/Function optional, defaults to
nilmap of variable names to numbers, or function(name) returning a number
Returns
- Table result, or nil and String error message naming the position
Formats a result for display, listing every die after its term and the total at the end.
Dice.format(Dice.roll('4d6kh3')) -- '4d6kh3 (5, 4, 3, [1]) = 12'
Parameters
- result Table result from Dice.roll or Dice.evaluate
Returns
- String formatted roll
Formats a result like Dice.format, only shorter, for a line of chat: a term of one die loses its count ('1d20' becomes 'd20'), and a formula that is a single die and nothing else shows the die and the total alone, as the face and the total are the same number.
Dice.format_compact(Dice.roll('1d20 + 3')) -- 'd20 (14) + 3 = 17'
Dice.format_compact(Dice.roll('1d20')) -- 'd20 = 14'
Dice.format_compact(Dice.roll('4d6kh3')) -- '4d6kh3 (5, 4, 3, [1]) = 12'
Parameters
- result Table result from Dice.roll or Dice.evaluate
Returns
- String formatted roll
Checks whether a formula can be rolled. Without variables only the syntax is checked. When variables are given, the formula is rolled once with them, so that unknown variables, dice counts and sides out of range and divisions by zero are reported as well.
Parameters
- formula String dice formula
- variables Table/Function optional, defaults to
nilvariable values to check against
Returns
- Boolean whether the formula is valid
- String error message when it is not
Parses a dice formula into a syntax tree that can be evaluated any number of times.
local ast, err = Dice.parse('2d6kh1 + strength')
Parameters
- formula String dice formula
Returns
- Table syntax tree, or nil and String error message naming the position
Parses and rolls a dice formula.
local result, err = Dice.roll('1d20 + 5')
print(result.total, result.natural, result.critical)
Parameters
- formula String dice formula
- variables Table/Function optional, defaults to
nilmap of variable names to numbers, or function(name) returning a number
Returns
- Table result as described in Dice.evaluate, or nil and String error message