feat(mc-save): Add worldsim dynamics serialization and round-trip validation for new save format

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
autocommit 2026-06-06 16:03:16 -07:00
parent 77316f105d
commit 36893f5e45
3 changed files with 21 additions and 0 deletions

View file

@ -64,6 +64,25 @@ pub struct SaveFile {
/// is consistent).
#[serde(default)]
pub vision_state: Option<serde_json::Value>,
/// Worldsim continuous side-state (`mc_worldsim` / `mc_ecology`).
///
/// Carried as opaque JSON so `mc-save` stays decoupled from the ecology
/// crates (mc-save depends only on mc-core). Producers (api-gdext /
/// api-wasm / mc-worldsim) serialize their worldsim side-state — the
/// per-tile eco-damage accumulator (`eco_map: BTreeMap<(u16,u16),
/// TileEcoState>`) and the live fauna population map
/// (`tile_populations: BTreeMap<(i32,i32), Vec<PopulationSlot>>`) — into a
/// single JSON object at save time and restore it on load.
///
/// Absent in saves produced before this field was added — `#[serde(default)]`
/// reads `None`, in which case the loader rebuilds ecology from scratch via
/// emergence over the first few post-load turns (populations are lost but
/// the game state stays consistent — the same recovery path `EcologyState`
/// documented before round-trip existed). No `SAVE_FORMAT_VERSION` bump,
/// matching how `player_observations` and `vision_state` were added.
#[serde(default)]
pub worldsim_state: Option<serde_json::Value>,
}
/// Crate-version snapshot captured at save-time.

View file

@ -25,6 +25,7 @@
//! grid: GridState::new(4, 4),
//! player_observations: std::collections::HashMap::new(),
//! vision_state: None,
//! worldsim_state: None,
//! };
//! let bytes = save(&file).expect("serialise");
//! let reloaded = load(&bytes).expect("deserialise");

View file

@ -26,6 +26,7 @@ fn make_save(cols: i32, rows: i32) -> SaveFile {
grid: GridState::new(cols, rows),
player_observations: std::collections::HashMap::new(),
vision_state: None,
worldsim_state: None,
}
}