Crates.io | rpg-dice-roller |
lib.rs | rpg-dice-roller |
version | 0.1.3 |
source | src |
created_at | 2024-12-01 09:53:10.786095 |
updated_at | 2024-12-01 17:22:02.129742 |
description | A crate to roll dice with modifiers and apply expressions to them. |
homepage | |
repository | https://github.com/riccardofano/rpg-dice-roller |
max_upload_size | |
id | 1467308 |
size | 121,303 |
rpg-dice-roller
allows you to simulate dice rolls with modifiers and mathematical expressions.
This crate is inspired by and based on the popular JavaScript library rpg-dice-roller, please check out their wonderful documentation for more details on the functionality provided by the library.
cargo add rpg-dice-roller
use rpg-dice-roller::roll;
// Roll 4 d10.
let rolled = roll("4d10")?;
println!("{rolled} = {}", rolled.value()); // [3, 2, 3, 3] = 11
// Roll 3 d20, make the minimum value 5, keep the lowest 2 rolls.
let rolled = roll("3d20min5kl2")?;
println!("{rolled} = {}", rolled.value()); // [6, 5^, 17d] = 11
// Roll a group of 3 expressions:
// 3d100,
// 3 fudge dice (d6s with 2 -1 sides, 2 +1 sides and 2 blank ones) and add 5 to the result,
// 1 d50 and evalate its result to the power of 2
// Count the number of expressions whose value was greater than 50
let rolled = roll("{3d%, 3dF.2 + 5, pow(d50, 2)}>50")?;
println!("{rolled} = {}", rolled.value()); // {[68, 8, 31]*, [1, 1, 1] + 5, pow([14], 2)*} = 2
More details on the dice kind documentation
More details in the modifier documentation
+
, subtraction -
, multiplication *
, division /
, remainder %
, power ^
or **
.abs()
, floor()
, ceil()
, round()
, sign()
, sqrt()
, log(E)
, exp()
, sin()
, cos()
, tan()
.min(_, _)
, max(_, _)
, pow(_, _)
.{}
will sum (or apply target success/failure if modifiers were applied) to the comma separated expressions inside.This crates uses rand::thread_rng()
by default but provides _with
functions (roll_with
, Dice::roll_once_with()
, etc.) so you can use anything the rand::Rng
trait with them.