Files
gamedev-the-steward/simulation/knowledge/EventKnowledgeSystem.gd
T
2026-07-11 19:58:47 +02:00

147 lines
4.3 KiB
GDScript

class_name EventKnowledgeSystem
extends RefCounted
const WITNESS_RADIUS := 10.0
var known_events: Dictionary = {}
func observe_event(event: EconomicEventRecord, npcs: Array[SimNPC]) -> Array[KnownEventStateRecord]:
var learned: Array[KnownEventStateRecord] = []
if not _is_witnessable(event):
return learned
var actor := _find_npc(int(event.data["actor_id"]), npcs)
if actor == null:
return learned
var event_id := int(event.data["event_id"])
var actor_record := _remember(actor.id, event_id, SimulationIds.KNOWLEDGE_ACQUISITION_PERFORMED)
if actor_record != null:
learned.append(actor_record)
var witness_radius_squared := WITNESS_RADIUS * WITNESS_RADIUS
var event_position := event.get_world_position()
for npc in npcs:
if npc.id == actor.id or npc.is_dead:
continue
if npc.position.distance_squared_to(event_position) > witness_radius_squared:
continue
var witness_record := _remember(
npc.id, event_id, SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED
)
if witness_record != null:
learned.append(witness_record)
learned.sort_custom(_sort_records)
return learned
func communicate_event(
event: EconomicEventRecord, speaker_id: int, listener_id: int
) -> KnownEventStateRecord:
if (
event == null
or speaker_id == listener_id
or listener_id == int(event.data["actor_id"])
or not _is_witnessable(event)
):
return null
var event_id := int(event.data["event_id"])
var speaker_record := get_record(speaker_id, event_id)
if speaker_record == null:
return null
if (
speaker_record.get_acquisition_method()
not in [
SimulationIds.KNOWLEDGE_ACQUISITION_PERFORMED,
SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED,
]
):
return null
return _remember(
listener_id, event_id, SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED, speaker_id
)
func knows_event(knower_id: int, event_id: int) -> bool:
return known_events.has(_key(knower_id, event_id))
func get_record(knower_id: int, event_id: int) -> KnownEventStateRecord:
return known_events.get(_key(knower_id, event_id)) as KnownEventStateRecord
func get_knowers(event_id: int) -> Array[int]:
var knower_ids: Array[int] = []
for record in known_events.values():
if record.get_event_id() == event_id:
knower_ids.append(record.get_knower_id())
knower_ids.sort()
return knower_ids
func get_known_event_ids(knower_id: int, max_count: int = 3) -> Array[int]:
var event_ids: Array[int] = []
for record in known_events.values():
if record.get_knower_id() == knower_id:
event_ids.append(record.get_event_id())
event_ids.sort()
if max_count <= 0 or event_ids.size() <= max_count:
return event_ids
var recent_ids: Array[int] = []
for index in range(event_ids.size() - max_count, event_ids.size()):
recent_ids.append(event_ids[index])
return recent_ids
func get_all_sorted() -> Array[KnownEventStateRecord]:
var records: Array[KnownEventStateRecord] = []
for record in known_events.values():
records.append(record)
records.sort_custom(_sort_records)
return records
func restore(records: Array[KnownEventStateRecord]) -> void:
known_events.clear()
for record in records:
known_events[_key(record.get_knower_id(), record.get_event_id())] = record
func _remember(
knower_id: int,
event_id: int,
acquisition_method: StringName,
source_npc_id: int = KnownEventStateRecord.NO_SOURCE_NPC_ID
) -> KnownEventStateRecord:
var key := _key(knower_id, event_id)
if known_events.has(key):
return null
var record := KnownEventStateRecord.create(
knower_id, event_id, acquisition_method, source_npc_id
)
known_events[key] = record
return record
func _is_witnessable(event: EconomicEventRecord) -> bool:
return (
StringName(event.data["event_type"]) == SimulationIds.EVENT_STORAGE_DEPOSITED
and StringName(event.data["item_id"]) == SimulationIds.RESOURCE_FOOD
and float(event.data["amount"]) > 0.0
)
static func _find_npc(npc_id: int, npcs: Array[SimNPC]) -> SimNPC:
for npc in npcs:
if npc.id == npc_id:
return npc
return null
static func _key(knower_id: int, event_id: int) -> String:
return "%d:%d" % [knower_id, event_id]
static func _sort_records(first: KnownEventStateRecord, second: KnownEventStateRecord) -> bool:
if first.get_knower_id() != second.get_knower_id():
return first.get_knower_id() < second.get_knower_id()
return first.get_event_id() < second.get_event_id()