ghast-pathfinder 0.5.6

Physics-exact path planning and path following for the ghast Minecraft Java Edition bot library (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3): every move type is validated against the vanilla-exact ghast-physics
Documentation
//! Physics-exact path planning and following for the ghast Minecraft Java Edition bot library.
//!
//! - [`Pathfinder`]: A* over feet blocks. Moves: walk (incl. step-ups onto slabs and partial
//!   blocks), diagonal, jump up, drop down (configurable maximum fall), gap jumps (up to 3
//!   blocks, also one up or down; each validated in the physics), swimming, ladder / vine
//!   climbing, doors and fence gates (opened with a click), and optionally digging (also
//!   straight down) and placing (filling holes, bridging gaps, towering up). Costs are
//!   ticks **measured with `ghast-physics`** (see [`Costs`]). Unknown (unloaded) blocks are
//!   never treated as air: they are impassable, and a goal beyond them yields a partial path.
//! - [`Controller`]: turns a path into per-tick keys and aim. It picks the keys by simulating
//!   the vanilla-exact physics one tick ahead, and turns in whole mouse counts exactly like the
//!   client does, so a simulated run and a live run make the same decisions.
//! - [`simulate`]: runs a path with the controller in `ghast-physics`; the validation tests use
//!   it to check every move type on random terrain, and [`Pathfinder::plan_verified`] uses it
//!   to repair a plan before the bot moves.
//! - `Follower` (feature `client`, default): a `ghast_client::Behavior` helper that plans,
//!   follows, replans on deviations, teleports and blocked moves, all through `TickContext`.
//!
//! ```
//! use ghast_pathfinder::{Config, Goal, Pathfinder, Version};
//!
//! let mut world = ghast_physics::v1_8::memory_world();
//! world.load_chunks(-2, -2, 2, 2);
//! world.fill_raw([-20, 63, -20], [20, 63, 20], 1 << 4); // stone floor
//! world.fill_raw([3, 64, -5], [3, 64, 5], 1 << 4); // a low wall to jump over
//! let pf = Pathfinder::new(Config::default());
//! let path = pf.plan(Version::V1_8, &world, [0, 64, 0], &Goal::Block([8, 64, 0])).unwrap();
//! assert!(!path.partial);
//! assert_eq!(path.steps.last().unwrap().pos, [8, 64, 0]);
//! ```

#![forbid(unsafe_code)]
#![warn(missing_docs, unnameable_types)]
// Without a version feature the crate builds (nothing can be planned: `Version` has no
// variant and `Version::from_game` is always `None`) but the per-version machinery is unused.
#![cfg_attr(
    not(any(
        feature = "v1_8",
        feature = "v1_12_2",
        feature = "v1_16_5",
        feature = "v26_1",
        feature = "v26_2",
        feature = "v26_3"
    )),
    allow(
        dead_code,
        unused_imports,
        unused_variables,
        unused_mut,
        unreachable_code
    )
)]

mod class;
mod control;
mod cost;
mod door;
#[cfg(feature = "client")]
mod follower;
mod goal;
mod jump;
mod moves;
mod physics;
mod plan;
mod sim;
mod world;

pub use control::{Command, Controller, ControllerConfig, Progress};
pub use cost::Costs;
#[cfg(feature = "client")]
pub use follower::{FollowConfig, FollowError, FollowStatus, Follower};
pub use goal::Goal;
pub use moves::{MoveKind, NodeKind};
pub use physics::Observation;
pub use plan::{Budget, Config, DigCost, Path, Pathfinder, Place, PlanError, Search, Step};
pub use sim::{SimOutcome, SimReport, Simulation, simulate};
pub use world::BlockSource;

/// The protocol version whose blocks and physics a plan uses.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Version {
    /// 1.8.9 (protocol 47).
    #[cfg(feature = "v1_8")]
    V1_8,
    /// 1.12.2 (protocol 340).
    #[cfg(feature = "v1_12_2")]
    V1_12,
    /// 1.16.5 (protocol 754).
    #[cfg(feature = "v1_16_5")]
    V1_16_5,
    /// 26.1 – 26.1.2 (protocol 775): the modern physics core (`ghast_physics::modern`, rule set
    /// `V26_1`). Attributes, effects and the build height come from the live player when the
    /// caller provides it ([`Controller::set_modern_player`], [`Simulation::with_modern_player`];
    /// the `Follower` does it every tick), vanilla defaults otherwise.
    #[cfg(feature = "v26_1")]
    V26_1,
    /// 26.2 (protocol 776): the modern core with the `V26_2` rule set (collision restitution),
    /// live player state as for 26.1.
    #[cfg(feature = "v26_2")]
    V26_2,
    /// 26.3 (protocol 777): the modern core with the `V26_3` rule set (26.2 plus the 26.3 rule
    /// items: closed-interval liquid scan, bounce threshold, camera-height eye fluid, signs
    /// blocking flow), live player state as for 26.1.
    #[cfg(feature = "v26_3")]
    V26_3,
}

impl Version {
    /// The planner's version for a client version: `None` when the planner does not support it
    /// or its feature is off.
    #[cfg(feature = "client")]
    #[must_use]
    pub fn from_game(v: ghast_client::GameVersion) -> Option<Version> {
        use ghast_client::version::PhysicsRules as R;
        // By rule set: sub-versions (1.12.1, 1.16.2, ...) move by their row's rules.
        match v.caps().physics {
            #[cfg(feature = "v1_8")]
            R::V1_8 => Some(Version::V1_8),
            #[cfg(feature = "v1_12_2")]
            R::V1_12 => Some(Version::V1_12),
            #[cfg(feature = "v1_16_5")]
            R::V1_16_5 => Some(Version::V1_16_5),
            #[cfg(feature = "v26_1")]
            R::Modern26_1 => Some(Version::V26_1),
            #[cfg(feature = "v26_2")]
            R::Modern26_2 => Some(Version::V26_2),
            #[cfg(feature = "v26_3")]
            R::Modern26_3 => Some(Version::V26_3),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests_util {
    use crate::Version;

    /// Every version this build supports.
    pub(crate) fn versions() -> Vec<Version> {
        vec![
            #[cfg(feature = "v1_8")]
            Version::V1_8,
            #[cfg(feature = "v1_12_2")]
            Version::V1_12,
            #[cfg(feature = "v1_16_5")]
            Version::V1_16_5,
            #[cfg(feature = "v26_1")]
            Version::V26_1,
            #[cfg(feature = "v26_2")]
            Version::V26_2,
            #[cfg(feature = "v26_3")]
            Version::V26_3,
        ]
    }
}

#[cfg(all(test, feature = "client", feature = "v1_12_2", feature = "v1_16_5"))]
mod from_game_tests {
    use crate::Version;
    use ghast_client::GameVersion;

    #[test]
    fn sub_versions_plan_with_their_row() {
        assert_eq!(
            Version::from_game(GameVersion::V1_12_1),
            Some(Version::V1_12)
        );
        assert_eq!(
            Version::from_game(GameVersion::V1_16_2),
            Some(Version::V1_16_5)
        );
        assert_eq!(
            Version::from_game(GameVersion::V1_16_3),
            Some(Version::V1_16_5)
        );
    }
}