545d4777e8
build / mod (push) Successful in 3m54s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
357 lines
15 KiB
Rust
357 lines
15 KiB
Rust
//! What a kill does to the air target (#97): it falls burning on, it breaks up, or its warhead goes
|
|
//! off in the air. The chances are game profiles, all here and nowhere else; they are not the real
|
|
//! weapons' figures. The roll is combat randomness (a detonation means no ground impact), one draw
|
|
//! per kill; everything drawn from it (pieces, sparks) is decoration and uses only the visual RNG.
|
|
//! The outcome is recorded with the kill, so a replay shows it and never rolls again.
|
|
use super::*;
|
|
|
|
/// How an air target ended up when it was brought down.
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
|
|
pub enum Outcome {
|
|
/// Control lost: the burning hull falls on with what momentum it had. A recording without an
|
|
/// outcome (an old one) is read as this, which is what every kill used to be.
|
|
#[default]
|
|
DisabledFalling,
|
|
/// The airframe tears into several pieces; the warhead section may survive and fall on.
|
|
Breakup,
|
|
/// The warhead goes off in the air: a flash and a rain of debris, nothing left to hit the ground.
|
|
AirDetonation,
|
|
}
|
|
|
|
/// What brought the target down.
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
pub enum KillCause {
|
|
/// Shells of this system's guns.
|
|
Gun(SamKind),
|
|
Missile(MissileKind),
|
|
}
|
|
|
|
/// A hit, as far as the outcome is concerned.
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub struct Impact {
|
|
pub cause: KillCause,
|
|
/// Game energy of the hit (see `energy_of`).
|
|
pub energy: f64,
|
|
pub at: DVec3,
|
|
/// Closing speed, blocks a tick.
|
|
pub rel_speed: f64,
|
|
}
|
|
|
|
impl Impact {
|
|
pub fn gun(sam: SamKind, at: DVec3, rel_speed: f64) -> Self {
|
|
Impact { cause: KillCause::Gun(sam), energy: energy_of(KillCause::Gun(sam)), at, rel_speed }
|
|
}
|
|
pub fn missile(kind: MissileKind, at: DVec3, rel_speed: f64) -> Self {
|
|
Impact { cause: KillCause::Missile(kind), energy: energy_of(KillCause::Missile(kind)), at, rel_speed }
|
|
}
|
|
}
|
|
|
|
// ---- the profiles: the one place to tune ------------------------------------------------------
|
|
|
|
/// Game energy of one hit: a shell is small, an interceptor's warhead is big.
|
|
pub fn energy_of(cause: KillCause) -> f64 {
|
|
match cause {
|
|
KillCause::Gun(SamKind::Gepard) => 0.15,
|
|
KillCause::Gun(_) => 0.13,
|
|
KillCause::Missile(m) => match m {
|
|
MissileKind::Pac3 => 1.0,
|
|
MissileKind::M57e6 => 0.5,
|
|
MissileKind::M48n6 => 1.2,
|
|
MissileKind::M77n6 => 1.6,
|
|
MissileKind::IrisT => 0.7,
|
|
MissileKind::Amraam => 0.6,
|
|
MissileKind::M9m317 => 0.8,
|
|
MissileKind::M9m338 => 0.7,
|
|
// Rams: a lot of speed, no warhead to speak of.
|
|
MissileKind::P1sun => 0.25,
|
|
},
|
|
}
|
|
}
|
|
|
|
/// How much a target takes before it comes apart: size, then what it carries.
|
|
pub fn toughness(kind: DroneKind) -> f64 {
|
|
let s = kind.spec();
|
|
0.2 + 0.2 * s.scale + 0.03 * s.warhead as f64
|
|
}
|
|
|
|
/// Chance of an air detonation: nothing without a warhead, rare for shells, likely when a strong
|
|
/// interceptor meets a small target.
|
|
const DET_BASE: f64 = 0.04;
|
|
const DET_SLOPE: f64 = 0.32;
|
|
const DET_FLOOR: f64 = 0.03;
|
|
const DET_CEIL: f64 = 0.75;
|
|
/// Of the rest, the share that breaks up instead of falling on.
|
|
const BREAK_BASE: f64 = 0.12;
|
|
const BREAK_SLOPE: f64 = 0.3;
|
|
const BREAK_CEIL: f64 = 0.8;
|
|
/// A broken-up target whose warhead section stays whole and falls on (and may cook off on landing).
|
|
pub const WARHEAD_SURVIVES: f64 = 0.3;
|
|
|
|
/// The relative strength of the hit against the target.
|
|
pub fn ratio(kind: DroneKind, imp: &Impact) -> f64 {
|
|
let speed = (0.8 + 0.05 * imp.rel_speed).clamp(0.8, 1.2);
|
|
imp.energy * speed / toughness(kind)
|
|
}
|
|
|
|
/// (chance of an air detonation, chance of a breakup) of a hit on `kind`; the rest falls on.
|
|
pub fn odds(kind: DroneKind, imp: &Impact) -> (f64, f64) {
|
|
let s = kind.spec();
|
|
let r = ratio(kind, imp);
|
|
let det = if s.decoy || s.warhead == 0 { 0.0 } else { (DET_BASE + DET_SLOPE * (r - 0.4)).clamp(DET_FLOOR, DET_CEIL) };
|
|
let share = (BREAK_BASE + BREAK_SLOPE * r).clamp(0.0, BREAK_CEIL);
|
|
(det, (1.0 - det) * share)
|
|
}
|
|
|
|
/// The outcome for a roll of 0..1.
|
|
pub fn choose(kind: DroneKind, imp: &Impact, roll: f64) -> Outcome {
|
|
let (det, brk) = odds(kind, imp);
|
|
if roll < det {
|
|
Outcome::AirDetonation
|
|
} else if roll < det + brk {
|
|
Outcome::Breakup
|
|
} else {
|
|
Outcome::DisabledFalling
|
|
}
|
|
}
|
|
|
|
/// The look of a kill by the target's warhead and the outcome: (fireball radius, ticks it burns,
|
|
/// loudness, sound). A bare decoy pops; a detonation lights the sky. The live raid and its replay
|
|
/// both take it from here.
|
|
pub fn look(kind: DroneKind, outcome: Outcome) -> (f32, u32, f32, &'static str) {
|
|
let w = kind.spec().warhead.min(8) as f32;
|
|
let size = 3.5 * (0.85 + 0.12 * w);
|
|
let life = (8 + (w as u32) / 2) * 9;
|
|
let gain = 1.0 + 0.06 * w;
|
|
match outcome {
|
|
Outcome::DisabledFalling => (size, life, gain, "airdef:blast.air"),
|
|
Outcome::Breakup => (size * 0.9, life, gain * 0.9, "airdef:blast.medium"),
|
|
Outcome::AirDetonation => (size * 1.9, life * 8 / 5, gain * 1.5, "airdef:blast.big"),
|
|
}
|
|
}
|
|
|
|
// ---- pieces -----------------------------------------------------------------------------------
|
|
|
|
/// Most pieces alive at once; a mass battle thins them out.
|
|
pub const PIECES_MAX: usize = 320;
|
|
/// Longest a piece lives, ticks (a replay's pieces end here too).
|
|
pub const PIECE_LIFE: u32 = 110;
|
|
|
|
/// One piece thrown off: its own velocity, spin, size, drag and life.
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub struct Piece {
|
|
pub vel: DVec3,
|
|
pub spin: DQuat,
|
|
pub scale: f64,
|
|
pub drag: f64,
|
|
pub life: u32,
|
|
/// Trails smoke while it falls.
|
|
pub burns: bool,
|
|
}
|
|
|
|
/// The pieces of an outcome from `seed` (decorative randomness only), moving on with `base` (the
|
|
/// target's velocity). None for a fall.
|
|
pub fn pieces(seed: u64, outcome: Outcome, base: DVec3) -> Vec<Piece> {
|
|
let (n, speed, size, life) = match outcome {
|
|
Outcome::DisabledFalling => return Vec::new(),
|
|
Outcome::Breakup => (7, 0.35, 1.0, 70),
|
|
Outcome::AirDetonation => (12, 0.8, 0.8, 50),
|
|
};
|
|
let mut rng = Rng::new(seed ^ 0x9e37_79b9_7f4a_7c15);
|
|
(0..n)
|
|
.map(|_| {
|
|
let dir = DVec3::new(rng.s(), rng.s() * 0.8 + 0.2, rng.s()).normalize_or(DVec3::Y);
|
|
let v = base * if outcome == Outcome::Breakup { 0.55 } else { 0.3 } + dir * speed * (0.4 + 0.8 * rng.f());
|
|
let spin = DQuat::from_euler(glam::EulerRot::XYZ, rng.s() * 0.5, rng.s() * 0.5, rng.s() * 0.5);
|
|
Piece {
|
|
vel: v,
|
|
spin,
|
|
scale: size * (0.5 + 0.8 * rng.f()),
|
|
drag: 0.96 + 0.025 * rng.f(),
|
|
life: (life as f64 * (0.6 + 0.4 * rng.f())) as u32,
|
|
burns: rng.chance(0.6),
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// One tick of a piece's flight: gravity, then drag (the replays and the live raid move the same).
|
|
pub fn fly(pos: &mut DVec3, vel: &mut DVec3, drag: f64) {
|
|
vel.y -= 0.05;
|
|
*vel *= drag;
|
|
*pos += *vel;
|
|
}
|
|
|
|
/// Where a piece is `age` ticks after it was thrown from `at`, as a pure function (for replays).
|
|
pub fn piece_at(at: DVec3, p: &Piece, age: u32) -> DVec3 {
|
|
let (mut pos, mut vel) = (at, p.vel);
|
|
for _ in 0..age.min(p.life) {
|
|
fly(&mut pos, &mut vel, p.drag);
|
|
}
|
|
pos
|
|
}
|
|
|
|
/// How a piece is turned `age` ticks after it was thrown (a replay's, from the start).
|
|
pub fn piece_rot(p: &Piece, age: u32) -> DQuat {
|
|
let mut rot = DQuat::IDENTITY;
|
|
for _ in 0..age.min(p.life) {
|
|
rot = p.spin * rot;
|
|
}
|
|
rot
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn shares(kind: DroneKind, imp: Impact, seeds: u64) -> [u32; 3] {
|
|
let mut n = [0u32; 3];
|
|
let mut rng = Rng::new(97);
|
|
for _ in 0..seeds {
|
|
let roll = rng.f();
|
|
n[match choose(kind, &imp, roll) {
|
|
Outcome::DisabledFalling => 0,
|
|
Outcome::Breakup => 1,
|
|
Outcome::AirDetonation => 2,
|
|
}] += 1;
|
|
}
|
|
n
|
|
}
|
|
|
|
// #97: the profiles are tendencies, not laws: shells mostly leave a falling hull, a strong interceptor
|
|
// against a small target often detonates it, and everything still can happen.
|
|
#[test]
|
|
fn outcomes_follow_the_weapon_and_the_target() {
|
|
let at = DVec3::ZERO;
|
|
let gun = shares(DroneKind::Shahed, Impact::gun(SamKind::Gepard, at, 10.0), 4000);
|
|
assert!(gun[0] > 2400 && gun[2] < 400, "gun against a Shahed: {gun:?}");
|
|
assert!(gun[1] > 0 && gun[2] > 0, "not a law: some break up, a few go off: {gun:?}");
|
|
let strong = shares(DroneKind::Shahed, Impact::missile(MissileKind::M77n6, at, 6.0), 4000);
|
|
assert!(strong[2] > 2000, "a strong interceptor against a small target: {strong:?}");
|
|
let weak = shares(DroneKind::Shahed, Impact::missile(MissileKind::M57e6, at, 5.0), 4000);
|
|
assert!(weak[2] < strong[2] && weak[2] > gun[2], "in between: {weak:?}");
|
|
// A big missile is tougher than a small drone for the same interceptor.
|
|
let big = shares(DroneKind::Iskander, Impact::missile(MissileKind::Pac3, at, 6.0), 4000);
|
|
let small = shares(DroneKind::Shahed, Impact::missile(MissileKind::Pac3, at, 6.0), 4000);
|
|
assert!(big[2] < small[2], "{big:?} {small:?}");
|
|
// Without a warhead nothing goes off: a decoy or a bus breaks up or falls.
|
|
for kind in [DroneKind::Gerbera, DroneKind::Oreshnik] {
|
|
assert_eq!(shares(kind, Impact::missile(MissileKind::M77n6, at, 6.0), 2000)[2], 0, "{kind:?}");
|
|
}
|
|
}
|
|
|
|
fn kill(seed: u64, shader: bool, imp: impl Fn(DVec3) -> Impact) -> (Outcome, usize, u64) {
|
|
let mut sim = Sim::new(seed);
|
|
sim.shader = shader;
|
|
let mut out = Out::default();
|
|
let sam = sim.id();
|
|
sim.sams.push(Sam::new(sam, SamKind::Patriot, DVec3::new(0.0, 70.0, 0.0), 0.0));
|
|
sim.add_drone(DroneKind::Shahed, DVec3::new(0.0, 120.0, 300.0), DVec3::X, DVec3::new(900.0, 70.0, 0.0));
|
|
let at = sim.drones[0].pos;
|
|
let o = sim.shoot_down_by(0, sam, None, imp(at), &mut out).unwrap();
|
|
(o, sim.drones.len(), sim.rng.state())
|
|
}
|
|
|
|
// #97: the roll is combat randomness: the same with graphics on and off, and the drawing never moves it.
|
|
#[test]
|
|
fn the_outcome_is_the_same_with_graphics_on_and_off() {
|
|
for seed in 1..40 {
|
|
let strong = |at| Impact::missile(MissileKind::M77n6, at, 6.0);
|
|
assert_eq!(kill(seed, true, strong), kill(seed, false, strong), "seed {seed}");
|
|
let gun = |at| Impact::gun(SamKind::Gepard, at, 10.0);
|
|
assert_eq!(kill(seed, true, gun), kill(seed, false, gun), "seed {seed}");
|
|
}
|
|
}
|
|
|
|
// #97: a detonated warhead is spent: no wreck falls, so no second blast on landing; the kill counts once.
|
|
#[test]
|
|
fn a_detonated_target_does_no_ground_damage_and_counts_once() {
|
|
let mut found = 0;
|
|
for seed in 1..60 {
|
|
let mut sim = Sim::new(seed);
|
|
sim.damage = true;
|
|
let mut out = Out::default();
|
|
let mut ground = Ground::default();
|
|
ground.answer((-50..50).flat_map(|x| (-50..400).map(move |z| (x, z, 70))).collect(), 0);
|
|
let sam = sim.id();
|
|
sim.sams.push(Sam::new(sam, SamKind::Patriot, DVec3::new(0.0, 70.0, 0.0), 0.0));
|
|
sim.add_drone(DroneKind::Shahed, DVec3::new(0.0, 120.0, 300.0), DVec3::X, DVec3::new(900.0, 70.0, 0.0));
|
|
let at = sim.drones[0].pos;
|
|
let o = sim.shoot_down_by(0, sam, Some(5), Impact::missile(MissileKind::M77n6, at, 6.0), &mut out).unwrap();
|
|
if o != Outcome::AirDetonation {
|
|
continue;
|
|
}
|
|
found += 1;
|
|
assert!(sim.drones.is_empty(), "the main mass is gone");
|
|
assert_eq!((sim.kills, sim.hits, sim.crashes), (1, 0, 0));
|
|
assert_eq!(sim.sams[0].kills, 1);
|
|
let mut cmds = out.commands.len();
|
|
for _ in 0..400 {
|
|
sim.tick += 1;
|
|
let mut o = Out::default();
|
|
sim.step_drones(&mut ground, None, &mut o);
|
|
sim.step_fx(&mut ground, &mut o);
|
|
cmds += o.commands.len();
|
|
assert!(o.sounds.iter().all(|s| !s.0.starts_with("airdef:blast")), "a second blast");
|
|
}
|
|
assert_eq!(cmds, 0, "no primed TNT for the land from a detonated warhead");
|
|
assert_eq!(sim.kills, 1);
|
|
}
|
|
assert!(found >= 10, "{found} detonations in 59 seeds");
|
|
}
|
|
|
|
// #97: a breakup throws pieces that have a life: they expire, and the number alive is bounded in a mass battle.
|
|
#[test]
|
|
fn pieces_expire_and_a_mass_battle_keeps_them_bounded() {
|
|
let mut sim = Sim::new(3);
|
|
let mut out = Out::default();
|
|
let mut ground = Ground::default();
|
|
for i in 0..150 {
|
|
let sam = sim.id();
|
|
sim.sams.push(Sam::new(sam, SamKind::Patriot, DVec3::new(f64::from(i) * 500.0, 70.0, 0.0), 0.0));
|
|
sim.add_drone(DroneKind::Shahed, DVec3::new(f64::from(i) * 500.0, 400.0, 300.0), DVec3::X, DVec3::new(9e5, 70.0, 0.0));
|
|
let at = sim.drones.last().unwrap().pos;
|
|
let idx = sim.drones.len() - 1;
|
|
let _ = sim.shoot_down_by(idx, sam, None, Impact::missile(MissileKind::M77n6, at, 6.0), &mut out);
|
|
}
|
|
let pieces = |s: &Sim| s.fx.iter().filter(|(_, f)| matches!(f, Fx::Piece { .. })).count();
|
|
let n = pieces(&sim);
|
|
assert!(n > 0 && n <= PIECES_MAX, "{n} pieces");
|
|
for _ in 0..PIECE_LIFE + 1 {
|
|
sim.tick += 1;
|
|
sim.step_fx(&mut ground, &mut Out::default());
|
|
}
|
|
assert_eq!(pieces(&sim), 0, "pieces expire");
|
|
}
|
|
|
|
// #97: a breakup either keeps the warhead section (a wreck still falling, which may go off on landing by the
|
|
// existing roll) or leaves nothing to fall.
|
|
#[test]
|
|
fn a_breakup_keeps_the_warhead_section_only_sometimes() {
|
|
let (mut kept, mut gone) = (0, 0);
|
|
for seed in 1..200 {
|
|
let (o, drones, _) = kill(seed, true, |at| Impact::missile(MissileKind::M57e6, at, 5.0));
|
|
if o == Outcome::Breakup {
|
|
if drones == 1 {
|
|
kept += 1;
|
|
} else {
|
|
gone += 1;
|
|
}
|
|
}
|
|
}
|
|
assert!(kept > 0 && gone > kept, "kept {kept} gone {gone}");
|
|
}
|
|
|
|
// #97: a flight of a piece is a pure function of its age (so a replay moves it as the raid did).
|
|
#[test]
|
|
fn a_pieces_flight_is_the_same_stepped_or_computed() {
|
|
let ps = pieces(5, Outcome::AirDetonation, DVec3::new(0.5, 0.0, 0.2));
|
|
assert!(!ps.is_empty() && pieces(5, Outcome::DisabledFalling, DVec3::ZERO).is_empty());
|
|
let (mut pos, mut vel) = (DVec3::new(1.0, 100.0, 2.0), ps[0].vel);
|
|
for _ in 0..20 {
|
|
fly(&mut pos, &mut vel, ps[0].drag);
|
|
}
|
|
assert!(pos.distance(piece_at(DVec3::new(1.0, 100.0, 2.0), &ps[0], 20)) < 1e-9);
|
|
assert!(ps.iter().all(|p| p.life <= PIECE_LIFE));
|
|
}
|
|
}
|