diff --git a/weaponry/src/weaponry_structs/equipement_advanced_repr.rs b/weaponry/src/weaponry_structs/equipement_advanced_repr.rs index ea8cf36..88de8a0 100644 --- a/weaponry/src/weaponry_structs/equipement_advanced_repr.rs +++ b/weaponry/src/weaponry_structs/equipement_advanced_repr.rs @@ -205,7 +205,7 @@ pub struct DamageMatrix { } impl DamageMatrix { /// Get the damage value from given condition - pub fn get_damage(&self, precision: Precision, dices: DiceNumber) -> f64 { + pub fn get_damage(&self, precision: FixedPrecision, dices: FixedDiceNumber) -> f64 { self.damage_matrix[precision as usize + 3 * dices as usize] } @@ -213,9 +213,9 @@ impl DamageMatrix { // TODO: This safe damage actually uses 5 dices * 2 instead of doing max(5 * 1 , 2 * 1 + 3 * 1) pub fn get_safe_dmg(&self, attack_cost: i8) -> f64 { let _ = attack_cost; - (self.get_damage(Precision::Low, DiceNumber::Few) - + self.get_damage(Precision::Medium, DiceNumber::Few) - + self.get_damage(Precision::High, DiceNumber::Few)) + (self.get_damage(FixedPrecision::Low, FixedDiceNumber::Few) + + self.get_damage(FixedPrecision::Medium, FixedDiceNumber::Few) + + self.get_damage(FixedPrecision::High, FixedDiceNumber::Few)) / 3. } /// The "unsage" damage, as in, full dice, 2 attacks @@ -223,9 +223,9 @@ impl DamageMatrix { pub fn get_unsafe_dmg(&self, attack_cost: i8) -> f64 { f64::max( self.get_safe_dmg(attack_cost) * 2., - (self.get_damage(Precision::Low, DiceNumber::Many) - + self.get_damage(Precision::Medium, DiceNumber::Many) - + self.get_damage(Precision::High, DiceNumber::Many)) + (self.get_damage(FixedPrecision::Low, FixedDiceNumber::Many) + + self.get_damage(FixedPrecision::Medium, FixedDiceNumber::Many) + + self.get_damage(FixedPrecision::High, FixedDiceNumber::Many)) / 3., ) } @@ -250,14 +250,14 @@ impl Display for DamageMatrix { table.apply_modifier(UTF8_ROUND_CORNERS); let mut headers = vec![String::from("D\\P")]; headers.append( - &mut Precision::iter() + &mut FixedPrecision::iter() .map(|e| format!("{:.3}", e)) .collect::>(), ); table.set_header(headers); - table.add_rows(DiceNumber::iter().map(|dices| { + table.add_rows(FixedDiceNumber::iter().map(|dices| { vec![format!("{:.3}", dices)].into_iter().chain( - Precision::iter() + FixedPrecision::iter() .map(move |precision| format!("{:5.2}", self.get_damage(precision, dices))), ) })); @@ -269,9 +269,9 @@ impl DamageMatrix { /// Calculate the DamageMatrix for a given InnerEquipementStats fn calculate_for(value: &InnerEquipementStats) -> Self { DamageMatrix { - damage_matrix: (DiceNumber::iter() + damage_matrix: (FixedDiceNumber::iter() .flat_map(|dices| { - Precision::iter() + FixedPrecision::iter() .map(move |prec| { binomial_dice_distribution(value, prec.value(), dices.value()) }) @@ -285,13 +285,13 @@ impl DamageMatrix { /// Calculate the binomial distribution and average the damage from there fn binomial_dice_distribution( equipement: &InnerEquipementStats, - precision: usize, - dices: usize, + precision: Precision, + dices: Dices, ) -> f64 { - let p = ((precision as i8 + equipement.precision) as f64 / 10.).clamp(0. + 1E-10, 1. - 1E-10); - let binomial = Binomial::new(dices, p); + let p = ((*precision + equipement.precision) as f64 / 10.).clamp(0. + 1E-10, 1. - 1E-10); + let binomial = Binomial::new(*dices, p); - (0..dices + 1) + (0..*dices + 1) .map(|target_dices| { let probability_success = binomial.mass(target_dices); // Let's have weird half dices as usize; if target_dices >= 1 { @@ -313,40 +313,74 @@ fn binomial_dice_distribution( /// The precision infered for a damage calculation event #[derive(EnumIter, Clone, Copy, Display)] #[allow(clippy::missing_docs_in_private_items)] -pub enum Precision { +pub enum FixedPrecision { High, Medium, Low, } +/// Thin representation of Precision to avoid mistakes +#[derive(Clone, Copy)] +pub struct Precision(i8); + +/// Thin representation of Dices to avoid mistakes +#[derive(Clone, Copy)] +pub struct Dices(usize); + +impl std::ops::Deref for Precision { + type Target = i8; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Display for Precision { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> core::fmt::Result { + ::fmt(&self.0, f) + } +} + +impl std::ops::Deref for Dices { + type Target = usize; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Display for Dices { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> core::fmt::Result { + ::fmt(&self.0, f) + } +} + /// The number of dices rolled for a damage simulation #[derive(EnumIter, Clone, Copy, Display)] #[allow(clippy::missing_docs_in_private_items)] -pub enum DiceNumber { +pub enum FixedDiceNumber { Many, Average, Few, } -impl Precision { +impl FixedPrecision { #[inline] /// Associate a value to the different values - fn value(&self) -> usize { + fn value(&self) -> Precision { match self { - Precision::High => 9, - Precision::Medium => 6, - Precision::Low => 4, + FixedPrecision::High => Precision(9), + FixedPrecision::Medium => Precision(6), + FixedPrecision::Low => Precision(4), } } } -impl DiceNumber { +impl FixedDiceNumber { #[inline] /// Associate a value to the different values - fn value(&self) -> usize { + fn value(&self) -> Dices { match self { - DiceNumber::Many => 7, - DiceNumber::Average => 5, - DiceNumber::Few => 3, + FixedDiceNumber::Many => Dices(7), + FixedDiceNumber::Average => Dices(5), + FixedDiceNumber::Few => Dices(3), } } } @@ -354,7 +388,7 @@ impl DiceNumber { #[cfg(test)] mod test { use crate::weaponry_structs::{ - equipement_advanced_repr::{DamageMatrix, DiceNumber, Precision}, + equipement_advanced_repr::{DamageMatrix, FixedDiceNumber, FixedPrecision}, equipement_structs::InnerEquipementStats, }; @@ -383,8 +417,8 @@ mod test { println!("{}", mat); println!( "(High,Many): {}, (Low,Few): {}", - mat.get_damage(Precision::High, DiceNumber::Many), - mat.get_damage(Precision::Low, DiceNumber::Few) + mat.get_damage(FixedPrecision::High, FixedDiceNumber::Many), + mat.get_damage(FixedPrecision::Low, FixedDiceNumber::Few) ); } }