Crates.io | diceroll |
lib.rs | diceroll |
version | 0.1.0 |
source | src |
created_at | 2022-11-10 09:18:00.829388 |
updated_at | 2022-11-10 09:18:00.829388 |
description | A simple dice rolling lib for RPG purposes. |
homepage | |
repository | https://github.com/sikker/diceroll-rs |
max_upload_size | |
id | 709391 |
size | 10,346 |
A simple dice rolling lib for RPG purposes in Rust.
Install the package by adding it to Cargo.toml:
[dependencies]
diceroll = "0.1.0"
The basic way you use the library is creating a diceroll using DiceRoll::new()
, configuring it with the builder pattern with the parameters you want. Then you pass said configuration to the roll()
function, giving you a RollResult
struct containing an i32 total
and a Vecrolls
. It is also possible to pass the configured diceroll to roll_with_advantage
or to roll_with_disadvantage
, which will return an array of two roll results with index 0 always containing the "winning" roll (with disadvantage this means the lowest result, with advantage this means the highest), and with index 1 always containing the discarded roll.
The following parameters are supported:
use diceroll::*;
fn main() {
let amount_of_dice = 2;
let sides = 6;
let modifier = 2;
let result = roll(DiceRoll::new()
.dice(amount_of_dice)
.sides(sides)
.modifier(modifier))
println!("We rolled {}d{}+{}, which yielded a total of {}.", amount_of_dice, sides, modifier, result.total);
}
This will give an output looking something like this:
We rolled 2d6+2, which yielded a total of 9.
Note that we are using the itertools crate for joining the roll results in this example.
use itertools::Itertools;
use diceroll::*;
fn main() {
let amount_of_dice = 1;
let sides = 20;
let modifier = 2;
let result = roll_with_advantage(DiceRoll::new()
.dice(amount_of_dice)
.sides(sides)
.modifier(modifier));
println!("We rolled {}d{}+{} with advantage", amount_of_dice, sides, modifier);
println!("--- The winning roll ({}) yielded a total of {}", Itertools::join(&mut result[0].rolls.iter(), ","), result[0].total);
println!("--- The discarded roll ({}) yielded a total of {}", Itertools::join(&mut result[1].rolls.iter(), ","), result[1].total);
}
Note that we are using the itertools crate for joining the roll results in this example.
use itertools::Itertools;
use diceroll::*;
fn main() {
let amount_of_dice = 5;
let sides = 10;
let target = 8;
let explode_on = 10;
let result = roll(DiceRoll::new()
.dice(amount_of_dice)
.sides(sides)
.target(target)
.explode_on(explode_on));
println!("We rolled a dice pool of {}d{} with a target number of {} and exploding successes on {}", amount_of_dice, sides, target, explode_on);
println!("--- The result of the rolls ({}) yielded a total of {}", Itertools::join(&mut result[0].rolls.iter(), ","), result.total);
}