aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/pathfinder/mod.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-10-08 15:34:41 -0500
committermat <git@matdoes.dev>2023-10-08 15:34:41 -0500
commit0297b8aacee27d9e86cea781b3751591e32df401 (patch)
tree058fa11e12fc88dc705b97be933aa7e246575e7d /azalea/src/pathfinder/mod.rs
parentf10535b5c80ab5e4c7fe524c0e822352e45e8ce8 (diff)
downloadazalea-drasl-0297b8aacee27d9e86cea781b3751591e32df401.tar.xz
PathfinderCtx
Diffstat (limited to 'azalea/src/pathfinder/mod.rs')
-rw-r--r--azalea/src/pathfinder/mod.rs41
1 files changed, 23 insertions, 18 deletions
diff --git a/azalea/src/pathfinder/mod.rs b/azalea/src/pathfinder/mod.rs
index 0e7c021a..e92457b8 100644
--- a/azalea/src/pathfinder/mod.rs
+++ b/azalea/src/pathfinder/mod.rs
@@ -6,6 +6,7 @@ pub mod costs;
pub mod goals;
pub mod moves;
pub mod simulation;
+pub mod world;
use crate::bot::{JumpEvent, LookAtEvent};
use crate::pathfinder::astar::a_star;
@@ -20,6 +21,7 @@ use crate::ecs::{
system::{Commands, Query, Res},
};
use crate::pathfinder::moves::PathfinderCtx;
+use crate::pathfinder::world::CachedWorld;
use azalea_client::chat::SendChatEvent;
use azalea_client::movement::walk_listener;
use azalea_client::{StartSprintEvent, StartWalkEvent};
@@ -219,12 +221,8 @@ fn goto_listener(
let task = thread_pool.spawn(async move {
debug!("start: {start:?}");
- let ctx = PathfinderCtx::new(world_lock);
- let successors = |pos: BlockPos| {
- let mut edges = Vec::with_capacity(16);
- successors_fn(&mut edges, &ctx, pos);
- edges
- };
+ let cached_world = CachedWorld::new(world_lock);
+ let successors = |pos: BlockPos| call_successors_fn(&cached_world, successors_fn, pos);
let mut attempt_number = 0;
@@ -332,12 +330,9 @@ fn path_found_listener(
.get(instance_name)
.expect("Entity tried to pathfind but the entity isn't in a valid world");
let successors_fn: moves::SuccessorsFn = event.successors_fn;
- let ctx = PathfinderCtx::new(world_lock);
- let successors = |pos: BlockPos| {
- let mut edges = Vec::with_capacity(16);
- successors_fn(&mut edges, &ctx, pos);
- edges
- };
+ let cached_world = CachedWorld::new(world_lock);
+ let successors =
+ |pos: BlockPos| call_successors_fn(&cached_world, successors_fn, pos);
if let Some(first_node_of_new_path) = path.front() {
if successors(last_node_of_current_path.target)
@@ -521,12 +516,8 @@ fn check_for_path_obstruction(
.expect("Entity tried to pathfind but the entity isn't in a valid world");
// obstruction check (the path we're executing isn't possible anymore)
- let ctx = PathfinderCtx::new(world_lock);
- let successors = |pos: BlockPos| {
- let mut edges = Vec::with_capacity(16);
- successors_fn(&mut edges, &ctx, pos);
- edges
- };
+ let cached_world = CachedWorld::new(world_lock);
+ let successors = |pos: BlockPos| call_successors_fn(&cached_world, successors_fn, pos);
if let Some(obstructed_index) = check_path_obstructed(
executing_path.last_reached_node,
@@ -816,6 +807,20 @@ where
None
}
+pub fn call_successors_fn(
+ cached_world: &CachedWorld,
+ successors_fn: SuccessorsFn,
+ pos: BlockPos,
+) -> Vec<astar::Edge<BlockPos, moves::MoveData>> {
+ let mut edges = Vec::with_capacity(16);
+ let mut ctx = PathfinderCtx {
+ edges: &mut edges,
+ world: cached_world,
+ };
+ successors_fn(&mut ctx, pos);
+ edges
+}
+
#[cfg(test)]
mod tests {
use std::{collections::HashSet, sync::Arc};