Plan one Smart order on one knowledge snapshot (#146)
build / mod (push) Failing after 25s

order_raid read one picture and one list of recon marks for the whole
order, but the Plan kept neither: plan_step priced each kind's first
route on whatever picture_of_defences() held at the tick the budget
reached that kind, so one raid could hold several versions of the map
at once. A retarget job carried its version's contacts, yet
price_grid_known aged them at self.tick, so two jobs of one version
reached on different ticks could disagree about a contact's own sensor.

KnowledgeSnapshot fixes the observation tick together with the contacts
and marks read at it. The plan stores the order's snapshot and passes it
explicitly to price_grid_on/price_grid_known; radar_strikes takes the
order's marks instead of re-reading the recon memory. A retarget job
shares its adaptive tick's snapshot and prices age at the snapshot's
tick, never at the current one: current time still cancels a stale job
(prune_air, job_live), separately from the version's own arithmetic, and
an explicitly new retarget gets a new snapshot and may use a fresh
report. The save rebuilds each version's snapshot from the contacts and
the marks reported by it; the section format is unchanged. Nothing here
reads the force: only published kinds and the contacts' own observations.

SDK-free regressions: a Smart order of two kinds whose land answers
starve while a visible contact is disclosed mid-wait keeps every kind's
targets and routes on the order's snapshot (and differs from an order
that knew the contact from the start); two jobs of one version worked
out across the emission-belief boundary price the same world, a new
version with a fresh report believes again, and a new version without
one does not.

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
2026-11-21 09:53:54 +03:00
parent f2bc8550f5
commit 63ebd4ac6c
5 changed files with 237 additions and 54 deletions
+41 -25
View File
@@ -100,6 +100,9 @@ struct Item {
/// A raid being planned: its drones, the prices and routes so far, and the planned ones.
pub(super) struct Plan {
grid: Grid,
/// The one knowledge version the whole order was chosen on and is routed on (#146): every
/// kind's prices are worked out on it, whenever the budget reaches that kind.
knowledge: ew::KnowledgeSnapshot,
prices: HashMap<DroneKind, Vec<f32>>,
routes: HashMap<(DroneKind, i32, i32, i32), Vec<DVec3>>,
items: Vec<Item>,
@@ -280,13 +283,14 @@ impl Sim {
return 0;
}
let smart = order.aim == Aim::Smart;
// The planner's picture of the layout: one snapshot for the whole order. It is
// read-only while the plan is built, and taking it per drone made a big second order
// quadratic in the force's size (#122); a map of contacts makes it linear in them (#127).
let picture = self.picture_of_defences();
// The planner's picture of the layout: one snapshot for the whole order, kept in the plan
// (#146). It is read-only while the plan is built, and taking it per drone made a big second
// order quadratic in the force's size (#122); a map of contacts makes it linear in them (#127).
let knowledge = self.knowledge_now();
let picture = &knowledge.contacts;
// The partial picture the recon flights have radioed home. It is memory, not the
// force: a mark has an area, a category and an age, never a unit's exact place (#135).
let leads = self.recon.leads(self.tick);
let leads = &knowledge.marks;
// A smart enemy comes from where its raids got through best.
let bearing = if smart { self.best_bearing() } else { self.rng.f() * TAU };
// The raid's sides (1, 2, or 4-5): its launch sites go there (#100).
@@ -299,7 +303,7 @@ impl Sim {
let grid = Grid { min: centre - DVec3::new(span, 0.0, span), cell, size: (2.0 * span / cell) as i32 + 1 };
let mut items = Vec::new();
// A smart raid goes for the radars too (a few strikers each); the others pick as they always did.
let radar_jobs = if smart { self.radar_strikes(&order.kinds, &picture) } else { Vec::new() };
let radar_jobs = if smart { self.radar_strikes(&order.kinds, picture, leads) } else { Vec::new() };
let mut index = 0usize;
for (group, &(kind, count)) in order.kinds.iter().enumerate() {
let spec = kind.spec();
@@ -329,8 +333,8 @@ impl Sim {
let confidence = picture
.iter()
.find(|c| c.id == id)
.map(|c| c.confidence(self.tick))
.or_else(|| leads.iter().find(|c| c.id == id).map(|c| c.confidence(self.tick)));
.map(|c| c.confidence(knowledge.tick))
.or_else(|| leads.iter().find(|c| c.id == id).map(|c| c.confidence(knowledge.tick)));
d.intent = if job.is_some() { ew::Intent::Sead } else { ew::Intent::Plan };
d.aim = confidence.map_or_else(|| ew::Aim::point(self.tick), |c| ew::Aim::unit(id, self.tick, c));
} else {
@@ -352,6 +356,7 @@ impl Sim {
}
self.plan = Some(Plan {
grid,
knowledge,
prices: HashMap::new(),
routes: HashMap::new(),
items,
@@ -465,7 +470,7 @@ impl Sim {
None => {
if !plan.prices.contains_key(&kind) {
let heights = (!plan.heights.is_empty()).then_some(plan.heights.as_slice());
let (p, cost) = self.price_grid_on(&plan.grid, kind, heights);
let (p, cost) = self.price_grid_on(&plan.grid, kind, heights, &plan.knowledge);
work += cost;
plan.prices.insert(kind, p);
}
@@ -546,6 +551,13 @@ impl Sim {
self.plan.as_ref().map(|p| p.items.iter().map(|i| (i.d.aim.unit, i.d.intent)).collect()).unwrap_or_default()
}
/// The plan's grid, land, knowledge version and the prices worked out so far (#146; tests).
#[cfg(test)]
pub(super) fn plan_pricing(&self) -> Option<(Grid, Vec<f32>, ew::KnowledgeSnapshot, HashMap<DroneKind, Vec<f32>>)> {
let p = self.plan.as_ref()?;
Some((p.grid, p.heights.clone(), p.knowledge.clone(), p.prices.clone()))
}
/// A drone ready to fly from `from`, not yet in the sky.
pub(super) fn make_drone(&mut self, kind: DroneKind, from: DVec3, dir: DVec3, target: DVec3) -> Drone {
let id = self.id();
@@ -591,10 +603,11 @@ impl Sim {
/// the last observed place and id for those that go for one. Each radar
/// gets two or three, the fast strikers first, and all together no more than about a quarter of the raid.
/// Only contacts the picture still trusts: a doubtful one (aged, never re-seen) is not worth a missile.
pub(super) fn radar_strikes(&self, kinds: &[(DroneKind, u32)], picture: &[ew::Contact]) -> Vec<Option<(DVec3, u32)>> {
pub(super) fn radar_strikes(&self, kinds: &[(DroneKind, u32)], picture: &[ew::Contact], leads: &[recon::Mark]) -> Vec<Option<(DVec3, u32)>> {
// Exact disclosure contacts and recon marks of radars, best first. The order is the
// observed role, freshness and quality -- never the id: a hidden station has no id,
// and a big observed radar is worth more than a modest one (#135).
// and a big observed radar is worth more than a modest one (#135). Both lists are the
// order's own knowledge version, not a later picture (#146).
let now = self.tick;
let mut radars: Vec<(u8, u64, u32, DVec3)> = picture
.iter()
@@ -607,7 +620,7 @@ impl Sim {
};
(seen * 10 + 3, c.tick, c.id, c.at)
})
.chain(self.recon.leads(now).into_iter().filter(|m| m.category == recon::Category::Radar && m.confidence(now) >= ew::Confidence::Probable).map(|m| (m.priority(now), m.observed, m.id, m.area)))
.chain(leads.iter().copied().filter(|m| m.category == recon::Category::Radar && m.confidence(now) >= ew::Confidence::Probable).map(|m| (m.priority(now), m.observed, m.id, m.area)))
.collect();
radars.sort_by(|a, b| b.0.cmp(&a.0).then(b.1.cmp(&a.1)).then(a.2.cmp(&b.2)));
let radars: Vec<(u32, DVec3)> = radars.into_iter().map(|(_, _, id, at)| (id, at)).collect();
@@ -681,25 +694,28 @@ impl Sim {
/// system sees it there (its radar's reach for it, the land) and can shoot it.
#[cfg(test)]
pub(super) fn price_grid(&self, grid: &Grid, kind: DroneKind) -> (Vec<f32>, usize) {
self.price_grid_on(grid, kind, None)
self.price_grid_on(grid, kind, None, &self.knowledge_now())
}
/// The same over the picture this moment holds: a smart plan's own prices are priced on the
/// picture the plan was ordered with (#127). Later observations change nothing here.
pub(super) fn price_grid_on(&self, grid: &Grid, kind: DroneKind, land: Option<&[f32]>) -> (Vec<f32>, usize) {
self.price_grid_known(grid, kind, land, &self.picture_of_defences())
/// The same over a knowledge version given explicitly: a smart plan's prices are priced on the
/// snapshot the plan was ordered with (#127, #146), whenever the budget reaches this kind.
/// Later observations change nothing here.
pub(super) fn price_grid_on(&self, grid: &Grid, kind: DroneKind, land: Option<&[f32]>, knowledge: &ew::KnowledgeSnapshot) -> (Vec<f32>, usize) {
self.price_grid_known(grid, kind, land, knowledge)
}
/// The same over a picture of the defences: a retarget is priced on the picture its target was
/// chosen from, so the jobs of one adaptive tick do not each see a different world (#127). Every
/// The same over a snapshot of the defences: a retarget is priced on the knowledge its target was
/// chosen from, so the jobs of one adaptive tick do not each see a different world (#127), and a
/// job reached on a later tick still ages its contacts at its own version's tick (#146). Every
/// capability read here is the contact's published kind and the contact's own observations —
/// never the unit's actual switch settings, damage, roles, link health or magazine.
pub(super) fn price_grid_known(&self, grid: &Grid, kind: DroneKind, land: Option<&[f32]>, picture: &[ew::Contact]) -> (Vec<f32>, usize) {
pub(super) fn price_grid_known(&self, grid: &Grid, kind: DroneKind, land: Option<&[f32]>, knowledge: &ew::KnowledgeSnapshot) -> (Vec<f32>, usize) {
let mut cost = 0;
let spec = kind.spec();
let height = spec.clearance.max(6.0);
let n = grid.size;
let now = self.tick;
let now = knowledge.tick;
let picture = &knowledge.contacts;
let mut prices = vec![1.0f32; (n * n) as usize];
// Known jamming stations: drones that navigate by satellite keep away.
if kind.jammable() {
@@ -1067,23 +1083,23 @@ mod tests {
sim.sams.push(radar);
let kinds = [(DroneKind::Kh22, 8)];
let none = Vec::new();
assert!(sim.radar_strikes(&kinds, &none).iter().all(Option::is_none));
assert!(sim.radar_strikes(&kinds, &none, &[]).iter().all(Option::is_none));
sim.tick = 20;
sim.intel.observe(&sim.sams[0], sim.tick, ew::Seen::Emission);
// The truth moving after the fix is not an observation: the strike goes to the seen place.
sim.sams[0].pos = DVec3::new(900.0, 70.0, 900.0);
sim.tick = 40;
let picture = sim.picture_of_defences();
let jobs = sim.radar_strikes(&kinds, &picture);
let jobs = sim.radar_strikes(&kinds, &picture, &[]);
assert_eq!(jobs.iter().flatten().count(), 2);
assert!(jobs.iter().flatten().all(|job| *job == (old, 1)));
// An old contact is not worth a strike either.
sim.tick = 3021;
let picture = sim.picture_of_defences();
assert!(sim.radar_strikes(&kinds, &picture).iter().all(Option::is_none), "a doubtful contact was struck");
assert!(sim.radar_strikes(&kinds, &picture, &[]).iter().all(Option::is_none), "a doubtful contact was struck");
sim.tick = 6020;
let picture = sim.picture_of_defences();
assert!(sim.radar_strikes(&kinds, &picture).iter().all(Option::is_none));
assert!(sim.radar_strikes(&kinds, &picture, &[]).iter().all(Option::is_none));
}
#[test]