ravenous-tools/weaponry/src/weaponry_structs/equipement_advanced_repr.rs

308 lines
11 KiB
Rust

// Workspace: weaponry
// File: weaponry/src/weaponry_structs/equipement_advanced_repr.rs
//! Give an advanced representation of the equipement, infering more fields (such a dpt, max_dps etc...)
//!
//! The generation of this fields is primordial to allow post filtering and visualisation
//!
//! Note that while ALL fields and representation will likely be backed into a megablob,
//! nothing prevents us to implement the trait for other objects and avoid massive computation
//! of all the fields even when the user will not be interested in them
#![allow(dead_code)]
use crate::weaponry_structs::equipement_thin_types::Dices;
use crate::weaponry_structs::equipement_thin_types::Precision;
use crate::{
add_field_to_chart,
error::FatalEquipementError,
export::initial_chart,
interfaces::{RecipeCollection, RefinedList, ToAnalysis, ToTex},
weaponry_structs::{
Equipement, InitialEquipementList, equipement_structs::InnerEquipementStats,
},
};
use core::f64;
use std::cmp::Ordering;
use charming::Chart;
use serde::Serialize;
use super::equipement_advanced_tools::{
BestDamageScenarios, get_damage_with_simulation, get_sustained_damage,
};
use super::equipement_thin_types::Actions;
use super::equipement_thin_types::Attacks;
/// The final representation of the equipement inplementing all the fields needed for processing
// TODO: Add a public getter for the inner data
pub struct RefinedEquipement {
/// The original inner stats
///
/// This structs takes ownership of the inner data
// #[serde(skip_deserializing)]
pub inner: InnerEquipementStats,
/// Advanced stats
// #[serde(flatten)]
pub advanced: AdvancedStats,
}
impl Serialize for RefinedEquipement {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.advanced.serialize(serializer)
}
}
impl From<Equipement> for RefinedEquipement {
fn from(value: Equipement) -> Self {
Self {
advanced: AdvancedStats::from(&value.stats),
inner: value.stats,
}
}
}
/// The complete equipement list, refined with all advanced stats
pub struct RefinedEquipementList {
/// Inner list of Refined Equipements
// TODO: Add a public getter in on of the traits
pub inner: Vec<RefinedEquipement>,
}
// Just a checkmark
impl RefinedList for RefinedEquipementList {}
impl ToTex for RefinedEquipementList {
fn to_tex(&self) -> String {
todo!()
}
}
#[macro_export]
/// A simple macro to sort the inner advanced field by a specific field
// TODO[6] find a way to not have to import the refinedequipement
macro_rules! sort_by {
($field:ident) => {
|a: &RefinedEquipement, b: &RefinedEquipement| {
a.advanced.$field.total_cmp(&b.advanced.$field)
}
};
}
#[macro_export]
/// A simple macro to sort the inner advanced field by a specific field
// TODO[6] find a way to not have to import the refinedequipement
macro_rules! sort_by_cat_then {
($field:ident) => {
|a, b| match a.inner.id[0].cmp(&b.inner.id[0]) {
std::cmp::Ordering::Equal => a.advanced.$field.total_cmp(&b.advanced.$field),
ord => ord,
}
};
}
impl ToAnalysis for RefinedEquipementList {
type InnerData = RefinedEquipement;
fn inner_sort<T>(&mut self, closure: T)
where
T: FnMut(&RefinedEquipement, &RefinedEquipement) -> Ordering,
{
self.inner.sort_by(closure)
}
fn inner_filter<T>(self, closure: T) -> Self
where
T: FnMut(&RefinedEquipement) -> bool,
{
Self {
inner: self.inner.into_iter().filter(closure).collect::<Vec<_>>(),
}
}
// TODO[3]: Consider wether the naming should be a parameter,
// : Consider wether the closure should be a parameter
// or a config should be given or if the scenario should be fed again the Refined and ...
fn write_html(&self, save_path: &str) -> Result<(), FatalEquipementError> {
let mut renderer = charming::HtmlRenderer::new("Ravenous Balancing", 4000, 2000)
.theme(charming::theme::Theme::Dark);
let initial_chart: Chart = initial_chart(&format!(
"Damage for all cross-gen products ({} Elements)",
self.inner.len()
));
macro_rules! round {
($a:expr) => {
($a * 100.).round() / 100.
};
}
add_field_to_chart!(
self,
initial_chart,
(
index # &String,
max_damage_per_attack # f64,
armor_break_per_attack # f64,
expected_armor_break_per_attack # f64,
avg_damage_2a_safe # f64,
avg_damage_single_huge_hit # f64,
// speed # f64,
variance # f64,
variance_pct # f64,
scenario_damage # f64,
// defensive # f64,
earlygame_sustained_damage_per_turn # f64,
midgame_sustained_damage_per_turn # f64,
endgame_sustained_damage_per_turn # f64,
),
|ref_equipement| {
(
&ref_equipement.advanced.display_name,
round!(ref_equipement.advanced.max_damage_per_attack ),
round!(ref_equipement.advanced.armor_break_per_attack ),
round!(ref_equipement.advanced.expected_armor_break_per_attack ),
round!(ref_equipement.advanced.avg_damage_2a_safe ),
round!(ref_equipement.advanced.avg_damage_single_huge_hit ),
// round!(ref_equipement.advanced.speed),
round!(ref_equipement.advanced.variance),
round!(ref_equipement.advanced.variance_pct ),
round!(ref_equipement.advanced.scenario_damage ),
// round!(ref_equipement.advanced.defensive ),
round!(ref_equipement.advanced.earlygame_sustained_damage_per_turn ),
round!(ref_equipement.advanced.midgame_sustained_damage_per_turn ),
round!(ref_equipement.advanced.endgame_sustained_damage_per_turn ),
)
}
);
renderer.save(&initial_chart, format!("{save_path}.html"))?;
Ok(())
}
}
impl From<InitialEquipementList> for RefinedEquipementList {
fn from(value: InitialEquipementList) -> Self {
Self {
inner: value
.inner
.into_iter()
.map(RefinedEquipement::from)
.collect(),
}
}
}
// #[cfg(test)] // Used in exemples
impl Default for RefinedEquipementList {
fn default() -> Self {
let eq = InitialEquipementList::from_vectored(vec![Equipement::default(); 10]);
RefinedEquipementList::from(eq)
}
}
/// The list of advanced stats infered from an Equipement
// TODO: The two philosophies to drop the old inner or convert the useful fields are considered right now
// TODO: This whole public might be overkill ? A good inner view or scenario could implement what is needed
#[derive(Serialize)]
pub struct AdvancedStats {
/// A simple name created by merging all components
pub display_name: String,
/// The absolute maximum damage done with each attack
pub max_damage_per_attack: f64,
// /// The absolute maximum damage done every turn (with two attacks)
// pub max_damage_per_turn: f64,
/// The armor break per attack
pub armor_break_per_attack: f64,
// This category of every result, using this would be better than id shenenigans
// pub category: Option<WeaponMacroCategory>,
/// Armor break factoring in the damage output:
/// While a weapon might have high AB, not having any damage will seriously hinder that AB
pub expected_armor_break_per_attack: f64,
/// Dps with two actions and 5 dices over a variety of scenarios
pub avg_damage_2a_safe: f64,
/// Dps with four actions and 11 dices over a variety of scenarios
pub avg_damage_single_huge_hit: f64,
/// The speed given by the weapon
pub speed: f64,
/// The damage obtained after a 10 turn pseudo scenario
pub scenario_damage: f64,
/// A defensive metric giving the best defense bonus for the weapon
pub defensive: f64,
/// The sustained damage of an earlygame character, normalized over 10 turns
pub earlygame_sustained_damage_per_turn: f64,
/// The sustained damage of an earlygame character, normalized over 10 turns
pub midgame_sustained_damage_per_turn: f64,
/// The sustained damage of an endgame character, normalized over 10 turns
pub endgame_sustained_damage_per_turn: f64,
/// The damage variance between the base damage and the potential damage
pub variance: f64,
/// This same variance as a percent
pub variance_pct: f64,
// QOL Metrics
}
impl From<&InnerEquipementStats> for AdvancedStats {
fn from(value: &InnerEquipementStats) -> Self {
let max_damage_per_attack = (value.damage + value.p_damage * (value.rmax)) as f64;
let armor_break_per_attack =
(value.armor_break as f64 + value.antimaterial as f64).min(max_damage_per_attack);
let variance = (value.p_damage * value.rmax) as f64;
Self {
display_name: value.name.join(" "),
max_damage_per_attack,
speed: value.speed.into(),
armor_break_per_attack,
// Adds the base strength, substracts the expected protection
expected_armor_break_per_attack: (max_damage_per_attack + 6. - 8.)
.min(armor_break_per_attack)
* 2.
/ value.action_cost as f64,
scenario_damage: get_damage_with_simulation(value, Actions(4), Attacks(2)),
defensive: (value.parry as f64).max(value.dodge.into()),
variance,
variance_pct: variance / value.damage as f64,
avg_damage_2a_safe: BestDamageScenarios::from_action_constraints(
value,
Actions(2),
Attacks(2),
Dices(4),
Precision(8),
)
.map(|f| *f)
.unwrap_or_default(),
avg_damage_single_huge_hit: BestDamageScenarios::from_action_constraints(
value,
Actions(2),
Attacks(2),
Dices(8),
Precision(9),
)
.map(|f| *f)
.unwrap_or_default(),
earlygame_sustained_damage_per_turn: get_sustained_damage(
value,
Actions(4),
Attacks(2),
Dices(9),
Precision(8),
),
midgame_sustained_damage_per_turn: get_sustained_damage(
value,
Actions(5),
Attacks(3),
Dices(11),
Precision(9),
),
endgame_sustained_damage_per_turn: get_sustained_damage(
value,
Actions(6),
Attacks(3),
Dices(13),
Precision(10),
),
}
}
}
// mini_scenario_damage: (value.damage + value.p_damage > 0)
// .then(|| get_sustained_damage(value))
// .unwrap_or_default(),