spring_ai_rs/ai_interface/callback/unit_def/
death.rs

1use std::{error::Error, ffi::CStr};
2
3use serde::{Deserialize, Serialize};
4
5use crate::get_callback;
6
7#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
8pub struct UnitDeath {
9    pub ai_id: i32,
10    pub def_id: i32,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct UnitDeathAll {
15    pub wreck_name: String,
16    pub death_explosion: i32,
17    pub self_d_explosion: i32,
18    pub able_to_self_d: bool,
19    pub self_d_countdown: i32,
20}
21
22impl UnitDeath {
23    pub fn wreck_name(&self) -> Result<String, Box<dyn Error>> {
24        let get_wreck_name_func = get_callback!(self.ai_id, UnitDef_getWreckName)?;
25        Ok(String::from(
26            unsafe { CStr::from_ptr(get_wreck_name_func(self.ai_id, self.def_id)) }.to_str()?,
27        ))
28    }
29
30    pub fn death_explosion(&self) -> Result<i32, Box<dyn Error>> {
31        let get_death_explosion_func = get_callback!(self.ai_id, UnitDef_getDeathExplosion)?;
32        Ok(unsafe { get_death_explosion_func(self.ai_id, self.def_id) })
33    }
34
35    pub fn self_d_explosion(&self) -> Result<i32, Box<dyn Error>> {
36        let get_self_d_explosion_func = get_callback!(self.ai_id, UnitDef_getSelfDExplosion)?;
37        Ok(unsafe { get_self_d_explosion_func(self.ai_id, self.def_id) })
38    }
39
40    pub fn able_to_self_d(&self) -> Result<bool, Box<dyn Error>> {
41        let get_is_able_to_self_d_func = get_callback!(self.ai_id, UnitDef_isAbleToSelfD)?;
42        Ok(unsafe { get_is_able_to_self_d_func(self.ai_id, self.def_id) })
43    }
44
45    pub fn self_d_countdown(&self) -> Result<i32, Box<dyn Error>> {
46        let get_is_able_to_self_d_func = get_callback!(self.ai_id, UnitDef_getSelfDCountdown)?;
47        Ok(unsafe { get_is_able_to_self_d_func(self.ai_id, self.def_id) })
48    }
49
50    pub fn all(&self) -> Result<UnitDeathAll, Box<dyn Error>> {
51        Ok(UnitDeathAll {
52            wreck_name: self.wreck_name()?,
53            death_explosion: self.death_explosion()?,
54            self_d_explosion: self.self_d_explosion()?,
55            able_to_self_d: self.able_to_self_d()?,
56            self_d_countdown: self.self_d_countdown()?,
57        })
58    }
59}