spring_ai_rs/ai_interface/callback/
trace.rs

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
use std::error::Error;

use spring_ai_sys::COMMAND_TO_ID_ENGINE;

use crate::ai_interface::{
    callback::{
        command::{
            command_data::{
                trace::{FeatureTraceRayCommandData, TraceRayCommandData},
                CommandData,
            },
            command_topic::CommandTopic,
        },
        engine::handle_command,
        feature::Feature,
        unit::Unit,
    },
    AIInterface,
};

pub struct Trace {
    ai_id: i32,
}

impl AIInterface {
    fn trace(&self) -> Trace {
        Trace { ai_id: self.ai_id }
    }
}

impl Trace {
    pub fn ray(
        &self,
        position: [f32; 3],
        direction: [f32; 3],
        length: f32,
        source_unit_id: i32,
        flags: i32,
    ) -> Result<Unit, Box<dyn Error>> {
        let mut command_c_data = TraceRayCommandData {
            ray_position: position,
            ray_direction: direction,
            ray_length: length,
            source_unit_id,
            flags,
        }
        .c_data();

        handle_command(
            self.ai_id,
            COMMAND_TO_ID_ENGINE,
            -1,
            CommandTopic::TraceRay.into(),
            &mut command_c_data,
        )?;

        Ok(Unit {
            ai_id: self.ai_id,
            unit_id: command_c_data.ret_hitUnitId,
        })
    }

    pub fn feature_ray(
        &self,
        position: [f32; 3],
        direction: [f32; 3],
        length: f32,
        source_unit_id: i32,
        flags: i32,
    ) -> Result<Feature, Box<dyn Error>> {
        let mut command_c_data = FeatureTraceRayCommandData {
            ray_position: position,
            ray_direction: direction,
            ray_length: length,
            source_unit_id,
            flags,
        }
        .c_data();

        handle_command(
            self.ai_id,
            COMMAND_TO_ID_ENGINE,
            -1,
            CommandTopic::TraceRayFeature.into(),
            &mut command_c_data,
        )?;

        Ok(Feature {
            ai_id: self.ai_id,
            feature_id: command_c_data.ret_hitFeatureId,
        })
    }
}