summaryrefslogtreecommitdiff
path: root/src/modules/tractorbeam.rs
blob: 726c8b11cff05cbeafd58eca9afa7ab4e572f319 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::constants;
use crate::mass::{Mass, MassType};
use crate::math::ControlSystem;
use crate::math::Vector;
use crate::modules::types::ModuleType;

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Tractorbeam {
    pub range: f64,
    pub status: Status,
    strength: f64,
    desired_distance: Option<f64>,
    control_system: ControlSystem,
}

impl Tractorbeam {
    pub fn new() -> Tractorbeam {
        Tractorbeam {
            range: constants::SHIP_TRACTORBEAM_RANGE,
            status: Status::None,
            strength: constants::SHIP_TRACTORBEAM_STRENGTH,
            desired_distance: None,
            control_system: ControlSystem::new(ModuleType::Tractorbeam),
        }
    }

    pub fn process(&mut self, ship_position: Vector, target: &mut Mass) -> bool {
        let distance = ship_position.distance_from(target.position.clone());
        if self.range < distance {
            self.off()
        } else {
            let direction = target.position.clone() - ship_position.clone();
            let acceleration = match self.status {
                Status::Push => direction.unitize() * self.strength,
                Status::Pull => direction.unitize() * -1.0 * self.strength,
                Status::Bring => match self.desired_distance {
                    Some(desired_distance) => {
                        direction.unitize()
                            * self
                                .control_system
                                .compute(self.strength, distance, desired_distance)
                    }
                    None => Vector::default(),
                },
                Status::Acquire => {
                    match target.mass_type {
                        MassType::Item { .. } => (),
                        _ => {
                            self.status = Status::None;
                            Vector::default();
                        }
                    }
                    if distance > constants::SHIP_TRACTORBEAM_ACQUIRE_RANGE {
                        direction.unitize()
                            * self.control_system.compute(
                                self.strength,
                                distance,
                                constants::SHIP_TRACTORBEAM_ACQUIRE_RANGE,
                            )
                    } else {
                        self.status = Status::None;
                        return true;
                    }
                }
                Status::None => Vector::default(),
            };

            target.effects.give_acceleration(acceleration);
        }
        false
    }

    pub fn get_client_data(&self, target: Option<&Mass>) -> String {
        let client_data = ClientData {
            desired_distance: self.desired_distance,
            has_target: target.is_some(),
            status: self.status.clone(),
        };

        serde_json::to_string(&client_data).unwrap() + "\n"
    }

    pub fn give_received_data(&mut self, recv: String) {
        let server_recv_data: Result<ServerRecvData, serde_json::Error> =
            serde_json::from_str(&recv);
        if let Ok(server_recv_data) = server_recv_data {
            match server_recv_data.key.as_ref() {
                "o" => self.toggle_pull(),
                "p" => self.toggle_push(),
                "b" => self.toggle_bring(server_recv_data.desired_distance),
                "a" => self.toggle_acquire(),
                _ => (),
            }
        }
    }

    fn toggle_pull(&mut self) {
        self.status = match self.status {
            Status::Pull => Status::None,
            _ => Status::Pull,
        }
    }

    fn toggle_push(&mut self) {
        self.status = match self.status {
            Status::Push => Status::None,
            _ => Status::Push,
        }
    }

    fn toggle_bring(&mut self, desired_distance: Option<f64>) {
        self.desired_distance = desired_distance;
        self.control_system = ControlSystem::new(ModuleType::Tractorbeam);
        self.status = match self.status {
            Status::Bring => Status::None,
            _ => Status::Bring,
        }
    }

    fn toggle_acquire(&mut self) {
        self.control_system = ControlSystem::new(ModuleType::Tractorbeam);
        self.status = match self.status {
            Status::Acquire => Status::None,
            _ => Status::Acquire,
        }
    }

    fn off(&mut self) {
        self.status = Status::None
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientData {
    pub has_target: bool,
    pub desired_distance: Option<f64>,
    pub status: Status,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ServerRecvData {
    pub key: String,
    pub desired_distance: Option<f64>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum Status {
    None,
    Push,
    Pull,
    Bring,
    Acquire,
}

impl Default for Status {
    fn default() -> Self {
        Status::None
    }
}