Add a nice turn simulator and a base senario
This commit is contained in:
parent
56289649a4
commit
101b8c778a
|
|
@ -13,22 +13,19 @@ use crate::{
|
|||
error::FatalEquipementError,
|
||||
export::initial_chart,
|
||||
interfaces::{RecipeCollection, RefinedList, ToAnalysis, ToTex},
|
||||
unfinished,
|
||||
weaponry_structs::{
|
||||
Equipement, InitialEquipementList, equipement_structs::InnerEquipementStats,
|
||||
},
|
||||
};
|
||||
use core::f64;
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
fmt::{Debug, Display},
|
||||
ops::{Add, Deref},
|
||||
};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use charming::Chart;
|
||||
use probability::prelude::{Binomial, Discrete};
|
||||
use serde::Serialize;
|
||||
use strum::{Display, EnumIter};
|
||||
|
||||
use super::equipement_advanced_tools::{
|
||||
Dices, Precision, get_damage_with_simulation, get_sustained_damage,
|
||||
};
|
||||
|
||||
/// The final representation of the equipement inplementing all the fields needed for processing
|
||||
// TODO: Add a public getter for the inner data
|
||||
|
|
@ -223,339 +220,10 @@ impl From<&InnerEquipementStats> for AdvancedStats {
|
|||
avg_damage_2a_safe: value.average_damage(&[Precision(7)], &[Dices(5)]), // damage_matrix.get_safe_dmg(value.action_cost),
|
||||
// .average_damage(&[Precision(9), Precision(7), Precision(5)], &[Dices(5)]), // damage_matrix.get_safe_dmg(value.action_cost),
|
||||
scenario_damage: get_damage_with_simulation(value),
|
||||
mini_scenario_damage: (value.damage > 0)
|
||||
mini_scenario_damage: (value.damage + value.p_damage > 0)
|
||||
.then(|| get_sustained_damage(value))
|
||||
.unwrap_or_default(),
|
||||
defensive: (value.parry as f64).max(value.dodge.into()), // TODO[b]: Integrity and prot should be taken into account
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Create the damage profile with the following simulation:
|
||||
/// 10turns total
|
||||
/// - 1 turn no damage
|
||||
/// - 2 turns full damage (12 dice, 130% precision)
|
||||
/// - 2 turns safe attacks (5 dice, 50%p)
|
||||
/// - 1 turn highspeed/range hit (7 dice, 80% precision)
|
||||
/// - 4 turns normal damage (7 dice, 90% precision)
|
||||
// TODO[!] Implementation
|
||||
fn get_damage_with_simulation(value: &InnerEquipementStats) -> f64 {
|
||||
let _ = value;
|
||||
// unfinished!();
|
||||
0.
|
||||
}
|
||||
|
||||
/// Sustained damage
|
||||
///
|
||||
/// Average damage factoring reload, calculated with 9 dice and 90% precision
|
||||
// TODO[!] Replace avg_damage with this
|
||||
fn get_sustained_damage(value: &InnerEquipementStats) -> f64 {
|
||||
// unfinished!();
|
||||
// Actions total and damage total are arbitrarely choosen
|
||||
|
||||
// With reload, damage calculation is rather complicated, since you can half reload but cant half attack
|
||||
// so a reload is valid even if you don't reload fully, but you have to start your turn with less actions
|
||||
// instead of building a complex scenario, the attack pattern is always: Ac:Attack + Re:Reload(and maybe reminder) every turn
|
||||
// value.damage.into()
|
||||
BestDamageScenarios::from_action_constraints(value, 6, 3, Dices(14), Precision(7))
|
||||
.map_or(0., |v| v.damage_done)
|
||||
}
|
||||
|
||||
/// The best resulting attack pattern of a virtual scenrio where some conditions are given
|
||||
#[derive(Debug)]
|
||||
pub struct BestDamageScenarios {
|
||||
/// How many actions were used by the best attack
|
||||
actions_used: i8,
|
||||
/// Attacks actions used by the scenario
|
||||
attacks_used: i8,
|
||||
/// Resulting damage
|
||||
damage_done: f64,
|
||||
/// Remaining actions that need to be used to finish reload
|
||||
reload_remaining: i8,
|
||||
/// A debug value, should be taken down in final builds
|
||||
// TODO[6]: Remove debug once the tests are done
|
||||
depth: u8,
|
||||
}
|
||||
|
||||
impl Add for BestDamageScenarios {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
actions_used: self.actions_used + rhs.actions_used,
|
||||
attacks_used: self.attacks_used + rhs.attacks_used,
|
||||
damage_done: self.damage_done + rhs.damage_done,
|
||||
reload_remaining: self.reload_remaining + rhs.reload_remaining,
|
||||
depth: self.depth + rhs.depth,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Deref for BestDamageScenarios {
|
||||
type Target = f64;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.damage_done
|
||||
}
|
||||
}
|
||||
|
||||
// TODO[!!]: Add a lot of testing
|
||||
impl BestDamageScenarios {
|
||||
/// Calculate the best alternative with a set of contraints
|
||||
///
|
||||
/// Returns None if the scenario cant exist with the current conditions (too few actions for an attack to be launched)
|
||||
fn from_action_constraints(
|
||||
value: &InnerEquipementStats,
|
||||
actions_allowed: i8,
|
||||
attacks_allowed: i8,
|
||||
dices_allowed: Dices,
|
||||
precision: Precision,
|
||||
) -> Option<Self> {
|
||||
Self::from_action_constraints_recursive(
|
||||
value,
|
||||
actions_allowed,
|
||||
attacks_allowed,
|
||||
dices_allowed,
|
||||
precision,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
/// Recursive part of the from_action_constraints call
|
||||
///
|
||||
/// This algorithm takes a recursive approact to avoid too much forward complexity
|
||||
fn from_action_constraints_recursive(
|
||||
value: &InnerEquipementStats,
|
||||
actions_allowed: i8,
|
||||
attacks_allowed: i8,
|
||||
dices_allowed: Dices,
|
||||
precision: Precision,
|
||||
reload_remaining: i8,
|
||||
depth: u8,
|
||||
) -> Option<Self> {
|
||||
// If we can't attack return early
|
||||
if value.action_cost > actions_allowed || attacks_allowed == 0 || reload_remaining != 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let atkreload_cost = value.reload_cost.unwrap_or_default() + value.action_cost;
|
||||
|
||||
// We create our first contender, One attack only
|
||||
let one_attack = Self {
|
||||
// No matter what we try to reload, even if we overcap the attack was still valid
|
||||
actions_used: (atkreload_cost).clamp(0, actions_allowed),
|
||||
attacks_used: 1,
|
||||
damage_done: binomial_dice_distribution(value, precision, dices_allowed),
|
||||
// If the reload+attack exceed the actions, we must bookkeep it
|
||||
// as a reminder that we have leftover actions to spend next turn
|
||||
reload_remaining: (atkreload_cost - actions_allowed).clamp(0, actions_allowed),
|
||||
depth,
|
||||
};
|
||||
println!(
|
||||
"[{depth}] > One attack ({}): {one_attack:?}",
|
||||
Dices(*dices_allowed)
|
||||
);
|
||||
|
||||
// We can only try two attacks if two attack would be valid aka:
|
||||
// - We can spend: 2 attacks and (at least) 1 reload (as actions)
|
||||
// - : 2 attacks (as attacks)
|
||||
if atkreload_cost + value.action_cost < actions_allowed && attacks_allowed > 1 {
|
||||
// Cumbersome because of rounding errors
|
||||
if let (Some(a), Some(b)) = (
|
||||
(Self::from_action_constraints_recursive(
|
||||
value,
|
||||
actions_allowed - atkreload_cost,
|
||||
attacks_allowed - 1,
|
||||
Dices(dices_allowed.div_floor(2)),
|
||||
precision,
|
||||
reload_remaining,
|
||||
depth + 1,
|
||||
)),
|
||||
(Self::from_action_constraints_recursive(
|
||||
value,
|
||||
actions_allowed - atkreload_cost,
|
||||
attacks_allowed - 1,
|
||||
Dices(dices_allowed.div_ceil(2)),
|
||||
precision,
|
||||
reload_remaining,
|
||||
depth + 1,
|
||||
)),
|
||||
) {
|
||||
let sum_sub_attacks = a + b;
|
||||
println!(
|
||||
"[{depth}] > Two attacks ({} & {}) : {sum_sub_attacks:?}",
|
||||
Dices(dices_allowed.div_floor(2)),
|
||||
Dices(dices_allowed.div_ceil(2))
|
||||
);
|
||||
// The alternative was better
|
||||
if one_attack.damage_done < sum_sub_attacks.damage_done {
|
||||
return Some(sum_sub_attacks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The alternative didn't exist or was worse
|
||||
Some(one_attack)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Consider implementing something else instead ?
|
||||
impl InnerEquipementStats {
|
||||
/// Calculate the average damage expected for a set of precisions and dices
|
||||
fn average_damage(&self, precisions: &[Precision], list_dices: &[Dices]) -> f64 {
|
||||
precisions
|
||||
.iter()
|
||||
.map(|precision| {
|
||||
list_dices
|
||||
.iter()
|
||||
.map(|dices| binomial_dice_distribution(self, *precision, *dices))
|
||||
.sum::<f64>()
|
||||
})
|
||||
.sum::<f64>()
|
||||
/ (precisions.len() as f64 * list_dices.len() as f64)
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the binomial distribution and average the damage from there
|
||||
fn binomial_dice_distribution(
|
||||
equipement: &InnerEquipementStats,
|
||||
precision: Precision,
|
||||
dices: Dices,
|
||||
) -> f64 {
|
||||
let p = ((*precision + equipement.precision) as f64 / 10.).clamp(0. + 1E-10, 1. - 1E-10);
|
||||
let binomial = Binomial::new(*dices, p);
|
||||
|
||||
(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 {
|
||||
(equipement.damage as f64
|
||||
+ equipement.p_damage as f64
|
||||
* (target_dices
|
||||
.clamp(0, equipement.rmax as usize)
|
||||
.saturating_sub(1)) as f64)
|
||||
* probability_success
|
||||
} else {
|
||||
0.
|
||||
}
|
||||
})
|
||||
.fold(0., |a, b| a + b)
|
||||
}
|
||||
|
||||
// TODO: Change this to be a thin type and replace enum by new(nb) et high(), med() etc..
|
||||
// : And add a way to do ranges easily
|
||||
/// The precision infered for a damage calculation event
|
||||
#[derive(EnumIter, Clone, Copy, Display)]
|
||||
#[allow(clippy::missing_docs_in_private_items)]
|
||||
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 {
|
||||
<i8 as Display>::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 {
|
||||
<usize as Display>::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 FixedDiceNumber {
|
||||
Many,
|
||||
Average,
|
||||
Few,
|
||||
}
|
||||
|
||||
// TODO[9] These implementations date back to the matrix code, rewriting them would be for the best
|
||||
impl FixedPrecision {
|
||||
#[inline]
|
||||
/// Associate a value to the different values
|
||||
fn value(&self) -> Precision {
|
||||
match self {
|
||||
FixedPrecision::High => Precision(9),
|
||||
FixedPrecision::Medium => Precision(6),
|
||||
FixedPrecision::Low => Precision(4),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl FixedDiceNumber {
|
||||
#[inline]
|
||||
/// Associate a value to the different values
|
||||
fn value(&self) -> Dices {
|
||||
match self {
|
||||
FixedDiceNumber::Many => Dices(8),
|
||||
FixedDiceNumber::Average => Dices(6),
|
||||
FixedDiceNumber::Few => Dices(4),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::weaponry_structs::{
|
||||
equipement_advanced_repr::{FixedDiceNumber, FixedPrecision},
|
||||
equipement_structs::InnerEquipementStats,
|
||||
};
|
||||
|
||||
fn new_inner_eq() -> InnerEquipementStats {
|
||||
InnerEquipementStats {
|
||||
damage: 10,
|
||||
p_damage: 2,
|
||||
precision: 0,
|
||||
rmax: 3,
|
||||
armor_break: 2,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_binomial() {
|
||||
let eq = new_inner_eq();
|
||||
assert!(
|
||||
(eq.average_damage(
|
||||
&[FixedPrecision::High.value()],
|
||||
&[FixedDiceNumber::Many.value()]
|
||||
) - 13.99995162)
|
||||
.abs()
|
||||
< 1E-2
|
||||
);
|
||||
assert!(
|
||||
(eq.average_damage(
|
||||
&[FixedPrecision::Low.value()],
|
||||
&[FixedDiceNumber::Few.value()]
|
||||
) - 10.1119999)
|
||||
.abs()
|
||||
< 1E-2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,433 @@
|
|||
// Workspace: weaponry
|
||||
// File: weaponry/src/weaponry_structs/equipement_advanced_tools.rs
|
||||
//! Tools for the advanced representation, all grouped in a module for convenience
|
||||
use crate::weaponry_structs::equipement_structs::InnerEquipementStats;
|
||||
use probability::distribution::Discrete;
|
||||
use probability::prelude::Binomial;
|
||||
use std::fmt::Display;
|
||||
use std::ops::Add;
|
||||
use std::ops::Deref;
|
||||
use strum::Display;
|
||||
use strum::EnumIter;
|
||||
|
||||
/// Create the damage profile with the following simulation:
|
||||
/// 10turns total
|
||||
/// - 1 turn no damage
|
||||
/// - 2 turns full damage (12 dice, 130% precision)
|
||||
/// - 2 turns safe attacks (5 dice, 50%p)
|
||||
/// - 1 turn highspeed/range hit (7 dice, 80% precision)
|
||||
/// - 4 turns normal damage (7 dice, 90% precision)
|
||||
// TODO[!] Implementation
|
||||
pub(super) fn get_damage_with_simulation(value: &InnerEquipementStats) -> f64 {
|
||||
let _ = value;
|
||||
// unfinished!();
|
||||
0.
|
||||
}
|
||||
|
||||
/// Sustained damage
|
||||
///
|
||||
/// Average damage factoring reload, calculated with 9 dice and 90% precision
|
||||
// TODO[!] Replace avg_damage with this
|
||||
pub(super) fn get_sustained_damage(value: &InnerEquipementStats) -> f64 {
|
||||
// unfinished!();
|
||||
// Actions total and damage total are arbitrarely choosen
|
||||
|
||||
// With reload, damage calculation is rather complicated, since you can half reload but cant half attack
|
||||
// so a reload is valid even if you don't reload fully, but you have to start your turn with less actions
|
||||
// instead of building a complex scenario, the attack pattern is always: Ac:Attack + Re:Reload(and maybe reminder) every turn
|
||||
let mut reloading_left = 0;
|
||||
let mut total_damage = 0.;
|
||||
for _ in 0..10 {
|
||||
if let Some(BestDamageScenarios {
|
||||
actions_used: _,
|
||||
attacks_used: _,
|
||||
damage_done,
|
||||
reload_remaining,
|
||||
depth: _,
|
||||
}) = BestDamageScenarios::from_action_constraints(
|
||||
value,
|
||||
4 - reloading_left,
|
||||
2,
|
||||
Dices(9),
|
||||
Precision(8),
|
||||
) {
|
||||
reloading_left = reload_remaining;
|
||||
total_damage += damage_done;
|
||||
}
|
||||
}
|
||||
total_damage / 10.
|
||||
// Iterate 10 turns, every turn we need to substract the leftover reloading
|
||||
// value.damage.into()
|
||||
}
|
||||
|
||||
/// The best resulting attack pattern of a virtual scenrio where some conditions are given
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(test, derive(Default, Clone))]
|
||||
pub struct BestDamageScenarios {
|
||||
/// How many actions were used by the best attack
|
||||
actions_used: i8,
|
||||
/// Attacks actions used by the scenario
|
||||
attacks_used: i8,
|
||||
/// Resulting damage
|
||||
damage_done: f64,
|
||||
/// Remaining actions that need to be used to finish reload
|
||||
reload_remaining: i8,
|
||||
/// A debug value, should be taken down in final builds
|
||||
// TODO[6]: Remove debug once the tests are done
|
||||
depth: u8,
|
||||
}
|
||||
|
||||
impl Add for BestDamageScenarios {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
actions_used: self.actions_used + rhs.actions_used,
|
||||
attacks_used: self.attacks_used + rhs.attacks_used,
|
||||
damage_done: self.damage_done + rhs.damage_done,
|
||||
reload_remaining: self.reload_remaining + rhs.reload_remaining,
|
||||
depth: self.depth + rhs.depth,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Deref for BestDamageScenarios {
|
||||
type Target = f64;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.damage_done
|
||||
}
|
||||
}
|
||||
|
||||
// TODO[!!]: Add a lot of testing
|
||||
impl BestDamageScenarios {
|
||||
/// Calculate the best alternative with a set of contraints
|
||||
///
|
||||
/// Returns None if the scenario cant exist with the current conditions (too few actions for an attack to be launched)
|
||||
fn from_action_constraints(
|
||||
value: &InnerEquipementStats,
|
||||
actions_allowed: i8,
|
||||
attacks_allowed: i8,
|
||||
dices_allowed: Dices,
|
||||
precision: Precision,
|
||||
) -> Option<Self> {
|
||||
Self::from_action_constraints_recursive(
|
||||
value,
|
||||
actions_allowed,
|
||||
attacks_allowed,
|
||||
dices_allowed,
|
||||
precision,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
/// Recursive part of the from_action_constraints call
|
||||
///
|
||||
/// This algorithm takes a recursive approact to avoid too much forward complexity
|
||||
fn from_action_constraints_recursive(
|
||||
value: &InnerEquipementStats,
|
||||
actions_allowed: i8,
|
||||
attacks_allowed: i8,
|
||||
dices_allowed: Dices,
|
||||
precision: Precision,
|
||||
reload_remaining: i8,
|
||||
depth: u8,
|
||||
) -> Option<Self> {
|
||||
// If we can't attack return early
|
||||
if value.action_cost > actions_allowed
|
||||
|| attacks_allowed == 0
|
||||
|| reload_remaining > 0
|
||||
|| *dices_allowed < 1
|
||||
{
|
||||
println!(
|
||||
"[{depth}] > Reach the end of recursion {}>{actions_allowed}, {attacks_allowed}a, {dices_allowed}d {reload_remaining}r",
|
||||
value.action_cost
|
||||
);
|
||||
return None;
|
||||
}
|
||||
|
||||
let atkreload_cost = value.reload_cost.unwrap_or_default() + value.action_cost;
|
||||
|
||||
// We create our first contender, One attack only
|
||||
let one_attack = Self {
|
||||
// No matter what we try to reload, even if we overcap the attack was still valid
|
||||
actions_used: (atkreload_cost).clamp(0, actions_allowed),
|
||||
attacks_used: 1,
|
||||
damage_done: binomial_dice_distribution(value, precision, dices_allowed),
|
||||
// If the reload+attack exceed the actions, we must bookkeep it
|
||||
// as a reminder that we have leftover actions to spend next turn
|
||||
reload_remaining: (atkreload_cost - actions_allowed + reload_remaining)
|
||||
.clamp(0, actions_allowed),
|
||||
depth,
|
||||
};
|
||||
println!(
|
||||
"[{depth}] > One attack ({}): {one_attack:?}",
|
||||
Dices(*dices_allowed)
|
||||
);
|
||||
|
||||
// We can only try two attacks if two attack would be valid aka:
|
||||
// - We can spend: 2 attacks and (at least) 1 reload (as actions)
|
||||
// - : 2 attacks (as attacks)
|
||||
if atkreload_cost + value.action_cost <= actions_allowed && attacks_allowed > 1 {
|
||||
// Cumbersome because of rounding errors
|
||||
if let (Some(a), Some(b)) = (
|
||||
(Self::from_action_constraints_recursive(
|
||||
value,
|
||||
actions_allowed - atkreload_cost, //.div_floor(2).div_floor(2) * 2 -value.reload_cost.unwrap_or_default(),
|
||||
attacks_allowed.div_floor(2),
|
||||
Dices(dices_allowed.div_floor(2)),
|
||||
precision,
|
||||
reload_remaining - value.reload_cost.unwrap_or_default(),
|
||||
depth + 1,
|
||||
)),
|
||||
(Self::from_action_constraints_recursive(
|
||||
value,
|
||||
actions_allowed - atkreload_cost, // .div_ceil(2).div_ceil(2) * 2 -value.reload_cost.unwrap_or_default(),
|
||||
attacks_allowed.div_ceil(2),
|
||||
Dices(dices_allowed.div_ceil(2)),
|
||||
precision,
|
||||
reload_remaining,
|
||||
depth + 1,
|
||||
)),
|
||||
) {
|
||||
let sum_sub_attacks = a + b;
|
||||
// println!(
|
||||
// "[{depth}] > Two attacks ({} & {}) : {sum_sub_attacks:?}",
|
||||
// Dices(dices_allowed.div_floor(2)),
|
||||
// Dices(dices_allowed.div_ceil(2))
|
||||
// );
|
||||
// The alternative was better
|
||||
if one_attack.damage_done < sum_sub_attacks.damage_done {
|
||||
return Some(sum_sub_attacks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The alternative didn't exist or was worse
|
||||
Some(one_attack)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Consider implementing something else instead ?
|
||||
impl InnerEquipementStats {
|
||||
/// Calculate the average damage expected for a set of precisions and dices
|
||||
pub(super) fn average_damage(&self, precisions: &[Precision], list_dices: &[Dices]) -> f64 {
|
||||
precisions
|
||||
.iter()
|
||||
.map(|precision| {
|
||||
list_dices
|
||||
.iter()
|
||||
.map(|dices| binomial_dice_distribution(self, *precision, *dices))
|
||||
.sum::<f64>()
|
||||
})
|
||||
.sum::<f64>()
|
||||
/ (precisions.len() as f64 * list_dices.len() as f64)
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the binomial distribution and average the damage from there
|
||||
pub(super) fn binomial_dice_distribution(
|
||||
equipement: &InnerEquipementStats,
|
||||
precision: Precision,
|
||||
dices: Dices,
|
||||
) -> f64 {
|
||||
let p = ((*precision + equipement.precision) as f64 / 10.).clamp(0. + 1E-10, 1. - 1E-10);
|
||||
let binomial = Binomial::new(*dices, p);
|
||||
|
||||
(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 {
|
||||
(equipement.damage as f64
|
||||
+ equipement.p_damage as f64
|
||||
* (target_dices
|
||||
.clamp(0, equipement.rmax as usize)
|
||||
.saturating_sub(1)) as f64)
|
||||
* probability_success
|
||||
} else {
|
||||
0.
|
||||
}
|
||||
})
|
||||
.fold(0., |a, b| a + b)
|
||||
}
|
||||
|
||||
// TODO: Change this to be a thin type and replace enum by new(nb) et high(), med() etc..
|
||||
// : And add a way to do ranges easily
|
||||
/// The precision infered for a damage calculation event
|
||||
#[derive(EnumIter, Clone, Copy, Display)]
|
||||
#[allow(clippy::missing_docs_in_private_items)]
|
||||
pub enum FixedPrecision {
|
||||
High,
|
||||
Medium,
|
||||
Low,
|
||||
}
|
||||
|
||||
/// Thin representation of Precision to avoid mistakes
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Precision(pub i8);
|
||||
|
||||
/// Thin representation of Dices to avoid mistakes
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Dices(pub 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 {
|
||||
<i8 as Display>::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 {
|
||||
<usize as Display>::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 FixedDiceNumber {
|
||||
Many,
|
||||
Average,
|
||||
Few,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::weaponry_structs::equipement_structs::InnerEquipementStats;
|
||||
|
||||
use super::{BestDamageScenarios, Dices, Precision};
|
||||
|
||||
fn new_inner_eq() -> InnerEquipementStats {
|
||||
InnerEquipementStats {
|
||||
damage: 10,
|
||||
p_damage: 2,
|
||||
precision: 0,
|
||||
rmax: 3,
|
||||
armor_break: 2,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! assert_close {
|
||||
($a:expr, $b:expr) => {
|
||||
assert!(($a - $b).abs() < 1E-3);
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_binomial() {
|
||||
let eq = new_inner_eq();
|
||||
assert_close!(eq.average_damage(&[Precision(9)], &[Dices(9)]), 13.99995162);
|
||||
assert_close!(eq.average_damage(&[Precision(4)], &[Dices(4)]), 10.1119999);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_damage_optimisation() {
|
||||
let eq = InnerEquipementStats {
|
||||
damage: 10,
|
||||
p_damage: 2,
|
||||
precision: 0,
|
||||
rmax: 3,
|
||||
action_cost: 1,
|
||||
..Default::default()
|
||||
};
|
||||
let eq2 = InnerEquipementStats {
|
||||
damage: 10,
|
||||
p_damage: 2,
|
||||
precision: 0,
|
||||
rmax: 3,
|
||||
action_cost: 2,
|
||||
..Default::default()
|
||||
};
|
||||
let eq3 = InnerEquipementStats {
|
||||
damage: 10,
|
||||
p_damage: 2,
|
||||
precision: 0,
|
||||
rmax: 3,
|
||||
action_cost: 2,
|
||||
reload_cost: Some(2),
|
||||
..Default::default()
|
||||
};
|
||||
let known_data = &[
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq, 2, 1, Dices(3), Precision(10)),
|
||||
14.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq, 2, 2, Dices(3), Precision(10)),
|
||||
22.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq, 3, 3, Dices(3), Precision(10)),
|
||||
30.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq, 3, 3, Dices(6), Precision(10)),
|
||||
36.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq, 3, 3, Dices(6), Precision(10)),
|
||||
36.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq2, 1, 1, Dices(3), Precision(10)),
|
||||
0.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq2, 2, 1, Dices(3), Precision(10)),
|
||||
14.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq2, 4, 2, Dices(3), Precision(10)),
|
||||
22.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq2, 5, 3, Dices(6), Precision(10)),
|
||||
28.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq2, 6, 3, Dices(6), Precision(10)),
|
||||
36.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq3, 2, 1, Dices(3), Precision(10)),
|
||||
14.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq3, 4, 2, Dices(3), Precision(10)),
|
||||
14.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq3, 6, 2, Dices(3), Precision(10)),
|
||||
22.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq3, 6, 3, Dices(6), Precision(10)),
|
||||
28.,
|
||||
),
|
||||
(
|
||||
BestDamageScenarios::from_action_constraints(&eq3, 10, 3, Dices(6), Precision(10)),
|
||||
36.,
|
||||
),
|
||||
];
|
||||
known_data.iter().for_each(|(val, res)| {
|
||||
println!("{:?} ~= {res}", val.clone().unwrap_or_default());
|
||||
assert_close!(val.clone().map_or(0., |v| v.damage_done), res);
|
||||
});
|
||||
// todo!()
|
||||
}
|
||||
}
|
||||
|
|
@ -219,7 +219,7 @@ impl Default for DatabaseEquipementStats {
|
|||
#[doc(hidden)]
|
||||
macro_rules! r {
|
||||
() => {
|
||||
fastrand::i8(1..i8::MAX)
|
||||
fastrand::i8(1..20)
|
||||
};
|
||||
}
|
||||
// TODO: Make this a test requirement or hide it behind a profile
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
// static GLOBAL: Jemalloc = Jemalloc;
|
||||
|
||||
mod equipement_advanced_repr;
|
||||
mod equipement_advanced_tools;
|
||||
mod equipement_fields;
|
||||
mod equipement_impl_advanced;
|
||||
mod equipement_impl_basics;
|
||||
|
|
|
|||
Loading…
Reference in New Issue