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
//! Walking along planned paths (feature `pathfinder`).
use ghast_client::{Buttons, Controls};
use ghast_pathfinder::{
Config, FollowConfig, FollowError, FollowStatus, Follower, Goal, Pathfinder,
};
use crate::{Bot, Error};
/// Releases the keys and buttons when a walk ends early.
struct Release(Option<Bot>);
impl Drop for Release {
fn drop(&mut self) {
if let Some(bot) = self.0.take() {
bot.act(|ctx| {
ctx.set_controls(Controls::NONE);
if ctx.buttons().left {
ctx.set_buttons(Buttons::default());
}
});
}
}
}
impl Bot {
/// Walks to `goal` along a path planned by [`ghast_pathfinder`] with the default
/// [`Config`] (sprinting, gap jumps, swimming, ladders; no digging or placing) and
/// followed by its [`Follower`]: every plan is first run through the vanilla-exact
/// physics, moves the simulation fails are planned around, and the follower replans when
/// it gets stuck or leaves the path.
///
/// Fails with [`Error::NoPath`] when no path exists (or the start is not loaded),
/// [`Error::Stuck`] after too many replans, and the usual interrupts (teleport / death,
/// see [`crate::Ext::interrupt_on_teleport`]). Unloaded terrain is never treated as air:
/// a goal beyond it is approached through partial paths. Dropping the future releases
/// the keys.
///
/// A long plan is spread over several ticks (by default at most 2000 nodes and 5 ms of
/// planning per tick, see [`FollowConfig::budget`]); the bot stands still meanwhile and
/// the client keeps sending its movement packet every tick.
///
/// ```no_run
/// use ghast_ext::pathfinder::Goal;
/// # async fn f(bot: ghast_ext::Bot) -> Result<(), ghast_ext::Error> {
/// bot.goto(Goal::Block([20, 64, -5])).await?;
/// bot.goto(Goal::Near { pos: [0, 64, 0], radius: 3.0 }).await?;
/// # Ok(()) }
/// ```
pub async fn goto(&self, goal: Goal) -> Result<(), Error> {
let follower = Follower::new(Pathfinder::new(Config::default()), FollowConfig::default());
self.goto_with(follower, goal).await
}
/// [`Bot::goto`] with a configured [`Follower`] (planner options such as digging or
/// placing, controller options, replan budget).
///
/// ```no_run
/// use ghast_ext::pathfinder::{Config, DigCost, FollowConfig, Follower, Goal, Pathfinder};
/// # async fn f(bot: ghast_ext::Bot) -> Result<(), ghast_ext::Error> {
/// let pf = Pathfinder::new(Config::default().with_dig(Some(DigCost::default())));
/// let follower = Follower::new(pf, FollowConfig::default());
/// bot.goto_with(follower, Goal::Xz { x: 100, z: 40 }).await?;
/// # Ok(()) }
/// ```
pub async fn goto_with(&self, mut follower: Follower, goal: Goal) -> Result<(), Error> {
let mark = self.mark();
let mut release = Release(Some(self.clone()));
follower.goto(goal);
loop {
let (f, status) = self
.with(move |ctx| {
let mut f = follower;
let s = f.tick(ctx);
(f, s)
})
.await?;
follower = f;
match status {
FollowStatus::Arrived => {
release.0 = None;
return Ok(());
}
FollowStatus::Failed(FollowError::Plan(e)) => {
log::debug!("goto: {e}");
return Err(Error::NoPath);
}
FollowStatus::Failed(_) => return Err(Error::Stuck),
_ => {}
}
self.next_tick().await;
self.check(mark)?;
}
}
}