1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

add Bot::look_at

This commit is contained in:
Ubuntu 2022-10-28 18:24:15 +00:00
parent d888c0569b
commit f7c8d57b68
7 changed files with 73 additions and 26 deletions

1
Cargo.lock generated
View file

@ -105,6 +105,7 @@ dependencies = [
"anyhow",
"async-trait",
"azalea-client",
"azalea-core",
"azalea-protocol",
"env_logger",
"parking_lot 0.12.1",

View file

@ -235,6 +235,14 @@ impl Client {
player_entity.jumping
}
/// Sets your rotation. `y_rot` is pitch (looking up and down), `x_rot` is
/// yaw (looking to the side). You can get these numbers from the vanilla
/// f3 screen.
pub fn set_rotation(&mut self, y_rot: f32, x_rot: f32) {
let mut player_entity = self.entity_mut();
player_entity.set_rotation(y_rot, x_rot);
}
}
#[derive(Clone, Copy, Debug, Default)]

View file

@ -15,7 +15,7 @@ pub struct AABB {
}
pub struct ClipPointOpts<'a> {
pub t: &'a mut [f64],
pub t: &'a mut f64,
pub approach_dir: Option<Direction>,
pub delta: &'a Vec3,
pub begin: f64,
@ -225,13 +225,10 @@ impl AABB {
}
pub fn clip(&self, min: &Vec3, max: &Vec3) -> Option<Vec3> {
let mut t = [1.0];
let x = max.x - min.x;
let y = max.y - min.y;
let z = max.z - min.z;
let _dir = self.get_direction(self, min, &mut t, None, &Vec3 { x, y, z })?;
let t = t[0];
Some(min.offset(t * x, t * y, t * z))
let mut t = 1.0;
let delta = max - min;
let _dir = self.get_direction(self, min, &mut t, None, &delta)?;
Some(min + &(delta * t))
}
pub fn clip_iterable(
@ -241,19 +238,16 @@ impl AABB {
to: &Vec3,
pos: &BlockPos,
) -> Option<BlockHitResult> {
let mut t = [1.0];
let mut t = 1.0;
let mut dir = None;
let x = to.x - from.x;
let y = to.y - from.y;
let z = to.z - from.z;
let delta = to - from;
for aabb in boxes {
dir = self.get_direction(aabb, from, &mut t, dir, &Vec3 { x, y, z });
dir = self.get_direction(aabb, from, &mut t, dir, &delta);
}
let dir = dir?;
let t = t[0];
Some(BlockHitResult {
location: from.offset(t * x, t * y, t * z),
location: from + &(delta * t),
direction: dir,
block_pos: *pos,
inside: false,
@ -265,7 +259,7 @@ impl AABB {
&self,
aabb: &AABB,
from: &Vec3,
t: &mut [f64],
t: &mut f64,
dir: Option<Direction>,
delta: &Vec3,
) -> Option<Direction> {
@ -393,13 +387,13 @@ impl AABB {
let t_y = (opts.start.y + t_x) / opts.delta.y;
let t_z = (opts.start.z + t_x) / opts.delta.z;
if 0.0 < t_x
&& t_x < opts.t[0]
&& t_x < *opts.t
&& opts.min_x - EPSILON < t_y
&& t_y < opts.max_x + EPSILON
&& opts.min_z - EPSILON < t_z
&& t_z < opts.max_z + EPSILON
{
opts.t[0] = t_x;
*opts.t = t_x;
Some(opts.result_dir)
} else {
opts.approach_dir

View file

@ -2,7 +2,7 @@ use crate::ResourceLocation;
use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
use std::{
io::{Cursor, Write},
ops::{Add, AddAssign, Rem},
ops::{Add, AddAssign, Mul, Rem, Sub},
};
macro_rules! vec3_impl {
@ -11,17 +11,13 @@ macro_rules! vec3_impl {
pub fn new(x: $type, y: $type, z: $type) -> Self {
Self { x, y, z }
}
pub fn offset(&self, x: $type, y: $type, z: $type) -> Self {
Self {
x: self.x + x,
y: self.y + y,
z: self.z + z,
}
}
pub fn length_sqr(&self) -> $type {
self.x * self.x + self.y * self.y + self.z * self.z
}
/// Return a new instance of this position with the y coordinate
/// decreased by the given number.
pub fn down(&self, y: $type) -> Self {
Self {
x: self.x,
@ -29,6 +25,8 @@ macro_rules! vec3_impl {
z: self.z,
}
}
/// Return a new instance of this position with the y coordinate
/// increased by the given number.
pub fn up(&self, y: $type) -> Self {
Self {
x: self.x,
@ -76,6 +74,38 @@ macro_rules! vec3_impl {
}
}
}
impl Sub for &$name {
type Output = $name;
/// Find the difference between two positions.
fn sub(self, other: Self) -> Self::Output {
Self::Output {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
}
}
}
impl Sub for $name {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
(&self).sub(&other)
}
}
impl Mul<$type> for $name {
type Output = Self;
fn mul(self, multiplier: $type) -> Self::Output {
Self {
x: self.x * multiplier,
y: self.y * multiplier,
z: self.z * multiplier,
}
}
}
};
}

View file

@ -74,6 +74,7 @@ impl Trait for azalea_client::Client {
fn execute_path(&self, path: &Vec<Node>) {
let start = path[0];
// self.entity_mut().set_rotation(y_rot, x_rot)
// self.look_at(start.pos);
}
}

View file

@ -12,6 +12,7 @@ anyhow = "^1.0.65"
async-trait = "^0.1.57"
azalea-client = { version = "0.2.2", path = "../azalea-client" }
azalea-protocol = { version = "0.2.0", path = "../azalea-protocol" }
azalea-core = { version = "0.2.0", path = "../azalea-core" }
parking_lot = "^0.12.1"
thiserror = "^1.0.37"
tokio = "^1.21.1"

View file

@ -1,5 +1,6 @@
use crate::{Client, Event};
use async_trait::async_trait;
use azalea_core::Vec3;
use parking_lot::Mutex;
use std::sync::Arc;
@ -15,6 +16,7 @@ pub struct State {
pub trait BotTrait {
fn jump(&self);
fn look_at(&mut self, pos: &Vec3);
}
impl BotTrait for azalea_client::Client {
@ -29,6 +31,16 @@ impl BotTrait for azalea_client::Client {
player_entity.jumping = true;
}
/// Turn the bot's head to look at the coordinate in the world.
fn look_at(&mut self, pos: &Vec3) {
// borrowed from mineflayer's Bot.lookAt because i didn't want to do math
let delta = self.entity().pos() - pos;
let x_rot = f64::atan2(-delta.x, -delta.z);
let ground_distance = f64::sqrt(delta.x * delta.x + delta.z * delta.z);
let y_rot = f64::atan2(delta.y, ground_distance);
self.set_rotation(y_rot as f32, x_rot as f32);
}
}
#[async_trait]