aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/bot.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-11-12 23:54:05 -0600
committerGitHub <noreply@github.com>2022-11-12 23:54:05 -0600
commit6eee543a3367d38a6f0e9bffb457a2bd76a8f9cc (patch)
treea5e493ccd7ec24293b8d866242c3836146517122 /azalea/src/bot.rs
parentfa57d03627aa20b1df44caed7cb025b6db1d9b53 (diff)
downloadazalea-drasl-6eee543a3367d38a6f0e9bffb457a2bd76a8f9cc.tar.xz
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
Diffstat (limited to 'azalea/src/bot.rs')
-rwxr-xr-x[-rw-r--r--]azalea/src/bot.rs35
1 files changed, 23 insertions, 12 deletions
diff --git a/azalea/src/bot.rs b/azalea/src/bot.rs
index 566ab1e7..961f093d 100644..100755
--- 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::<Plugin>().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)
+}