- Halls per region give open air; "open rides" (12+ tiles) run through rock and open air between far-apart spots, often across screens, as long as each region's air stays connected. Tube counts and winding raised. - Same-kind tubes may cross straight across a straight tile (cross tile; new orange one-way cross alternative). Auto/one-way go straight, manual/goo join networks (checked live). - Fix: sideways blower landing must be 1 tile tall; one-way tubes with both ends in one region are checked together. - maze_check: hand-made junction map passes as manual, fails as auto. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
175 lines
7.1 KiB
GDScript
175 lines
7.1 KiB
GDScript
class_name MazeCarver
|
|
extends RefCounted
|
|
|
|
# Turns a MazeGraph into tiles: rock everywhere, a labyrinth of corridors and
|
|
# a few open halls in each region (MazeCorridors), tubes (MazeTubeRouter):
|
|
# between regions, long open rides, wall pipes. Then Start and End on 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.
|
|
const MIN_OPEN_RIDE = 12 # Tiles an open tube is long, at least.
|
|
const OPEN_RIDE_TRIES = 3 # Per open tube: pairs of places to try.
|
|
const FAR_SAMPLES = 8 # To pick a far-off end for an open tube, the furthest of this many random tiles.
|
|
|
|
var regionRects: Array[Rect2i] = []
|
|
var regionAir: Array[Dictionary] = [] # Per region: its air tiles (tile -> true).
|
|
var openAir := {} # Air tiles tubes may run through: halls, and corridors 3 or more wide.
|
|
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, regionAir, openAir)
|
|
isComplete = true
|
|
for tube in graph.tubes:
|
|
var fallback := MazeGraph.pick_two_way_kind(random, level.steeredShare)
|
|
var fromAir := regionAir[tube.from]
|
|
var toAir := regionAir[tube.to]
|
|
if _router.route(fromAir, MazeTubeRouter.middle(toAir), toAir, MazeTubeRouter.middle(fromAir), tube.kind, fallback):
|
|
routedTubeCount += 1
|
|
elif tube.isRequired:
|
|
isComplete = false
|
|
return
|
|
var screenCount: int = screens.x * screens.y
|
|
_add_open_tubes(graph, roundi(level.openTubesPerScreen * screenCount), level.steeredShare)
|
|
_add_wall_pipes(roundi(level.wallPipesPerScreen * screenCount), 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 area := rect.grow(-REGION_BORDER)
|
|
var air := {}
|
|
for tile in MazeCorridors.carve(_layer, area, level.corridorWidth, level.braid, _random):
|
|
air[tile] = true
|
|
if level.corridorWidth >= 3:
|
|
openAir[tile] = true
|
|
for tile in MazeCorridors.carve_halls(_layer, area, level.hallsPerRegion, _random):
|
|
air[tile] = true
|
|
openAir[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)
|
|
|
|
|
|
# Long rides: from somewhere in a region to somewhere far off in it or in a
|
|
# neighbouring region (maybe on another screen), through rock and open air.
|
|
func _add_open_tubes(graph: MazeGraph, count: int, steeredShare: float) -> void:
|
|
for i in count:
|
|
var twoWay := MazeGraph.pick_two_way_kind(_random, steeredShare)
|
|
var kind := MazeGraph.ONE_WAY if _random.randf() < 0.35 else twoWay
|
|
for attempt in OPEN_RIDE_TRIES:
|
|
var from := _random.randi_range(0, graph.region_count() - 1)
|
|
var to := graph.region_at((graph.grid_cell(from) + Vector2i(_random.randi_range(-1, 1), _random.randi_range(-1, 1))).clamp(Vector2i.ZERO, graph.gridSize - Vector2i.ONE))
|
|
if regionAir[from].is_empty() or regionAir[to].is_empty():
|
|
continue
|
|
var start := _random_tile(regionAir[from])
|
|
var end := _far_tile(regionAir[to], start)
|
|
if _router.route(regionAir[from], Vector2(start), regionAir[to], Vector2(end), kind, twoWay, MIN_OPEN_RIDE):
|
|
break
|
|
|
|
|
|
func _random_tile(air: Dictionary) -> Vector2i:
|
|
var tiles: Array = air.keys()
|
|
return tiles[_random.randi_range(0, tiles.size() - 1)]
|
|
|
|
|
|
func _far_tile(air: Dictionary, from: Vector2i) -> Vector2i:
|
|
var best := _random_tile(air)
|
|
for i in FAR_SAMPLES:
|
|
var tile := _random_tile(air)
|
|
if Vector2(tile).distance_squared_to(Vector2(from)) > Vector2(best).distance_squared_to(Vector2(from)):
|
|
best = tile
|
|
return best
|
|
|
|
|
|
# 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
|