Merge pull request 'B3: Add procedural doors and room roles' (#20) from slice/b3-doors-room-roles into main
This commit was merged in pull request #20.
This commit is contained in:
@@ -7,6 +7,23 @@ const ROOM_MIN_SIZE := Vector2i(5, 4)
|
|||||||
const ROOM_MAX_SIZE := Vector2i(10, 8)
|
const ROOM_MAX_SIZE := Vector2i(10, 8)
|
||||||
const ROOM_PADDING := 2
|
const ROOM_PADDING := 2
|
||||||
const ROOM_PLACEMENT_ATTEMPTS := 256
|
const ROOM_PLACEMENT_ATTEMPTS := 256
|
||||||
|
const SHOP_ACTIVE_FROM_FLOOR := 3
|
||||||
|
const BOSS_FLOOR_INTERVAL := 5
|
||||||
|
const GUILD_ROOM_CHANCE := 0.35
|
||||||
|
const ROOM_KIND_WEIGHTS := {
|
||||||
|
&"combat": 26,
|
||||||
|
&"treasure": 14,
|
||||||
|
&"trap": 10,
|
||||||
|
&"sanctuary": 6,
|
||||||
|
&"shop": 7,
|
||||||
|
&"mystery": 10,
|
||||||
|
&"puzzle": 6,
|
||||||
|
&"arena": 7,
|
||||||
|
&"narrative": 9,
|
||||||
|
&"refuge": 5,
|
||||||
|
&"guild": 4,
|
||||||
|
}
|
||||||
|
const UNIQUE_ROOM_KINDS := [&"shop", &"refuge", &"sanctuary", &"guild"]
|
||||||
|
|
||||||
const DeterministicRngType = preload("res://scripts/domain/run/deterministic_rng.gd")
|
const DeterministicRngType = preload("res://scripts/domain/run/deterministic_rng.gd")
|
||||||
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
||||||
@@ -23,14 +40,29 @@ static func generate(seed: String, floor_number: int, _catalog: ContentCatalog)
|
|||||||
|
|
||||||
var graph := RoomGraphType.new()
|
var graph := RoomGraphType.new()
|
||||||
graph.connect_each_room_to_nearest_prior(rooms)
|
graph.connect_each_room_to_nearest_prior(rooms)
|
||||||
for edge in graph.edges:
|
var tree_door_cells: Array[Array] = []
|
||||||
_carve_l_corridor(map, _room_center(rooms[edge.from].bounds), _room_center(rooms[edge.to].bounds))
|
for edge in graph.tree_edges:
|
||||||
|
var path := _carve_l_corridor(map, _room_center(rooms[edge.from].bounds), _room_center(rooms[edge.to].bounds))
|
||||||
|
var edge_doors := _place_tree_boundary_doors(map, path, rooms[edge.from], rooms[edge.to], tree_door_cells)
|
||||||
|
tree_door_cells.append_array(edge_doors)
|
||||||
|
|
||||||
|
graph.add_local_loops(rooms, rng)
|
||||||
|
for edge in graph.loop_edges:
|
||||||
|
_carve_l_corridor_preserving_doors(map, _room_center(rooms[edge.from].bounds), _room_center(rooms[edge.to].bounds))
|
||||||
|
for serialized_door in tree_door_cells:
|
||||||
|
map.set_cell(Vector2i(int(serialized_door[0]), int(serialized_door[1])), GridMapStateType.CellKind.CLOSED_DOOR)
|
||||||
|
|
||||||
|
var entrance_index := 0
|
||||||
|
var exit_index := graph.farthest_tree_room(entrance_index)
|
||||||
|
_assign_room_kinds(rng, rooms, entrance_index, exit_index, floor_number)
|
||||||
|
|
||||||
var floor := FloorStateType.new(map, _seed_value(seed, floor_number), floor_number)
|
var floor := FloorStateType.new(map, _seed_value(seed, floor_number), floor_number)
|
||||||
floor.rooms = rooms
|
floor.rooms = rooms
|
||||||
floor.entrance = _room_center(rooms.front().bounds)
|
floor.entrance = _room_center(rooms[entrance_index].bounds)
|
||||||
floor.exit = _room_center(rooms.back().bounds)
|
floor.exit = _room_center(rooms[exit_index].bounds)
|
||||||
floor.map.set_cell(floor.exit, GridMapStateType.CellKind.STAIRS)
|
floor.map.set_cell(floor.exit, GridMapStateType.CellKind.STAIRS)
|
||||||
|
floor.metadata["room_graph"] = graph.to_dict()
|
||||||
|
floor.metadata["tree_door_cells"] = tree_door_cells
|
||||||
return floor
|
return floor
|
||||||
|
|
||||||
static func _place_rooms(rng: DeterministicRng) -> Array[RoomState]:
|
static func _place_rooms(rng: DeterministicRng) -> Array[RoomState]:
|
||||||
@@ -82,11 +114,90 @@ static func _carve_room(map: GridMapState, room: RoomState) -> void:
|
|||||||
map.set_cell(cell, GridMapStateType.CellKind.FLOOR)
|
map.set_cell(cell, GridMapStateType.CellKind.FLOOR)
|
||||||
map.set_room_id(cell, room.id)
|
map.set_room_id(cell, room.id)
|
||||||
|
|
||||||
static func _carve_l_corridor(map: GridMapState, from: Vector2i, to: Vector2i) -> void:
|
static func _carve_l_corridor(map: GridMapState, from: Vector2i, to: Vector2i) -> Array[Vector2i]:
|
||||||
|
return _carve_l_corridor_with_door_policy(map, from, to, false)
|
||||||
|
|
||||||
|
static func _carve_l_corridor_preserving_doors(map: GridMapState, from: Vector2i, to: Vector2i) -> void:
|
||||||
|
_carve_l_corridor_with_door_policy(map, from, to, true)
|
||||||
|
|
||||||
|
static func _carve_l_corridor_with_door_policy(map: GridMapState, from: Vector2i, to: Vector2i, preserve_doors: bool) -> Array[Vector2i]:
|
||||||
|
var path: Array[Vector2i] = []
|
||||||
for x in range(mini(from.x, to.x), maxi(from.x, to.x) + 1):
|
for x in range(mini(from.x, to.x), maxi(from.x, to.x) + 1):
|
||||||
map.set_cell(Vector2i(x, from.y), GridMapStateType.CellKind.FLOOR)
|
var cell := Vector2i(x, from.y)
|
||||||
|
if not preserve_doors or map.get_cell(cell) != GridMapStateType.CellKind.CLOSED_DOOR or not map.get_room_id(cell).is_empty():
|
||||||
|
map.set_cell(cell, GridMapStateType.CellKind.FLOOR)
|
||||||
|
path.append(cell)
|
||||||
for y in range(mini(from.y, to.y), maxi(from.y, to.y) + 1):
|
for y in range(mini(from.y, to.y), maxi(from.y, to.y) + 1):
|
||||||
map.set_cell(Vector2i(to.x, y), GridMapStateType.CellKind.FLOOR)
|
var cell := Vector2i(to.x, y)
|
||||||
|
if not preserve_doors or map.get_cell(cell) != GridMapStateType.CellKind.CLOSED_DOOR or not map.get_room_id(cell).is_empty():
|
||||||
|
map.set_cell(cell, GridMapStateType.CellKind.FLOOR)
|
||||||
|
if not path.has(cell):
|
||||||
|
path.append(cell)
|
||||||
|
return path
|
||||||
|
|
||||||
|
static func _place_tree_boundary_doors(map: GridMapState, path: Array[Vector2i], first_room: RoomState, second_room: RoomState, existing_doors: Array[Array]) -> Array[Array]:
|
||||||
|
var serialized_cells: Array[Array] = []
|
||||||
|
var placed_for: Dictionary[StringName, bool] = {}
|
||||||
|
for cell in path:
|
||||||
|
if not map.get_room_id(cell).is_empty() or map.get_cell(cell) == GridMapStateType.CellKind.CLOSED_DOOR:
|
||||||
|
continue
|
||||||
|
for direction in [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN]:
|
||||||
|
var adjacent_room_id := map.get_room_id(cell + direction)
|
||||||
|
if adjacent_room_id in [first_room.id, second_room.id] and not placed_for.has(adjacent_room_id) and not _is_adjacent_to_serialized_door(cell, existing_doors):
|
||||||
|
map.set_cell(cell, GridMapStateType.CellKind.CLOSED_DOOR)
|
||||||
|
placed_for[adjacent_room_id] = true
|
||||||
|
serialized_cells.append([cell.x, cell.y])
|
||||||
|
break
|
||||||
|
return serialized_cells
|
||||||
|
|
||||||
|
static func _is_adjacent_to_serialized_door(cell: Vector2i, serialized_doors: Array[Array]) -> bool:
|
||||||
|
for serialized_door in serialized_doors:
|
||||||
|
var door_cell := Vector2i(int(serialized_door[0]), int(serialized_door[1]))
|
||||||
|
if abs(cell.x - door_cell.x) + abs(cell.y - door_cell.y) <= 1:
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
|
||||||
|
static func _assign_room_kinds(rng: DeterministicRng, rooms: Array[RoomState], entrance_index: int, exit_index: int, floor_number: int) -> void:
|
||||||
|
var used_unique: Dictionary[StringName, bool] = {}
|
||||||
|
rooms[entrance_index].kind = &"entrance"
|
||||||
|
rooms[exit_index].kind = &"boss" if floor_number % BOSS_FLOOR_INTERVAL == 0 else &"combat"
|
||||||
|
var candidates: Array[int] = []
|
||||||
|
for room_index in range(rooms.size()):
|
||||||
|
if room_index != entrance_index and room_index != exit_index:
|
||||||
|
candidates.append(room_index)
|
||||||
|
|
||||||
|
# ASCII's early tutorial guild is an explicit gated chance rather than a
|
||||||
|
# second independent weighted roll. On later floors guild stays in the pool.
|
||||||
|
if floor_number <= 3 and not candidates.is_empty() and rng.next_float() < GUILD_ROOM_CHANCE:
|
||||||
|
var guild_candidate := candidates[rng.next_int() % candidates.size()]
|
||||||
|
rooms[guild_candidate].kind = &"guild"
|
||||||
|
used_unique[&"guild"] = true
|
||||||
|
candidates.erase(guild_candidate)
|
||||||
|
|
||||||
|
for room_index in candidates:
|
||||||
|
var kind := _weighted_room_kind(rng, floor_number, used_unique)
|
||||||
|
rooms[room_index].kind = kind
|
||||||
|
if UNIQUE_ROOM_KINDS.has(kind):
|
||||||
|
used_unique[kind] = true
|
||||||
|
|
||||||
|
static func _weighted_room_kind(rng: DeterministicRng, floor_number: int, used_unique: Dictionary[StringName, bool]) -> StringName:
|
||||||
|
var available: Array[StringName] = []
|
||||||
|
var total_weight := 0
|
||||||
|
for kind: StringName in ROOM_KIND_WEIGHTS:
|
||||||
|
if kind == &"shop" and floor_number < SHOP_ACTIVE_FROM_FLOOR:
|
||||||
|
continue
|
||||||
|
if kind == &"guild" and floor_number <= 3:
|
||||||
|
continue
|
||||||
|
if UNIQUE_ROOM_KINDS.has(kind) and used_unique.has(kind):
|
||||||
|
continue
|
||||||
|
available.append(kind)
|
||||||
|
total_weight += int(ROOM_KIND_WEIGHTS[kind])
|
||||||
|
var roll := rng.next_int() % total_weight
|
||||||
|
for kind in available:
|
||||||
|
roll -= int(ROOM_KIND_WEIGHTS[kind])
|
||||||
|
if roll < 0:
|
||||||
|
return kind
|
||||||
|
return &"combat"
|
||||||
|
|
||||||
static func _room_center(bounds: Rect2i) -> Vector2i:
|
static func _room_center(bounds: Rect2i) -> Vector2i:
|
||||||
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)
|
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)
|
||||||
|
|||||||
@@ -1,24 +1,130 @@
|
|||||||
class_name RoomGraph
|
class_name RoomGraph
|
||||||
extends RefCounted
|
extends RefCounted
|
||||||
|
|
||||||
# A room graph is intentionally limited to the generator's tree topology. Doors,
|
# Tree edges are the only topology used to guarantee a route from the entrance
|
||||||
# loops, and route semantics are added by later world-generation slices.
|
# to the exit. Loop edges are deliberately kept separate so future special
|
||||||
|
# gates cannot accidentally become part of that guarantee.
|
||||||
|
var tree_edges: Array[Dictionary] = []
|
||||||
|
var loop_edges: Array[Dictionary] = []
|
||||||
var edges: Array[Dictionary] = []
|
var edges: Array[Dictionary] = []
|
||||||
|
|
||||||
func connect_each_room_to_nearest_prior(rooms: Array[RoomState]) -> void:
|
func connect_each_room_to_nearest_prior(rooms: Array[RoomState]) -> void:
|
||||||
edges.clear()
|
tree_edges.clear()
|
||||||
|
loop_edges.clear()
|
||||||
for room_index in range(1, rooms.size()):
|
for room_index in range(1, rooms.size()):
|
||||||
var nearest_index := 0
|
var nearest_index := 0
|
||||||
var nearest_distance := _manhattan_distance(_room_center(rooms[room_index].bounds), _room_center(rooms[0].bounds))
|
var nearest_distance := _squared_distance(_room_center(rooms[room_index].bounds), _room_center(rooms[0].bounds))
|
||||||
for prior_index in range(1, room_index):
|
for prior_index in range(1, room_index):
|
||||||
var distance := _manhattan_distance(_room_center(rooms[room_index].bounds), _room_center(rooms[prior_index].bounds))
|
var distance := _squared_distance(_room_center(rooms[room_index].bounds), _room_center(rooms[prior_index].bounds))
|
||||||
if distance < nearest_distance:
|
if distance < nearest_distance:
|
||||||
nearest_index = prior_index
|
nearest_index = prior_index
|
||||||
nearest_distance = distance
|
nearest_distance = distance
|
||||||
edges.append({"from": nearest_index, "to": room_index})
|
tree_edges.append(_edge(nearest_index, room_index, false))
|
||||||
|
_refresh_edges()
|
||||||
|
|
||||||
|
func add_local_loops(rooms: Array[RoomState], rng: DeterministicRng) -> void:
|
||||||
|
var requested_count := rooms.size() / 4
|
||||||
|
var attempts := 0
|
||||||
|
while loop_edges.size() < requested_count and attempts < rooms.size() * 2:
|
||||||
|
attempts += 1
|
||||||
|
var from_index := rng.next_int() % rooms.size()
|
||||||
|
var candidates := _nearby_unconnected_indices(rooms, from_index)
|
||||||
|
if candidates.is_empty():
|
||||||
|
continue
|
||||||
|
var to_index: int = candidates[rng.next_int() % candidates.size()]
|
||||||
|
loop_edges.append(_edge(from_index, to_index, true))
|
||||||
|
_refresh_edges()
|
||||||
|
|
||||||
|
func tree_distances(start_index: int) -> Dictionary[int, int]:
|
||||||
|
var adjacency: Dictionary[int, Array] = {}
|
||||||
|
for edge in tree_edges:
|
||||||
|
var from_index := int(edge["from"])
|
||||||
|
var to_index := int(edge["to"])
|
||||||
|
if not adjacency.has(from_index):
|
||||||
|
adjacency[from_index] = []
|
||||||
|
if not adjacency.has(to_index):
|
||||||
|
adjacency[to_index] = []
|
||||||
|
adjacency[from_index].append(to_index)
|
||||||
|
adjacency[to_index].append(from_index)
|
||||||
|
var distances: Dictionary[int, int] = {start_index: 0}
|
||||||
|
var frontier: Array[int] = [start_index]
|
||||||
|
while not frontier.is_empty():
|
||||||
|
var current := frontier.pop_front()
|
||||||
|
for neighbor in adjacency.get(current, []):
|
||||||
|
if not distances.has(neighbor):
|
||||||
|
distances[neighbor] = distances[current] + 1
|
||||||
|
frontier.append(neighbor)
|
||||||
|
return distances
|
||||||
|
|
||||||
|
func farthest_tree_room(start_index: int) -> int:
|
||||||
|
var distances := tree_distances(start_index)
|
||||||
|
var farthest_index := start_index
|
||||||
|
var farthest_distance := -1
|
||||||
|
for room_index in range(distances.size()):
|
||||||
|
var distance := distances.get(room_index, -1)
|
||||||
|
if distance > farthest_distance:
|
||||||
|
farthest_index = room_index
|
||||||
|
farthest_distance = distance
|
||||||
|
return farthest_index
|
||||||
|
|
||||||
|
func to_dict() -> Dictionary:
|
||||||
|
return {
|
||||||
|
"tree_edges": tree_edges.duplicate(true),
|
||||||
|
"loop_edges": loop_edges.duplicate(true),
|
||||||
|
}
|
||||||
|
|
||||||
|
static func from_dict(data: Dictionary) -> RoomGraph:
|
||||||
|
var graph := RoomGraph.new()
|
||||||
|
for edge_data in data.get("tree_edges", []):
|
||||||
|
graph.tree_edges.append(edge_data.duplicate(true))
|
||||||
|
for edge_data in data.get("loop_edges", []):
|
||||||
|
graph.loop_edges.append(edge_data.duplicate(true))
|
||||||
|
graph._refresh_edges()
|
||||||
|
return graph
|
||||||
|
|
||||||
|
func _nearby_unconnected_indices(rooms: Array[RoomState], from_index: int) -> Array[int]:
|
||||||
|
var ranked: Array[Dictionary] = []
|
||||||
|
var from_center := _room_center(rooms[from_index].bounds)
|
||||||
|
for candidate_index in range(rooms.size()):
|
||||||
|
if candidate_index == from_index:
|
||||||
|
continue
|
||||||
|
ranked.append({
|
||||||
|
"index": candidate_index,
|
||||||
|
"distance": _squared_distance(from_center, _room_center(rooms[candidate_index].bounds)),
|
||||||
|
})
|
||||||
|
ranked.sort_custom(func(first: Dictionary, second: Dictionary) -> bool:
|
||||||
|
if int(first["distance"]) == int(second["distance"]):
|
||||||
|
return int(first["index"]) < int(second["index"])
|
||||||
|
return int(first["distance"]) < int(second["distance"])
|
||||||
|
)
|
||||||
|
var nearby: Array[int] = []
|
||||||
|
for candidate in ranked.slice(0, mini(6, ranked.size())):
|
||||||
|
var candidate_index := int(candidate["index"])
|
||||||
|
if not _has_edge_between(from_index, candidate_index):
|
||||||
|
nearby.append(candidate_index)
|
||||||
|
return nearby
|
||||||
|
|
||||||
|
func _has_edge_between(first_index: int, second_index: int) -> bool:
|
||||||
|
for edge in edges:
|
||||||
|
if (int(edge["from"]) == first_index and int(edge["to"]) == second_index) \
|
||||||
|
or (int(edge["from"]) == second_index and int(edge["to"]) == first_index):
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
|
||||||
|
func _refresh_edges() -> void:
|
||||||
|
edges = tree_edges.duplicate(true)
|
||||||
|
edges.append_array(loop_edges.duplicate(true))
|
||||||
|
|
||||||
|
func _edge(from_index: int, to_index: int, future_special: bool) -> Dictionary:
|
||||||
|
return {
|
||||||
|
"from": from_index,
|
||||||
|
"to": to_index,
|
||||||
|
"future_special": future_special,
|
||||||
|
}
|
||||||
|
|
||||||
func _room_center(bounds: Rect2i) -> Vector2i:
|
func _room_center(bounds: Rect2i) -> Vector2i:
|
||||||
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)
|
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)
|
||||||
|
|
||||||
func _manhattan_distance(from: Vector2i, to: Vector2i) -> int:
|
func _squared_distance(from: Vector2i, to: Vector2i) -> int:
|
||||||
return abs(from.x - to.x) + abs(from.y - to.y)
|
var delta := to - from
|
||||||
|
return delta.x * delta.x + delta.y * delta.y
|
||||||
|
|||||||
@@ -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)
|
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_gte(floor.rooms.size(), 8, "seed %d creates at least eight padded rooms" % seed_index)
|
||||||
_assert_padded_rooms(floor.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]
|
var required_cells: Array[Vector2i] = [floor.entrance, floor.exit]
|
||||||
for room in floor.rooms:
|
for room in floor.rooms:
|
||||||
required_cells.append(_room_center(room.bounds))
|
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]
|
"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 reachable: Dictionary[Vector2i, bool] = {}
|
||||||
var frontier: Array[Vector2i] = [start]
|
var frontier: Array[Vector2i] = [start]
|
||||||
reachable[start] = true
|
reachable[start] = true
|
||||||
@@ -37,7 +37,7 @@ func _reachable_cells(map: GridMapState, start: Vector2i) -> Dictionary[Vector2i
|
|||||||
var current := frontier.pop_front()
|
var current := frontier.pop_front()
|
||||||
for direction in CARDINAL_DIRECTIONS:
|
for direction in CARDINAL_DIRECTIONS:
|
||||||
var next_cell: Vector2i = current + direction
|
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
|
reachable[next_cell] = true
|
||||||
frontier.append(next_cell)
|
frontier.append(next_cell)
|
||||||
return reachable
|
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
|
||||||
@@ -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
|
||||||
Reference in New Issue
Block a user