feat: add simulation action definitions

This commit is contained in:
2026-07-05 13:35:33 +02:00
parent 12f9ba616f
commit f816f3976a
32 changed files with 608 additions and 113 deletions
+35 -15
View File
@@ -1,6 +1,10 @@
extends Node
signal npc_task_changed(npc: SimNPC, old_task: String, new_task: String)
signal npc_task_changed(
npc: SimNPC,
old_task: StringName,
new_task: StringName
)
signal village_changed(village: SimVillage)
signal npc_died(npc: SimNPC)
@@ -20,16 +24,14 @@ var names := [
"Amina", "Tarik", "Jasmin"
]
var professions := [
"farmer",
"woodcutter",
"guard",
"scholar",
"wanderer"
]
func _ready() -> void:
add_to_group("simulation_manager")
var definition_errors := SimulationDefinitions.validate()
if not definition_errors.is_empty():
for error in definition_errors:
push_error("SimulationManager: " + error)
set_process(false)
return
clock = SimulationClock.new(tick_interval)
village.debug_logs = debug_logs
village.update_modifiers()
@@ -45,13 +47,17 @@ func _process(delta: float) -> void:
simulate_tick()
func generate_npcs() -> void:
var profession_ids := SimulationDefinitions.get_profession_ids()
for i in range(names.size()):
var npc_random := _create_random_source(i, 0)
var profession_index := npc_random.randi_range(0, professions.size() - 1)
var profession_index := npc_random.randi_range(
0,
profession_ids.size() - 1
)
var npc := SimNPC.new(
i,
names[i],
professions[profession_index],
profession_ids[profession_index],
npc_random.randf_range(1.0, 10.0),
npc_random.randf_range(1.0, 10.0),
npc_random
@@ -138,7 +144,10 @@ func simulate_tick() -> void:
elif debug_logs:
print("[SimulationManager] ", npc.npc_name, " could not complete ", completed_task, ": target reservation was invalid")
release_npc_reservation(npc.id)
elif completed_task not in ["gather_food", "gather_wood"]:
elif completed_task not in [
SimulationIds.ACTION_GATHER_FOOD,
SimulationIds.ACTION_GATHER_WOOD
]:
village.apply_npc_task(npc)
elif debug_logs:
print("[SimulationManager] ", npc.npc_name, " could not complete ", completed_task, ": no resource target")
@@ -148,10 +157,10 @@ func simulate_tick() -> void:
npc.task_complete = true
npc.task_state = SimNPC.TASK_STATE_IDLE
npc.last_task = completed_task
npc.current_task = "idle"
npc.current_task = SimulationIds.ACTION_IDLE
if needs_immediate_food_after_gather and village.food > 0:
npc.set_task("eat", 1.0)
npc.set_task(SimulationIds.ACTION_EAT, 1.0)
npc_task_changed.emit(npc, completed_task, npc.current_task)
if debug_logs:
@@ -244,7 +253,7 @@ func notify_npc_navigation_failed(npc_id: int) -> void:
var failed_task := npc.current_task
release_npc_reservation(npc.id)
npc.last_task = failed_task
npc.set_task("wander", 2.0)
npc.set_task(SimulationIds.ACTION_WANDER, 2.0)
npc_task_changed.emit(npc, failed_task, npc.current_task)
if debug_logs:
@@ -261,6 +270,17 @@ func register_loaded_resource_nodes() -> void:
func register_resource_node(node: ResourceNode) -> bool:
if node == null or node.node_id.is_empty():
return false
var action_definition := SimulationDefinitions.get_action(node.action_id)
if (
action_definition == null
or action_definition.target_type != SimulationIds.TARGET_RESOURCE
or action_definition.resource_action_id != node.action_id
):
push_error(
"SimulationManager: ResourceNode '%s' has invalid action_id '%s'"
% [node.node_id, node.action_id]
)
return false
var resource_state := get_resource_state(node.node_id)
if resource_state == null:
resource_state = ResourceStateRecord.create_from_node(node)