gotnull

Part 3 of 13 in Ghostveil

Pulling the rules out of the game

Ghostveil's puzzle rules move into plain Dart, and a solver finds the shortest solution to every room.

By October 2026 the rules of Ghostveil lived in the same code that drew it. Collision was decided in four different places, they disagreed, and most of the bugs came from that. So I started again with the rules on their own.

The rules are now plain Dart with nothing from Flame in them. Everything that changes as you play a room sits in one object:

/// Everything about a room that changes as the player plays it. Immutable:
/// every move produces a new state, which is what makes undo, the solver
/// and replays trivial.
class PuzzleState {

Every input goes through one function, which hands back a new state, or nothing if the input changes nothing:

/// Applies one player input. Returns null when nothing would change at all
/// (not even facing), which the solver uses to prune.
StepResult? step(PuzzleState s, Dir d) => _burn(_step(s, d));

StepResult carries the new state and a list of events, such as PlayerMoved, BlockPushed, BlockSank and TurnstileTurned. The game no longer decides whether a move is allowed or what it changes. It animates the events, asks the rules what is powered and which gates are open, and the comment at the top of it says as much: "No gameplay rule lives here." Undo is a list of earlier states to pop. A reset goes onto the same list, so you can undo a reset too.

Here's Spark, the first room with live stone. Fire from the brazier runs along the channel, the stone pushed into the pit carries it the rest of the way, and the door opens. The walk and the sinking stone are events the rules handed back, and the fire and the open door come from asking the rules what the stone now carries.

A solver

With the rules out of the game, I can search through every way a room can go without drawing any of it. Each state has a string key, two states with the same key play the same, and the search keeps the cheapest cost it has seen for each key, queueing a state again only if it finds a cheaper way there. A step costs one. In rooms with saws, standing still while the blades move costs the player nothing, so it costs the search nothing either. That makes it a 0-1 breadth-first search: a wait goes to the front of the queue and a step to the back.

if (d == null) {
  queue.addFirst((r.state, k, cost));
} else {
  queue.addLast((r.state, k, cost));
}

The first winning state to come off the queue has the fewest steps there are, and that number is the room's par:

par⁡(R)=min⁡π ∈ wins⁡(R)  ∑a∈πc(a),c(a)={0a=wait1otherwise\operatorname{par}(R) = \min_{\pi \,\in\, \operatorname{wins}(R)} \; \sum_{a \in \pi} c(a), \qquad c(a) = \begin{cases} 0 & a = \text{wait} \\ 1 & \text{otherwise} \end{cases}

Here RR is the room, wins⁡(R)\operatorname{wins}(R) is every sequence of moves that solves it, π\pi is one of those sequences, aa is each move in it, and c(a)c(a) is what that move costs: nothing for a wait, one for a step.

A longer solution doesn't have to mean more states to search. Across the 25 rooms the game had that night, the one with the longest solution wasn't the hardest:

Scatter plot of par against states searched, log scale, for 25 rooms coloured by chapter: f03, a saw room with par 22, needs about 100,000 states, more than d03 with par 63 at about 31,000

f03 is a saw room with a par of 22, and proving that took 103,513 states. d03 needs 63 steps and only 30,800. Counting the new states the search finds at each step shows why. The blades keep moving while Fantasma stands still, so every square she can be on comes in as many versions as there are blade positions and beats, and there are nine states before she has taken a single step, every one of them reached just by waiting for the blades. At step 21 alone it finds 17,448 new ones. d03 has no saws, fans out slowly, and never finds more than a thousand at any step:

Rooms from the solver

The solver also builds the rooms. The generator starts from a random outline and random pieces, then tries thousands of small changes, keeping a change only when the room scores better. Every candidate goes through the solver with the same rules as the game, and only a candidate with a solution can become a room.

A room that can be solved can still be padded with a stone or a turnstile that does nothing. The comment on the check for that says that without any one turnstile, stone or crate, "the room must become unsolvable or take more steps":

for every p∈pieces⁡(R):R∖{p} has no solutionorpar⁡(R∖{p})>par⁡(R)\begin{gathered} \text{for every } p \in \operatorname{pieces}(R): \\ R \setminus \{p\} \text{ has no solution} \quad \text{or} \quad \operatorname{par}(R \setminus \{p\}) > \operatorname{par}(R) \end{gathered}

Here pieces⁡(R)\operatorname{pieces}(R) is the room's turnstiles, stones and crates, pp is any one of them, R∖{p}R \setminus \{p\} is the same room with pp taken out, and par is the fewest steps, as above.

The code under it doesn't match:

for (final level in without) {
  final sol = Solver(level, maxStates: 60000).solve();
  if (sol != null && sol.steps >= g.solution.steps) return false;
}
return true;

It turns a room down when taking a piece away leaves a solution as long as the original or longer. So the room is rejected for a piece whose removal makes the solution longer, exactly what the comment asks for, while a removal that makes it shorter passes that part of the check. A removal that leaves no solution passes too, as intended, and so does one whose search hits the 60,000-state limit, because the solver hands back the same null for both.

The finished rooms still had pieces they didn't need. A separate removal check finds them: leaving aside the stones that belong to secrets, two crates in f01, a stone in f02, and one crate in the saw room. Without that crate the saw room still solves in 22 steps, and its search drops from 103,513 states to 55,227. Had the finished room gone through the check as written, that equal-length solution would have rejected it.

A dungeon room with two saw blades on floor tracks, two wooden crates, two stone pillars, and stairs leading out on the right

All 25 rooms in the campaign came out of the generator, and a check that solves each one from scratch passed on all of them.