Terrain-aware flight: drones look 8 s ahead and climb in time, the planner prices terrain, SAMs hold fire at targets masked by terrain (long-range ones loft over ridges), interceptors keep clear of the land or self-destruct in the air
build / mod (push) Successful in 3m24s
build / mod (push) Successful in 3m24s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+108
-6
@@ -84,6 +84,12 @@ pub(super) struct Plan {
|
||||
approach: Approach,
|
||||
total: u32,
|
||||
centre: DVec3,
|
||||
/// The land under the grid's cells (NaN where the game has none to tell of), the cells still unanswered, and
|
||||
/// the ticks the planner has waited for the answers. A smart raid is routed over the land, not round it.
|
||||
heights: Vec<f32>,
|
||||
unanswered: Vec<u32>,
|
||||
waited: u32,
|
||||
mapped: bool,
|
||||
}
|
||||
|
||||
/// The raid in progress: the counts at its start, for the report.
|
||||
@@ -123,6 +129,20 @@ impl Grid {
|
||||
|
||||
/// The cheapest way over `grid` from `from` to `to` by its prices (A*): points along it, `to` last.
|
||||
pub(super) fn plan_route(grid: &Grid, prices: &[f32], from: DVec3, to: DVec3) -> (Vec<DVec3>, usize) {
|
||||
plan_route_on(grid, prices, None, from, to)
|
||||
}
|
||||
|
||||
/// The land a route crosses: its height under each cell (NaN unknown), the climb a kind holds for long
|
||||
/// (rise a block), and the height above which the land forces a drone up from its low altitude.
|
||||
pub(super) struct Relief<'a> {
|
||||
pub heights: &'a [f32],
|
||||
pub slope: f32,
|
||||
pub floor: f32,
|
||||
}
|
||||
|
||||
/// [`plan_route`] over relief: a climb steeper than the kind holds is dear (a cliff or a ridge it would have to
|
||||
/// pull up over), and high land costs more than a valley, where it can fly low and stay under the radars.
|
||||
pub(super) fn plan_route_on(grid: &Grid, prices: &[f32], relief: Option<&Relief>, from: DVec3, to: DVec3) -> (Vec<DVec3>, usize) {
|
||||
let mut opened = 0;
|
||||
let n = grid.size;
|
||||
let at = |(i, j): (i32, i32)| (i * n + j) as usize;
|
||||
@@ -151,7 +171,21 @@ pub(super) fn plan_route(grid: &Grid, prices: &[f32], from: DVec3, to: DVec3) ->
|
||||
}
|
||||
let len = if di != 0 && dj != 0 { std::f32::consts::SQRT_2 } else { 1.0 };
|
||||
// The target's own cell is where it must go, whatever covers it.
|
||||
let price = if nb == goal { len } else { len * prices[at(nb)] };
|
||||
let mut price = if nb == goal { len } else { len * prices[at(nb)] };
|
||||
if let Some(r) = relief
|
||||
&& nb != goal
|
||||
{
|
||||
let (h0, h1) = (r.heights[at(c)], r.heights[at(nb)]);
|
||||
if h1.is_finite() {
|
||||
price += len * ((h1 - r.floor) / 30.0).clamp(0.0, 4.0);
|
||||
if h0.is_finite() {
|
||||
let slope = (h1 - h0) / (len * grid.cell as f32);
|
||||
if slope > r.slope {
|
||||
price += (slope - r.slope) * 20.0 * len;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let ng = g + price;
|
||||
if ng < best[at(nb)] {
|
||||
best[at(nb)] = ng;
|
||||
@@ -250,15 +284,53 @@ impl Sim {
|
||||
approach: order.approach,
|
||||
total,
|
||||
centre,
|
||||
heights: Vec::new(),
|
||||
unanswered: Vec::new(),
|
||||
waited: 0,
|
||||
mapped: !smart,
|
||||
});
|
||||
// A raid without routes is ready at once.
|
||||
self.plan_step(out);
|
||||
self.plan_step(None, out);
|
||||
total
|
||||
}
|
||||
|
||||
/// Works on the raid's plan a while (a tick's share), and launches it when it is ready.
|
||||
pub(super) fn plan_step(&mut self, out: &mut Out) {
|
||||
pub(super) fn plan_step(&mut self, ground: Option<&mut Ground>, out: &mut Out) {
|
||||
let Some(mut plan) = self.plan.take() else { return };
|
||||
// A smart raid first learns the land under its grid: asked for in bulk, a thousand columns a tick, for
|
||||
// up to two seconds (what the game has no land for stays unknown and costs nothing).
|
||||
if !plan.mapped {
|
||||
let Some(ground) = ground else {
|
||||
self.plan = Some(plan);
|
||||
return;
|
||||
};
|
||||
let n = plan.grid.size;
|
||||
if plan.heights.is_empty() {
|
||||
plan.heights = vec![f32::NAN; (n * n) as usize];
|
||||
plan.unanswered = (0..(n * n) as u32).collect();
|
||||
}
|
||||
let Plan { grid, heights, unanswered, .. } = &mut plan;
|
||||
unanswered.retain(|&k| {
|
||||
let (i, j) = (k as i32 / n, k as i32 % n);
|
||||
let c = grid.centre((i, j), 0.0);
|
||||
match ground.column(c.x.floor() as i32, c.z.floor() as i32, 1024) {
|
||||
None => true,
|
||||
Some(h) => {
|
||||
if let Some(h) = h {
|
||||
heights[k as usize] = h as f32;
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
});
|
||||
plan.waited += 1;
|
||||
if plan.unanswered.is_empty() || plan.waited >= 40 {
|
||||
plan.mapped = true;
|
||||
} else {
|
||||
self.plan = Some(plan);
|
||||
return;
|
||||
}
|
||||
}
|
||||
let mut work = 0usize;
|
||||
while plan.next < plan.items.len() && work < WORK_PER_TICK {
|
||||
let i = plan.next;
|
||||
@@ -268,17 +340,39 @@ impl Sim {
|
||||
};
|
||||
let spec = kind.spec();
|
||||
let mut d = plan.items[i].d.clone();
|
||||
// It starts over the land, not in it: a hill under the starting point lifts it.
|
||||
let mut from = from;
|
||||
if !plan.heights.is_empty() {
|
||||
let (gi, gj) = plan.grid.index(from);
|
||||
let g = plan.heights[(gi * plan.grid.size + gj) as usize];
|
||||
let floor = f64::from(g) + if spec.ballistic.is_some() { 10.0 } else { spec.clearance + 10.0 };
|
||||
if g.is_finite() && from.y < floor {
|
||||
from.y = floor;
|
||||
d.pos.y = floor;
|
||||
if spec.ballistic.is_some() {
|
||||
let flat = horizontal(aim - from);
|
||||
let time = (flat.length() / spec.speed).max(30.0);
|
||||
d.vel = flat / time + DVec3::Y * ((aim.y - from.y + 0.5 * GRAVITY * time * time) / time);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut path = from.distance(aim);
|
||||
if let Some(key) = key {
|
||||
let route = match plan.routes.get(&key) {
|
||||
Some(r) => r.clone(),
|
||||
None => {
|
||||
if !plan.prices.contains_key(&kind) {
|
||||
let (p, cost) = self.price_grid(&plan.grid, kind);
|
||||
let heights = (!plan.heights.is_empty()).then_some(plan.heights.as_slice());
|
||||
let (p, cost) = self.price_grid_on(&plan.grid, kind, heights);
|
||||
work += cost;
|
||||
plan.prices.insert(kind, p);
|
||||
}
|
||||
let (r, cost) = plan_route(&plan.grid, &plan.prices[&kind], from, aim);
|
||||
let relief = (!plan.heights.is_empty()).then(|| Relief {
|
||||
heights: &plan.heights,
|
||||
slope: (kind.climb_pitch().tan() * 0.5) as f32,
|
||||
floor: (aim.y + spec.clearance) as f32,
|
||||
});
|
||||
let (r, cost) = plan_route_on(&plan.grid, &plan.prices[&kind], relief.as_ref(), from, aim);
|
||||
work += cost;
|
||||
plan.routes.insert(key, r.clone());
|
||||
r
|
||||
@@ -357,6 +451,7 @@ impl Sim {
|
||||
jammed: false,
|
||||
sector: 0,
|
||||
smart: false,
|
||||
nav: Nav::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -404,6 +499,12 @@ impl Sim {
|
||||
/// The price of each cell of `grid` for this kind flying low: 1, and much more where a
|
||||
/// system sees it there (its radar's reach for it, the land) and can shoot it.
|
||||
pub(super) fn price_grid(&self, grid: &Grid, kind: DroneKind) -> (Vec<f32>, usize) {
|
||||
self.price_grid_on(grid, kind, None)
|
||||
}
|
||||
|
||||
/// The same over known land: a cell's drone flies `height` over the land there, not over the system's feet,
|
||||
/// so what a radar sees and what a hill hides follow the relief.
|
||||
pub(super) fn price_grid_on(&self, grid: &Grid, kind: DroneKind, land: Option<&[f32]>) -> (Vec<f32>, usize) {
|
||||
let mut cost = 0;
|
||||
let spec = kind.spec();
|
||||
let height = spec.clearance.max(6.0);
|
||||
@@ -446,7 +547,8 @@ impl Sim {
|
||||
for i in (ci - r).max(0)..=(ci + r).min(n - 1) {
|
||||
for j in (cj - r).max(0)..=(cj + r).min(n - 1) {
|
||||
cost += 1;
|
||||
let p = grid.centre((i, j), s.pos.y + height);
|
||||
let ground = land.map(|l| l[(i * n + j) as usize]).filter(|g| g.is_finite());
|
||||
let p = grid.centre((i, j), ground.map_or(s.pos.y + height, |g| f64::from(g) + height));
|
||||
let d = horizontal(p - s.pos).length();
|
||||
if d > far {
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user