ghast-pathfinder 0.4.2

Physics-exact path planning and path following for the ghast Minecraft Java Edition bot library (1.8.9, 1.12.2, 1.16.5 and 26.1): every move type is validated against the vanilla-exact ghast-physics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! A version-generic face over `ghast-physics`, used for costs, the follower's one-tick
//! look-ahead and path validation.

use ghast_physics::{BlockView, Keys, Packets, TickInput, WorldBorder};

use crate::Version;
use crate::world::BlockSource;

/// What the follower observes of the player (the client's `Pose`, version-free).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Observation {
    /// Feet position.
    pub pos: [f64; 3],
    /// Motion per tick (as stored after the last tick).
    pub vel: [f64; 3],
    /// On ground.
    pub on_ground: bool,
    /// Yaw (degrees).
    pub yaw: f32,
    /// Pitch (degrees).
    pub pitch: f32,
    /// Sprinting.
    pub sprinting: bool,
    /// Food level (sprinting needs more than 6).
    pub food: i32,
}

/// What the caller knows of the live player beyond the [`Observation`]: on 26.1 the complete
/// movement state (attributes such as movement speed, step height, jump strength and scale come
/// from the server; effects, abilities, the level's build height). Without it, vanilla defaults.
#[derive(Clone, Debug, Default)]
pub(crate) struct Live {
    /// 26.1: the client player's state.
    #[cfg(ghast_modern)]
    pub(crate) modern: Option<Box<ghast_physics::modern::PlayerState>>,
}

impl Live {
    /// Stores the 26.1 player state (reusing the allocation).
    #[cfg(ghast_modern)]
    pub(crate) fn set_modern(&mut self, p: &ghast_physics::modern::PlayerState) {
        match &mut self.modern {
            Some(b) => **b = *p,
            None => self.modern = Some(Box::new(*p)),
        }
    }
}

/// One version's movement rules.
pub(crate) trait Physics: 'static {
    type State: Copy;
    type Block: Copy;
    const VERSION: Version;

    /// A player standing still at `pos` (settled: on ground if a floor is right below).
    fn new_state(pos: [f64; 3], yaw: f32, pitch: f32) -> Self::State;
    /// A standing player like [`Physics::new_state`], with the live player's attributes,
    /// effects, abilities and level when `live` has them (26.1).
    #[cfg_attr(not(ghast_modern), allow(dead_code))]
    fn new_state_live(pos: [f64; 3], yaw: f32, pitch: f32, live: &Live) -> Self::State {
        let _ = live;
        Self::new_state(pos, yaw, pitch)
    }
    /// A state rebuilt from what the client exposes (approximate where `live` has nothing:
    /// timers are not known).
    fn from_observation(o: &Observation, live: &Live) -> Self::State;
    /// Records a simulated state as the live player (26.1), so a controller driven by a
    /// simulation sees what a client would expose.
    fn feed(live: &mut Live, s: &Self::State) {
        let _ = (live, s);
    }
    /// The state as the 26.1 movement state (tests drive the follower with it).
    #[cfg(all(
        test,
        feature = "v1_8",
        feature = "v1_12_2",
        feature = "v1_16_5",
        feature = "v26_1"
    ))]
    fn as_modern(s: &Self::State) -> Option<&ghast_physics::modern::PlayerState> {
        let _ = s;
        None
    }
    /// The observation of a state.
    fn observe(s: &Self::State) -> Observation;
    /// One tick.
    fn tick<W: BlockView<State = Self::Block>>(
        s: &mut Self::State,
        keys: Keys,
        yaw: f32,
        pitch: f32,
        world: &W,
    );
    /// Horizontal collision in the last tick.
    fn horizontal_collision(s: &Self::State) -> bool;
    /// Fall distance so far (reset on landing).
    fn fall_distance(s: &Self::State) -> f32;
    /// Decodes a raw state the way the world reads it.
    fn decode(raw: u32) -> Self::Block;
    /// Air.
    fn air() -> Self::Block;
}

#[cfg(feature = "v1_8")]
pub(crate) struct P18;
#[cfg(feature = "v1_12_2")]
pub(crate) struct P12;
#[cfg(feature = "v1_16_5")]
pub(crate) struct P16;
#[cfg(feature = "v26_1")]
pub(crate) struct P26;
#[cfg(feature = "v26_2")]
pub(crate) struct P262;

#[cfg(feature = "v1_8")]
impl Physics for P18 {
    type State = ghast_physics::v1_8::PlayerState;
    type Block = ghast_data::v1_8::BlockState;
    const VERSION: Version = Version::V1_8;

    fn new_state(pos: [f64; 3], yaw: f32, pitch: f32) -> Self::State {
        let mut s = ghast_physics::v1_8::PlayerState::new(pos, yaw, pitch);
        s.sent.pos = pos;
        s.sent.yaw = yaw;
        s.sent.pitch = pitch;
        s
    }

    fn from_observation(o: &Observation, _live: &Live) -> Self::State {
        let mut s = Self::new_state(o.pos, o.yaw, o.pitch);
        s.vel = o.vel;
        s.on_ground = o.on_ground;
        // Through the setter: sprinting also carries the speed modifier.
        s.set_sprinting(o.sprinting);
        s.sent.sprinting = o.sprinting;
        s.food_level = o.food;
        s
    }

    fn observe(s: &Self::State) -> Observation {
        Observation {
            pos: [s.pos[0], s.bb.min_y, s.pos[2]],
            vel: s.vel,
            on_ground: s.on_ground,
            yaw: s.yaw,
            pitch: s.pitch,
            sprinting: s.sprinting,
            food: s.food_level,
        }
    }

    #[inline]
    fn tick<W: BlockView<State = Self::Block>>(
        s: &mut Self::State,
        keys: Keys,
        yaw: f32,
        pitch: f32,
        world: &W,
    ) {
        let mut out = Packets::new();
        let input = TickInput {
            keys,
            yaw,
            pitch,
            using_item: false,
        };
        ghast_physics::v1_8::tick(s, &input, world, &mut out);
    }

    fn horizontal_collision(s: &Self::State) -> bool {
        s.horizontal_collision
    }

    fn fall_distance(s: &Self::State) -> f32 {
        s.fall_distance
    }

    #[inline]
    fn decode(raw: u32) -> Self::Block {
        ghast_data::v1_8::BlockState::from_raw_vanilla(raw as u16)
    }

    fn air() -> Self::Block {
        ghast_data::v1_8::BlockState::AIR
    }
}

#[cfg(feature = "v1_12_2")]
impl Physics for P12 {
    type State = ghast_physics::v1_12::PlayerState;
    type Block = ghast_data::v1_12::BlockState;
    const VERSION: Version = Version::V1_12;

    fn new_state(pos: [f64; 3], yaw: f32, pitch: f32) -> Self::State {
        let mut s = ghast_physics::v1_12::PlayerState::new(pos, yaw, pitch);
        s.sent.pos = pos;
        s.sent.yaw = yaw;
        s.sent.pitch = pitch;
        s
    }

    fn from_observation(o: &Observation, _live: &Live) -> Self::State {
        let mut s = Self::new_state(o.pos, o.yaw, o.pitch);
        s.vel = o.vel;
        s.on_ground = o.on_ground;
        // Through the setter: sprinting also carries the speed modifier.
        s.set_sprinting(o.sprinting);
        s.sent.sprinting = o.sprinting;
        s.food_level = o.food;
        s
    }

    fn observe(s: &Self::State) -> Observation {
        Observation {
            pos: [s.pos[0], s.bb.min_y, s.pos[2]],
            vel: s.vel,
            on_ground: s.on_ground,
            yaw: s.yaw,
            pitch: s.pitch,
            sprinting: s.sprinting,
            food: s.food_level,
        }
    }

    #[inline]
    fn tick<W: BlockView<State = Self::Block>>(
        s: &mut Self::State,
        keys: Keys,
        yaw: f32,
        pitch: f32,
        world: &W,
    ) {
        let mut out = Packets::new();
        let input = TickInput {
            keys,
            yaw,
            pitch,
            using_item: false,
        };
        ghast_physics::v1_12::tick(s, &input, world, &mut out);
    }

    fn horizontal_collision(s: &Self::State) -> bool {
        s.horizontal_collision
    }

    fn fall_distance(s: &Self::State) -> f32 {
        s.fall_distance
    }

    #[inline]
    fn decode(raw: u32) -> Self::Block {
        ghast_data::v1_12::BlockState::from_raw_vanilla(raw as u16)
    }

    fn air() -> Self::Block {
        ghast_data::v1_12::BlockState::AIR
    }
}

#[cfg(feature = "v1_16_5")]
impl Physics for P16 {
    type State = ghast_physics::v1_16_5::PlayerState;
    type Block = ghast_data::v1_16_5::BlockState;
    const VERSION: Version = Version::V1_16_5;

    fn new_state(pos: [f64; 3], yaw: f32, pitch: f32) -> Self::State {
        let mut s = ghast_physics::v1_16_5::PlayerState::new(pos, yaw, pitch);
        s.sent.pos = pos;
        s.sent.yaw = yaw;
        s.sent.pitch = pitch;
        s.first_update = false;
        s.auto_jump_enabled = false;
        s
    }

    fn from_observation(o: &Observation, _live: &Live) -> Self::State {
        let mut s = Self::new_state(o.pos, o.yaw, o.pitch);
        s.vel = o.vel;
        s.on_ground = o.on_ground;
        // Through the setter: sprinting also carries the speed modifier.
        s.set_sprinting(o.sprinting);
        s.sent.sprinting = o.sprinting;
        s.food_level = o.food;
        s
    }

    fn observe(s: &Self::State) -> Observation {
        Observation {
            pos: s.pos,
            vel: s.vel,
            on_ground: s.on_ground,
            yaw: s.yaw,
            pitch: s.pitch,
            sprinting: s.sprinting,
            food: s.food_level,
        }
    }

    #[inline]
    fn tick<W: BlockView<State = Self::Block>>(
        s: &mut Self::State,
        keys: Keys,
        yaw: f32,
        pitch: f32,
        world: &W,
    ) {
        let mut out = Packets::new();
        let input = TickInput {
            keys,
            yaw,
            pitch,
            using_item: false,
        };
        ghast_physics::v1_16_5::tick(s, &input, world, &mut out);
    }

    fn horizontal_collision(s: &Self::State) -> bool {
        s.horizontal_collision
    }

    fn fall_distance(s: &Self::State) -> f32 {
        s.fall_distance
    }

    #[inline]
    fn decode(raw: u32) -> Self::Block {
        ghast_data::v1_16_5::BlockState::from_id(raw as u16)
            .unwrap_or(ghast_data::v1_16_5::BlockState::AIR)
    }

    fn air() -> Self::Block {
        ghast_data::v1_16_5::BlockState::AIR
    }
}

/// The movement rules of a modern version: the shared modern core with the version's rule set
/// and block states (one row per version).
#[cfg(ghast_modern)]
macro_rules! modern_physics {
    ($p:ident, $version:expr, $block:ty, $rules:ty) => {
        impl Physics for $p {
            type State = ghast_physics::modern::PlayerState;
            type Block = $block;
            const VERSION: Version = $version;

            fn new_state(pos: [f64; 3], yaw: f32, pitch: f32) -> Self::State {
                let mut s = ghast_physics::modern::PlayerState::new(pos, yaw, pitch);
                s.sent.pos = pos;
                s.sent.yaw = yaw;
                s.sent.pitch = pitch;
                s.first_tick = false;
                s.auto_jump_enabled = false;
                s
            }

            fn new_state_live(pos: [f64; 3], yaw: f32, pitch: f32, live: &Live) -> Self::State {
                let mut s = Self::new_state(pos, yaw, pitch);
                if let Some(t) = live.modern.as_deref() {
                    s.attributes = t.attributes;
                    s.effects = t.effects;
                    s.abilities = t.abilities;
                    s.spectator = t.spectator;
                    s.food_level = t.food_level;
                    s.leather_boots = t.leather_boots;
                    s.options = t.options;
                    s.level = t.level;
                    // The box follows the scale attribute.
                    s.dims = t.dims;
                    s.scale_dirty = t.scale_dirty;
                    s.set_pos(pos[0], pos[1], pos[2]);
                    // Sprinting is the controller's choice: the copied speed modifier goes.
                    s.set_sprinting(false);
                }
                s
            }

            fn from_observation(o: &Observation, live: &Live) -> Self::State {
                let Some(t) = live.modern.as_deref() else {
                    let mut s = Self::new_state(o.pos, o.yaw, o.pitch);
                    s.vel = o.vel;
                    s.on_ground = o.on_ground;
                    s.set_sprinting(o.sprinting);
                    s.sent.sprinting = o.sprinting;
                    s.food_level = o.food;
                    return s;
                };
                // The live state is exact (timers, input, attributes); the observation only wins
                // where they differ.
                let mut s = *t;
                if s.pos != o.pos {
                    s.set_pos(o.pos[0], o.pos[1], o.pos[2]);
                }
                s.vel = o.vel;
                s.on_ground = o.on_ground;
                s.yaw = o.yaw;
                s.pitch = o.pitch;
                if s.sprinting != o.sprinting {
                    s.set_sprinting(o.sprinting);
                }
                s.food_level = o.food;
                s
            }

            fn feed(live: &mut Live, s: &Self::State) {
                live.set_modern(s);
            }

            #[cfg(all(
                test,
                feature = "v1_8",
                feature = "v1_12_2",
                feature = "v1_16_5",
                ghast_modern
            ))]
            fn as_modern(s: &Self::State) -> Option<&ghast_physics::modern::PlayerState> {
                Some(s)
            }

            fn observe(s: &Self::State) -> Observation {
                Observation {
                    pos: s.pos,
                    vel: s.vel,
                    on_ground: s.on_ground,
                    yaw: s.yaw,
                    pitch: s.pitch,
                    sprinting: s.sprinting,
                    food: s.food_level,
                }
            }

            #[inline]
            fn tick<W: BlockView<State = Self::Block>>(
                s: &mut Self::State,
                keys: Keys,
                yaw: f32,
                pitch: f32,
                world: &W,
            ) {
                let mut out = Packets::new();
                let input = TickInput {
                    keys,
                    yaw,
                    pitch,
                    using_item: false,
                };
                ghast_physics::modern::tick::<$rules, W>(s, &input, world, &mut out);
            }

            fn horizontal_collision(s: &Self::State) -> bool {
                s.horizontal_collision
            }

            fn fall_distance(s: &Self::State) -> f32 {
                s.fall_distance as f32
            }

            #[inline]
            fn decode(raw: u32) -> Self::Block {
                u16::try_from(raw)
                    .ok()
                    .and_then(<$block>::from_id)
                    .unwrap_or(<$block>::AIR)
            }

            fn air() -> Self::Block {
                <$block>::AIR
            }
        }
    };
}

#[cfg(feature = "v26_1")]
modern_physics!(
    P26,
    Version::V26_1,
    ghast_data::v26_1::BlockState,
    ghast_physics::modern::V26_1
);

#[cfg(feature = "v26_2")]
modern_physics!(
    P262,
    Version::V26_2,
    ghast_data::v26_2::BlockState,
    ghast_physics::modern::V26_2
);

/// A physics world view over a [`BlockSource`] (unknown reads as air, like the vanilla client),
/// with optional block overrides (simulated digs and places).
pub(crate) struct SourceView<'a, P: Physics, S: ?Sized> {
    pub(crate) src: &'a S,
    pub(crate) overrides: Vec<([i32; 3], u32)>,
    _p: std::marker::PhantomData<P>,
}

impl<'a, P: Physics, S: BlockSource + ?Sized> SourceView<'a, P, S> {
    pub(crate) fn new(src: &'a S) -> Self {
        Self::with_overrides(src, Vec::new())
    }

    /// A view with earlier overrides (a resumed simulation).
    pub(crate) fn with_overrides(src: &'a S, overrides: Vec<([i32; 3], u32)>) -> Self {
        SourceView {
            src,
            overrides,
            _p: std::marker::PhantomData,
        }
    }

    /// The raw state including overrides.
    pub(crate) fn raw(&self, x: i32, y: i32, z: i32) -> Option<u32> {
        if let Some((_, r)) = self.overrides.iter().rev().find(|(p, _)| *p == [x, y, z]) {
            return Some(*r);
        }
        self.src.raw_block(x, y, z)
    }

    pub(crate) fn set(&mut self, p: [i32; 3], raw: u32) {
        self.overrides.push((p, raw));
    }
}

impl<P: Physics, S: BlockSource + ?Sized> BlockSource for SourceView<'_, P, S> {
    fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
        self.raw(x, y, z)
    }

    fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
        self.src.is_column_loaded(chunk_x, chunk_z)
    }
}

impl<P: Physics, S: BlockSource + ?Sized> BlockView for SourceView<'_, P, S> {
    type State = P::Block;

    #[inline]
    fn block(&self, x: i32, y: i32, z: i32) -> P::Block {
        match self.raw(x, y, z) {
            Some(r) => P::decode(r),
            None => P::air(),
        }
    }

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

    fn world_border(&self) -> WorldBorder {
        WorldBorder::default()
    }
}