ghast-physics 0.5.5

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
//! What an experience orb's tick reads of the players: every version's orb follows the nearest
//! player within 8 blocks (`followNearbyPlayer` / the legacy target search), on the server and on
//! a client alike.
//!
//! The caller hands each orb tick the players of the level in the order the level lists them
//! (`World.playerEntities` / `ClientLevel.players()`: the local player first, then the others
//! in the order they were added), each as the orb sees it at that moment of the entity ticks:
//! a player that ticks before the orb is seen where its own tick left it.
//!
//! The orb keeps the player it follows as an object reference: once that player is gone from
//! the level (removed, out of range) the orb keeps following that object (where its last tick
//! left it) until it looks for a target again. [`OrbPlayer::id`] is that identity across ticks.
//! Such an object still answers `isSpectator()` from the tab list: the caller passes it with
//! [`OrbPlayer::removed`] set. Without such an entry the orb keeps the copy it last read.

/// One player as an experience orb's tick reads it.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct OrbPlayer {
    /// The player's identity across ticks (its entity id).
    pub id: i32,
    /// Position (feet).
    pub pos: [f64; 3],
    /// `getEyeHeight()` / `getStandingEyeHeight()` as the version reads it (1.8.9: 1.62, 1.54
    /// sneaking; later versions: the pose's eye height).
    pub eye_height: f32,
    /// `isSpectator()`: spectators are never followed.
    pub spectator: bool,
    /// `isDeadOrDying()` (26.x only: a dead nearest player means no target).
    pub dead: bool,
    /// No longer in the level (an orb still follows it): never a new target. Its position and
    /// eye height are where its last tick left them; its spectator flag is still current.
    pub removed: bool,
}

impl OrbPlayer {
    /// A living, non-spectating player at `pos` with eyes `eye_height` above its feet.
    #[must_use]
    pub const fn new(id: i32, pos: [f64; 3], eye_height: f32) -> OrbPlayer {
        OrbPlayer {
            id,
            pos,
            eye_height,
            spectator: false,
            dead: false,
            removed: false,
        }
    }

    /// Sets the removed flag (see [`OrbPlayer::removed`]).
    #[must_use]
    pub const fn with_removed(mut self, removed: bool) -> OrbPlayer {
        self.removed = removed;
        self
    }

    /// Sets the spectator flag.
    #[must_use]
    pub const fn with_spectator(mut self, spectator: bool) -> OrbPlayer {
        self.spectator = spectator;
        self
    }

    /// Sets the dead flag (`isDeadOrDying`).
    #[must_use]
    pub const fn with_dead(mut self, dead: bool) -> OrbPlayer {
        self.dead = dead;
        self
    }
}

/// The squared distance between a player and a point (`distanceToSqr`).
#[inline]
#[allow(dead_code)] // each version module uses the helpers it needs
pub(crate) fn dist_sqr(p: &OrbPlayer, x: f64, y: f64, z: f64) -> f64 {
    let (dx, dy, dz) = (p.pos[0] - x, p.pos[1] - y, p.pos[2] - z);
    dx * dx + dy * dy + dz * dz
}

/// `getNearestPlayer(x, y, z, range, NO_SPECTATORS)`: the first player in list order with the
/// smallest squared distance below `range_sqr`.
#[allow(dead_code)]
pub(crate) fn nearest(
    players: &[OrbPlayer],
    x: f64,
    y: f64,
    z: f64,
    range_sqr: f64,
) -> Option<OrbPlayer> {
    let mut best = -1.0;
    let mut out = None;
    for p in players {
        if p.spectator || p.removed {
            continue;
        }
        let d = dist_sqr(p, x, y, z);
        if d < range_sqr && (best == -1.0 || d < best) {
            best = d;
            out = Some(*p);
        }
    }
    out
}

/// The followed player as it is now: its entry in `players` (removed or not), else the copy
/// last read.
#[inline]
#[allow(dead_code)]
pub(crate) fn refresh(target: &mut Option<OrbPlayer>, players: &[OrbPlayer]) {
    if let Some(t) = target
        && let Some(p) = players.iter().find(|p| p.id == t.id)
    {
        *t = *p;
    }
}

/// The legacy follow (1.8.9, 1.12.2, 1.16.5): the target search every 20 + `id % 100` ticks of
/// the orb's own clock, then a spectator drops out. Returns the target to pull towards.
#[allow(dead_code)]
pub(crate) fn legacy_target(
    target: &mut Option<OrbPlayer>,
    last_search: &mut i32,
    clock: i32,
    id: i32,
    players: &[OrbPlayer],
    pos: [f64; 3],
) -> Option<OrbPlayer> {
    refresh(target, players);
    if *last_search < clock.wrapping_sub(20).wrapping_add(id % 100) {
        let far = target.is_none_or(|t| dist_sqr(&t, pos[0], pos[1], pos[2]) > 64.0);
        if far {
            *target = nearest(players, pos[0], pos[1], pos[2], 64.0);
        }
        *last_search = clock;
    }
    if target.is_some_and(|t| t.spectator) {
        *target = None;
    }
    *target
}

/// The 1.8.9 / 1.12.2 pull: `(target - orb) / 8` with the target's eye (1.8.9: the full eye
/// height, 1.12.2: half of it), `1 - length` squared times 0.1 along it while positive.
#[allow(dead_code)]
pub(crate) fn legacy_pull(vel: &mut [f64; 3], pos: [f64; 3], t: &OrbPlayer, half_eye: bool) {
    let eye = if half_eye {
        f64::from(t.eye_height) / 2.0
    } else {
        f64::from(t.eye_height)
    };
    let e = (t.pos[0] - pos[0]) / 8.0;
    let f = (t.pos[1] + eye - pos[1]) / 8.0;
    let g = (t.pos[2] - pos[2]) / 8.0;
    let h = (e * e + f * f + g * g).sqrt();
    let mut i = 1.0 - h;
    if i > 0.0 {
        i *= i;
        vel[0] += e / h * i * 0.1;
        vel[1] += f / h * i * 0.1;
        vel[2] += g / h * i * 0.1;
    }
}