From d00b111fbd269beb4a0ad27f2eed13292bc6997d Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 12 Feb 2025 23:08:08 +0100 Subject: [PATCH] Adds better filters --- weaponry/src/interfaces.rs | 20 +++---- .../src/weaponry_structs/equipement_fields.rs | 5 +- .../equipement_impl_advanced.rs | 24 ++++++++ .../equipement_impl_basics.rs | 59 +++++++------------ .../weaponry_structs/equipement_impl_logic.rs | 12 ++-- .../weaponry_structs/equipement_impl_math.rs | 1 + .../weaponry_structs/equipement_structs.rs | 41 +++++++------ weaponry/src/weaponry_structs/mod.rs | 1 + 8 files changed, 83 insertions(+), 80 deletions(-) create mode 100644 weaponry/src/weaponry_structs/equipement_impl_advanced.rs diff --git a/weaponry/src/interfaces.rs b/weaponry/src/interfaces.rs index 8a05883..c54f718 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -22,14 +22,14 @@ where for<'a> &'a <::Recipes as Combinable>::Inner: std::ops::Add, { /// The type of the inner recipes, this type must be combinable - type Recipes<'a>: Combinable<'a>; + type Recipes: Combinable; /// A blanket implementation giving all combinaisons of recipes with another RecipeCollection /// /// This method doesn't make use of parallel iterators as the workload is way faster than would be the memory dispatch - fn combine_all<'a, G>(&mut self, other: &G) + fn combine_all(&mut self, other: &G) where - G: RecipeCollection = Self::Recipes<'a>>, + G: RecipeCollection, { self.assign_inner( self.ref_inner() @@ -55,11 +55,6 @@ where /// /// This allows to only clone when needed fn ref_inner(&self) -> &Vec; - /// - /// Gives access to a reference of the inner list - /// - /// This allows to only clone when needed - fn owned_inner(&self) -> Vec; /// Create a collection from a vector fn from_vectored(list: Vec) -> Self; @@ -68,12 +63,11 @@ where /// Trait that ensure that two elements of a recipe are combinable, /// /// Being combinable means it can be coerced into elements that implement add -pub trait Combinable<'a> +pub trait Combinable where for<'a> &'a ::Inner: std::ops::Add, { /// The inner type of the combined elements, this type must implement Add<&Inner> - // TODO : Make this help much more readable type Inner; /// Give access to a reference of the underlying data to be combined @@ -105,15 +99,15 @@ where // #![feature(trait_alias)] pub(super) type FilterFunctionType = dyn Fn(&T, &T) -> bool; /// Type alias representing a filter -pub(super) type FilterType<'a, T> = Box>>; +pub(super) type FilterType = Box>>; #[derive(Clone)] /// A generic Filtering function -pub(crate) struct FilterFunction<'a, T>(pub(super) FilterType<'a, T>); +pub(crate) struct FilterFunction(pub(super) FilterType); // impl FilterFunction { // fn new_from_closure(m: &FilterFunctionType) -> Self { -// FilterFunction(Box::new(Rc::new(m.to_owned()))) +// FilterFunction(Box::new(Rc::new(m))) // } // } diff --git a/weaponry/src/weaponry_structs/equipement_fields.rs b/weaponry/src/weaponry_structs/equipement_fields.rs index 847c1e0..0940494 100644 --- a/weaponry/src/weaponry_structs/equipement_fields.rs +++ b/weaponry/src/weaponry_structs/equipement_fields.rs @@ -8,6 +8,7 @@ pub struct SpecialCategory(pub(super) Vec); /// The kind of target this nature can apply to +#[derive(Clone)] pub enum NatureTarget { /// Can apply to a weapon and is unique WeaponUnique, @@ -71,8 +72,8 @@ impl From<&str> for DamageType { /// List all weapon categories #[allow(clippy::missing_docs_in_private_items, missing_docs)] -#[derive(Clone, Debug)] -#[cfg_attr(test, derive(PartialEq))] +#[derive(Clone, Debug, PartialEq)] +// #[cfg_attr(test, derive(PartialEq))] pub enum WeaponCategory { Exotic, Dagger, diff --git a/weaponry/src/weaponry_structs/equipement_impl_advanced.rs b/weaponry/src/weaponry_structs/equipement_impl_advanced.rs new file mode 100644 index 0000000..c97cf7f --- /dev/null +++ b/weaponry/src/weaponry_structs/equipement_impl_advanced.rs @@ -0,0 +1,24 @@ +// Workspace: weaponry +// File: weaponry/src/weaponry_structs/equipement_impl_advanced.rs +//! Advanced implementation, reserved for traits and impls that are implemented only once on the main equipement and it's helper +//! +//! These impls are here to either prettify output, or allow for smoother implementation of other traits and inner workings + +use crate::interfaces::FilterFunction; +use std::{fmt::Debug, rc::Rc}; + +impl Debug for FilterFunction { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Filter:{}", std::any::type_name::()) + } +} +impl Default for FilterFunction { + fn default() -> Self { + FilterFunction(Box::new(Rc::new(|_, _| true))) + } +} +impl PartialEq for FilterFunction { + fn eq(&self, _other: &Self) -> bool { + true + } +} diff --git a/weaponry/src/weaponry_structs/equipement_impl_basics.rs b/weaponry/src/weaponry_structs/equipement_impl_basics.rs index fc66e84..fc7f57d 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_basics.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_basics.rs @@ -1,5 +1,5 @@ // Workspace: weaponry -// File: weaponry/src/weaponry_structs/equipement_into_impls.rs +// File: weaponry/src/weaponry_structs/equipement_impl_basics.rs // TODO: At some point a trait might want to be used to use these into instead of calling it directly // : Doing so would ensure all prerequists are met // : Another way of doing so might be to instead use Into instead @@ -8,18 +8,14 @@ //! Implementations to transform all Specialized equipement into a generic Equipement //! A specific implementation could be preferable, but this simplicity might allow for easier developpement -use crate::interfaces::{FilterFunction, FilterType}; -use std::fmt::Debug; -use std::rc::Rc; +use crate::interfaces::FilterFunction; use super::{ Equipement, - equipement_structs::{ - EqFinishing, EqMaterial, EqNature, EqShape, default_filter_for_equipement, - }, + equipement_structs::{EqFinishing, EqMaterial, EqNature, EqShape}, }; -impl From for Equipement<'_> { +impl From for Equipement { fn from(val: EqShape) -> Self { Self { stats: val.stats, @@ -27,7 +23,7 @@ impl From for Equipement<'_> { } } } -impl From for Equipement<'_> { +impl From for Equipement { fn from(val: EqMaterial) -> Self { Self { stats: val.stats, @@ -35,15 +31,27 @@ impl From for Equipement<'_> { } } } -impl From for Equipement<'_> { +impl From for Equipement { fn from(val: EqNature) -> Self { Self { stats: val.stats, - filter_function: FilterFunction(default_filter_for_equipement()), + // filter_function: FilterFunction(default_filter_for_equipement()), + filter_function: FilterFunction::new(&|sel, lhs| { + match (&sel.target, &lhs.category) { + (Some(a), Some(b)) => a == b, + (None, None) => true, + _ => false, + } + // if let Some(targ) = &sel.target { + // &lhs.category + // } + // & default_filter()(sel, lhs) + }), } } } -impl From for Equipement<'_> { + +impl From for Equipement { fn from(val: EqFinishing) -> Self { Self { stats: val.stats, @@ -51,30 +59,3 @@ impl From for Equipement<'_> { } } } - -impl Debug for FilterFunction<'_, T> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "Filter:{}", - // std::any::type_name::(), - std::any::type_name::() - ) - } -} -impl Default for FilterFunction<'_, T> { - fn default() -> Self { - FilterFunction(Box::new(Rc::new(&|_, _| true))) - } -} -impl PartialEq for FilterFunction<'_, T> { - fn eq(&self, _other: &Self) -> bool { - true - } -} -// impl FilterFunction { -// /// Generate a new filter -// pub fn new(filter: FilterFunctionType) -> Self { -// Self(Box::new(Rc::new(filter))) -// } -// } diff --git a/weaponry/src/weaponry_structs/equipement_impl_logic.rs b/weaponry/src/weaponry_structs/equipement_impl_logic.rs index a6c448f..bb7bdc6 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_logic.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_logic.rs @@ -6,7 +6,7 @@ use crate::interfaces::{Combinable, RecipeCollection}; use super::{Equipement, FinalEquipementList, equipement_structs::InnerStats}; -impl Combinable for Equipement<'_> { +impl Combinable for Equipement { type Inner = InnerStats; fn combinable_part(&self) -> &Self::Inner { @@ -31,8 +31,8 @@ impl Combinable for Equipement<'_> { } } -impl RecipeCollection for FinalEquipementList<'_> { - type Recipes<'a> = Equipement<'a>; +impl RecipeCollection for FinalEquipementList { + type Recipes = Equipement; fn from_vectored(list: Vec) -> Self { Self { inner: list } @@ -45,10 +45,6 @@ impl RecipeCollection for FinalEquipementList<'_> { fn assign_inner(&mut self, inner: Vec) { self.inner = inner; } - - fn owned_inner(&self) -> Vec { - self.inner.clone() - } } #[cfg(test)] @@ -174,7 +170,7 @@ mod test { #[test] fn test_basic_logic() {} - impl Equipement<'_> { + impl Equipement { fn newish(name: &[&str], i: u8) -> Self { Equipement { stats: InnerStats { diff --git a/weaponry/src/weaponry_structs/equipement_impl_math.rs b/weaponry/src/weaponry_structs/equipement_impl_math.rs index 0f49394..7951a98 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_math.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_math.rs @@ -54,6 +54,7 @@ impl std::ops::Add for &InnerStats { speed: self.speed + rhs.speed, stealth: self.stealth + rhs.stealth, category: self.category.clone().or(rhs.category.clone()), // Can be further optimized + target: self.target.clone().or(rhs.target.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), diff --git a/weaponry/src/weaponry_structs/equipement_structs.rs b/weaponry/src/weaponry_structs/equipement_structs.rs index 1d31e98..94900f2 100644 --- a/weaponry/src/weaponry_structs/equipement_structs.rs +++ b/weaponry/src/weaponry_structs/equipement_structs.rs @@ -6,44 +6,50 @@ use std::rc::Rc; use struct_field_names_as_array::FieldNamesAsArray; -use crate::interfaces::{FilterFunction, FilterType}; +use crate::interfaces::{FilterFunction, FilterFunctionType}; -use super::{DamageType, NatureTarget, SpecialCategory, WeaponCategory}; +use super::{DamageType, SpecialCategory, WeaponCategory}; /// The final equipement list /// /// This list is initialized with the base part and updated by succesive combinaisons #[derive(Clone, Debug)] #[cfg_attr(test, derive(PartialEq))] -pub struct FinalEquipementList<'a> { +pub struct FinalEquipementList { /// Inner List of final equipements - pub(super) inner: Vec>, + pub(super) inner: Vec, } /// The final equipement, obtained by combining all parts #[derive(Clone, Debug)] #[cfg_attr(test, derive(PartialEq))] -pub struct Equipement<'a> { +pub struct Equipement { /// The inner stats used by the equipement pub(super) stats: InnerStats, /// The filter, used to determine if adding is warranted - pub(super) filter_function: FilterFunction<'a, InnerStats>, + pub(super) filter_function: FilterFunction, } -impl Default for Equipement<'_> { +impl Default for Equipement { fn default() -> Self { Self { - filter_function: FilterFunction(default_filter_for_equipement()), + filter_function: FilterFunction::new(default_filter()), stats: InnerStats::random_new(), } } } -/// The default filter function used by the equipements -pub(super) fn default_filter_for_equipement() -> FilterType<'static, InnerStats> { - Box::new(Rc::new(&|self_, lhs| { - !self_.id.iter().any(|a| *a == lhs.id[0]) - })) +/// The default filter to use, usefull to implement a new filter function that also uses the default filter +pub(crate) fn default_filter() -> &'static impl Fn(&InnerStats, &InnerStats) -> bool { + &|self_, lhs| !self_.id.iter().any(|a| *a == lhs.id[0]) +} + +impl FilterFunction { + /// Generate a new filter + // TODO: This static lifetime is basically telling us that an owned value is needed + pub fn new(filter: &'static FilterFunctionType) -> Self { + Self(Box::new(Rc::new(filter))) + } } /// The inner statistics of the equipement, used across all parts and final weapon @@ -74,6 +80,7 @@ pub struct InnerStats { pub(super) speed: u8, pub(super) stealth: u8, pub(super) category: Option, + pub(super) target: Option, pub(super) special: SpecialCategory, pub(super) action_cost: u8, pub(super) reload_cost: Option, @@ -81,6 +88,7 @@ pub struct InnerStats { pub(super) agility_bonus: u8, } +// TODO: Make this a test requirement or hide it behind a profile impl InnerStats { /// Testing function to fuzz data pub fn random_new() -> Self { @@ -109,8 +117,6 @@ pub struct EqMaterial { pub struct EqNature { /// The inner stats of the part pub(super) stats: InnerStats, - /// Which kind of target this nature applies to - pub(super) target: NatureTarget, } /// Finishing touches, since all of them have a cost, they are treated in a special fashion @@ -123,7 +129,7 @@ pub struct EqFinishing { mod test { // use super::*; - use crate::{interfaces::RecipeCollection, weaponry_structs::NatureTarget}; + use crate::interfaces::RecipeCollection; use super::{ EqFinishing, EqMaterial, EqNature, EqShape, Equipement, FinalEquipementList, InnerStats, @@ -131,7 +137,7 @@ mod test { // use pretty_assertions::assert_eq; - fn list_equip() -> Vec> { + fn list_equip() -> Vec { (0..10) .map(|_| Equipement { ..Default::default() @@ -160,7 +166,6 @@ mod test { let nature = vec![ EqNature { stats: InnerStats::default(), - target: NatureTarget::Armor, } .into(), ]; diff --git a/weaponry/src/weaponry_structs/mod.rs b/weaponry/src/weaponry_structs/mod.rs index 482bd6b..9bcb277 100644 --- a/weaponry/src/weaponry_structs/mod.rs +++ b/weaponry/src/weaponry_structs/mod.rs @@ -16,6 +16,7 @@ // static GLOBAL: Jemalloc = Jemalloc; mod equipement_fields; +mod equipement_impl_advanced; mod equipement_impl_basics; mod equipement_impl_database; mod equipement_impl_io;