Struct SpawnSelector

Source
pub struct SpawnSelector { /* private fields */ }

Implementations§

Source§

impl SpawnSelector

Source

pub const MAX_CANDIDATES: usize = 256usize

Maximum input length for both legacy and checked paths.

Source

pub fn new(minimum: f32) -> Result<SpawnSelector, LoopError>

Source

pub fn choose( &mut self, positions: &[Vec2], player: Vec2, eligible: impl FnMut(usize) -> bool, ) -> Result<Option<usize>, LoopError>

Farthest eligible candidate wins, with rotating stable tie breaks. At most 256 eligibility queries. None means defer spawning, never fallback into an unsafe location. Callbacks should be read-only world queries.

Source

pub fn assess_into<E>( &self, positions: &[Vec2], reference: Vec2, output: &mut [SpawnAssessment], available: impl FnMut(usize) -> Result<bool, E>, score: impl FnMut(usize, f32) -> Result<Option<f32>, E>, unobstructed: impl FnMut(usize) -> Result<bool, E>, ) -> Result<Option<usize>, SpawnCheckError<E>>

Assess candidates without rotating the selector. Returns the winning source index, or None when all candidates are excluded (defer spawning).

Requires 1..=256 positions, a valid reference (finite, each absolute component <= 1e6), and output capacity >= input length, checked in that order before any callbacks. Bad individual positions are exclusions, unlike the legacy method’s whole-request error. The minimum-distance boundary is inclusive and uses the same arithmetic as Self::choose.

Starting at the existing cursor modulo input length, visit each source index once, wrapping. At each index: geometry, minimum distance, available, score, unobstructed, stopping at the first exclusion. Each callback is invoked at most once per candidate (at most 256 each, 768 total); no obstruction query runs after any prior exclusion or invalid score. Unlike legacy choose, eligible lower-scored candidates still get obstruction queries so that diagnostics describe every candidate.

score(index, distance_squared) returns None for authored exclusion, or a finite score (negative/zero allowed). Higher scores win; exactly equal scores keep the first in rotating source order, with no random seed. Return the supplied distance for legacy farthest-distance ranking. Availability and score callbacks should be cheap read-only checks; clearance/LOS stays entirely caller-controlled in unobstructed.

On success, output[..positions.len()] is replaced in source index order and the tail is untouched. Errors (including a later query failure or nonfinite score) leave all output and selector state untouched using fixed stack scratch storage, with no heap allocation by this method. External callback effects/allocations/panics cannot be rolled back; no remaining queries run after a reported failure. Diagnostics are a snapshot, not a reservation or permission to skip actual admission/revalidation.

use ferravex_math::Vec2;
use ferravex_play::gameplay::spawn::{SpawnAssessment, SpawnSelector};
let selector = SpawnSelector::new(10.0).unwrap();
let points = [Vec2::new(20.0, 0.0), Vec2::new(-20.0, 0.0)];
let mut output = [SpawnAssessment::EMPTY; 2];
for _ in 0..2 {
    let best = selector.assess_into(&points, Vec2::ZERO, &mut output,
        |_| Ok::<_, ()>(true), |_, d| Ok(Some(d)), |_| Ok(true)).unwrap();
    assert_eq!(best, Some(0)); // Reading does not rotate ties.
}
Source

pub fn choose_checked_into<E>( &mut self, positions: &[Vec2], reference: Vec2, output: &mut [SpawnAssessment], available: impl FnMut(usize) -> Result<bool, E>, score: impl FnMut(usize, f32) -> Result<Option<f32>, E>, unobstructed: impl FnMut(usize) -> Result<bool, E>, ) -> Result<Option<usize>, SpawnCheckError<E>>

Assess and accept a selection, rotating only on Ok(Some(index)). All query, output and failure contracts are those of Self::assess_into. This accepts selector state only, not an actor or encounter credit spend. Use a copied selector and commit it after actual admission if that can fail; see the module’s runnable encounter recipe.

use ferravex_math::Vec2;
use ferravex_play::gameplay::spawn::{SpawnAssessment, SpawnSelector};
let mut selector = SpawnSelector::new(0.0).unwrap();
let points = [Vec2::X, -Vec2::X];
let mut output = [SpawnAssessment::EMPTY; 2];
for expected in [0, 1] {
    assert_eq!(selector.choose_checked_into(&points, Vec2::ZERO, &mut output,
        |_| Ok::<_, ()>(true), |_, d| Ok(Some(d)), |_| Ok(true)).unwrap(),
        Some(expected));
}
Source

pub fn reset(&mut self)

Reset tie rotation to source index zero, retaining the authored minimum. Reset on a new run or when intentionally restarting candidate identity.

use ferravex_math::Vec2;
use ferravex_play::gameplay::spawn::SpawnSelector;
let mut selector = SpawnSelector::new(0.0).unwrap();
let points = [Vec2::X, -Vec2::X];
assert_eq!(selector.choose(&points, Vec2::ZERO, |_| true), Ok(Some(0)));
selector.reset();
assert_eq!(selector.choose(&points, Vec2::ZERO, |_| true), Ok(Some(0)));

Trait Implementations§

Source§

impl Clone for SpawnSelector

Source§

fn clone(&self) -> SpawnSelector

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SpawnSelector

Source§

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

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

impl Copy for SpawnSelector

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.