spring_ai_rs/ai_interface/callback/unit_def/
movement.rs

1use std::error::Error;
2
3use serde::{Deserialize, Serialize};
4
5use crate::get_callback;
6
7#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
8pub struct UnitMovement {
9    pub ai_id: i32,
10    pub def_id: i32,
11}
12
13#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
14pub enum MoveType {
15    Ground,
16    Hover,
17    Ship,
18}
19
20impl From<i32> for MoveType {
21    fn from(i: i32) -> Self {
22        match i {
23            1 => MoveType::Hover,
24            2 => MoveType::Ship,
25            _ => MoveType::Ground,
26        }
27    }
28}
29
30#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
31pub enum SpeedModClass {
32    Tank,
33    KBot,
34    Hover,
35    Ship,
36}
37
38impl From<i32> for SpeedModClass {
39    fn from(i: i32) -> Self {
40        match i {
41            1 => SpeedModClass::KBot,
42            2 => SpeedModClass::Hover,
43            3 => SpeedModClass::Ship,
44            _ => SpeedModClass::Tank,
45        }
46    }
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct UnitMovementAll {
51    pub speed: f32,
52    pub turn_rate: f32,
53    pub can_turn_in_place: bool,
54    pub turn_in_place_distance: f32,
55    pub turn_in_place_speed_limit: f32,
56    pub able_to_submerge: bool,
57    pub able_to_fly: bool,
58    pub able_to_move: bool,
59    pub able_to_hover: bool,
60    pub floater: bool,
61    // TODO:
62    pub move_state: i32,
63    //    pub x_size: i32,
64    //    pub z_size: i32,
65    //    pub depth: f32,
66    //    pub max_slope: f32,
67    //    pub slope_mod: f32,
68    //    pub path_type: i32,
69    //    pub crush_strength: f32,
70    //    pub move_type: MoveType,
71    //    pub speed_mod_class: SpeedModClass,
72    //    pub terrain_class: i32,
73    //    pub follow_ground: bool,
74    //    pub sub_marine: bool,
75}
76
77impl UnitMovement {
78    pub fn speed(&self) -> Result<f32, Box<dyn Error>> {
79        let get_speed_func = get_callback!(self.ai_id, UnitDef_getSpeed)?;
80        Ok(unsafe { get_speed_func(self.ai_id, self.def_id) })
81    }
82
83    pub fn turn_rate(&self) -> Result<f32, Box<dyn Error>> {
84        let get_turn_rate_func = get_callback!(self.ai_id, UnitDef_getTurnRate)?;
85        Ok(unsafe { get_turn_rate_func(self.ai_id, self.def_id) })
86    }
87
88    pub fn can_turn_in_place(&self) -> Result<bool, Box<dyn Error>> {
89        let get_is_turn_in_place_func = get_callback!(self.ai_id, UnitDef_isTurnInPlace)?;
90        Ok(unsafe { get_is_turn_in_place_func(self.ai_id, self.def_id) })
91    }
92
93    pub fn turn_in_place_distance(&self) -> Result<f32, Box<dyn Error>> {
94        let get_turn_in_place_distance_func =
95            get_callback!(self.ai_id, UnitDef_getTurnInPlaceDistance)?;
96        Ok(unsafe { get_turn_in_place_distance_func(self.ai_id, self.def_id) })
97    }
98
99    pub fn turn_in_place_speed_limit(&self) -> Result<f32, Box<dyn Error>> {
100        let get_turn_in_place_speed_limit_func =
101            get_callback!(self.ai_id, UnitDef_getTurnInPlaceSpeedLimit)?;
102        Ok(unsafe { get_turn_in_place_speed_limit_func(self.ai_id, self.def_id) })
103    }
104
105    pub fn able_to_submerge(&self) -> Result<bool, Box<dyn Error>> {
106        let able_to_submerge_func = get_callback!(self.ai_id, UnitDef_isAbleToSubmerge)?;
107        Ok(unsafe { able_to_submerge_func(self.ai_id, self.def_id) })
108    }
109
110    pub fn able_to_fly(&self) -> Result<bool, Box<dyn Error>> {
111        let able_to_fly_func = get_callback!(self.ai_id, UnitDef_isAbleToFly)?;
112        Ok(unsafe { able_to_fly_func(self.ai_id, self.def_id) })
113    }
114
115    pub fn able_to_move(&self) -> Result<bool, Box<dyn Error>> {
116        let able_to_move_func = get_callback!(self.ai_id, UnitDef_isAbleToMove)?;
117        Ok(unsafe { able_to_move_func(self.ai_id, self.def_id) })
118    }
119
120    pub fn able_to_hover(&self) -> Result<bool, Box<dyn Error>> {
121        let able_to_hover_func = get_callback!(self.ai_id, UnitDef_isAbleToHover)?;
122        Ok(unsafe { able_to_hover_func(self.ai_id, self.def_id) })
123    }
124
125    pub fn floater(&self) -> Result<bool, Box<dyn Error>> {
126        let floater_func = get_callback!(self.ai_id, UnitDef_isFloater)?;
127        Ok(unsafe { floater_func(self.ai_id, self.def_id) })
128    }
129
130    pub fn move_state(&self) -> Result<i32, Box<dyn Error>> {
131        let get_move_state_func = get_callback!(self.ai_id, UnitDef_getMoveState)?;
132        Ok(unsafe { get_move_state_func(self.ai_id, self.def_id) })
133    }
134
135    //    pub fn x_size(&self) -> Result<i32, Box<dyn Error>> {
136    //        let get_x_size_func = get_callback!(self.ai_id, UnitDef_MoveData_getXSize)?;
137    //        Ok(unsafe { get_x_size_func(self.ai_id, self.def_id) })
138    //    }
139    //
140    //    pub fn z_size(&self) -> Result<i32, Box<dyn Error>> {
141    //        let get_z_size_func = get_callback!(self.ai_id, UnitDef_MoveData_getZSize)?;
142    //        Ok(unsafe { get_z_size_func(self.ai_id, self.def_id) })
143    //    }
144    //
145    //    pub fn depth(&self) -> Result<f32, Box<dyn Error>> {
146    //        let get_depth_func = get_callback!(self.ai_id, UnitDef_MoveData_getDepth)?;
147    //        Ok(unsafe { get_depth_func(self.ai_id, self.def_id) })
148    //    }
149    //
150    //    pub fn max_slope(&self) -> Result<f32, Box<dyn Error>> {
151    //        let get_max_slope_func = get_callback!(self.ai_id, UnitDef_MoveData_getMaxSlope)?;
152    //        Ok(unsafe { get_max_slope_func(self.ai_id, self.def_id) })
153    //    }
154    //
155    //    pub fn slope_mod(&self) -> Result<f32, Box<dyn Error>> {
156    //        let get_slope_mod_func = get_callback!(self.ai_id, UnitDef_MoveData_getSlopeMod)?;
157    //        Ok(unsafe { get_slope_mod_func(self.ai_id, self.def_id) })
158    //    }
159
160    //    pub fn depth_mod(&self, height: f32) -> Result<f32, Box<dyn Error>> {
161    //        let get_depth_mod_func = get_callback!(self.ai_id, UnitDef_MoveData_getDepthMod)?;
162    //        Ok(unsafe { get_depth_mod_func(self.ai_id, self.def_id, height) })
163    //    }
164
165    //    pub fn path_type(&self) -> Result<i32, Box<dyn Error>> {
166    //        let get_path_type_func = get_callback!(self.ai_id, UnitDef_MoveData_getPathType)?;
167    //        Ok(unsafe { get_path_type_func(self.ai_id, self.def_id) })
168    //    }
169    //
170    //    pub fn crush_strength(&self) -> Result<f32, Box<dyn Error>> {
171    //        let get_crush_strength_func = get_callback!(self.ai_id, UnitDef_MoveData_getCrushStrength)?;
172    //        Ok(unsafe { get_crush_strength_func(self.ai_id, self.def_id) })
173    //    }
174    //
175    //    pub fn move_type(&self) -> Result<MoveType, Box<dyn Error>> {
176    //        let get_move_type_func = get_callback!(self.ai_id, UnitDef_MoveData_getMoveType)?;
177    //        Ok(MoveType::from(unsafe {
178    //            get_move_type_func(self.ai_id, self.def_id)
179    //        }))
180    //    }
181    //
182    //    pub fn speed_mod_class(&self) -> Result<SpeedModClass, Box<dyn Error>> {
183    //        let get_speed_mod_class_func =
184    //            get_callback!(self.ai_id, UnitDef_MoveData_getSpeedModClass)?;
185    //        Ok(SpeedModClass::from(unsafe {
186    //            get_speed_mod_class_func(self.ai_id, self.def_id)
187    //        }))
188    //    }
189    //
190    //    pub fn terrain_class(&self) -> Result<i32, Box<dyn Error>> {
191    //        let get_terrain_class_func = get_callback!(self.ai_id, UnitDef_MoveData_getTerrainClass)?;
192    //        Ok(unsafe { get_terrain_class_func(self.ai_id, self.def_id) })
193    //    }
194    //
195    //    pub fn follow_ground(&self) -> Result<bool, Box<dyn Error>> {
196    //        let get_follow_ground_func = get_callback!(self.ai_id, UnitDef_MoveData_getFollowGround)?;
197    //        Ok(unsafe { get_follow_ground_func(self.ai_id, self.def_id) })
198    //    }
199    //
200    //    pub fn sub_marine(&self) -> Result<bool, Box<dyn Error>> {
201    //        let get_sub_marine_func = get_callback!(self.ai_id, UnitDef_MoveData_isSubMarine)?;
202    //        Ok(unsafe { get_sub_marine_func(self.ai_id, self.def_id) })
203    //    }
204
205    //    pub fn name(&self) -> Result<(), Box<dyn Error>> {
206    //        let get_name_func = get_callback!(self.ai_id, UnitDef_MoveData_getName)?;
207    //        Ok(unsafe { get_name_func(self.ai_id, self.def_id) })
208    //    }
209
210    pub fn all(&self) -> Result<UnitMovementAll, Box<dyn Error>> {
211        Ok(UnitMovementAll {
212            speed: self.speed()?,
213            turn_rate: self.turn_rate()?,
214            can_turn_in_place: self.can_turn_in_place()?,
215            turn_in_place_distance: self.turn_in_place_distance()?,
216            turn_in_place_speed_limit: self.turn_in_place_speed_limit()?,
217            able_to_submerge: self.able_to_submerge()?,
218            able_to_fly: self.able_to_fly()?,
219            able_to_move: self.able_to_move()?,
220            able_to_hover: self.able_to_hover()?,
221            floater: self.floater()?,
222            move_state: self.move_state()?,
223            //            x_size: self.x_size()?,
224            //            z_size: self.z_size()?,
225            //            depth: self.depth()?,
226            //            max_slope: self.max_slope()?,
227            //            slope_mod: self.slope_mod()?,
228            //            path_type: self.path_type()?,
229            //            crush_strength: self.crush_strength()?,
230            //            move_type: self.move_type()?,
231            //            speed_mod_class: self.speed_mod_class()?,
232            //            terrain_class: self.terrain_class()?,
233            //            follow_ground: self.follow_ground()?,
234            //            sub_marine: self.sub_marine()?,
235        })
236    }
237}