From c91ca2f26f7420a42b9660e9c8a4494bfaa9816d Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 24 Jul 2026 21:07:29 +0200 Subject: [PATCH] More balancing and refactor --- crates/monsters/src/monster_analysis.rs | 286 +++++------------- .../src/monster_analysis/attack_analysis.rs | 148 +++++++++ .../monsters/src/monster_analysis/helpers.rs | 33 ++ .../monster_analysis_structs.rs | 29 +- crates/monsters/src/monster_render.rs | 3 +- ravenous_databases | 2 +- 6 files changed, 294 insertions(+), 207 deletions(-) create mode 100644 crates/monsters/src/monster_analysis/attack_analysis.rs create mode 100644 crates/monsters/src/monster_analysis/helpers.rs diff --git a/crates/monsters/src/monster_analysis.rs b/crates/monsters/src/monster_analysis.rs index 2927870..00fee82 100644 --- a/crates/monsters/src/monster_analysis.rs +++ b/crates/monsters/src/monster_analysis.rs @@ -1,20 +1,19 @@ //! Monster export module #![allow(dead_code)] +use comfy_table::{Cell, ContentArrangement, Table, modifiers, presets}; +use more_fields::FieldsAsSlice; + use crate::{ Config, Connection, MonsterConfig, - monster_analysis::monster_analysis_structs::{ArmorBreakCategory, DamageCategory}, + monster_analysis::monster_analysis_structs::DefenseMode, monster_database::{fetch_all_creature_data, fetch_all_player_data}, - monster_render::ToTable, - monster_structs::{PlayerBalancing, ProfileStance}, + monster_render::{ToCell, ToTable}, + monster_structs::{FullMonster, PlayerBalancing, ProfileHSE, ProfileStance}, }; -use more_fields::FieldsAsSlice; -use probability::{distribution::Discrete, prelude::Binomial}; -use std::{ - cmp::{max, min}, - collections::HashMap, -}; -use strum::Display; +pub mod attack_analysis; +mod helpers; +use std::collections::HashMap; pub mod monster_analysis_structs; /// Shows balancing information for monsters and players. @@ -43,15 +42,84 @@ pub fn show_balancing_data( .map(|profile| (profile.profile_stance, profile)) .collect(); - display_monster_attacks(args, creature, profiles); + display_player_attacks(args, &creature, &profiles); + display_monster_attacks(args, creature, &profiles); } Ok(()) } +fn display_player_attacks( + args: &MonsterConfig, + creature: &FullMonster, + profiles: &HashMap, +) -> () { + let r = DefenseReport { + id: creature.balancing.creature_id, + name: creature.name.clone(), + profile_hse: creature.balancing.profile_hse.clone(), + time_to_p2: creature.balancing.time_to_p2, + time_to_armorbreak: creature.balancing.time_to_armorbreak, + time_to_first_kill: creature.balancing.time_to_first_kill, + monster_groupsize: creature.balancing.monster_groupsize, + player_groupsize: creature.balancing.player_groupsize, + }; + r.to_table(); + println!("{}", r.to_table()); +} + +impl ToTable for DefenseReport { + fn to_table(&self) -> comfy_table::Table { + let mut table = Table::new(); + table + .load_preset(presets::UTF8_FULL_CONDENSED) + .apply_modifier(modifiers::UTF8_ROUND_CORNERS) + .set_content_arrangement(ContentArrangement::Dynamic) + .set_width(120) + .set_header( + DefenseReport::attributes_slice() + .iter() + .map(|(a, _b)| Cell::new(a)), + ); + let row: Vec = vec![ + format!("{:?}", self.id).to_cell(), + format!("{:?}", self.name).to_cell(), + format!("{:?}", self.profile_hse).to_cell(), + format!("{:?}", self.time_to_p2).to_cell(), + format!("{:?}", self.time_to_armorbreak).to_cell(), + ]; + table.add_row(row); + table + } +} + +#[derive(Debug, FieldsAsSlice)] +struct DefenseReport { + #[INTEGER] + pub id: i64, + pub name: String, + /// (Fixed) HSE profile of the monster (see `ProfileHSE`). + pub profile_hse: ProfileHSE, + #[INTEGER] + /// (Goal) Turns needed to reach phase 2. + pub time_to_p2: i64, + #[INTEGER] + /// (Goal) Turns until the monster’s armor breaks. + pub time_to_armorbreak: i64, + #[INTEGER] + /// (GOAL) Avg. turns to kill a single target. + pub time_to_first_kill: i64, + #[INTEGER] + /// (FIXED) Size of a single monster group (usually one). + pub monster_groupsize: i64, + #[INTEGER] + /// (FIXED) The number of players this fight was designed for + pub player_groupsize: i64, +} + fn display_monster_attacks( args: &MonsterConfig, - creature: crate::monster_structs::FullMonster, - profiles: HashMap, + creature: FullMonster, + profiles: &HashMap, ) { let attacks: Vec = creature.into(); if args.show_profiles { @@ -61,198 +129,8 @@ fn display_monster_attacks( let mut res = Vec::new(); for attack in attacks { - let mut report = all_attacks(args, &attack, &profiles, DefenseMode::Best); + let mut report = attack_analysis::all_attacks(args, &attack, profiles, DefenseMode::Best); res.append(&mut report); } println!("{}", res.to_table()); } - -#[derive(Debug, Copy, Clone, Display)] -pub enum DefenseMode { - Best, - Worst, -} - -/// Returns the Balancing profile, and eventually the other profiles as well -pub fn all_attacks( - args: &MonsterConfig, - attack: &monster_analysis_structs::MonsterBalancingAttack, - victims: &HashMap, - mode: DefenseMode, -) -> Vec { - let mut balancing = vec![ - attack_player( - attack, - &victims[&attack.target_stance], - mode, - Showcase::Balancing, - ) - .unwrap(), - ]; - // Filter - if balancing[0].damage_category == attack.target_damage - && balancing[0].armor_category == attack.target_ab - { - return Vec::new(); - } - if !args.show_extra { - return balancing; - } - let mut v = victims - .iter() - .filter(|&(stance, _)| *stance != attack.target_stance) // keep only non‑targets - .map(|(_, victim)| attack_player(attack, victim, mode, Showcase::Display)) - .collect::>>() - .unwrap(); - - balancing.append(&mut v); - balancing -} - -pub fn attack_player( - attack: &monster_analysis_structs::MonsterBalancingAttack, - victim: &PlayerBalancing, - mode: DefenseMode, - showcase: Showcase, -) -> Option { - // How much successes left - let threshold = match mode { - DefenseMode::Best => max(attack.threshold_parry, attack.threshold_dodge), - DefenseMode::Worst => max(attack.threshold_parry, attack.threshold_dodge), - } + victim.specialty_def; - - 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::() - / 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 * p - }) - .sum::(); - // / bin.len() as f64; - - // println!( - // "{} DMG:{:?} {:.2?} Left[{avg_successes_left:.2}/{} with {}] = {avg_damage_incomming}", - // attack.name, attack.damage, bin, attack.successes, victim.successes_def - // ); - // let mut damage_incoming = how_much_with_succeses(&attack.damage, successes_left); - let damage_incoming = avg_damage_incomming; - let damage_tanked = (damage_incoming - (victim.protection + victim.strength) as f64).max(0.0); - let 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, - armor_loss, - 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, - armor_category: ArmorBreakCategory::from_percent( - armor_loss as f64 / victim.integrity as f64, - ), - player_health: victim.health, - player_integrity: victim.integrity, - threshold, - attack_successes: attack.successes, - mode, - showcase, - stance: victim.profile_stance, - }; - 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::>() -} - -#[derive(Debug, FieldsAsSlice)] -pub struct DamageReport { - pub name: String, - #[INTEGER(name = "dmg_inc")] - pub damage_incoming: f64, - #[INTEGER(name = "r_left")] - pub successes_left: f64, - #[INTEGER(name = "dmg_tak")] - 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: f64, - #[TEXT(name = "arm_cat")] - pub armor_category: ArmorBreakCategory, - #[TEXT(name = "arm_expt")] - pub armor_expected: ArmorBreakCategory, - #[SKIP] - pub player_health: i64, - #[SKIP] - pub player_integrity: i64, - #[SKIP] - pub threshold: i64, - #[SKIP] - pub mode: DefenseMode, - #[SKIP] - pub stance: ProfileStance, - #[SKIP] - pub attack_successes: i64, - #[SKIP] - pub showcase: Showcase, -} - -#[derive(Debug, Clone, Copy)] -pub enum Showcase { - Display, - Balancing, -} -impl DamageCategory { - fn from_percent(percent: f64) -> Self { - match percent { - ..0.01 => Self::None, - 0.01..0.15 => Self::Chip, - 0.15..0.5 => Self::Moderate, - 0.5..0.8 => Self::High, - 0.8..1.0 => Self::NearDeath, - 1.0..1.5 => Self::Kill, - 1.5.. => Self::Overkill, - _ => Self::None, - } - } -} - -fn thld(r_succeses: i64, threshold_parry: i64) -> i64 { - (r_succeses * min(threshold_parry, 10)) / 10 -} - -fn how_much_with_succeses(damage: &[i64], succeses: i64) -> i64 { - if succeses <= 0 { - 0 - } else { - damage[(succeses - 1).clamp(0_i64, damage.len() as i64 - 1) as usize] - } -} diff --git a/crates/monsters/src/monster_analysis/attack_analysis.rs b/crates/monsters/src/monster_analysis/attack_analysis.rs new file mode 100644 index 0000000..80e64b9 --- /dev/null +++ b/crates/monsters/src/monster_analysis/attack_analysis.rs @@ -0,0 +1,148 @@ +use crate::{ + MonsterConfig, + monster_analysis::{ + helpers, + monster_analysis_structs::{ + self, ArmorBreakCategory, DamageCategory, DefenseMode, Showcase, + }, + }, + monster_structs::{PlayerBalancing, ProfileStance}, +}; +use more_fields::FieldsAsSlice; +use std::cmp::max; +use std::collections::HashMap; + +/// Returns the Balancing profile, and eventually the other profiles as well +pub fn all_attacks( + args: &MonsterConfig, + attack: &monster_analysis_structs::MonsterBalancingAttack, + victims: &HashMap, + mode: DefenseMode, +) -> Vec { + let mut balancing = vec![ + attack_player( + attack, + &victims[&attack.target_stance], + mode, + Showcase::Balancing, + ) + .unwrap(), + ]; + // Filter + if balancing[0].damage_category == attack.target_damage + && balancing[0].armor_category == attack.target_ab + { + return Vec::new(); + } + if !args.show_extra { + return balancing; + } + let mut v = victims + .iter() + .filter(|&(stance, _)| *stance != attack.target_stance) // keep only non‑targets + .map(|(_, victim)| attack_player(attack, victim, mode, Showcase::Display)) + .collect::>>() + .unwrap(); + + balancing.append(&mut v); + balancing +} + +pub fn attack_player( + attack: &monster_analysis_structs::MonsterBalancingAttack, + victim: &PlayerBalancing, + mode: DefenseMode, + showcase: Showcase, +) -> Option { + // How much successes left + let threshold = match mode { + DefenseMode::Best => max(attack.threshold_parry, attack.threshold_dodge), + DefenseMode::Worst => max(attack.threshold_parry, attack.threshold_dodge), + } + victim.specialty_def; + + let bin = helpers::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::() + / bin.len() as f64; + let avg_damage_incomming: f64 = bin + .iter() + .map(|(dices, p)| { + helpers::how_much_with_succeses(&attack.damage, attack.successes - dices) as f64 * p + }) + .sum::(); + // / bin.len() as f64; + + // println!( + // "{} DMG:{:?} {:.2?} Left[{avg_successes_left:.2}/{} with {}] = {avg_damage_incomming}", + // attack.name, attack.damage, bin, attack.successes, victim.successes_def + // ); + // let mut damage_incoming = how_much_with_succeses(&attack.damage, successes_left); + let damage_incoming = avg_damage_incomming; + let damage_tanked = (damage_incoming - (victim.protection + victim.strength) as f64).max(0.0); + let 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, + armor_loss, + 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, + armor_category: ArmorBreakCategory::from_percent( + armor_loss as f64 / victim.integrity as f64, + ), + player_health: victim.health, + player_integrity: victim.integrity, + threshold, + attack_successes: attack.successes, + mode, + showcase, + stance: victim.profile_stance, + }; + Some(report) +} + +#[derive(Debug, FieldsAsSlice)] +pub struct DamageReport { + pub name: String, + #[INTEGER(name = "dmg_inc")] + pub damage_incoming: f64, + #[INTEGER(name = "r_left")] + pub successes_left: f64, + #[INTEGER(name = "dmg_tak")] + 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: f64, + #[TEXT(name = "arm_cat")] + pub armor_category: ArmorBreakCategory, + #[TEXT(name = "arm_expt")] + pub armor_expected: ArmorBreakCategory, + #[SKIP] + pub player_health: i64, + #[SKIP] + pub player_integrity: i64, + #[SKIP] + pub threshold: i64, + #[SKIP] + pub mode: DefenseMode, + #[SKIP] + pub stance: ProfileStance, + #[SKIP] + pub attack_successes: i64, + #[SKIP] + pub showcase: monster_analysis_structs::Showcase, +} diff --git a/crates/monsters/src/monster_analysis/helpers.rs b/crates/monsters/src/monster_analysis/helpers.rs new file mode 100644 index 0000000..5281fba --- /dev/null +++ b/crates/monsters/src/monster_analysis/helpers.rs @@ -0,0 +1,33 @@ +use std::cmp::min; + +use probability::distribution::Discrete; +use probability::prelude::Binomial; + +// 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::>() +} + +pub fn thld(r_succeses: i64, threshold_parry: i64) -> i64 { + (r_succeses * min(threshold_parry, 10)) / 10 +} + +pub fn how_much_with_succeses(damage: &[i64], succeses: i64) -> i64 { + if succeses <= 0 { + 0 + } else { + damage[(succeses - 1).clamp(0_i64, damage.len() as i64 - 1) as usize] + } +} diff --git a/crates/monsters/src/monster_analysis/monster_analysis_structs.rs b/crates/monsters/src/monster_analysis/monster_analysis_structs.rs index 0fb0cca..a2c9171 100644 --- a/crates/monsters/src/monster_analysis/monster_analysis_structs.rs +++ b/crates/monsters/src/monster_analysis/monster_analysis_structs.rs @@ -1,7 +1,7 @@ use crate::monster_structs::{FullMonster, PlayerBalancing, ProfileStance}; use more_fields::FieldsAsSlice; use std::str::FromStr; -use strum::EnumString; +use strum::{Display, EnumString}; impl From for Vec { fn from(creature: FullMonster) -> Self { @@ -119,6 +119,18 @@ pub enum ArmorBreakCategory { Overkill, } +#[derive(Debug, Clone, Copy)] +pub enum Showcase { + Display, + Balancing, +} + +#[derive(Debug, Copy, Clone, Display)] +pub enum DefenseMode { + Best, + Worst, +} + impl ArmorBreakCategory { pub fn from_percent(value: f64) -> Self { match value { @@ -196,3 +208,18 @@ pub(crate) fn from_opt_i64(val: Option<&i64>) -> i64 { None => 0, } } + +impl DamageCategory { + pub fn from_percent(percent: f64) -> Self { + match percent { + ..0.01 => Self::None, + 0.01..0.15 => Self::Chip, + 0.15..0.5 => Self::Moderate, + 0.5..0.8 => Self::High, + 0.8..1.0 => Self::NearDeath, + 1.0..1.5 => Self::Kill, + 1.5.. => Self::Overkill, + _ => Self::None, + } + } +} diff --git a/crates/monsters/src/monster_render.rs b/crates/monsters/src/monster_render.rs index eb55b10..83021f0 100644 --- a/crates/monsters/src/monster_render.rs +++ b/crates/monsters/src/monster_render.rs @@ -1,8 +1,9 @@ +use crate::monster_analysis::monster_analysis_structs::Showcase; use comfy_table::{Color, Table, *}; use crate::{ monster_analysis::{ - DamageReport, Showcase, + attack_analysis::DamageReport, monster_analysis_structs::{ArmorBreakCategory, DamageCategory, MonsterBalancingAttack}, }, monster_structs::{PlayerBalancing, ProfileHSE, ProfileStance}, diff --git a/ravenous_databases b/ravenous_databases index 521c7f3..1048cf2 160000 --- a/ravenous_databases +++ b/ravenous_databases @@ -1 +1 @@ -Subproject commit 521c7f3cad4417ec340b6a4d0dbaea92944daa4c +Subproject commit 1048cf2617201894af650d88406ffa49578d7b11