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
//! Event bindings & utilities.

mod category;
mod common;
mod event_kind;
mod old;

pub use self::category::*;
pub use self::common::*;
pub use self::event_kind::*;
pub use self::old::*;

#[allow(deprecated)]
pub 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, EffectGUID, EffectOld},
    log::{ErrorEvent, LogEvent},
    marker::{AgentMarkerEvent, SquadMarkerEvent},
    player::{GuildEvent, RewardEvent, TagEvent},
    position::PositionEvent,
    skill::{ActivationEvent, SkillInfo, SkillTiming},
    strike::StrikeEvent,
    weapon::WeaponSwapEvent,
};

use crate::{
    buff::{BuffCycle, BuffRemove},
    extract::Extract,
    skill::Activation,
    strike::Strike,
    Affinity, StateChange, TryExtract,
};

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

/// ArcDPS event.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Event {
    /// `timeGetTime()` at time of registering the event.
    pub time: u64,

    /// Agent that caused the event.
    pub src_agent: u64,

    /// Agent the event happened to.
    pub dst_agent: u64,

    /// Value, if relevant to the event.
    pub value: i32,

    /// Buff damage, if relevant to the event.
    pub buff_dmg: i32,

    /// Overstack value, if relevant to the event.
    pub overstack_value: u32,

    /// Skill id of the relevant skill (can be zero).
    pub skill_id: u32,

    /// Instance id of source agent as appears in game at time of event.
    pub src_instance_id: u16,

    /// Instance id of destination agent as appears in game at time of event.
    pub dst_instance_id: u16,

    /// If `src_agent` has a master (e.g. is minion), will be equal to instance id of master, zero otherwise.
    pub src_master_instance_id: u16,

    /// If `dst_agent` has a master (e.g. is minion), will be equal to instance id of master, zero otherwise.
    pub dst_master_instance_id: u16,

    /// Current affinity of `src_agent` and `dst_agent`.
    ///
    /// Use [`Event::get_affinity`] to obtain the value as [`Affinity`].
    ///
    /// *Arc calls this "iff" for if friend/foe.*
    pub affinity: u8,

    /// Buff, if relevant to the event.
    pub buff: u8,

    /// Combat result.
    ///
    /// For strike (direct damage) events this contains the kind of strike.
    ///
    /// Use [`Event::get_strike`] to obtain the value as [`Strike`].
    pub result: u8,

    /// Whether event is a kind of activation event.
    ///
    /// Use [`Event::get_activation`] to obtain the value as [`Activation`].
    ///
    /// For [`Activation::CancelFire`] and [`Activation::CancelCancel`] `value` will be the ms duration of the time spent in animation.
    /// `buff_dmg` will be the ms duration of the scaled (as if not affected) time spent.
    ///
    /// For Normal or Quickness, `value` will be the ms duration at which all significant effects have happened.
    /// `buff_dmg` will be the ms duration at which control is expected to be returned to character.
    ///
    /// `dst_agent` will be x/y of target of skill effect.
    /// `overstack_value` will be z of target of skill effect.
    pub is_activation: u8,

    /// Whether event is a kind of buff remove event.
    ///
    /// Use [`Event::get_buffremove`] to obtain the value as [`BuffRemove`].
    ///
    /// `src_agent` is agent that had buff removed, `dst_agent` is the agent that removed it.
    /// `value` will be the remaining time removed calculated as duration.
    /// `buff_dmg` will be the remaining time removed calculated as intensity.
    ///
    /// For [`BuffRemove::All`] `result` will be the number of stacks removed.
    /// For [`BuffRemove::Single`] pad61-64 (uint32) will be buff instance id of buff removed.
    pub is_buffremove: u8,

    /// Whether `src_agent` is above 90% Health.
    pub is_ninety: u8,

    /// Whether `dst_agent` is below 50% Health.
    pub is_fifty: u8,

    /// Whether `src_agent` is moving at time of event.
    pub is_moving: u8,

    /// Whether event is a kind of state change event.
    ///
    /// Use [Event::get_statechange] to obtain the value as [`StateChange`].
    pub is_statechange: u8,

    /// Whether `src_agent` is flanking at time of event.
    ///
    /// The value lies in a range of `1` to `135` degrees where `135` is rear.
    pub is_flanking: u8,

    /// Shields, if relevant to the event.
    pub is_shields: u8,

    /// For relevant events this contains when the buff cycle happened.
    ///
    /// Use [`Event::get_buffcycle`] to obtain the value as [`BuffCycle`].
    pub is_offcycle: u8,

    /// Padding.
    ///
    /// May contain information depending on the kind of event.
    pub pad61: u8,

    /// Padding.
    ///
    /// May contain information depending on the kind of event.
    pub pad62: u8,

    /// Padding.
    ///
    /// May contain information depending on the kind of event.
    pub pad63: u8,

    /// Padding.
    ///
    /// May contain information depending on the kind of event.
    pub pad64: u8,
}

impl Event {
    /// Determines the [`EventCategory`] of the event.
    #[inline]
    pub fn categorize(&self) -> EventCategory {
        self.into()
    }

    /// Converts the event into its [`EventKind`] representation.
    #[inline]
    pub fn into_kind(self) -> EventKind {
        self.into()
    }

    /// Returns the event `is_statechange` as [`StateChange`].
    #[inline]
    pub fn get_statechange(&self) -> StateChange {
        self.is_statechange.into()
    }

    /// Returns the event `affinity` as [`Affinity`].
    ///
    /// This will return [`Affinity::Unknown`] if the event has no valid data in `affinity`.
    #[inline]
    pub fn get_affinity(&self) -> Affinity {
        self.affinity.into()
    }

    /// Returns the event `is_activation` as [`Activation`].
    ///
    /// This will return [`Activation::Unknown`] if the event has no valid data in `is_activation`.
    #[inline]
    pub fn get_activation(&self) -> Activation {
        self.is_activation.into()
    }

    /// Returns the event `is_buffremove` as [`BuffRemove`].
    ///
    /// This will return [`BuffRemove::Unknown`] if the event has no valid data in `is_buffremove`.
    #[inline]
    pub fn get_buffremove(&self) -> BuffRemove {
        self.is_buffremove.into()
    }

    /// Returns the event `result` as [`Strike`].
    ///
    /// This will return [`Strike::Unknown`] if the event has no valid data in `result`.
    #[inline]
    pub fn get_strike(&self) -> Strike {
        self.result.into()
    }

    /// Returns the event `is_offcycle` as [`BuffCycle`].
    ///
    /// This will return [`BuffCycle::Unknown`] if the event has no valid data in `is_offcycle`.
    #[inline]
    pub fn get_buffcycle(&self) -> BuffCycle {
        self.is_offcycle.into()
    }

    /// Returns the padding as [`u32`] id/signature.
    #[inline]
    pub fn get_pad_id(&self) -> u32 {
        u32::from_le_bytes([self.pad61, self.pad62, self.pad63, self.pad64])
    }

    /// Checks whether the event has a timestamp.
    #[inline]
    pub fn has_time(&self) -> bool {
        self.get_statechange().has_time()
    }

    /// Retrieves the event time, if present.
    #[inline]
    pub fn time(&self) -> Option<u64> {
        self.has_time().then_some(self.time)
    }

    /// Forcefully extracts a type implementing [`Extract`] from the event.
    ///
    /// # Safety
    /// This is safe when the given event is a valid event to extract the type from.
    #[inline]
    pub unsafe fn extract<T>(&self) -> T
    where
        T: Extract,
    {
        T::extract(self)
    }

    /// Attempts to extract a type implementing [`TryExtract`] from the event.
    #[inline]
    pub fn try_extract<T>(&self) -> Option<T>
    where
        T: TryExtract,
    {
        T::try_extract(self)
    }

    /// Attempts to extract an [`ActivationEvent`] from the event.
    #[inline]
    pub fn try_to_activation(&self) -> Option<ActivationEvent> {
        self.try_extract()
    }

    /// Attempts to extract a [`BuffRemoveEvent`] from the event.
    #[inline]
    pub fn try_to_buff_remove(&self) -> Option<BuffRemoveEvent> {
        self.try_extract()
    }

    /// Attempts to extract a [`BuffApplyEvent`] from the event.
    #[inline]
    pub fn try_to_buff_apply(&self) -> Option<BuffApplyEvent> {
        self.try_extract()
    }

    /// Attempts to extract a [`BuffDamageEvent`] from the event.
    #[inline]
    pub fn try_to_buff_damage(&self) -> Option<BuffDamageEvent> {
        self.try_extract()
    }

    /// Attempts to extract a [`StrikeEvent`] from the event.
    #[inline]
    pub fn try_to_strike(&self) -> Option<StrikeEvent> {
        self.try_extract()
    }

    /// Checks whether the event is an initial buff event.
    #[inline]
    pub fn is_buffinitial(&self) -> bool {
        self.get_statechange() == StateChange::BuffInitial && self.buff == 18
    }
}