aboutsummaryrefslogtreecommitdiff
path: root/src/analyzers/strikes.rs
blob: 02c22173a392b953b888fadd01f298a1dc890e16 (plain)
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
//! Analyzers for Strike Mission logs.
use crate::{
    analyzers::{helpers, Analyzer, Outcome},
    gamedata::Boss,
    EventKind, Log,
};

/// Analyzer for strikes.
///
/// Since there are currently no strikes requiring special logic, this analyzer is used for all
/// strike missions.
#[derive(Debug, Clone, Copy)]
pub struct GenericStrike<'log> {
    log: &'log Log,
}

impl<'log> GenericStrike<'log> {
    /// Create a new [`GenericStrike`] analyzer for the given log.
    ///
    /// **Do not** use this method unless you know what you are doing. Instead, rely on
    /// [`Log::analyzer`]!
    pub fn new(log: &'log Log) -> Self {
        GenericStrike { log }
    }
}

impl<'log> Analyzer for GenericStrike<'log> {
    fn log(&self) -> &Log {
        self.log
    }

    fn is_cm(&self) -> bool {
        false
    }

    fn outcome(&self) -> Option<Outcome> {
        Outcome::from_bool(helpers::boss_is_dead(self.log))
    }
}

/// Analyzer for the Captain Mai Trin/Aetherblade Hideout strike.
#[derive(Debug, Clone, Copy)]
pub struct CaptainMaiTrin<'log> {
    log: &'log Log,
}

impl<'log> CaptainMaiTrin<'log> {
    /// ID of the Echo of Scarlet Briar in normal mode.
    pub const ECHO_OF_SCARLET_BRIAR: u16 = 24_768;
    /// ID of the ECho of Scarlet Briar with the challenge mote active.
    pub const ECHO_OF_SCARLET_BRIAR_CM: u16 = 25_247;
    /// Determined buff that is used in Mai Trin's Strike.
    ///
    /// Thanks to ArenaNet's consistency, there are multiple versions of the Determined buff in
    /// use.
    ///
    /// The chat link for this buff is `[&Bn8DAAA=]`.
    pub const DETERMINED_ID: u32 = 895;
    /// Cutoff for when the fight is considered CM.
    ///
    /// See
    /// <https://wiki.guildwars2.com/wiki/Strike_Mission:_Aetherblade_Hideout#Stats_of_encounter_relevant_enemies>
    /// for a reference.
    pub const MAI_CM_HEALTH: u64 = 8_000_000;

    /// Create a new [`CaptainMaiTrin`] analyzer for the given log.
    ///
    /// **Do not** use this method unless you know what you are doing. Instead, rely on
    /// [`Log::analyzer`]!
    pub fn new(log: &'log Log) -> Self {
        CaptainMaiTrin { log }
    }
}

impl<'log> Analyzer for CaptainMaiTrin<'log> {
    fn log(&self) -> &Log {
        self.log
    }

    fn is_cm(&self) -> bool {
        helpers::boss_health(self.log).unwrap_or_default() > Self::MAI_CM_HEALTH
    }

    fn outcome(&self) -> Option<Outcome> {
        check_reward!(self.log);

        let scarlet = self.log.characters().find(|npc| {
            npc.id() == Self::ECHO_OF_SCARLET_BRIAR || npc.id() == Self::ECHO_OF_SCARLET_BRIAR_CM
        });
        // If the log ends before Scarlet even spawns, then it for sure is a failure.
        let scarlet = match scarlet {
            Some(s) => s,
            None => return Some(Outcome::Failure),
        };
        let mai = self
            .log
            .characters()
            .find(|npc| npc.id() == Boss::CaptainMaiTrin as u16)?;

        for event in self.log.events() {
            if let EventKind::BuffApplication {
                destination_agent_addr,
                buff_id,
                ..
            } = event.kind()
            {
                if *buff_id == Self::DETERMINED_ID
                    && *destination_agent_addr == mai.addr()
                    && event.time() > scarlet.first_aware()
                {
                    return Some(Outcome::Success);
                }
            }
        }

        Some(Outcome::Failure)
    }
}

/// Analyzer for the Ankka/Xunlai Jade Junkyard strike.
#[derive(Debug, Clone, Copy)]
pub struct Ankka<'log> {
    log: &'log Log,
}

impl<'log> Ankka<'log> {
    /// Determined buff that is used in Ankka's Strike.
    ///
    /// Thanks to ArenaNet's consistency, there are multiple versions of the Determined buff in
    /// use.
    ///
    /// The chat link for this buff is `[&Bn8DAAA=]`.
    pub const DETERMINED_ID: u32 = CaptainMaiTrin::DETERMINED_ID;
    /// The minimum duration of [`Ankka::DETERMINED_ID`] buff applications.
    pub const DURATION_CUTOFF: i32 = i32::MAX;
    /// The expected number of times that Ankka needs to phase before we consider it a success.
    pub const EXPECTED_PHASE_COUNT: usize = 3;
    /// Cutoff for when the fight is considered CM.
    pub const ANKKA_CM_HEALTH: u64 = 50_000_000;

    /// Create a new [`Ankka`] analyzer for the given log.
    ///
    /// **Do not** use this method unless you know what you are doing. Instead, rely on
    /// [`Log::analyzer`]!
    pub fn new(log: &'log Log) -> Self {
        Ankka { log }
    }
}

impl<'log> Analyzer for Ankka<'log> {
    fn log(&self) -> &Log {
        self.log
    }

    fn is_cm(&self) -> bool {
        helpers::boss_health(self.log).unwrap_or_default() > Self::ANKKA_CM_HEALTH
    }

    fn outcome(&self) -> Option<Outcome> {
        check_reward!(self.log);

        let ankka = self
            .log
            .characters()
            .find(|npc| npc.id() == Boss::Ankka as u16)?;

        let phase_change_count = self
            .log
            .events()
            .iter()
            .filter(|event| {
                if let EventKind::BuffApplication {
                    destination_agent_addr,
                    buff_id,
                    duration,
                    ..
                } = event.kind()
                {
                    *buff_id == Self::DETERMINED_ID
                        && *destination_agent_addr == ankka.addr()
                        && *duration == Self::DURATION_CUTOFF
                } else {
                    false
                }
            })
            .count();

        Outcome::from_bool(phase_change_count == Self::EXPECTED_PHASE_COUNT)
    }
}

/// Analyzer for the Minister Li/Kaineng Overlook strike.
#[derive(Debug, Clone, Copy)]
pub struct MinisterLi<'log> {
    log: &'log Log,
}

impl<'log> MinisterLi<'log> {
    /// Determined buff that is used in Minister Li's Strike.
    ///
    /// Thanks to ArenaNet's consistency, there are multiple versions of the Determined buff in
    /// use.
    ///
    /// The chat link for this buff is `[&BvoCAAA=]`.
    pub const DETERMINED_ID: u32 = 762;
    /// The minimum number of times that Minister Li needs to phase before we consider it a success.
    pub const MINIMUM_PHASE_COUNT: usize = 3;

    /// Create a new [`MinisterLi`] analyzer for the given log.
    ///
    /// **Do not** use this method unless you know what you are doing. Instead, rely on
    /// [`Log::analyzer`]!
    pub fn new(log: &'log Log) -> Self {
        MinisterLi { log }
    }
}

impl<'log> Analyzer for MinisterLi<'log> {
    fn log(&self) -> &Log {
        self.log
    }

    fn is_cm(&self) -> bool {
        // EoD strike CMs are not implemented yet as of 2022-03-31
        false
    }

    fn outcome(&self) -> Option<Outcome> {
        check_reward!(self.log);

        let li = self
            .log
            .characters()
            .find(|npc| npc.id() == Boss::MinisterLi as u16)?;

        let phase_change_count = self
            .log
            .events()
            .iter()
            .filter(|event| {
                if let EventKind::BuffApplication {
                    destination_agent_addr,
                    buff_id,
                    ..
                } = event.kind()
                {
                    *buff_id == Self::DETERMINED_ID && *destination_agent_addr == li.addr()
                } else {
                    false
                }
            })
            .count();

        Outcome::from_bool(phase_change_count >= Self::MINIMUM_PHASE_COUNT)
    }
}

/// Analyzer for the Dragonvoid/Harvest Temple strike.
#[derive(Debug, Clone, Copy)]
pub struct Dragonvoid<'log> {
    log: &'log Log,
}

impl<'log> Dragonvoid<'log> {
    pub const EXPECTED_TARGET_OFF_COUNT: usize = 2;

    /// Create a new [`Dragonvoid`] analyzer for the given log.
    ///
    /// **Do not** use this method unless you know what you are doing. Instead, rely on
    /// [`Log::analyzer`]!
    pub fn new(log: &'log Log) -> Self {
        Dragonvoid { log }
    }
}

impl<'log> Analyzer for Dragonvoid<'log> {
    fn log(&self) -> &Log {
        self.log
    }

    fn is_cm(&self) -> bool {
        // EoD strike CMs are not implemented yet as of 2022-03-31
        false
    }

    fn outcome(&self) -> Option<Outcome> {
        // check_reward is pointless because the reward is delayed.

        // First, we find the right agent_addr
        let mut first_voids = None;
        for event in self.log.events() {
            if let EventKind::AttackTarget {
                agent_addr,
                parent_agent_addr,
                ..
            } = event.kind()
            {
                if first_voids.is_none() {
                    first_voids = Some(parent_agent_addr);
                } else if first_voids != Some(parent_agent_addr) {
                    // We find the amount of target off switches that occurred after a target on
                    // switch.
                    let mut is_on = false;
                    let mut target_off_count = 0;

                    // The nested loop over events is not ideal, but it is currently the easiest
                    // way to implement this logic without trying to cram it into a single loop.
                    for e in self.log.events() {
                        if let EventKind::Targetable {
                            agent_addr: taa,
                            targetable,
                        } = e.kind()
                        {
                            if *taa != *agent_addr {
                                continue;
                            }
                            if *targetable {
                                is_on = true;
                            } else if !targetable && is_on {
                                target_off_count += 1;
                            }
                        }
                    }

                    if target_off_count == Self::EXPECTED_TARGET_OFF_COUNT {
                        return Some(Outcome::Success);
                    }
                }
            }
        }
        Some(Outcome::Failure)
    }
}