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
//     dis-rust - A rust implementation of the DIS simulation protocol.
//     Copyright (C) 2022 Thomas Mann
// 
//     This software is dual-licensed. It is available under the conditions of
//     the GNU Affero General Public License (see the LICENSE file included) 
//     or under a commercial license (email contact@coffeebreakdevs.com for
//     details).

use bytes::{BytesMut, BufMut, Buf};

#[derive(Copy, Clone, Debug, Default)]
/// Entity Capabilities Record as defined in IEEE 1278.1 standard. Used to communicate the capabilities of an entity during the simulation.
pub struct EntityCapabilitiesRecord {
    pub ammunition_supply_field: bool,
    pub fuel_supply_field: bool,
    pub recovery_field: bool,
    pub repair_field: bool,
}

impl EntityCapabilitiesRecord {
    /// Provides a function to create a new EntityCapabilitiesRecord.
    pub fn new(
        ammunition_supply_field: bool,
        fuel_supply_field: bool,
        recovery_field: bool,
        repair_field: bool,) -> Self {
        EntityCapabilitiesRecord {
            ammunition_supply_field,
            fuel_supply_field,
            recovery_field,
            repair_field,
        }
    }

    /// Provides a function to create a default EntityCapabilitiesRecord 
    /// representing an entity with no capabilities.
    pub fn default() -> Self {
        EntityCapabilitiesRecord {
            ammunition_supply_field: false,
            fuel_supply_field: false,
            recovery_field: false,
            repair_field: false,
        }
    }

    /// Fills a BytesMut struct with a EntityCapabilitiesRecord serialised into binary. This buffer is then ready to be sent via
    /// UDP to the simluation network.
    pub fn serialize(&self, buf: &mut BytesMut) {
        let ammunition_supply = if self.ammunition_supply_field { 1u32 } else { 0u32 } << 31;
        let fuel_supply = if self.fuel_supply_field { 1u32 } else { 0u32 } << 30;
        let recovery = if self.recovery_field { 1u32 } else { 0u32 } << 29;
        let repair = if self.repair_field { 1u32 } else { 0u32 } << 28;
        let capabilities = 0u32 | ammunition_supply | fuel_supply | recovery | repair;
        buf.put_u32(capabilities);
    }

    pub fn decode(buf: &mut BytesMut) -> EntityCapabilitiesRecord {
        let bytes = buf.get_u32();
        EntityCapabilitiesRecord {
            ammunition_supply_field: (bytes >> 1) != 0,
            fuel_supply_field: (bytes >> 1) != 0,
            recovery_field: (bytes >> 1) != 0,
            repair_field: (bytes >> 1) != 0,
        }
    }
}