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
impl TargetSelector
Sourcepub fn new(switch_margin: f32) -> Result<TargetSelector, AiError>
pub fn new(switch_margin: f32) -> Result<TargetSelector, AiError>
Rejects negative, NaN and infinite margins with InvalidConfiguration.
Sourcepub fn current(&self) -> Option<u64>
pub fn current(&self) -> Option<u64>
Last selected ID, not a claim that the target is still alive or visible.
Sourcepub 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>
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);Sourcepub fn select_memory(
&mut self,
memory: &Memory,
mask: u32,
priority: impl FnMut(&Track) -> f32,
) -> TargetSelectionReport<Track>
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
impl Clone for TargetSelector
Source§fn clone(&self) -> TargetSelector
fn clone(&self) -> TargetSelector
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more