Let the restart action replay shops and events, and rewind the run record with it #324

Merged
nelsong6 merged 2 commits from claude/restart-events-shops-a4138a into main 2026-08-13 22:53:00 -07:00
nelsong6 commented 2026-08-13 22:52:55 -07:00 (Migrated from github.com)

The options-menu restart was combat-only. It never needed to be: it replays
the run save, and that save is a room-boundary snapshot for every room type.

Why shops and events are replayable

The whole game assembly has three gameplay-path SaveRun calls:

  • RunManager.EnterMapPointInternal — SaveRun(null) on map-node entry,
    written before the room type is even rolled.
  • CombatManager at victory — MarkPreFinished() then SaveRun(room, …).
  • EventRoom.OnEventStateChanged — the same, but only for AncientEventModel.

MerchantRoom, RestSiteRoom and TreasureRoom never save, and nothing is
written during a fight or an ordinary event. So for the duration of a shop or an
event, current_run.save is that room's opening state, and replaying it restores
the same stock or the same event because the RNG comes back with it.

What changed

CombatResetter becomes RoomResetter, and BlockedReason() becomes
Describe() — returning either the noun of the room a restart would replay or
the reason none can be. Availability keys off those save write points rather than
room type alone:

  • It reads RunState.BaseRoom, not CurrentRoom. An event option that starts a
    fight pushes a CombatRoom on top, but the save replays the map point, so what
    comes back is the event. The row now says so instead of offering to "restart
    this combat".
  • It refuses once the game has written a pre-finished room over the opening
    state: a combat room with no fight running (won, on rewards), an EventRoom
    with IsPreFinished (Ancient, done), or an event whose map point has hosted a
    combat room that is no longer running. Those replay into the saved aftermath
    and would undo nothing.

Rest sites and treasure rooms are replayable by the same mechanism and are
deliberately not offered here.

The record has to rewind too

Combat survived a restart on its own — stats sit in _pendingCombat until
promotion and the adopt path drops the buffer. Shops and events have no such
buffer. RecordGoldLoss writes straight to _currentRun.GoldStats whenever
inCombat is false and drags the map-legend category, the gold room-visit
counters and the Maw Bank / Old Coin / Golden Pearl aggregates with it. Replaying
a shop without rewinding banked every purchase twice — in the exact stats the
room exists to produce, per-relic attribution included.

So the record rewinds with the game:

  • RunTracker.CaptureRoomEntrySnapshot serializes _currentRun through
    RunStorage.Options as a room opens, riding SignetRingStatsPatch — the
    existing shared observer on Hook.AfterRoomEntered, so no new Harmony
    target
    . That hook fires just after EnterMapPointInternal wrote the save,
    which makes snapshot and save the same instant by construction. Taken last in
    that patch, and skipped when CurrentRoomCount > 1 so an event-spawned combat
    cannot clobber the event's snapshot.
  • RunTracker.RollBackToRoomEntry restores it. RoomResetter calls it after
    every abort path and immediately before CleanUp(), the point of no return, so
    a corrupt-save abort still leaves both the game and the record untouched. It
    persists via RunStorage.SaveAsync rather than SaveCurrentRun, since the
    latter would re-stamp the live post-purchase instance numbers over the restored
    ones; the identity maps are left for AdoptRunLocked to rebuild.

Side effect: combat restart is now exactly clean as well. Cards acquired
mid-combat commit immediately like shop purchases do, and the snapshot covers
that without enumerating every such path.

The snapshot is in memory, so a hot reload mid-room drops it and the row blocks
with no room-entry snapshot until the next room re-arms it. Refusing a restart
is cheap; silently inflating a run is not.

Notes

  • No persisted-shape change: the snapshot is a serialization of the existing
    RunData in the existing on-disk shape, so no new fixture or
    SchemaLoadingTests assertion is needed.
  • No new Harmony targets, so no full-game restart is required to pick this up.
  • Per AGENTS.md user-owned verification, tests and live behavioral verification
    were not run in this session.
The options-menu restart was combat-only. It never needed to be: it replays the run save, and that save is a room-boundary snapshot for every room type. ## Why shops and events are replayable The whole game assembly has three gameplay-path `SaveRun` calls: - `RunManager.EnterMapPointInternal` — `SaveRun(null)` on map-node entry, written *before* the room type is even rolled. - `CombatManager` at victory — `MarkPreFinished()` then `SaveRun(room, …)`. - `EventRoom.OnEventStateChanged` — the same, but only for `AncientEventModel`. `MerchantRoom`, `RestSiteRoom` and `TreasureRoom` never save, and nothing is written during a fight or an ordinary event. So for the duration of a shop or an event, `current_run.save` is that room's opening state, and replaying it restores the same stock or the same event because the RNG comes back with it. ## What changed `CombatResetter` becomes `RoomResetter`, and `BlockedReason()` becomes `Describe()` — returning either the noun of the room a restart would replay or the reason none can be. Availability keys off those save write points rather than room type alone: - It reads `RunState.BaseRoom`, not `CurrentRoom`. An event option that starts a fight pushes a `CombatRoom` on top, but the save replays the map point, so what comes back is the event. The row now says so instead of offering to "restart this combat". - It refuses once the game has written a pre-finished room over the opening state: a combat room with no fight running (won, on rewards), an `EventRoom` with `IsPreFinished` (Ancient, done), or an event whose map point has hosted a combat room that is no longer running. Those replay into the saved aftermath and would undo nothing. Rest sites and treasure rooms are replayable by the same mechanism and are deliberately not offered here. ## The record has to rewind too Combat survived a restart on its own — stats sit in `_pendingCombat` until promotion and the adopt path drops the buffer. Shops and events have no such buffer. `RecordGoldLoss` writes straight to `_currentRun.GoldStats` whenever `inCombat` is false and drags the map-legend category, the gold room-visit counters and the Maw Bank / Old Coin / Golden Pearl aggregates with it. Replaying a shop without rewinding banked every purchase twice — in the exact stats the room exists to produce, per-relic attribution included. So the record rewinds with the game: - `RunTracker.CaptureRoomEntrySnapshot` serializes `_currentRun` through `RunStorage.Options` as a room opens, riding `SignetRingStatsPatch` — the existing shared observer on `Hook.AfterRoomEntered`, so **no new Harmony target**. That hook fires just after `EnterMapPointInternal` wrote the save, which makes snapshot and save the same instant by construction. Taken last in that patch, and skipped when `CurrentRoomCount > 1` so an event-spawned combat cannot clobber the event's snapshot. - `RunTracker.RollBackToRoomEntry` restores it. `RoomResetter` calls it after every abort path and immediately before `CleanUp()`, the point of no return, so a corrupt-save abort still leaves both the game and the record untouched. It persists via `RunStorage.SaveAsync` rather than `SaveCurrentRun`, since the latter would re-stamp the live post-purchase instance numbers over the restored ones; the identity maps are left for `AdoptRunLocked` to rebuild. Side effect: combat restart is now exactly clean as well. Cards acquired mid-combat commit immediately like shop purchases do, and the snapshot covers that without enumerating every such path. The snapshot is in memory, so a hot reload mid-room drops it and the row blocks with `no room-entry snapshot` until the next room re-arms it. Refusing a restart is cheap; silently inflating a run is not. ## Notes - No persisted-shape change: the snapshot is a serialization of the existing `RunData` in the existing on-disk shape, so no new fixture or `SchemaLoadingTests` assertion is needed. - No new Harmony targets, so no full-game restart is required to pick this up. - Per AGENTS.md user-owned verification, tests and live behavioral verification were not run in this session.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
romaine-life/spirelens!324
No description provided.