SlimePlatformer/Levels/Maze/maze_corridors.gd
tachyonwolf df89fa4ac7 Maze-book labyrinths, sucking one-way entrances, slime movement model
- 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>
2026-11-16 16:15:23 -06:00

99 lines
3.9 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]
# 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
# 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