ghast-physics 0.4.9

Vanilla-exact, pure and deterministic player movement for the ghast Minecraft Java Edition client library (1.8.9 first), verified bit for bit against the real game
Documentation
//! The 1.8.9 crosshair block pick (vanilla `Entity.rayTrace(reach, 1.0F)`:
//! `World.rayTrace(eye, eye + look * reach, false, false, true)`), from
//! [`crate::v1_8::ray_shape`] (neighbour-dependent bounds, the eight-pass stairs test).
//! Verified against the oracle (`tests/golden/1.8.9/raytrace.jsonl.gz`).

use ghast_data::v1_8::BlockState;

pub use super::legacy::Target;
use super::legacy::{Shape, target, trace};
pub use super::legacy::{eye_position, look_vector, ray_end};
use super::{BlockHit, PickEntity};
use crate::math::Aabb;
use crate::v1_8::{RayShape, ray_shape};
use crate::world::BlockView;

/// Survival block reach.
pub const REACH: f64 = 4.5;
/// Creative block reach.
pub const CREATIVE_REACH: f64 = 5.0;

/// What the ray sees at a block.
#[must_use]
pub fn shape<W: BlockView<State = BlockState>>(world: &W, pos: [i32; 3], liquids: bool) -> Shape {
    let r = ray_shape(world, pos[0], pos[1], pos[2], liquids);
    // The moving piston's rayTrace returns null: the ray considers it but never hits it.
    if r != RayShape::None && world.block(pos[0], pos[1], pos[2]).block().id() == 36 {
        return Shape::Never;
    }
    match r {
        RayShape::None => Shape::Skip,
        RayShape::Bounds(b) => Shape::One(b),
        RayShape::Stairs(p) => {
            let mut boxes = [Aabb::FULL; 8];
            let mut len = 0u8;
            for b in p.boxes().into_iter().flatten() {
                boxes[usize::from(len)] = b;
                len += 1;
            }
            Shape::Many(boxes, len)
        }
    }
}

/// `World.rayTrace(start, end, liquids, false, return_last)`.
pub fn clip<W: BlockView<State = BlockState>>(
    world: &W,
    start: [f64; 3],
    end: [f64; 3],
    liquids: bool,
    return_last: bool,
) -> Option<BlockHit> {
    trace(start, end, return_last, |p| shape(world, p, liquids))
}

/// The crosshair block: `Entity.rayTrace(reach, 1.0F)` from `eye` along `look`. In 1.8 the
/// player's look vector uses the **head** yaw (`LivingEntity.getRotationVector` override):
/// `look_vector(head_yaw, pitch)`. `None` when the
/// ray ends without hitting or passing a block it skips.
pub fn pick<W: BlockView<State = BlockState>>(
    world: &W,
    eye: [f64; 3],
    look: [f64; 3],
    reach: f64,
) -> Option<BlockHit> {
    clip(world, eye, ray_end(eye, look, reach), false, true)
}

/// The crosshair target (vanilla `GameRenderer.updateTargetedEntity(1.0F)`): the block pick at the
/// game mode's reach (4.5, creative 5.0), unless an entity of `entities` is hit nearer along the
/// ray to the reach (creative: 6.0); outside creative an entity farther than 3 blocks gives a miss
/// at its hit point ([`Target::TooFar`]). `player_bb` is the player's bounding box; `entities` the
/// pickable ones (`collides()`, no spectators) in the world's iteration order.
pub fn pick_target<W: BlockView<State = BlockState>>(
    world: &W,
    eye: [f64; 3],
    look: [f64; 3],
    creative: bool,
    player_bb: &Aabb,
    entities: &[PickEntity],
) -> Target {
    let reach = if creative { CREATIVE_REACH } else { REACH };
    target(
        pick(world, eye, look, reach),
        eye,
        look,
        reach,
        creative,
        player_bb,
        entities,
    )
}

/// The reach of an item's own ray (`Item.onHit`, `BoatItem.onStartUse`).
pub const ITEM_REACH: f64 = 5.0;

/// The ray of an item's use in the air: `Item.onHit(world, player, liquids)` (`World.rayTrace(eye,
/// eye + look * 5, liquids, !liquids, false)`: with `liquids` it stops at still liquids, without
/// it passes the blocks that have no collision box, [`crate::v1_8::no_collision_box`]) and
/// `BoatItem`'s (`rayTrace(eye, end, true)`: still liquids, every block; `solid_only` false).
/// `look` from the player's own yaw and pitch ([`look_vector`]; not the head yaw). `None` when
/// nothing is hit.
pub fn item_ray<W: BlockView<State = BlockState>>(
    world: &W,
    eye: [f64; 3],
    look: [f64; 3],
    liquids: bool,
    solid_only: bool,
) -> Option<BlockHit> {
    let end = ray_end(eye, look, ITEM_REACH);
    trace(eye, end, false, |p| {
        if solid_only && crate::v1_8::no_collision_box_at(world, p[0], p[1], p[2]) {
            Shape::Skip
        } else {
            shape(world, p, liquids)
        }
    })
}