ghast-physics 0.5.6

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.16.5 movement state beyond [`PlayerState`](super::PlayerState), kept apart so the 0.4.0
//! types stay as they were: the level's dimension type, riptide and gliding (elytra) with
//! firework rockets.
//!
//! [`tick_ext`](super::tick_ext) runs the same tick as [`tick_in`](super::tick_in) with this
//! state; with a default [`PlayerExt`] the two are identical.
//!
//! # The dimension type, as the 1.16.5 client does it
//!
//! The client world is built with the dimension type of the join packet (`dimension`, the whole
//! dimension type compound), and a respawn packet replaces the world (and its dimension type)
//! only when the world name changes. Of its fields only `ultrawarm` changes the player's
//! movement: `Entity.updateWaterState` pushes with lava's flow at 0.007 instead of
//! 0.0023333333333333335 (the nether). Set [`PlayerExt::ultrawarm`] from it.
//!
//! # Riptide, as the 1.16.5 client does it
//!
//! * **Use.** `TridentItem.use` refuses a riptide trident unless the player is in water or rain
//!   ([`in_water_or_rain`](super::in_water_or_rain)) or the next point of damage would break
//!   it; a held use key retries every 4 ticks (`MinecraftClient.itemUseCooldown`).
//! * **Launch.** Releasing the use key after at least 10 player ticks of use, still in water or
//!   rain, calls [`riptide`](super::riptide) on the client: a push along the look, the 20-tick
//!   counter ([`PlayerExt::riptide_ticks`]) and, on the ground, a 1.2-block move up. No riding or
//!   durability check at the release.
//! * **Spin.** After each tick's travel the counter goes down; a living entity in the swept box
//!   ends it and reverses the motion (×−0.2); with no entity in that box a horizontal collision
//!   ends it (`LivingEntity.tickRiptide`; pass the other entities as
//!   [`Surroundings::others`](super::Surroundings)). The spin pose follows the server's living
//!   entity flag 4 ([`PlayerExt::apply_living_flags`]), not the counter.
/// Movement state beyond [`PlayerState`](super::PlayerState). Default: the overworld, no spin.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PlayerExt {
    /// The level's dimension type is `ultrawarm` (the nether): lava pushes harder.
    pub ultrawarm: bool,
    /// Living entity flag 4, `isUsingRiptide()`: the spin pose (vanilla `SPIN_ATTACK`, held as [`Pose::FallFlying`](super::Pose), the same box).
    /// Set and cleared only by the server's entity data ([`PlayerExt::apply_living_flags`]).
    pub spin_attack: bool,
    /// The client's riptide spin counter (`riptideTicks`): 20 after a launch
    /// ([`riptide`](super::riptide)), counted down each tick, ended by a hit or a horizontal
    /// collision.
    pub riptide_ticks: i32,
    /// Shared entity flag 7, `isFallFlying()`: the player glides. Set by the client when it starts
    /// gliding; cleared by the server's entity data ([`PlayerExt::apply_shared_flags`]) or by
    /// creative flight.
    pub fall_flying: bool,
    /// A usable elytra is in the chest slot (`Items.ELYTRA` with `ElytraItem.isUsable`: damage
    /// below 431). Kept by the caller from the player's equipment; read only when a glide starts.
    pub elytra: bool,
    /// The [`Pose::FallFlying`](super::Pose::FallFlying) the player is in is the glide's
    /// (vanilla `FALL_FLYING`), not the riptide spin's (`SPIN_ATTACK`, the same box). Kept by the
    /// tick.
    pub glide_pose: bool,
    /// Asleep in a bed (`isSleeping`: the server's sleeping position in our entity data). The
    /// pose is [`Pose::Sleeping`](super::Pose::Sleeping) (`updatePose`) and `pushOutOfBlocks`
    /// probes the corners of its 0.2 wide box. Set and cleared by the caller.
    pub sleeping: bool,
    /// The rider's jump bar counter (`ClientPlayerEntity.field_3938`): ticks the jump key has been
    /// held on a jumping mount, or −10 after a release counting back to 0.
    pub mount_jump_ticks: i32,
    /// The jump bar (`field_3922`) as float bits (kept as bits so the state stays `Eq`).
    mount_jump_bar: u32,
    /// A release of this tick not yet handed to the mount (its strength ×100).
    mount_jump: Option<i32>,
}

impl PlayerExt {
    /// The default state: the overworld, nothing else.
    #[must_use]
    pub fn new() -> PlayerExt {
        PlayerExt::default()
    }

    /// The server's entity data for the player's living entity flags byte (index 7): bit 4 is
    /// the spin. Bits 1 and 2 (the item use the server sees) are the caller's: the client starts
    /// using the held item when bit 1 arrives set and it is not using one, and stops when it
    /// arrives clear while it is (`ClientPlayerEntity.onTrackedDataSet`).
    pub fn apply_living_flags(&mut self, flags: u8) {
        self.spin_attack = flags & 4 != 0;
    }

    /// The server's entity data for the player's own shared flags byte (index 0): sets sprinting
    /// (bit value 8; the flag only, the sprint speed modifier is not touched, as in vanilla),
    /// swimming (16) and fall flying (128). Other bits are not read by movement.
    pub fn apply_shared_flags(&mut self, p: &mut super::PlayerState, flags: u8) {
        p.sprinting = flags & 0x08 != 0;
        p.swimming = flags & 0x10 != 0;
        self.fall_flying = flags & 0x80 != 0;
    }

    /// The bits of the player's shared flags byte that movement reads: 8 sprinting, 16 swimming,
    /// 128 fall flying.
    #[must_use]
    pub fn shared_flags(&self, p: &super::PlayerState) -> u8 {
        (u8::from(p.sprinting) << 3)
            | (u8::from(p.swimming) << 4)
            | (u8::from(self.fall_flying) << 7)
    }

    /// The rider's jump bar (`getMountJumpStrength`, 0..1).
    #[must_use]
    pub fn mount_jump_bar(&self) -> f32 {
        f32::from_bits(self.mount_jump_bar)
    }

    /// The jump-bar part of `ClientPlayerEntity.tickMovement` (`prev`: the jump key of the previous
    /// tick, `now`: of this tick). Returns the strength ×100 of a release (the `entity_action`
    /// start riding jump; the mount gets it through [`PlayerExt::take_mount_jump`]).
    pub(super) fn mount_jump_bar_tick(
        &mut self,
        jumping_mount: bool,
        prev: bool,
        now: bool,
    ) -> Option<i32> {
        if !jumping_mount {
            self.mount_jump_bar = 0;
            return None;
        }
        let mut bar = f32::from_bits(self.mount_jump_bar);
        let mut released = None;
        if self.mount_jump_ticks < 0 {
            self.mount_jump_ticks += 1;
            if self.mount_jump_ticks == 0 {
                bar = 0.0;
            }
        }
        if prev && !now {
            self.mount_jump_ticks = -10;
            let s = (bar * 100.0).floor() as i32;
            self.mount_jump = Some(s);
            released = Some(s);
        } else if !prev && now {
            self.mount_jump_ticks = 0;
            bar = 0.0;
        } else if prev {
            self.mount_jump_ticks += 1;
            bar = if self.mount_jump_ticks < 10 {
                self.mount_jump_ticks as f32 * 0.1
            } else {
                0.8 + 2.0 / (self.mount_jump_ticks - 9) as f32 * 0.1
            };
        }
        self.mount_jump_bar = bar.to_bits();
        released
    }

    /// The jump bar release of the last tick, once (`setJumpStrength` of the mount).
    pub fn take_mount_jump(&mut self) -> Option<i32> {
        self.mount_jump.take()
    }

    /// `true` when the state changes nothing: [`tick_ext`](super::tick_ext) is then the same
    /// tick as [`tick_in`](super::tick_in).
    #[must_use]
    pub fn is_inert(&self) -> bool {
        *self == PlayerExt::default()
    }
}