Smart raids, wave menu, radar detection (horizon, RCS, terrain), vulnerable SAMs, busiest-first grid
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
//! 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;
|
||||
/// The antenna's height over the vehicle's ground.
|
||||
const MAST: f64 = 5.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).
|
||||
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 system's radar is.
|
||||
pub(super) fn antenna(sam: &Sam) -> DVec3 {
|
||||
sam.pos + DVec3::Y * 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 radar = sam.kind.range() * 1.3 * sam.fitness() * kind.rcs().powf(0.25);
|
||||
let horizon = HORIZON * (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 alive: Vec<usize> = (0..self.sams.len()).filter(|&i| self.sams[i].alive()).collect();
|
||||
if !alive.is_empty() {
|
||||
let i = alive[(now / 3) as usize % alive.len()];
|
||||
let sam = &self.sams[i];
|
||||
let h = Horizon::build(antenna(sam), sam.kind.range() * 1.3, ground, now);
|
||||
self.horizons.insert(sam.id, h);
|
||||
}
|
||||
}
|
||||
self.horizons.retain(|id, _| self.sams.iter().any(|s| s.id == *id && s.alive()));
|
||||
// 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();
|
||||
if !sam.alive() {
|
||||
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.seen.push(d.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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user