spring_ai_rs/ai_interface/callback/unit_def/
weapon_mount.rs

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
use std::{error::Error, ffi::CStr};

use serde::{Deserialize, Serialize};

use crate::{ai_interface::callback::unit_def::UnitDef, get_callback};

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct UnitWeaponMount {
    pub ai_id: i32,
    pub def_id: i32,
    mount_id: i32,
    weapon_def_id: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnitWeaponMountAll {
    pub mount_name: String,
    pub slaved_to: UnitWeaponMount,
    pub main_direction: [f32; 3],
    pub max_angle: f32,
    pub target_blacklist: i32,
    pub target_whitelist: i32,
}

impl UnitDef {
    pub fn weapon_mounts(&self) -> Result<Vec<UnitWeaponMount>, Box<dyn Error>> {
        let get_weapon_mounts_func = get_callback!(self.ai_id, UnitDef_getWeaponMounts)?;

        let number_of_mounts = unsafe { get_weapon_mounts_func(self.ai_id, self.def_id) };
        Ok((0..number_of_mounts)
            .filter_map(|mount_id| {
                UnitWeaponMount::get_weapon_def(self.ai_id, self.def_id, mount_id).ok()
            })
            .collect())
    }
}

impl UnitWeaponMount {
    pub fn get_weapon_def(ai_id: i32, def_id: i32, mount_id: i32) -> Result<Self, Box<dyn Error>> {
        let get_weapon_def_func = get_callback!(ai_id, UnitDef_WeaponMount_getWeaponDef)?;

        Ok(Self {
            ai_id,
            def_id,
            mount_id,
            weapon_def_id: unsafe { get_weapon_def_func(ai_id, def_id, mount_id) },
        })
    }

    pub fn mount_name(&self) -> Result<String, Box<dyn Error>> {
        let get_mount_name_func = get_callback!(self.ai_id, UnitDef_WeaponMount_getName)?;
        Ok(String::from(
            unsafe { CStr::from_ptr(get_mount_name_func(self.ai_id, self.def_id, self.mount_id)) }
                .to_str()?,
        ))
    }

    pub fn slaved_to(&self) -> Result<Self, Box<dyn Error>> {
        let get_slaved_to_func = get_callback!(self.ai_id, UnitDef_WeaponMount_getSlavedTo)?;
        let slave_id = unsafe { get_slaved_to_func(self.ai_id, self.def_id, self.mount_id) };
        Self::get_weapon_def(self.ai_id, self.def_id, slave_id)
    }

    pub fn main_direction(&self) -> Result<[f32; 3], Box<dyn Error>> {
        let get_main_direction_func = get_callback!(self.ai_id, UnitDef_WeaponMount_getMainDir)?;

        let mut ret = [0.0_f32; 3];
        unsafe {
            get_main_direction_func(self.ai_id, self.def_id, self.mount_id, ret.as_mut_ptr())
        };

        Ok(ret)
    }

    pub fn max_angle(&self) -> Result<f32, Box<dyn Error>> {
        let get_max_angle_func = get_callback!(self.ai_id, UnitDef_WeaponMount_getMaxAngleDif)?;
        Ok(unsafe { get_max_angle_func(self.ai_id, self.def_id, self.mount_id) })
    }

    pub fn target_blacklist(&self) -> Result<i32, Box<dyn Error>> {
        let get_target_category_func =
            get_callback!(self.ai_id, UnitDef_WeaponMount_getBadTargetCategory)?;
        Ok(unsafe { get_target_category_func(self.ai_id, self.def_id, self.mount_id) })
    }

    pub fn target_whitelist(&self) -> Result<i32, Box<dyn Error>> {
        let get_only_target_category_func =
            get_callback!(self.ai_id, UnitDef_WeaponMount_getOnlyTargetCategory)?;
        Ok(unsafe { get_only_target_category_func(self.ai_id, self.def_id, self.mount_id) })
    }

    pub fn all(&self) -> Result<UnitWeaponMountAll, Box<dyn Error>> {
        Ok(UnitWeaponMountAll {
            mount_name: self.mount_name()?,
            slaved_to: self.slaved_to()?,
            main_direction: self.main_direction()?,
            max_angle: self.max_angle()?,
            target_blacklist: self.target_blacklist()?,
            target_whitelist: self.target_whitelist()?,
        })
    }
}