SlimePlatformer/Levels/Maze/slime_moves.gd
SnekInAScarf 58bd9fc1cd Remove goo tubes; steered maze tubes are always manual
Goo tubes played the same as manual tubes. Drop TubeGoo, the purple
tile alternatives and the tube_goo custom data layer; the maze
generator now picks auto or manual for two-way tubes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:04:09 -08:00

235 lines
8.7 KiB
GDScript

class_name SlimeMoves
extends RefCounted
# A model of how the slime can move round a tile map, read from the tiles, for
# MazeValidator. It only has moves the slime is sure to manage (checked in the
# game in 1-tile corridors and shafts), so "can reach End" here really means
# the slime can. See docs/MAZE.md.
#
# A state is the slime in an air tile, stuck to one of its sides that's solid:
# Vector3i(x, y, side), side indexing SIDES. From a state the slime can:
# - slide along the surface to the next tile, if the surface goes on there,
# - turn an inside corner onto the other solid side of the same tile,
# - wrap round an outside corner onto the far side of the block it's on,
# - cross a 1-tile gap to the opposite side (a hop or a bridge),
# - let go of a ceiling and fall straight down onto a floor,
# - go into a tube mouth in the surface it's on, coming out stuck to the face
# of the mouth it leaves by (any mouth of a manual network).
# "Forced" tiles move the slime without asking (it can't stay or cross):
# - the tile in front of a one-way tube's entrance sucks it in,
# - the blow zone in front of a one-way exit blows it to known spots (sideways:
# it slides onto the floor of the landing tile or the one after; down: it
# falls from the landing tile onto a floor).
# A state on a forced tile can reach End only if every place it can be moved
# to can. Exits that blow up, into a wall, sideways where it isn't 1 tile tall,
# or down through another forced tile can't be modelled, so they're a problem.
const SIDES: Array[Vector2i] = [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT]
const SLIDE_AFTER_BLOW = 1 # Tiles a sideways blow can slide the slime on past the landing tile.
var problem := "" # Why the tiles can't be modelled, or "".
var _layer: TileMapLayer
var _bounds: Rect2i
var _forced := {} # Air tile -> Array of states the air moves the slime to.
var _downBlowLandings: Array[Vector2i] = []
func _init(layer: TileMapLayer, bounds: Rect2i) -> void:
_layer = layer
_bounds = bounds
_find_blow_zones()
if problem.is_empty():
_find_suction_tiles()
if problem.is_empty():
_check_down_blows()
func is_air(cell: Vector2i) -> bool:
return _bounds.has_point(cell) and _layer.get_cell_source_id(cell) == -1
func is_forced(cell: Vector2i) -> bool:
return _forced.has(cell)
# The states the slime can be in at an air tile.
func states_at(cell: Vector2i) -> Array[Vector3i]:
var states: Array[Vector3i] = []
for side in SIDES.size():
if not is_air(cell + SIDES[side]):
states.append(Vector3i(cell.x, cell.y, side))
return states
# The states the slime comes out in after going into the tube tile `cell`
# heading `direction` (none if it can't go in there).
func tube_exits(cell: Vector2i, direction: Vector2i) -> Array[Vector3i]:
var exits: Array[Vector3i] = []
if TubeSteer.is_manual(_layer, cell):
for mouth in _network_mouths(cell):
exits.append(_state(mouth.front, mouth.tile - mouth.front))
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 := Vector2i((exitMouth - path[path.size() - 2]).normalized().round())
var halfTile := Vector2(_layer.tile_set.tile_size) / 2.0
var front := _layer.local_to_map(_layer.to_local(exitMouth + Vector2(outward) * halfTile))
if is_air(front):
exits.append(_state(front, -outward))
return exits
# Every state from which the slime can get to `endCell`.
func can_finish(endCell: Vector2i) -> Dictionary:
var required := {} # State -> how many of its next states must reach End first.
var previous := {} # State -> states that lead to it.
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 not is_air(cell):
continue
for state in states_at(cell):
var nextStates: Array = _forced[cell] if _forced.has(cell) else _moves(state)
required[state] = nextStates.size() if _forced.has(cell) else 1
for next: Vector3i in nextStates:
if not previous.has(next):
previous[next] = []
previous[next].append(state)
var finished := {}
var queue: Array[Vector3i] = []
if not _forced.has(endCell):
queue = states_at(endCell)
for state in queue:
finished[state] = true
while not queue.is_empty():
var state: Vector3i = queue.pop_back()
for earlier: Vector3i in previous.get(state, []):
if finished.has(earlier):
continue
required[earlier] -= 1
if required[earlier] <= 0:
finished[earlier] = true
queue.append(earlier)
return finished
# ---- Moves ----
func _moves(state: Vector3i) -> Array[Vector3i]:
var moves: Array[Vector3i] = []
var cell := Vector2i(state.x, state.y)
var side := SIDES[state.z]
for along: Vector2i in [Vector2i(side.y, side.x), Vector2i(-side.y, -side.x)]:
var next := cell + along
if is_air(next) and not is_air(next + side):
moves.append(_state(next, side)) # Slide along.
if not is_air(next):
moves.append(_state(cell, along)) # Inside corner.
if is_air(next) and is_air(next + side) and not _forced.has(next):
moves.append(_state(next + side, -along)) # Round an outside corner (through `next`).
if not is_air(cell - side):
moves.append(_state(cell, -side)) # Across a 1-tile gap.
if side == Vector2i.UP and is_air(cell + Vector2i.DOWN):
var landing := _fall_from(cell)
if landing != Vector2i(-1, -1):
moves.append(_state(landing, Vector2i.DOWN))
var tube := cell + side
if TubeSteer.get_openings(_layer, tube) & TubeSteer.side_bit(-side):
moves.append_array(tube_exits(tube, side))
return moves
# Where the slime lands falling straight down from `cell`, or (-1, -1) if it
# falls through a forced tile on the way.
func _fall_from(cell: Vector2i) -> Vector2i:
while is_air(cell + Vector2i.DOWN):
cell += Vector2i.DOWN
if _forced.has(cell):
return Vector2i(-1, -1)
return cell
# ---- Forced tiles ----
func _find_blow_zones() -> void:
for exit in OneWayTube.find_exits(_layer):
var outward: Vector2i = exit.outward
var zone := OneWayTube.blow_zone(_layer, exit.cell, outward)
var landing: Vector2i = exit.cell + outward * (OneWayTube.BLOW_RANGE + 1)
var where := "The exit at %s" % exit.cell
if outward == Vector2i.UP:
problem = "%s blows up (the slime could bounce in place)" % where
return
if zone.size() < OneWayTube.BLOW_RANGE or not is_air(landing):
problem = "%s blows into a wall (it could pin the slime)" % where
return
var outcomes: Array[Vector3i] = []
if outward.y == 0:
for tile in zone + [landing]:
if is_air(tile + Vector2i.UP) or is_air(tile + Vector2i.DOWN):
problem = "%s blows sideways where it isn't 1 tile tall" % where
return
for step in SLIDE_AFTER_BLOW + 1:
var tile := landing + outward * step
if not is_air(tile) or is_air(tile + Vector2i.DOWN):
break
outcomes.append(_state(tile, Vector2i.DOWN))
else:
outcomes.append(_state(_fall_from(landing), Vector2i.DOWN))
_downBlowLandings.append(landing)
for tile in zone:
if _forced.has(tile):
problem = "Two blow zones overlap at %s" % tile
return
_forced[tile] = outcomes
# Once every forced tile is known: a down blow mustn't drop the slime through one.
func _check_down_blows() -> void:
for landing in _downBlowLandings:
if _forced.has(landing) or _fall_from(landing) == Vector2i(-1, -1):
problem = "A down blow at %s drops the slime through another forced tile" % landing
return
func _find_suction_tiles() -> void:
for entrance in OneWayTube.find_entrances(_layer):
var front: Vector2i = entrance.cell + entrance.outward
var outcomes := tube_exits(entrance.cell, -entrance.outward)
if outcomes.is_empty():
continue # Leads nowhere: it doesn't pull.
if _forced.has(front):
problem = "The entrance at %s is in a blow zone" % entrance.cell
return
_forced[front] = outcomes
# The mouths of the manual tube network `first` is in (joined the way
# TubeSteer joins tiles: both manual, both open): [{tile, front}].
func _network_mouths(first: Vector2i) -> Array[Dictionary]:
var seen := {first: true}
var toVisit: Array[Vector2i] = [first]
var mouths: Array[Dictionary] = []
while not toVisit.is_empty():
var cell: Vector2i = toVisit.pop_back()
var openings := TubeSteer.get_openings(_layer, cell)
for direction in SIDES:
if openings & TubeSteer.side_bit(direction) == 0:
continue
var next := cell + direction
if is_air(next):
mouths.append({tile = cell, front = next})
continue
if TubeSteer.is_manual(_layer, next) and TubeSteer.get_openings(_layer, next) & TubeSteer.side_bit(-direction) and not seen.has(next):
seen[next] = true
toVisit.append(next)
return mouths
static func _state(cell: Vector2i, side: Vector2i) -> Vector3i:
return Vector3i(cell.x, cell.y, SIDES.find(side))