evtc\agent/realtime.rs
1//! Realtime API [`Agent`] types.
2
3use super::AgentKind;
4use std::{ffi::CStr, os::raw::c_char};
5
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9/// Represents an agent in a realtime combat event.
10///
11/// Names are available for the duration of the fight.
12/// Due to this, this struct is not usable for longer than the function call.
13/// If you need it for longer than its lifetime, consider converting it to [`AgentOwned`].
14///
15/// ```no_run
16/// # use evtc::agent::realtime::{Agent, AgentOwned};
17/// # let agent: &Agent = todo!();
18/// let owned = agent.to_owned();
19/// let owned: AgentOwned = agent.into();
20/// ```
21#[derive(Debug)]
22#[repr(C)]
23pub struct Agent {
24 /// Name of the agent.
25 name: *const c_char,
26
27 /// Unique id.
28 pub id: usize,
29
30 /// Profession of the agent.
31 pub prof: u32,
32
33 /// Elite (specialization) of the agent.
34 pub elite: u32,
35
36 /// Whether the agent is self (the local player).
37 pub is_self: u32,
38
39 /// Team the agent is in.
40 pub team: u16,
41}
42
43impl Agent {
44 /// Returns the agent's name.
45 #[inline]
46 pub fn name(&self) -> Option<&str> {
47 if !self.name.is_null() {
48 unsafe { CStr::from_ptr(self.name) }.to_str().ok()
49 } else {
50 None
51 }
52 }
53
54 /// Returns the raw pointer to the agent's name.
55 #[inline]
56 pub fn name_ptr(&self) -> *const c_char {
57 self.name
58 }
59
60 /// Converts the [`Agent`] to the owned version [`AgentOwned`].
61 #[inline]
62 pub fn to_owned(&self) -> AgentOwned {
63 self.into()
64 }
65
66 /// Determines the kind of agent.
67 #[inline]
68 pub const fn kind(&self) -> AgentKind {
69 AgentKind::new(self.prof, self.elite)
70 }
71}
72
73/// [`Agent`] with an owned [`String`] name.
74#[derive(Debug, Clone)]
75#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
76pub struct AgentOwned {
77 /// Name of the agent.
78 pub name: Option<String>,
79
80 /// Unique id.
81 pub id: usize,
82
83 /// Profession of the agent.
84 pub prof: u32,
85
86 /// Elite (specialization) of the agent.
87 pub elite: u32,
88
89 /// Whether the agent is self (the local player).
90 pub is_self: u32,
91
92 /// Team the agent is in.
93 pub team: u16,
94}
95
96impl From<Agent> for AgentOwned {
97 #[inline]
98 fn from(agent: Agent) -> Self {
99 (&agent).into()
100 }
101}
102
103impl From<&Agent> for AgentOwned {
104 #[inline]
105 fn from(agent: &Agent) -> Self {
106 Self {
107 name: agent.name().map(|string| string.to_owned()),
108 id: agent.id,
109 prof: agent.prof,
110 elite: agent.elite,
111 is_self: agent.is_self,
112 team: agent.team,
113 }
114 }
115}