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

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

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

    pub fn process(&mut self, velocity: Vector) {
        if let Some(target_velocity) = self.target_velocity.clone() {
            self.acceleration += target_velocity - velocity;
            if self.acceleration == Vector::default() {
                self.target_velocity = None
            }
        }
    }

    pub fn give_received_data(
        &mut self,
        recv: String,
        position: Vector,
        velocity: Vector,
        target: Option<&Mass>,
    ) {
        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.target_velocity = Some(Vector::default()),
            "c" => {
                if let Some(target) = target {
                    self.target_velocity = Some(target.velocity.clone());
                }
            }
            "t" => {
                if let Some(target) = target {
                    acceleration = (target.position.clone() - position).unitize()
                        * constants::SHIP_ENGINES_ACCELERATION;
                }
            }
            _ => (),
        }
        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,
}