diff --git a/docs/controls.md b/docs/controls.md index b1fc07fb..6d14f103 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -110,17 +110,30 @@ bind_Y = pad:north, key:mouse_l ## Mouse camera `mouse_mode = direct` (the default) turns the camera by the raw mouse motion on game 1.4.3, through hooks in the game's camera code (`runtime/mouse_direct.c`, the `mouse_direct` sites of `recomp/patches-v143.txt`). None of them changes a step of the gamepad alone. The free-orbit camera receives raw-count -angular increments at its final spherical-angle output: 0.08 degrees per count on both axes at sensitivity 1, with -no time multiplier, curve or carry into later frames. The controller's persistent pitch/yaw move with the output. -Mouse input is removed from its stick path first, so the camera's later vertical signed-square curve and velocity -filters cannot process it a second time. Other camera modes receive input after the radial dead zone, unit-circle -clamp and stick response exponent; Ultrahand has a separate hook after its manipulation input's power curves. -The game's camera inversion and gamepad response remain in effect. `mouse_sens` scales raw counts; its scale -differs from the stick-emulation mode. Start at 1. +angular increments at its final spherical-angle output: 70/1500 = 0.0467 degrees per count on both axes at +sensitivity 1 (the on-foot camera's full-stick 70 degrees a second for 1500 counts a second; about 24.5 cm a turn at +800 DPI), with no time multiplier, curve or carry into later frames. The controller's persistent pitch/yaw move with +the output. Mouse input is removed from its stick path first, so the camera's later vertical signed-square curve and +velocity filters cannot process it a second time. Other camera modes receive input after the radial dead zone, +unit-circle clamp and stick response exponent, at the same degrees per count: x is 0.02 stick units a count over the +frame ratio (the stick turns yaw 70 x / 30 degrees a step), and y is the signed square root that the camera's +signed-square pitch (51 y |y| / 30 degrees a step) makes linear again, so pitch turns as far as yaw for the same +motion. `stick_sens` (which scales the game's stick turn rates) is divided out. While the player uses an ability +(their action verb UseSpecialPower), the ability camera turns the stored stick linearly at its own scales, yaw x 0.8 +and pitch 1.5 x 51 y; the mouse's part is scaled by 1 / 0.8 and 70 / (1.5 x 51) instead, so abilities turn as on foot. +The player's verbs are read every 50 ms from the player actor (the player root's GOT slot main+0x3969918, the +component array +0x228, its AS component +0x158 and the verbs of its five slots). Ultrahand has a separate hook after +its manipulation input's power curves; it gets the camera's stick value per count, at most three full tilts a step. +`mouse_aim_scale` (0.1 to 3, default 1) multiplies the mouse's motion while the bow is drawn (the verb UseBow or +DrawnBow with ZR held). The game's camera inversion and gamepad response remain in effect. `mouse_sens` scales raw +counts; its scale differs from the stick-emulation mode. Start at 1. The Controls tab switches between *Direct* and *Stick emulation* (`mouse_mode = stick`). The orbit camera keeps its desired pitch at the final angular output, including stationary frames, so a later game filter cannot recenter it. The persistent filter state accounts for the camera's current vertical offset instead of -accumulating corrections. A gamepad vertical input or the game's lock-on/override releases that mouse target. +accumulating corrections. A gamepad vertical input releases that mouse target to the game's own. The game's +lock-on, recenter and override (aiming) drive the camera themselves: the mouse stops holding without restoring its +earlier target, and when they end (or after the camera went 100 ms without a step) it holds the pitch the camera has +then, so the camera does not spring back to a pitch from before the aim or to the game's default after a shot. While L is held, mouse counts move the ability wheel's virtual cursor, which stays where it was when the mouse stops. The horizontal quick lists (D-pad up/left/right: materials, shields and weapons) use separate 32 ms right-stick steps, with at least 32 ms of neutral between them. At sensitivity 1, 32 horizontal counts request a step; a large flick diff --git a/runtime/cfg.c b/runtime/cfg.c index 2921dd08..b17e644c 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -44,7 +44,7 @@ static const char *const known[] = { "FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_TEST", - "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", + "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", @@ -84,7 +84,7 @@ static const struct { const char *key, *section; } sections[] = { {"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"}, {"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"}, {"STICK_SENS", "input"}, - {"MENU_CURSOR", "input"}, {"MOUSE_MODE", "input"}, {"MOUSE_SENS", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"}, + {"MENU_CURSOR", "input"}, {"MOUSE_MODE", "input"}, {"MOUSE_SENS", "input"}, {"MOUSE_AIM_SCALE", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"}, {"BIND_A", "input"}, {"BIND_B", "input"}, {"BIND_X", "input"}, {"BIND_Y", "input"}, {"BIND_LCLICK", "input"}, {"BIND_RCLICK", "input"}, {"BIND_L", "input"}, {"BIND_R", "input"}, {"BIND_ZL", "input"}, {"BIND_ZR", "input"}, {"BIND_PLUS", "input"}, {"BIND_MINUS", "input"}, {"BIND_LEFT", "input"}, {"BIND_UP", "input"}, @@ -149,7 +149,7 @@ static const CfgMeta metas[] = { {"STICK_DEADZONE", "8", "0..50", LIVE}, {"STICK_SENS", "1", "0.1..3", RESTART}, {"LSTICK_DEADZONE", "", "0..50", LIVE}, {"RSTICK_DEADZONE", "", "0..50", LIVE}, {"MOUSE_MODE", "direct", "stick|direct", LIVE}, - {"MOUSE_SENS", "1", "0..10", LIVE}, {"MENU_CURSOR", "1", "0|1", LIVE}, + {"MOUSE_SENS", "1", "0..10", LIVE}, {"MOUSE_AIM_SCALE", "1", "0.1..3", LIVE}, {"MENU_CURSOR", "1", "0|1", LIVE}, {"WALK_KEY", "none", NULL, LIVE}, {"WALK_SCALE", "0.51", "0.2..0.9", LIVE}, {"MENU_LANG", "", "en|ru|pt-BR|zh-Hans", LIVE}, {"AMIIBO_DIR", "", NULL, LIVE}, diff --git a/runtime/input.c b/runtime/input.c index 894253bb..c06db556 100644 --- a/runtime/input.c +++ b/runtime/input.c @@ -579,6 +579,8 @@ int input_pad_stick_percent(double pct[2]) { return ok; } +static uint64_t g_buttons_last; // the merged buttons of the last poll (input_buttons_last) + static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) { uint64_t b = 0; int32_t sl[2] = {0, 0}, sr[2] = {0, 0}; @@ -644,9 +646,12 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx if (overlay) menu_perf_cycle(); stick_bits(&b, sl[0], sl[1], B_SL_LEFT); stick_bits(&b, sr[0], sr[1], B_SR_LEFT); + __atomic_store_n(&g_buttons_last, b, __ATOMIC_RELAXED); *buttons = b; *lx = sl[0]; *ly = sl[1]; *rx = sr[0]; *ry = sr[1]; } +uint64_t input_buttons_last(void) { return __atomic_load_n(&g_buttons_last, __ATOMIC_RELAXED); } + // Text entry (swkbd_*.c): while it is open the game reads no input, as behind the keyboard applet; after it closes // the game reads none until every button and stick direction is released, so the press that closed it stays there. // g_block: 0 off, 1 text entry open, 2 closed, waiting for the release. The cursor is released while it is open. diff --git a/runtime/input.h b/runtime/input.h index 8de89e77..d27ad8e4 100644 --- a/runtime/input.h +++ b/runtime/input.h @@ -23,6 +23,8 @@ int input_direct_mouse_selector_active(void); // Consume direct mouse counts, including one-shot MOUSE events from an input script. enabled=0 discards scripted // counts while leaving the real mouse for menu input. Live counts require cursor capture and foreground focus. void input_direct_mouse_sample(int enabled, int32_t *dx, int32_t *dy); +// The buttons (B_* bits) of the last poll, keys and gamepad merged; any thread (mouse_direct.c: ZR for the bow's scale) +uint64_t input_buttons_last(void); // menu_cursor.c: the pointer in the game's menus (1.4.3). poll: once per pad read, adds a click's A or B; pointer: a // menu has the pointer, so the mouse camera's capture is released void menu_cursor_poll(uint64_t *buttons); diff --git a/runtime/mouse_direct.c b/runtime/mouse_direct.c index 979fa24b..2dc785a4 100644 --- a/runtime/mouse_direct.c +++ b/runtime/mouse_direct.c @@ -15,13 +15,35 @@ #include "cfg.h" #include "host.h" #include "input.h" +#include "input_map.h" #include "input_wsys.h" +#include "layout.h" #include "platform/platform.h" #if __has_include("patches.h") #include "patches.h" #endif +// One turn rate for every camera, as a PC game has: degrees per raw count at sensitivity 1. The on-foot camera turns +// 70 degrees a second at a full stick, so this is a full stick's turn for 1500 counts a second. The stick rates and the +// ability camera's scales below are those Weeping of the Empire's engine/runtime/pc_mouse.cpp works with. +#define DEG_PER_COUNT (70.0f / 1500.0f) +// The stick turns yaw 70 x / 30 degrees per step at the 30 Hz frame ratio, and pitch 51 y |y| / 30 (a signed square): +// a processed stick value of STICK_PER_COUNT a count turns yaw DEG_PER_COUNT, and the mouse's part of y is the square +// root of PITCH_GAIN |v| so pitch turns as many degrees a count as yaw, at any speed. +#define STICK_PER_COUNT (DEG_PER_COUNT * 30.0f / 70.0f) +#define PITCH_GAIN (70.0f / 51.0f) +// The ability camera (ExecutePlayerCameraSpecialPower) turns the stored stick its own way: yaw x * 0.8 of on foot's +// rate and pitch y * 1.5 * 51, linear (no signed square). The mouse's part is scaled back to on foot's degrees a count: +// x / 0.8, and pitch linear at 70 / (1.5 * 51). +#define ABILITY_GAIN_X (1.0f / 0.8f) +#define ABILITY_GAIN_Y (70.0f / (1.5f * 51.0f)) +// Ultrahand's held object moves by its post-curve input times its speed at a full tilt, as the camera turns by the +// stick: the mouse's part is the camera's stick value a count, so the object follows the hand as the camera would, +// at most HAND_MAX full tilts a step +#define HAND_PER_COUNT STICK_PER_COUNT +#define HAND_MAX 3.0f + static int direct_mode(void) { // cfg_value: unset is the settings table's default (direct), as the menu shows it return !strcmp(cfg_value("TOTK_MOUSE_MODE"), "direct") && rt_patch_enabled(rt_patch_set("mouse_direct")); @@ -97,9 +119,9 @@ static MouseSample sample; static PlatMutex sample_lock = PLAT_MUTEX_INIT; static _Thread_local struct { uint64_t object; MouseSample mouse; } orbit; typedef struct { - uint64_t object, generation, input; + uint64_t object, generation, input, seen; float pitch, original_target; - int hold_pitch, target_saved; + int hold_pitch, target_saved, resume; } OrbitTrack; static OrbitTrack orbit_seen[16]; @@ -134,6 +156,75 @@ static MouseSample sample_read(void) { } static float wrap_degrees(float a) { return remainderf(a, 360.0f); } +// ---- the player's state: using an ability, the bow in hand. The player's action verbs, read as Weeping of the +// Empire's engine/runtime/totk_player_state.cpp reads them, with its 1.4.3 placement (games/totk/patches/ +// pins_v143.json, fields_v143.json): the player root's GOT slot main+0x3969918 (1.0.0 +0x4562308), its link +0x20 +// (id +0x10, unit +8; the unit's id +0x48 and proc +0x40, the proc's state +0x24 not 7..11 while it is deleted), the +// actor's component array +0x228 (1.0.0 +0x220), its AS component +0x158, whose 5 slots (+0x1f0) hold the verb at +0x28 +// (+0x4c when +0x28 is Invalid). Every pointer is checked before it is followed; any miss reads as "nothing". +#define PLAYER_ROOT_SLOT 0x3969918ull +#define MAIN_IMAGE_SIZE 0x3adf000ull +enum { VERB_USE_BOW = 59, VERB_USE_SPECIAL_POWER = 61, VERB_DRAWN_BOW = 205, VERB_INVALID = 284 }; +enum { PLAYER_FOUND = 1, PLAYER_ABILITY = 2, PLAYER_BOW = 4 }; + +static int readable(uint64_t p, uint64_t n) { + return p >= 0x10000 && p < (1ull << 47) && !(p & 7) && plat_mem_readable(p, n); +} +static int32_t guest_s32(uint64_t at) { int32_t v; memcpy(&v, (void *)(uintptr_t)at, sizeof v); return v; } +static int game_object(uint64_t p, uint64_t n) { // readable, its vtable in main's image + if (!readable(p, n)) return 0; + uint64_t vt = guest_pointer(p); + return vt >= MOD_MAIN_BASE && vt < MOD_MAIN_BASE + MAIN_IMAGE_SIZE; +} + +static uint32_t player_state(void) { + uint64_t slot = MOD_MAIN_BASE + PLAYER_ROOT_SLOT; + if (!readable(slot, 8)) return 0; + uint64_t var = guest_pointer(slot); + if (!readable(var, 8)) return 0; + uint64_t root = guest_pointer(var); + if (!readable(root, 0x38)) return 0; + uint64_t link = root + 0x20, unit = guest_pointer(link + 8); + int32_t id = guest_s32(link + 0x10); + if (id == -1 || !readable(unit, 0x50) || guest_s32(unit + 0x48) != id) return 0; + uint64_t proc = guest_pointer(unit + 0x40); + if (!game_object(proc, 0x230) || (uint32_t)(guest_s32(proc + 0x24) - 7) < 5) return 0; + uint64_t comps = guest_pointer(proc + 0x228); + if (!readable(comps, 0x160)) return 0; + uint64_t as = guest_pointer(comps + 0x158); + if (!game_object(as, 0x1f0 + 5 * 8)) return 0; + uint32_t f = PLAYER_FOUND; + for (int i = 0; i < 5; i++) { + uint64_t s = guest_pointer(as + 0x1f0 + 8ull * i); + if (!readable(s, 0x50)) continue; + int32_t v = guest_s32(s + 0x28); + if (v == VERB_INVALID) v = guest_s32(s + 0x4c); + if (v == VERB_USE_SPECIAL_POWER) f |= PLAYER_ABILITY; + if (v == VERB_USE_BOW || v == VERB_DRAWN_BOW) f |= PLAYER_BOW; + } + return f; +} + +// The player's state for this camera step, read again every 50 ms (each read costs memory queries), and its changes in +// the log (the first reads, then each change, 40 lines) +static uint32_t player_state_logged(void) { + static uint32_t last = ~0u, f; + static uint64_t at; + static int lines; + uint64_t now = plat_now_ns(); + if (at && now - at < 50000000ull) return f; + at = now; + f = player_state(); + if (f != last && lines < 40) { + lines++; + rt_log("mouse: player %s%s%s", f & PLAYER_FOUND ? "found" : "not found", + f & PLAYER_ABILITY ? ", using an ability (ability camera gains)" : "", + f & PLAYER_BOW ? ", bow in hand" : ""); + } + last = f; + return f; +} + // main+0x191d78 builds the pitch target from +0x15c and +0x11c; the orbit update filters +0x108 toward it and adds // (1 - +0x88) * +0x10c at output. Updating only +0x108 makes the camera spring back to the unchanged target. static float orbit_pitch_offset(uint64_t object) { @@ -146,7 +237,33 @@ static void orbit_pitch_target(uint64_t object, float pitch) { static void orbit_pitch_release(OrbitTrack *track) { if (track->target_saved) guest_float_store(track->object + 0x15c, track->original_target); - track->hold_pitch = track->target_saved = 0; + track->hold_pitch = track->target_saved = track->resume = 0; +} + +// The pitch the camera has now (the filtered pitch plus its vertical offset), as orbit_angles leaves it +static float orbit_pitch_now(uint64_t object) { + return wrap_degrees(guest_float(object + 0x108) + orbit_pitch_offset(object)); +} + +// The camera stays where it is now: the mouse holds the pitch the camera has (orbit_begin writes its target), so +// neither the game's default target nor the mouse's target from before an aim or a lock-on pulls it back. +static void orbit_pitch_settle(OrbitTrack *track) { + track->pitch = orbit_pitch_now(track->object); + track->hold_pitch = 1, track->resume = 0; +} + +// The mouse's part of the processed stick for dx / dy counts at k stick units a count. On foot x is linear and y the +// signed square root that makes the camera's signed-square pitch linear again; the ability camera's linear scales are +// undone instead. +static void mouse_stick_add(int32_t dx, int32_t dy, float k, int ability, float *x, float *y) { + float v = -dy * k; + if (ability) { + *x += dx * k * ABILITY_GAIN_X; + *y += v * ABILITY_GAIN_Y; + } else { + *x += dx * k; + *y += copysignf(sqrtf(PITCH_GAIN * fabsf(v)), v); + } } // A host call at an instruction site, not an AAPCS64 function replacement. Preserve every guest register except @@ -167,6 +284,11 @@ static void camera_input(Cpu *c) { input_direct_mouse_sample(!input_direct_mouse_selector_active() && (accept || hand_active(plat_now_ns())), &dx, &dy); float sensitivity = (float)cfg_double("TOTK_MOUSE_SENS"); sensitivity = fminf(10, fmaxf(0, sensitivity)); + uint32_t player = (dx || dy) ? player_state_logged() : 0; + int ability = (player & PLAYER_ABILITY) != 0; + // The bow drawn (in hand with ZR held): mouse_aim_scale times the mouse's motion on every path + if ((player & PLAYER_BOW) && (input_buttons_last() & (1ull << B_ZR))) + sensitivity *= (float)fminf(3, fmaxf(0.1, cfg_double("TOTK_MOUSE_AIM_SCALE"))); // The game multiplies camera input by its 30 Hz frame ratio later. Divide by that exact ratio, rather // than wall-clock time: an equal distance of mouse motion then gives equal input at every frame rate. float ratio = 1; @@ -180,12 +302,14 @@ static void camera_input(Cpu *c) { float x, y; memcpy(&x, &c->v[9], sizeof x); memcpy(&y, &c->v[10], sizeof y); - float scale = 0.01f * sensitivity / ratio; - next.motion[0] = dx * scale * (flags & 2 ? -1 : 1); - next.motion[1] = -dy * scale * (flags & 4 ? -1 : 1); - next.angle[0] = -dx * 0.08f * sensitivity * (flags & 2 ? -1 : 1); - next.angle[1] = dy * 0.08f * sensitivity * (flags & 4 ? -1 : 1); - if (accept) { x += dx * scale; y -= dy * scale; } + float hand = HAND_PER_COUNT * sensitivity / ratio; + next.motion[0] = fminf(HAND_MAX, fmaxf(-HAND_MAX, dx * hand * (flags & 2 ? -1 : 1))); + next.motion[1] = fminf(HAND_MAX, fmaxf(-HAND_MAX, -dy * hand * (flags & 4 ? -1 : 1))); + next.angle[0] = -dx * DEG_PER_COUNT * sensitivity * (flags & 2 ? -1 : 1); + next.angle[1] = dy * DEG_PER_COUNT * sensitivity * (flags & 4 ? -1 : 1); + // stick_sens multiplies the game's stick turn rates (camera_speed.c): the mouse divides it out + if (accept) mouse_stick_add(dx, dy, STICK_PER_COUNT * sensitivity / (ratio * cam_speed_applied()), ability, + &x, &y); uint32_t xb, yb; memcpy(&xb, &x, sizeof xb); memcpy(&yb, &y, sizeof yb); @@ -225,8 +349,17 @@ static void orbit_begin(Cpu *c) { memcpy(&override, (void *)(uintptr_t)(input + 0x15c), 1); plat_mutex_lock(&sample_lock); OrbitTrack *track = orbit_track(object); - if (track->input != input) track->hold_pitch = track->target_saved = 0; - if (m.pad[1] != 0 || (held & 0x20) || (override & 0x40)) orbit_pitch_release(track); + if (track->input != input) track->hold_pitch = track->target_saved = track->resume = 0; + uint64_t now = plat_now_ns(); + // A gamepad's vertical input gives the pitch back to the game's own target. The game's lock-on or override + // (aiming) drives the camera itself: the mouse lets go without restoring the target it had before, and + // once that ends (or after this camera went 100 ms without a step) settles on the pitch the camera has + if (m.pad[1] != 0) orbit_pitch_release(track); + else if ((held & 0x20) || (override & 0x40)) { + if (track->hold_pitch) track->hold_pitch = 0, track->resume = 1; + } else if (track->resume || (track->hold_pitch && now - track->seen > 100000000ull)) + orbit_pitch_settle(track); + track->seen = now; take = moved || track->hold_pitch; if (take) track->input = input; if (track->hold_pitch) orbit_pitch_target(object, track->pitch); @@ -273,7 +406,8 @@ static void orbit_angles(Cpu *c) { static int logged; if (!logged) { logged = 1; - rt_log("mouse: free-camera angles directly, 0.08 degrees per count at sensitivity 1, equal X/Y"); + rt_log("mouse: free-camera angles directly, %.4f degrees per count at sensitivity 1, equal X/Y", + DEG_PER_COUNT); } } plat_mutex_unlock(&sample_lock); diff --git a/tests/mouse_direct_check.py b/tests/mouse_direct_check.py index 0dadee48..c5d2edaa 100644 --- a/tests/mouse_direct_check.py +++ b/tests/mouse_direct_check.py @@ -43,6 +43,9 @@ void rt_register_host(uint64_t addr, GuestFn fn) { void plat_mutex_lock(PlatMutex *m) { (void)m; } void plat_mutex_unlock(PlatMutex *m) { (void)m; } void rt_log(const char *fmt, ...) { (void)fmt; } +float cam_speed_applied(void) { return 1; } +uint64_t input_buttons_last(void) { return 0; } +int plat_mem_readable(uint64_t a, uint64_t n) { (void)a; (void)n; return 0; } // no player: on-foot gains static float scalar(__uint128_t v) { float f; memcpy(&f,&v,4); return f; } int main(void) { assert(hook && activity_hook); @@ -53,19 +56,29 @@ int main(void) { expected=c; raw_x=100; raw_y=-50; hook(&c); - assert(fabsf(scalar(c.v[9])-2)<1e-6f); - assert(fabsf(scalar(c.v[10])-1)<1e-6f); + // x is STICK_PER_COUNT a count over the frame ratio; y the signed square root the camera's signed-square pitch undoes + assert(fabsf(scalar(c.v[9])-100*STICK_PER_COUNT/ratio)<1e-5f); + assert(fabsf(scalar(c.v[10])-sqrtf(PITCH_GAIN*50*STICK_PER_COUNT/ratio))<1e-5f); float stored; memcpy(&stored,target+0x420,4); assert(stored==scalar(c.v[9])); expected.v[9]=c.v[9]; expected.v[10]=c.v[10]; assert(!memcmp(&expected,&c,sizeof c)); assert(input_direct_mouse_active()); // The next read sees no repeated movement. Gamepad/keyboard input is preserved. expected=c; hook(&c); assert(!memcmp(&expected,&c,sizeof c)); - // Equal mouse distance produces equal integrated input at 30, 60 and 144 Hz; there is no unit-circle clamp. + // Equal mouse distance turns the camera equally at 30, 60 and 144 Hz, yaw by x and pitch by the camera's signed + // square of y, and as many degrees a count on both axes; there is no unit-circle clamp. float rates[]={30,60,144}; for(int i=0;i<3;i++) { ratio=30/rates[i]; raw_x=10000; raw_y=10000; c.v[9]=c.v[10]=0; hook(&c); - assert(fabsf(scalar(c.v[9])*ratio-100)<0.001f); - assert(fabsf(scalar(c.v[10])*ratio+100)<0.001f); + float y=scalar(c.v[10]); + assert(fabsf(scalar(c.v[9])*ratio-10000*STICK_PER_COUNT)<0.001f); + assert(fabsf(y*fabsf(y)*ratio/PITCH_GAIN+10000*STICK_PER_COUNT)<0.001f); + } + // The ability camera turns the stored stick linearly at its own scales: they are undone instead of the square. + { + float x=0, y=0; mouse_stick_add(100,-50,0.04f,1,&x,&y); + assert(fabsf(x-100*0.04f*ABILITY_GAIN_X)<1e-5f && fabsf(y-50*0.04f*ABILITY_GAIN_Y)<1e-5f); + x=y=0; mouse_stick_add(100,-50,0.04f,0,&x,&y); + assert(fabsf(x-4)<1e-5f && fabsf(y*fabsf(y)-PITCH_GAIN*2)<1e-5f); } // One count is not discarded by a dead zone. ratio=0.5f; raw_x=1; raw_y=0; c.v[9]=c.v[10]=0; hook(&c); assert(scalar(c.v[9])>0); @@ -100,18 +113,18 @@ int main(void) { c.x[0]=(uintptr_t)camera; begin_hook(&c); assert(guest_float((uintptr_t)target+0x420)==0 && guest_float((uintptr_t)target+0x424)==0); c.x[20]=(uintptr_t)camera; c.sp=(uintptr_t)frame; angles_hook(&c); - assert(fabsf(guest_float((uintptr_t)frame+0x44)-26)<0.0001f); - assert(fabsf(guest_float((uintptr_t)frame+0x48)-32)<0.0001f); - assert(fabsf(guest_float((uintptr_t)camera+0x15c)-26)<0.0001f); + assert(fabsf(guest_float((uintptr_t)frame+0x44)-(30-50*DEG_PER_COUNT))<0.0001f); + assert(fabsf(guest_float((uintptr_t)frame+0x48)-(40-100*DEG_PER_COUNT))<0.0001f); + assert(fabsf(guest_float((uintptr_t)camera+0x15c)-(30-50*DEG_PER_COUNT))<0.0001f); c.x[0]=(uintptr_t)camera; begin_hook(&c); angles_hook(&c); - assert(fabsf(guest_float((uintptr_t)frame+0x48)-32)<0.0001f); // one sample cannot repeat next update + assert(fabsf(guest_float((uintptr_t)frame+0x48)-(40-100*DEG_PER_COUNT))<0.0001f); // one sample cannot repeat next update // The next input frame is stationary and the game's parameter update rewrites the old default target. // Restoring the mouse's desired target makes the camera filter stay at the new pitch, not spring to zero. c.x[20]=(uintptr_t)target; c.v[9]=c.v[10]=0; raw_x=raw_y=0; hook(&c); guest_float_store((uintptr_t)camera+0x15c,0); c.x[0]=(uintptr_t)camera; begin_hook(&c); float current=guest_float((uintptr_t)camera+0x108), goal=guest_float((uintptr_t)camera+0x15c); - assert(fabsf(goal-26)<0.0001f && fabsf((current+goal)*0.5f-26)<0.0001f); + assert(fabsf(goal-(30-50*DEG_PER_COUNT))<0.0001f && fabsf((current+goal)*0.5f-(30-50*DEG_PER_COUNT))<0.0001f); // A later game filter overwrites the pitch after the begin hook. Neither a stationary frame nor the next // tiny mouse delta may integrate from that recentered result. guest_float_store((uintptr_t)camera+0x108,0); @@ -120,18 +133,18 @@ int main(void) { guest_float_store((uintptr_t)camera+0x88,0.3f); guest_float_store((uintptr_t)frame+0x44,0); c.x[20]=(uintptr_t)camera; angles_hook(&c); - assert(fabsf(guest_float((uintptr_t)frame+0x44)-26)<0.0001f); - assert(fabsf(guest_float((uintptr_t)camera+0x15c)-7)<0.0001f); - assert(fabsf(guest_float((uintptr_t)camera+0x108)-12)<0.0001f); + assert(fabsf(guest_float((uintptr_t)frame+0x44)-(30-50*DEG_PER_COUNT))<0.0001f); + assert(fabsf(guest_float((uintptr_t)camera+0x15c)-((30-50*DEG_PER_COUNT)-19))<0.0001f); + assert(fabsf(guest_float((uintptr_t)camera+0x108)-((30-50*DEG_PER_COUNT)-14))<0.0001f); for(int i=0;i<100;i++) { guest_float_store((uintptr_t)frame+0x44,0); angles_hook(&c); - assert(fabsf(guest_float((uintptr_t)camera+0x108)-12)<0.0001f); + assert(fabsf(guest_float((uintptr_t)camera+0x108)-((30-50*DEG_PER_COUNT)-14))<0.0001f); } c.x[20]=(uintptr_t)target; c.v[9]=c.v[10]=0; raw_y=1; hook(&c); c.x[0]=(uintptr_t)camera; begin_hook(&c); guest_float_store((uintptr_t)frame+0x44,0); c.x[20]=(uintptr_t)camera; angles_hook(&c); - assert(fabsf(guest_float((uintptr_t)frame+0x44)-26.08f)<0.0001f); + assert(fabsf(guest_float((uintptr_t)frame+0x44)-((30-50*DEG_PER_COUNT)+DEG_PER_COUNT))<0.0001f); // An explicit controller recenter still hands pitch back to the game. uint64_t recenter=0x20; memcpy(target+0x148,&recenter,8); c.x[20]=(uintptr_t)target; c.v[9]=c.v[10]=0; hook(&c); @@ -140,6 +153,19 @@ int main(void) { c.x[20]=(uintptr_t)camera; angles_hook(&c); assert(guest_float((uintptr_t)frame+0x44)==5); memset(target+0x148,0,8); + // When the game lets go (the recenter or an aim ends), the mouse holds the pitch the camera has then: neither its + // target from before nor the game's default target pulls the camera back. + guest_float_store((uintptr_t)camera+0x108,3); // the game left the pitch at 3 + the offset 14 + c.x[20]=(uintptr_t)target; c.v[9]=c.v[10]=0; hook(&c); + c.x[0]=(uintptr_t)camera; begin_hook(&c); + assert(fabsf(guest_float((uintptr_t)camera+0x15c)-(17-14-5))<0.0001f); + guest_float_store((uintptr_t)frame+0x44,0); + c.x[20]=(uintptr_t)camera; angles_hook(&c); + assert(fabsf(guest_float((uintptr_t)frame+0x44)-17)<0.0001f); + guest_float_store((uintptr_t)camera+0x15c,0); // the game's parameter update writes its default target + c.x[20]=(uintptr_t)target; hook(&c); + c.x[0]=(uintptr_t)camera; begin_hook(&c); + assert(fabsf(guest_float((uintptr_t)camera+0x15c)-(17-14-5))<0.0001f); memset(camera+0x88,0,4); memset(camera+0x10c,0,4); memset(camera+0x11c,0,4); } // A gamepad-only step (no mouse motion, no pitch hold) leaves the camera input and the angles as the game made them. @@ -163,14 +189,15 @@ int main(void) { guest_float_store((uintptr_t)frame+0x48,guest_float((uintptr_t)camera+0x120)); angles_hook(&c); } - assert(fabsf(guest_float((uintptr_t)camera+0x120)-32)<0.001f); - // A held object receives mouse deltas after its own curves, once per sample, including with camera input off. + assert(fabsf(guest_float((uintptr_t)camera+0x120)-(40-100*DEG_PER_COUNT))<0.001f); + // A held object receives mouse deltas after its own curves at the camera's stick rate, at most HAND_MAX full + // tilts a step, once per sample, including with camera input off. c.x[20]=(uintptr_t)target; c.v[9]=c.v[10]=0; raw_x=100; raw_y=50; ratio=0.5f; hook(&c); c.x[19]=(uintptr_t)hand; c.x[8]=0x12345678; c.v[15]=0; guest_float_store((uintptr_t)frame+0x60,0); hand_hook(&c); - assert(fabsf(guest_float((uintptr_t)frame+0x60)-2)<0.00001f); - assert(fabsf(scalar(c.v[15])+1)<0.00001f); - hand_hook(&c); assert(fabsf(scalar(c.v[15])+1)<0.00001f); + assert(100*HAND_PER_COUNT/ratio>HAND_MAX && fabsf(guest_float((uintptr_t)frame+0x60)-HAND_MAX)<0.00001f); + assert(fabsf(scalar(c.v[15])+50*HAND_PER_COUNT/ratio)<0.00001f); + hand_hook(&c); assert(fabsf(scalar(c.v[15])+50*HAND_PER_COUNT/ratio)<0.00001f); c.x[21]=1; raw_x=5; raw_y=6; hook(&c); assert(raw_x==0 && raw_y==0 && input_direct_mouse_active()); // A radial selector retains its virtual cursor between mouse packets, including repeated HID polls. float cursor[2]={0}; int32_t selector[2], held_selector[2]; @@ -205,6 +232,7 @@ int main(void) { puts("direct mouse: final orbit angles, single-count motion, equal axes and held-object routing passed"); puts("direct mouse: persistent pitch target, radial cursor hold and gamepad-only steps untouched passed"); puts("direct mouse: final-pitch recenter rejection, explicit handoff and bounded quick-list steps passed"); + puts("direct mouse: one turn rate, the pitch's square undone, ability gains and the hold after a handoff passed"); } ''' @@ -291,6 +319,7 @@ def main(): with tempfile.TemporaryDirectory(prefix='totk-mouse-') as tmp: folder = Path(tmp) (folder / 'patches.h').write_text('#define RT_PATCH_PAGE 0x7003adf000ull\n') + (folder / 'layout.h').write_text('#define MOD_MAIN_BASE 0x7000000000ull\n') source = folder / 'check.c' source.write_text(HARNESS) exe = folder / ('check.exe' if os.name == 'nt' else 'check') diff --git a/totk.ini.example b/totk.ini.example index bad5072c..d3158e4e 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -308,8 +308,12 @@ # menu has the game's focus box the pointer shows, the item under it takes the focus, a left click is A on it and a # right click is B. 0 leaves the menus to the keys and the pad. Changes while the game runs. # menu_cursor = 1 -# Mouse camera sensitivity (0 to 10; 0 = no mouse camera and no cursor capture). +# Mouse camera sensitivity (0 to 10; 0 = no mouse camera and no cursor capture). With mouse_mode = direct, 1 turns the +# camera 0.0467 degrees per count, the same in every camera mode (about 24.5 cm a turn at 800 DPI). # mouse_sens = 1 +# The mouse's camera motion times this while the bow is drawn (0.1 to 3; mouse_mode = direct, game 1.4.3). The game's +# bow camera already turns slower than on foot; this changes the mouse on top of that. Changes while the game runs. +# mouse_aim_scale = 1 # The keyboard's sticks are all or nothing, so the game only sees a full tilt from them; while this key is held the # keyboard-driven stick values shrink and the keyboard can walk instead of run. Key names as in the bind_ lines below, # none turns it off. 0.2 to 0.9, the default holds for anything else. Only the keyboard is scaled: the mouse camera and