zones: stop the tools/backtrack path losing rooms and inventory

Review fallout from the locked-doors commit. Five defects, all in the
seams that commit opened:

backtrackFromDeadFork stepped to VisitedNodes[idx-1]. That slice is a
first-entry ordered *set*, not a path stack (see appendVisited), so once
a run has doubled back once the entry before CurrentNode can sit on a
different branch entirely — the party teleports across the map to a room
no edge connects. `!revisit` refuses exactly that move via
adjacentNodes; the autopilot has no business doing what the player is
forbidden from doing. Now routed through backtrackTarget, which also
refuses to fall back into a corridor whose only exit is the sealed fork:
the lock rolls are seeded per (run, edge), so walking back in gets the
same answer every time, forever.

The autopilot spent the player's thieves' tools *before* committing the
move, so an advanceZoneRunNode failure left them charged and then had
the caller's backtrack clear the fork they had just paid to open. The
tools are now reported by autoPickWithTools and only removed once the
party is actually through the door.

`sell all` never learned the new "tool" type and turned a €600 set into
€300 of loot — the same silent deletion Robbie was taught to avoid two
commits ago. It was already doing this to "key" quest tokens, so both
now sit with the special gear.

The shop's tools branch asked "is the reply a substring of Thieves'
Tools", which is true for a bare "s", for "to", and for an empty message
body — and it runs ahead of the consumable list, so a stray keystroke in
the Supplies view bought a set. isThievesToolsReply matches on the
item's own words instead.

Plus two cleanups in the same code: Robbie built his haul gifts by
calling consumableCache(tier, 1) in a loop when it already takes a
count, and the unlock flow read the whole inventory three times per
command.
This commit is contained in:
prosolis
2026-07-21 00:20:24 -07:00
parent 6bcac41aa2
commit 2ce3e682ea
5 changed files with 177 additions and 57 deletions
+60
View File
@@ -0,0 +1,60 @@
package plugin
import "testing"
// backtrackGraph — a fork at z.fork with two branches, plus a dead-end spur
// hanging off z.b whose only exit is back into z.b.
//
// z.entry → z.fork → z.a
// → z.b → z.spur
func backtrackGraph() ZoneGraph {
return ZoneGraph{
Nodes: map[string]ZoneNode{},
Edges: map[string][]ZoneEdge{
"z.entry": {{From: "z.entry", To: "z.fork"}},
"z.fork": {{From: "z.fork", To: "z.a"}, {From: "z.fork", To: "z.b"}},
"z.b": {{From: "z.b", To: "z.spur"}},
},
}
}
// TestBacktrackTargetRequiresAdjacency is the regression guard for the bug this
// fixes: VisitedNodes is a first-entry ordered set, not a path stack, so the
// entry before CurrentNode can sit on a branch the party never walked from
// here. Stepping to it blind teleports them across the map.
func TestBacktrackTargetRequiresAdjacency(t *testing.T) {
// Walked entry → fork → a, doubled back, then took fork → b. The visited
// set is [entry, fork, a, b]; z.a is *not* joined to z.b.
run := &DungeonRun{
CurrentNode: "z.b",
VisitedNodes: []string{"z.entry", "z.fork", "z.a", "z.b"},
}
got, ok := backtrackTarget(backtrackGraph(), run)
if !ok {
t.Fatal("should fall back to the fork")
}
if got != "z.fork" {
t.Errorf("backtrack target = %s, want z.fork (z.a shares no edge with z.b)", got)
}
}
// TestBacktrackTargetSkipsOneWayCorridor — backing into a room whose only exit
// is the sealed fork walks straight back in, and the lock rolls are seeded per
// (run, edge), so it would do it forever. Refuse instead.
func TestBacktrackTargetSkipsOneWayCorridor(t *testing.T) {
run := &DungeonRun{
CurrentNode: "z.spur",
VisitedNodes: []string{"z.entry", "z.fork", "z.b", "z.spur"},
}
if got, ok := backtrackTarget(backtrackGraph(), run); ok {
t.Errorf("backtracked to %s; z.b only leads back to z.spur, so this loops", got)
}
}
// TestBacktrackTargetAtEntry — nowhere behind the entry node.
func TestBacktrackTargetAtEntry(t *testing.T) {
run := &DungeonRun{CurrentNode: "z.entry", VisitedNodes: []string{"z.entry"}}
if _, ok := backtrackTarget(backtrackGraph(), run); ok {
t.Error("entry node has nowhere to back out to")
}
}