99 lines
4.0 KiB
GDScript
99 lines
4.0 KiB
GDScript
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 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")
|
|
|
|
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)
|
|
for edge in graph.edges:
|
|
_carve_l_corridor(map, _room_center(rooms[edge.from].bounds), _room_center(rooms[edge.to].bounds))
|
|
|
|
var floor := FloorStateType.new(map, _seed_value(seed, floor_number), floor_number)
|
|
floor.rooms = rooms
|
|
floor.entrance = _room_center(rooms.front().bounds)
|
|
floor.exit = _room_center(rooms.back().bounds)
|
|
floor.map.set_cell(floor.exit, GridMapStateType.CellKind.STAIRS)
|
|
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) -> void:
|
|
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)
|
|
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)
|
|
|
|
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
|