Counter-strikes collide, persist and record their flights (#106 #108 #109 #113)

Co-Authored-By: Kimi <noreply@moonshot.ai>
This commit is contained in:
2026-11-01 02:34:39 +03:00
parent 1a693763fd
commit 31d967b036
10 changed files with 646 additions and 70 deletions
+144 -3
View File
@@ -164,6 +164,10 @@ pub struct Sites {
pub list: Vec<LaunchSite>,
next: u32,
jobs: Vec<(u32, u32)>,
/// Sites a new wave kept for something still in the air (#108): their kit stays until that is gone.
holding: Vec<u32>,
/// Drones that left a site and are still in the air: (drone, site), so their site is not taken away under them.
flying: Vec<(u32, u32)>,
}
/// What a site says about a drone due to leave.
@@ -194,6 +198,8 @@ impl Sites {
pub fn clear(&mut self) {
self.list.clear();
self.jobs.clear();
self.holding.clear();
self.flying.clear();
}
pub(super) fn add(&mut self, mut site: LaunchSite) -> u32 {
self.next += 1;
@@ -205,11 +211,34 @@ impl Sites {
pub(super) fn job(&mut self, drone: u32, site: u32) {
self.jobs.push((drone, site));
}
/// The old raid's sites go, but not those with jobs still to fly.
pub(super) fn retire(&mut self, queued: &[u32]) {
/// The old raid's sites go, but not those with jobs still to fly, nor those something in the air still came
/// from or is flying to (#108: a missile on its way is not left shooting at a vanished base).
pub(super) fn retire(&mut self, queued: &[u32], refs: &[u32]) {
self.jobs.retain(|j| queued.contains(&j.0));
let jobs = &self.jobs;
self.list.retain(|s| jobs.iter().any(|j| j.1 == s.id));
let mut list = std::mem::take(&mut self.list);
list.retain(|s| jobs.iter().any(|j| j.1 == s.id) || refs.contains(&s.id));
self.list = list;
// What only the references hold: let go once they are gone, so no old bases pile up.
self.holding = self.list.iter().filter(|s| !jobs.iter().any(|j| j.1 == s.id)).map(|s| s.id).collect();
}
/// Drops the sites `retire` held only for a flight, a drone or a scout link that is now gone.
pub(super) fn prune(&mut self, refs: &[u32]) {
if self.holding.is_empty() {
return;
}
let live = |id: &u32| refs.contains(id) || self.jobs.iter().any(|j| j.1 == *id);
let gone: Vec<u32> = self.holding.iter().copied().filter(|id| !live(id)).collect();
if gone.is_empty() {
return;
}
self.holding.retain(|id| live(id));
self.list.retain(|s| !gone.contains(&s.id));
}
/// Sites drones still in the air left: (drone, site) for those alive in the list, forgetting the others.
pub(super) fn flying_sites(&mut self, alive: &[u32]) -> Vec<u32> {
self.flying.retain(|(d, _)| alive.contains(d));
self.flying.iter().map(|f| f.1).collect()
}
fn take(&mut self, drone: u32, now: u64) -> Slot {
let Some(j) = self.jobs.iter().position(|j| j.0 == drone) else { return Slot::Free };
@@ -229,6 +258,8 @@ impl Sites {
s.next_free = now + s.class.cadence();
let air = s.offmap;
self.jobs.swap_remove(j);
// A drone in the air still belongs to its site: a new wave must not take the pad from under it (#108).
self.flying.push((drone, site));
if air { Slot::Air } else { Slot::Go }
}
}
@@ -1052,4 +1083,114 @@ mod tests {
};
assert_eq!(run(false), run(true));
}
// ---------------------------------------------------------------- #108: what holds a site beyond its wave
/// A site with the radars' fix on it, and no launches left: a strike can be sent at it without any drone flying.
fn fixed_site(sim: &mut Sim, pos: DVec3) -> u32 {
let id = sim.sites.add(LaunchSite {
id: 0,
side: 0,
sector: 0,
bearing: 0.0,
class: SiteClass::Ramp,
pos,
range: pos.z,
water: false,
stock: 0,
planned: 0,
alive: true,
next_free: 0,
offmap: false,
});
sim.strikes.known.push(super::super::strike::Known {
site: id,
class: SiteClass::Ramp,
obs: Vec::new(),
est: Some(super::super::strike::Estimate {
pos: glam::DVec2::new(pos.x, pos.z),
radius: 5.0,
fix: true,
bearing: 0.0,
bearing_err: 0.01,
origin: glam::DVec2::ZERO,
}),
launches: 1,
last: 0,
state: super::super::strike::SiteState::Active,
cover: None,
aimed: false,
});
id
}
#[test]
fn a_wave_keeps_the_site_a_strike_is_still_flying_to() {
let mut sim = Sim::new(11);
sim.strikes.cover_override = Some(0);
let id = fixed_site(&mut sim, DVec3::new(0.0, 64.0, 300.0));
let mut ground = Ground::default();
let mut out = Out::default();
sim.order_strike(id, DVec3::new(0.0, 64.0, 0.0), &mut out).unwrap();
// The next wave is planned while the missile is in the air: its housekeeping must not take the target away.
let refs = sim.site_refs();
assert!(refs.contains(&id), "the flight is no reference to its site");
sim.sites.retire(&[], &refs);
assert!(sim.sites.get(id).is_some(), "a new wave took the site a missile was flying to");
// The missile lands: the site is hit, and nothing keeps it afterwards.
for _ in 0..300 {
sim.step(&mut ground, None, &mut out);
if sim.strikes.flights.is_empty() {
break;
}
}
assert!(sim.strikes.flights.is_empty());
assert_eq!(sim.strikes.fired, 1);
assert_eq!(sim.strikes.destroyed + sim.strikes.stunned + sim.strikes.missed, 1, "{:?}", out.messages);
assert!(sim.sites.get(id).is_none(), "the old base stays on the map for ever");
assert!(sim.strikes.flights.is_empty());
}
#[test]
fn a_wave_keeps_the_site_a_drone_left_from_until_that_is_gone() {
let mut sim = Sim::new(11);
let id = sim.sites.add(LaunchSite {
id: 0,
side: 0,
sector: 0,
bearing: 0.0,
class: SiteClass::Ramp,
pos: DVec3::new(0.0, 64.0, 300.0),
range: 300.0,
water: false,
stock: 1,
planned: 1,
alive: true,
next_free: 0,
offmap: false,
});
let site = sim.sites.get(id).unwrap().clone();
let d = sim.make_drone(DroneKind::Shahed, site.muzzle(0), -DVec3::Z, DVec3::new(0.0, 70.0, 0.0));
sim.sites.job(d.id, id);
sim.queue.push((1, d));
let (mut ground, mut out) = (Ground::default(), Out::default());
// Let the drone leave the ramp.
let mut left = false;
for _ in 0..200 {
sim.step(&mut ground, None, &mut out);
left |= sim.drones.iter().any(|d| d.alive());
if left {
break;
}
}
assert!(left, "the drone never left");
let refs = sim.site_refs();
assert!(refs.contains(&id), "a drone in the air is no reference to the site it left");
sim.sites.retire(&[], &refs);
assert!(sim.sites.get(id).is_some(), "the site of a flying drone was taken away");
// Once the drone is gone, the site is let go: no old bases pile up.
sim.drones.clear();
let refs = sim.site_refs();
sim.sites.prune(&refs);
assert!(sim.sites.get(id).is_none(), "an old base stays after its last drone");
}
}