pub struct SpawnSelector { /* private fields */ }Implementations§
Source§impl SpawnSelector
impl SpawnSelector
Sourcepub const MAX_CANDIDATES: usize = 256usize
pub const MAX_CANDIDATES: usize = 256usize
Maximum input length for both legacy and checked paths.
pub fn new(minimum: f32) -> Result<SpawnSelector, LoopError>
Sourcepub fn choose(
&mut self,
positions: &[Vec2],
player: Vec2,
eligible: impl FnMut(usize) -> bool,
) -> Result<Option<usize>, LoopError>
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.
Sourcepub 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>>
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.
}Sourcepub 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>>
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));
}Sourcepub fn reset(&mut self)
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
impl Clone for SpawnSelector
Source§fn clone(&self) -> SpawnSelector
fn clone(&self) -> SpawnSelector
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more