//! Night fighting: guns aim by sight, and at night they hit far less unless //! the target is lit. Searchlights follow the network's tracks with their //! beams; illumination flares (fired by the guns on their own, or by the //! player) hang over the sky for half a minute and light everything round. use super::*; /// A flare's life, how far it lights, and when it stops climbing. const FLARE_LIFE: u32 = 600; const FLARE_LIGHT: f64 = 90.0; const FLARE_CLIMB: u32 = 28; /// How far a searchlight's beam reaches. pub const BEAM: f64 = 450.0; /// Guns' hits at night on an unlit target. pub const DARK_HITS: f64 = 0.4; /// Ticks between a gun's own flares. const FLARE_EVERY: u32 = 500; pub struct Flare { id: u32, pub pos: DVec3, vel: DVec3, age: u32, } impl Sim { /// Fires an illumination flare from `from` towards `dir`: it climbs, bursts and hangs. pub fn fire_flare(&mut self, from: DVec3, dir: DVec3, out: &mut Out) { let id = self.id(); let vel = dir.normalize_or(DVec3::Y) * 2.4 + DVec3::Y * 1.4; self.flares.push(Flare { id, pos: from, vel, age: 0 }); out.sound("minecraft:entity.firework_rocket.launch", from, 2.0, 0.6); if self.flares.len() > 12 { self.flares.remove(0); } } /// The flares and the searchlights, and what they light; the guns' own flares. pub(super) fn step_lights(&mut self, out: &mut Out) { self.lit.clear(); let now = self.tick; // Flares: up, a burst, then down slowly, swaying, lighting all round. let mut keep = Vec::with_capacity(self.flares.len()); for mut f in std::mem::take(&mut self.flares) { f.age += 1; if f.age < FLARE_CLIMB { f.pos += f.vel; f.vel = f.vel * 0.94 - DVec3::Y * 0.04; } else { if f.age == FLARE_CLIMB { out.sound("minecraft:entity.firework_rocket.blast", f.pos, 3.0, 0.5); out.particles("minecraft:firework", f.pos, 1.0, DVec3::ZERO, 0.15, 30); } let sway = (f.age as f64 * 0.05 + f.id as f64).sin() * 0.04; f.pos += DVec3::new(sway, -0.06, sway * 0.5); for d in self.drones.iter().filter(|d| d.alive() && d.pos.distance(f.pos) < FLARE_LIGHT) { self.lit.push(d.id); } if now % 3 == 0 { out.particles("minecraft:firework", f.pos, 0.2, DVec3::new(0.0, -0.05, 0.0), 0.02, 1); } } let fade = if f.age > FLARE_LIFE - 60 { (FLARE_LIFE - f.age) as f64 / 60.0 } else { 1.0 }; out.model(f.id, "airdef:flare", f.pos, DQuat::IDENTITY, 1.4 * fade.max(0.05), true); if f.age < FLARE_LIFE { keep.push(f); } } self.flares = keep; // What the network tracks, for the searchlights. let tracked: Vec = self.sams.iter().flat_map(|s| s.seen.iter().copied()).collect(); let gun_targets: Vec = self.sams.iter().filter_map(|s| s.gun_target).collect(); for i in 0..self.sams.len() { if self.sams[i].kind != SamKind::Light || !self.sams[i].alive() || self.sams[i].moving() { continue; } let (pos, id) = (self.sams[i].pos, self.sams[i].id); if !self.night { let sam = &mut self.sams[i]; sam.target = None; sam.pitch += (0.0 - sam.pitch) * 0.05; continue; } // The guns' target first, else the nearest tracked drone in reach. let target = self .drones .iter() .filter(|d| d.alive() && tracked.contains(&d.id) && d.pos.distance(pos) < BEAM) .min_by(|a, b| { let key = |d: &Drone| d.pos.distance(pos) * if gun_targets.contains(&d.id) { 0.3 } else { 1.0 }; key(a).total_cmp(&key(b)) }) .map(|d| (d.id, d.pos)); let sam = &mut self.sams[i]; let (head, _) = sam.mount(); let (want_yaw, want_pitch, reach) = match target { Some((_, at)) => { let to = at - head; (heading_of(to), (to.y / to.length().max(0.01)).asin(), to.length()) } // No target: a slow sweep of the sky. None => (now as f64 * 0.01 + id as f64, 0.9 + (now as f64 * 0.013).sin() * 0.3, 260.0), }; sam.yaw = slew(sam.yaw, want_yaw, 0.12); sam.pitch = slew(sam.pitch, want_pitch.clamp(0.05, 1.5), 0.08); sam.target = target.map(|t| t.0); // The beam: segments widening with distance, out to the target. let (head, rot) = self.sams[i].mount(); let dir = rot * DVec3::NEG_Z; // The target is lit once the beam is on it, not while it still swings round. if let Some((t, at)) = target && dir.angle_between(at - head) < 0.08 { self.lit.push(t); } let mut along = 1.0; // Up to 14 segments (the unit's part ids 2..15), each longer than the last: one // reaches about 550 blocks, past the beam's reach, about 9 blocks wide out there. for k in 0..14u32 { if along >= reach { break; } let size = 0.8 + along * 0.12; let len = 3.0 * size; out.model(id + 2 + k, "airdef:beam", head + dir * (along + len / 2.0), rot, size, true); along += len; } } // Guns at night light their own target with a flare now and then. if self.night { let mut fire: Vec<(DVec3, DVec3)> = Vec::new(); for sam in &mut self.sams { sam.flare_cd = sam.flare_cd.saturating_sub(1); if sam.kind.spec().gun.is_none() || !sam.alive() || sam.flare_cd > 0 { continue; } let Some(t) = sam.gun_target.and_then(|t| self.drones.iter().find(|d| d.id == t)) else { continue }; if self.lit.contains(&t.id) || self.flares.iter().any(|f| f.pos.distance(t.pos) < FLARE_LIGHT) { continue; } sam.flare_cd = FLARE_EVERY; let from = sam.pos + DVec3::Y * 2.0; fire.push((from, (t.pos + DVec3::Y * 25.0) - from)); } for (from, dir) in fire { self.fire_flare(from, dir, out); } } } }