Files
gamedev-the-steward/tests/simulation_population_view_test.gd
2026-07-16 23:51:08 +02:00

78 lines
2.6 KiB
GDScript

extends SceneTree
const RelationshipSystemScript := preload("res://simulation/relationships/RelationshipSystem.gd")
var failures: Array[String] = []
func _initialize() -> void:
call_deferred("_run")
func _run() -> void:
var first := _create_npc(0, "First")
var second := _create_npc(1, "Second")
var observer := _create_npc(2, "Observer")
first.is_starving = true
var npcs: Array[SimNPC] = [first, second, observer]
var view := SimulationPopulationView.new(npcs)
_check(view.living_count() == 3, "The initial view should index every living NPC")
_check(view.starving_count() == 1, "The initial view should index the starving NPC once")
_check(view.get_starving(first.id) == first, "The starving lookup should preserve identity")
second.is_starving = true
view.refresh_npc(second)
_check(view.starving_count() == 2, "Refreshing should add a newly starving NPC")
first.die_from_starvation()
view.refresh_npc(first)
_check(
view.get_living(first.id) == null and view.get_starving(first.id) == null,
"Refreshing should remove a dead NPC from both indexes"
)
_check(view.get_any(first.id) == first, "The identity index should retain a dead NPC record")
var relationships := RelationshipSystemScript.new()
var records: Array[RelationshipStateRecord] = [
RelationshipStateRecord.create(observer.id, first.id, 0.8, 0.9),
RelationshipStateRecord.create(observer.id, second.id, 0.7, 0.7),
]
relationships.restore(records)
_check(
relationships.get_trusted_starving_subject(observer.id, npcs, view) == second,
"The relationship query should ignore the stronger dead subject through the shared view"
)
_check(
relationships.get_most_familiar_living(observer.id, npcs, view) == second,
"The living-subject query should use the same refreshed view"
)
first.is_dead = false
first.is_starving = true
view.refresh_npc(first)
_check(
relationships.get_trusted_starving_subject(observer.id, npcs, view) == first,
"A refreshed living subject should regain eligibility without rebuilding the view"
)
_finish()
func _create_npc(npc_id: int, npc_name: String) -> SimNPC:
var random_source := RandomNumberGenerator.new()
random_source.seed = 7000 + npc_id
return SimNPC.new(npc_id, npc_name, SimulationIds.PROFESSION_FARMER, 5.0, 5.0, random_source)
func _check(condition: bool, message: String) -> void:
if not condition:
failures.append(message)
func _finish() -> void:
if failures.is_empty():
print("[TEST] Simulation population view passed: refresh -> shared stable-ID queries")
quit(0)
return
for failure in failures:
push_error("[TEST] " + failure)
quit(1)