SlimePlatformer/docs/MAZE.md
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

138 lines
7.4 KiB
Markdown

# Maze mode
A "Maze" button on the main menu asks for a size and a complexity, then
generates a level of rock chambers joined by tubes, with a Start and an End.
## Goals (checkable)
1. Main menu has a **Maze** button. It opens a setup panel (same look as
Options) with **Width** (1-4 screens), **Height** (1-3 screens) and
**Complexity** (1-5), plus **Generate** and **Back**.
2. Generate builds a maze level from those inputs and a random seed. The
same inputs and seed always give the same maze.
3. The maze spans `Width x Height` screens (one screen = 20 x 15 tiles, the
RoomCamera's room). Chambers sit inside screens; tubes run through the rock
inside a screen and across screen borders.
4. It uses the existing tube kinds (auto, manual, goo) and a new **one-way**
kind.
5. One-way tubes: orange tiles; only their entrance mouth lets the slime in;
orange motes drift into the entrance like a vacuum. Built by extending the
existing tube code (the auto tube's `PixelStream`), not by copying it.
6. Start and End are clearly visible (a green START pad, a gold END pad).
Touching End shows a win panel with **New maze** and **Main menu**.
7. **No soft-locks.** For every chamber the slime can reach from Start, End is
still reachable. Checked on the finished tiles by a validator that does not
trust the generator; a failed maze is thrown away and the next seed tried.
8. A headless check generates many mazes (all sizes, complexities, many seeds)
and runs the validator on each. Target: 100 % pass on the first try.
What the inputs control:
| Complexity | Chambers per screen | One-way tubes on the Start-End route | Extra tubes (per screen) | Steered (manual/goo) share | Tube winding |
|---|---|---|---|---|---|
| 1 | 2 x 1 | 0 | 0 | 0 % | low |
| 2 | 2 x 2 | 1 | 0.5 | 20 % | low |
| 3 | 2 x 2 | 2 | 1 | 30 % | medium |
| 4 | 3 x 2 | 3 | 1.5 | 35 % | high |
| 5 | 3 x 2 | 4 | 2 | 40 % | high |
Extra tubes are about half one-way (traps/shortcuts), half two-way (loops).
## Non-goals
- No change to player movement, existing levels or other menus (besides the
one button). The player only gains the "one-way tubes only let you in at
their entrance" rule, which is part of the new tube kind.
- No tube crossings or branching tube networks in mazes.
- No ledges, hazards, items or enemies inside chambers: chambers are plain
rectangles (this is what keeps the movement model below exact).
- No saving of mazes; the seed is not shown in the UI.
## How the player moves (the model the check uses)
From `Characters/player.gd`: the slime crawls on floors, walls and ceilings,
wraps round outside corners and takes inside corners. So inside one closed
rectangle of air it can reach every spot on the rectangle's boundary, and so
every tube mouth in it. Chambers are therefore nodes of a graph, and the only
ways between them are tubes:
- **Auto** tube (green): either end in, traced to the other end
(`PixelStream.trace_tube`, the same function the player uses). Edge both ways.
- **One-way** tube (orange): an auto tube that only accepts the slime at the
mouth whose tile is marked as the entrance (`OneWayTube.can_enter`, also used
by the player). Edge from the entrance chamber to the exit chamber only.
- **Manual / goo** tubes (grey / pink): steered; the slime can come out
of any mouth with room or back out. Edges between all their mouths' chambers.
- A mouth only counts as an exit if the tile in front of it is air (room for
the slime to reform).
## The soft-lock check (`MazeValidator`)
Works only from the TileMapLayer:
1. Flood-fill the air cells inside the maze into chambers. Fail if air leaks
out of the maze bounds or a chamber is not a full rectangle (the movement
model above would not hold).
2. Find every tube mouth (a tube tile side that is open onto air).
3. Build a directed chamber graph from the tube rules above.
4. **Forward** search from the Start chamber gives `reachable`. **Backward**
search (reversed edges) from the End chamber gives `canFinish`.
5. Pass if End is in `reachable` and every chamber in `reachable` is also in
`canFinish`. That is the no-soft-lock rule; "Start reaches End" alone is not
enough, because a one-way tube could drop the slime where End is out of reach.
## Approaches considered
1. **Chamber graph first, then carve tiles (chosen).** Lay out a grid of
rectangular chambers, build a spanning tree over neighbouring chambers
(a classic maze), pick Start and End, root the tree at End, make some tree
edges on the Start-End route one-way towards End, and add extra tubes. Then
carve: rock everywhere, air chambers, each tube routed through the rock with
`AStarGrid2D` and random weights so tubes wind.
*Safe by construction:* with the tree rooted at End and every tree edge
usable towards End, every chamber can reach End, so any extra tube in any
direction is safe. The validator then checks the real tiles anyway.
2. **Tile-level random level + search.** Random caves and random tubes, then
a search over the player's moves at tile level, keep if it passes. Simple to
write but most tries fail, with no control over difficulty; the per-tile
movement search is also a big, fragile copy of `player.gd`.
3. **Template rooms stitched together.** Hand-made screens with tube stubs at
the edges, joined like jigsaw pieces. Looks good, but needs many hand-made
rooms, gives little variety per size, and one-way placement still needs the
same graph reasoning as approach 1.
4. **Graph with a solver-guided one-way search.** Start from a strongly
connected graph and keep adding one-way tubes while a solver says it is
still safe. Works, but is slower and less predictable than approach 1, which
gets the same safety for free from the rooted tree.
Approach 1 meets every goal: inputs map directly to grid size and edge counts,
chambers stay plain rectangles (so the movement model is exact), tubes cross
screens naturally, and safety does not depend on luck.
## Files
- `UI/maze_setup_menu.*` - the size/complexity panel.
- `Levels/Maze/maze_settings.gd` - the chosen inputs (static, read by the level).
- `Levels/Maze/maze_graph.gd` - chambers, spanning tree, one-way and extra tubes.
- `Levels/Maze/maze_carver.gd` - turns the graph into tiles, routes tubes.
- `Levels/Maze/maze_tiles.gd` - finds the right tile for a tube shape and kind.
- `Levels/Maze/maze_validator.gd` - the soft-lock check.
- `Levels/Maze/maze_builder.gd` - graph, carve, check; next seed on failure.
- `Levels/Maze/maze_level.*` - the playable scene: generate, validate, place.
- `Levels/Maze/maze_marker.gd` - the Start and End pads.
- `Levels/Maze/maze_win_menu.*` - the win panel.
- `Effects/one_way_tube.gd` - one-way tube rules (entrance check).
- `Effects/pixel_stream.gd` - `trace_tube()`, moved here from `player.gd` so
the player and the validator share it.
- `Assets/level_tiles.tres` - orange alternatives 3-5 on the six two-sided
tube tiles (3 = body/exit, 4 and 5 = entrance on each side), and the
`tube_one_way` / `tube_one_way_entry` custom data layers.
- `Effects/tube_vacuum.gd` - the orange motes drifting into entrances.
- `tests/maze_check.gd` - the headless batch check.
Run the check:
`godot --headless --audio-driver Dummy --script res://tests/maze_check.gd`
The check also builds one known soft-lock (a chamber whose tubes are all
turned into one-way tubes leading in, while Start still reaches End another
way) and makes sure the validator rejects it.