ghast-physics 0.5.2

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
//! Other entities around the player: the ones it collides with (boats, shulkers) and the ones
//! whose client tick pushes it (mobs, boats).
//!
//! # As the 26.1 client does it
//!
//! * **Collision.** In its own move the player collides with the boxes of entities that
//!   `canBeCollidedWith` it (boats and rafts always, a living shulker, an adult happy ghast under
//!   conditions) like with blocks: entity boxes first, then the border, then blocks, also for the
//!   step-up and for every "does the box fit" test (poses, sneaking at edges). Pass the boxes as
//!   they are when the player ticks in [`Surroundings::entities`] to [`tick_in`](super::tick_in).
//! * **Pushing.** On the client only the local player is pushable (`EntitySelector.pushableBy`
//!   rejects every other entity there), and the player's own push finds nothing: it is the other
//!   entities' client ticks that push it. A mob's `aiStep` ends with `pushEntities` over its own
//!   box; a boat's tick pushes what is in its box grown by 0.2 horizontally (and shrunk by 0.01
//!   vertically) when the player's feet are not above the boat's bottom. Call [`push_by`] for
//!   each such entity, in the client's entity tick order, with its box after its tick (entities
//!   added after the player tick after it; the player is added first at a login).
//!   [`push_kind_26_1`] says which entity types push.
//! * Team collision rules (`set_player_team` `collisionRule`) are the caller's: with a rule other
//!   than `always` on either team, skip what it forbids.

// Vanilla's negated float comparisons are kept as written: they decide the NaN cases.
#![allow(clippy::neg_cmp_op_on_partial_ord)]

use super::ext::PlayerExt;
use super::math::{V3, intersects};
use super::player::{PlayerState, on_climbable_ext};
use super::rules::Rules;
use crate::math::Aabb;
use crate::world::BlockView;

/// What else is around the player for one tick. Default: nothing.
#[derive(Clone, Copy, Debug, Default)]
#[non_exhaustive]
pub struct Surroundings<'a> {
    /// Boxes of the entities the player collides with, as they are when the player ticks.
    pub entities: &'a [Aabb],
}

impl<'a> Surroundings<'a> {
    /// Nothing around.
    #[must_use]
    pub fn new() -> Surroundings<'a> {
        Surroundings::default()
    }

    /// With these collidable entity boxes.
    #[must_use]
    pub fn with_entities(mut self, entities: &'a [Aabb]) -> Surroundings<'a> {
        self.entities = entities;
        self
    }
}

/// How an entity's client tick pushes the player.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PushKind {
    /// `LivingEntity.pushEntities`: the player overlaps the entity's box.
    Living,
    /// `AbstractBoat.tick`: the player overlaps the boat's box grown by 0.2 horizontally and
    /// shrunk by 0.01 vertically, and its feet are not above the boat's bottom.
    Boat,
}

/// An entity whose tick may push the player: its position and box after its tick.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Pusher {
    /// Position (`getX()`, `getZ()` decide the direction).
    pub pos: [f64; 3],
    /// Bounding box.
    pub bb: Aabb,
    /// Push rule of its type.
    pub kind: PushKind,
}

impl Pusher {
    /// An entity at `pos` with box `bb` pushing by `kind`.
    #[must_use]
    pub fn new(pos: [f64; 3], bb: Aabb, kind: PushKind) -> Pusher {
        Pusher { pos, bb, kind }
    }
}

/// The push rule of a 26.1 entity type (registry name without namespace) for the local player,
/// or `None` when its client tick never pushes the player: living entities except bats, parrots
/// and armor stands push; boats and rafts push; everything else (minecarts, items, projectiles,
/// …) does not. `living` is `instanceof LivingEntity` of the type
/// (`ghast_data::v26_1::entity_type(id).is_living()`).
#[must_use]
pub fn push_kind_26_1(name: &str, living: bool) -> Option<PushKind> {
    if is_boat(name) {
        return Some(PushKind::Boat);
    }
    match name {
        "bat" | "parrot" | "armor_stand" => None,
        _ if living => Some(PushKind::Living),
        _ => None,
    }
}

/// `true` for the 26.1 entity types the player always collides with (`canBeCollidedWith`):
/// boats and rafts, and a shulker while it is alive. (An adult happy ghast collides only under
/// conditions: with a player standing at or above its top, or while it keeps still after a
/// rider got off; the caller decides that one.)
#[must_use]
pub fn collides_26_1(name: &str) -> bool {
    is_boat(name) || name == "shulker"
}

fn is_boat(name: &str) -> bool {
    name.ends_with("_boat") || name.ends_with("_raft")
}

/// The push of one entity's client tick on the player (`Entity.push(Entity)` through the entity's
/// push rule). Returns `true` when the player's velocity changed. The player is pushable when it
/// is not a spectator and not on a climbable (vanilla also: alive, not riding something it is
/// the vehicle of; the caller does not call this for a dead player).
pub fn push_by<R: Rules, W: BlockView<State = R::State>>(
    p: &mut PlayerState,
    x: &PlayerExt,
    world: &W,
    other: &Pusher,
) -> bool {
    push_by_pair::<R, W>(p, x, world, other).is_some()
}

/// [`push_by`], also returning the other entity's half of the push (x, z), which vanilla adds
/// to its motion in the same `Entity.push` when it is pushable and carries no passenger (a mob:
/// `mob - player`; a boat: `boat - player`). `None`: nobody was pushed.
pub fn push_by_pair<R: Rules, W: BlockView<State = R::State>>(
    p: &mut PlayerState,
    x: &PlayerExt,
    world: &W,
    other: &Pusher,
) -> Option<[f64; 2]> {
    let area = match other.kind {
        PushKind::Living => other.bb,
        PushKind::Boat => super::math::inflate(
            &other.bb,
            f64::from(0.2f32),
            f64::from(-0.01f32),
            f64::from(0.2f32),
        ),
    };
    let b = p.bb;
    if !intersects(
        &b, area.min_x, area.min_y, area.min_z, area.max_x, area.max_y, area.max_z,
    ) {
        return None;
    }
    // A boat finds what it pushes with `Level.getEntities`, which visits only the entity sections
    // around the queried box: the player is found only when the section of its position is among
    // them, however far its box reaches (a scaled-up player). Mobs ask
    // `ClientLevel.getPushableEntities`, which tests the local player's box directly.
    if other.kind == PushKind::Boat && !section_query_reaches(&area, p.pos) {
        return None;
    }
    // EntitySelector.pushableBy: no spectators, isPushable (alive, not a spectator, not on a
    // climbable).
    if p.spectator || on_climbable_ext::<R, W>(p, x, world) {
        return None;
    }
    if other.kind == PushKind::Boat && !(b.min_y <= other.bb.min_y) {
        return None;
    }
    // Entity.push(entity): the player moves away from the other entity. A mob pushes through
    // `player.push(mob)` (the player gets `-(mob - player)`), a boat through `boat.push(player)`
    // (the player gets `player - boat`): the same value, but a zero keeps its sign.
    let (mut xa, mut za) = match other.kind {
        PushKind::Living => (other.pos[0] - p.pos[0], other.pos[2] - p.pos[2]),
        PushKind::Boat => (p.pos[0] - other.pos[0], p.pos[2] - other.pos[2]),
    };
    let mut dd = xa.abs().max(za.abs());
    if !(dd >= f64::from(0.01f32)) {
        return None;
    }
    dd = dd.sqrt();
    xa /= dd;
    za /= dd;
    let pow = (1.0 / dd).min(1.0);
    xa *= pow;
    za *= pow;
    xa *= f64::from(0.05f32);
    za *= f64::from(0.05f32);
    if !(xa.is_finite() && za.is_finite()) {
        return None;
    }
    // The other entity's half: a mob gets `+(mob - player)`, a boat `-(player - boat)`.
    let other_half = match other.kind {
        PushKind::Living => [xa, za],
        PushKind::Boat => [-xa, -za],
    };
    if other.kind == PushKind::Living {
        (xa, za) = (-xa, -za);
    }
    let v = V3::of(p.vel).add(V3::new(xa, 0.0, za));
    if v.is_finite() {
        p.vel = v.arr();
    }
    Some(other_half)
}

/// `true` when an entity query over `area` (`Level.getEntities` through
/// `EntitySectionStorage.forEachAccessibleNonEmptySection`) visits the entity section holding an
/// entity at `pos`. The query looks at the sections of `area` grown by 2 blocks horizontally and
/// 4 blocks downwards (not upwards); an entity is stored in the section of its block position,
/// so an entity whose box reaches farther from its position than that (a large `scale`, a large
/// mob) is not found by queries that only touch the far part of its box. Boat pushes go this way
/// (applied by [`push_by`]), and so do the player's entity collisions (`Level.getEntityCollisions`
/// over the player's box): pass only the [`Surroundings::entities`] boxes whose entity
/// position this accepts for the player's movement box. Mob pushes do not
/// (`ClientLevel.getPushableEntities` tests the local player's box directly).
#[must_use]
pub fn section_query_reaches(area: &Aabb, pos: [f64; 3]) -> bool {
    use crate::math::floor;
    let sec = |v: f64| floor(v) >> 4;
    let (sx, sy, sz) = (sec(pos[0]), sec(pos[1]), sec(pos[2]));
    sec(area.min_x - 2.0) <= sx
        && sx <= sec(area.max_x + 2.0)
        && sec(area.min_y - 4.0) <= sy
        && sy <= sec(area.max_y + 0.0)
        && sec(area.min_z - 2.0) <= sz
        && sz <= sec(area.max_z + 2.0)
}