Skip to content

Commit

Permalink
remove BlockPosGoal::from and Goal::goal_node
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Sep 30, 2023
1 parent 4b9499a commit 3f62ff1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
2 changes: 1 addition & 1 deletion azalea/examples/steal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<(
bot.chat("No chest found");
return Ok(());
};
// bot.goto(BlockPosGoal::from(chest_block));
// bot.goto(BlockPosGoal(chest_block));
let Some(chest) = bot.open_container(chest_block).await else {
println!("Couldn't open chest");
return Ok(());
Expand Down
8 changes: 4 additions & 4 deletions azalea/examples/testbot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
let entity_pos = bot.entity_component::<Position>(entity);
let target_pos: BlockPos = entity_pos.into();
println!("going to {target_pos:?}");
bot.goto(BlockPosGoal::from(target_pos));
bot.goto(BlockPosGoal(target_pos));
}
"worldborder" => {
bot.goto(BlockPosGoal::from(BlockPos::new(30_000_000, 70, 0)));
bot.goto(BlockPosGoal(BlockPos::new(30_000_000, 70, 0)));
}
"look" => {
let Some(entity) = entity else {
Expand Down Expand Up @@ -176,7 +176,7 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
);
if let Some(target_pos) = target_pos {
// +1 to stand on top of the block
bot.goto(BlockPosGoal::from(target_pos.up(1)));
bot.goto(BlockPosGoal(target_pos.up(1)));
} else {
bot.chat("no diamond block found");
}
Expand Down Expand Up @@ -205,7 +205,7 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
bot.chat("no lever found");
return Ok(());
};
bot.goto(BlockPosGoal::from(target_pos));
bot.goto(BlockPosGoal(target_pos));
bot.look_at(target_pos.center());
bot.block_interact(target_pos);
}
Expand Down
21 changes: 5 additions & 16 deletions azalea/src/pathfinder/goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@ use azalea_core::BlockPos;

use super::Goal;

pub struct BlockPosGoal {
pub pos: BlockPos,
}
pub struct BlockPosGoal(pub BlockPos);
impl Goal for BlockPosGoal {
fn heuristic(&self, n: BlockPos) -> f32 {
let dx = (self.pos.x - n.x) as f32;
let dy = (self.pos.y - n.y) as f32;
let dz = (self.pos.z - n.z) as f32;
let dx = (self.0.x - n.x) as f32;
let dy = (self.0.y - n.y) as f32;
let dz = (self.0.z - n.z) as f32;
dx * dx + dy * dy + dz * dz
}
fn success(&self, n: BlockPos) -> bool {
n == self.pos
}
fn goal_node(&self) -> BlockPos {
self.pos
}
}

impl From<BlockPos> for BlockPosGoal {
fn from(pos: BlockPos) -> Self {
Self { pos }
n == self.0
}
}
10 changes: 3 additions & 7 deletions azalea/src/pathfinder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl PathfinderClientExt for azalea_client::Client {
/// # use azalea::prelude::*;
/// # use azalea::{BlockPos, pathfinder::goals::BlockPosGoal};
/// # fn example(bot: &Client) {
/// bot.goto(BlockPosGoal::from(BlockPos::new(0, 70, 0)));
/// bot.goto(BlockPosGoal(BlockPos::new(0, 70, 0)));
/// # }
/// ```
fn goto(&self, goal: impl Goal + Send + Sync + 'static) {
Expand Down Expand Up @@ -160,13 +160,12 @@ fn goto_listener(
let world_lock = instance_container
.get(instance_name)
.expect("Entity tried to pathfind but the entity isn't in a valid world");
let end = event.goal.goal_node();

let goal = event.goal.clone();
let entity = event.entity;

let task = thread_pool.spawn(async move {
debug!("start: {start:?}, end: {end:?}");
debug!("start: {start:?}");

let successors = |pos: BlockPos| {
let world = world_lock.read();
Expand Down Expand Up @@ -477,9 +476,6 @@ fn stop_pathfinding_on_instance_change(
pub trait Goal {
fn heuristic(&self, n: BlockPos) -> f32;
fn success(&self, n: BlockPos) -> bool;
// TODO: this should be removed and mtdstarlite should stop depending on
// being given a goal node
fn goal_node(&self) -> BlockPos;
}

/// Returns whether the entity is at the node and should start going to the
Expand Down Expand Up @@ -563,7 +559,7 @@ mod tests {

simulation.app.world.send_event(GotoEvent {
entity: simulation.entity,
goal: Arc::new(BlockPosGoal::from(end_pos)),
goal: Arc::new(BlockPosGoal(end_pos)),
});
simulation
}
Expand Down

0 comments on commit 3f62ff1

Please sign in to comment.