ghast_pathfinder/lib.rs
1//! Physics-exact path planning and following for the ghast Minecraft Java Edition bot library.
2//!
3//! - [`Pathfinder`]: A* over feet blocks. Moves: walk (incl. step-ups onto slabs and partial
4//! blocks), diagonal, jump up, drop down (configurable maximum fall), gap jumps (up to 3
5//! blocks, also one up or down; each validated in the physics), swimming, ladder / vine
6//! climbing, doors and fence gates (opened with a click), and optionally digging (also
7//! straight down) and placing (filling holes, bridging gaps, towering up). Costs are
8//! ticks **measured with `ghast-physics`** (see [`Costs`]). Unknown (unloaded) blocks are
9//! never treated as air: they are impassable, and a goal beyond them yields a partial path.
10//! - [`Controller`]: turns a path into per-tick keys and aim. It picks the keys by simulating
11//! the vanilla-exact physics one tick ahead, and turns in whole mouse counts exactly like the
12//! client does, so a simulated run and a live run make the same decisions.
13//! - [`simulate`]: runs a path with the controller in `ghast-physics`; the validation tests use
14//! it to check every move type on random terrain, and [`Pathfinder::plan_verified`] uses it
15//! to repair a plan before the bot moves.
16//! - `Follower` (feature `client`, default): a `ghast_client::Behavior` helper that plans,
17//! follows, replans on deviations, teleports and blocked moves, all through `TickContext`.
18//!
19//! ```
20//! use ghast_pathfinder::{Config, Goal, Pathfinder, Version};
21//!
22//! let mut world = ghast_physics::v1_8::memory_world();
23//! world.load_chunks(-2, -2, 2, 2);
24//! world.fill_raw([-20, 63, -20], [20, 63, 20], 1 << 4); // stone floor
25//! world.fill_raw([3, 64, -5], [3, 64, 5], 1 << 4); // a low wall to jump over
26//! let pf = Pathfinder::new(Config::default());
27//! let path = pf.plan(Version::V1_8, &world, [0, 64, 0], &Goal::Block([8, 64, 0])).unwrap();
28//! assert!(!path.partial);
29//! assert_eq!(path.steps.last().unwrap().pos, [8, 64, 0]);
30//! ```
31
32#![forbid(unsafe_code)]
33#![warn(missing_docs, unnameable_types)]
34// Without a version feature the crate builds (nothing can be planned: `Version` has no
35// variant and `Version::from_game` is always `None`) but the per-version machinery is unused.
36#![cfg_attr(
37 not(any(
38 feature = "v1_8",
39 feature = "v1_12_2",
40 feature = "v1_16_5",
41 feature = "v26_1",
42 feature = "v26_2",
43 feature = "v26_3"
44 )),
45 allow(
46 dead_code,
47 unused_imports,
48 unused_variables,
49 unused_mut,
50 unreachable_code
51 )
52)]
53
54mod class;
55mod control;
56mod cost;
57mod door;
58#[cfg(feature = "client")]
59mod follower;
60mod goal;
61mod jump;
62mod moves;
63mod physics;
64mod plan;
65mod sim;
66mod world;
67
68pub use control::{Command, Controller, ControllerConfig, Progress};
69pub use cost::Costs;
70#[cfg(feature = "client")]
71pub use follower::{FollowConfig, FollowError, FollowStatus, Follower};
72pub use goal::Goal;
73pub use moves::{MoveKind, NodeKind};
74pub use physics::Observation;
75pub use plan::{Budget, Config, DigCost, Path, Pathfinder, Place, PlanError, Search, Step};
76pub use sim::{SimOutcome, SimReport, Simulation, simulate};
77pub use world::BlockSource;
78
79/// The protocol version whose blocks and physics a plan uses.
80#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
81#[non_exhaustive]
82pub enum Version {
83 /// 1.8.9 (protocol 47).
84 #[cfg(feature = "v1_8")]
85 V1_8,
86 /// 1.12.2 (protocol 340).
87 #[cfg(feature = "v1_12_2")]
88 V1_12,
89 /// 1.16.5 (protocol 754).
90 #[cfg(feature = "v1_16_5")]
91 V1_16_5,
92 /// 26.1 – 26.1.2 (protocol 775): the modern physics core (`ghast_physics::modern`, rule set
93 /// `V26_1`). Attributes, effects and the build height come from the live player when the
94 /// caller provides it ([`Controller::set_modern_player`], [`Simulation::with_modern_player`];
95 /// the `Follower` does it every tick), vanilla defaults otherwise.
96 #[cfg(feature = "v26_1")]
97 V26_1,
98 /// 26.2 (protocol 776): the modern core with the `V26_2` rule set (collision restitution),
99 /// live player state as for 26.1.
100 #[cfg(feature = "v26_2")]
101 V26_2,
102 /// 26.3 (protocol 777): the modern core with the `V26_3` rule set (26.2 plus the 26.3 rule
103 /// items: closed-interval liquid scan, bounce threshold, camera-height eye fluid, signs
104 /// blocking flow), live player state as for 26.1.
105 #[cfg(feature = "v26_3")]
106 V26_3,
107}
108
109impl Version {
110 /// The planner's version for a client version: `None` when the planner does not support it
111 /// or its feature is off.
112 #[cfg(feature = "client")]
113 #[must_use]
114 pub fn from_game(v: ghast_client::GameVersion) -> Option<Version> {
115 use ghast_client::version::PhysicsRules as R;
116 // By rule set: sub-versions (1.12.1, 1.16.2, ...) move by their row's rules.
117 match v.caps().physics {
118 #[cfg(feature = "v1_8")]
119 R::V1_8 => Some(Version::V1_8),
120 #[cfg(feature = "v1_12_2")]
121 R::V1_12 => Some(Version::V1_12),
122 #[cfg(feature = "v1_16_5")]
123 R::V1_16_5 => Some(Version::V1_16_5),
124 #[cfg(feature = "v26_1")]
125 R::Modern26_1 => Some(Version::V26_1),
126 #[cfg(feature = "v26_2")]
127 R::Modern26_2 => Some(Version::V26_2),
128 #[cfg(feature = "v26_3")]
129 R::Modern26_3 => Some(Version::V26_3),
130 _ => None,
131 }
132 }
133}
134
135#[cfg(test)]
136mod tests_util {
137 use crate::Version;
138
139 /// Every version this build supports.
140 pub(crate) fn versions() -> Vec<Version> {
141 vec![
142 #[cfg(feature = "v1_8")]
143 Version::V1_8,
144 #[cfg(feature = "v1_12_2")]
145 Version::V1_12,
146 #[cfg(feature = "v1_16_5")]
147 Version::V1_16_5,
148 #[cfg(feature = "v26_1")]
149 Version::V26_1,
150 #[cfg(feature = "v26_2")]
151 Version::V26_2,
152 #[cfg(feature = "v26_3")]
153 Version::V26_3,
154 ]
155 }
156}
157
158#[cfg(all(test, feature = "client", feature = "v1_12_2", feature = "v1_16_5"))]
159mod from_game_tests {
160 use crate::Version;
161 use ghast_client::GameVersion;
162
163 #[test]
164 fn sub_versions_plan_with_their_row() {
165 assert_eq!(
166 Version::from_game(GameVersion::V1_12_1),
167 Some(Version::V1_12)
168 );
169 assert_eq!(
170 Version::from_game(GameVersion::V1_16_2),
171 Some(Version::V1_16_5)
172 );
173 assert_eq!(
174 Version::from_game(GameVersion::V1_16_3),
175 Some(Version::V1_16_5)
176 );
177 }
178}