esf-dogma-engine 10.2.0

Calculates the dogma attributes of EVE Online ship fits
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::collections::BTreeSet;

use esf_data::eve;

use super::attribute_ids::{ATTRIBUTE_CAPACITOR_NEED_ID, ATTRIBUTE_SKILLS};
use super::item::{Attribute, Effect, EffectCategory, EffectOperator, Item, Object};
use super::{Info, Objects, Pass};

/** Categories of the effect source which are exempt of stacking penalty.
 * Ship (6), Charge (8), Skill (16), Implant (20), Subsystem (32) and Structure (65) */
const EXEMPT_PENALTY_CATEGORY_IDS: [i32; 6] = [6, 8, 16, 20, 32, 65];

pub struct PassTwo {}

/* Variant names mirror the dogma modifier names used by EVE. */
#[allow(clippy::enum_variant_names)]
#[derive(Debug)]
enum Modifier {
    LocationRequiredSkillModifier(i32),
    LocationGroupModifier(i32),
    LocationModifier(),
    OwnerRequiredSkillModifier(i32),
    ItemModifier(),
}

#[derive(Debug)]
struct Pass2Effect {
    effect_id: i32,
    modifier: Modifier,
    operator: EffectOperator,
    source: Object,
    source_category: EffectCategory,
    source_attribute_id: i32,
    source_quantity: u32,
    target: Object,
    target_attribute_id: i32,
}

fn get_modifier_func(
    func: eve::ModifierFunc,
    skill_type_id: i32,
    group_id: i32,
) -> Option<Modifier> {
    match func {
        eve::ModifierFunc::LocationRequiredSkillModifier => {
            Some(Modifier::LocationRequiredSkillModifier(skill_type_id))
        }
        eve::ModifierFunc::LocationGroupModifier => Some(Modifier::LocationGroupModifier(group_id)),
        eve::ModifierFunc::LocationModifier => Some(Modifier::LocationModifier()),
        eve::ModifierFunc::ItemModifier => Some(Modifier::ItemModifier()),
        eve::ModifierFunc::OwnerRequiredSkillModifier => {
            Some(Modifier::OwnerRequiredSkillModifier(skill_type_id))
        }
        /* EffectStopper has no effect on the attributes; just on what you can bring online. */
        eve::ModifierFunc::EffectStopper => None,
        func => panic!("Unknown modifier func: {:?}", func),
    }
}

const STRUCTURE_CATEGORY_ID: i32 = 65;

fn get_target_object(domain: eve::ModifierDomain, origin: Object) -> Object {
    match domain {
        eve::ModifierDomain::ShipID => Object::Ship,
        eve::ModifierDomain::CharID => Object::Char,
        eve::ModifierDomain::OtherID => match origin {
            Object::Item(index) => Object::Charge(index),
            Object::Charge(index) => Object::Item(index),
            _ => panic!("Invalid origin for OtherID domain"),
        },
        /* On a structure fit the hull is the structure. */
        eve::ModifierDomain::StructureID => Object::Ship,
        eve::ModifierDomain::ItemID => origin,
        eve::ModifierDomain::TargetID => Object::Target,
        eve::ModifierDomain::Target => Object::Target,
        domain => panic!("Unknown modifier domain: {:?}", domain),
    }
}

fn for_each_in_location(
    objects: &mut Objects,
    location: Object,
    mut apply: impl FnMut(Object, &mut Item),
) {
    match location {
        Object::Ship => {
            apply(Object::Ship, &mut objects.ship);

            for (index, item) in objects.items.iter_mut().enumerate() {
                if !item.is_in_ship() {
                    continue;
                }

                apply(Object::Item(index), item);

                if let Some(charge) = &mut item.charge {
                    apply(Object::Charge(index), charge);
                }
            }
        }
        Object::Char => {
            apply(Object::Char, &mut objects.char);
            for (index, skill) in objects.skills.iter_mut().enumerate() {
                apply(Object::Skill(index), skill);
            }
        }
        Object::Item(_) | Object::Charge(_) | Object::Skill(_) | Object::Target => {
            apply(location, objects.get_mut(location).unwrap())
        }
    }
}

fn get_effect_category(category: eve::EffectCategory) -> EffectCategory {
    match category {
        eve::EffectCategory::Passive => EffectCategory::Passive,
        eve::EffectCategory::Active => EffectCategory::Active,
        eve::EffectCategory::Target => EffectCategory::Target,
        eve::EffectCategory::Area => EffectCategory::Area,
        eve::EffectCategory::Online => EffectCategory::Online,
        eve::EffectCategory::Overload => EffectCategory::Overload,
        eve::EffectCategory::Dungeon => EffectCategory::Dungeon,
        eve::EffectCategory::System => EffectCategory::System,
        category => panic!("Unknown effect category: {:?}", category),
    }
}

fn get_effect_operator(operation: eve::ModifierOperation) -> Option<EffectOperator> {
    match operation {
        eve::ModifierOperation::PreAssign => Some(EffectOperator::PreAssign),
        eve::ModifierOperation::PreMul => Some(EffectOperator::PreMul),
        eve::ModifierOperation::PreDiv => Some(EffectOperator::PreDiv),
        eve::ModifierOperation::ModAdd => Some(EffectOperator::ModAdd),
        eve::ModifierOperation::ModSub => Some(EffectOperator::ModSub),
        eve::ModifierOperation::PostMul => Some(EffectOperator::PostMul),
        eve::ModifierOperation::PostDiv => Some(EffectOperator::PostDiv),
        eve::ModifierOperation::PostPercent => Some(EffectOperator::PostPercent),
        eve::ModifierOperation::PostAssign => Some(EffectOperator::PostAssign),
        /* Operator 9 calculates Skill Level based on Skill Points; irrelevant
         * for fits. It has no name in the schema, so match on the raw value. */
        eve::ModifierOperation(9) => None,
        eve::ModifierOperation::Unset => panic!("Effect operation is not set"),
        operation => panic!("Unknown effect operation: {:?}", operation),
    }
}

impl Item {
    fn required_skills(&self) -> BTreeSet<i32> {
        ATTRIBUTE_SKILLS
            .iter()
            .filter_map(|attribute_skill_id| self.attributes.get(attribute_skill_id))
            .map(|attribute| attribute.base_value as i32)
            .collect()
    }

    fn add_effect(
        &mut self,
        info: &impl Info,
        target: Object,
        attribute_id: i32,
        source_category_id: i32,
        effect: &Pass2Effect,
    ) {
        let attr = info.get_dogma_attribute(attribute_id);

        /* Penalties are only count when an attribute is not stackable and when the item is not in the exempt category. */
        let stackable = attr.is_some_and(|attr| attr.stackable());
        let penalty = !stackable && !EXEMPT_PENALTY_CATEGORY_IDS.contains(&source_category_id);

        let attribute = self.attributes.entry(attribute_id).or_insert_with(|| {
            Attribute::new(attr.map_or(0.0, |attr| attr.default_value() as f64))
        });
        attribute.effects.push(Effect {
            effect_id: effect.effect_id,
            operator: effect.operator,
            penalty,
            source: effect.source,
            source_category: effect.source_category,
            source_attribute_id: effect.source_attribute_id,
            /* A stack bonuses others once per item, but itself only once. */
            quantity: if target == effect.source {
                1
            } else {
                effect.source_quantity
            },
        });
    }

    fn collect_effects(
        &mut self,
        info: &impl Info,
        origin: Object,
        skip_ship_domain: bool,
        effects: &mut Vec<Pass2Effect>,
    ) {
        for dogma_effect in info.get_dogma_effects(self.type_id).into_iter().flatten() {
            let Some(type_dogma_effect) = info.get_dogma_effect(dogma_effect.effect_id()) else {
                continue;
            };
            let category = get_effect_category(type_dogma_effect.effect_category());

            /* Find the highest state an item can be in. */
            if category > self.max_state && category <= EffectCategory::Overload {
                self.max_state = category;
            }

            /* Every non-passive effect of a fighter is an ability, and the fit picks which run. */
            if self.is_fighter() && category != EffectCategory::Passive {
                let used = match &self.fighter_abilities {
                    Some(abilities) => abilities.contains(&dogma_effect.effect_id()),
                    None => dogma_effect.is_default(),
                };
                if !used {
                    continue;
                }
            }

            let modifiers = type_dogma_effect
                .modifiers()
                .filter(|modifiers| !modifiers.is_empty());

            let Some(modifiers) = modifiers else {
                self.effects.push(dogma_effect.effect_id());
                continue;
            };

            for modifier in modifiers {
                let effect_modifier = get_modifier_func(
                    modifier.func(),
                    modifier.skill_type_id(),
                    modifier.group_id(),
                );
                let Some(effect_modifier) = effect_modifier else {
                    continue;
                };

                let Some(operator) = get_effect_operator(modifier.operation()) else {
                    continue;
                };

                /* If the origin is an Item(), the domain is OtherID, but there is no charge, skip the effect. */
                if matches!(origin, Object::Item(_))
                    && modifier.domain() == eve::ModifierDomain::OtherID
                    && self.charge.is_none()
                {
                    continue;
                }

                if skip_ship_domain && modifier.domain() == eve::ModifierDomain::ShipID {
                    continue;
                }

                let target = get_target_object(modifier.domain(), origin);
                effects.push(Pass2Effect {
                    effect_id: dogma_effect.effect_id(),
                    modifier: effect_modifier,
                    operator,
                    source: origin,
                    source_category: category,
                    source_attribute_id: modifier.modifying_attribute_id(),
                    source_quantity: self.quantity,
                    target,
                    target_attribute_id: modifier.modified_attribute_id(),
                });
            }
        }

        /* Any module that has a capacitorNeed, can be activated. */
        if self.attributes.contains_key(&ATTRIBUTE_CAPACITOR_NEED_ID)
            && self.max_state < EffectCategory::Active
        {
            self.max_state = EffectCategory::Active;
        }

        if self.state > self.max_state {
            self.state = self.max_state;
        }
    }
}

impl Pass for PassTwo {
    fn pass(info: &impl Info, objects: &mut Objects) {
        let mut effects = Vec::new();

        /* Collect all the effects in a single list. */
        objects
            .ship
            .collect_effects(info, Object::Ship, false, &mut effects);
        objects
            .char
            .collect_effects(info, Object::Char, false, &mut effects);
        for (index, item) in objects.items.iter_mut().enumerate() {
            if !item.is_calculated() {
                continue;
            }

            item.collect_effects(info, Object::Item(index), false, &mut effects);
            if let Some(charge) = &mut item.charge {
                charge.collect_effects(info, Object::Charge(index), false, &mut effects);
            }
        }
        /* A structure is not the pilot's ship; only the structure skills, via the structure domain, reach it. */
        let structure_fit = objects.ship.category_id == STRUCTURE_CATEGORY_ID;
        for (index, skill) in objects.skills.iter_mut().enumerate() {
            skill.collect_effects(info, Object::Skill(index), structure_fit, &mut effects);
        }

        let ship_skills = objects.ship.required_skills();
        let item_skills: Vec<_> = objects
            .items
            .iter()
            .map(|item| {
                let charge_skills = item.charge.as_ref().map(|charge| charge.required_skills());
                (item.required_skills(), charge_skills.unwrap_or_default())
            })
            .collect();

        /* Depending on the modifier, move the effects to the correct attribute. */
        for effect in effects {
            let source = objects.get(effect.source).unwrap();
            let source_type_id = source.type_id;
            let category_id = source.category_id;

            match effect.modifier {
                Modifier::ItemModifier() => {
                    let target = objects.get_mut(effect.target).unwrap();

                    target.add_effect(
                        info,
                        effect.target,
                        effect.target_attribute_id,
                        category_id,
                        &effect,
                    );
                }
                Modifier::LocationModifier() => {
                    for_each_in_location(objects, effect.target, |target, item| {
                        item.add_effect(
                            info,
                            target,
                            effect.target_attribute_id,
                            category_id,
                            &effect,
                        );
                    });
                }
                Modifier::LocationGroupModifier(group_id) => {
                    for_each_in_location(objects, effect.target, |target, item| {
                        if item.group_id == group_id {
                            item.add_effect(
                                info,
                                target,
                                effect.target_attribute_id,
                                category_id,
                                &effect,
                            );
                        }
                    });
                }
                Modifier::OwnerRequiredSkillModifier(skill_type_id)
                | Modifier::LocationRequiredSkillModifier(skill_type_id) => {
                    /* Some skills apply on -1, indicating they should apply on anything that uses that skill. */
                    let skill_type_id = if skill_type_id == -1 {
                        source_type_id
                    } else {
                        skill_type_id
                    };
                    let location_only =
                        matches!(effect.modifier, Modifier::LocationRequiredSkillModifier(_));

                    if ship_skills.contains(&skill_type_id) {
                        objects.ship.add_effect(
                            info,
                            Object::Ship,
                            effect.target_attribute_id,
                            category_id,
                            &effect,
                        );
                    }

                    for (index, (item, (skills, charge_skills))) in objects
                        .items
                        .iter_mut()
                        .zip(&item_skills)
                        .enumerate()
                        .filter(|(_, (item, _))| !location_only || item.is_in_ship())
                    {
                        if skills.contains(&skill_type_id) {
                            item.add_effect(
                                info,
                                Object::Item(index),
                                effect.target_attribute_id,
                                category_id,
                                &effect,
                            );
                        }

                        if let Some(charge) = &mut item.charge
                            && charge_skills.contains(&skill_type_id)
                        {
                            charge.add_effect(
                                info,
                                Object::Charge(index),
                                effect.target_attribute_id,
                                category_id,
                                &effect,
                            );
                        }
                    }
                }
            }
        }
    }
}