esf-dogma-engine 13.0.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use std::collections::BTreeSet;

use esf_data::eve;

use super::attribute_ids::{
    ATTRIBUTE_CAPACITOR_NEED_ID, ATTRIBUTE_REMOTE_RESISTANCE_ID, ATTRIBUTE_SKILLS,
};
use super::item::{
    Attribute, Effect, EffectCategory, EffectOperator, Item, ItemState, Object, Origin,
};
use super::{Info, Objects, Pass};
use crate::projection::ProjectedBuff;

/** 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 {
    origin: Origin,
    modifier: Modifier,
    operator: EffectOperator,
    source_quantity: u32,
    target: Object,
    target_attribute_id: i32,
    resistance: Option<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) -> Option<Object> {
    match domain {
        eve::ModifierDomain::ShipID => Some(Object::Ship),
        eve::ModifierDomain::CharID => Some(Object::Char),
        eve::ModifierDomain::OtherID => match origin {
            Object::Item(index) => Some(Object::Charge(index)),
            Object::Charge(index) => Some(Object::Item(index)),
            _ => panic!("Invalid origin for OtherID domain"),
        },
        /* On a structure fit the hull is the structure. */
        eve::ModifierDomain::StructureID => Some(Object::Ship),
        eve::ModifierDomain::ItemID => Some(origin),
        eve::ModifierDomain::TargetID | eve::ModifierDomain::Target => None,
        domain => panic!("Unknown modifier domain: {:?}", domain),
    }
}

fn get_projected_object(domain: eve::ModifierDomain) -> Option<Object> {
    match domain {
        eve::ModifierDomain::ShipID
        | eve::ModifierDomain::StructureID
        | eve::ModifierDomain::TargetID
        | eve::ModifierDomain::Target => Some(Object::Ship),
        eve::ModifierDomain::CharID => Some(Object::Char),
        eve::ModifierDomain::ItemID | eve::ModifierDomain::OtherID => None,
        domain => panic!("Unknown modifier domain: {:?}", domain),
    }
}

fn collect_projected_effects(
    info: &impl Info,
    item: &Item,
    origin: Object,
    effect_id: i32,
    effects: &mut Vec<Pass2Effect>,
) {
    let Some(effect) = info.get_dogma_effect(effect_id) else {
        return;
    };
    let category = get_effect_category(effect.effect_category());
    let resistance = get_resistance(item, &effect);

    for modifier in effect.modifiers().into_iter().flatten() {
        let Some(target) = get_projected_object(modifier.domain()) else {
            continue;
        };
        let modifier_func = get_modifier_func(
            modifier.func(),
            modifier.skill_type_id(),
            modifier.group_id(),
        );
        let Some(effect_modifier) = modifier_func else {
            continue;
        };
        let Some(operator) = get_effect_operator(modifier.operation()) else {
            continue;
        };

        effects.push(Pass2Effect {
            origin: Origin::Effect {
                effect_id,
                source: origin,
                source_category: category,
                attribute_id: modifier.modifying_attribute_id(),
            },
            modifier: effect_modifier,
            operator,
            source_quantity: 1,
            target,
            target_attribute_id: modifier.modified_attribute_id(),
            resistance,
        });
    }
}

fn get_resistance(item: &Item, effect: &eve::DogmaEffect) -> Option<i32> {
    let named = item
        .attributes
        .get(&ATTRIBUTE_REMOTE_RESISTANCE_ID)
        .map_or(0, |attribute| attribute.base_value as i32);

    match (named, effect.resistance_attribute_id()) {
        (0, 0) => None,
        (0, from_effect) => Some(from_effect),
        (named, _) => Some(named),
    }
}

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);
            }
            for (index, item) in objects.items.iter_mut().enumerate() {
                if item.is_on_char() {
                    apply(Object::Item(index), item);
                }
            }
        }
        Object::Mode
        | Object::Item(_)
        | Object::Charge(_)
        | Object::Skill(_)
        | Object::Projected(_) => apply(location, objects.get_mut(location).unwrap()),
    }
}

pub(super) 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),
    }
}

/* A buff carries no effects: its collection names the attributes it changes,
 * on whom and with what operation, and the buff itself holds the strength. It
 * lands on the ship, the same as a modifier with the ship domain. */
fn collect_buff_effects(info: &impl Info, buffs: &[ProjectedBuff], effects: &mut Vec<Pass2Effect>) {
    for buff in buffs {
        let Some(collection) = info.get_dbuff_collection(buff.id) else {
            continue;
        };
        let Some(operator) = get_effect_operator(collection.operation()) else {
            continue;
        };

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

            effects.push(Pass2Effect {
                origin: Origin::Buff {
                    buff_id: buff.id,
                    value: buff.value,
                },
                modifier: effect_modifier,
                operator,
                source_quantity: 1,
                target: Object::Ship,
                target_attribute_id: modifier.modified_attribute_id(),
                resistance: None,
            });
        }
    }
}

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 {
            origin: effect.origin,
            operator: effect.operator,
            penalty,
            /* A stack bonuses others once per item, but itself only once. */
            quantity: match effect.origin {
                Origin::Effect { source, .. } if source == target => 1,
                _ => effect.source_quantity,
            },
            resistance: effect.resistance,
        });
    }

    fn collect_effects(
        &mut self,
        info: &impl Info,
        origin: Object,
        skip_ship_domain: bool,
        effects: &mut Vec<Pass2Effect>,
    ) {
        let own_effects = info.get_dogma_effects(self.type_id);
        /* Like its attributes, a mutated item has the effects of its base too. */
        let base_effects = self
            .mutation_base
            .and_then(|base| info.get_dogma_effects(base))
            .into_iter()
            .flatten()
            .filter(|base_effect| {
                !own_effects.is_some_and(|own_effects| {
                    own_effects
                        .iter()
                        .any(|effect| effect.effect_id() == base_effect.effect_id())
                })
            });

        for dogma_effect in own_effects.into_iter().flatten().chain(base_effects) {
            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 let Some(state) = category.required_state() {
                self.max_state = self.max_state.max(state);
            }

            /* 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;
                }
            }

            if type_dogma_effect.fitting_usage_chance_attribute_id() != 0
                && !self
                    .booster_side_effects
                    .contains(&dogma_effect.effect_id())
            {
                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 Some(target) = get_target_object(modifier.domain(), origin) else {
                    continue;
                };
                effects.push(Pass2Effect {
                    origin: Origin::Effect {
                        effect_id: dogma_effect.effect_id(),
                        source: origin,
                        source_category: category,
                        attribute_id: modifier.modifying_attribute_id(),
                    },
                    modifier: effect_modifier,
                    operator,
                    source_quantity: self.quantity,
                    target,
                    target_attribute_id: modifier.modified_attribute_id(),
                    resistance: None,
                });
            }
        }

        /* Any module that has a capacitorNeed, can be activated. */
        if self.attributes.contains_key(&ATTRIBUTE_CAPACITOR_NEED_ID)
            && self.max_state < ItemState::Active
        {
            self.max_state = ItemState::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);
        if let Some(mode) = &mut objects.mode {
            mode.collect_effects(info, Object::Mode, false, &mut effects);
        }
        objects
            .char
            .collect_effects(info, Object::Char, false, &mut effects);
        for index in 0..objects.projected.len() {
            let projected = &objects.projected[index];
            collect_projected_effects(
                info,
                &projected.item,
                Object::Projected(index),
                projected.effect_id,
                &mut effects,
            );
        }
        collect_buff_effects(info, &objects.buffs, &mut effects);

        /* A structure is not the pilot's ship; only the structure skills, via
         * the structure domain, reach it. Implants and boosters not at all. */
        let structure_fit = objects.ship.category_id == STRUCTURE_CATEGORY_ID;

        for (index, item) in objects.items.iter_mut().enumerate() {
            if structure_fit && item.is_on_char() {
                item.state = ItemState::Passive;
                continue;
            }
            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);
            }
        }
        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_type_id, category_id) = match effect.origin {
                Origin::Effect { source, .. } => {
                    let source = objects.get(source).unwrap();
                    (source.type_id, source.category_id)
                }
                /* A buff has no object behind it: it is never a skill, and
                 * never exempt from the stacking penalty. */
                Origin::Buff { .. } => (0, 0),
            };

            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(_));
                    let char_location = location_only && effect.target == Object::Char;
                    let in_location = |item: &Item| match char_location {
                        true => item.is_on_char(),
                        false => item.is_in_ship(),
                    };

                    if !char_location && 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 || in_location(item))
                    {
                        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,
                            );
                        }
                    }
                }
            }
        }
    }
}