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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use derivative::*;
use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hash;
#[derive(Debug, Clone, Serialize, Deserialize, new, Builder)]
pub struct StatDefinition<K> {
pub key: K,
pub name: String,
pub friendly_name: String,
pub default_value: f64,
#[new(default)]
pub min_value: Option<f64>,
#[new(default)]
pub max_value: Option<f64>,
#[new(default)]
pub icon_path: Option<String>,
}
impl<K: Clone> StatDefinition<K> {
pub fn default_instance(&self) -> StatInstance<K> {
StatInstance::new(self.key.clone(), self.default_value)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, new, Builder)]
pub struct StatInstance<K> {
pub key: K,
pub value: f64,
#[new(value = "value")]
pub value_with_effectors: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize, new)]
pub struct StatDefinitions<K: Hash + Eq> {
pub defs: HashMap<K, StatDefinition<K>>,
}
impl<K: Hash + Eq> Default for StatDefinitions<K> {
fn default() -> Self {
Self {
defs: HashMap::default(),
}
}
}
impl<K: Hash + Eq + Clone> StatDefinitions<K> {
pub fn to_statset(&self) -> StatSet<K> {
let instances = self
.defs
.iter()
.map(|(k, v)| (k.clone(), v.default_instance()))
.collect::<HashMap<_, _>>();
StatSet::new(instances)
}
}
impl<K: Hash + Eq + Clone> From<Vec<StatDefinition<K>>> for StatDefinitions<K> {
fn from(t: Vec<StatDefinition<K>>) -> Self {
let defs = t
.into_iter()
.map(|s| (s.key.clone(), s))
.collect::<HashMap<_, _>>();
Self::new(defs)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, new)]
pub struct EffectorDefinitions<K, E: Hash + Eq> {
pub defs: HashMap<E, EffectorDefinition<K, E>>,
}
impl<K, E: Hash + Eq> Default for EffectorDefinitions<K, E> {
fn default() -> Self {
Self {
defs: HashMap::default(),
}
}
}
impl<K: Hash + Eq + Clone, E: Hash + Eq + Clone> From<Vec<EffectorDefinition<K, E>>>
for EffectorDefinitions<K, E>
{
fn from(t: Vec<EffectorDefinition<K, E>>) -> Self {
let defs = t
.into_iter()
.map(|s| (s.key.clone(), s))
.collect::<HashMap<_, _>>();
Self::new(defs)
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, new)]
pub struct StatSet<K: Hash + Eq> {
pub stats: HashMap<K, StatInstance<K>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, new)]
pub struct EffectorSet<E> {
pub effectors: Vec<EffectorInstance<E>>,
}
impl<E> Default for EffectorSet<E> {
fn default() -> Self {
Self { effectors: vec![] }
}
}
impl<E: Hash + Eq> EffectorSet<E> {
pub fn apply_to<K: Eq + Hash>(
self: &Self,
effector_defs: &EffectorDefinitions<K, E>,
stat_set: &mut StatSet<K>,
_delta_time: f32,
) {
for mut s in stat_set.stats.values_mut() {
let mut new_value = s.value;
let mut multiplicative_multiplier = 1.0;
let mut additive_multiplier = 0.0;
let mut additive = 0.0;
for e in self.effectors.iter() {
let def = effector_defs
.defs
.get(&e.effector_key)
.expect("Tried to get unknown stat key.");
for (key, ty) in def.effects.iter() {
if *key == s.key {
match ty {
EffectorType::Additive(v) => additive += v,
EffectorType::AdditiveMultiplier(v) => additive_multiplier += v,
EffectorType::MultiplicativeMultiplier(v) => {
multiplicative_multiplier *= v
}
}
}
}
}
let multiplier = multiplicative_multiplier + additive_multiplier;
new_value += additive;
new_value *= multiplier;
s.value_with_effectors = new_value;
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, new)]
pub struct StatCondition<K> {
pub stat_key: K,
pub condition: StatConditionType,
}
impl<K: Hash + Eq + Debug> StatCondition<K> {
pub fn check(&self, stats: &StatSet<K>, stat_defs: &StatDefinitions<K>) -> bool {
let v = stats.stats.get(&self.stat_key).expect(&format!(
"Requested stat key {:?} is not in provided StatSet.",
self.stat_key
));
let def = stat_defs.defs.get(&self.stat_key).expect(&format!(
"Requested stat key {:?} is not in provided StatDefinitions.",
self.stat_key
));
self.condition
.is_true(v.value, def.min_value, def.max_value)
}
}
#[derive(Clone, Serialize, Deserialize, new, Derivative)]
#[derivative(Debug)]
pub enum StatConditionType {
MinValue(f64),
BetweenValue(f64, f64),
MaxValue(f64),
MinPercent(f64),
BetweenPercent(f64, f64),
MaxPercent(f64),
DivisibleBy(i32),
#[serde(skip)]
Custom(#[derivative(Debug = "ignore")] fn(f64) -> bool),
}
impl StatConditionType {
pub fn is_true(&self, value: f64, min_value: Option<f64>, max_value: Option<f64>) -> bool {
let percent = if let (Some(min_value), Some(max_value)) = (min_value, max_value) {
Some((value - min_value) / (max_value - min_value))
} else {
None
};
match &*self {
StatConditionType::MinValue(v) => value >= *v,
StatConditionType::BetweenValue(min, max) => value >= *min && value <= *max,
StatConditionType::MaxValue(v) => value <= *v,
StatConditionType::MinPercent(p) => {
percent.expect("This stat doesn't have min/max values.") >= *p
}
StatConditionType::BetweenPercent(min, max) => {
percent.expect("This stat doesn't have min/max values.") >= *min
&& percent.expect("This stat doesn't have min/max values.") <= *max
}
StatConditionType::MaxPercent(p) => {
percent.expect("This stat doesn't have min/max values.") <= *p
}
StatConditionType::DivisibleBy(p) => value as i32 % p == 0,
StatConditionType::Custom(e) => e(value),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, new)]
pub struct EffectorDefinition<K, E> {
pub key: E,
pub duration: Option<f64>,
pub effects: Vec<(K, EffectorType)>,
}
#[derive(Debug, Clone, Serialize, Deserialize, new)]
pub enum EffectorType {
Additive(f64),
AdditiveMultiplier(f64),
MultiplicativeMultiplier(f64),
}
#[derive(Debug, Clone, Serialize, Deserialize, new)]
pub struct EffectorInstance<E> {
pub effector_key: E,
pub disable_in: Option<f64>,
}