From 36e1b84ae8b767d1f78cdc55d7b5b68a6bb9f8b1 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 12 Feb 2025 22:14:59 +0100 Subject: [PATCH] Adds better filters --- weaponry/src/interfaces.rs | 13 +++--- .../src/weaponry_structs/equipement_fields.rs | 5 ++- .../equipement_impl_advanced.rs | 24 ++++++++++ .../equipement_impl_basics.rs | 45 ++++++------------- .../weaponry_structs/equipement_impl_logic.rs | 4 -- .../weaponry_structs/equipement_impl_math.rs | 1 + .../weaponry_structs/equipement_structs.rs | 30 ++++++++----- weaponry/src/weaponry_structs/mod.rs | 1 + 8 files changed, 69 insertions(+), 54 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 c78d5e3..c54f718 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -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; @@ -73,7 +68,6 @@ 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 @@ -102,6 +96,7 @@ where } /// Type alias represinting the inner function parameter +// #![feature(trait_alias)] pub(super) type FilterFunctionType = dyn Fn(&T, &T) -> bool; /// Type alias representing a filter pub(super) type FilterType = Box>>; @@ -110,6 +105,12 @@ pub(super) type FilterType = Box>>; /// A generic Filtering function pub(crate) struct FilterFunction(pub(super) FilterType); +// impl FilterFunction { +// fn new_from_closure(m: &FilterFunctionType) -> Self { +// FilterFunction(Box::new(Rc::new(m))) +// } +// } + // /// 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 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 218b5f9..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 @@ -9,8 +9,6 @@ //! A specific implementation could be preferable, but this simplicity might allow for easier developpement use crate::interfaces::FilterFunction; -use std::fmt::Debug; -use std::rc::Rc; use super::{ Equipement, @@ -37,10 +35,22 @@ impl From for Equipement { fn from(val: EqNature) -> Self { Self { stats: val.stats, - ..Default::default() + // 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 { fn from(val: EqFinishing) -> Self { Self { @@ -49,30 +59,3 @@ impl From for Equipement { } } } - -impl Debug for FilterFunction { - 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 { - fn default() -> Self { - FilterFunction(Box::new(Rc::new(|_, _| true))) - } -} -impl PartialEq for FilterFunction { - 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 21acb2e..bb7bdc6 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_logic.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_logic.rs @@ -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)] 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 c4ccdf8..94900f2 100644 --- a/weaponry/src/weaponry_structs/equipement_structs.rs +++ b/weaponry/src/weaponry_structs/equipement_structs.rs @@ -6,9 +6,9 @@ use std::rc::Rc; use struct_field_names_as_array::FieldNamesAsArray; -use crate::interfaces::FilterFunction; +use crate::interfaces::{FilterFunction, FilterFunctionType}; -use super::{DamageType, NatureTarget, SpecialCategory, WeaponCategory}; +use super::{DamageType, SpecialCategory, WeaponCategory}; /// The final equipement list /// @@ -33,14 +33,25 @@ pub struct Equipement { impl Default for Equipement { fn default() -> Self { Self { - filter_function: FilterFunction(Box::new(Rc::new(|self_, lhs| { - !self_.id.iter().any(|a| *a == lhs.id[0]) - }))), + filter_function: FilterFunction::new(default_filter()), stats: InnerStats::random_new(), } } } +/// 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 #[allow(clippy::missing_docs_in_private_items, missing_docs)] #[derive(Clone, Default, FieldNamesAsArray, Debug)] @@ -69,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, @@ -76,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 { @@ -104,10 +117,6 @@ pub struct EqMaterial { pub struct EqNature { /// The inner stats of the part pub(super) stats: InnerStats, - // TODO: Be sure to take this into account - #[allow(dead_code)] - /// 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 @@ -120,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, @@ -157,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; -- 2.45.1