evtc\event/
common.rs

1use crate::{Affinity, AgentId, Event};
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// Information common to combat events.
7#[derive(Debug, Clone)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub struct CommonEvent {
10    /// Time of registering the event.
11    pub time: u64,
12
13    /// Agent that caused the event.
14    pub src: AgentId,
15
16    /// Agent the event happened to.
17    pub dst: AgentId,
18
19    /// Skill id of the relevant skill (can be zero).
20    pub skill_id: u32,
21
22    /// Current affinity of `src` and `dst`.
23    ///
24    /// *Arc calls this "iff" for if friend/foe.*
25    pub affinity: Affinity,
26
27    /// Whether `src` is above 90% Health.
28    pub is_ninety: u8,
29
30    /// Whether `dst` is below 50% Health.
31    pub is_fifty: u8,
32
33    /// Whether `src` is moving at time of event.
34    pub is_moving: u8,
35
36    /// Whether `src` is flanking at time of event.
37    ///
38    /// The value lies in a range of `1` to `135` degrees where `135` is rear.
39    pub is_flanking: u8,
40}
41
42impl From<&Event> for CommonEvent {
43    #[inline]
44    fn from(event: &Event) -> Self {
45        Self {
46            time: event.time,
47            src: AgentId::from_src(event),
48            dst: AgentId::from_dst(event),
49            skill_id: event.skill_id,
50            affinity: event.get_affinity(),
51            is_ninety: event.is_ninety,
52            is_fifty: event.is_fifty,
53            is_moving: event.is_moving,
54            is_flanking: event.is_flanking,
55        }
56    }
57}
58
59/// Helper macro to implement traits for events with a [`CommonEvent`] field.
60macro_rules! impl_common {
61    ($ty:ty) => {
62        impl ::core::convert::AsRef<$crate::event::CommonEvent> for $ty {
63            #[inline]
64            fn as_ref(&self) -> &$crate::event::CommonEvent {
65                &self.common
66            }
67        }
68
69        impl ::core::convert::AsMut<$crate::event::CommonEvent> for $ty {
70            #[inline]
71            fn as_mut(&mut self) -> &mut $crate::event::CommonEvent {
72                &mut self.common
73            }
74        }
75
76        impl ::core::convert::From<$ty> for $crate::event::CommonEvent {
77            #[inline]
78            fn from(value: $ty) -> Self {
79                value.common
80            }
81        }
82
83        impl ::core::ops::Deref for $ty {
84            type Target = $crate::event::CommonEvent;
85
86            #[inline]
87            fn deref(&self) -> &Self::Target {
88                &self.common
89            }
90        }
91
92        impl ::core::ops::DerefMut for $ty {
93            #[inline]
94            fn deref_mut(&mut self) -> &mut Self::Target {
95                &mut self.common
96            }
97        }
98    };
99}
100
101pub(crate) use impl_common;