67a2a523b4
build / mod (push) Successful in 2m26s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
6.0 KiB
Rust
146 lines
6.0 KiB
Rust
//! What a system's radar holds. Small targets come up late (the radar's reach
|
|
//! goes with the fourth root of their cross-section), low ones later still
|
|
//! (over the curve of the earth, scaled to the game's distances), and the land
|
|
//! hides what flies behind hills and woods: each system keeps its horizon, the
|
|
//! highest the land rises seen from its antenna, ray by ray round it.
|
|
use super::*;
|
|
|
|
/// Rays round a radar, and blocks between the land's samples along them.
|
|
const RAYS: usize = 48;
|
|
const STEP: f64 = 16.0;
|
|
/// How far units share their tracks (the network's data links).
|
|
pub(super) const LINK: f64 = 1500.0;
|
|
/// Radio horizon: `HORIZON * (√mast + √height)` blocks.
|
|
const HORIZON: f64 = 35.0;
|
|
/// Anything this near is seen (and heard), whatever hides it.
|
|
const NEAR: f64 = 24.0;
|
|
|
|
pub(super) struct Horizon {
|
|
/// Per ray, per sample out: the steepest rise of the land so far (tan of its elevation).
|
|
pub(super) rays: Vec<Vec<f32>>,
|
|
}
|
|
|
|
impl Horizon {
|
|
fn build(antenna: DVec3, reach: f64, ground: &mut Ground, now: u64) -> Self {
|
|
let steps = (reach / STEP).ceil() as usize + 1;
|
|
let rays = (0..RAYS)
|
|
.map(|r| {
|
|
let a = r as f64 / RAYS as f64 * TAU;
|
|
let dir = DVec3::new(a.sin(), 0.0, a.cos());
|
|
let mut top = f32::NEG_INFINITY;
|
|
(1..=steps)
|
|
.map(|k| {
|
|
let d = k as f64 * STEP;
|
|
// Unknown land (not loaded, not answered yet) hides nothing.
|
|
if let Some(g) = ground.at(antenna + dir * d, now) {
|
|
top = top.max(((g - antenna.y) / d) as f32);
|
|
}
|
|
top
|
|
})
|
|
.collect()
|
|
})
|
|
.collect();
|
|
Self { rays }
|
|
}
|
|
|
|
/// Whether the land between the antenna and `p` rises over the line to it.
|
|
pub(super) fn hides(&self, antenna: DVec3, p: DVec3) -> bool {
|
|
let rel = p - antenna;
|
|
let flat = (rel.x * rel.x + rel.z * rel.z).sqrt();
|
|
let k = (flat / STEP) as usize;
|
|
if k == 0 {
|
|
return false;
|
|
}
|
|
let a = rel.x.atan2(rel.z).rem_euclid(TAU);
|
|
let ray = &self.rays[(a / TAU * RAYS as f64).round() as usize % RAYS];
|
|
let top = ray[(k - 1).min(ray.len() - 1)];
|
|
((rel.y / flat) as f32) < top
|
|
}
|
|
}
|
|
|
|
/// Where a unit's radar antenna is.
|
|
pub(super) fn antenna(sam: &Sam) -> DVec3 {
|
|
sam.pos + DVec3::Y * sam.kind.spec().mast
|
|
}
|
|
|
|
/// How far a system's search radar reaches for this kind at `height` over the land:
|
|
/// its own reach (worse when damaged) for the target's size, under the radio horizon.
|
|
pub(super) fn reach(sam: &Sam, kind: DroneKind, height: f64) -> f64 {
|
|
let spec = sam.kind.spec();
|
|
if spec.sensor <= 0.0 {
|
|
return 0.0;
|
|
}
|
|
let radar = spec.sensor * sam.fitness() * kind.rcs().powf(0.25);
|
|
let horizon = HORIZON * (spec.mast.sqrt() + height.max(0.0).sqrt());
|
|
radar.min(horizon).max(NEAR)
|
|
}
|
|
|
|
impl Sim {
|
|
/// Rebuilds the systems' horizons in turn, and what each radar holds now.
|
|
pub(super) fn update_sight(&mut self, ground: &mut Ground) {
|
|
let now = self.tick;
|
|
// One horizon every few ticks: the land answers come in over time.
|
|
if !self.sams.is_empty() && now % 3 == 0 {
|
|
let radars: Vec<usize> =
|
|
(0..self.sams.len()).filter(|&i| self.sams[i].alive() && self.sams[i].kind.spec().sensor > 0.0).collect();
|
|
if !radars.is_empty() {
|
|
let i = radars[(now / 3) as usize % radars.len()];
|
|
let sam = &self.sams[i];
|
|
let h = Horizon::build(antenna(sam), sam.kind.spec().sensor, ground, now);
|
|
self.horizons.insert(sam.id, h);
|
|
}
|
|
}
|
|
let alive: std::collections::HashSet<u32> = self.sams.iter().filter(|s| s.alive()).map(|s| s.id).collect();
|
|
self.horizons.retain(|id, _| alive.contains(id));
|
|
// Each drone's height over the land under it (or over the systems' ground, unknown).
|
|
let heights: Vec<Option<f64>> =
|
|
self.drones.iter().map(|d| ground.at(d.pos, now).map(|g| d.pos.y - g)).collect();
|
|
for sam in &mut self.sams {
|
|
sam.seen.clear();
|
|
sam.own.clear();
|
|
if !sam.alive() || sam.kind.spec().sensor <= 0.0 || sam.moving() {
|
|
continue;
|
|
}
|
|
let at = antenna(sam);
|
|
let horizon = self.horizons.get(&sam.id);
|
|
for (d, h) in self.drones.iter().zip(&heights) {
|
|
if !d.alive() {
|
|
continue;
|
|
}
|
|
let dist = d.pos.distance(at);
|
|
let height = h.unwrap_or(d.pos.y - sam.pos.y);
|
|
if dist > reach(sam, d.kind, height) {
|
|
continue;
|
|
}
|
|
if dist > NEAR && horizon.is_some_and(|hz| hz.hides(at, d.pos)) {
|
|
continue;
|
|
}
|
|
sam.own.push(d.id);
|
|
}
|
|
}
|
|
// The network: every unit gets the tracks of the radars within data-link range.
|
|
let tracks: Vec<(DVec3, Vec<u32>)> =
|
|
self.sams.iter().filter(|s| !s.own.is_empty()).map(|s| (s.pos, s.own.clone())).collect();
|
|
// Merged in the radars' order, each track once: a set marks what is in already.
|
|
let mut have: std::collections::HashSet<u32> = std::collections::HashSet::new();
|
|
for sam in &mut self.sams {
|
|
sam.seen.clone_from(&sam.own);
|
|
if !sam.alive() || sam.moving() || !sam.kind.armed() {
|
|
continue;
|
|
}
|
|
have.clear();
|
|
have.extend(sam.own.iter().copied());
|
|
for (at, ids) in &tracks {
|
|
if at.distance(sam.pos) <= LINK {
|
|
sam.seen.extend(ids.iter().copied().filter(|id| have.insert(*id)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Whether any system's radar holds this drone (the player's radar shows only those).
|
|
pub fn tracked(&self, id: u32) -> bool {
|
|
self.sams.iter().any(|s| s.seen.contains(&id))
|
|
}
|
|
}
|