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