From ead07e1a672bcc3a34e91a5f54489265c8c64d7c Mon Sep 17 00:00:00 2001 From: goose Date: Fri, 2 Oct 2026 18:48:19 -0400 Subject: [PATCH] fix: guarantee minimum generated room count --- scripts/domain/world/floor_generator.gd | 18 ++++++++++++++++-- .../test_floor_generator_connectivity.gd | 11 +++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/scripts/domain/world/floor_generator.gd b/scripts/domain/world/floor_generator.gd index 5a80c6e..647e8cc 100644 --- a/scripts/domain/world/floor_generator.gd +++ b/scripts/domain/world/floor_generator.gd @@ -44,8 +44,22 @@ static func _place_rooms(rng: DeterministicRng) -> Array[RoomState]: placed = true break if not placed: - push_error("FloorGenerator could not place room %d" % room_index) - break + # The fixed 80x50 B2 map always supports this minimum-size fallback. + # 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 static func _random_room_bounds(rng: DeterministicRng) -> Rect2i: diff --git a/tests/integration/test_floor_generator_connectivity.gd b/tests/integration/test_floor_generator_connectivity.gd index 135282e..db1c2a4 100644 --- a/tests/integration/test_floor_generator_connectivity.gd +++ b/tests/integration/test_floor_generator_connectivity.gd @@ -4,12 +4,15 @@ const ContentCatalogType = preload("res://scripts/domain/content/content_catalog const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd") 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: var catalog := ContentCatalogType.new() for seed_index in range(100): 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 required_cells: Array[Vector2i] = [floor.entrance, floor.exit] 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: 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]: var reachable: Dictionary[Vector2i, bool] = {} var frontier: Array[Vector2i] = [start]