Crates.io | dice-bag |
lib.rs | dice-bag |
version | 0.2.0 |
source | src |
created_at | 2023-10-12 08:05:04.974066 |
updated_at | 2024-05-22 03:50:31.012901 |
description | This library uses standard RPG notation for generating dice rolls producing a total score, but also a break down of all the rolls should they be needed. |
homepage | https://codeberg.org/brotherwolf/dice-bag |
repository | https://codeberg.org/brotherwolf/dice-bag |
max_upload_size | |
id | 1001034 |
size | 21,362 |
This library uses standard RPG notation for generating dice rolls producing a total but also a break down of all the rolls should they be needed.
To import it use:
// Cargo.toml
[dependencies]
dice-bag = "0.2"
It has a main entry point of:
use dice::dice;
dice::roll("3d10+1")
produces a structure:
AllRollResults {
pub total: isize,
pub rolled: Vec<RollResult>,
}
RollResult {
pub total: isize,
pub rolled: DiceRolls,
pub selected_rolls: DiceRolls,
pub sides: usize,
pub num_dice: usize,
pub modifier: isize,
}
e.g. for 3d10+1 rolling 6,5,2
{
total: 14,
rolled: [
{
total: 14
rolled: [ 6, 5, 2]
selected_rolls: [ 6, 5, 2]
sides: 6
num_dice: 3
modifier: 1
}
]
}
Dice can also be chained together:
dice::roll("2d8 4d4+8")
where a result might look like this:
{
total: 25,
rolled: [
{
total: 9
rolled: [ 8, 1]
selected_rolls: [ 8, 1]
sides: 8
num_dice: 2
modifier: 0
},
{
total: 16
rolled: [ 3, 2, 2, 1]
selected_rolls: [ 3, 2, 2, 1]
sides: 4
num_dice: 4
modifier: 8
}
]
}
You can directly use the roll method:
dice::Dice::roll(3, 6, -2);
which produces a single Result<RollResult, String>
, for example:
Ok( RollResult {
total: 6
rolled: [ 5, 2, 1 ]
selected_rolls: [ 5, 2, 1 ]
sides: 6
num_dice: 3
modifier: -2
} )
Dice roll results are all sorted highest to lowest.
Finally the Display
for the results is overwritten to produce a parsable slug:
dice::roll("3d6-2 2d4+1")).to_string();
could produce:
3d6-2,2d4+1:[4,2,1],[4,3]:13
dice::roll("2d10+3")).to_string();
could produce:
2d10+3:[10,1]:14