ghast_physics/input.rs
1//! Version-independent per-tick input and the movement packets a tick produces.
2
3/// Movement keys held during a tick (what the keyboard state would be in vanilla).
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
5pub struct Keys {
6 /// Walk forward (W).
7 pub forward: bool,
8 /// Walk backward (S).
9 pub back: bool,
10 /// Strafe left (A).
11 pub left: bool,
12 /// Strafe right (D).
13 pub right: bool,
14 /// Jump (space): jumps, swims up, flies up, double-tap toggles creative flight.
15 pub jump: bool,
16 /// Sneak (shift).
17 pub sneak: bool,
18 /// Sprint key (ctrl).
19 pub sprint: bool,
20}
21
22impl Keys {
23 /// No key held.
24 pub const NONE: Keys = Keys {
25 forward: false,
26 back: false,
27 left: false,
28 right: false,
29 jump: false,
30 sneak: false,
31 sprint: false,
32 };
33}
34
35/// Everything the player controls for one tick.
36#[derive(Clone, Copy, Debug, Default, PartialEq)]
37pub struct TickInput {
38 /// Held keys.
39 pub keys: Keys,
40 /// Yaw set before the tick (vanilla applies mouse look between ticks). For packets to pass
41 /// rotation checks it should be a whole number of mouse steps from the last value; see
42 /// [`crate::rotation`].
43 pub yaw: f32,
44 /// Pitch set before the tick, `-90..=90`.
45 pub pitch: f32,
46 /// An item is in use (eating, drinking, blocking with a sword, drawing a bow): movement input
47 /// is scaled by 0.2, sprinting cannot start and the sprint double-tap window resets. The
48 /// use / release packets are the client's; this is only the movement effect.
49 pub using_item: bool,
50}
51
52/// The player abilities as both directions of the abilities packet carry them.
53#[derive(Clone, Copy, Debug, PartialEq)]
54pub struct Abilities {
55 /// Takes no damage.
56 pub invulnerable: bool,
57 /// Currently flying.
58 pub flying: bool,
59 /// May fly (creative / spectator, or granted by the server).
60 pub allow_flying: bool,
61 /// Creative mode (instant break etc.; no effect on movement).
62 pub creative: bool,
63 /// Flying speed (default 0.05).
64 pub fly_speed: f32,
65 /// Walking speed (default 0.1; the server turns it into the movement speed attribute).
66 pub walk_speed: f32,
67}
68
69impl Default for Abilities {
70 fn default() -> Self {
71 Abilities {
72 invulnerable: false,
73 flying: false,
74 allow_flying: false,
75 creative: false,
76 fly_speed: 0.05,
77 walk_speed: 0.1,
78 }
79 }
80}
81
82/// A movement-related packet the client sends, in the order the tick produced it.
83#[derive(Clone, Copy, Debug, PartialEq)]
84#[non_exhaustive]
85pub enum Packet {
86 /// Entity action "start sneaking".
87 StartSneaking,
88 /// Entity action "stop sneaking".
89 StopSneaking,
90 /// Entity action "start sprinting".
91 StartSprinting,
92 /// Entity action "stop sprinting".
93 StopSprinting,
94 /// Abilities update (sent when the client toggles flight).
95 Abilities(Abilities),
96 /// Entity action "start riding jump": the rider let go of the jump key with a mount's jump
97 /// bar charged; `strength` is the bar ×100 as the action's jump-boost value.
98 StartRidingJump {
99 /// Jump strength ×100.
100 strength: i32,
101 },
102 /// Entity action "stop riding jump" (1.9+).
103 StopRidingJump,
104 /// Ground flag only.
105 Flying {
106 /// On ground.
107 on_ground: bool,
108 },
109 /// Position (`y` is the bounding box bottom).
110 Position {
111 /// X.
112 x: f64,
113 /// Feet Y (bounding box minimum).
114 y: f64,
115 /// Z.
116 z: f64,
117 /// On ground.
118 on_ground: bool,
119 },
120 /// Rotation.
121 Look {
122 /// Yaw.
123 yaw: f32,
124 /// Pitch.
125 pitch: f32,
126 /// On ground.
127 on_ground: bool,
128 },
129 /// A modern (26.x) movement packet: position and / or rotation (both absent = status only),
130 /// on-ground and horizontal-collision flags.
131 Move {
132 /// Position (feet), when included.
133 pos: Option<[f64; 3]>,
134 /// `(yaw, pitch)`, when included.
135 look: Option<(f32, f32)>,
136 /// On ground.
137 on_ground: bool,
138 /// Horizontal collision.
139 horizontal_collision: bool,
140 },
141 /// The modern `player_input` packet: the key state, sent when it changes.
142 PlayerInput(Keys),
143 /// The 26.1 `player_loaded` packet: the client has loaded the level (see
144 /// `modern::LoadGate`).
145 PlayerLoaded,
146 /// The modern `player_command` `START_FALL_FLYING`: the client started gliding (see
147 /// `modern::PlayerExt`).
148 StartFallFlying,
149 /// The modern `vehicle_move` packet: the position and rotation of the vehicle the player
150 /// controls (see `modern::Boat`).
151 MoveVehicle {
152 /// Position.
153 pos: [f64; 3],
154 /// Yaw.
155 yaw: f32,
156 /// Pitch.
157 pitch: f32,
158 /// The vehicle's on-ground flag.
159 on_ground: bool,
160 },
161 /// The legacy (1.9 - 1.21.1) `steer_vehicle` packet: the passenger's movement input after
162 /// its tick (sideways, forward after the ×0.98), jump and sneak keys.
163 SteerVehicle {
164 /// Sideways input.
165 sideways: f32,
166 /// Forward input.
167 forward: f32,
168 /// Jump key.
169 jump: bool,
170 /// Sneak key.
171 sneak: bool,
172 },
173 /// The modern `steer_boat` (paddle) packet: the boat's paddle states, sent every boat tick.
174 PaddleBoat {
175 /// Left paddle.
176 left: bool,
177 /// Right paddle.
178 right: bool,
179 },
180 /// Position and rotation.
181 PositionLook {
182 /// X.
183 x: f64,
184 /// Feet Y (bounding box minimum).
185 y: f64,
186 /// Z.
187 z: f64,
188 /// Yaw.
189 yaw: f32,
190 /// Pitch.
191 pitch: f32,
192 /// On ground.
193 on_ground: bool,
194 },
195}
196
197/// Capacity of [`Packets`]: a tick sends at most two abilities updates, two entity actions and
198/// one movement packet.
199const MAX_PACKETS: usize = 8;
200
201/// The packets of one tick, in send order. Fixed capacity, no allocation.
202#[derive(Clone, Copy, Debug, PartialEq)]
203pub struct Packets {
204 buf: [Packet; MAX_PACKETS],
205 len: usize,
206}
207
208impl Default for Packets {
209 fn default() -> Self {
210 Packets {
211 buf: [Packet::Flying { on_ground: false }; MAX_PACKETS],
212 len: 0,
213 }
214 }
215}
216
217impl Packets {
218 /// An empty list.
219 #[must_use]
220 pub fn new() -> Self {
221 Packets::default()
222 }
223
224 /// Removes all packets.
225 pub fn clear(&mut self) {
226 self.len = 0;
227 }
228
229 /// Appends a packet.
230 ///
231 /// # Panics
232 /// When more than the fixed capacity is pushed (a tick never does).
233 pub fn push(&mut self, p: Packet) {
234 assert!(self.len < MAX_PACKETS, "packet list overflow");
235 self.buf[self.len] = p;
236 self.len += 1;
237 }
238
239 /// The packets in send order.
240 #[must_use]
241 pub fn as_slice(&self) -> &[Packet] {
242 &self.buf[..self.len]
243 }
244
245 /// The packets in send order, mutable (a later step of the same tick amends one).
246 #[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
247 pub(crate) fn as_mut_slice(&mut self) -> &mut [Packet] {
248 &mut self.buf[..self.len]
249 }
250
251 /// Number of packets.
252 #[must_use]
253 pub fn len(&self) -> usize {
254 self.len
255 }
256
257 /// `true` when empty.
258 #[must_use]
259 pub fn is_empty(&self) -> bool {
260 self.len == 0
261 }
262}
263
264impl<'a> IntoIterator for &'a Packets {
265 type Item = &'a Packet;
266 type IntoIter = std::slice::Iter<'a, Packet>;
267
268 fn into_iter(self) -> Self::IntoIter {
269 self.as_slice().iter()
270 }
271}