ghast-physics 0.5.4

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
//! The player as one of several passengers of a living mount, a mount the server moves while the
//! player rides it, and the state of a mount at the moment the player boards it.
//!
//! - **Seats** (`set_passengers` order): only the first passenger can steer
//!   (`getFirstPassenger`); the player behind it rides along. A camel seats two: with two
//!   passengers the front one sits half a block forward and the one behind 0.7 back, at the body
//!   anchor height of its own end of the camel (`Camel.getPassengerAttachmentPoint`). One
//!   passenger alone sits in front. A happy ghast's four seats: `mount_fly.rs`.
//! - **Moved by the server** (nobody steers it on this client): the mount's position and rotation
//!   are the entity tracker's interpolation (`InterpolationHandler`, run at the start of
//!   `LivingEntity.aiStep`; [`Mount::follow_tracker`]); while it interpolates it does not slow
//!   down; a teleport sets its motion and ground flag ([`Mount::server_teleport`]).
//! - **Hand-over**: when the player becomes the first passenger of a saddled mount it steers
//!   from the state the mount has then; when someone else is put in front, the server moves it
//!   again.
//! - **Boarding**: a camel's or a nautilus' dash cooldown keeps running on the client while
//!   nobody rides it (`Camel.tick` / `AbstractNautilus.tick`); [`Mount::dash_clock`] runs it on
//!   from the server's `DASH` entity data to the boarding, [`Mount::set_dash_state`] puts the
//!   result into a mount created at the boarding, [`Mount::mark_ticked`] marks an entity the
//!   client has ticked before (its first tick is over).

use super::super::player::PlayerState;
use super::{Mount, MountKind, camel};

/// The interpolation step's `setPos` / `setRot` with the tracker's values.
pub(super) fn follow(e: &mut PlayerState, pos: [f64; 3], yaw: f32, pitch: f32) {
    e.set_pos(pos[0], pos[1], pos[2]);
    if yaw.is_finite() {
        e.yaw = yaw;
    }
    if pitch.is_finite() {
        e.pitch = pitch.clamp(-90.0, 90.0);
    }
}

impl Mount {
    /// The number of passengers the server's `set_passengers` lists (with the player's index from
    /// [`Mount::set_seat`]): a camel seats its second passenger behind the first. Also for a
    /// mount the player is not on: one with passengers is not pushed off the player
    /// ([`Mount::tick_alone`]).
    pub fn set_passenger_count(&mut self, count: usize) {
        self.s.passengers = count.min(usize::from(u8::MAX)) as u8;
    }

    /// The number of passengers (0 before any `set_passengers`).
    #[must_use]
    pub fn passenger_count(&self) -> usize {
        usize::from(self.s.passengers)
    }

    /// Where the entity tracker has a mount the player rides but does not steer after this tick's
    /// interpolation step (until 26.2 `InterpolationHandler.interpolate` in `aiStep` after the
    /// entity's base tick, since 26.3 in `commonTick` before its tick), whether an interpolation
    /// was active before the step (`was`) and is still after it (`still`): the mount does not
    /// slow down while it interpolates. Call it before [`tick_mount`](super::tick_mount); it is
    /// applied at that point of the mount's tick. A mount the player steers interpolates too
    /// (vanilla runs the step before the rider's travel, after a teleport or when the player
    /// takes over or boards a mount that still interpolates): pass the tracker's step only while
    /// an interpolation was active (`was`), with the tracker kept on the client's own moves
    /// (`Tracker::client_moved` in ghast-entity), and its target with
    /// [`Mount::set_interpolation_target`].
    pub fn follow_tracker(&mut self, pos: [f64; 3], yaw: f32, pitch: f32, was: bool, still: bool) {
        self.s.follow = Some((pos, yaw, pitch));
        self.s.follow_interp = (was, still);
    }

    /// The interpolation target (position, yaw, pitch) of a mount the player steers while it
    /// still interpolates after this tick's step: this tick's `vehicle_move` carries it
    /// (`ServerboundMoveVehiclePacket.fromEntity`), not the mount's own pose.
    pub fn set_interpolation_target(&mut self, target: ([f64; 3], f32, f32)) {
        self.s.send_target = Some(target);
    }

    /// A teleport-entity packet for the mount (`setValuesFromPositionPacket`): its motion becomes
    /// the packet's (absolute) delta and its ground flag the packet's. The position goes through
    /// the tracker's interpolation ([`Mount::follow_tracker`]).
    pub fn server_teleport(&mut self, delta: [f64; 3], on_ground: bool) {
        self.e.vel = delta;
        self.e.on_ground = on_ground;
        self.s.ground = Some(on_ground);
    }

    /// The state of the client copy of a mount the client watched before the player boarded it:
    /// its own motion (`getDeltaMovement`: the server's last motion or teleport delta, slowed on
    /// the ticks without an interpolation, cut under 0.003, pushed by the player; ghast-entity's
    /// `Entities::client_motion`) and the last position packet's ground flag. The first ridden
    /// tick starts from them.
    pub fn set_watched_state(&mut self, vel: [f64; 3], on_ground: bool) {
        self.e.vel = vel;
        self.e.on_ground = on_ground;
        self.s.ground = Some(on_ground);
    }

    /// A `set_entity_motion` packet for the mount the player rides (`lerpMotion`: its motion,
    /// also while the player steers it).
    pub fn server_motion(&mut self, vel: [f64; 3]) {
        self.e.vel = vel;
    }

    /// A relative move or position sync packet for a mount the player does not steer: its
    /// ground flag (`setOnGround`, which also finds the supporting block).
    pub fn server_on_ground(&mut self, on_ground: bool) {
        self.e.on_ground = on_ground;
        self.s.ground = Some(on_ground);
    }

    /// A teleport-entity packet too far to interpolate (more than 64 blocks from where the mount
    /// is, `setValuesFromPositionPacket`): the mount is put there at once, before its next tick
    /// (with [`Mount::server_teleport`] for the motion and the ground flag).
    pub fn server_snap(&mut self, pos: [f64; 3], yaw: f32, pitch: f32) {
        follow(&mut self.e, pos, yaw, pitch);
        self.s.follow = None;
    }

    /// `positionRider` of the player outside the tick (after [`Mount::server_snap`], the
    /// teleport handler places the passenger at once).
    pub fn position_passenger(&self, p: &mut PlayerState) {
        super::position_rider(self, p);
    }

    /// The client has ticked this entity before (a mount created at the boarding of an entity
    /// the client has been tracking): its first tick is over (`Entity.firstTick`).
    pub fn mark_ticked(&mut self) {
        self.e.first_tick = false;
    }

    /// The dash cooldown and `DASH` flag of a camel or nautilus (from [`Mount::dash_clock`]);
    /// ignored for other kinds.
    pub fn set_dash_state(&mut self, cooldown: i32, dashing: bool) {
        if matches!(self.kind, MountKind::Camel | MountKind::CamelHusk) {
            self.cm.dash_cooldown = cooldown;
            self.cm.dashing = dashing;
        } else if super::swim::is_nautilus(self.kind) {
            self.sw.set_state(cooldown, dashing);
        }
    }

    /// A dash cooldown and `DASH` flag `(cooldown, dashing)` of an unridden camel or nautilus of
    /// `kind`, run on over `ticks` client ticks of the entity (`Camel.tick`: the flag clears
    /// below 50 ticks of cooldown while `grounded` (on the ground or in a liquid), clearing it
    /// with no cooldown left starts a new one; `AbstractNautilus.tick`: below 35; then the
    /// cooldown counts down). `grounded` is taken as constant over the ticks. Other kinds: the
    /// state unchanged.
    #[must_use]
    pub fn dash_clock(
        kind: MountKind,
        state: (i32, bool),
        ticks: u32,
        grounded: bool,
    ) -> (i32, bool) {
        let (mut cd, mut on) = state;
        let camel = matches!(kind, MountKind::Camel | MountKind::CamelHusk);
        if !camel && !super::swim::is_nautilus(kind) {
            return state;
        }
        for _ in 0..ticks {
            if on && cd == 0 && !(camel && !grounded) {
                // The flag clears; onSyncedDataUpdated with no cooldown running starts one.
                on = false;
                cd = if camel {
                    camel::DASH_COOLDOWN
                } else {
                    super::swim::DASH_COOLDOWN
                };
            } else if on && cd < if camel { 50 } else { 35 } && (!camel || grounded) {
                on = false;
            }
            if cd > 0 {
                cd -= 1;
            }
            if cd == 0 && (!on || camel && !grounded) {
                // Nothing changes any more.
                break;
            }
        }
        (cd, on)
    }

    /// The server's `DASH` entity data for an unridden camel or nautilus (`onSyncedDataUpdated`):
    /// `(cooldown, dashing)` after it, from the state before; `ticked` is false while the entity
    /// has not had its first client tick (the update then starts no cooldown).
    #[must_use]
    pub fn dash_synced(
        kind: MountKind,
        state: (i32, bool),
        dashing: bool,
        ticked: bool,
    ) -> (i32, bool) {
        let cd = match kind {
            MountKind::Camel | MountKind::CamelHusk if ticked && state.0 == 0 => {
                camel::DASH_COOLDOWN
            }
            MountKind::Nautilus | MountKind::ZombieNautilus if ticked && state.0 == 0 => {
                super::swim::DASH_COOLDOWN
            }
            _ => state.0,
        };
        (cd, dashing)
    }
}