diff options
| author | mat <git@matdoes.dev> | 2024-12-24 04:37:55 +0000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2024-12-24 04:37:55 +0000 |
| commit | 958848e8ed10c7b8a83c9faea1fac6eaac39b018 (patch) | |
| tree | 9e09f0994b779db00c65c9a25168818b61724e8c /azalea/src/pathfinder/astar.rs | |
| parent | 30cbeecdfea4199b8f134c8394c677588ff9a2ce (diff) | |
| download | azalea-drasl-958848e8ed10c7b8a83c9faea1fac6eaac39b018.tar.xz | |
improve some docs and apis related to pathfinder
Diffstat (limited to 'azalea/src/pathfinder/astar.rs')
| -rw-r--r-- | azalea/src/pathfinder/astar.rs | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/azalea/src/pathfinder/astar.rs b/azalea/src/pathfinder/astar.rs index 9e48ba2d..2442adb6 100644 --- a/azalea/src/pathfinder/astar.rs +++ b/azalea/src/pathfinder/astar.rs @@ -23,12 +23,24 @@ const COEFFICIENTS: [f32; 7] = [1.5, 2., 2.5, 3., 4., 5., 10.]; const MIN_IMPROVEMENT: f32 = 0.01; +pub enum PathfinderTimeout { + /// Time out after a certain duration has passed. This is a good default so + /// you don't waste too much time calculating a path if you're on a slow + /// computer. + Time(Duration), + /// Time out after this many nodes have been considered. + /// + /// This is useful as an alternative to a time limit if you're doing + /// something like running tests where you want consistent results. + Nodes(usize), +} + pub fn a_star<P, M, HeuristicFn, SuccessorsFn, SuccessFn>( start: P, heuristic: HeuristicFn, mut successors: SuccessorsFn, success: SuccessFn, - timeout: Duration, + timeout: PathfinderTimeout, ) -> Path<P, M> where P: Eq + Hash + Copy + Debug, @@ -104,10 +116,16 @@ where } // check for timeout every ~1ms - if num_nodes % 1000 == 0 && start_time.elapsed() > timeout { - // timeout, just return the best path we have so far - trace!("A* couldn't find a path in time, returning best path"); - break; + if num_nodes % 1000 == 0 { + let timed_out = match timeout { + PathfinderTimeout::Time(max_duration) => start_time.elapsed() > max_duration, + PathfinderTimeout::Nodes(max_nodes) => num_nodes > max_nodes, + }; + if timed_out { + // timeout, just return the best path we have so far + trace!("A* couldn't find a path in time, returning best path"); + break; + } } } |
