spring_ai_rs/ai_interface/callback/unit_def/
miscellaneous.rs1use 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 UnitMiscellaneous {
9 pub ai_id: i32,
10 pub def_id: i32,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct UnitMiscellaneousAll {
15 pub idle_time: i32,
16 pub power: f32,
17 pub health: f32,
18 pub category_id: i32,
19 pub category_string: String,
20 pub mass: f32,
21 pub upright: bool,
22 pub collide: bool,
23 pub push_resistant: bool,
24 pub min_collision_speed: f32,
25 pub slide_tolerance: f32,
26 pub waterline: f32,
27 pub tooltip: String,
28 pub active_when_built: bool,
29 pub on_offable: bool,
30 pub full_health_factory: bool,
31 pub factory_heading_take_off: bool,
32 pub reclaimable: bool,
33 pub auto_reclaimable: bool,
34 pub capturable: bool,
35 pub x_size: i32,
36 pub z_size: i32,
37 pub max_this_unit: i32,
38}
39
40impl UnitMiscellaneous {
41 pub fn idle_time(&self) -> Result<i32, Box<dyn Error>> {
42 let get_idle_time_func = get_callback!(self.ai_id, UnitDef_getIdleTime)?;
43 Ok(unsafe { get_idle_time_func(self.ai_id, self.def_id) })
44 }
45
46 pub fn power(&self) -> Result<f32, Box<dyn Error>> {
47 let get_power_func = get_callback!(self.ai_id, UnitDef_getPower)?;
48 Ok(unsafe { get_power_func(self.ai_id, self.def_id) })
49 }
50
51 pub fn health(&self) -> Result<f32, Box<dyn Error>> {
52 let get_health_func = get_callback!(self.ai_id, UnitDef_getHealth)?;
53 Ok(unsafe { get_health_func(self.ai_id, self.def_id) })
54 }
55
56 pub fn category_id(&self) -> Result<i32, Box<dyn Error>> {
57 let get_category_func = get_callback!(self.ai_id, UnitDef_getCategory)?;
58 Ok(unsafe { get_category_func(self.ai_id, self.def_id) })
59 }
60
61 pub fn category_string(&self) -> Result<String, Box<dyn Error>> {
62 let get_category_string_func = get_callback!(self.ai_id, UnitDef_getCategoryString)?;
63 Ok(String::from(
64 unsafe { CStr::from_ptr(get_category_string_func(self.ai_id, self.def_id)) }
65 .to_str()?,
66 ))
67 }
68
69 pub fn mass(&self) -> Result<f32, Box<dyn Error>> {
70 let get_mass_func = get_callback!(self.ai_id, UnitDef_getMass)?;
71 Ok(unsafe { get_mass_func(self.ai_id, self.def_id) })
72 }
73
74 pub fn upright(&self) -> Result<bool, Box<dyn Error>> {
75 let get_is_upright_func = get_callback!(self.ai_id, UnitDef_isUpright)?;
76 Ok(unsafe { get_is_upright_func(self.ai_id, self.def_id) })
77 }
78
79 pub fn collide(&self) -> Result<bool, Box<dyn Error>> {
80 let get_is_collide_func = get_callback!(self.ai_id, UnitDef_isCollide)?;
81 Ok(unsafe { get_is_collide_func(self.ai_id, self.def_id) })
82 }
83
84 pub fn push_resistant(&self) -> Result<bool, Box<dyn Error>> {
85 let get_is_push_resistant_func = get_callback!(self.ai_id, UnitDef_isPushResistant)?;
86 Ok(unsafe { get_is_push_resistant_func(self.ai_id, self.def_id) })
87 }
88
89 pub fn min_collision_speed(&self) -> Result<f32, Box<dyn Error>> {
90 let get_min_collision_speed_func = get_callback!(self.ai_id, UnitDef_getMinCollisionSpeed)?;
91 Ok(unsafe { get_min_collision_speed_func(self.ai_id, self.def_id) })
92 }
93
94 pub fn slide_tolerance(&self) -> Result<f32, Box<dyn Error>> {
95 let get_slide_tolerance_func = get_callback!(self.ai_id, UnitDef_getSlideTolerance)?;
96 Ok(unsafe { get_slide_tolerance_func(self.ai_id, self.def_id) })
97 }
98
99 pub fn waterline(&self) -> Result<f32, Box<dyn Error>> {
100 let get_water_line_func = get_callback!(self.ai_id, UnitDef_getWaterline)?;
101 Ok(unsafe { get_water_line_func(self.ai_id, self.def_id) })
102 }
103
104 pub fn tooltip(&self) -> Result<String, Box<dyn Error>> {
105 let get_tooltip_func = get_callback!(self.ai_id, UnitDef_getTooltip)?;
106 Ok(String::from(
107 unsafe { CStr::from_ptr(get_tooltip_func(self.ai_id, self.def_id)) }.to_str()?,
108 ))
109 }
110
111 pub fn active_when_built(&self) -> Result<bool, Box<dyn Error>> {
112 let active_when_built_func = get_callback!(self.ai_id, UnitDef_isActivateWhenBuilt)?;
113 Ok(unsafe { active_when_built_func(self.ai_id, self.def_id) })
114 }
115
116 pub fn on_offable(&self) -> Result<bool, Box<dyn Error>> {
117 let on_offable_func = get_callback!(self.ai_id, UnitDef_isOnOffable)?;
118 Ok(unsafe { on_offable_func(self.ai_id, self.def_id) })
119 }
120
121 pub fn full_health_factory(&self) -> Result<bool, Box<dyn Error>> {
122 let full_health_factory_func = get_callback!(self.ai_id, UnitDef_isFullHealthFactory)?;
123 Ok(unsafe { full_health_factory_func(self.ai_id, self.def_id) })
124 }
125
126 pub fn factory_heading_take_off(&self) -> Result<bool, Box<dyn Error>> {
127 let factory_heading_take_off_func =
128 get_callback!(self.ai_id, UnitDef_isFactoryHeadingTakeoff)?;
129 Ok(unsafe { factory_heading_take_off_func(self.ai_id, self.def_id) })
130 }
131
132 pub fn reclaimable(&self) -> Result<bool, Box<dyn Error>> {
133 let reclaimable_func = get_callback!(self.ai_id, UnitDef_isReclaimable)?;
134 Ok(unsafe { reclaimable_func(self.ai_id, self.def_id) })
135 }
136
137 pub fn auto_reclaimable(&self) -> Result<bool, Box<dyn Error>> {
138 let auto_reclaimable_func = get_callback!(self.ai_id, FeatureDef_isAutoreclaimable)?;
139 Ok(unsafe { auto_reclaimable_func(self.ai_id, self.def_id) })
140 }
141
142 pub fn capturable(&self) -> Result<bool, Box<dyn Error>> {
143 let capturable_func = get_callback!(self.ai_id, UnitDef_isCapturable)?;
144 Ok(unsafe { capturable_func(self.ai_id, self.def_id) })
145 }
146
147 pub fn x_size(&self) -> Result<i32, Box<dyn Error>> {
148 let get_x_size_func = get_callback!(self.ai_id, UnitDef_getXSize)?;
149 Ok(unsafe { get_x_size_func(self.ai_id, self.def_id) } * 8)
150 }
151
152 pub fn z_size(&self) -> Result<i32, Box<dyn Error>> {
153 let get_z_size_func = get_callback!(self.ai_id, UnitDef_getZSize)?;
154 Ok(unsafe { get_z_size_func(self.ai_id, self.def_id) } * 8)
155 }
156
157 pub fn max_this_unit(&self) -> Result<i32, Box<dyn Error>> {
158 let get_max_this_unit_func = get_callback!(self.ai_id, UnitDef_getMaxThisUnit)?;
159 Ok(unsafe { get_max_this_unit_func(self.ai_id, self.def_id) })
160 }
161
162 pub fn all(&self) -> Result<UnitMiscellaneousAll, Box<dyn Error>> {
163 Ok(UnitMiscellaneousAll {
164 idle_time: self.idle_time()?,
165 power: self.power()?,
166 health: self.health()?,
167 category_id: self.category_id()?,
168 category_string: self.category_string()?,
169 mass: self.mass()?,
170 upright: self.upright()?,
171 collide: self.collide()?,
172 push_resistant: self.push_resistant()?,
173 min_collision_speed: self.min_collision_speed()?,
174 slide_tolerance: self.slide_tolerance()?,
175 waterline: self.waterline()?,
176 tooltip: self.tooltip()?,
177 active_when_built: self.active_when_built()?,
178 on_offable: self.on_offable()?,
179 full_health_factory: self.full_health_factory()?,
180 factory_heading_take_off: self.factory_heading_take_off()?,
181 reclaimable: self.reclaimable()?,
182 auto_reclaimable: self.auto_reclaimable()?,
183 capturable: self.capturable()?,
184 x_size: self.x_size()?,
185 z_size: self.z_size()?,
186 max_this_unit: self.max_this_unit()?,
187 })
188 }
189}