ghast-physics 0.5.1

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 modern client player movement (26.1 and later), one core generic over a [`Rules`] row.
//!
//! Same calling convention as the older version modules: state, input and world view in; new
//! state and the tick's movement packets out. Pure and deterministic. A version is a type implementing
//! [`Rules`] (its data tables and constants); the core is monomorphised per version, with no
//! run-time version branch.
//!
//! | rule set | version | status |
//! |---|---|---|
//! | [`V26_1`] | 26.1 (protocol 775) | bit-exact against the vanilla oracle |
//! | [`V26_2`] | 26.2 (protocol 776) | bit-exact against the vanilla oracle (26.1 + collision restitution) |
//! | [`V26_3`] | 26.3 (protocol 777) | bit-exact against the vanilla oracle for player movement, boats, minecarts and pistons (26.2 + the rule items marked 26.3 on [`Rules`]) |
//!
//! Unloaded chunks are handled exactly as the 26.1 client does: gravity stays normal (the client
//! reports every chunk as present), collision skips unloaded chunks, and the fluid sweep sees no
//! fluid when a chunk around the box is unloaded. The caller is responsible for not ticking the
//! player before its level is loaded (26.1's `player_loaded` gate: the client freezes the player
//! until the chunks around it have arrived); the project rule "never treat unloaded chunks as air"
//! is enforced there, one layer up.
//!
//! Pistons: [`piston`] (moving blocks, their shapes and pushes). Not modelled (see the crate
//! README): other block entities. Known gap: an opening shulker box's collision reaches past its cube (its lid moves
//! with the block entity); the tables have the closed box, and the client push-out probe relies on
//! every suffocating large shape staying inside its cube (pinned by the `ghast-data` test
//! `suffocating_large_shapes_stay_in_their_cube`).

mod attr;
mod boat;
mod border;
pub(crate) mod collide;
mod entity;
mod ext;
mod fluid;
mod inside;
mod jdk_math;
mod load;
pub(crate) mod math;
mod moving;
pub mod piston;
mod piston_facts;
mod piston_shapes;
mod player;
mod rules;
#[cfg(feature = "v26_3")]
mod sturdy_facts;

pub use attr::{
    Attribute, MAX_MODIFIERS, Modifier, ModifierId, SPRINT_AMOUNT, identifier_hash,
    java_string_hash, sprinting_id,
};
pub use boat::{Boat, BoatKind, BoatStatus, Seat, tick_boat, tick_passenger};
pub use border::BorderState;
pub use entity::{
    PushKind, Pusher, Surroundings, collides_26_1, push_by, push_kind_26_1, section_query_reaches,
};
pub use ext::{MAX_ROCKETS, MovementAttributes, PlayerExt};
pub use load::LoadGate;
pub use player::{
    AttributeKind, Attributes, ClientInput, ClientOptions, CollisionContext, Dimensions, Effects,
    LevelInfo, PlayerState, Pose, SentState, UseEffects, in_water_or_rain, no_block_collision,
    on_climbable, on_climbable_ext, riptide, state_collides, tick, tick_ext, tick_in,
};
pub use rules::Rules;
#[cfg(feature = "v26_1")]
pub use rules::V26_1;
#[cfg(feature = "v26_2")]
pub use rules::V26_2;
#[cfg(feature = "v26_3")]
pub use rules::V26_3;

use crate::input::{Packets, TickInput};
use crate::world::BlockView;

/// Options of the modern rule sets (none yet).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Options {}

#[cfg(feature = "v26_1")]
impl crate::Ruleset for V26_1 {
    type BlockState = ghast_data::v26_1::BlockState;
    type Player = PlayerState;
    type Options = Options;

    #[inline]
    fn tick_with<W: BlockView<State = Self::BlockState>>(
        player: &mut PlayerState,
        input: &TickInput,
        world: &W,
        _options: &Options,
        out: &mut Packets,
    ) {
        tick::<V26_1, W>(player, input, world, out);
    }
}

#[cfg(feature = "v26_2")]
impl crate::Ruleset for V26_2 {
    type BlockState = ghast_data::v26_2::BlockState;
    type Player = PlayerState;
    type Options = Options;

    #[inline]
    fn tick_with<W: BlockView<State = Self::BlockState>>(
        player: &mut PlayerState,
        input: &TickInput,
        world: &W,
        _options: &Options,
        out: &mut Packets,
    ) {
        tick::<V26_2, W>(player, input, world, out);
    }
}

#[cfg(feature = "v26_3")]
impl crate::Ruleset for V26_3 {
    type BlockState = ghast_data::v26_3::BlockState;
    type Player = PlayerState;
    type Options = Options;

    #[inline]
    fn tick_with<W: BlockView<State = Self::BlockState>>(
        player: &mut PlayerState,
        input: &TickInput,
        world: &W,
        _options: &Options,
        out: &mut Packets,
    ) {
        tick::<V26_3, W>(player, input, world, out);
    }
}

/// A [`crate::MemoryWorld`]-like in-memory world for modern versions (build height `-64..320`).
#[cfg(feature = "v26_1")]
pub type MemoryWorld26 = ModernMemoryWorld<ghast_data::v26_1::BlockState>;

/// [`MemoryWorld26`] over 26.2 block states.
#[cfg(feature = "v26_2")]
pub type MemoryWorld262 = ModernMemoryWorld<ghast_data::v26_2::BlockState>;

/// [`MemoryWorld26`] over 26.3 block states.
#[cfg(feature = "v26_3")]
pub type MemoryWorld263 = ModernMemoryWorld<ghast_data::v26_3::BlockState>;

/// An in-memory world with a configurable build height: explicitly loaded chunk columns,
/// everything else unloaded. For tests, benches and offline planning.
#[derive(Clone, Debug)]
pub struct ModernMemoryWorld<S> {
    /// Decoded states (decoding happens when a block is stored, not when it is read).
    columns: std::collections::HashMap<(i32, i32), Box<[S]>>,
    loaded_rects: Vec<[i32; 4]>,
    decode: fn(u16) -> S,
    air: S,
    min_y: i32,
    height: i32,
    border: crate::WorldBorder,
}

impl<S: Copy> ModernMemoryWorld<S> {
    /// An empty world; `decode` turns a stored raw value into a state (raw 0 must be air).
    #[must_use]
    pub fn new(decode: fn(u16) -> S, min_y: i32, height: i32) -> Self {
        ModernMemoryWorld {
            columns: std::collections::HashMap::new(),
            loaded_rects: Vec::new(),
            decode,
            air: decode(0),
            min_y,
            height,
            border: crate::WorldBorder::default(),
        }
    }

    /// Lowest block Y of the build height.
    #[must_use]
    pub fn min_y(&self) -> i32 {
        self.min_y
    }

    /// Number of block rows of the build height.
    #[must_use]
    pub fn height(&self) -> i32 {
        self.height
    }

    /// Marks the inclusive chunk rectangle `[cx0, cz0] ..= [cx1, cz1]` as loaded.
    pub fn load_chunks(&mut self, cx0: i32, cz0: i32, cx1: i32, cz1: i32) {
        self.loaded_rects.push([cx0, cz0, cx1, cz1]);
    }

    /// Sets the world border.
    pub fn set_border(&mut self, b: crate::WorldBorder) {
        self.border = b;
    }

    /// Stores a raw block value (positions outside the build height are ignored).
    pub fn set_raw(&mut self, x: i32, y: i32, z: i32, raw: u16) {
        if y < self.min_y || y >= self.min_y + self.height {
            return;
        }
        let len = (16 * 16 * self.height) as usize;
        let air = self.air;
        let state = (self.decode)(raw);
        let col = self
            .columns
            .entry((x >> 4, z >> 4))
            .or_insert_with(|| vec![air; len].into_boxed_slice());
        col[Self::index(x, y - self.min_y, z)] = state;
    }

    /// Fills the inclusive box with a raw value.
    pub fn fill_raw(&mut self, from: [i32; 3], to: [i32; 3], raw: u16) {
        for x in from[0]..=to[0] {
            for z in from[2]..=to[2] {
                for y in from[1]..=to[1] {
                    self.set_raw(x, y, z, raw);
                }
            }
        }
    }

    #[inline]
    fn index(x: i32, ry: i32, z: i32) -> usize {
        ((ry as usize) << 8) | (((z & 15) as usize) << 4) | ((x & 15) as usize)
    }

    #[inline]
    fn loaded(&self, cx: i32, cz: i32) -> bool {
        self.loaded_rects
            .iter()
            .any(|r| cx >= r[0] && cx <= r[2] && cz >= r[1] && cz <= r[3])
    }
}

impl<S: Copy> BlockView for ModernMemoryWorld<S> {
    type State = S;

    #[inline]
    fn block(&self, x: i32, y: i32, z: i32) -> S {
        let (cx, cz) = (x >> 4, z >> 4);
        let ry = y - self.min_y;
        if ry < 0 || ry >= self.height || !self.loaded(cx, cz) {
            return self.air;
        }
        match self.columns.get(&(cx, cz)) {
            Some(col) => col[Self::index(x, ry, z)],
            None => self.air,
        }
    }

    #[inline]
    fn is_chunk_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
        self.loaded(chunk_x, chunk_z)
    }

    fn world_border(&self) -> crate::WorldBorder {
        self.border
    }
}

/// An empty 26.3 world (overworld build height); unknown ids decode to air.
#[cfg(feature = "v26_3")]
#[must_use]
pub fn memory_world_26_3() -> MemoryWorld263 {
    ModernMemoryWorld::new(
        |raw| {
            ghast_data::v26_3::BlockState::from_id(raw)
                .unwrap_or(ghast_data::v26_3::BlockState::AIR)
        },
        -64,
        384,
    )
}

/// An empty 26.2 world (overworld build height); unknown ids decode to air.
#[cfg(feature = "v26_2")]
#[must_use]
pub fn memory_world_26_2() -> MemoryWorld262 {
    ModernMemoryWorld::new(
        |raw| {
            ghast_data::v26_2::BlockState::from_id(raw)
                .unwrap_or(ghast_data::v26_2::BlockState::AIR)
        },
        -64,
        384,
    )
}

/// An empty 26.1 world (overworld build height); unknown ids decode to air.
#[cfg(feature = "v26_1")]
#[must_use]
pub fn memory_world_26_1() -> MemoryWorld26 {
    ModernMemoryWorld::new(
        |raw| {
            ghast_data::v26_1::BlockState::from_id(raw)
                .unwrap_or(ghast_data::v26_1::BlockState::AIR)
        },
        -64,
        384,
    )
}