1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::ffi::CString;

use spring_ai_sys::COMMAND_TO_ID_ENGINE;

use crate::ai_interface::{
    callback::{
        command::{
            command_data::{
                send::{
                    SendResourcesCommandData, SendStartPosCommandData, SendTextMessageCommandData,
                    SendUnitsCommandData,
                },
                CommandData,
            },
            command_topic::CommandTopic,
        },
        engine::handle_command,
        resource::Resource,
        teams::Team,
        unit::Unit,
    },
    AIInterface,
};

pub struct Send {
    pub ai_id: i32,
}

impl AIInterface {
    pub fn send(&self) -> Send {
        Send { ai_id: self.ai_id }
    }
}

impl Send {
    pub fn resources(
        &self,
        resource: &Resource,
        amount: f32,
        receiving_team: &Team,
    ) -> Result<(), &'static str> {
        let mut command_data = SendResourcesCommandData {
            resource_id: resource.resource_id,
            amount: 0.0,
            receiving_team_id: receiving_team.team_id,
        };

        handle_command(
            self.ai_id,
            COMMAND_TO_ID_ENGINE,
            -1,
            CommandTopic::SendResources.into(),
            &mut command_data.c_data(),
        )
    }

    pub fn start_position(&self, ready: bool, position: [f32; 3]) -> Result<(), &'static str> {
        let mut command_data = SendStartPosCommandData { ready, position };

        handle_command(
            self.ai_id,
            COMMAND_TO_ID_ENGINE,
            -1,
            CommandTopic::SendStartPOS.into(),
            &mut command_data.c_data(),
        )
    }

    pub fn text_message<S>(&self, message: S, zone: i32) -> Result<(), &'static str>
    where
        S: Into<Vec<u8>>,
    {
        let mut command_data = SendTextMessageCommandData {
            text: CString::new(message).unwrap(),
            zone,
        };

        handle_command(
            self.ai_id,
            COMMAND_TO_ID_ENGINE,
            -1,
            CommandTopic::SendTextMessage.into(),
            &mut command_data.c_data(),
        )
    }

    pub fn units(&self, units: &[Unit]) -> Result<(), &'static str> {
        let mut command_data = SendUnitsCommandData {
            unit_ids: units.iter().map(|u| u.unit_id).collect(),
            receiving_team_id: 0,
        };

        handle_command(
            self.ai_id,
            COMMAND_TO_ID_ENGINE,
            -1,
            CommandTopic::SendUnits.into(),
            &mut command_data.c_data(),
        )
    }
}