Skip to content

Latest commit

 

History

History
43 lines (31 loc) · 10.2 KB

gestures.md

File metadata and controls

43 lines (31 loc) · 10.2 KB

Spell gestures and the input path

The spell lessons ask the player to draw a shape with the mouse, and two native functions of Engine.dll score the drawing against the lesson's template: UGesture::CompareGesture (native 426, the whole drawing) and UGesture::CompareGesturePoint (native 427, one point). The decompiler cannot read either body (each reserves a frame of about 16 KB and the output loses calls and arguments), so the numbers come from the instruction stream. 426 contains a real bug of the original that reads uninitialised memory; the port deliberately takes the "no discards" reading, which a live capture of the running original supports. The second half of this note records how keyboard and mouse events travel through the original's engine, as far as the repo has traced it.

Where the natives sit

  • UGesture::execCompareGesture: body 0x103a3f60 (export thunk 0x103035ee, ordinal 2226). UGesture::execCompareGesturePoint: body 0x103a3cc0 (thunk 0x10301244, ordinal 2227). Shared scorer: 0x103a39c0 (thunk 0x1030342c). (port: UELevelRuntime.GestureNatives.cs, CompareGestureNative, CompareGesturePointNative)
  • Call order in a lesson: the drawing state only records points and switches to the judge state; the judge state's first step calls 426 once, and its tick then calls 427. So the first 426 of an attempt has no 427 before it.
  • Both bodies clamp their point counts to 0x400 before looping (0x103a3fed, 0x103a409d, 0x103a3d2d): a longer array is truncated, not rejected.

What the scorer does

  • Drawn set. Points with z == -1.0f exactly (0x103a401b) are unused slots and are skipped. All kept points are flattened to z = 0.
  • Template densified eight times. Each template point emits itself followed by seven interpolations toward the next one, at fractions 0.125 to 0.875 (doubles in .rdata, 0x104765e8..0x10476588), computed as (next - cur) * f + cur on the x87 stack and stored as float. The last template point emits only itself, so the count handed to the scorer is m*8 - 7 (0x103a41ce).
  • Distance and running minimum. The squared sum is built on the register stack, stored to a double and passed to appSqrt; it is then compared with a float running minimum that starts at 1.0 and is stored back as float.
  • Score (0x103a39c0). Pass 1 over the template: a template point with a drawn point within accuracy adds 2 to the total only; otherwise its cost is trunc(d / (accuracy * 5) + 1) capped at 2, added to both penalty and total (the quotient lives in a register and must not be rounded to float; rounding it makes 1.99999994 become 2). Pass 2 over drawn points: one farther than accuracy from every template point costs trunc(d / accuracy) capped at 3, added to both. Fewer than 10 drawn points adds a flat 500 to both. Score is clamp(1 - penalty/total, 0, 1); total == 0 yields infinity or NaN on the x87 and the lower clamp returns 0.
  • Casts use _ftol semantics: NaN, infinities and out-of-range values give the 64-bit integer indefinite, whose low dword is zero. With accuracy == 0 the division gives infinity and every missed point adds zero to both sums, so the outcome depends on the parity of such terms. See x87-precision.md.
  • Point native (427). It densifies the same way but is different code: the difference next - cur is computed once and kept on the register stack, with x kept wide for all seven steps while y is stored as float after the first step and re-read narrowed for steps 0.25 to 0.875. The result is the raw minimum distance, or 1.0 if nothing is nearer. accuracy is evaluated by the exec and never read (no reference to its slot after 0x103a3e44).

The original's bug and the port's reading (decision 426)

  • The second filter of the drawn set is meant to drop duplicates. Its inner loop (0x103a402c..0x103a4077) compares each candidate with [edx-4] and [edx], where edx is the write cursor, advanced only after a store (0x103a4088); the pointer is not indexed by the loop counter. So a candidate is compared with the slot 8 bytes ahead of the last written one: memory the call has not written yet, holding whatever earlier code left in the stack (about 4 KB below the entry stack pointer). The answer is not a function of the arguments alone.
  • The densify loop also overruns: the template buffer holds 128 points of 8 bytes and the write is unchecked (0x103a40b7). Measured: 128 template points give 0.4377880096, 129 give 0.6610058546 because the extra slot lands on the first drawn point. The longest shipped pattern has 32 points (the Alohomora one), so the port stops loudly beyond 128 instead of imitating the overrun.
  • Live capture (decision 426). The running 1.53 original was probed at the dedup loop and the result store (0x103a4203) on the Alohomora and Wingardium Leviosa lessons: 3 cold starts, 10 attempts. Replaying each capture on the real body is bit exact, the port matches the game bitwise 10 of 10, and no drops occurred in any attempt. The decision (author, 2026-09-29, closed 2026-10-09) is option 1: no discards. "Fixing" the comparison to check all saved points is forbidden, because it changes scores: on sets shorter than ten points it moves the penalty by 500 and flips the lesson outcome. (port: GestureDedupDrawn)
  • Open: the live capture covers the campaign's lessons; on synthetic input the original's answer genuinely depends on stack residue (a constructed repeat of the call changed the answer in 48 of 144 cases, 23 of them with real campaign patterns). A live playthrough of the lessons is listed separately as not done in that record.

Input path of the original

This part comes from an audit of the Windows driver (WinDrv.dll) and engine input code against the port. The port side is a snapshot and some differences listed there are still open.

  • Everything is synchronous between ticks. The main loop (HP.exe MainLoop 0x10914a60) alternates the engine tick and the OS message pump (order taken from a comment, not re-verified). Each input event runs the console script and bindings inside the window procedure, in OS message order. Nothing is queued per frame. Inside the tick, AActor::Tick (around 0x103b414a) calls Player->ReadInput(dt), then PlayerInput, PlayerTick, and ReadInput(-1.0).
  • UViewport::ReadInput (0x10385d20): if dt != -1 it calls UpdateInput(0), then Input->ReadInput(dt). UWindowsViewport::UpdateInput(0) only releases: for each virtual key whose entry is in KeyDownTable but whose GetKeyState says up, it sends a Release (final loop 0x11109c52..0x11109cf6). Presses come from UpdateInput(1), called by UInput::ResetInput (0x103a5f30..0x103a60a1).
  • Events. WM_KEYDOWN sends a Press on every message, autorepeat included; Alt+Enter toggles fullscreen and sends no event (0x1110b2ec, 0x1110b2fa); WM_KEYUP sends Release (0x1110b38b). WM_CHAR (control characters too: Backspace, Tab, Escape) goes to UEngine::Key, so the order is Press then KeyType. The shipped ini has UseDirectInput=False; the mouse arrives as WM_MOUSEMOVE, drained per batch into one MouseX and one MouseY axis event. Each WM_MOUSEWHEEL sends a MouseW axis (raw plus or minus 120) and a Press and Release of MouseWheelUp or Down (0x1110bec9..0x1110bf15).
  • UEngine::InputEvent (0x10393a08..0x10393a8b): the console script's KeyEvent runs first. If it consumes the event, a Release still goes to UInput::PreProcess (clearing the down table) but the binding is not executed; otherwise PreProcess then Process run the binding. PreProcess drops a Press on a key already down and a Release on a key not down.
  • UInput::ReadInput(dt): for keys 0 to 254 in index order, a down key gets a Hold through the bindings; then every input float property of the pawn is multiplied by 20/dt (with dt == -1, by 0, which zeroes them). Smoothing of the mouse is script (PlayerInput bytecode). Mouse chain: pixels, then the Axis binding with Speed=6.0, += delta * speed * 0.01, then 20/dt, then the sensitivity setting in script.
  • Reset points. ResetInput is called on focus gained and lost (0x1110add4, 0x1110ad08) and by LoadMap for each viewport; it clears the table and input properties then calls UpdateInput(1), which re-sends a Press for every key down, never a Release. Opening or closing the menu does not reset input.
  • Menu consequence. The console script owns Escape (HPConsole.KeyEvent, then the menu book). Keys held when the menu opens stay in the down table; their Release is consumed by the console, so only PreProcess runs and the binding's release half never does. A button held across the opening of the menu and released inside it stays pressed after the menu closes until pressed and released again.

Where the port differs (from the input audit)

  • The audit lists 30 rows. Matches: console-first dispatch, dedup, Exec forms, the 20/dt scaling, Alt+Enter handling and the Enter filter on WM_CHAR. Mismatches: hold loop order (the port iterates a hash set instead of key index order, which can change the last bit when two held keys feed one axis); ReadInput is called by the frontend before the tick instead of inside the player's actor tick; the menu half bypasses UEngine::InputEvent; Escape release and middle mouse never reach the menu console; per-frame mouse sums instead of one axis per batch; wheel as one Press and Release per frame without the axis event (invisible with shipped data, since the bindings are empty).
  • Improvements (not original): Alt+Enter autorepeat does not toggle again; the mouse wheel scrolls combo lists in the menu (D-70).
  • Open: whether the original's "stuck AltFire after a menu release" is wanted (author to see an original and port pair); the effect on native Windows of the mouse speed setting the original installs while the mouse is captured ({0,0,0x10000}, via SystemParametersInfo); windowed-mode mouse capture is not traced; the main loop order (tick then pump) is taken from a comment, not re-verified. The proposed redesign (the frontend sends raw device events and Core owns the window-procedure logic) is a spec, not implemented as of this note. (port: UEInput.cs, hpengine/Input/)