refactor(client/movement): wrap common pathfinder goals

This commit is contained in:
Ryan 2025-02-16 21:26:54 -05:00
parent d6abd3fd06
commit 54c6b8c88f
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
2 changed files with 85 additions and 26 deletions

View File

@ -122,7 +122,6 @@ impl UserData for Client {
m.add_method_mut("attack", interaction::attack); m.add_method_mut("attack", interaction::attack);
m.add_method_mut("block_interact", interaction::block_interact); m.add_method_mut("block_interact", interaction::block_interact);
m.add_method_mut("goto", movement::goto); m.add_method_mut("goto", movement::goto);
m.add_method_mut("goto_without_mining", movement::goto_without_mining);
m.add_method_mut("jump", movement::jump); m.add_method_mut("jump", movement::jump);
m.add_method_mut("look_at", movement::look_at); m.add_method_mut("look_at", movement::look_at);
m.add_method_mut("set_direction", movement::set_direction); m.add_method_mut("set_direction", movement::set_direction);

View File

@ -1,39 +1,99 @@
use super::{Client, Vec3}; use super::{Client, Vec3};
use azalea::{ use azalea::{
BlockPos, SprintDirection, WalkDirection, pathfinder::goals::BlockPosGoal, prelude::*, BlockPos, BotClientExt, Client as AzaleaClient, SprintDirection, WalkDirection,
pathfinder::{
PathfinderClientExt,
goals::{BlockPosGoal, Goal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal},
},
}; };
use mlua::{Lua, Result}; use mlua::{FromLua, Lua, Result, Table, Value};
pub fn stop_pathfinding(_lua: &Lua, client: &Client, _: ()) -> Result<()> { pub fn stop_pathfinding(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
client.inner.as_ref().unwrap().stop_pathfinding(); client.inner.as_ref().unwrap().stop_pathfinding();
Ok(()) Ok(())
} }
pub fn goto(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> { pub fn goto(
#[allow(clippy::cast_possible_truncation)] lua: &Lua,
client client: &mut Client,
.inner (data, metadata): (Value, Option<Table>),
.as_ref() ) -> Result<()> {
.unwrap() fn g(client: &AzaleaClient, without_mining: bool, goal: impl Goal + Send + Sync + 'static) {
.goto(BlockPosGoal(BlockPos::new( if without_mining {
position.x as i32, client.goto_without_mining(goal);
position.y as i32, } else {
position.z as i32, client.goto(goal);
))); }
Ok(()) }
}
let error = mlua::Error::FromLuaConversionError {
from: data.type_name(),
to: "Table".to_string(),
message: None,
};
let client = client.inner.as_ref().unwrap();
let (goal_type, without_mining) = metadata
.map(|t| {
(
t.get("type").unwrap_or_default(),
t.get("without_mining").unwrap_or_default(),
)
})
.unwrap_or_default();
pub fn goto_without_mining(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> {
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
client match goal_type {
.inner 1 => {
.as_ref() let t = data.as_table().ok_or(error)?;
.unwrap() let p = Vec3::from_lua(t.get("position")?, lua)?;
.goto_without_mining(BlockPosGoal(BlockPos::new( g(
position.x as i32, client,
position.y as i32, without_mining,
position.z as i32, RadiusGoal {
))); pos: azalea::Vec3::new(p.x, p.y, p.z),
radius: t.get("radius")?,
},
);
}
2 => {
let p = Vec3::from_lua(data, lua)?;
g(
client,
without_mining,
ReachBlockPosGoal {
pos: BlockPos::new(p.x as i32, p.y as i32, p.z as i32),
chunk_storage: client.world().read().chunks.clone(),
},
);
}
3 => {
let t = data.as_table().ok_or(error)?;
g(
client,
without_mining,
XZGoal {
x: t.get("x")?,
z: t.get("z")?,
},
);
}
4 => g(
client,
without_mining,
YGoal {
y: data.as_integer().ok_or(error)?,
},
),
_ => {
let p = Vec3::from_lua(data, lua)?;
g(
client,
without_mining,
BlockPosGoal(BlockPos::new(p.x as i32, p.y as i32, p.z as i32)),
);
}
}
Ok(()) Ok(())
} }