From 2c44cdd817e590e1a11cbc67dd61602c2fa4f4ca Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 11 Feb 2025 23:34:51 +0100 Subject: [PATCH 1/2] Switched to a blanket implementation --- .gitignore | 1 + Cargo.toml | 2 + weaponry/benches/iter_creation.rs | 45 ++-- weaponry/src/interfaces.rs | 88 ++++++-- .../weaponry_structs/equipement_impl_logic.rs | 202 +++++++++++------- .../weaponry_structs/equipement_structs.rs | 11 +- weaponry/src/weaponry_structs/mod.rs | 13 +- 7 files changed, 240 insertions(+), 122 deletions(-) diff --git a/.gitignore b/.gitignore index 6126be9..a47be09 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ Cargo.lock /target .cargo/ +*.tar.gz diff --git a/Cargo.toml b/Cargo.toml index 46bf157..f6e0776 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,3 +28,5 @@ lto = false codegen-units = 256 incremental = true panic = "abort" + + diff --git a/weaponry/benches/iter_creation.rs b/weaponry/benches/iter_creation.rs index 0d5511f..75c07b9 100644 --- a/weaponry/benches/iter_creation.rs +++ b/weaponry/benches/iter_creation.rs @@ -2,37 +2,28 @@ use criterion::{Criterion, criterion_group, criterion_main}; use std::{hint::black_box, time::Duration}; use weaponry::{ interfaces::RecipeCollection, - weaponry_structs::{ - Equipement, FinalEquipementList, equipement_structs::FinalEquipementListPar, - }, + weaponry_structs::{Equipement, FinalEquipementList}, }; fn criterion_benchmark(c: &mut Criterion) { - let steps = [ + let blk_steps = [ FinalEquipementList::from_vectored(vec![Equipement::default(); 100]), FinalEquipementList::from_vectored(vec![Equipement::default(); 100]), FinalEquipementList::from_vectored(vec![Equipement::default(); 100]), ]; - let par_steps = [ - FinalEquipementListPar::from_vectored(vec![Equipement::default(); 100]), - FinalEquipementListPar::from_vectored(vec![Equipement::default(); 100]), - FinalEquipementListPar::from_vectored(vec![Equipement::default(); 100]), - ]; c.bench_function("creating 100 Elements", |b| { - b.iter(|| FinalEquipementList::from_vectored(black_box(vec![Equipement::default(); 100]))) - }); - c.bench_function("IterGen: 100x100x100", |b| { b.iter(|| { - let mut s = steps[0].clone(); - s.combine_all(steps[1].clone()); - s.combine_all(steps[2].clone()); + FinalEquipementList::from_vectored(black_box(vec![ + black_box(Equipement::default()); + 100 + ])) }) }); - c.bench_function("IterGen: 100x100x100", |b| { + c.bench_function("IterGenBlanket: 100x100x100", |b| { b.iter(|| { - let mut s = par_steps[0].clone(); - s.combine_all(par_steps[1].clone()); - s.combine_all(par_steps[2].clone()); + let mut s = blk_steps[0].clone(); + s.combine_all_by_blanket(black_box(&blk_steps[1])); + s.combine_all_by_blanket(black_box(&blk_steps[2])); }) }); // c.bench_function("IterGen: 100x100x100x100", |b| { @@ -43,14 +34,14 @@ fn criterion_benchmark(c: &mut Criterion) { // s.combine_all(steps[2].clone()); // }) // }); - c.bench_function("IterGenPar: 100x100x100x100", |b| { - b.iter(|| { - let mut s = par_steps[0].clone(); - s.combine_all(par_steps[1].clone()); - s.combine_all(par_steps[2].clone()); - s.combine_all(par_steps[2].clone()); - }) - }); + // c.bench_function("IterGenPar: 100x100x100x100", |b| { + // b.iter(|| { + // let mut s = par_steps[0].clone(); + // s.combine_all(par_steps[1].clone()); + // s.combine_all(par_steps[2].clone()); + // s.combine_all(par_steps[2].clone()); + // }) + // }); } criterion_group! { diff --git a/weaponry/src/interfaces.rs b/weaponry/src/interfaces.rs index bb75100..eb18263 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -18,14 +18,62 @@ pub trait RecipeCollection { /// The type of the inner recipes, this type must be combinable type Recipes: Combineable; - /// Combine all current recipies with all the parts given - fn combine_all(&mut self, other: G) - where - G: RecipeCollection + std::marker::Sync; + // /// Combine all current recipies with all the parts given + // #[deprecated] + // fn combine_all(&mut self, other: G) + // where + // G: RecipeCollection + std::marker::Sync; + + /// Do the parrallel combination all inside + fn combine_all_by_blanket(&mut self, other: &G) + where + G: RecipeCollection, + { + self.assign_inner( + self.ref_inner() + .iter() + .flat_map(|inner_recipes| { + other + .ref_inner() + .iter() + .map(|external_recipes: &Self::Recipes| { + inner_recipes.add_ref_to_self(external_recipes) + }) + .collect::>() + }) + .collect::>(), + ); + } + + // fn combine_all_by_ref(&mut self, other: G) + // where + // G: RecipeCollection + std::marker::Sync, + // { + // let mut future_receipes = vec![self.ref_inner().clone(); other.ref_inner().len()]; + // future_receipes + // .par_iter_mut() + // .zip(other.ref_inner()) + // .for_each(move |(a, b)| { + // a.par_iter_mut().for_each(move |e| { + // e.add_to_self(b); + // }); + // }); + + // self.inner = future_receipes.into_par_iter().flatten().collect(); + // } + + /// Assign the inner value from an owned value, used for blanket implementations + fn assign_inner(&mut self, inner: Vec); + + /// Gives access to a reference of the inner list + /// + /// This allows to only clone when needed + fn ref_inner(&self) -> &Vec; + + // /// Gives access to a mutable reference of the inner list + // #[deprecated] + // fn refmut_inner(&mut self) -> &mut Vec; - /// Access the inner list - // TODO: Use iterators ? - fn inner(&self) -> Vec; // Optional filter ? /// Create a collection from a vector @@ -38,16 +86,32 @@ pub trait RecipeCollection { pub trait Combineable { /// The inner type of the combined elements, this type must implement Add // TODO : Make this help much more readable - type Inner: std::ops::Add; + type Inner: std::ops::Add + std::ops::AddAssign; - /// Give access to an owned underlying type that can be combined - // TODO: the ownership part might not be needed - fn clone_combinable_part(&self) -> Self::Inner; + /// Give access to a reference of the underlying data to be combined + /// + /// The designed combinaison method is std::ops::Add + fn combinable_part(&self) -> &Self::Inner; /// Add an element to self and return a new value /// /// This method will clone it's own combinable part - fn add_to_self(&self, rhs: T) -> Self + #[deprecated] + fn add_to_other(&self, rhs: T) -> Self + where + T: Combineable; + + /// Add an element to a mutable self + // #[deprecated(note = "Mutate in place")] + fn add_to_self(&mut self, rhs: &T) + where + T: Combineable; + + /// Add an element to self and generate a new Self + /// + /// As the signature implies, it will not mutate self not rhs and will generate a new value from the operation + /// This pushes the actual requirement to &Inner : Add<&Inner> + fn add_ref_to_self(&self, rhs: &T) -> Self where T: Combineable; } diff --git a/weaponry/src/weaponry_structs/equipement_impl_logic.rs b/weaponry/src/weaponry_structs/equipement_impl_logic.rs index f7198e9..f34fccc 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_logic.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_logic.rs @@ -2,14 +2,9 @@ // File: weaponry/src/weaponry_structs/equipement_impl_logic.rs //! Implementation of the logic traits, used for equipement generation -use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; - use crate::interfaces::{Combineable, RecipeCollection}; -use super::{ - Equipement, FinalEquipementList, SpecialCategory, - equipement_structs::{FinalEquipementListPar, InnerStats}, -}; +use super::{Equipement, FinalEquipementList, SpecialCategory, equipement_structs::InnerStats}; impl std::ops::Add for InnerStats { type Output = Self; @@ -58,6 +53,97 @@ impl std::ops::Add for InnerStats { } } } +impl std::ops::Add for &InnerStats { + type Output = InnerStats; + + fn add(self, rhs: Self) -> Self::Output { + InnerStats { + // TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless + id: self.id, + name: [self.name.clone(), rhs.name.clone()].join("+"), + rarity: self.rarity + rhs.rarity, + cost: self.cost + rhs.cost, + difficulty: self.difficulty + rhs.difficulty, + tier: self.tier + rhs.tier, + damage: self.damage + rhs.damage, + p_damage: self.p_damage + rhs.p_damage, + precision: self.precision + rhs.precision, + rmax: self.rmax + rhs.rmax, + armor_break: self.armor_break + rhs.armor_break, + armor_pierce: self.armor_pierce + rhs.armor_pierce, + antimaterial: self.antimaterial + rhs.antimaterial, + damage_type: { + match (self.damage_type.clone(), rhs.damage_type.clone()) { + (None, None) => None, + (None, Some(a)) => Some(a), + (Some(a), None) => Some(a), + (Some(mut a), Some(mut b)) => { + a.0.append(&mut b.0); + Some(a) + } + } + }, + protection: self.protection + rhs.protection, + integrity: self.integrity + rhs.integrity, + torque_two_handed: self.torque_two_handed + rhs.torque_two_handed, + torque_one_handed: self.torque_one_handed + rhs.torque_one_handed, + dodge: self.dodge + rhs.dodge, + parry: self.parry + rhs.parry, + speed: self.speed + rhs.speed, + stealth: self.stealth + rhs.stealth, + category: self.category.clone().or(rhs.category.clone()), // Can be further optimized + special: self.special.clone() + rhs.special.clone(), // Can be further optimized + action_cost: self.action_cost + rhs.action_cost, + reload_cost: self.reload_cost.or(rhs.reload_cost), + range: self.range + rhs.range, + agility_bonus: self.agility_bonus + rhs.agility_bonus, + } + } +} + +impl std::ops::AddAssign for InnerStats { + fn add_assign(&mut self, rhs: InnerStats) { + // self.id; + self.name.push('+'); + self.name.push_str(&rhs.name); + self.rarity += rhs.rarity; + self.cost += rhs.cost; + self.difficulty += rhs.difficulty; + self.tier += rhs.tier; + self.damage += rhs.damage; + self.p_damage += rhs.p_damage; + self.precision += rhs.precision; + self.rmax += rhs.rmax; + self.armor_break += rhs.armor_break; + self.armor_pierce += rhs.armor_pierce; + self.antimaterial += rhs.antimaterial; + self.damage_type = { + match (self.damage_type.clone(), rhs.damage_type.clone()) { + (None, None) => None, + (None, Some(a)) => Some(a), + (Some(a), None) => Some(a), + (Some(mut a), Some(mut b)) => { + a.0.append(&mut b.0); + Some(a) + } + } + }; + self.protection += rhs.protection; + self.integrity += rhs.integrity; + self.torque_two_handed += rhs.torque_two_handed; + self.torque_one_handed += rhs.torque_one_handed; + self.dodge += rhs.dodge; + self.parry += rhs.parry; + self.speed += rhs.speed; + self.stealth += rhs.stealth; + self.category.clone().or(rhs.category.clone()); // Can be further optimized + // self.special += rhs.special.clone(); // Can be further optimized + self.action_cost += rhs.action_cost; + self.reload_cost.or(rhs.reload_cost); + self.range += rhs.range; + self.agility_bonus += rhs.agility_bonus; + } +} impl std::ops::Add for SpecialCategory { type Output = Self; @@ -72,16 +158,33 @@ impl std::ops::Add for SpecialCategory { impl Combineable for Equipement { type Inner = InnerStats; - fn clone_combinable_part(&self) -> InnerStats { - self.stats.clone() - } - fn add_to_self(&self, rhs: T) -> Self + fn add_to_other(&self, rhs: T) -> Self where T: Combineable, { Self { - stats: self.clone_combinable_part() + rhs.clone_combinable_part(), + stats: self.combinable_part() + rhs.combinable_part(), + } + } + + fn combinable_part(&self) -> &Self::Inner { + &self.stats + } + + fn add_to_self(&mut self, rhs: &T) + where + T: Combineable, + { + self.stats += rhs.combinable_part().clone() + } + + fn add_ref_to_self(&self, rhs: &T) -> Self + where + T: Combineable, + { + Self { + stats: self.combinable_part() + rhs.combinable_part(), } } } @@ -89,58 +192,16 @@ impl Combineable for Equipement { impl RecipeCollection for FinalEquipementList { type Recipes = Equipement; - fn combine_all(&mut self, other: G) - where - G: RecipeCollection, - { - self.inner = self - .inner() - .iter() - .flat_map(|e| { - other - .inner() - .iter() - .map(|f: &Self::Recipes| e.add_to_self(f.clone())) - .collect::>() - }) - .collect(); - } - - fn inner(&self) -> Vec { - self.inner.clone() - } - fn from_vectored(list: Vec) -> Self { Self { inner: list } } -} -impl RecipeCollection for FinalEquipementListPar { - type Recipes = Equipement; - - fn combine_all(&mut self, other: G) - where - G: RecipeCollection + std::marker::Sync, - { - self.inner = self - .inner() - .par_iter() - .flat_map(|e| { - other - .inner() - .par_iter() - .map(|f: &Self::Recipes| e.add_to_self(f.clone())) - .collect::>() - }) - .collect(); + fn ref_inner(&self) -> &Vec { + &self.inner } - fn inner(&self) -> Vec { - self.inner.clone() - } - - fn from_vectored(list: Vec) -> Self { - Self { inner: list } + fn assign_inner(&mut self, inner: Vec) { + self.inner = inner; } } @@ -156,38 +217,35 @@ mod test { use pretty_assertions::assert_eq; #[test] - fn test_struct_generation() { + fn test_blanket_struct_generation() { let mut f = FinalEquipementList::from_vectored(vec![ - Equipement::newish("", 1), - Equipement::newish("", 2), + Equipement::newish("a", 1), + Equipement::newish("b", 2), ]); let g = FinalEquipementList::from_vectored(vec![ - Equipement::newish("", 4), - Equipement::newish("", 8), + Equipement::newish("c", 4), + Equipement::newish("d", 8), ]); assert_eq!( - f.inner(), - vec![Equipement::newish("", 1), Equipement::newish("", 2)] + f.ref_inner(), + &vec![Equipement::newish("a", 1), Equipement::newish("b", 2)] ); - f.combine_all(g); + f.combine_all_by_blanket(&g); assert_eq!( f, FinalEquipementList::from_vectored(vec![ - Equipement::newish("+", 5), - Equipement::newish("+", 9), - Equipement::newish("+", 6), - Equipement::newish("+", 10) + Equipement::newish("a+c", 5), + Equipement::newish("a+d", 9), + Equipement::newish("b+c", 6), + Equipement::newish("b+d", 10) ]) ); } #[test] fn test_base_operation() { - // let inner = InnerStats::default(); - // let inner2 = inner.clone() + inner.clone(); - // assert_eq!(inner, inner2); // True because of the 0 initialisation let i1 = InnerStats { rarity: 1, integrity: 2, diff --git a/weaponry/src/weaponry_structs/equipement_structs.rs b/weaponry/src/weaponry_structs/equipement_structs.rs index c73bc63..eddb28d 100644 --- a/weaponry/src/weaponry_structs/equipement_structs.rs +++ b/weaponry/src/weaponry_structs/equipement_structs.rs @@ -5,6 +5,7 @@ use struct_field_names_as_array::FieldNamesAsArray; use super::{DamageType, NatureTarget, SpecialCategory, WeaponCategory}; +// use derive_more::Add; /// The final equipement list /// @@ -16,16 +17,6 @@ pub struct FinalEquipementList { pub(super) inner: Vec, } -/// A parallel Implementation of the final equipement list -/// -/// This list only serve for benchmark purposes -#[derive(Clone)] -#[cfg_attr(test, derive(PartialEq, Debug))] -pub struct FinalEquipementListPar { - /// Inner List of final equipements - pub(super) inner: Vec, -} - /// The final equipement, obtained by combining all parts #[derive(Clone, Default)] #[cfg_attr(test, derive(PartialEq, Debug))] diff --git a/weaponry/src/weaponry_structs/mod.rs b/weaponry/src/weaponry_structs/mod.rs index 5f7d7d9..2f7ba60 100644 --- a/weaponry/src/weaponry_structs/mod.rs +++ b/weaponry/src/weaponry_structs/mod.rs @@ -2,7 +2,18 @@ // File: weaponry/src/weaponry_structs/mod.rs //! All weaponry intermod structures -#![allow(dead_code)] +// #![allow(dead_code)] +// use mimalloc::MiMalloc; + +// #[global_allocator] +// static GLOBAL: MiMalloc = MiMalloc; + +// #[cfg(not(target_env = "msvc"))] +// use tikv_jemallocator::Jemalloc; + +// #[cfg(not(target_env = "msvc"))] +// #[global_allocator] +// static GLOBAL: Jemalloc = Jemalloc; mod equipement_fields; mod equipement_impl_database; -- 2.45.1 From 9e1521e37357ee778563ebf0bd90df55288f0909 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 11 Feb 2025 23:30:27 +0100 Subject: [PATCH 2/2] Refactor of impls --- weaponry/src/interfaces.rs | 15 +- .../weaponry_structs/equipement_impl_logic.rs | 190 ++-------------- .../weaponry_structs/equipement_impl_math.rs | 208 ++++++++++++++++++ .../weaponry_structs/equipement_structs.rs | 21 +- weaponry/src/weaponry_structs/mod.rs | 1 + 5 files changed, 250 insertions(+), 185 deletions(-) create mode 100644 weaponry/src/weaponry_structs/equipement_impl_math.rs diff --git a/weaponry/src/interfaces.rs b/weaponry/src/interfaces.rs index eb18263..cfa97cc 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -29,6 +29,7 @@ pub trait RecipeCollection { where G: RecipeCollection, { + // TODO: Test Add to Self instead self.assign_inner( self.ref_inner() .iter() @@ -93,13 +94,13 @@ pub trait Combineable { /// The designed combinaison method is std::ops::Add fn combinable_part(&self) -> &Self::Inner; - /// Add an element to self and return a new value - /// - /// This method will clone it's own combinable part - #[deprecated] - fn add_to_other(&self, rhs: T) -> Self - where - T: Combineable; + // /// Add an element to self and return a new value + // /// + // /// This method will clone it's own combinable part + // #[deprecated] + // fn add_to_other(&self, rhs: T) -> Self + // where + // T: Combineable; /// Add an element to a mutable self // #[deprecated(note = "Mutate in place")] diff --git a/weaponry/src/weaponry_structs/equipement_impl_logic.rs b/weaponry/src/weaponry_structs/equipement_impl_logic.rs index f34fccc..ac25025 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_logic.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_logic.rs @@ -4,170 +4,11 @@ use crate::interfaces::{Combineable, RecipeCollection}; -use super::{Equipement, FinalEquipementList, SpecialCategory, equipement_structs::InnerStats}; - -impl std::ops::Add for InnerStats { - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output { - Self { - // TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless - id: self.id, - name: [self.name, rhs.name].join("+"), - rarity: self.rarity + rhs.rarity, - cost: self.cost + rhs.cost, - difficulty: self.difficulty + rhs.difficulty, - tier: self.tier + rhs.tier, - damage: self.damage + rhs.damage, - p_damage: self.p_damage + rhs.p_damage, - precision: self.precision + rhs.precision, - rmax: self.rmax + rhs.rmax, - armor_break: self.armor_break + rhs.armor_break, - armor_pierce: self.armor_pierce + rhs.armor_pierce, - antimaterial: self.antimaterial + rhs.antimaterial, - damage_type: { - match (self.damage_type, rhs.damage_type) { - (None, None) => None, - (None, Some(a)) => Some(a), - (Some(a), None) => Some(a), - (Some(mut a), Some(mut b)) => { - a.0.append(&mut b.0); - Some(a) - } - } - }, - protection: self.protection + rhs.protection, - integrity: self.integrity + rhs.integrity, - torque_two_handed: self.torque_two_handed + rhs.torque_two_handed, - torque_one_handed: self.torque_one_handed + rhs.torque_one_handed, - dodge: self.dodge + rhs.dodge, - parry: self.parry + rhs.parry, - speed: self.speed + rhs.speed, - stealth: self.stealth + rhs.stealth, - category: self.category.or(rhs.category), - special: self.special + rhs.special, - action_cost: self.action_cost + rhs.action_cost, - reload_cost: self.reload_cost.or(rhs.reload_cost), - range: self.range + rhs.range, - agility_bonus: self.agility_bonus + rhs.agility_bonus, - } - } -} -impl std::ops::Add for &InnerStats { - type Output = InnerStats; - - fn add(self, rhs: Self) -> Self::Output { - InnerStats { - // TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless - id: self.id, - name: [self.name.clone(), rhs.name.clone()].join("+"), - rarity: self.rarity + rhs.rarity, - cost: self.cost + rhs.cost, - difficulty: self.difficulty + rhs.difficulty, - tier: self.tier + rhs.tier, - damage: self.damage + rhs.damage, - p_damage: self.p_damage + rhs.p_damage, - precision: self.precision + rhs.precision, - rmax: self.rmax + rhs.rmax, - armor_break: self.armor_break + rhs.armor_break, - armor_pierce: self.armor_pierce + rhs.armor_pierce, - antimaterial: self.antimaterial + rhs.antimaterial, - damage_type: { - match (self.damage_type.clone(), rhs.damage_type.clone()) { - (None, None) => None, - (None, Some(a)) => Some(a), - (Some(a), None) => Some(a), - (Some(mut a), Some(mut b)) => { - a.0.append(&mut b.0); - Some(a) - } - } - }, - protection: self.protection + rhs.protection, - integrity: self.integrity + rhs.integrity, - torque_two_handed: self.torque_two_handed + rhs.torque_two_handed, - torque_one_handed: self.torque_one_handed + rhs.torque_one_handed, - dodge: self.dodge + rhs.dodge, - parry: self.parry + rhs.parry, - speed: self.speed + rhs.speed, - stealth: self.stealth + rhs.stealth, - category: self.category.clone().or(rhs.category.clone()), // Can be further optimized - special: self.special.clone() + rhs.special.clone(), // Can be further optimized - action_cost: self.action_cost + rhs.action_cost, - reload_cost: self.reload_cost.or(rhs.reload_cost), - range: self.range + rhs.range, - agility_bonus: self.agility_bonus + rhs.agility_bonus, - } - } -} - -impl std::ops::AddAssign for InnerStats { - fn add_assign(&mut self, rhs: InnerStats) { - // self.id; - self.name.push('+'); - self.name.push_str(&rhs.name); - self.rarity += rhs.rarity; - self.cost += rhs.cost; - self.difficulty += rhs.difficulty; - self.tier += rhs.tier; - self.damage += rhs.damage; - self.p_damage += rhs.p_damage; - self.precision += rhs.precision; - self.rmax += rhs.rmax; - self.armor_break += rhs.armor_break; - self.armor_pierce += rhs.armor_pierce; - self.antimaterial += rhs.antimaterial; - self.damage_type = { - match (self.damage_type.clone(), rhs.damage_type.clone()) { - (None, None) => None, - (None, Some(a)) => Some(a), - (Some(a), None) => Some(a), - (Some(mut a), Some(mut b)) => { - a.0.append(&mut b.0); - Some(a) - } - } - }; - self.protection += rhs.protection; - self.integrity += rhs.integrity; - self.torque_two_handed += rhs.torque_two_handed; - self.torque_one_handed += rhs.torque_one_handed; - self.dodge += rhs.dodge; - self.parry += rhs.parry; - self.speed += rhs.speed; - self.stealth += rhs.stealth; - self.category.clone().or(rhs.category.clone()); // Can be further optimized - // self.special += rhs.special.clone(); // Can be further optimized - self.action_cost += rhs.action_cost; - self.reload_cost.or(rhs.reload_cost); - self.range += rhs.range; - self.agility_bonus += rhs.agility_bonus; - } -} - -impl std::ops::Add for SpecialCategory { - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output { - let mut c = self.clone(); - // TODO: Overcopy, kinda meaningless optimisation tho - c.0.append(&mut rhs.clone().0); - c - } -} +use super::{Equipement, FinalEquipementList, equipement_structs::InnerStats}; impl Combineable for Equipement { type Inner = InnerStats; - fn add_to_other(&self, rhs: T) -> Self - where - T: Combineable, - { - Self { - stats: self.combinable_part() + rhs.combinable_part(), - } - } - fn combinable_part(&self) -> &Self::Inner { &self.stats } @@ -219,27 +60,27 @@ mod test { #[test] fn test_blanket_struct_generation() { let mut f = FinalEquipementList::from_vectored(vec![ - Equipement::newish("a", 1), - Equipement::newish("b", 2), + Equipement::newish(&["a"], 1), + Equipement::newish(&["b"], 2), ]); let g = FinalEquipementList::from_vectored(vec![ - Equipement::newish("c", 4), - Equipement::newish("d", 8), + Equipement::newish(&["c"], 4), + Equipement::newish(&["d"], 8), ]); assert_eq!( f.ref_inner(), - &vec![Equipement::newish("a", 1), Equipement::newish("b", 2)] + &vec![Equipement::newish(&["a"], 1), Equipement::newish(&["b"], 2)] ); f.combine_all_by_blanket(&g); assert_eq!( f, FinalEquipementList::from_vectored(vec![ - Equipement::newish("a+c", 5), - Equipement::newish("a+d", 9), - Equipement::newish("b+c", 6), - Equipement::newish("b+d", 10) + Equipement::newish(&["a", "c"], 5), + Equipement::newish(&["a", "d"], 9), + Equipement::newish(&["b", "c"], 6), + Equipement::newish(&["b", "d"], 10) ]) ); } @@ -271,7 +112,6 @@ mod test { assert_eq!( i1 + i2, InnerStats { - name: "+".into(), rarity: 1, integrity: 5, agility_bonus: 9, @@ -287,15 +127,14 @@ mod test { } ); assert_eq!( - InnerStats { + &InnerStats { damage_type: Some(DamageType(vec![ DamageMod::Secondary(PhysicalDamageType::Slash,), DamageMod::Primary(PhysicalDamageType::Blunt,), ])), ..Default::default() - } + InnerStats::default(), + } + &InnerStats::default(), InnerStats { - name: "+".into(), damage_type: Some(DamageType(vec![ DamageMod::Secondary(PhysicalDamageType::Slash,), DamageMod::Primary(PhysicalDamageType::Blunt,), @@ -313,7 +152,6 @@ mod test { ..Default::default() }, InnerStats { - name: "+".into(), damage_type: Some(DamageType(vec![ DamageMod::Secondary(PhysicalDamageType::Slash,), DamageMod::Primary(PhysicalDamageType::Blunt,), @@ -332,10 +170,10 @@ mod test { fn test_basic_logic() {} impl Equipement { - fn newish(name: &str, i: u8) -> Self { + fn newish(name: &[&str], i: u8) -> Self { Equipement { stats: InnerStats { - name: name.to_owned(), + name: name.iter().map(|s| s.to_string()).collect(), rarity: i, ..Default::default() }, diff --git a/weaponry/src/weaponry_structs/equipement_impl_math.rs b/weaponry/src/weaponry_structs/equipement_impl_math.rs new file mode 100644 index 0000000..721d6dd --- /dev/null +++ b/weaponry/src/weaponry_structs/equipement_impl_math.rs @@ -0,0 +1,208 @@ +// Workspace: weaponry +// File: weaponry/src/weaponry_structs/equipement_math.rs +//! Implements base math for each field +//! + +use super::equipement_structs::InnerStats; +use crate::weaponry_structs::SpecialCategory; + +impl std::ops::Add for InnerStats { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { + // TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless + id: self.id, + name: { + let mut b = self.name.clone(); + b.append(&mut rhs.name.clone()); + b + }, + rarity: self.rarity + rhs.rarity, + cost: self.cost + rhs.cost, + difficulty: self.difficulty + rhs.difficulty, + tier: self.tier + rhs.tier, + damage: self.damage + rhs.damage, + p_damage: self.p_damage + rhs.p_damage, + precision: self.precision + rhs.precision, + rmax: self.rmax + rhs.rmax, + armor_break: self.armor_break + rhs.armor_break, + armor_pierce: self.armor_pierce + rhs.armor_pierce, + antimaterial: self.antimaterial + rhs.antimaterial, + damage_type: { + match (self.damage_type, rhs.damage_type) { + (None, None) => None, + (None, Some(a)) => Some(a), + (Some(a), None) => Some(a), + (Some(mut a), Some(mut b)) => { + a.0.append(&mut b.0); + Some(a) + } + } + }, + protection: self.protection + rhs.protection, + integrity: self.integrity + rhs.integrity, + torque_two_handed: self.torque_two_handed + rhs.torque_two_handed, + torque_one_handed: self.torque_one_handed + rhs.torque_one_handed, + dodge: self.dodge + rhs.dodge, + parry: self.parry + rhs.parry, + speed: self.speed + rhs.speed, + stealth: self.stealth + rhs.stealth, + category: self.category.or(rhs.category), + special: self.special + rhs.special, + action_cost: self.action_cost + rhs.action_cost, + reload_cost: self.reload_cost.or(rhs.reload_cost), + range: self.range + rhs.range, + agility_bonus: self.agility_bonus + rhs.agility_bonus, + } + } +} +impl std::ops::Add for &InnerStats { + type Output = InnerStats; + + fn add(self, rhs: Self) -> Self::Output { + InnerStats { + // TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless + id: self.id, + name: { + let mut b = self.name.clone(); + b.append(&mut rhs.name.clone()); + b + }, + rarity: self.rarity + rhs.rarity, + cost: self.cost + rhs.cost, + difficulty: self.difficulty + rhs.difficulty, + tier: self.tier + rhs.tier, + damage: self.damage + rhs.damage, + p_damage: self.p_damage + rhs.p_damage, + precision: self.precision + rhs.precision, + rmax: self.rmax + rhs.rmax, + armor_break: self.armor_break + rhs.armor_break, + armor_pierce: self.armor_pierce + rhs.armor_pierce, + antimaterial: self.antimaterial + rhs.antimaterial, + damage_type: { + match (self.damage_type.clone(), rhs.damage_type.clone()) { + (None, None) => None, + (None, Some(a)) => Some(a), + (Some(a), None) => Some(a), + (Some(mut a), Some(mut b)) => { + a.0.append(&mut b.0); + Some(a) + } + } + }, + protection: self.protection + rhs.protection, + integrity: self.integrity + rhs.integrity, + torque_two_handed: self.torque_two_handed + rhs.torque_two_handed, + torque_one_handed: self.torque_one_handed + rhs.torque_one_handed, + dodge: self.dodge + rhs.dodge, + parry: self.parry + rhs.parry, + speed: self.speed + rhs.speed, + stealth: self.stealth + rhs.stealth, + category: self.category.clone().or(rhs.category.clone()), // Can be further optimized + special: self.special.clone() + rhs.special.clone(), // Can be further optimized + action_cost: self.action_cost + rhs.action_cost, + reload_cost: self.reload_cost.or(rhs.reload_cost), + range: self.range + rhs.range, + agility_bonus: self.agility_bonus + rhs.agility_bonus, + } + } +} + +impl std::ops::AddAssign for InnerStats { + fn add_assign(&mut self, rhs: InnerStats) { + // self.id; + // self.name.push('+'); + // self.name.push_str(&rhs.name); + self.rarity += rhs.rarity; + self.cost += rhs.cost; + self.difficulty += rhs.difficulty; + self.tier += rhs.tier; + self.damage += rhs.damage; + self.p_damage += rhs.p_damage; + self.precision += rhs.precision; + self.rmax += rhs.rmax; + self.armor_break += rhs.armor_break; + self.armor_pierce += rhs.armor_pierce; + self.antimaterial += rhs.antimaterial; + self.damage_type = { + match (self.damage_type.clone(), rhs.damage_type.clone()) { + (None, None) => None, + (None, Some(a)) => Some(a), + (Some(a), None) => Some(a), + (Some(mut a), Some(mut b)) => { + a.0.append(&mut b.0); + Some(a) + } + } + }; + self.protection += rhs.protection; + self.integrity += rhs.integrity; + self.torque_two_handed += rhs.torque_two_handed; + self.torque_one_handed += rhs.torque_one_handed; + self.dodge += rhs.dodge; + self.parry += rhs.parry; + self.speed += rhs.speed; + self.stealth += rhs.stealth; + self.category.clone().or(rhs.category.clone()); // Can be further optimized + // self.special += rhs.special.clone(); // Can be further optimized + self.action_cost += rhs.action_cost; + self.reload_cost.or(rhs.reload_cost); + self.range += rhs.range; + self.agility_bonus += rhs.agility_bonus; + } +} + +impl std::ops::Add for SpecialCategory { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + let mut c = self.clone(); + // TODO: Overcopy, kinda meaningless optimisation tho + c.0.append(&mut rhs.clone().0); + c + } +} + +// impl std::ops::AddAssign for SpecialCategory { +// fn add_assign(&mut self, rhs: Self) { +// todo!() +// } +// } + +// impl std::ops::Add for OptionnalReloadType { +// type Output = OptionnalReloadType; + +// fn add(self, rhs: Self) -> Self::Output { +// todo!() +// } +// } +// impl std::ops::AddAssign for OptionnalReloadType { +// fn add_assign(&mut self, rhs: Self) { +// todo!() +// } +// } +// impl std::ops::Add for OptionnalWeaponCategory { +// type Output = OptionnalWeaponCategory; + +// fn add(self, rhs: Self) -> Self::Output { +// todo!() +// } +// } +// impl std::ops::AddAssign for OptionnalWeaponCategory { +// fn add_assign(&mut self, rhs: Self) { +// todo!() +// } +// } +// impl std::ops::Add for OptionnalDamageType { +// type Output = OptionnalDamageType; + +// fn add(self, rhs: Self) -> Self::Output { +// todo!() +// } +// } +// impl std::ops::AddAssign for OptionnalDamageType { +// fn add_assign(&mut self, rhs: Self) { +// todo!() +// } +// } diff --git a/weaponry/src/weaponry_structs/equipement_structs.rs b/weaponry/src/weaponry_structs/equipement_structs.rs index eddb28d..33624e3 100644 --- a/weaponry/src/weaponry_structs/equipement_structs.rs +++ b/weaponry/src/weaponry_structs/equipement_structs.rs @@ -5,7 +5,6 @@ use struct_field_names_as_array::FieldNamesAsArray; use super::{DamageType, NatureTarget, SpecialCategory, WeaponCategory}; -// use derive_more::Add; /// The final equipement list /// @@ -31,7 +30,7 @@ pub struct Equipement { #[cfg_attr(test, derive(Debug, PartialEq))] pub struct InnerStats { pub id: u8, - pub name: String, + pub name: Vec, pub(super) rarity: u8, pub(super) cost: u8, pub(super) difficulty: u8, @@ -60,6 +59,21 @@ pub struct InnerStats { pub(super) agility_bonus: u8, } +// /// A type representing a weapon reload potential +// #[derive(Clone, Default)] +// #[cfg_attr(test, derive(Debug, PartialEq))] +// pub struct OptionnalReloadType(Option); + +// /// Optionnal Weapon +// #[derive(Clone, Default)] +// #[cfg_attr(test, derive(Debug, PartialEq))] +// pub struct OptionnalWeaponCategory(Option); + +// /// Optionnal damage type +// #[derive(Clone, Default)] +// #[cfg_attr(test, derive(Debug, PartialEq))] +// pub struct OptionnalDamageType(Option); + /// Shape of the equipement, first mandatory part of an equipement pub struct EqShape { /// The inner stats of the part @@ -115,6 +129,9 @@ mod test { assert_eq!(list_eq[0], eq1); } + #[test] + fn test_creating_all_equipements() {} + #[ignore] #[test] fn test_default_behaviour() { diff --git a/weaponry/src/weaponry_structs/mod.rs b/weaponry/src/weaponry_structs/mod.rs index 2f7ba60..228a565 100644 --- a/weaponry/src/weaponry_structs/mod.rs +++ b/weaponry/src/weaponry_structs/mod.rs @@ -19,6 +19,7 @@ mod equipement_fields; mod equipement_impl_database; mod equipement_impl_io; mod equipement_impl_logic; +mod equipement_impl_math; pub mod equipement_structs; // Republishes the most used symbols -- 2.45.1