SlimePlatformer/Levels/Maze/maze_corridors.gd
tachyonwolf 43bf9c6d18 More and longer tube rides, tubes through open air, tube crossings
- 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>
2026-11-16 16:28:17 -06:00

114 lines
4.6 KiB
GDScript

class_name MazeCorridors
extends RefCounted
# Carves a puzzle-book labyrinth into solid rock: a grid of corridor cells
# `width` tiles wide with 1-tile walls between them, joined by a recursive
# backtracker (long winding corridors, branches, dead ends). Then `braid` of the
# dead ends are opened into a neighbour, making loops.
# Every corridor tile ends up connected to every other one.
const STEPS: Array[Vector2i] = [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT]
const HALL_MIN = Vector2i(4, 3)
const HALL_MAX = Vector2i(7, 5)
# Carves into `area` (tiles) and returns the air tiles it made.
static func carve(layer: TileMapLayer, area: Rect2i, width: int, braid: float,
random: RandomNumberGenerator) -> Array[Vector2i]:
var pitch := width + 1
var gridSize := Vector2i((area.size.x + 1) / pitch, (area.size.y + 1) / pitch)
var air: Array[Vector2i] = []
if gridSize.x < 1 or gridSize.y < 1:
return air
# Centred in the area; any spare tiles stay rock.
var used := gridSize * pitch - Vector2i.ONE
var origin := area.position + (area.size - used) / 2
var openings := _backtrack(gridSize, random)
_braid(openings, gridSize, braid, random)
for y in gridSize.y:
for x in gridSize.x:
var cell := Vector2i(x, y)
var corner := origin + cell * pitch
_carve_rect(layer, Rect2i(corner, Vector2i(width, width)), air)
var bits: int = openings[_index(cell, gridSize)]
if bits & TubeSteer.side_bit(Vector2i.RIGHT):
_carve_rect(layer, Rect2i(corner + Vector2i(width, 0), Vector2i(1, width)), air)
if bits & TubeSteer.side_bit(Vector2i.DOWN):
_carve_rect(layer, Rect2i(corner + Vector2i(0, width), Vector2i(width, 1)), air)
return air
# Carves `count` open halls (rooms of 4-7 x 3-5 tiles) into `area`, over the
# corridors, and returns their tiles. They join corridors into loops and give
# tubes open air to run through.
static func carve_halls(layer: TileMapLayer, area: Rect2i, count: int, random: RandomNumberGenerator) -> Array[Vector2i]:
var hall: Array[Vector2i] = []
for i in count:
var size := Vector2i(random.randi_range(HALL_MIN.x, HALL_MAX.x), random.randi_range(HALL_MIN.y, HALL_MAX.y)).min(area.size)
var corner := Vector2i(random.randi_range(area.position.x, area.end.x - size.x),
random.randi_range(area.position.y, area.end.y - size.y))
_carve_rect(layer, Rect2i(corner, size), hall)
return hall
# Per grid cell: which sides are open (TubeSteer.side_bit bits).
static func _backtrack(gridSize: Vector2i, random: RandomNumberGenerator) -> PackedInt32Array:
var openings := PackedInt32Array()
openings.resize(gridSize.x * gridSize.y)
var visited := {}
var first := Vector2i(random.randi_range(0, gridSize.x - 1), random.randi_range(0, gridSize.y - 1))
var stack: Array[Vector2i] = [first]
visited[first] = true
while not stack.is_empty():
var cell: Vector2i = stack.back()
var unvisited: Array[Vector2i] = []
for step in STEPS:
var next := cell + step
if _is_inside(next, gridSize) and not visited.has(next):
unvisited.append(next)
if unvisited.is_empty():
stack.pop_back()
continue
var chosen: Vector2i = unvisited[random.randi_range(0, unvisited.size() - 1)]
_open(openings, gridSize, cell, chosen - cell)
visited[chosen] = true
stack.append(chosen)
return openings
# Opens `share` of the dead ends into a neighbour they aren't joined to yet.
static func _braid(openings: PackedInt32Array, gridSize: Vector2i, share: float, random: RandomNumberGenerator) -> void:
for y in gridSize.y:
for x in gridSize.x:
var cell := Vector2i(x, y)
var bits: int = openings[_index(cell, gridSize)]
var isDeadEnd := bits in [1, 2, 4, 8]
if not isDeadEnd or random.randf() >= share:
continue
var closed: Array[Vector2i] = []
for step in STEPS:
if _is_inside(cell + step, gridSize) and bits & TubeSteer.side_bit(step) == 0:
closed.append(step)
if not closed.is_empty():
_open(openings, gridSize, cell, closed[random.randi_range(0, closed.size() - 1)])
static func _open(openings: PackedInt32Array, gridSize: Vector2i, cell: Vector2i, step: Vector2i) -> void:
openings[_index(cell, gridSize)] |= TubeSteer.side_bit(step)
openings[_index(cell + step, gridSize)] |= TubeSteer.side_bit(-step)
static func _carve_rect(layer: TileMapLayer, rect: Rect2i, air: Array[Vector2i]) -> void:
for y in range(rect.position.y, rect.end.y):
for x in range(rect.position.x, rect.end.x):
layer.erase_cell(Vector2i(x, y))
air.append(Vector2i(x, y))
static func _index(cell: Vector2i, gridSize: Vector2i) -> int:
return cell.y * gridSize.x + cell.x
static func _is_inside(cell: Vector2i, gridSize: Vector2i) -> bool:
return cell.x >= 0 and cell.y >= 0 and cell.x < gridSize.x and cell.y < gridSize.y