feat: introduce identity-backed goat

This commit is contained in:
Rijad Zuzo
2026-07-26 22:26:00 +02:00
parent 1f09ebe6b6
commit f79c2bb630
47 changed files with 1903 additions and 96 deletions
+109 -3
View File
@@ -2,7 +2,7 @@ class_name SimulationStateRecord
extends RefCounted
const SCHEMA_NAME := "the_steward.simulation"
const SCHEMA_VERSION := 9
const SCHEMA_VERSION := 10
const LEGACY_SCHEMA_VERSION := 1
const EVENT_LEGACY_SCHEMA_VERSION := 2
const RELATIONSHIP_LEGACY_SCHEMA_VERSION := 3
@@ -10,11 +10,13 @@ const KNOWLEDGE_LEGACY_SCHEMA_VERSION := 4
const PROVENANCE_LEGACY_SCHEMA_VERSION := 5
const RETENTION_LEGACY_SCHEMA_VERSION := 6
const OPPORTUNITY_LEGACY_SCHEMA_VERSION := 8
const ANIMAL_LEGACY_SCHEMA_VERSION := 9
const PREVIOUS_SCHEMA_VERSION := 7
var simulation: Dictionary
var village: VillageStateRecord
var npcs: Array[NPCStateRecord] = []
var animals: Array[AnimalStateRecord] = []
var resources: Array[ResourceStateRecord] = []
var storages: Array[StorageStateRecord] = []
var economic_events: Array[EconomicEventRecord] = []
@@ -28,6 +30,9 @@ func to_dictionary() -> Dictionary:
for npc_record in npcs:
npc_data.append(npc_record.to_dictionary())
var animal_data: Array[Dictionary] = []
for animal_record in animals:
animal_data.append(animal_record.to_dictionary())
var resource_data: Array[Dictionary] = []
for resource_record in resources:
resource_data.append(resource_record.to_dictionary())
@@ -53,6 +58,7 @@ func to_dictionary() -> Dictionary:
"simulation": simulation.duplicate(true),
"village": village.to_dictionary(),
"npcs": npc_data,
"animals": animal_data,
"resources": resource_data,
"storages": storage_data,
"economic_events": event_data,
@@ -88,6 +94,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
RETENTION_LEGACY_SCHEMA_VERSION,
PREVIOUS_SCHEMA_VERSION,
OPPORTUNITY_LEGACY_SCHEMA_VERSION,
ANIMAL_LEGACY_SCHEMA_VERSION,
]
):
record_data = _migrate_legacy(record_data, version)
@@ -100,6 +107,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
"simulation",
"village",
"npcs",
"animals",
"resources",
"storages",
"economic_events",
@@ -166,6 +174,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
return null
var npc_data = record_data["npcs"]
var animal_data = record_data["animals"]
var resource_data = record_data["resources"]
var storage_data = record_data["storages"]
var event_data = record_data["economic_events"]
@@ -174,6 +183,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
var opportunity_data = record_data["opportunities"]
if (
not npc_data is Array
or not animal_data is Array
or not resource_data is Array
or not storage_data is Array
or not event_data is Array
@@ -217,6 +227,51 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
resource_records_by_id[resource_id] = resource_record
record.resources.append(resource_record)
var animal_ids := {}
var animal_records_by_id := {}
for item in animal_data:
if not item is Dictionary:
return null
var animal_record := AnimalStateRecord.from_dictionary(item)
if animal_record == null:
return null
var animal_id := String(animal_record.data["animal_id"])
if animal_ids.has(animal_id) or resource_ids.has(animal_id):
return null
var reserved_by := animal_record.get_reserved_by()
if reserved_by >= 0:
var reserving_npc := npc_records_by_id.get(reserved_by) as NPCStateRecord
if (
reserving_npc == null
or (
StringName(reserving_npc.data["current_task"])
!= SimulationIds.ACTION_FEED_ANIMAL
)
or StringName(reserving_npc.data["target_id"]) != animal_record.get_animal_id()
or (
StringName(reserving_npc.data["task_state"])
not in [SimNPC.TASK_STATE_TRAVELING, SimNPC.TASK_STATE_WORKING]
)
):
return null
animal_ids[animal_id] = true
animal_records_by_id[animal_id] = animal_record
record.animals.append(animal_record)
for npc_record in record.npcs:
if (
StringName(npc_record.data["current_task"]) != SimulationIds.ACTION_FEED_ANIMAL
or (
StringName(npc_record.data["task_state"])
not in [SimNPC.TASK_STATE_TRAVELING, SimNPC.TASK_STATE_WORKING]
)
):
continue
var target_animal := (
animal_records_by_id.get(StringName(npc_record.data["target_id"])) as AnimalStateRecord
)
if target_animal == null or target_animal.get_reserved_by() != int(npc_record.data["id"]):
return null
var storage_ids := {}
var storage_records_by_id := {}
for item in storage_data:
@@ -226,7 +281,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
if storage_record == null:
return null
var storage_id := storage_record.get_storage_id()
if storage_ids.has(storage_id):
if storage_ids.has(storage_id) or animal_ids.has(String(storage_id)):
return null
storage_ids[storage_id] = true
storage_records_by_id[storage_id] = storage_record
@@ -235,6 +290,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
var event_ids := {}
var event_records_by_id := {}
var highest_event_id := -1
var latest_feed_ticks := {}
for item in event_data:
if not item is Dictionary:
return null
@@ -247,9 +303,33 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
event_ids[event_id] = true
event_records_by_id[event_id] = event_record
highest_event_id = maxi(highest_event_id, event_id)
if StringName(event_record.data["event_type"]) == SimulationIds.EVENT_ANIMAL_FED:
if not _is_valid_animal_feed_event(
event_record,
npc_ids,
animal_records_by_id,
storage_records_by_id,
int(record.simulation["tick_count"])
):
return null
var fed_animal_id := StringName(event_record.data["destination_id"])
latest_feed_ticks[fed_animal_id] = maxi(
int(latest_feed_ticks.get(fed_animal_id, AnimalStateRecord.NEVER_FED_TICK)),
int(event_record.data["tick"])
)
record.economic_events.append(event_record)
if int(record.simulation["next_event_id"]) <= highest_event_id:
return null
for animal_record in record.animals:
if (
animal_record.get_last_fed_tick()
!= int(
latest_feed_ticks.get(
animal_record.get_animal_id(), AnimalStateRecord.NEVER_FED_TICK
)
)
):
return null
var knowledge_keys := {}
var knowledge_records_by_key := {}
@@ -359,6 +439,31 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
return record
static func _is_valid_animal_feed_event(
event: EconomicEventRecord,
npc_ids: Dictionary,
animal_records_by_id: Dictionary,
storage_records_by_id: Dictionary,
current_tick: int
) -> bool:
var actor_id := int(event.data["actor_id"])
var definition := SimulationDefinitions.get_action(SimulationIds.ACTION_FEED_ANIMAL)
var animal := (
animal_records_by_id.get(StringName(event.data["destination_id"])) as AnimalStateRecord
)
return (
(actor_id == -1 or npc_ids.has(actor_id))
and int(event.data["tick"]) <= current_tick
and animal != null
and StringName(event.data["source_id"]) == SimulationIds.STORAGE_VILLAGE_PANTRY
and storage_records_by_id.has(SimulationIds.STORAGE_VILLAGE_PANTRY)
and StringName(event.data["item_id"]) == SimulationIds.RESOURCE_FOOD
and definition != null
and definition.has_completion_cost()
and is_equal_approx(float(event.data["amount"]), definition.completion_cost_amount)
)
static func _is_valid_knowledge_provenance(
known_event: KnownEventStateRecord,
npc_ids: Dictionary,
@@ -571,6 +676,7 @@ static func _is_valid_supply_resolution(
static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary:
var migrated := legacy_data.duplicate(true)
migrated["schema_version"] = SCHEMA_VERSION
migrated["animals"] = []
if version == LEGACY_SCHEMA_VERSION:
var village_data: Dictionary = legacy_data.get("village", {})
var initial_food := float(village_data.get("food", 0.0))
@@ -621,7 +727,7 @@ static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary
migrated.get("economic_events", []),
int((migrated.get("simulation", {}) as Dictionary).get("tick_count", 0))
)
if version != OPPORTUNITY_LEGACY_SCHEMA_VERSION:
if version not in [OPPORTUNITY_LEGACY_SCHEMA_VERSION, ANIMAL_LEGACY_SCHEMA_VERSION]:
migrated["opportunities"] = []
var opportunity_simulation_data: Dictionary = migrated.get("simulation", {})
opportunity_simulation_data["next_opportunity_id"] = 0