From b43d47170626e5a403d4c210246cce8fd4b763bc Mon Sep 17 00:00:00 2001 From: chexit Date: Fri, 30 Oct 2026 14:57:19 +0300 Subject: [PATCH] Combat fixes: exact-ray answers tied to their segment and block (#73), no fuse kill through a wall while waiting (#74), seeker needs confirmed sight and stops reading true velocity (#75), combat RNG and ids independent of graphics (#85), saved layouts validated whole before the scene is touched, refused autosave kept (#86) Co-Authored-By: Claude Opus 5.5 --- airdef-mod/src/layout.rs | 201 +++++++++++++++++++++++++++ airdef-mod/src/lib.rs | 143 ++++++++++---------- airdef-mod/src/scenario.rs | 4 +- airdef-mod/src/sim/mod.rs | 78 +++++++---- airdef-mod/src/sim/regress.rs | 248 +++++++++++++++++++++++++++++++++- airdef-mod/src/sim/units.rs | 2 +- airdef-mod/src/sim/world.rs | 122 +++++++++++++---- airdef-mod/src/wave.rs | 21 +++ 8 files changed, 695 insertions(+), 124 deletions(-) create mode 100644 airdef-mod/src/layout.rs diff --git a/airdef-mod/src/layout.rs b/airdef-mod/src/layout.rs new file mode 100644 index 0000000..6226697 --- /dev/null +++ b/airdef-mod/src/layout.rs @@ -0,0 +1,201 @@ +//! The text of a saved layout (the autosave, the body of a scenario) read and checked as a whole before +//! anything of the scene is touched (#86). `Layout::parse` is pure: it either returns every record with its +//! values proven (finite, in range, within the limits) or the line and field that are wrong; `AirDef` swaps the +//! scene for it only after that. Lines it doesn't know are skipped, so older and newer files read alike. +use super::*; + +/// Most systems and most objects one file may hold. +pub const MAX_UNITS: usize = 2000; +/// Largest coordinate: the world's border and then some. +const COORD: f64 = 3.0e7; +/// Largest count (missiles, bursts, cargo, reload ticks). +const COUNT: f64 = 1.0e6; + +pub struct SamRec { + pub kind: SamKind, + pub pos: DVec3, + pub heading: f64, + pub roles: Option, + pub health: Option, + pub cargo: Option, + /// Missiles, bursts, empty, reloading: older saves have no ammunition (full). + pub ammo: Option<(u32, u32, bool, u32)>, + pub dest: Option, + /// The parts, from the sensor on; older saves have fewer or none. + pub comps: Vec, +} + +#[derive(Default)] +pub struct Layout { + pub sams: Vec, + pub targets: Vec<(DVec3, f64)>, + pub damage: Option, + pub seed: Option, + pub wave: Option>, + pub far: Option, + pub effects: Option<(bool, bool)>, + pub roles: Vec<(SamKind, u8)>, +} + +/// One line's words, with the line number for the messages. +struct Line<'a> { + n: usize, + word: &'a str, + w: Vec<&'a str>, +} + +impl Line<'_> { + fn bad(&self, field: &str, why: &str) -> String { + format!("строка {}: «{}», поле {field}: {why}", self.n, self.word) + } + /// A finite number in `lo..=hi`; `None` where the field is absent. + fn num(&self, i: usize, field: &str, lo: f64, hi: f64) -> Result, String> { + let Some(v) = self.w.get(i) else { return Ok(None) }; + let x: f64 = v.parse().map_err(|_| self.bad(field, &format!("«{v}» не число")))?; + if !x.is_finite() { + return Err(self.bad(field, &format!("«{v}» не конечное число"))); + } + if x < lo || x > hi { + return Err(self.bad(field, &format!("{x} вне допустимого {lo}..{hi}"))); + } + Ok(Some(x)) + } + fn need(&self, i: usize, field: &str, lo: f64, hi: f64) -> Result { + self.num(i, field, lo, hi)?.ok_or_else(|| self.bad(field, "строка оборвана")) + } + fn flag(&self, i: usize, field: &str) -> Result, String> { + match self.w.get(i) { + None => Ok(None), + Some(&"true") => Ok(Some(true)), + Some(&"false") => Ok(Some(false)), + Some(v) => Err(self.bad(field, &format!("«{v}»: нужно true или false"))), + } + } + fn kind(&self, i: usize) -> Result { + let v = self.w.get(i).ok_or_else(|| self.bad("тип", "строка оборвана"))?; + SamKind::from_key(v).ok_or_else(|| self.bad("тип", &format!("неизвестный тип «{v}»"))) + } + fn xyz(&self, i: usize, field: &str) -> Result { + Ok(DVec3::new(self.need(i, field, -COORD, COORD)?, self.need(i + 1, field, -COORD, COORD)?, self.need(i + 2, field, -COORD, COORD)?)) + } +} + +impl Layout { + pub fn parse(text: &str) -> Result { + let mut out = Layout::default(); + for (n, raw) in text.lines().enumerate() { + let w: Vec<&str> = raw.split_whitespace().collect(); + let Some(&word) = w.first() else { continue }; + let l = Line { n: n + 1, word, w }; + match word { + "sam" => { + if out.sams.len() >= MAX_UNITS { + return Err(l.bad("число комплексов", &format!("больше {MAX_UNITS}"))); + } + let kind = l.kind(1)?; + let pos = l.xyz(2, "координаты")?; + let heading = l.need(5, "курс", -1.0e6, 1.0e6)?; + let roles = l.num(6, "роли", 0.0, 255.0)?.map(|r| r as u8); + let health = l.num(7, "прочность", 0.0, 1.0)?; + let cargo = l.num(8, "груз", 0.0, COUNT)?.map(|c| c as u32); + let ammo = match (l.num(9, "ракеты", 0.0, COUNT)?, l.num(10, "очереди", 0.0, COUNT)?) { + (Some(m), Some(b)) => Some((m as u32, b as u32, l.num(11, "пуст", 0.0, 1.0)? == Some(1.0), l.num(12, "перезарядка", 0.0, COUNT)?.map_or(0, |r| r as u32))), + _ => None, + }; + // "- - -": no destination. + let dest = if l.w.get(13) == Some(&"-") { None } else if l.w.len() > 13 { Some(l.xyz(13, "цель движения")?) } else { None }; + let mut comps = Vec::new(); + for k in 0..6 { + match l.num(16 + k, "часть", 0.0, 1.0)? { + Some(c) => comps.push(c), + None => break, + } + } + out.sams.push(SamRec { kind, pos, heading, roles, health, cargo, ammo, dest, comps }); + } + "target" => { + if out.targets.len() >= MAX_UNITS { + return Err(l.bad("число объектов", &format!("больше {MAX_UNITS}"))); + } + let pos = l.xyz(1, "координаты")?; + let hp = l.num(4, "прочность", 0.0, 100.0)?.unwrap_or(100.0); + out.targets.push((pos, hp)); + } + "damage" => out.damage = l.flag(1, "значение")?, + "far" => out.far = l.flag(1, "значение")?, + "effects" => out.effects = Some((l.flag(1, "шейдер")?.unwrap_or(true), l.flag(2, "HD")?.unwrap_or(false))), + "seed" => { + let v = l.w.get(1).ok_or_else(|| l.bad("значение", "строка оборвана"))?; + out.seed = Some(v.parse::().map_err(|_| l.bad("значение", &format!("«{v}» не число")))?); + } + "wave" => { + crate::wave::WaveConfig::default().check(&l.w[1..]).map_err(|e| l.bad("налёт", &e))?; + out.wave = Some(l.w[1..].iter().map(|s| s.to_string()).collect()); + } + "roles" => { + let kind = l.kind(1)?; + out.roles.push((kind, l.need(2, "роли", 0.0, 255.0)? as u8)); + } + _ => {} + } + } + Ok(out) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn key() -> &'static str { + SamKind::Patriot.key() + } + + // #86: the issue's file, and the other ways a saved state can be wrong: refused whole, with the line + #[test] + fn a_damaged_state_is_refused_as_a_whole_with_its_line_and_field() { + let good = format!("sam {} 10 70 20 0.5\ntarget 1 2 3 100\n", key()); + assert!(Layout::parse(&good).is_ok()); + let cases: Vec<(String, usize, &str)> = vec![ + ("target NaN inf 0 -100\nsam s400 NaN 70 0 0\n".into(), 1, "координаты"), + (format!("{good}target 1 inf 3 100\n"), 3, "не конечное"), + (format!("{good}target 1 2 3 -100\n"), 3, "прочность"), + (format!("{good}target 1 2 3 NaN\n"), 3, "не конечное"), + (format!("{good}target 1 2\n"), 3, "оборвана"), + (format!("{good}sam {} 1 2\n", key()), 3, "оборвана"), + (format!("{good}sam nosuchkind 1 2 3 0\n"), 3, "тип"), + (format!("{good}sam {} 1 2 3 0 0 7.5\n", key()), 3, "прочность"), + (format!("{good}sam {} 1 2 3 0 0 1 0 1 1 0 0 - - - 0.5 abc\n", key()), 3, "часть"), + (format!("{good}sam {} 1e400 2 3 0\n", key()), 3, "координаты"), + (format!("{good}seed twelve\n"), 3, "значение"), + (format!("{good}wave 1,2,x 0 300 0 1\n"), 3, "налёт"), + (format!("{good}damage maybe\n"), 3, "значение"), + ]; + for (text, line, what) in cases { + let e = Layout::parse(&text).err().unwrap_or_else(|| panic!("accepted: {text}")); + assert!(e.contains(&format!("строка {line}")) && e.contains(what), "{e} for {text}"); + } + let many = "target 0 0 0 100\n".repeat(MAX_UNITS + 1); + assert!(Layout::parse(&many).is_err(), "no limit on the number of objects"); + } + + // #86: files made by older builds (no ammunition, no parts, no destination marks) and unknown lines still load + #[test] + fn older_valid_saves_still_load() { + let old = format!( + "sam {k} 10.5 70 -20 1.57\nsam {k} 1 2 3 0 3 0.8 2\nsam {k} 1 2 3 0 3 0.8 2 4 12 1 30 - - -\nsam {k} 1 2 3 0 3 1 0 4 12 0 0 5 70 5 1 0.9 0.8 0.7 0.6 0.5\ntarget 5 6 7\ntarget 5 6 7 0\ndamage false\nfar true\neffects true false\nroles {k} 3\nfuture-line with words\nscenario 1\nname x\nworld state-0\nseed 123456789\nwave 0,1,0,2,0,0,0,0,0 1 300 0 1\n", + k = key() + ); + let l = Layout::parse(&old).expect("an old valid save was refused"); + assert_eq!((l.sams.len(), l.targets.len()), (4, 2)); + assert_eq!(l.targets[0].1, 100.0); + assert_eq!(l.sams[2].ammo, Some((4, 12, true, 30))); + assert!(l.sams[2].dest.is_none() && l.sams[3].dest == Some(DVec3::new(5.0, 70.0, 5.0))); + assert_eq!(l.sams[3].comps.len(), 6); + assert_eq!((l.damage, l.far, l.effects, l.seed), (Some(false), Some(true), Some((true, false)), Some(123456789))); + assert_eq!(l.roles.len(), 1); + // What the build writes now reads back (a wave line from the menu's own text). + let w = crate::wave::WaveConfig::default().text(); + assert!(Layout::parse(&format!("wave {w}\n")).is_ok(), "{w}"); + } +} diff --git a/airdef-mod/src/lib.rs b/airdef-mod/src/lib.rs index f431760..a96d131 100644 --- a/airdef-mod/src/lib.rs +++ b/airdef-mod/src/lib.rs @@ -13,6 +13,7 @@ use horizon_mod_sdk::{ }; mod cinema; +mod layout; mod film; mod monitor; mod panel; @@ -783,83 +784,79 @@ impl AirDef { fn load_from(&mut self, key: &str) { let Some(bytes) = storage::get(key) else { return }; - self.apply_state(&String::from_utf8_lossy(&bytes)); + if let Err(e) = self.apply_state(&String::from_utf8_lossy(&bytes)) { + horizon_mod_sdk::warn(&format!("saved state not loaded: {e}")); + // The next autosave would write over it: keep the refused file for a look or a repair. + if let Err(e) = storage::set(&format!("{key}-refused"), &bytes) { + horizon_mod_sdk::warn(&format!("could not keep the refused state: {e}")); + } + } } /// Adds the state in `text` (`state_text`'s lines; unknown lines are skipped, so older and - /// newer files read alike). - fn apply_state(&mut self, text: &str) { - for line in text.lines() { - let w: Vec<&str> = line.split_whitespace().collect(); - let num = |i: usize| w.get(i).and_then(|v| v.parse::().ok()); - match w.first() { - Some(&"sam") => { - if let (Some(kind), Some(x), Some(y), Some(z), Some(h)) = - (w.get(1).and_then(|k| SamKind::from_key(k)), num(2), num(3), num(4), num(5)) - { - let id = self.sim.id(); - let mut sam = Sam::new(id, kind, DVec3::new(x, y, z), h); - if let Some(r) = w.get(6).and_then(|v| v.parse().ok()) { - sam.roles = r; - } - if let Some(h) = num(7) { - sam.health = h.clamp(0.0, 1.0); - } - if let Some(c) = num(8) { - sam.cargo = c as u32; - } - // Older saves have no ammunition: full, as before. - if let (Some(m), Some(b)) = (num(9), num(10)) { - sam.missiles = (m as u32).min(sam.missiles); - sam.bursts = (b as u32).min(sam.bursts); - sam.empty = num(11) == Some(1.0); - sam.reloading = num(12).map_or(0, |r| r as u32); - } - let dest = match (num(13), num(14), num(15)) { - (Some(x), Some(y), Some(z)) => Some(DVec3::new(x, y, z)), - _ => None, - }; - // The parts: absent in older saves, which load with every part whole. - for (k, v) in sam.comps.0.iter_mut().enumerate() { - if let Some(c) = num(16 + k) { - *v = c.clamp(0.0, 1.0); - } - } - sam.fit_cells(); - self.sim.sams.push(sam); - if let Some(to) = dest { - let i = self.sim.sams.len() - 1; - self.sim.order_move(i, to); - } - } - } - Some(&"target") => { - if let (Some(x), Some(y), Some(z)) = (num(1), num(2), num(3)) { - self.sim.targets.push(DVec3::new(x, y, z)); - self.sim.target_health.push(num(4).unwrap_or(100.0)); - } - } - Some(&"damage") => self.sim.damage = w.get(1) != Some(&"false"), - // A scenario's: the random generator's state, and the wave menu's raid. - Some(&"seed") => { - if let Some(v) = w.get(1).and_then(|v| v.parse::().ok()) { - self.sim.rng = sim::Rng::new(v); - } - } - Some(&"wave") => self.wave.parse(&w[1..]), - Some(&"far") => self.far = w.get(1) != Some(&"false"), - Some(&"effects") => { - let shader = w.get(1) != Some(&"false"); - let hd = w.get(2) == Some(&"true"); - self.set_effects(shader, hd); - } - Some(&"roles") => { - if let (Some(kind), Some(r)) = (w.get(1).and_then(|k| SamKind::from_key(k)), w.get(2).and_then(|v| v.parse().ok())) { - self.sim.roles.insert(kind, r); - } - } - _ => {} + /// newer files read alike). The whole text is read and checked first: a bad line refuses all of it, + /// naming the line and field, and the scene stays as it was (#86). + fn apply_state(&mut self, text: &str) -> Result<(), String> { + let layout = layout::Layout::parse(text)?; + self.commit_layout(layout); + Ok(()) + } + + /// Adds a checked layout to the scene: nothing here can fail. + fn commit_layout(&mut self, layout: layout::Layout) { + for r in layout.sams { + let id = self.sim.id(); + let mut sam = Sam::new(id, r.kind, r.pos, r.heading); + if let Some(v) = r.roles { + sam.roles = v; } + if let Some(h) = r.health { + sam.health = h; + } + if let Some(c) = r.cargo { + sam.cargo = c; + } + // Older saves have no ammunition: full, as before. + if let Some((m, b, empty, reloading)) = r.ammo { + sam.missiles = m.min(sam.missiles); + sam.bursts = b.min(sam.bursts); + sam.empty = empty; + sam.reloading = reloading; + } + // The parts: absent in older saves, which load with every part whole. + for (v, c) in sam.comps.0.iter_mut().zip(&r.comps) { + *v = *c; + } + sam.fit_cells(); + self.sim.sams.push(sam); + if let Some(to) = r.dest { + let i = self.sim.sams.len() - 1; + self.sim.order_move(i, to); + } + } + for (pos, hp) in layout.targets { + self.sim.targets.push(pos); + self.sim.target_health.push(hp); + } + if let Some(d) = layout.damage { + self.sim.damage = d; + } + // A scenario's: the random generator, and the wave menu's raid. + if let Some(v) = layout.seed { + self.sim.reseed(v); + } + if let Some(w) = layout.wave { + let w: Vec<&str> = w.iter().map(String::as_str).collect(); + self.wave.parse(&w); + } + if let Some(f) = layout.far { + self.far = f; + } + if let Some((shader, hd)) = layout.effects { + self.set_effects(shader, hd); + } + for (kind, r) in layout.roles { + self.sim.roles.insert(kind, r); } } diff --git a/airdef-mod/src/scenario.rs b/airdef-mod/src/scenario.rs index 69c9cd6..3235bfd 100644 --- a/airdef-mod/src/scenario.rs +++ b/airdef-mod/src/scenario.rs @@ -49,6 +49,8 @@ impl AirDef { if !force && world != self.state_key() { return Err("Сценарий сделан в другом мире: координаты могут не подойти (загрузить всё равно: добавьте force)".into()); } + // The whole body is read and checked before anything is touched (#86). + let layout = layout::Layout::parse(text).map_err(|e| format!("Сценарий не загружен, сцена осталась прежней: {e}"))?; if self.replay.is_some() { self.stop_replay(); } @@ -67,7 +69,7 @@ impl AirDef { self.moving = None; self.pending.clear(); render::set_models(&[]); - self.apply_state(text); + self.commit_layout(layout); self.dirty = true; Ok(()) } diff --git a/airdef-mod/src/sim/mod.rs b/airdef-mod/src/sim/mod.rs index f4b2105..ec708cd 100644 --- a/airdef-mod/src/sim/mod.rs +++ b/airdef-mod/src/sim/mod.rs @@ -1393,7 +1393,13 @@ pub struct Sim { /// The next blast's id (separate from `next`, so the other units' ids stay as they were). blast_seq: u32, next: u32, + /// The combat randomness: what is rolled for aim, hits, damage and raids. Drawing, sound and camera never + /// spend it (#85). pub rng: Rng, + /// The decorative randomness (flicker, pitch, spin, scatter of tracers and debris), and the ids of what is + /// only drawn: neither moves with the graphics settings of the combat. + vis: Rng, + fx_next: u32, pub tick: u64, /// The game's tick number (the one `tick` is called with), for a recording to know the world's time by. pub engine_tick: u64, @@ -1447,6 +1453,15 @@ pub struct Sim { /// Ids: objects get multiples of 16, their parts the low bits. const PARTS: u32 = 16; +/// Ids of what is only drawn (debris, fireball models, tracers) come from their own range and counter, so that +/// the number of effects never moves the ids of the combat's objects (#85). +const FX_IDS: u32 = 0x2000_0000; + +/// The next id for a drawn-only thing. +fn fx_ident(counter: &mut u32) -> u32 { + *counter = (*counter + 1) % 0x0100_0000; + FX_IDS + *counter * PARTS +} impl Sim { pub fn new(seed: u64) -> Self { @@ -1461,6 +1476,8 @@ impl Sim { blast_seq: 0, next: 1, rng: Rng::new(seed), + vis: Rng::new(seed ^ 0x5DEE_CE66_D1CE_4E5B), + fx_next: 0, tick: 0, engine_tick: 0, kills: 0, @@ -1491,6 +1508,14 @@ impl Sim { spot_hd: false, } } + /// A scenario's start: the combat generator, the dice of stateless rolls and the decorative generator all + /// come from `state` alone, whatever was played before. The clock and the id counter go on (what is in the + /// world keeps its ids), so a restart repeats the draws, not the whole fight. + pub fn reseed(&mut self, state: u64) { + self.rng = Rng::new(state); + self.vis = Rng::new(state ^ 0x5DEE_CE66_D1CE_4E5B); + self.seed = state.wrapping_mul(0x2545_F491_4F6C_DD1D) | 1; + } pub fn id(&mut self) -> u32 { self.next += 1; self.next * PARTS @@ -1645,7 +1670,7 @@ impl Sim { } else if let Some(t) = ground.unread(&sweep, &mut d.hold, below.is_some_and(|g| from.y < g + 12.0)) { d.pos = from.lerp(d.pos, t); } - if self.rng.chance(0.5) { + if self.vis.chance(0.5) { out.particles("minecraft:large_smoke", d.pos, 0.2, DVec3::new(0.0, 0.05, 0.0), 0.02, 1); } out.particles("minecraft:flame", d.pos, 0.25, DVec3::ZERO, 0.02, 2); @@ -1976,14 +2001,14 @@ impl Sim { } Engine::Jet(up, back, size) => { let at = d.pos + rot * DVec3::new(0.0, up * s, back * s); - let flicker = 0.7 + 0.3 * self.rng.f(); + let flicker = 0.7 + 0.3 * self.vis.f(); out.model(d.id + 1, "airdef:geran3_flame", at, rot, s * size * flicker, true); } Engine::Rocket(back, size) => { // Ballistic missiles fly far and high: their glow keeps a size on screen, so they are seen // all the way (the motor while it burns, then the hot body). let far = player.map_or(1.0, |p| (d.pos.distance(p) / 140.0).max(1.0)); - let flicker = 0.8 + 0.2 * self.rng.f(); + let flicker = 0.8 + 0.2 * self.vis.f(); if d.flight != Flight::Ballistic || d.age < 60 { let size = if d.flight == Flight::Ballistic { size * far.sqrt() } else { size }; let at = d.pos + rot * DVec3::new(0.0, 0.0, back * s + size * flicker); @@ -2019,7 +2044,7 @@ impl Sim { out.particles("minecraft:flame", pos, size as f32 * 0.3, DVec3::ZERO, 0.2, n); out.particles("minecraft:lava", pos, 1.0, DVec3::ZERO, 0.0, 16); out.particles("minecraft:campfire_signal_smoke", pos, 1.2, DVec3::new(0.0, 0.04, 0.0), 0.01, if big { 30 } else { 10 }); - let pitch = 0.9 + self.rng.f() as f32 * 0.2; + let pitch = 0.9 + self.vis.f() as f32 * 0.2; let event = ["airdef:blast.air", "airdef:blast.medium", "airdef:blast.big", "airdef:blast.heavy", "airdef:blast.huge"][power]; out.sound(event, pos, 1.0, if small { pitch * 1.15 } else { pitch }); if power >= 3 { @@ -2058,8 +2083,8 @@ impl Sim { if self.shader { return; } - let id = self.id(); - let spin = DQuat::from_euler(glam::EulerRot::XYZ, self.rng.s() * 0.2, self.rng.s() * 0.2, self.rng.s() * 0.2); + let id = fx_ident(&mut self.fx_next); + let spin = DQuat::from_euler(glam::EulerRot::XYZ, self.vis.s() * 0.2, self.vis.s() * 0.2, self.vis.s() * 0.2); self.fx.push((id, Fx::Fireball { pos, size, age: 0, life, spin })); } @@ -2095,11 +2120,11 @@ impl Sim { out.particles("minecraft:explosion", pos, 0.6, DVec3::ZERO, 0.0, 4); out.particles("minecraft:large_smoke", pos, 1.0, DVec3::ZERO, 0.06, 25); out.particles("minecraft:firework", pos, 0.3, DVec3::ZERO, 0.35, 30); - out.sound("airdef:blast.air", pos, gain, 1.0 + self.rng.f() as f32 * 0.2); + out.sound("airdef:blast.air", pos, gain, 1.0 + self.vis.f() as f32 * 0.2); for _ in 0..5 { - let id = self.id(); - let v = vel + DVec3::new(self.rng.s(), self.rng.f() * 0.8, self.rng.s()) * 0.35; - let spin = DQuat::from_euler(glam::EulerRot::XYZ, self.rng.s() * 0.4, self.rng.s() * 0.4, self.rng.s() * 0.4); + let id = fx_ident(&mut self.fx_next); + let v = vel + DVec3::new(self.vis.s(), self.vis.f() * 0.8, self.vis.s()) * 0.35; + let spin = DQuat::from_euler(glam::EulerRot::XYZ, self.vis.s() * 0.4, self.vis.s() * 0.4, self.vis.s() * 0.4); self.fx.push((id, Fx::Debris { pos, vel: v, rot: DQuat::IDENTITY, spin, age: 0 })); } } @@ -2333,16 +2358,15 @@ impl Sim { let dir = (lead - muzzle).normalize_or(DVec3::NEG_Z); let len = muzzle.distance(lead).min(gun.range * 1.2); // Every round is a red tracer flying out at the shell's speed. - let scatter = DVec3::new(self.rng.s(), self.rng.s(), self.rng.s()) * 0.012; - let id = self.next * PARTS + 0x0800_0000; - self.next += 1; + let scatter = DVec3::new(self.vis.s(), self.vis.s(), self.vis.s()) * 0.012; + let id = fx_ident(&mut self.fx_next); tracers.push((id, muzzle + dir * 1.5, (dir + scatter).normalize() * gun.shell, (len / gun.shell) as u32 + 2)); out.particles("minecraft:smoke", muzzle, 0.1, dir * 0.05, 0.01, 2); } // A gun sound holds a few shots: one every other burst. if sam.bursts % 2 == 0 { let event = if sam.kind == SamKind::Pantsir { "airdef:gun.pantsir" } else { "airdef:gun.gepard" }; - out.sound(event, mount, 1.0, 0.95 + self.rng.f() as f32 * 0.1); + out.sound(event, mount, 1.0, 0.95 + self.vis.f() as f32 * 0.1); } // At night a gun sees what is lit (a searchlight, a flare) and little else. let dark = if self.night && !self.lit.contains(&t.id) { lights::DARK_HITS } else { 1.0 }; @@ -2448,7 +2472,7 @@ impl Sim { out.particles("minecraft:cloud", muzzle - dir * 2.0, 1.4, DVec3::new(0.0, 0.02, 0.0), 0.06, 70); out.particles("minecraft:campfire_cosy_smoke", muzzle - dir * 3.0, 1.2, DVec3::ZERO, 0.02, 20); let event = if kind == MissileKind::M57e6 { "airdef:launch.pantsir" } else { "airdef:launch.patriot" }; - out.sound(event, muzzle, 1.0, 0.95 + self.rng.f() as f32 * 0.1); + out.sound(event, muzzle, 1.0, 0.95 + self.vis.f() as f32 * 0.1); } } for (id, pos, vel, life) in tracers { @@ -2478,7 +2502,7 @@ impl Sim { .iter() .filter_map(|m| { let s = self.sams.iter().find(|s| s.id == m.sam)?; - (s.alive() && !s.moving()).then_some(())?; + (s.alive() && !s.moving() && s.link_range() > 0.0).then_some(())?; Some(((m.sam, m.target), *s.track(m.target)?)) }) .collect(); @@ -2507,12 +2531,12 @@ impl Sim { let range = m.kind.seeker() * t.kind.rcs().powf(0.25).clamp(0.6, 1.6); let to = t.pos - m.pos; let in_view = to.length() < range && to.normalize_or_zero().dot(dir) > 0.5; - if in_view && !matches!(ground.world.probe(m.pos, t.pos, now), world::Sweep::Hit { .. }) { + // Only a line of sight the world has confirmed counts: blocks not read (Unknown) are not air (#75). + if in_view && matches!(ground.world.probe(m.pos, t.pos, now), world::Sweep::Clear) { m.seen = now; - } - if m.seen > 0 && now.saturating_sub(m.seen) <= REACQUIRE { - // The seeker measures the target (the track of its own, newer than any link's). - m.fix = Some(Fix { pos: if m.seen == now { t.pos } else { m.fix.map_or(t.pos, |f| f.pos) }, vel: t.vel, tick: if m.seen == now { now } else { m.fix.map_or(now, |f| f.tick) }, seeker: true }); + // The seeker measures the target: position and velocity, now. Out of sight (behind a hill, + // an unread section) nothing new is measured and the last fix is carried on as it was. + m.fix = Some(Fix { pos: t.pos, vel: t.vel, tick: now, seeker: true }); } } // Nothing new for too long and no seeker on it: the interceptor has lost its target. @@ -2544,6 +2568,9 @@ impl Sim { let before = m.pos; m.vel = new * speed; m.pos += m.vel; + let end = m.pos; + // Where the exact shape is still being asked for, the contact point (by the cube) bounds the fuse (#74). + let mut waiting: Option = None; // The first block on the way this tick; a fuse that goes off earlier along the way still counts. ground.ahead(m.pos, m.vel, 10.0, now); let sweep = ground.trace(m.id | 0x8000_0000, before, m.pos, m.age <= 12, now); @@ -2561,16 +2588,19 @@ impl Sim { } } else if let world::Sweep::Wait { t } = sweep { m.pos = before.lerp(m.pos, (t - 1e-6).max(0.0)); + waiting = Some(t); } else if matches!(sweep, world::Sweep::Clear) { m.hold = 0; } if let Some(t) = target { // Closest approach this tick, both moving (fast targets cross a fuse in a tick). let rel0 = (t.pos - t.vel) - before; - let rel1 = t.pos - m.pos; + // Waiting for the shape: the fuse is judged on the whole step against the cube's face, as for a + // known hit, so that a pending answer is neither a free pass nor a different outcome. + let rel1 = t.pos - if waiting.is_some() { end } else { m.pos }; let delta = rel1 - rel0; let k = (-rel0.dot(delta) / delta.length_squared().max(1e-9)).clamp(0.0, 1.0); - if (rel0 + delta * k).length() < spec.fuse && world.is_none_or(|(w, _)| k <= w) { + if (rel0 + delta * k).length() < spec.fuse && world.map(|w| w.0).or(waiting).is_none_or(|w| k <= w) { // A target faster than the interceptor is hard to hit. let ratio = (spec.speed / t.vel.length().max(0.1)).min(1.0).powf(1.5); let fit = sams_fit.get(&m.sam).copied().unwrap_or(1.0); @@ -2660,7 +2690,7 @@ impl Sim { out.model(m.id, spec.model, m.pos, rot, spec.scale, false); if m.age > spec.cold && spec.flame > 0.0 { let back = (spec.rear - 8.0) / 16.0 * spec.scale + spec.flame; - let size = spec.flame * if m.age < 40 { 1.2 } else { 0.8 } * (0.85 + 0.15 * self.rng.f()); + let size = spec.flame * if m.age < 40 { 1.2 } else { 0.8 } * (0.85 + 0.15 * self.vis.f()); out.model(m.id + 1, "airdef:flame", m.pos + rot * DVec3::new(0.0, 0.0, back), rot, size, true); } } diff --git a/airdef-mod/src/sim/regress.rs b/airdef-mod/src/sim/regress.rs index 47eda29..d0cb50b 100644 --- a/airdef-mod/src/sim/regress.rs +++ b/airdef-mod/src/sim/regress.rs @@ -442,6 +442,51 @@ fn missile_cannot_pass_through_a_wall() { assert!(sim.blasts.iter().all(|b| b.at.x < 3.5), "burst behind the wall"); } +// #74: a pending exact answer is no free pass for the fuse: the same outcome whether the reply is late or not +#[test] +fn pending_exact_answer_does_not_let_the_fuse_kill_through_a_wall() { + let src = Blocks(|x, y, z| Some(y < 64 || (x == 3 && (64..141).contains(&y) && z.abs() < 50))); + let run = |seed: u64, target_x: f64, reply: bool| { + let mut sim = Sim::new(seed); + let mut ground = Ground::default(); + sim.add_drone(DroneKind::Shahed, DVec3::new(target_x, 100.0, 0.0), DVec3::ZERO, DVec3::new(0.0, 100.0, 900.0)); + sim.drones[0].vel = DVec3::ZERO; + sim.drones[0].engaged = 1; + let (target, id) = (sim.drones[0].id, sim.id()); + sim.tick = 20; + let fix = Fix { pos: DVec3::new(target_x, 100.0, 0.0), vel: DVec3::ZERO, tick: 20, seeker: false }; + sim.missiles.push(Missile { id, kind: MissileKind::Pac3, pos: DVec3::new(0.0, 100.0, 0.0), vel: DVec3::X * 6.0, target, sam: 0, age: 20, hold: 0, fix: Some(fix), seen: 0 }); + warm(&mut ground, &src, DVec3::new(0.0, 100.0, 0.0), DVec3::new(30.0, 100.0, 0.0)); + let mut first = 0; + for i in 0..4 { + sim.tick += 1; + sim.step_missiles(&mut ground, &mut Out::default()); + if i == 0 { + first = sim.kills; + } + let wanted = ground.world.take_wanted(sim.tick); + if reply { + ground.world.serve(&src, wanted, sim.tick); + } else { + // Sections only: the exact rays are never answered inside the test. + let answers = wanted.into_iter().map(|sec| world::SectionAnswer { sec, fill: world::section_of(&src, sec) }).collect(); + ground.world.answer(answers, sim.tick); + } + } + (first, sim.kills, sim.drones[0].alive()) + }; + for seed in 1..=10 { + // Just behind the wall, inside the fuse's reach of a missile held at its face. + for reply in [true, false] { + let (first, total, alive) = run(seed, 5.5, reply); + assert_eq!((first, total, alive), (0, 0, true), "seed {seed} reply {reply}: kill through the wall"); + } + } + // In front of the wall the fuse still works, with the reply late or on time. + let kills = |reply| (1..=10).filter(|&seed| run(seed, 2.0, reply).1 > 0).count(); + assert!(kills(true) > 0 && kills(true) == kills(false), "{} vs {}", kills(true), kills(false)); +} + // #42, #43: a bridge deck above the flight is no ground under it #[test] fn flight_under_a_bridge_is_not_a_ground_hit() { @@ -1087,6 +1132,89 @@ fn partial_blocks_use_exact_shapes() { assert!(matches!(last, world::Sweep::Hit { .. }), "{last:?}"); } +// #73: an exact answer belongs to the segment and the geometry it was asked of +#[test] +fn exact_answers_are_not_reused_for_other_segments_or_changed_blocks() { + use std::cell::Cell; + let cube = Cell::new(false); + let world = Shaped(|x, y, z| { + if y < 64 { + Some(vec![[0.0, 0.0, 0.0, 1.0, 1.0, 1.0]]) + } else if (x, y, z) == (10, 70, 0) { + Some(vec![[0.0, 0.0, 0.0, 1.0, if cube.get() { 1.0 } else { 0.5 }, 1.0]]) + } else { + Some(vec![]) + } + }); + let mut g = Ground::default(); + g.world.changed(1, false, &[]); + let (upper_a, upper_b) = (DVec3::new(9.5, 70.75, 0.5), DVec3::new(11.5, 70.75, 0.5)); + let (lower_a, lower_b) = (DVec3::new(9.5, 70.25, 0.5), DVec3::new(11.5, 70.25, 0.5)); + // Settles a step: waits for the game's answers the way a held flight does. + let now = Cell::new(1u64); + let settle = |g: &mut Ground, a: DVec3, b: DVec3| { + let mut r = g.trace(7, a, b, false, now.get()); + for _ in 0..8 { + if !matches!(r, world::Sweep::Wait { .. } | world::Sweep::Unknown { .. }) { + break; + } + let q = g.world.take_wanted(now.get()); + g.world.serve(&world, q, now.get()); + now.set(now.get() + 1); + r = g.trace(7, a, b, false, now.get()); + } + r + }; + assert_eq!(settle(&mut g, upper_a, upper_b), world::Sweep::Clear); + // The same flight and block, the other half of the slab: not the old Clear, a new question. + now.set(now.get() + 1); + let r = g.trace(7, lower_a, lower_b, false, now.get()); + assert!(matches!(r, world::Sweep::Wait { .. }), "old Clear reused through the slab: {r:?}"); + let r = settle(&mut g, lower_a, lower_b); + assert!(matches!(r, world::Sweep::Hit { block: [10, 70, 0], .. }), "{r:?}"); + // A repeat of the same step is served from memory and agrees with a fresh ask. + let r2 = g.trace(7, lower_a, lower_b, false, now.get() + 1); + assert_eq!(r, r2); + // The slab becomes a full cube: the section is read again, the old Clear is void. + assert_eq!(settle(&mut g, upper_a, upper_b), world::Sweep::Clear, "the upper step is clear again over the slab"); + cube.set(true); + g.world.changed(1, false, &[(0, 4, 0)]); + now.set(now.get() + 1); + let r = g.trace(7, upper_a, upper_b, false, now.get()); + assert!(!matches!(r, world::Sweep::Clear), "Clear survived the block's replacement: {r:?}"); + // Not the cache's data but the game's: the section is read and the ray asked again. + let r = settle(&mut g, upper_a, upper_b); + assert!(matches!(r, world::Sweep::Hit { block: [10, 70, 0], .. }), "{r:?}"); + // A late reply to a question asked before a change does not touch the new world. + cube.set(false); + let mut h = Ground::default(); + h.world.changed(1, false, &[]); + h.world.sweep(upper_a, upper_b, false, 1); + let q = h.world.take_wanted(1); + h.world.serve(&world, q, 1); + let r = h.trace(8, upper_a, upper_b, false, 2); + assert!(matches!(r, world::Sweep::Wait { .. }), "{r:?}"); + let old = h.world.take_rays(2); + assert_eq!(old.len(), 1); + cube.set(true); + h.world.changed(1, false, &[(0, 4, 0)]); + h.world.serve_rays(&world, old); + let r = h.trace(8, upper_a, upper_b, false, 3); + assert!(!matches!(r, world::Sweep::Clear), "a late reply was applied to the new world: {r:?}"); + // The wait is bounded however often the question is asked again. + let mut h = Ground::default(); + h.world.changed(1, false, &[]); + h.world.sweep(upper_a, upper_b, false, 1); + let q = h.world.take_wanted(1); + h.world.serve(&world, q, 1); + let mut last = world::Sweep::Clear; + for i in 0..60u64 { + let dy = DVec3::new(0.0, 0.001 * i as f64, 0.0); + last = h.trace(9, lower_a + dy, lower_b + dy, false, 2 + i); + } + assert!(matches!(last, world::Sweep::Hit { .. }), "a flight changing course every tick hangs: {last:?}"); +} + // #64: heights known are old when blocks change, a chunk is replaced, or time passes; they are asked again #[test] fn land_heights_follow_world_changes_and_age() { @@ -1206,7 +1334,15 @@ fn interceptors_guide_on_tracks_and_seekers_not_on_truth() { let stale = Some(Fix { pos: DVec3::new(-60.0, 100.0, 80.0), vel: DVec3::ZERO, tick: 90, seeker: false }); sim.missiles.push(Missile { id, kind: MissileKind::Pac3, pos: DVec3::new(0.0, 100.0, 0.0), vel: DVec3::Z * 3.0, target, sam: 999, age: 100, hold: 0, fix: stale, seen: 0 }); sim.tick = 100; - sim.step_missiles(&mut Ground::default(), &mut Out::default()); + // The world is read: open air between them is a confirmed line of sight. + let air = Blocks(|_, y, _| Some(y < 64)); + let mut ground = Ground::default(); + for t in 0..16 { + ground.world.probe(DVec3::new(0.0, 100.0, 0.0), at, t); + let q = ground.world.take_wanted(t); + ground.world.serve(&air, q, t); + } + sim.step_missiles(&mut ground, &mut Out::default()); (sim.missiles[0].vel, sim.missiles[0].seen) }; let (v, seen) = near(DVec3::new(40.0, 100.0, 70.0)); @@ -1221,7 +1357,7 @@ fn interceptors_guide_on_tracks_and_seekers_not_on_truth() { sim.missiles.push(Missile { id, kind: MissileKind::Pac3, pos: DVec3::new(0.0, 100.0, 0.0), vel: DVec3::Z * 3.0, target, sam: 999, age: 100, hold: 0, fix: Some(Fix { pos: DVec3::new(0.0, 100.0, 300.0), vel: DVec3::ZERO, tick: 90, seeker: false }), seen: 0 }); let hill = Blocks(|x, y, z| Some(y < 64 || ((10..14).contains(&x) && (0..40).contains(&z) && y < 300))); let mut ground = Ground::default(); - for t in 0..6 { + for t in 0..16 { ground.world.probe(DVec3::new(0.0, 100.0, 0.0), DVec3::new(40.0, 100.0, 70.0), t); let q = ground.world.take_wanted(t); ground.world.serve(&hill, q, t); @@ -1241,6 +1377,114 @@ fn interceptors_guide_on_tracks_and_seekers_not_on_truth() { assert!(sim.missiles.is_empty() && sim.tally.expired == 1, "{:?}", sim.tally); } +// #75: unread blocks are not a line of sight; out of sight the seeker coasts on its last estimate whole +#[test] +fn seeker_needs_confirmed_sight_and_coasts_on_its_last_estimate() { + let setup = |target_at: DVec3, tv: DVec3, fix: Option| { + let mut sim = Sim::new(1); + sim.add_drone(DroneKind::Shahed, target_at, DVec3::ZERO, DVec3::new(0.0, 100.0, 5000.0)); + sim.drones[0].vel = tv; + let (target, id) = (sim.drones[0].id, sim.id()); + sim.missiles.push(Missile { id, kind: MissileKind::Pac3, pos: DVec3::new(0.0, 100.0, 0.0), vel: DVec3::Z * 3.0, target, sam: 999, age: 100, hold: 0, fix, seen: 0 }); + sim.tick = 100; + sim + }; + let at = DVec3::new(0.0, 100.0, 70.0); + // Cold world: nothing read, nothing locked, however close the target is. + let mut sim = setup(DVec3::new(20.0, 100.0, 70.0), DVec3::ZERO, None); + sim.step_missiles(&mut Ground::default(), &mut Out::default()); + assert!(sim.missiles.iter().all(|m| m.seen == 0 && m.fix.is_none()), "an unread world was taken for line of sight"); + // Warm open air: locked. + let air = Blocks(|_, y, _| Some(y < 64)); + let mut ground = Ground::default(); + for t in 0..16 { + ground.world.probe(DVec3::new(0.0, 100.0, 0.0), at, t); + let q = ground.world.take_wanted(t); + ground.world.serve(&air, q, t); + } + let mut sim = setup(at, DVec3::new(0.5, 0.0, 0.0), None); + sim.step_missiles(&mut ground, &mut Out::default()); + let f = sim.missiles[0].fix.expect("no lock in clear air"); + assert!(sim.missiles[0].seen == 100 && f.seeker && f.tick == 100 && f.vel == DVec3::new(0.5, 0.0, 0.0), "{f:?}"); + // Then a wall comes between them and the target manoeuvres: the estimate stays as it was, pos, vel and tick. + let wall = Blocks(|x, y, z| Some(y < 64 || (x == 5 && y < 200 && z.abs() < 200))); + let mut ground = Ground::default(); + for t in 0..16 { + ground.world.probe(DVec3::new(0.0, 100.0, 0.0), DVec3::new(10.0, 100.0, 70.0), t); + let q = ground.world.take_wanted(t); + ground.world.serve(&wall, q, t); + } + let last = Fix { pos: at, vel: DVec3::ZERO, tick: 100, seeker: true }; + let mut sim = setup(DVec3::new(10.0, 100.0, 70.0), DVec3::new(2.0, 0.0, 0.0), Some(last)); + sim.missiles[0].seen = 100; + sim.tick = 101; + sim.step_missiles(&mut ground, &mut Out::default()); + let f = sim.missiles[0].fix.expect("the estimate was dropped"); + assert!(f.pos == last.pos && f.vel == DVec3::ZERO && f.tick == 100, "truth leaked through the wall: {f:?}"); +} + +// #85: the graphics settings never reach the combat: the same seed and commands play out the same +#[test] +fn graphics_settings_do_not_change_combat_randomness_or_ids() { + // The issue's case: one explosion, only the shader differs. + let after_blast = |shader: bool| { + let mut sim = Sim::new(12345); + sim.shader = shader; + sim.fireball(DVec3::new(0.0, 80.0, 0.0), 4.0, 11); + let id = sim.id(); + let aim = sim.aim_error(DroneKind::Shahed); + (id, aim) + }; + assert_eq!(after_blast(true), after_blast(false), "a fireball spent entity ids or combat randomness"); + // A shot-down drone, with its debris and sound: no more than the same shot-down without them. + let after_kill = |shader: bool, effects: u32| { + let mut sim = Sim::new(99); + sim.shader = shader; + let mut out = Out::default(); + for i in 0..effects { + sim.fireball(DVec3::new(f64::from(i), 80.0, 0.0), 4.0, 11); + } + sim.add_drone(DroneKind::Shahed, DVec3::new(0.0, 80.0, 0.0), DVec3::X, DVec3::new(900.0, 80.0, 0.0)); + sim.shoot_down(0, 0, None, &mut out); + let id = sim.id(); + (sim.drones[0].id, id, sim.aim_error(DroneKind::Shahed)) + }; + assert_eq!(after_kill(true, 0), after_kill(false, 0)); + assert_eq!(after_kill(false, 3), after_kill(true, 9)); + // A whole raid: the combat's outcome is the same with the shader on and off (and drawn or not). + let play = |shader: bool, drawn: bool| { + let mut sim = Sim::new(31); + sim.shader = shader; + sim.damage = false; + let mut ground = Ground::default(); + let mut out = Out::default(); + for (i, kind) in [SamKind::Patriot, SamKind::Pantsir, SamKind::Gepard, SamKind::Patriot].into_iter().enumerate() { + let id = sim.id(); + sim.sams.push(Sam::new(id, kind, DVec3::new(i as f64 * 25.0 - 40.0, 70.0, 10.0 * i as f64), 0.0)); + } + sim.raid(DroneKind::Shahed, DVec3::new(0.0, 70.0, 0.0), 14, 500.0, &mut out); + // Whatever the settings, the world answers the same: nothing but the land is asked for here. + let mut trace = Vec::new(); + for _ in 0..1500 { + if !drawn { + out = Out::default(); + } + sim.step(&mut ground, None, &mut out); + if out.models.len() > 20_000 { + out = Out::default(); + } + trace.push((sim.kills, sim.hits, sim.launched, sim.drones.len(), sim.missiles.len())); + } + let drones: Vec<_> = sim.drones.iter().map(|d| (d.id, d.pos.to_array().map(f64::to_bits))).collect(); + let missiles: Vec<_> = sim.missiles.iter().map(|m| (m.id, m.pos.to_array().map(f64::to_bits))).collect(); + let sams: Vec<_> = sim.sams.iter().map(|s| (s.id, s.kills)).collect(); + (trace, drones, missiles, sams, sim.crashes, sim.tally.blocked, sim.tally.expired) + }; + let (a, b) = (play(true, true), play(false, false)); + assert!(a.0.last().is_some_and(|t| t.2 > 0), "nothing was launched: the test proves nothing"); + assert_eq!(a, b, "the combat differs with the graphics"); +} + // #69: many separate strikes: the list, what is drawn and the lights are bounded; the rest end properly #[test] fn mass_strikes_keep_fires_effects_and_lights_bounded() { diff --git a/airdef-mod/src/sim/units.rs b/airdef-mod/src/sim/units.rs index b6c33fa..c585e39 100644 --- a/airdef-mod/src/sim/units.rs +++ b/airdef-mod/src/sim/units.rs @@ -631,7 +631,7 @@ impl Sam { impl Sim { /// Draws unit `i`: its vehicle, its launcher or turret where it points, its antennas turning. pub(super) fn draw_sam(&mut self, i: usize, out: &mut Out) { - draw_sam_model(&self.sams[i], &mut self.rng, out); + draw_sam_model(&self.sams[i], &mut self.vis, out); } } diff --git a/airdef-mod/src/sim/world.rs b/airdef-mod/src/sim/world.rs index d9f1b3c..d3c6adb 100644 --- a/airdef-mod/src/sim/world.rs +++ b/airdef-mod/src/sim/world.rs @@ -112,6 +112,10 @@ struct RayEntry { b: DVec3, state: Exact, asked: u64, + /// When this wait began: re-asking for a changed step does not restart the clock (#73). + first: u64, + /// No usable exact answer (timed out, or no verdict): the solid cube of the sections stands. + cube: bool, } /// The game's verdict on an exact ray, as `render.rays` gives it. @@ -128,8 +132,28 @@ pub struct RayReply { /// Ticks an exact ray may take to be answered before the block counts as the solid cube the sections say. const RAY_WAIT: u64 = 30; /// Exact rays asked in one tick (the game's budget is 256). +/// How many steps long the exact ray is asked. +const RAY_REACH: f64 = 3.0; const RAYS_PER_TICK: usize = 128; +/// Whether step `a`..`b` lies on the asked segment `a0`..`b0` (same line, same way, inside it, give or take a +/// hair): the fractions of its ends along the asked one. +fn within(a0: DVec3, b0: DVec3, a: DVec3, b: DVec3) -> Option<(f64, f64)> { + const EPS: f64 = 1e-3; + let d0 = b0 - a0; + let l2 = d0.length_squared(); + if l2 < 1e-12 { + return (a.distance(a0) < EPS && b.distance(b0) < EPS).then_some((0.0, 1.0)); + } + let (sa, sb) = ((a - a0).dot(d0) / l2, (b - a0).dot(d0) / l2); + let len = l2.sqrt(); + let tol = EPS / len; + if sa < -tol || sb > 1.0 + tol || sb <= sa || a.distance(a0 + d0 * sa) > EPS || b.distance(a0 + d0 * sb) > EPS { + return None; + } + Some((sa.max(0.0), sb.min(1.0))) +} + #[derive(Default)] struct RayDesk { next: u32, @@ -215,6 +239,7 @@ impl WorldCache { self.stats.invalidated += self.secs.len() as u64; } if epoch != self.epoch { + self.rays = RayDesk { next: self.rays.next, ..RayDesk::default() }; if self.epoch != 0 { self.secs.clear(); self.urgent.clear(); @@ -227,6 +252,7 @@ impl WorldCache { } if overflow || unexplained { self.changed_all = true; + self.drop_rays(|_| true); } if overflow { for e in self.secs.values_mut() { @@ -234,6 +260,10 @@ impl WorldCache { } self.stats.invalidated += self.secs.len() as u64; } + if !sections.is_empty() { + let set: HashSet = sections.iter().copied().collect(); + self.drop_rays(|b| set.contains(&sec_of(b[0], b[1], b[2]))); + } for s in sections { self.note_column(s.0, s.2); if let Some(e) = self.secs.get_mut(s) { @@ -272,6 +302,7 @@ impl WorldCache { n += 1; } } + self.drop_rays(|b| set.contains(&(b[0] >> 4, b[2] >> 4))); self.stats.invalidated += n; self.stats.columns_changed += columns.len() as u64; } @@ -280,6 +311,10 @@ impl WorldCache { pub fn touched(&mut self, at: DVec3, radius: f64) { let lo = sec_of((at.x - radius).floor() as i32, (at.y - radius).floor() as i32, (at.z - radius).floor() as i32); let hi = sec_of((at.x + radius).floor() as i32, (at.y + radius).floor() as i32, (at.z + radius).floor() as i32); + self.drop_rays(|b| { + let s = sec_of(b[0], b[1], b[2]); + (lo.0..=hi.0).contains(&s.0) && (lo.1..=hi.1).contains(&s.1) && (lo.2..=hi.2).contains(&s.2) + }); for x in lo.0..=hi.0 { for y in lo.1..=hi.1 { for z in lo.2..=hi.2 { @@ -462,33 +497,70 @@ impl WorldCache { pub fn narrow(&mut self, key: u32, hit: Sweep, a: DVec3, b: DVec3, now: u64) -> Sweep { let Sweep::Hit { t, block, .. } = hit else { return hit }; let k = (key, block); - let Some(e) = self.rays.entries.get_mut(&k) else { - if self.rays.entries.len() >= 4096 { + let mut first = now; + if let Some(e) = self.rays.entries.get_mut(&k) { + if e.cube { return hit; } - self.rays.next = self.rays.next.wrapping_add(1); - let id = self.rays.next; - self.rays.entries.insert(k, RayEntry { id, a, b, state: Exact::Pending, asked: now }); - self.rays.by_id.insert(id, k); - self.rays.out.push((id, a, b)); - return Sweep::Wait { t }; - }; - if e.state == Exact::Pending && now.saturating_sub(e.asked) > RAY_WAIT { - e.state = Exact::Hit { point: a + (b - a) * t, normal: [0; 3], block }; - } - match e.state { - Exact::Pending => Sweep::Wait { t }, - Exact::Clear => Sweep::Clear, - Exact::Hit { point, normal, block } => { - let d = b - a; - let f = ((point - a).dot(d) / d.length_squared().max(1e-12)).max(0.0); - if f > 1.0 { - // The shape's face is beyond this step: nothing is met yet. - Sweep::Clear - } else { - Sweep::Hit { t: f, point, normal, block } + // An answer is for the segment it was asked of: it serves this step only if the step lies on it. + if let Some((sa, sb)) = within(e.a, e.b, a, b) { + match e.state { + Exact::Pending => { + if now.saturating_sub(e.first) > RAY_WAIT { + e.cube = true; + return hit; + } + return Sweep::Wait { t }; + } + Exact::Clear => return Sweep::Clear, + Exact::Hit { point, normal, block } => { + let d0 = e.b - e.a; + let f_old = ((point - e.a).dot(d0) / d0.length_squared().max(1e-12)).clamp(0.0, 1.0); + let f = (f_old - sa) / (sb - sa).max(1e-12); + if f > 1.0 { + // The shape's face is beyond this step: nothing is met yet. + return Sweep::Clear; + } + let f = f.max(0.0); + return Sweep::Hit { t: f, point: a + (b - a) * f, normal, block }; + } } } + // A different step: the old answer says nothing of it. Ask again; an unanswered wait keeps its clock. + if e.state == Exact::Pending { + first = e.first; + if now.saturating_sub(first) > RAY_WAIT { + e.cube = true; + return hit; + } + } + let old = e.id; + self.rays.by_id.remove(&old); + self.rays.out.retain(|r| r.0 != old); + } else if self.rays.entries.len() >= 4096 { + return hit; + } + self.rays.next = self.rays.next.wrapping_add(1); + let id = self.rays.next; + // Asked over a longer stretch than the step: a flight held at the face tries again from there with a + // step of about the same line, which the answer then covers. What lies beyond the step is mapped back + // when it is used (a face past the step is no contact). + let far = a + (b - a) * RAY_REACH; + self.rays.entries.insert(k, RayEntry { id, a, b: far, state: Exact::Pending, asked: now, first, cube: false }); + self.rays.by_id.insert(id, k); + self.rays.out.push((id, a, far)); + Sweep::Wait { t } + } + + /// Forgets the exact answers (and questions) for blocks where `gone` says the geometry may have changed: + /// a late reply to a forgotten question is dropped, the next step asks anew (#73). + fn drop_rays(&mut self, gone: impl Fn(&[i32; 3]) -> bool) { + let dead: Vec<(u32, [i32; 3])> = self.rays.entries.keys().filter(|k| gone(&k.1)).copied().collect(); + for k in dead { + if let Some(e) = self.rays.entries.remove(&k) { + self.rays.by_id.remove(&e.id); + self.rays.out.retain(|r| r.0 != e.id); + } } } @@ -512,12 +584,16 @@ impl WorldCache { for r in replies { let Some(k) = self.rays.by_id.get(&r.id).copied() else { continue }; let Some(e) = self.rays.entries.get_mut(&k) else { continue }; + if e.cube { + continue; + } e.state = if r.clear { Exact::Clear } else if r.hit { Exact::Hit { point: e.a + (e.b - e.a) * r.fraction, normal: r.normal, block: r.block } } else { // No verdict: the solid cube the sections said. + e.cube = true; Exact::Hit { point: e.a, normal: [0; 3], block: k.1 } }; } diff --git a/airdef-mod/src/wave.rs b/airdef-mod/src/wave.rs index 08fa474..8c71e61 100644 --- a/airdef-mod/src/wave.rs +++ b/airdef-mod/src/wave.rs @@ -113,6 +113,27 @@ impl WaveConfig { let aim = Aim::ALL.iter().position(|a| *a == self.aim).unwrap_or(0); format!("{} {approach} {} {aim} {}", counts.join(","), self.dist, self.preset) } + /// Whether `w` are words `parse` would read whole: an error says which is wrong (#86). + pub fn check(&self, w: &[&str]) -> Result<(), String> { + if let Some(c) = w.first() { + if c.split(',').take(KINDS.len()).any(|v| v.parse::().is_err()) { + return Err(format!("число по типам: «{c}»")); + } + } + if w.get(1).is_some_and(|v| v.parse::().ok().is_none_or(|i| i >= Approach::ALL.len())) { + return Err(format!("подход: «{}»", w[1])); + } + if w.get(2).is_some_and(|v| v.parse::().ok().is_none_or(|d| !d.is_finite())) { + return Err(format!("дальность: «{}»", w[2])); + } + if w.get(3).is_some_and(|v| v.parse::().ok().is_none_or(|i| i >= Aim::ALL.len())) { + return Err(format!("цель налёта: «{}»", w[3])); + } + if w.get(4).is_some_and(|v| v.parse::().is_err()) { + return Err(format!("набор: «{}»", w[4])); + } + Ok(()) + } /// Reads `text`'s words; what does not parse keeps its value. pub fn parse(&mut self, w: &[&str]) { if let Some(c) = w.first() {