Adds better filters

This commit is contained in:
Dandaedre 2025-02-12 23:08:08 +01:00
parent 701bb52a6c
commit d00b111fbd
8 changed files with 83 additions and 80 deletions

View File

@ -22,14 +22,14 @@ where
for<'a> &'a <<Self as RecipeCollection>::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<G>(&mut self, other: &G)
where
G: RecipeCollection<Recipes<'a> = Self::Recipes<'a>>,
G: RecipeCollection<Recipes = Self::Recipes>,
{
self.assign_inner(
self.ref_inner()
@ -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;
@ -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 <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
@ -105,15 +99,15 @@ where
// #![feature(trait_alias)]
pub(super) type FilterFunctionType<T> = dyn Fn(&T, &T) -> bool;
/// Type alias representing a filter
pub(super) type FilterType<'a, T> = Box<Rc<&'a FilterFunctionType<T>>>;
pub(super) type FilterType<T> = Box<Rc<FilterFunctionType<T>>>;
#[derive(Clone)]
/// A generic Filtering function
pub(crate) struct FilterFunction<'a, T>(pub(super) FilterType<'a, T>);
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.to_owned())))
// FilterFunction(Box::new(Rc::new(m)))
// }
// }

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
@ -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<EqShape> for Equipement<'_> {
impl From<EqShape> for Equipement {
fn from(val: EqShape) -> Self {
Self {
stats: val.stats,
@ -27,7 +23,7 @@ impl From<EqShape> for Equipement<'_> {
}
}
}
impl From<EqMaterial> for Equipement<'_> {
impl From<EqMaterial> for Equipement {
fn from(val: EqMaterial) -> Self {
Self {
stats: val.stats,
@ -35,15 +31,27 @@ impl From<EqMaterial> for Equipement<'_> {
}
}
}
impl From<EqNature> for Equipement<'_> {
impl From<EqNature> 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<EqFinishing> for Equipement<'_> {
impl From<EqFinishing> for Equipement {
fn from(val: EqFinishing) -> Self {
Self {
stats: val.stats,
@ -51,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

@ -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::Recipes>) -> Self {
Self { inner: list }
@ -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)]
@ -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 {

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,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<Equipement<'a>>,
pub(super) inner: Vec<Equipement>,
}
/// 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<InnerStats>,
}
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<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
@ -74,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>,
@ -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<Equipement<'static>> {
fn list_equip() -> Vec<Equipement> {
(0..10)
.map(|_| Equipement {
..Default::default()
@ -160,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;