Files
airdef/airdef-mod/tests/saturation.rs
T
chexit aa842d8a1f Fix raid gameplay, finite ammunition and truck routing
Check launch terrain, reserve ballistic targets for marked objectives, tune in-game reach and fallback fire, and support bounded terrain work on supply routes.

Co-Authored-By: Codex <noreply@openai.com>
2026-11-25 18:53:17 +03:00

170 lines
7.9 KiB
Rust

//! The saturation regression (#145): a deterministic native replica of the WASM budget
//! scenario's checkpoint (`tests/budgets/wasm_flat_budget.toml`, tick 2102). 64 integrated
//! complexes stand where `/airdef deploy` puts them (8 blocks south of the player at the world
//! origin), two `airdef wave oreshnik 40 2600` orders 100 ticks apart, flat land at y=64, the
//! engine's own seed for the first morning. The render answers are produced exactly as the WASM
//! test host produces them (`horizon_mods::world_query::trace`, the same section fill), so the
//! replica replays the same fight the heavy run measures. It exists to explain and guard the
//! intercept count under saturation: the counters and `Sim::launch_audit` say why the systems
//! hold their fire, which the fuel-bound WASM run cannot afford to diagnose per tick.
//!
//! Run: `cargo test --test saturation -- --nocapture`.
use airdef::sim::world::{Fill, RayReply, SectionAnswer};
use airdef::sim::{Aim, Approach, DroneKind, Ground, 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;
/// Where the test player stands, and where every deploy lands (`deploy_configured`: 8 blocks ahead).
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;
/// The fixture's deploy order: nine rounds of seven kinds, then one more S-400 (64 complexes).
const KINDS: [SamKind; 7] = [SamKind::S400, SamKind::Patriot, SamKind::Pantsir, SamKind::Gepard, SamKind::Nasams, SamKind::IrisT, SamKind::Tor];
/// The checkpoint the WASM fixture asserts at step 271.
const CHECKPOINT: u64 = 2102;
/// The flat world the test host answers from: solid below `GROUND_Y`, air above.
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 }
}
}
/// A section's fill, exactly as `answer_flat_world` computes it.
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 }
}
/// A ray's reply, exactly as `answer_flat_world` traces it.
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 order(sim: &mut Sim, out: &mut Out) -> u32 {
let o = RaidOrder { kinds: vec![(DroneKind::Oreshnik, 40)], approach: Approach::Sector, dist: 2600.0, aim: Aim::Objects };
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);
}
/// Steps the replica to `ticks`, ordering the two waves and the HD effects where the fixture does,
/// and answering render queries one tick later exactly as the WASM host does.
fn run_to(ticks: u64) -> Sim {
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<SectionAnswer> = Vec::new();
let mut rays: Vec<RayReply> = Vec::new();
let mut changes_epoch = 0u32;
for now in 1..=ticks {
// Commands run between ticks: deploys and the first wave after tick 2, the second after
// tick 102, `airdef shader hd` after tick 1102 (effects only, but kept exact).
if now == 3 {
deploy(&mut sim);
assert!(order(&mut sim, &mut out) > 0, "the first wave was refused");
}
if now == 103 {
assert!(order(&mut sim, &mut out) > 0, "the second wave was refused (the first still planning)");
}
if now == 1103 {
sim.shader = true;
}
// The mod's tick reads the previous tick's answers at the clock it had then (sim.tick).
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 queries this tick asks; the host answers them before the next 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();
}
sim
}
/// No depot is deployed in this fixture. Both waves spend the initial ammunition;
/// the checkpoint records the combat outcome with finite magazines and the extended ranges.
const REPLICA_KILLS: u32 = 144;
const REPLICA_HITS: u32 = 2152;
const REPLICA_AIRBORNE: usize = 184;
const REPLICA_FIRED: u32 = 355;
#[test]
fn checkpoint_replica_matches_the_finite_ammunition_baseline() {
let sim = run_to(CHECKPOINT);
let airborne = sim.drones.iter().filter(|d| d.alive()).count();
let fired: u32 = sim.sams.iter().map(|s| s.fired).sum();
eprintln!(
"checkpoint tick {}: airborne {airborne}, missiles {}, kills {}, hits {}, fired {}",
sim.tick,
sim.missiles.len(),
sim.kills,
sim.hits,
fired
);
eprintln!("tally: {}", sim.tally.text());
eprintln!("audit: {}", sim.launch_audit().text());
assert_eq!(airborne, REPLICA_AIRBORNE, "the sky at the checkpoint");
assert_eq!(sim.kills, REPLICA_KILLS, "intercepts at the checkpoint (deterministic replica baseline)");
assert_eq!(sim.hits, REPLICA_HITS, "hits at the checkpoint");
assert_eq!(fired, REPLICA_FIRED, "launches at the checkpoint");
assert!(sim.sams.iter().all(|s| s.fired <= s.kind.missile_magazine().unwrap_or(0)),
"a battery fired more than its initial load with no depot");
}
#[test]
fn the_saturation_loadout_has_no_separate_radar_channels_to_hold() {
// #140 (stale own fire channels held by network relays) is a separate-radar bug: it binds only
// where a profile has channels. This loadout deploys integrated complexes alone — every radar
// is the weapon's embedded one, whose profile has no channel limit — so #140 cannot affect it.
// Fact, not assumption: the A/B that relaxed the channel limit changed nothing (243 -> 243).
let sim = run_to(CHECKPOINT);
assert_eq!(sim.sams.len(), 64);
for s in &sim.sams {
assert_ne!(s.kind, SamKind::Radar, "the fixture deploys no separate radar");
assert_eq!(s.radar_profile().channels, 0, "an embedded radar has no channel limit");
}
}