spring_ai_rs/ai_interface/callback/
send.rs

1use std::ffi::CString;
2
3use spring_ai_sys::COMMAND_TO_ID_ENGINE;
4
5use crate::ai_interface::{
6    callback::{
7        command::{
8            command_data::{
9                send::{
10                    SendResourcesCommandData, SendStartPosCommandData, SendTextMessageCommandData,
11                    SendUnitsCommandData,
12                },
13                CommandData,
14            },
15            command_topic::CommandTopic,
16        },
17        engine::handle_command,
18        resource::Resource,
19        teams::Team,
20        unit::Unit,
21    },
22    AIInterface,
23};
24
25pub struct Send {
26    pub ai_id: i32,
27}
28
29impl AIInterface {
30    pub fn send(&self) -> Send {
31        Send { ai_id: self.ai_id }
32    }
33}
34
35impl Send {
36    pub fn resources(
37        &self,
38        resource: &Resource,
39        amount: f32,
40        receiving_team: &Team,
41    ) -> Result<(), &'static str> {
42        let mut command_data = SendResourcesCommandData {
43            resource_id: resource.resource_id,
44            amount: 0.0,
45            receiving_team_id: receiving_team.team_id,
46        };
47
48        handle_command(
49            self.ai_id,
50            COMMAND_TO_ID_ENGINE,
51            -1,
52            CommandTopic::SendResources.into(),
53            &mut command_data.c_data(),
54        )
55    }
56
57    pub fn start_position(&self, ready: bool, position: [f32; 3]) -> Result<(), &'static str> {
58        let mut command_data = SendStartPosCommandData { ready, position };
59
60        handle_command(
61            self.ai_id,
62            COMMAND_TO_ID_ENGINE,
63            -1,
64            CommandTopic::SendStartPOS.into(),
65            &mut command_data.c_data(),
66        )
67    }
68
69    pub fn text_message<S>(&self, message: S, zone: i32) -> Result<(), &'static str>
70    where
71        S: Into<Vec<u8>>,
72    {
73        let mut command_data = SendTextMessageCommandData {
74            text: CString::new(message).unwrap(),
75            zone,
76        };
77
78        handle_command(
79            self.ai_id,
80            COMMAND_TO_ID_ENGINE,
81            -1,
82            CommandTopic::SendTextMessage.into(),
83            &mut command_data.c_data(),
84        )
85    }
86
87    pub fn units(&self, units: &[Unit]) -> Result<(), &'static str> {
88        let mut command_data = SendUnitsCommandData {
89            unit_ids: units.iter().map(|u| u.unit_id).collect(),
90            receiving_team_id: 0,
91        };
92
93        handle_command(
94            self.ai_id,
95            COMMAND_TO_ID_ENGINE,
96            -1,
97            CommandTopic::SendUnits.into(),
98            &mut command_data.c_data(),
99        )
100    }
101}