1a693763fd
build / mod (push) Successful in 8m25s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
261 lines
11 KiB
Rust
261 lines
11 KiB
Rust
//! 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<DVec3> = 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
|
|
}
|
|
|
|
/// Whether the enemy has this unit in its intelligence: a radar emits, so it is known; the rest only once
|
|
/// they gave themselves away (a launch, jamming) and are in `known`.
|
|
pub(super) fn enemy_knows(s: &Sam, known: &[(u32, DVec3)]) -> bool {
|
|
s.alive() && (s.kind == SamKind::Radar || known.iter().any(|k| k.0 == s.id))
|
|
}
|
|
|
|
/// 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;
|
|
// The drones sent at a new target: (waiting in the queue, index), to get a new route.
|
|
let mut retargeted: Vec<(bool, usize)> = Vec::new();
|
|
let mut n = 0usize;
|
|
for (qi, (_, d)) in self.queue.iter_mut().enumerate().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 - d.err) < 6.0) {
|
|
continue;
|
|
}
|
|
let to = if !ew.is_empty() { pick(n, &ew) } else { pick(n, &known) };
|
|
d.target = to + d.err;
|
|
d.route.clear();
|
|
retargeted.push((true, qi));
|
|
n += 1;
|
|
}
|
|
// In flight: a target system destroyed — the next known one, or keep going.
|
|
for (di, d) in self.drones.iter_mut().enumerate().filter(|(_, d)| d.alive() && d.smart) {
|
|
let aimed_at_wreck = self.sams.iter().any(|s| !s.alive() && s.pos.distance(d.target - d.err) < 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.err;
|
|
d.route.clear();
|
|
retargeted.push((false, di));
|
|
}
|
|
}
|
|
}
|
|
self.replan_routes(&retargeted);
|
|
}
|
|
|
|
/// Routes round the known defences for drones that were just sent at a new target, from where they are
|
|
/// (or wait). One grid and one set of prices cover them all, and the work is the planner's share for a
|
|
/// tick: those left over fly straight, as they did before there was a replan. A Lancet's strike on a
|
|
/// unit and the ballistic missiles go straight on purpose, as in the raid's own plan.
|
|
fn replan_routes(&mut self, jobs: &[(bool, usize)]) {
|
|
let drone = |s: &Sim, (queued, i): (bool, usize)| if queued { s.queue[i].1.clone() } else { s.drones[i].clone() };
|
|
let jobs: Vec<(bool, usize)> = jobs
|
|
.iter()
|
|
.copied()
|
|
.filter(|&j| {
|
|
let d = drone(self, j);
|
|
let spec = d.kind.spec();
|
|
d.smart && spec.ballistic.is_none() && !spec.decoy && d.kind != DroneKind::Lancet
|
|
})
|
|
.collect();
|
|
if jobs.is_empty() {
|
|
return;
|
|
}
|
|
// One grid over all their starts and goals, with room round them to go by.
|
|
let (mut lo, mut hi) = (DVec3::splat(f64::INFINITY), DVec3::splat(f64::NEG_INFINITY));
|
|
for &j in &jobs {
|
|
let d = drone(self, j);
|
|
for p in [d.pos, d.target] {
|
|
lo = lo.min(p);
|
|
hi = hi.max(p);
|
|
}
|
|
}
|
|
let span = (hi.x - lo.x).max(hi.z - lo.z) / 2.0 + 450.0;
|
|
let cell = raid::CELL.max(2.0 * span / raid::CELLS as f64);
|
|
let centre = (lo + hi) / 2.0;
|
|
let grid = raid::Grid { min: centre - DVec3::new(span, 0.0, span), cell, size: (2.0 * span / cell) as i32 + 1 };
|
|
let mut prices: HashMap<DroneKind, Vec<f32>> = HashMap::new();
|
|
let mut routes: HashMap<(DroneKind, (i32, i32), (i32, i32)), Vec<DVec3>> = HashMap::new();
|
|
let mut work = 0usize;
|
|
for j in jobs {
|
|
let d = drone(self, j);
|
|
if work >= raid::WORK_PER_TICK {
|
|
break;
|
|
}
|
|
let key = (d.kind, grid.index(d.pos), grid.index(d.target));
|
|
let route = match routes.get(&key) {
|
|
Some(r) => r.clone(),
|
|
None => {
|
|
if !prices.contains_key(&d.kind) {
|
|
let (p, cost) = self.price_grid(&grid, d.kind);
|
|
work += cost;
|
|
prices.insert(d.kind, p);
|
|
}
|
|
let (r, cost) = raid::plan_route(&grid, &prices[&d.kind], d.pos, d.target);
|
|
work += cost;
|
|
routes.insert(key, r.clone());
|
|
r
|
|
}
|
|
};
|
|
let d = if j.0 { &mut self.queue[j.1].1 } else { &mut self.drones[j.1] };
|
|
d.route = route;
|
|
}
|
|
}
|
|
}
|