feat: introduce identity-backed goat
This commit is contained in:
@@ -2,6 +2,7 @@ extends Node
|
||||
|
||||
const SimulationEventLogScript := preload("res://simulation/events/SimulationEventLog.gd")
|
||||
const VillageEconomyScript := preload("res://simulation/economy/VillageEconomy.gd")
|
||||
const AnimalCareSystemScript := preload("res://simulation/animals/AnimalCareSystem.gd")
|
||||
const RelationshipSystemScript := preload("res://simulation/relationships/RelationshipSystem.gd")
|
||||
const EventKnowledgeSystemScript := preload("res://simulation/knowledge/EventKnowledgeSystem.gd")
|
||||
|
||||
@@ -38,6 +39,7 @@ var wander_random_sources := {}
|
||||
var resource_states: Dictionary = {}
|
||||
var event_log := SimulationEventLogScript.new()
|
||||
var economy := VillageEconomyScript.new()
|
||||
var animal_care := AnimalCareSystemScript.new()
|
||||
var relationship_system := RelationshipSystemScript.new()
|
||||
var event_knowledge_system := EventKnowledgeSystemScript.new()
|
||||
var opportunity_system := VillageOpportunitySystem.new()
|
||||
@@ -71,6 +73,8 @@ func _ready() -> void:
|
||||
economy.inventory_changed.connect(_on_economy_inventory_changed)
|
||||
economy.economic_event_requested.connect(_record_economic_event)
|
||||
economy.narrative_event_requested.connect(record_narrative_event)
|
||||
animal_care.economic_event_requested.connect(_record_economic_event_at)
|
||||
animal_care.narrative_event_requested.connect(record_narrative_event)
|
||||
action_selector.relationship_system = relationship_system
|
||||
var definition_errors := SimulationDefinitions.validate()
|
||||
if not definition_errors.is_empty():
|
||||
@@ -83,12 +87,14 @@ func _ready() -> void:
|
||||
clock.elapsed_ticks = int(0.25 * cycle_duration_seconds / tick_interval)
|
||||
village.debug_logs = debug_logs
|
||||
economy.configure(village, debug_logs)
|
||||
animal_care.configure(economy, active_world_adapter)
|
||||
economy.initialize_storage()
|
||||
village.update_modifiers()
|
||||
village.update_priorities()
|
||||
generate_npcs()
|
||||
relationship_system.initialize_households(npcs)
|
||||
call_deferred("register_loaded_resource_nodes")
|
||||
animal_care.call_deferred("register_loaded_nodes")
|
||||
call_deferred("register_loaded_storage_nodes")
|
||||
if debug_logs:
|
||||
print("--- Simulation started ---")
|
||||
@@ -156,6 +162,7 @@ func simulate_tick() -> void:
|
||||
tick_count += 1
|
||||
if debug_logs:
|
||||
print("--- Tick ", tick_count, " ---")
|
||||
animal_care.advance()
|
||||
var village_was_changed := false
|
||||
_population_view.rebuild(npcs)
|
||||
for npc in npcs:
|
||||
@@ -210,8 +217,9 @@ func _select_action_if_idle(npc: SimNPC, previous_state: StringName) -> void:
|
||||
if npc.task_state not in [SimNPC.TASK_STATE_IDLE, SimNPC.TASK_STATE_COMPLETE]:
|
||||
return
|
||||
var helper := get_active_opportunity_helper()
|
||||
var animal_feed_name := animal_care.get_available_feed_name(npc.id)
|
||||
var selection := action_selector.select_action(
|
||||
npc, village, clock.time_of_day(), npcs, helper, _population_view
|
||||
npc, village, clock.time_of_day(), npcs, helper, _population_view, animal_feed_name
|
||||
)
|
||||
if selection == null:
|
||||
return
|
||||
@@ -239,7 +247,12 @@ func _handle_npc_death(npc: SimNPC, previous_task: StringName, previous_target:
|
||||
func _complete_current_action(npc: SimNPC) -> void:
|
||||
var completed_task := npc.current_task
|
||||
var definition := SimulationDefinitions.get_action(completed_task)
|
||||
if economy.consume_completion_cost(npc, definition):
|
||||
var completion_succeeded := false
|
||||
if completed_task == SimulationIds.ACTION_FEED_ANIMAL:
|
||||
completion_succeeded = feed_animal(npc.target_id, npc.id)
|
||||
else:
|
||||
completion_succeeded = economy.consume_completion_cost(npc, definition)
|
||||
if completion_succeeded:
|
||||
_apply_action_completion(npc, completed_task, definition)
|
||||
elif not npc.target_id.is_empty():
|
||||
release_npc_reservation(npc.id)
|
||||
@@ -272,6 +285,8 @@ func _apply_action_completion(
|
||||
npc.energy = minf(npc.energy + 40.0, 100.0)
|
||||
npc.position = npc.home_position
|
||||
record_narrative_event(SimulationIds.EVENT_NPC_SLEPT, npc.id)
|
||||
SimulationIds.ACTION_FEED_ANIMAL:
|
||||
pass
|
||||
SimulationIds.ACTION_PATROL, SimulationIds.ACTION_STUDY, SimulationIds.ACTION_REST, SimulationIds.ACTION_WANDER:
|
||||
village.apply_npc_task(npc)
|
||||
if debug_logs and completed_task == SimulationIds.ACTION_SLEEP:
|
||||
@@ -364,6 +379,9 @@ func release_npc_reservation(npc_id: int) -> void:
|
||||
var resource_state := get_resource_state(npc.target_id)
|
||||
if resource_state != null:
|
||||
resource_state.release(npc.id)
|
||||
var animal_state := animal_care.get_state(npc.target_id)
|
||||
if animal_state != null:
|
||||
animal_state.release(npc.id)
|
||||
npc.target_id = &""
|
||||
return
|
||||
|
||||
@@ -397,6 +415,22 @@ func notify_npc_arrived(npc_id: int) -> void:
|
||||
)
|
||||
notify_npc_navigation_failed(npc.id)
|
||||
return
|
||||
if (
|
||||
npc.target_id != &""
|
||||
and definition != null
|
||||
and definition.target_type == SimulationIds.TARGET_ANIMAL
|
||||
):
|
||||
if not animal_care.can_complete_feed(npc.target_id, npc.id):
|
||||
if debug_logs:
|
||||
print(
|
||||
"[SimulationManager] ",
|
||||
npc.npc_name,
|
||||
" arrived but animal ",
|
||||
npc.target_id,
|
||||
" no longer needs feeding, replanning"
|
||||
)
|
||||
notify_npc_navigation_failed(npc.id)
|
||||
return
|
||||
|
||||
npc.has_travel_target = false
|
||||
npc.start_working()
|
||||
@@ -903,6 +937,13 @@ func _on_economy_inventory_changed(npc: SimNPC, item_id: StringName, amount: flo
|
||||
npc_inventory_changed.emit(npc, item_id, amount)
|
||||
|
||||
|
||||
func feed_animal(animal_id: StringName, actor_id: int = -1) -> bool:
|
||||
var succeeded := animal_care.feed(animal_id, actor_id, npcs, tick_count)
|
||||
if succeeded:
|
||||
village_changed.emit(village)
|
||||
return succeeded
|
||||
|
||||
|
||||
func register_loaded_resource_nodes() -> void:
|
||||
for node in ResourceNode.get_all():
|
||||
register_resource_node(node)
|
||||
@@ -1051,50 +1092,6 @@ func get_starving_count() -> int:
|
||||
return _population_view.starving_count()
|
||||
|
||||
|
||||
func get_state_snapshot() -> Dictionary:
|
||||
var npc_snapshots: Array[Dictionary] = []
|
||||
for npc in npcs:
|
||||
npc_snapshots.append(
|
||||
{
|
||||
"id": npc.id,
|
||||
"name": npc.npc_name,
|
||||
"profession": npc.profession,
|
||||
"hunger": npc.hunger,
|
||||
"energy": npc.energy,
|
||||
"strength": npc.strength,
|
||||
"intelligence": npc.intelligence,
|
||||
"task": npc.current_task,
|
||||
"task_state": npc.task_state,
|
||||
"task_duration": npc.task_duration,
|
||||
"task_progress": npc.task_progress,
|
||||
"target_id": String(npc.target_id),
|
||||
"position": [npc.position.x, npc.position.y, npc.position.z],
|
||||
"travel_target_position":
|
||||
[
|
||||
npc.travel_target_position.x,
|
||||
npc.travel_target_position.y,
|
||||
npc.travel_target_position.z
|
||||
],
|
||||
"has_travel_target": npc.has_travel_target,
|
||||
"starvation_ticks": npc.starvation_ticks,
|
||||
"is_dead": npc.is_dead
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
"seed": simulation_seed,
|
||||
"tick_count": tick_count,
|
||||
"village":
|
||||
{
|
||||
"food": village.food,
|
||||
"wood": village.wood,
|
||||
"safety": village.safety,
|
||||
"knowledge": village.knowledge
|
||||
},
|
||||
"npcs": npc_snapshots
|
||||
}
|
||||
|
||||
|
||||
func get_state_checksum() -> String:
|
||||
return create_state_record().to_json().sha256_text()
|
||||
|
||||
@@ -1127,6 +1124,7 @@ func create_state_record() -> SimulationStateRecord:
|
||||
record.village = VillageStateRecord.capture(village)
|
||||
for npc in npcs:
|
||||
record.npcs.append(NPCStateRecord.capture(npc))
|
||||
animal_care.append_state_records(record)
|
||||
var sorted_resource_ids: Array = resource_states.keys()
|
||||
sorted_resource_ids.sort()
|
||||
for resource_id in sorted_resource_ids:
|
||||
@@ -1194,6 +1192,7 @@ func restore_state(record: SimulationStateRecord) -> bool:
|
||||
for resource_record in record.resources:
|
||||
resource_states[resource_record.get_node_id()] = resource_record
|
||||
register_loaded_resource_nodes()
|
||||
animal_care.restore_state_records(record.animals)
|
||||
village_changed.emit(village)
|
||||
state_restored.emit()
|
||||
return true
|
||||
|
||||
@@ -23,7 +23,8 @@ func select_action(
|
||||
time_of_day: float = 0.5,
|
||||
all_npcs: Array = [],
|
||||
opportunity_helper: OpportunityHelperResult = null,
|
||||
population_view: SimulationPopulationView = null
|
||||
population_view: SimulationPopulationView = null,
|
||||
animal_feed_name: String = ""
|
||||
) -> ActionSelectionResult:
|
||||
if npc.is_dead:
|
||||
return null
|
||||
@@ -126,6 +127,14 @@ func select_action(
|
||||
return ActionSelectionResult.new(
|
||||
SimulationIds.ACTION_REST, -1.0, "Energy is below the rest threshold"
|
||||
)
|
||||
if not animal_feed_name.is_empty():
|
||||
var feed_definition := SimulationDefinitions.get_action(SimulationIds.ACTION_FEED_ANIMAL)
|
||||
if _get_cost_rejection(feed_definition, village).is_empty():
|
||||
return ActionSelectionResult.new(
|
||||
SimulationIds.ACTION_FEED_ANIMAL,
|
||||
-1.0,
|
||||
"%s is hungry; pantry food is available" % animal_feed_name
|
||||
)
|
||||
if village.food <= RELATIONSHIP_AID_PANTRY_THRESHOLD and relationship_system != null:
|
||||
var trusted_starving_npc: SimNPC = relationship_system.get_trusted_starving_subject(
|
||||
npc.id, all_npcs, population_view
|
||||
|
||||
@@ -17,6 +17,8 @@ func resolve(
|
||||
)
|
||||
SimulationIds.TARGET_ACTIVITY:
|
||||
return _resolve_activity(npc, origin, simulation_manager, active_world_adapter)
|
||||
SimulationIds.TARGET_ANIMAL:
|
||||
return _resolve_animal(npc, origin, simulation_manager, active_world_adapter)
|
||||
SimulationIds.TARGET_FREE:
|
||||
return {
|
||||
"target_id": "", "position": origin + simulation_manager.get_wander_offset(npc.id)
|
||||
@@ -202,6 +204,32 @@ func _resolve_activity(
|
||||
return active_world_adapter.get_activity_target(npc.current_task, origin)
|
||||
|
||||
|
||||
func _resolve_animal(
|
||||
npc: SimNPC, origin: Vector3, simulation_manager: Node, active_world_adapter: Node
|
||||
) -> Dictionary:
|
||||
if not active_world_adapter.has_method("get_animal_candidates"):
|
||||
return {}
|
||||
var best: Dictionary = {}
|
||||
var best_distance := INF
|
||||
var candidates: Array[Dictionary] = active_world_adapter.get_animal_candidates(npc.current_task)
|
||||
for candidate in candidates:
|
||||
var animal_id := StringName(candidate["target_id"])
|
||||
var state: AnimalStateRecord = simulation_manager.animal_care.get_state(animal_id)
|
||||
if state == null or not state.can_npc_feed() or not state.is_available_for(npc.id):
|
||||
continue
|
||||
var position: Vector3 = candidate["position"]
|
||||
var distance := origin.distance_squared_to(position)
|
||||
if distance < best_distance:
|
||||
best_distance = distance
|
||||
best = candidate
|
||||
if best.is_empty():
|
||||
return {}
|
||||
var target_id := StringName(best["target_id"])
|
||||
if not simulation_manager.animal_care.reserve(target_id, npc.id):
|
||||
return {}
|
||||
return best
|
||||
|
||||
|
||||
func score_resource_candidate(
|
||||
npc: SimNPC, origin: Vector3, position: Vector3, state: ResourceStateRecord
|
||||
) -> float:
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
class_name AnimalCareSystem
|
||||
extends RefCounted
|
||||
|
||||
signal economic_event_requested(
|
||||
event_type: StringName,
|
||||
actor_id: int,
|
||||
source_id: StringName,
|
||||
destination_id: StringName,
|
||||
item_id: StringName,
|
||||
amount: float,
|
||||
world_position: Vector3
|
||||
)
|
||||
signal narrative_event_requested(
|
||||
event_type: StringName,
|
||||
actor_id: int,
|
||||
source_id: StringName,
|
||||
action_display: String,
|
||||
action_id: StringName,
|
||||
item_id: StringName,
|
||||
required_amount: float
|
||||
)
|
||||
|
||||
var states: Dictionary = {}
|
||||
var economy: RefCounted
|
||||
var active_world_adapter: Node
|
||||
|
||||
|
||||
func configure(economy_service: RefCounted, world_adapter: Node) -> void:
|
||||
economy = economy_service
|
||||
active_world_adapter = world_adapter
|
||||
|
||||
|
||||
func advance() -> void:
|
||||
var animal_ids: Array = states.keys()
|
||||
animal_ids.sort()
|
||||
for animal_id in animal_ids:
|
||||
var animal_state := states[animal_id] as AnimalStateRecord
|
||||
if animal_state != null:
|
||||
animal_state.advance_tick()
|
||||
|
||||
|
||||
func register_loaded_nodes() -> void:
|
||||
for node in AnimalNode.get_all():
|
||||
register_node(node)
|
||||
|
||||
|
||||
func register_node(node: AnimalNode) -> bool:
|
||||
if node == null or node.animal_id.is_empty():
|
||||
return false
|
||||
if (
|
||||
ResourceNode.get_by_id(node.animal_id) != null
|
||||
or StorageNode.get_by_id(node.animal_id) != null
|
||||
or ActivitySite.get_by_id(node.animal_id) != null
|
||||
):
|
||||
push_error("AnimalCareSystem: Animal ID '%s' collides with a world target" % node.animal_id)
|
||||
return false
|
||||
var action_definition := SimulationDefinitions.get_action(SimulationIds.ACTION_FEED_ANIMAL)
|
||||
if (
|
||||
action_definition == null
|
||||
or action_definition.target_type != SimulationIds.TARGET_ANIMAL
|
||||
or node.species_id != SimulationIds.SPECIES_GOAT
|
||||
):
|
||||
push_error(
|
||||
(
|
||||
"AnimalCareSystem: AnimalNode '%s' has an invalid feed action or species '%s'"
|
||||
% [node.animal_id, node.species_id]
|
||||
)
|
||||
)
|
||||
return false
|
||||
var animal_state := get_state(node.animal_id)
|
||||
if animal_state == null:
|
||||
animal_state = AnimalStateRecord.create_from_node(node)
|
||||
states[node.animal_id] = animal_state
|
||||
elif not animal_state.apply_definition(node):
|
||||
push_error("AnimalCareSystem: AnimalNode definition mismatch for '%s'" % node.animal_id)
|
||||
return false
|
||||
return node.bind_state(animal_state)
|
||||
|
||||
|
||||
func get_state(animal_id: StringName) -> AnimalStateRecord:
|
||||
return states.get(animal_id) as AnimalStateRecord
|
||||
|
||||
|
||||
func reserve(animal_id: StringName, agent_id: int) -> bool:
|
||||
var animal_state := get_state(animal_id)
|
||||
return animal_state != null and animal_state.can_npc_feed() and animal_state.reserve(agent_id)
|
||||
|
||||
|
||||
func release(animal_id: StringName, agent_id: int) -> void:
|
||||
var animal_state := get_state(animal_id)
|
||||
if animal_state != null:
|
||||
animal_state.release(agent_id)
|
||||
|
||||
|
||||
func can_complete_feed(animal_id: StringName, agent_id: int) -> bool:
|
||||
var animal_state := get_state(animal_id)
|
||||
return (
|
||||
animal_state != null
|
||||
and animal_state.needs_feed()
|
||||
and animal_state.get_reserved_by() == agent_id
|
||||
)
|
||||
|
||||
|
||||
func get_available_feed_name(npc_id: int) -> String:
|
||||
if active_world_adapter == null or not active_world_adapter.has_method("get_animal_candidates"):
|
||||
return ""
|
||||
var candidates: Array[Dictionary] = active_world_adapter.get_animal_candidates(
|
||||
SimulationIds.ACTION_FEED_ANIMAL
|
||||
)
|
||||
for candidate in candidates:
|
||||
var animal_state := get_state(StringName(candidate["target_id"]))
|
||||
if (
|
||||
animal_state != null
|
||||
and animal_state.can_npc_feed()
|
||||
and animal_state.is_available_for(npc_id)
|
||||
):
|
||||
return animal_state.get_display_name()
|
||||
return ""
|
||||
|
||||
|
||||
func find_node_for_player(from_position: Vector3, max_distance: float) -> AnimalNode:
|
||||
if (
|
||||
active_world_adapter == null
|
||||
or not active_world_adapter.has_method("get_animal_nodes_in_radius")
|
||||
):
|
||||
return null
|
||||
var candidates: Array[AnimalNode] = active_world_adapter.get_animal_nodes_in_radius(
|
||||
from_position, max_distance
|
||||
)
|
||||
for node in candidates:
|
||||
var animal_state := get_state(node.animal_id)
|
||||
if (
|
||||
animal_state != null
|
||||
and animal_state.can_player_feed_animal()
|
||||
and animal_state.needs_feed()
|
||||
):
|
||||
return node
|
||||
return null
|
||||
|
||||
|
||||
func feed(animal_id: StringName, actor_id: int, npcs: Array[SimNPC], current_tick: int) -> bool:
|
||||
var animal_state := get_state(animal_id)
|
||||
if animal_state == null or not animal_state.needs_feed():
|
||||
return false
|
||||
if actor_id >= 0:
|
||||
if (
|
||||
_find_npc(actor_id, npcs) == null
|
||||
or not animal_state.can_npc_feed()
|
||||
or animal_state.get_reserved_by() != actor_id
|
||||
):
|
||||
return false
|
||||
elif not animal_state.can_player_feed_animal():
|
||||
return false
|
||||
|
||||
var definition := SimulationDefinitions.get_action(SimulationIds.ACTION_FEED_ANIMAL)
|
||||
if definition == null or not definition.has_completion_cost() or economy == null:
|
||||
return false
|
||||
var resource_id := definition.completion_cost_resource_id
|
||||
var required_amount := definition.completion_cost_amount
|
||||
var pantry: StorageStateRecord = economy.get_storage_for_resource(resource_id)
|
||||
var available := pantry.get_amount(resource_id) if pantry != null else 0.0
|
||||
if pantry == null or available < required_amount:
|
||||
if actor_id >= 0:
|
||||
(
|
||||
narrative_event_requested
|
||||
. emit(
|
||||
SimulationIds.EVENT_TASK_BLOCKED,
|
||||
actor_id,
|
||||
pantry.get_storage_id() if pantry != null else &"",
|
||||
(
|
||||
"%s: needs %.0f %s (%.1f available)"
|
||||
% [
|
||||
definition.display_name,
|
||||
required_amount,
|
||||
String(resource_id).capitalize(),
|
||||
available,
|
||||
]
|
||||
),
|
||||
definition.action_id,
|
||||
resource_id,
|
||||
required_amount
|
||||
)
|
||||
)
|
||||
return false
|
||||
|
||||
var consumed: float = economy.withdraw_resource(resource_id, required_amount)
|
||||
if consumed < required_amount:
|
||||
economy.deposit_resource(resource_id, consumed)
|
||||
return false
|
||||
if not animal_state.feed(actor_id, current_tick):
|
||||
economy.deposit_resource(resource_id, consumed)
|
||||
return false
|
||||
|
||||
economic_event_requested.emit(
|
||||
SimulationIds.EVENT_ANIMAL_FED,
|
||||
actor_id,
|
||||
pantry.get_storage_id(),
|
||||
animal_state.get_animal_id(),
|
||||
resource_id,
|
||||
consumed,
|
||||
animal_state.get_position()
|
||||
)
|
||||
return true
|
||||
|
||||
|
||||
func append_state_records(record: SimulationStateRecord) -> void:
|
||||
var animal_ids: Array = states.keys()
|
||||
animal_ids.sort()
|
||||
for animal_id in animal_ids:
|
||||
var animal_state := states[animal_id] as AnimalStateRecord
|
||||
record.animals.append(animal_state)
|
||||
|
||||
|
||||
func restore_state_records(records: Array[AnimalStateRecord]) -> void:
|
||||
states.clear()
|
||||
for animal_record in records:
|
||||
states[animal_record.get_animal_id()] = animal_record
|
||||
register_loaded_nodes()
|
||||
|
||||
|
||||
static func _find_npc(npc_id: int, npcs: Array[SimNPC]) -> SimNPC:
|
||||
for npc in npcs:
|
||||
if npc.id == npc_id:
|
||||
return npc
|
||||
return null
|
||||
@@ -0,0 +1 @@
|
||||
uid://kilnbq2k276w
|
||||
@@ -22,7 +22,10 @@ func validate() -> Array[String]:
|
||||
if (
|
||||
target_type
|
||||
not in [
|
||||
SimulationIds.TARGET_RESOURCE, SimulationIds.TARGET_ACTIVITY, SimulationIds.TARGET_FREE
|
||||
SimulationIds.TARGET_RESOURCE,
|
||||
SimulationIds.TARGET_ACTIVITY,
|
||||
SimulationIds.TARGET_ANIMAL,
|
||||
SimulationIds.TARGET_FREE,
|
||||
]
|
||||
):
|
||||
errors.append("target_type '%s' is invalid for '%s'" % [target_type, action_id])
|
||||
|
||||
@@ -4,6 +4,7 @@ extends RefCounted
|
||||
const ACTION_PATHS := [
|
||||
"res://simulation/definitions/actions/gather_food.tres",
|
||||
"res://simulation/definitions/actions/gather_wood.tres",
|
||||
"res://simulation/definitions/actions/feed_animal.tres",
|
||||
"res://simulation/definitions/actions/patrol.tres",
|
||||
"res://simulation/definitions/actions/study.tres",
|
||||
"res://simulation/definitions/actions/eat.tres",
|
||||
|
||||
@@ -6,6 +6,7 @@ const ACTION_DEAD := &"dead"
|
||||
const ACTION_SLEEP := &"sleep"
|
||||
const ACTION_GATHER_FOOD := &"gather_food"
|
||||
const ACTION_GATHER_WOOD := &"gather_wood"
|
||||
const ACTION_FEED_ANIMAL := &"feed_animal"
|
||||
const ACTION_PATROL := &"patrol"
|
||||
const ACTION_STUDY := &"study"
|
||||
const ACTION_EAT := &"eat"
|
||||
@@ -23,11 +24,15 @@ const PROFESSION_WANDERER := &"wanderer"
|
||||
|
||||
const TARGET_RESOURCE := &"resource"
|
||||
const TARGET_ACTIVITY := &"activity"
|
||||
const TARGET_ANIMAL := &"animal"
|
||||
const TARGET_FREE := &"free"
|
||||
|
||||
const RESOURCE_FOOD := &"food"
|
||||
const RESOURCE_WOOD := &"wood"
|
||||
|
||||
const ANIMAL_DUNJA := &"goat_dunja"
|
||||
const SPECIES_GOAT := &"goat"
|
||||
|
||||
const STORAGE_VILLAGE_PANTRY := &"village_pantry"
|
||||
const STORAGE_VILLAGE_WOODPILE := &"village_woodpile"
|
||||
|
||||
@@ -40,6 +45,7 @@ const EVENT_NPC_DIED := &"npc_died"
|
||||
const EVENT_TASK_STARTED := &"task_started"
|
||||
const EVENT_TASK_BLOCKED := &"task_blocked"
|
||||
const EVENT_RESOURCE_DEPLETED := &"resource_depleted"
|
||||
const EVENT_ANIMAL_FED := &"animal_fed"
|
||||
|
||||
const OPPORTUNITY_RESTOCK_EMPTY_PANTRY := &"restock_empty_pantry"
|
||||
const OPPORTUNITY_SUPPLY_MISSING_WOOD := &"supply_missing_wood"
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
[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 = &"feed_animal"
|
||||
display_name = "Feed Dunja"
|
||||
default_duration = 2.0
|
||||
preferred_profession_id = &"farmer"
|
||||
target_type = &"animal"
|
||||
completion_cost_resource_id = &"food"
|
||||
completion_cost_amount = 1.0
|
||||
@@ -0,0 +1,223 @@
|
||||
class_name AnimalStateRecord
|
||||
extends RefCounted
|
||||
|
||||
signal changed(state: AnimalStateRecord)
|
||||
signal fed(state: AnimalStateRecord, actor_id: int)
|
||||
|
||||
const SCHEMA_VERSION := 1
|
||||
const HUNGER_PER_TICK := 0.125
|
||||
const FEED_THRESHOLD := 65.0
|
||||
const HUNGER_RELIEF := 55.0
|
||||
const NEVER_FED_TICK := -1
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
|
||||
func _init(record_data: Dictionary = {}) -> void:
|
||||
data = record_data.duplicate(true)
|
||||
|
||||
|
||||
static func create_from_node(node: AnimalNode) -> AnimalStateRecord:
|
||||
return (
|
||||
AnimalStateRecord
|
||||
. new(
|
||||
{
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"animal_id": String(node.animal_id),
|
||||
"display_name": node.display_name,
|
||||
"species_id": String(node.species_id),
|
||||
"position":
|
||||
[node.global_position.x, node.global_position.y, node.global_position.z],
|
||||
"hunger": node.initial_hunger,
|
||||
"last_fed_tick": NEVER_FED_TICK,
|
||||
"reserved_by": -1,
|
||||
"enabled": node.initial_enabled,
|
||||
"can_npcs_feed": node.can_npcs_feed,
|
||||
"can_player_feed": node.can_player_feed,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
static func from_dictionary(record_data: Dictionary) -> AnimalStateRecord:
|
||||
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
||||
return null
|
||||
if not (
|
||||
record_data
|
||||
. has_all(
|
||||
[
|
||||
"animal_id",
|
||||
"display_name",
|
||||
"species_id",
|
||||
"position",
|
||||
"hunger",
|
||||
"last_fed_tick",
|
||||
"reserved_by",
|
||||
"enabled",
|
||||
"can_npcs_feed",
|
||||
"can_player_feed",
|
||||
]
|
||||
)
|
||||
):
|
||||
return null
|
||||
var animal_id := String(record_data["animal_id"])
|
||||
var display_name := String(record_data["display_name"])
|
||||
var species_id := String(record_data["species_id"])
|
||||
var saved_position = record_data["position"]
|
||||
var hunger := float(record_data["hunger"])
|
||||
var last_fed_tick := int(record_data["last_fed_tick"])
|
||||
var reserved_by := int(record_data["reserved_by"])
|
||||
if animal_id.is_empty() or display_name.is_empty() or species_id.is_empty():
|
||||
return null
|
||||
if StringName(species_id) != SimulationIds.SPECIES_GOAT:
|
||||
return null
|
||||
if not saved_position is Array or saved_position.size() != 3:
|
||||
return null
|
||||
for component in saved_position:
|
||||
if not is_finite(float(component)):
|
||||
return null
|
||||
if not is_finite(hunger) or hunger < 0.0 or hunger > 100.0:
|
||||
return null
|
||||
if last_fed_tick < NEVER_FED_TICK or reserved_by < -1:
|
||||
return null
|
||||
if reserved_by >= 0 and hunger < FEED_THRESHOLD:
|
||||
return null
|
||||
|
||||
var normalized := record_data.duplicate(true)
|
||||
normalized["schema_version"] = SCHEMA_VERSION
|
||||
normalized["animal_id"] = animal_id
|
||||
normalized["display_name"] = display_name
|
||||
normalized["species_id"] = species_id
|
||||
normalized["position"] = [
|
||||
float(saved_position[0]), float(saved_position[1]), float(saved_position[2])
|
||||
]
|
||||
normalized["hunger"] = hunger
|
||||
normalized["last_fed_tick"] = last_fed_tick
|
||||
normalized["reserved_by"] = reserved_by
|
||||
normalized["enabled"] = bool(record_data["enabled"])
|
||||
normalized["can_npcs_feed"] = bool(record_data["can_npcs_feed"])
|
||||
normalized["can_player_feed"] = bool(record_data["can_player_feed"])
|
||||
return AnimalStateRecord.new(normalized)
|
||||
|
||||
|
||||
func apply_definition(node: AnimalNode) -> bool:
|
||||
if node == null or node.animal_id != get_animal_id():
|
||||
return false
|
||||
return (
|
||||
node.display_name == get_display_name()
|
||||
and node.species_id == get_species_id()
|
||||
and node.can_npcs_feed == can_npc_feed()
|
||||
and node.can_player_feed == can_player_feed_animal()
|
||||
)
|
||||
|
||||
|
||||
func get_animal_id() -> StringName:
|
||||
return StringName(data["animal_id"])
|
||||
|
||||
|
||||
func get_display_name() -> String:
|
||||
return String(data["display_name"])
|
||||
|
||||
|
||||
func get_species_id() -> StringName:
|
||||
return StringName(data["species_id"])
|
||||
|
||||
|
||||
func get_position() -> Vector3:
|
||||
var saved_position: Array = data["position"]
|
||||
return Vector3(float(saved_position[0]), float(saved_position[1]), float(saved_position[2]))
|
||||
|
||||
|
||||
func get_hunger() -> float:
|
||||
return float(data["hunger"])
|
||||
|
||||
|
||||
func get_last_fed_tick() -> int:
|
||||
return int(data["last_fed_tick"])
|
||||
|
||||
|
||||
func get_reserved_by() -> int:
|
||||
return int(data["reserved_by"])
|
||||
|
||||
|
||||
func is_enabled() -> bool:
|
||||
return bool(data["enabled"])
|
||||
|
||||
|
||||
func can_npc_feed() -> bool:
|
||||
return bool(data["can_npcs_feed"])
|
||||
|
||||
|
||||
func can_player_feed_animal() -> bool:
|
||||
return bool(data["can_player_feed"])
|
||||
|
||||
|
||||
func needs_feed() -> bool:
|
||||
return is_enabled() and get_hunger() >= FEED_THRESHOLD
|
||||
|
||||
|
||||
func is_available_for(agent_id: int) -> bool:
|
||||
return needs_feed() and (get_reserved_by() == -1 or get_reserved_by() == agent_id)
|
||||
|
||||
|
||||
func reserve(agent_id: int) -> bool:
|
||||
if agent_id < 0 or not is_available_for(agent_id):
|
||||
return false
|
||||
data["reserved_by"] = agent_id
|
||||
changed.emit(self)
|
||||
return true
|
||||
|
||||
|
||||
func release(agent_id: int) -> void:
|
||||
if get_reserved_by() != agent_id:
|
||||
return
|
||||
data["reserved_by"] = -1
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func advance_tick() -> void:
|
||||
if not is_enabled():
|
||||
return
|
||||
var next_hunger := minf(get_hunger() + HUNGER_PER_TICK, 100.0)
|
||||
if is_equal_approx(next_hunger, get_hunger()):
|
||||
return
|
||||
data["hunger"] = next_hunger
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func feed(actor_id: int, current_tick: int) -> bool:
|
||||
if not needs_feed() or current_tick < 0:
|
||||
return false
|
||||
data["hunger"] = maxf(get_hunger() - HUNGER_RELIEF, 0.0)
|
||||
data["last_fed_tick"] = current_tick
|
||||
data["reserved_by"] = -1
|
||||
changed.emit(self)
|
||||
fed.emit(self, actor_id)
|
||||
return true
|
||||
|
||||
|
||||
func set_position(value: Vector3) -> void:
|
||||
if not value.is_finite() or value.is_equal_approx(get_position()):
|
||||
return
|
||||
data["position"] = [value.x, value.y, value.z]
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func set_hunger(value: float) -> void:
|
||||
if not is_finite(value):
|
||||
return
|
||||
data["hunger"] = clampf(value, 0.0, 100.0)
|
||||
if not needs_feed():
|
||||
data["reserved_by"] = -1
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func set_enabled(value: bool) -> void:
|
||||
data["enabled"] = value
|
||||
if not value:
|
||||
data["reserved_by"] = -1
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
return data.duplicate(true)
|
||||
@@ -0,0 +1 @@
|
||||
uid://c8rgw7h25bcmr
|
||||
@@ -158,5 +158,7 @@ func description(npc_names: Dictionary = {}) -> String:
|
||||
return "%s could not complete %s" % [actor_name, action_name]
|
||||
"resource_depleted":
|
||||
return "%s was depleted" % source
|
||||
"animal_fed":
|
||||
return "%s fed %s %.0f %s" % [actor_name, destination, amount, item]
|
||||
_:
|
||||
return "tick %d: %s" % [int(data["tick"]), event_type]
|
||||
|
||||
@@ -2,7 +2,7 @@ class_name SimulationStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_NAME := "the_steward.simulation"
|
||||
const SCHEMA_VERSION := 9
|
||||
const SCHEMA_VERSION := 10
|
||||
const LEGACY_SCHEMA_VERSION := 1
|
||||
const EVENT_LEGACY_SCHEMA_VERSION := 2
|
||||
const RELATIONSHIP_LEGACY_SCHEMA_VERSION := 3
|
||||
@@ -10,11 +10,13 @@ const KNOWLEDGE_LEGACY_SCHEMA_VERSION := 4
|
||||
const PROVENANCE_LEGACY_SCHEMA_VERSION := 5
|
||||
const RETENTION_LEGACY_SCHEMA_VERSION := 6
|
||||
const OPPORTUNITY_LEGACY_SCHEMA_VERSION := 8
|
||||
const ANIMAL_LEGACY_SCHEMA_VERSION := 9
|
||||
const PREVIOUS_SCHEMA_VERSION := 7
|
||||
|
||||
var simulation: Dictionary
|
||||
var village: VillageStateRecord
|
||||
var npcs: Array[NPCStateRecord] = []
|
||||
var animals: Array[AnimalStateRecord] = []
|
||||
var resources: Array[ResourceStateRecord] = []
|
||||
var storages: Array[StorageStateRecord] = []
|
||||
var economic_events: Array[EconomicEventRecord] = []
|
||||
@@ -28,6 +30,9 @@ func to_dictionary() -> Dictionary:
|
||||
for npc_record in npcs:
|
||||
npc_data.append(npc_record.to_dictionary())
|
||||
|
||||
var animal_data: Array[Dictionary] = []
|
||||
for animal_record in animals:
|
||||
animal_data.append(animal_record.to_dictionary())
|
||||
var resource_data: Array[Dictionary] = []
|
||||
for resource_record in resources:
|
||||
resource_data.append(resource_record.to_dictionary())
|
||||
@@ -53,6 +58,7 @@ func to_dictionary() -> Dictionary:
|
||||
"simulation": simulation.duplicate(true),
|
||||
"village": village.to_dictionary(),
|
||||
"npcs": npc_data,
|
||||
"animals": animal_data,
|
||||
"resources": resource_data,
|
||||
"storages": storage_data,
|
||||
"economic_events": event_data,
|
||||
@@ -88,6 +94,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
RETENTION_LEGACY_SCHEMA_VERSION,
|
||||
PREVIOUS_SCHEMA_VERSION,
|
||||
OPPORTUNITY_LEGACY_SCHEMA_VERSION,
|
||||
ANIMAL_LEGACY_SCHEMA_VERSION,
|
||||
]
|
||||
):
|
||||
record_data = _migrate_legacy(record_data, version)
|
||||
@@ -100,6 +107,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
"simulation",
|
||||
"village",
|
||||
"npcs",
|
||||
"animals",
|
||||
"resources",
|
||||
"storages",
|
||||
"economic_events",
|
||||
@@ -166,6 +174,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
return null
|
||||
|
||||
var npc_data = record_data["npcs"]
|
||||
var animal_data = record_data["animals"]
|
||||
var resource_data = record_data["resources"]
|
||||
var storage_data = record_data["storages"]
|
||||
var event_data = record_data["economic_events"]
|
||||
@@ -174,6 +183,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
var opportunity_data = record_data["opportunities"]
|
||||
if (
|
||||
not npc_data is Array
|
||||
or not animal_data is Array
|
||||
or not resource_data is Array
|
||||
or not storage_data is Array
|
||||
or not event_data is Array
|
||||
@@ -217,6 +227,51 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
resource_records_by_id[resource_id] = resource_record
|
||||
record.resources.append(resource_record)
|
||||
|
||||
var animal_ids := {}
|
||||
var animal_records_by_id := {}
|
||||
for item in animal_data:
|
||||
if not item is Dictionary:
|
||||
return null
|
||||
var animal_record := AnimalStateRecord.from_dictionary(item)
|
||||
if animal_record == null:
|
||||
return null
|
||||
var animal_id := String(animal_record.data["animal_id"])
|
||||
if animal_ids.has(animal_id) or resource_ids.has(animal_id):
|
||||
return null
|
||||
var reserved_by := animal_record.get_reserved_by()
|
||||
if reserved_by >= 0:
|
||||
var reserving_npc := npc_records_by_id.get(reserved_by) as NPCStateRecord
|
||||
if (
|
||||
reserving_npc == null
|
||||
or (
|
||||
StringName(reserving_npc.data["current_task"])
|
||||
!= SimulationIds.ACTION_FEED_ANIMAL
|
||||
)
|
||||
or StringName(reserving_npc.data["target_id"]) != animal_record.get_animal_id()
|
||||
or (
|
||||
StringName(reserving_npc.data["task_state"])
|
||||
not in [SimNPC.TASK_STATE_TRAVELING, SimNPC.TASK_STATE_WORKING]
|
||||
)
|
||||
):
|
||||
return null
|
||||
animal_ids[animal_id] = true
|
||||
animal_records_by_id[animal_id] = animal_record
|
||||
record.animals.append(animal_record)
|
||||
for npc_record in record.npcs:
|
||||
if (
|
||||
StringName(npc_record.data["current_task"]) != SimulationIds.ACTION_FEED_ANIMAL
|
||||
or (
|
||||
StringName(npc_record.data["task_state"])
|
||||
not in [SimNPC.TASK_STATE_TRAVELING, SimNPC.TASK_STATE_WORKING]
|
||||
)
|
||||
):
|
||||
continue
|
||||
var target_animal := (
|
||||
animal_records_by_id.get(StringName(npc_record.data["target_id"])) as AnimalStateRecord
|
||||
)
|
||||
if target_animal == null or target_animal.get_reserved_by() != int(npc_record.data["id"]):
|
||||
return null
|
||||
|
||||
var storage_ids := {}
|
||||
var storage_records_by_id := {}
|
||||
for item in storage_data:
|
||||
@@ -226,7 +281,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
if storage_record == null:
|
||||
return null
|
||||
var storage_id := storage_record.get_storage_id()
|
||||
if storage_ids.has(storage_id):
|
||||
if storage_ids.has(storage_id) or animal_ids.has(String(storage_id)):
|
||||
return null
|
||||
storage_ids[storage_id] = true
|
||||
storage_records_by_id[storage_id] = storage_record
|
||||
@@ -235,6 +290,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
var event_ids := {}
|
||||
var event_records_by_id := {}
|
||||
var highest_event_id := -1
|
||||
var latest_feed_ticks := {}
|
||||
for item in event_data:
|
||||
if not item is Dictionary:
|
||||
return null
|
||||
@@ -247,9 +303,33 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
event_ids[event_id] = true
|
||||
event_records_by_id[event_id] = event_record
|
||||
highest_event_id = maxi(highest_event_id, event_id)
|
||||
if StringName(event_record.data["event_type"]) == SimulationIds.EVENT_ANIMAL_FED:
|
||||
if not _is_valid_animal_feed_event(
|
||||
event_record,
|
||||
npc_ids,
|
||||
animal_records_by_id,
|
||||
storage_records_by_id,
|
||||
int(record.simulation["tick_count"])
|
||||
):
|
||||
return null
|
||||
var fed_animal_id := StringName(event_record.data["destination_id"])
|
||||
latest_feed_ticks[fed_animal_id] = maxi(
|
||||
int(latest_feed_ticks.get(fed_animal_id, AnimalStateRecord.NEVER_FED_TICK)),
|
||||
int(event_record.data["tick"])
|
||||
)
|
||||
record.economic_events.append(event_record)
|
||||
if int(record.simulation["next_event_id"]) <= highest_event_id:
|
||||
return null
|
||||
for animal_record in record.animals:
|
||||
if (
|
||||
animal_record.get_last_fed_tick()
|
||||
!= int(
|
||||
latest_feed_ticks.get(
|
||||
animal_record.get_animal_id(), AnimalStateRecord.NEVER_FED_TICK
|
||||
)
|
||||
)
|
||||
):
|
||||
return null
|
||||
|
||||
var knowledge_keys := {}
|
||||
var knowledge_records_by_key := {}
|
||||
@@ -359,6 +439,31 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
return record
|
||||
|
||||
|
||||
static func _is_valid_animal_feed_event(
|
||||
event: EconomicEventRecord,
|
||||
npc_ids: Dictionary,
|
||||
animal_records_by_id: Dictionary,
|
||||
storage_records_by_id: Dictionary,
|
||||
current_tick: int
|
||||
) -> bool:
|
||||
var actor_id := int(event.data["actor_id"])
|
||||
var definition := SimulationDefinitions.get_action(SimulationIds.ACTION_FEED_ANIMAL)
|
||||
var animal := (
|
||||
animal_records_by_id.get(StringName(event.data["destination_id"])) as AnimalStateRecord
|
||||
)
|
||||
return (
|
||||
(actor_id == -1 or npc_ids.has(actor_id))
|
||||
and int(event.data["tick"]) <= current_tick
|
||||
and animal != null
|
||||
and StringName(event.data["source_id"]) == SimulationIds.STORAGE_VILLAGE_PANTRY
|
||||
and storage_records_by_id.has(SimulationIds.STORAGE_VILLAGE_PANTRY)
|
||||
and StringName(event.data["item_id"]) == SimulationIds.RESOURCE_FOOD
|
||||
and definition != null
|
||||
and definition.has_completion_cost()
|
||||
and is_equal_approx(float(event.data["amount"]), definition.completion_cost_amount)
|
||||
)
|
||||
|
||||
|
||||
static func _is_valid_knowledge_provenance(
|
||||
known_event: KnownEventStateRecord,
|
||||
npc_ids: Dictionary,
|
||||
@@ -571,6 +676,7 @@ static func _is_valid_supply_resolution(
|
||||
static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary:
|
||||
var migrated := legacy_data.duplicate(true)
|
||||
migrated["schema_version"] = SCHEMA_VERSION
|
||||
migrated["animals"] = []
|
||||
if version == LEGACY_SCHEMA_VERSION:
|
||||
var village_data: Dictionary = legacy_data.get("village", {})
|
||||
var initial_food := float(village_data.get("food", 0.0))
|
||||
@@ -621,7 +727,7 @@ static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary
|
||||
migrated.get("economic_events", []),
|
||||
int((migrated.get("simulation", {}) as Dictionary).get("tick_count", 0))
|
||||
)
|
||||
if version != OPPORTUNITY_LEGACY_SCHEMA_VERSION:
|
||||
if version not in [OPPORTUNITY_LEGACY_SCHEMA_VERSION, ANIMAL_LEGACY_SCHEMA_VERSION]:
|
||||
migrated["opportunities"] = []
|
||||
var opportunity_simulation_data: Dictionary = migrated.get("simulation", {})
|
||||
opportunity_simulation_data["next_opportunity_id"] = 0
|
||||
|
||||
Reference in New Issue
Block a user