WP-106: Hotspot Magnet navigation core — navigate() and its self-test

Status: ✅ Done (2026-12-01). Fork PR barefootbetters/scummvm#61 merged on 2026-12-01 (merge commit 837ca576da). All nine exit criteria are met, with one recorded deviation: the Pi timing is left to packet 11 (§Result). Phase: 5 (polish and accessibility). Depends on:

Unblocks:

Companion EC: EC-016. Pre-flight: required. The packet adds files under engines/terraglyph/**. The pre-flight is written at the start of execution, as docs/reference/pre-flight-WP-106-<date>.md. Targets generation(s): none for the core, which carries no generation. Its self-test runs inside a Gen 1 game run, but reads no room. Estimated effort: half a day to a day.


Goal

Write packet 4 of the plan’s sequence: the navigation core, component B. It’s one pure function, navigate(), plus isGeometricConnected(), written by hand from the plan’s §5.1–5.5 and §5.8.

A self-test check runs:

Nothing calls the core in play yet. With or without Hotspot Assist, a player sees no change.

Background

Scope

In scope

  1. The pre-flight (required for engines/terraglyph/**). It covers:
    • the fork pin;
    • a fork worktree from tools/New-ForkWorktree.ps1;
    • the file names, the self-test call site and the risks.
  2. The core in new files, for example engines/terraglyph/hotspot_magnet.h and .cpp, added to engines/terraglyph/module.mk:
    • namespace HotspotMagnet with Direction, navigate() and isGeometricConnected(), as declared in Appendix B;
    • one addition for the check, geometricChoice(targets, from, dir), which gives 5.1’s choice before the fallback. It’s recorded in Appendix B as an addition;
    • every rule of §5.1–5.5 and the fallback of §5.8, exactly as written;
    • integer arithmetic only. Scores fit int32 for coordinates 0–16383 (§5.2); a debug assert checks that range;
    • no engine type, no global state, no allocation that outlives a call;
    • ScummVM’s code formatting and its GPL header.
  3. The self-test check, for example checkMagnetCore() in a new magnet_check.cpp, called with the other script checks:
    • Appendix C, one-to-one: 264 navigate() expectations. They are:
      • C1–C13’s four-direction tables: 232;
      • C9’s no-selection rows: 8;
      • C14’s four presses from a, and its four from the cursor (10, 10): 8;
      • C15’s grid: 16.
    • Each expectation in three variants: as given; with the list reversed (expecting index n − 1 − i); and with repeat true (expecting the same result). That’s 792 checks.
    • C9’s ten “5.1 alone” cells through geometricChoice(): 802 checks in all, and the log states the total.
    • isGeometricConnected(): false for C9, true for the rest.
    • A property test with a fixed seed (Common::RandomSource), in two tiers:
      • The screen. Thousands of random layouts of distinct on-screen points, 40–64 of them, run through isGeometricConnected() alone (O(4n²) each), to collect the rare layouts that need the fallback.
      • The full check. Every fallback layout collected, plus a few hundred others of 1–64 points, gets these checks:
        • the press graph from navigate() is strongly connected, and the same under the reversed list;
        • left and right follow reading order exactly when isGeometricConnected() is false;
        • up and down always equal geometricChoice();
        • with no selection, from a grid of cursor positions including the screen’s edges, each press selects a target.
      • The empty list returns −1.
      • The log: the seed, the layout counts, how many needed the fallback, and the time it took.
    • Performance: time a few thousand navigate() calls at 54 targets, and log the time per call. The check flags it if a call takes over 1 ms (§5.7).
    • The result: one pass line with the counts. On a failure: each failing case with its layout, origin, direction, and expected and actual result, and the self-test fails.
  4. The engine’s self-test passes on both titles, with no new warnings.
    • The baseline: a self-test run at the pin before any change.
    • The comparison: every existing check’s result matches it. The new timing lines are expected to vary between runs.
  5. The fork PR against scooby-engine, merged by hand after CI passes. It’s reviewed by tools/Check-ScummvmStyle.ps1 once that tool is on main (it’s in open PR #209 today).
  6. Records:
    • the plan: §11 packet 4 marked done; §5.7’s estimate replaced with the measured time; Appendix B with geometricChoice() and any other departure; a revision-history row;
    • this WP’s §Result;
    • EC-016;
    • the WORK_INDEX row;
    • the tv-dpad note in dashboard.json.

Out of scope

Exit criteria

  1. Pre-flight. It exists, with the pin, the file names and the call site.
  2. The core implements §5.1–5.5 and §5.8 with the Appendix B signatures, plus geometricChoice(). It has no engine type, no floating point and no state.
  3. Appendix C passes: all 802 checks, and the logged total is 802.
  4. The property test passes. Its seed, its layout counts and its fallback count are in the log.
  5. Performance is measured.
    • Windows: logged, with no call over 1 ms at 54 targets.
    • The Raspberry Pi: measured if it’s set up (§5.7 assigns the Pi run to this packet). If it isn’t, that deviation is recorded and packet 11 measures it.
  6. The self-test passes on both titles with no new warnings, and every existing check matches the pre-change baseline.
  7. No change in play. A git grep of the fork shows navigate() called only from the check. No keymap, option or input path changes.
  8. The fork PR is merged against scooby-engine, and the merge commit is recorded.
  9. The records are updated:
    • plan §11 and §5.7, Appendix B and the revision history;
    • WORK_INDEX and dashboard.json;
    • this WP’s §Result.

    The strict wiki build passes (hugo --config ewiki/hugo.toml --minify --panicOnWarning).

Deliverables

Notes

Result

Pre-flight: pre-flight-WP-106-2026-12-01, at fork 1cb2dd945a. Run sheet: EC-016.

In the fork (barefootbetters/scummvm#61, two commits, 569aa6d637 and ee71799aba, merged as 837ca576da):

The self-test, on both titles, debug build, --debuglevel=11 (logs kept local):

magnet core check examples: 802 checks, 0 failed
magnet core check connectivity: 15 checks, 0 failed
magnet core check targeted: 4 checks, 0 failed
magnet core check property: seed 9106; 20000 layouts of 40-64 screened, 11 need the fallback,
  0 disagree with the independent search; 211 of 211 layouts checked in full passed; empty list ok; 11148 ms
magnet core check time: 2160 presses at 54 targets in 129 ms, 59 us a press, the slowest 1 ms

Phantom’s lines are identical, except the timings: 10950 ms; 140 ms and 64 µs a press.

# Exit criterion Met Evidence
1 Pre-flight ✅ The pre-flight above, verdict READY: the pin, the files, the call site and the risks
2 The core ✅ hotspot_magnet.cpp implements §5.1–5.5 and §5.8 with Appendix B’s signatures, plus geometricChoice() and readingOrder() (recorded in Appendix B). Integers only, no engine type, no state
3 Appendix C passes ✅ “examples: 802 checks, 0 failed”. The C++ tables were compared with the plan’s tables as text by a script before the build: 14 layouts, 264 expectations, 0 differences
4 The property test ✅ Seed 9106: 20,000 layouts of 40–64 targets screened, 11 needing the fallback. All 211 layouts checked in full pass (the 11 and 200 others of 1–64). isGeometricConnected() agrees with an independent search on every screened layout. The empty list gives −1
5 Performance ✅ (deviation) Windows, debug build: about 60 µs a press at 54 targets, and the slowest single press is 1 ms, the clock’s resolution. The Pi isn’t measured: §5.7 gave that to this packet, and it’s left to packet 11, which sets up the Pi. §5.7 now carries the measured figure
6 Self-test on both titles ✅ Every check line matches the baseline run at the pin (Showdown 2,220 lines, Phantom 1,998), except lines that differ between any two runs: random sound and page picks, item positions, and one millisecond timing. All of them are logged before the magnet check runs. Warnings: none new in the build (two in shared code, as at the baseline), and in the logs Showdown 0 and Phantom 18, as at the baseline
7 No change in play ✅ git grep in the fork: navigate() is called only from magnet_check.cpp. No keymap, option or input file changed
8 The fork PR ✅ #61 merged as 837ca576da
9 Records ✅ Plan §11 (packet 4 ✅), §5.7, Appendix B, §14.3 and the revision history; WORK_INDEX; dashboard.json; this section. The strict wiki build passes

The review. A cold review ported the core line for line and confirmed every rule, the integer bounds, both connectivity searches and the check’s arithmetic. It found one bug and some gaps in the check, all fixed in the second commit before the merge:

For the next packets: