502d94c528
The overlay clipped the network's tracks to the nearest 64 by distance before projecting them, and the truth and interceptor layers the same way. A crowd behind the camera filled the budget, so a raid could show nothing even with a target right in front of the player; the native reproduction (one contact ahead, 64 behind) came out with zero markers and a header saying all 65 were out of frame. Now visibility comes before the draw budget. Each layer orders its candidates nearest first, projects at most MAX_SCAN = 4 x MAX_MARKERS of them (bounded work, no sort of the whole picture beyond that prefix), skips those the frame does not hold without spending a marker, and draws the nearest MAX_MARKERS visible ones. Each layer keeps its own draw budget, so the interceptors and the truth markers cannot crowd out the network's. The header separates the two ways a target stays unmarked: "за кадром" (behind the camera or past an edge) and "сверх бюджета" (past the scan or draw budget), alongside "в кадре". Regression tests: 64 behind plus one ahead for the network, truth and interceptor layers; a mixed crowd proving the nearest visible keep the markers and the farthest is budget-capped, not offscreen; the projection count bounded by MAX_SCAN; every case stays inside the HUD's 1024 shapes. Co-Authored-By: Kimi Code <noreply@moonshot.ai>
449 lines
23 KiB
Rust
449 lines
23 KiB
Rust
//! The world overlay (#132): while the player holds the overlay key, square markers over the
|
||
//! targets they can see, each with a short line — the class the radars estimated (#133) and the
|
||
//! distance. The picture is the network's, as the radar and the map read it; the sandbox's truth
|
||
//! mode adds white markers of the real drones, labelled with their actual type, apart from the
|
||
//! network's. The player's own interceptors wear small yellow squares, as the radar shows them.
|
||
//! Letting the key go hides everything. The markers are projected through the camera the last
|
||
//! drawn frame used (`render.project`, API 31), so they sit on the world as it is drawn.
|
||
//! Visibility comes before the draw budget (#141): each layer projects at most a bounded number
|
||
//! of its nearest candidates, keeps the markers of the nearest visible ones, and counts a target
|
||
//! that leaves the frame without spending a slot.
|
||
use crate::panel::distance;
|
||
use crate::sim::{ClassEst, Sim, Track};
|
||
use glam::DVec3;
|
||
use horizon_mod_sdk::render::{FillShape, Point, Rect, Shape, TextShape};
|
||
|
||
/// The most markers of one picture (the network's, the truth's and the interceptors' each): a full
|
||
/// raid still fits the HUD's 1024 shapes — a marker is four sides and a label.
|
||
const MAX_MARKERS: usize = 64;
|
||
/// The most candidates of one layer the frame looks at: the nearest ones by distance. The draw
|
||
/// budget is spent on what is in frame, but the projection work stays bounded — a crowd the frame
|
||
/// does not hold cannot buy an unbounded scan, and what the scan never reaches counts as past the
|
||
/// budget rather than out of the frame.
|
||
const MAX_SCAN: usize = 4 * MAX_MARKERS;
|
||
/// A marker's side thickness, pixels.
|
||
const LINE: f32 = 1.0;
|
||
/// A label line's height (the HUD font with its shadow).
|
||
const LH: f32 = 9.0;
|
||
/// The half-size a marker clamps to, near and far.
|
||
const HALF_MIN: f32 = 5.0;
|
||
const HALF_MAX: f32 = 16.0;
|
||
|
||
const RED: u32 = 0xFF4040;
|
||
const ORANGE: u32 = 0xFFB040;
|
||
const WHITE: u32 = 0xFFFFFFFF;
|
||
const GREEN: u32 = 0xFF80FF90;
|
||
const YELLOW: u32 = 0xFFFFE070;
|
||
/// The interceptors' own colour (the radar's missile dot).
|
||
const MISSILE: u32 = 0xFFE060;
|
||
/// An interceptor's marker half-size: small, it is not a target.
|
||
const MISSILE_HALF: f32 = 3.0;
|
||
|
||
fn pt(x: f32, y: f32) -> Point {
|
||
Point { x, y }
|
||
}
|
||
|
||
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 square outline around a screen point: four thin fills, the world's marker.
|
||
fn square_outline(x: f32, y: f32, half: f32, color: u32) -> Vec<Shape> {
|
||
let (w, h) = (half * 2.0, LINE);
|
||
vec![
|
||
Shape::Fill(FillShape { rect: Rect { x: x - half, y: y - half, w, h }, color }),
|
||
Shape::Fill(FillShape { rect: Rect { x: x - half, y: y + half - LINE, w, h }, color }),
|
||
Shape::Fill(FillShape { rect: Rect { x: x - half, y: y - half, w: h, h: w }, color }),
|
||
Shape::Fill(FillShape { rect: Rect { x: x + half - LINE, y: y - half, w: h, h: w }, color }),
|
||
]
|
||
}
|
||
|
||
/// A marker's half-size by the distance: near targets read bigger, far ones stay legible.
|
||
fn half(dist: f64) -> f32 {
|
||
(700.0 / dist.max(1.0)).clamp(HALF_MIN as f64, HALF_MAX as f64) as f32
|
||
}
|
||
|
||
/// The short line under a network marker: the estimated class's mark (#133) and the distance to
|
||
/// the forecast place; a forecast says how old it is. An unidentified track gets a `?`, never the
|
||
/// target's type.
|
||
fn track_label(t: &Track, now: u64, player: DVec3) -> String {
|
||
let mark = match t.class {
|
||
ClassEst::Unknown => "?",
|
||
c => c.mark(),
|
||
};
|
||
let mut s = format!("{mark} {}", distance(t.est(now).distance(player)));
|
||
if t.forecast(now) {
|
||
s += &format!(" ~{}с", t.age(now) / 20);
|
||
}
|
||
s
|
||
}
|
||
|
||
/// Where a marker's label sits: under the square, or over it when the bottom edge is close.
|
||
fn label_y(y: f32, half: f32, gui_h: f32, above: bool) -> f32 {
|
||
if above {
|
||
if y - half - LH - 2.0 >= 0.0 { y - half - LH - 2.0 } else { y + half + 2.0 }
|
||
} else if y + half + LH + 2.0 <= gui_h {
|
||
y + half + 2.0
|
||
} else {
|
||
y - half - LH - 2.0
|
||
}
|
||
}
|
||
|
||
/// What one layer shows this frame: the markers to draw (the nearest visible candidates), how
|
||
/// many of the scanned candidates left the frame, and how many the budgets left out.
|
||
struct Pick<T> {
|
||
marks: Vec<(T, Point, f32)>,
|
||
offscreen: usize,
|
||
capped: usize,
|
||
}
|
||
|
||
/// The markers one layer gets this frame. The candidates are ordered nearest the player first and
|
||
/// at most [`MAX_SCAN`] of them are projected; a candidate the frame does not hold (behind the
|
||
/// camera or past an edge) is counted and its slot stays free for the next, so the nearest
|
||
/// [`MAX_MARKERS`] visible candidates are drawn. Work stays bounded by the scan, not the picture.
|
||
fn pick<T: Copy>(
|
||
items: impl Iterator<Item = T>,
|
||
at: impl Fn(T) -> DVec3,
|
||
half_at: impl Fn(T) -> f32,
|
||
player: DVec3,
|
||
gui: (f32, f32),
|
||
project: &impl Fn(DVec3) -> Option<Point>,
|
||
) -> Pick<T> {
|
||
let near = |a: &T, b: &T| at(*a).distance(player).total_cmp(&at(*b).distance(player));
|
||
let mut items: Vec<T> = items.collect();
|
||
let total = items.len();
|
||
// Only the nearest candidates are ever projected: no sort of the whole picture is needed.
|
||
if items.len() > MAX_SCAN {
|
||
items.select_nth_unstable_by(MAX_SCAN, near);
|
||
items.truncate(MAX_SCAN);
|
||
}
|
||
items.sort_by(near);
|
||
let mut marks = Vec::new();
|
||
let mut offscreen = 0usize;
|
||
for it in items {
|
||
if marks.len() >= MAX_MARKERS {
|
||
break;
|
||
}
|
||
let h = half_at(it);
|
||
match project(at(it)) {
|
||
Some(p) if p.x >= h && p.x <= gui.0 - h && p.y >= h && p.y <= gui.1 - h => marks.push((it, p, h)),
|
||
_ => offscreen += 1,
|
||
}
|
||
}
|
||
Pick { capped: total - marks.len() - offscreen, marks, offscreen }
|
||
}
|
||
|
||
/// The overlay's shapes for this frame. `project` is the game's `render.project` (the last drawn
|
||
/// frame's camera); a point behind the camera projects to None, a point off the screen still
|
||
/// projects past its edges — both are counted, not drawn. Drawn only while the key is held.
|
||
pub fn draw(sim: &Sim, player: DVec3, gui: (f32, f32), project: impl Fn(DVec3) -> Option<Point>) -> Vec<Shape> {
|
||
let mut s = Vec::new();
|
||
if gui.0 <= 0.0 || gui.1 <= 0.0 {
|
||
return s;
|
||
}
|
||
let pic = sim.air_picture();
|
||
let now = pic.now;
|
||
let truth = sim.truth && !pic.unaided;
|
||
// The frame's counts: what got a marker and what did not — out of the frame (behind the camera
|
||
// or past an edge) or past the budget. The header keeps the two apart (#141).
|
||
let mut shown = 0usize;
|
||
let mut offscreen = 0usize;
|
||
let mut capped = 0usize;
|
||
// The network's tracks: the nearest visible keep the markers; a target behind the camera no
|
||
// longer spends one, so a crowd behind cannot crowd out the target in frame.
|
||
let net = pick(pic.tracks.iter(), |t| t.est(now), |t| half(t.est(now).distance(player)), player, gui, &project);
|
||
offscreen += net.offscreen;
|
||
capped += net.capped;
|
||
for (t, p, h) in net.marks {
|
||
// Fresh: red; a forecast: orange, fading as it ages towards the track's drop (the radar's colours).
|
||
let (color, a) = if pic.unaided {
|
||
(RED, 1.0)
|
||
} else if t.forecast(now) {
|
||
(ORANGE, (1.0 - t.age(now) as f64 / t.ttl.max(1) as f64 * 0.7).max(0.3))
|
||
} else {
|
||
(RED, 1.0)
|
||
};
|
||
let color = alpha(color, a);
|
||
s.extend(square_outline(p.x, p.y, h, color));
|
||
s.push(text(track_label(t, now, player), p.x, label_y(p.y, h, gui.1, false), color, true));
|
||
shown += 1;
|
||
}
|
||
// The truth's own markers: the real drones, even the ones no radar holds, white and labelled
|
||
// with the actual type the network never shows. A tracked drone wears both: the network's
|
||
// estimate under the square, the truth over it — the offset between them is the point.
|
||
if truth {
|
||
let real = pick(
|
||
sim.drones.iter().filter(|d| d.alive()),
|
||
|d| d.pos,
|
||
|d| half(d.pos.distance(player)) + 2.0,
|
||
player,
|
||
gui,
|
||
&project,
|
||
);
|
||
offscreen += real.offscreen;
|
||
capped += real.capped;
|
||
for (d, p, h) in real.marks {
|
||
s.extend(square_outline(p.x, p.y, h, WHITE));
|
||
let label = format!("{} {}", d.kind.spec().name, distance(d.pos.distance(player)));
|
||
s.push(text(label, p.x, label_y(p.y, h, gui.1, true), WHITE, true));
|
||
shown += 1;
|
||
}
|
||
}
|
||
// The player's own interceptors: small yellow squares, no label — they are not targets, and
|
||
// the network never hid them (the radar has always shown them). They keep their own budget.
|
||
let own = pick(sim.missiles.iter(), |m| m.pos, |_| MISSILE_HALF, player, gui, &project);
|
||
for (_, p, h) in own.marks {
|
||
s.extend(square_outline(p.x, p.y, h, MISSILE));
|
||
}
|
||
// The mode, named as the map names it: the network's data by default, the sandbox's truth
|
||
// explicitly marked. The counts say what the frame holds, what left it, and what the budget cut.
|
||
let counts = format!("в кадре {shown}, за кадром {offscreen}, сверх бюджета {capped}");
|
||
let head = if truth {
|
||
format!("ОБЗОР · ИСТИНА (песочница): белые рамки — реальные дроны · {counts}")
|
||
} else {
|
||
format!("ОБЗОР · данные сети РЛС · {counts}")
|
||
};
|
||
s.push(text(head, gui.0 / 2.0, 2.0, if truth { YELLOW } else { GREEN }, true));
|
||
s
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use crate::sim::{DroneKind, Out, Sam, SamKind};
|
||
|
||
const GUI: (f32, f32) = (800.0, 600.0);
|
||
|
||
/// A projector as the game's: the world ahead lands on the screen, behind the camera None.
|
||
/// Here: south (+z) is ahead, x spreads across the screen, the height lines up at 64.
|
||
fn ahead(p: DVec3) -> Option<Point> {
|
||
if p.z <= 0.0 {
|
||
return None;
|
||
}
|
||
Some(pt(400.0 + p.x as f32, 300.0 - (p.y - 64.0) as f32))
|
||
}
|
||
|
||
fn sim_with(kind: DroneKind, at: DVec3) -> Sim {
|
||
let mut sim = Sim::new(1);
|
||
sim.launch(kind, at, DVec3::ZERO, DVec3::new(at.x, 64.0, 5000.0), &mut Out::default());
|
||
sim
|
||
}
|
||
|
||
fn fills(shapes: &[Shape], rgb: u32) -> usize {
|
||
shapes.iter().filter(|s| matches!(s, Shape::Fill(f) if f.color & 0xFFFFFF == rgb & 0xFFFFFF)).count()
|
||
}
|
||
|
||
fn texts(shapes: &[Shape]) -> Vec<&TextShape> {
|
||
shapes.iter().filter_map(|s| match s {
|
||
Shape::Text(t) => Some(t),
|
||
_ => None,
|
||
}).collect()
|
||
}
|
||
|
||
fn head(shapes: &[Shape]) -> String {
|
||
texts(shapes).into_iter().find(|t| t.text.starts_with("ОБЗОР")).expect("no header").text.clone()
|
||
}
|
||
|
||
/// `n` targets in a row right behind the camera, each nearer than anything the tests put ahead.
|
||
fn crowd_behind(sim: &mut Sim, n: u32) {
|
||
for i in 0..n {
|
||
sim.launch(DroneKind::Shahed, DVec3::new(i as f64, 64.0, -50.0), DVec3::ZERO, DVec3::new(0.0, 64.0, -5000.0), &mut Out::default());
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn a_visible_target_gets_a_square_and_a_short_line() {
|
||
let sim = sim_with(DroneKind::Shahed, DVec3::new(0.0, 64.0, 100.0));
|
||
let shapes = draw(&sim, DVec3::new(0.0, 64.0, 0.0), GUI, ahead);
|
||
// One marker: four red sides, and a label with the firm class's mark and the distance.
|
||
assert_eq!(fills(&shapes, RED), 4, "the marker is not one square outline");
|
||
let label = texts(&shapes).into_iter().find(|t| t.text.contains("100 м")).expect("no distance label");
|
||
assert!(label.text.starts_with("Д "), "the firm drone class is not marked: {}", label.text);
|
||
assert!(label.centered && label.at.y > 300.0, "the label sits under the square");
|
||
// The header names the network's data and the counts.
|
||
let head = texts(&shapes).into_iter().find(|t| t.text.starts_with("ОБЗОР")).unwrap();
|
||
assert!(head.text.contains("данные сети РЛС") && head.text.contains("в кадре 1, за кадром 0"), "{}", head.text);
|
||
}
|
||
|
||
#[test]
|
||
fn behind_the_camera_and_off_screen_are_counted_not_drawn() {
|
||
let mut sim = sim_with(DroneKind::Shahed, DVec3::new(0.0, 64.0, 100.0));
|
||
sim.launch(DroneKind::Shahed, DVec3::new(0.0, 64.0, -100.0), DVec3::ZERO, DVec3::new(0.0, 64.0, -5000.0), &mut Out::default());
|
||
let shapes = draw(&sim, DVec3::ZERO, GUI, ahead);
|
||
assert_eq!(fills(&shapes, RED), 4, "the drone behind the camera drew a marker");
|
||
let head = texts(&shapes).into_iter().find(|t| t.text.starts_with("ОБЗОР")).unwrap();
|
||
assert!(head.text.contains("в кадре 1, за кадром 1"), "{}", head.text);
|
||
// Off the screen's edge: still a place, but no marker.
|
||
let shapes = draw(&sim, DVec3::ZERO, GUI, |_| Some(pt(-40.0, 300.0)));
|
||
assert_eq!(fills(&shapes, RED), 0);
|
||
let head = texts(&shapes).into_iter().find(|t| t.text.starts_with("ОБЗОР")).unwrap();
|
||
assert!(head.text.contains("в кадре 0, за кадром 2"), "{}", head.text);
|
||
}
|
||
|
||
#[test]
|
||
fn the_label_never_leaks_the_type_of_an_unidentified_track() {
|
||
let t = Track { id: 1, class: ClassEst::Unknown, pos: DVec3::new(0.0, 64.0, 100.0), vel: DVec3::ZERO, tick: 0, own_tick: 0, source: 0, product: crate::sim::battery::Measure::Search, quality: 40, own_product: crate::sim::battery::Measure::Search, own_quality: 0, ttl: 200, hits: 1, locked: false };
|
||
let label = track_label(&t, 0, DVec3::ZERO);
|
||
assert!(label.starts_with("? "), "{label}");
|
||
assert!(!label.contains("дрон") && !label.contains("Шахед"), "{label}");
|
||
// A probable class keeps its question mark; a firm one loses it.
|
||
let t = Track { class: ClassEst::Probable(crate::sim::Class::Cruise), ..t };
|
||
assert!(track_label(&t, 0, DVec3::ZERO).starts_with("К? "));
|
||
let t = Track { class: ClassEst::Firm(crate::sim::Class::Cruise), hits: 3, ..t };
|
||
assert!(track_label(&t, 0, DVec3::ZERO).starts_with("К "));
|
||
// A forecast says its age.
|
||
let label = track_label(&t, 100, DVec3::ZERO);
|
||
assert!(label.contains("~5с"), "{label}");
|
||
}
|
||
|
||
#[test]
|
||
fn the_truth_mode_marks_the_real_drones_apart_from_the_network() {
|
||
let mut sim = sim_with(DroneKind::Shahed, DVec3::new(0.0, 64.0, 100.0));
|
||
let id = sim.id();
|
||
sim.sams.push(Sam::new(id, SamKind::Buk, DVec3::new(0.0, 64.0, 0.0), 0.0));
|
||
// Without the truth mode: no white markers, no actual type anywhere.
|
||
let shapes = draw(&sim, DVec3::ZERO, GUI, ahead);
|
||
assert_eq!(fills(&shapes, WHITE), 0);
|
||
assert!(!texts(&shapes).iter().any(|t| t.text.contains("Шахед")), "the network overlay leaked the type");
|
||
// With it: a white square with the real type, and the header says whose picture it is.
|
||
sim.truth = true;
|
||
let shapes = draw(&sim, DVec3::new(0.0, 64.0, 0.0), GUI, ahead);
|
||
assert_eq!(fills(&shapes, WHITE), 4, "the truth marker is not one white square");
|
||
let label = texts(&shapes).into_iter().find(|t| t.text.contains("Шахед-136")).expect("no truth label");
|
||
assert!(label.text.contains("100 м") && label.at.y < 300.0, "the truth label sits over the square: {label:?}");
|
||
let head = texts(&shapes).into_iter().find(|t| t.text.starts_with("ОБЗОР")).unwrap();
|
||
assert!(head.text.contains("ИСТИНА (песочница)"), "{}", head.text);
|
||
// The truth mode with no system deployed adds nothing: the sky as it is was already shown.
|
||
sim.sams.clear();
|
||
let shapes = draw(&sim, DVec3::ZERO, GUI, ahead);
|
||
assert_eq!(fills(&shapes, WHITE), 0);
|
||
}
|
||
|
||
#[test]
|
||
fn a_full_raid_stays_inside_the_huds_shape_budget() {
|
||
let mut sim = Sim::new(1);
|
||
sim.raid(DroneKind::Shahed, DVec3::new(0.0, 64.0, 0.0), 80, 300.0, &mut Out::default());
|
||
sim.truth = true;
|
||
let shapes = draw(&sim, DVec3::ZERO, GUI, ahead);
|
||
assert!(shapes.len() <= 1024, "the overlay overflowed the HUD: {} shapes", shapes.len());
|
||
// The nearest markers were kept, the rest counted.
|
||
let head = texts(&shapes).into_iter().find(|t| t.text.starts_with("ОБЗОР")).unwrap();
|
||
assert!(head.text.contains("за кадром"), "{}", head.text);
|
||
assert!(fills(&shapes, RED) / 4 <= MAX_MARKERS);
|
||
}
|
||
|
||
/// #141: 64 targets behind the camera and one in frame. The budget used to be spent on the
|
||
/// crowd before the visibility check, so the picture came out empty; the target in frame must
|
||
/// keep its marker, and the crowd is out of frame, not past the budget.
|
||
#[test]
|
||
fn a_crowd_behind_the_camera_leaves_the_network_budget_to_the_target_in_frame() {
|
||
let mut sim = Sim::new(1);
|
||
crowd_behind(&mut sim, 64);
|
||
sim.launch(DroneKind::Shahed, DVec3::new(0.0, 64.0, 100.0), DVec3::ZERO, DVec3::new(0.0, 64.0, 5000.0), &mut Out::default());
|
||
let shapes = draw(&sim, DVec3::new(0.0, 64.0, 0.0), GUI, ahead);
|
||
assert_eq!(fills(&shapes, RED), 4, "the target in frame got no marker");
|
||
assert_eq!(head(&shapes), "ОБЗОР · данные сети РЛС · в кадре 1, за кадром 64, сверх бюджета 0");
|
||
assert!(shapes.len() <= 1024, "{} shapes", shapes.len());
|
||
}
|
||
|
||
/// The truth layer keeps its own budget the same way: the real drone in frame is drawn, the
|
||
/// crowd behind the camera costs no white marker.
|
||
#[test]
|
||
fn a_crowd_behind_the_camera_leaves_the_truth_budget_to_the_target_in_frame() {
|
||
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.truth = true;
|
||
crowd_behind(&mut sim, 64);
|
||
sim.launch(DroneKind::Shahed, DVec3::new(0.0, 64.0, 100.0), DVec3::ZERO, DVec3::new(0.0, 64.0, 5000.0), &mut Out::default());
|
||
let shapes = draw(&sim, DVec3::new(0.0, 64.0, 0.0), GUI, ahead);
|
||
assert_eq!(fills(&shapes, WHITE), 4, "the drone in frame got no truth marker");
|
||
assert_eq!(fills(&shapes, RED), 0, "the network drew a track it does not hold");
|
||
assert_eq!(head(&shapes), "ОБЗОР · ИСТИНА (песочница): белые рамки — реальные дроны · в кадре 1, за кадром 64, сверх бюджета 0");
|
||
assert!(shapes.len() <= 1024, "{} shapes", shapes.len());
|
||
}
|
||
|
||
/// The interceptors keep their own budget too: a salvo behind the camera leaves the marker
|
||
/// to the one in frame.
|
||
#[test]
|
||
fn a_crowd_behind_the_camera_leaves_the_interceptor_budget_to_the_one_in_frame() {
|
||
let mut sim = Sim::new(1);
|
||
for i in 0..64 {
|
||
sim.missiles.push(crate::sim::Missile::test_at(i + 1, DVec3::new(i as f64, 64.0, -50.0), 0));
|
||
}
|
||
sim.missiles.push(crate::sim::Missile::test_at(100, DVec3::new(0.0, 64.0, 100.0), 0));
|
||
let shapes = draw(&sim, DVec3::new(0.0, 64.0, 0.0), GUI, ahead);
|
||
assert_eq!(fills(&shapes, MISSILE), 4, "the interceptor in frame got no marker");
|
||
assert!(shapes.len() <= 1024, "{} shapes", shapes.len());
|
||
}
|
||
|
||
/// The header tells what really left the frame from what the draw budget cut, and the markers
|
||
/// go to the nearest of the visible: 64 behind the camera, 65 in frame.
|
||
#[test]
|
||
fn the_header_separates_the_frames_edge_from_the_draw_budget() {
|
||
let mut sim = Sim::new(1);
|
||
crowd_behind(&mut sim, 64);
|
||
for i in 0..65 {
|
||
let z = 100.0 + i as f64 * 10.0;
|
||
sim.launch(DroneKind::Shahed, DVec3::new(0.0, 64.0, z), DVec3::ZERO, DVec3::new(0.0, 64.0, 5000.0), &mut Out::default());
|
||
}
|
||
let shapes = draw(&sim, DVec3::new(0.0, 64.0, 0.0), GUI, ahead);
|
||
// The 64 nearest in frame are drawn; the farthest one is past the draw budget, not out of frame.
|
||
assert_eq!(fills(&shapes, RED), 4 * MAX_MARKERS);
|
||
assert!(texts(&shapes).iter().any(|t| t.text.contains("100 м")), "the nearest visible lost its marker");
|
||
assert!(!texts(&shapes).iter().any(|t| t.text.contains("740 м")), "the farthest visible took a nearer one's marker");
|
||
assert_eq!(head(&shapes), "ОБЗОР · данные сети РЛС · в кадре 64, за кадром 64, сверх бюджета 1");
|
||
assert!(shapes.len() <= 1024, "{} shapes", shapes.len());
|
||
}
|
||
|
||
/// The projection work itself is bounded: with more candidates than the scan budget, only the
|
||
/// nearest `MAX_SCAN` are looked at, and the rest are counted as past the budget.
|
||
#[test]
|
||
fn the_projection_work_is_bounded_by_the_scan_budget() {
|
||
let mut sim = Sim::new(1);
|
||
for i in 0..1000 {
|
||
sim.launch(DroneKind::Shahed, DVec3::new((i % 100) as f64, 64.0, -50.0), DVec3::ZERO, DVec3::new(0.0, 64.0, -5000.0), &mut Out::default());
|
||
}
|
||
let calls = std::cell::Cell::new(0usize);
|
||
let shapes = draw(&sim, DVec3::new(0.0, 64.0, 0.0), GUI, |p| {
|
||
calls.set(calls.get() + 1);
|
||
ahead(p)
|
||
});
|
||
assert_eq!(calls.get(), MAX_SCAN, "one layer projected more than its scan budget");
|
||
assert_eq!(head(&shapes), "ОБЗОР · данные сети РЛС · в кадре 0, за кадром 256, сверх бюджета 744");
|
||
assert!(shapes.len() <= 1024, "{} shapes", shapes.len());
|
||
}
|
||
|
||
#[test]
|
||
fn interceptors_wear_small_yellow_squares_without_labels() {
|
||
let mut sim = sim_with(DroneKind::Shahed, DVec3::new(0.0, 64.0, 100.0));
|
||
sim.missiles.push(crate::sim::Missile::test_at(1, DVec3::new(10.0, 70.0, 120.0), 0));
|
||
sim.missiles.push(crate::sim::Missile::test_at(2, DVec3::new(0.0, 64.0, -50.0), 0));
|
||
let shapes = draw(&sim, DVec3::ZERO, GUI, ahead);
|
||
// The one ahead: four yellow sides; the one behind the camera: nothing.
|
||
assert_eq!(fills(&shapes, MISSILE), 4);
|
||
assert!(!texts(&shapes).iter().any(|t| t.text.contains("перехват")), "an interceptor is not a target");
|
||
// The budget holds with a full salvo too.
|
||
for id in 3..100 {
|
||
sim.missiles.push(crate::sim::Missile::test_at(id, DVec3::new(id as f64, 64.0, 100.0), 0));
|
||
}
|
||
let shapes = draw(&sim, DVec3::ZERO, GUI, ahead);
|
||
assert!(shapes.len() <= 1024, "{} shapes", shapes.len());
|
||
assert!(fills(&shapes, MISSILE) / 4 <= MAX_MARKERS);
|
||
}
|
||
|
||
#[test]
|
||
fn labels_flip_over_the_square_near_the_edges() {
|
||
// Near the bottom: the label goes over the square; near the top: under it.
|
||
assert!(label_y(590.0, 10.0, 600.0, false) < 590.0);
|
||
assert!(label_y(10.0, 10.0, 600.0, true) > 10.0);
|
||
assert!(label_y(300.0, 10.0, 600.0, false) > 300.0);
|
||
assert!(label_y(300.0, 10.0, 600.0, true) < 300.0);
|
||
}
|
||
}
|