refactor: move resource authority into simulation

This commit is contained in:
2026-07-05 13:26:19 +02:00
parent f83d2c7704
commit 363620b731
10 changed files with 480 additions and 174 deletions
+18 -3
View File
@@ -87,11 +87,26 @@ func _run() -> void:
"Jajce navigation path should exist from %s to %s" % [origin, destination]
)
var selected := ResourceNode.find_available(&"gather_food", 777, Vector3.ZERO)
var manager: Node = load("res://simulation/SimulationManager.gd").new()
manager.debug_logs = false
root.add_child(manager)
manager.set_process(false)
await process_frame
var selected: ResourceNode = manager.acquire_resource_target(
&"gather_food",
777,
Vector3.ZERO
)
_check(selected != null, "Target discovery should find Jajce resources without a parent path")
if selected != null:
_check(selected.reserve(777), "Selected Jajce resource should be reservable")
selected.release(777)
var selected_state: ResourceStateRecord = manager.get_resource_state(
selected.node_id
)
_check(
selected_state.get_reserved_by() == 777,
"Selected Jajce resource should be authoritatively reserved"
)
manager.release_resource(selected.node_id, 777)
if failures.is_empty():
print("[TEST] Jajce scaffold passed: Terrain3D, stable IDs, 24 navigation routes")
+27 -6
View File
@@ -18,22 +18,43 @@ func _run() -> void:
var tree := main_scene.get_node("ResourceNodes/Tree_01") as ResourceNode
simulation_manager.set_process(false)
bush.amount_remaining = 1.0
_check(bush.reserve(999), "Player contention setup should reserve the bush")
var bush_state: ResourceStateRecord = simulation_manager.get_resource_state(
bush.node_id
)
var tree_state: ResourceStateRecord = simulation_manager.get_resource_state(
tree.node_id
)
bush_state.set_amount_remaining(1.0)
_check(
simulation_manager.reserve_resource(bush.node_id, 999),
"Player contention setup should reserve the bush"
)
player.global_position = bush.interaction_point.global_position
var food_before: float = simulation_manager.village.food
player.try_interact()
_check(is_equal_approx(bush.amount_remaining, 0.0), "Player should deplete the nearby bush")
_check(
is_equal_approx(bush_state.get_amount_remaining(), 0.0),
"Player should deplete the nearby bush"
)
_check(is_equal_approx(simulation_manager.village.food, food_before + 1.0), "Village should receive exactly the bush's remaining food")
_check(bush.reserved_by == -1, "Depletion should release the NPC reservation")
_check(
bush_state.get_reserved_by() == -1,
"Depletion should release the NPC reservation"
)
player.global_position = tree.interaction_point.global_position
var wood_before: float = simulation_manager.village.wood
var tree_before := tree.amount_remaining
var tree_before := tree_state.get_amount_remaining()
player.try_interact()
_check(is_equal_approx(tree.amount_remaining, tree_before - tree.yield_per_action), "Player should extract the tree's configured yield")
_check(
is_equal_approx(
tree_state.get_amount_remaining(),
tree_before - tree.yield_per_action
),
"Player should extract the tree's configured yield"
)
_check(is_equal_approx(simulation_manager.village.wood, wood_before + tree.yield_per_action), "Village should receive exactly the extracted wood")
if failures.is_empty():
+66 -12
View File
@@ -8,6 +8,7 @@ func _initialize() -> void:
func _run() -> void:
await _test_deterministic_continuation()
await _test_resource_state_round_trip()
_test_legacy_resource_migration()
_test_schema_rejection()
if failures.is_empty():
@@ -64,33 +65,65 @@ func _test_resource_state_round_trip() -> void:
"res://world/resource_nodes/ResourceNode.tscn"
).instantiate()
resource.node_id = &"SerializationTestBerry"
resource.amount_remaining = 9.0
resource.initial_amount = 9.0
resource.yield_per_action = 2.0
root.add_child(resource)
await process_frame
_check(resource.reserve(42), "Resource should accept the test reservation")
resource.extract()
var manager := _create_manager(77)
_check(
manager.register_resource_node(resource),
"Resource presentation should bind to simulation authority"
)
var resource_state: ResourceStateRecord = manager.get_resource_state(
resource.node_id
)
_check(resource_state.reserve(42), "Resource should accept the test reservation")
resource_state.extract()
var saved_json: String = manager.serialize_state()
resource.extract(5.0)
resource.release(42)
resource.enabled = false
resource.free()
_check(
is_equal_approx(resource_state.get_amount_remaining(), 7.0),
"Resource state should survive without its presentation node"
)
resource_state.extract(5.0)
resource_state.release(42)
resource_state.set_enabled(false)
_check(
manager.restore_state_from_json(saved_json),
"Resource state should restore with the simulation record"
"Unloaded resource state should restore with the simulation record"
)
resource_state = manager.get_resource_state(&"SerializationTestBerry")
_check(
is_equal_approx(resource.amount_remaining, 7.0),
is_equal_approx(resource_state.get_amount_remaining(), 7.0),
"Resource amount should round-trip"
)
_check(resource.reserved_by == 42, "Resource reservation should round-trip")
_check(resource.enabled, "Resource enabled state should round-trip")
_check(
resource_state.get_reserved_by() == 42,
"Resource reservation should round-trip"
)
_check(resource_state.is_enabled(), "Resource enabled state should round-trip")
var rebound: ResourceNode = load(
"res://world/resource_nodes/ResourceNode.tscn"
).instantiate()
rebound.node_id = &"SerializationTestBerry"
rebound.initial_amount = 999.0
root.add_child(rebound)
await process_frame
_check(
rebound.state == resource_state,
"Reloaded presentation should bind to the existing authoritative record"
)
_check(
is_equal_approx(rebound.get_amount_remaining(), 7.0),
"Reloaded presentation must not overwrite authoritative amount"
)
manager.free()
resource.free()
rebound.free()
func _test_schema_rejection() -> void:
var unsupported := JSON.stringify({
@@ -106,6 +139,27 @@ func _test_schema_rejection() -> void:
"Non-record JSON should be rejected"
)
func _test_legacy_resource_migration() -> void:
var migrated := ResourceStateRecord.from_dictionary({
"schema_version": 1,
"node_id": "legacy_tree",
"amount_remaining": 6.0,
"reserved_by": 12,
"enabled": true
})
_check(migrated != null, "ResourceStateRecord v1 should migrate explicitly")
if migrated == null:
return
_check(
migrated.data["schema_version"] == ResourceStateRecord.SCHEMA_VERSION,
"Migrated resource state should use the current nested schema"
)
_check(
is_equal_approx(migrated.get_amount_remaining(), 6.0)
and migrated.get_reserved_by() == 12,
"Resource migration should preserve mutable authority"
)
func _create_manager(seed_value: int) -> Node:
var manager: Node = load("res://simulation/SimulationManager.gd").new()
manager.simulation_seed = seed_value