Make the direct mouse camera work on game 1.1.1.

mouse_mode = direct is the default, but on 1.1.1 it did nothing: only the 1.4.3 patch table had the sites, so the mouse always went through the stick emulation, which gives no turn at slow mouse speeds and tops out at about 107 degrees a second. The 1.1.1 table now has the set mouse_direct (on by default): at main+0x37c45c, where the camera's input update holds the processed stick in s1 and s0, a host call performs the displaced store and adds the raw mouse counts to both, so the mouse meets no dead zone and no curve; at main+0x37c4f0 a second call clears the idle bit for an update the mouse moved in. The counts are divided by the frames the game steps per update and by the stick camera speed, so the same hand movement turns the same angle at every frame rate. The Controls tab gets the mouse mode and mouse sensitivity rows, and mouse_mode = stick or NX2NAVI_MOUSE_DIRECT_PATCH=0 gives the old behaviour.

Checked here: the generator lifts the 1.1.1 game with the two sites (it compares each site's original word), the lifted game compiles and links with the runtime, and tests/mouse_direct_check.py passes its 1.1.1 harness (register contract, displaced store and load, equal turn at every frame rate and stick speed, the reset pause, capture and mode). Not checked here: the game was not run, so aiming with the mouse was not tried, and the differential tests against Unicorn were not run because no Python on this PC has Unicorn. The contributor measured 0.0800 degrees per count on both axes in the game.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit bb8fecfbe43517006eafd7be9c22e8dbe9601de7)
This commit is contained in:
chrg6
2026-11-11 00:04:18 -05:00
parent 1e01f90db5
commit 861357b2a4
14 changed files with 260 additions and 19 deletions

View File

@@ -44,6 +44,8 @@ int input_direct_mouse_active(void) {
int input_direct_mouse_enabled(void) { return direct_mode(); }
int input_direct_mouse_available(void) { return rt_patch_enabled(rt_patch_set("mouse_direct")); }
void input_direct_mouse_radial(int32_t dx, int32_t dy, float sensitivity, float cursor[2], int32_t out[2]) {
if (!(sensitivity > 0)) { cursor[0] = cursor[1] = 0; out[0] = out[1] = 0; return; }
cursor[0] += dx * (double)sensitivity / 200;
@@ -330,7 +332,90 @@ static void camera_activity(Cpu *c) {
c->v[8] = d8;
}
// ---- Ocarina 1.1.1 (recomp/patches-v111.txt, set mouse_direct)
// Its camera input update is main+0x37c300: the same steps as above with other registers and offsets. At the site s1
// and s0 hold the processed x and y (both zero while the camera-reset timer at +0x5e0 runs), x20 the input object and
// x21 the flags (bit 0 camera input off, bit 1 invert x, bit 2 invert y). The two stores, the inversion and the idle
// test that follow read s1 and s0, so the counts added here go through them as the stick's part does.
#define OOT_STICK 0x538 // the processed stick: x, and y at +0x53c
#define OOT_RESET_TIMER 0x5e0 // 60 Hz frames left of the pause after a camera reset (right stick click)
#define OOT_FLAG_IDLE 0x10u // flags (+0x530): the processed stick is under the game's 10% idle threshold
// NX2NAVI_MOUSE_TRACE=1 (test runs): one line per camera input update, with the stick the update made, the counts taken
// and what they added. vk_dlaa.c adds the camera's direction per frame, so a scripted run gives degrees per count.
static int mouse_trace(void) {
static int on = -1;
if (on < 0) {
const char *e = cfg_get("NX2NAVI_MOUSE_TRACE");
on = e && *e && strcmp(e, "0");
}
return on;
}
static void camera_input_oot(Cpu *c) {
mouse_input_object = 0;
uint64_t object = c->x[20], now = plat_now_ns();
uint32_t flags = (uint32_t)c->x[21];
float timer = guest_float(object + OOT_RESET_TIMER);
int accept = !(flags & 1) && !(timer > 0) && !input_direct_mouse_selector_active();
__atomic_store_n(&accepts_mouse, accept, __ATOMIC_RELAXED);
__atomic_store_n(&last_update, now, __ATOMIC_RELEASE);
float x, y, add[2] = {0, 0}, frames = frame_step_frames();
memcpy(&x, &c->v[1], sizeof x);
memcpy(&y, &c->v[0], sizeof y);
int32_t dx = 0, dy = 0;
if (direct_mode()) {
input_direct_mouse_sample(accept, &dx, &dy);
float sensitivity = fminf(10, fmaxf(0, (float)cfg_double("NX2NAVI_MOUSE_SENS")));
// The game multiplies the processed stick by the frames it steps and by its camera speeds, which
// NX2NAVI_STICK_SENS scales (camera_speed.c): divided by both, a count turns the camera by the same angle at
// every frame rate and stick speed. Nothing after the site clamps the sum, so a fast flick is not cut short.
float per = frames * cam_speed_applied();
if (accept && sensitivity > 0 && isfinite(per) && per > 0 && (dx || dy)) {
mouse_input_object = object;
add[0] = dx * (float)INPUT_OOT_COUNT_X * sensitivity / per;
add[1] = -dy * (float)INPUT_OOT_COUNT_Y * sensitivity / per;
float nx = x + add[0], ny = y + add[1];
uint32_t xb, yb;
memcpy(&xb, &nx, sizeof xb);
memcpy(&yb, &ny, sizeof yb);
c->v[1] = xb;
c->v[0] = yb;
static int logged;
if (!logged) {
logged = 1;
rt_log("mouse: direct camera input after the stick dead zone, clamp and exponent (Ocarina 1.1.1)");
}
}
}
if (mouse_trace())
rt_log("mousetrace: in t=%.3f frames=%.4f flags=%x accept=%d timer=%.2f stick=%.5f,%.5f counts=%d,%d add=%.5f,%.5f",
now / 1e6, frames, flags, accept, timer, x, y, dx, dy, add[0], add[1]);
uint32_t original = (uint32_t)c->v[1];
memcpy((void *)(uintptr_t)(object + OOT_STICK), &original, sizeof original); // displaced str s1, [x20, #0x538]
}
// The idle test follows the first site and already counts the mouse's part, but one slow count adds less than the
// game's 10% threshold: an update the mouse moved in is not idle. The site is the displaced ldp d9, d8, [sp, #0x10]
// before the update's last `str w8, [x20, #0x530]`, with the flags in w8.
static void camera_activity_oot(Cpu *c) {
if (mouse_input_object && mouse_input_object == c->x[20]) {
c->x[8] = (uint32_t)c->x[8] & ~OOT_FLAG_IDLE;
mouse_input_object = 0;
}
uint64_t d9, d8;
memcpy(&d9, (const void *)(uintptr_t)(c->sp + 0x10), sizeof d9);
memcpy(&d8, (const void *)(uintptr_t)(c->sp + 0x18), sizeof d8);
c->v[9] = d9;
c->v[8] = d8;
}
__attribute__((constructor)) static void mouse_direct_register(void) {
if (rt_game_version == 111) { // Ocarina: two sites of its own, no orbit or held-object site
rt_register_host(HOST_MOUSE_DIRECT, camera_input_oot);
rt_register_host(HOST_MOUSE_ACTIVITY, camera_activity_oot);
return;
}
rt_register_host(HOST_MOUSE_DIRECT, camera_input);
rt_register_host(HOST_MOUSE_ACTIVITY, camera_activity);
rt_register_host(HOST_MOUSE_ORBIT_BEGIN, orbit_begin);