Flights over unknown land hold a safe height, air starts wait for the land, systems and aims settle on the surface; map labels, filters, coverage by terrain and scenes
build / mod (push) Successful in 8m34s

Far raids over mountains with a 300-block known disc: crashes from 19 to 2 of 368 (mixed), 22 to about 9 (smart). Balance tests gain hilly land.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-31 02:19:31 +03:00
parent 57a5426e65
commit 38e515dea9
9 changed files with 1650 additions and 211 deletions
+162
View File
@@ -93,6 +93,97 @@ pub(super) fn reach(sam: &Sam, kind: DroneKind, height: f64) -> f64 {
radar.min(horizon).max(NEAR)
}
/// The low flyer the command map shows the radars' coverage for: a Shahed over the land.
const LOW_KIND: DroneKind = DroneKind::Shahed;
/// A radar as the command map sees it: where its antenna is and how far it reaches a low flyer.
#[derive(Clone, Copy)]
pub struct LowSensor {
pub id: u32,
pub ant: DVec3,
/// The ground under the unit (a place whose land is unknown is taken as level with it).
pub base: f64,
pub reach: f64,
/// Its horizon is known: the land hides what it hides.
pub terrain: bool,
}
/// Rays of `Sim::low_rim`.
pub const RIM_RAYS: usize = RAYS;
impl Ground {
/// The land's height at `p` from what the game has already answered; asks for nothing.
pub fn peek(&self, p: DVec3) -> Option<f64> {
const LAT: i32 = super::terrain::LATTICE;
let k = (p.x.floor() as i32, p.z.floor() as i32);
let hit = self.known.get(&k).or_else(|| {
self.known.get(&(k.0.div_euclid(LAT) * LAT + LAT / 2, k.1.div_euclid(LAT) * LAT + LAT / 2))
});
hit.and_then(|&(y, _)| (y > -64).then_some(f64::from(y)))
}
}
impl Sim {
/// A radar for a unit of `kind` at `pos` as the map draws it; `id`: the unit it is (its parts and horizon), None for a ghost.
pub fn low_sensor(&self, kind: SamKind, pos: DVec3, id: Option<u32>) -> Option<LowSensor> {
let spec = kind.spec();
if spec.sensor <= 0.0 {
return None;
}
let fit = id.and_then(|i| self.sams.iter().find(|s| s.id == i)).map_or(1.0, |s| s.radar_fit());
let height = LOW_KIND.spec().clearance;
let radar = spec.sensor * fit * LOW_KIND.rcs().powf(0.25);
let reach = radar.min(HORIZON * (spec.mast.sqrt() + height.sqrt())).max(NEAR);
let terrain = id.is_some_and(|i| self.horizons.contains_key(&i));
Some(LowSensor { id: id.unwrap_or(u32::MAX), ant: pos + DVec3::Y * spec.mast, base: pos.y, reach, terrain })
}
/// The radars that are up now.
pub fn low_sensors(&self) -> Vec<LowSensor> {
self.sams.iter().filter(|s| s.radar_up()).filter_map(|s| self.low_sensor(s.kind, s.pos, Some(s.id))).collect()
}
/// Whether the radar sees a low flyer (over the land) at the column (`x`, `z`): in its reach and not behind the land.
pub fn low_sees(&self, ground: &Ground, s: &LowSensor, x: f64, z: f64) -> bool {
let (dx, dz) = (x - s.ant.x, z - s.ant.z);
let d2 = dx * dx + dz * dz;
if d2 > s.reach * s.reach {
return false;
}
if !s.terrain || d2 <= NEAR * NEAR {
return true;
}
let Some(h) = self.horizons.get(&s.id) else { return true };
let g = ground.peek(DVec3::new(x, 0.0, z)).unwrap_or(s.base);
!h.hides(s.ant, DVec3::new(x, g + LOW_KIND.spec().clearance, z))
}
/// Per ray round the radar, how far a low flyer is seen before the land first hides it (at most the reach).
pub fn low_rim(&self, ground: &Ground, s: &LowSensor) -> Vec<f32> {
let mut out = vec![s.reach as f32; RAYS];
let Some(h) = self.horizons.get(&s.id).filter(|_| s.terrain) else { return out };
let steps = ((s.reach / STEP) as usize).min(h.rays[0].len());
let height = LOW_KIND.spec().clearance;
for (r, o) in out.iter_mut().enumerate() {
let a = r as f64 / RAYS as f64 * TAU;
let (sx, sz) = (a.sin(), a.cos());
for k in 1..=steps {
let d = k as f64 * STEP;
if d <= NEAR {
continue;
}
let p = DVec3::new(s.ant.x + sx * d, 0.0, s.ant.z + sz * d);
let y = ground.peek(p).unwrap_or(s.base) + height - s.ant.y;
if ((y / d) as f32) < h.rays[r][k - 1] {
*o = (d - STEP).max(NEAR) as f32;
break;
}
}
}
out
}
}
/// How a kind's radar looks round.
pub(super) struct Scan {
/// Ticks for the beam to go round.
@@ -367,3 +458,74 @@ impl Sim {
best
}
}
#[cfg(test)]
mod low_view_tests {
use super::*;
/// A radar on level land (70) with a hill east of it (140 high, x 128..160): the east ray is cut at the hill, the others are not.
fn world() -> (Sim, Ground, u32) {
let mut sim = Sim::new(3);
let id = sim.id();
sim.sams.push(Sam::new(id, SamKind::Buk, DVec3::new(0.0, 70.0, 0.0), 0.0));
let mut ground = Ground::default();
let cols: Vec<(i32, i32, i32)> = (-60..60).map(|k| (k * 16, 0, if (128..160).contains(&(k * 16)) { 140 } else { 70 })).collect();
ground.answer(cols, 0);
let h = Horizon::build(antenna(&sim.sams[0]), sim.sams[0].kind.spec().sensor, &mut ground, 0);
sim.horizons.insert(id, h);
(sim, ground, id)
}
#[test]
fn the_land_cuts_the_coverage_edge_behind_a_hill() {
let (sim, ground, id) = world();
let ls = sim.low_sensor(SamKind::Buk, DVec3::new(0.0, 70.0, 0.0), Some(id)).unwrap();
assert!(ls.terrain && ls.reach > 200.0, "{}", ls.reach);
let rim = sim.low_rim(&ground, &ls);
let east = rim[RIM_RAYS / 4];
assert!((128.0..=176.0).contains(&east), "east edge {east}");
// Rays whose land is unknown hide nothing: the whole reach.
assert_eq!(rim[0], ls.reach as f32);
assert_eq!(rim[RIM_RAYS * 3 / 4], ls.reach as f32);
assert!(sim.low_sees(&ground, &ls, -200.0, 0.0));
assert!(sim.low_sees(&ground, &ls, 100.0, 0.0));
assert!(!sim.low_sees(&ground, &ls, 200.0, 0.0), "behind the hill");
assert!(!sim.low_sees(&ground, &ls, ls.reach + 50.0, 0.0), "out of reach");
// A ghost (no horizon) sees the whole circle.
let ghost = sim.low_sensor(SamKind::Buk, DVec3::new(0.0, 70.0, 0.0), None).unwrap();
assert!(sim.low_sees(&ground, &ghost, 200.0, 0.0));
}
// The map's tint asks `low_sees` for a cell of a coarse grid at every radar that reaches it, and the edges
// ask `low_rim`: with a Polygon's worth of systems both stay cheap (native time, a few ms; wasm is a few times that).
#[test]
fn coverage_of_128_systems_is_cheap() {
let (mut sim, mut ground, _) = world();
for i in 0..127 {
let id = sim.id();
let pos = DVec3::new((i % 16) as f64 * 40.0 - 300.0, 70.0, (i / 16) as f64 * 40.0 - 150.0);
sim.sams.push(Sam::new(id, SamKind::Buk, pos, 0.0));
let k = sim.sams.len() - 1;
let h = Horizon::build(antenna(&sim.sams[k]), sim.sams[k].kind.spec().sensor, &mut ground, 0);
sim.horizons.insert(id, h);
}
let sensors = sim.low_sensors();
assert_eq!(sensors.len(), 128);
let t = std::time::Instant::now();
let mut seen = 0;
for cy in 0..28 {
for cx in 0..36 {
let (x, z) = (cx as f64 * 30.0 - 500.0, cy as f64 * 30.0 - 400.0);
// Worst case for the early exit: a cell no radar sees is tested against all that reach it.
if sensors.iter().any(|s| sim.low_sees(&ground, s, x, z)) {
seen += 1;
}
}
}
let tint = t.elapsed();
let t = std::time::Instant::now();
let rims: usize = sensors.iter().take(3).map(|s| sim.low_rim(&ground, s).len()).sum();
eprintln!("tint of 1008 cells: {tint:?} ({seen} seen); 3 rims: {:?} ({rims})", t.elapsed());
assert!(seen > 0 && tint.as_millis() < 100, "{tint:?}");
}
}