From 2778db81c6973078dc0e8e04c4bb711143aef84d Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Fri, 12 Sep 2025 18:19:16 +0800 Subject: feat: add new reroll modifiers for enhanced dice functionality --- src/calculator.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/calculator.rs') diff --git a/src/calculator.rs b/src/calculator.rs index 5376f76..07518b0 100644 --- a/src/calculator.rs +++ b/src/calculator.rs @@ -45,7 +45,7 @@ impl DiceCalculator { } } - // handle reroll + // handle reroll variants for modifier in &dice.modifiers { match modifier { DiceModifier::Reroll(threshold) => { @@ -60,6 +60,27 @@ impl DiceCalculator { final_rolls[pos] = new_roll as i32; } } + DiceModifier::RerollUntil(threshold) => { + // keep rolling until > threshold (safety cap) + let mut current = *final_rolls.last().unwrap_or(&((roll) as i32)); + let mut attempts = 0; + const MAX_ATTEMPTS: usize = 100; + while current <= *threshold && attempts < MAX_ATTEMPTS { + let new_roll = rand::random::() % dice.sides as u32 + 1; + current = new_roll as i32; + final_rolls = vec![current]; + attempts += 1; + } + } + DiceModifier::RerollAndAdd(threshold) => { + // if <= threshold, roll again and add to the last value + if final_rolls.iter().any(|&r| r <= *threshold) { + let new_roll = rand::random::() % dice.sides as u32 + 1; + let mut sum = final_rolls.iter().sum::(); + sum += new_roll as i32; + final_rolls = vec![sum]; + } + } _ => {} } } @@ -246,6 +267,8 @@ impl DiceCalculator { DiceModifier::ExplodeKeepHigh(n) => result.push_str(&format!("K{}", n)), DiceModifier::Reroll(n) => result.push_str(&format!("r{}", n)), DiceModifier::RerollOnce(n) => result.push_str(&format!("ro{}", n)), + DiceModifier::RerollUntil(n) => result.push_str(&format!("R{}", n)), + DiceModifier::RerollAndAdd(n) => result.push_str(&format!("a{}", n)), DiceModifier::KeepAlias(n) => result.push_str(&format!("k{}", n)), DiceModifier::KeepHigh(n) => result.push_str(&format!("kh{}", n)), DiceModifier::KeepLow(n) => result.push_str(&format!("kl{}", n)), -- cgit v1.2.3-70-g09d2