Skip to main content

PathFinder

Struct PathFinder 

Source
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

Source

pub fn new() -> Self

Creates a finder with no buffers allocated.

Source

pub fn reserve(&mut self, size: UVec2) -> Result<(), PathError>

Allocates buffers for grids up to size ahead of the first search.

Source

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.

Source

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.

Source

pub fn is_pending(&self) -> bool

Whether a budgeted search is waiting for resume.

Source

pub fn cancel(&mut self)

Drops a pending search. Buffers keep their capacity.

Source

pub fn expanded(&self) -> u64

Cells expanded by the current or most recent search, across resumes.

Trait Implementations§

Source§

impl Clone for PathFinder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PathFinder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PathFinder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Component for T
where T: Send + Sync + 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.