aboutsummaryrefslogtreecommitdiff
path: root/lib/Aircraft.py
blob: 5e6e24d16536accb780ae1eec84774924f4fd898 (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
from lib.PacketManager.packets import FSNETCMD_AIRPLANESTATE
from lib.PacketManager.packets.FSNETCMD_AIRCMD import FSNETCMD_AIRCMD
from logging import debug

class Aircraft:
    """
    An aircraft class - this will hold the info from the Airplane state, weapons etc packets."""
    def __init__(self, parent=None):
        self.parent = parent
        self.name = ""
        self.position = [0,0,0]
        self.attitude = [0,0,0]
        self.initial_config = {}
        self.custom_config = {}
        self.life = -1
        self.prev_life = -1
        self.id = -1
        self.last_packet = None
        self.damage_engine_warn_sent = False
        self.last_over_g_message = 0
        self.just_repaired = False

    def reset(self):
        """Resets the aircraft"""
        self.name = ""
        self.position = [0,0,0]
        self.attitude = [0,0,0]
        self.initial_config = {}
        self.custom_config = {}
        self.life = -1
        self.prev_life = -1
        self.id = -1
        self.last_packet = None
        self.damage_engine_warn_sent = False
        self.just_repaired = False

    def set_position(self, position:list):
        """Sets the position of the aircraft from the Airplane state packet"""
        self.position = position

    def set_attitude(self, attitude:list):
        """Sets the attitude of the aircraft from the Airplane state packet"""
        self.attitude = attitude

    def get_position(self):
        """Returns the position of the aircraft"""
        return self.position

    def get_altitude(self):
        """Returns the altitude in m"""
        return self.position[2]

    def get_attitude(self):
        """Returns the attitude of the aircraft"""
        return self.attitude

    def set_initial_config(self, config:dict):
        """Sets the initial config of the aircraft"""
        for key in config:
            self.initial_config[key] = config[key]

    def get_initial_config_value(self, key:str):
        """Returns the value of the initial config"""
        if key in self.initial_config:
            return self.initial_config[key]

        return None

    def set_custom_config_value(self, key:str, value):
        """Sets a custom config value"""
        self.custom_config[key] = value
        #Send this to the client.

    def add_state(self, packet:FSNETCMD_AIRPLANESTATE):
        """Adds the state of the aircraft"""

        if packet.player_id != self.id:
            return None
        if self.life == -1:
            self.life=packet.life
        
        self.prev_life = self.life
        
        self.life = packet.life
        self.set_position(packet.position)
        self.set_attitude(packet.atti)
        self.last_packet = packet
        return packet

    def check_command(self,command:FSNETCMD_AIRCMD):
        """Checks the command, and adds it to the aircraft"""
        if command.aircraft_id != self.id:
            return
        if command.command:
            self.initial_config[command.command[0]] = command.command[1]
        debug(f"Command: {command.command}")

    def set_afterburner(self, enabled:bool):
        """If the afterburner is avaialble on the aircraft, will send a command
        to toggle it."""
        if self.get_initial_config_value("AFTBURNR") == "TRUE":
            return FSNETCMD_AIRCMD.set_afterburner(self.id,enabled, True)
        return None