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:
- WP-091 ✅: the rules, the guarantee and the examples, in the plan’s §5 and Appendix C;
- Jeff’s decisions H1–H24 ✅ (H15 conditional, and not this packet’s).
Unblocks:
- WP-107, the provider, whose per-room
check calls this packet’s
navigate(); - packet 6, the input integration.
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:
- every case of Appendix C;
- a fixed-seed property test of the reachability guarantee.
Nothing calls the core in play yet. With or without Hotspot Assist, a player sees no change.
Background
- The contract (plan §4.3, Appendix B; H17, H24):
- Signature:
int navigate(const Common::Array<Graphics::HotspotInfo> &targets, int current, const Common::Point &from, Direction dir, bool repeat). - Returns: the index to select. To stay, it returns
currentitself; it returns −1 only for an empty list. - It never activates anything.
- It keeps no state and names no engine type.
- No directions parameter (H24: packet 16 adds one if it needs it).
- Signature:
- The rules (plan §5.1–5.5; H18, H19):
- a strict half-plane of candidates, with a 45° tier first;
- the score major² + 4·minor², in integers;
- ties broken by major, then y, then x;
- stay when nothing lies that way;
- with no selection, the rules run from the cursor, else the nearest target is selected;
HotspotInfo::typeis ignored;repeatis handled as a press (H21).
- The guarantee (plan §5.8; H20):
- The check: on each press the core checks whether the list’s geometric press graph is strongly connected.
- The fallback: if it isn’t, left and right follow the reading-order cycle (32 px rows) for that list, and up and down stay geometric.
- No selection: with no selection, a press is always geometric.
- The precondition: distinct points (H22, the provider’s job, WP-107).
- The tests (plan §5.8; H23). Phase 1 runs the core’s cases in the
engine’s self-test, not CxxTest; CxxTest comes when the core moves to
engines/(packet 15).- Where they run: the self-test runs its script checks when the
scriptdebug channel is at level 3 or more. That’s the blockcheckRoomSetup()throughcheckAllMovies(),terraglyph.cpp:134-153at fork55c8b6b32ae3, read while drafting.
- Where they run: the self-test runs its script checks when the
- Where it lives (H4): inside
engines/terraglyph/for Phase 1, in its own namespace with no TerraGlyph type, so packet 15 can move it. - Hand-written (plan §14.1). The C++ is written from §5’s text. WP-091’s scratch check was never committed and isn’t a source for this packet.
- Fork tip while drafting:
55c8b6b32ae3. The pre-flight re-pins. - Supersession check (2026-11-28). WORK_INDEX has no other core or navigation implementation packet, and plan §11 names this as packet 4. WP-104 (TV theme, draft PR open) holds EC-015, so this packet takes WP-106 and EC-016.
Scope
In scope
- 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.
- The core in new files, for example
engines/terraglyph/hotspot_magnet.hand.cpp, added toengines/terraglyph/module.mk:namespace HotspotMagnetwithDirection,navigate()andisGeometricConnected(), 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
int32for 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.
- The self-test check, for example
checkMagnetCore()in a newmagnet_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
repeattrue (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 press graph from
- The empty list returns −1.
- The log: the seed, the layout counts, how many needed the fallback, and the time it took.
- The screen. Thousands of random layouts of distinct on-screen
points, 40–64 of them, run through
- 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.
- Appendix C, one-to-one: 264
- 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.
- The fork PR against
scooby-engine, merged by hand after CI passes. It’s reviewed bytools/Check-ScummvmStyle.ps1once that tool is on main (it’s in open PR #209 today). - 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-dpadnote indashboard.json.
- the plan: §11 packet 4 marked done; §5.7’s estimate replaced with the
measured time; Appendix B with
Out of scope
- The provider, the point search, the hover and cursor-at-point rules, the listing hook and the per-room check. That’s WP-107.
- The input adapter (component C), the keymaps, the cursor adapter, activation and H21’s 80 ms constant. Packet 6.
- The selection’s lifetime (§4.4). Packet 7.
- The Hotspot Assist option. Packet 8.
- Calling the core from play. Nothing calls it until packet 6.
- CxxTest (H23). Packet 15.
- Any change to the rules. If the C++ shows a rule is ambiguous or wrong, the packet stops, records it, and raises it to Jeff as a plan correction. It doesn’t pick an answer in code.
Exit criteria
- Pre-flight. It exists, with the pin, the file names and the call site.
- 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. - Appendix C passes: all 802 checks, and the logged total is 802.
- The property test passes. Its seed, its layout counts and its fallback count are in the log.
- 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.
- The self-test passes on both titles with no new warnings, and every existing check matches the pre-change baseline.
- No change in play. A
git grepof the fork showsnavigate()called only from the check. No keymap, option or input path changes. - The fork PR is merged against
scooby-engine, and the merge commit is recorded. - 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
- Fork:
engines/terraglyph/hotspot_magnet.{h,cpp};engines/terraglyph/magnet_check.cpp;- the
module.mklines and the self-test call.
The pre-flight fixes the final names.
- This repo:
- the pre-flight;
- this WP, EC-016 and the WORK_INDEX row;
- plan §11, §5.7, Appendix B and the revision history;
dashboard.json.
Notes
- Hand-written (plan §14.1). Appendix B’s declarations and §5’s rules are the specification, not code to paste.
- Integers on every platform. The tie-break and the 45° boundary are exact comparisons. Keep them in integers so x86 and ARM agree (§5.2).
- Cost of a whole graph.
navigate()re-runs the connectivity check on each call, so building one press graph costs O(16n³). That’s fine in a room, but time the property test, especially in a debug build. - Fork safety. Work in a fork worktree, and never switch branches in the
shared
C:\dev\scummvmcheckout. - Build hint.
New-ForkWorktree.ps1still prints--enable-engine=scooby. Useterraglyph; the fix is in open PR #211.
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):
engines/terraglyph/hotspot_magnet.{h,cpp}:HotspotMagnet::navigate(),isGeometricConnected(),geometricChoice()andreadingOrder();engines/terraglyph/magnet_check.cpp:checkMagnetCore(), called last among the script checks;- the two objects in
module.mk, and the declaration and call interraglyph.{h,cpp}.
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:
- The seed’s layouts depended on the compiler. A point’s two random draws were in one constructor call, whose argument order C++ leaves open.
- Mutations the check couldn’t catch. Dropping or reversing the tie on
major, and three mistakes in reading order’s rows, all passed. Four
targeted cases now catch them, through the exposed
readingOrder(). - The backward half of
isGeometricConnected()was never exercised. It now is, by C9 with its stranded target listed first. An independent search also checks every screened layout. - A stale
currentwas read as no selection.navigate()now asserts it’s −1 or a valid index. - The coordinate assert would crash a release build. It’s now in development builds only.
For the next packets:
- WP-107’s provider must keep every point on screen and pass a valid
current. In a development build a slip stops on the assert; in a release build an off-screen point isn’t checked. - The check runs about 11 s in a debug build, mostly the property test’s screen.
- WP-113’s keymap (
terraglyph-default, OK as left click) is what packet 6 builds on. WP-113 already corrected the plan’s §6.4.