dis_rust/common/
entity_type_record.rs

1//     dis-rust - A rust implementation of the DIS simulation protocol.
2//     Copyright (C) 2022 Thomas Mann
3// 
4//     This software is dual-licensed. It is available under the conditions of
5//     the GNU Affero General Public License (see the LICENSE file included) 
6//     or under a commercial license (email contact@coffeebreakdevs.com for
7//     details).
8
9use bytes::{BytesMut, BufMut, Buf};
10use num_derive::FromPrimitive;
11use serde::{Serialize, Deserialize};    
12
13#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
14/// Entity Type Record as defined in IEEE 1278.1 standard. Used to communicate the type of an entity during the simulation.
15pub struct EntityTypeRecord {
16    pub kind_field: Kind,
17    pub domain_field: u8,
18    pub country_field: Country,
19    pub category_field: u8,
20    pub subcategory_field: u8,
21    pub specific_field: u8,
22    pub extra_field: u8
23}
24
25impl EntityTypeRecord {
26    /// Provides a function to create a new EntityTypeRecord.
27    pub fn new(
28        kind: Kind,
29        domain: u8,
30        country: Country,
31        category: u8,
32        subcategory: u8,
33        specific: u8,
34        extra: u8) -> Self {
35        EntityTypeRecord {
36            kind_field: kind,
37            domain_field: domain,
38            country_field: country,
39            category_field: category,
40            subcategory_field: subcategory,
41            specific_field: specific,
42            extra_field: extra
43        }
44    }
45
46    /// Fills a BytesMut struct with a EntityTypeRecord serialised into binary. This buffer is then ready to be sent via
47    /// UDP to the simluation network.
48    pub fn serialize(&self, buf: &mut BytesMut) {
49        buf.put_u8(self.kind_field as u8);
50        buf.put_u8(self.domain_field);
51        buf.put_u16(self.country_field as u16);
52        buf.put_u8(self.category_field);
53        buf.put_u8(self.subcategory_field);
54        buf.put_u8(self.specific_field);
55        buf.put_u8(self.extra_field);
56    }
57
58    pub fn decode(buf: &mut BytesMut) -> EntityTypeRecord {
59        EntityTypeRecord { 
60            kind_field: EntityTypeRecord::decode_kind(buf.get_u8()), 
61            domain_field: buf.get_u8(), 
62            country_field: EntityTypeRecord::decode_country(buf.get_u16()), 
63            category_field: buf.get_u8(), 
64            subcategory_field: buf.get_u8(), 
65            specific_field: buf.get_u8(), 
66            extra_field: buf.get_u8()
67        }
68    }
69
70    fn decode_kind(data: u8) -> Kind {
71        match data {
72            1 => Kind::Platform,
73            2 => Kind::Munition,
74            3 => Kind::LifeForm,
75            4 => Kind::Environmental,
76            5 => Kind::CulturalFeature,
77            6 => Kind::Supply,
78            7 => Kind::Radio,
79            8 => Kind::Expendable,
80            9 => Kind::SensorEmittor,
81            _ => Kind::Other
82        }
83    }
84
85    fn decode_country(data: u16) -> Country {
86        match data {
87            1 => Country::Afghanistian,
88            2 => Country::Argentina,
89            3 => Country::Indonesia,
90            4 => Country::Iran,
91            5 => Country::Iraq,
92            6 => Country::Ireland,
93            7 => Country::Israel,
94            8 => Country::Italy,
95            9 => Country::NorthKorea,
96            120 => Country::SouthKorea,
97            224 => Country::UnitedKingdom,
98            225 => Country::UnitedStates,
99            260 => Country::Russia,
100            265 => Country::Ukraine,
101            _ => Country::Other
102        }
103    }
104}
105
106#[derive(FromPrimitive, Debug, Copy, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
107/// Enum to represent the kind of entity.
108pub enum Kind {
109    Other = 0,
110    Platform = 1,
111    Munition = 2,
112    LifeForm = 3,
113    Environmental = 4,
114    CulturalFeature = 5,
115    Supply = 6,
116    Radio = 7,
117    Expendable = 8,
118    SensorEmittor = 9
119}
120
121#[derive(FromPrimitive, Debug,Copy, Clone, PartialEq, Serialize, Deserialize)]
122/// Enum to represent the country of the entity.
123pub enum Country {
124    Other = 0,
125    Afghanistian = 1,
126    Argentina = 10,
127    Indonesia = 100,
128    Iran = 101,
129    Iraq = 102,
130    Ireland = 104,
131    Israel = 105,
132    Italy = 106,
133    Japan = 110,
134    NorthKorea = 119,
135    SouthKorea = 120,
136    UnitedKingdom = 224,
137    UnitedStates = 225,
138    Russia = 260,
139    Ukraine = 265,
140    // ...
141}