spring_ai_rs/ai_interface/callback/
position.rs

1use std::{
2    error::Error,
3    ops::{Index, IndexMut},
4};
5
6use crate::ai_interface::callback::map::Map;
7
8pub trait Position {
9    fn to_f32_array(&self, ai_id: i32) -> Result<[f32; 3], Box<dyn Error>>;
10}
11
12#[derive(Copy, Clone, Debug)]
13pub struct AirPosition(pub f32, pub f32);
14
15impl Index<usize> for AirPosition {
16    type Output = f32;
17
18    fn index(&self, index: usize) -> &Self::Output {
19        match index {
20            0 => &self.0,
21            1 => &self.1,
22            _ => unimplemented!(),
23        }
24    }
25}
26
27impl IndexMut<usize> for AirPosition {
28    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
29        match index {
30            0 => &mut self.0,
31            1 => &mut self.1,
32            _ => unimplemented!(),
33        }
34    }
35}
36
37impl Position for AirPosition {
38    fn to_f32_array(&self, ai_id: i32) -> Result<[f32; 3], Box<dyn Error>> {
39        Ok([self[0], Map { ai_id }.max_height()?, self[1]])
40    }
41}
42
43#[derive(Copy, Clone, Debug)]
44pub struct GroundPosition(pub f32, pub f32);
45
46impl Index<usize> for GroundPosition {
47    type Output = f32;
48
49    fn index(&self, index: usize) -> &Self::Output {
50        match index {
51            0 => &self.0,
52            1 => &self.1,
53            _ => unimplemented!(),
54        }
55    }
56}
57
58impl IndexMut<usize> for GroundPosition {
59    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
60        match index {
61            0 => &mut self.0,
62            1 => &mut self.1,
63            _ => unimplemented!(),
64        }
65    }
66}
67
68impl Position for GroundPosition {
69    fn to_f32_array(&self, ai_id: i32) -> Result<[f32; 3], Box<dyn Error>> {
70        Ok([
71            self[0],
72            // TODO: (Takes too long; I think it's the copy of the whole map) Map { ai_id }.center_height_map()?[(self[0] as usize / 8, self[1] as usize / 8)],
73            0.0, self[1],
74        ])
75    }
76}
77
78#[derive(Copy, Clone, Debug)]
79pub struct WaterPosition(pub f32, pub f32);
80
81impl Index<usize> for WaterPosition {
82    type Output = f32;
83
84    fn index(&self, index: usize) -> &Self::Output {
85        match index {
86            0 => &self.0,
87            1 => &self.1,
88            _ => unimplemented!(),
89        }
90    }
91}
92
93impl IndexMut<usize> for WaterPosition {
94    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
95        match index {
96            0 => &mut self.0,
97            1 => &mut self.1,
98            _ => unimplemented!(),
99        }
100    }
101}
102
103impl Position for WaterPosition {
104    fn to_f32_array(&self, _: i32) -> Result<[f32; 3], Box<dyn Error>> {
105        Ok([self[0], 0.0, self[1]])
106    }
107}
108
109impl Position for [f32; 3] {
110    fn to_f32_array(&self, _: i32) -> Result<[f32; 3], Box<dyn Error>> {
111        Ok([self[0], 0.0, self[1]])
112    }
113}