feat: add procedural doors and room roles

This commit is contained in:
goose
2026-10-02 23:13:52 -04:00
parent eb595da756
commit 7c9390bb0a
7 changed files with 390 additions and 18 deletions
@@ -13,7 +13,7 @@ func test_one_hundred_seeds_keep_every_room_center_entrance_and_exit_reachable()
var floor := FloorGeneratorType.generate("B2-connectivity-%03d" % seed_index, 1, catalog)
assert_gte(floor.rooms.size(), 8, "seed %d creates at least eight padded rooms" % seed_index)
_assert_padded_rooms(floor.rooms, seed_index)
var reachable := _reachable_cells(floor.map, floor.entrance)
var reachable := _reachable_cells_treating_closed_doors_openable(floor.map, floor.entrance)
var required_cells: Array[Vector2i] = [floor.entrance, floor.exit]
for room in floor.rooms:
required_cells.append(_room_center(room.bounds))
@@ -29,7 +29,7 @@ func _assert_padded_rooms(rooms: Array[RoomState], seed_index: int) -> void:
"seed %d keeps %s and %s padded" % [seed_index, rooms[first_index].id, rooms[second_index].id]
)
func _reachable_cells(map: GridMapState, start: Vector2i) -> Dictionary[Vector2i, bool]:
func _reachable_cells_treating_closed_doors_openable(map: GridMapState, start: Vector2i) -> Dictionary[Vector2i, bool]:
var reachable: Dictionary[Vector2i, bool] = {}
var frontier: Array[Vector2i] = [start]
reachable[start] = true
@@ -37,7 +37,7 @@ func _reachable_cells(map: GridMapState, start: Vector2i) -> Dictionary[Vector2i
var current := frontier.pop_front()
for direction in CARDINAL_DIRECTIONS:
var next_cell: Vector2i = current + direction
if map.is_walkable(next_cell) and not reachable.has(next_cell):
if (map.is_walkable(next_cell) or map.get_cell(next_cell) == GridMapState.CellKind.CLOSED_DOOR) and not reachable.has(next_cell):
reachable[next_cell] = true
frontier.append(next_cell)
return reachable
@@ -0,0 +1,85 @@
extends GutTest
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
const RoomGraphType = preload("res://scripts/domain/world/room_graph.gd")
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
const CARDINAL_DIRECTIONS := [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN]
const LOOP_NEARBY_RANK_LIMIT := 6
func test_one_hundred_seeds_keep_the_farthest_tree_exit_reachable_through_openable_doors() -> void:
var catalog := ContentCatalogType.new()
for seed_index in range(100):
var floor := FloorGeneratorType.generate("B3-exit-path-%03d" % seed_index, 1 + seed_index % 6, catalog)
var graph := RoomGraphType.from_dict(floor.metadata["room_graph"])
var entrance_index := _room_index(floor, floor.entrance)
var exit_index := _room_index(floor, floor.exit)
var distances := graph.tree_distances(entrance_index)
var maximum_distance := 0
for distance in distances.values():
maximum_distance = maxi(maximum_distance, int(distance))
assert_ne(entrance_index, exit_index, "seed %d keeps entry and exit rooms distinct" % seed_index)
assert_eq(distances[exit_index], maximum_distance, "seed %d selects a farthest tree exit" % seed_index)
assert_true(
_reachable_cells_treating_closed_doors_openable(floor.map, floor.entrance).has(floor.exit),
"seed %d reaches the exit through openable tree doors" % seed_index
)
_assert_loops_are_local(graph, floor, seed_index)
func _assert_loops_are_local(graph: RoomGraph, floor: FloorState, seed_index: int) -> void:
for edge: Dictionary in graph.loop_edges:
var from_index := int(edge["from"])
var to_index := int(edge["to"])
var ranked_neighbors := _nearest_room_indices(floor, from_index)
assert_true(
ranked_neighbors.slice(0, mini(LOOP_NEARBY_RANK_LIMIT, ranked_neighbors.size())).has(to_index),
"seed %d loop %d-%d joins nearby rooms" % [seed_index, from_index, to_index]
)
func _nearest_room_indices(floor: FloorState, room_index: int) -> Array[int]:
var ranked: Array[Dictionary] = []
var from_center := _room_center(floor.rooms[room_index].bounds)
for candidate_index in range(floor.rooms.size()):
if candidate_index == room_index:
continue
ranked.append({
"index": candidate_index,
"distance": from_center.distance_to(_room_center(floor.rooms[candidate_index].bounds)),
})
ranked.sort_custom(func(first: Dictionary, second: Dictionary) -> bool:
if is_equal_approx(first["distance"], second["distance"]):
return int(first["index"]) < int(second["index"])
return first["distance"] < second["distance"]
)
var indices: Array[int] = []
for candidate: Dictionary in ranked:
indices.append(int(candidate["index"]))
return indices
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 _is_openable_walkable(map, next_cell) and not reachable.has(next_cell):
reachable[next_cell] = true
frontier.append(next_cell)
return reachable
func _is_openable_walkable(map: GridMapState, cell: Vector2i) -> bool:
return map.is_walkable(cell) or map.get_cell(cell) == GridMapStateType.CellKind.CLOSED_DOOR
func _room_index(floor: FloorState, cell: Vector2i) -> int:
var id := floor.map.get_room_id(cell)
for index in range(floor.rooms.size()):
if floor.rooms[index].id == id:
return index
return -1
func _room_center(bounds: Rect2i) -> Vector2i:
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)
@@ -0,0 +1 @@
uid://wa7y21v3hbvl
+68
View File
@@ -0,0 +1,68 @@
extends GutTest
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
const RoomGraphType = preload("res://scripts/domain/world/room_graph.gd")
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
const ROOM_KINDS := [
&"entrance", &"combat", &"treasure", &"trap", &"sanctuary", &"shop",
&"mystery", &"puzzle", &"arena", &"narrative", &"refuge", &"guild", &"boss",
]
func test_exit_uses_a_different_farthest_room_on_the_spanning_tree() -> void:
var floor := FloorGeneratorType.generate("B3-farthest-exit", 1, ContentCatalogType.new())
var graph := RoomGraphType.from_dict(floor.metadata["room_graph"])
var entrance_index := _room_index(floor, floor.entrance)
var exit_index := _room_index(floor, floor.exit)
var distances := graph.tree_distances(entrance_index)
var maximum_distance := 0
for distance in distances.values():
maximum_distance = maxi(maximum_distance, int(distance))
assert_ne(exit_index, entrance_index)
assert_eq(distances[exit_index], maximum_distance)
assert_eq(graph.tree_edges.size(), floor.rooms.size() - 1)
func test_tree_corridors_have_closed_openable_boundary_doors() -> void:
var floor := FloorGeneratorType.generate("B3-tree-doors", 2, ContentCatalogType.new())
var tree_doors: Array = floor.metadata["tree_door_cells"]
assert_gt(tree_doors.size(), 0)
for serialized_cell: Array in tree_doors:
var door_cell := Vector2i(int(serialized_cell[0]), int(serialized_cell[1]))
assert_eq(floor.map.get_cell(door_cell), GridMapStateType.CellKind.CLOSED_DOOR)
assert_true(_has_adjacent_room(floor.map, door_cell), "door %s is at a room/corridor boundary" % door_cell)
func test_room_roles_follow_ascii_weights_and_floor_gates() -> void:
var first_floor := FloorGeneratorType.generate("B3-role-gates", 1, ContentCatalogType.new())
var boss_floor := FloorGeneratorType.generate("B3-role-gates", 5, ContentCatalogType.new())
_assert_valid_roles(first_floor)
_assert_valid_roles(boss_floor)
assert_eq(_room_for_cell(first_floor, first_floor.entrance).kind, &"entrance")
assert_eq(_room_for_cell(first_floor, first_floor.exit).kind, &"combat")
assert_eq(_room_for_cell(boss_floor, boss_floor.exit).kind, &"boss")
for room in first_floor.rooms:
assert_ne(room.kind, &"shop", "shops are gated until floor 3")
assert_ne(room.kind, &"boss", "boss rooms are gated to boss floors")
func _assert_valid_roles(floor: FloorState) -> void:
for room in floor.rooms:
assert_has(ROOM_KINDS, room.kind, "%s has a supported B3 room kind" % room.id)
func _room_index(floor: FloorState, cell: Vector2i) -> int:
var id := floor.map.get_room_id(cell)
for index in range(floor.rooms.size()):
if floor.rooms[index].id == id:
return index
return -1
func _room_for_cell(floor: FloorState, cell: Vector2i) -> RoomState:
return floor.rooms[_room_index(floor, cell)]
func _has_adjacent_room(map: GridMapState, cell: Vector2i) -> bool:
for direction in [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN]:
if not map.get_room_id(cell + direction).is_empty():
return true
return false
@@ -0,0 +1 @@
uid://cl11r8ohwnjfb