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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//! 1.8.9: vehicles and mounts the player rides (boats, minecarts, living mounts): the watched
//! mounts, boarding, their packets and the ride's tick.
use super::*;
impl Sim18 {
/// The server's packets about the horses and pigs the client watches (not the one we ride):
/// a mob spawn of a mount type starts watching it (spawn position, angle bytes, entity data
/// index 16); moves, looks, head yaw and entity data follow; a removal ends it.
#[cold]
#[inline(never)]
pub(super) fn watched_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
use ghast_protocol::defs::p47::types::EntityMetadataValue as V;
use ghast_protocol::v1_8::play::clientbound as cb;
let flags =
|m: &mut ghast_physics::v1_8::Mount,
list: &[ghast_protocol::defs::p47::types::EntityMetadataEntry]| {
for e in list {
match (e.key, &e.value) {
(16, V::Int(f)) => m.set_flags(*f),
(16, V::Byte(b)) => m.set_flags(i32::from(*b)),
_ => {}
}
}
};
if raw.id == cb::id::ENTITY_DESTROY {
let p: cb::EntityDestroy = raw.decode_as()?;
self.watched.retain(|w| !p.entity_ids.contains(&w.0));
return Ok(());
}
if raw.id == cb::id::SPAWN_ENTITY_LIVING {
let p: cb::SpawnEntityLiving = raw.decode_as()?;
self.watched.retain(|w| w.0 != p.entity_id);
if let Some(kind) = ghast_physics::v1_8::MountKind::of_mob_type(p.r#type) {
let mut m = ghast_physics::v1_8::Mount::spawned(
kind,
[p.x, p.y, p.z],
p.yaw,
p.pitch,
p.head_pitch,
);
flags(&mut m, &p.metadata);
self.watched.push((p.entity_id, m));
}
return Ok(());
}
let id = ghast_protocol::Reader::new(&raw.body).read_varint()?;
if self.ride.as_ref().is_some_and(|r| r.id == id) {
return Ok(());
}
let Some((_, m)) = self.watched.iter_mut().find(|w| w.0 == id) else {
return Ok(());
};
match raw.id {
cb::id::ENTITY_TELEPORT => {
let p: cb::EntityTeleport = raw.decode_as()?;
m.server_teleport([p.x, p.y, p.z], p.yaw, p.pitch, p.on_ground);
}
cb::id::REL_ENTITY_MOVE => {
let p: cb::RelEntityMove = raw.decode_as()?;
m.server_update([p.d_x, p.d_y, p.d_z], None, p.on_ground);
}
cb::id::ENTITY_MOVE_LOOK => {
let p: cb::EntityMoveLook = raw.decode_as()?;
m.server_update([p.d_x, p.d_y, p.d_z], Some((p.yaw, p.pitch)), p.on_ground);
}
cb::id::ENTITY_LOOK => {
let p: cb::EntityLook = raw.decode_as()?;
m.server_update([0; 3], Some((p.yaw, p.pitch)), p.on_ground);
}
cb::id::ENTITY => m.server_update([0; 3], None, false),
cb::id::ENTITY_HEAD_ROTATION => {
let p: cb::EntityHeadRotation = raw.decode_as()?;
m.server_head_yaw(p.head_yaw);
}
cb::id::ENTITY_METADATA => {
let p: cb::EntityMetadata = raw.decode_as()?;
flags(m, &p.metadata);
}
_ => {}
}
Ok(())
}
/// The boat packets (with entity tracking): `attach_entity` naming us (boarding a tracked
/// boat, or leaving it with vehicle -1), and while riding the server's teleport, relative
/// moves, looks and velocity of that boat and its destruction (vanilla's
/// `World.removeEntity` takes the rider off).
#[cold]
#[inline(never)]
pub(super) fn vehicle_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
use ghast_protocol::v1_8::play::clientbound as cb;
let Some(t) = &self.entities else {
return Ok(());
};
if raw.id == cb::id::ATTACH_ENTITY {
let p: cb::AttachEntity = raw.decode_as()?;
if p.leash {
return Ok(());
}
if t.self_id() != Some(p.entity_id) {
// Another entity boards a boat: vanilla sets its empty flag again.
self.boarded.retain(|&b| b != p.vehicle_id);
return Ok(());
}
if p.vehicle_id == -1 {
if let Some(r) = self.ride.take() {
log::debug!("left boat {}", r.id);
// A living mount: the client watches it again from where it is.
if let Seat18::Mount(m) = r.seat {
self.watched.retain(|w| w.0 != r.id);
self.watched.push((r.id, m));
}
}
return Ok(());
}
// onEntityAttach: a boat the client knows starts its physics; a minecart follows
// the server. The client's interpolation of either at rest ends at the server's
// last position.
let shadow = self
.watched
.iter()
.position(|w| w.0 == p.vehicle_id)
.map(|k| self.watched.swap_remove(k).1);
let seat = t.get(p.vehicle_id).and_then(|e| {
let fixed = e.position().map(|v| (v * 32.0).round() as i32);
let yaw = (e.yaw() * 256.0 / 360.0).round() as i32 as i8;
let pitch = (e.pitch() * 256.0 / 360.0).round() as i32 as i8;
match e.entity_type() {
ghast_entity::EntityType::LegacyObject(1) => Some(Seat18::Boat(
ghast_physics::v1_8::Boat::spawned(fixed, yaw, pitch),
)),
ghast_entity::EntityType::LegacyObject(10) => Some(Seat18::Cart(
ghast_physics::v1_8::Minecart::spawned(fixed, yaw, pitch),
)),
ghast_entity::EntityType::LegacyMob(id) => {
ghast_physics::v1_8::MountKind::of_mob_type(id).map(|k| match shadow {
// What the client held while it watched the mob: its
// interpolation, body clock, rearing animation and entity data.
Some(m) if m.kind == k => Seat18::Mount(m),
_ => mount_of(e, k),
})
}
_ => None,
}
});
if matches!(seat, Some(Seat18::Boat(_))) && !self.boarded.contains(&p.vehicle_id) {
// Vanilla clears the boat's empty flag ([`Sim18::bounds_movers`]).
self.boarded.push(p.vehicle_id);
}
match seat {
Some(seat) => {
log::debug!("riding {}", p.vehicle_id);
self.ride = Some(Box::new(Ride18 {
id: p.vehicle_id,
seat,
}));
}
None => {
self.ride = None;
log::debug!(
"riding {} (not simulated: not a boat, minecart, horse or pig)",
p.vehicle_id
);
}
}
return Ok(());
}
let Some(r) = self.ride.as_deref_mut() else {
return Ok(());
};
if let Seat18::Mount(m) = &mut r.seat {
let id = r.id;
match raw.id {
cb::id::ENTITY_TELEPORT => {
let p: cb::EntityTeleport = raw.decode_as()?;
if p.entity_id == id {
m.server_teleport([p.x, p.y, p.z], p.yaw, p.pitch, p.on_ground);
}
}
cb::id::REL_ENTITY_MOVE => {
let p: cb::RelEntityMove = raw.decode_as()?;
if p.entity_id == id {
m.server_update([p.d_x, p.d_y, p.d_z], None, p.on_ground);
}
}
cb::id::ENTITY_MOVE_LOOK => {
let p: cb::EntityMoveLook = raw.decode_as()?;
if p.entity_id == id {
m.server_update([p.d_x, p.d_y, p.d_z], Some((p.yaw, p.pitch)), p.on_ground);
}
}
cb::id::ENTITY_LOOK => {
let p: cb::EntityLook = raw.decode_as()?;
if p.entity_id == id {
m.server_update([0; 3], Some((p.yaw, p.pitch)), p.on_ground);
}
}
cb::id::ENTITY => {
// The bare entity packet: no move, its ground flag is false.
let p: cb::Entity = raw.decode_as()?;
if p.entity_id == id {
m.server_update([0; 3], None, false);
}
}
cb::id::ENTITY_HEAD_ROTATION => {
let p: cb::EntityHeadRotation = raw.decode_as()?;
if p.entity_id == id {
m.server_head_yaw(p.head_yaw);
}
}
cb::id::ENTITY_DESTROY => {
let p: cb::EntityDestroy = raw.decode_as()?;
if p.entity_ids.contains(&id) {
log::debug!("mount {id} destroyed");
self.ride = None;
}
}
// A living mob's client motion does not move it.
_ => {}
}
return Ok(());
}
if let Seat18::Cart(c) = &mut r.seat {
let id = r.id;
match raw.id {
cb::id::ENTITY_TELEPORT => {
let p: cb::EntityTeleport = raw.decode_as()?;
if p.entity_id == id {
c.server_teleport([p.x, p.y, p.z], p.yaw, p.pitch);
}
}
cb::id::REL_ENTITY_MOVE => {
let p: cb::RelEntityMove = raw.decode_as()?;
if p.entity_id == id {
c.server_update([p.d_x, p.d_y, p.d_z], None);
}
}
cb::id::ENTITY_MOVE_LOOK => {
let p: cb::EntityMoveLook = raw.decode_as()?;
if p.entity_id == id {
c.server_update([p.d_x, p.d_y, p.d_z], Some((p.yaw, p.pitch)));
}
}
cb::id::ENTITY_LOOK => {
let p: cb::EntityLook = raw.decode_as()?;
if p.entity_id == id {
c.server_update([0; 3], Some((p.yaw, p.pitch)));
}
}
cb::id::ENTITY => {
let p: cb::Entity = raw.decode_as()?;
if p.entity_id == id {
c.server_update([0; 3], None);
}
}
cb::id::ENTITY_DESTROY => {
let p: cb::EntityDestroy = raw.decode_as()?;
if p.entity_ids.contains(&id) {
log::debug!("minecart {id} destroyed");
self.ride = None;
}
}
// A minecart's client motion does not move it.
_ => {}
}
return Ok(());
}
let Seat18::Boat(b) = &mut r.seat else {
return Ok(());
};
match raw.id {
cb::id::ENTITY_TELEPORT => {
let p: cb::EntityTeleport = raw.decode_as()?;
if p.entity_id == r.id {
b.server_teleport([p.x, p.y, p.z], p.yaw, p.pitch, p.on_ground);
}
}
cb::id::REL_ENTITY_MOVE => {
let p: cb::RelEntityMove = raw.decode_as()?;
if p.entity_id == r.id {
b.server_move([p.d_x, p.d_y, p.d_z], p.on_ground);
}
}
cb::id::ENTITY_MOVE_LOOK => {
let p: cb::EntityMoveLook = raw.decode_as()?;
if p.entity_id == r.id {
b.server_move([p.d_x, p.d_y, p.d_z], p.on_ground);
}
}
cb::id::ENTITY_LOOK => {
let p: cb::EntityLook = raw.decode_as()?;
if p.entity_id == r.id {
b.server_move([0; 3], p.on_ground);
}
}
cb::id::ENTITY => {
// The bare entity packet: no move, its ground flag is false.
let p: cb::Entity = raw.decode_as()?;
if p.entity_id == r.id {
b.server_move([0; 3], false);
}
}
cb::id::ENTITY_VELOCITY => {
let p: cb::EntityVelocity = raw.decode_as()?;
if p.entity_id == r.id {
let v = p.velocity;
let c = |x: i16| f64::from(x) / 8000.0;
b.server_velocity([c(v.x), c(v.y), c(v.z)]);
}
}
cb::id::ENTITY_DESTROY => {
let p: cb::EntityDestroy = raw.decode_as()?;
if p.entity_ids.contains(&r.id) {
log::debug!("boat {} destroyed", r.id);
self.ride = None;
}
}
_ => {}
}
Ok(())
}
/// A tick in the boat the 1.8 client simulates (`ghast_physics::v1_8::tick_boat`).
#[cold]
#[inline(never)]
pub(super) fn step_ride(&mut self, input: &ghast_physics::TickInput) {
// The ride's ticks follow neither the ray nor the queries that set shared bounds.
self.pistons.forget_shared_bounds();
// The ride's ticks do not trace the crosshair ray.
if self.head_watch && self.heads_near() {
self.ray_missed = true;
}
// Other entities' queries and rays meet the loaded heads too.
if self.head_watch && self.bounds_movers() {
self.pistons.forget_head_bounds(0x3f);
}
let Some(r) = self.ride.as_deref_mut() else {
return;
};
let id = r.id;
let boat = match &mut r.seat {
Seat18::Boat(b) => b,
Seat18::Mount(m) => {
// The living mount the server moves (`ghast_physics::v1_8::tick_mount`).
ghast_physics::v1_8::tick_mount(
&mut self.player,
m,
&mut self.jump_bar,
input,
&self.world,
&ghast_physics::v1_8::Options::default(),
&mut self.packets,
);
return;
}
Seat18::Cart(c) => {
self.jump_bar.idle();
// The minecart the server moves (`ghast_physics::v1_8::tick_minecart`).
ghast_physics::v1_8::tick_minecart(
&mut self.player,
c,
input,
&self.world,
&ghast_physics::v1_8::Options::default(),
&mut self.packets,
);
return;
}
};
self.jump_bar.idle();
#[cfg(test)]
let still = ghast_physics::v1_8::tick_boat_with(
&mut self.player,
boat,
input,
&self.world,
&ghast_physics::v1_8::Options::default(),
&TableTrig(&self.trig),
&mut self.packets,
);
#[cfg(not(test))]
let still = ghast_physics::v1_8::tick_boat(
&mut self.player,
boat,
input,
&self.world,
&ghast_physics::v1_8::Options::default(),
&mut self.packets,
);
for &[x, y, z] in boat.broken_blocks() {
self.world
.set_block_state(x, y, z, ghast_data::v1_8::BlockState::AIR);
}
if !still {
log::debug!("boat {id} fell out of the world");
self.ride = None;
}
}
}