feat: populate procedural floors
This commit is contained in:
@@ -8,7 +8,9 @@ const SUPPORTED_CATALOGS := {
|
||||
"spells.json": "spell",
|
||||
"loot_tables.json": "loot_table",
|
||||
"npcs.json": "npc",
|
||||
"traps.json": "trap",
|
||||
"quests": "quest",
|
||||
"events": "event",
|
||||
}
|
||||
|
||||
func test_every_supported_ascii_id_has_exactly_one_catalog_entry() -> void:
|
||||
@@ -23,7 +25,7 @@ func test_every_supported_ascii_id_has_exactly_one_catalog_entry() -> void:
|
||||
|
||||
func _source_ids(source_path: String) -> Array[StringName]:
|
||||
var ids: Array[StringName] = []
|
||||
if source_path == "quests":
|
||||
if source_path in ["quests", "events"]:
|
||||
var directory := DirAccess.open(ASCII_DATA_PATH.path_join(source_path))
|
||||
directory.list_dir_begin()
|
||||
var filename := directory.get_next()
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
extends GutTest
|
||||
|
||||
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
|
||||
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
|
||||
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
||||
|
||||
const ASCII_CATALOG_PATH := "res://data/ascii"
|
||||
const CARDINAL_DIRECTIONS := [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN]
|
||||
|
||||
func test_one_hundred_seeds_have_safe_catalog_backed_reachable_population() -> void:
|
||||
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
|
||||
assert_eq(Array(catalog.get_diagnostics()), [])
|
||||
|
||||
for seed_index in range(100):
|
||||
var floor := FloorGeneratorType.generate("B4-safety-%03d" % seed_index, 1 + seed_index % 10, catalog)
|
||||
var reachable := _reachable_cells_treating_closed_doors_openable(floor.map, floor.entrance)
|
||||
var occupied: Dictionary[Vector2i, bool] = {}
|
||||
|
||||
assert_false(_has_blocking_occupant_at(floor, floor.entrance), "seed %d keeps entrance clear" % seed_index)
|
||||
assert_false(_has_blocking_occupant_at(floor, floor.exit), "seed %d keeps exit clear" % seed_index)
|
||||
assert_true(reachable.has(floor.exit), "seed %d keeps exit reachable" % seed_index)
|
||||
|
||||
for enemy in floor.enemies.values():
|
||||
assert_ne(catalog.get_enemy(enemy.content_id), {}, "seed %d enemy is catalog backed" % seed_index)
|
||||
assert_lte(int(catalog.get_enemy(enemy.content_id).get("min_floor", 1)), floor.floor_number)
|
||||
_assert_valid_unique_reachable_cell(floor, enemy.cell, occupied, reachable, seed_index)
|
||||
for npc in floor.npcs.values():
|
||||
assert_ne(catalog.get_npc(npc.content_id), {}, "seed %d NPC is catalog backed" % seed_index)
|
||||
_assert_valid_unique_reachable_cell(floor, npc.cell, occupied, reachable, seed_index)
|
||||
for cell in floor.chests:
|
||||
assert_ne(catalog.get_loot_table(StringName(floor.chests[cell]["loot_table_id"])), {})
|
||||
_assert_valid_unique_reachable_cell(floor, cell, occupied, reachable, seed_index)
|
||||
for cell in floor.traps:
|
||||
assert_ne(catalog.get_trap(StringName(floor.traps[cell]["trap_id"])), {})
|
||||
_assert_valid_unique_reachable_cell(floor, cell, occupied, reachable, seed_index)
|
||||
for event in floor.events:
|
||||
assert_ne(catalog.get_event(StringName(event["event_id"])), {})
|
||||
var cell_data: Array = event["cell"]
|
||||
_assert_valid_unique_reachable_cell(floor, Vector2i(int(cell_data[0]), int(cell_data[1])), occupied, reachable, seed_index)
|
||||
|
||||
func _assert_valid_unique_reachable_cell(floor: FloorState, cell: Vector2i, occupied: Dictionary[Vector2i, bool], reachable: Dictionary[Vector2i, bool], seed_index: int) -> void:
|
||||
assert_false(occupied.has(cell), "seed %d has no overlapping population" % seed_index)
|
||||
occupied[cell] = true
|
||||
assert_ne(cell, floor.entrance)
|
||||
assert_ne(cell, floor.exit)
|
||||
assert_true(reachable.has(cell), "seed %d population is reachable" % seed_index)
|
||||
assert_eq(floor.map.get_cell(cell), GridMapStateType.CellKind.FLOOR)
|
||||
|
||||
func _has_blocking_occupant_at(floor: FloorState, cell: Vector2i) -> bool:
|
||||
for actor in floor.enemies.values() + floor.npcs.values():
|
||||
if actor.cell == cell:
|
||||
return true
|
||||
return floor.chests.has(cell) or floor.traps.has(cell)
|
||||
|
||||
func _reachable_cells_treating_closed_doors_openable(map: GridMapState, start: Vector2i) -> Dictionary[Vector2i, bool]:
|
||||
var reachable: Dictionary[Vector2i, bool] = {start: true}
|
||||
var frontier: Array[Vector2i] = [start]
|
||||
while not frontier.is_empty():
|
||||
var current := frontier.pop_front()
|
||||
for direction in CARDINAL_DIRECTIONS:
|
||||
var next_cell: Vector2i = current + direction
|
||||
if (map.is_walkable(next_cell) or map.get_cell(next_cell) == GridMapStateType.CellKind.CLOSED_DOOR) and not reachable.has(next_cell):
|
||||
reachable[next_cell] = true
|
||||
frontier.append(next_cell)
|
||||
return reachable
|
||||
@@ -0,0 +1 @@
|
||||
uid://dfpcbrduher8h
|
||||
@@ -12,6 +12,12 @@ func test_item_fixture_preserves_rusty_sword_fields() -> void:
|
||||
assert_eq(sword.get("rarity"), "commun")
|
||||
assert_eq(sword.get("damage"), 6)
|
||||
|
||||
func test_catalog_exposes_ascii_traps_and_events() -> void:
|
||||
var catalog := ContentCatalog.load_from_directory("res://data/ascii")
|
||||
|
||||
assert_ne(catalog.get_trap(&"trap_spikes"), {})
|
||||
assert_ne(catalog.get_event(&"event_narrative_note"), {})
|
||||
|
||||
func test_missing_required_id_records_explicit_diagnostic() -> void:
|
||||
var catalog := ContentCatalog.load_from_directory("res://tests/fixtures/content_loader_missing_id")
|
||||
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
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_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)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dnrqxd18bgmdm
|
||||
@@ -18,9 +18,10 @@ func test_floor_state_round_trip_preserves_rooms_tiles_and_floor_collections() -
|
||||
floor.rooms.append(RoomStateType.new(&"exit", Rect2i(5, 3, 2, 2), &"combat"))
|
||||
floor.entrance = Vector2i(2, 2)
|
||||
floor.exit = Vector2i(6, 4)
|
||||
floor.enemies[&"goblin"] = ActorStateType.new(&"goblin", Vector2i(5, 4), 6, 2)
|
||||
floor.npcs[&"guide"] = ActorStateType.new(&"guide", Vector2i(3, 3), 9, 0)
|
||||
floor.enemies[&"goblin"] = ActorStateType.new(&"goblin", Vector2i(5, 4), 6, 2, &"enemy_goblin")
|
||||
floor.npcs[&"guide"] = ActorStateType.new(&"guide", Vector2i(3, 3), 9, 0, &"npc_guide_mordecai")
|
||||
floor.chests[Vector2i(3, 2)] = {"opened": false, "loot": [{"id": "potion_minor", "quantity": 2}]}
|
||||
floor.traps[Vector2i(3, 3)] = {"trap_id": "trap_spikes"}
|
||||
floor.ground_items[Vector2i(4, 2)] = [{"id": "gold", "quantity": 10}]
|
||||
floor.events.append({"id": "entry_event", "resolved": false})
|
||||
floor.explored_cells[Vector2i(2, 2)] = true
|
||||
@@ -45,10 +46,13 @@ func test_floor_state_round_trip_preserves_rooms_tiles_and_floor_collections() -
|
||||
assert_eq(restored.entrance, Vector2i(2, 2))
|
||||
assert_eq(restored.exit, Vector2i(6, 4))
|
||||
assert_eq(restored.enemies[&"goblin"].cell, Vector2i(5, 4))
|
||||
assert_eq(restored.enemies[&"goblin"].content_id, &"enemy_goblin")
|
||||
assert_eq(restored.npcs[&"guide"].cell, Vector2i(3, 3))
|
||||
assert_eq(restored.npcs[&"guide"].content_id, &"npc_guide_mordecai")
|
||||
assert_true(restored.entities.has(&"goblin"))
|
||||
assert_false(restored.entities.has(&"guide"))
|
||||
assert_eq(restored.chests[Vector2i(3, 2)], {"opened": false, "loot": [{"id": "potion_minor", "quantity": 2}]})
|
||||
assert_eq(restored.traps[Vector2i(3, 3)], {"trap_id": "trap_spikes"})
|
||||
assert_eq(restored.ground_items[Vector2i(4, 2)], [{"id": "gold", "quantity": 10}])
|
||||
assert_eq(restored.events, [{"id": "entry_event", "resolved": false}])
|
||||
assert_eq(restored.explored_cells[Vector2i(2, 2)], true)
|
||||
|
||||
Reference in New Issue
Block a user