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 (d20 is 1d20), fudge dice dF/NdF (-1 to 1) and d% (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 N keep the highest, kl N keep the lowest, dh N drop the highest, dl N drop the lowest (N is 1 when left out), ! explode on the highest face, !N explode on N or more (at most 100 extra dice per term), r N/ro N reroll once when N or less, rr N reroll until above N (at most 100 times per die), min N/max N clamp every die;
  • functions floor(x), ceil(x), round(x), abs(x), min(a, b, ...), max(a, b, ...);
  • variables: bare names such as strength or 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

Rolls plain dice without a formula.
Rolls a parsed formula.
Formats a result for display, listing every die after its term and the total at the end.
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.
Checks whether a formula can be rolled.
Parses a dice formula into a syntax tree that can be evaluated any number of times.
Parses and rolls a dice formula.

Functions

Dice.d(sides, count)

shared lib/stdlib/sh_dice.lua:1287

Rolls plain dice without a formula.

Parameters

  • sides Number sides of each die
  • count Number optional, defaults to 1 how many dice to roll

Returns

  • Number sum of the dice, 0 when the arguments are invalid

Dice.evaluate(ast, variables)

shared lib/stdlib/sh_dice.lua:1146

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 nil map of variable names to numbers, or function(name) returning a number

Returns

  • Table result, or nil and String error message naming the position

Dice.format(result)

shared lib/stdlib/sh_dice.lua:1185

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

Dice.format_compact(result)

shared lib/stdlib/sh_dice.lua:1240

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

Dice.is_valid(formula, variables)

shared lib/stdlib/sh_dice.lua:1265

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 nil variable values to check against

Returns

  • Boolean whether the formula is valid
  • String error message when it is not

Dice.parse(formula)

shared lib/stdlib/sh_dice.lua:1122

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

Dice.roll(formula, variables)

shared lib/stdlib/sh_dice.lua:1169

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 nil map 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

Defined in