summaryrefslogtreecommitdiff
path: root/src/module.rs
blob: 333ba05fc2b2d3b63f9491f0c9a56ab8d4209805 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::time::SystemTime;
use std::collections::HashMap;

use mass::Mass;
use math::distance;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum ModuleType {
    Navigation,
    Mining,
    Engines,
    Dashboard,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum NavigationStatus {
    None,
    Targeting,
    Targeted,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Navigation {
    pub range       : f64,
    pub status      : NavigationStatus,
    pub target_name : Option<String>,
    time            : u64,
    start           : Option<SystemTime>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Mining {
    pub range       : f64,
    pub status      : bool,
    time            : u64,
    start           : Option<SystemTime>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Engines {
    acceleration : (f64, f64, f64),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Dashboard {}

impl Mining {
    pub fn new() -> Mining {
        Mining {
            range   : 10.0,
            status  : false,
            time    : 1,
            start   : None,
        }
    }

    pub fn toggle(&self) {
    }
}

impl Navigation {
    pub fn new() -> Navigation {
        Navigation {
            target_name : None,
            range       : 100.0,
            status      : NavigationStatus::None,
            time        : 3,
            start       : None,
        }
    }

    pub fn process(&mut self) {
        match self.start.clone() {
            Some(timer) => {
                if timer.elapsed().unwrap().as_secs() > self.time {
                    self.status = NavigationStatus::Targeted;
                    self.start = None;
                }
            }
            _ => (),
        }
    }

    pub fn give_target(&mut self, target_name : String) {
        self.start = Some(SystemTime::now());
        self.status = NavigationStatus::Targeting;
        self.target_name = Some(target_name);
    }

    pub fn verify_target(&mut self, ship_position : (f64, f64, f64), masses : &HashMap<String, Mass>) {
        match self.target_name.clone() {
            Some(name) => {
                let target = masses.get(&name).unwrap();
                if distance(target.position, ship_position) > self.range {
                    self.target_name = None;
                    self.status = NavigationStatus::None;
                }
            }
            _ => (),
        }
    }
}

impl Dashboard {
    pub fn new() -> Dashboard {
        Dashboard {}
    }
}

impl Engines {
    pub fn new() -> Engines {
        Engines {
            acceleration : (0.0, 0.0, 0.0)
        }
    }

    pub fn recv_acceleration(&mut self) -> (f64, f64, f64) {
        let acceleration = self.acceleration;
        self.acceleration = (0.0, 0.0, 0.0);
        acceleration
    }

    pub fn give_client_data(&mut self, ship : &Mass, target : Option<&Mass>, data : String) {
        let mut acceleration = (0.0, 0.0, 0.0);
        match data.as_bytes() {
            b"5\n" => acceleration.0 += 0.1,
            b"0\n" => acceleration.0 -= 0.1,
            b"8\n" => acceleration.1 += 0.1,
            b"2\n" => acceleration.1 -= 0.1,
            b"4\n" => acceleration.2 += 0.1,
            b"6\n" => acceleration.2 -= 0.1,
            b"+\n" => {
                let m_v = ship.velocity;
                acceleration = (m_v.0 * 0.05,
                                m_v.1 * 0.05,
                                m_v.2 * 0.05);
            },
            b"-\n" => {
                let m_v = ship.velocity;
                acceleration = (-1.0 * m_v.0 * 0.05,
                                -1.0 * m_v.1 * 0.05,
                                -1.0 * m_v.2 * 0.05);
            },
            b"c\n" => {
                match target {
                    Some(target) => {
                        let d_v = target.velocity;
                        let m_v = ship.velocity;
                        acceleration = (d_v.0 - m_v.0,
                                        d_v.1 - m_v.1,
                                        d_v.2 - m_v.2);
                    },
                    None => (),
                }
            },
            b"t\n" => {
                match target {
                    Some(target) => {
                        let d_p = target.position;
                        let m_p = ship.position;
                        acceleration = ((d_p.0 - m_p.0) * 0.01,
                                        (d_p.1 - m_p.1) * 0.01,
                                        (d_p.2 - m_p.2) * 0.01);
                    },
                    None => (),
                }
            },
            _ => (),
        }
        self.acceleration = acceleration;
    }
}