spring_ai_rs/ai_interface/callback/unit_def/
weapon_mount.rs

1use std::{error::Error, ffi::CStr};
2
3use serde::{Deserialize, Serialize};
4
5use crate::{ai_interface::callback::unit_def::UnitDef, get_callback};
6
7#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
8pub struct UnitWeaponMount {
9    pub ai_id: i32,
10    pub def_id: i32,
11    mount_id: i32,
12    weapon_def_id: i32,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct UnitWeaponMountAll {
17    pub mount_name: String,
18    pub slaved_to: UnitWeaponMount,
19    pub main_direction: [f32; 3],
20    pub max_angle: f32,
21    pub target_blacklist: i32,
22    pub target_whitelist: i32,
23}
24
25impl UnitDef {
26    pub fn weapon_mounts(&self) -> Result<Vec<UnitWeaponMount>, Box<dyn Error>> {
27        let get_weapon_mounts_func = get_callback!(self.ai_id, UnitDef_getWeaponMounts)?;
28
29        let number_of_mounts = unsafe { get_weapon_mounts_func(self.ai_id, self.def_id) };
30        Ok((0..number_of_mounts)
31            .filter_map(|mount_id| {
32                UnitWeaponMount::get_weapon_def(self.ai_id, self.def_id, mount_id).ok()
33            })
34            .collect())
35    }
36}
37
38impl UnitWeaponMount {
39    pub fn get_weapon_def(ai_id: i32, def_id: i32, mount_id: i32) -> Result<Self, Box<dyn Error>> {
40        let get_weapon_def_func = get_callback!(ai_id, UnitDef_WeaponMount_getWeaponDef)?;
41
42        Ok(Self {
43            ai_id,
44            def_id,
45            mount_id,
46            weapon_def_id: unsafe { get_weapon_def_func(ai_id, def_id, mount_id) },
47        })
48    }
49
50    pub fn mount_name(&self) -> Result<String, Box<dyn Error>> {
51        let get_mount_name_func = get_callback!(self.ai_id, UnitDef_WeaponMount_getName)?;
52        Ok(String::from(
53            unsafe { CStr::from_ptr(get_mount_name_func(self.ai_id, self.def_id, self.mount_id)) }
54                .to_str()?,
55        ))
56    }
57
58    pub fn slaved_to(&self) -> Result<Self, Box<dyn Error>> {
59        let get_slaved_to_func = get_callback!(self.ai_id, UnitDef_WeaponMount_getSlavedTo)?;
60        let slave_id = unsafe { get_slaved_to_func(self.ai_id, self.def_id, self.mount_id) };
61        Self::get_weapon_def(self.ai_id, self.def_id, slave_id)
62    }
63
64    pub fn main_direction(&self) -> Result<[f32; 3], Box<dyn Error>> {
65        let get_main_direction_func = get_callback!(self.ai_id, UnitDef_WeaponMount_getMainDir)?;
66
67        let mut ret = [0.0_f32; 3];
68        unsafe {
69            get_main_direction_func(self.ai_id, self.def_id, self.mount_id, ret.as_mut_ptr())
70        };
71
72        Ok(ret)
73    }
74
75    pub fn max_angle(&self) -> Result<f32, Box<dyn Error>> {
76        let get_max_angle_func = get_callback!(self.ai_id, UnitDef_WeaponMount_getMaxAngleDif)?;
77        Ok(unsafe { get_max_angle_func(self.ai_id, self.def_id, self.mount_id) })
78    }
79
80    pub fn target_blacklist(&self) -> Result<i32, Box<dyn Error>> {
81        let get_target_category_func =
82            get_callback!(self.ai_id, UnitDef_WeaponMount_getBadTargetCategory)?;
83        Ok(unsafe { get_target_category_func(self.ai_id, self.def_id, self.mount_id) })
84    }
85
86    pub fn target_whitelist(&self) -> Result<i32, Box<dyn Error>> {
87        let get_only_target_category_func =
88            get_callback!(self.ai_id, UnitDef_WeaponMount_getOnlyTargetCategory)?;
89        Ok(unsafe { get_only_target_category_func(self.ai_id, self.def_id, self.mount_id) })
90    }
91
92    pub fn all(&self) -> Result<UnitWeaponMountAll, Box<dyn Error>> {
93        Ok(UnitWeaponMountAll {
94            mount_name: self.mount_name()?,
95            slaved_to: self.slaved_to()?,
96            main_direction: self.main_direction()?,
97            max_angle: self.max_angle()?,
98            target_blacklist: self.target_blacklist()?,
99            target_whitelist: self.target_whitelist()?,
100        })
101    }
102}