SlimePlatformer/tests/maze_check.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

121 lines
4.9 KiB
GDScript

extends SceneTree
# Headless batch check for Maze mode: builds many mazes (every size and
# complexity, several seeds) and runs the soft-lock check on each.
# godot --headless --audio-driver Dummy --script res://tests/maze_check.gd
# Also checks the validator catches a soft-lock, by turning one tube of a good
# maze into a one-way trap.
const SEEDS_PER_SETTING = 6
const TILE_SET = preload("res://Assets/level_tiles.tres")
func _initialize() -> void:
var layer := TileMapLayer.new()
layer.tile_set = TILE_SET
root.add_child(layer)
var mazeCount := 0
var firstTryCount := 0
var validCount := 0
var startTime := Time.get_ticks_msec()
for complexity in range(MazeSettings.MIN_COMPLEXITY, MazeSettings.MAX_COMPLEXITY + 1):
for width in range(MazeSettings.MIN_WIDTH, MazeSettings.MAX_WIDTH + 1):
for height in range(MazeSettings.MIN_HEIGHT, MazeSettings.MAX_HEIGHT + 1):
for seedIndex in SEEDS_PER_SETTING:
var firstSeed := 1000 * seedIndex + 37 * complexity + 7 * width + height
var builder := MazeBuilder.new(layer, Vector2i(width, height), complexity, firstSeed)
mazeCount += 1
if builder.isValid:
validCount += 1
if builder.attempts == 1:
firstTryCount += 1
for problem in builder.problems:
print(" c%d %dx%d: %s" % [complexity, width, height, problem])
print("Mazes: %d, valid: %d, valid on the first try: %d (%d ms)"
% [mazeCount, validCount, firstTryCount, Time.get_ticks_msec() - startTime])
var caught := _check_trap_is_caught(layer)
print("Validator catches a one-way trap: %s" % ("yes" if caught else "NO"))
quit(0 if validCount == mazeCount and caught else 1)
# A real soft-lock: in a good maze, take a tube from Start's chamber to some
# chamber X (not End's), and turn every tube with a mouth in X into a one-way
# tube that only leads into X. X is then reachable from Start but has no way
# out. Tries mazes until Start can still reach End some other way, so a plain
# "Start reaches End" check would pass. The validator must reject it as a
# soft-lock.
func _check_trap_is_caught(layer: TileMapLayer) -> bool:
for trapSeed in range(4242, 4282):
var builder := MazeBuilder.new(layer, Vector2i(2, 2), 3, trapSeed)
if not builder.isValid or not _set_trap(layer, builder.carver):
continue
var validator := MazeValidator.new(layer, builder.carver.bounds, builder.carver.startCell, builder.carver.endCell)
if validator.problem.begins_with("Soft-lock"):
print(" trap test (seed %d): %s" % [trapSeed, validator.problem])
return true
if validator.isValid:
return false # Missed a real trap.
return false
# Turns the first chamber a tube from Start leads to (not End's) into a trap.
func _set_trap(layer: TileMapLayer, carver: MazeCarver) -> bool:
var startRect := _rect_with(carver, carver.startCell)
var endRect := _rect_with(carver, carver.endCell)
var trapRect := Rect2i()
for mouth in _mouths_of(layer, startRect):
var tube := _follow_tube(layer, mouth.cell, mouth.outward)
var farRect := _rect_with(carver, tube.cells.back() + tube.farOutward)
if farRect != endRect and farRect != startRect:
trapRect = farRect
break
if trapRect.size == Vector2i.ZERO:
return false
var tiles := MazeTiles.new(layer.tile_set)
for mouth in _mouths_of(layer, trapRect):
var tube := _follow_tube(layer, mouth.cell, mouth.outward)
var cells: Array[Vector2i] = tube.cells
for i in cells.size():
var cell := cells[i]
var isEntrance := i == cells.size() - 1
var entrySide := TubeSteer.side_bit(tube.farOutward) if isEntrance else 0
tiles.set_tile(layer, cell, TubeSteer.get_openings(layer, cell), MazeGraph.ONE_WAY, entrySide)
return true
func _rect_with(carver: MazeCarver, cell: Vector2i) -> Rect2i:
for rect in carver.chamberRects:
if rect.has_point(cell):
return rect
return Rect2i()
# Tube mouths opening into `rect`: [{cell, outward}].
func _mouths_of(layer: TileMapLayer, rect: Rect2i) -> Array[Dictionary]:
var mouths: Array[Dictionary] = []
for cell in layer.get_used_cells():
for outward in TubeSteer.DIRECTIONS:
if TubeSteer.get_openings(layer, cell) & TubeSteer.side_bit(outward) and rect.has_point(cell + outward):
mouths.append({cell = cell, outward = outward})
return mouths
# Walks a maze tube (a simple chain of tiles) from the mouth at `cell` to its
# other end: {cells: from `cell` on, farOutward: which way the far mouth opens}.
func _follow_tube(layer: TileMapLayer, cell: Vector2i, outward: Vector2i) -> Dictionary:
var cells: Array[Vector2i] = [cell]
var back := outward
while true:
var openings := TubeSteer.get_openings(layer, cells.back())
var ahead := Vector2i.ZERO
for direction in TubeSteer.DIRECTIONS:
if direction != back and openings & TubeSteer.side_bit(direction):
ahead = direction
var next: Vector2i = cells.back() + ahead
if layer.get_cell_source_id(next) == -1:
return {cells = cells, farOutward = ahead}
cells.append(next)
back = -ahead
return {}