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
@@ -0,0 +1,30 @@
class_name ActionDefinition
extends Resource
@export var action_id: StringName
@export var display_name: String
@export_range(0.0, 1000.0, 0.1) var default_duration := 1.0
@export var preferred_profession_id: StringName
@export var target_type: StringName = SimulationIds.TARGET_ACTIVITY
@export var resource_action_id: StringName
func validate() -> Array[String]:
var errors: Array[String] = []
if action_id.is_empty():
errors.append("action_id is empty")
if display_name.is_empty():
errors.append("display_name is empty for '%s'" % action_id)
if default_duration <= 0.0:
errors.append("default_duration must be positive for '%s'" % action_id)
if target_type not in [
SimulationIds.TARGET_RESOURCE,
SimulationIds.TARGET_ACTIVITY,
SimulationIds.TARGET_FREE
]:
errors.append("target_type '%s' is invalid for '%s'" % [target_type, action_id])
if (
target_type == SimulationIds.TARGET_RESOURCE
and resource_action_id.is_empty()
):
errors.append("resource_action_id is required for '%s'" % action_id)
return errors
@@ -0,0 +1 @@
uid://b1utbqqel3pyw
@@ -0,0 +1,13 @@
class_name ProfessionDefinition
extends Resource
@export var profession_id: StringName
@export var display_name: String
func validate() -> Array[String]:
var errors: Array[String] = []
if profession_id.is_empty():
errors.append("profession_id is empty")
if display_name.is_empty():
errors.append("display_name is empty for '%s'" % profession_id)
return errors
@@ -0,0 +1 @@
uid://cimqtsrs3huq0
@@ -0,0 +1,97 @@
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"
]
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 func get_actions() -> Array[ActionDefinition]:
var definitions: Array[ActionDefinition] = []
for path in ACTION_PATHS:
var definition := load(path) as ActionDefinition
if definition != null:
definitions.append(definition)
return definitions
static func get_professions() -> Array[ProfessionDefinition]:
var definitions: Array[ProfessionDefinition] = []
for path in PROFESSION_PATHS:
var definition := load(path) as ProfessionDefinition
if definition != null:
definitions.append(definition)
return definitions
static func get_action(action_id: StringName) -> ActionDefinition:
for definition in get_actions():
if definition.action_id == action_id:
return definition
return null
static func get_profession(
profession_id: StringName
) -> ProfessionDefinition:
for definition in get_professions():
if definition.profession_id == profession_id:
return definition
return null
static func get_profession_ids() -> Array[StringName]:
var ids: Array[StringName] = []
for definition in get_professions():
ids.append(definition.profession_id)
return ids
static func validate() -> Array[String]:
var errors: Array[String] = []
if get_actions().size() != ACTION_PATHS.size():
errors.append("One or more ActionDefinition resources failed to load")
if get_professions().size() != PROFESSION_PATHS.size():
errors.append("One or more ProfessionDefinition resources failed to load")
var action_ids := {}
for definition in get_actions():
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 get_professions():
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 get_actions():
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
@@ -0,0 +1 @@
uid://v1sohi6bbuga
+25
View File
@@ -0,0 +1,25 @@
class_name SimulationIds
extends RefCounted
const ACTION_IDLE := &"idle"
const ACTION_DEAD := &"dead"
const ACTION_GATHER_FOOD := &"gather_food"
const ACTION_GATHER_WOOD := &"gather_wood"
const ACTION_PATROL := &"patrol"
const ACTION_STUDY := &"study"
const ACTION_EAT := &"eat"
const ACTION_REST := &"rest"
const ACTION_WANDER := &"wander"
const PROFESSION_FARMER := &"farmer"
const PROFESSION_WOODCUTTER := &"woodcutter"
const PROFESSION_GUARD := &"guard"
const PROFESSION_SCHOLAR := &"scholar"
const PROFESSION_WANDERER := &"wanderer"
const TARGET_RESOURCE := &"resource"
const TARGET_ACTIVITY := &"activity"
const TARGET_FREE := &"free"
const RESOURCE_FOOD := &"food"
const RESOURCE_WOOD := &"wood"
@@ -0,0 +1 @@
uid://brf7tywgsj6v7
+10
View File
@@ -0,0 +1,10 @@
[gd_resource type="Resource" script_class="ActionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ActionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
action_id = &"eat"
display_name = "Eat"
default_duration = 2.0
target_type = &"activity"
@@ -0,0 +1,12 @@
[gd_resource type="Resource" script_class="ActionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ActionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
action_id = &"gather_food"
display_name = "Gather Food"
default_duration = 5.0
preferred_profession_id = &"farmer"
target_type = &"resource"
resource_action_id = &"gather_food"
@@ -0,0 +1,12 @@
[gd_resource type="Resource" script_class="ActionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ActionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
action_id = &"gather_wood"
display_name = "Gather Wood"
default_duration = 5.0
preferred_profession_id = &"woodcutter"
target_type = &"resource"
resource_action_id = &"gather_wood"
@@ -0,0 +1,11 @@
[gd_resource type="Resource" script_class="ActionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ActionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
action_id = &"patrol"
display_name = "Patrol"
default_duration = 6.0
preferred_profession_id = &"guard"
target_type = &"activity"
+10
View File
@@ -0,0 +1,10 @@
[gd_resource type="Resource" script_class="ActionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ActionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
action_id = &"rest"
display_name = "Rest"
default_duration = 4.0
target_type = &"activity"
+11
View File
@@ -0,0 +1,11 @@
[gd_resource type="Resource" script_class="ActionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ActionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
action_id = &"study"
display_name = "Study"
default_duration = 6.0
preferred_profession_id = &"scholar"
target_type = &"activity"
@@ -0,0 +1,10 @@
[gd_resource type="Resource" script_class="ActionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ActionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
action_id = &"wander"
display_name = "Wander"
default_duration = 4.0
target_type = &"free"
@@ -0,0 +1,8 @@
[gd_resource type="Resource" script_class="ProfessionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ProfessionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
profession_id = &"farmer"
display_name = "Farmer"
@@ -0,0 +1,8 @@
[gd_resource type="Resource" script_class="ProfessionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ProfessionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
profession_id = &"guard"
display_name = "Guard"
@@ -0,0 +1,8 @@
[gd_resource type="Resource" script_class="ProfessionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ProfessionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
profession_id = &"scholar"
display_name = "Scholar"
@@ -0,0 +1,8 @@
[gd_resource type="Resource" script_class="ProfessionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ProfessionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
profession_id = &"wanderer"
display_name = "Wanderer"
@@ -0,0 +1,8 @@
[gd_resource type="Resource" script_class="ProfessionDefinition" load_steps=2 format=3]
[ext_resource type="Script" path="res://simulation/definitions/ProfessionDefinition.gd" id="1"]
[resource]
script = ExtResource("1")
profession_id = &"woodcutter"
display_name = "Woodcutter"