pub struct PathFinder { /* private fields */ }Expand description
Reusable A* search state for grids up to the largest size seen.
Buffers grow to the grid’s cell count on first use and are reused; later searches on grids no larger than that do not allocate, apart from the open queue growing past its previous peak. Paths are written into a caller-owned vector so its capacity is reused too.
use rayengine_core::glam::UVec2;
use rayengine_core::pathfinding::{CostGrid, PathFinder, PathOptions, PathStatus};
let mut grid = CostGrid::new(UVec2::new(5, 3), 1.0);
grid.block_rect(UVec2::new(2, 0), UVec2::new(2, 1));
let mut finder = PathFinder::new();
let mut path = Vec::new();
let status = finder
.find_path(&grid, UVec2::new(0, 0), UVec2::new(4, 0), &PathOptions::default(), &mut path)
.unwrap();
assert!(matches!(status, PathStatus::Found { .. }));
assert_eq!(path.first(), Some(&UVec2::new(0, 0)));
assert_eq!(path.last(), Some(&UVec2::new(4, 0)));
assert!(path.contains(&UVec2::new(2, 2)));Implementations§
Source§impl PathFinder
impl PathFinder
Sourcepub fn reserve(&mut self, size: UVec2) -> Result<(), PathError>
pub fn reserve(&mut self, size: UVec2) -> Result<(), PathError>
Allocates buffers for grids up to size ahead of the first search.
Sourcepub fn find_path<G: NavGrid + ?Sized>(
&mut self,
grid: &G,
start: UVec2,
goal: UVec2,
options: &PathOptions,
path: &mut Vec<UVec2>,
) -> Result<PathStatus, PathError>
pub fn find_path<G: NavGrid + ?Sized>( &mut self, grid: &G, start: UVec2, goal: UVec2, options: &PathOptions, path: &mut Vec<UVec2>, ) -> Result<PathStatus, PathError>
Searches from start to goal, replacing path with the result.
Any pending search is cancelled. The start cell is never checked for
walkability, so an agent standing in a blocked cell can still leave it.
A blocked goal is unreachable, except that start == goal is always
found with cost zero. With PathOptions::budget the search
may return PathStatus::Pending; continue it with
resume.
Errors for invalid options, out-of-bounds cells, oversized grids, or an invalid cell cost met during the search; errors cancel the search.
Sourcepub fn resume<G: NavGrid + ?Sized>(
&mut self,
grid: &G,
budget: Option<u32>,
path: &mut Vec<UVec2>,
) -> Result<PathStatus, PathError>
pub fn resume<G: NavGrid + ?Sized>( &mut self, grid: &G, budget: Option<u32>, path: &mut Vec<UVec2>, ) -> Result<PathStatus, PathError>
Continues a search that returned PathStatus::Pending, expanding at
most budget cells (None for no limit).
Pass the same, unchanged grid. Errors with PathError::NoSearch when
nothing is pending, PathError::GridResized (keeping the search
pending) when the grid size changed, or PathError::InvalidOptions
for a zero budget.
Sourcepub fn is_pending(&self) -> bool
pub fn is_pending(&self) -> bool
Whether a budgeted search is waiting for resume.