State per world, damage saved, /airdef import
build / build (push) Failing after 26m9s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 11:25:45 +03:00
parent 94170823cb
commit 3443bafedf
11 changed files with 211 additions and 12 deletions
+73 -4
View File
@@ -58,6 +58,10 @@ struct AirDef {
replay: Option<replay::Player>,
/// The world drawn beyond the render distance (replays and cameras see far off).
far: 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.
health_mark: u64,
/// The menu's page while it is open.
menu: Option<Page>,
/// Where the player is, for the sounds' delay and far variants.
@@ -362,14 +366,18 @@ impl AirDef {
for (kind, roles) in &self.sim.roles {
text += &format!("roles {} {}\n", kind.key(), roles);
}
if let Err(e) = storage::set("state", text.as_bytes()) {
if let Err(e) = storage::set(&self.state_key(), text.as_bytes()) {
horizon_mod_sdk::warn(&format!("could not save: {e}"));
}
self.dirty = false;
}
fn load(&mut self) {
let Some(bytes) = storage::get("state") else { return };
self.load_from(&self.state_key());
}
fn load_from(&mut self, key: &str) {
let Some(bytes) = storage::get(key) else { return };
let text = String::from_utf8_lossy(&bytes);
for line in text.lines() {
let w: Vec<&str> = line.split_whitespace().collect();
@@ -417,6 +425,42 @@ impl AirDef {
}
impl AirDef {
/// Where the open world's state is kept: a key of its own per world.
fn state_key(&self) -> String {
match self.world.as_ref().and_then(|w| w.as_ref()) {
Some(w) => {
// FNV-1a: storage keys are short, lowercase and plain.
let h = w.bytes().fold(0xcbf2_9ce4_8422_2325u64, |h, b| (h ^ b as u64).wrapping_mul(0x100_0000_01b3));
format!("state-{h:016x}")
}
None => "state".into(),
}
}
/// 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();
}
if self.replay.is_some() {
self.stop_replay();
}
let shader = self.sim.shader;
let seed = game::get_clock().map_or(0x5eed, |c| c.game_time.wrapping_mul(2654435761) ^ 0x9e37);
self.sim = Sim::new(seed);
self.sim.shader = shader;
self.ground = Ground::default();
self.monitor = None;
self.grid = false;
self.grid_ids.clear();
self.far = true;
self.world = Some(world);
self.load();
self.apply_far();
self.dirty = false;
self.health_mark = 0;
}
/// Distant terrain (48 chunks, coarse) while `far` is on: the raid's whole area has land,
/// in the world and in the cameras.
fn apply_far(&self) {
@@ -640,7 +684,7 @@ impl AirDef {
}
}
const HELP: &str = "/airdef menu | monitor | grid | replay [map|full|reel|stop] | raid | repair | target here | give | deploy <комплекс> | wave <тип|mix> [кол-во] [дальность] | clear | targets clear | damage on|off | shader on|off | hold on|off | roles [комплекс классы] | stats";
const HELP: &str = "/airdef menu | monitor | grid | replay [map|full|reel|stop] | raid | repair | import | target here | give | deploy <комплекс> | wave <тип|mix> [кол-во] [дальность] | clear | targets clear | damage on|off | shader on|off | hold on|off | roles [комплекс классы] | stats";
impl Mod for AirDef {
fn init() -> Result<(Self, Registration), String> {
@@ -657,6 +701,8 @@ impl Mod for AirDef {
wave: wave::WaveConfig::default(),
replay: None,
far: true,
world: None,
health_mark: 0,
menu: None,
listener: DVec3::ZERO,
pending: Vec::new(),
@@ -673,7 +719,6 @@ impl Mod for AirDef {
// Not Enter: it opens the chat.
let _ = input::register_key("rp_pause", "Повтор: пауза", "key.keyboard.space");
let _ = input::register_key("rp_exit", "Повтор: выход", "key.keyboard.backspace");
me.load();
input::claim_items(&ITEMS.map(String::from));
render::set_post(include_str!("../shaders/effects.wgsl"));
me.sim.shader = true;
@@ -683,6 +728,11 @@ impl Mod for AirDef {
fn tick(&mut self, _tick: u64) {
let Some(p) = player() else { return };
// Each world keeps its own systems, objects and settings: loaded when it opens.
let world = game::world_id();
if self.world.as_ref() != Some(&world) {
self.open_world(world);
}
for event in input::poll() {
match event {
Event::UseStart(u) => self.use_item(&u.item.id),
@@ -779,6 +829,18 @@ impl Mod for AirDef {
}
}
}
// Damage is saved too: a destroyed system stays destroyed.
let mark = self
.sim
.sams
.iter()
.map(|s| (s.health * 1000.0) as u64)
.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));
if mark != self.health_mark {
self.health_mark = mark;
self.dirty = true;
}
if self.dirty {
self.save();
}
@@ -988,6 +1050,13 @@ impl Mod for AirDef {
self.launch_wave(&mut out)?;
None
}
["import"] => {
// The state the mod kept before worlds had their own: brought into this world.
let before = self.sim.sams.len();
self.load_from("state");
self.dirty = true;
Some(format!("Перенесено комплексов из старой версии: {}", self.sim.sams.len() - before))
}
["repair"] => {
let n = self.sim.repair();
self.dirty = true;