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
//! Effect bindings & utilities.
//!
//! Effects are visual effects rendered by the game client.

mod guid;
mod old;

pub use self::guid::*;
pub use self::old::*;

use crate::extract::transmute_field;
use crate::{extract::Extract, Event, Position, StateChange, TryExtract};

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

/// Effect information from an [`Event`] with [`StateChange::Effect`].
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Effect {
    /// Time of registering the effect.
    pub time: u64,

    /// Id of the effect.
    ///
    /// Use to map to a GUID using [`StateChange::IdToGUID`] events.
    pub effect_id: u32,

    /// Owner of the effect.
    pub owner: u64,

    /// Whether the effect is on a moving platform.
    pub moving_platform: u8,

    /// Location of the effect.
    pub location: EffectLocation,

    /// Duration of the effect in milliseconds.
    pub duration: u32,

    /// Trackable id for effect end.
    pub tracking_id: u32,

    /// Effect orientation.
    pub orientation: EffectOrientation,
}

impl Effect {
    /// Checks whether this is the end of an effect.
    #[inline]
    pub fn is_end(&self) -> bool {
        self.effect_id == 0
    }
}

impl Extract for Effect {
    #[inline]
    unsafe fn extract(event: &Event) -> Self {
        let effect_id = event.skill_id;
        let duration = transmute_field!(event.affinity as u32);
        let tracking_id = transmute_field!(event.is_buffremove as u32);
        let orientation = transmute_field!(event.is_shields as [i16; 3]);

        Self {
            time: event.time,
            effect_id,
            owner: event.src_agent,
            moving_platform: event.is_flanking,
            location: EffectLocation::extract(event),
            duration,
            tracking_id,
            orientation: orientation.into(),
        }
    }
}

impl TryExtract for Effect {
    #[inline]
    fn can_extract(event: &Event) -> bool {
        event.get_statechange() == StateChange::Effect
    }
}

/// Location of an effect.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum EffectLocation {
    Agent(u64),
    Position(Position),
}

impl Extract for EffectLocation {
    #[inline]
    unsafe fn extract(event: &Event) -> Self {
        if event.dst_agent != 0 {
            Self::Agent(event.dst_agent)
        } else {
            let pos = transmute_field!(event.value as [f32; 3]);
            Self::Position(pos.into())
        }
    }
}

/// Orientation of an effect.
///
/// Values represent rotation along each axis multiplied by `1000` or [`i16::MIN`]/[`i16::MAX`] if out of range.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct EffectOrientation {
    pub x: i16,
    pub y: i16,
    pub z: i16,
}

impl EffectOrientation {
    /// Ratio between [`i16`] and [`f32`] representation.
    pub const RATIO: f32 = 1000.0;

    /// Maximum value in [`f32`] representation.
    ///
    /// For [`i16`] representation use [`i16::MAX`].
    pub const MAX: f32 = i16::MAX as f32 / Self::RATIO;

    /// Minimum value in [`f32`] representation.
    ///
    /// For [`i16`] representation use [`i16::MIN`].
    pub const MIN: f32 = i16::MIN as f32 / Self::RATIO;

    /// Creates a new effect orientation from radians in [`i16`] representation.
    #[inline]
    pub const fn new(x: i16, y: i16, z: i16) -> Self {
        Self { x, y, z }
    }

    /// Creates a new effect orientation from radians in [`f32`] representation.
    #[inline]
    pub fn from_floats(x: f32, y: f32, z: f32) -> Self {
        Self::new(Self::to_int(x), Self::to_int(y), Self::to_int(z))
    }

    /// Converts int to float.
    #[inline]
    pub fn to_float(int: i16) -> f32 {
        int as f32 / Self::RATIO
    }

    /// Converts int to float.
    #[inline]
    pub fn to_int(float: f32) -> i16 {
        (float * Self::RATIO).round() as i16
    }

    /// Converts the orientation to a [`Position`].
    #[inline]
    pub fn as_position(&self) -> Position {
        Position::new(
            Self::to_float(self.x),
            Self::to_float(self.y),
            Self::to_float(self.z),
        )
    }

    /// Converts the orientation to a rotation matrix.
    #[inline]
    pub fn as_rotation_matrix(&self) -> [[f32; 3]; 3] {
        self.as_position().as_rotation_matrix()
    }

    /// Rotates the [`Position`] vector.
    #[inline]
    pub fn rotate(&self, vector: Position) -> Position {
        self.as_position().rotate(vector)
    }
}

impl From<[i16; 3]> for EffectOrientation {
    #[inline]
    fn from(value: [i16; 3]) -> Self {
        let [x, y, z] = value;
        Self::new(x, y, z)
    }
}

impl From<EffectOrientation> for [i16; 3] {
    #[inline]
    fn from(orientation: EffectOrientation) -> Self {
        [orientation.x, orientation.y, orientation.z]
    }
}

impl From<EffectOrientation> for Position {
    #[inline]
    fn from(orientation: EffectOrientation) -> Self {
        orientation.as_position()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn orientation() {
        let orient = EffectOrientation::from_floats(12.345, 6.789, 0.0);
        assert_eq!(orient, EffectOrientation::new(12345, 6789, 0));
    }

    #[test]
    fn orientation_round() {
        assert_eq!(EffectOrientation::to_int(30.9999), 31000);
    }

    #[test]
    fn orientation_saturate() {
        let orient = EffectOrientation::from_floats(12345.0, -6789.0, 0.0);
        assert_eq!(orient, EffectOrientation::new(i16::MAX, i16::MIN, 0));
    }
}