119 lines
3.0 KiB
GDScript
119 lines
3.0 KiB
GDScript
class_name KnownEventStateRecord
|
|
extends RefCounted
|
|
|
|
const SCHEMA_VERSION := 3
|
|
const NO_SOURCE_NPC_ID := -1
|
|
|
|
const VALID_ACQUISITION_METHODS := [
|
|
SimulationIds.KNOWLEDGE_ACQUISITION_PERFORMED,
|
|
SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED,
|
|
SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED,
|
|
SimulationIds.KNOWLEDGE_ACQUISITION_LEGACY,
|
|
]
|
|
const DIRECT_ACQUISITION_METHODS := [
|
|
SimulationIds.KNOWLEDGE_ACQUISITION_PERFORMED,
|
|
SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED,
|
|
]
|
|
|
|
var data: Dictionary
|
|
|
|
|
|
func _init(record_data: Dictionary = {}) -> void:
|
|
data = record_data.duplicate(true)
|
|
|
|
|
|
static func create(
|
|
knower_id: int,
|
|
event_id: int,
|
|
acquisition_method: StringName = SimulationIds.KNOWLEDGE_ACQUISITION_LEGACY,
|
|
source_npc_id: int = NO_SOURCE_NPC_ID,
|
|
acquired_tick: int = 0,
|
|
source_acquisition_method: StringName = &""
|
|
) -> KnownEventStateRecord:
|
|
return (
|
|
KnownEventStateRecord
|
|
. new(
|
|
{
|
|
"schema_version": SCHEMA_VERSION,
|
|
"knower_id": knower_id,
|
|
"event_id": event_id,
|
|
"acquisition_method": String(acquisition_method),
|
|
"source_npc_id": source_npc_id,
|
|
"acquired_tick": acquired_tick,
|
|
"source_acquisition_method": String(source_acquisition_method),
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
static func from_dictionary(record_data: Dictionary) -> KnownEventStateRecord:
|
|
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
|
return null
|
|
if not (
|
|
record_data
|
|
. has_all(
|
|
[
|
|
"knower_id",
|
|
"event_id",
|
|
"acquisition_method",
|
|
"source_npc_id",
|
|
"acquired_tick",
|
|
"source_acquisition_method",
|
|
]
|
|
)
|
|
):
|
|
return null
|
|
var knower_id := int(record_data["knower_id"])
|
|
var event_id := int(record_data["event_id"])
|
|
var acquisition_method := StringName(record_data["acquisition_method"])
|
|
var source_npc_id := int(record_data["source_npc_id"])
|
|
var acquired_tick := int(record_data["acquired_tick"])
|
|
var source_acquisition_method := StringName(record_data["source_acquisition_method"])
|
|
if knower_id < 0 or event_id < 0 or acquired_tick < 0:
|
|
return null
|
|
if acquisition_method not in VALID_ACQUISITION_METHODS:
|
|
return null
|
|
if acquisition_method == SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED:
|
|
if source_npc_id < 0 or source_npc_id == knower_id:
|
|
return null
|
|
if source_acquisition_method not in DIRECT_ACQUISITION_METHODS:
|
|
return null
|
|
elif source_npc_id != NO_SOURCE_NPC_ID or not source_acquisition_method.is_empty():
|
|
return null
|
|
return create(
|
|
knower_id,
|
|
event_id,
|
|
acquisition_method,
|
|
source_npc_id,
|
|
acquired_tick,
|
|
source_acquisition_method
|
|
)
|
|
|
|
|
|
func get_knower_id() -> int:
|
|
return int(data["knower_id"])
|
|
|
|
|
|
func get_event_id() -> int:
|
|
return int(data["event_id"])
|
|
|
|
|
|
func get_acquisition_method() -> StringName:
|
|
return StringName(data["acquisition_method"])
|
|
|
|
|
|
func get_source_npc_id() -> int:
|
|
return int(data["source_npc_id"])
|
|
|
|
|
|
func get_acquired_tick() -> int:
|
|
return int(data["acquired_tick"])
|
|
|
|
|
|
func get_source_acquisition_method() -> StringName:
|
|
return StringName(data["source_acquisition_method"])
|
|
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
return data.duplicate(true)
|