rpg_dice_roller/parse/mod.rs
1mod dice;
2mod expression;
3
4pub use dice::*;
5pub use expression::*;
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct Dice {
9 pub(crate) quantity: u32,
10 pub(crate) kind: DiceKind,
11 pub(crate) modifiers: Vec<Modifier>,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum DiceKind {
16 /// `d{sides}` or `d%` as a shortcut for `d100`\
17 /// Die with specified number of sides.
18 Standard(i32),
19 /// `dF.1`\
20 /// Fudge/Fate die with 4 blanks, 1 plus, 1 minus.
21 Fudge1,
22 /// `dF` or `dF.2`\
23 /// Fudge/Fate die with 2 blanks, 2 plus, 2 minus.
24 Fudge2,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq)]
28pub enum ComparePoint {
29 /// =
30 Equal(f64),
31 /// <>
32 NotEqual(f64),
33 /// <
34 LessThan(f64),
35 /// \>
36 GreaterThan(f64),
37 /// <=
38 LessThanOrEqual(f64),
39 /// \>=
40 GreaterThanOrEqual(f64),
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum ExplodingKind {
45 /// `!` or `!{compare_point}`\
46 /// Rolls an additional die.
47 Standard,
48 /// `!p` or `!p{compare_point}`\
49 /// Rolls an additional die but reduces its value by 1.
50 Penetrating,
51 /// `!!` or `!!{compare_point}`\
52 /// Rolls an additional die and adds its value to the previous roll.
53 Compounding,
54 /// `!!p` or `!!p{compare_point}`\
55 /// Rolls an additional die, reduces its value by 1 and adds it to the previous roll.
56 PenetratingCompounding,
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum SortKind {
61 Ascending,
62 Descending,
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub enum KeepKind {
67 Highest,
68 Lowest,
69}
70
71#[derive(Debug, Clone, Copy, PartialEq)]
72#[repr(u8)]
73/// Modifiers are special optional notations placed after a die or expression group.
74///
75/// Group expression can only use Keep/Drop, TargetSuccess/Failure, and Sort.
76/// Modifiers are applied in the order of the list below even if they were written in a different order in the input.\
77/// If more than one of the same modifier appears in the input, the last modifier is the one that will be applied.
78pub enum Modifier {
79 /// `min{amount}`\
80 /// Update the roll value to `amount` if it was **below** it.
81 Min(i32),
82 /// `max{amount}`\
83 /// Update the roll value to `amount` if it was **above** it.
84 Max(i32),
85 /// The roll explodes whenever the value passed the compare point or it's the highest value on the dice if not specified.\
86 /// The roll can explode more than once.
87 /// See [ExplodingKind](ExplodingKind) for the different variants of exploding available.
88 Exploding(ExplodingKind, Option<ComparePoint>),
89 /// `r` or `r{compare_point}` | `ro` or `ro{compare_point}` for reroll once\
90 /// Rerolls the dice if it was the lowest number on the dice or it hit the compare point.
91 // True means to only re-roll once
92 ReRoll(bool, Option<ComparePoint>),
93 /// `u` or `u{compare_point}` | `uo` or `uo{compare_point}` for reroll unique once\
94 /// Rerolls the dice if the value was previously seen before.
95 // True means to only re-roll a unique dice once
96 Unique(bool, Option<ComparePoint>),
97 /// `{compare_point}`\
98 /// The final roll value sum will be now determined by the amount of rolls that passed the compare point, +1 for every roll.
99 TargetSuccess(ComparePoint),
100 /// `{success_compare_point}f{failure_compare_point}`\
101 /// The final roll value sum will be now determined by the amount of rolls that passed or failed the compare points, +1 for every success roll, -1 for every fail roll.
102 TargetFailure(ComparePoint, ComparePoint),
103 /// `cs` or `cs{compare_point}`\
104 /// Purely cosmetic, adds the `**` notation if the roll was the highest value on the dice or passed the compare point.
105 CriticalSuccess(Option<ComparePoint>),
106 /// `cf` or `cf{compare_point}`\
107 /// Purely cosmetic, adds the `__` notation if the roll was the lowest value on the dice or hit the compare point.
108 CriticalFailure(Option<ComparePoint>),
109 /// `k{amount}`, `kh{amount}` or `kl{amount}`\
110 /// Drops every roll except the highest or lowest `{amount}`.
111 /// (Defaults to keep highest).
112 Keep(KeepKind, u32),
113 /// `d{amount}`, `dh{amount}` or `dl{amount}`\
114 /// Drops `{amount}` of lowest or highest rolls.
115 /// (Defaults to drop lowest).
116 Drop(KeepKind, u32),
117 /// `s`, `sa` or `sd`\
118 /// Sorts the roll values in ascending or descending order.
119 /// (Defaults to sort ascending).
120 Sort(SortKind),
121}
122
123#[derive(Debug, Clone, PartialEq)]
124pub enum Expression {
125 Value(f64),
126 DiceStandard(Option<Box<Expression>>, Box<Expression>, Vec<Modifier>),
127 DiceFudge1(Option<Box<Expression>>, Vec<Modifier>),
128 DiceFudge2(Option<Box<Expression>>, Vec<Modifier>),
129 DicePercentile(Option<Box<Expression>>, Vec<Modifier>),
130 Parens(Box<Expression>),
131 Group(Vec<Expression>, Vec<Modifier>),
132 Infix(Operator, Box<Expression>, Box<Expression>),
133 Fn1(MathFn1, Box<Expression>),
134 Fn2(MathFn2, Box<Expression>, Box<Expression>),
135}
136
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138pub enum Operator {
139 Add,
140 Sub,
141 Mul,
142 Div,
143 Rem,
144 Pow,
145}
146
147#[derive(Debug, Clone, Copy, PartialEq, Eq)]
148pub enum MathFn1 {
149 Abs,
150 Floor,
151 Ceil,
152 Round,
153 Sign,
154 Sqrt,
155 Log,
156 Exp,
157 Sin,
158 Cos,
159 Tan,
160}
161
162#[derive(Debug, Clone, Copy, PartialEq, Eq)]
163pub enum MathFn2 {
164 Min,
165 Max,
166 Pow,
167}