Module spawn

Source
Expand description

Deterministic safe-spawn selection. Hosts supply authoritative occupancy and line-of-sight eligibility before spending encounter credits.

The legacy SpawnSelector::choose keeps its farthest-first callback pruning. The checked APIs instead explain every candidate, rank finite caller scores, and never turn an unsafe result into a fallback position. Scores/availability are authoritative host policy, not client authority.

§Admission recipe

This uses the real encounter admission gate and one already prepared actor slot. Selection runs on a copy: failed admission does not rotate the live selector. No world mutation occurs between the queries and admission. Once admission spends credits, placing into the prepared slot cannot fail. For a fallible ECS/pool admission, reserve/validate its capacity first and arrange explicit rollback if a later gate can fail; selection is not a reservation.

use ferravex_math::Vec2;
use ferravex_play::{
    encounter::{EncounterConfig, EncounterDirector},
    gameplay::spawn::{SpawnAssessment, SpawnCheckError, SpawnSelector},
};

fn attempt(selector: &mut SpawnSelector, director: &mut EncounterDirector,
    actor: &mut Option<Vec2>, obstructed: bool) -> Result<bool, SpawnCheckError<()>>
{
    let points = [Vec2::new(20.0, 0.0), Vec2::new(-20.0, 0.0)];
    let mut output = [SpawnAssessment::EMPTY; 2];
    let mut pending = *selector;
    let chosen = pending.choose_checked_into(&points, Vec2::ZERO, &mut output,
        |_| Ok(actor.is_none()), // Prepared storage availability.
        |_, distance_squared| Ok(Some(distance_squared)),
        |_| Ok(!obstructed))?; // Replace with authoritative geometry/LOS query.
    if let Some(index) = chosen {
        if director.admit(1, usize::from(actor.is_some())) {
            *actor = Some(points[index]); // Guaranteed placement in prepared slot.
            *selector = pending;
            return Ok(true); // Emit accepted-event feedback here.
        }
    }
    Ok(false) // Defer; no placement or credits spent when unsafe/rejected.
}
let mut selector = SpawnSelector::new(10.0).unwrap();
let mut director = EncounterDirector::new(EncounterConfig {
    credits: 2, max_alive: 1, interval: 0.5, first_delay: 0.0,
    recovery_seconds: 1.0,
}).unwrap();
let mut actor = None;
assert!(!attempt(&mut selector, &mut director, &mut actor, true).unwrap());
assert_eq!(director.credits(), 2);
assert_eq!(actor, None);
assert!(attempt(&mut selector, &mut director, &mut actor, false).unwrap());
assert_eq!(director.credits(), 1);
assert_eq!(actor.take(), Some(Vec2::new(20.0, 0.0)));
// Pacing rejects another safe selection without accepting its cursor change.
assert!(!attempt(&mut selector, &mut director, &mut actor, false).unwrap());
assert_eq!(director.credits(), 1);
director.advance(0.25, 0.0).unwrap();
director.advance(0.25, 0.0).unwrap();
assert!(attempt(&mut selector, &mut director, &mut actor, false).unwrap());
assert_eq!(actor, Some(Vec2::new(-20.0, 0.0)));

Structs§

SpawnAssessment
Read-only diagnostic stored at the original source index in caller output. Indices identify this input slice only; reordering candidates changes identity.
SpawnSelector

Enums§

SpawnCheckError
Request/numeric/query failures, distinct from individual candidate exclusions. Every reported error preserves the selector and the entire output slice.
SpawnExclusion
First exclusion in the checked pipeline; later queries are skipped. See the module’s admission recipe and SpawnSelector::assess_into.
SpawnOutcome
One candidate’s eligibility, with the host’s finite score when eligible.
SpawnQuery
Which external query failed. Query side effects cannot be rolled back.