class_name FloorGenerator extends RefCounted const MAP_SIZE := Vector2i(80, 50) const ROOM_COUNT := 8 const ROOM_MIN_SIZE := Vector2i(5, 4) const ROOM_MAX_SIZE := Vector2i(10, 8) const ROOM_PADDING := 2 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 GridMapStateType = preload("res://scripts/domain/grid_map_state.gd") const FloorStateType = preload("res://scripts/domain/world/floor_state.gd") const RoomStateType = preload("res://scripts/domain/world/room_state.gd") const RoomGraphType = preload("res://scripts/domain/world/room_graph.gd") const FloorPopulatorType = preload("res://scripts/domain/world/floor_populator.gd") static func generate(seed: String, floor_number: int, catalog: ContentCatalog) -> FloorState: var rng := DeterministicRngType.from_seed("%s-floor%d" % [seed, floor_number]) var rooms := _place_rooms(rng) var map := GridMapStateType.new(MAP_SIZE, GridMapStateType.CellKind.WALL) for room in rooms: _carve_room(map, room) var graph := RoomGraphType.new() graph.connect_each_room_to_nearest_prior(rooms) var tree_door_cells: Array[Array] = [] 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) floor.rooms = rooms floor.entrance = _room_center(rooms[entrance_index].bounds) floor.exit = _room_center(rooms[exit_index].bounds) floor.map.set_cell(floor.exit, GridMapStateType.CellKind.STAIRS) floor.metadata["room_graph"] = graph.to_dict() floor.metadata["tree_door_cells"] = tree_door_cells FloorPopulatorType.populate(floor, catalog, seed) return floor static func _place_rooms(rng: DeterministicRng) -> Array[RoomState]: var rooms: Array[RoomState] = [] for room_index in range(ROOM_COUNT): var placed := false for _attempt in range(ROOM_PLACEMENT_ATTEMPTS): var bounds := _random_room_bounds(rng) if _has_required_padding(bounds, rooms): rooms.append(RoomStateType.new(StringName("room_%02d" % room_index), bounds)) placed = true break if not placed: # 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: var width := ROOM_MIN_SIZE.x + rng.next_int() % (ROOM_MAX_SIZE.x - ROOM_MIN_SIZE.x + 1) var height := ROOM_MIN_SIZE.y + rng.next_int() % (ROOM_MAX_SIZE.y - ROOM_MIN_SIZE.y + 1) var x := 1 + rng.next_int() % (MAP_SIZE.x - width - 1) var y := 1 + rng.next_int() % (MAP_SIZE.y - height - 1) return Rect2i(x, y, width, height) static func _has_required_padding(candidate: Rect2i, rooms: Array[RoomState]) -> bool: for room in rooms: if candidate.grow(ROOM_PADDING).intersects(room.bounds.grow(ROOM_PADDING)): return false return true static func _carve_room(map: GridMapState, room: RoomState) -> void: for y in range(room.bounds.position.y, room.bounds.end.y): for x in range(room.bounds.position.x, room.bounds.end.x): var cell := Vector2i(x, y) map.set_cell(cell, GridMapStateType.CellKind.FLOOR) map.set_room_id(cell, room.id) 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): 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): 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: return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2) static func _seed_value(seed: String, floor_number: int) -> int: var value := 0 for index in range(seed.length()): value = (value * 31 + seed.unicode_at(index)) % DeterministicRngType.MODULUS return value + floor_number