diff --git a/airdef-mod/src/replay.rs b/airdef-mod/src/replay.rs index 7cba5e2..f73c243 100644 --- a/airdef-mod/src/replay.rs +++ b/airdef-mod/src/replay.rs @@ -1044,11 +1044,11 @@ impl Player { What::Missile(..) => (0x90FFD040, 0xFFFFE070), What::Strike | What::StrikeCarrier(_) => (0x90FFA040, 0xFFFFC060), }; - let upto = ((self.t - tr.first as f64) / STEP as f64).floor(); + let upto = ((self.t - tr.first as f64) / tr.step() as f64).floor(); if upto < 0.0 { continue; } - let last = ((tr.last() - tr.first) / STEP) as f64; + let last = ((tr.last() - tr.first) / tr.step()) as f64; let count = upto.min(last) as usize + 1; if count < 2 { continue; diff --git a/airdef-mod/src/sim/mod.rs b/airdef-mod/src/sim/mod.rs index f022e46..9137121 100644 --- a/airdef-mod/src/sim/mod.rs +++ b/airdef-mod/src/sim/mod.rs @@ -288,8 +288,6 @@ pub struct Ground { chunks: Vec<(i32, i32)>, /// The game has answered "loading" at least once: it loads chunks on request (a server doesn't). pub loads: bool, - /// The tick of the last full clean-up (a big cache would otherwise be scanned on every answer). - swept: u64, /// The tick of the last answer round: what the heights' ages are measured against. now: u64, /// The world's epoch when the last questions went out: an answer to them is of that world or dimension, or of none. @@ -320,8 +318,15 @@ const HEIGHT_TTL: u64 = 1200; /// Ticks a flight waits for an unanswered section, and for a chunk the game doesn't have while low over the land (#61). pub const HOLD_PENDING: u16 = 45; pub const HOLD_NO_CHUNK: u16 = 240; -/// Ticks between full clean-ups of a big ground cache. -const SWEEP_EVERY: u64 = 200; +/// Columns whose heights are kept: the ground cache's share of the 64 MiB WASM linear memory +/// the whole mod lives in (shared with the block cache, the recordings and the simulation). +/// An entry is 24 bytes plus a control byte at a 7/8 load (~29 B): 100_000 columns are ~2.7 MiB +/// steady, and a hashbrown rehash holds the old and the new table at once (~8.2 MiB transient). +/// Beyond the cap the oldest answers go first — the cache is reconstructible: an evicted column +/// is asked again when it is next needed, exactly as one never seen. +const KNOWN_MAX: usize = 100_000; +/// Heights older than this go in a sweep: past `HEIGHT_TTL`, due to be asked again anyway. +const SWEEP_AGE: u64 = 2000; impl Ground { /// One above the highest solid block, if known; the column is asked for @@ -376,11 +381,22 @@ impl Ground { self.world.note_settled(how); self.forget(at, 8); } - if self.known.len() > 300_000 && now.saturating_sub(self.swept) >= SWEEP_EVERY { - self.swept = now; - self.known.retain(|_, (_, when)| now.saturating_sub(*when) < 2000); + if self.known.len() > KNOWN_MAX { + self.sweep_known(now); } } + /// The cache over its budget: the oldest answers go, down to three quarters of the cap so + /// a sweep is not every tick (at most a few hundred answers arrive in one). Heights past + /// `SWEEP_AGE` go with them. A height reported changed (`STALE`) is still in use and stays: + /// it sorts as the newest and is asked again within the refresh budget. Equally old answers + /// are ordered by column, so the same cache always sweeps to the same state. + fn sweep_known(&mut self, now: u64) { + let keep = KNOWN_MAX * 3 / 4; + let mut ages: Vec<(u64, (i32, i32))> = self.known.iter().map(|(&k, (_, when))| (*when, k)).collect(); + ages.sort_unstable(); + let cut = ages[ages.len() - keep]; + self.known.retain(|&k, (_, when)| (*when, k) >= cut && now.saturating_sub(*when) < SWEEP_AGE); + } /// Heights over blocks that changed (the block cache's notes of changes, chunk replacements and a new world) /// are marked old (`when` = STALE): still used, asked again within the budget. #64. fn age_heights(&mut self) { diff --git a/airdef-mod/src/sim/record.rs b/airdef-mod/src/sim/record.rs index 051b8de..2fe8e87 100644 --- a/airdef-mod/src/sim/record.rs +++ b/airdef-mod/src/sim/record.rs @@ -176,6 +176,8 @@ impl Recording { sam_keys: 0, sam_events: 0, sam_step: STEP, + track_step: STEP, + track_points: 0, ticks: Vec::new(), times: Vec::new(), quiet: 0, @@ -194,7 +196,8 @@ impl Recording { [p.x as f32, p.y as f32, p.z as f32] }); self.index.insert(id, self.tracks.len()); - self.tracks.push(Track { id, what, first, points: points.collect(), fell: None }); + self.tracks.push(Track { id, what, first, points: points.collect(), step: STEP, fell: None }); + self.track_points += n + 1; } } @@ -208,6 +211,11 @@ impl Recording { /// Ticks between a track's points. pub const STEP: u32 = 2; +/// The flight tracks' share of a recording's memory: 2 MiB of points. Beyond it the points are +/// thinned (every other kept, the ticks between them doubled) — the same flights drawn at half +/// the resolution, the #142 pattern for the systems' poses. A recording is kept four times over +/// (live and three replays) inside the 64 MiB the whole mod lives in. +const MAX_TRACK_POINTS: usize = (2 << 20) / std::mem::size_of::<[f32; 3]>(); /// The longest recording (10 minutes) and how many are kept. const LONGEST: u32 = 12_000; const KEPT: usize = 3; @@ -234,8 +242,11 @@ pub struct Track { pub what: What, /// The recording's tick of its first point. pub first: u32, - /// Its points, `STEP` ticks apart, relative to the recording's origin. + /// Its points, `step` ticks apart, relative to the recording's origin. points: Vec<[f32; 3]>, + /// Ticks between the points: `STEP`, doubled when the recording thins its points under + /// its memory budget (the #142 pattern for the systems' poses). + step: u32, /// When it was shot down (a drone falling from then on). pub fell: Option, } @@ -374,13 +385,23 @@ pub struct SceneEvent { pub kind: SceneKind, } -/// At most this many scene transitions per recording; overflow is explicitly marked. -const MAX_SCENE_EVENTS: usize = 65_536; +/// At most this many scene transitions per recording; overflow is explicitly marked. The +/// journal's share of the 64 MiB the whole mod lives in: a recording is kept four times over +/// (live and three replays), and ~24k events of ~72 B are ~1.7 MiB each. +const MAX_SCENE_EVENTS: usize = 24_576; +/// Track transitions (acquire, handoff, lock, lost) are the journal's bulk under a large raid: +/// every system's radar sees every contact, so they grow with systems × contacts, not with what +/// happened. They have their own smaller budget (~0.6 MiB); beyond it the later ones are not +/// recorded — the chains of the contacts that came first stay whole, and the launches, fires and +/// outcomes (the facts a replay reads) keep the rest of the journal. +const MAX_SCENE_TRACK_EVENTS: usize = 8_192; #[derive(Default)] pub struct SceneJournal { events: Vec, pub truncated: bool, + /// How many of the events are track transitions (their own budget, `MAX_SCENE_TRACK_EVENTS`). + track_events: usize, tracks: HashMap<(u32, u32), (bool, bool, DVec3)>, fires: HashMap)>, fire_events: Vec, @@ -410,6 +431,10 @@ impl SceneJournal { fn push(&mut self, event: SceneEvent) { debug_assert!(self.events.last().is_none_or(|last| last.at <= event.at), "scene events are kept in time order"); if self.events.len() >= MAX_SCENE_EVENTS { self.truncated = true; return; } + if matches!(event.kind, SceneKind::TrackAcquired { .. } | SceneKind::TrackLost { .. } | SceneKind::TrackSource { .. } | SceneKind::TrackLock { .. }) { + if self.track_events >= MAX_SCENE_TRACK_EVENTS { return; } + self.track_events += 1; + } let index = self.events.len(); match event.kind { SceneKind::MissileLaunch { missile, .. } => { self.launches.insert(missile, index); } @@ -568,6 +593,10 @@ pub struct Recording { /// How many of `sam_keys` are critical transitions (#142). sam_events: usize, sam_step: u32, + /// Ticks between the flight tracks' points now (doubled when they are thinned), and how + /// many points they hold together (`MAX_TRACK_POINTS`). + track_step: u32, + track_points: usize, /// 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. ticks: Vec<(u32, u64)>, @@ -595,9 +624,13 @@ pub struct Recording { const TIME_STEP: u32 = 20; impl Track { + /// Ticks between its points (`STEP` unless the recording thinned them under its budget). + pub fn step(&self) -> u32 { + self.step + } /// Where it was at `t` (the recording's ticks, fractional), if it was. pub fn at(&self, origin: DVec3, t: f64) -> Option { - let k = (t - self.first as f64) / STEP as f64; + let k = (t - self.first as f64) / self.step as f64; if k < 0.0 || self.points.is_empty() || k > (self.points.len() - 1) as f64 { return None; } @@ -616,18 +649,18 @@ impl Track { _ => DVec3::NEG_Z, } } - /// Its `i`-th point (every `STEP` ticks from `first`), if it has one. + /// Its `i`-th point (every `step` ticks from `first`), if it has one. pub fn sample(&self, origin: DVec3, i: usize) -> Option { self.points.get(i).map(|q| origin + DVec3::new(q[0] as f64, q[1] as f64, q[2] as f64)) } /// The tick of its last point. pub fn last(&self) -> u32 { - self.first + (self.points.len().max(1) as u32 - 1) * STEP + self.first + (self.points.len().max(1) as u32 - 1) * self.step } /// Its points, every `every`-th, up to tick `t`. pub fn path(&self, origin: DVec3, every: usize, t: f64) -> Vec { let p = |q: &[f32; 3]| origin + DVec3::new(q[0] as f64, q[1] as f64, q[2] as f64); - let upto = ((t - self.first as f64) / STEP as f64).floor(); + let upto = ((t - self.first as f64) / self.step as f64).floor(); if upto < 0.0 { return Vec::new(); } @@ -801,6 +834,8 @@ impl Recording { sam_keys: 0, sam_events: 0, sam_step: STEP, + track_step: STEP, + track_points: 0, ticks: Vec::new(), times: Vec::new(), quiet: 0, @@ -922,22 +957,20 @@ impl Recording { /// What the recording holds, about: tracks and their points, the event and scene /// journals, the systems' poses and the by-time index (see `sim::memory`). pub fn bytes(&self) -> usize { + let (tracks, journal, poses, misc) = self.memory_parts(); + tracks + journal + poses + misc + } + + /// The recording's memory in four parts: the flight tracks and their points, the event + /// and scene journals, the systems' poses, and the rest (index, snapshots, clocks). + pub fn memory_parts(&self) -> (usize, usize, usize, usize) { use super::memory::{map_bytes, vec_bytes}; - let mut n = vec_bytes(&self.tracks) + let tracks = vec_bytes(&self.tracks) + self.tracks.iter().map(|t| vec_bytes(&t.points)).sum::() - + map_bytes(self.index.len(), std::mem::size_of::() + std::mem::size_of::()) - + vec_bytes(&self.events) - + vec_bytes(&self.sam_tracks) - + self.sam_tracks.iter().map(|t| vec_bytes(&t.keys)).sum::() - + map_bytes(self.sam_index.len(), std::mem::size_of::() + std::mem::size_of::()) - + vec_bytes(&self.ticks) - + vec_bytes(&self.times) - + vec_bytes(&self.sams) - + vec_bytes(&self.objects) - + self.index_bytes() - + std::mem::size_of::(); + + map_bytes(self.index.len(), std::mem::size_of::() + std::mem::size_of::()); + let mut journal = vec_bytes(&self.events); if let Some(scene) = &self.scene { - n += vec_bytes(&scene.events) + journal += vec_bytes(&scene.events) + map_bytes(scene.tracks.len(), std::mem::size_of::<(u32, u32)>() + std::mem::size_of::<(bool, bool, DVec3)>()) + map_bytes(scene.fires.len(), std::mem::size_of::() + std::mem::size_of::<(DVec3, u32, f32, bool, bool, Option)>()) + vec_bytes(&scene.fire_events) @@ -945,7 +978,16 @@ impl Recording { + map_bytes(scene.ignitions.len(), std::mem::size_of::() + std::mem::size_of::()) + map_bytes(scene.burnouts.len(), std::mem::size_of::() + std::mem::size_of::()); } - n + let poses = vec_bytes(&self.sam_tracks) + + self.sam_tracks.iter().map(|t| vec_bytes(&t.keys)).sum::() + + map_bytes(self.sam_index.len(), std::mem::size_of::() + std::mem::size_of::()); + let misc = vec_bytes(&self.ticks) + + vec_bytes(&self.times) + + vec_bytes(&self.sams) + + vec_bytes(&self.objects) + + self.index_bytes() + + std::mem::size_of::(); + (tracks, journal, poses, misc) } /// The bytes the by-time index adds to a recording (its own journal aside). @@ -1156,12 +1198,42 @@ impl Recording { let q = [rel.x as f32, rel.y as f32, rel.z as f32]; let now = self.len; match self.index.get(&id) { - Some(&i) => self.tracks[i].points.push(q), + Some(&i) => { + let track = &mut self.tracks[i]; + // The first point is exact (a birth, a muzzle); the rest fall on the track's + // own grid, which the thinning doubles with the points it keeps. + if (now - track.first) % track.step != 0 { + return; + } + track.points.push(q); + self.track_points += 1; + } None => { self.index.insert(id, self.tracks.len()); - self.tracks.push(Track { id, what, first: now, points: vec![q], fell: None }); + self.tracks.push(Track { id, what, first: now, points: vec![q], step: self.track_step, fell: None }); + self.track_points += 1; } } + if self.track_points > MAX_TRACK_POINTS { + self.thin_track_points(); + } + } + /// Halves every track's points (the first stays, then every other), doubling the ticks + /// between them: the same flights drawn at half the resolution, within the budget (the + /// #142 pattern for the systems' poses). The next points fall on the new grid. + fn thin_track_points(&mut self) { + for track in &mut self.tracks { + let mut kept = 0; + for read in (0..track.points.len()).step_by(2) { + track.points[kept] = track.points[read]; + kept += 1; + } + track.points.truncate(kept); + track.points.shrink_to_fit(); + track.step = track.step.saturating_mul(2); + } + self.track_step = self.track_step.saturating_mul(2); + self.track_points = self.tracks.iter().map(|t| t.points.len()).sum(); } /// The time of day (ticks, 24000 a day) the world showed at the recording's tick `t`: the /// clock as sampled, run on between samples at the pace it ran (a jump is a step), or None @@ -1316,6 +1388,10 @@ impl Sim { pub fn replay(&self, number: u32) -> Option<&Recording> { self.replays.iter().find(|r| r.number == number) } + /// The recording in progress, if a raid is being recorded (the memory diagnostics read it). + pub fn recording(&self) -> Option<&Recording> { + self.rec.as_ref() + } } impl Sim { @@ -2067,8 +2143,10 @@ mod tests { r.note_tracks(&sim.sams); } let scene = r.scene.as_ref().unwrap(); - assert_eq!(scene.events.len(), MAX_SCENE_EVENTS); - assert!(scene.truncated); + // A pure track-transition stream stops at the transitions' own budget: the rest of the + // journal stays free for the launches, fires and outcomes. + assert_eq!(scene.events.len(), MAX_SCENE_TRACK_EVENTS); + assert!(!scene.truncated); eprintln!("scene stress: 12000 ticks, {} events, event storage {:.2} MiB, elapsed {:?}", scene.events.len(), scene.events.capacity() * std::mem::size_of::() / (1024 * 1024), started.elapsed()); } diff --git a/airdef-mod/src/sim/regress.rs b/airdef-mod/src/sim/regress.rs index 0d8b576..8d8678a 100644 --- a/airdef-mod/src/sim/regress.rs +++ b/airdef-mod/src/sim/regress.rs @@ -553,24 +553,31 @@ fn retarget_jobs_of_one_version_age_at_their_own_tick() { assert_ne!(r3, r4, "a new version without a new report kept the old belief"); } -// #31: a big cache of fresh columns is not scanned on every answer +// #31: the ground cache is bounded — beyond the cap the oldest answers go, and the sweep +// lands well under the cap so it is not every answer (the fullscreen-map OOM: a cache that +// only swept above 300_000 fresh columns grew without bound under a wide query pattern). #[test] -fn big_ground_cache_is_swept_rarely() { +fn big_ground_cache_is_bounded_and_swept_rarely() { let mut ground = Ground::default(); - ground.known.extend((0..300_001).map(|x| ((x, 0), (64, 2000)))); + ground.known.extend((0..KNOWN_MAX as i32).map(|x| ((x, 0), (64, 2000)))); ground.known.insert((-1, 0), (64, 0)); - // The first answer past the threshold sweeps what is stale and keeps what is fresh. + // The answer past the cap sweeps: the stale column goes, the fresh stay, and the cache + // lands at three quarters of the cap. ground.answer(vec![(-2, 0, 64)], 2100); assert!(!ground.known.contains_key(&(-1, 0)), "a stale column was never swept"); - assert!(ground.known.len() > 300_000); - // The next ones wait their pause: a stale column added now survives them. + assert!(ground.known.len() <= KNOWN_MAX * 3 / 4 + 1, "the sweep did not bound the cache: {}", ground.known.len()); + assert!(ground.known.len() > KNOWN_MAX / 2, "the sweep took too much: {}", ground.known.len()); + // The next answers do not sweep again at once: a stale column added now survives them. ground.known.insert((-1, 0), (64, 0)); - let swept = ground.swept; + let len = ground.known.len(); ground.answer(vec![(-3, 0, 64)], 2101); - assert_eq!(ground.swept, swept, "swept again at once"); + assert_eq!(ground.known.len(), len + 1, "swept again at once"); assert!(ground.known.contains_key(&(-1, 0))); - ground.answer(vec![(-4, 0, 64)], 2100 + SWEEP_EVERY); - assert!(!ground.known.contains_key(&(-1, 0)), "never swept again"); + // An evicted column is asked again when it is next needed: the cache is reconstructible. + let evicted = (0, 0); + assert!(!ground.known.contains_key(&evicted)); + assert!(ground.at(DVec3::new(evicted.0 as f64, 0.0, 0.0), 2102).is_none()); + assert!(ground.take_wanted().contains(&evicted), "an evicted column was not asked again"); } // #32: the pieces of an Oreshnik teach the enemy about the sector it came from diff --git a/airdef-mod/tests/ground_oom.rs b/airdef-mod/tests/ground_oom.rs new file mode 100644 index 0000000..80b27e9 --- /dev/null +++ b/airdef-mod/tests/ground_oom.rs @@ -0,0 +1,210 @@ +//! The fullscreen-map OOM replica (the `Ground::answer` rehash crash): a large raid watched +//! from all sides through a long session, with the wide ground-query pattern the command map +//! adds on top of the simulation's own. 64 integrated complexes stand where `/airdef deploy` +//! puts them; `airdef wave oreshnik 12 2600` orders come from all sides every 1500 ticks (six +//! raids, so recordings close and the kept replays pile up), and a smart raid maps its whole +//! grid. The render answers are produced exactly as the WASM test host produces them +//! (`horizon_mods::world_query::trace`, flat land at y=64). While the fight runs, a map pass +//! every other tick (the map's redraw pace) sweeps ground queries over a wide view — the +//! fullscreen map asks for the land under its whole grid, not just the flights' columns. +//! +//! The test measures what each consumer holds (`Sim::memory`) through the session, asserts the +//! peak fits the 64 MiB WASM linear memory with headroom for a hashbrown rehash transient, and +//! that the fight still resolves: intercepts happen, the sky clears, and the same seed replays +//! the same ordered combat trace. +//! +//! Run: `cargo test --test ground_oom -- --nocapture`. +use airdef::sim::world::{Fill, RayReply, SectionAnswer}; +use airdef::sim::{Aim, Approach, DroneKind, Ground, MemoryReport, Out, RaidOrder, Sam, SamKind, Sim}; +use glam::DVec3; +use horizon_mods::world_query::{self, BlockProbe, Cell, Trace}; + +/// The seed the mod derives on the first tick of a fresh world (`open_world`, game_time 1). +const SEED: u64 = 1u64.wrapping_mul(2654435761) ^ 0x9e37; +const PLAYER: DVec3 = DVec3::new(0.5, 64.0, 0.5); +const DEPLOY_AT: DVec3 = DVec3::new(0.5, 64.0, 8.5); +const GROUND_Y: i32 = 64; +const KINDS: [SamKind; 7] = [SamKind::S400, SamKind::Patriot, SamKind::Pantsir, SamKind::Gepard, SamKind::Nasams, SamKind::IrisT, SamKind::Tor]; +/// The engine's cap on a mod's linear memory (`horizon-mods` runtime `MEMORY`). +const WASM_MEMORY: usize = 64 << 20; +/// The peak the mod must stay under: headroom for the allocator, the transient query/answer +/// vectors and save serialization on top of the measured steady state. +const PEAK_BUDGET: usize = 48 << 20; +/// Six raids fought out, recordings closed and replays kept; long enough for the caches to fill. +const TICKS: u64 = 10_000; +/// A wave every 1500 ticks: the sky clears between raids, so each is its own recording. +const WAVE_EVERY: u64 = 1500; + +struct Flat; +impl BlockProbe for Flat { + fn cell(&mut self, _x: i32, y: i32, _z: i32) -> Cell<'_> { + if y < GROUND_Y { Cell::Solid(None) } else { Cell::Empty } + } +} + +fn section_fill(sec: (i32, i32, i32)) -> SectionAnswer { + let base = i64::from(sec.1) * 16; + let solid = (i64::from(GROUND_Y) - base).clamp(0, 16) as usize; + let fill = match solid { + 0 => Fill::Empty, + 16 => Fill::Full, + _ => { + let mut bits = [0u8; 512]; + bits[..solid * 32].fill(255); + Fill::Mixed(Box::new(bits)) + } + }; + SectionAnswer { sec, fill } +} + +fn ray_reply(id: u32, a: DVec3, b: DVec3) -> RayReply { + match world_query::trace(&mut Flat, [a.x, a.y, a.z], [b.x, b.y, b.z]) { + Trace::Clear => RayReply { id, clear: true, hit: false, fraction: 1.0, normal: [0; 3], block: [0; 3] }, + Trace::Hit { fraction, normal, block } => RayReply { id, clear: false, hit: true, fraction, normal, block }, + Trace::Unknown { .. } => RayReply { id, clear: false, hit: false, fraction: 0.0, normal: [0; 3], block: [0; 3] }, + } +} + +fn wave(sim: &mut Sim, out: &mut Out, count: u32, aim: Aim) -> u32 { + let o = RaidOrder { kinds: vec![(DroneKind::Oreshnik, count)], approach: Approach::AllSides, dist: 2600.0, aim }; + sim.order_raid(&o, &[PLAYER], out) +} + +fn deploy(sim: &mut Sim) { + for _ in 0..9 { + for kind in KINDS { + let id = sim.id(); + sim.sams.push(Sam::new(id, kind, DEPLOY_AT, std::f64::consts::PI)); + sim.settle(id); + } + } + let id = sim.id(); + sim.sams.push(Sam::new(id, SamKind::S400, DEPLOY_AT, std::f64::consts::PI)); + sim.settle(id); + assert_eq!(sim.sams.len(), 64); +} + +/// One run of the heavy session: the combat trace (ordered, for the determinism check), the +/// peak memory report and the counters at the end. +struct Run { + trace: Vec<(u64, u32, u32, usize)>, + peak: MemoryReport, + peak_total: usize, + peak_known: usize, + peak_airborne: usize, + sim: Sim, +} + +fn run() -> Run { + let mut sim = Sim::new(SEED); + let mut ground = Ground::default(); + let mut out = Out::default(); + let mut columns: Vec<(i32, i32, i32)> = Vec::new(); + let mut sections: Vec = Vec::new(); + let mut rays: Vec = Vec::new(); + let mut changes_epoch = 0u32; + let mut run = Run { trace: Vec::new(), peak: MemoryReport::default(), peak_total: 0, peak_known: 0, peak_airborne: 0, sim: Sim::new(SEED) }; + for now in 1..=TICKS { + if now == 3 { + deploy(&mut sim); + } + if now >= 3 && now < 8000 && (now - 3) % WAVE_EVERY == 0 { + assert!(wave(&mut sim, &mut out, 12, Aim::Objects) > 0, "wave at tick {now} was refused"); + } + if now == 1203 { + assert!(wave(&mut sim, &mut out, 10, Aim::Smart) > 0, "the smart raid was refused"); + } + if now == 1103 { + sim.shader = true; + } + let now0 = sim.tick; + ground.world.changed_rev(changes_epoch, 0, false, &[]); + ground.answer(std::mem::take(&mut columns), now0); + ground.world.answer(std::mem::take(&mut sections), now0); + ground.world.answer_rays(&std::mem::take(&mut rays)); + sim.day_time = Some(now); + sim.night = (13000..23000).contains(&(now % 24000)); + out = Out::default(); + sim.step(&mut ground, Some(PLAYER), &mut out); + // The fullscreen map redraws every other tick and asks for the land under its grid: + // a wide walk over the view (32 fresh columns a redraw), as panning and placing do. + if now % 2 == 0 { + let t = now as f64; + let x0 = (t * 13.37).rem_euclid(8000.0) - 4000.0; + let z0 = (t * 7.91).rem_euclid(8000.0) - 4000.0; + for k in 0..32 { + let _ = ground.at(DVec3::new(x0 + k as f64 * 8.0, 0.0, z0 + k as f64 * 3.0), sim.tick); + } + } + columns = ground.take_wanted().into_iter().map(|(x, z)| (x, z, GROUND_Y)).collect(); + let _chunks = ground.take_chunks(); + let rq = ground.world.take_rays(now); + let sq = ground.world.take_wanted(now); + if !rq.is_empty() || !sq.is_empty() { + changes_epoch = 1; + } + rays = rq.into_iter().map(|(id, a, b)| ray_reply(id, a, b)).collect(); + sections = sq.into_iter().map(section_fill).collect(); + + let airborne = sim.drones.iter().filter(|d| d.alive()).count(); + run.peak_airborne = run.peak_airborne.max(airborne); + run.peak_known = run.peak_known.max(ground.known_len()); + if now % 100 == 0 { + let m = sim.memory(&ground); + let total = m.total(); + if total > run.peak_total { + run.peak_total = total; + run.peak = m; + } + run.trace.push((now, sim.kills, sim.hits, airborne)); + } + } + run.sim = sim; + run +} + +#[test] +fn the_heavy_map_session_fits_the_memory_budget_and_the_fight_resolves() { + let run = run(); + let m = run.peak; + eprintln!("peak airborne {}, peak known columns {}", run.peak_airborne, run.peak_known); + eprintln!("peak memory at the worst sample:\n{}", m.text()); + if let Some(rec) = run.sim.recording() { + let (tracks, journal, poses, misc) = rec.memory_parts(); + eprintln!("live recording: tracks {:.2} MiB, journal {:.2} MiB, poses {:.2} MiB, misc {:.2} MiB", tracks as f64 / (1024.0 * 1024.0), journal as f64 / (1024.0 * 1024.0), poses as f64 / (1024.0 * 1024.0), misc as f64 / (1024.0 * 1024.0)); + } + for (i, rec) in run.sim.replays.iter().enumerate() { + let (tracks, journal, poses, misc) = rec.memory_parts(); + eprintln!("replay {}: len {} ticks, tracks {:.2} MiB, journal {:.2} MiB, poses {:.2} MiB, misc {:.2} MiB", i + 1, rec.len, tracks as f64 / (1024.0 * 1024.0), journal as f64 / (1024.0 * 1024.0), poses as f64 / (1024.0 * 1024.0), misc as f64 / (1024.0 * 1024.0)); + } + eprintln!( + "end: tick {}, kills {}, hits {}, crashes {}, airborne {}, replays {}", + run.sim.tick, + run.sim.kills, + run.sim.hits, + run.sim.crashes, + run.sim.drones.iter().filter(|d| d.alive()).count(), + run.sim.replays.len() + ); + // The load is the crash's load: a large raid, more than 200 objects in the sky at once. + assert!(run.peak_airborne > 200, "the replica never reached the crash's load: {}", run.peak_airborne); + // The fight resolves: intercepts happen and the sky clears by the end of the session. + assert!(run.sim.kills > 100, "the defence stopped intercepting: {}", run.sim.kills); + assert_eq!(run.sim.drones.iter().filter(|d| d.alive()).count(), 0, "the raid never fought out"); + // The memory budget: the steady peak plus the worst rehash transient (the old and the new + // table of the biggest map held at once) stays under the headroom target, far under the cap. + let peak = run.peak_total + m.rehash_headroom(); + assert!(peak < PEAK_BUDGET, "peak {} MiB (steady {} + rehash {}) over the {} MiB budget:\n{}", peak >> 20, run.peak_total >> 20, m.rehash_headroom() >> 20, PEAK_BUDGET >> 20, m.text()); + assert!(peak < WASM_MEMORY, "peak {} MiB over the engine's {} MiB cap", peak >> 20, WASM_MEMORY >> 20); +} + +#[test] +fn the_same_seed_replays_the_same_ordered_combat_trace() { + let a = run(); + let b = run(); + assert_eq!(a.trace, b.trace, "the combat trace diverged between identical runs"); + assert_eq!(a.sim.kills, b.sim.kills); + assert_eq!(a.sim.hits, b.sim.hits); + assert_eq!(a.sim.tally.text(), b.sim.tally.text()); + assert_eq!(a.peak_known, b.peak_known); +}