From a512e0518db5411f203f15e627ae24eb4006064c Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 13 Feb 2025 23:44:33 +0100 Subject: [PATCH] Corrects a flawed implementation of the binomial dist --- .gitignore | 1 + .../equipement_advanced_repr.rs | 43 ++++++++++++++++--- .../weaponry_structs/equipement_structs.rs | 37 +++++----------- 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 7d12725..f690d27 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ Cargo.lock *.json.gz callgrind* cache.out +tarpaulin-report.html diff --git a/weaponry/src/weaponry_structs/equipement_advanced_repr.rs b/weaponry/src/weaponry_structs/equipement_advanced_repr.rs index bdac53f..cb4ffe1 100644 --- a/weaponry/src/weaponry_structs/equipement_advanced_repr.rs +++ b/weaponry/src/weaponry_structs/equipement_advanced_repr.rs @@ -10,6 +10,7 @@ #![allow(dead_code)] use core::f64; +use std::fmt::Display; use crate::interfaces::RefinedList; use probability::prelude::{Binomial, Discrete}; @@ -96,6 +97,7 @@ impl From<&InnerEquipementStats> for AdvancedStats { /// A damage matrix, crossing damage conditions (high dice to low dice) with precision (high precision to low precision) /// /// This simulation does only calculate a very limited preset of damage values +#[cfg_attr(test, derive(Debug))] struct DamageMatrix { /// The inner damage matrix damage_matrix: Vec, @@ -107,6 +109,13 @@ impl DamageMatrix { } } +impl Display for DamageMatrix { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let _ = f; + todo!() + } +} + impl From<&InnerEquipementStats> for DamageMatrix { fn from(value: &InnerEquipementStats) -> Self { // let binomial_distribution = Binomial::new(dices, value.precision); @@ -127,20 +136,25 @@ impl From<&InnerEquipementStats> for DamageMatrix { /// Calculate the binomial distribution and average the damage from there fn binomial_dice_distribution(value: &InnerEquipementStats, precision: usize, dices: usize) -> f64 { - let binomial = Binomial::new(dices, (precision as u8 + value.precision) as f64); + let p = ((precision as i8 + value.precision) as f64 / 10.).clamp(0., 1. - 1E-10); + // println!("precision: {},{}", p, 9.99999); + let binomial = Binomial::new(dices, p); (0..dices + 1) .map(|target_dices| { // TODO: This non rounding can be veird - let number_success = binomial.mass(target_dices); // Let's have weird half dices as usize; - if number_success >= 1. { - (value.damage + value.p_damage) as f64 * (number_success - 1.) + let probability_success = binomial.mass(target_dices); // Let's have weird half dices as usize; + println!("probability : {:?}", probability_success); + if target_dices >= 1 { + (value.damage as f64 + + value.p_damage as f64 + * (target_dices.clamp(0, value.rmax as usize) - 1) as f64) + * probability_success } else { 0. } }) .fold(0., |a, b| a + b) - / dices as f64 } /// The precision infered for a damage calculation event @@ -186,8 +200,25 @@ impl DiceNumber { #[cfg(test)] mod test { + use crate::weaponry_structs::{ + equipement_advanced_repr::DamageMatrix, equipement_structs::InnerEquipementStats, + }; + #[test] - fn test_binomial() {} + fn test_binomial() { + let damage = InnerEquipementStats { + damage: 10, + p_damage: 2, + precision: 0, + rmax: 3, + armor_break: 2, + ..Default::default() + }; + let mat: DamageMatrix = DamageMatrix::from(&damage); + println!("{:#?}", damage); + println!("{:#?}", mat); + panic!() + } #[test] fn test_creation() {} diff --git a/weaponry/src/weaponry_structs/equipement_structs.rs b/weaponry/src/weaponry_structs/equipement_structs.rs index eab2fce..d4e6f5e 100644 --- a/weaponry/src/weaponry_structs/equipement_structs.rs +++ b/weaponry/src/weaponry_structs/equipement_structs.rs @@ -66,7 +66,7 @@ pub struct InnerEquipementStats { pub(super) tier: u8, pub(super) damage: u8, pub(super) p_damage: u8, - pub(super) precision: u8, + pub(super) precision: i8, pub(super) rmax: u8, pub(super) armor_break: u8, pub(super) armor_pierce: u8, @@ -101,12 +101,14 @@ impl InnerEquipementStats { } /// Shape of the equipement, first mandatory part of an equipement +#[derive(Default)] pub struct EqShape { /// The inner stats of the part pub(super) stats: InnerEquipementStats, } /// Material used for the equipement, all equipements are crafted with 1-2 materials +#[derive(Default)] pub struct EqMaterial { /// The inner stats of the part pub(super) stats: InnerEquipementStats, @@ -115,12 +117,14 @@ pub struct EqMaterial { /// Nature of the equipement, some natures are reserved and some can be chained /// /// Deciding on the combinaison can be tricky an will need an extra field +#[derive(Default)] pub struct EqNature { /// The inner stats of the part pub(super) stats: InnerEquipementStats, } /// Finishing touches, since all of them have a cost, they are treated in a special fashion +#[derive(Default)] pub struct EqFinishing { /// The inner stats of the part pub(super) stats: InnerEquipementStats, @@ -133,8 +137,7 @@ mod test { use crate::interfaces::RecipeCollection; use super::{ - EqFinishing, EqMaterial, EqNature, EqShape, Equipement, InitialEquipementList, - InnerEquipementStats, + EqMaterial, EqNature, EqShape, Equipement, InitialEquipementList, InnerEquipementStats, }; // use pretty_assertions::assert_eq; @@ -159,30 +162,10 @@ mod test { #[test] fn test_creating_all_equipements() { - let shape = vec![ - EqShape { - stats: InnerEquipementStats::default(), - } - .into(), - ]; - let nature = vec![ - EqNature { - stats: InnerEquipementStats::default(), - } - .into(), - ]; - let material = vec![ - EqMaterial { - stats: InnerEquipementStats::default(), - } - .into(), - ]; - let finishing = vec![ - EqFinishing { - stats: InnerEquipementStats::default(), - } - .into(), - ]; + let shape = vec![EqShape::default().into(), EqShape::default().into()]; + let nature = vec![EqNature::default().into(), EqNature::default().into()]; + let material = vec![EqMaterial::default().into(), EqMaterial::default().into()]; + let finishing = vec![EqMaterial::default().into(), EqMaterial::default().into()]; let mut leq = InitialEquipementList::from_vectored(shape); leq.combine_all(&InitialEquipementList::from_vectored(nature)); -- 2.45.1