New systems (IRIS-T, NASAMS, Buk, Tor, P1-SUN), air-defence network with long-range radars, logistics, move orders, EW, adaptive enemy, searchlights and flares; grid holds a kill in view
build / mod (push) Successful in 5m7s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 12:46:30 +03:00
parent a2f31c831c
commit d3359f4b39
98 changed files with 13623 additions and 298 deletions
+17 -10
View File
@@ -19,6 +19,11 @@ impl MissileKind {
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,
}
}
}
@@ -82,11 +87,16 @@ impl Sim {
let pos = sam.pos + DVec3::Y;
out.messages.push(format!("{} №{} УНИЧТОЖЕН", sam.kind.name(), i + 1));
let (id, kind) = (sam.id, sam.kind);
let charge = spec.missile.as_ref().map_or(0.0, |m| m.kind.charge() * missiles as f64) + bursts as f64 / 60.0;
// 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 {
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 };
@@ -186,14 +196,11 @@ impl Sim {
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 (body, part) = match sam.kind {
SamKind::Patriot => ("airdef:patriot_trailer", "airdef:patriot_launcher"),
SamKind::Gepard => ("airdef:gepard_hull", "airdef:gepard_turret"),
SamKind::Pantsir => ("airdef:pantsir_truck", "airdef:pantsir_turret"),
SamKind::S400 => ("airdef:s400_chassis", "airdef:s400_tubes"),
SamKind::S500 => ("airdef:s500_chassis", "airdef:s500_tubes"),
};
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);
out.model(sam.id + 1, part, sam.pos + side + DVec3::Y * 0.4 * s, thrown, s, false);
if let Some(part) = part {
out.model(sam.id + 1, part, sam.pos + side + DVec3::Y * 0.4 * s, thrown, s, false);
}
}
}
+190
View File
@@ -0,0 +1,190 @@
//! 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
}
/// During a raid: 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() {
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()) {
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();
}
}
}
}
}
+144
View File
@@ -0,0 +1,144 @@
//! 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<u32> = self.sams.iter().flat_map(|s| s.seen.iter().copied()).collect();
let gun_targets: Vec<u32> = 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);
if let Some((t, _)) = target {
self.lit.push(t);
}
// The beam: segments widening with distance, out to the target.
let (head, rot) = self.sams[i].mount();
let dir = rot * DVec3::NEG_Z;
let mut along = 1.0;
for k in 0..10u32 {
if along >= reach {
break;
}
let size = 0.8 + along * 0.025;
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);
}
}
}
}
+112
View File
@@ -0,0 +1,112 @@
//! Logistics: once a depot stands, magazines are no longer refilled from
//! nowhere. A system out of missiles waits; one within reach of a depot
//! reloads from its stock; the reload trucks fill up at a depot and drive
//! reloads to the systems that wait, nearest first, and back.
use super::*;
/// How near a depot a system reloads straight from it.
const DEPOT_REACH: f64 = 60.0;
/// How near a truck must come to hand a reload over, or to load up at a depot.
const HANDOVER: f64 = 10.0;
/// What a truck carries.
const TRUCK_LOAD: u32 = 2;
impl Sim {
/// Whether ammunition is counted: a depot stands (without one, magazines refill as before).
pub fn logistics(&self) -> bool {
self.sams.iter().any(|s| s.kind == SamKind::Depot && s.alive())
}
/// Reloads from depots near the systems, and the trucks' rounds.
pub(super) fn step_supply(&mut self, out: &mut Out) {
if !self.logistics() {
for s in &mut self.sams {
s.empty = false;
}
return;
}
let now = self.tick;
// Systems by a depot reload from it.
for i in 0..self.sams.len() {
if !self.sams[i].empty || !self.sams[i].alive() {
continue;
}
let pos = self.sams[i].pos;
if let Some(d) = self
.sams
.iter()
.position(|d| d.kind == SamKind::Depot && d.alive() && d.cargo > 0 && d.pos.distance(pos) < DEPOT_REACH)
{
self.sams[d].cargo -= 1;
self.reload_now(i, out);
}
}
// The trucks: a job each, when idle.
for t in 0..self.sams.len() {
let truck = &self.sams[t];
if truck.kind != SamKind::Tzm || !truck.alive() || truck.moving() || now % 10 != t as u64 % 10 {
continue;
}
let at = truck.pos;
// Where it was going: hand over, or load up.
if let Some(job) = truck.job
&& let Some(j) = self.sams.iter().position(|s| s.id == job)
{
if self.sams[j].pos.distance(at) <= HANDOVER {
if self.sams[j].kind == SamKind::Depot {
let take = TRUCK_LOAD.saturating_sub(self.sams[t].cargo).min(self.sams[j].cargo);
self.sams[j].cargo -= take;
self.sams[t].cargo += take;
out.sound("minecraft:block.piston.extend", at, 1.5, 0.8);
} else if self.sams[j].empty && self.sams[t].cargo > 0 {
self.sams[t].cargo -= 1;
self.reload_now(j, out);
}
self.sams[t].job = None;
} else if self.sams[j].alive() && (self.sams[j].empty || self.sams[j].kind == SamKind::Depot) {
continue; // still on its way (a new order follows when it arrives)
} else {
self.sams[t].job = None;
}
}
// A new job: the nearest waiting system no other truck is driving to, or a depot to load at.
let taken: Vec<u32> = self.sams.iter().filter(|s| s.kind == SamKind::Tzm).filter_map(|s| s.job).collect();
let next = if self.sams[t].cargo > 0 {
self.sams
.iter()
.filter(|s| s.empty && s.alive() && !s.moving() && !taken.contains(&s.id))
.min_by(|a, b| a.pos.distance(at).total_cmp(&b.pos.distance(at)))
.map(|s| (s.id, s.pos))
} else {
None
};
let next = next.or_else(|| {
(self.sams[t].cargo < TRUCK_LOAD)
.then(|| {
self.sams
.iter()
.filter(|d| d.kind == SamKind::Depot && d.alive() && d.cargo > 0)
.min_by(|a, b| a.pos.distance(at).total_cmp(&b.pos.distance(at)))
.map(|d| (d.id, d.pos))
})
.flatten()
});
if let Some((id, to)) = next {
self.sams[t].job = Some(id);
// Stop beside it, not on it.
let side = horizontal(at - to).normalize_or(DVec3::X) * (HANDOVER * 0.6);
if to.distance(at) > HANDOVER {
self.order_move(t, to + side);
}
}
}
}
/// System `i` gets a reload: its magazine fills after its reload time.
fn reload_now(&mut self, i: usize, out: &mut Out) {
let sam = &mut self.sams[i];
sam.empty = false;
sam.reloading = sam.kind.spec().reload.max(1);
out.sound("minecraft:block.piston.extend", sam.pos, 2.0, 0.6);
}
}
+266 -250
View File
@@ -13,6 +13,14 @@ mod damage;
mod raid;
pub mod record;
mod sight;
mod ew;
mod lights;
mod logistics;
mod orders;
mod units;
pub use ew::EW_RADIUS;
pub use units::{Family, SamKind};
use units::Muzzle;
pub use raid::{Approach, Aim, RaidOrder};
pub const TAU: f64 = std::f64::consts::TAU;
@@ -645,6 +653,10 @@ pub struct Drone {
route: Vec<DVec3>,
/// Hugs the land all the way, under the radars.
low: bool,
/// Its navigation is being jammed.
pub jammed: bool,
/// The approach sector it came from (the enemy keeps score).
sector: u8,
}
impl Drone {
@@ -667,6 +679,12 @@ pub enum MissileKind {
M57e6,
M48n6,
M77n6,
IrisT,
Amraam,
M9m317,
M9m338,
/// Not a missile: an interceptor drone, slow and cheap, that rams Shaheds.
P1sun,
}
struct MissileSpec {
@@ -695,6 +713,9 @@ impl MissileKind {
MissileKind::M57e6 => "airdef:launch.pantsir",
MissileKind::M48n6 => "airdef:launch.s400_eject",
MissileKind::M77n6 => "airdef:launch.s500_eject",
MissileKind::IrisT | MissileKind::M9m338 => "airdef:launch.s400_eject",
MissileKind::Amraam | MissileKind::M9m317 => "airdef:launch.patriot",
MissileKind::P1sun => "airdef:drone.lancet",
}
}
fn spec(self) -> &'static MissileSpec {
@@ -755,6 +776,77 @@ impl MissileKind {
flame: 1.6,
rear: 32.0,
},
MissileKind::IrisT => &MissileSpec {
model: "airdef:pac3",
speed: 7.0,
accel: 0.5,
turn: 0.35,
boost: 6,
cold: 4,
pk: 0.9,
fuse: 4.0,
life: 260,
scale: 0.95,
flame: 0.9,
rear: 28.0,
},
MissileKind::Amraam => &MissileSpec {
model: "airdef:pac3",
speed: 6.0,
accel: 0.45,
turn: 0.3,
boost: 6,
cold: 0,
pk: 0.85,
fuse: 4.0,
life: 260,
scale: 0.9,
flame: 0.9,
rear: 28.0,
},
MissileKind::M9m317 => &MissileSpec {
model: "airdef:m48n6",
speed: 6.5,
accel: 0.5,
turn: 0.25,
boost: 8,
cold: 0,
pk: 0.85,
fuse: 5.0,
life: 300,
scale: 1.1,
flame: 1.2,
rear: 32.0,
},
MissileKind::M9m338 => &MissileSpec {
model: "airdef:m57e6",
speed: 5.5,
accel: 0.6,
turn: 0.4,
boost: 4,
cold: 4,
pk: 0.9,
fuse: 3.0,
life: 160,
scale: 1.0,
flame: 0.8,
rear: 28.0,
},
MissileKind::P1sun => &MissileSpec {
model: "airdef:p1sun",
speed: 2.8,
accel: 0.12,
turn: 0.3,
boost: 4,
cold: 0,
pk: 0.8,
fuse: 3.0,
life: 900,
scale: 0.8,
// Electric: no flame.
flame: 0.0,
rear: 10.0,
},
}
}
}
@@ -772,120 +864,6 @@ pub struct Missile {
// ---------------------------------------------------------------- air defence
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum SamKind {
Patriot,
Gepard,
Pantsir,
S400,
S500,
}
struct MissileLoad {
kind: MissileKind,
magazine: u32,
min: f64,
max: f64,
/// Ticks between launches.
interval: u32,
}
struct GunSpec {
range: f64,
/// Starts tracking from here.
track: f64,
/// Chance per burst at point blank.
p: f64,
bursts: u32,
/// Shell speed for the lead, blocks a tick.
shell: f64,
slew: (f64, f64),
}
pub struct SamSpec {
pub name: &'static str,
pub key: &'static str,
missile: Option<MissileLoad>,
gun: Option<GunSpec>,
reload: u32,
scale: f64,
/// Where it points its launcher at rest (pitch, radians).
rest_pitch: f64,
/// Which classes of target it engages unless told otherwise.
pub roles: u8,
}
impl SamKind {
pub const ALL: [SamKind; 5] = [SamKind::Patriot, SamKind::Gepard, SamKind::Pantsir, SamKind::S400, SamKind::S500];
pub fn spec(self) -> &'static SamSpec {
match self {
SamKind::Patriot => &SamSpec {
name: "Patriot",
key: "patriot",
missile: Some(MissileLoad { kind: MissileKind::Pac3, magazine: 4, min: 10.0, max: 300.0, interval: 16 }),
gun: None,
reload: 220,
scale: 1.3,
rest_pitch: 0.66,
roles: 30,
},
SamKind::Gepard => &SamSpec {
name: "Гепард",
key: "gepard",
missile: None,
gun: Some(GunSpec { range: 90.0, track: 130.0, p: 0.3, bursts: 160, shell: 10.0, slew: (0.22, 0.16) }),
reload: 160,
scale: 1.25,
rest_pitch: 0.0,
roles: 7,
},
SamKind::Pantsir => &SamSpec {
name: "Панцирь-С1",
key: "pantsir",
missile: Some(MissileLoad { kind: MissileKind::M57e6, magazine: 12, min: 25.0, max: 150.0, interval: 14 }),
gun: Some(GunSpec { range: 60.0, track: 90.0, p: 0.25, bursts: 140, shell: 9.0, slew: (0.25, 0.18) }),
reload: 280,
scale: 1.3,
rest_pitch: 0.0,
roles: 15,
},
SamKind::S400 => &SamSpec {
name: "С-400",
key: "s400",
missile: Some(MissileLoad { kind: MissileKind::M48n6, magazine: 4, min: 30.0, max: 480.0, interval: 20 }),
gun: None,
reload: 420,
scale: 1.5,
rest_pitch: HALF_PI - 0.02,
roles: 30,
},
SamKind::S500 => &SamSpec {
name: "С-500",
key: "s500",
missile: Some(MissileLoad { kind: MissileKind::M77n6, magazine: 4, min: 40.0, max: 700.0, interval: 20 }),
gun: None,
reload: 520,
scale: 1.6,
rest_pitch: HALF_PI - 0.02,
roles: 24,
},
}
}
pub fn name(self) -> &'static str {
self.spec().name
}
pub fn key(self) -> &'static str {
self.spec().key
}
pub fn from_key(key: &str) -> Option<Self> {
Self::ALL.into_iter().find(|k| k.key() == key)
}
pub fn range(self) -> f64 {
let s = self.spec();
s.missile.as_ref().map_or(0.0, |m| m.max).max(s.gun.as_ref().map_or(0.0, |g| g.range))
}
}
/// What a system is doing, for the monitor.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SamState {
@@ -898,6 +876,12 @@ pub enum SamState {
Reload,
Hold,
Destroyed,
/// A move order: packing up, on the march, deploying again.
Packing,
Moving,
Deploying,
/// Out of missiles, waiting for a reload truck (logistics on: a depot stands).
Waiting,
}
impl SamState {
@@ -912,6 +896,10 @@ impl SamState {
SamState::Reload => "ПЕРЕЗАРЯДКА",
SamState::Hold => "ОГОНЬ ЗАПРЕЩЁН",
SamState::Destroyed => "УНИЧТОЖЕН",
SamState::Packing => "СВЁРТЫВАНИЕ",
SamState::Moving => "НА МАРШЕ",
SamState::Deploying => "РАЗВЁРТЫВАНИЕ",
SamState::Waiting => "ЖДЁТ БК",
}
}
pub fn color(self) -> u32 {
@@ -924,6 +912,8 @@ impl SamState {
SamState::Reload => 0x80A0FF,
SamState::Hold => 0xA0A0A0,
SamState::Destroyed => 0xFF2020,
SamState::Packing | SamState::Moving | SamState::Deploying => 0xC0A0FF,
SamState::Waiting => 0xFF9040,
}
}
}
@@ -959,8 +949,28 @@ pub struct Sam {
pub health: f64,
/// Ticks its wreck still burns.
burn: u32,
/// The drones its radar holds this tick.
/// The drones it has tracks of this tick: its own radar's and the network's.
pub seen: Vec<u32>,
/// The drones its own radar (or optics) holds: guns need these.
pub own: Vec<u32>,
/// What it just shot down, where, and till when its camera stays on it.
pub kill_view: Option<(u32, DVec3, u64)>,
/// A move order's destination, and ticks left packing up or deploying.
pub dest: Option<DVec3>,
march: u32,
/// A depot's reloads in stock; a reload truck's on board.
pub cargo: u32,
/// A reload truck's job: the system it is driving to, or a depot.
job: Option<u32>,
/// Out of ammunition, waiting for a reload (logistics on).
pub empty: bool,
/// An EW station: jamming, and its transmitter's heat (0 cold, 1 forced off).
pub ew_on: bool,
pub heat: f64,
/// Overheated: off until it cools.
pub ew_lock: bool,
/// Ticks till a gun fires its next illumination flare.
flare_cd: u32,
}
impl Sam {
@@ -991,6 +1001,21 @@ impl Sam {
health: 1.0,
burn: 0,
seen: Vec::new(),
own: Vec::new(),
kill_view: None,
dest: None,
march: 0,
cargo: match kind {
SamKind::Depot => 30,
SamKind::Tzm => 2,
_ => 0,
},
job: None,
empty: false,
ew_on: false,
heat: 0.0,
ew_lock: false,
flare_cd: 0,
}
}
pub fn alive(&self) -> bool {
@@ -1000,67 +1025,29 @@ impl Sam {
fn fitness(&self) -> f64 {
0.45 + 0.55 * self.health.clamp(0.0, 1.0)
}
fn base(&self) -> DQuat {
DQuat::from_rotation_y(self.heading)
}
fn scale(&self) -> f64 {
self.kind.spec().scale
}
/// Where shells and missiles leave: the centre of the turret or the
/// launcher's pivot, and the rotation they leave with.
fn mount(&self) -> (DVec3, DQuat) {
let s = self.scale();
let base = self.base();
match self.kind {
SamKind::Patriot => (
self.pos + base * DVec3::new(0.0, 0.69 * s, 1.2 * s),
DQuat::from_rotation_y(self.yaw) * DQuat::from_rotation_x(self.pitch),
),
SamKind::Gepard => (
self.pos + DVec3::new(0.0, 1.55 * s, 0.0) + base * DVec3::new(0.0, 0.0, 0.25 * s),
DQuat::from_rotation_y(self.yaw) * DQuat::from_rotation_x(self.pitch),
),
SamKind::Pantsir => (
self.pos + DVec3::new(0.0, 1.35 * s, 0.0) + base * DVec3::new(0.0, 0.0, 0.69 * s),
DQuat::from_rotation_y(self.yaw) * DQuat::from_rotation_x(self.pitch),
),
SamKind::S400 | SamKind::S500 => (
self.pos + base * DVec3::new(0.0, 0.7 * s, 1.45 * s),
base * DQuat::from_rotation_x(self.pitch),
),
}
}
/// Where missile `n` of the magazine leaves (world), and which way.
fn muzzle(&self, n: u32) -> (DVec3, DVec3) {
let (at, rot) = self.mount();
let s = self.scale();
let local = match self.kind {
SamKind::Patriot => DVec3::new(
if n % 2 == 0 { -0.42 } else { 0.42 },
0.625 + if n % 4 < 2 { 0.28 } else { -0.28 },
-2.8,
),
SamKind::Pantsir => {
let side = if n % 2 == 0 { -0.94 } else { 0.94 };
let row = (n / 2) % 6;
DVec3::new(side + ((row % 3) as f64 - 1.0) * 0.15, 0.1 + if row < 3 { 0.15 } else { -0.15 }, -1.5)
}
SamKind::S400 => {
let i = n % 4;
// From the pivot (rear bottom of the pack) to the top of tube i.
DVec3::new(if i % 2 == 0 { -0.25 } else { 0.25 }, if i < 2 { 0.22 } else { 0.72 }, -3.0)
}
SamKind::S500 => DVec3::new(if n % 2 == 0 { -0.33 } else { 0.33 }, 0.3, -3.0),
SamKind::Gepard => DVec3::ZERO,
};
(at + rot * local * s, rot * DVec3::NEG_Z)
}
pub fn status(&self) -> String {
let spec = self.kind.spec();
let mut text = spec.name.to_string();
if !self.alive() {
return text + " УНИЧТОЖЕН";
}
if self.moving() {
return text + " " + &self.state.name().to_lowercase();
}
match self.kind {
SamKind::Depot => return format!("{text}: запас {} перезарядок", self.cargo),
SamKind::Tzm => return format!("{text}: на борту {}/2{}", self.cargo, if self.job.is_some() { ", в пути" } else { "" }),
SamKind::Ew => {
let state = if self.ew_on { "ГЛУШИТ" } else if self.ew_lock { "остывает" } else { "готова" };
return format!("{text}: {state}, нагрев {:.0}%", self.heat * 100.0);
}
SamKind::Radar => return format!("{text}: целей {}", self.own.len()),
SamKind::Light => return format!("{text}{}", if self.target.is_some() { ": в луче цель" } else { "" }),
_ => {}
}
if self.empty && self.missiles == 0 {
text += " ЖДЁТ БК";
}
if self.health < 1.0 {
text += &format!(" ({:.0}%)", self.health * 100.0);
}
@@ -1129,6 +1116,13 @@ pub struct Sim {
rec: Option<record::Recording>,
pub replays: Vec<record::Recording>,
recordings: u32,
/// What the enemy has learned.
intel: ew::Intel,
/// Night: guns need light (the mod sets it from the game's clock).
pub night: bool,
/// Illumination flares in the sky, and the drones lit this tick.
pub flares: Vec<lights::Flare>,
lit: Vec<u32>,
}
/// Ids: objects get multiples of 16, their parts the low bits.
@@ -1162,6 +1156,10 @@ impl Sim {
rec: None,
replays: Vec::new(),
recordings: 0,
intel: ew::Intel::default(),
night: false,
flares: Vec::new(),
lit: Vec::new(),
}
}
pub fn id(&mut self) -> u32 {
@@ -1172,6 +1170,12 @@ impl Sim {
/// What a system's camera follows: its guns' or missiles' target, else what its missiles in
/// flight are after.
pub fn watched(&self, sam: &Sam) -> Option<&Drone> {
// A kill stays in view a few seconds: the wreck falling, before the next target.
if let Some((id, _, until)) = sam.kill_view
&& self.tick < until
{
return self.drones.iter().find(|d| d.id == id);
}
let id = sam
.gun_target
.or(sam.target)
@@ -1179,6 +1183,22 @@ impl Sim {
self.drones.iter().find(|d| d.id == id)
}
/// Where a system's camera looks: what it watches, or where its last kill was while that
/// stays in view (the wreck may be gone).
pub fn focus(&self, sam: &Sam) -> Option<DVec3> {
if let Some((_, at, until)) = sam.kill_view
&& self.tick < until
{
return Some(self.watched(sam).map_or(at, |d| d.pos));
}
self.watched(sam).map(|d| d.pos)
}
/// Whether a system's camera is showing its kill.
pub fn showing_kill(&self, sam: &Sam) -> bool {
sam.kill_view.is_some_and(|(_, _, until)| self.tick < until)
}
pub fn nearest_target(&self, to: DVec3) -> Option<DVec3> {
self.targets.iter().copied().min_by(|a, b| a.distance_squared(to).total_cmp(&b.distance_squared(to)))
}
@@ -1208,6 +1228,8 @@ impl Sim {
engaged: 0,
route: Vec::new(),
low: false,
jammed: false,
sector: 0,
});
self.launched += 1;
}
@@ -1253,10 +1275,15 @@ impl Sim {
pub fn step(&mut self, ground: &mut Ground, player: Option<DVec3>, out: &mut Out) {
self.tick += 1;
self.plan_step(out);
self.adapt();
self.release_queue();
self.step_drones(ground, player, out);
self.step_later(ground, player, out);
self.step_moves(ground, out);
self.step_supply(out);
self.step_ew(out);
self.update_sight(ground);
self.step_lights(out);
self.step_sams(out);
self.step_missiles(out);
self.step_fx(ground, out);
@@ -1384,6 +1411,10 @@ impl Sim {
if d.flight == Flight::Cruise && d.route.len() <= 1 && horizontal(d.target - d.pos).length() < spec.dive {
d.flight = Flight::Dive;
}
// An interceptor coming: drones drop to the treetops (radars lose them sooner).
if d.engaged > 0 && matches!(spec.class, Class::Drone | Class::Jet) {
d.low = true;
}
// A planned route: the next point, then the next.
// Reached when near (as near as it can turn), or passed: behind it and close.
let radius = (spec.speed / spec.turn.max(0.01)).max(24.0);
@@ -1453,6 +1484,9 @@ impl Sim {
self.log(pos, record::EventKind::Split { drone });
}
for (drone, kind, pos, tnt) in impacts {
if let Some(sector) = self.drones.iter().find(|x| x.id == drone).map(|x| x.sector as usize) {
self.learn(sector, false);
}
let power = match tnt {
0 => 0,
1 | 2 => 1,
@@ -1483,6 +1517,8 @@ impl Sim {
engaged: 0,
route: Vec::new(),
low: false,
jammed: false,
sector: 0,
});
}
for d in &self.drones {
@@ -1597,6 +1633,12 @@ impl Sim {
if !self.drones[idx].alive() {
return;
}
let sector = self.drones[idx].sector as usize;
self.learn(sector, true);
let until = self.tick + 70;
if let Some(s) = self.sams.iter_mut().find(|s| s.id == by) {
s.kill_view = Some((self.drones[idx].id, self.drones[idx].pos, until));
}
if let Some((i, s)) = self.sams.iter().enumerate().find(|(_, s)| s.id == by) {
let (drone, kind, pos) = (self.drones[idx].id, self.drones[idx].kind, self.drones[idx].pos);
let kill = record::EventKind::Kill { drone, kind, sam: by, sam_kind: s.kind, sam_index: i, missile };
@@ -1630,13 +1672,14 @@ impl Sim {
fn step_sams(&mut self, out: &mut Out) {
if self.hold {
for i in 0..self.sams.len() {
if self.sams[i].alive() {
if self.sams[i].alive() && !self.sams[i].moving() {
self.sams[i].state = SamState::Hold;
}
self.draw_sam(i, out);
}
return;
}
let logistics = self.logistics();
let mut launches: Vec<(MissileKind, DVec3, DVec3, u32, u32)> = Vec::new();
let mut gun_hits: Vec<(u32, u32)> = Vec::new();
let mut tracers: Vec<(u32, DVec3, DVec3, u32)> = Vec::new();
@@ -1666,10 +1709,21 @@ impl Sim {
}
}
}
if self.sams[i].moving() {
continue;
}
if !self.sams[i].kind.armed() {
let sam = &mut self.sams[i];
sam.state = SamState::Search;
sam.target = None;
sam.gun_target = None;
continue;
}
let pos = self.sams[i].pos;
let me = self.sams[i].id;
let roles = self.sams[i].roles;
let seen = self.sams[i].seen.clone();
let own = self.sams[i].own.clone();
let allowed = |d: &Drone| d.alive() && roles & d.kind.spec().class.bit() != 0 && seen.contains(&d.id);
// Interceptors a target has or will have: in flight, and other systems' launchers on it.
let claimed = |d: &Drone| {
@@ -1699,7 +1753,7 @@ impl Sim {
self.sams[i].gun_target = self
.drones
.iter()
.filter(|d| allowed(d) && d.pos.distance(pos) < gun.track)
.filter(|d| allowed(d) && own.contains(&d.id) && d.pos.distance(pos) < gun.track)
.map(|d| {
let busy = 1.0 + 2.0 * guns_on(d) + if d.engaged > 0 { 1.5 } else { 0.0 };
(d.pos.distance(pos) * busy, d.id)
@@ -1713,10 +1767,16 @@ impl Sim {
sam.launched = sam.launched.saturating_sub(1);
let Some(aim) = aim else {
// Out of missiles and nothing to shoot: reload now, guns or not.
if spec.missile.is_some() && sam.missiles == 0 && sam.reloading == 0 {
sam.reloading = spec.reload;
if spec.missile.is_some() && sam.missiles == 0 && sam.reloading == 0 && !sam.empty {
if logistics { sam.empty = true } else { sam.reloading = spec.reload }
}
sam.state = if sam.reloading > 0 { SamState::Reload } else { SamState::Search };
sam.state = if sam.reloading > 0 {
SamState::Reload
} else if sam.empty {
SamState::Waiting
} else {
SamState::Search
};
sam.yaw = slew(sam.yaw, sam.heading, 0.03);
sam.pitch += (spec.rest_pitch - sam.pitch) * 0.05;
continue;
@@ -1729,9 +1789,12 @@ impl Sim {
let want_pitch = (to.y / to.length().max(0.01)).asin().clamp(-0.1, 1.45);
let (yaw_rate, pitch_rate) = spec.gun.as_ref().map_or((0.05, 0.05), |g| g.slew);
let (yaw_rate, pitch_rate) = (yaw_rate * fit, pitch_rate * fit);
match sam.kind {
SamKind::S400 | SamKind::S500 => {} // vertical launch: no aiming
SamKind::Patriot => sam.yaw = slew(sam.yaw, want_yaw, 0.12 * fit),
let cells = matches!(spec.muzzle, Muzzle::Cells);
match spec.family {
// Vertical launch: no aiming (the Tor's turret turns its radar to the target).
Family::Vertical if cells => sam.yaw = slew(sam.yaw, want_yaw, 0.15 * fit),
Family::Vertical => {}
Family::Trainable => sam.yaw = slew(sam.yaw, want_yaw, 0.12 * fit),
_ => {
sam.yaw = slew(sam.yaw, want_yaw, yaw_rate);
sam.pitch = slew(sam.pitch, want_pitch, pitch_rate);
@@ -1739,9 +1802,11 @@ impl Sim {
}
let aim_error = angle_diff(sam.yaw, want_yaw).abs()
+ if spec.gun.is_some() { (want_pitch - sam.pitch).abs() } else { 0.0 };
let vertical = matches!(sam.kind, SamKind::S400 | SamKind::S500);
let vertical = spec.family == Family::Vertical;
sam.state = if sam.reloading > 0 {
SamState::Reload
} else if sam.empty && sam.missiles == 0 && sam.bursts == 0 {
SamState::Waiting
} else if sam.launched > 0 {
SamState::Launch
} else if sam.flash > 0 {
@@ -1763,7 +1828,7 @@ impl Sim {
sam.flash = 2;
sam.recoil = 1.0;
if sam.bursts == 0 && sam.missiles == 0 {
sam.reloading = spec.reload;
if logistics { sam.empty = true } else { sam.reloading = spec.reload }
}
let (_, rot) = sam.mount();
let s = spec.scale;
@@ -1787,7 +1852,9 @@ impl Sim {
let event = if sam.kind == SamKind::Pantsir { "airdef:gun.pantsir" } else { "airdef:gun.gepard" };
out.sound(event, mount, 1.0, 0.95 + self.rng.f() as f32 * 0.1);
}
let p = gun.p * fit / t.kind.spec().stealth * (1.0 - dist / (gun.range * 1.3)).max(0.35);
// At night a gun sees what is lit (a searchlight, a flare) and little else.
let dark = if self.night && !self.lit.contains(&t.id) { lights::DARK_HITS } else { 1.0 };
let p = gun.p * fit * dark / t.kind.spec().stealth * (1.0 - dist / (gun.range * 1.3)).max(0.35);
if self.rng.chance(p) {
gun_hits.push((gt, sam.id));
} else {
@@ -1799,13 +1866,13 @@ impl Sim {
// Missiles.
if let (Some(load), Some(mt)) = (&spec.missile, sam.target) {
let ready = sam.cooldown == 0 && sam.reloading == 0 && sam.missiles > 0;
let aligned = match sam.kind {
SamKind::Patriot => {
let aligned = match spec.family {
Family::Trainable => {
let t = self.drones.iter().find(|d| d.id == mt);
// The missile turns after its boost: the launcher needn't be exact.
t.is_some_and(|t| angle_diff(sam.yaw, heading_of(t.pos - mount)).abs() < 0.45)
}
SamKind::Pantsir => aim.id == mt && aim_error < 0.2,
Family::Turret => aim.id == mt && aim_error < 0.2,
_ => true,
};
if ready && aligned {
@@ -1819,12 +1886,16 @@ impl Sim {
sam.fired += 1;
sam.cooldown = load.interval;
if sam.missiles == 0 && (spec.gun.is_none() || sam.bursts == 0) {
sam.reloading = spec.reload;
if logistics { sam.empty = true } else { sam.reloading = spec.reload }
}
}
}
}
for (kind, muzzle, dir, target, sam) in launches {
// A launch gives the system away.
if let Some(s) = self.sams.iter().find(|s| s.id == sam) {
self.intel.revealed.push((sam, s.pos, self.tick));
}
let spec = kind.spec();
let id = self.id();
let v0 = if spec.cold > 0 { 0.7 } else { 0.8 };
@@ -1858,69 +1929,6 @@ impl Sim {
}
}
fn draw_sam(&mut self, i: usize, out: &mut Out) {
if !self.sams[i].alive() {
self.draw_wreck(i, out);
return;
}
let sam = &self.sams[i];
let base = sam.base();
let s = sam.scale();
let up = DVec3::Y * 0.5 * s;
let id = sam.id;
let (mount, rot) = sam.mount();
let turret = DQuat::from_rotation_y(sam.yaw);
match sam.kind {
SamKind::Patriot => {
out.model(id, "airdef:patriot_trailer", sam.pos + up, base, s, false);
out.model(id + 1, "airdef:patriot_truck", sam.pos + up + base * DVec3::new(0.0, 0.0, -3.0 * s), base, s, false);
// The launcher's pivot is its back bottom edge (0, -0.5, 1.375).
out.model(id + 2, "airdef:patriot_launcher", mount - rot * DVec3::new(0.0, -0.5, 1.375) * s, rot, s, false);
// Its radar stands right behind the trailer, facing the same way.
out.model(id + 3, "airdef:patriot_radar", sam.pos + up + base * DVec3::new(0.0, 0.0, 3.6 * s), base, s, false);
}
SamKind::Gepard => {
out.model(id, "airdef:gepard_hull", sam.pos + up, base, s, false);
out.model(id + 1, "airdef:gepard_turret", mount + DVec3::Y * 0.12 * s, turret, s, false);
let recoil = rot * DVec3::new(0.0, 0.0, 0.15 * sam.recoil * s);
out.model(id + 2, "airdef:gepard_guns", mount + recoil, rot, s, false);
out.model(id + 3, "airdef:gepard_dish", mount + turret * DVec3::new(0.0, 0.95 * s, 0.6 * s), DQuat::from_rotation_y(sam.dish), s, false);
}
SamKind::Pantsir => {
out.model(id, "airdef:pantsir_truck", sam.pos + up, base, s, false);
out.model(id + 1, "airdef:pantsir_turret", mount - DVec3::Y * 0.2 * s, turret, s, false);
let recoil = rot * DVec3::new(0.0, 0.0, 0.1 * sam.recoil * s);
out.model(id + 2, "airdef:pantsir_pack", mount + DVec3::Y * 0.1 * s + recoil, rot, s, false);
out.model(id + 3, "airdef:pantsir_dish", mount + turret * DVec3::new(0.0, 0.85 * s, 0.3 * s), DQuat::from_rotation_y(sam.dish) * DQuat::from_rotation_x(0.3), s, false);
}
SamKind::S400 | SamKind::S500 => {
let (chassis, tubes) = if sam.kind == SamKind::S400 {
("airdef:s400_chassis", "airdef:s400_tubes")
} else {
("airdef:s500_chassis", "airdef:s500_tubes")
};
out.model(id, chassis, sam.pos + up, base, s, false);
// The pack's pivot is its back bottom edge (0, -0.5, 1.5).
out.model(id + 1, tubes, mount - rot * DVec3::new(0.0, -0.5, 1.5) * s, rot, s, false);
// The battery's search radar behind it, turning.
let radar = sam.pos + DVec3::Y * 0.5 * s + base * DVec3::new(0.0, 0.0, 4.2 * s);
out.model(id + 2, "airdef:radar_trailer", radar, base, s, false);
out.model(id + 3, "airdef:radar_panel", radar + DVec3::Y * 0.9 * s, DQuat::from_rotation_y(sam.dish * 0.6) * DQuat::from_rotation_x(-0.3), s * 0.9, false);
}
}
if sam.flash > 0 {
let barrels: &[DVec3] = match sam.kind {
SamKind::Pantsir => &[DVec3::new(-0.95, 0.33, -1.7), DVec3::new(0.95, 0.33, -1.7)],
SamKind::Gepard => &[DVec3::new(-0.92, -0.11, -1.7), DVec3::new(0.92, -0.11, -1.7)],
_ => &[],
};
for (k, b) in barrels.iter().enumerate() {
let r = rot * DQuat::from_rotation_z(self.rng.f() * TAU);
out.model(id + 4 + k as u32, "airdef:flash", mount + rot * *b * s, r, 0.9, true);
}
}
}
// ------------------------------------------------ interceptors
fn step_missiles(&mut self, out: &mut Out) {
@@ -1971,13 +1979,21 @@ impl Sim {
done.push((m.id, None, false, m.sam));
continue;
}
if lit {
if lit && spec.flame == 0.0 {
if m.age % 20 == 1 {
out.sound("airdef:drone.lancet", m.pos, 0.8, 1.6);
}
} else if lit {
if (m.age - spec.cold) % 46 == 1 {
let pitch = match m.kind {
MissileKind::Pac3 => 1.0,
MissileKind::M57e6 => 1.12,
MissileKind::M48n6 => 0.88,
MissileKind::M77n6 => 0.8,
MissileKind::IrisT | MissileKind::Amraam => 1.05,
MissileKind::M9m317 => 0.9,
MissileKind::M9m338 => 1.15,
MissileKind::P1sun => 1.7,
};
out.sound("airdef:missile.flight", m.pos, 1.0, pitch);
}
@@ -2017,7 +2033,7 @@ impl Sim {
let spec = m.kind.spec();
let rot = look(m.vel, 0.0) * DQuat::from_rotation_z(m.age as f64 * 0.3);
out.model(m.id, spec.model, m.pos, rot, spec.scale, false);
if m.age > spec.cold {
if m.age > spec.cold && spec.flame > 0.0 {
let back = (spec.rear - 8.0) / 16.0 * spec.scale + spec.flame;
let size = spec.flame * if m.age < 40 { 1.2 } else { 0.8 } * (0.85 + 0.15 * self.rng.f());
out.model(m.id + 1, "airdef:flame", m.pos + rot * DVec3::new(0.0, 0.0, back), rot, size, true);
+113
View File
@@ -0,0 +1,113 @@
//! Move orders: a unit packs up (its launcher down, radar silent), drives
//! over the land to the point it was given, and deploys there; it fights
//! again only once deployed. The reload trucks drive the same way.
use super::*;
/// Blocks a tick a vehicle drives (about 25 km/h).
const SPEED: f64 = 0.35;
impl SamKind {
/// Ticks to pack up for the march, and to deploy again.
pub fn march_ticks(self) -> u32 {
match self.spec().family {
Family::Vertical => 200,
Family::Trainable => 120,
Family::Static => 160,
Family::Turret | Family::Gun => 40,
Family::Light => 40,
Family::Truck => 10,
}
}
/// A depot doesn't drive.
pub fn mobile(self) -> bool {
self != SamKind::Depot
}
}
impl Sam {
/// Packing up, driving or deploying: not fighting.
pub fn moving(&self) -> bool {
matches!(self.state, SamState::Packing | SamState::Moving | SamState::Deploying)
}
}
impl Sim {
/// Orders unit `i` to drive to `to`. False for one that can't (a depot, a wreck).
pub fn order_move(&mut self, i: usize, to: DVec3) -> bool {
let Some(sam) = self.sams.get_mut(i) else { return false };
if !sam.alive() || !sam.kind.mobile() {
return false;
}
sam.dest = Some(to);
sam.target = None;
sam.gun_target = None;
if sam.state != SamState::Moving {
sam.state = SamState::Packing;
sam.march = sam.kind.march_ticks();
}
true
}
/// Packing, driving and deploying, once a tick.
pub(super) fn step_moves(&mut self, ground: &mut Ground, out: &mut Out) {
let now = self.tick;
for sam in &mut self.sams {
if !sam.alive() {
continue;
}
let spec = sam.kind.spec();
match sam.state {
SamState::Packing => {
// The launcher comes down for the march.
sam.pitch += (0.0 - sam.pitch) * 0.06;
sam.yaw = slew(sam.yaw, sam.heading, 0.04);
sam.march = sam.march.saturating_sub(1);
if sam.march == 0 {
sam.state = SamState::Moving;
}
}
SamState::Moving => {
let Some(to) = sam.dest else {
sam.state = SamState::Deploying;
sam.march = sam.kind.march_ticks();
continue;
};
let flat = horizontal(to - sam.pos);
if flat.length() < SPEED * 2.0 {
sam.pos = DVec3::new(to.x, sam.pos.y, to.z);
sam.dest = None;
sam.state = SamState::Deploying;
sam.march = sam.kind.march_ticks();
out.sound("minecraft:block.piston.contract", sam.pos, 2.0, 0.6);
continue;
}
// Turn towards it, then drive: slower while turning, over the land's height.
sam.heading = slew(sam.heading, heading_of(flat), 0.06);
let ahead = DQuat::from_rotation_y(sam.heading) * DVec3::NEG_Z;
let turning = angle_diff(sam.heading, heading_of(flat)).abs();
let step = SPEED * (1.0 - turning / std::f64::consts::PI).max(0.2);
let next = sam.pos + ahead * step;
let y = ground.at(next, now).unwrap_or(sam.pos.y);
sam.pos = DVec3::new(next.x, sam.pos.y + (y - sam.pos.y).clamp(-0.5, 0.5), next.z);
sam.yaw = sam.heading;
if now % 4 == 0 {
out.particles("minecraft:campfire_cosy_smoke", sam.pos - ahead * 2.0 * spec.scale, 0.3, DVec3::new(0.0, 0.03, 0.0), 0.01, 1);
}
if now % 30 == 0 {
out.sound("minecraft:entity.minecart.riding", sam.pos, 0.6, 0.5);
}
}
SamState::Deploying => {
sam.pitch += (spec.rest_pitch - sam.pitch) * 0.05;
sam.march = sam.march.saturating_sub(1);
if sam.march == 0 {
sam.state = SamState::Search;
sam.pitch = spec.rest_pitch;
sam.yaw = sam.heading;
}
}
_ => {}
}
}
}
}
+36 -7
View File
@@ -199,9 +199,10 @@ impl Sim {
if total == 0 {
return 0;
}
let bearing = self.rng.f() * TAU;
let second = bearing + std::f64::consts::PI * (0.6 + self.rng.f() * 0.6);
let smart = order.aim == Aim::Smart;
// A smart enemy comes from where its raids got through best.
let bearing = if smart { self.best_bearing() } else { self.rng.f() * TAU };
let second = bearing + std::f64::consts::PI * (0.6 + self.rng.f() * 0.6);
// One grid over the whole raid; each kind's prices are worked out once, routes are shared
// by the drones of a kind going from the same bearing to the same target.
let span = order.dist + 450.0;
@@ -222,7 +223,8 @@ impl Sim {
let dist = spec.ballistic.map_or(order.dist, |far| far.max(order.dist)) + self.rng.f() * 50.0;
let height = if spec.ballistic.is_some() { 10.0 } else if spec.cruise > 0.0 { spec.cruise } else { 25.0 };
let from = centre + DVec3::new(a.sin() * dist, height + self.rng.s() * 6.0, a.cos() * dist);
let d = self.make_drone(kind, from, horizontal(aim - from), aim);
let mut d = self.make_drone(kind, from, horizontal(aim - from), aim);
d.sector = ew::sector(a) as u8;
let routed = smart && spec.ballistic.is_none() && !spec.decoy && !at_sam.is_some_and(|_| kind == DroneKind::Lancet);
let key = routed.then(|| (kind, (a.rem_euclid(TAU) / 0.35) as i32, (target.x / 24.0) as i32, (target.z / 24.0) as i32));
// Decoys come 15 s ahead, the rest together; "waves" one kind after another.
@@ -344,6 +346,8 @@ impl Sim {
engaged: 0,
route: Vec::new(),
low: false,
jammed: false,
sector: 0,
}
}
@@ -365,9 +369,19 @@ impl Sim {
if aim == Aim::ObjectsAndSams {
return if roll < 0.33 { pick(&alive, r).map_or((object, None), |(p, id)| (p, Some(id))) } else { (object, None) };
}
// Smart: each kind at what it is good against.
let guns: Vec<&Sam> = alive.iter().copied().filter(|s| matches!(s.kind, SamKind::Gepard | SamKind::Pantsir)).collect();
let long: Vec<&Sam> = alive.iter().copied().filter(|s| matches!(s.kind, SamKind::S400 | SamKind::S500 | SamKind::Patriot)).collect();
// Smart: each kind at what it is good against; what gave itself away first (a jamming
// station, a radar, a system that fired).
let known = self.known_units();
let known_ew: Vec<&Sam> = alive.iter().copied().filter(|s| s.kind == SamKind::Ew && known.iter().any(|k| k.0 == s.id)).collect();
if matches!(kind, DroneKind::Lancet | DroneKind::Kh22) && !known_ew.is_empty() && roll < 0.6 {
let s = known_ew[(r * known_ew.len() as f64) as usize % known_ew.len()];
return (s.pos, Some(s.id));
}
// Short-range systems (guns, Tor, P1-SUN) for the Lancets; long-range ones and the radars
// that feed them for the fast missiles.
let guns: Vec<&Sam> = alive.iter().copied().filter(|s| s.kind.armed() && s.kind.range() <= 200.0).collect();
let long: Vec<&Sam> =
alive.iter().copied().filter(|s| s.kind.range() >= 300.0 || s.kind == SamKind::Radar).collect();
let choice = match kind {
DroneKind::Lancet if roll < 0.75 => pick(&guns, r).or_else(|| pick(&alive, r)),
DroneKind::Kh22 | DroneKind::Zircon if roll < 0.45 => pick(&long, r),
@@ -386,6 +400,21 @@ impl Sim {
let height = spec.clearance.max(6.0);
let n = grid.size;
let mut prices = vec![1.0f32; (n * n) as usize];
// Known jamming stations: drones that navigate by satellite keep away.
if kind.jammable() {
let known = self.known_units();
for s in self.sams.iter().filter(|s| s.kind == SamKind::Ew && s.alive() && known.iter().any(|k| k.0 == s.id)) {
let (ci, cj) = grid.index(s.pos);
let r = (EW_RADIUS / grid.cell).ceil() as i32;
for i in (ci - r).max(0)..=(ci + r).min(n - 1) {
for j in (cj - r).max(0)..=(cj + r).min(n - 1) {
if horizontal(grid.centre((i, j), s.pos.y) - s.pos).length() < EW_RADIUS {
prices[(i * n + j) as usize] += 12.0;
}
}
}
}
}
for s in self.sams.iter().filter(|s| s.alive() && s.roles & spec.class.bit() != 0) {
let sp = s.kind.spec();
let (lo, hi) = sp.missile.as_ref().map_or((0.0, 0.0), |m| (m.min, m.max));
@@ -518,7 +547,7 @@ mod big {
fn sixty_one_systems_and_a_big_smart_raid() {
let mut sim = Sim::new(11);
for i in 0..61 {
let kind = SamKind::ALL[i % 5];
let kind = SamKind::ARMED[i % 5];
let id = sim.id();
let p = DVec3::new(((i % 8) as f64 - 4.0) * 40.0, 70.0, ((i / 8) as f64 - 4.0) * 40.0);
sim.sams.push(Sam::new(id, kind, p, 0.0));
+1 -1
View File
@@ -237,7 +237,7 @@ impl Sim {
let spec = kind.spec();
let rot = look(dir, 0.0) * DQuat::from_rotation_z(t * 0.3);
out.model(id, spec.model, pos, rot, spec.scale, false);
if t - tr.first as f64 > spec.cold as f64 {
if t - tr.first as f64 > spec.cold as f64 && spec.flame > 0.0 {
let back = (spec.rear - 8.0) / 16.0 * spec.scale + spec.flame;
out.model(id + 1, "airdef:flame", pos + rot * DVec3::new(0.0, 0.0, back), rot, spec.flame, true);
}
+34 -12
View File
@@ -8,8 +8,8 @@ use super::*;
/// Rays round a radar, and blocks between the land's samples along them.
const RAYS: usize = 48;
const STEP: f64 = 16.0;
/// The antenna's height over the vehicle's ground.
const MAST: f64 = 5.0;
/// How far units share their tracks (the network's data links).
pub(super) const LINK: f64 = 1500.0;
/// Radio horizon: `HORIZON * (√mast + √height)` blocks.
const HORIZON: f64 = 35.0;
/// Anything this near is seen (and heard), whatever hides it.
@@ -58,16 +58,20 @@ impl Horizon {
}
}
/// Where a system's radar is.
/// Where a unit's radar antenna is.
pub(super) fn antenna(sam: &Sam) -> DVec3 {
sam.pos + DVec3::Y * MAST
sam.pos + DVec3::Y * sam.kind.spec().mast
}
/// How far a system's search radar reaches for this kind at `height` over the land:
/// its own reach (worse when damaged) for the target's size, under the radio horizon.
pub(super) fn reach(sam: &Sam, kind: DroneKind, height: f64) -> f64 {
let radar = sam.kind.range() * 1.3 * sam.fitness() * kind.rcs().powf(0.25);
let horizon = HORIZON * (MAST.sqrt() + height.max(0.0).sqrt());
let spec = sam.kind.spec();
if spec.sensor <= 0.0 {
return 0.0;
}
let radar = spec.sensor * sam.fitness() * kind.rcs().powf(0.25);
let horizon = HORIZON * (spec.mast.sqrt() + height.max(0.0).sqrt());
radar.min(horizon).max(NEAR)
}
@@ -77,11 +81,12 @@ impl Sim {
let now = self.tick;
// One horizon every few ticks: the land answers come in over time.
if !self.sams.is_empty() && now % 3 == 0 {
let alive: Vec<usize> = (0..self.sams.len()).filter(|&i| self.sams[i].alive()).collect();
if !alive.is_empty() {
let i = alive[(now / 3) as usize % alive.len()];
let radars: Vec<usize> =
(0..self.sams.len()).filter(|&i| self.sams[i].alive() && self.sams[i].kind.spec().sensor > 0.0).collect();
if !radars.is_empty() {
let i = radars[(now / 3) as usize % radars.len()];
let sam = &self.sams[i];
let h = Horizon::build(antenna(sam), sam.kind.range() * 1.3, ground, now);
let h = Horizon::build(antenna(sam), sam.kind.spec().sensor, ground, now);
self.horizons.insert(sam.id, h);
}
}
@@ -91,7 +96,8 @@ impl Sim {
self.drones.iter().map(|d| ground.at(d.pos, now).map(|g| d.pos.y - g)).collect();
for sam in &mut self.sams {
sam.seen.clear();
if !sam.alive() {
sam.own.clear();
if !sam.alive() || sam.kind.spec().sensor <= 0.0 || sam.moving() {
continue;
}
let at = antenna(sam);
@@ -108,9 +114,25 @@ impl Sim {
if dist > NEAR && horizon.is_some_and(|hz| hz.hides(at, d.pos)) {
continue;
}
sam.seen.push(d.id);
sam.own.push(d.id);
}
}
// The network: every unit gets the tracks of the radars within data-link range.
let tracks: Vec<(DVec3, Vec<u32>)> =
self.sams.iter().filter(|s| !s.own.is_empty()).map(|s| (s.pos, s.own.clone())).collect();
for sam in &mut self.sams {
if !sam.alive() || sam.moving() || !sam.kind.armed() {
sam.seen = sam.own.clone();
continue;
}
let mut seen = sam.own.clone();
for (at, ids) in &tracks {
if at.distance(sam.pos) <= LINK {
seen.extend(ids.iter().copied().filter(|id| !seen.contains(id)).collect::<Vec<_>>());
}
}
sam.seen = seen;
}
}
/// Whether any system's radar holds this drone (the player's radar shows only those).
+690
View File
@@ -0,0 +1,690 @@
//! Every unit the player deploys, as data: the air-defence systems (their
//! weapons, radar and how their vehicles are put together and drawn) and the
//! units that serve them — long-range radars, ammunition depots, reload
//! trucks, EW stations and searchlights. They share a position, health,
//! wrecks, saving and move orders; only what they carry differs.
use super::*;
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum SamKind {
Patriot,
Gepard,
Pantsir,
S400,
S500,
IrisT,
Nasams,
Buk,
Tor,
P1sun,
/// A long-range search radar: no weapons, it feeds the network.
Radar,
/// An ammunition depot: the reload trucks fill up there.
Depot,
/// A transport-loader: drives missiles from a depot to the systems.
Tzm,
/// An electronic-warfare station: jams the drones' navigation while switched on.
Ew,
/// A searchlight: lights up targets at night for the guns.
Light,
}
/// How a vehicle is put together: how its launcher or turret moves and is drawn.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Family {
/// A launcher on a trailer that trains on the target (Patriot, NASAMS, P1-SUN).
Trainable,
/// A gun turret with a radar dish (Gepard).
Gun,
/// A turret with an elevating pack (Pantsir, Buk).
Turret,
/// Vertical launch from a raised pack (S-400, S-500, IRIS-T) or a turning turret (Tor).
Vertical,
/// Nothing that aims: a radar, a depot, an EW station.
Static,
/// A truck (the transport-loader).
Truck,
/// A searchlight.
Light,
}
/// Where missile `n` leaves the launcher, as the launcher's model lays its tubes out.
#[derive(Clone, Copy)]
pub enum Muzzle {
None,
Patriot,
Pantsir,
/// `cols × rows` tubes `size` model units wide, from the pack's pivot to its far end.
Grid(u32, u32, f64),
/// Missiles on rails, at these x (model units).
Rails(&'static [f64]),
/// The Tor's eight vertical cells on top of its turret.
Cells,
}
/// The models a unit is drawn with.
pub struct Parts {
/// The chassis, trailer or base.
pub body: &'static str,
/// The launcher, the tubes or the turret.
pub launcher: Option<&'static str>,
/// A turret's elevating pack or guns.
pub pack: Option<&'static str>,
/// A dish on the turret: model, height and forward offset (× scale), tilt.
pub dish: Option<(&'static str, f64, f64, f64)>,
/// Other fixed models: model, height, backward offset (× scale).
pub fixed: &'static [(&'static str, f64, f64)],
/// A turning antenna: model, height, backward offset, size, tilt.
pub spinner: Option<(&'static str, f64, f64, f64, f64)>,
}
pub(super) struct MissileLoad {
pub kind: MissileKind,
pub magazine: u32,
pub min: f64,
pub max: f64,
/// Ticks between launches.
pub interval: u32,
}
pub(super) struct GunSpec {
pub range: f64,
/// Starts tracking from here.
pub track: f64,
/// Chance per burst at point blank.
pub p: f64,
pub bursts: u32,
/// Shell speed for the lead, blocks a tick.
pub shell: f64,
pub slew: (f64, f64),
}
pub struct SamSpec {
pub name: &'static str,
pub key: &'static str,
pub(super) missile: Option<MissileLoad>,
pub(super) gun: Option<GunSpec>,
pub(super) reload: u32,
pub(super) scale: f64,
/// Where it points its launcher at rest (pitch, radians).
pub(super) rest_pitch: f64,
/// Which classes of target it engages unless told otherwise.
pub roles: u8,
pub family: Family,
pub parts: Parts,
/// The launcher's or turret's centre over the vehicle's centre: up and forward, × scale.
pub(super) mount: (f64, f64),
/// The launcher model's pivot from its centre (blocks).
pub(super) pivot: DVec3,
pub(super) muzzle: Muzzle,
/// Its own search radar's reach (0: none, it fires on the network's tracks), and the antenna's height.
pub sensor: f64,
pub mast: f64,
}
const NO_PARTS: Parts = Parts { body: "", launcher: None, pack: None, dish: None, fixed: &[], spinner: None };
/// The battery radar trailer behind a vertical launcher.
const BATTERY_RADAR: &[(&str, f64, f64)] = &[("airdef:radar_trailer", 0.0, 4.2)];
const BATTERY_SPINNER: Option<(&str, f64, f64, f64, f64)> = Some(("airdef:radar_panel", 0.9, 4.2, 0.9, -0.3));
const TUBE_PIVOT: DVec3 = DVec3::new(0.0, -0.5, 1.5);
impl SamKind {
pub const ALL: [SamKind; 15] = [
SamKind::Patriot,
SamKind::Gepard,
SamKind::Pantsir,
SamKind::S400,
SamKind::S500,
SamKind::IrisT,
SamKind::Nasams,
SamKind::Buk,
SamKind::Tor,
SamKind::P1sun,
SamKind::Radar,
SamKind::Depot,
SamKind::Tzm,
SamKind::Ew,
SamKind::Light,
];
/// The ones that shoot (the roles menu lists these).
pub const ARMED: [SamKind; 10] = [
SamKind::Patriot,
SamKind::Gepard,
SamKind::Pantsir,
SamKind::S400,
SamKind::S500,
SamKind::IrisT,
SamKind::Nasams,
SamKind::Buk,
SamKind::Tor,
SamKind::P1sun,
];
pub fn spec(self) -> &'static SamSpec {
match self {
SamKind::Patriot => {
const S: SamSpec = SamSpec {
name: "Patriot",
key: "patriot",
missile: Some(MissileLoad { kind: MissileKind::Pac3, magazine: 4, min: 10.0, max: 300.0, interval: 16 }),
gun: None,
reload: 220,
scale: 1.3,
rest_pitch: 0.66,
roles: 30,
family: Family::Trainable,
parts: Parts {
body: "airdef:patriot_trailer",
launcher: Some("airdef:patriot_launcher"),
fixed: &[("airdef:patriot_truck", 0.0, -3.0), ("airdef:patriot_radar", 0.0, 3.6)],
..NO_PARTS
},
mount: (0.69, 1.2),
pivot: DVec3::new(0.0, -0.5, 1.375),
muzzle: Muzzle::Patriot,
sensor: 390.0,
mast: 5.0,
};
&S
}
SamKind::Gepard => {
const S: SamSpec = SamSpec {
name: "Гепард",
key: "gepard",
missile: None,
gun: Some(GunSpec { range: 90.0, track: 130.0, p: 0.3, bursts: 160, shell: 10.0, slew: (0.22, 0.16) }),
reload: 160,
scale: 1.25,
rest_pitch: 0.0,
roles: 7,
family: Family::Gun,
parts: Parts {
body: "airdef:gepard_hull",
launcher: Some("airdef:gepard_turret"),
pack: Some("airdef:gepard_guns"),
dish: Some(("airdef:gepard_dish", 0.95, 0.6, 0.0)),
..NO_PARTS
},
mount: (1.55, 0.25),
pivot: DVec3::ZERO,
muzzle: Muzzle::None,
sensor: 170.0,
mast: 3.0,
};
&S
}
SamKind::Pantsir => {
const S: SamSpec = SamSpec {
name: "Панцирь-С1",
key: "pantsir",
missile: Some(MissileLoad { kind: MissileKind::M57e6, magazine: 12, min: 25.0, max: 150.0, interval: 14 }),
gun: Some(GunSpec { range: 60.0, track: 90.0, p: 0.25, bursts: 140, shell: 9.0, slew: (0.25, 0.18) }),
reload: 280,
scale: 1.3,
rest_pitch: 0.0,
roles: 15,
family: Family::Turret,
parts: Parts {
body: "airdef:pantsir_truck",
launcher: Some("airdef:pantsir_turret"),
pack: Some("airdef:pantsir_pack"),
dish: Some(("airdef:pantsir_dish", 0.85, 0.3, 0.3)),
..NO_PARTS
},
mount: (1.35, 0.69),
pivot: DVec3::ZERO,
muzzle: Muzzle::Pantsir,
sensor: 195.0,
mast: 4.0,
};
&S
}
SamKind::S400 => {
const S: SamSpec = SamSpec {
name: "С-400",
key: "s400",
missile: Some(MissileLoad { kind: MissileKind::M48n6, magazine: 4, min: 30.0, max: 480.0, interval: 20 }),
gun: None,
reload: 420,
scale: 1.5,
rest_pitch: HALF_PI - 0.02,
roles: 30,
family: Family::Vertical,
parts: Parts {
body: "airdef:s400_chassis",
launcher: Some("airdef:s400_tubes"),
fixed: BATTERY_RADAR,
spinner: BATTERY_SPINNER,
..NO_PARTS
},
mount: (0.7, 1.45),
pivot: TUBE_PIVOT,
muzzle: Muzzle::Grid(2, 2, 7.0),
sensor: 624.0,
mast: 5.0,
};
&S
}
SamKind::S500 => {
const S: SamSpec = SamSpec {
name: "С-500",
key: "s500",
missile: Some(MissileLoad { kind: MissileKind::M77n6, magazine: 4, min: 40.0, max: 700.0, interval: 20 }),
gun: None,
reload: 520,
scale: 1.6,
rest_pitch: HALF_PI - 0.02,
roles: 24,
family: Family::Vertical,
parts: Parts {
body: "airdef:s500_chassis",
launcher: Some("airdef:s500_tubes"),
fixed: BATTERY_RADAR,
spinner: BATTERY_SPINNER,
..NO_PARTS
},
mount: (0.7, 1.45),
pivot: TUBE_PIVOT,
muzzle: Muzzle::Grid(2, 1, 9.5),
sensor: 910.0,
mast: 5.0,
};
&S
}
SamKind::IrisT => {
const S: SamSpec = SamSpec {
name: "IRIS-T SLM",
key: "irist",
missile: Some(MissileLoad { kind: MissileKind::IrisT, magazine: 8, min: 15.0, max: 400.0, interval: 10 }),
gun: None,
reload: 300,
scale: 1.4,
rest_pitch: HALF_PI - 0.02,
roles: 15,
family: Family::Vertical,
parts: Parts {
body: "airdef:irist_chassis",
launcher: Some("airdef:irist_tubes"),
fixed: BATTERY_RADAR,
spinner: BATTERY_SPINNER,
..NO_PARTS
},
mount: (0.7, 1.45),
pivot: TUBE_PIVOT,
muzzle: Muzzle::Grid(4, 2, 4.0),
sensor: 520.0,
mast: 5.0,
};
&S
}
SamKind::Nasams => {
const S: SamSpec = SamSpec {
name: "NASAMS",
key: "nasams",
missile: Some(MissileLoad { kind: MissileKind::Amraam, magazine: 6, min: 10.0, max: 300.0, interval: 14 }),
gun: None,
reload: 260,
scale: 1.3,
rest_pitch: 0.5,
roles: 14,
family: Family::Trainable,
parts: Parts {
body: "airdef:nasams_trailer",
launcher: Some("airdef:nasams_launcher"),
fixed: &[("airdef:radar_trailer", 0.0, 3.4)],
spinner: Some(("airdef:radar_panel", 0.9, 3.4, 0.6, -0.3)),
..NO_PARTS
},
mount: (0.62, 0.4),
pivot: TUBE_PIVOT,
muzzle: Muzzle::Grid(3, 2, 4.0),
sensor: 390.0,
mast: 5.0,
};
&S
}
SamKind::Buk => {
const S: SamSpec = SamSpec {
name: "Бук-М2",
key: "buk",
missile: Some(MissileLoad { kind: MissileKind::M9m317, magazine: 4, min: 20.0, max: 450.0, interval: 18 }),
gun: None,
reload: 400,
scale: 1.4,
rest_pitch: 0.0,
roles: 14,
family: Family::Turret,
parts: Parts {
body: "airdef:buk_chassis",
launcher: Some("airdef:buk_turret"),
pack: Some("airdef:buk_rails"),
..NO_PARTS
},
mount: (1.55, 0.2),
pivot: DVec3::ZERO,
muzzle: Muzzle::Rails(&[-9.0, -3.0, 13.0, 19.0]),
sensor: 585.0,
mast: 4.0,
};
&S
}
SamKind::Tor => {
const S: SamSpec = SamSpec {
name: "Тор-М2",
key: "tor",
missile: Some(MissileLoad { kind: MissileKind::M9m338, magazine: 8, min: 8.0, max: 160.0, interval: 8 }),
gun: None,
reload: 300,
scale: 1.3,
rest_pitch: 0.0,
roles: 15,
family: Family::Vertical,
parts: Parts { body: "airdef:tor_chassis", launcher: Some("airdef:tor_turret"), ..NO_PARTS },
mount: (1.5, 0.0),
pivot: DVec3::ZERO,
muzzle: Muzzle::Cells,
sensor: 210.0,
mast: 3.5,
};
&S
}
SamKind::P1sun => {
const S: SamSpec = SamSpec {
name: "P1-SUN",
key: "p1sun",
missile: Some(MissileLoad { kind: MissileKind::P1sun, magazine: 8, min: 5.0, max: 450.0, interval: 12 }),
gun: None,
reload: 240,
scale: 1.1,
rest_pitch: 0.35,
roles: 1,
family: Family::Trainable,
parts: Parts { body: "airdef:nasams_trailer", launcher: Some("airdef:p1sun_launcher"), ..NO_PARTS },
mount: (0.6, 0.3),
pivot: TUBE_PIVOT,
muzzle: Muzzle::Grid(4, 2, 3.0),
// No radar of its own: it fires on the network's tracks.
sensor: 0.0,
mast: 2.0,
};
&S
}
SamKind::Radar => {
const S: SamSpec = SamSpec {
name: "РЛС дальнего обнаружения",
key: "radar",
missile: None,
gun: None,
reload: 0,
scale: 1.5,
rest_pitch: 0.0,
roles: 0,
family: Family::Static,
parts: Parts {
body: "airdef:radar_truck",
spinner: Some(("airdef:radar_panel", 1.2, 1.2, 1.5, -0.25)),
..NO_PARTS
},
mount: (0.0, 0.0),
pivot: DVec3::ZERO,
muzzle: Muzzle::None,
sensor: 1700.0,
mast: 16.0,
};
&S
}
SamKind::Depot => {
const S: SamSpec = SamSpec {
name: "Склад боеприпасов",
key: "depot",
missile: None,
gun: None,
reload: 0,
scale: 1.5,
rest_pitch: 0.0,
roles: 0,
family: Family::Static,
parts: Parts { body: "airdef:depot", ..NO_PARTS },
mount: (0.0, 0.0),
pivot: DVec3::ZERO,
muzzle: Muzzle::None,
sensor: 0.0,
mast: 1.0,
};
&S
}
SamKind::Tzm => {
const S: SamSpec = SamSpec {
name: "ТЗМ",
key: "tzm",
missile: None,
gun: None,
reload: 0,
scale: 1.3,
rest_pitch: 0.0,
roles: 0,
family: Family::Truck,
parts: Parts { body: "airdef:tzm_truck", ..NO_PARTS },
mount: (0.0, 0.0),
pivot: DVec3::ZERO,
muzzle: Muzzle::None,
sensor: 0.0,
mast: 2.0,
};
&S
}
SamKind::Ew => {
const S: SamSpec = SamSpec {
name: "Станция РЭБ",
key: "ew",
missile: None,
gun: None,
reload: 0,
scale: 1.3,
rest_pitch: 0.0,
roles: 0,
family: Family::Static,
parts: Parts { body: "airdef:ew_truck", ..NO_PARTS },
mount: (0.0, 0.0),
pivot: DVec3::ZERO,
muzzle: Muzzle::None,
sensor: 0.0,
mast: 3.0,
};
&S
}
SamKind::Light => {
const S: SamSpec = SamSpec {
name: "Прожектор",
key: "light",
missile: None,
gun: None,
reload: 0,
scale: 1.2,
rest_pitch: 0.6,
roles: 0,
family: Family::Light,
parts: Parts { body: "airdef:searchlight", launcher: Some("airdef:searchlight_head"), ..NO_PARTS },
mount: (0.85, 0.0),
pivot: DVec3::ZERO,
muzzle: Muzzle::None,
sensor: 0.0,
mast: 1.5,
};
&S
}
}
}
pub fn name(self) -> &'static str {
self.spec().name
}
pub fn key(self) -> &'static str {
self.spec().key
}
pub fn from_key(key: &str) -> Option<Self> {
Self::ALL.into_iter().find(|k| k.key() == key)
}
/// How far it reaches: its missiles or guns; 0 for a unit with neither.
pub fn range(self) -> f64 {
let s = self.spec();
s.missile.as_ref().map_or(0.0, |m| m.max).max(s.gun.as_ref().map_or(0.0, |g| g.range))
}
pub fn armed(self) -> bool {
let s = self.spec();
s.missile.is_some() || s.gun.is_some()
}
}
impl Sam {
pub(super) fn base(&self) -> DQuat {
DQuat::from_rotation_y(self.heading)
}
pub(super) fn scale(&self) -> f64 {
self.kind.spec().scale
}
/// Where shells and missiles leave: the centre of the turret or the launcher's pivot, and the
/// rotation they leave with.
pub(super) fn mount(&self) -> (DVec3, DQuat) {
let spec = self.kind.spec();
let s = spec.scale;
let base = self.base();
let (up, fwd) = spec.mount;
let aim = DQuat::from_rotation_y(self.yaw) * DQuat::from_rotation_x(self.pitch);
match spec.family {
Family::Trainable => (self.pos + base * DVec3::new(0.0, up * s, fwd * s), aim),
Family::Gun | Family::Turret | Family::Light => {
(self.pos + DVec3::new(0.0, up * s, 0.0) + base * DVec3::new(0.0, 0.0, fwd * s), aim)
}
Family::Vertical if matches!(spec.muzzle, Muzzle::Cells) => {
(self.pos + DVec3::new(0.0, up * s, 0.0), DQuat::from_rotation_y(self.yaw))
}
Family::Vertical => (self.pos + base * DVec3::new(0.0, up * s, fwd * s), base * DQuat::from_rotation_x(self.pitch)),
Family::Static | Family::Truck => (self.pos + DVec3::Y * s, base),
}
}
/// Where missile `n` of the magazine leaves (world), and which way.
pub(super) fn muzzle(&self, n: u32) -> (DVec3, DVec3) {
let (at, rot) = self.mount();
let s = self.scale();
let local = match self.kind.spec().muzzle {
Muzzle::Patriot => DVec3::new(
if n % 2 == 0 { -0.42 } else { 0.42 },
0.625 + if n % 4 < 2 { 0.28 } else { -0.28 },
-2.8,
),
Muzzle::Pantsir => {
let side = if n % 2 == 0 { -0.94 } else { 0.94 };
let row = (n / 2) % 6;
DVec3::new(side + ((row % 3) as f64 - 1.0) * 0.15, 0.1 + if row < 3 { 0.15 } else { -0.15 }, -1.5)
}
Muzzle::Grid(cols, rows, size) => {
// From the pivot (rear bottom of the pack) to the far end of tube n.
let (i, j) = (n % cols, (n / cols) % rows);
DVec3::new(
(i as f64 - (cols - 1) as f64 / 2.0) * (size + 1.0) / 16.0,
(j as f64 * (size + 1.0) + size / 2.0) / 16.0,
-3.0,
)
}
Muzzle::Rails(xs) => {
let x = xs[n as usize % xs.len()];
DVec3::new((x - 8.0) / 16.0, 0.1 - 2.5 / 16.0, -1.5)
}
Muzzle::Cells => {
let (i, j) = (n % 4, (n / 4) % 2);
DVec3::new((i as f64 * 5.0 + 1.0 - 8.0) / 16.0, 0.6, (j as f64 * 8.0 + 7.0 - 8.0) / 16.0)
}
Muzzle::None => DVec3::ZERO,
};
// Cells launch straight up whichever way the turret faces.
let dir = if matches!(self.kind.spec().muzzle, Muzzle::Cells) { DVec3::Y } else { rot * DVec3::NEG_Z };
(at + rot * local * s, dir)
}
}
impl Sim {
/// Draws unit `i`: its vehicle, its launcher or turret where it points, its antennas turning.
pub(super) fn draw_sam(&mut self, i: usize, out: &mut Out) {
if !self.sams[i].alive() {
self.draw_wreck(i, out);
return;
}
let sam = &self.sams[i];
let spec = sam.kind.spec();
let parts = &spec.parts;
let base = sam.base();
let s = spec.scale;
let up = DVec3::Y * 0.5 * s;
let id = sam.id;
let (mount, rot) = sam.mount();
let turret = DQuat::from_rotation_y(sam.yaw);
// A unit on the move is packed: launcher down, turret ahead.
out.model(id, parts.body, sam.pos + up, base, s, false);
let mut k = 1;
let mut part = |out: &mut Out, model: &'static str, at: DVec3, r: DQuat, size: f64| {
out.model(id + k, model, at, r, size, false);
k += 1;
};
match spec.family {
Family::Trainable => {
if let Some(l) = parts.launcher {
part(out, l, mount - rot * spec.pivot * s, rot, s);
}
}
Family::Vertical if !matches!(spec.muzzle, Muzzle::Cells) => {
if let Some(l) = parts.launcher {
part(out, l, mount - rot * spec.pivot * s, rot, s);
}
}
Family::Vertical => {
if let Some(l) = parts.launcher {
part(out, l, mount, turret, s);
}
}
Family::Gun => {
if let Some(l) = parts.launcher {
part(out, l, mount + DVec3::Y * 0.12 * s, turret, s);
}
let recoil = rot * DVec3::new(0.0, 0.0, 0.15 * sam.recoil * s);
if let Some(p) = parts.pack {
part(out, p, mount + recoil, rot, s);
}
}
Family::Turret => {
if let Some(l) = parts.launcher {
part(out, l, mount - DVec3::Y * 0.2 * s, turret, s);
}
let recoil = rot * DVec3::new(0.0, 0.0, 0.1 * sam.recoil * s);
if let Some(p) = parts.pack {
part(out, p, mount + DVec3::Y * 0.1 * s + recoil, rot, s);
}
}
Family::Light => {
if let Some(l) = parts.launcher {
part(out, l, mount, rot, s);
}
}
Family::Static | Family::Truck => {}
}
if let Some((model, h, f, tilt)) = parts.dish {
part(out, model, mount + turret * DVec3::new(0.0, h * s, f * s), DQuat::from_rotation_y(sam.dish) * DQuat::from_rotation_x(tilt), s);
}
for &(model, h, back) in parts.fixed {
part(out, model, sam.pos + up + DVec3::Y * h * s + base * DVec3::new(0.0, 0.0, back * s), base, s);
}
if let Some((model, h, back, size, tilt)) = parts.spinner {
let at = sam.pos + up + DVec3::Y * h * s + base * DVec3::new(0.0, 0.0, back * s);
part(out, model, at, DQuat::from_rotation_y(sam.dish * 0.6) * DQuat::from_rotation_x(tilt), s * size);
}
if sam.flash > 0 {
let barrels: &[DVec3] = match sam.kind {
SamKind::Pantsir => &[DVec3::new(-0.95, 0.33, -1.7), DVec3::new(0.95, 0.33, -1.7)],
SamKind::Gepard => &[DVec3::new(-0.92, -0.11, -1.7), DVec3::new(0.92, -0.11, -1.7)],
_ => &[],
};
for (n, b) in barrels.iter().enumerate() {
let r = rot * DQuat::from_rotation_z(self.rng.f() * TAU);
out.model(id + 12 + n as u32, "airdef:flash", mount + rot * *b * s, r, 0.9, true);
}
}
}
}