ghast-physics 0.5.3

Vanilla-exact, pure and deterministic player movement for the ghast Minecraft Java Edition client library (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3), verified bit for bit against the real game
Documentation
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! The rule-set seam of the modern core: what differs between modern versions (data tables and
//! constants). Everything is an associated item, so a version is monomorphised into the core with
//! no run-time branch.

/// The data types every modern version shares (`ghast_data::v26_1` and `ghast_data::v26_2`
/// re-export the same types; whichever is compiled in).
#[cfg(feature = "v26_1")]
pub(crate) use ghast_data::v26_1 as shared;
#[cfg(all(feature = "v26_2", not(feature = "v26_1")))]
pub(crate) use ghast_data::v26_2 as shared;
#[cfg(all(feature = "v26_3", not(any(feature = "v26_1", feature = "v26_2"))))]
pub(crate) use ghast_data::v26_3 as shared;

use shared::{StatePhys, VoxelShapeRef};

/// One modern version's rules.
///
/// The packed per-state record ([`StatePhys`]) and the voxel shapes are the types every modern
/// version shares. The per-block movement facts are read through [`Rules::friction`],
/// [`Rules::speed_factor`], [`Rules::jump_factor`], [`Rules::block_flags`], [`Rules::offset`] and
/// [`Rules::block_name`]; with the `v26_1` feature they default to the fields of
/// [`Rules::block`] (the 26.1 block record).
pub trait Rules: 'static {
    /// The version's block state (global palette id).
    type State: Copy + PartialEq;

    /// Protocol number (informational).
    const PROTOCOL: i32;

    /// Air (what the client reads outside the build height and in unloaded columns).
    const AIR: Self::State;

    /// Packed per-state movement record.
    fn phys(s: Self::State) -> StatePhys;

    /// The state's block as a 26.1 block record (the layout of `ghast_data::v26_1`). The core
    /// reads the per-block facts through the methods below; a later rule set's record is the one
    /// of the 26.1 block of the same name (see that rule set).
    #[cfg(feature = "v26_1")]
    fn block(s: Self::State) -> &'static ghast_data::v26_1::BlockData;

    /// `Block.getFriction`.
    #[cfg(feature = "v26_1")]
    #[inline]
    fn friction(s: Self::State) -> f32 {
        Self::block(s).friction
    }
    /// `Block.getFriction`.
    #[cfg(not(feature = "v26_1"))]
    fn friction(s: Self::State) -> f32;

    /// `Block.getSpeedFactor`.
    #[cfg(feature = "v26_1")]
    #[inline]
    fn speed_factor(s: Self::State) -> f32 {
        Self::block(s).speed_factor
    }
    /// `Block.getSpeedFactor`.
    #[cfg(not(feature = "v26_1"))]
    fn speed_factor(s: Self::State) -> f32;

    /// `Block.getJumpFactor`.
    #[cfg(feature = "v26_1")]
    #[inline]
    fn jump_factor(s: Self::State) -> f32 {
        Self::block(s).jump_factor
    }
    /// `Block.getJumpFactor`.
    #[cfg(not(feature = "v26_1"))]
    fn jump_factor(s: Self::State) -> f32;

    /// The block's flag bits (`block_flags`: tags and block classes).
    #[cfg(feature = "v26_1")]
    #[inline]
    fn block_flags(s: Self::State) -> u32 {
        Self::block(s).flags
    }
    /// The block's flag bits (`block_flags`: tags and block classes).
    #[cfg(not(feature = "v26_1"))]
    fn block_flags(s: Self::State) -> u32;

    /// The block's model offset (`(max horizontal, max vertical, xyz)`), if it has one.
    #[cfg(feature = "v26_1")]
    #[inline]
    fn offset(s: Self::State) -> Option<(f32, f32, bool)> {
        Self::block(s).offset
    }
    /// The block's model offset (`(max horizontal, max vertical, xyz)`), if it has one.
    #[cfg(not(feature = "v26_1"))]
    fn offset(s: Self::State) -> Option<(f32, f32, bool)>;

    /// The block's registry name without namespace.
    #[cfg(feature = "v26_1")]
    #[inline]
    fn block_name(s: Self::State) -> &'static str {
        Self::block(s).name
    }
    /// The block's registry name without namespace.
    #[cfg(not(feature = "v26_1"))]
    fn block_name(s: Self::State) -> &'static str;

    /// A collision voxel shape by index.
    fn voxel(i: u16) -> &'static VoxelShapeRef;

    /// Index of `Shapes.block()`.
    const FULL_CUBE_VOXEL: u16;
    /// Index of scaffolding's bottom shape.
    const SCAFFOLDING_BOTTOM_VOXEL: u16;
    /// Index of powder snow's falling shape.
    const POWDER_SNOW_FALLING_VOXEL: u16;

    /// `LocalPlayer.sendPosition`: a position is sent when the squared move exceeds this.
    const POSITION_THRESHOLD_SQ: f64;
    /// Ticks after which a position is sent anyway (`POSITION_REMINDER_INTERVAL`).
    const POSITION_REMINDER: i32;
    /// Players zero horizontal velocity when its squared length is below this.
    const HORIZONTAL_CUTOFF_SQ: f64;

    /// 26.2's collision response (`Entity.restituteMovementAfterCollisions`) instead of 26.1's
    /// (`setDeltaMovement` with zeroed axes + `Block.updateEntityMovementAfterFallOn`): every
    /// colliding axis is reflected with a restitution (the `bounciness` attribute, on landing at
    /// least the block's bounce restitution), with gravity and drag compensated for the part of
    /// the tick after the impact; with no restitution the axis becomes a signed zero
    /// (`-v * 0`). The movement attributes `friction_modifier`, `air_drag_modifier` and
    /// `bounciness` ([`MovementAttributes`](super::MovementAttributes)) exist from 26.2 on.
    const RESTITUTION: bool = false;

    /// `Block.getBounceRestitution` (read with [`Rules::RESTITUTION`]): slime blocks 1, beds
    /// 0.75, everything else 0.
    #[inline]
    fn bounce_restitution(s: Self::State) -> f32 {
        if Self::phys(s).kind() == shared::BlockKind::SlimeBlock as u8 {
            1.0
        } else if Self::block_flags(s) & shared::block_flags::BED != 0 {
            0.75
        } else {
            0.0
        }
    }

    /// 26.3's teleport answer (`ClientPacketListener.handleMovePlayer`): one accept-teleportation
    /// packet that carries the position and rotation the player took, instead of the bare
    /// accept-teleportation followed by a position-rotation packet (26.1, 26.2). Read by the
    /// client, not by the tick: [`PlayerState::apply_teleport`](super::PlayerState::apply_teleport)
    /// returns the pose either way (the packet to send after the confirm, or the confirm's fields).
    const TELEPORT_CONFIRM_POSE: bool = false;

    /// 26.3's `LevelReader.containsAnyLiquid` (a `findBlocksIn(box)` scan): the cells
    /// `floor(min)..=floor(max)` on each axis instead of `floor(min)..ceil(max)`, so a box whose
    /// maximum lies exactly on a block boundary also sees the block beyond it.
    const LIQUID_SCAN_CLOSED: bool = false;

    /// 26.3's landing bounce test in [`Rules::RESTITUTION`]: a landing bounces only when the
    /// downward speed exceeds the effective gravity (`-y > g`; 26.2: `-y >= g`).
    const BOUNCE_ABOVE_GRAVITY: bool = false;

    /// 26.3's eyes-in-fluid test (`EntityFluidInteraction` with `FluidState.getHeightForCamera`):
    /// a source fluid under a block whose bottom face is sturdy ([`Rules::sturdy_down`]) reaches
    /// the top of its cell.
    const EYE_CAMERA_HEIGHT: bool = false;

    /// `isFaceSturdy(DOWN)` of a state; read only with [`Rules::EYE_CAMERA_HEIGHT`].
    #[inline]
    fn sturdy_down(s: Self::State) -> bool {
        let _ = s;
        false
    }

    /// 26.3's `FlowingFluid.getFlow`: a neighbour without fluid lets the flow look below it
    /// unless it is in `#blocks_fluid_flow` (`#blocks_motion` and `#all_signs`); 26.2 tested
    /// `blocksMotion()`, which signs had.
    const FLOW_BLOCKED_BY_SIGNS: bool = false;

    /// 26.3's tick order: `LocalPlayer.tick` sends nothing; `Minecraft.tick` calls
    /// `LocalPlayer.sendChanges` (input, passenger rotation + vehicle move, or `sendPosition`)
    /// after the level's entities and block entities ticked. For a passenger the rotation packet
    /// carries the yaw after its vehicle turned it (`positionRider`). Also: a passenger does not
    /// run the push-out (`moveTowardsClosestSpace`).
    const SEND_AFTER_ENTITIES: bool = false;

    /// 26.3's `Player.tryToStartFallFlying`: no glide start in lava either (`isInLiquid`, 26.2:
    /// `isInWater`).
    const GLIDE_START_NOT_IN_LAVA: bool = false;

    /// 26.3's `Entity.collide`: the entity colliders are queried in the box expanded towards
    /// the movement and then upward by `maxUpStep` (26.2: towards the movement only).
    const ENTITY_QUERY_UP_STEP: bool = false;

    /// 26.3's `LivingEntity.floatInLiquidWhileRidden`: a ridden mount that floats checks its
    /// water height after a lava step too (26.2: `floatInWaterWhileRidden`, after a water step
    /// only).
    const RIDDEN_FLOAT_AFTER_LAVA: bool = false;

    /// 26.3's `AbstractBoat.tick`: a boat someone else steers stops its turn (`deltaRotation =
    /// 0`); before, it kept the turn speed it had and went on turning its passengers. Also 26.3:
    /// only a player in front steers a boat (`getControllingPassenger`).
    const UNSTEERED_BOAT_STOPS_TURNING: bool = false;

    /// 26.3's `Entity.commonTick`: the client's interpolation step of an entity runs before its
    /// tick (26.2: in `LivingEntity.aiStep` / `AbstractBoat.tick`, after the base tick), and a
    /// living entity's x 0.98 slowdown then asks whether it still interpolates after the step.
    const INTERPOLATE_IN_COMMON_TICK: bool = false;

    /// `BlockState.getInteractionShape` (the projectile clip re-faces a collision hit by it, as
    /// `BlockGetter.clipWithInteractionOverride` does). The default is the empty shape (no
    /// re-facing); every built-in row answers its version's table.
    #[inline]
    fn interaction_shape(s: Self::State) -> &'static ghast_data::SelectionShape {
        let _ = s;
        &EMPTY_SELECTION
    }

    /// The state's global palette id, to tell two states apart (an arrow compares the state it
    /// hit with the one it is in). `None` (the default) never equals anything.
    #[inline]
    fn state_id(s: Self::State) -> Option<u32> {
        let _ = s;
        None
    }

    /// 26.2's `Entity.getAirDrag()`: item entities, primed TNT and falling blocks scale their
    /// motion by the float `0.98F` (before: the double `0.98`). Read by
    /// [`drop`](mod@super::drop) only.
    const FLOAT_AIR_DRAG: bool = false;

    /// 26.3's `MoveSimulationType.SERVER_AND_CLIENT` of projectiles, item entities, experience
    /// orbs, primed TNT and falling blocks: a client also runs their collision response (before:
    /// only the side that is authoritative, the server). Read by [`projectile`](super::projectile)
    /// and [`drop`](mod@super::drop) only.
    const SIMULATED_ON_CLIENT: bool = false;

    /// 26.3's fishing bobber has a default gravity, the float `0.03F`, which it sinks by out of
    /// water and which its restitution reads (before: it added the double `0.03` and had no
    /// gravity of its own). Read by [`projectile`](super::projectile) only.
    const BOBBER_DEFAULT_GRAVITY: bool = false;
}

/// `Shapes.empty()` as a selection shape (the default [`Rules::interaction_shape`]).
static EMPTY_SELECTION: ghast_data::SelectionShape = ghast_data::SelectionShape {
    cube: true,
    sizes: [0, 0, 0],
    xs: &[0.0],
    ys: &[0.0],
    zs: &[0.0],
    cells: &[],
    empty: true,
    boxes: &[],
};

/// Minecraft 26.1 (protocol 775).
#[cfg(feature = "v26_1")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct V26_1;

#[cfg(feature = "v26_1")]
impl Rules for V26_1 {
    type State = ghast_data::v26_1::BlockState;

    const PROTOCOL: i32 = 775;
    const AIR: Self::State = ghast_data::v26_1::BlockState::AIR;

    #[inline]
    fn phys(s: Self::State) -> StatePhys {
        s.phys()
    }

    #[inline]
    fn block(s: Self::State) -> &'static ghast_data::v26_1::BlockData {
        s.block().data()
    }

    #[inline]
    fn voxel(i: u16) -> &'static VoxelShapeRef {
        &ghast_data::v26_1::voxel_shapes()[usize::from(i)]
    }

    const FULL_CUBE_VOXEL: u16 = ghast_data::v26_1::FULL_CUBE_VOXEL;
    const SCAFFOLDING_BOTTOM_VOXEL: u16 = ghast_data::v26_1::SCAFFOLDING_BOTTOM_VOXEL;
    const POWDER_SNOW_FALLING_VOXEL: u16 = ghast_data::v26_1::POWDER_SNOW_FALLING_VOXEL;

    const POSITION_THRESHOLD_SQ: f64 = 2.0e-4 * 2.0e-4;
    const POSITION_REMINDER: i32 = 20;
    const HORIZONTAL_CUTOFF_SQ: f64 = 9.0e-6;

    #[inline]
    fn interaction_shape(s: Self::State) -> &'static ghast_data::SelectionShape {
        s.interaction_shape()
    }

    #[inline]
    fn state_id(s: Self::State) -> Option<u32> {
        Some(u32::from(s.id()))
    }
}

/// Minecraft 26.2 (protocol 776): the 26.1 rules with 26.2's collision restitution
/// ([`Rules::RESTITUTION`]) over the 26.2 tables.
#[cfg(feature = "v26_2")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct V26_2;

#[cfg(feature = "v26_2")]
impl Rules for V26_2 {
    type State = ghast_data::v26_2::BlockState;

    const PROTOCOL: i32 = 776;
    const AIR: Self::State = ghast_data::v26_2::BlockState::AIR;

    #[inline]
    fn phys(s: Self::State) -> StatePhys {
        s.phys()
    }

    /// The record of the 26.1 block with the same name, stone for a block 26.1 does not have.
    /// The core does not read it: the per-block facts below come from the 26.2 tables.
    #[cfg(feature = "v26_1")]
    fn block(s: Self::State) -> &'static ghast_data::v26_1::BlockData {
        let stone = ghast_data::v26_1::BlockState::from_id(1).map(|s| s.block());
        match ghast_data::v26_1::Block::from_name(s.block().name()).or(stone) {
            Some(b) => b.data(),
            None => ghast_data::v26_1::BlockState::AIR.block().data(),
        }
    }

    #[inline]
    fn friction(s: Self::State) -> f32 {
        s.block().data().friction
    }

    #[inline]
    fn speed_factor(s: Self::State) -> f32 {
        s.block().data().speed_factor
    }

    #[inline]
    fn jump_factor(s: Self::State) -> f32 {
        s.block().data().jump_factor
    }

    #[inline]
    fn block_flags(s: Self::State) -> u32 {
        s.block().data().flags
    }

    #[inline]
    fn offset(s: Self::State) -> Option<(f32, f32, bool)> {
        s.block().data().offset
    }

    #[inline]
    fn block_name(s: Self::State) -> &'static str {
        s.block().name()
    }

    #[inline]
    fn voxel(i: u16) -> &'static VoxelShapeRef {
        &ghast_data::v26_2::voxel_shapes()[usize::from(i)]
    }

    const FULL_CUBE_VOXEL: u16 = ghast_data::v26_2::FULL_CUBE_VOXEL;
    const SCAFFOLDING_BOTTOM_VOXEL: u16 = ghast_data::v26_2::SCAFFOLDING_BOTTOM_VOXEL;
    const POWDER_SNOW_FALLING_VOXEL: u16 = ghast_data::v26_2::POWDER_SNOW_FALLING_VOXEL;

    const POSITION_THRESHOLD_SQ: f64 = 2.0e-4 * 2.0e-4;
    const POSITION_REMINDER: i32 = 20;
    const HORIZONTAL_CUTOFF_SQ: f64 = 9.0e-6;
    const RESTITUTION: bool = true;
    const FLOAT_AIR_DRAG: bool = true;

    #[inline]
    fn interaction_shape(s: Self::State) -> &'static ghast_data::SelectionShape {
        s.interaction_shape()
    }

    #[inline]
    fn state_id(s: Self::State) -> Option<u32> {
        Some(u32::from(s.id()))
    }
}

/// Minecraft 26.3 (protocol 777): the 26.2 rules over the 26.3 tables.
#[cfg(feature = "v26_3")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct V26_3;

#[cfg(feature = "v26_3")]
impl Rules for V26_3 {
    type State = ghast_data::v26_3::BlockState;

    const PROTOCOL: i32 = 777;
    const AIR: Self::State = ghast_data::v26_3::BlockState::AIR;

    #[inline]
    fn phys(s: Self::State) -> StatePhys {
        s.phys()
    }

    /// The record of the 26.1 block with the same name, stone for a block 26.1 does not have.
    /// The core does not read it: the per-block facts below come from the 26.3 tables.
    #[cfg(feature = "v26_1")]
    fn block(s: Self::State) -> &'static ghast_data::v26_1::BlockData {
        let stone = ghast_data::v26_1::BlockState::from_id(1).map(|s| s.block());
        match ghast_data::v26_1::Block::from_name(s.block().name()).or(stone) {
            Some(b) => b.data(),
            None => ghast_data::v26_1::BlockState::AIR.block().data(),
        }
    }

    #[inline]
    fn friction(s: Self::State) -> f32 {
        s.block().data().friction
    }

    #[inline]
    fn speed_factor(s: Self::State) -> f32 {
        s.block().data().speed_factor
    }

    #[inline]
    fn jump_factor(s: Self::State) -> f32 {
        s.block().data().jump_factor
    }

    #[inline]
    fn block_flags(s: Self::State) -> u32 {
        s.block().data().flags
    }

    #[inline]
    fn offset(s: Self::State) -> Option<(f32, f32, bool)> {
        s.block().data().offset
    }

    #[inline]
    fn block_name(s: Self::State) -> &'static str {
        s.block().name()
    }

    #[inline]
    fn voxel(i: u16) -> &'static VoxelShapeRef {
        &ghast_data::v26_3::voxel_shapes()[usize::from(i)]
    }

    const FULL_CUBE_VOXEL: u16 = ghast_data::v26_3::FULL_CUBE_VOXEL;
    const SCAFFOLDING_BOTTOM_VOXEL: u16 = ghast_data::v26_3::SCAFFOLDING_BOTTOM_VOXEL;
    const POWDER_SNOW_FALLING_VOXEL: u16 = ghast_data::v26_3::POWDER_SNOW_FALLING_VOXEL;

    const POSITION_THRESHOLD_SQ: f64 = 2.0e-4 * 2.0e-4;
    const POSITION_REMINDER: i32 = 20;
    const HORIZONTAL_CUTOFF_SQ: f64 = 9.0e-6;
    const RESTITUTION: bool = true;
    const TELEPORT_CONFIRM_POSE: bool = true;
    const LIQUID_SCAN_CLOSED: bool = true;
    const BOUNCE_ABOVE_GRAVITY: bool = true;
    const EYE_CAMERA_HEIGHT: bool = true;
    const FLOW_BLOCKED_BY_SIGNS: bool = true;
    const SEND_AFTER_ENTITIES: bool = true;
    const GLIDE_START_NOT_IN_LAVA: bool = true;
    const ENTITY_QUERY_UP_STEP: bool = true;
    const RIDDEN_FLOAT_AFTER_LAVA: bool = true;
    const UNSTEERED_BOAT_STOPS_TURNING: bool = true;
    const INTERPOLATE_IN_COMMON_TICK: bool = true;
    const FLOAT_AIR_DRAG: bool = true;
    const SIMULATED_ON_CLIENT: bool = true;
    const BOBBER_DEFAULT_GRAVITY: bool = true;

    #[inline]
    fn interaction_shape(s: Self::State) -> &'static ghast_data::SelectionShape {
        s.interaction_shape()
    }

    #[inline]
    fn state_id(s: Self::State) -> Option<u32> {
        Some(u32::from(s.id()))
    }

    /// 26.3: the shelf mushroom bounces like a bed (0.75); the straw bed is no `BedBlock` and
    /// does not bounce.
    #[inline]
    fn bounce_restitution(s: Self::State) -> f32 {
        if s.phys().kind() == shared::BlockKind::SlimeBlock as u8 {
            1.0
        } else if s.block().data().flags & shared::block_flags::BED != 0
            || s.block() == ghast_data::v26_3::Block::SHELF_MUSHROOM
        {
            0.75
        } else {
            0.0
        }
    }

    /// The state flag from the oracle's `side_solid_down`.
    #[inline]
    fn sturdy_down(s: Self::State) -> bool {
        s.phys().flags() & shared::state_flags::STURDY_DOWN != 0
    }
}