spring_ai_rs/ai_interface/callback/unit_def/
construction.rs

1use std::{collections::HashMap, error::Error};
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    ai_interface::{callback::resource::Resource, AIInterface},
7    get_callback,
8};
9
10#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
11pub struct UnitConstruction {
12    pub ai_id: i32,
13    pub def_id: i32,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct UnitConstructionAll {
18    pub resource: HashMap<Resource, f32>,
19    pub build_time: f32,
20    pub upkeep: HashMap<Resource, f32>,
21    pub max_height_difference: f32,
22    pub min_water_depth: f32,
23    pub max_water_depth: f32,
24}
25
26impl UnitConstruction {
27    pub fn resource(&self, resource: Resource) -> Result<f32, Box<dyn Error>> {
28        let get_cost_func = get_callback!(self.ai_id, UnitDef_getCost)?;
29        Ok(unsafe { get_cost_func(self.ai_id, self.def_id, resource.resource_id) })
30    }
31
32    pub fn build_time(&self) -> Result<f32, Box<dyn Error>> {
33        let get_build_time_func = get_callback!(self.ai_id, UnitDef_getBuildTime)?;
34        Ok(unsafe { get_build_time_func(self.ai_id, self.def_id) })
35    }
36
37    pub fn upkeep(&self, resource: Resource) -> Result<f32, Box<dyn Error>> {
38        let get_upkeep_func = get_callback!(self.ai_id, UnitDef_getUpkeep)?;
39        Ok(unsafe { get_upkeep_func(self.ai_id, self.def_id, resource.resource_id) })
40    }
41
42    pub fn max_height_difference(&self) -> Result<f32, Box<dyn Error>> {
43        let get_max_height_dif_func = get_callback!(self.ai_id, UnitDef_getMaxHeightDif)?;
44        Ok(unsafe { get_max_height_dif_func(self.ai_id, self.def_id) })
45    }
46
47    pub fn min_water_depth(&self) -> Result<f32, Box<dyn Error>> {
48        let get_min_water_depth_func = get_callback!(self.ai_id, UnitDef_getMinWaterDepth)?;
49        Ok(unsafe { get_min_water_depth_func(self.ai_id, self.def_id) })
50    }
51
52    pub fn max_water_depth(&self) -> Result<f32, Box<dyn Error>> {
53        let get_max_water_depth_func = get_callback!(self.ai_id, UnitDef_getMaxWaterDepth)?;
54        Ok(unsafe { get_max_water_depth_func(self.ai_id, self.def_id) })
55    }
56
57    pub fn all(&self) -> Result<UnitConstructionAll, Box<dyn Error>> {
58        Ok(UnitConstructionAll {
59            resource: AIInterface::new(self.ai_id)
60                .resource_interface()
61                .get_resources()?
62                .into_iter()
63                .filter_map(|resource| {
64                    if let Ok(res) = self.resource(resource) {
65                        Some((resource, res))
66                    } else {
67                        None
68                    }
69                })
70                .collect(),
71            build_time: self.build_time()?,
72            upkeep: AIInterface::new(self.ai_id)
73                .resource_interface()
74                .get_resources()?
75                .into_iter()
76                .filter_map(|resource| {
77                    if let Ok(upk) = self.upkeep(resource) {
78                        Some((resource, upk))
79                    } else {
80                        None
81                    }
82                })
83                .collect(),
84            max_height_difference: self.max_height_difference()?,
85            min_water_depth: self.min_water_depth()?,
86            max_water_depth: self.min_water_depth()?,
87        })
88    }
89}