diff --git a/weaponry/src/interfaces.rs b/weaponry/src/interfaces.rs index 32b050c..3252bd9 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -10,3 +10,40 @@ pub trait ToTex {} /// Specify this element can be made from parts (Unsure, maybe a way to ensure Add is presend ?) pub trait FromParts {} + +/// Trait implemented for the final collection of a recipe, allowing it to be combined with something +/// +/// A recipe list will be combined with a part list to be updated and generate all combinaisons, so on and so forth +pub trait RecipeCombinaison { + /// 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) -> Self + where + G: RecipeCombinaison; + + /// Access the inner list + // TODO: Use iterators ? + fn inner(&self) -> Vec; + // Optional filter ? +} + +/// 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 Combineable { + /// The inner type of the combined elements, this type must implement Add + type Inner: std::ops::Add; + + /// 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; + + /// 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 + where + T: Combineable; +} diff --git a/weaponry/src/strategy.rs b/weaponry/src/strategy.rs index 15c8c97..03b8c4f 100644 --- a/weaponry/src/strategy.rs +++ b/weaponry/src/strategy.rs @@ -8,8 +8,6 @@ //! #![allow(dead_code)] -use rayon::iter::ParallelIterator; - /// Generation strategy, how to generate all the data, instead of a full permutation pub enum EquipementStrategy { /// Full permutation, generate all possible outcomes @@ -44,50 +42,39 @@ pub enum EquipementFilter { Named, } -// /// D -// struct Gen -// where -// T: Send, -// { -// /// E -// elem: Vec, -// } - -// impl ParallelIterator for Gen { -// type Item = T; - -// fn drive_unindexed(self, consumer: C) -> C::Result -// where -// C: rayon::iter::plumbing::UnindexedConsumer, -// { -// let _ = consumer; -// todo!() -// } -// } // /// Default full generation strategy // pub fn default_generation() -> impl ParallelIterator { // Gen:: { elem: Vec::new() } // } -/// Default full generation strategy -pub fn default_generation(a: Vec, b: Vec) -> Vec<(T, T)> { - a.iter() - .flat_map(|e| { - b.iter() - .map(|f| ((e.clone()), (f.clone()))) - .collect::>() - }) - // .collect::>>() - // .flatten() - .collect() -} + +// /// Default full generation strategy +// pub fn naive_generation(left: Vec, right: Vec) -> Vec +// where +// T: Clone, +// for<'a> &'a T: std::ops::Add<&'a T>, +// { +// left.iter() +// .flat_map(move |e| right.iter().map(move |f| (e + f)).collect::>()) +// .collect() +// } + +// /// Default full generation strategy +// /// +// /// Take a list of various parts that are combinable and combines them +// pub fn naive_generation( +// list_recipes: Vec>>, +// ) -> Vec { +// list_recipes.iter().reduce(|acc, e| acc.combine_all(e)); +// todo!() +// } #[cfg(test)] mod test { - use super::*; + // use super::*; + #[test] - fn test_creator() { - let t = default_generation(vec![1, 2, 3], vec![5, 6, 7]); - println!("{:?}", t); - panic!() + fn test_naive_generator() { + // let t = naive_generation(vec![1, 2, 3], vec![5, 6, 7]); + // assert_eq!(t.len(), 3 * 3); } } diff --git a/weaponry/src/weaponry_structs.rs b/weaponry/src/weaponry_structs.rs index 73c6389..f5f60d0 100644 --- a/weaponry/src/weaponry_structs.rs +++ b/weaponry/src/weaponry_structs.rs @@ -4,9 +4,12 @@ #![allow(dead_code)] +use crate::interfaces::{Combineable, RecipeCombinaison}; + /// The inner statistics of the equipement, used across all parts and final weapon // #[derive(Default)] #[allow(clippy::missing_docs_in_private_items)] +#[derive(Clone, Default)] pub struct InnerStats { id: usize, name: String, @@ -21,7 +24,7 @@ pub struct InnerStats { armor_break: u8, armor_pierce: u8, antimaterial: u8, - damage_type: DamageMod, + damage_type: Option, protection: u8, integrity: u8, torque_two_handed: Option, @@ -30,20 +33,87 @@ pub struct InnerStats { parry: u8, speed: u8, stealth: u8, - category: WeaponCategory, - special: Vec, + category: Option, + special: SpecialCategory, action_cost: u8, reload_cost: Option, range: u8, agility_bonus: u8, } +impl std::ops::Add for InnerStats { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { + 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: self.damage_type.or(rhs.damage_type), + protection: self.protection + rhs.protection, + integrity: self.integrity + rhs.integrity, + two_handed: self.two_handed + rhs.two_handed, + one_handed: self.one_handed + rhs.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 + rhs.reload_cost, + range: self.range + rhs.range, + agility_bonus: self.agility_bonus + rhs.agility_bonus, + } + } +} + +#[derive(Clone, Default)] +/// The special category, a vec of string, for display purposes only +struct SpecialCategory(Vec); + +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 + } +} + /// The final equipement, obtained by combining all parts +#[derive(Clone)] pub struct Equipement { /// The inner stats used by the equipement stats: InnerStats, } +impl Combineable for Equipement { + type Inner = InnerStats; + fn clone_combinable_part(&self) -> InnerStats { + self.stats.clone() + } + + fn add_to_self(&self, rhs: T) -> Self + where + T: Combineable, + { + let mut new = self.clone(); + new.stats = self.clone_combinable_part() + rhs.clone_combinable_part(); + new + } +} + /// Shape of the equipement, first mandatory part of an equipement pub struct EqShape { /// The inner stats of the part @@ -84,6 +154,7 @@ pub enum NatureTarget { /// Enum of all different physical weapon damage types #[allow(clippy::missing_docs_in_private_items)] +#[derive(Clone)] pub enum PhysicalDamageType { Blunt, Slash, @@ -102,6 +173,7 @@ impl From<&str> for PhysicalDamageType { /// /// Signifying wether the damage is pure or [secondary] or [[tertiary]] #[allow(clippy::missing_docs_in_private_items)] +#[derive(Clone)] pub enum DamageMod { Primary(PhysicalDamageType), Secondary(PhysicalDamageType), @@ -131,6 +203,7 @@ impl From<&str> for DamageType { /// List all weapon categories #[allow(clippy::missing_docs_in_private_items)] +#[derive(Clone)] pub enum WeaponCategory { Exotic, Dagger, @@ -156,6 +229,42 @@ impl From<&str> for WeaponCategory { } } +/// The final equipement list +/// +/// This list is initialized with the base part and updated by succesive combinaisons +#[derive(Clone)] +struct FinalEquipementList { + /// Inner List of final equipements + inner: Vec, +} + +impl RecipeCombinaison for FinalEquipementList { + type Recipes = Equipement; + + fn combine_all(&mut self, other: G) -> Self + where + G: RecipeCombinaison, + { + self.inner = self + .inner() + .iter() + .flat_map(|e| { + other + .inner() + .iter() + .map(|f: &Self::Recipes| e.add_to_self(f.clone())) + .collect::>() + // .collect::>>() + }) + .collect(); + todo!() + } + + fn inner(&self) -> Vec { + self.inner.clone() + } +} + #[cfg(test)] mod test { // use super::*;