feat(simulation): persist template ownership and references in save state

Adds TemplateOwnership to NpcSaveState and TemplateReferenceMap to
SaveStateV1 so cross-template links survive save/load and tier
eviction (D-025, D-026). Both fields use serde(default) for backward
compatibility with existing saves.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-25 22:33:13 +01:00
co-authored by Claude Opus 4.6
parent 0e8ac56fc1
commit 9eaca0b5a4
2 changed files with 35 additions and 0 deletions
+12
View File
@@ -16,6 +16,7 @@ use thiserror::Error;
use crate::bridge::types::SaveLoadResultWire;
use crate::bridge::types::SnapshotBuffer;
use crate::content::template::TemplateReferenceMap;
use crate::knowledge::graph::KnowledgeGraph;
use crate::knowledge::registry::EntityRegistry;
use crate::npc::Npc;
@@ -105,6 +106,13 @@ pub fn save_to_file(path: &Path, world: &mut World) -> Result<(), SaveLoadError>
npc_states.sort_by_key(|s| s.stable_id.0);
let npc_count = npc_states.len();
// Capture TemplateReferenceMap if present — default to empty if not yet initialised.
let template_references = world
.get_resource::<TemplateReferenceMap>()
.cloned()
.unwrap_or_default();
let state = SaveStateV1 {
format_version: SAVE_FORMAT_VERSION,
tick,
@@ -113,6 +121,7 @@ pub fn save_to_file(path: &Path, world: &mut World) -> Result<(), SaveLoadError>
player_knowledge,
relationship_graph,
npc_states,
template_references,
};
let bytes = state
@@ -200,6 +209,7 @@ pub fn load_from_file(path: &Path, world: &mut World) -> Result<(), SaveLoadErro
// Restore simulation resources.
world.insert_resource(state.relationship_graph);
world.insert_resource(state.template_references);
{
let mut t = world.resource_mut::<SimulationTime>();
t.tick = state.tick;
@@ -487,6 +497,7 @@ mod tests {
#[test]
fn load_from_file_rejects_wrong_format_version() {
use crate::content::template::TemplateReferenceMap;
// Craft a save with a wrong format_version
let bad_state = SaveStateV1 {
format_version: 0xFF, // deliberately wrong
@@ -496,6 +507,7 @@ mod tests {
player_knowledge: KnowledgeGraph::new(),
relationship_graph: RelationshipGraph::new(),
npc_states: vec![],
template_references: TemplateReferenceMap::default(),
};
let bytes = bad_state.to_bytes().expect("serialize");
let path = temp_path();