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
use crate::{
    agent::{
        AgentStatusEvent, AttackTargetEvent, BarrierUpdateEvent, BreakbarPercentEvent,
        BreakbarStateEvent, DownContributionEvent, EnterCombatEvent, HealthUpdateEvent,
        MaxHealthEvent, TargetableEvent, TeamChangeEvent,
    },
    buff::{
        BuffApplyEvent, BuffDamageEvent, BuffFormula, BuffInfo, BuffInitialEvent, BuffRemoveEvent,
        StackActiveEvent, StackResetEvent,
    },
    effect::{Effect, EffectOld},
    guid::ContentGUID,
    log::{ErrorEvent, LogEvent},
    marker::{AgentMarkerEvent, SquadMarkerEvent},
    player::{GuildEvent, RewardEvent},
    position::PositionEvent,
    ruleset::Ruleset,
    skill::{ActivationEvent, SkillInfo, SkillTiming},
    strike::StrikeEvent,
    weapon::WeaponSwapEvent,
    Event, EventCategory, Language, StateChange,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Possible [`Event`] kinds.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind"))]
pub enum EventKind {
    /// Agent entered combat.
    EnterCombat(EnterCombatEvent),

    /// Agent left combat.
    ExitCombat(AgentStatusEvent),

    /// Agent is now alive.
    ChangeUp(AgentStatusEvent),

    /// Agent is now dead.
    ChangeDead(AgentStatusEvent),

    /// Agent is now downed.
    ChangeDown(AgentStatusEvent),

    /// Agent is now in game tracking range.
    Spawn(AgentStatusEvent),

    /// Agent is no longer being tracked or out of game tracking range.
    Despawn(AgentStatusEvent),

    /// Agent health change.
    HealthUpdate(HealthUpdateEvent),

    /// Log started.
    LogStart(LogEvent),

    /// Log ended.
    LogEnd(LogEvent),

    /// Agent swapped weapon set.
    WeaponSwap(WeaponSwapEvent),

    /// Agent maximum health change.
    MaxHealthUpdate(MaxHealthEvent),

    /// Agent is "recording" player.
    PointOfView(AgentStatusEvent),

    /// Game text language.
    Language {
        time: u64,
        language: Result<Language, u64>,
    },

    /// Game build.
    GWBuild { time: u64, build: u64 },

    /// Sever shard id.
    ShardId { time: u64, shard: u64 },

    /// Agent got a reward chest.
    Reward(RewardEvent),

    /// Appears once per buff per agent on logging start.
    BuffInitial(BuffInitialEvent),

    /// Agent position change.
    Position(PositionEvent),

    /// Agent velocity change.
    Velocity(PositionEvent),

    /// Agent facing change.
    Facing(PositionEvent),

    /// Agent team change.
    TeamChange(TeamChangeEvent),

    /// Agent is now an attack target.
    AttackTarget(AttackTargetEvent),

    /// Agent targetability change.
    Targetable(TargetableEvent),

    /// Map id.
    MapId { time: u64, map: u64 },

    /// Agent with active buff.
    StackActive(StackActiveEvent),

    /// Agent with reset buff.
    StackReset(StackResetEvent),

    /// Agent is in guild.
    Guild(GuildEvent),

    /// Buff information.
    BuffInfo(BuffInfo),

    /// Buff formula.
    BuffFormula(BuffFormula),

    /// Skill information.
    SkillInfo(SkillInfo),

    /// Skill action.
    SkillTiming(SkillTiming),

    /// Agent breakbar state change.
    BreakbarState(BreakbarStateEvent),

    /// Breakbar percentage.
    BreakbarPercent(BreakbarPercentEvent),

    /// Error.
    Error(ErrorEvent),

    /// Agent has marker.
    AgentMarker(AgentMarkerEvent),

    /// Agent barrier change.
    BarrierUpdate(BarrierUpdateEvent),

    /// Arc UI stats reset.
    StatReset { time: u64, target: u64 },

    /// A custom event created by an extension (addon/plugin).
    Extension { sig: u32, event: Event },

    /// Delayed combat event.
    ApiDelayed { event: Box<EventKind> },

    /// Instance started.
    InstanceStart { time: u64, start: u64 },

    /// Tick rate.
    Tickrate { time: u64, rate: u64 },

    /// Last 90% before down for downs contribution.
    Last90BeforeDown(DownContributionEvent),

    /// Effect created or ended.
    EffectOld(EffectOld),

    /// Id to GUID.
    ///
    /// This maps a volatile content id to a stable GUID.
    IdToGUID(ContentGUID),

    /// Log NPC changed.
    LogNPCUpdate(LogEvent),

    /// A custom combat event created by an extension (addon/plugin).
    ExtensionCombat { sig: u32, event: Event },

    /// Fractal scale.
    FractalScale { time: u64, scale: u64 },

    /// Effect created or ended.
    Effect(Effect),

    /// Combat ruleset.
    Ruleset(Ruleset),

    /// Activation (cast) event.
    Activation(ActivationEvent),

    /// Buff removed.
    BuffRemove(BuffRemoveEvent),

    /// Buff applied.
    BuffApply(BuffApplyEvent),

    /// Buff damage.
    BuffDamage(BuffDamageEvent),

    /// Direct (strike) damage.
    Strike(StrikeEvent),

    /// Squad marker placed or removed.
    SquadMarker(SquadMarkerEvent),

    /// Unknown event.
    Unknown(Event),
}

impl From<Event> for EventKind {
    #[inline]
    fn from(mut event: Event) -> Self {
        unsafe {
            match event.categorize() {
                EventCategory::StateChange => match event.get_statechange() {
                    StateChange::None => unreachable!("statechange none in statechange category"),
                    StateChange::IdleEvent | StateChange::ReplInfo => {
                        unreachable!("illegal internal statechange")
                    }
                    StateChange::EnterCombat => Self::EnterCombat(event.extract()),
                    StateChange::ExitCombat => Self::ExitCombat(event.extract()),
                    StateChange::ChangeUp => Self::ChangeUp(event.extract()),
                    StateChange::ChangeDead => Self::ChangeDead(event.extract()),
                    StateChange::ChangeDown => Self::ChangeDown(event.extract()),
                    StateChange::Spawn => Self::Spawn(event.extract()),
                    StateChange::Despawn => Self::Despawn(event.extract()),
                    StateChange::HealthUpdate => Self::HealthUpdate(event.extract()),
                    StateChange::LogStart => Self::LogStart(event.extract()),
                    StateChange::LogEnd => Self::LogEnd(event.extract()),
                    StateChange::WeaponSwap => Self::WeaponSwap(event.extract()),
                    StateChange::MaxHealthUpdate => Self::MaxHealthUpdate(event.extract()),
                    StateChange::PointOfView => Self::PointOfView(event.extract()),
                    StateChange::Language => Self::Language {
                        time: event.time,
                        language: Language::try_from(event.src_agent as i32)
                            .map_err(|_| event.src_agent),
                    },
                    StateChange::GWBuild => Self::GWBuild {
                        time: event.time,
                        build: event.src_agent,
                    },
                    StateChange::ShardId => Self::ShardId {
                        time: event.time,
                        shard: event.src_agent,
                    },
                    StateChange::Reward => Self::Reward(event.extract()),
                    StateChange::BuffInitial => Self::BuffInitial(event.extract()),
                    StateChange::Position => Self::Position(event.extract()),
                    StateChange::Velocity => Self::Velocity(event.extract()),
                    StateChange::Facing => Self::Facing(event.extract()),
                    StateChange::TeamChange => Self::TeamChange(event.extract()),
                    StateChange::AttackTarget => Self::AttackTarget(event.extract()),
                    StateChange::Targetable => Self::Targetable(event.extract()),
                    StateChange::MapId => Self::MapId {
                        time: event.time,
                        map: event.src_agent,
                    },
                    StateChange::StackActive => Self::StackActive(event.extract()),
                    StateChange::StackReset => Self::StackReset(event.extract()),
                    StateChange::Guild => Self::Guild(event.extract()),
                    StateChange::BuffInfo => Self::BuffInfo(event.extract()),
                    StateChange::BuffFormula => Self::BuffFormula(event.extract()),
                    StateChange::SkillInfo => Self::SkillInfo(event.extract()),
                    StateChange::SkillTiming => Self::SkillTiming(event.extract()),
                    StateChange::BreakbarState => Self::BreakbarState(event.extract()),
                    StateChange::BreakbarPercent => Self::BreakbarPercent(event.extract()),
                    StateChange::Error => Self::Error(event.extract()),
                    StateChange::Marker => Self::AgentMarker(event.extract()),
                    StateChange::BarrierUpdate => Self::BarrierUpdate(event.extract()),
                    StateChange::StatReset => Self::StatReset {
                        time: event.time,
                        target: event.src_agent,
                    },
                    StateChange::Extension => Self::Extension {
                        sig: event.get_pad_id(),
                        event,
                    },
                    StateChange::ApiDelayed => {
                        event.is_statechange = StateChange::None.into();
                        Self::ApiDelayed {
                            event: event.into_kind().into(),
                        }
                    }
                    StateChange::InstanceStart => Self::InstanceStart {
                        time: event.time,
                        start: event.src_agent,
                    },
                    StateChange::Tickrate => Self::Tickrate {
                        time: event.time,
                        rate: event.src_agent,
                    },
                    StateChange::Last90BeforeDown => Self::Last90BeforeDown(event.extract()),
                    StateChange::EffectOld => Self::EffectOld(event.extract()),
                    StateChange::IdToGUID => Self::IdToGUID(event.extract()),
                    StateChange::LogNPCUpdate => Self::LogNPCUpdate(event.extract()),
                    StateChange::ExtensionCombat => Self::ExtensionCombat {
                        sig: event.get_pad_id(),
                        event,
                    },
                    StateChange::FractalScale => Self::FractalScale {
                        time: event.time,
                        scale: event.src_agent,
                    },
                    StateChange::Effect => Self::Effect(event.extract()),
                    StateChange::Ruleset => {
                        Self::Ruleset(Ruleset::from_bits_retain(event.src_agent))
                    }
                    StateChange::SquadMarker => Self::SquadMarker(event.extract()),
                    StateChange::Unknown(_) => Self::Unknown(event),
                },
                EventCategory::Activation => Self::Activation(event.extract()),
                EventCategory::BuffRemove => Self::BuffRemove(event.extract()),
                EventCategory::BuffApply => Self::BuffApply(event.extract()),
                EventCategory::BuffDamage => Self::BuffDamage(event.extract()),
                EventCategory::Strike => Self::Strike(event.extract()),
            }
        }
    }
}