SlimePlatformer/Levels/Maze/maze_validator.gd
tachyonwolf e7ae22b8cd Add Maze mode with one-way tubes and a soft-lock check
- Main menu "Maze" button opens a setup panel (width, height in screens;
  complexity 1-5) and generates a maze of rock chambers joined by tubes,
  with Start and End pads and a win panel.
- New one-way tube kind: orange tiles that only let the slime in at their
  entrance, with motes drifting into it (OneWayTube, TubeVacuum). The
  auto-tube trace moved to PixelStream.trace_tube so the player and the
  maze validator share it.
- Generation: chamber graph (spanning tree rooted at End, one-way tubes
  only towards End on the route, extra tubes), carved with AStarGrid2D.
  MazeValidator re-derives the chamber graph from the tiles and checks
  that End is reachable from every chamber reachable from Start.
- tests/maze_check.gd: headless batch check (360 mazes) plus a known
  soft-lock the validator must reject. See docs/MAZE.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:45:46 -06:00

171 lines
6 KiB
GDScript

class_name MazeValidator
extends RefCounted
# Checks a finished maze for soft-locks, from its tiles alone (it doesn't trust
# MazeGraph or MazeCarver). See docs/MAZE.md.
#
# The slime crawls on floors, walls and ceilings and round corners, so inside a
# plain rectangle of air it can get to every tile side, and so to every tube
# mouth. That makes each chamber (a connected patch of air) one node; tubes
# are the only ways between them:
# - Ordinary and one-way tubes: in at any mouth the player could go in by
# (OneWayTube.can_enter), traced to their other end with the same
# PixelStream.trace_tube the player uses.
# - Manual and goo tubes: in at any mouth, out at any mouth of the same network.
# A mouth only leads somewhere if there's air in front of it.
#
# The maze passes if End can be reached from Start, and End can still be
# reached from every chamber that can be reached from Start.
var isValid := false
var problem := "" # Why it failed.
var chamberCount := 0
var reachableCount := 0 # Chambers reachable from Start.
var mouthCount := 0
var _layer: TileMapLayer
var _bounds: Rect2i
var _chamberAt := {} # Air tile -> chamber index.
var _links: Array[Dictionary] = [] # Per chamber: chamber -> true for each one a tube leads to.
func _init(layer: TileMapLayer, bounds: Rect2i, startCell: Vector2i, endCell: Vector2i) -> void:
_layer = layer
_bounds = bounds
if not _find_chambers():
return
if not _chamberAt.has(startCell) or not _chamberAt.has(endCell):
problem = "Start or End is not in the air"
return
_find_links()
var reachable := _search(_chamberAt[startCell], false)
var canFinish := _search(_chamberAt[endCell], true)
reachableCount = reachable.size()
if not reachable.has(_chamberAt[endCell]):
problem = "End can't be reached from Start"
return
for chamber: int in reachable:
if not canFinish.has(chamber):
problem = "Soft-lock: chamber %d is reachable but End isn't reachable from it" % chamber
return
isValid = true
# Groups the air into chambers. False if air leaks out of the maze or a
# chamber isn't a plain rectangle (the movement model above needs that).
func _find_chambers() -> bool:
for y in range(_bounds.position.y, _bounds.end.y):
for x in range(_bounds.position.x, _bounds.end.x):
var cell := Vector2i(x, y)
if _is_air(cell) and not _chamberAt.has(cell):
if not _flood_chamber(cell, chamberCount):
return false
chamberCount += 1
_links.append({})
return true
func _flood_chamber(first: Vector2i, chamber: int) -> bool:
var cells: Array[Vector2i] = [first]
_chamberAt[first] = chamber
var low := first
var high := first
var index := 0
while index < cells.size():
var cell := cells[index]
index += 1
low = low.min(cell)
high = high.max(cell)
for direction in TubeSteer.DIRECTIONS:
var next := cell + direction
if not _is_air(next) or _chamberAt.has(next):
continue
if not _bounds.has_point(next):
problem = "Air leaks out of the maze at %s" % next
return false
_chamberAt[next] = chamber
cells.append(next)
var size := high - low + Vector2i.ONE
if size.x * size.y != cells.size():
problem = "Chamber %d is not a rectangle" % chamber
return false
return true
func _is_air(cell: Vector2i) -> bool:
return _layer.get_cell_source_id(cell) == -1
# Every tube mouth (a tube tile side open onto air) and where going in there
# can take the slime.
func _find_links() -> void:
for cell: Vector2i in _layer.get_used_cells():
var openings := TubeSteer.get_openings(_layer, cell)
if openings == 0:
continue
for outward in TubeSteer.DIRECTIONS:
if openings & TubeSteer.side_bit(outward) == 0 or not _chamberAt.has(cell + outward):
continue
mouthCount += 1
var from: int = _chamberAt[cell + outward]
for to in _exits(cell, -outward):
_links[from][to] = true
# The chambers the slime can come out in after going into `cell` heading `direction`.
func _exits(cell: Vector2i, direction: Vector2i) -> Array[int]:
var exits: Array[int] = []
if TubeSteer.is_manual(_layer, cell) or TubeGoo.is_goo(_layer, cell):
for mouth in _network_mouths(cell):
exits.append(_chamberAt[mouth])
return exits
if not OneWayTube.can_enter(_layer, cell, direction):
return exits
var path := PixelStream.trace_tube(_layer, cell, direction)
if path.size() < 2:
return exits
var exitMouth := path[path.size() - 1]
var outward := (exitMouth - path[path.size() - 2]).normalized()
var halfTile := Vector2(_layer.tile_set.tile_size) / 2.0
var exitCell := _layer.local_to_map(_layer.to_local(exitMouth + outward * halfTile))
if _chamberAt.has(exitCell):
exits.append(_chamberAt[exitCell])
return exits
# The air tiles in front of every mouth of the steered tube network `cell` is
# in (joined the way TubeSteer and TubeGoo join tiles: same kind, both open).
func _network_mouths(first: Vector2i) -> Array[Vector2i]:
var isGoo := TubeGoo.is_goo(_layer, first)
var seen := {first: true}
var toVisit: Array[Vector2i] = [first]
var mouths: Array[Vector2i] = []
while not toVisit.is_empty():
var cell: Vector2i = toVisit.pop_back()
var openings := TubeSteer.get_openings(_layer, cell)
for direction in TubeSteer.DIRECTIONS:
if openings & TubeSteer.side_bit(direction) == 0:
continue
var next := cell + direction
if _chamberAt.has(next):
mouths.append(next)
continue
var isSameKind := TubeGoo.is_goo(_layer, next) if isGoo else TubeSteer.is_manual(_layer, next)
if isSameKind and TubeSteer.get_openings(_layer, next) & TubeSteer.side_bit(-direction) and not seen.has(next):
seen[next] = true
toVisit.append(next)
return mouths
# Chambers reachable from `from` (or, if `isBackward`, that can reach it).
func _search(from: int, isBackward: bool) -> Dictionary:
var found := {from: true}
var queue: Array[int] = [from]
while not queue.is_empty():
var chamber: int = queue.pop_front()
for other in chamberCount:
var isLinked: bool = _links[other].has(chamber) if isBackward else _links[chamber].has(other)
if isLinked and not found.has(other):
found[other] = true
queue.append(other)
return found