Files
airdef/airdef-mod/src/panel.rs
T

117 lines
4.5 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::{Drone, 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;
/// Metres (a block is a metre) as a short distance.
pub fn distance(m: f64) -> String {
if m >= 1000.0 { format!("{:.1} км", m / 1000.0) } else { format!("{:.0} м", m) }
}
/// Blocks a tick as km/h.
fn kmh(v: DVec3) -> f64 {
v.length() * 20.0 * 3.6
}
pub fn summary(sim: &Sim, air: &[&Drone], player: DVec3) -> Vec<Line> {
let mut lines = Vec::new();
let line = |text: String, color: u32| Line { text, color };
if air.is_empty() {
lines.push(line("Небо чистое".into(), GREEN));
} else {
let blink = (sim.tick / 10) % 2 == 0;
lines.push(line(format!("ВОЗДУШНАЯ ТРЕВОГА — целей: {}", air.len()), if blink { RED } else { PINK }));
// By type.
let mut by_type = String::new();
for kind in DroneKind::ALL {
let n = air.iter().filter(|d| d.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 threat to the player.
if let Some(d) = air.iter().min_by(|a, b| a.pos.distance(player).total_cmp(&b.pos.distance(player))) {
lines.push(line(
format!(
"Ближ.: {} {} выс.{:.0} {:.0}км/ч",
d.kind.spec().name,
distance(d.pos.distance(player)),
d.pos.y - player.y,
kmh(d.vel)
),
PINK,
));
}
}
if sim.planning() {
lines.push(line("Противник планирует налёт…".into(), YELLOW));
}
if !sim.queue.is_empty() {
lines.push(line(format!("Ещё взлетает: {}", sim.queue.len()), PINK));
}
let hidden = air.iter().filter(|d| !sim.sams.is_empty() && !sim.tracked(d.id)).count();
if hidden > 0 {
lines.push(line(format!("Вне видимости РЛС: {hidden}"), GREY));
}
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| sim.drones.iter().find(|d| d.id == t)) {
text += &format!(" > {} {}", t.kind.spec().name, distance(t.pos.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
}