feat: attenuate indirect blast damage behind known cover
build / mod (push) Failing after 18s

Use a bounded read-only pre-destruction occupancy sample; preserve conservative damage for unresolved geometry and do not defer or repeat damage on replies.

Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
2026-11-02 23:55:38 +03:00
parent a7d400ea7e
commit c96676f3e5
8 changed files with 196 additions and 19 deletions
+112 -4
View File
@@ -141,6 +141,16 @@ impl Comps {
/// Health points an object loses at the centre of a blast.
const OBJECT_HIT: [f64; 5] = [2.0, 6.0, 14.0, 26.0, 40.0];
/// Three cheap game samples across a hull. Known cover leaves residual splash;
/// unknown geometry keeps the previous unattenuated damage (not a claim of air).
/// Zero async wait freezes the pre-destruction cache and avoids late double damage.
fn blast_exposure(ground: &Ground, from: DVec3, centre: DVec3, size: f64, now: u64) -> f64 {
let across = horizontal(centre - from).cross(DVec3::Y).normalize_or(DVec3::X) * size;
[centre, centre - across, centre + across].into_iter().map(|to| {
if ground.world.blast_cover(from, to, now) == Some(true) { 0.15 } else { 1.0 }
}).sum::<f64>() / 3.0
}
impl MissileKind {
/// The explosive a missile left in a burning launcher adds (TNT).
fn charge(self) -> f64 {
@@ -240,7 +250,7 @@ impl Sim {
}
/// A blast of `power` (0 to 4) at `pos`: the systems and objects round it take damage.
pub(super) fn blast_damage(&mut self, pos: DVec3, power: usize, out: &mut Out) {
pub(super) fn blast_damage(&mut self, pos: DVec3, power: usize, ground: &Ground, out: &mut Out) {
let (reach, hit) = (REACH[power], HIT[power]);
let mut destroyed = Vec::new();
for (i, sam) in self.sams.iter_mut().enumerate() {
@@ -253,8 +263,9 @@ impl Sim {
continue;
}
let k = 1.0 - d / reach;
let cover = blast_exposure(ground, pos, sam.pos + DVec3::Y * (1.5 * sam.kind.spec().scale), sam.kind.spec().scale, self.tick);
let before = sam.health;
sam.health -= hit * k * k * if d < 1.0 { 1.5 } else { 1.0 };
sam.health -= cover * hit * k * k * if d < 1.0 { 1.5 } else { 1.0 };
if sam.health <= 0.0 {
destroyed.push(i);
continue;
@@ -279,7 +290,7 @@ impl Sim {
let kp = 1.0 - dp / reach;
let roll = dice(self.seed, self.tick, sam.id as u64, part as u64);
// The parts are softer than the hull: a blast the hull survives can still put one out.
let dmg = hit * kp * kp * (0.7 + roll) * 1.5 * if dp < 1.0 { 1.4 } else { 1.0 };
let dmg = cover * hit * kp * kp * (0.7 + roll) * 1.5 * if dp < 1.0 { 1.4 } else { 1.0 };
let was = sam.comps.state(part);
let v = &mut sam.comps.0[part as usize];
*v = (*v - dmg).max(0.0);
@@ -300,7 +311,8 @@ impl Sim {
if d < r {
if let Some(h) = self.target_health.get_mut(i) {
let before = *h;
*h = (*h - OBJECT_HIT[power] * (1.0 - d / r)).max(0.0);
let cover = blast_exposure(ground, pos, *t + DVec3::Y * 0.8, 0.75, self.tick);
*h = (*h - cover * OBJECT_HIT[power] * (1.0 - d / r)).max(0.0);
if before > 0.0 && *h <= 0.0 {
out.messages.push(format!("ОБЪЕКТ №{} УНИЧТОЖЕН", i + 1));
}
@@ -523,3 +535,99 @@ pub(super) fn draw_wreck_of(sam: &Sam, out: &mut Out) {
}
}
}
#[cfg(test)]
mod cover_tests {
use super::*;
struct CoverWorld { wall: bool, opening: bool }
impl world::WorldSource for CoverWorld {
fn solid(&self, x: i32, _y: i32, z: i32) -> Option<bool> {
Some(self.wall && x == 8 && !(self.opening && z == 8))
}
}
fn cache(src: &CoverWorld) -> Ground {
let mut ground = Ground::default();
let mut answers = Vec::new();
for x in -1..=1 { for z in -1..=1 {
let sec = (x, 4, z);
answers.push(world::SectionAnswer { sec, fill: world::section_of(src, sec) });
}}
ground.world.answer(answers, 10);
ground
}
fn victim() -> Sim {
let mut sim = Sim::new(7);
sim.tick = 10;
let id = sim.id();
sim.sams.push(Sam::new(id, SamKind::S400, DVec3::new(10.5, 70.0, 8.5), 0.0));
sim.targets.push(DVec3::new(10.5, 71.0, 8.5));
sim.target_health.push(100.0);
sim
}
#[test]
fn known_wall_and_opening_attenuate_hull_parts_and_objects() {
let from = DVec3::new(0.5, 71.5, 8.5);
let mut results = Vec::new();
for src in [CoverWorld { wall: false, opening: false }, CoverWorld { wall: true, opening: false }, CoverWorld { wall: true, opening: true }] {
let mut sim = victim();
sim.blast_damage(from, 2, &cache(&src), &mut Out::default());
results.push((1.0 - sim.sams[0].health, 1.0 - sim.sams[0].comps.get(Part::Tracks), 100.0 - sim.target_health[0]));
}
for i in 0..3 {
let open = [results[0].0, results[0].1, results[0].2][i];
let wall = [results[1].0, results[1].1, results[1].2][i];
let gap = [results[2].0, results[2].1, results[2].2][i];
assert!(open > 0.0);
assert!((wall / open - 0.15).abs() < 1e-9);
assert!(wall < gap && gap < open, "sample {i}: wall {wall}, gap {gap}, open {open}");
}
}
#[test]
fn unresolved_cover_has_bounded_zero_wait_and_no_late_damage() {
let from = DVec3::new(0.5, 71.5, 8.5);
let mut unknown = Ground::default();
assert_eq!(unknown.world.blast_cover(from, DVec3::new(10.5, 71.5, 8.5), 10), None);
let mut sim = victim();
let mut open = victim();
sim.blast_damage(from, 2, &unknown, &mut Out::default());
open.blast_damage(from, 2, &cache(&CoverWorld { wall: false, opening: false }), &mut Out::default());
assert_eq!(sim.sams[0].health, open.sams[0].health);
let health = sim.sams[0].health;
let hit_points = sim.target_health[0];
assert!(unknown.world.take_wanted(10).is_empty());
assert!(unknown.world.take_rays(10).is_empty());
// A reply, even one proving cover, is not a second combat event.
unknown.world.answer(vec![world::SectionAnswer { sec: (0, 4, 0), fill: world::Fill::Full }], 11);
assert_eq!(sim.sams[0].health, health);
assert_eq!(sim.target_health[0], hit_points);
}
#[test]
fn direct_contact_is_not_weakened_by_cover_and_blast_does_not_mutate_world() {
let mut sim = victim();
let ground = cache(&CoverWorld { wall: true, opening: false });
let from = DVec3::new(0.5, 71.5, 8.5);
let end = DVec3::new(10.5, 71.5, 8.5);
assert_eq!(ground.world.blast_cover(from, end, 10), Some(true));
sim.blast_damage(from, 2, &ground, &mut Out::default());
assert_eq!(ground.world.blast_cover(from, end, 10), Some(true));
let at = sim.sams[0].pos + DVec3::Y;
sim.direct_hit(at, DVec3::X * 2.0, 1, &mut Out::default());
assert!(!sim.sams[0].alive());
}
#[test]
fn stale_cover_is_unresolved_and_source_voxel_does_not_shield_everything() {
let ground = cache(&CoverWorld { wall: true, opening: false });
let from = DVec3::new(0.5, 71.5, 8.5);
let end = DVec3::new(10.5, 71.5, 8.5);
assert_eq!(ground.world.blast_cover(from, end, 1210), None);
let source = DVec3::new(8.5, 71.5, 8.5);
assert_eq!(ground.world.blast_cover(source, end, 10), Some(false));
}
}