diff --git a/airdef-mod/src/sim/record.rs b/airdef-mod/src/sim/record.rs index 5259ef0..033f5fe 100644 --- a/airdef-mod/src/sim/record.rs +++ b/airdef-mod/src/sim/record.rs @@ -30,6 +30,9 @@ pub struct SamKey { spent_cells: u8, cap_cell: u8, cap_open: f32, + /// A discrete transition or a frame of a tube cover's opening rather than a pose sample (#142): + /// decimation keeps it, within its own budget. + event: bool, } impl SamKey { @@ -51,6 +54,7 @@ impl SamKey { spent_cells: sam.spent_cells, cap_cell: sam.cap_cell, cap_open: sam.cap_open, + event: false, } } /// The same pose (to within what could be seen). @@ -68,6 +72,18 @@ impl SamKey { && self.spent_cells == o.spent_cells && self.cap_cell == o.cap_cell && near(self.cap_open, o.cap_open, 0.001) } + /// The states a replay reads as facts rather than as interpolated poses: which round is spent, + /// which tube the cover opens, whether the radar emits, and the mount's configuration. + fn discrete(&self) -> (battery::SamMode, battery::RadarRole, bool, u8, u8) { + (self.mode, self.radar_role, self.radar_on, self.spent_cells, self.cap_cell) + } + /// The tube cover's stage: shut, on its way open, or wide. + fn cover(&self) -> u8 { cover_stage(self.cap_open) } +} + +/// The cover's stage an opening (`launch::OPEN_TICKS` long) passes through. +fn cover_stage(open: f32) -> u8 { + if open <= 0.0 { 0 } else if open < 1.0 { 1 } else { 2 } } /// A system through a recording: its poses (a new one only when it changed, with the @@ -115,6 +131,7 @@ impl SamTrack { spent_cells: a.spent_cells, cap_cell: a.cap_cell, cap_open: if a.cap_cell == b.cap_cell && a.spent_cells == b.spent_cells { l(a.cap_open, b.cap_open) } else { a.cap_open }, + event: false, }) } /// The system as it stood at `t`, as a ghost to draw with the ids from `id`. @@ -157,6 +174,7 @@ impl Recording { sam_tracks: Vec::new(), sam_index: HashMap::new(), sam_keys: 0, + sam_events: 0, sam_step: STEP, ticks: Vec::new(), times: Vec::new(), @@ -190,6 +208,10 @@ const LONGEST: u32 = 12_000; const KEPT: usize = 3; /// SAM poses share a fixed memory budget; under pressure old poses are thinned and new ones slow down. const MAX_SAM_KEYS: usize = 32_768; +/// Discrete transitions and cover-opening frames have their own small budget (#142): they are +/// facts a replay must not lose, but they may not spend the pose budget either. When it runs out +/// the oldest stop being critical (the newest are kept) and decimation may thin them as poses. +const MAX_SAM_EVENTS: usize = 4_096; #[derive(Clone, Copy, PartialEq, Debug)] pub enum What { @@ -387,6 +409,8 @@ pub struct Recording { pub sam_tracks: Vec, sam_index: HashMap, sam_keys: usize, + /// How many of `sam_keys` are critical transitions (#142). + sam_events: usize, sam_step: u32, /// The game's tick number as sampled with the clock: (recording tick, game tick). A replay /// shows the world's blocks as of this time. @@ -597,37 +621,70 @@ impl Recording { sam_tracks: Vec::new(), sam_index: HashMap::new(), sam_keys: 0, + sam_events: 0, sam_step: STEP, ticks: Vec::new(), times: Vec::new(), quiet: 0, } } - fn thin_sam_tracks(&mut self) { + /// Halves each track's poses, keeping its critical transitions, its first and its last key (#142). + fn thin_sam_poses(&mut self) { for track in &mut self.sam_tracks { let len = track.keys.len(); let mut kept = 0; - for read in (0..len).step_by(2) { - track.keys[kept] = track.keys[read]; - kept += 1; - } - if len % 2 == 0 && len > 0 { - track.keys[kept] = track.keys[len - 1]; - kept += 1; + let mut poses = 0; + for read in 0..len { + let key = track.keys[read]; + let keep = if key.event { + true + } else { + poses += 1; + poses % 2 == 1 || read + 1 == len + }; + if keep { + track.keys[kept] = key; + kept += 1; + } } track.keys.truncate(kept); track.keys.shrink_to_fit(); } - self.sam_keys = self.sam_tracks.iter().map(|track| track.keys.len()).sum(); + self.count_sam_keys(); self.sam_step = self.sam_step.saturating_mul(2).min(LONGEST); } + /// The transitions' budget ran out: the oldest stop being critical, so decimation may thin them + /// as poses. The newest transitions, which a replay is watched closest for, are kept (#142). + fn thin_sam_events(&mut self) { + let mut over = self.sam_events.saturating_sub(MAX_SAM_EVENTS); + while over > 0 { + let mut demoted = 0; + for track in &mut self.sam_tracks { + if over == 0 { break; } + if let Some(key) = track.keys.iter_mut().find(|key| key.event) { + key.event = false; + over -= 1; + demoted += 1; + } + } + if demoted == 0 { break; } + } + self.count_sam_keys(); + } + fn count_sam_keys(&mut self) { + self.sam_keys = self.sam_tracks.iter().map(|track| track.keys.len()).sum(); + self.sam_events = self.sam_tracks.iter().map(|track| track.keys.iter().filter(|key| key.event).count()).sum(); + } /// Notes the systems' poses at the recording's tick `now`: a pose only when it changed, with /// the pose it held marked before it so a replay doesn't glide through the stillness. + /// + /// Poses are sampled on the adaptive `sam_step` grid under its own budget, but the cover's + /// opening (`launch::OPEN_TICKS`, shorter than a decimated interval) and the discrete + /// transitions — a round ejected, a tube cancelled, the radar switched, the mount's + /// configuration — are facts: they are kept on the tick they happen, in their own budget (#142). fn note_sams(&mut self, sams: &[Sam], now: u32) { - if now % self.sam_step != 0 { - return; - } + let pose_due = now % self.sam_step == 0; for sam in sams { let key = SamKey::of(sam, self.origin, now); let i = *self.sam_index.entry(sam.id).or_insert_with(|| { @@ -636,20 +693,41 @@ impl Recording { }); let track = &mut self.sam_tracks[i]; let before = track.keys.len(); - match track.keys.last().copied() { - None => track.keys.push(key), - Some(last) if last.same(&key) => {} - Some(last) => { - if last.at + self.sam_step < now { - track.keys.push(SamKey { at: now - self.sam_step, ..last }); - } - track.keys.push(key); - } + let Some(last) = track.keys.last().copied() else { + track.keys.push(key); + self.sam_keys += 1; + continue; + }; + if last.same(&key) { + continue; } - self.sam_keys += track.keys.len() - before; + // The cover's opening is short: its first frame only differs from the shut pose by a + // fraction, so the stage's start is backdated to the tick it began (#142). + if last.cover() == 0 && key.cover() == 1 && now > last.at + 1 { + track.keys.push(SamKey { at: now - 1, cap_open: 0.0, event: true, ..key }); + } + let transitioning = last.discrete() != key.discrete() || last.cover() != key.cover(); + // Every frame of the cover's own movement is kept, so the stage's six ticks survive intact. + let moving = key.cover() == 1 && last.cap_open != key.cap_open; + if transitioning || moving { + let mut event = key; + event.event = true; + track.keys.push(event); + } else if pose_due { + if last.at + self.sam_step < now { + track.keys.push(SamKey { at: now - self.sam_step, event: false, ..last }); + } + track.keys.push(key); + } + let added = track.keys.len() - before; + self.sam_keys += added; + self.sam_events += track.keys[before..].iter().filter(|key| key.event).count(); } - if self.sam_keys > MAX_SAM_KEYS { - self.thin_sam_tracks(); + if self.sam_events > MAX_SAM_EVENTS { + self.thin_sam_events(); + } + if self.sam_keys.saturating_sub(self.sam_events) > MAX_SAM_KEYS { + self.thin_sam_poses(); } } /// Actual launch, if observed during this recording. Missing is not proof of no launch: @@ -874,9 +952,10 @@ impl Sim { } r.ticks.push((r.len, self.engine_tick)); } + // Poses share the adaptive grid inside; the discrete transitions and the cover's short + // opening must be seen on the tick they happen, whatever the grid (#142). + r.note_sams(&self.sams, r.len); if r.len % STEP == 0 { - let now = r.len; - r.note_sams(&self.sams, now); for d in self.drones.iter().filter(|d| !recon::is_drone_id(d.id)) { r.point(d.id, What::Drone(d.kind), d.pos); } @@ -1089,6 +1168,7 @@ mod tests { sam_tracks: Vec::new(), sam_index: HashMap::new(), sam_keys: 0, + sam_events: 0, sam_step: STEP, ticks: Vec::new(), times: Vec::new(), @@ -1244,6 +1324,7 @@ mod tests { let keys: usize = r.sam_tracks.iter().map(|track| track.keys.len()).sum(); let allocated: usize = r.sam_tracks.iter().map(|track| track.keys.capacity()).sum(); assert_eq!(keys, r.sam_keys); + assert_eq!(r.sam_events, 0, "a turning dish is a pose, not a transition"); assert!(keys <= MAX_SAM_KEYS, "a full recording retained {keys} SamKeys"); assert!(allocated <= 2 * MAX_SAM_KEYS, "SamKey allocations reached {} MiB", allocated * size_of::() / (1024 * 1024)); assert!(r.sam_step > STEP, "the rotating radars never engaged adaptive sampling"); @@ -1252,6 +1333,104 @@ mod tests { assert!(last.dish > 100.0, "the final radar pose was not recorded"); } + // #142: sixty-four turning radars on a 16-tick pose grid plus a cold S-400 launch. The + // cover's six-tick opening, the ejection and the spent cell must survive the decimation, and + // so must the radar's switch and a cancelled cover, on the tick they happened. + #[test] + fn decimation_keeps_a_cold_launch_cover_ejection_and_the_radar_switch() { + let mut r = recording(); + let mut sams: Vec<_> = (0..64).map(|id| Sam::new(id, SamKind::Radar, DVec3::ZERO, 0.0)).collect(); + sams.push(Sam::new(100, SamKind::S400, DVec3::ZERO, 0.0)); + let turned_off = 5_usize; + // The pose budget of a long recording has already slowed the sampling down. + r.sam_step = 16; + let eject = 4103u32; + let start = eject - launch::OPEN_TICKS as u32; + let (reopen, close, switch) = (4120u32, 4122u32, 4131u32); + for now in 0..4_200 { + for sam in &mut sams[..64] { + sam.dish = now as f64 * 0.01; + } + let s400 = sams.len() - 1; + if now == start { + assert!(!sams[s400].prepare_tube(7, now as u64)); + } else if (start + 1..=eject).contains(&now) { + if sams[s400].prepare_tube(7, now as u64) { + assert_eq!(now, eject); + sams[s400].eject_cell(0); + } + } else if (reopen..close).contains(&now) { + // A second opening, cancelled before it is wide: the cover closes. + sams[s400].prepare_tube(7, now as u64); + } else if now == close { + sams[s400].cancel_prepare(); + } + if now == switch { + sams[turned_off].radar_on = false; + } + r.note_sams(&sams, now); + } + let track = |id: u32| r.sam_tracks.iter().find(|track| track.id == id).unwrap(); + let pose = |id: u32, t: f64| track(id).pose(t).unwrap(); + let s400 = track(100); + assert!(s400.keys.len() < 32, "the launch track kept {} keys", s400.keys.len()); + // The cover was shut before the stage and opens exactly over `OPEN_TICKS` ticks. + assert_eq!(pose(100, f64::from(start - 1)).cap_open, 0.0); + for k in 1..launch::OPEN_TICKS as u32 { + let want = k as f32 / launch::OPEN_TICKS as f32; + let got = pose(100, f64::from(start + k)).cap_open; + assert!((got - want).abs() < 1e-5, "tick {} of the opening: {got} not {want}", start + k); + } + assert!(s400.keys.iter().any(|k| k.at == start && k.event && k.cap_open == 0.0), "the stage's start key is kept"); + // The last tick before the ejection: the cover stands five sixths open, not shut. + assert!((pose(100, f64::from(eject - 1)).cap_open - 5.0 / 6.0).abs() < 1e-5); + // The ejection: the cell is spent on its own tick, not on the next pose sample. + assert_eq!(pose(100, f64::from(eject - 1)).spent_cells, 0); + assert_eq!(pose(100, f64::from(eject)).spent_cells, 1); + assert!(track(100).keys.iter().any(|k| k.at == eject && k.event)); + // The cancelled opening closes the cover on the tick it was cancelled. + assert!(pose(100, f64::from(close - 1)).cap_open > 0.0); + assert_eq!(pose(100, f64::from(close)).cap_open, 0.0); + // The radar's switch is on its own tick, between two 16-tick pose samples. + assert!(pose(turned_off as u32, f64::from(switch - 1)).radar_on); + assert!(!pose(turned_off as u32, f64::from(switch)).radar_on); + assert!(track(turned_off as u32).keys.iter().any(|k| k.at == switch && k.event)); + // Later decimation halves poses only: the recorded launch still reads as it did. + r.thin_sam_poses(); + r.thin_sam_poses(); + let track = |id: u32| r.sam_tracks.iter().find(|track| track.id == id).unwrap(); + let pose = |id: u32, t: f64| track(id).pose(t).unwrap(); + assert!((pose(100, f64::from(eject - 1)).cap_open - 5.0 / 6.0).abs() < 1e-5); + assert_eq!(pose(100, f64::from(eject)).spent_cells, 1); + assert!(!pose(turned_off as u32, f64::from(switch)).radar_on); + } + + // #142: the transitions have their own explicit budget. A recording that chatters must stay + // bounded, and the newest transitions — what a replay is watched closest for — stay exact. + #[test] + fn critical_transitions_have_their_own_explicit_budget() { + let mut r = recording(); + let mut sams: Vec<_> = (0..64).map(|id| Sam::new(id, SamKind::Radar, DVec3::ZERO, 0.0)).collect(); + let ticks = 1_500u32; + for now in 0..ticks { + for sam in &mut sams { + sam.radar_on = now % 2 == 0; + } + r.note_sams(&sams, now); + } + let keys: usize = r.sam_tracks.iter().map(|track| track.keys.len()).sum(); + assert_eq!(keys, r.sam_keys); + assert_eq!(r.sam_events, MAX_SAM_EVENTS, "the transitions' budget is explicit"); + assert!(keys <= MAX_SAM_KEYS + MAX_SAM_EVENTS + sams.len(), "{keys} SamKeys retained"); + assert!(keys - r.sam_events <= MAX_SAM_KEYS, "poses broke their own budget"); + // The newest switch reads exactly; the oldest transitions were the ones let go. + let newest = r.sam_tracks[0].pose(f64::from(ticks - 1)).unwrap(); + assert_eq!(newest.radar_on, (ticks - 1) % 2 == 0); + assert!(r.sam_tracks[0].keys.iter().any(|key| key.at == ticks - 1 && key.event)); + let earliest = r.sam_tracks[0].keys.iter().position(|key| key.event); + assert!(earliest.is_none_or(|i| r.sam_tracks[0].keys[i].at > ticks / 2), "old transitions hold on to the budget"); + } + #[test] fn a_system_keeps_its_pose_through_stillness_and_moves_when_it_moved() { let mut r = recording(); diff --git a/docs/RAID-STABILITY.md b/docs/RAID-STABILITY.md index 2a5529a..cb6ee85 100644 --- a/docs/RAID-STABILITY.md +++ b/docs/RAID-STABILITY.md @@ -33,6 +33,33 @@ 137346889/300M, command 129151/1B, failures=0. Лимит памяти 64 MiB не увеличивался. Это синтетический host без GPU и не приёмка Windows GUI. +### Прореживание не съедает короткие переходы (#142) + +Прореживание #122 не различало непрерывную позу и дискретный факт: при большом числе меняющихся +РЛС оно пропускало целиком открытие крышки ТПК (`launch::OPEN_TICKS = 6` тиков, короче шага +sampling) и могло убрать уже записанный переход — ракета в реплее вылетала с нарисованной +закрытой крышкой, а `spent_cells` менялось только на следующем общем сэмпле. + +Теперь `Recording::note_sams` вызывается каждый тик: позы по-прежнему снимаются на адаптивной +сетке `sam_step` из прежнего бюджета 32768, а дискретные переходы (`cap_cell`, `spent_cells`, +`radar_on`, режим и роль) и кадры открытия крышки пишутся на том тике, когда произошли, и +помечаются как события. Начало открытия дотягивается отдельным ключом на тик раньше, поэтому +ramp занимает ровно `OPEN_TICKS`, а не «примерно». Прореживание поз половинит только позы и +сохраняет события, первую и последнюю позу. + +У событий свой явный бюджет — 4096 ключей на запись поверх 32768 поз; общий бюджет поз не +поднимался. При переполнении старые события перестают быть критическими (прореживаются как +позы), новейшие сохраняются: под записью с «болтливым» датчиком реплей остаётся ограниченным, +а свежий пуск — точным. + +Проверки: `airdef-core` `sim::record::tests::decimation_keeps_a_cold_launch_cover_ejection_and_the_radar_switch` +— 64 вращающихся РЛС + С-400 при `sam_step = 16`, выброс на тике 4103: за тик до выброса +`cap_open = 0.8333333` (как в контрольной записи без давления на бюджет), `spent_cells = 0` +до и `1` на тике выброса, отмена/закрытие и `radar_on` читаются на своих тиках, повторное +прореживание их не убирает; +`critical_transitions_have_their_own_explicit_budget` — 64 РЛС, переключающихся каждый тик, +1500 тиков: событий ровно 4096, позы в своём бюджете, новейшее переключение точное. + ## Второй налёт из UI: событие, не тик Точный лог Windows: `Horizon: mod airdef stopped in UI event (fuel budget 1000000000,