Adds binomial distribution

This commit is contained in:
Dandaedre 2026-07-24 07:19:41 +02:00
parent a0f05fb638
commit 886f139bbb
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
6 changed files with 71 additions and 25 deletions

View File

@ -62,3 +62,4 @@ rusqlite = { version = "0.40.1" } # , features = ["bundled"]
serde_rusqlite = "0.43.0"
handlebars = "6.4.3"
strum = { version = "0.28.0", features = ["derive"] }
probability = "0.20.3"

View File

@ -15,6 +15,7 @@ serde_rusqlite = {workspace = true}
serde_json = {workspace = true}
handlebars = {workspace = true}
strum = {workspace = true}
probability = {workspace = true}
comfy-table = "7.2.2"
colored = "3.1.1"

View File

@ -9,6 +9,8 @@ use crate::{
monster_structs::PlayerBalancing,
};
use more_fields::FieldsAsSlice;
use probability::distribution::Discrete;
use probability::prelude::Binomial;
use std::cmp::{max, min};
pub mod monster_analysis_structs;
@ -32,10 +34,12 @@ pub fn show_balancing_data(
.into_iter()
.filter(|v| &v.profile_hse == player_level)
.collect::<Vec<PlayerBalancing>>();
// TODO: This doen't dynamically select profiles !
let profile_a = &available_profiles[0];
let attacks: Vec<monster_analysis_structs::MonsterBalancingAttack> = creature.into();
let table = attacks.to_table();
println!("{table}");
// let table = attacks.to_table();
// println!("{table}");
let mut res = Vec::new();
for attack in attacks {
@ -68,25 +72,42 @@ pub fn attack_player(
let threshold = match mode {
DefenseMode::Best => max(attack.threshold_parry, attack.threshold_dodge),
DefenseMode::Worst => max(attack.threshold_parry, attack.threshold_dodge),
};
let rem_s = attack.successes - thld(victim.successes_def, threshold);
} + victim.specialty_def;
let mut damage_incoming = how_much_with_succeses(&attack.damage, rem_s);
let mut effective_damage = (damage_incoming - victim.protection - victim.strength).max(0);
let mut armor_loss = (damage_incoming - victim.protection).clamp(0, attack.armor_break);
if rem_s < 0 {
damage_incoming = 0;
effective_damage = 0;
armor_loss = 0;
}
let bin = binomial_dice_distribution(threshold, victim.successes_def);
// let successes_left = attack.successes - thld(victim.successes_def, threshold);
let avg_successes_left: f64 = bin
.iter()
.map(|(dice, p)| (attack.successes - dice) as f64 * p)
.sum::<f64>()
/ bin.len() as f64;
let avg_damage_incomming: f64 = bin
.iter()
.map(|(dices, p)| how_much_with_succeses(&attack.damage, (attack.successes - dices)) as f64)
.sum::<f64>()
/ bin.len() as f64;
// println!(
// "{}{:.2?}{avg_successes_left:.2} {avg_damage_incomming}",
// attack.name, bin
// );
// let mut damage_incoming = how_much_with_succeses(&attack.damage, successes_left);
let mut damage_incoming = avg_damage_incomming;
let mut damage_tanked =
(damage_incoming - (victim.protection + victim.strength) as f64).max(0.0);
let mut armor_loss =
(damage_incoming - victim.protection as f64).clamp(0.0, attack.armor_break as f64);
// if avg_successes_left < 0.0 {
// damage_incoming = 0.0;
// damage_tanked = 0.0;
// armor_loss = 0.0;
// }
let report = DamageReport {
damage_incoming,
damage_tanked: effective_damage,
damage_tanked,
armor_loss,
successes_left: rem_s,
damage_category: DamageCategory::from_percent(
effective_damage as f64 / victim.health as f64,
),
successes_left: avg_successes_left,
damage_category: DamageCategory::from_percent(damage_tanked as f64 / victim.health as f64),
armor_expected: attack.target_ab,
name: attack.name.clone(),
damage_expected: attack.target_damage,
@ -102,21 +123,38 @@ pub fn attack_player(
Some(report)
}
// Binomial damage distribution
// As a (0s, p), (1s, p) etc
// can be used to calc avg damage
// with avg(dmg_with_successes(i)*j)
/// Calculate the binomial distribution and average the damage from there
pub(super) fn binomial_dice_distribution(threshold: i64, dices: i64) -> Vec<(i64, f64)> {
let p = ((threshold as f64) / 10.).clamp(0. + 1E-10, 1. - 1E-10);
let binomial = Binomial::new(dices as usize, p);
(0..(dices + 1) as usize)
.map(|target_dices| {
let probability_success = binomial.mass(target_dices);
(target_dices as i64, probability_success)
})
.collect::<Vec<(i64, f64)>>()
}
#[derive(Debug, FieldsAsSlice)]
pub struct DamageReport {
pub name: String,
#[INTEGER(name = "dmg_inc")]
pub damage_incoming: i64,
pub damage_incoming: f64,
#[INTEGER(name = "r_left")]
pub successes_left: i64,
pub successes_left: f64,
#[INTEGER(name = "dmg_tak")]
pub damage_tanked: i64,
pub damage_tanked: f64,
#[TEXT(name = "dmg_cat")]
pub damage_category: DamageCategory,
#[TEXT(name = "dmg_expt")]
pub damage_expected: DamageCategory,
#[INTEGER(name = "arm_loss")]
pub armor_loss: i64,
pub armor_loss: f64,
#[TEXT(name = "arm_cat")]
pub armor_category: ArmorBreakCategory,
#[TEXT(name = "arm_expt")]
@ -156,6 +194,6 @@ fn how_much_with_succeses(damage: &[i64], succeses: i64) -> i64 {
if succeses <= 0 {
0
} else {
damage[min(succeses, (damage.len() - 1) as i64) as usize]
damage[(succeses - 1).clamp(0_i64, damage.len() as i64 - 1) as usize]
}
}

View File

@ -75,9 +75,9 @@ impl ToRow for DamageReport {
vec![
self.name.to_cell().fg(Color::Green),
format!("{} [{}r]", self.damage_incoming, self.attack_successes,).to_cell(),
format!("{} [>{}]", self.successes_left, self.threshold)
format!("{:.1} [>{:.1}]", self.successes_left, self.threshold)
.to_cell()
.fg(if self.successes_left > 0 {
.fg(if self.successes_left > 0.0 {
Color::Blue
} else {
Color::Red

View File

@ -468,6 +468,12 @@ pub struct PlayerBalancing {
#[INTEGER]
/// Current health points.
pub health: i64,
#[INTEGER]
/// Current precision.
pub precision: i64,
#[INTEGER]
/// Current deffensive specialty.
pub specialty_def: i64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default, EnumString)]

View File

@ -11,7 +11,7 @@ regex = {workspace = true}
csv = {workspace = true}
serde = {workspace = true}
comfy-table = "7.2.2"
probability = "0.20.3"
probability = {workspace = true}
rusqlite = {workspace = true}
serde_rusqlite = {workspace = true}
serde_json = {workspace = true}