557346548a
In a clean real game with 15 systems: interceptors scrubbed at relief 58 to 2, kills 33 to 46 of 82. Smart raids from 2500 blocks: crashes 10 to 3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
384 lines
16 KiB
Brainfuck
384 lines
16 KiB
Brainfuck
//! Flights and the land. Strike drones follow the terrain with a look-ahead worth several seconds of flight
|
|
//! (what they can still climb is a question of their speed and turn rate), dive only down a clear line, and
|
|
//! interceptors keep a safety height over the land ahead, loft over a ridge when the line is blocked, and
|
|
//! blow up high when there is no way over. The launch predicates (`why`) ask the same questions of a
|
|
//! shot before it is fired, so a system doesn't spend missiles on targets behind a hill.
|
|
//!
|
|
//! The land comes from `Ground::land`: heights sampled on a coarse lattice, so many flights share columns and
|
|
//! the questions to the game stay few; what the heights miss (trees, buildings, overhangs) is caught by a
|
|
//! probe through the block cache.
|
|
use super::*;
|
|
|
|
/// Land heights are sampled at the centres of cells this wide.
|
|
pub(super) const LATTICE: i32 = 8;
|
|
/// Land questions waiting at once: flights ask for more than a tick can send, and wait.
|
|
const ASK_MAX: usize = 192;
|
|
|
|
/// What the game has said of the land at a place.
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
|
pub(super) enum Rise {
|
|
/// Not yet: asked.
|
|
Pending,
|
|
/// There is no land to tell of (no chunk).
|
|
Void,
|
|
/// One above the highest solid block.
|
|
At(f64),
|
|
}
|
|
|
|
impl Ground {
|
|
/// One above the highest solid block at the lattice cell holding `p`, if known; asked for when it isn't.
|
|
/// Like `at` it asks again for a column that is old or was reported changed (#64), and it leaves room for the questions that matter more.
|
|
pub fn land(&mut self, p: DVec3) -> Option<f64> {
|
|
match self.rise(p) {
|
|
Rise::At(g) => Some(g),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// For a planner: None while the game has not answered, Some(None) where it has no land to
|
|
/// tell of, else Some(Some(height)).
|
|
pub fn land_state(&mut self, p: DVec3) -> Option<Option<f64>> {
|
|
match self.rise(p) {
|
|
Rise::Pending => None,
|
|
Rise::Void => Some(None),
|
|
Rise::At(g) => Some(Some(g)),
|
|
}
|
|
}
|
|
|
|
/// The same, saying whether the game has not answered yet or has no land there to tell of.
|
|
pub(super) fn rise(&mut self, p: DVec3) -> Rise {
|
|
let key = (
|
|
(p.x.floor() as i32).div_euclid(LATTICE) * LATTICE + LATTICE / 2,
|
|
(p.z.floor() as i32).div_euclid(LATTICE) * LATTICE + LATTICE / 2,
|
|
);
|
|
match self.known.get(&key) {
|
|
Some(&(y, when)) => {
|
|
// Old or reported changed: still used, asked again with the questions to spare.
|
|
if self.due(when) && self.wanted.len() < ASK_MAX / 2 && self.asked.insert(key) {
|
|
self.wanted.push(key);
|
|
}
|
|
if y > -64 { Rise::At(f64::from(y)) } else { Rise::Void }
|
|
}
|
|
None => {
|
|
if self.wanted.len() < ASK_MAX && self.asked.insert(key) {
|
|
self.wanted.push(key);
|
|
}
|
|
Rise::Pending
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The land's height at a column for the raid planner: `None` while the game hasn't answered, `Some(None)`
|
|
/// where it has no land to tell of. Asks, up to `cap` questions waiting.
|
|
pub(super) fn column(&mut self, x: i32, z: i32, cap: usize) -> Option<Option<f64>> {
|
|
match self.known.get(&(x, z)) {
|
|
Some(&(y, when)) => {
|
|
if self.due(when) && self.wanted.len() < cap / 2 && self.asked.insert((x, z)) {
|
|
self.wanted.push((x, z));
|
|
}
|
|
Some((y > -64).then_some(f64::from(y)))
|
|
}
|
|
None => {
|
|
// A chunk the game is loading is asked about again in a few ticks, not every one.
|
|
let loading = self.loading.get(&(x, z)).is_some_and(|&t| self.now.saturating_sub(t) < LOAD_RECHECK);
|
|
if !loading && self.wanted.len() < cap && self.asked.insert((x, z)) {
|
|
self.wanted.push((x, z));
|
|
}
|
|
None
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// What a flight can do about a rise ahead.
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub(super) struct Climb {
|
|
/// Radius of its tightest pull-up (blocks).
|
|
pub radius: f64,
|
|
/// The steepest climb it holds (radians).
|
|
pub pitch: f64,
|
|
}
|
|
|
|
impl Climb {
|
|
/// How much height it can have gained by the time it has gone `s` blocks on, starting level.
|
|
pub fn gain(&self, s: f64) -> f64 {
|
|
let arc = self.radius * self.pitch.sin();
|
|
if s <= arc {
|
|
self.radius - (self.radius * self.radius - s * s).max(0.0).sqrt()
|
|
} else {
|
|
self.radius * (1.0 - self.pitch.cos()) + (s - arc) * self.pitch.tan()
|
|
}
|
|
}
|
|
}
|
|
|
|
impl DroneKind {
|
|
/// The steepest climb it holds in cruise (radians): cruise missiles are the shallowest.
|
|
pub(super) fn climb_pitch(self) -> f64 {
|
|
match self {
|
|
DroneKind::Kh101 | DroneKind::Kalibr => 0.45,
|
|
DroneKind::Lancet => 0.7,
|
|
DroneKind::Geran3 | DroneKind::Kh22 => 0.6,
|
|
_ => 0.5,
|
|
}
|
|
}
|
|
pub(super) fn climb(self) -> Climb {
|
|
let s = self.spec();
|
|
Climb { radius: (s.speed / s.turn.max(0.01) * 1.15).max(12.0), pitch: self.climb_pitch() }
|
|
}
|
|
/// Blocks it looks ahead: eight seconds of flight, at least a pull-up and a climb over a hill.
|
|
pub(super) fn look_len(self) -> f64 {
|
|
let s = self.spec();
|
|
(s.speed * 160.0).clamp(160.0, 900.0)
|
|
}
|
|
}
|
|
|
|
/// A flight's new heading: it turns in yaw towards `heading` and in pitch towards a climb of `slope`, together
|
|
/// at most `rate` radians a tick, so a turn on to a new course doesn't dip or climb by itself (a great-circle
|
|
/// turn between nearly opposite headings can go either way). With `climb` set the pitch comes first and the
|
|
/// yaw gets what is left of the rate.
|
|
pub(super) fn fly_towards(old: DVec3, heading: DVec3, slope: f64, rate: f64, climb: bool) -> DVec3 {
|
|
let (old_yaw, old_pitch) = (old.x.atan2(old.z), old.y.clamp(-1.0, 1.0).asin());
|
|
let want_yaw = heading.x.atan2(heading.z);
|
|
let mut yaw = angle_diff(old_yaw, want_yaw);
|
|
let mut pitch = slope.atan() - old_pitch;
|
|
if climb {
|
|
pitch = pitch.clamp(-rate, rate);
|
|
yaw = yaw.clamp(-(rate * rate - pitch * pitch).max(0.0).sqrt(), (rate * rate - pitch * pitch).max(0.0).sqrt());
|
|
} else {
|
|
let total = yaw.hypot(pitch);
|
|
if total > rate {
|
|
yaw *= rate / total;
|
|
pitch *= rate / total;
|
|
}
|
|
}
|
|
let (yaw, pitch) = (old_yaw + yaw, old_pitch + pitch);
|
|
DVec3::new(yaw.sin() * pitch.cos(), pitch.sin(), yaw.cos() * pitch.cos())
|
|
}
|
|
|
|
/// How far over the land a dive's line passes.
|
|
pub(super) const DIVE_MARGIN: f64 = 8.0;
|
|
|
|
/// `v` with its pitch held between `lo` and `hi` (radians); `level` is the way it faces if it points straight up or down.
|
|
pub(super) fn clamp_pitch(v: DVec3, lo: f64, hi: f64, level: DVec3) -> DVec3 {
|
|
let pitch = v.y.clamp(-1.0, 1.0).asin();
|
|
if (lo..=hi).contains(&pitch) {
|
|
return v;
|
|
}
|
|
let pitch = pitch.clamp(lo, hi);
|
|
horizontal(v).normalize_or(level) * pitch.cos() + DVec3::Y * pitch.sin()
|
|
}
|
|
|
|
/// Chunks a tick the flights' look-ahead may ask the game to load (the rest of the budget is the launch-site search's).
|
|
const CHUNK_BUDGET: usize = 46;
|
|
|
|
/// Distances ahead where the land is sampled.
|
|
const REACH: [f64; 21] = [
|
|
0.0, 8.0, 16.0, 24.0, 32.0, 44.0, 56.0, 72.0, 90.0, 112.0, 136.0, 164.0, 196.0, 232.0, 272.0, 316.0, 366.0, 424.0, 500.0, 600.0,
|
|
720.0,
|
|
];
|
|
|
|
/// The height a flight must be at *now* to clear the land along each of `lines` (a unit horizontal direction and
|
|
/// how far out to look) by `clear`, given what it can climb: the greatest over the samples of the land's height plus clearance
|
|
/// less the height it can gain on the way. `None` when nothing is known.
|
|
pub(super) fn required_height(ground: &mut Ground, pos: DVec3, lines: &[(DVec3, f64)], clear: f64, climb: &Climb) -> (Option<f64>, bool) {
|
|
let mut need: Option<f64> = None;
|
|
// Land nobody has told of within the first half of the look-ahead: the flight is blind there.
|
|
let mut blind = false;
|
|
for &(dir, len) in lines {
|
|
for &s in REACH.iter().chain(&[900.0]) {
|
|
if s > len {
|
|
break;
|
|
}
|
|
if let Some(g) = ground.land(pos + dir * s) {
|
|
let h = g + clear - climb.gain(s);
|
|
need = Some(need.map_or(h, |n: f64| n.max(h)));
|
|
} else {
|
|
if s <= len * 0.5 {
|
|
blind = true;
|
|
}
|
|
// Land the game has not told of ahead: its chunk is asked for (it loads in a couple of seconds, a flight
|
|
// is that far from it), so the pull-up starts from the first known column and not at the ridge.
|
|
if ground.chunks_wanted() < CHUNK_BUDGET {
|
|
let p = pos + dir * s;
|
|
ground.want_chunk(((p.x.floor() as i32) >> 4, (p.z.floor() as i32) >> 4));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
(need, blind)
|
|
}
|
|
|
|
/// The least height to start from, over here, so that the straight line down to `target` stays `margin` over the
|
|
/// land (the last stretch before the aim is the target's own business). `None` when the land isn't known.
|
|
pub(super) fn dive_height(ground: &mut Ground, pos: DVec3, target: DVec3, margin: f64) -> Option<f64> {
|
|
let flat = horizontal(target - pos);
|
|
let total = flat.length();
|
|
if total < 20.0 {
|
|
return None;
|
|
}
|
|
let dir = flat / total;
|
|
let mut need: Option<f64> = None;
|
|
let n = ((total - 14.0) / 12.0).clamp(1.0, 40.0) as i32;
|
|
for k in 0..=n {
|
|
let s = (total - 14.0) * f64::from(k) / f64::from(n);
|
|
if let Some(g) = ground.land(pos + dir * s) {
|
|
let f = s / total;
|
|
let y = (g + margin - target.y * f) / (1.0 - f);
|
|
need = Some(need.map_or(y, |m: f64| m.max(y)));
|
|
}
|
|
}
|
|
need
|
|
}
|
|
|
|
/// The slope (rise a block) a flight at `from` must hold to clear the land by `margin` on its way towards
|
|
/// `to`, with a pull-up of `radius` for the climb to begin early, ignoring the last `skip` blocks before `to`.
|
|
/// Returns the slope and how far away the land that asks for it is; `None` when nothing is in the way or known.
|
|
pub(super) fn rope(ground: &mut Ground, from: DVec3, to: DVec3, margin: f64, radius: f64, (near, reach, skip): (f64, f64, f64), pending: &mut bool) -> Option<(f64, f64)> {
|
|
let flat = horizontal(to - from);
|
|
let total = flat.length();
|
|
if total < 1.0 {
|
|
return None;
|
|
}
|
|
let dir = flat / total;
|
|
let end = (total - skip).min(reach);
|
|
let mut best: Option<(f64, f64)> = None;
|
|
let mut s = near;
|
|
while s <= end {
|
|
match ground.rise(from + dir * s) {
|
|
Rise::At(g) => {
|
|
let slope = (g + margin - from.y) / (s - radius * 0.6).max(6.0);
|
|
if best.is_none_or(|b| slope > b.0) {
|
|
best = Some((slope, s));
|
|
}
|
|
}
|
|
Rise::Pending => *pending = true,
|
|
Rise::Void => {}
|
|
}
|
|
s += if s < 64.0 { 8.0 } else { 16.0 };
|
|
}
|
|
best
|
|
}
|
|
|
|
// ---------------------------------------------------------------- interceptors
|
|
|
|
impl MissileKind {
|
|
/// The steepest climb it flies to get over land (radians): long-range missiles loft, short-range ones don't.
|
|
pub(super) fn loft(self) -> f64 {
|
|
match self {
|
|
MissileKind::M77n6 => 1.25,
|
|
MissileKind::M48n6 => 1.1,
|
|
MissileKind::Pac3 | MissileKind::IrisT | MissileKind::Amraam => 0.9,
|
|
MissileKind::M9m317 => 0.8,
|
|
MissileKind::P1sun => 0.5,
|
|
MissileKind::M57e6 | MissileKind::M9m338 => 0.3,
|
|
}
|
|
}
|
|
/// Whether it lofts over a ridge to a target behind it.
|
|
pub(super) fn lofts(self) -> bool {
|
|
self.loft() >= 0.5
|
|
}
|
|
}
|
|
|
|
/// How high above the land an interceptor keeps.
|
|
pub(super) const MISSILE_MARGIN: f64 = 8.0;
|
|
|
|
/// What keeps an interceptor from being launched at a target for the land in the way.
|
|
pub(super) const MASKED: u8 = 1;
|
|
pub(super) const PATH_BLOCKED: u8 = 2;
|
|
/// The land under the line of fire has been asked for and not answered: the shot waits a tick or two.
|
|
pub(super) const WAIT: u8 = 4;
|
|
/// Ticks a shot waits for the land to be answered before it is judged on what is known.
|
|
pub(super) const WAIT_TICKS: u64 = 30;
|
|
|
|
/// Whether a shot from `muzzle` (launcher at `antenna`) at a target forecast at `est` moving at `vel` may be
|
|
/// fired for the land: the intercept point must not be behind a hill from the launcher, unless the missile
|
|
/// lofts, and the path the missile would fly must be clear.
|
|
pub(super) fn launch_blocks(ground: &mut Ground, kind: MissileKind, muzzle: DVec3, antenna: DVec3, est: DVec3, vel: DVec3) -> u8 {
|
|
let spec = kind.spec();
|
|
// Where it meets the target: the forecast carried on for the flight time.
|
|
let mut at = est;
|
|
for _ in 0..2 {
|
|
let tgo = muzzle.distance(at) / (spec.speed * 0.8);
|
|
at = est + vel * tgo;
|
|
}
|
|
let flat = horizontal(at - antenna);
|
|
let total = flat.length();
|
|
if total < 40.0 {
|
|
return 0;
|
|
}
|
|
let dir = flat / total;
|
|
// The line of sight from the launcher: any rise of the land over the line to the intercept point masks it.
|
|
let mut masked = false;
|
|
let mut waiting = false;
|
|
let mut s = 12.0;
|
|
while s < total - 12.0 {
|
|
match ground.rise(antenna + dir * s) {
|
|
Rise::At(g) if g + 1.0 > antenna.y + (at.y - antenna.y) * s / total => {
|
|
masked = true;
|
|
break;
|
|
}
|
|
Rise::Pending => waiting = true,
|
|
_ => {}
|
|
}
|
|
s += 16.0;
|
|
}
|
|
if !masked && waiting {
|
|
return WAIT;
|
|
}
|
|
if !masked {
|
|
return 0;
|
|
}
|
|
if !kind.lofts() {
|
|
return MASKED;
|
|
}
|
|
// A lofted path: climbs at the slope the highest land in the way asks for, and comes down on the target.
|
|
let radius = spec.speed / spec.turn.max(0.05);
|
|
let mut pending = false;
|
|
let path = rope(ground, muzzle, at, MISSILE_MARGIN, radius, (32.0, total, 24.0), &mut pending);
|
|
match path {
|
|
Some((slope, _)) if slope > kind.loft().tan() => PATH_BLOCKED,
|
|
_ if pending => WAIT,
|
|
_ => 0,
|
|
}
|
|
}
|
|
|
|
impl Sam {
|
|
/// What the land said of a shot at `target` when it was last judged, if lately.
|
|
pub(super) fn terrain_verdict(&self, target: u32, now: u64) -> Option<u8> {
|
|
self.terrain.get(&target).filter(|(when, _)| now.saturating_sub(*when) <= VERDICT_AGE).map(|(_, bits)| *bits)
|
|
}
|
|
}
|
|
|
|
/// Ticks a launch verdict (the land in the way) is kept before the shot is judged again.
|
|
pub(super) const VERDICT_AGE: u64 = 20;
|
|
|
|
/// Which way an interceptor steers to keep clear of the land: `aim` is where it would go; the result is a direction
|
|
/// (unit) raised as far as its loft allows, and whether the land is in the way beyond that.
|
|
pub(super) fn steer(ground: &mut Ground, kind: MissileKind, pos: DVec3, speed: f64, aim: DVec3, now: u64) -> (DVec3, bool) {
|
|
let spec = kind.spec();
|
|
let want = (aim - pos).normalize_or(DVec3::Y);
|
|
let flat = horizontal(want);
|
|
let flat_len = flat.length();
|
|
let reach = (speed * 18.0).clamp(60.0, 320.0);
|
|
let radius = speed / spec.turn.max(0.05);
|
|
let Some((slope, at)) = rope(ground, pos, aim, MISSILE_MARGIN, radius, (8.0, reach, 20.0), &mut false) else { return (want, false) };
|
|
let wanted = if flat_len > 1e-6 { want.y / flat_len } else { f64::INFINITY };
|
|
if slope <= wanted {
|
|
return (want, false);
|
|
}
|
|
let cap = kind.loft().tan();
|
|
let dir = flat.normalize_or(DVec3::X);
|
|
let raised = (dir + DVec3::Y * slope.min(cap)).normalize();
|
|
// No way over, and the land close enough ahead that the next ticks will end in it: blow up before.
|
|
let mut fail = false;
|
|
if slope > cap + 0.02 && at < (speed * 6.0).max(40.0) {
|
|
// The heights are a coarse upper bound (the canopy of a forest, the roof of a building): it blows up only before
|
|
// blocks that are really there, not before what the height map says is higher than the missile.
|
|
fail = (1..=4).any(|k| {
|
|
let p = pos + raised * speed * f64::from(k);
|
|
ground.land(p).is_some_and(|g| g + 2.0 > p.y)
|
|
}) && matches!(ground.world.probe(pos, pos + raised * speed * 4.0, now), world::Sweep::Hit { .. });
|
|
}
|
|
(raised, fail)
|
|
}
|