//! The air-defence monitor: a system's state, its radar's picture round it //! and its camera (a real view of the world from the system, the game //! renders it) locked on its target. K shows one system at a time, J all of //! them in a grid. use crate::panel::distance; use crate::sim::{ClassEst, OpticalState, Sam, SamKind, SamState, Sim, heading_of, roles_text}; use glam::DVec3; use horizon_mod_sdk::render::{self, CameraView, FillShape, Point, QuadShape, Rect, Shape, TextShape, ViewShape}; fn fill(x: f32, y: f32, w: f32, h: f32, color: u32) -> Shape { Shape::Fill(FillShape { rect: Rect { x, y, w, h }, color }) } fn text(t: impl Into, x: f32, y: f32, color: u32) -> Shape { Shape::Text(TextShape { text: t.into(), at: Point { x, y }, color, centered: false, shadow: true }) } fn quad(a: (f32, f32), b: (f32, f32), c: (f32, f32), d: (f32, f32), color: u32) -> Shape { let p = |(x, y): (f32, f32)| Point { x, y }; Shape::Quad(QuadShape { corners: (p(a), p(b), p(c), p(d)), color }) } /// `t` cut to `w` GUI pixels, with an ellipsis. fn fit(t: String, w: f32) -> String { if render::text_width(&t) <= w { return t; } // The width grows with the prefix: bisect over its char starts, so a long // label costs a few host calls rather than one per dropped char. let starts: Vec = t.char_indices().map(|(i, _)| i).collect(); let (mut lo, mut hi) = (0, starts.len()); while lo < hi { let mid = (lo + hi) / 2; if render::text_width(&format!("{}…", &t[..starts[mid]])) <= w { lo = mid + 1; } else { hi = mid; } } if lo == 0 { return "…".into(); } format!("{}…", &t[..starts[lo - 1]]) } fn argb(rgb: u32, a: u32) -> u32 { (a << 24) | (rgb & 0xFFFFFF) } /// Corner brackets of a box. fn brackets(s: &mut Vec, x: f32, y: f32, w: f32, h: f32, len: f32, color: u32) { for (cx, cy, dx, dy) in [(x, y, 1.0, 1.0), (x + w, y, -1.0, 1.0), (x, y + h, 1.0, -1.0), (x + w, y + h, -1.0, -1.0)] { let fx = if dx > 0.0 { cx } else { cx - len }; s.push(fill(fx, if dy > 0.0 { cy } else { cy - 1.0 }, len, 1.0, color)); let gx = if dx > 0.0 { cx } else { cx - 1.0 }; s.push(fill(gx, if dy > 0.0 { cy } else { cy - len }, 1.0, len, color)); } } /// The camera's picture size, pixels. const PICTURE: (u32, u32) = (320, 200); /// Where system `sam`'s camera is and looks: at its target, zoomed so the /// target fills a good part of the picture; else ahead and a little up. pub fn camera(sim: &Sim, sam: &Sam) -> CameraView { let eye = sam.pos + DVec3::Y * 3.5; let (dir, fov) = match sim.focus(sam) { Some(p) => { let rel = p - eye; let dist = rel.length().max(1.0); // About 14 blocks of view round a missile far off, wider when it is near. let fov = (2.0 * (7.0 / dist).atan()).to_degrees().clamp(1.5, 50.0); (rel / dist, fov as f32) } None => { let h = sam.heading + (sim.tick as f64 * 0.004).sin() * 0.6; (DVec3::new(-h.sin(), 0.25, -h.cos()).normalize(), 55.0) } }; // Minecraft's yaw: 0 looks to +z, 90 to -x; pitch 90 looks down. let yaw = (-dir.x).atan2(dir.z).to_degrees() as f32; let pitch = (-dir.y).asin().to_degrees() as f32; CameraView { id: sam.id, x: eye.x, y: eye.y, z: eye.z, yaw, pitch, fov, width: PICTURE.0, height: PICTURE.1 } } /// One system's monitor in the box (x, y, w, h). fn cell(sim: &Sim, index: usize, x: f32, y: f32, w: f32, h: f32, s: &mut Vec) { let Some(sam) = sim.sams.get(index) else { return }; let spec = sam.kind.spec(); let tick = sim.tick; s.push(fill(x, y, w, h, 0xC0080C0A)); s.push(fill(x, y, w, 12.0, 0xE0142018)); let state = sam.state; let optical = sam.kind == SamKind::Optical; let lock = matches!(state, SamState::Lock | SamState::Launch | SamState::Fire); let blink = lock && (tick / 4) % 2 == 0; let label = if optical { sam.optical.state().name() } else { state.name() }; let color = if optical { match sam.optical.state() { OpticalState::Observing => 0xFF60FF80, OpticalState::Holding => 0xFF9FE0FF, OpticalState::NoSight => 0xFFFFA040, OpticalState::Searching => 0xFFE0E0E0, } } else { state.color() }; let lw = render::text_width(label); s.push(text(fit(format!("№{} {}", index + 1, spec.name), w - lw - 12.0), x + 3.0, y + 2.0, 0xFF9FE0FF)); s.push(text(label, x + w - 3.0 - lw, y + 2.0, argb(color, if blink { 0x80 } else { 0xFF }))); // The radar, at the left: round the system, north up. let rr = ((h - 28.0) / 2.0).min(w * 0.2).max(10.0); let (rx, ry) = (x + 4.0 + rr, y + 14.0 + (h - 26.0) / 2.0); s.push(fill(rx - rr, ry - rr, rr * 2.0, rr * 2.0, 0x60103018)); let seg = if rr > 30.0 { 32 } else { 20 }; for k in [0.5f32, 1.0] { for i in 0..seg { let a0 = i as f32 / seg as f32 * std::f32::consts::TAU; let a1 = (i + 1) as f32 / seg as f32 * std::f32::consts::TAU; let p = |a: f32, r: f32| (rx + a.sin() * r, ry - a.cos() * r); let r = rr * k; s.push(quad(p(a0, r - 0.4), p(a0, r + 0.4), p(a1, r + 0.4), p(a1, r - 0.4), 0x9040FF60)); } } // A system that only fires (no radar) has no reach: a radar's sensor // range stands in, and nothing is divided by zero. let reach = if optical { crate::sim::OPTICAL_RANGE } else { match sam.kind.range() { r if r > 0.0 => r, _ => spec.sensor, } }; let scale = if reach > 0.0 { rr as f64 / (reach * 1.25) } else { 0.0 }; // The beam is the radar's real one: targets are measured as it passes them. let sweep = sam.beam() as f32; let radar_up = sam.radar_up(); for k in 0..if radar_up { 6 } else { 0 } { let a1 = sweep - k as f32 * 0.09; let a0 = a1 - 0.09; let p = |a: f32| (rx + a.sin() * rr, ry - a.cos() * rr); s.push(quad((rx, ry), p(a0), p(a1), p(a1), argb(0x40FF70, (0x70 - k * 10) as u32))); } let target = sim.watched(sam); let mut seen = 0; // Only what its own radar holds. for (d, tr) in sim.drones.iter().filter(|d| d.alive()).filter_map(|d| sam.track(d.id).map(|t| (d, t))) { // Where the system believes it is: the last measurement carried on; a forecast is drawn hollow. let est = tr.est(tick); if sim.truth { let r = d.pos - sam.pos; s.push(fill(rx + r.x as f32 * scale as f32 - 0.5, ry + r.z as f32 * scale as f32 - 0.5, 1.0, 1.0, 0xFFFFFFFF)); } let rel = est - sam.pos; let flat = (rel.x * rel.x + rel.z * rel.z).sqrt(); if reach <= 0.0 || flat > reach * 1.25 { continue; } seen += 1; let k = scale as f32; let (bx, by) = (rx + rel.x as f32 * k, ry + rel.z as f32 * k); // By the class the track was estimated (#133): the unknown are candidates too, shown in // their own colour — the explicit policy — never the target's own class. No roles means // holding fire, and that greys out the unknown as well. let color = if Some(d.id) == target.map(|t| t.id) { 0xFFFFE040 } else if !tr.class.in_roles(sam.roles) { 0xFF808080 } else if tr.class == ClassEst::Unknown { 0xFFFFA040 } else { 0xFFFF4040 }; if tr.forecast(tick) { s.push(fill(bx - 1.5, by - 1.5, 3.0, 1.0, color)); s.push(fill(bx - 1.5, by + 0.5, 3.0, 1.0, color)); s.push(fill(bx - 1.5, by - 0.5, 1.0, 1.0, color)); s.push(fill(bx + 0.5, by - 0.5, 1.0, 1.0, color)); } else { s.push(fill(bx - 1.5, by - 1.5, 3.0, 3.0, color)); } } s.push(fill(rx - 1.5, ry - 1.5, 3.0, 3.0, 0xFF40A0FF)); let radar_text = if optical { format!("ОПТИКА · {}", sam.optical.state().name()) } else if spec.sensor <= 0.0 { format!("сеть · {seen}") } else if radar_up { let profile = sam.radar_profile(); let channels = if profile.channels == 0 { String::new() } else { format!(" · {}/{}", sam.own_channels_used(tick), profile.channels) }; format!("{} · {} · {}{channels}", profile.name, distance(reach), seen) } else { "РЛС ОТКАЗ".to_string() }; let radar_color = if optical { match sam.optical.state() { OpticalState::Observing => 0xFF60FF80, OpticalState::Holding => 0xFF9FE0FF, OpticalState::NoSight => 0xFFFFA040, OpticalState::Searching => 0xFFB0B0B0, } } else if spec.sensor > 0.0 && !radar_up { 0xFFFF5040 } else { 0xFF80FF90 }; s.push(text(fit(radar_text, rr * 2.0 + 4.0), x + 4.0, y + h - 10.0, radar_color)); // Its health under the radar: a bar, red when it is low. if sam.alive() && sam.health < 1.0 { let bw = rr * 2.0; let hc = if sam.health < 0.35 { 0xFFFF3030 } else if sam.health < 0.7 { 0xFFFFB030 } else { 0xFF60FF60 }; s.push(fill(x + 4.0, ry + rr + 1.0, bw, 2.0, 0x80000000)); s.push(fill(x + 4.0, ry + rr + 1.0, bw * sam.health as f32, 2.0, hc)); } // The camera, at the right: the world as the system sees it, thermal. let cx = rx + rr + 6.0; let (cy, cw, ch) = (y + 15.0, x + w - 4.0 - cx, h - 28.0); if !sam.alive() { // A dead system shows a dead screen. s.push(fill(cx, cy, cw, ch, 0xFF100606)); let noise = (tick * 2654435761) % 997; for i in 0..24 { let nx = cx + ((noise as f32 * (i + 3) as f32 * 13.7) % cw); let ny = cy + ((noise as f32 * (i + 7) as f32 * 7.3) % ch); s.push(fill(nx, ny, 3.0, 1.0, 0x60FFFFFF)); } s.push(text("НЕТ СВЯЗИ", cx + cw / 2.0 - render::text_width("НЕТ СВЯЗИ") / 2.0, cy + ch / 2.0 - 4.0, 0xFFFF4040)); s.push(text(if sam.burning() { "горит" } else { "уничтожен" }, cx, y + h - 10.0, 0xFFFF8060)); return; } s.push(fill(cx - 1.0, cy - 1.0, cw + 2.0, ch + 2.0, 0xFF203020)); // Searching: colour by day, night vision at night; tracking: thermal. let night = horizon_mod_sdk::game::get_clock().is_some_and(|c| (13000..23000).contains(&(c.day_time % 24000))); let mode = match (target.is_some(), night) { (true, _) => 1, (false, true) => 2, (false, false) => 0, }; s.push(Shape::View(ViewShape { view: sam.id, rect: Rect { x: cx, y: cy, w: cw, h: ch }, mode })); // Worn or failed parts, over the picture: orange, red when one has failed. if !sam.comps.whole() { let failed = crate::sim::Part::ALL.iter().any(|p| sam.part_state(*p) == crate::sim::PartState::Failed); s.push(text(fit(crate::why_view::parts(&sam.comps, sam.kind), cw * 0.55), cx + 3.0, cy + 3.0, if failed { 0xFFFF4040 } else { 0xFFFFB030 })); } // Crosshair. s.push(fill(cx + cw / 2.0 - 0.5, cy + 3.0, 1.0, ch * 0.18, 0x90FFFFFF)); s.push(fill(cx + cw / 2.0 - 0.5, cy + ch * 0.82 - 3.0, 1.0, ch * 0.18, 0x90FFFFFF)); s.push(fill(cx + 3.0, cy + ch / 2.0 - 0.5, cw * 0.18, 1.0, 0x90FFFFFF)); s.push(fill(cx + cw * 0.82 - 3.0, cy + ch / 2.0 - 0.5, cw * 0.18, 1.0, 0x90FFFFFF)); match target { Some(_) if optical => { // The optical post's picture: its own class observation, never the true model name. let (color, info) = match sam.optical.state() { OpticalState::Observing => (0xFF60FF80, sam.optical.status_text()), OpticalState::Holding => (0xFF9FE0FF, sam.optical.status_text()), OpticalState::NoSight => (0xFFFFA040, sam.optical.status_text()), OpticalState::Searching => (0xFFB0B0B0, sam.optical.status_text()), }; let (bw, bh) = (cw * 0.3, ch * 0.34); brackets(s, cx + cw / 2.0 - bw / 2.0, cy + ch / 2.0 - bh / 2.0, bw, bh, 6.0, color); if ch > 50.0 { s.push(text(fit(info.clone(), cw - 6.0), cx + 3.0, cy + ch - 20.0, color)); } s.push(text(fit(info, cw - 6.0), cx + 3.0, cy + ch - 10.0, 0xFFC0FFC0)); } Some(t) => { let color = if lock { if blink { 0xFFFF3030 } else { 0xFFFF8080 } } else { 0xFFFFE060 }; let (bw, bh) = (cw * 0.3, ch * 0.34); brackets(s, cx + cw / 2.0 - bw / 2.0, cy + ch / 2.0 - bh / 2.0, bw, bh, 6.0, color); if lock { s.push(text("ЗАХВАТ", cx + cw / 2.0 - 18.0, cy + 3.0, color)); } if !t.alive() || sim.showing_kill(sam) { s.push(text("ПОРАЖЕНА", cx + cw / 2.0 - 24.0, cy + ch / 2.0 + bh / 2.0 + 2.0, 0xFF60FF60)); } // The system knows the target by its track: the forecast of the last measurement. let track = sam.track(t.id); let at = track.map_or(t.pos, |tr| tr.est(tick)); let rel = at - sam.pos; let bearing = (heading_of(rel).to_degrees() + 180.0).rem_euclid(360.0); if ch > 50.0 { // What the system knows it by: the estimated class; the sandbox's truth names the model. let name = if sim.truth { t.kind.spec().name.to_string() } else { track.map_or_else(|| "тип не определён".into(), |tr| tr.class.name()) }; s.push(text(fit(name, cw - 6.0), cx + 3.0, cy + ch - 20.0, 0xFFFFFFFF)); } // The speed its own measurements showed, never the target's own figures. let mut info = format!("{} в{:.0} {:.0}км/ч", distance(rel.length()), at.y - sam.pos.y, track.map_or(0.0, |tr| tr.vel.length()) * 72.0); if let Some(tr) = track { if tr.forecast(tick) { info += &format!(" прогноз {:.1}с", tr.age(tick) as f64 / 20.0); } else if cw > 200.0 { info += &format!(" {}", tr.ident().name()); } } if cw > 220.0 { info += &format!(" П{bearing:03.0}"); } s.push(text(fit(info, cw - 6.0), cx + 3.0, cy + ch - 10.0, 0xFFC0FFC0)); } None if optical => s.push(text(fit(sam.optical.status_text(), cw - 6.0), cx + 3.0, cy + ch - 10.0, 0xFFB0B0B0)), None => s.push(text("ПОИСК", cx + 3.0, cy + ch - 10.0, 0xFF80FF90)), } // What it carries and does. let mut line = String::new(); if optical { line = sam.optical.status_text(); } else { if !matches!(sam.kind, SamKind::Gepard) { line += &format!("Р{} ", sam.missiles); if sam.kind == SamKind::S400 { line += &format!("У{} ", sam.ground_rounds); } } if crate::sim::catalog::unit(sam.kind).has_gun() { line += &format!("Оч{} ", sam.bursts); } line += &format!("сб.{} [{}]", sam.kills, roles_text(sam.roles)); } s.push(text(fit(line, cw), cx, y + h - 10.0, 0xFF9FE0FF)); } /// One system, top left. pub fn draw(sim: &Sim, index: usize) -> Vec { let mut s = Vec::new(); cell(sim, index, 4.0, 4.0, 300.0, 150.0, &mut s); // Why it fires or doesn't: the conditions the simulation decides by, worked out now for this one. let checks = sim.why(index); s.push(fill(4.0, 156.0, 300.0, 4.0 + 10.0 * checks.len() as f32, 0xA0080C0A)); for (k, c) in checks.iter().enumerate() { let color = if c.ok { 0xFF80FF90 } else { 0xFFFF9060 }; s.push(text(fit(format!("{} {}", if c.ok { "+" } else { "-" }, crate::why_view::reason(&c.reason)), 294.0), 7.0, 158.0 + 10.0 * k as f32, color)); } s } /// Every system in a grid over the screen. pub fn grid(sim: &Sim, picked: &[usize], (gw, gh): (f32, f32)) -> Vec { let mut s = Vec::new(); let n = picked.len().min(8); s.push(fill(0.0, 0.0, gw, gh, 0xE0050806)); s.push(text( fit( { let total = sim.sams.len(); let busy = sim .sams .iter() .filter(|s| !matches!(s.state, SamState::Search | SamState::Hold | SamState::Destroyed)) .count(); let lost = sim.sams.iter().filter(|s| !s.alive()).count(); let shown = if total > n { format!("показано {n} из {total}") } else { format!("комплексов {total}") }; format!( "КОМАНДНЫЙ ПУНКТ ПВО · {shown} · в работе {busy} · потеряно {lost} · целей {} · сбито {} · J закрыть", sim.drones.iter().filter(|d| d.alive()).count(), sim.kills ) }, gw - 16.0, ), 8.0, 6.0, 0xFF9FE0FF, )); if n == 0 { s.push(text("Нет комплексов: поставьте их пультами развёртывания", 8.0, 24.0, 0xFFFFC060)); return s; } let cols = match n { 1 => 1, 2..=4 => 2, _ => 3, }; let rows = n.div_ceil(cols); let (top, gap) = (18.0, 4.0); let cw = (gw - gap * (cols as f32 + 1.0)) / cols as f32; let ch = ((gh - top - gap * rows as f32) / rows as f32).min(cw * 0.62); for (k, &i) in picked.iter().take(n).enumerate() { let (c, r) = (k % cols, k / cols); let x = gap + c as f32 * (cw + gap); let y = top + r as f32 * (ch + gap); cell(sim, i, x, y, cw, ch, &mut s); } s }