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
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
//! Running a path offline: the [`Controller`] drives the vanilla-exact `ghast-physics` player
//! through the path, turning in whole mouse counts like ghast-client does.

use crate::Version;
use crate::class::{flag, table};
use crate::control::{Controller, ControllerConfig, Progress};
use crate::door;
use crate::moves::MoveKind;
#[cfg(feature = "v1_12_2")]
use crate::physics::P12;
#[cfg(feature = "v1_16_5")]
use crate::physics::P16;
#[cfg(feature = "v1_8")]
use crate::physics::P18;
#[cfg(feature = "v26_1")]
use crate::physics::P26;
#[cfg(feature = "v26_2")]
use crate::physics::P262;
#[cfg(feature = "v26_3")]
use crate::physics::P263;
use crate::physics::{Live, Observation, Physics, SourceView};
use crate::plan::Path;
use crate::world::BlockSource;

/// How a simulated run ended.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SimOutcome {
    /// The last step was reached.
    Arrived,
    /// The controller made no progress ([`Progress::Stuck`]).
    Stuck,
    /// The player left the path ([`Progress::OffPath`]).
    OffPath,
    /// The body touched lava or a block the planner avoids (fire, cobweb, ...).
    Hazard,
    /// Out of ticks.
    Timeout,
}

/// The result of [`simulate`].
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct SimReport {
    /// How it ended.
    pub outcome: SimOutcome,
    /// Ticks run.
    pub ticks: u32,
    /// Index of the last step reached.
    pub reached: usize,
    /// Feet position at the end.
    pub end: [f64; 3],
    /// Ticks with a horizontal collision during moves that should not touch anything
    /// (walks, diagonals, drops, gap jumps). Jumps up, climbs and swims onto land are
    /// expected to touch the block they go onto and are not counted.
    pub collisions: u32,
    /// The first tick of such a collision.
    pub first_collision: Option<u32>,
    /// Largest fall distance at a landing (vanilla fall damage starts above 3).
    pub max_fall: f32,
    /// Feet position after every tick.
    pub trace: Vec<[f64; 3]>,
}

/// The client's rotation (ghast-client `rotation::Rotation` at sensitivity 0.5): whole mouse
/// counts, at most 14° yaw / 10.5° pitch per tick with a carried budget.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Turner {
    yaw_budget: f64,
    pitch_budget: f64,
}

impl Turner {
    const MAX_YAW: f64 = 14.0;
    const MAX_PITCH: f64 = 10.5;
    /// `mouse_multiplier(0.5)`: `(0.5 * 0.6 + 0.2)^3 * 8` in `f32`.
    const MULTIPLIER: f32 = 1.0;

    fn counts(budget: &mut f64, delta: f32, per_tick_deg: f64) -> i64 {
        let step = f64::from(Self::MULTIPLIER) * 0.15;
        let per_tick = per_tick_deg / step;
        *budget = (*budget + per_tick).min(per_tick + 1.0);
        let wanted = (f64::from(delta) / step + 0.5).floor() as i64;
        let lim = budget.trunc() as i64;
        let count = wanted.clamp(-lim, lim);
        *budget -= count.unsigned_abs() as f64;
        if count == wanted {
            *budget = 0.0;
        }
        count
    }

    /// One tick from (`yaw`, `pitch`) toward the target (pitch target clamped to ±89.5).
    pub(crate) fn step(&mut self, yaw: f32, pitch: f32, target: (f32, f32)) -> (f32, f32) {
        let m = Self::MULTIPLIER;
        let ty = target.0;
        let tp = target.1.clamp(-89.5, 89.5);
        let dx = Self::counts(
            &mut self.yaw_budget,
            crate::control::wrap(ty - yaw),
            Self::MAX_YAW,
        );
        let dy = Self::counts(&mut self.pitch_budget, tp - pitch, Self::MAX_PITCH);
        let fx = dx as f32 * m;
        let fy = -(dy as f32) * m;
        let new_yaw = (f64::from(yaw) + f64::from(fx) * 0.15) as f32;
        let new_pitch = ((f64::from(pitch) - f64::from(fy) * 0.15) as f32).clamp(-90.0, 90.0);
        (new_yaw, new_pitch)
    }
}

/// Runs `path` in `world` with a [`Controller`]: the player starts standing at the first
/// step's centre, facing the second, and the physics is `ghast-physics` (bit-exact vanilla)
/// for the path's version. Digs complete after `5 + 30 × hardness` ticks (a bare hand's
/// order of magnitude) and places after 4 ticks (a stone block appears); the world itself is
/// not changed.
///
/// ```
/// use ghast_pathfinder::{Config, ControllerConfig, Goal, Pathfinder, SimOutcome, Version, simulate};
///
/// 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);
/// world.fill_raw([4, 64, -3], [4, 64, 3], 1 << 4); // a step
/// let path = Pathfinder::new(Config::default())
///     .plan(Version::V1_8, &world, [0, 64, 0], &Goal::Block([9, 65, 0]))
///     .unwrap();
/// let report = simulate(&path, &world, &ControllerConfig::default());
/// assert_eq!(report.outcome, SimOutcome::Arrived);
/// ```
#[must_use]
pub fn simulate<S: BlockSource + ?Sized>(
    path: &Path,
    world: &S,
    cfg: &ControllerConfig,
) -> SimReport {
    let mut sim = Simulation::new(path, cfg);
    while sim.step(world, u32::MAX, None).is_none() {}
    sim.into_report()
}

/// Whether a client at `obs` can place `p`: the clicked face faces the eyes and is within
/// 4.5 blocks of them, and the new block does not overlap the player.
fn place_ok(obs: &Observation, p: crate::plan::Place) -> bool {
    let [fx, fy, fz] = p.face();
    let eyes = [obs.pos[0], obs.pos[1] + 1.62, obs.pos[2]];
    let c = [
        f64::from(p.against[0]) + 0.5 + 0.5 * f64::from(fx),
        f64::from(p.against[1]) + 0.5 + 0.5 * f64::from(fy),
        f64::from(p.against[2]) + 0.5 + 0.5 * f64::from(fz),
    ];
    let d = [eyes[0] - c[0], eyes[1] - c[1], eyes[2] - c[2]];
    let facing = d[0] * f64::from(fx) + d[1] * f64::from(fy) + d[2] * f64::from(fz) > 0.0;
    let reach = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt() <= 4.5;
    let (x, y, z) = (
        f64::from(p.pos[0]),
        f64::from(p.pos[1]),
        f64::from(p.pos[2]),
    );
    let o = obs.pos;
    let overlap = o[0] + 0.3 > x
        && o[0] - 0.3 < x + 1.0
        && o[1] + 1.8 > y
        && o[1] < y + 1.0
        && o[2] + 0.3 > z
        && o[2] - 0.3 < z + 1.0;
    facing && reach && !overlap
}

/// The raw stone state of a version (what a simulated place puts down).
pub(crate) fn stone_raw(version: Version) -> u32 {
    match version {
        #[cfg(feature = "v1_8")]
        Version::V1_8 => 1 << 4,
        #[cfg(feature = "v1_12_2")]
        Version::V1_12 => 1 << 4,
        #[cfg(feature = "v1_16_5")]
        Version::V1_16_5 => u32::from(ghast_data::v1_16_5::Block::STONE.default_state().id()),
        #[cfg(feature = "v26_1")]
        Version::V26_1 => u32::from(ghast_data::v26_1::Block::STONE.default_state().id()),
        #[cfg(feature = "v26_2")]
        Version::V26_2 => u32::from(ghast_data::v26_2::Block::STONE.default_state().id()),
        #[cfg(feature = "v26_3")]
        Version::V26_3 => u32::from(ghast_data::v26_3::Block::STONE.default_state().id()),
    }
}

/// The simulated player of either version.
#[derive(Clone, Debug)]
enum Body {
    #[cfg(feature = "v1_8")]
    V18(<P18 as Physics>::State),
    #[cfg(feature = "v1_12_2")]
    V12(<P12 as Physics>::State),
    #[cfg(feature = "v1_16_5")]
    V16(<P16 as Physics>::State),
    #[cfg(feature = "v26_1")]
    V26(Box<<P26 as Physics>::State>),
    #[cfg(feature = "v26_2")]
    V262(Box<<P262 as Physics>::State>),
    #[cfg(feature = "v26_3")]
    V263(Box<<P263 as Physics>::State>),
}

/// A resumable [`simulate`]: [`Simulation::step`] runs a slice of ticks, so a follower can
/// verify a long path across several game ticks. Owns its state (no borrows).
///
/// ```
/// use ghast_pathfinder::{Config, ControllerConfig, Goal, Pathfinder, SimOutcome, Simulation, Version};
///
/// let mut world = ghast_physics::v1_8::memory_world();
/// world.load_chunks(-1, -1, 1, 1);
/// world.fill_raw([-10, 63, -10], [10, 63, 10], 1 << 4);
/// let path = Pathfinder::new(Config::default())
///     .plan(Version::V1_8, &world, [0, 64, 0], &Goal::Block([8, 64, 3]))
///     .unwrap();
/// let mut sim = Simulation::new(&path, &ControllerConfig::default());
/// let outcome = loop {
///     if let Some(r) = sim.step(&world, 20, None) {
///         break r.outcome;
///     }
/// };
/// assert_eq!(outcome, SimOutcome::Arrived);
/// ```
#[derive(Clone, Debug)]
pub struct Simulation {
    body: Body,
    run: Run,
}

/// Everything of a simulation but the player.
#[derive(Clone, Debug)]
struct Run {
    version: Version,
    turner: Turner,
    ctrl: Controller,
    overrides: Vec<([i32; 3], u32)>,
    job: Option<([i32; 3], u32)>,
    tick: u32,
    max_ticks: u32,
    report: SimReport,
    done: bool,
}

impl Simulation {
    /// A simulation of `path` from its first step (standing at the centre, facing the
    /// second), not started yet.
    #[must_use]
    pub fn new(path: &Path, cfg: &ControllerConfig) -> Simulation {
        Self::with_live(path, cfg, &Live::default())
    }

    /// 26.1: like [`Simulation::new`], with the live client player's attributes (movement
    /// speed, step height, jump strength, scale, ...), effects, abilities and build height
    /// (`ghast_client::TickContext::modern_player`) instead of vanilla defaults. The `Follower`
    /// verifies its plans this way. On other versions the player is ignored.
    #[cfg(ghast_modern)]
    #[must_use]
    pub fn with_modern_player(
        path: &Path,
        cfg: &ControllerConfig,
        player: &ghast_physics::modern::PlayerState,
    ) -> Simulation {
        let mut live = Live::default();
        live.set_modern(player);
        Self::with_live(path, cfg, &live)
    }

    pub(crate) fn with_live(path: &Path, cfg: &ControllerConfig, live: &Live) -> Simulation {
        let _ = live;
        let start = path.steps[0].target();
        let yaw0 = path.steps.get(1).map_or(0.0, |s| {
            let t = s.target();
            (-(t[0] - start[0])).atan2(t[2] - start[2]).to_degrees() as f32
        });
        let body = match path.version {
            #[cfg(feature = "v1_8")]
            Version::V1_8 => Body::V18(P18::new_state(start, yaw0, 0.0)),
            #[cfg(feature = "v1_12_2")]
            Version::V1_12 => Body::V12(P12::new_state(start, yaw0, 0.0)),
            #[cfg(feature = "v1_16_5")]
            Version::V1_16_5 => Body::V16(P16::new_state(start, yaw0, 0.0)),
            #[cfg(feature = "v26_1")]
            Version::V26_1 => Body::V26(Box::new(P26::new_state_live(start, yaw0, 0.0, live))),
            #[cfg(feature = "v26_2")]
            Version::V26_2 => Body::V262(Box::new(P262::new_state_live(start, yaw0, 0.0, live))),
            #[cfg(feature = "v26_3")]
            Version::V26_3 => Body::V263(Box::new(P263::new_state_live(start, yaw0, 0.0, live))),
        };
        let max_ticks = (path.cost * 4.0) as u32 + 20 * path.steps.len() as u32 + 200;
        Simulation {
            body,
            run: Run {
                version: path.version,
                turner: Turner::default(),
                ctrl: Controller::new(path.clone(), cfg.clone()),
                overrides: Vec::new(),
                job: None,
                tick: 0,
                max_ticks,
                report: SimReport {
                    outcome: SimOutcome::Timeout,
                    ticks: 0,
                    reached: 0,
                    end: start,
                    collisions: 0,
                    first_collision: None,
                    max_fall: 0.0,
                    trace: Vec::with_capacity(max_ticks.min(4096) as usize),
                },
                done: false,
            },
        }
    }

    /// The path being simulated.
    #[must_use]
    pub fn path(&self) -> &Path {
        self.run.ctrl.path()
    }

    /// Runs at most `ticks` simulated ticks (and stops after `time`, checked every 16
    /// ticks); the report once the run has ended (`None`: call again).
    pub fn step<S: BlockSource + ?Sized>(
        &mut self,
        world: &S,
        ticks: u32,
        time: Option<std::time::Duration>,
    ) -> Option<&SimReport> {
        if !self.run.done {
            let done = match self.body {
                #[cfg(feature = "v1_8")]
                Body::V18(ref mut s) => self.run.advance::<P18, S>(s, world, ticks, time),
                #[cfg(feature = "v1_12_2")]
                Body::V12(ref mut s) => self.run.advance::<P12, S>(s, world, ticks, time),
                #[cfg(feature = "v1_16_5")]
                Body::V16(ref mut s) => self.run.advance::<P16, S>(s, world, ticks, time),
                #[cfg(feature = "v26_1")]
                Body::V26(ref mut s) => self.run.advance::<P26, S>(s, world, ticks, time),
                #[cfg(feature = "v26_2")]
                Body::V262(ref mut s) => self.run.advance::<P262, S>(s, world, ticks, time),
                #[cfg(feature = "v26_3")]
                Body::V263(ref mut s) => self.run.advance::<P263, S>(s, world, ticks, time),
            };
            self.run.done = done;
        }
        self.run.done.then_some(&self.run.report)
    }

    /// The report so far (final once [`Simulation::step`] returned it).
    #[must_use]
    pub fn report(&self) -> &SimReport {
        &self.run.report
    }

    /// The report.
    #[must_use]
    pub fn into_report(self) -> SimReport {
        self.run.report
    }
}

impl Run {
    /// Runs up to `ticks` ticks; `true` once the run ended.
    fn advance<P: Physics, S: BlockSource + ?Sized>(
        &mut self,
        state: &mut P::State,
        world: &S,
        ticks: u32,
        time: Option<std::time::Duration>,
    ) -> bool {
        let tbl = table(self.version);
        let started = time.map(|_| std::time::Instant::now());
        let mut view =
            SourceView::<P, S>::with_overrides(world, std::mem::take(&mut self.overrides));
        let class = |v: &SourceView<'_, P, S>, x: i32, y: i32, z: i32| {
            v.raw(x, y, z).map_or(crate::class::Class::UNKNOWN, |r| {
                tbl.classes[tbl.index(r) as usize]
            })
        };
        let report = &mut self.report;
        let ctrl = &mut self.ctrl;
        let mut done = false;
        let mut n = 0u32;
        while !done {
            if self.tick >= self.max_ticks {
                done = true;
                break;
            }
            if n >= ticks
                || (n % 16 == 15
                    && started
                        .zip(time)
                        .is_some_and(|(t0, max)| t0.elapsed() >= max))
            {
                break;
            }
            n += 1;
            let tick = self.tick;
            self.tick += 1;
            let mut obs: Observation = P::observe(state);
            obs.food = 20;
            P::feed(ctrl.live_mut(), state);
            let want = ctrl.aim(&obs, &view);
            let look = self.turner.step(obs.yaw, obs.pitch, want);
            let cmd = ctrl.keys(&obs, look, &view);
            report.reached = ctrl.reached();
            match ctrl.progress() {
                Progress::Arrived => {
                    report.outcome = SimOutcome::Arrived;
                    done = true;
                    break;
                }
                Progress::Stuck => {
                    report.outcome = SimOutcome::Stuck;
                    done = true;
                    break;
                }
                Progress::OffPath => {
                    report.outcome = SimOutcome::OffPath;
                    done = true;
                    break;
                }
                Progress::Moving | Progress::Working => {}
            }
            // Jobs: a dig or place takes a while, then the block changes.
            // A place counts only where a client could make it: the face in sight and reach,
            // the new block clear of the player.
            let place = cmd.place.filter(|p| place_ok(&obs, *p));
            let target = cmd.dig.or(place.map(|p| p.pos)).or(cmd.interact);
            match (target, self.job) {
                (Some(p), Some((q, k))) if p == q => {
                    let need = match (cmd.dig, cmd.interact) {
                        (Some(d), _) => {
                            5 + (f64::from(class(&view, d[0], d[1], d[2]).hardness.max(0.0)) * 30.0)
                                as u32
                        }
                        (None, Some(_)) => 2,
                        // A place in the air (towering) is answered as fast as the server can.
                        (None, None) if !obs.on_ground => 2,
                        (None, None) => 4,
                    };
                    if k + 1 >= need {
                        if cmd.interact.is_some() && cmd.dig.is_none() && place.is_none() {
                            for (q, raw) in door::toggle(self.version, &view, p).unwrap_or_default()
                            {
                                view.set(q, raw);
                            }
                        } else {
                            let raw = if cmd.dig.is_some() {
                                0
                            } else {
                                stone_raw(self.version)
                            };
                            view.set(p, raw);
                        }
                        self.job = None;
                    } else {
                        self.job = Some((q, k + 1));
                    }
                }
                (Some(p), _) => self.job = Some((p, 0)),
                (None, _) => self.job = None,
            }
            let keys = cmd.keys;
            let kind = ctrl
                .path()
                .steps
                .get(ctrl.reached() + 1)
                .map_or(MoveKind::Start, |s| s.kind);
            let fall_before = P::fall_distance(state);
            P::tick(state, keys, look.0, look.1, &view);
            let o = P::observe(state);
            report.ticks = tick + 1;
            report.end = o.pos;
            report.trace.push(o.pos);
            if o.on_ground {
                report.max_fall = report.max_fall.max(fall_before);
            }
            if P::horizontal_collision(state)
                && matches!(
                    kind,
                    MoveKind::Walk
                        | MoveKind::Diagonal
                        | MoveKind::Descend
                        | MoveKind::Parkour { .. }
                )
            {
                report.collisions += 1;
                report.first_collision.get_or_insert(tick);
            }
            // Hazards in the body's column.
            let (x, z) = (o.pos[0].floor() as i32, o.pos[2].floor() as i32);
            let y0 = o.pos[1].floor() as i32;
            let y1 = (o.pos[1] + 1.79).floor() as i32;
            if (y0..=y1).any(|y| class(&view, x, y, z).has(flag::LAVA | flag::AVOID)) {
                report.outcome = SimOutcome::Hazard;
                done = true;
            }
        }
        self.overrides = view.overrides;
        done
    }
}

#[cfg(all(
    test,
    feature = "client",
    feature = "v1_8",
    feature = "v1_12_2",
    feature = "v1_16_5"
))]
mod tests {
    use super::Turner;

    /// The simulator turns exactly like ghast-client.
    #[test]
    fn turner_matches_the_client_rotation() {
        let mut r = ghast_client::rotation::Rotation::new(0.5);
        let mut t = Turner::default();
        let (mut yaw, mut pitch) = (12.5_f32, 3.0_f32);
        let targets = [
            (100.0, 40.0),
            (-170.0, -80.0),
            (365.0, 0.0),
            (12.3, 1.1),
            (-45.0, 89.9),
        ];
        let (mut y2, mut p2) = (yaw, pitch);
        for (i, &(ty, tp)) in targets.iter().cycle().take(200).enumerate() {
            if i % 7 == 0 {
                r.set_target(ty, tp);
            }
            let target = r.target();
            let a = r.step(yaw, pitch);
            let b = t.step(y2, p2, target);
            assert_eq!(a, b, "tick {i}");
            (yaw, pitch) = a;
            (y2, p2) = b;
        }
    }
}