From 6eee543a3367d38a6f0e9bffb457a2bd76a8f9cc Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sat, 12 Nov 2022 23:54:05 -0600 Subject: Pathfinder (#25) Pathfinding is very much not done, but it works enough and I want to get this merged. TODO: fast replanning, goals that aren't a single node, falling moves (it should be able to play the dropper), parkour moves --- azalea/src/bot.rs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) mode change 100644 => 100755 azalea/src/bot.rs (limited to 'azalea/src/bot.rs') diff --git a/azalea/src/bot.rs b/azalea/src/bot.rs old mode 100644 new mode 100755 index 566ab1e7..961f093d --- a/azalea/src/bot.rs +++ b/azalea/src/bot.rs @@ -1,7 +1,8 @@ use crate::{Client, Event}; use async_trait::async_trait; +use azalea_core::Vec3; use parking_lot::Mutex; -use std::sync::Arc; +use std::{f64::consts::PI, sync::Arc}; #[derive(Default, Clone)] pub struct Plugin { @@ -14,20 +15,22 @@ pub struct State { } pub trait BotTrait { - fn jump(&self); + fn jump(&mut self); + fn look_at(&mut self, pos: &Vec3); } impl BotTrait for azalea_client::Client { /// Queue a jump for the next tick. - fn jump(&self) { - let player_lock = self.player.lock(); - let mut dimension_lock = self.dimension.lock(); - - let mut player_entity = player_lock - .entity_mut(&mut dimension_lock) - .expect("Player must exist"); + fn jump(&mut self) { + self.set_jumping(true); + let state = self.plugins.get::().unwrap().state.clone(); + *state.jumping_once.lock() = true; + } - player_entity.jumping = true; + /// Turn the bot's head to look at the coordinate in the world. + fn look_at(&mut self, pos: &Vec3) { + let (y_rot, x_rot) = direction_looking_at(self.entity().pos(), pos); + self.set_rotation(y_rot, x_rot); } } @@ -38,10 +41,18 @@ impl crate::Plugin for Plugin { if *self.state.jumping_once.lock() { if bot.jumping() { *self.state.jumping_once.lock() = false; - } else { - bot.set_jumping(true); + bot.set_jumping(false); } } } } } + +fn direction_looking_at(current: &Vec3, target: &Vec3) -> (f32, f32) { + // borrowed from mineflayer's Bot.lookAt because i didn't want to do math + let delta = target - current; + let y_rot = (PI - f64::atan2(-delta.x, -delta.z)) * (180.0 / PI); + let ground_distance = f64::sqrt(delta.x * delta.x + delta.z * delta.z); + let x_rot = f64::atan2(delta.y, ground_distance) * -(180.0 / PI); + (y_rot as f32, x_rot as f32) +} -- cgit v1.2.3