From 32166dfe7d1b5bcda6126b822f4069c4d433d694 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Thu, 14 May 2026 13:10:05 -0500 Subject: [PATCH] corrected _LOOKING. now uses the same latch logic as _TURNING --- src/Offsets.h | 18 +++++++++ src/debug/Probe.cpp | 22 ++++++++++- src/player/InputEvents.cpp | 75 ++++++++++++++++++++++++++++---------- 3 files changed, 94 insertions(+), 21 deletions(-) diff --git a/src/Offsets.h b/src/Offsets.h index 7cb795aa..e3d4e8c4 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -1736,6 +1736,24 @@ enum Offsets { // rotation (vs the mouselook button merely being held). OFF_PLAYER_BODY_YAW = 0x9C4, + // Camera object pointer chain. The game-state singleton lives + // at `*0x00B4B2BC`; the camera object hangs off it at `+0x65B8`. + // Verified via `Script_CameraZoomIn` (`0x0050B400`) which loads + // this chain to call `FUN_0050FC60` on the camera. Returns the + // camera struct or `nullptr` if the game state isn't yet + // initialized (pre-login). + VAR_GAME_STATE_PTR = 0x00B4B2BC, + OFF_GAME_STATE_CAMERA_PTR = 0x65B8, + + // Camera yaw, relative to the character (radians). `0` = + // camera-behind-character default. Accumulates as the user + // LMB-orbits; stays at `0` during RMB-mouselook (camera rotates + // *with* the character, so relative offset doesn't change). + // Verified empirically: clean 180° LMB orbit moves this from + // `0` to `~π`. Mirrored at `+0x210` and `+0x214` (smoothing / + // last-applied buffers); we read the primary at `+0xF0`. + OFF_CAMERA_RELATIVE_YAW = 0xF0, + // UI input controller — heap-allocated struct holding the // engine's master input-state bitfield (mouselook / free-look / // turn-key / autorun / strafe-modifier / etc.). The slot at diff --git a/src/debug/Probe.cpp b/src/debug/Probe.cpp index 0439c0f4..c8d99197 100644 --- a/src/debug/Probe.cpp +++ b/src/debug/Probe.cpp @@ -91,6 +91,17 @@ const uint8_t *ResolvePlayerExtra() { player + Offsets::OFF_CGPLAYER_INFO); } +// Camera object pointer chain — `[*0x00B4B2BC + 0x65B8]`. Verified +// via `Script_CameraZoomIn` (`0x0050B400`) which loads the same +// chain to call `FUN_0050FC60` on the camera. The camera's body +// has fields like `+0x128` (flags) and `+0x15C` (zoom). +const uint8_t *ResolveCamera() { + const uint8_t *gameState = *reinterpret_cast(0x00B4B2BC); + if (gameState == nullptr) + return nullptr; + return *reinterpret_cast(gameState + 0x65B8); +} + // Writes non-zero u32 entries in [start, start+len) into `out` as // space-separated `0xOFFSET=0xVALUE` pairs. Returns the length // written (excluding the nul terminator). Output is truncated if it @@ -192,6 +203,7 @@ struct Snap { Snap descSnap; Snap playerSnap; Snap extraSnap; +Snap cameraSnap; void TakeSnap(Snap &s, const uint8_t *base, int start, int len) { s.valid = false; @@ -257,11 +269,12 @@ int __fastcall Script_SnapAll(void *) { TakeSnap(descSnap, ResolvePlayerDescriptor(), 0, 0x400); TakeSnap(playerSnap, ResolvePlayer(), 0, 0x2000); TakeSnap(extraSnap, ResolvePlayerExtra(), 0, 0x2000); + TakeSnap(cameraSnap, ResolveCamera(), 0, 0x800); return 0; } int __fastcall Script_DiffAll(void *L) { - static char buf[3500]; + static char buf[4500]; int pos = 0; pos += FormatDiff(descSnap, ResolvePlayerDescriptor(), "DESC", buf + pos, sizeof(buf) - pos); @@ -269,6 +282,8 @@ int __fastcall Script_DiffAll(void *L) { buf + pos, sizeof(buf) - pos); pos += FormatDiff(extraSnap, ResolvePlayerExtra(), "EXTRA", buf + pos, sizeof(buf) - pos); + pos += FormatDiff(cameraSnap, ResolveCamera(), "CAMERA", + buf + pos, sizeof(buf) - pos); if (pos >= 0 && pos < static_cast(sizeof(buf))) buf[pos] = '\0'; else @@ -297,6 +312,11 @@ int __fastcall Script_DiffAllLog(void *L) { } pos += FormatDiff(extraSnap, ResolvePlayerExtra(), "EXTRA", buf + pos, sizeof(buf) - pos); + if (pos < static_cast(sizeof(buf)) - 2) { + buf[pos++] = '\n'; + } + pos += FormatDiff(cameraSnap, ResolveCamera(), "CAMERA", + buf + pos, sizeof(buf) - pos); if (pos >= 0 && pos < static_cast(sizeof(buf))) buf[pos] = '\0'; else diff --git a/src/player/InputEvents.cpp b/src/player/InputEvents.cpp index 84acc1bd..c2f5143c 100644 --- a/src/player/InputEvents.cpp +++ b/src/player/InputEvents.cpp @@ -26,12 +26,10 @@ // on it alone would over-fire on every click. // // PLAYER_STARTED_LOOKING / STOPPED_LOOKING -// Source: UI-input free-look bit (LMB-drag). Currently -// bit-based (fires on click, not on actual camera move) — -// a known divergence from retail. The camera-yaw global -// lives outside CGPlayer; finding its address is a TODO. -// Addons that want raw click detection should use -// `GLOBAL_MOUSE_DOWN`/`UP` for now. +// Source: same latch model as TURNING, but driven by the +// camera-relative yaw at `[camera + 0xF0]`. STARTED fires when +// the free-look bit is held AND the camera yaw changes; STOPPED +// fires on LMB release. #include "Offsets.h" #include "event/Custom.h" @@ -62,7 +60,9 @@ bool g_wasLooking = false; bool g_wasTurning = false; float g_prevBodyYaw = 0.0f; -bool g_havePrevYaw = false; +bool g_havePrevBodyYaw = false; +float g_prevCameraYaw = 0.0f; +bool g_havePrevCameraYaw = false; using ResolveUnitToken_t = void *(__fastcall *)(const char *token); @@ -75,6 +75,15 @@ const uint8_t *Player() { return static_cast(fn("player")); } +const uint8_t *Camera() { + const uint8_t *gameState = *reinterpret_cast( + Offsets::VAR_GAME_STATE_PTR); + if (gameState == nullptr) + return nullptr; + return *reinterpret_cast( + gameState + Offsets::OFF_GAME_STATE_CAMERA_PTR); +} + void FireTransition(bool now, bool &prev, const char *startedName, const char *stoppedName) { if (now == prev) @@ -88,44 +97,70 @@ void FireTransition(bool now, bool &prev, const char *startedName, void OnWorldTick() { auto *ctrl = Controller(); auto *player = Player(); + auto *camera = Camera(); if (ctrl == nullptr || player == nullptr) { // Pre-login / teardown — reset so first valid tick doesn't // fire spurious STOPPED transitions. g_wasMoving = false; g_wasLooking = false; g_wasTurning = false; - g_havePrevYaw = false; + g_havePrevBodyYaw = false; + g_havePrevCameraYaw = false; return; } const uint32_t flags = *reinterpret_cast( ctrl + Offsets::OFF_UI_INPUT_FLAGS); const bool moving = (flags & Offsets::INPUT_FLAGS_MOVING_ANY) != 0; - const bool looking = (flags & Offsets::INPUT_FLAG_FREE_LOOK) != 0; + const bool freeLookHeld = (flags & Offsets::INPUT_FLAG_FREE_LOOK) != 0; const bool mouselookHeld = (flags & Offsets::INPUT_FLAG_MOUSELOOK) != 0; - // TURNING is a latched bit-AND-rotation signal: - // STARTED fires when the mouselook bit is held AND the body - // yaw changes (the first actual drag after pressing RMB). + // TURNING — latched bit-AND-rotation signal: + // STARTED fires when mouselook bit is held AND the body yaw + // changes (the first actual drag after pressing RMB). // STOPPED fires when the mouselook bit clears (RMB release). // Once STARTED has fired during a hold, the latch stays on // until the bit clears — so a drag-stop-drag motion within // the same RMB hold doesn't flap STARTED/STOPPED. - const float yaw = *reinterpret_cast( + const float bodyYaw = *reinterpret_cast( player + Offsets::OFF_PLAYER_BODY_YAW); bool turning; if (!mouselookHeld) { turning = false; } else if (g_wasTurning) { - turning = true; // latched on for the rest of this hold - } else if (g_havePrevYaw && - std::fabs(yaw - g_prevBodyYaw) > YAW_EPSILON) { - turning = true; // first rotation within this RMB hold + turning = true; + } else if (g_havePrevBodyYaw && + std::fabs(bodyYaw - g_prevBodyYaw) > YAW_EPSILON) { + turning = true; } else { - turning = false; // bit held but no drag yet — waiting + turning = false; + } + g_prevBodyYaw = bodyYaw; + g_havePrevBodyYaw = true; + + // LOOKING — same latch model, but driven by camera-relative yaw + // at `[camera + 0xF0]`. That field stays at 0 during RMB- + // mouselook (camera rotates *with* the character), so this + // doesn't double-fire when both buttons are involved. + bool looking; + if (!freeLookHeld) { + looking = false; + } else if (camera == nullptr) { + looking = false; // can't read camera yaw — bail + } else { + const float cameraYaw = *reinterpret_cast( + camera + Offsets::OFF_CAMERA_RELATIVE_YAW); + if (g_wasLooking) { + looking = true; + } else if (g_havePrevCameraYaw && + std::fabs(cameraYaw - g_prevCameraYaw) > YAW_EPSILON) { + looking = true; + } else { + looking = false; + } + g_prevCameraYaw = cameraYaw; + g_havePrevCameraYaw = true; } - g_prevBodyYaw = yaw; - g_havePrevYaw = true; FireTransition(moving, g_wasMoving, kStartedMovingEvent, kStoppedMovingEvent); FireTransition(turning, g_wasTurning, kStartedTurningEvent, kStoppedTurningEvent);