Struct TargetSelector

Source
pub struct TargetSelector { /* private fields */ }
Expand description

Opt-in, allocation-free target retention using a finite, nonnegative score margin. Keep one selector per observer/selection stream and call it after sensing on actual simulation ticks. Reset it when restarting that stream.

Higher scores win; equal scores prefer the lower host ID on acquisition. An eligible incumbent is retained until a challenger improves its score by strictly more than the margin. A zero margin still retains equal scores. Scores are recomputed each call; no stale positions, scores or sight are kept. select_target and Memory::best retain their original behavior.

use ferravex_math::Vec3;
use ferravex_play::ai::{
    Candidate, Memory, MemorySettings, Sense, TargetSelector, TargetSelectionReason,
};
let mut memory = Memory::new(MemorySettings::default())?;
let mut selector = TargetSelector::new(0.25)?;
memory.observe(Candidate { id: 9, position: Vec3::Z, groups: 1 }, Sense::Sight)?;
let first = selector.select_memory(&memory, 1, |_| 1.0);
assert_eq!(first.reason, TargetSelectionReason::Acquired);
memory.observe(Candidate { id: 2, position: Vec3::X, groups: 1 }, Sense::Sight)?;
let next = selector.select_memory(&memory, 1, |t| if t.id == 2 { 1.1 } else { 1.0 });
assert_eq!(next.target.unwrap().id, 9);
assert_eq!(next.best, Some((2, 1.1)));
assert_eq!(next.reason, TargetSelectionReason::RetainedByHysteresis);
assert_eq!(selector.current(), Some(9));
selector.reset();
assert_eq!(selector.current(), None);

Implementations§

Source§

impl TargetSelector

Source

pub fn new(switch_margin: f32) -> Result<TargetSelector, AiError>

Rejects negative, NaN and infinite margins with InvalidConfiguration.

Source

pub fn current(&self) -> Option<u64>

Last selected ID, not a claim that the target is still alive or visible.

Source

pub fn reset(&mut self)

Forgets target identity while preserving the configured margin.

Source

pub fn select_visible( &mut self, candidates: impl IntoIterator<Item = Candidate>, observer: Observer, sight: Sight, budget: &mut WorkBudget, query: &mut impl VisibilityQuery, priority: impl FnMut(Candidate) -> f32, ) -> Result<TargetSelectionReport<Candidate>, AiError>

Applies hysteresis to select_target’s budgeted sight scan. Every returned candidate passed sight in this call, including the incumbent. No extra query is made to retain an incumbent outside the evaluated prefix; hosts can put it first when budgets are small. Rejected/deferred candidates never get scored. Non-finite scores are ignored.

Incomplete scans can replace or clear an unevaluated incumbent. Inspect incomplete before treating absence as a confirmed loss of sight. On an input error the selected ID is unchanged, but work/query/callback effects already performed are not rolled back. Empty input consumes no work.

use ferravex_math::Vec3;
use ferravex_play::ai::{
    Candidate, Observer, Sight, TargetSelector, Unoccluded, WorkBudget,
};
let mut selector = TargetSelector::new(0.5)?;
let observer = Observer { position: Vec3::ZERO, forward: Vec3::Z };
let sight = Sight::new(10.0, std::f32::consts::TAU, 1)?;
let candidates = [Candidate { id: 7, position: Vec3::Z, groups: 1 }];
let report = selector.select_visible(
    candidates, observer, sight, &mut WorkBudget::new(1, 1, 0),
    &mut Unoccluded, |_| 1.0,
)?;
assert_eq!(report.target.unwrap().id, 7);
assert!(!report.incomplete);
Source

pub fn select_memory( &mut self, memory: &Memory, mask: u32, priority: impl FnMut(&Track) -> f32, ) -> TargetSelectionReport<Track>

Scores matching tracks in existing bounded memory without observing, refreshing expiry or issuing visibility queries. Call Memory::advance once per simulation tick before recording accepted detections and selecting. Removed, expired or masked-out tracks cannot be retained.

As in Memory::best, fresh sight outranks remembered sight/hearing. Within that tier, finite scores and the switch margin determine selection; the callback can use -(track.age as f32) for youngest-first priority. Each matching track is scored once; non-finite scores exclude it. Returned tracks preserve their visibility, age and remaining lifetime. A selected memory is awareness, not a new sight detection or permission to attack.

Trait Implementations§

Source§

impl Clone for TargetSelector

Source§

fn clone(&self) -> TargetSelector

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 TargetSelector

Source§

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

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

impl Copy for TargetSelector

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.