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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! The per-tick movement packet (`ClientPlayerEntity.sendMovementPackets`, 1.8 and 1.16) and the stand-in mover used until `ghast-physics` is
//! integrated.
use crate::common::{Controls, EntityAction, MovePacket, Pose, Vec3};
/// Squared distance above which the position is resent (both 1.8 and 1.16).
const POSITION_EPSILON_SQ: f64 = 9.0e-4;
/// The position is resent at least every this many ticks.
const POSITION_REFRESH_TICKS: u32 = 20;
/// Chooses the tick's packets the way vanilla does: sprint change, sneak change, then exactly
/// one of `flying` / `position` / `look` / `position_look`.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct MovementSender {
last_pos: Vec3,
last_yaw: f32,
last_pitch: f32,
ticks_since_pos: u32,
server_sprint: bool,
server_sneak: bool,
/// 1.9+: `ticksSinceLastPositionPacketSent` is incremented *before* the check (a position at least
/// every 20 ticks); 1.8 increments after it (every 21st tick).
increment_first: bool,
}
/// The packets of one movement step, in send order.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MoveStep {
/// Sprint change (`entity_action`), sent first.
pub sprint: Option<EntityAction>,
/// Sneak change (`entity_action`), sent second.
pub sneak: Option<EntityAction>,
/// The movement packet.
pub packet: MovePacket,
}
impl MovementSender {
/// A fresh 1.8 client player: `lastReported*` are all zero, nothing sprinting or
/// sneaking; the position is refreshed after 20 ticks without one (every 21st tick).
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// A fresh 1.9+ client player: like [`MovementSender::new`], but `ticksSinceLastPositionPacketSent`
/// is incremented before the check, so a position goes out at least every 20 ticks
/// (Grim's BadPacketsE flags a 21-tick gap on these versions).
#[must_use]
pub fn new_1_9() -> Self {
MovementSender {
increment_first: true,
..Self::default()
}
}
/// Ticks since the position was last sent.
#[must_use]
pub fn ticks_since_pos(&self) -> u32 {
self.ticks_since_pos
}
/// One tick's packets for the pose after the physics step.
pub fn update(&mut self, pose: &Pose) -> MoveStep {
let sprint = (pose.sprinting != self.server_sprint).then(|| {
self.server_sprint = pose.sprinting;
if pose.sprinting {
EntityAction::StartSprinting
} else {
EntityAction::StopSprinting
}
});
let sneak = (pose.sneaking != self.server_sneak).then(|| {
self.server_sneak = pose.sneaking;
if pose.sneaking {
EntityAction::StartSneaking
} else {
EntityAction::StopSneaking
}
});
let dx = pose.pos.x - self.last_pos.x;
let dy = pose.pos.y - self.last_pos.y;
let dz = pose.pos.z - self.last_pos.z;
// `(double)(yaw - lastYaw) != 0`: an f32 difference.
let turned = pose.yaw - self.last_yaw != 0.0 || pose.pitch - self.last_pitch != 0.0;
if self.increment_first {
self.ticks_since_pos += 1;
}
let moved = dx * dx + dy * dy + dz * dz > POSITION_EPSILON_SQ
|| self.ticks_since_pos >= POSITION_REFRESH_TICKS;
let on_ground = pose.on_ground;
let packet = match (moved, turned) {
(true, true) => MovePacket::PositionLook {
horizontal_collision: false,
pos: pose.pos,
yaw: pose.yaw,
pitch: pose.pitch,
on_ground,
},
(true, false) => MovePacket::Position {
horizontal_collision: false,
pos: pose.pos,
on_ground,
},
(false, true) => MovePacket::Look {
horizontal_collision: false,
yaw: pose.yaw,
pitch: pose.pitch,
on_ground,
},
(false, false) => MovePacket::Flying {
horizontal_collision: false,
on_ground,
},
};
if !self.increment_first {
self.ticks_since_pos += 1;
}
if moved {
self.last_pos = pose.pos;
self.ticks_since_pos = 0;
}
if turned {
self.last_yaw = pose.yaw;
self.last_pitch = pose.pitch;
}
MoveStep {
sprint,
sneak,
packet,
}
}
}
/// The stand-in mover used until `ghast-physics` is integrated: the player never moves.
///
/// It reproduces what the vanilla client computes for a player standing still on a floor
/// right after a (non-relative) teleport: the first tick moves by velocity y = 0 and is not on
/// ground; gravity then makes the next move collide with the floor, so every later tick is on
/// ground. It assumes the server put the player on a floor; without world data it cannot
/// know. Movement keys are ignored (only the sneak key is honoured, it does not move).
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct StandStill {
ticks_since_teleport: u32,
}
impl StandStill {
/// The server teleported the player.
pub fn teleported(&mut self) {
self.ticks_since_teleport = 0;
}
/// One tick: updates `on_ground`, `sneaking` and `sprinting`; position and look are kept.
pub fn step(&mut self, pose: &mut Pose, controls: &Controls) {
pose.on_ground = self.ticks_since_teleport > 0;
self.ticks_since_teleport = self.ticks_since_teleport.saturating_add(1);
pose.motion = Vec3::default();
pose.sneaking = controls.sneak && !pose.flying;
pose.sprinting = false;
}
}
#[cfg(test)]
mod tests {
use super::*;
fn pose(x: f64, y: f64, z: f64, yaw: f32, pitch: f32) -> Pose {
Pose {
pos: Vec3::new(x, y, z),
yaw,
pitch,
on_ground: true,
..Pose::default()
}
}
#[test]
fn packet_choice() {
let mut s = MovementSender::new();
// Fresh player: last reported is the origin, so the first packet carries everything.
let p = pose(10.5, 64.0, -3.5, 90.0, 10.0);
assert!(matches!(
s.update(&p).packet,
MovePacket::PositionLook { .. }
));
// Standing still: 20 flying packets, then a position refresh (vanilla checks
// `ticksSinceLastPositionPacketSent >= 20` before incrementing it: a 21-tick period).
for _ in 0..20 {
assert!(matches!(
s.update(&p).packet,
MovePacket::Flying {
on_ground: true,
..
}
));
}
assert!(matches!(s.update(&p).packet, MovePacket::Position { .. }));
// A tiny move below the threshold is not sent; a look change is.
let p2 = pose(10.51, 64.0, -3.5, 91.0, 10.0);
assert!(matches!(s.update(&p2).packet, MovePacket::Look { .. }));
let p3 = pose(10.6, 64.0, -3.5, 91.0, 10.0);
assert!(matches!(s.update(&p3).packet, MovePacket::Position { .. }));
}
#[test]
fn position_refresh_1_9() {
let mut s = MovementSender::new_1_9();
let p = pose(10.5, 64.0, -3.5, 90.0, 10.0);
assert!(matches!(
s.update(&p).packet,
MovePacket::PositionLook { .. }
));
for _ in 0..19 {
assert!(matches!(s.update(&p).packet, MovePacket::Flying { .. }));
}
assert!(matches!(s.update(&p).packet, MovePacket::Position { .. }));
}
#[test]
fn sneak_and_sprint_order() {
let mut s = MovementSender::new();
let mut p = pose(0.0, 0.0, 0.0, 0.0, 0.0);
p.sneaking = true;
p.sprinting = true;
let st = s.update(&p);
assert_eq!(st.sprint, Some(EntityAction::StartSprinting));
assert_eq!(st.sneak, Some(EntityAction::StartSneaking));
let st = s.update(&p);
assert_eq!((st.sprint, st.sneak), (None, None));
p.sneaking = false;
assert_eq!(s.update(&p).sneak, Some(EntityAction::StopSneaking));
}
#[test]
fn stand_still_ground() {
let mut m = StandStill::default();
let mut p = Pose::default();
m.teleported();
m.step(&mut p, &Controls::default());
assert!(!p.on_ground);
m.step(&mut p, &Controls::default());
assert!(p.on_ground);
}
}