pub struct Controller { /* private fields */ }Expand description
Follows a Path tick by tick.
Pure (no I/O, no clock): it reads an Observation of the player and the world, and
answers with an aim target and keys. Where a decision depends on the physics (when to
jump off an edge, when to let go of the keys to land on a block, whether a step would
leave the floor) it runs ghast-physics a few ticks ahead from the observation, so a
simulated run (crate::simulate) and a live run make the same decisions.
Each tick is two calls, because the client turns toward the aim in whole mouse counts and the keys must be chosen for the look the movement step will really use:
Controller::aim: advances along the path and returns the look to aim at;- the caller sets that aim and asks for the look this tick’s movement will use
(
TickContext::planned_lookin ghast-client); Controller::keys: the keys (and a dig / place job) for that look.
It never gives up silently: Controller::progress turns Progress::Stuck or
Progress::OffPath and Controller::current_move names the move, to plan around.
use ghast_pathfinder::{Config, Controller, ControllerConfig, Goal, Pathfinder, Progress, 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([5, 64, 0]))
.unwrap();
let mut c = Controller::new(path, ControllerConfig::default());
// Each tick, with `obs` the player's observation:
let (yaw, pitch) = c.aim(&obs, &world);
// ... the client turns toward (yaw, pitch); `look` is what this tick's movement uses.
let look = (yaw, pitch);
let cmd = c.keys(&obs, look, &world);
assert_eq!(c.progress(), Progress::Moving);
assert!(cmd.keys.forward || cmd.keys == ghast_physics::Keys::NONE);Implementations§
Source§impl Controller
impl Controller
Sourcepub fn new(path: Path, cfg: ControllerConfig) -> Self
pub fn new(path: Path, cfg: ControllerConfig) -> Self
A controller at the start of path.
Sourcepub fn set_modern_player(&mut self, player: &PlayerState)
pub fn set_modern_player(&mut self, player: &PlayerState)
26.1: the live client player (ghast_client::TickContext::modern_player), whose
attributes (movement speed, step height, jump strength, scale, …), effects, abilities,
input timers and build height the one-tick look-ahead and the pre-jump check simulate
with. Set it every tick before Controller::aim (the Follower does). Without it the
look-ahead uses vanilla defaults rebuilt from the Observation.
Sourcepub fn reached(&self) -> usize
pub fn reached(&self) -> usize
Index (into Path::steps) of the last step reached.
Sourcepub fn config(&self) -> &ControllerConfig
pub fn config(&self) -> &ControllerConfig
The configuration.
Sourcepub fn waypoint(&self) -> Option<[f64; 3]>
pub fn waypoint(&self) -> Option<[f64; 3]>
The point the controller walks to now: the centre of the next step’s feet block at its
feet height (None once the last step is reached).
Sourcepub fn current_move(&self) -> Option<([i32; 3], [i32; 3])>
pub fn current_move(&self) -> Option<([i32; 3], [i32; 3])>
The move (from, to feet blocks) the controller is on: after Progress::Stuck /
Progress::OffPath the move that failed, for Pathfinder::plan_avoiding.
Sourcepub fn aim<S: BlockSource + ?Sized>(
&mut self,
obs: &Observation,
world: &S,
) -> (f32, f32)
pub fn aim<S: BlockSource + ?Sized>( &mut self, obs: &Observation, world: &S, ) -> (f32, f32)
Advances along the path from obs and returns the look (yaw, pitch) to aim at this
tick. Call once per tick, before Controller::keys.
Sourcepub fn keys<S: BlockSource + ?Sized>(
&mut self,
obs: &Observation,
look: (f32, f32),
world: &S,
) -> Command
pub fn keys<S: BlockSource + ?Sized>( &mut self, obs: &Observation, look: (f32, f32), world: &S, ) -> Command
The keys (and dig / place job) for this tick, given the look the movement step will
use (look, after the client’s turn toward Controller::aim’s target).