Refactor of impls

This commit is contained in:
Dandaedre 2025-02-11 23:30:27 +01:00
parent 2c44cdd817
commit 9e1521e373
5 changed files with 250 additions and 185 deletions

View File

@ -29,6 +29,7 @@ pub trait RecipeCollection {
where
G: RecipeCollection<Recipes = Self::Recipes>,
{
// TODO: Test Add to Self instead
self.assign_inner(
self.ref_inner()
.iter()
@ -93,13 +94,13 @@ pub trait Combineable {
/// The designed combinaison method is std::ops::Add
fn combinable_part(&self) -> &Self::Inner;
/// Add an element to self and return a new value
///
/// This method will clone it's own combinable part
#[deprecated]
fn add_to_other<T>(&self, rhs: T) -> Self
where
T: Combineable<Inner = Self::Inner>;
// /// Add an element to self and return a new value
// ///
// /// This method will clone it's own combinable part
// #[deprecated]
// fn add_to_other<T>(&self, rhs: T) -> Self
// where
// T: Combineable<Inner = Self::Inner>;
/// Add an element to a mutable self
// #[deprecated(note = "Mutate in place")]

View File

@ -4,170 +4,11 @@
use crate::interfaces::{Combineable, RecipeCollection};
use super::{Equipement, FinalEquipementList, SpecialCategory, equipement_structs::InnerStats};
impl std::ops::Add for InnerStats {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
// TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless
id: self.id,
name: [self.name, rhs.name].join("+"),
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: {
match (self.damage_type, rhs.damage_type) {
(None, None) => None,
(None, Some(a)) => Some(a),
(Some(a), None) => Some(a),
(Some(mut a), Some(mut b)) => {
a.0.append(&mut b.0);
Some(a)
}
}
},
protection: self.protection + rhs.protection,
integrity: self.integrity + rhs.integrity,
torque_two_handed: self.torque_two_handed + rhs.torque_two_handed,
torque_one_handed: self.torque_one_handed + rhs.torque_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.or(rhs.reload_cost),
range: self.range + rhs.range,
agility_bonus: self.agility_bonus + rhs.agility_bonus,
}
}
}
impl std::ops::Add for &InnerStats {
type Output = InnerStats;
fn add(self, rhs: Self) -> Self::Output {
InnerStats {
// TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless
id: self.id,
name: [self.name.clone(), rhs.name.clone()].join("+"),
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: {
match (self.damage_type.clone(), rhs.damage_type.clone()) {
(None, None) => None,
(None, Some(a)) => Some(a),
(Some(a), None) => Some(a),
(Some(mut a), Some(mut b)) => {
a.0.append(&mut b.0);
Some(a)
}
}
},
protection: self.protection + rhs.protection,
integrity: self.integrity + rhs.integrity,
torque_two_handed: self.torque_two_handed + rhs.torque_two_handed,
torque_one_handed: self.torque_one_handed + rhs.torque_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.clone().or(rhs.category.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),
range: self.range + rhs.range,
agility_bonus: self.agility_bonus + rhs.agility_bonus,
}
}
}
impl std::ops::AddAssign for InnerStats {
fn add_assign(&mut self, rhs: InnerStats) {
// self.id;
self.name.push('+');
self.name.push_str(&rhs.name);
self.rarity += rhs.rarity;
self.cost += rhs.cost;
self.difficulty += rhs.difficulty;
self.tier += rhs.tier;
self.damage += rhs.damage;
self.p_damage += rhs.p_damage;
self.precision += rhs.precision;
self.rmax += rhs.rmax;
self.armor_break += rhs.armor_break;
self.armor_pierce += rhs.armor_pierce;
self.antimaterial += rhs.antimaterial;
self.damage_type = {
match (self.damage_type.clone(), rhs.damage_type.clone()) {
(None, None) => None,
(None, Some(a)) => Some(a),
(Some(a), None) => Some(a),
(Some(mut a), Some(mut b)) => {
a.0.append(&mut b.0);
Some(a)
}
}
};
self.protection += rhs.protection;
self.integrity += rhs.integrity;
self.torque_two_handed += rhs.torque_two_handed;
self.torque_one_handed += rhs.torque_one_handed;
self.dodge += rhs.dodge;
self.parry += rhs.parry;
self.speed += rhs.speed;
self.stealth += rhs.stealth;
self.category.clone().or(rhs.category.clone()); // Can be further optimized
// self.special += rhs.special.clone(); // Can be further optimized
self.action_cost += rhs.action_cost;
self.reload_cost.or(rhs.reload_cost);
self.range += rhs.range;
self.agility_bonus += rhs.agility_bonus;
}
}
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
}
}
use super::{Equipement, FinalEquipementList, equipement_structs::InnerStats};
impl Combineable for Equipement {
type Inner = InnerStats;
fn add_to_other<T>(&self, rhs: T) -> Self
where
T: Combineable<Inner = Self::Inner>,
{
Self {
stats: self.combinable_part() + rhs.combinable_part(),
}
}
fn combinable_part(&self) -> &Self::Inner {
&self.stats
}
@ -219,27 +60,27 @@ mod test {
#[test]
fn test_blanket_struct_generation() {
let mut f = FinalEquipementList::from_vectored(vec![
Equipement::newish("a", 1),
Equipement::newish("b", 2),
Equipement::newish(&["a"], 1),
Equipement::newish(&["b"], 2),
]);
let g = FinalEquipementList::from_vectored(vec![
Equipement::newish("c", 4),
Equipement::newish("d", 8),
Equipement::newish(&["c"], 4),
Equipement::newish(&["d"], 8),
]);
assert_eq!(
f.ref_inner(),
&vec![Equipement::newish("a", 1), Equipement::newish("b", 2)]
&vec![Equipement::newish(&["a"], 1), Equipement::newish(&["b"], 2)]
);
f.combine_all_by_blanket(&g);
assert_eq!(
f,
FinalEquipementList::from_vectored(vec![
Equipement::newish("a+c", 5),
Equipement::newish("a+d", 9),
Equipement::newish("b+c", 6),
Equipement::newish("b+d", 10)
Equipement::newish(&["a", "c"], 5),
Equipement::newish(&["a", "d"], 9),
Equipement::newish(&["b", "c"], 6),
Equipement::newish(&["b", "d"], 10)
])
);
}
@ -271,7 +112,6 @@ mod test {
assert_eq!(
i1 + i2,
InnerStats {
name: "+".into(),
rarity: 1,
integrity: 5,
agility_bonus: 9,
@ -287,15 +127,14 @@ mod test {
}
);
assert_eq!(
InnerStats {
&InnerStats {
damage_type: Some(DamageType(vec![
DamageMod::Secondary(PhysicalDamageType::Slash,),
DamageMod::Primary(PhysicalDamageType::Blunt,),
])),
..Default::default()
} + InnerStats::default(),
} + &InnerStats::default(),
InnerStats {
name: "+".into(),
damage_type: Some(DamageType(vec![
DamageMod::Secondary(PhysicalDamageType::Slash,),
DamageMod::Primary(PhysicalDamageType::Blunt,),
@ -313,7 +152,6 @@ mod test {
..Default::default()
},
InnerStats {
name: "+".into(),
damage_type: Some(DamageType(vec![
DamageMod::Secondary(PhysicalDamageType::Slash,),
DamageMod::Primary(PhysicalDamageType::Blunt,),
@ -332,10 +170,10 @@ mod test {
fn test_basic_logic() {}
impl Equipement {
fn newish(name: &str, i: u8) -> Self {
fn newish(name: &[&str], i: u8) -> Self {
Equipement {
stats: InnerStats {
name: name.to_owned(),
name: name.iter().map(|s| s.to_string()).collect(),
rarity: i,
..Default::default()
},

View File

@ -0,0 +1,208 @@
// Workspace: weaponry
// File: weaponry/src/weaponry_structs/equipement_math.rs
//! Implements base math for each field
//!
use super::equipement_structs::InnerStats;
use crate::weaponry_structs::SpecialCategory;
impl std::ops::Add for InnerStats {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
// TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless
id: self.id,
name: {
let mut b = self.name.clone();
b.append(&mut rhs.name.clone());
b
},
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: {
match (self.damage_type, rhs.damage_type) {
(None, None) => None,
(None, Some(a)) => Some(a),
(Some(a), None) => Some(a),
(Some(mut a), Some(mut b)) => {
a.0.append(&mut b.0);
Some(a)
}
}
},
protection: self.protection + rhs.protection,
integrity: self.integrity + rhs.integrity,
torque_two_handed: self.torque_two_handed + rhs.torque_two_handed,
torque_one_handed: self.torque_one_handed + rhs.torque_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.or(rhs.reload_cost),
range: self.range + rhs.range,
agility_bonus: self.agility_bonus + rhs.agility_bonus,
}
}
}
impl std::ops::Add for &InnerStats {
type Output = InnerStats;
fn add(self, rhs: Self) -> Self::Output {
InnerStats {
// TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless
id: self.id,
name: {
let mut b = self.name.clone();
b.append(&mut rhs.name.clone());
b
},
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: {
match (self.damage_type.clone(), rhs.damage_type.clone()) {
(None, None) => None,
(None, Some(a)) => Some(a),
(Some(a), None) => Some(a),
(Some(mut a), Some(mut b)) => {
a.0.append(&mut b.0);
Some(a)
}
}
},
protection: self.protection + rhs.protection,
integrity: self.integrity + rhs.integrity,
torque_two_handed: self.torque_two_handed + rhs.torque_two_handed,
torque_one_handed: self.torque_one_handed + rhs.torque_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.clone().or(rhs.category.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),
range: self.range + rhs.range,
agility_bonus: self.agility_bonus + rhs.agility_bonus,
}
}
}
impl std::ops::AddAssign for InnerStats {
fn add_assign(&mut self, rhs: InnerStats) {
// self.id;
// self.name.push('+');
// self.name.push_str(&rhs.name);
self.rarity += rhs.rarity;
self.cost += rhs.cost;
self.difficulty += rhs.difficulty;
self.tier += rhs.tier;
self.damage += rhs.damage;
self.p_damage += rhs.p_damage;
self.precision += rhs.precision;
self.rmax += rhs.rmax;
self.armor_break += rhs.armor_break;
self.armor_pierce += rhs.armor_pierce;
self.antimaterial += rhs.antimaterial;
self.damage_type = {
match (self.damage_type.clone(), rhs.damage_type.clone()) {
(None, None) => None,
(None, Some(a)) => Some(a),
(Some(a), None) => Some(a),
(Some(mut a), Some(mut b)) => {
a.0.append(&mut b.0);
Some(a)
}
}
};
self.protection += rhs.protection;
self.integrity += rhs.integrity;
self.torque_two_handed += rhs.torque_two_handed;
self.torque_one_handed += rhs.torque_one_handed;
self.dodge += rhs.dodge;
self.parry += rhs.parry;
self.speed += rhs.speed;
self.stealth += rhs.stealth;
self.category.clone().or(rhs.category.clone()); // Can be further optimized
// self.special += rhs.special.clone(); // Can be further optimized
self.action_cost += rhs.action_cost;
self.reload_cost.or(rhs.reload_cost);
self.range += rhs.range;
self.agility_bonus += rhs.agility_bonus;
}
}
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
}
}
// impl std::ops::AddAssign for SpecialCategory {
// fn add_assign(&mut self, rhs: Self) {
// todo!()
// }
// }
// impl std::ops::Add for OptionnalReloadType {
// type Output = OptionnalReloadType;
// fn add(self, rhs: Self) -> Self::Output {
// todo!()
// }
// }
// impl std::ops::AddAssign for OptionnalReloadType {
// fn add_assign(&mut self, rhs: Self) {
// todo!()
// }
// }
// impl std::ops::Add for OptionnalWeaponCategory {
// type Output = OptionnalWeaponCategory;
// fn add(self, rhs: Self) -> Self::Output {
// todo!()
// }
// }
// impl std::ops::AddAssign for OptionnalWeaponCategory {
// fn add_assign(&mut self, rhs: Self) {
// todo!()
// }
// }
// impl std::ops::Add for OptionnalDamageType {
// type Output = OptionnalDamageType;
// fn add(self, rhs: Self) -> Self::Output {
// todo!()
// }
// }
// impl std::ops::AddAssign for OptionnalDamageType {
// fn add_assign(&mut self, rhs: Self) {
// todo!()
// }
// }

View File

@ -5,7 +5,6 @@
use struct_field_names_as_array::FieldNamesAsArray;
use super::{DamageType, NatureTarget, SpecialCategory, WeaponCategory};
// use derive_more::Add;
/// The final equipement list
///
@ -31,7 +30,7 @@ pub struct Equipement {
#[cfg_attr(test, derive(Debug, PartialEq))]
pub struct InnerStats {
pub id: u8,
pub name: String,
pub name: Vec<String>,
pub(super) rarity: u8,
pub(super) cost: u8,
pub(super) difficulty: u8,
@ -60,6 +59,21 @@ pub struct InnerStats {
pub(super) agility_bonus: u8,
}
// /// A type representing a weapon reload potential
// #[derive(Clone, Default)]
// #[cfg_attr(test, derive(Debug, PartialEq))]
// pub struct OptionnalReloadType(Option<u8>);
// /// Optionnal Weapon
// #[derive(Clone, Default)]
// #[cfg_attr(test, derive(Debug, PartialEq))]
// pub struct OptionnalWeaponCategory(Option<WeaponCategory>);
// /// Optionnal damage type
// #[derive(Clone, Default)]
// #[cfg_attr(test, derive(Debug, PartialEq))]
// pub struct OptionnalDamageType(Option<DamageType>);
/// Shape of the equipement, first mandatory part of an equipement
pub struct EqShape {
/// The inner stats of the part
@ -115,6 +129,9 @@ mod test {
assert_eq!(list_eq[0], eq1);
}
#[test]
fn test_creating_all_equipements() {}
#[ignore]
#[test]
fn test_default_behaviour() {

View File

@ -19,6 +19,7 @@ mod equipement_fields;
mod equipement_impl_database;
mod equipement_impl_io;
mod equipement_impl_logic;
mod equipement_impl_math;
pub mod equipement_structs;
// Republishes the most used symbols