ghast-pathfinder 0.2.3

Physics-exact path planning and path following for the ghast Minecraft Java Edition bot library (1.8.9 and 1.16.5): 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
//! Following a path from a `ghast_client::Behavior`: plan, follow, replan.

use std::collections::HashSet;
use std::time::{Duration, Instant};

use ghast_client::{BlockPos, Buttons, Controls, Face, TickContext};

use crate::control::{Controller, ControllerConfig, Progress};
use crate::goal::Goal;
use crate::physics::Observation;
use crate::plan::{Budget, Path, Pathfinder, PlanError, Search};
use crate::sim::{SimOutcome, Simulation};

/// Options of the [`Follower`]. Build with [`FollowConfig::default`] and the `with_*`
/// methods.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct FollowConfig {
    /// The controller's options.
    pub controller: ControllerConfig,
    /// Replans after a stuck / off-path move or a blocked partial path before giving up.
    /// Default 8. Teleports replan without counting.
    pub max_replans: u32,
    /// Simulate each plan in `ghast-physics` before moving and plan around moves the
    /// simulation fails (see [`Pathfinder::plan_verified`]). Default on.
    pub verify: bool,
    /// Planning work per game tick: the search expands at most `budget.nodes` nodes and
    /// planning plus verification stop after `budget.time`. Default 2000 nodes and 5 ms
    /// (about 4 ms of A* on a desktop core), so a long plan spreads over several ticks
    /// while the player stands still.
    pub budget: Budget,
    /// Most simulated ticks of the verification per game tick (also bounded by
    /// `budget.time`). Default 400.
    pub verify_ticks: u32,
}

impl Default for FollowConfig {
    fn default() -> Self {
        FollowConfig {
            controller: ControllerConfig::default(),
            max_replans: 8,
            verify: true,
            budget: Budget::nodes(2000).with_time(Duration::from_millis(5)),
            verify_ticks: 400,
        }
    }
}

impl FollowConfig {
    /// The controller's options.
    #[must_use]
    pub fn with_controller(mut self, c: ControllerConfig) -> Self {
        self.controller = c;
        self
    }

    /// Replan budget.
    #[must_use]
    pub fn with_max_replans(mut self, n: u32) -> Self {
        self.max_replans = n;
        self
    }

    /// Simulated verification of plans on or off.
    #[must_use]
    pub fn with_verify(mut self, on: bool) -> Self {
        self.verify = on;
        self
    }

    /// Planning work per game tick ([`Budget::UNLIMITED`]: plan in one tick).
    #[must_use]
    pub fn with_budget(mut self, budget: Budget) -> Self {
        self.budget = budget;
        self
    }

    /// Simulated verification ticks per game tick.
    #[must_use]
    pub fn with_verify_ticks(mut self, ticks: u32) -> Self {
        self.verify_ticks = ticks.max(1);
        self
    }
}

/// Why following failed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FollowError {
    /// Planning failed.
    Plan(PlanError),
    /// The replan budget ([`FollowConfig::max_replans`]) is used up.
    GaveUp,
}

impl std::fmt::Display for FollowError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FollowError::Plan(e) => write!(f, "planning failed: {e}"),
            FollowError::GaveUp => f.write_str("gave up after too many replans"),
        }
    }
}

impl std::error::Error for FollowError {}

/// What the [`Follower`] is doing.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FollowStatus {
    /// No goal.
    Idle,
    /// Walking (or digging / placing) toward the goal.
    Moving,
    /// The goal is reached; keys released.
    Arrived,
    /// Following failed; keys released.
    Failed(FollowError),
}

/// Plans and follows a path from a behaviour's tick: call [`Follower::tick`] once per tick from
/// `Behavior::tick`. It sets the aim, the movement keys and (for digs) the left button
/// through the [`TickContext`], so the client's rules hold: rotation in whole mouse counts,
/// actions before the tick's single movement packet, digs / places only with the sent
/// crosshair on the block.
///
/// It replans after a teleport, when the controller gets stuck or leaves the path (that move
/// is avoided from then on), and at the end of a partial path (goal beyond loaded terrain).
/// Digging and placing follow [`crate::Config::dig`] / [`crate::Config::place`]; for places
/// the behaviour keeps a block in the hand.
///
/// ```no_run
/// use ghast_client::{Behavior, TickContext};
/// use ghast_pathfinder::{Config, FollowConfig, FollowStatus, Follower, Goal, Pathfinder};
///
/// struct GoThere(Follower);
///
/// impl Behavior for GoThere {
///     fn tick(&mut self, ctx: &mut TickContext<'_>) {
///         match self.0.tick(ctx) {
///             FollowStatus::Arrived => ctx.disconnect("arrived"),
///             FollowStatus::Failed(e) => ctx.disconnect(format!("failed: {e}")),
///             _ => {}
///         }
///     }
/// }
///
/// let mut f = Follower::new(Pathfinder::new(Config::default()), FollowConfig::default());
/// f.goto(Goal::Block([20, 64, 5]));
/// let mut behaviour = GoThere(f);
/// ```
#[derive(Clone, Debug)]
pub struct Follower {
    pf: Pathfinder,
    cfg: FollowConfig,
    goal: Option<Goal>,
    ctrl: Option<Controller>,
    status: FollowStatus,
    replans: u32,
    banned: HashSet<([i32; 3], [i32; 3])>,
    /// Where the last partial path ended (no progress beyond it = blocked).
    partial_end: Option<[i32; 3]>,
    /// Ticks to wait after a place was sent.
    place_wait: u32,
    digging: bool,
    plans: u64,
    /// A plan in progress (spread over ticks).
    planning: Option<Planning>,
    /// Ticks the current (or last) plan took.
    plan_ticks: u32,
}

/// A plan in progress: the search, then the simulated check.
#[derive(Clone, Debug)]
enum Planning {
    Search {
        search: Box<Search>,
        /// Moves the verification failed (on top of [`Follower::banned`]).
        bans: HashSet<([i32; 3], [i32; 3])>,
        attempt: u32,
        /// The previous attempt's path (followed if a later search fails).
        last: Option<Path>,
    },
    Verify {
        sim: Box<Simulation>,
        bans: HashSet<([i32; 3], [i32; 3])>,
        attempt: u32,
    },
}

/// Plans per goal and replan: the first one and two repairs after failed simulations.
const VERIFY_ATTEMPTS: u32 = 3;

impl Follower {
    /// A follower without a goal.
    #[must_use]
    pub fn new(pathfinder: Pathfinder, cfg: FollowConfig) -> Self {
        Follower {
            pf: pathfinder,
            cfg,
            goal: None,
            ctrl: None,
            status: FollowStatus::Idle,
            replans: 0,
            banned: HashSet::new(),
            partial_end: None,
            place_wait: 0,
            digging: false,
            plans: 0,
            planning: None,
            plan_ticks: 0,
        }
    }

    /// Sets a new goal (planned on the next [`Follower::tick`]).
    pub fn goto(&mut self, goal: Goal) {
        self.goal = Some(goal);
        self.ctrl = None;
        self.planning = None;
        self.status = FollowStatus::Moving;
        self.replans = 0;
        self.banned.clear();
        self.partial_end = None;
    }

    /// Drops the goal and releases the keys and buttons (in this tick).
    pub fn stop(&mut self, ctx: &mut TickContext<'_>) {
        self.goal = None;
        self.ctrl = None;
        self.planning = None;
        self.status = FollowStatus::Idle;
        self.release(ctx);
    }

    /// The goal.
    #[must_use]
    pub fn goal(&self) -> Option<&Goal> {
        self.goal.as_ref()
    }

    /// The current state.
    #[must_use]
    pub fn status(&self) -> FollowStatus {
        self.status
    }

    /// The path being followed.
    #[must_use]
    pub fn path(&self) -> Option<&crate::Path> {
        self.ctrl.as_ref().map(Controller::path)
    }

    /// Plans made since the follower was created (for logs and tests).
    #[must_use]
    pub fn plans(&self) -> u64 {
        self.plans
    }

    /// A plan is in progress: the player stands still (keys released, no turning) and the
    /// client keeps sending its normal movement packet every tick.
    #[must_use]
    pub fn is_planning(&self) -> bool {
        self.planning.is_some()
    }

    /// Ticks the current (or the last) plan took, verification included (1 = planned within
    /// the tick it started).
    #[must_use]
    pub fn plan_ticks(&self) -> u32 {
        self.plan_ticks
    }

    /// The planner.
    #[must_use]
    pub fn pathfinder(&self) -> &Pathfinder {
        &self.pf
    }

    fn release(&mut self, ctx: &mut TickContext<'_>) {
        ctx.set_controls(Controls::NONE);
        if self.digging || ctx.buttons().left {
            ctx.set_buttons(Buttons::default());
        }
        self.digging = false;
    }

    fn fail(&mut self, ctx: &mut TickContext<'_>, e: FollowError) -> FollowStatus {
        self.ctrl = None;
        self.planning = None;
        self.status = FollowStatus::Failed(e);
        self.release(ctx);
        self.status
    }

    /// Starts a plan from the player's feet block.
    fn start_plan(&mut self, ctx: &TickContext<'_>) {
        let goal = self.goal.clone().expect("planning without a goal");
        let p = ctx.pose().pos;
        let start = Pathfinder::start_block([p.x, p.y, p.z]);
        let version = crate::Version::from(ctx.version());
        self.plans += 1;
        self.plan_ticks = 0;
        self.planning = Some(Planning::Search {
            search: Box::new(self.pf.search(version, start, goal, self.banned.clone())),
            bans: HashSet::new(),
            attempt: 0,
            last: None,
        });
    }

    /// This tick's share of the plan in progress: the controller once the path is ready.
    fn work(&mut self, ctx: &TickContext<'_>) -> Result<Option<Controller>, PlanError> {
        self.plan_ticks += 1;
        let t0 = Instant::now();
        let left = |budget: Option<Duration>| budget.map(|b| b.saturating_sub(t0.elapsed()));
        loop {
            if let Some(max) = self.cfg.budget.time
                && t0.elapsed() >= max
            {
                return Ok(None);
            }
            let Some(phase) = self.planning.take() else {
                return Ok(None);
            };
            match phase {
                Planning::Search {
                    mut search,
                    bans,
                    attempt,
                    last,
                } => {
                    let budget = Budget {
                        nodes: self.cfg.budget.nodes,
                        time: left(self.cfg.budget.time),
                    };
                    match search.step(ctx.world(), budget) {
                        None => {
                            self.planning = Some(Planning::Search {
                                search,
                                bans,
                                attempt,
                                last,
                            });
                            return Ok(None);
                        }
                        Some(Err(e)) => match last {
                            // A repair failed: follow the previous path after all.
                            Some(p) => return Ok(Some(self.ready(p))),
                            None => return Err(e),
                        },
                        Some(Ok(path)) => {
                            if !self.cfg.verify {
                                return Ok(Some(self.ready(path)));
                            }
                            let sim = Simulation::new(&path, &self.cfg.controller);
                            self.planning = Some(Planning::Verify {
                                sim: Box::new(sim),
                                bans,
                                attempt,
                            });
                        }
                    }
                }
                Planning::Verify {
                    mut sim,
                    mut bans,
                    attempt,
                } => {
                    let Some((outcome, reached)) = sim
                        .step(
                            ctx.world(),
                            self.cfg.verify_ticks,
                            left(self.cfg.budget.time),
                        )
                        .map(|r| (r.outcome, r.reached))
                    else {
                        self.planning = Some(Planning::Verify { sim, bans, attempt });
                        return Ok(None);
                    };
                    let path = sim.path();
                    if outcome == SimOutcome::Arrived {
                        let path = path.clone();
                        return Ok(Some(self.ready(path)));
                    }
                    // Plan around the move the simulation failed.
                    let k = (reached + 1).min(path.steps.len() - 1);
                    let m = (path.steps[k.saturating_sub(1)].pos, path.steps[k].pos);
                    log::debug!("pathfinder: simulation {outcome:?} on move {m:?}");
                    if k == 0 || attempt + 1 >= VERIFY_ATTEMPTS || !bans.insert(m) {
                        let path = path.clone();
                        return Ok(Some(self.ready(path)));
                    }
                    let goal = self.goal.clone().expect("planning without a goal");
                    let mut all = self.banned.clone();
                    all.extend(bans.iter().copied());
                    let search = self.pf.search(path.version, path.steps[0].pos, goal, all);
                    self.planning = Some(Planning::Search {
                        search: Box::new(search),
                        bans,
                        attempt: attempt + 1,
                        last: Some(path.clone()),
                    });
                }
            }
        }
    }

    fn ready(&mut self, path: Path) -> Controller {
        log::debug!(
            "pathfinder: {} steps, cost {:.0} ticks, {} nodes{}, planned in {} ticks",
            path.steps.len(),
            path.cost,
            path.expanded,
            if path.partial { ", partial" } else { "" },
            self.plan_ticks
        );
        self.planning = None;
        Controller::new(path, self.cfg.controller.clone())
    }

    /// One tick: plans when needed, then aims and sets the keys (and digs / places). Returns
    /// the status after this tick.
    pub fn tick(&mut self, ctx: &mut TickContext<'_>) -> FollowStatus {
        if self.goal.is_none() || !matches!(self.status, FollowStatus::Moving) {
            return self.status;
        }
        if !ctx.events().teleports.is_empty() || ctx.events().respawned {
            self.ctrl = None;
            self.planning = None;
        }
        if ctx.is_dead() {
            self.release(ctx);
            return self.status;
        }
        if self.ctrl.is_none() {
            if self.planning.is_none() {
                self.start_plan(ctx);
            }
            match self.work(ctx) {
                Ok(Some(c)) => self.ctrl = Some(c),
                Ok(None) => {
                    // Still planning: stand still; the movement packet goes out as usual.
                    self.release(ctx);
                    return self.status;
                }
                Err(e) => return self.fail(ctx, FollowError::Plan(e)),
            }
        }
        let pose = *ctx.pose();
        let obs = Observation {
            pos: [pose.pos.x, pose.pos.y, pose.pos.z],
            vel: [pose.motion.x, pose.motion.y, pose.motion.z],
            on_ground: pose.on_ground,
            yaw: pose.yaw,
            pitch: pose.pitch,
            sprinting: pose.sprinting,
            food: ctx.health().food,
        };
        let ctrl = self.ctrl.as_mut().expect("planned above");
        let (yaw, pitch) = ctrl.aim(&obs, ctx.world());
        let done = matches!(
            ctrl.progress(),
            Progress::Arrived | Progress::Stuck | Progress::OffPath
        );
        if !done {
            ctx.set_aim(yaw, pitch);
        }
        let look = ctx.planned_look();
        let cmd = ctrl.keys(&obs, look, ctx.world());
        match ctrl.progress() {
            Progress::Arrived => {
                let path = ctrl.path();
                if !path.partial {
                    self.release(ctx);
                    self.status = FollowStatus::Arrived;
                    return self.status;
                }
                // The end of a partial path: plan the rest from here.
                let end = path.steps.last().map(|s| s.pos);
                if end.is_some() && end == self.partial_end {
                    self.replans += 1;
                }
                self.partial_end = end;
                self.ctrl = None;
                self.release(ctx);
                if self.replans > self.cfg.max_replans {
                    return self.fail(ctx, FollowError::GaveUp);
                }
                return self.status;
            }
            Progress::Stuck | Progress::OffPath => {
                if let Some(m) = ctrl.current_move() {
                    log::debug!(
                        "pathfinder: {:?} on move {m:?}, replanning",
                        ctrl.progress()
                    );
                    self.banned.insert(m);
                }
                self.ctrl = None;
                self.replans += 1;
                self.release(ctx);
                if self.replans > self.cfg.max_replans {
                    return self.fail(ctx, FollowError::GaveUp);
                }
                return self.status;
            }
            Progress::Moving | Progress::Working => {}
        }
        let k = cmd.keys;
        ctx.set_controls(Controls {
            forward: k.forward,
            back: k.back,
            left: k.left,
            right: k.right,
            jump: k.jump,
            sneak: k.sneak,
            sprint: k.sprint,
        });
        if let Some(d) = cmd.dig {
            let pos = BlockPos::new(d[0], d[1], d[2]);
            let on = ctx.target().is_some_and(|t| t.pos == pos);
            ctx.set_buttons(Buttons {
                left: on,
                right: false,
            });
            self.digging = true;
        } else if self.digging {
            ctx.set_buttons(Buttons::default());
            self.digging = false;
        }
        if let Some(p) = cmd.place {
            if self.place_wait > 0 {
                self.place_wait -= 1;
            } else {
                let against = BlockPos::new(p.against[0], p.against[1], p.against[2]);
                if let Some(hit) = ctx
                    .target()
                    .filter(|t| t.pos == against && t.face == Face::Up)
                    && ctx.place(against, Face::Up, hit.hit)
                {
                    self.place_wait = 10;
                }
            }
        } else {
            self.place_wait = 0;
        }
        self.status
    }
}