SlimePlatformer/Levels/Maze/maze_validator.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

113 lines
4 KiB
GDScript

class_name MazeValidator
extends RefCounted
# Checks a finished maze for soft-locks, from its tiles alone (it doesn't trust
# MazeGraph, MazeCarver or MazeTubeRouter). See docs/MAZE.md.
#
# Two models, one on each side of the check, so it can't be fooled:
# - Where the slime might get to (generous): any air tile joined to Start's by
# air, or by a tube it could go into. It may not really get to all of them,
# but it can't get anywhere else.
# - Where the slime can surely get to End from (strict): SlimeMoves, the moves
# it's sure to manage, with forced tiles (suction and blow zones).
# The maze passes if every state at every tile it might get to can surely
# reach End. That's the no-soft-lock rule; Start reaching End is part of it.
var isValid := false
var problem := "" # Why it failed.
var areaCount := 0 # Patches of air joined by air.
var reachableCount := 0 # Areas the slime might get to from Start.
var mouthCount := 0
var _layer: TileMapLayer
var _bounds: Rect2i
var _moves: SlimeMoves
var _areaAt := {} # Air tile -> area index.
var _areaTiles: Array[Array] = [] # Per area: its air tiles.
var _links: Array[Dictionary] = [] # Per area: area -> true for each one a tube leads to.
func _init(layer: TileMapLayer, bounds: Rect2i, startCell: Vector2i, endCell: Vector2i) -> void:
_layer = layer
_bounds = bounds
_moves = SlimeMoves.new(layer, bounds)
if not _moves.problem.is_empty():
problem = _moves.problem
return
if not _find_areas():
return
if not _moves.is_air(startCell) or not _moves.is_air(endCell):
problem = "Start or End is not in the air"
return
if _moves.is_forced(startCell) or _moves.is_forced(endCell):
problem = "Start or End is on a tile the air moves the slime off"
return
_find_links()
var finished := _moves.can_finish(endCell)
if not finished.has(Vector3i(startCell.x, startCell.y, SlimeMoves.SIDES.find(Vector2i.DOWN))):
problem = "End can't be reached from Start"
return
var reachable := _reachable_areas(_areaAt[startCell])
reachableCount = reachable.size()
for area: int in reachable:
for tile: Vector2i in _areaTiles[area]:
for state in _moves.states_at(tile):
if not finished.has(state):
problem = "Soft-lock: the slime can be at %s (side %s) but can't get to End from there" % [
tile, SlimeMoves.SIDES[state.z]]
return
isValid = true
# Groups the air into areas joined by air. False if air leaks out of the maze.
func _find_areas() -> bool:
for y in range(_bounds.position.y, _bounds.end.y):
for x in range(_bounds.position.x, _bounds.end.x):
var first := Vector2i(x, y)
if not _moves.is_air(first) or _areaAt.has(first):
continue
var tiles: Array[Vector2i] = [first]
_areaAt[first] = areaCount
var index := 0
while index < tiles.size():
var tile := tiles[index]
index += 1
for step in SlimeMoves.SIDES:
var next := tile + step
if _layer.get_cell_source_id(next) != -1 or _areaAt.has(next):
continue
if not _bounds.has_point(next):
problem = "Air leaks out of the maze at %s" % next
return false
_areaAt[next] = areaCount
tiles.append(next)
_areaTiles.append(tiles)
_links.append({})
areaCount += 1
return true
# Every tube mouth (a tube tile side open onto air), and the areas going in
# there can lead to.
func _find_links() -> void:
for cell: Vector2i in _layer.get_used_cells():
var openings := TubeSteer.get_openings(_layer, cell)
for outward in SlimeMoves.SIDES:
if openings & TubeSteer.side_bit(outward) == 0 or not _areaAt.has(cell + outward):
continue
mouthCount += 1
var from: int = _areaAt[cell + outward]
for exit in _moves.tube_exits(cell, -outward):
_links[from][_areaAt[Vector2i(exit.x, exit.y)]] = true
func _reachable_areas(from: int) -> Dictionary:
var found := {from: true}
var queue: Array[int] = [from]
while not queue.is_empty():
var area: int = queue.pop_front()
for other: int in _links[area]:
if not found.has(other):
found[other] = true
queue.append(other)
return found