Files
DungeonCrawlerWorld/tests/unit/test_floor_populator.gd
T

133 lines
6.1 KiB
GDScript

extends GutTest
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
const FloorPopulatorType = preload("res://scripts/domain/world/floor_populator.gd")
const SpawnRulesType = preload("res://scripts/domain/world/spawn_rules.gd")
const FloorStateType = preload("res://scripts/domain/world/floor_state.gd")
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
const RoomStateType = preload("res://scripts/domain/world/room_state.gd")
const ASCII_CATALOG_PATH := "res://data/ascii"
const ROLE_KINDS := [&"entrance", &"combat", &"boss", &"arena", &"treasure", &"trap", &"shop", &"guild", &"sanctuary", &"refuge", &"narrative", &"mystery", &"puzzle"]
func test_population_is_deterministic_for_an_identical_seed_and_catalog() -> void:
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
var first := FloorGeneratorType.generate("B4-identical", 3, catalog)
var second := FloorGeneratorType.generate("B4-identical", 3, catalog)
assert_eq(first.to_dict(), second.to_dict())
func test_population_varies_with_the_seed() -> void:
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
var first := FloorGeneratorType.generate("B4-variation-a", 3, catalog)
var second := FloorGeneratorType.generate("B4-variation-b", 3, catalog)
assert_ne(_population_snapshot(first), _population_snapshot(second))
func test_spawn_rules_filter_enemies_by_minimum_floor() -> void:
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
var first_floor := SpawnRulesType.eligible_enemies(catalog, 1)
var late_floor := SpawnRulesType.eligible_enemies(catalog, 20)
assert_gt(first_floor.size(), 0)
assert_gt(late_floor.size(), first_floor.size())
for definition: Dictionary in first_floor:
assert_lte(int(definition.get("min_floor", 1)), 1)
func test_role_population_uses_catalog_content_and_respects_reserved_cells() -> void:
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
var floor := _role_fixture_floor()
FloorPopulatorType.populate(floor, catalog, "B4-role-contract")
assert_eq(floor.enemies.size(), 3, "combat, boss, and arena rooms receive enemies")
assert_eq(floor.chests.size(), 1, "treasure rooms receive a chest")
assert_eq(floor.traps.size(), 1, "trap rooms receive a trap")
assert_eq(floor.npcs.size(), 4, "shop, guild, sanctuary, and refuge rooms receive NPC records")
assert_eq(floor.events.size(), 3, "narrative, mystery, and puzzle rooms receive event records")
assert_eq(_npc_content_ids(floor, &"shop"), [&"npc_wandering_merchant"])
assert_eq(_npc_content_ids(floor, &"guild"), [&"npc_guild_keeper"])
_assert_all_records_are_catalog_backed(catalog, floor)
_assert_reserved_cells_are_clear_and_all_occupants_are_distinct(floor)
func _role_fixture_floor() -> FloorState:
var map := GridMapStateType.new(Vector2i(96, 12), GridMapStateType.CellKind.WALL)
var floor := FloorStateType.new(map, 44, 3)
for index in range(ROLE_KINDS.size()):
var bounds := Rect2i(Vector2i(1 + index * 7, 3), Vector2i(5, 5))
var room := RoomStateType.new(StringName("room_%02d" % index), bounds, ROLE_KINDS[index])
floor.rooms.append(room)
for y in range(bounds.position.y, bounds.end.y):
for x in range(bounds.position.x, bounds.end.x):
var cell := Vector2i(x, y)
map.set_cell(cell, GridMapStateType.CellKind.FLOOR)
map.set_room_id(cell, room.id)
floor.entrance = _room_center(floor.rooms[0].bounds)
floor.exit = _room_center(floor.rooms[1].bounds)
map.set_cell(floor.exit, GridMapStateType.CellKind.STAIRS)
return floor
func _population_snapshot(floor: FloorState) -> Dictionary:
return {
"enemies": floor.to_dict()["enemies"],
"npcs": floor.to_dict()["npcs"],
"chests": floor.to_dict()["chests"],
"traps": floor.to_dict()["traps"],
"events": floor.events,
}
func _npc_content_ids(floor: FloorState, room_kind: StringName) -> Array[StringName]:
var ids: Array[StringName] = []
for npc in floor.npcs.values():
if _room_for_cell(floor, npc.cell).kind == room_kind:
ids.append(npc.content_id)
ids.sort()
return ids
func _assert_all_records_are_catalog_backed(catalog: ContentCatalog, floor: FloorState) -> void:
for enemy in floor.enemies.values():
assert_ne(catalog.get_enemy(enemy.content_id), {})
assert_lte(int(catalog.get_enemy(enemy.content_id).get("min_floor", 1)), floor.floor_number)
for npc in floor.npcs.values():
assert_ne(catalog.get_npc(npc.content_id), {})
for chest in floor.chests.values():
assert_eq(StringName(chest["loot_table_id"]), &"floor_treasure")
assert_ne(catalog.get_loot_table(StringName(chest["loot_table_id"])), {})
for trap in floor.traps.values():
assert_ne(catalog.get_trap(StringName(trap["trap_id"])), {})
for event in floor.events:
assert_ne(catalog.get_event(StringName(event["event_id"])), {})
func _assert_reserved_cells_are_clear_and_all_occupants_are_distinct(floor: FloorState) -> void:
var occupied: Dictionary[Vector2i, bool] = {}
for actor in floor.enemies.values() + floor.npcs.values():
assert_false(occupied.has(actor.cell), "actor cells do not overlap")
occupied[actor.cell] = true
for cell in floor.chests:
assert_false(occupied.has(cell), "chests do not overlap actors or each other")
occupied[cell] = true
for cell in floor.traps:
assert_false(occupied.has(cell), "traps do not overlap any other occupant")
occupied[cell] = true
for event in floor.events:
var cell := _event_cell(event)
assert_false(occupied.has(cell), "events do not overlap any other occupant")
occupied[cell] = true
assert_false(occupied.has(floor.entrance))
assert_false(occupied.has(floor.exit))
func _event_cell(event: Dictionary) -> Vector2i:
var serialized: Array = event["cell"]
return Vector2i(int(serialized[0]), int(serialized[1]))
func _room_for_cell(floor: FloorState, cell: Vector2i) -> RoomState:
var room_id := floor.map.get_room_id(cell)
for room in floor.rooms:
if room.id == room_id:
return room
return null
func _room_center(bounds: Rect2i) -> Vector2i:
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)