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

328 lines
9.8 KiB
Rust

// Workspace: weaponry
// File: weaponry/src/weaponry_structs/equipement_structs.rs
//! Base equipement structures
use std::rc::Rc;
use crate::interfaces::{FilterFunction, FilterFunctionType};
use more_fields::FieldsAsSlice;
use serde::{Deserialize, Serialize};
use super::{DamageType, SpecialCategory, WeaponCategory, equipement_fields::WeaponMacroCategory};
/// 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, FieldsAsSlice, Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct InnerEquipementStats {
pub id: Vec<u8>,
pub name: Vec<String>,
pub(super) rarity: i8,
pub(super) cost: i8,
pub(super) difficulty: i8,
pub(super) tier: i8,
pub(super) damage: i8,
pub(super) p_damage: i8,
pub(super) precision: i8,
pub(super) rmax: i8,
pub(super) armor_break: i8,
pub(super) armor_pierce: i8,
pub(super) antimaterial: i8,
pub(super) damage_type: Option<DamageType>,
pub(super) protection: i8,
pub(super) integrity: i8,
pub(super) torque_two_handed: Option<i8>, // Might be option (single claw)
pub(super) torque_one_handed: Option<i8>, // Should be option (bows)
pub(super) dodge: f32, // I could also use a custom HalfValue
pub(super) parry: i8,
pub(super) speed: i8,
pub(super) stealth: i8,
/// The initial macro category, in turn combined with other stats it will describe a final category
pub(super) macro_category: Option<WeaponMacroCategory>, // TODO : This initial macro category fill as a final stat feed the final category
pub(super) target: Option<WeaponMacroCategory>, // TODO: This might be incorrect, to check
pub(super) special: SpecialCategory,
pub(super) action_cost: i8,
pub(super) reload_cost: Option<i8>, // Shouldn't it be Option ?
pub(super) range: i16, // Shoudln't it be Option ?
pub(super) agility_bonus: f32, // Also convert this one to HalfValue
}
// TODO: This implementation of default is painfull
// TODO: Have either an auto-macro implementation or a manual one
// TODO: This implementation is Quasi-redondant
// TODO: Have a way to limit code duplication
#[allow(clippy::missing_docs_in_private_items, missing_docs)]
#[derive(Clone, FieldsAsSlice, Debug, Deserialize, Serialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct DatabaseEquipementStats {
#[INTEGER]
pub id: u8,
pub meta_category: String,
pub name: String,
#[INTEGER]
pub rarity: i8,
#[INTEGER]
pub cost: i8,
#[INTEGER]
pub difficulty: i8,
#[INTEGER]
pub tier: i8,
#[INTEGER]
pub damage: i8,
#[INTEGER]
pub p_damage: i8,
#[INTEGER]
pub precision: i8,
#[INTEGER]
pub rmax: i8,
#[INTEGER]
pub armor_break: i8,
#[INTEGER]
pub armor_pierce: i8,
#[INTEGER]
pub antimaterial: i8,
pub damage_type: String,
#[INTEGER]
pub protection: i8,
#[INTEGER]
pub integrity: i8,
pub torque_two_handed: String,
pub torque_one_handed: String,
#[REAL]
pub dodge: f32,
#[INTEGER]
pub parry: i8,
#[INTEGER]
pub speed: i8,
#[INTEGER]
pub stealth: i8,
pub category: String,
pub target: String,
pub special: String,
#[INTEGER]
pub action_cost: i8,
pub reload_cost: String,
#[INTEGER]
pub range: i8,
#[REAL]
pub agility_bonus: f32,
}
impl Default for DatabaseEquipementStats {
fn default() -> Self {
Self {
id: 0,
meta_category: "EqShape".to_string(),
name: String::new(),
damage_type: serde_json::to_string(&None::<DamageType>).unwrap(),
category: serde_json::to_string(&None::<WeaponMacroCategory>).unwrap(),
target: serde_json::to_string(&None::<WeaponCategory>).unwrap(),
special: serde_json::to_string(&SpecialCategory::default()).unwrap(),
reload_cost: serde_json::to_string(&None::<u8>).unwrap(),
rarity: 0,
cost: 0,
difficulty: 0,
tier: 0,
damage: 0,
p_damage: 0,
precision: 0,
rmax: 0,
armor_break: 0,
armor_pierce: 0,
antimaterial: 0,
protection: 0,
integrity: 0,
torque_two_handed: serde_json::to_string(&0).unwrap(),
torque_one_handed: serde_json::to_string(&None::<u8>).unwrap(),
dodge: 0.,
parry: 0,
speed: 0,
stealth: 0,
range: 0,
agility_bonus: 0.,
action_cost: 0,
}
}
}
#[allow(dead_code)]
/// A manually implemented hashmap of all values
#[doc(hidden)]
macro_rules! r {
() => {
fastrand::i8(1..i8::MAX)
};
}
// 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!() as u8],
range: (r!() - 10) as i16,
damage: r!() - 10,
p_damage: r!() - 10,
protection: r!() - 10,
..Default::default()
}
}
}
// TODO: Make this a test requirement or hide it behind a profile
impl DatabaseEquipementStats {
/// Testing function to fuzz data
pub fn random_new() -> Self {
Self {
id: r!() as u8,
range: r!() - 19,
damage: r!() - 10,
p_damage: r!() - 10,
protection: r!() - 10,
..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,
}
impl From<InnerEquipementStats> for EqShape {
fn from(value: InnerEquipementStats) -> Self {
Self { stats: value }
}
}
/// 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,
}
impl From<InnerEquipementStats> for EqMaterial {
fn from(value: InnerEquipementStats) -> Self {
Self { stats: value }
}
}
/// 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,
}
impl From<InnerEquipementStats> for EqNature {
fn from(value: InnerEquipementStats) -> Self {
Self { stats: value }
}
}
/// 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,
}
impl From<InnerEquipementStats> for EqFinishing {
fn from(value: InnerEquipementStats) -> Self {
Self { stats: value }
}
}
#[cfg(test)]
mod test {
use super::{
EqFinishing, EqMaterial, EqNature, EqShape, Equipement, InitialEquipementList,
InnerEquipementStats,
};
use crate::interfaces::RecipeCollection;
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()];
// Commented because of the generic nature and the from not a tryfrom
// TODO: Reinstate this part
// 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!()
}
}