feat: add versioned simulation state records

This commit is contained in:
2026-07-05 12:52:07 +02:00
parent c690937afa
commit 30cac28fc0
12 changed files with 514 additions and 1 deletions
+88
View File
@@ -0,0 +1,88 @@
class_name NPCStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func capture(npc: SimNPC) -> NPCStateRecord:
return NPCStateRecord.new({
"schema_version": SCHEMA_VERSION,
"id": npc.id,
"name": npc.npc_name,
"profession": npc.profession,
"hunger": npc.hunger,
"energy": npc.energy,
"strength": npc.strength,
"intelligence": npc.intelligence,
"current_task": npc.current_task,
"task_state": npc.task_state,
"position": [npc.position.x, npc.position.y, npc.position.z],
"is_starving": npc.is_starving,
"starvation_ticks": npc.starvation_ticks,
"starvation_death_threshold": npc.starvation_death_threshold,
"is_dead": npc.is_dead,
"task_duration": npc.task_duration,
"task_progress": npc.task_progress,
"task_complete": npc.task_complete,
"target_id": String(npc.target_id),
"last_task": npc.last_task,
"random_seed": str(npc.random_source.seed),
"random_state": str(npc.random_source.state)
})
static func from_dictionary(record_data: Dictionary) -> NPCStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not record_data.has_all([
"id", "name", "profession", "hunger", "energy", "strength",
"intelligence", "current_task", "task_state", "position",
"is_starving", "starvation_ticks", "starvation_death_threshold",
"is_dead", "task_duration", "task_progress", "task_complete",
"target_id", "last_task", "random_seed", "random_state"
]):
return null
var saved_position = record_data["position"]
if not saved_position is Array or saved_position.size() != 3:
return null
return NPCStateRecord.new(record_data)
func restore(debug_logs: bool) -> SimNPC:
var random_source := RandomNumberGenerator.new()
random_source.seed = String(data["random_seed"]).to_int()
var npc := SimNPC.new(
int(data["id"]),
String(data["name"]),
String(data["profession"]),
float(data["strength"]),
float(data["intelligence"]),
random_source
)
var saved_position: Array = data["position"]
npc.hunger = float(data["hunger"])
npc.energy = float(data["energy"])
npc.current_task = String(data["current_task"])
npc.task_state = String(data["task_state"])
npc.position = Vector3(
float(saved_position[0]),
float(saved_position[1]),
float(saved_position[2])
)
npc.is_starving = bool(data["is_starving"])
npc.starvation_ticks = int(data["starvation_ticks"])
npc.starvation_death_threshold = int(data["starvation_death_threshold"])
npc.is_dead = bool(data["is_dead"])
npc.task_duration = float(data["task_duration"])
npc.task_progress = float(data["task_progress"])
npc.task_complete = bool(data["task_complete"])
npc.target_id = StringName(data["target_id"])
npc.last_task = String(data["last_task"])
npc.random_source.state = String(data["random_state"]).to_int()
npc.debug_logs = debug_logs
return npc
func to_dictionary() -> Dictionary:
return data.duplicate(true)
+1
View File
@@ -0,0 +1 @@
uid://wf605vpwv8vn
+42
View File
@@ -0,0 +1,42 @@
class_name ResourceStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func capture(node: ResourceNode) -> ResourceStateRecord:
return ResourceStateRecord.new({
"schema_version": SCHEMA_VERSION,
"node_id": String(node.node_id),
"amount_remaining": node.amount_remaining,
"reserved_by": node.reserved_by,
"enabled": node.enabled
})
static func from_dictionary(record_data: Dictionary) -> ResourceStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not record_data.has_all([
"node_id", "amount_remaining", "reserved_by", "enabled"
]):
return null
if String(record_data["node_id"]).is_empty():
return null
return ResourceStateRecord.new(record_data)
func restore(node: ResourceNode) -> bool:
if node == null or node.node_id != StringName(data["node_id"]):
return false
node.restore_persistent_state(
float(data["amount_remaining"]),
int(data["reserved_by"]),
bool(data["enabled"])
)
return true
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://pflq8apn87d6
+108
View File
@@ -0,0 +1,108 @@
class_name SimulationStateRecord
extends RefCounted
const SCHEMA_NAME := "the_steward.simulation"
const SCHEMA_VERSION := 1
var simulation: Dictionary
var village: VillageStateRecord
var npcs: Array[NPCStateRecord] = []
var resources: Array[ResourceStateRecord] = []
func to_dictionary() -> Dictionary:
var npc_data: Array[Dictionary] = []
for npc_record in npcs:
npc_data.append(npc_record.to_dictionary())
var resource_data: Array[Dictionary] = []
for resource_record in resources:
resource_data.append(resource_record.to_dictionary())
return {
"schema": SCHEMA_NAME,
"schema_version": SCHEMA_VERSION,
"simulation": simulation.duplicate(true),
"village": village.to_dictionary(),
"npcs": npc_data,
"resources": resource_data
}
func to_json() -> String:
return JSON.stringify(to_dictionary())
static func from_json(json_text: String) -> SimulationStateRecord:
var parsed = JSON.parse_string(json_text)
if not parsed is Dictionary:
return null
return from_dictionary(parsed)
static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
if record_data.get("schema", "") != SCHEMA_NAME:
return null
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not record_data.has_all([
"simulation", "village", "npcs", "resources"
]):
return null
var simulation_data = record_data["simulation"]
if not simulation_data is Dictionary:
return null
if not simulation_data.has_all([
"seed", "tick_interval", "tick_count", "clock_accumulator",
"clock_elapsed_ticks", "wander_random_streams"
]):
return null
var wander_streams = simulation_data["wander_random_streams"]
if not wander_streams is Array:
return null
for stream_data in wander_streams:
if not stream_data is Dictionary:
return null
if not stream_data.has_all(["npc_id", "seed", "state"]):
return null
var village_data = record_data["village"]
if not village_data is Dictionary:
return null
var village_record := VillageStateRecord.from_dictionary(village_data)
if village_record == null:
return null
var npc_data = record_data["npcs"]
var resource_data = record_data["resources"]
if not npc_data is Array or not resource_data is Array:
return null
var record := SimulationStateRecord.new()
record.simulation = simulation_data.duplicate(true)
record.village = village_record
var npc_ids := {}
for item in npc_data:
if not item is Dictionary:
return null
var npc_record := NPCStateRecord.from_dictionary(item)
if npc_record == null:
return null
var npc_id := int(npc_record.data["id"])
if npc_ids.has(npc_id):
return null
npc_ids[npc_id] = true
record.npcs.append(npc_record)
var resource_ids := {}
for item in resource_data:
if not item is Dictionary:
return null
var resource_record := ResourceStateRecord.from_dictionary(item)
if resource_record == null:
return null
var resource_id := String(resource_record.data["node_id"])
if resource_ids.has(resource_id):
return null
resource_ids[resource_id] = true
record.resources.append(resource_record)
return record
@@ -0,0 +1 @@
uid://nmke245eadkk
+39
View File
@@ -0,0 +1,39 @@
class_name VillageStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func capture(village: SimVillage) -> VillageStateRecord:
return VillageStateRecord.new({
"schema_version": SCHEMA_VERSION,
"food": village.food,
"wood": village.wood,
"safety": village.safety,
"knowledge": village.knowledge
})
static func from_dictionary(record_data: Dictionary) -> VillageStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not record_data.has_all(["food", "wood", "safety", "knowledge"]):
return null
return VillageStateRecord.new(record_data)
func restore(debug_logs: bool) -> SimVillage:
var village := SimVillage.new()
village.debug_logs = debug_logs
village.food = float(data["food"])
village.wood = float(data["wood"])
village.safety = float(data["safety"])
village.knowledge = float(data["knowledge"])
village.update_modifiers()
village.update_priorities()
return village
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://cipncpfigveuh