Files
airdef/airdef-mod/src/radar.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

349 lines
18 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.
//! The radar screen (bottom right): a round scope round the player, north up,
//! with a sweeping beam; blips fade after the beam passes them. A track's blip is a shape by the
//! class the radar measured (#132), big enough to read at a glance; close ones merge with a count.
use crate::panel::distance;
use crate::sim::{Class, ClassEst, SamKind, Sim};
use glam::DVec3;
use horizon_mod_sdk::render::{FillShape, Point, QuadShape, Rect, Shape, TextShape};
/// The scope's side in GUI pixels.
pub const SIZE: f32 = 112.0;
/// The corner box's height: the scope and its range label, plus the truth mode's own line when it shows one.
pub fn height(truth_label: bool) -> f32 {
SIZE + if truth_label { 22.0 } else { 12.0 }
}
/// Blocks from the centre to the edge.
const RANGE: f64 = 500.0;
const SEGMENTS: usize = 48;
/// A track blip's half-size: the icons span about six pixels, against the old dot's three and a half.
const BLIP: f32 = 3.0;
/// Tracks this close on the scope merge into one blip with their number.
const MERGE_R: f32 = 4.5;
fn pt(x: f32, y: f32) -> Point {
Point { x, y }
}
fn quad(a: Point, b: Point, c: Point, d: Point, color: u32) -> Shape {
Shape::Quad(QuadShape { corners: (a, b, c, d), color })
}
fn square(x: f32, y: f32, half: f32, color: u32) -> Shape {
Shape::Fill(FillShape { rect: Rect { x: x - half, y: y - half, w: half * 2.0, h: half * 2.0 }, color })
}
fn text(t: String, x: f32, y: f32, color: u32, centered: bool) -> Shape {
Shape::Text(TextShape { text: t, at: pt(x, y), color, centered, shadow: true })
}
/// 0xAARRGGBB with this alpha (0..1).
fn alpha(rgb: u32, a: f64) -> u32 {
((a.clamp(0.0, 1.0) * 255.0) as u32) << 24 | (rgb & 0xFFFFFF)
}
/// A track's blip: a shape by the estimated class, pointing along the measured course.
/// Drones are triangles, jets darts, cruise squares, supersonic diamonds, ballistic octagons;
/// an unidentified track gets a question mark instead of leaking the target's type (#133).
fn blip(class: ClassEst, x: f32, y: f32, color: u32, vel: DVec3) -> Vec<Shape> {
let h = DVec3::new(vel.x, 0.0, vel.z).normalize_or_zero();
let (hx, hy) = if h == DVec3::ZERO { (0.0, -1.0) } else { (h.x as f32, h.z as f32) };
let (px, py) = (-hy, hx);
let r = BLIP;
let at = |along: f32, side: f32| pt(x + hx * along + px * side, y + hy * along + py * side);
match class.class() {
Some(Class::Drone) => vec![quad(at(r * 1.2, 0.0), at(-r * 0.8, r * 0.9), at(-r * 0.8, -r * 0.9), at(-r * 0.8, -r * 0.9), color)],
Some(Class::Jet) => vec![quad(at(r * 1.3, 0.0), at(-r * 0.9, r * 0.9), at(-r * 0.3, 0.0), at(-r * 0.9, -r * 0.9), color)],
Some(Class::Cruise) => vec![square(x, y, 2.6, color)],
Some(Class::Supersonic) => vec![quad(pt(x, y - 3.2), pt(x + 3.2, y), pt(x, y + 3.2), pt(x - 3.2, y), color)],
Some(Class::Ballistic) => vec![square(x, y, 2.3, color), quad(pt(x, y - r), pt(x + r, y), pt(x, y + r), pt(x - r, y), color)],
None => vec![square(x, y, 2.4, color), text("?".into(), x, y - 3.0, 0xFF200808, true)],
}
}
/// Keeps a blip's centre far enough inside the disc that the whole icon stays on the scope: the longest point
/// of any class's icon is 1.3 × a half-size out.
fn inside(x: f32, y: f32, cx: f32, cy: f32, r: f32) -> (f32, f32) {
let (dx, dy) = (x - cx, y - cy);
let d = dx.hypot(dy);
let max = r - BLIP * 1.4 - 0.5;
if d > max && d > 0.0 { (cx + dx / d * max, cy + dy / d * max) } else { (x, y) }
}
pub fn draw(sim: &Sim, player: DVec3, yaw: f32) -> Vec<Shape> {
let mut s = Vec::new();
let r = SIZE / 2.0 - 2.0;
let (cx, cy) = (SIZE / 2.0, SIZE / 2.0);
let at = |a: f32, rad: f32| pt(cx + a.sin() * rad, cy - a.cos() * rad);
let tau = std::f32::consts::TAU;
// The scope: a dark disc, rings every fifth of the range, a cross.
for i in 0..SEGMENTS {
let (a0, a1) = (i as f32 / SEGMENTS as f32 * tau, (i + 1) as f32 / SEGMENTS as f32 * tau);
s.push(quad(pt(cx, cy), at(a0, r), at(a1, r), at(a1, r), 0xD0061208));
}
for k in 1..=4 {
let rr = r * k as f32 / 4.0;
for i in 0..SEGMENTS {
let (a0, a1) = (i as f32 / SEGMENTS as f32 * tau, (i + 1) as f32 / SEGMENTS as f32 * tau);
s.push(quad(at(a0, rr - 0.5), at(a0, rr + 0.5), at(a1, rr + 0.5), at(a1, rr - 0.5), if k == 4 { 0xC040FF60 } else { 0x5030C050 }));
}
}
s.push(Shape::Fill(FillShape { rect: Rect { x: cx - 0.5, y: cy - r, w: 1.0, h: r * 2.0 }, color: 0x3030C050 }));
s.push(Shape::Fill(FillShape { rect: Rect { x: cx - r, y: cy - 0.5, w: r * 2.0, h: 1.0 }, color: 0x3030C050 }));
// The beam and its afterglow.
let sweep = (sim.tick as f32 * 0.07) % tau;
for k in 0..14 {
let a1 = sweep - k as f32 * 0.06;
let a0 = a1 - 0.06;
let glow = 0.55 * (1.0 - k as f32 / 14.0);
s.push(quad(pt(cx, cy), at(a0, r), at(a1, r), at(a1, r), alpha(0x40FF70, glow as f64)));
}
// World to scope: north (-z) up.
let scope = |p: DVec3| -> Option<(f32, f32, f32)> {
let d = p - player;
let flat = (d.x * d.x + d.z * d.z).sqrt();
if flat > RANGE {
return None;
}
let k = (r as f64 / RANGE) as f32;
let bearing = (d.x as f32).atan2(-d.z as f32).rem_euclid(tau);
Some((cx + d.x as f32 * k, cy + d.z as f32 * k, bearing))
};
// A blip is brightest just after the beam passes it.
let fade = |bearing: f32| -> f64 {
let since = (sweep - bearing).rem_euclid(tau) / tau;
(1.0 - since as f64 * 0.8).max(0.25)
};
// Air defence: their reach, and themselves.
for sam in &sim.sams {
if let Some((x, y, _)) = scope(sam.pos) {
let reach = (sam.kind.range() / RANGE) as f32 * r;
if reach < r * 1.5 {
for i in 0..SEGMENTS {
let (a0, a1) = (i as f32 / SEGMENTS as f32 * tau, (i + 1) as f32 / SEGMENTS as f32 * tau);
let p = |a: f32, rad: f32| pt(x + a.sin() * rad, y - a.cos() * rad);
if i % 2 == 0 {
s.push(quad(p(a0, reach - 0.4), p(a0, reach + 0.4), p(a1, reach + 0.4), p(a1, reach - 0.4), 0x5060C0FF));
}
}
}
let color = match sam.kind {
SamKind::Gepard | SamKind::Pantsir => 0xFF60E0FF,
_ => 0xFF40A0FF,
};
s.push(square(x, y, 2.5, color));
}
}
for t in &sim.targets {
if let Some((x, y, _)) = scope(*t) {
s.push(quad(pt(x - 3.0, y - 2.0), pt(x - 2.0, y - 3.0), pt(x + 3.0, y + 2.0), pt(x + 2.0, y + 3.0), 0xFFFF3030));
s.push(quad(pt(x + 2.0, y - 3.0), pt(x + 3.0, y - 2.0), pt(x - 2.0, y + 3.0), pt(x - 3.0, y + 2.0), 0xFFFF3030));
}
}
for m in &sim.missiles {
if let Some((x, y, _)) = scope(m.pos) {
s.push(square(x, y, 1.0, 0xFFFFE060));
}
}
// Launch sites the radars found: a diamond where the place is known (with the circle of its error), and a mark on
// the rim along the bearing when it is beyond the scope or only the bearing is known.
for k in &sim.strikes.known {
let Some(e) = &k.est else { continue };
let gone = matches!(k.state, crate::sim::strike::SiteState::Destroyed(_));
let color = if gone { 0xFF707070 } else { 0xFFFFA040 };
let inside = e.fix.then(|| scope(DVec3::new(e.pos.x, player.y, e.pos.y))).flatten();
if let Some((x, y, _)) = inside {
let q = 3.0;
s.push(quad(pt(x, y - q), pt(x + q, y), pt(x, y + q), pt(x - q, y), color));
let rad = (e.radius / RANGE) as f32 * r;
if rad > 3.5 {
for i in 0..SEGMENTS {
let (a0, a1) = (i as f32 / SEGMENTS as f32 * tau, (i + 1) as f32 / SEGMENTS as f32 * tau);
if i % 2 == 0 {
let p = |a: f32, rr: f32| pt(x + a.sin() * rr, y - a.cos() * rr);
s.push(quad(p(a0, rad - 0.4), p(a0, rad + 0.4), p(a1, rad + 0.4), p(a1, rad - 0.4), alpha(color, 0.5)));
}
}
}
s.push(text(k.site.to_string(), x + 5.0, y - 9.0, color, false));
} else {
// On the rim, along the bearing (compass: north up, east right).
let a = e.bearing as f32;
let (x, y) = (cx + a.sin() * (r - 3.0), cy - a.cos() * (r - 3.0));
let q = if e.fix { 3.0 } else { 2.0 };
s.push(quad(pt(x, y - q), pt(x + q, y), pt(x, y + q), pt(x - q, y), color));
s.push(text(format!("{}{}", k.site, if e.fix { "" } else { "?" }), x - a.sin() * 8.0, y + a.cos() * -8.0 - 4.0, color, true));
}
}
// Strike missiles in flight, on the same footing: a small bright dot where the scope reaches, else nothing.
for f in &sim.strikes.flights {
if let Some((x, y, _)) = scope(f.pos) {
s.push(square(x, y, 1.5, 0xFFFFFFA0));
}
}
// The player is below target blips: a close target must not disappear under the white arrow.
// Minecraft's yaw 0 looks south (+z, down the scope).
let face = yaw.to_radians();
let tip = |a: f32, l: f32| pt(cx - (face + a).sin() * l, cy + (face + a).cos() * l);
s.push(quad(tip(0.0, 6.0), tip(2.5, 4.0), pt(cx, cy), tip(-2.5, 4.0), 0xFFFFFFFF));
let mut far = 0;
// The air picture is what the systems' radars hold, as they last measured it (a track older than a second
// is a forecast, dimmer); with no system deployed, the sky as it is. Nothing else is read (#93).
let pic = sim.air_picture();
let now = pic.now;
// (x, y, class, colour, brightness, velocity) of every track on the scope, its icon kept inside the disc.
let mut marks: Vec<(f32, f32, ClassEst, u32, f64, DVec3)> = Vec::new();
for t in &pic.tracks {
let (pos, vel) = (t.est(now), t.vel);
match scope(pos) {
Some((x, y, bearing)) => {
// Fresh: bright and red; a forecast: orange, fading as it ages towards the track's drop.
// The product is visible: a coarse search plot is amber, a refinement yellow, a fire plot red.
let fresh_color = match t.product {
crate::sim::battery::Measure::Search => 0xFFB060,
crate::sim::battery::Measure::Refine => 0xFFE070,
crate::sim::battery::Measure::Fire => 0xFF4040,
};
let (a, color) = if pic.unaided {
(fade(bearing), 0xFF4040)
} else if t.forecast(now) {
((1.0 - t.age(now) as f64 / t.ttl.max(1) as f64 * 0.7).max(0.3), 0xFFB040)
} else {
(1.0, fresh_color)
};
let (x, y) = inside(x, y, cx, cy, r);
marks.push((x, y, t.class, color, a, vel));
}
None => far += 1,
}
}
// A tight raid reads as one blip with its number, not a pile of icons.
let pos: Vec<(f32, f32)> = marks.iter().map(|m| (m.0, m.1)).collect();
for grp in crate::map::group(&pos, MERGE_R) {
let (x, y, class, color, a, vel) = marks[grp[0]];
s.extend(blip(class, x, y, alpha(color, a), vel));
// Its heading, a short tail, kept on the disc too.
let v = vel.normalize_or_zero();
let (ex, ey) = inside(x - v.x as f32 * 7.0, y - v.z as f32 * 7.0, cx, cy, r);
s.push(quad(pt(x, y), pt(x + 1.0, y), pt(ex + 1.0, ey), pt(ex, ey), alpha(color, a * 0.6)));
if grp.len() > 1 {
s.push(text(format!("×{}", grp.len()), x + 4.0, y - 9.0, alpha(color, a), false));
}
}
// The true places, when the sandbox asks for them: a white dot beside the track, to see how far the picture is off.
if sim.truth && !pic.unaided {
for d in sim.drones.iter().filter(|d| d.alive()) {
if let Some((x, y, _)) = scope(d.pos) {
let (x, y) = inside(x, y, cx, cy, r);
s.push(square(x, y, 1.3, 0xFFFFFFFF));
}
}
}
// Labels.
s.push(text(format!("РЛС · {}", distance(RANGE)), cx, SIZE + 2.0, 0xFF80FF90, true));
if sim.truth && !pic.unaided {
s.push(text("ИСТИНА · песочница".into(), cx, SIZE + 12.0, 0xFFFFE070, true));
}
s.push(text("С".into(), cx, 3.0, 0xFF80FF90, true));
if far > 0 {
s.push(text(format!("+{far} за пределом"), cx, SIZE - 14.0, 0xFFFF8080, true));
}
s
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sim::{DroneKind, Out, Sam};
#[test]
fn target_blips_are_drawn_over_the_player_arrow() {
let mut sim = Sim::new(1);
sim.launch(DroneKind::Shahed, DVec3::new(0.0, 64.0, -10.0), DVec3::ZERO, DVec3::ZERO, &mut Out::default());
let shapes = draw(&sim, DVec3::ZERO, 0.0);
let arrow = shapes.iter().rposition(|s| matches!(s, Shape::Quad(q) if q.color == 0xFFFFFFFF)).unwrap();
// The Shahed's blip is a red triangle (a quad with a repeated corner), drawn after the arrow.
let target = shapes.iter().rposition(|s| matches!(s, Shape::Quad(q) if q.color == 0xFFFF4040)).unwrap();
assert!(target > arrow, "the player arrow covers the target blip");
}
#[test]
fn every_class_has_its_own_blip_shape() {
let vel = DVec3::new(0.5, 0.0, 0.0);
let shapes: Vec<Vec<Shape>> = Class::ALL.iter().map(|c| blip(ClassEst::Firm(*c), 50.0, 50.0, 0xFFFF4040, vel)).collect();
for (i, a) in shapes.iter().enumerate() {
assert!(!a.is_empty());
for b in &shapes[i + 1..] {
assert_ne!(format!("{a:?}"), format!("{b:?}"), "two classes share one icon");
}
}
// A courseless track (a ballistic one straight overhead) still draws, pointing north.
assert!(!blip(ClassEst::Firm(Class::Ballistic), 50.0, 50.0, 0xFFFF4040, DVec3::Y).is_empty());
assert!(blip(ClassEst::Unknown, 50.0, 50.0, 0xFFFF4040, vel).iter().any(|s| matches!(s, Shape::Text(t) if t.text == "?")));
}
#[test]
fn blips_stay_inside_the_scope() {
let mut sim = Sim::new(1);
// One track near the rim on every bearing, one of each class among them, one just beyond it.
for k in 0..8 {
let a = k as f64 / 8.0 * std::f64::consts::TAU;
sim.launch(DroneKind::Shahed, DVec3::new(a.sin() * 495.0, 64.0, a.cos() * 495.0), DVec3::ZERO, DVec3::ZERO, &mut Out::default());
}
for (k, kind) in [DroneKind::Geran3, DroneKind::Kh101, DroneKind::Kh22, DroneKind::Kinzhal].into_iter().enumerate() {
let a = (k as f64 + 0.5) / 4.0 * std::f64::consts::TAU;
sim.launch(kind, DVec3::new(a.sin() * 490.0, 64.0, a.cos() * 490.0), DVec3::ZERO, DVec3::ZERO, &mut Out::default());
}
sim.launch(DroneKind::Shahed, DVec3::new(0.0, 64.0, -700.0), DVec3::ZERO, DVec3::ZERO, &mut Out::default());
let shapes = draw(&sim, DVec3::ZERO, 0.0);
let (cx, cy, r) = (SIZE / 2.0, SIZE / 2.0, SIZE / 2.0 - 2.0);
let mut blips = 0;
for s in &shapes {
let corners = match s {
Shape::Quad(q) if q.color & 0xFFFFFF == 0xFF4040 => vec![q.corners.0, q.corners.1, q.corners.2, q.corners.3],
Shape::Fill(f) if f.color & 0xFFFFFF == 0xFF4040 => vec![pt(f.rect.x, f.rect.y), pt(f.rect.x + f.rect.w, f.rect.y), pt(f.rect.x, f.rect.y + f.rect.h), pt(f.rect.x + f.rect.w, f.rect.y + f.rect.h)],
_ => continue,
};
blips += 1;
for p in corners {
assert!((p.x - cx).hypot(p.y - cy) <= r + 0.01, "a blip corner leaves the disc: {p:?}");
}
}
assert!(blips >= 8, "the rim tracks were not drawn");
assert!(shapes.iter().any(|s| matches!(s, Shape::Text(t) if t.text.contains("за пределом"))), "the track beyond the range is counted");
}
#[test]
fn a_tight_raid_merges_into_one_blip_with_a_count() {
let mut sim = Sim::new(1);
for k in 0..3 {
sim.launch(DroneKind::Shahed, DVec3::new(100.0 + k as f64, 64.0, -100.0), DVec3::ZERO, DVec3::ZERO, &mut Out::default());
}
let shapes = draw(&sim, DVec3::ZERO, 0.0);
assert!(shapes.iter().any(|s| matches!(s, Shape::Text(t) if t.text == "×3")), "the merged blip has no count");
let triangles = shapes
.iter()
.filter(|s| matches!(s, Shape::Quad(q) if q.color & 0xFFFFFF == 0xFF4040 && q.corners.2.x == q.corners.3.x && q.corners.2.y == q.corners.3.y))
.count();
assert_eq!(triangles, 1, "three close tracks drew three icons");
}
#[test]
fn the_truth_mode_is_labelled_on_the_scope() {
let mut sim = Sim::new(1);
let id = sim.id();
sim.sams.push(Sam::new(id, SamKind::Buk, DVec3::new(0.0, 64.0, 0.0), 0.0));
sim.launch(DroneKind::Shahed, DVec3::new(0.0, 64.0, -100.0), DVec3::ZERO, DVec3::ZERO, &mut Out::default());
let plain = draw(&sim, DVec3::ZERO, 0.0);
assert!(!plain.iter().any(|s| matches!(s, Shape::Text(t) if t.text.contains("ИСТИНА"))));
sim.truth = true;
let shapes = draw(&sim, DVec3::ZERO, 0.0);
let label = shapes.iter().find_map(|s| match s {
Shape::Text(t) if t.text.contains("ИСТИНА") && t.text.contains("песочница") => Some(t),
_ => None,
}).expect("the truth mode is not marked");
// The corner box is sized to hold the label: a line of the HUD font is 9 pixels with its shadow.
assert!(label.at.y + 9.0 <= height(true), "the truth label falls outside the corner box");
assert!(height(true) > height(false));
}
}