//! Air defence can be hit too: a blast near a system damages it (it turns //! slower, sees less, hits worse, smokes, then burns), a destroyed one leaves //! a burning wreck and its magazine cooks off — the more missiles or shells //! were left in it, the bigger the blast and the longer the secondary ones. //! Marked objects take the same blasts. use super::*; /// By blast power (as `explode` grades it): how far it reaches a vehicle, and how hard it hits one at the centre. const REACH: [f64; 5] = [4.0, 7.0, 12.0, 18.0, 26.0]; const HIT: [f64; 5] = [0.2, 0.45, 0.85, 1.25, 1.7]; /// 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]; impl MissileKind { /// The explosive a missile left in a burning launcher adds (TNT). fn charge(self) -> f64 { match self { MissileKind::Pac3 => 0.8, MissileKind::M57e6 => 0.45, MissileKind::M48n6 => 1.8, MissileKind::M77n6 => 2.2, MissileKind::IrisT => 0.6, MissileKind::Amraam => 0.5, MissileKind::M9m317 => 1.2, MissileKind::M9m338 => 0.4, MissileKind::P1sun => 0.1, } } } 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) { let (reach, hit) = (REACH[power], HIT[power]); let mut destroyed = Vec::new(); for (i, sam) in self.sams.iter_mut().enumerate() { if !sam.alive() { continue; } // The vehicle is a few blocks long: measured from its hull. let d = (sam.pos.distance(pos) - 2.5).max(0.0); if d >= reach { continue; } let k = 1.0 - d / reach; let before = sam.health; sam.health -= hit * k * k * if d < 1.0 { 1.5 } else { 1.0 }; if sam.health <= 0.0 { destroyed.push(i); } else if before >= 0.5 && sam.health < 0.5 { out.messages.push(format!("{} №{} повреждён", sam.kind.name(), i + 1)); } } for i in destroyed { self.destroy(i, out); } for (i, t) in self.targets.iter().enumerate() { let d = t.distance(pos); let r = reach * 1.6; 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); if before > 0.0 && *h <= 0.0 { out.messages.push(format!("ОБЪЕКТ №{} УНИЧТОЖЕН", i + 1)); } } } } } /// System `i` destroyed: its magazine goes up, the wreck burns. fn destroy(&mut self, i: usize, out: &mut Out) { let now = self.tick; let sam = &mut self.sams[i]; sam.health = 0.0; sam.burn = 900; sam.state = SamState::Destroyed; sam.target = None; sam.gun_target = None; sam.reloading = 0; let spec = sam.kind.spec(); let missiles = sam.missiles; let bursts = sam.bursts; sam.missiles = 0; sam.bursts = 0; let pos = sam.pos + DVec3::Y; out.messages.push(format!("{} №{} УНИЧТОЖЕН", sam.kind.name(), i + 1)); let (id, kind) = (sam.id, sam.kind); // A depot's stock (or a truck's load) goes up with it. let stock = if matches!(self.sams[i].kind, SamKind::Depot | SamKind::Tzm) { self.sams[i].cargo } else { 0 }; self.sams[i].cargo = 0; let missiles = missiles + stock * 2; let charge = spec.missile.as_ref().map_or(0.0, |m| m.kind.charge() * missiles as f64) + bursts as f64 / 60.0 + stock as f64 * 1.5; // The vehicle's own fuel and the first of its load at once, then the rest cooking off. let first = (1.0 + charge * 0.6).round().clamp(1.0, 8.0) as u32; self.later.push((now + 2, pos, first, false)); for k in 0..missiles.min(40) { let when = now + 15 + (k as u64) * 12 + (self.rng.f() * 25.0) as u64; let at = pos + DVec3::new(self.rng.s() * 3.0, self.rng.f() * 2.0, self.rng.s() * 3.0); let tnt = if charge > 4.0 && self.rng.chance(0.4) { 2 } else { 1 }; self.later.push((when, at, tnt, true)); } self.log(pos, record::EventKind::SamLost { sam: id, kind, index: i }); // Shells pop in strings. for k in 0..(bursts / 12).min(14) { let when = now + 10 + (k as u64) * 6 + (self.rng.f() * 8.0) as u64; let at = pos + DVec3::new(self.rng.s() * 2.0, self.rng.f() * 1.5, self.rng.s() * 2.0); self.later.push((when, at, 0, true)); } } /// The blasts whose time has come. pub(super) fn step_later(&mut self, ground: &mut Ground, player: Option, out: &mut Out) { let now = self.tick; let due: Vec<_> = self.later.iter().filter(|l| l.0 <= now).copied().collect(); self.later.retain(|l| l.0 > now); for (_, pos, tnt, small) in due { self.explode(pos, tnt, small, ground, player, out); if small { // Something flies out of a burning launcher. out.particles("minecraft:lava", pos, 0.8, DVec3::new(0.0, 0.3, 0.0), 0.3, 12); out.particles("minecraft:firework", pos, 0.5, DVec3::new(0.0, 0.2, 0.0), 0.25, 10); } } } /// Damaged systems smoke, wrecks burn. pub(super) fn step_wrecks(&mut self, out: &mut Out) { let tick = self.tick; for sam in &mut self.sams { let top = sam.pos + DVec3::Y * (1.5 * sam.kind.spec().scale); if !sam.alive() { if sam.burn > 0 { sam.burn -= 1; out.particles("minecraft:flame", top, 1.0, DVec3::new(0.0, 0.05, 0.0), 0.03, 3); out.particles("minecraft:large_smoke", top, 0.8, DVec3::new(0.0, 0.12, 0.0), 0.03, 2); if tick % 8 == 0 { out.particles("minecraft:campfire_cosy_smoke", top, 1.0, DVec3::new(0.0, 0.07, 0.0), 0.01, 1); } if tick % 40 == 0 { out.sound("minecraft:block.fire.ambient", top, 3.0, 0.7); } } else if tick % 12 == 0 { out.particles("minecraft:campfire_signal_smoke", top, 0.6, DVec3::new(0.0, 0.05, 0.0), 0.01, 1); } } else if sam.health < 0.7 { let every = if sam.health < 0.35 { 2 } else { 5 }; if tick % every == 0 { out.particles("minecraft:large_smoke", top, 0.4, DVec3::new(0.0, 0.08, 0.0), 0.02, 1); } if sam.health < 0.35 && tick % 3 == 0 { out.particles("minecraft:flame", top, 0.3, DVec3::ZERO, 0.01, 1); } } } } } impl Sam { /// Its wreck still burns. pub fn burning(&self) -> bool { self.burn > 0 } } impl Sim { /// Repairs every system (wrecks rebuilt, magazines full) and object. pub fn repair(&mut self) -> u32 { let mut n = 0; for sam in &mut self.sams { if sam.health < 1.0 { n += 1; } let spec = sam.kind.spec(); sam.health = 1.0; sam.burn = 0; sam.state = SamState::Search; sam.reloading = 0; sam.empty = false; // Rebuilt where it stands: a march under way is called off. sam.dest = None; sam.march = 0; sam.missiles = spec.missile.as_ref().map_or(0, |m| m.magazine); sam.bursts = spec.gun.as_ref().map_or(0, |g| g.bursts); } for h in &mut self.target_health { *h = 100.0; } // The wrecks' ammunition still to cook off goes with them. self.later.clear(); n } /// A wreck: the hull sunk and turned, the launcher or turret thrown down beside it. pub(super) fn draw_wreck(&self, i: usize, out: &mut Out) { let sam = &self.sams[i]; let s = sam.kind.spec().scale; let tilt = DQuat::from_rotation_y(sam.heading) * DQuat::from_rotation_z(0.12) * DQuat::from_rotation_x(-0.06); let hull = sam.pos + DVec3::Y * (0.5 * s - 0.35); let side = DQuat::from_rotation_y(sam.heading) * DVec3::new(1.8 * s, 0.0, 0.6 * s); let thrown = DQuat::from_rotation_y(sam.heading + 1.1) * DQuat::from_rotation_z(1.2); let parts = &sam.kind.spec().parts; let (body, part) = (parts.body, parts.launcher.or(parts.pack)); out.model(sam.id, body, hull, tilt, s, false); if let Some(part) = part { out.model(sam.id + 1, part, sam.pos + side + DVec3::Y * 0.4 * s, thrown, s, false); } } }