31d967b036
Co-Authored-By: Kimi <noreply@moonshot.ai>
192 lines
8.7 KiB
Rust
192 lines
8.7 KiB
Rust
//! The radar screen (top left): a round scope round the player, north up,
|
||
//! with a sweeping beam; blips fade after the beam passes them.
|
||
use crate::panel::distance;
|
||
use crate::sim::{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;
|
||
/// Blocks from the centre to the edge.
|
||
const RANGE: f64 = 500.0;
|
||
const SEGMENTS: usize = 48;
|
||
|
||
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)
|
||
}
|
||
|
||
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));
|
||
}
|
||
}
|
||
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;
|
||
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.
|
||
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, 0xFF4040)
|
||
};
|
||
s.push(square(x, y, 1.8, alpha(color, a)));
|
||
// Its heading, a short tail.
|
||
let v = vel.normalize_or_zero();
|
||
s.push(quad(pt(x, y), pt(x + 0.6, y), pt(x - v.x as f32 * 6.0 + 0.6, y - v.z as f32 * 6.0), pt(x - v.x as f32 * 6.0, y - v.z as f32 * 6.0), alpha(color, a * 0.6)));
|
||
}
|
||
None => far += 1,
|
||
}
|
||
}
|
||
// 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) {
|
||
s.push(square(x, y, 0.8, 0xFFFFFFFF));
|
||
}
|
||
}
|
||
}
|
||
// The player: a white arrow the way they look.
|
||
// 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));
|
||
// Labels.
|
||
s.push(text(format!("РЛС · {}", distance(RANGE)), cx, SIZE + 2.0, 0xFF80FF90, 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
|
||
}
|