Files
airdef/airdef-mod/src/panel.rs
T
chexit 5f07027faa Build canonical simulation without Horizon SDK and remove hidden launch fixes (#87 #107)
Preserve the adapter facade and move adapter-only checks out of domain tests. Late tracks do not reveal an unseen launch origin or its timestamp.

Co-Authored-By: Codex GPT-6.1 Terra <noreply@openai.com>
2026-11-02 01:17:03 +03:00

122 lines
5.3 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 summary panel (top right): the alert, what is in the air, each
//! air-defence system's state and the score.
use crate::sim::{AirPicture, DroneKind, Sim};
use glam::DVec3;
use horizon_mod_sdk::hud::Line;
const RED: u32 = 0xFF5050;
const PINK: u32 = 0xFFB0B0;
const CYAN: u32 = 0x9FE0FF;
const GREY: u32 = 0xB0B0B0;
const YELLOW: u32 = 0xFFE070;
const GREEN: u32 = 0x80FF90;
/// The most lines the summary holds (the HUD's room).
const MAX_LINES: usize = 16;
pub use crate::sim::distance;
/// Blocks a tick as km/h.
fn kmh(v: DVec3) -> f64 {
v.length() * 20.0 * 3.6
}
/// The panel reads the air picture only (#93): what the network holds, with the age of a forecast. The sandbox's
/// `truth` adds one marked line of the real figures; it changes nothing else.
pub fn summary(sim: &Sim, pic: &AirPicture, player: DVec3) -> Vec<Line> {
let mut lines = Vec::new();
let line = |text: String, color: u32| Line { text, color };
let now = pic.now;
if pic.tracks.is_empty() {
lines.push(line("Небо чистое".into(), GREEN));
} else {
let blink = (sim.tick / 10) % 2 == 0;
lines.push(line(format!("ВОЗДУШНАЯ ТРЕВОГА — целей: {}", pic.tracks.len()), if blink { RED } else { PINK }));
// By type, as the radars classed them.
let mut by_type = String::new();
for kind in DroneKind::ALL {
let n = pic.tracks.iter().filter(|t| t.kind == kind).count();
if n > 0 {
if !by_type.is_empty() {
by_type += ", ";
}
by_type += &format!("{} ×{n}", kind.spec().name);
}
}
lines.push(line(by_type, PINK));
// The nearest track to the player: where it was last measured (a forecast says so, with its age).
if let Some(t) = pic.nearest(player) {
let e = t.est(now);
let mark = if t.forecast(now) { format!(" ~{}с", t.age(now) / 20) } else { String::new() };
lines.push(line(
format!("Ближ.: {} {} выс.{:.0} {:.0}км/ч{mark}", t.kind.spec().name, distance(e.distance(player)), e.y - player.y, kmh(t.vel)),
PINK,
));
}
}
if sim.truth {
let real = sim.drones.iter().filter(|d| d.alive()).count();
let unseen = sim.drones.iter().filter(|d| d.alive() && pic.get(d.id).is_none()).count();
let mut text = format!("ИСТИНА: в воздухе {real}, вне картины {unseen}, взлетает {}", sim.queue.len());
if sim.planning() {
text += ", противник планирует налёт";
}
lines.push(line(text, YELLOW));
}
// The launch sites found, and the strike missiles (#101).
if !sim.strikes.known.is_empty() || !sim.strikes.flights.is_empty() {
let st = &sim.strikes;
let live = st.known.iter().filter(|k| !matches!(k.state, crate::sim::strike::SiteState::Destroyed(_))).count();
let mut text = format!("Известных ПУ: {live} · ударных ракет: {}", st.stock);
if let Some(f) = st.flights.iter().map(|f| f.arrive.saturating_sub(sim.tick)).min() {
text += &format!(" · в полёте {} (до цели {} с)", st.flights.len(), f / 20);
}
lines.push(line(text, YELLOW));
}
if !sim.target_health.is_empty() {
let list: Vec<String> = sim.target_health.iter().map(|h| format!("{h:.0}%")).collect();
let worst = sim.target_health.iter().copied().fold(100.0, f64::min);
lines.push(line(format!("Объекты: {}", list.join(" ")), if worst < 50.0 { RED } else { GREEN }));
}
let lost = sim.sams.iter().filter(|s| !s.alive()).count();
if lost > 0 {
lines.push(line(format!("Потеряно комплексов: {lost}"), RED));
}
if !sim.missiles.is_empty() {
lines.push(line(format!("Перехватчиков в полёте: {}", sim.missiles.len()), YELLOW));
}
// The score is the last line and always shown: the unit list gets what
// room is left, and cuts its own tail.
let room = (MAX_LINES - 1).saturating_sub(lines.len());
let total = sim.sams.len();
let mut shown = total.min(8);
if shown + usize::from(total > 8) > room {
shown = room.saturating_sub(1);
}
for sam in sim.sams.iter().take(shown) {
let mut text = if sam.kind.armed() {
format!("{} [{}]", sam.status(), crate::sim::roles_text(sam.roles))
} else {
sam.status()
};
if let Some(t) = sam.gun_target.or(sam.target).and_then(|t| pic.get(t)) {
text += &format!(" > {} {}", t.kind.spec().name, distance(t.est(now).distance(sam.pos)));
}
if sam.kills > 0 {
text += &format!(" ({})", sam.kills);
}
lines.push(line(text, CYAN));
}
if total > shown && room > 0 {
lines.push(line(format!("…и ещё {} комплексов", total - shown), CYAN));
}
// Room for the score whatever came before.
lines.truncate(MAX_LINES - 1);
let real = sim.launched.saturating_sub(0);
let rate = if real > 0 { sim.kills as f64 * 100.0 / real as f64 } else { 0.0 };
lines.push(line(
format!("Сбито {} из {} ({rate:.0}%) · прорвалось {}", sim.kills, sim.launched, sim.hits),
GREY,
));
lines
}