ghast_physics/lib.rs
1//! Vanilla-exact player movement for the ghast Minecraft Java Edition client library.
2//!
3//! The crate is a **pure, deterministic function**: `(player state, input, world view) -> new
4//! player state + the movement packets of the tick`. No IO, no async, no clock, no global mutable
5//! state (the only static is vanilla's constant sine table). The same code path serves the live
6//! client, trace replays and look-ahead planning (copy the state, simulate, discard).
7//!
8//! Rules are per protocol version, each in its own module selected at compile time:
9//!
10//! | module | version | status |
11//! |---|---|---|
12//! | [`v1_8`] | 1.8.9 (protocol 47; also 1.8.2 – 1.8.8) | bit-exact against the vanilla oracle |
13//! | [`v1_12`] | 1.12.2 (protocol 340; also 1.12.1) | bit-exact against the vanilla oracle |
14//! | [`v1_16_5`] | 1.16.5 (protocol 754; also 1.16.2 – 1.16.4) | bit-exact against the vanilla oracle |
15//! | [`modern`] (`V26_1`) | 26.1 (protocol 775) | bit-exact against the vanilla oracle |
16//! | [`modern`] (`V26_2`) | 26.2 (protocol 776) | bit-exact against the vanilla oracle |
17//! | [`modern`] (`V26_3`) | 26.3 (protocol 777) | bit-exact against the vanilla oracle |
18//!
19//! Code that is generic over versions uses the [`Ruleset`] trait (static dispatch, no runtime
20//! cost).
21//!
22//! [`pick`] is the crosshair target (block, face, hit point, or entity) for 1.8.9, 1.12.2,
23//! 1.16.5, 26.1, 26.2 and 26.3.
24//!
25//! ```
26//! # #[cfg(feature = "v1_8")] {
27//! use ghast_physics::{Keys, Packets, TickInput, v1_8};
28//!
29//! let mut world = v1_8::memory_world();
30//! world.load_chunks(-1, -1, 0, 0);
31//! world.fill_raw([-16, 63, -16], [15, 63, 15], 1 << 4); // stone floor
32//! let mut player = v1_8::PlayerState::new([0.5, 64.0, 0.5], 0.0, 0.0);
33//! let mut out = Packets::new();
34//! let keys = Keys { forward: true, ..Keys::NONE };
35//! let input = TickInput { keys, yaw: 0.0, pitch: 0.0, using_item: false };
36//! for _ in 0..20 {
37//! out.clear();
38//! v1_8::tick(&mut player, &input, &world, &mut out);
39//! }
40//! assert!(player.on_ground && player.pos[2] > 2.0);
41//! # }
42//! ```
43
44#![forbid(unsafe_code)]
45#![warn(missing_docs, unnameable_types)]
46
47pub mod attribute;
48mod input;
49#[cfg(any(
50 feature = "v1_8",
51 feature = "v1_12",
52 feature = "v1_16_5",
53 feature = "v26_1",
54 feature = "v26_2",
55 feature = "v26_3"
56))]
57mod jdk_trig;
58pub mod math;
59#[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
60pub mod modern;
61#[cfg(any(feature = "v1_8", feature = "v1_12", feature = "v1_16_5"))]
62mod nonliving;
63pub mod orb;
64pub mod pick;
65pub mod rotation;
66#[cfg(feature = "v1_12")]
67pub mod v1_12;
68#[cfg(feature = "v1_16_5")]
69pub mod v1_16_5;
70#[cfg(feature = "v1_8")]
71pub mod v1_8;
72pub mod voxel;
73mod world;
74
75pub use input::{Abilities, Keys, Packet, Packets, TickInput};
76pub use world::{BlockView, MemoryWorld, WorldBorder};
77
78/// A version's movement rules, for code generic over protocol versions.
79pub trait Ruleset {
80 /// The version's block state type.
81 type BlockState: Copy;
82 /// The version's player movement state.
83 type Player: Clone;
84 /// The version's rule options (`Default` = the project policy).
85 type Options: Default;
86
87 /// Runs one client tick with explicit options (see the version module's `tick_with`).
88 fn tick_with<W: BlockView<State = Self::BlockState>>(
89 player: &mut Self::Player,
90 input: &TickInput,
91 world: &W,
92 options: &Self::Options,
93 out: &mut Packets,
94 );
95
96 /// Runs one client tick with the default options.
97 fn tick<W: BlockView<State = Self::BlockState>>(
98 player: &mut Self::Player,
99 input: &TickInput,
100 world: &W,
101 out: &mut Packets,
102 ) {
103 Self::tick_with(player, input, world, &Self::Options::default(), out);
104 }
105}