]> git.lizzy.rs Git - mt_net.git/blob - src/to_srv.rs
Use cgmath and support BS constant
[mt_net.git] / src / to_srv.rs
1 use super::*;
2
3 #[mt_derive(to = "srv", repr = "u32", enumset)]
4 pub enum Key {
5     Forward,
6     Backward,
7     Left,
8     Right,
9     Jump,
10     Special,
11     Sneak,
12     Dig,
13     Place,
14     Zoom,
15 }
16
17 #[cfg(feature = "client")]
18 fn ser_cast_err() -> mt_ser::SerializeError {
19     mt_ser::SerializeError::Other("cast failed".into())
20 }
21
22 #[cfg(feature = "server")]
23 fn des_cast_err() -> mt_ser::DeserializeError {
24     mt_ser::DeserializeError::Other("cast failed".into())
25 }
26
27 #[mt_derive(to = "srv")]
28 pub struct PlayerPos {
29     #[mt(multiplier = "100.0 * BS")]
30     #[mt(map_ser = "|x| x.cast::<i32>().ok_or_else(ser_cast_err)")]
31     #[mt(map_des = "|x: Point3<i32>| x.cast::<f32>().ok_or_else(des_cast_err)")]
32     pub pos: Point3<f32>,
33     #[mt(multiplier = "100.0 * BS")]
34     #[mt(map_ser = "|x| x.cast::<i32>().ok_or_else(ser_cast_err)")]
35     #[mt(map_des = "|x: Vector3<i32>| x.cast::<f32>().ok_or_else(des_cast_err)")]
36     pub vel: Vector3<f32>,
37     #[mt(map_ser = "|x| Ok(x.0 as i32)", map_des = "|x: i32| Ok(Deg(x as f32))")]
38     pub pitch: Deg<f32>,
39     #[mt(map_ser = "|x| Ok(x.0 as i32)", map_des = "|x: i32| Ok(Deg(x as f32))")]
40     pub yaw: Deg<f32>,
41     pub keys: EnumSet<Key>,
42     #[mt(multiplier = "80.0")]
43     #[mt(map_ser = "|x| Ok(x.0 as u8)", map_des = "|x: u8| Ok(Rad(x as f32))")]
44     pub fov: Rad<f32>,
45     pub wanted_range: u8,
46 }
47
48 #[mt_derive(to = "srv", repr = "u8")]
49 pub enum Interaction {
50     Dig = 0,
51     StopDigging,
52     Dug,
53     Place,
54     Use,
55     Activate,
56 }
57
58 #[mt_derive(to = "srv", repr = "u8", tag = "type")]
59 #[mt(const_before = "0u8")] // version
60 pub enum PointedThing {
61     None = 0,
62     Node {
63         under: Point3<i16>,
64         above: Point3<i16>,
65     },
66     Obj {
67         obj: u16,
68     },
69 }
70
71 #[mt_derive(to = "srv", repr = "u16", tag = "type", content = "data")]
72 pub enum ToSrvPkt {
73     Nil = 0,
74     Init {
75         serialize_version: u8,
76         #[mt(const_before = "1u16")] // supported compression
77         proto_version: RangeInclusive<u16>,
78         player_name: String,
79         #[mt(default)]
80         send_full_item_meta: bool,
81     } = 2,
82     Init2 {
83         lang: String,
84     } = 17,
85     JoinModChan {
86         channel: String,
87     } = 23,
88     LeaveModChan {
89         channel: String,
90     } = 24,
91     MsgModChan {
92         channel: String,
93         msg: String,
94     } = 25,
95     PlayerPos(PlayerPos) = 35,
96     GotBlocks {
97         #[mt(len = "u8")]
98         blocks: Vec<Point3<i16>>,
99     } = 36,
100     DeletedBlocks {
101         #[mt(len = "u8")]
102         blocks: Vec<[i16; 3]>,
103     } = 37,
104     HaveMedia {
105         #[mt(len = "u8")]
106         tokens: Vec<u32>,
107     } = 41,
108     InvAction {
109         #[mt(len = "()")]
110         action: String,
111     } = 49,
112     ChatMsg {
113         #[mt(len = "Utf16")]
114         msg: String,
115     } = 50,
116     FallDmg {
117         amount: u16,
118     } = 53,
119     SelectItem {
120         select_item: u16,
121     } = 55,
122     Respawn = 56,
123     Interact {
124         action: Interaction,
125         item_slot: u16,
126         #[mt(size = "u32")]
127         pointed: PointedThing,
128         pos: PlayerPos,
129     } = 57,
130     RemovedSounds {
131         ids: Vec<i32>,
132     } = 58,
133     NodeMetaFields {
134         pos: [i16; 3],
135         formname: String,
136         #[mt(len = "(DefCfg, (DefCfg, u32))")]
137         fields: HashMap<String, String>,
138     } = 59,
139     InvFields {
140         formname: String,
141         #[mt(len = "(DefCfg, (DefCfg, u32))")]
142         fields: HashMap<String, String>,
143     } = 60,
144     RequestMedia {
145         filenames: Vec<String>,
146     } = 64,
147     CltReady {
148         major: u8,
149         minor: u8,
150         patch: u8,
151         reserved: u8,
152         version: String,
153         formspec: u16,
154     } = 67,
155     FirstSrp {
156         salt: Vec<u8>,
157         verifier: Vec<u8>,
158         empty_passwd: bool,
159     } = 80,
160     SrpBytesA {
161         a: Vec<u8>,
162         no_sha1: bool,
163     } = 81,
164     SrpBytesM {
165         m: Vec<u8>,
166     } = 82,
167     Disco = 0xffff,
168 }
169
170 impl PktInfo for ToSrvPkt {
171     fn pkt_info(&self) -> (u8, bool) {
172         use ToSrvPkt::*;
173
174         match self {
175             Init { .. } => (1, false),
176             Init2 { .. }
177             | RequestMedia { .. }
178             | CltReady { .. }
179             | FirstSrp { .. }
180             | SrpBytesA { .. }
181             | SrpBytesM { .. } => (1, true),
182             PlayerPos { .. } => (0, false),
183             GotBlocks { .. } | HaveMedia { .. } | DeletedBlocks { .. } | RemovedSounds { .. } => {
184                 (2, true)
185             }
186             _ => (0, true),
187         }
188     }
189 }