734fe12098
Previously get_action(), get_profession(), get_actions(), get_professions(), get_profession_ids(), and validate() reloaded all .tres files from disk on every call. These are called per-tick (action selection, task completion, NPC inspector refresh) producing unnecessary disk I/O. Now definitions are loaded once and stored in static dicts/lists keyed by ID. Add invalidate_cache() for hot-reload scenarios.
131 lines
4.0 KiB
GDScript
131 lines
4.0 KiB
GDScript
class_name SimulationDefinitions
|
|
extends RefCounted
|
|
|
|
const ACTION_PATHS := [
|
|
"res://simulation/definitions/actions/gather_food.tres",
|
|
"res://simulation/definitions/actions/gather_wood.tres",
|
|
"res://simulation/definitions/actions/patrol.tres",
|
|
"res://simulation/definitions/actions/study.tres",
|
|
"res://simulation/definitions/actions/eat.tres",
|
|
"res://simulation/definitions/actions/rest.tres",
|
|
"res://simulation/definitions/actions/wander.tres",
|
|
"res://simulation/definitions/actions/deposit_food.tres",
|
|
"res://simulation/definitions/actions/withdraw_food.tres"
|
|
]
|
|
|
|
const PROFESSION_PATHS := [
|
|
"res://simulation/definitions/professions/farmer.tres",
|
|
"res://simulation/definitions/professions/woodcutter.tres",
|
|
"res://simulation/definitions/professions/guard.tres",
|
|
"res://simulation/definitions/professions/scholar.tres",
|
|
"res://simulation/definitions/professions/wanderer.tres"
|
|
]
|
|
|
|
static var _action_list: Array[ActionDefinition] = []
|
|
static var _profession_list: Array[ProfessionDefinition] = []
|
|
static var _action_map: Dictionary = {}
|
|
static var _profession_map: Dictionary = {}
|
|
static var _profession_id_list: Array[StringName] = []
|
|
static var _cache_valid := false
|
|
|
|
|
|
static func _ensure_cache() -> void:
|
|
if _cache_valid:
|
|
return
|
|
_action_list = []
|
|
_profession_list = []
|
|
_action_map = {}
|
|
_profession_map = {}
|
|
_profession_id_list = []
|
|
|
|
for path in ACTION_PATHS:
|
|
var definition := load(path) as ActionDefinition
|
|
if definition != null:
|
|
_action_list.append(definition)
|
|
_action_map[definition.action_id] = definition
|
|
|
|
for path in PROFESSION_PATHS:
|
|
var definition := load(path) as ProfessionDefinition
|
|
if definition != null:
|
|
_profession_list.append(definition)
|
|
_profession_map[definition.profession_id] = definition
|
|
_profession_id_list.append(definition.profession_id)
|
|
|
|
_cache_valid = true
|
|
|
|
|
|
static func invalidate_cache() -> void:
|
|
_cache_valid = false
|
|
|
|
|
|
static func get_actions() -> Array[ActionDefinition]:
|
|
_ensure_cache()
|
|
return _action_list
|
|
|
|
|
|
static func get_professions() -> Array[ProfessionDefinition]:
|
|
_ensure_cache()
|
|
return _profession_list
|
|
|
|
|
|
static func get_action(action_id: StringName) -> ActionDefinition:
|
|
_ensure_cache()
|
|
return _action_map.get(action_id)
|
|
|
|
|
|
static func get_profession(profession_id: StringName) -> ProfessionDefinition:
|
|
_ensure_cache()
|
|
return _profession_map.get(profession_id)
|
|
|
|
|
|
static func get_profession_ids() -> Array[StringName]:
|
|
_ensure_cache()
|
|
return _profession_id_list
|
|
|
|
|
|
static func validate() -> Array[String]:
|
|
_ensure_cache()
|
|
var errors: Array[String] = []
|
|
if _action_list.size() != ACTION_PATHS.size():
|
|
errors.append("One or more ActionDefinition resources failed to load")
|
|
if _profession_list.size() != PROFESSION_PATHS.size():
|
|
errors.append("One or more ProfessionDefinition resources failed to load")
|
|
var action_ids := {}
|
|
for definition in _action_list:
|
|
for error in definition.validate():
|
|
errors.append("ActionDefinition: " + error)
|
|
if action_ids.has(definition.action_id):
|
|
errors.append("Duplicate action_id '%s'" % definition.action_id)
|
|
action_ids[definition.action_id] = true
|
|
|
|
var profession_ids := {}
|
|
for definition in _profession_list:
|
|
for error in definition.validate():
|
|
errors.append("ProfessionDefinition: " + error)
|
|
if profession_ids.has(definition.profession_id):
|
|
errors.append("Duplicate profession_id '%s'" % definition.profession_id)
|
|
profession_ids[definition.profession_id] = true
|
|
|
|
for definition in _action_list:
|
|
if (
|
|
not definition.preferred_profession_id.is_empty()
|
|
and not profession_ids.has(definition.preferred_profession_id)
|
|
):
|
|
errors.append(
|
|
(
|
|
"Action '%s' references unknown profession '%s'"
|
|
% [definition.action_id, definition.preferred_profession_id]
|
|
)
|
|
)
|
|
if (
|
|
definition.target_type == SimulationIds.TARGET_RESOURCE
|
|
and not action_ids.has(definition.resource_action_id)
|
|
):
|
|
errors.append(
|
|
(
|
|
"Action '%s' references unknown resource action '%s'"
|
|
% [definition.action_id, definition.resource_action_id]
|
|
)
|
|
)
|
|
return errors
|