2026-11-16 15:45:46 -06:00
|
|
|
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.
|
|
|
|
|
#
|
2026-11-16 15:52:58 -06:00
|
|
|
# Air blows out of one-way exits (OneWayTube.blow_zone, TubeBlower) and knocks
|
|
|
|
|
# the slime away, so it can't stay in a blow zone. A blow zone is a strip of
|
|
|
|
|
# air tiles straight out from a wall. If it's shorter than the chamber is deep
|
|
|
|
|
# that way, it cuts the chamber's boundary (the loop of tile sides the slime
|
|
|
|
|
# crawls round) in one place only, so every tile side outside it can still be
|
|
|
|
|
# reached the other way round. Two zones could cut the loop twice and split the
|
|
|
|
|
# chamber, so each chamber may have one at most. No other mouth may open into a
|
|
|
|
|
# zone, and Start and End can't be in one. Then the model above still holds.
|
|
|
|
|
# The slime can't be pinned: outside the zone there's no push, it's always
|
|
|
|
|
# pushed out of the zone, and it can steer in the air.
|
|
|
|
|
#
|
2026-11-16 15:45:46 -06:00
|
|
|
# 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.
|
2026-11-16 15:52:58 -06:00
|
|
|
var _chamberSizes: Array[Vector2i] = [] # Per chamber: its size in tiles.
|
2026-11-16 15:45:46 -06:00
|
|
|
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
|
2026-11-16 15:52:58 -06:00
|
|
|
if not _check_blowers(startCell, endCell):
|
|
|
|
|
return
|
2026-11-16 15:45:46 -06:00
|
|
|
_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
|
2026-11-16 15:52:58 -06:00
|
|
|
_chamberSizes.append(size)
|
2026-11-16 15:45:46 -06:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2026-11-16 15:52:58 -06:00
|
|
|
# Checks the blow zones of one-way exits keep the movement model true (see the
|
|
|
|
|
# top of this file).
|
|
|
|
|
func _check_blowers(startCell: Vector2i, endCell: Vector2i) -> bool:
|
|
|
|
|
var blownChambers := {}
|
|
|
|
|
var blowCellOwners := {} # Air tile in a blow zone -> the exit tube tile blowing there.
|
|
|
|
|
for exit in OneWayTube.find_exits(_layer):
|
|
|
|
|
var zone := OneWayTube.blow_zone(_layer, exit.cell, exit.outward)
|
|
|
|
|
var chamber: int = _chamberAt[zone[0]]
|
|
|
|
|
if blownChambers.has(chamber):
|
|
|
|
|
problem = "Chamber %d has more than one blow zone" % chamber
|
|
|
|
|
return false
|
|
|
|
|
blownChambers[chamber] = true
|
|
|
|
|
var depth := _chamberSizes[chamber].x if exit.outward.x != 0 else _chamberSizes[chamber].y
|
|
|
|
|
if zone.size() >= depth:
|
|
|
|
|
problem = "A blow zone spans chamber %d" % chamber
|
|
|
|
|
return false
|
|
|
|
|
for zoneCell in zone:
|
|
|
|
|
blowCellOwners[zoneCell] = exit.cell
|
|
|
|
|
if blowCellOwners.has(startCell) or blowCellOwners.has(endCell):
|
|
|
|
|
problem = "Start or End is in a blow zone"
|
|
|
|
|
return false
|
|
|
|
|
for mouth in _mouths():
|
|
|
|
|
var front: Vector2i = mouth.cell + mouth.outward
|
|
|
|
|
if blowCellOwners.has(front) and blowCellOwners[front] != mouth.cell:
|
|
|
|
|
problem = "A mouth at %s opens into a blow zone" % mouth.cell
|
|
|
|
|
return false
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Every tube mouth: a tube tile side open onto air. [{cell, outward}].
|
|
|
|
|
func _mouths() -> Array[Dictionary]:
|
|
|
|
|
var mouths: Array[Dictionary] = []
|
2026-11-16 15:45:46 -06:00
|
|
|
for cell: Vector2i in _layer.get_used_cells():
|
|
|
|
|
var openings := TubeSteer.get_openings(_layer, cell)
|
|
|
|
|
for outward in TubeSteer.DIRECTIONS:
|
2026-11-16 15:52:58 -06:00
|
|
|
if openings & TubeSteer.side_bit(outward) and _chamberAt.has(cell + outward):
|
|
|
|
|
mouths.append({cell = cell, outward = outward})
|
|
|
|
|
return mouths
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Where going into each mouth can take the slime.
|
|
|
|
|
func _find_links() -> void:
|
|
|
|
|
for mouth in _mouths():
|
|
|
|
|
mouthCount += 1
|
|
|
|
|
var from: int = _chamberAt[mouth.cell + mouth.outward]
|
|
|
|
|
for to in _exits(mouth.cell, -mouth.outward):
|
|
|
|
|
_links[from][to] = true
|
2026-11-16 15:45:46 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|