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

235 lines
7.7 KiB
Rust

// Workspace: weaponry
// File: weaponry/src/weaponry_structs/equipement_structs.rs
//! Base equipement structures
use std::{collections::HashMap, rc::Rc, sync::LazyLock};
use serde::{Deserialize, Serialize};
use struct_field_names_as_array::FieldNamesAsArray;
use crate::interfaces::{FilterFunction, FilterFunctionType};
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 InitialEquipementList {
/// Inner List of final equipements
pub(super) inner: Vec<Equipement>,
}
/// The final equipement, obtained by combining all parts
#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct Equipement {
/// The inner stats used by the equipement
pub(super) stats: InnerEquipementStats,
/// The filter, used to determine if adding is warranted
pub(super) filter_function: FilterFunction<InnerEquipementStats>,
}
impl Default for Equipement {
fn default() -> Self {
Self {
filter_function: FilterFunction::new(default_filter()),
stats: InnerEquipementStats::random_new(),
}
}
}
/// 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(&InnerEquipementStats, &InnerEquipementStats) -> 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
#[allow(clippy::missing_docs_in_private_items, missing_docs)]
#[derive(Clone, Default, FieldNamesAsArray, Debug, Deserialize, Serialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct InnerEquipementStats {
pub id: Vec<u8>,
pub name: Vec<String>,
pub(super) rarity: u8,
pub(super) cost: u8,
pub(super) difficulty: u8,
pub(super) tier: u8,
pub(super) damage: u8,
pub(super) p_damage: u8,
pub(super) precision: i8,
pub(super) rmax: u8,
pub(super) armor_break: u8,
pub(super) armor_pierce: u8,
pub(super) antimaterial: u8,
pub(super) damage_type: Option<DamageType>,
pub(super) protection: u8,
pub(super) integrity: u8,
pub(super) torque_two_handed: u8,
pub(super) torque_one_handed: u8,
pub(super) dodge: u8,
pub(super) parry: u8,
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>,
pub(super) range: u8,
pub(super) agility_bonus: u8,
}
#[allow(dead_code)]
/// A manually implemented hashmap of all values
// TODO: ADD A PROC MACRO TO AUTO_DERIVE THIS
static INNER_EQ_HASHMAP: LazyLock<HashMap<String, String>> = LazyLock::new(|| {
HashMap::from([
("id".to_string(), "INTEGER UNIQUE".to_string()),
("name".to_string(), "TEXT".to_string()),
("rarity".to_string(), "INTEGER".to_string()),
("cost".to_string(), "INTEGER".to_string()),
("difficulty".to_string(), "INTEGER".to_string()),
("tier".to_string(), "INTEGER".to_string()),
("damage".to_string(), "INTEGER".to_string()),
("p_damage".to_string(), "INTEGER".to_string()),
("precision".to_string(), "INTEGER".to_string()),
("rmax".to_string(), "INTEGER".to_string()),
("armor_break".to_string(), "INTEGER".to_string()),
("armor_pierce".to_string(), "INTEGER".to_string()),
("antimaterial".to_string(), "INTEGER".to_string()),
("damage_type".to_string(), "INTEGER".to_string()),
("protection".to_string(), "INTEGER".to_string()),
("integrity".to_string(), "INTEGER".to_string()),
("torque_two_handed".to_string(), "INTEGER".to_string()),
("torque_one_handed".to_string(), "INTEGER".to_string()),
("dodge".to_string(), "INTEGER".to_string()),
("parry".to_string(), "INTEGER".to_string()),
("speed".to_string(), "INTEGER".to_string()),
("stealth".to_string(), "INTEGER".to_string()),
("category".to_string(), "TEXT".to_string()),
("target".to_string(), "INTEGER".to_string()),
("special".to_string(), "TEXT".to_string()),
("action_cost".to_string(), "INTEGER".to_string()),
("reload_cost".to_string(), "INTEGER".to_string()),
("range".to_string(), "INTEGER".to_string()),
("agility_bonus".to_string(), "INTEGER".to_string()),
])
});
#[doc(hidden)]
macro_rules! r {
() => {
fastrand::u8(1..10)
};
}
// TODO: Make this a test requirement or hide it behind a profile
impl InnerEquipementStats {
/// Testing function to fuzz data
pub fn random_new() -> Self {
Self {
id: vec![r!()],
range: r!(),
damage: r!(),
p_damage: r!(),
protection: r!(),
..Default::default()
}
}
}
/// Shape of the equipement, first mandatory part of an equipement
#[derive(Default)]
pub struct EqShape {
/// The inner stats of the part
pub(super) stats: InnerEquipementStats,
}
/// Material used for the equipement, all equipements are crafted with 1-2 materials
#[derive(Default)]
pub struct EqMaterial {
/// The inner stats of the part
pub(super) stats: InnerEquipementStats,
}
/// Nature of the equipement, some natures are reserved and some can be chained
///
/// Deciding on the combinaison can be tricky an will need an extra field
#[derive(Default)]
pub struct EqNature {
/// The inner stats of the part
pub(super) stats: InnerEquipementStats,
}
/// Finishing touches, since all of them have a cost, they are treated in a special fashion
#[derive(Default)]
pub struct EqFinishing {
/// The inner stats of the part
pub(super) stats: InnerEquipementStats,
}
#[cfg(test)]
mod test {
use super::{
EqFinishing, EqMaterial, EqNature, EqShape, Equipement, InitialEquipementList,
InnerEquipementStats,
};
use crate::{
interfaces::RecipeCollection, weaponry_structs::equipement_structs::INNER_EQ_HASHMAP,
};
fn list_equip() -> Vec<Equipement> {
(0..10)
.map(|_| Equipement {
..Default::default()
})
.collect::<Vec<_>>()
}
#[test]
fn test_struct_creation() {
let _stats = InnerEquipementStats::default();
let _eq1 = Equipement {
..Default::default()
};
let _list_eq = list_equip();
}
#[test]
fn test_creating_all_equipements() {
let shape = vec![EqShape::default().into(), EqShape::default().into()];
let nature = vec![EqNature::default().into(), EqNature::default().into()];
let material = vec![EqMaterial::default().into(), EqMaterial::default().into()];
let finishing = vec![EqFinishing::default().into(), EqMaterial::default().into()];
let mut leq = InitialEquipementList::from_vectored(shape);
leq.combine_all(&InitialEquipementList::from_vectored(nature));
leq.combine_all(&InitialEquipementList::from_vectored(material));
leq.combine_all(&InitialEquipementList::from_vectored(finishing));
}
#[ignore]
#[test]
fn test_default_behaviour() {
unimplemented!()
}
#[test]
fn test_const_fields() {
let c = &INNER_EQ_HASHMAP;
assert!(!c.keys().len() > 0);
assert!(!c.values().len() > 0);
}
}