spring_ai_rs/ai_interface/callback/unit_def/
building.rs

1use std::error::Error;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{ai_interface::callback::unit_def::UnitDef, get_callback};
6
7const MAX_BUILD_OPTIONS: usize = 128;
8
9#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
10pub struct UnitBuilding {
11    pub ai_id: i32,
12    pub def_id: i32,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct UnitBuildingAll {
17    pub builder: bool,
18    pub is_build_range_3d: bool,
19    pub build_distance: f32,
20    pub build_speed: f32,
21    pub reclaim_speed: f32,
22    pub repair_speed: f32,
23    pub max_repair_speed: f32,
24    pub resurrect_speed: f32,
25    pub capture_speed: f32,
26    pub up_direction_smoothing: f32,
27    pub terraform_speed: f32,
28    pub able_to_restore: bool,
29    pub able_to_repair: bool,
30    pub able_to_self_repair: bool,
31    pub able_to_reclaim: bool,
32    pub assistable: bool,
33    pub able_to_repeat: bool,
34    pub able_to_capture: bool,
35    pub able_to_resurrect: bool,
36    pub build_options: Vec<UnitDef>,
37}
38
39impl UnitBuilding {
40    pub fn builder(&self) -> Result<bool, Box<dyn Error>> {
41        let builder_func = get_callback!(self.ai_id, UnitDef_isBuilder)?;
42        Ok(unsafe { builder_func(self.ai_id, self.def_id) })
43    }
44
45    pub fn is_build_range_3d(&self) -> Result<bool, Box<dyn Error>> {
46        let get_is_build_range_3d_func = get_callback!(self.ai_id, UnitDef_isBuildRange3D)?;
47        Ok(unsafe { get_is_build_range_3d_func(self.ai_id, self.def_id) })
48    }
49
50    pub fn build_distance(&self) -> Result<f32, Box<dyn Error>> {
51        let get_build_distance_func = get_callback!(self.ai_id, UnitDef_getBuildDistance)?;
52        Ok(unsafe { get_build_distance_func(self.ai_id, self.def_id) })
53    }
54
55    pub fn build_speed(&self) -> Result<f32, Box<dyn Error>> {
56        let get_build_speed_func = get_callback!(self.ai_id, UnitDef_getBuildSpeed)?;
57        Ok(unsafe { get_build_speed_func(self.ai_id, self.def_id) })
58    }
59
60    pub fn reclaim_speed(&self) -> Result<f32, Box<dyn Error>> {
61        let get_reclaim_speed_func = get_callback!(self.ai_id, UnitDef_getReclaimSpeed)?;
62        Ok(unsafe { get_reclaim_speed_func(self.ai_id, self.def_id) })
63    }
64
65    pub fn repair_speed(&self) -> Result<f32, Box<dyn Error>> {
66        let get_repair_speed_func = get_callback!(self.ai_id, UnitDef_getRepairSpeed)?;
67        Ok(unsafe { get_repair_speed_func(self.ai_id, self.def_id) })
68    }
69
70    pub fn max_repair_speed(&self) -> Result<f32, Box<dyn Error>> {
71        let get_max_repair_speed_func = get_callback!(self.ai_id, UnitDef_getMaxRepairSpeed)?;
72        Ok(unsafe { get_max_repair_speed_func(self.ai_id, self.def_id) })
73    }
74
75    pub fn resurrect_speed(&self) -> Result<f32, Box<dyn Error>> {
76        let get_ressurect_speed_func = get_callback!(self.ai_id, UnitDef_getResurrectSpeed)?;
77        Ok(unsafe { get_ressurect_speed_func(self.ai_id, self.def_id) })
78    }
79
80    pub fn capture_speed(&self) -> Result<f32, Box<dyn Error>> {
81        let get_capture_speed_func = get_callback!(self.ai_id, UnitDef_getCaptureSpeed)?;
82        Ok(unsafe { get_capture_speed_func(self.ai_id, self.def_id) })
83    }
84
85    pub fn up_direction_smoothing(&self) -> Result<f32, Box<dyn Error>> {
86        let get_up_dir_smoothing = get_callback!(self.ai_id, UnitDef_getUpDirSmoothing)?;
87        Ok(unsafe { get_up_dir_smoothing(self.ai_id, self.def_id) })
88    }
89
90    pub fn terraform_speed(&self) -> Result<f32, Box<dyn Error>> {
91        let get_terraform_speed_func = get_callback!(self.ai_id, UnitDef_getTerraformSpeed)?;
92        Ok(unsafe { get_terraform_speed_func(self.ai_id, self.def_id) })
93    }
94
95    pub fn able_to_restore(&self) -> Result<bool, Box<dyn Error>> {
96        let able_to_restore_func = get_callback!(self.ai_id, UnitDef_isAbleToRestore)?;
97        Ok(unsafe { able_to_restore_func(self.ai_id, self.def_id) })
98    }
99
100    pub fn able_to_repair(&self) -> Result<bool, Box<dyn Error>> {
101        let able_to_repair_func = get_callback!(self.ai_id, UnitDef_isAbleToRepair)?;
102        Ok(unsafe { able_to_repair_func(self.ai_id, self.def_id) })
103    }
104
105    pub fn able_to_self_repair(&self) -> Result<bool, Box<dyn Error>> {
106        let able_to_self_repair_func = get_callback!(self.ai_id, UnitDef_isAbleToSelfRepair)?;
107        Ok(unsafe { able_to_self_repair_func(self.ai_id, self.def_id) })
108    }
109
110    pub fn able_to_reclaim(&self) -> Result<bool, Box<dyn Error>> {
111        let able_to_reclaim_func = get_callback!(self.ai_id, UnitDef_isAbleToReclaim)?;
112        Ok(unsafe { able_to_reclaim_func(self.ai_id, self.def_id) })
113    }
114
115    pub fn able_to_assist(&self) -> Result<bool, Box<dyn Error>> {
116        let able_to_assist_func = get_callback!(self.ai_id, UnitDef_isAbleToAssist)?;
117        Ok(unsafe { able_to_assist_func(self.ai_id, self.def_id) })
118    }
119
120    pub fn assistable(&self) -> Result<bool, Box<dyn Error>> {
121        let assistable_func = get_callback!(self.ai_id, UnitDef_isAssistable)?;
122        Ok(unsafe { assistable_func(self.ai_id, self.def_id) })
123    }
124
125    pub fn able_to_repeat(&self) -> Result<bool, Box<dyn Error>> {
126        let able_to_repeat_func = get_callback!(self.ai_id, UnitDef_isAbleToRepeat)?;
127        Ok(unsafe { able_to_repeat_func(self.ai_id, self.def_id) })
128    }
129
130    pub fn able_to_capture(&self) -> Result<bool, Box<dyn Error>> {
131        let get_able_to_capture_func = get_callback!(self.ai_id, UnitDef_isAbleToCapture)?;
132        Ok(unsafe { get_able_to_capture_func(self.ai_id, self.def_id) })
133    }
134
135    pub fn able_to_resurrect(&self) -> Result<bool, Box<dyn Error>> {
136        let get_is_able_to_resurrect_func = get_callback!(self.ai_id, UnitDef_isAbleToResurrect)?;
137        Ok(unsafe { get_is_able_to_resurrect_func(self.ai_id, self.def_id) })
138    }
139
140    pub fn build_options(&self) -> Result<Vec<UnitDef>, Box<dyn Error>> {
141        let get_build_options_func = get_callback!(self.ai_id, UnitDef_getBuildOptions)?;
142
143        let mut ret = [-1_i32; MAX_BUILD_OPTIONS];
144        unsafe {
145            get_build_options_func(
146                self.ai_id,
147                self.def_id,
148                ret.as_mut_ptr(),
149                MAX_BUILD_OPTIONS as i32,
150            )
151        };
152
153        Ok(ret
154            .iter()
155            .filter_map(|&def_id| {
156                if def_id == -1 {
157                    None
158                } else {
159                    Some(UnitDef {
160                        ai_id: self.ai_id,
161                        def_id,
162                    })
163                }
164            })
165            .collect())
166    }
167
168    pub fn all(&self) -> Result<UnitBuildingAll, Box<dyn Error>> {
169        Ok(UnitBuildingAll {
170            builder: self.builder()?,
171            is_build_range_3d: self.is_build_range_3d()?,
172            build_distance: self.build_distance()?,
173            build_speed: self.build_speed()?,
174            reclaim_speed: self.reclaim_speed()?,
175            repair_speed: self.repair_speed()?,
176            max_repair_speed: self.max_repair_speed()?,
177            resurrect_speed: self.resurrect_speed()?,
178            capture_speed: self.capture_speed()?,
179            terraform_speed: self.terraform_speed()?,
180            up_direction_smoothing: self.up_direction_smoothing()?,
181            able_to_restore: self.able_to_restore()?,
182            able_to_repair: self.able_to_repair()?,
183            able_to_self_repair: self.able_to_self_repair()?,
184            able_to_reclaim: self.able_to_reclaim()?,
185            assistable: self.assistable()?,
186            able_to_repeat: self.able_to_repeat()?,
187            able_to_capture: self.able_to_capture()?,
188            able_to_resurrect: self.able_to_resurrect()?,
189            build_options: self.build_options()?,
190        })
191    }
192}