- Regions are now dense labyrinths (recursive backtracker on a coarse grid, 1-3 tile corridors, braided loops by complexity); tubes run in the rock between regions, wall pipes give shortcuts inside a region. - One-way entrances suck the slime in (player.pull_into_tube); blow zone is now 1 tile; blown motes are white. TubeBlower -> TubeAirCurrents. - Validator: generous "might get to" areas vs strict SlimeMoves model (surface slides, corners, wraps, 1-tile gaps, ceiling falls, tubes, forced suction/blow tiles as all-outcomes states). - maze_check: new hand-made blow-zone and suction traps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
5 KiB
GDScript
130 lines
5 KiB
GDScript
class_name MazeCarver
|
|
extends RefCounted
|
|
|
|
# Turns a MazeGraph into tiles: rock everywhere, a labyrinth of corridors in
|
|
# each region (MazeCorridors), tubes between regions and through walls
|
|
# (MazeTubeRouter), then Start and End on corridor floors.
|
|
#
|
|
# Each screen (one RoomCamera room) is split into a grid of regions, one per
|
|
# graph region. A region keeps REGION_BORDER tiles of rock round its edge, so
|
|
# neighbouring regions are 4 tiles apart; tubes run there and only they join
|
|
# regions.
|
|
|
|
const REGION_BORDER = 2 # Rock tiles round the inside of each region: room for tubes between regions.
|
|
|
|
var regionRects: Array[Rect2i] = []
|
|
var regionAir: Array[Dictionary] = [] # Per region: its air tiles (tile -> true).
|
|
var bounds := Rect2i() # The whole maze, in tiles.
|
|
var startCell := Vector2i.ZERO # The air tile Start sits on (on a corridor floor).
|
|
var endCell := Vector2i.ZERO
|
|
var isComplete := false # Whether every tube the maze needs got a route.
|
|
var routedTubeCount := 0
|
|
|
|
var _layer: TileMapLayer
|
|
var _tiles: MazeTiles
|
|
var _random: RandomNumberGenerator
|
|
var _router: MazeTubeRouter
|
|
|
|
|
|
func _init(graph: MazeGraph, screens: Vector2i, layer: TileMapLayer, random: RandomNumberGenerator, level: Dictionary) -> void:
|
|
_layer = layer
|
|
_tiles = MazeTiles.new(layer.tile_set)
|
|
_random = random
|
|
var screenTiles := Vector2i(RoomCamera.ROOM_SIZE) / layer.tile_set.tile_size
|
|
bounds = Rect2i(Vector2i.ZERO, screenTiles * screens)
|
|
|
|
_fill_rock()
|
|
_carve_regions(graph, screenTiles, level)
|
|
var interiors: Array[Rect2i] = []
|
|
for rect in regionRects:
|
|
interiors.append(rect.grow(-REGION_BORDER))
|
|
_router = MazeTubeRouter.new(layer, _tiles, random, bounds, level.winding, interiors)
|
|
isComplete = true
|
|
for tube in graph.tubes:
|
|
var fallback := MazeGraph.pick_two_way_kind(random, level.steeredShare)
|
|
if _router.route(regionAir[tube.from], regionAir[tube.to], tube.kind, fallback):
|
|
routedTubeCount += 1
|
|
elif tube.isRequired:
|
|
isComplete = false
|
|
return
|
|
_add_wall_pipes(roundi(level.wallPipesPerScreen * screens.x * screens.y), level.steeredShare)
|
|
startCell = _random_floor(regionAir[graph.start])
|
|
var endFrom := startCell if graph.start == graph.end else _random_floor(regionAir[graph.end])
|
|
endCell = _furthest_floor(regionAir[graph.end], endFrom)
|
|
|
|
|
|
func _fill_rock() -> void:
|
|
_layer.clear()
|
|
for y in range(bounds.position.y, bounds.end.y):
|
|
for x in range(bounds.position.x, bounds.end.x):
|
|
_tiles.set_tile(_layer, Vector2i(x, y), 0, MazeTiles.ROCK)
|
|
|
|
|
|
func _carve_regions(graph: MazeGraph, screenTiles: Vector2i, level: Dictionary) -> void:
|
|
var split: Vector2i = graph.gridSize / (bounds.size / screenTiles)
|
|
for region in graph.region_count():
|
|
var rect := _region(graph.grid_cell(region), split, screenTiles)
|
|
regionRects.append(rect)
|
|
var air := {}
|
|
for tile in MazeCorridors.carve(_layer, rect.grow(-REGION_BORDER), level.corridorWidth, level.braid, _random):
|
|
air[tile] = true
|
|
regionAir.append(air)
|
|
|
|
|
|
# The tiles a region covers. The last region in each screen row or column
|
|
# takes what's left over.
|
|
static func _region(gridCell: Vector2i, split: Vector2i, screenTiles: Vector2i) -> Rect2i:
|
|
var screen := gridCell / split
|
|
var inScreen := gridCell % split
|
|
var regionSize := screenTiles / split
|
|
var position := screen * screenTiles + inScreen * regionSize
|
|
var size := regionSize
|
|
if inScreen.x == split.x - 1:
|
|
size.x = screenTiles.x - inScreen.x * regionSize.x
|
|
if inScreen.y == split.y - 1:
|
|
size.y = screenTiles.y - inScreen.y * regionSize.y
|
|
return Rect2i(position, size)
|
|
|
|
|
|
# Short tubes through single walls, in random regions: shortcuts, and one-way
|
|
# ones where that's safe.
|
|
func _add_wall_pipes(count: int, steeredShare: float) -> void:
|
|
for i in count:
|
|
var air: Dictionary = regionAir[_random.randi_range(0, regionAir.size() - 1)]
|
|
if air.is_empty():
|
|
continue
|
|
var twoWay := MazeGraph.pick_two_way_kind(_random, steeredShare)
|
|
var kind := MazeGraph.ONE_WAY if _random.randf() < 0.4 else twoWay
|
|
_router.add_wall_pipe(air, kind, twoWay)
|
|
|
|
|
|
# Air tiles with solid ground below, not ones the air moves the slime off.
|
|
func _floors(air: Dictionary) -> Array[Vector2i]:
|
|
var floors: Array[Vector2i] = []
|
|
for tile: Vector2i in air:
|
|
if not air.has(tile + Vector2i.DOWN) and not _router.forcedCells.has(tile):
|
|
floors.append(tile)
|
|
floors.sort() # Same seed, same maze.
|
|
return floors
|
|
|
|
|
|
func _random_floor(air: Dictionary) -> Vector2i:
|
|
var floors := _floors(air)
|
|
return floors[_random.randi_range(0, floors.size() - 1)]
|
|
|
|
|
|
# The floor tile of a region furthest along the corridors from `from`.
|
|
func _furthest_floor(air: Dictionary, from: Vector2i) -> Vector2i:
|
|
var distances := {from: 0}
|
|
var queue: Array[Vector2i] = [from]
|
|
var furthest := from
|
|
while not queue.is_empty():
|
|
var tile: Vector2i = queue.pop_front()
|
|
if distances[tile] > distances[furthest] and not air.has(tile + Vector2i.DOWN):
|
|
furthest = tile
|
|
for step in MazeCorridors.STEPS:
|
|
var next := tile + step
|
|
if air.has(next) and not distances.has(next) and not _router.forcedCells.has(next):
|
|
distances[next] = distances[tile] + 1
|
|
queue.append(next)
|
|
return furthest
|