feat: add structured economic events

This commit is contained in:
2026-07-05 18:04:11 +02:00
parent ebf70a711a
commit f9601a67d1
11 changed files with 358 additions and 17 deletions
+49 -15
View File
@@ -2,14 +2,16 @@ class_name SimulationStateRecord
extends RefCounted
const SCHEMA_NAME := "the_steward.simulation"
const SCHEMA_VERSION := 2
const SCHEMA_VERSION := 3
const LEGACY_SCHEMA_VERSION := 1
const PREVIOUS_SCHEMA_VERSION := 2
var simulation: Dictionary
var village: VillageStateRecord
var npcs: Array[NPCStateRecord] = []
var resources: Array[ResourceStateRecord] = []
var storages: Array[StorageStateRecord] = []
var economic_events: Array[EconomicEventRecord] = []
func to_dictionary() -> Dictionary:
var npc_data: Array[Dictionary] = []
@@ -22,6 +24,9 @@ func to_dictionary() -> Dictionary:
var storage_data: Array[Dictionary] = []
for storage_record in storages:
storage_data.append(storage_record.to_dictionary())
var event_data: Array[Dictionary] = []
for event_record in economic_events:
event_data.append(event_record.to_dictionary())
return {
"schema": SCHEMA_NAME,
@@ -30,7 +35,8 @@ func to_dictionary() -> Dictionary:
"village": village.to_dictionary(),
"npcs": npc_data,
"resources": resource_data,
"storages": storage_data
"storages": storage_data,
"economic_events": event_data
}
func to_json() -> String:
@@ -46,12 +52,13 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
if record_data.get("schema", "") != SCHEMA_NAME:
return null
var version := int(record_data.get("schema_version", -1))
if version == LEGACY_SCHEMA_VERSION:
record_data = _migrate_v1(record_data)
if version in [LEGACY_SCHEMA_VERSION, PREVIOUS_SCHEMA_VERSION]:
record_data = _migrate_legacy(record_data, version)
elif version != SCHEMA_VERSION:
return null
if not record_data.has_all([
"simulation", "village", "npcs", "resources", "storages"
"simulation", "village", "npcs", "resources", "storages",
"economic_events"
]):
return null
@@ -60,7 +67,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
return null
if not simulation_data.has_all([
"seed", "tick_interval", "tick_count", "clock_accumulator",
"clock_elapsed_ticks", "wander_random_streams"
"clock_elapsed_ticks", "wander_random_streams", "next_event_id"
]):
return null
var wander_streams = simulation_data["wander_random_streams"]
@@ -82,10 +89,12 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
var npc_data = record_data["npcs"]
var resource_data = record_data["resources"]
var storage_data = record_data["storages"]
var event_data = record_data["economic_events"]
if (
not npc_data is Array
or not resource_data is Array
or not storage_data is Array
or not event_data is Array
):
return null
@@ -132,17 +141,42 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
storage_ids[storage_id] = true
record.storages.append(storage_record)
var event_ids := {}
var highest_event_id := -1
for item in event_data:
if not item is Dictionary:
return null
var event_record := EconomicEventRecord.from_dictionary(item)
if event_record == null:
return null
var event_id := int(event_record.data["event_id"])
if event_ids.has(event_id):
return null
event_ids[event_id] = true
highest_event_id = maxi(highest_event_id, event_id)
record.economic_events.append(event_record)
if int(record.simulation["next_event_id"]) <= highest_event_id:
return null
return record
static func _migrate_v1(legacy_data: Dictionary) -> Dictionary:
static func _migrate_legacy(
legacy_data: Dictionary,
version: int
) -> Dictionary:
var migrated := legacy_data.duplicate(true)
migrated["schema_version"] = SCHEMA_VERSION
var village_data: Dictionary = legacy_data.get("village", {})
var initial_food := float(village_data.get("food", 0.0))
migrated["storages"] = [
StorageStateRecord.create(
SimulationIds.STORAGE_VILLAGE_PANTRY,
{String(SimulationIds.RESOURCE_FOOD): initial_food}
).to_dictionary()
]
if version == LEGACY_SCHEMA_VERSION:
var village_data: Dictionary = legacy_data.get("village", {})
var initial_food := float(village_data.get("food", 0.0))
migrated["storages"] = [
StorageStateRecord.create(
SimulationIds.STORAGE_VILLAGE_PANTRY,
{String(SimulationIds.RESOURCE_FOOD): initial_food}
).to_dictionary()
]
migrated["economic_events"] = []
var simulation_data: Dictionary = migrated.get("simulation", {})
simulation_data["next_event_id"] = 0
migrated["simulation"] = simulation_data
return migrated