summaryrefslogtreecommitdiff
path: root/src/modules/engines.rs
blob: 9aa8297cf31dd70ad2cf816d35c94a2f45ff8ac1 (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
use crate::constants;
use crate::mass::Mass;
use crate::math::Vector;
use crate::modules::navigation;

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Engines {
    pub status: Status,
    acceleration: Vector,
    target_velocity: Option<Vector>,
    pub fuel: f64,
}

impl Engines {
    pub fn new() -> Engines {
        Engines {
            status: Status::None,
            acceleration: Vector::default(),
            target_velocity: None,
            fuel: constants::SHIP_ENGINES_FUEL_START,
        }
    }

    pub fn process(&mut self, position: Vector, velocity: Vector, target: Option<&Mass>) {
        if self.target_velocity.is_none() && self.status != Status::None {
            if self.status == Status::Stopping {
                self.target_velocity = Some(Vector::default());
            }
            if let Some(target) = target {
                match self.status {
                    Status::TowardsTarget => {
                        self.acceleration = (target.position.clone() - position).unitize()
                            * constants::SHIP_ENGINES_ACCELERATION;
                        self.status = Status::None;
                    }
                    Status::FollowingTarget => self.target_velocity = Some(target.velocity.clone()),
                    _ => (),
                }
            } else {
                self.status = Status::None;
            }
        }

        if let Some(target_velocity) = self.target_velocity.clone() {
            self.acceleration += target_velocity - velocity;
            if self.acceleration == Vector::default() {
                self.target_velocity = None;
                self.status = Status::None;
            }
        }
    }

    pub fn give_received_data(&mut self, recv: String, velocity: Vector) {
        let mut acceleration = Vector::default();
        match recv.as_str() {
            "5" => acceleration.x += constants::SHIP_ENGINES_ACCELERATION,
            "0" => acceleration.x -= constants::SHIP_ENGINES_ACCELERATION,
            "8" => acceleration.y += constants::SHIP_ENGINES_ACCELERATION,
            "2" => acceleration.y -= constants::SHIP_ENGINES_ACCELERATION,
            "4" => acceleration.z += constants::SHIP_ENGINES_ACCELERATION,
            "6" => acceleration.z -= constants::SHIP_ENGINES_ACCELERATION,
            "+" => acceleration = velocity.unitize() * constants::SHIP_ENGINES_ACCELERATION,
            "-" => acceleration = velocity.unitize() * -1.0 * constants::SHIP_ENGINES_ACCELERATION,
            "s" => self.status = Status::Stopping,
            "c" => self.status = Status::FollowingTarget,
            "t" => self.status = Status::TowardsTarget,
            _ => (),
        }
        self.acceleration = acceleration;
    }

    pub fn get_client_data(&self, status: navigation::Status) -> String {
        let client_data = ClientData {
            has_target: status == navigation::Status::Targeted,
            fuel: self.fuel,
        };
        serde_json::to_string(&client_data).unwrap() + "\n"
    }

    pub fn take_acceleration(&mut self) -> Vector {
        let mut acceleration = self.acceleration.clone();
        self.acceleration = Vector::default();

        if acceleration.magnitude() >= constants::SHIP_ENGINES_ACCELERATION {
            acceleration = acceleration.unitize() * constants::SHIP_ENGINES_ACCELERATION;
        }
        if self.fuel - acceleration.magnitude() >= 0.0 {
            self.fuel -= acceleration.magnitude();
            acceleration
        } else {
            Vector::default()
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientData {
    pub has_target: bool,
    pub fuel: f64,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum Status {
    None,
    Stopping,
    FollowingTarget,
    TowardsTarget,
}

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