feat: define action completion costs
This commit is contained in:
@@ -179,7 +179,13 @@ func simulate_tick() -> void:
|
||||
if not was_complete and npc.task_complete and not npc.is_dead:
|
||||
var completed_task := npc.current_task
|
||||
var completed_definition := SimulationDefinitions.get_action(completed_task)
|
||||
if (
|
||||
var completion_cost_paid := _consume_action_completion_cost(
|
||||
npc, completed_definition
|
||||
)
|
||||
if not completion_cost_paid:
|
||||
if not npc.target_id.is_empty():
|
||||
release_npc_reservation(npc.id)
|
||||
elif (
|
||||
npc.target_id != &""
|
||||
and completed_definition != null
|
||||
and completed_definition.target_type == SimulationIds.TARGET_RESOURCE
|
||||
@@ -271,11 +277,9 @@ func simulate_tick() -> void:
|
||||
):
|
||||
village.apply_npc_task(npc)
|
||||
elif completed_task == SimulationIds.ACTION_PATROL:
|
||||
if _consume_wood_for_work(npc, completed_task):
|
||||
village.apply_npc_task(npc)
|
||||
village.apply_npc_task(npc)
|
||||
elif completed_task == SimulationIds.ACTION_STUDY:
|
||||
if _consume_wood_for_work(npc, completed_task):
|
||||
village.apply_npc_task(npc)
|
||||
village.apply_npc_task(npc)
|
||||
elif debug_logs:
|
||||
print(
|
||||
"[SimulationManager] ",
|
||||
@@ -635,6 +639,15 @@ func get_woodpile() -> StorageStateRecord:
|
||||
return storage_states.get(SimulationIds.STORAGE_VILLAGE_WOODPILE) as StorageStateRecord
|
||||
|
||||
|
||||
func get_storage_for_resource(resource_id: StringName) -> StorageStateRecord:
|
||||
match resource_id:
|
||||
SimulationIds.RESOURCE_FOOD:
|
||||
return get_pantry()
|
||||
SimulationIds.RESOURCE_WOOD:
|
||||
return get_woodpile()
|
||||
return null
|
||||
|
||||
|
||||
func register_loaded_storage_nodes() -> void:
|
||||
for candidate in StorageNode.get_all():
|
||||
var node := candidate as StorageNode
|
||||
@@ -670,6 +683,14 @@ func _sync_village_wood() -> void:
|
||||
village.update_priorities()
|
||||
|
||||
|
||||
func _sync_village_resource(resource_id: StringName) -> void:
|
||||
match resource_id:
|
||||
SimulationIds.RESOURCE_FOOD:
|
||||
_sync_village_food()
|
||||
SimulationIds.RESOURCE_WOOD:
|
||||
_sync_village_wood()
|
||||
|
||||
|
||||
func deposit_npc_inventory(npc: SimNPC, item_id: StringName) -> float:
|
||||
var available := npc.get_inventory_amount(item_id)
|
||||
var deposited := get_pantry().deposit(item_id, available)
|
||||
@@ -711,26 +732,44 @@ func deposit_npc_wood(npc: SimNPC) -> float:
|
||||
return deposited
|
||||
|
||||
|
||||
func _consume_wood_for_work(npc: SimNPC, action_id: StringName) -> bool:
|
||||
var woodpile := get_woodpile()
|
||||
if woodpile == null:
|
||||
func _consume_action_completion_cost(npc: SimNPC, definition: ActionDefinition) -> bool:
|
||||
if definition == null or not definition.has_completion_cost():
|
||||
return true
|
||||
var resource_id := definition.completion_cost_resource_id
|
||||
var required_amount := definition.completion_cost_amount
|
||||
var storage := get_storage_for_resource(resource_id)
|
||||
var available := storage.get_amount(resource_id) if storage != null else 0.0
|
||||
if storage == null or available < required_amount:
|
||||
var reason := "%s: needs %.0f %s (%.1f available)" % [
|
||||
definition.display_name,
|
||||
required_amount,
|
||||
String(resource_id).capitalize(),
|
||||
available,
|
||||
]
|
||||
record_narrative_event(
|
||||
SimulationIds.EVENT_TASK_BLOCKED,
|
||||
npc.id,
|
||||
storage.get_storage_id() if storage != null else &"",
|
||||
reason
|
||||
)
|
||||
return false
|
||||
if woodpile.get_amount(SimulationIds.RESOURCE_WOOD) < 1.0:
|
||||
return false
|
||||
var consumed := woodpile.withdraw(SimulationIds.RESOURCE_WOOD, 1.0)
|
||||
_sync_village_wood()
|
||||
if consumed < 1.0:
|
||||
var consumed := storage.withdraw(resource_id, required_amount)
|
||||
_sync_village_resource(resource_id)
|
||||
if consumed < required_amount:
|
||||
return false
|
||||
_record_economic_event(
|
||||
SimulationIds.EVENT_ITEM_CONSUMED,
|
||||
npc.id,
|
||||
SimulationIds.STORAGE_VILLAGE_WOODPILE,
|
||||
storage.get_storage_id(),
|
||||
&"consumed",
|
||||
SimulationIds.RESOURCE_WOOD,
|
||||
resource_id,
|
||||
consumed
|
||||
)
|
||||
if debug_logs:
|
||||
print("[SimulationManager] %s consumed wood for %s" % [npc.npc_name, action_id])
|
||||
print(
|
||||
"[SimulationManager] %s consumed %.1f %s for %s"
|
||||
% [npc.npc_name, consumed, resource_id, definition.action_id]
|
||||
)
|
||||
return true
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user