First implementation of the Polymorphism

This commit is contained in:
Dandaedre 2025-02-11 00:45:34 +01:00
parent 5279365511
commit d27cc588b1
3 changed files with 175 additions and 42 deletions

View File

@ -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<G>(&mut self, other: G) -> Self
where
G: RecipeCombinaison<Recipes = Self::Recipes>;
/// Access the inner list
// TODO: Use iterators ?
fn inner(&self) -> Vec<Self::Recipes>;
// 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<T>(&self, rhs: T) -> Self
where
T: Combineable<Inner = Self::Inner>;
}

View File

@ -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<T>
// where
// T: Send,
// {
// /// E
// elem: Vec<T>,
// }
// impl<T: Send> ParallelIterator for Gen<T> {
// type Item = T;
// fn drive_unindexed<C>(self, consumer: C) -> C::Result
// where
// C: rayon::iter::plumbing::UnindexedConsumer<Self::Item>,
// {
// let _ = consumer;
// todo!()
// }
// }
// /// Default full generation strategy
// pub fn default_generation() -> impl ParallelIterator {
// Gen::<u8> { elem: Vec::new() }
// }
/// Default full generation strategy
pub fn default_generation<T: Copy>(a: Vec<T>, b: Vec<T>) -> Vec<(T, T)> {
a.iter()
.flat_map(|e| {
b.iter()
.map(|f| ((e.clone()), (f.clone())))
.collect::<Vec<(T, T)>>()
})
// .collect::<Vec<Vec<(T, T)>>>()
// .flatten()
.collect()
}
// /// Default full generation strategy
// pub fn naive_generation<T>(left: Vec<T>, right: Vec<T>) -> Vec<T>
// 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::<Vec<T>>())
// .collect()
// }
// /// Default full generation strategy
// ///
// /// Take a list of various parts that are combinable and combines them
// pub fn naive_generation<T>(
// list_recipes: Vec<Box<impl ReceipeCombinaison<Recipes = u8>>>,
// ) -> Vec<T> {
// 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);
}
}

View File

@ -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<DamageMod>,
protection: u8,
integrity: u8,
torque_two_handed: Option<u8>,
@ -30,20 +33,87 @@ pub struct InnerStats {
parry: u8,
speed: u8,
stealth: u8,
category: WeaponCategory,
special: Vec<String>,
category: Option<WeaponCategory>,
special: SpecialCategory,
action_cost: u8,
reload_cost: Option<u8>,
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<String>);
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<T>(&self, rhs: T) -> Self
where
T: Combineable<Inner = Self::Inner>,
{
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<Equipement>,
}
impl RecipeCombinaison for FinalEquipementList {
type Recipes = Equipement;
fn combine_all<G>(&mut self, other: G) -> Self
where
G: RecipeCombinaison<Recipes = Self::Recipes>,
{
self.inner = self
.inner()
.iter()
.flat_map(|e| {
other
.inner()
.iter()
.map(|f: &Self::Recipes| e.add_to_self(f.clone()))
.collect::<Vec<_>>()
// .collect::<Vec<Vec<Self::Recipes>>>()
})
.collect();
todo!()
}
fn inner(&self) -> Vec<Self::Recipes> {
self.inner.clone()
}
}
#[cfg(test)]
mod test {
// use super::*;