ravenous-tools/weaponry/src/weaponry_structs/equipement_impl_math.rs

117 lines
4.1 KiB
Rust

// Workspace: weaponry
// File: weaponry/src/weaponry_structs/equipement_math.rs
//! Implements base math for each field
//!
use super::equipement_structs::InnerEquipementStats;
use crate::weaponry_structs::{SpecialField, WeaponBaseShape};
/// Add a new vector to a clone of a inner field
macro_rules! add_to_vec {
($self: expr, $rhs: expr) => {{
let mut new = $self.clone();
new.append(&mut $rhs.clone());
new
}};
}
/// Adds either of the vectors or merge them
macro_rules! add_two_options {
($self: expr, $rhs: expr) => {{
match ($self.as_ref(), $rhs.as_ref()) {
(None, None) => None,
(None, Some(a)) => Some(a.clone()),
(Some(a), None) => Some(a.clone()),
(Some(a), Some(b)) => Some(a.clone() + b.clone()),
}
}};
}
/// Clones the first option that has some data
macro_rules! first_correct_option {
($self:expr, $rhs:expr) => {
match ($self.as_ref(), $rhs.as_ref()) {
(Some(a), _) => Some(a.clone()),
(_, Some(b)) => Some(b.clone()),
_ => None,
}
};
}
impl std::ops::Add for &InnerEquipementStats {
type Output = InnerEquipementStats;
fn add(self, rhs: Self) -> Self::Output {
InnerEquipementStats {
id: add_to_vec!(self.id, rhs.id),
name: add_to_vec!(self.name, rhs.name),
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: {
if self.armor_pierce > 0
|| matches!(self.category_base, Some(WeaponBaseShape::Ranged))
{
self.armor_pierce + rhs.armor_pierce
} else {
0
}
},
antimaterial: self.antimaterial + rhs.antimaterial,
damage_type: add_two_options!(self.damage_type, rhs.damage_type),
protection: self.protection + rhs.protection,
integrity: self.integrity + rhs.integrity,
torque_two_handed: add_two_options!(self.torque_two_handed, rhs.torque_two_handed),
torque_one_handed: add_two_options!(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,
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,
category_base: first_correct_option!(self.category_base, rhs.category_base),
category_size: first_correct_option!(self.category_size, rhs.category_size),
target_category: first_correct_option!(self.target_category, rhs.target_category),
}
}
}
impl std::ops::Add for SpecialField {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let mut c = self.clone();
c.append(&mut rhs.clone());
c
}
}
#[cfg(test)]
mod test {
#[test]
fn test_macros() {
assert_eq!(first_correct_option!(None::<i8>, None::<i8>), None);
assert_eq!(first_correct_option!(Some(3), None::<i8>), Some(3));
assert_eq!(first_correct_option!(None::<i8>, Some(4)), Some(4));
assert_eq!(first_correct_option!(Some(1), Some(3)), Some(1));
assert_eq!(add_two_options!(Some(1), Some(3)), Some(4));
assert_eq!(add_two_options!(Some(3), None::<i8>), Some(3));
assert_eq!(add_two_options!(None::<i8>, Some(4)), Some(4));
let a = vec![1, 2];
let b = vec![3];
assert_eq!(add_to_vec!(a, b), vec![1, 2, 3]);
}
}