fix: enforce village resource invariants

This commit is contained in:
Rijad Zuzo
2026-07-09 23:10:18 +02:00
parent d6520c426a
commit 5cbab527bf
9 changed files with 136 additions and 42 deletions
+46
View File
@@ -112,6 +112,7 @@ func _run() -> void:
"Exactly one food unit should leave the world when eaten"
)
_check_economic_event_chain(manager)
_test_wood_work_requires_material()
var restored: Node = load("res://simulation/SimulationManager.gd").new()
restored.debug_logs = false
@@ -179,3 +180,48 @@ func _check_economic_event_chain(manager: Node) -> void:
),
"Economic events should record exact transferred quantities"
)
func _test_wood_work_requires_material() -> void:
var manager: Node = load("res://simulation/SimulationManager.gd").new()
manager.debug_logs = false
root.add_child(manager)
manager.set_process(false)
var woodpile: StorageStateRecord = manager.get_woodpile()
woodpile.withdraw(SimulationIds.RESOURCE_WOOD, 1000.0)
woodpile.deposit(SimulationIds.RESOURCE_WOOD, 0.5)
manager._sync_village_wood()
var npc: SimNPC = manager.npcs[0]
var safety_before: float = manager.village.safety
npc.set_task(SimulationIds.ACTION_PATROL, 1.0)
npc.start_working()
manager.simulate_tick()
_check(
(
is_equal_approx(manager.village.safety, safety_before)
and is_equal_approx(woodpile.get_amount(SimulationIds.RESOURCE_WOOD), 0.5)
),
"Patrol should not consume partial wood or create safety when its full cost cannot be paid"
)
manager.add_wood(1.5)
_check(
(
is_equal_approx(woodpile.get_amount(SimulationIds.RESOURCE_WOOD), 2.0)
and is_equal_approx(manager.village.wood, 2.0)
),
"Wood changes should update authoritative storage and the village projection"
)
var knowledge_before: float = manager.village.knowledge
npc.set_task(SimulationIds.ACTION_STUDY, 1.0)
npc.start_working()
manager.simulate_tick()
_check(
(
is_equal_approx(woodpile.get_amount(SimulationIds.RESOURCE_WOOD), 1.0)
and manager.village.knowledge > knowledge_before
),
"Funded study should consume one wood and produce knowledge"
)
manager.free()