-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpass_3.rs
294 lines (262 loc) · 11.1 KB
/
pass_3.rs
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
use std::collections::BTreeMap;
use strum::IntoEnumIterator;
use super::item::{Attribute, EffectOperator, Item, Object};
use super::{Info, Pass, Ship};
/* Penalty factor: 1 / math.exp((1 / 2.67) ** 2) */
const PENALTY_FACTOR: f64 = 0.8691199808003974;
const OPERATOR_HAS_PENALTY: [EffectOperator; 5] = [
EffectOperator::PreMul,
EffectOperator::PostMul,
EffectOperator::PostPercent,
EffectOperator::PreDiv,
EffectOperator::PostDiv,
];
pub struct PassThree {}
#[derive(Default)]
struct Cache {
hull: BTreeMap<i32, f64>,
char: BTreeMap<i32, f64>,
structure: BTreeMap<i32, f64>,
target: BTreeMap<i32, f64>,
items: BTreeMap<usize, BTreeMap<i32, f64>>,
charge: BTreeMap<usize, BTreeMap<i32, f64>>,
skills: BTreeMap<usize, BTreeMap<i32, f64>>,
}
impl Attribute {
fn calculate_value(
&self,
info: &impl Info,
ship: &Ship,
cache: &mut Cache,
item: Object,
attribute_id: i32,
) -> f64 {
if self.value.is_some() {
return self.value.unwrap();
}
let cache_value = match item {
Object::Ship => cache.hull.get(&attribute_id),
Object::Char => cache.char.get(&attribute_id),
Object::Structure => cache.structure.get(&attribute_id),
Object::Target => cache.target.get(&attribute_id),
Object::Item(index) => cache.items.get(&index).and_then(|x| x.get(&attribute_id)),
Object::Charge(index) => cache.charge.get(&index).and_then(|x| x.get(&attribute_id)),
Object::Skill(index) => cache.skills.get(&index).and_then(|x| x.get(&attribute_id)),
};
if cache_value.is_some() {
return *cache_value.unwrap();
}
let mut current_value = self.base_value;
for operator in EffectOperator::iter() {
let mut values = (Vec::new(), Vec::new(), Vec::new());
/* Collect all the values for this operator. */
for effect in &self.effects {
if effect.operator != operator {
continue;
}
let source = match effect.source {
Object::Ship => &ship.hull,
Object::Item(index) => &ship.items[index],
Object::Charge(index) => match &ship.items[index].charge {
Some(charge) => &*charge,
None => continue,
},
Object::Skill(index) => &ship.skills[index],
Object::Char => &ship.char,
Object::Structure => &ship.structure,
Object::Target => &ship.target,
};
if effect.source_category > source.state {
continue;
}
let source_value = match source.attributes.get(&effect.source_attribute_id) {
Some(attribute) => attribute.calculate_value(
info,
ship,
cache,
effect.source,
effect.source_attribute_id,
),
None => {
let dogma_attribute = info.get_dogma_attribute(effect.source_attribute_id);
dogma_attribute.defaultValue
}
};
/* Simplify the values so we can do the math easier later on. */
let source_value = match operator {
EffectOperator::PreAssign => source_value,
EffectOperator::PreMul => source_value - 1.0,
EffectOperator::PreDiv => 1.0 / source_value - 1.0,
EffectOperator::ModAdd => source_value,
EffectOperator::ModSub => -source_value,
EffectOperator::PostMul => source_value - 1.0,
EffectOperator::PostDiv => 1.0 / source_value - 1.0,
EffectOperator::PostPercent => source_value / 100.0,
EffectOperator::PostAssign => source_value,
};
/* Check whether stacking penalty counts; negative and positive values have their own penalty. */
if effect.penalty && OPERATOR_HAS_PENALTY.contains(&effect.operator) {
if source_value < 0.0 {
values.2.push(source_value);
} else {
values.1.push(source_value);
}
} else {
values.0.push(source_value);
}
}
if values.0.is_empty() && values.1.is_empty() && values.2.is_empty() {
continue;
}
/* Apply the operator on the values. */
match operator {
EffectOperator::PreAssign | EffectOperator::PostAssign => {
let dogma_attribute = info.get_dogma_attribute(attribute_id);
current_value = if dogma_attribute.highIsGood {
*values
.0
.iter()
.max_by(|x, y| x.abs().partial_cmp(&y.abs()).unwrap())
.unwrap()
} else {
*values
.0
.iter()
.min_by(|x, y| x.abs().partial_cmp(&y.abs()).unwrap())
.unwrap()
};
assert!(values.1.is_empty());
assert!(values.2.is_empty());
}
EffectOperator::PreMul
| EffectOperator::PreDiv
| EffectOperator::PostMul
| EffectOperator::PostDiv
| EffectOperator::PostPercent => {
/* values.0 are non-stacking. */
for value in values.0 {
current_value *= 1.0 + value;
}
/* For positive values, the highest number goes first. For negative values, the lowest number. */
let sort_func = |x: &f64, y: &f64| y.abs().partial_cmp(&x.abs()).unwrap();
values.1.sort_by(sort_func);
values.2.sort_by(sort_func);
/* Apply positive stacking penalty. */
for (index, value) in values.1.iter().enumerate() {
current_value *= 1.0 + value * PENALTY_FACTOR.powi(index.pow(2) as i32);
}
/* Apply negative stacking penalty. */
for (index, value) in values.2.iter().enumerate() {
current_value *= 1.0 + value * PENALTY_FACTOR.powi(index.pow(2) as i32);
}
}
EffectOperator::ModAdd | EffectOperator::ModSub => {
for value in values.0 {
current_value += value;
}
assert!(values.1.is_empty());
assert!(values.2.is_empty());
}
}
}
match item {
Object::Ship => {
cache.hull.insert(attribute_id, current_value);
}
Object::Char => {
cache.char.insert(attribute_id, current_value);
}
Object::Structure => {
cache.structure.insert(attribute_id, current_value);
}
Object::Target => {
cache.target.insert(attribute_id, current_value);
}
Object::Item(index) => {
if !cache.items.contains_key(&index) {
cache.items.insert(index, BTreeMap::new());
}
cache
.items
.get_mut(&index)
.unwrap()
.insert(attribute_id, current_value);
}
Object::Charge(index) => {
if !cache.charge.contains_key(&index) {
cache.charge.insert(index, BTreeMap::new());
}
cache
.charge
.get_mut(&index)
.unwrap()
.insert(attribute_id, current_value);
}
Object::Skill(index) => {
if !cache.skills.contains_key(&index) {
cache.skills.insert(index, BTreeMap::new());
}
cache
.skills
.get_mut(&index)
.unwrap()
.insert(attribute_id, current_value);
}
}
current_value
}
}
impl Item {
fn calculate_values(&self, info: &impl Info, ship: &Ship, cache: &mut Cache, item: Object) {
for attribute_id in self.attributes.keys() {
self.attributes[&attribute_id].calculate_value(info, ship, cache, item, *attribute_id);
}
}
fn store_cached_values(&mut self, info: &impl Info, cache: &BTreeMap<i32, f64>) {
for (attribute_id, value) in cache {
if let Some(attribute) = self.attributes.get_mut(&attribute_id) {
attribute.value = Some(*value);
} else {
let dogma_attribute = info.get_dogma_attribute(*attribute_id);
let mut attribute = Attribute::new(dogma_attribute.defaultValue);
attribute.value = Some(*value);
self.attributes.insert(*attribute_id, attribute);
}
}
}
}
impl Pass for PassThree {
fn pass(info: &impl Info, ship: &mut Ship) {
let mut cache = Cache::default();
ship.hull
.calculate_values(info, ship, &mut cache, Object::Ship);
ship.char
.calculate_values(info, ship, &mut cache, Object::Char);
ship.structure
.calculate_values(info, ship, &mut cache, Object::Structure);
ship.target
.calculate_values(info, ship, &mut cache, Object::Target);
for (index, item) in ship.items.iter().enumerate() {
item.calculate_values(info, ship, &mut cache, Object::Item(index));
if let Some(charge) = &item.charge {
charge.calculate_values(info, ship, &mut cache, Object::Charge(index));
}
}
for (index, skill) in ship.skills.iter().enumerate() {
skill.calculate_values(info, ship, &mut cache, Object::Skill(index));
}
ship.hull.store_cached_values(info, &cache.hull);
ship.char.store_cached_values(info, &cache.char);
ship.structure.store_cached_values(info, &cache.structure);
ship.target.store_cached_values(info, &cache.target);
for (index, item) in ship.items.iter_mut().enumerate() {
item.store_cached_values(info, &cache.items[&index]);
if let Some(charge) = &mut item.charge {
charge.store_cached_values(info, &cache.charge[&index]);
}
}
for (index, skill) in ship.skills.iter_mut().enumerate() {
skill.store_cached_values(info, &cache.skills[&index]);
}
}
}