B2: Generate connected dungeon rooms #19
@@ -44,8 +44,22 @@ static func _place_rooms(rng: DeterministicRng) -> Array[RoomState]:
|
|||||||
placed = true
|
placed = true
|
||||||
break
|
break
|
||||||
if not placed:
|
if not placed:
|
||||||
push_error("FloorGenerator could not place room %d" % room_index)
|
# The fixed 80x50 B2 map always supports this minimum-size fallback.
|
||||||
break
|
# Never return a partial layout after exhausting random placement attempts.
|
||||||
|
return _fallback_rooms()
|
||||||
|
return rooms
|
||||||
|
|
||||||
|
static func _fallback_rooms() -> Array[RoomState]:
|
||||||
|
var rooms: Array[RoomState] = []
|
||||||
|
var positions := [
|
||||||
|
Vector2i(3, 5), Vector2i(20, 5), Vector2i(37, 5), Vector2i(54, 5),
|
||||||
|
Vector2i(3, 20), Vector2i(20, 20), Vector2i(37, 20), Vector2i(54, 20),
|
||||||
|
]
|
||||||
|
for room_index in range(ROOM_COUNT):
|
||||||
|
rooms.append(RoomStateType.new(
|
||||||
|
StringName("room_%02d" % room_index),
|
||||||
|
Rect2i(positions[room_index], ROOM_MIN_SIZE)
|
||||||
|
))
|
||||||
return rooms
|
return rooms
|
||||||
|
|
||||||
static func _random_room_bounds(rng: DeterministicRng) -> Rect2i:
|
static func _random_room_bounds(rng: DeterministicRng) -> Rect2i:
|
||||||
|
|||||||
@@ -4,12 +4,15 @@ const ContentCatalogType = preload("res://scripts/domain/content/content_catalog
|
|||||||
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
|
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
|
||||||
|
|
||||||
const CARDINAL_DIRECTIONS := [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN]
|
const CARDINAL_DIRECTIONS := [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN]
|
||||||
|
const ROOM_PADDING := 2
|
||||||
|
|
||||||
func test_one_hundred_seeds_keep_every_room_center_entrance_and_exit_reachable() -> void:
|
func test_one_hundred_seeds_keep_every_room_center_entrance_and_exit_reachable() -> void:
|
||||||
var catalog := ContentCatalogType.new()
|
var catalog := ContentCatalogType.new()
|
||||||
|
|
||||||
for seed_index in range(100):
|
for seed_index in range(100):
|
||||||
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_padded_rooms(floor.rooms, seed_index)
|
||||||
var reachable := _reachable_cells(floor.map, floor.entrance)
|
var reachable := _reachable_cells(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:
|
||||||
@@ -18,6 +21,14 @@ func test_one_hundred_seeds_keep_every_room_center_entrance_and_exit_reachable()
|
|||||||
for cell in required_cells:
|
for cell in required_cells:
|
||||||
assert_true(reachable.has(cell), "seed %d reaches %s" % [seed_index, cell])
|
assert_true(reachable.has(cell), "seed %d reaches %s" % [seed_index, cell])
|
||||||
|
|
||||||
|
func _assert_padded_rooms(rooms: Array[RoomState], seed_index: int) -> void:
|
||||||
|
for first_index in range(rooms.size()):
|
||||||
|
for second_index in range(first_index + 1, rooms.size()):
|
||||||
|
assert_false(
|
||||||
|
rooms[first_index].bounds.grow(ROOM_PADDING).intersects(rooms[second_index].bounds.grow(ROOM_PADDING)),
|
||||||
|
"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(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]
|
||||||
|
|||||||
Reference in New Issue
Block a user