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