diff --git a/azalea/src/pathfinder/astar.rs b/azalea/src/pathfinder/astar.rs
index 8069ae6a..d4812e9b 100644
--- a/azalea/src/pathfinder/astar.rs
+++ b/azalea/src/pathfinder/astar.rs
@@ -6,7 +6,7 @@ use std::{
time::{Duration, Instant},
};
-use log::{info, warn};
+use log::{trace, warn};
use priority_queue::PriorityQueue;
pub struct Path
@@ -74,7 +74,7 @@ where
.get(&neighbor.movement.target)
.map(|n| n.g_score)
.unwrap_or(f32::MAX);
- if tentative_g_score < neighbor_g_score {
+ if tentative_g_score - neighbor_g_score < MIN_IMPROVEMENT {
let heuristic = heuristic(neighbor.movement.target);
let f_score = tentative_g_score + heuristic;
nodes.insert(
@@ -101,20 +101,22 @@ where
if start_time.elapsed() > timeout {
// timeout, just return the best path we have so far
- info!("Pathfinder timeout");
+ trace!("A* couldn't find a path in time, returning best path");
break;
}
}
+ let best_path = determine_best_path(&best_paths, &heuristic);
+
Path {
- movements: reconstruct_path(nodes, determine_best_path(&best_paths, heuristic)),
+ movements: reconstruct_path(nodes, best_path),
partial: true,
}
}
const MIN_DISTANCE_PATH: f32 = 5.;
-fn determine_best_path
(best_node: &[P; 7], heuristic: HeuristicFn) -> P
+fn determine_best_path
(best_node: &[P; 7], heuristic: &HeuristicFn) -> P
where
HeuristicFn: Fn(P) -> f32,
P: Eq + Hash + Copy + Debug,
@@ -128,7 +130,7 @@ where
}
}
warn!("No best node found, returning first node");
- return best_node[0];
+ best_node[0]
}
fn reconstruct_path
(mut nodes: HashMap
>, current: P) -> Vec>
diff --git a/azalea/src/pathfinder/mod.rs b/azalea/src/pathfinder/mod.rs
index e2be4da1..890ef9df 100644
--- a/azalea/src/pathfinder/mod.rs
+++ b/azalea/src/pathfinder/mod.rs
@@ -30,10 +30,10 @@ use bevy_ecs::query::Changed;
use bevy_ecs::schedule::IntoSystemConfigs;
use bevy_tasks::{AsyncComputeTaskPool, Task};
use futures_lite::future;
-use log::{debug, error, trace, warn};
+use log::{debug, error, info, trace, warn};
use std::collections::VecDeque;
use std::sync::Arc;
-use std::time::Duration;
+use std::time::{Duration, Instant};
use self::moves::ExecuteCtx;
@@ -69,8 +69,27 @@ impl Plugin for PathfinderPlugin {
#[derive(Component, Default)]
pub struct Pathfinder {
pub path: VecDeque>,
+ pub queued_path: Option>>,
+ pub is_path_partial: bool,
+
pub last_reached_node: Option,
+ pub last_node_reached_at: Option,
+ pub goal: Option>,
+ pub is_calculating: bool,
}
+#[derive(Event)]
+pub struct GotoEvent {
+ pub entity: Entity,
+ pub goal: Arc,
+}
+#[derive(Event)]
+pub struct PathFoundEvent {
+ pub entity: Entity,
+ pub start: BlockPos,
+ pub path: Option>>,
+ pub is_partial: bool,
+}
+
#[allow(clippy::type_complexity)]
fn add_default_pathfinder(
mut commands: Commands,
@@ -100,17 +119,6 @@ impl PathfinderClientExt for azalea_client::Client {
});
}
}
-#[derive(Event)]
-pub struct GotoEvent {
- pub entity: Entity,
- pub goal: Arc,
-}
-#[derive(Event)]
-pub struct PathFoundEvent {
- pub entity: Entity,
- pub start: BlockPos,
- pub path: Option>>,
-}
#[derive(Component)]
pub struct ComputePath(Task