#roll #expressions #dice-roll #parser #d-d #die #check

critfail

A library for parsing and rolling D&D 5e roll expressions

1 unstable release

0.3.0 May 10, 2020

#14 in #die

21 downloads per month

Custom license

62KB
1K SLoC

Critfail

A rust library for parsing and rolling D&D 5e roll expressions.

Usage

Add this to your Cargo.toml:

[dependencies]
critfail = "0.3"

See the docs for usage and examples.


lib.rs:

Evaluation and rolling of D&D 5e die roll expressions.

The RollExpression trait provides methods for dealing with the various kinds of rolls. Roll provides the simplest text-in, text-out interface for rolling expressions an printing the result regardless of the type of roll.

use critfail::{RollExpression, Roll};

let check = Roll::new("r-3").unwrap();
let check_outcome = check.roll();
print!("{}", check_outcome); // eg. "11"
print!("{:?}", check_outcome); // eg. "(14)-3"

let damage = Roll::new("2d8+6").unwrap();
let damage_outcome = damage.roll();
print!("{}", damage_outcome); // eg. "13"
print!("{:?}", damage_outcome); // eg. "[2+5]+6"

let attack = Roll::new("r+1?2d6+4").unwrap();
let attack_outcome = attack.roll();
print!("{}", attack_outcome); // eg. "10 ? 16"
print!("{:?}", attack_outcome); // eg. "(9)+1 ? [6+6]+4"

In order to handle the outcome of a Roll programatically, roll expressions are split into Check rolls, Damage rolls, and Attack rolls, each with their own outcome type which provides methods for determining the score and makeup of the results for each.

Features

  • wasm-bindgen: Enable this when compiling for wasm32 targets, or random number generation won't work.

Dependencies

~2.6–4MB
~69K SLoC