spring_ai_rs/ai_interface/callback/unit_def/
flanking.rs1use std::error::Error;
2
3use serde::{Deserialize, Serialize};
4
5use crate::get_callback;
6
7#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
8pub struct UnitFlanking {
9 pub ai_id: i32,
10 pub def_id: i32,
11}
12
13#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
14pub enum FlankingMode {
15 NoBonus,
16 GlobalCoordsMobile,
17 UnitCoordsMobile,
18 UnitCoordsLocked,
19}
20
21impl From<i32> for FlankingMode {
22 fn from(i: i32) -> Self {
23 match i {
24 1 => FlankingMode::GlobalCoordsMobile,
25 2 => FlankingMode::UnitCoordsMobile,
26 3 => FlankingMode::UnitCoordsLocked,
27 _ => FlankingMode::NoBonus,
28 }
29 }
30}
31
32#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
33pub struct UnitFlankingAll {
34 pub mode: FlankingMode,
35 pub direction: [f32; 3],
36 pub max: f32,
37 pub min: f32,
38 pub mobility_add: f32,
39}
40
41impl UnitFlanking {
42 pub fn mode(&self) -> Result<FlankingMode, Box<dyn Error>> {
43 let get_flanking_bonus_mode_func =
44 get_callback!(self.ai_id, UnitDef_FlankingBonus_getMode)?;
45 Ok(FlankingMode::from(unsafe {
46 get_flanking_bonus_mode_func(self.ai_id, self.def_id)
47 }))
48 }
49
50 pub fn direction(&self) -> Result<[f32; 3], Box<dyn Error>> {
51 let get_flanking_bonus_dir_func = get_callback!(self.ai_id, UnitDef_FlankingBonus_getDir)?;
52
53 let mut temp = [0.0_f32; 3];
54 unsafe { get_flanking_bonus_dir_func(self.ai_id, self.def_id, temp.as_mut_ptr()) };
55
56 Ok(temp)
57 }
58
59 pub fn max(&self) -> Result<f32, Box<dyn Error>> {
60 let get_flanking_bonus_max_func = get_callback!(self.ai_id, UnitDef_FlankingBonus_getMax)?;
61 Ok(unsafe { get_flanking_bonus_max_func(self.ai_id, self.def_id) })
62 }
63
64 pub fn min(&self) -> Result<f32, Box<dyn Error>> {
65 let get_flanking_bonus_min_func = get_callback!(self.ai_id, UnitDef_FlankingBonus_getMin)?;
66 Ok(unsafe { get_flanking_bonus_min_func(self.ai_id, self.def_id) })
67 }
68
69 pub fn mobility_add(&self) -> Result<f32, Box<dyn Error>> {
70 let get_flanking_bonus_mobility_add_func =
71 get_callback!(self.ai_id, UnitDef_FlankingBonus_getMobilityAdd)?;
72 Ok(unsafe { get_flanking_bonus_mobility_add_func(self.ai_id, self.def_id) })
73 }
74
75 pub fn all(&self) -> Result<UnitFlankingAll, Box<dyn Error>> {
76 Ok(UnitFlankingAll {
77 mode: self.mode()?,
78 direction: self.direction()?,
79 max: self.max()?,
80 min: self.min()?,
81 mobility_add: self.mobility_add()?,
82 })
83 }
84}