//! Electronic warfare, and what the enemy learns. //! //! An EW station jams the satellite navigation of the drones and cruise //! missiles near it (ballistic missiles and the Lancet's optics don't care): //! they lose their route and drift off their aim. It runs only a short while //! before its transmitter overheats, and while it transmits it gives itself //! away. The enemy keeps what it learns — which systems fired and which //! stations jammed (its next drones go for them), and from which bearings its //! raids were shot down or got through (its next raid comes from the best). use super::*; /// How far a station jams. pub const EW_RADIUS: f64 = 350.0; /// Heat a tick on (20 s to overheat), and cooling a tick off (45 s from hot). const HEAT: f64 = 1.0 / 400.0; const COOL: f64 = 1.0 / 900.0; /// After overheating it may switch on again only below this. const READY: f64 = 0.3; /// Approach sectors the enemy keeps score of. pub const SECTORS: usize = 12; #[derive(Default)] pub(super) struct Intel { /// Shot down and got through, by approach sector, over every raid. pub kills: [u32; SECTORS], pub hits: [u32; SECTORS], /// Units that gave themselves away: (id, where, when) — launches, jamming. pub revealed: Vec<(u32, DVec3, u64)>, } impl DroneKind { /// It navigates by satellite: EW throws it off. pub fn jammable(self) -> bool { matches!(self, DroneKind::Shahed | DroneKind::Geran3 | DroneKind::Gerbera | DroneKind::Kh101 | DroneKind::Kalibr) } } /// The sector of a bearing (radians). pub fn sector(bearing: f64) -> usize { ((bearing.rem_euclid(TAU) / TAU) * SECTORS as f64) as usize % SECTORS } impl Sim { /// Switches every EW station on or off; one cooling down after overheating stays off. /// Returns how many are now on, and how many are still too hot. pub fn set_ew(&mut self, on: bool) -> (u32, u32) { let (mut n, mut hot) = (0, 0); for s in self.sams.iter_mut().filter(|s| s.kind == SamKind::Ew && s.alive()) { if on && s.ew_lock { hot += 1; continue; } s.ew_on = on && !s.moving(); n += u32::from(s.ew_on); } (n, hot) } pub fn ew_active(&self) -> bool { self.sams.iter().any(|s| s.ew_on) } /// The stations' heat, and their jamming. pub(super) fn step_ew(&mut self, out: &mut Out) { let now = self.tick; let mut on: Vec = Vec::new(); for s in self.sams.iter_mut().filter(|s| s.kind == SamKind::Ew) { if !s.alive() || s.moving() { s.ew_on = false; } if s.ew_on { s.heat += HEAT; if s.heat >= 1.0 { s.heat = 1.0; s.ew_on = false; s.ew_lock = true; out.messages.push("РЭБ: передатчик перегрет, выключен".into()); out.sound("minecraft:block.fire.extinguish", s.pos, 2.0, 0.7); } } else { s.heat = (s.heat - COOL).max(0.0); if s.ew_lock && s.heat < READY { s.ew_lock = false; } } if s.ew_on { let top = s.pos + DVec3::Y * 2.6 * s.kind.spec().scale; on.push(s.pos); if now % 3 == 0 { out.particles("minecraft:electric_spark", top, 0.6, DVec3::ZERO, 0.2, 3); } if now % 40 == 0 { out.sound("minecraft:block.beacon.ambient", top, 2.0, 1.4); } // A transmitter on the air: the enemy finds it. if now % 20 == 0 { self.intel.revealed.push((s.id, s.pos, now)); } } } if on.is_empty() { return; } for d in self.drones.iter_mut().filter(|d| d.alive() && d.kind.jammable()) { let Some(k) = on.iter().map(|p| 1.0 - horizontal(d.pos - *p).length() / EW_RADIUS).reduce(f64::max) else { continue }; if k <= 0.0 { continue; } // Its fix wanders: the aim drifts, the route is lost. d.route.clear(); d.target += DVec3::new(self.rng.s(), 0.0, self.rng.s()) * (1.2 * k); d.jammed = true; } } /// What the enemy's raids learned: their drones' fates by sector. pub(super) fn learn(&mut self, sector: usize, killed: bool) { if killed { self.intel.kills[sector % SECTORS] += 1; } else { self.intel.hits[sector % SECTORS] += 1; } } /// The bearing a smart raid comes from: the sector its raids got through best, a little random. pub(super) fn best_bearing(&mut self) -> f64 { let score = |i: usize| { let (k, h) = (self.intel.kills[i] as f64, self.intel.hits[i] as f64); (h + 1.0) / (k + h + 2.0) }; let best = (0..SECTORS).max_by(|&a, &b| score(a).total_cmp(&score(b))).unwrap_or(0); // Unknown sectors are worth a look too. let pick = if self.rng.chance(0.3) { (self.rng.f() * SECTORS as f64) as usize } else { best }; (pick as f64 + 0.5 + self.rng.s() * 0.4) / SECTORS as f64 * TAU } /// Units the enemy knows of and would go for, newest first: (id, where). pub(super) fn known_units(&self) -> Vec<(u32, DVec3)> { let mut out: Vec<(u32, DVec3)> = Vec::new(); for &(id, at, _) in self.intel.revealed.iter().rev() { if !out.iter().any(|o| o.0 == id) && self.sams.iter().any(|s| s.id == id && s.alive()) { out.push((id, at)); } } out } /// During a smart raid: its drones not yet launched go for what just gave itself away (a jamming station /// first, then systems that fired), and drones whose system target is gone pick another. pub(super) fn adapt(&mut self) { if self.tick % 40 != 0 { return; } self.intel.revealed.retain(|r| self.tick - r.2 < 6000); let known = self.known_units(); let ew: Vec<(u32, DVec3)> = known.iter().copied().filter(|(id, _)| self.sams.iter().any(|s| s.id == *id && s.kind == SamKind::Ew)).collect(); if known.is_empty() { return; } let pick = |i: usize, list: &[(u32, DVec3)]| list[i % list.len()].1; let mut n = 0usize; for (_, d) in self.queue.iter_mut().filter(|(_, d)| d.smart) { let hunter = matches!(d.kind, DroneKind::Lancet | DroneKind::Kh22 | DroneKind::Zircon); if !hunter && !(d.kind == DroneKind::Shahed && n % 4 == 0) { n += 1; continue; } // Only those not yet sent at a unit. if self.sams.iter().any(|s| s.pos.distance(d.target) < 6.0) { continue; } let to = if !ew.is_empty() { pick(n, &ew) } else { pick(n, &known) }; d.target = to; d.route.clear(); n += 1; } // In flight: a target system destroyed — the next known one, or keep going. for d in self.drones.iter_mut().filter(|d| d.alive() && d.smart) { let aimed_at_wreck = self.sams.iter().any(|s| !s.alive() && s.pos.distance(d.target) < 6.0); if aimed_at_wreck { let next = known.iter().min_by(|a, b| a.1.distance(d.pos).total_cmp(&b.1.distance(d.pos))); if let Some((_, at)) = next { d.target = *at; d.route.clear(); } } } } }