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
+50 -15
View File
@@ -101,7 +101,7 @@ struct AirDef {
hd: bool,
/// The world whose state is loaded (`game.world-id`), once one is.
world: Option<Option<String>>,
/// The systems' and objects' health as last saved, to save again when it changes.
/// The durable systems, objects and strike stock as last saved, to save again when they change.
health_mark: u64,
/// The menu's page while it is open.
menu: Option<Page>,
@@ -1060,6 +1060,29 @@ fn world_identity() -> Option<String> {
})
}
/// What an autosave must not miss (#7, #113): the systems' places, ammunition and damage, the objects' health, and
/// the strike missiles' stock and refill. A change here marks the durable state dirty; the write itself is still
/// paced by `next_save`, and a failed write is retried.
fn durable_mark(sim: &Sim) -> u64 {
let st = &sim.strikes;
// The stock exactly; the refill at checkpoints, so that a tick-by-tick progress doesn't ask for a save (and a
// full state dump) every tick. A restart is at most `progress_save` ticks (5 s) behind on the next missile.
let strikes = u64::from(st.stock) | u64::from(st.progress / sim::strike::TUNE.progress_save.max(1)) << 8;
sim.sams
.iter()
.map(|s| {
let at = s.dest.unwrap_or(s.pos);
let place = (at.x * 10.0) as i64 as u64 ^ ((at.z * 10.0) as i64 as u64).rotate_left(21) ^ (if s.dest.is_some() { 0 } else { (s.heading * 100.0) as i64 as u64 }).rotate_left(42);
let ammo = u64::from(s.missiles) | u64::from(s.bursts) << 16 | u64::from(s.cargo) << 32 | u64::from(s.empty) << 48 | u64::from(s.reloading > 0) << 49;
let parts = s.comps.0.iter().fold(0u64, |a, v| a.wrapping_mul(131).wrapping_add((v * 100.0) as u64));
((s.health * 1000.0) as u64).wrapping_mul(1_000_003) ^ place ^ ammo.rotate_left(7) ^ parts.rotate_left(13)
})
.chain(sim.target_health.iter().map(|h| (h * 10.0) as u64))
.fold(sim.sams.len() as u64, |a, h| a.wrapping_mul(31).wrapping_add(h))
.wrapping_mul(31)
.wrapping_add(strikes)
}
impl AirDef {
/// The EW stations: on if none is, else off.
fn toggle_ew(&mut self) {
@@ -1889,19 +1912,7 @@ impl Mod for AirDef {
}
// Damage is saved too: a destroyed system stays destroyed. So are where the units stand
// (or drive to: not every step of the way), their ammunition and the depots' stock.
let mark = self
.sim
.sams
.iter()
.map(|s| {
let at = s.dest.unwrap_or(s.pos);
let place = (at.x * 10.0) as i64 as u64 ^ ((at.z * 10.0) as i64 as u64).rotate_left(21) ^ (if s.dest.is_some() { 0 } else { (s.heading * 100.0) as i64 as u64 }).rotate_left(42);
let ammo = u64::from(s.missiles) | u64::from(s.bursts) << 16 | u64::from(s.cargo) << 32 | u64::from(s.empty) << 48 | u64::from(s.reloading > 0) << 49;
let parts = s.comps.0.iter().fold(0u64, |a, v| a.wrapping_mul(131).wrapping_add((v * 100.0) as u64));
((s.health * 1000.0) as u64).wrapping_mul(1_000_003) ^ place ^ ammo.rotate_left(7) ^ parts.rotate_left(13)
})
.chain(self.sim.target_health.iter().map(|h| (h * 10.0) as u64))
.fold(self.sim.sams.len() as u64, |a, h| a.wrapping_mul(31).wrapping_add(h));
let mark = durable_mark(&self.sim);
if mark != self.health_mark {
self.health_mark = mark;
self.dirty = true;
@@ -2208,7 +2219,10 @@ impl Mod for AirDef {
// A strike missile at known site n, from where the player stands.
let p = player().ok_or("Нет игрока")?;
let id = n.parse::<u32>().map_err(|_| "/airdef strike <номер ПУ из /airdef sites>")?;
Some(self.sim.order_strike(id, DVec3::new(p.x, p.y, p.z), &mut out)?)
let said = self.sim.order_strike(id, DVec3::new(p.x, p.y, p.z), &mut out)?;
// The stock went down: the durable state must not forget it (#113).
self.dirty = true;
Some(said)
}
["move", n] => {
// Unit n (as the summary numbers them) drives to where the player stands.
@@ -2355,4 +2369,25 @@ mod tests {
assert!(switch_allowed(true).is_err());
assert!(switch_allowed(false).is_ok());
}
#[test]
fn strike_stock_and_refill_change_the_durable_mark() {
let mut sim = Sim::new(7);
let full = durable_mark(&sim);
sim.strikes.stock -= 1;
assert_ne!(durable_mark(&sim), full, "spending a missile is not saved");
sim.strikes.progress = sim::strike::TUNE.resupply - 1;
let before = durable_mark(&sim);
let (mut ground, mut out) = (sim::Ground::default(), sim::Out::default());
sim.step(&mut ground, None, &mut out);
assert_eq!(sim.strikes.stock, 3);
assert_ne!(durable_mark(&sim), before, "refilling a missile is not saved");
let checkpoint = durable_mark(&sim);
sim.strikes.progress = 1;
assert_eq!(durable_mark(&sim), checkpoint, "progress inside one checkpoint saves every tick");
sim.strikes.progress = sim::strike::TUNE.progress_save;
assert_ne!(durable_mark(&sim), checkpoint, "a refill checkpoint is not saved");
}
}