Give the enemy a structured intent and a durable route queue (#127)
Stage 2 of #127. A smart drone now carries why it is aimed where it is (Intent: plan, sead, react, replace) apart from the aim bookkeeping (unit, since, switches, confidence). Assignments are throttled per adaptive tick and per contact, a fresh disclosure replaces an aim only after a hold, within a switch cap and when the change is worth a new route, and a killed aim is replaced as a separate, lower-priority decision. The queue is bounded and taken by priority (plan > sead > react > replace); unknown land is no longer free in a replanned route. The autosave now carries the air state: the drones in flight, the launch queue, every pending job with its id, aim, unit, version, priority, intent and relief version, the contacts the jobs were chosen from and the relief snapshots they plan over. The format is a versioned `air 1` section; older saves read as before, scenarios do not carry the sky, stale, ghost and duplicate jobs are cancelled on load, and the section is bounded (512 drones, 512 route points, 4096 contacts, 8 reliefs, 900 KB). A periodic save waits while a raid is only being planned. enemy_picture() now explains targets and pending jobs (aims, aimed_by, pending_jobs, reason, work_per_tick) in English, and intel_bench records the queue and intent evidence next to completion, losses and failures.
This commit is contained in:
+91
-2
@@ -886,7 +886,13 @@ impl AirDef {
|
||||
|
||||
/// The state as text, lines of words: the autosave's format, and the body of a scenario.
|
||||
/// `fresh`: every system and object whole, magazines full (a scenario to replay from the start).
|
||||
/// The air state (the raid's drones, launch queue, retarget jobs and the picture behind them,
|
||||
/// #127) belongs to the autosave only: a scenario is a start state, not a raid frozen mid-flight.
|
||||
fn state_text(&self, fresh: bool, view: bool) -> String {
|
||||
self.state_text_with(fresh, view, !fresh)
|
||||
}
|
||||
|
||||
fn state_text_with(&self, fresh: bool, view: bool, air: bool) -> String {
|
||||
let mut text = String::new();
|
||||
for s in &self.sim.sams {
|
||||
// Then its ammunition (missiles, bursts, out and waiting for a truck, reload ticks
|
||||
@@ -939,11 +945,27 @@ impl AirDef {
|
||||
for (i, s) in self.sim.sams.iter().enumerate().filter(|(_, s)| !s.doctrine.is_default()) {
|
||||
text += &format!("doctrine {i} {}\n", s.doctrine.save_text());
|
||||
}
|
||||
// The enemy's air state, last: it names its own format version, so an older build (or an
|
||||
// older save) reads the layout and ignores it (#127).
|
||||
if air && let Some(a) = self.sim.air_text() {
|
||||
text += &a;
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
fn save(&mut self) {
|
||||
let text = self.state_text(false, true);
|
||||
self.save_with(true);
|
||||
}
|
||||
|
||||
/// Writes the state. `air`: include the enemy's air state (#127). A raid still being *planned* has
|
||||
/// no durable form — its items, sites and routes are half made — so a periodic save waits for it
|
||||
/// to be launched (at most `sites::SEARCH_TICKS`), while a world change takes the layout alone.
|
||||
fn save_with(&mut self, air: bool) {
|
||||
if air && self.sim.planning() {
|
||||
self.next_save = self.sim.tick + 20;
|
||||
return;
|
||||
}
|
||||
let text = if air { self.state_text(false, true) } else { self.state_text_with(false, true, false) };
|
||||
// Clean only once written: a failed write is tried again after a pause.
|
||||
match storage::set(&self.state_key(), text.as_bytes()) {
|
||||
Ok(()) => {
|
||||
@@ -993,7 +1015,15 @@ impl AirDef {
|
||||
/// naming the line and field, and the scene stays as it was (#86).
|
||||
fn apply_state(&mut self, text: &str) -> Result<(), String> {
|
||||
let layout = layout::Layout::parse(text)?;
|
||||
// The air section is checked before anything is touched, like the layout (#127).
|
||||
let air = sim::Sim::parse_air(text)?;
|
||||
self.commit_layout(layout);
|
||||
if let Some(air) = air {
|
||||
let cancelled = self.sim.restore_air(air);
|
||||
if cancelled > 0 {
|
||||
say(&format!("Воздушная обстановка восстановлена: отменено устаревших заданий {cancelled}"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1134,6 +1164,10 @@ fn durable_mark(sim: &Sim) -> u64 {
|
||||
.fold(sim.sams.len() as u64, |a, h| a.wrapping_mul(31).wrapping_add(h))
|
||||
.wrapping_mul(31)
|
||||
.wrapping_add(strikes)
|
||||
// The air state's shape (#127): a launch, a loss, a new or finished route job makes the
|
||||
// autosave take the raid as it stands, so a reload resumes it rather than an empty sky.
|
||||
.wrapping_mul(31)
|
||||
.wrapping_add(sim.air_mark())
|
||||
}
|
||||
|
||||
impl AirDef {
|
||||
@@ -1158,7 +1192,9 @@ impl AirDef {
|
||||
/// Another world opened: the last one's state saved, a clean simulation with this one's loaded.
|
||||
fn open_world(&mut self, world: Option<String>) {
|
||||
if self.world.is_some() && self.dirty {
|
||||
self.save();
|
||||
// Leaving the world: the layout is kept, a raid still being planned is not (it would have
|
||||
// no drones to resume and the world it belonged to is going).
|
||||
self.save_with(false);
|
||||
}
|
||||
self.end_session(presentation::ExitCause::WorldChange);
|
||||
let shader = self.sim.shader;
|
||||
@@ -2951,4 +2987,57 @@ mod tests {
|
||||
sim.strikes.progress = sim::strike::TUNE.progress_save;
|
||||
assert_ne!(durable_mark(&sim), checkpoint, "a refill checkpoint is not saved");
|
||||
}
|
||||
|
||||
/// #127: the air section of a save is written and read by the adapter, and a save without it (an
|
||||
/// older one) still loads as before. The section itself is checked in the core's own tests; here
|
||||
/// the contract is that the text the game writes is the text the next load restores, and that a
|
||||
/// state with a sky replaces a state without one rather than merging into it.
|
||||
#[test]
|
||||
fn the_air_section_of_a_save_is_written_and_read_by_the_adapter() {
|
||||
let zeros = "0 ".repeat(24);
|
||||
let air = format!(
|
||||
"air 1 120 2 0 1 1 1 1\n\
|
||||
airintel {zeros}\n\
|
||||
airglobal -\n\
|
||||
airrelief 0 0 0 16 8 u64\n\
|
||||
aircontact 32 s400 0 70 0 100 emission - - 0\n\
|
||||
airqueue 200 48 shahed 0 90 -600 0 1 0 0 70 0 cruise react 32 120 0 confirmed 0 0 0 0 0 0 false false true 0 - 0 - -\n\
|
||||
airjob 1 48 0 70 0 32 120 1 react 0\n"
|
||||
);
|
||||
let saved = format!("sam {} 0 70 300 0\n{air}", SamKind::S400.key());
|
||||
let mut game = test_mod();
|
||||
game.apply_state(&saved).expect("a saved raid must load");
|
||||
assert_eq!(game.sim.tick, 120);
|
||||
assert_eq!(game.sim.queue.len(), 1, "the launch queue is not restored");
|
||||
assert_eq!(game.sim.queue[0].0, 200);
|
||||
assert_eq!(game.sim.drones.len(), 0);
|
||||
let report = game.sim.enemy_picture();
|
||||
assert_eq!(report.pending.len(), 1, "the pending route job is not restored");
|
||||
assert_eq!(report.pending[0].intent, "react");
|
||||
assert_eq!(report.pending[0].unit, 32);
|
||||
assert_eq!(report.contacts.len(), 1);
|
||||
|
||||
// The game writes the same section back: what it read is what it saves.
|
||||
let back = game.state_text(false, false);
|
||||
assert!(back.contains("\nair 1 120 2 0 1 1 1 1\n"), "{back}");
|
||||
assert!(back.contains("\nairjob 1 48 0.00 70.00 0.00 32 120 1 react 0\n"), "{back}");
|
||||
assert!(back.contains("\nairqueue 200 48 "), "{back}");
|
||||
|
||||
// The air state is replaced, not merged: a save of a quiet world clears the sky.
|
||||
let quiet = format!("sam {} 0 70 300 0\n", SamKind::S400.key());
|
||||
let quiet = format!("{quiet}air 1 300 2 0 0 0 0 0\nairintel {zeros}\nairglobal -\n");
|
||||
game.apply_state(&quiet).unwrap();
|
||||
assert!(game.sim.queue.is_empty() && game.sim.enemy_picture().pending.is_empty(), "an empty sky merged with the loaded one");
|
||||
|
||||
// An older save has no section at all: it loads its layout and leaves the sky as it was.
|
||||
let mut old = test_mod();
|
||||
old.apply_state(&format!("sam {} 0 70 0 0\n", SamKind::S400.key())).unwrap();
|
||||
assert_eq!(old.sim.sams.len(), 1);
|
||||
assert!(sim::Sim::parse_air("sam s400 0 70 0 0\n").unwrap().is_none());
|
||||
|
||||
// A scenario's body is a start state: it does not carry the sky.
|
||||
let scenario = game.scenario_text("t", false);
|
||||
assert!(!scenario.contains("\nair 1 "), "a scenario carries the air state");
|
||||
assert!(sim::Sim::parse_air(&scenario).unwrap().is_none());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user