1use crate::*;
2use std::collections::HashMap;
3use std::fmt::Debug;
4use std::hash::Hash;
5
6#[derive(new, Clone, Serialize, Deserialize, Debug, Builder)]
11pub struct SkillDefinition<K, E, S, I> {
12 pub key: S,
14 pub name: String,
16 pub friendly_name: String,
18 pub description: String,
20 pub cooldown: f64,
22 pub passive: bool,
25 pub conditions: Vec<StatCondition<K>>,
27 pub item_conditions: Vec<(I, usize, UseMode)>,
29 pub stat_effectors: Vec<E>,
31}
32
33impl<K: Hash + Eq + Debug, E, S, I: Hash + Eq + Clone + PartialEq + Debug>
41 SkillDefinition<K, E, S, I>
42{
43 pub fn check_conditions<IT: SlotType, CD: PartialEq + Default + Clone + Debug>(
45 &self,
46 stats: &StatSet<K>,
47 inventory: &Inventory<I, IT, CD>,
48 stat_defs: &StatDefinitions<K>,
49 ) -> bool {
50 for c in &self.conditions {
51 if !c.check(stats, stat_defs) {
52 return false;
53 }
54 }
55 for ic in &self.item_conditions {
56 if !inventory.has_quantity(&ic.0, ic.1) {
57 return false;
58 }
59 }
60 true
61 }
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize, new)]
68pub struct SkillInstance<S> {
69 pub skill_key: S,
71 pub current_cooldown: f64,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize, new)]
77pub struct SkillSet<S: Hash + Eq> {
78 pub skills: HashMap<S, SkillInstance<S>>,
80}
81
82impl<S: Hash + Eq + Clone> From<Vec<S>> for SkillSet<S> {
83 fn from(t: Vec<S>) -> Self {
84 let mut h = HashMap::new();
85 for s in t {
86 h.insert(s.clone(), SkillInstance::new(s, 0.0));
87 }
88 Self { skills: h }
89 }
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize, new)]
94pub struct SkillDefinitions<K, E, S: Hash + Eq, I> {
95 pub defs: HashMap<S, SkillDefinition<K, E, S, I>>,
97}
98
99impl<K, E, S: Hash + Eq, I> Default for SkillDefinitions<K, E, S, I> {
100 fn default() -> Self {
101 Self {
102 defs: HashMap::default(),
103 }
104 }
105}
106
107impl<K, E, S: Hash + Eq + Clone, I> From<Vec<SkillDefinition<K, E, S, I>>>
108 for SkillDefinitions<K, E, S, I>
109{
110 fn from(t: Vec<SkillDefinition<K, E, S, I>>) -> Self {
111 let defs = t
112 .into_iter()
113 .map(|s| (s.key.clone(), s))
114 .collect::<HashMap<_, _>>();
115 Self::new(defs)
116 }
117}