261 lines
6.5 KiB
Rust
261 lines
6.5 KiB
Rust
//! Recorded choices at the firing checks, on the same clock as the replay.
|
|
use super::{ClassEst, Sam, battery::Measure, memory, why};
|
|
use std::collections::{HashMap, VecDeque};
|
|
|
|
const MAX_ENTRIES: usize = 4096;
|
|
const MAX_STREAMS: usize = 1024;
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
|
pub enum Channel {
|
|
Unit,
|
|
Gun,
|
|
Launcher,
|
|
}
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
pub enum Block {
|
|
Hold,
|
|
Moving,
|
|
Reload,
|
|
Empty,
|
|
NoSelection,
|
|
WaitingForTerrain,
|
|
Terrain,
|
|
Aim,
|
|
Opening,
|
|
BusyWithGroundShot,
|
|
Cooldown,
|
|
GunFault,
|
|
LauncherFault,
|
|
NoLock,
|
|
Range,
|
|
}
|
|
|
|
impl Block {
|
|
pub(super) fn gun(bits: u8) -> Self {
|
|
if bits & why::RELOADING != 0 {
|
|
Self::Reload
|
|
} else if bits & why::NO_ROUNDS != 0 {
|
|
Self::Empty
|
|
} else if bits & why::JAMMED != 0 {
|
|
Self::GunFault
|
|
} else if bits & why::NO_LOCK != 0 {
|
|
Self::NoLock
|
|
} else if bits & (why::NOT_AIMED | why::AIM_ERROR) != 0 {
|
|
Self::Aim
|
|
} else if bits & why::OUT_OF_RANGE != 0 {
|
|
Self::Range
|
|
} else {
|
|
Self::Cooldown
|
|
}
|
|
}
|
|
|
|
pub(super) fn launcher(bits: u8) -> Self {
|
|
if bits & why::RELOADING != 0 {
|
|
Self::Reload
|
|
} else if bits & why::NO_ROUNDS != 0 {
|
|
Self::Empty
|
|
} else if bits & why::LAUNCHER_DOWN != 0 {
|
|
Self::LauncherFault
|
|
} else {
|
|
Self::Cooldown
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
pub enum Outcome {
|
|
Held(Block),
|
|
Burst,
|
|
BurstMissed,
|
|
BurstHit,
|
|
Launched(u32),
|
|
Intercepted(u32),
|
|
InterceptorMissed(u32),
|
|
InterceptorEnded(u32),
|
|
}
|
|
|
|
/// What the unit itself measured or received, with IDs fixed at recording time.
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub struct Mark {
|
|
pub source: u32,
|
|
pub age: u16,
|
|
pub own: bool,
|
|
pub quality: u8,
|
|
pub product: Measure,
|
|
pub class: ClassEst,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub struct Entry {
|
|
pub at: u32,
|
|
pub unit: u32,
|
|
pub target: Option<u32>,
|
|
pub channel: Channel,
|
|
pub outcome: Outcome,
|
|
pub mark: Option<Mark>,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub struct History {
|
|
entries: VecDeque<Entry>,
|
|
first: u64,
|
|
last: HashMap<(u32, Channel), (Option<u32>, Outcome)>,
|
|
truncated: bool,
|
|
}
|
|
|
|
impl History {
|
|
pub fn first_index(&self) -> u64 { self.first }
|
|
pub fn entries(&self) -> impl DoubleEndedIterator<Item = &Entry> {
|
|
self.entries.iter()
|
|
}
|
|
|
|
pub fn truncated(&self) -> bool {
|
|
self.truncated
|
|
}
|
|
|
|
pub(super) fn resume(&mut self, unit: u32, channel: Channel) {
|
|
self.last.remove(&(unit, channel));
|
|
}
|
|
|
|
pub(super) fn note(
|
|
&mut self,
|
|
at: u32,
|
|
now: u64,
|
|
sam: &Sam,
|
|
channel: Channel,
|
|
target: Option<u32>,
|
|
outcome: Outcome,
|
|
) {
|
|
let key = (sam.id, channel);
|
|
if matches!(outcome, Outcome::Held(_)) {
|
|
if self.last.get(&key) == Some(&(target, outcome)) {
|
|
return;
|
|
}
|
|
if self.last.len() == MAX_STREAMS && !self.last.contains_key(&key) {
|
|
self.truncated = true;
|
|
return;
|
|
}
|
|
self.last.insert(key, (target, outcome));
|
|
} else {
|
|
self.last.remove(&key);
|
|
}
|
|
let mark = target.and_then(|id| sam.track(id)).map(|track| Mark {
|
|
source: track.source,
|
|
age: track.age(now).min(u64::from(u16::MAX)) as u16,
|
|
own: track.source == sam.id,
|
|
quality: track.quality,
|
|
product: track.product,
|
|
class: track.class,
|
|
});
|
|
// A refusal is for a public track, or the absence of a selected track.
|
|
// An actual shot still identifies the track it was fired at.
|
|
let target = if matches!(outcome, Outcome::Held(_)) && mark.is_none() {
|
|
None
|
|
} else {
|
|
target
|
|
};
|
|
self.push(Entry {
|
|
at,
|
|
unit: sam.id,
|
|
target,
|
|
channel,
|
|
outcome,
|
|
mark,
|
|
});
|
|
}
|
|
|
|
pub(super) fn outcome(
|
|
&mut self,
|
|
at: u32,
|
|
unit: u32,
|
|
target: u32,
|
|
channel: Channel,
|
|
outcome: Outcome,
|
|
) {
|
|
self.push(Entry {
|
|
at,
|
|
unit,
|
|
target: Some(target),
|
|
channel,
|
|
outcome,
|
|
mark: None,
|
|
});
|
|
}
|
|
|
|
fn push(&mut self, entry: Entry) {
|
|
if self.entries.len() == MAX_ENTRIES {
|
|
self.entries.pop_front();
|
|
self.first += 1;
|
|
self.truncated = true;
|
|
}
|
|
self.entries.push_back(entry);
|
|
}
|
|
|
|
pub(super) fn close(&mut self) {
|
|
self.last = HashMap::new();
|
|
}
|
|
|
|
pub(super) fn bytes(&self) -> usize {
|
|
memory::vecdeque_bytes(&self.entries)
|
|
+ memory::map_bytes(
|
|
self.last.capacity(),
|
|
size_of::<((u32, Channel), (Option<u32>, Outcome))>(),
|
|
)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::sim::SamKind;
|
|
use glam::DVec3;
|
|
|
|
#[test]
|
|
fn deduplicates_continuous_refusals_and_preserves_a_known_miss() {
|
|
let sam = Sam::new(16, SamKind::S400, DVec3::ZERO, 0.0);
|
|
let mut history = History::default();
|
|
for at in 0..100 {
|
|
history.note(
|
|
at,
|
|
u64::from(at),
|
|
&sam,
|
|
Channel::Unit,
|
|
None,
|
|
Outcome::Held(Block::Hold),
|
|
);
|
|
}
|
|
history.note(
|
|
100,
|
|
100,
|
|
&sam,
|
|
Channel::Launcher,
|
|
Some(93),
|
|
Outcome::Launched(94),
|
|
);
|
|
history.outcome(
|
|
130,
|
|
sam.id,
|
|
93,
|
|
Channel::Launcher,
|
|
Outcome::InterceptorMissed(94),
|
|
);
|
|
let entries: Vec<_> = history.entries().collect();
|
|
assert_eq!(entries.len(), 3);
|
|
assert_eq!(entries[0].at, 0);
|
|
assert_eq!(entries[1].outcome, Outcome::Launched(94));
|
|
assert_eq!(entries[2].outcome, Outcome::InterceptorMissed(94));
|
|
}
|
|
|
|
#[test]
|
|
fn a_full_journal_keeps_recent_entries_and_marks_the_gap() {
|
|
let mut history = History::default();
|
|
for at in 0..MAX_ENTRIES as u32 + 3 {
|
|
history.outcome(at, 16, 93, Channel::Gun, Outcome::BurstHit);
|
|
}
|
|
assert!(history.truncated());
|
|
assert_eq!(history.entries().count(), MAX_ENTRIES);
|
|
assert_eq!(history.entries().next().unwrap().at, 3);
|
|
}
|
|
}
|