Files
airdef/airdef-mod/src/sim/units.rs
T
chexit 858735a4c6 Implement the first full radar-profile slice (#134)
Give the separate radar three real profiles over SamKind::Radar and the
typed catalog variants:

- radar_search: coarse, slow, long-range search plots; no fire authority.
- radar_height: raised mast, local refinement of low plots; no fire authority.
- radar_track: local search, fast revisit, two refined/fire channels.
- radar_multi remains the compatible universal profile, not best everywhere.

Tracks now carry the measuring station, product (search/refine/fire), and
quality (separate from identification), and keep the station's own plot when a
fresher network relay replaces the published position. A relay never refreshes
the measurement or authorizes a launch. Ordinary launches need the battery's
own confirmed fire plot; launcher-only batteries use a supporting radar's own
plot, and the existing heavy far shot keeps its cue-radar exception.

Per-tick refinement/fire work is bounded by profile channels and local search
radius. Off, damage, network and save/replay behavior are preserved: new saves
write the stable profile ID, old role-only lines migrate, and embedded SAM
radars keep their own fire authority. The deploy menu, status lines, map,
monitor, radar scope and /airdef why explain the profile, source, age, quality,
channels and refusal reason.

Models/textures are untouched: all profiles reuse the existing radar
silhouette (the separate art zone remains for its owner).

Tests: profile single/pair/full scenarios and duplicate station, channels,
relay source/age/quality, Off/damage, save migration, replay, embedded and
Multifunction radars, catalog/generator checks, airdef-mod suites, core lib
(non-balance on one thread; the four active balance tests in release) and core
scenarios.

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
2026-11-20 23:11:12 +03:00

396 lines
16 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 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),
}
/// How fast a weapon's mount turns: per axis the top speed (radians a tick) and the acceleration, which
/// is also the braking (radians a tick²). Game settings, not the real machines' figures.
#[derive(Clone, Copy)]
pub(super) struct Drive {
pub yaw: (f64, f64),
pub pitch: (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,
}
impl SamSpec {
/// The mount's drive: the guns' own slew rates, the old rates of the launchers, scaled down.
pub(super) fn drive(&self) -> Drive {
let (yaw, pitch) = match (&self.gun, self.family) {
(Some(g), _) => g.slew,
(None, Family::Trainable) => DRIVE_TRAINABLE,
(None, Family::Vertical) => DRIVE_VERTICAL,
(None, _) => DRIVE_OTHER,
};
let axis = |speed: f64| (speed * DRIVE_SCALE, speed * DRIVE_SCALE / DRIVE_RAMP);
Drive { yaw: axis(yaw), pitch: axis(pitch) }
}
}
include!("unit_specs_generated.rs");
impl SamKind {
pub const ALL: [SamKind; super::catalog::UNIT_COUNT] = super::catalog::UNIT_KINDS;
/// 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 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()
}
/// Missiles its launcher's magazine holds; `None` without a launcher.
pub fn missile_magazine(self) -> Option<u32> {
self.spec().missile.as_ref().map(|m| m.magazine)
}
/// Whether it has a gun (its bursts are read by the status line).
pub fn has_gun(self) -> bool {
self.spec().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) {
draw_sam_model(&self.sams[i], &mut self.vis, out);
}
}
/// Draws a system as it stands (a live one, or a replay's ghost built from the recording).
pub(super) fn draw_sam_model(sam: &Sam, rng: &mut Rng, out: &mut Out) {
if !sam.alive() {
damage::draw_wreck_of(sam, out);
return;
}
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 sam.mode != battery::SamMode::LauncherOnly {
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 extra = if sam.kind == SamKind::Radar { sam.radar_profile().mast } else { 0.0 };
let foot = sam.pos + up + DVec3::Y * h * s + base * DVec3::new(0.0, 0.0, back * s);
for segment in 0..(extra as u32 / 2) {
part(out, "airdef:radar_mast", foot + DVec3::Y * (segment as f64 * 2.0 + 0.5), base, 1.0);
}
let at = foot + DVec3::Y * extra;
let heading = if sam.kind == SamKind::Radar && sam.radar_role == battery::RadarRole::Engagement {
sam.tracks.first().map_or(sam.dish * 0.6, |t| heading_of(t.pos - sam.pos))
} else { sam.dish * 0.6 };
part(out, model, at, DQuat::from_rotation_y(heading) * DQuat::from_rotation_x(tilt), s * size);
}
}
launch::draw_caps(sam, out);
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(rng.f() * TAU);
out.model(id + 12 + n as u32, "airdef:flash", mount + rot * *b * s, r, 0.9, true);
}
}
}
/// The charred variant of a part's model, for wrecks (tools/gen_models.py `WRECKS`); a part without one keeps its model.
pub(super) fn wreck(model: &'static str) -> &'static str {
match model {
"airdef:patriot_trailer" => "airdef:patriot_trailer_burnt",
"airdef:patriot_launcher" => "airdef:patriot_launcher_burnt",
"airdef:gepard_hull" => "airdef:gepard_hull_burnt",
"airdef:gepard_turret" => "airdef:gepard_turret_burnt",
"airdef:gepard_guns" => "airdef:gepard_guns_burnt",
"airdef:pantsir_truck" => "airdef:pantsir_truck_burnt",
"airdef:pantsir_turret" => "airdef:pantsir_turret_burnt",
"airdef:pantsir_pack" => "airdef:pantsir_pack_burnt",
"airdef:s400_chassis" => "airdef:s400_chassis_burnt",
"airdef:s400_tubes" => "airdef:s400_tubes_burnt",
"airdef:s500_chassis" => "airdef:s500_chassis_burnt",
"airdef:s500_tubes" => "airdef:s500_tubes_burnt",
"airdef:irist_chassis" => "airdef:irist_chassis_burnt",
"airdef:irist_tubes" => "airdef:irist_tubes_burnt",
"airdef:nasams_trailer" => "airdef:nasams_trailer_burnt",
"airdef:nasams_launcher" => "airdef:nasams_launcher_burnt",
"airdef:buk_chassis" => "airdef:buk_chassis_burnt",
"airdef:buk_turret" => "airdef:buk_turret_burnt",
"airdef:buk_rails" => "airdef:buk_rails_burnt",
"airdef:tor_chassis" => "airdef:tor_chassis_burnt",
"airdef:tor_turret" => "airdef:tor_turret_burnt",
"airdef:p1sun_launcher" => "airdef:p1sun_launcher_burnt",
"airdef:radar_truck" => "airdef:radar_truck_burnt",
"airdef:depot" => "airdef:depot_burnt",
"airdef:tzm_truck" => "airdef:tzm_truck_burnt",
"airdef:ew_truck" => "airdef:ew_truck_burnt",
"airdef:searchlight" => "airdef:searchlight_burnt",
"airdef:searchlight_head" => "airdef:searchlight_head_burnt",
other => other,
}
}