Adds better filters #32

Merged
Dandaedre merged 1 commits from woo into project_weaponry 2025-02-12 23:29:08 +01:00
8 changed files with 69 additions and 54 deletions

View File

@ -55,11 +55,6 @@ where
///
/// This allows to only clone when needed
fn ref_inner(&self) -> &Vec<Self::Recipes>;
///
/// Gives access to a reference of the inner list
///
/// This allows to only clone when needed
fn owned_inner(&self) -> Vec<Self::Recipes>;
/// Create a collection from a vector
fn from_vectored(list: Vec<Self::Recipes>) -> Self;
@ -73,7 +68,6 @@ where
for<'a> &'a <Self as Combinable>::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<T> = dyn Fn(&T, &T) -> bool;
/// Type alias representing a filter
pub(super) type FilterType<T> = Box<Rc<FilterFunctionType<T>>>;
@ -110,6 +105,12 @@ pub(super) type FilterType<T> = Box<Rc<FilterFunctionType<T>>>;
/// A generic Filtering function
pub(crate) struct FilterFunction<T>(pub(super) FilterType<T>);
// impl<T> FilterFunction<T> {
// fn new_from_closure(m: &FilterFunctionType<T>) -> 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

View File

@ -8,6 +8,7 @@
pub struct SpecialCategory(pub(super) Vec<String>);
/// 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,

View File

@ -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<T> Debug for FilterFunction<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Filter:{}", std::any::type_name::<Self>())
}
}
impl<T> Default for FilterFunction<T> {
fn default() -> Self {
FilterFunction(Box::new(Rc::new(|_, _| true)))
}
}
impl<T> PartialEq for FilterFunction<T> {
fn eq(&self, _other: &Self) -> bool {
true
}
}

View File

@ -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<Combinable> 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<EqNature> 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<EqFinishing> for Equipement {
fn from(val: EqFinishing) -> Self {
Self {
@ -49,30 +59,3 @@ impl From<EqFinishing> for Equipement {
}
}
}
impl<T> Debug for FilterFunction<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Filter:{}",
// std::any::type_name::<T>(),
std::any::type_name::<Self>()
)
}
}
impl<T> Default for FilterFunction<T> {
fn default() -> Self {
FilterFunction(Box::new(Rc::new(|_, _| true)))
}
}
impl<T> PartialEq for FilterFunction<T> {
fn eq(&self, _other: &Self) -> bool {
true
}
}
// impl<T> FilterFunction<T> {
// /// Generate a new filter
// pub fn new(filter: FilterFunctionType<T>) -> Self {
// Self(Box::new(Rc::new(filter)))
// }
// }

View File

@ -45,10 +45,6 @@ impl RecipeCollection for FinalEquipementList {
fn assign_inner(&mut self, inner: Vec<Self::Recipes>) {
self.inner = inner;
}
fn owned_inner(&self) -> Vec<Self::Recipes> {
self.inner.clone()
}
}
#[cfg(test)]

View File

@ -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),

View File

@ -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<T> FilterFunction<T> {
/// Generate a new filter
// TODO: This static lifetime is basically telling us that an owned value is needed
pub fn new(filter: &'static FilterFunctionType<T>) -> 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<WeaponCategory>,
pub(super) target: Option<WeaponCategory>,
pub(super) special: SpecialCategory,
pub(super) action_cost: u8,
pub(super) reload_cost: Option<u8>,
@ -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(),
];

View File

@ -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;