From 96a4edaaa153c06574f71141080bcfb09714c8d1 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Thu, 14 May 2026 12:52:22 -0500 Subject: [PATCH] Fixed _TURNING and added GLOBAL_MOUSE_ --- README.md | 2 + docs/API.md | 140 +++++++++++++++++++---------------- src/Offsets.h | 28 +++++++ src/event/Custom.cpp | 9 +++ src/event/Custom.h | 5 ++ src/input/GlobalMouse.cpp | 95 ++++++++++++++++++++++++ src/player/InputEvents.cpp | 145 +++++++++++++++++++++++++++++++++++++ src/player/MouseLook.cpp | 83 --------------------- src/player/Movement.cpp | 87 ---------------------- 9 files changed, 362 insertions(+), 232 deletions(-) create mode 100644 src/input/GlobalMouse.cpp create mode 100644 src/player/InputEvents.cpp delete mode 100644 src/player/MouseLook.cpp delete mode 100644 src/player/Movement.cpp diff --git a/README.md b/README.md index 8dd55a0f..ac343e2e 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ functions, just behavior the stock 1.12 engine didn't have. See the | `PLAYER_STOPPED_LOOKING` | *(none)* | | `PLAYER_STARTED_TURNING` | *(none)* | | `PLAYER_STOPPED_TURNING` | *(none)* | +| `GLOBAL_MOUSE_DOWN` | `button` | +| `GLOBAL_MOUSE_UP` | `button` | | `FACTION_STANDING_CHANGED` | `factionID, newStanding, repGained` | | `ITEM_DATA_LOAD_RESULT` | `itemID, success` | | `MODIFIER_STATE_CHANGED` | `keyName, down` | diff --git a/docs/API.md b/docs/API.md index e2f26562..f7c5211d 100644 --- a/docs/API.md +++ b/docs/API.md @@ -174,9 +174,8 @@ build instructions. - [Events](#events) - [`C_EventUtils.IsEventValid(eventName)`](#c_eventutilsiseventvalideventname) - [`BAG_UPDATE_DELAYED` event](#bag_update_delayed-event) - - [`PLAYER_STARTED_MOVING` / `PLAYER_STOPPED_MOVING` events](#player_started_moving--player_stopped_moving-events) - - [`PLAYER_STARTED_LOOKING` / `PLAYER_STOPPED_LOOKING` events](#player_started_looking--player_stopped_looking-events) - - [`PLAYER_STARTED_TURNING` / `PLAYER_STOPPED_TURNING` events](#player_started_turning--player_stopped_turning-events) + - [Player input-state events (`PLAYER_STARTED_MOVING` / `LOOKING` / `TURNING` + `STOPPED_*`)](#player-input-state-events) + - [`GLOBAL_MOUSE_DOWN` / `GLOBAL_MOUSE_UP` events](#global_mouse_down--global_mouse_up-events) - [Globals](#globals) - [`CLASSIC_API_VERSION`](#classic_api_version) - [`LE_EXPANSION_*`](#le_expansion_) @@ -4176,75 +4175,92 @@ event handlers. > currently won't trigger `BAG_UPDATE_DELAYED`. Player bag (0..4) > and bank (5..10) updates work normally — the 95% case. -### `PLAYER_STARTED_MOVING` / `PLAYER_STOPPED_MOVING` events +### Player input-state events -Fires (with no payload) on the transition where the player begins or -ends translational movement, matching the modern events of the same -name. Vanilla 1.12 doesn't expose any movement-state event — addons -that need to know "is the player moving" otherwise poll -`GetUnitSpeed("player")` or `IsFalling` on every `OnUpdate`. +Three pairs of zero-payload events, all driven off the same +UI-input controller flag word (`*0x00BE1148 + 0x04`) — the master +input bitfield the engine updates on every mouse-button / movement- +key press and release. One per-frame `Tick::WorldTick` callback +reads the word once and fires whichever pairs transitioned. + +| Event | Fires on | Source | +|-------|----------|--------| +| `PLAYER_STARTED_MOVING` / `PLAYER_STOPPED_MOVING` | WASD or autorun toggle | UI-input controller flags (`0x10` W \| `0x20` S \| `0x40` A \| `0x80` D \| `0x1000` autorun) | +| `PLAYER_STARTED_TURNING` / `PLAYER_STOPPED_TURNING` | Character body actually rotating (RMB-drag, `MouselookStart`, etc.) | Per-frame delta on `CGPlayer + 0x9C4` (body yaw, radians) | +| `PLAYER_STARTED_LOOKING` / `PLAYER_STOPPED_LOOKING` | LMB free-look bit (camera-only rotation) | UI-input controller bit `0x02` | ```lua local f = CreateFrame("Frame") -f:RegisterEvent("PLAYER_STARTED_MOVING") -f:RegisterEvent("PLAYER_STOPPED_MOVING") -f:SetScript("OnEvent", function() - if event == "PLAYER_STARTED_MOVING" then ... end -end) -``` - -What counts as "moving" matches modern's behavior: - -| Bit | Counts? | Note | -|-----|---------|------| -| `FORWARD` / `BACKWARD` / `STRAFE_LEFT` / `STRAFE_RIGHT` | yes | W/S/A/D press, mouse-click move | -| `JUMPING` / `FALLING` / `FALLING_FAR` | yes | spacebar, walking off a ledge | -| `TURN_LEFT` / `TURN_RIGHT` (turn-in-place) | no | matches modern (turning isn't moving) | -| `WALK_MODE` (toggle), `ONTRANSPORT`, `LEVITATING`, `SWIMMING` alone | no | protocol-level flags, not active motion | - -Implementation: subscribes to the shared `Tick::WorldTick` per-frame -registry (single `MinHook` on `FUN_0066FD50` shared with -`BAG_UPDATE_DELAYED`) and edge-detects on the moving-bits subset of -the player's movement-flags word at `CGPlayer + 0x9E8`. State resets -between zones — if the player pointer is unresolvable (loading -screen, character select), the next valid tick won't fire a stale -`STOPPED_MOVING`. - -### `PLAYER_STARTED_LOOKING` / `PLAYER_STOPPED_LOOKING` events - -Fires when free-look mode (LMB-held mouse drag) starts and stops. -Free-look rotates the camera around the character without turning -the character's body — the same mode mouse-look addons use for -"look around without moving". - -### `PLAYER_STARTED_TURNING` / `PLAYER_STOPPED_TURNING` events - -Fires when mouselook mode (RMB-held mouse drag, or programmatic -`MouselookStart`) starts and stops. Mouselook turns the character -to face whatever direction the mouse is dragged toward — the camera -follows the character's facing. - -```lua -local f = CreateFrame("Frame") -for _, ev in ipairs({"PLAYER_STARTED_LOOKING", "PLAYER_STOPPED_LOOKING", - "PLAYER_STARTED_TURNING", "PLAYER_STOPPED_TURNING"}) do +for _, ev in ipairs({ + "PLAYER_STARTED_MOVING", "PLAYER_STOPPED_MOVING", + "PLAYER_STARTED_LOOKING", "PLAYER_STOPPED_LOOKING", + "PLAYER_STARTED_TURNING", "PLAYER_STOPPED_TURNING", +}) do f:RegisterEvent(ev) end -f:SetScript("OnEvent", function(_, event) - print(event) +f:SetScript("OnEvent", function(_, event) print(event) end) +``` + +**Key-state semantics for MOVING**: `STOPPED_MOVING` fires the +instant the movement keys release, even if the character is still +airborne from a jump. Matches retail (verified empirically). For +an "is the character actually displacing this frame" signal, use +`GetUnitSpeed("player")` and watch the first return. + +**TURNING fires on actual rotation** (per-frame delta on the +body-yaw float at `CGPlayer + 0x9C4`), not merely on the +mouselook bit being held — matches retail. The mouselook bit +stays set the whole time RMB is held even without drag; gating on +it alone would over-fire on bare clicks. + +**LOOKING is still bit-based** — fires on LMB press, not on actual +camera-yaw change. Known divergence from retail. The camera-yaw +global lives outside CGPlayer; finding it is a TODO. Addons that +want the raw click signal should use `GLOBAL_MOUSE_DOWN` / +`GLOBAL_MOUSE_UP` below. + +Implementation: single subscriber on the shared `Tick::WorldTick` +registry (same `MinHook` on `FUN_0066FD50` shared with +`BAG_UPDATE_DELAYED`). When the player or input controller is +unresolvable (pre-login, loading screen, character select), all +states reset so the next valid tick doesn't fire spurious STOPPED +transitions. + +### `GLOBAL_MOUSE_DOWN` / `GLOBAL_MOUSE_UP` events + +Fires on every raw mouse-button press or release while WoW has +focus. Payload is the button identifier string — matches modern +WoW's signature exactly. + +```lua +local f = CreateFrame("Frame") +f:RegisterEvent("GLOBAL_MOUSE_DOWN") +f:RegisterEvent("GLOBAL_MOUSE_UP") +f:SetScript("OnEvent", function(_, event, button) + print(event, button) -- "GLOBAL_MOUSE_DOWN", "LeftButton" end) ``` -Both pairs are edge-detected per-frame off the same UI-input -controller (`*0x00BE1148 + 0x04`) that `Script_IsMouselooking` -reads — bit `0x01` is mouselook (`TURNING`), bit `0x02` is free-look -(`LOOKING`). Same `Tick::WorldTick` subscription as -`PLAYER_STARTED_MOVING`; no extra hook overhead. +| Payload | Source | +|---------|--------| +| `"LeftButton"` | `VK_LBUTTON` | +| `"RightButton"` | `VK_RBUTTON` | +| `"MiddleButton"` | `VK_MBUTTON` | +| `"Button4"` | `VK_XBUTTON1` | +| `"Button5"` | `VK_XBUTTON2` | -**Mouse-only**, matching modern WoW. Keyboard turn-in-place -(`TurnLeftStart` / `TurnRightStart`, bits 8-9 of the same flag -word) does NOT fire `TURNING` on retail — verified empirically — -so we don't either. +Distinct from per-frame `OnMouseDown` scripts — these fire even +when the click misses every UI frame (clicks in the world / on +nothing). Distinct from `PLAYER_STARTED_LOOKING` / +`PLAYER_STARTED_TURNING` — those are camera-rotation events; +these are raw input events. + +**Focus-gated.** Polls Win32 `GetAsyncKeyState` per frame on the +shared `Tick::WorldTick` registry. DOWN transitions are only +honored when WoW is the foreground window — clicking in another +app while alt-tabbed doesn't fire. UP transitions always fire, +even if the user alt-tabbed mid-click, so an addon never gets +left in a "button is held" state. ## Globals diff --git a/src/Offsets.h b/src/Offsets.h index f4442094..7cb795aa 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -1727,6 +1727,15 @@ enum Offsets { VAR_REALM_INFO_PTR = 0x00C28130, OFF_REALM_INFO_NAME = 0x20, + // Player body yaw (orientation in radians, 0..2π) — the + // character's facing direction in the world. Inline float on + // CGPlayer; mirrored at `+0x9F8` (current vs last-broadcast). + // Verified empirically: a clean 180° RMB-drag swings this by + // ~π rad, and LMB-only camera orbits leave it untouched. + // Used by `PLAYER_STARTED_TURNING` to detect actual character + // rotation (vs the mouselook button merely being held). + OFF_PLAYER_BODY_YAW = 0x9C4, + // 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 @@ -1746,6 +1755,25 @@ enum Offsets { // Bit 1 — set when the user is in free-look mode (LMB-drag). // Camera-only rotation; character doesn't turn. INPUT_FLAG_FREE_LOOK = 0x02, + // Bits 4-7 — WASD movement-key state. Each `MoveForwardStart` + // / `StrafeLeftStart` / etc. pushes one of these as the bit + // mask into the engine's button-press handler. Bit `0x1000` is + // the autorun-active flag (set by `ToggleAutoRun` and the + // both-mouse-buttons combo). `INPUT_FLAGS_MOVING_ANY` is the + // engine's own "is the user currently inputting translational + // movement" mask, used to drive `PLAYER_STARTED_MOVING` — + // matches modern's "STOPPED fires on key release, even + // mid-air" semantics because no physics/airborne bit is + // involved. + INPUT_FLAG_MOVE_FORWARD = 0x10, + INPUT_FLAG_MOVE_BACKWARD = 0x20, + INPUT_FLAG_STRAFE_LEFT = 0x40, + INPUT_FLAG_STRAFE_RIGHT = 0x80, + INPUT_FLAG_AUTORUN = 0x1000, + INPUT_FLAGS_MOVING_ANY = + INPUT_FLAG_MOVE_FORWARD | INPUT_FLAG_MOVE_BACKWARD | + INPUT_FLAG_STRAFE_LEFT | INPUT_FLAG_STRAFE_RIGHT | + INPUT_FLAG_AUTORUN, // CGUnit -> MovementInfo pointer. Holds the unit's per-instance // position / orientation / movement-flags / per-direction-speed diff --git a/src/event/Custom.cpp b/src/event/Custom.cpp index ba1fcf14..8cf561cd 100644 --- a/src/event/Custom.cpp +++ b/src/event/Custom.cpp @@ -163,6 +163,15 @@ void Fire_SD(int eventID, const char *arg1, int arg2) { fn(eventID, "%s%d", arg1, arg2); } +void Fire_S(int eventID, const char *arg1) { + if (eventID < 0) + return; + using FireEvent_S_t = void(__cdecl *)(int eventID, const char *format, + const char *a); + auto fn = reinterpret_cast(Offsets::FUN_FIRE_EVENT); + fn(eventID, "%s", arg1); +} + // Diagnostic Lua functions — registered via the auto-register pattern // at the bottom of this file. Not on any hot path. namespace { diff --git a/src/event/Custom.h b/src/event/Custom.h index a70f6143..1f9a9ded 100644 --- a/src/event/Custom.h +++ b/src/event/Custom.h @@ -70,6 +70,11 @@ void Fire_DDD(int eventID, int arg1, int arg2, int arg3); // literals this is automatic. void Fire_SD(int eventID, const char *arg1, int arg2); +// Single string arg (format `"%s"`). Same lifetime caveat as +// `Fire_SD` — used by `GLOBAL_MOUSE_DOWN` / `GLOBAL_MOUSE_UP` for +// `(button)` payloads. +void Fire_S(int eventID, const char *arg1); + // Internal: try to claim a slot for every reservation that's still // unclaimed (`slot < 0`). Called from the `Frame::RegisterEvent` hook // in DllMain — every time Lua calls `frame:RegisterEvent(...)`, we diff --git a/src/input/GlobalMouse.cpp b/src/input/GlobalMouse.cpp new file mode 100644 index 00000000..99992d10 --- /dev/null +++ b/src/input/GlobalMouse.cpp @@ -0,0 +1,95 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU Lesser General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU Lesser General Public License for more details. + +// `GLOBAL_MOUSE_DOWN` / `GLOBAL_MOUSE_UP` — modern WoW fires these +// on every raw mouse-button press/release, with a `button` string +// payload (`"LeftButton"` / `"RightButton"` / `"MiddleButton"` / +// `"Button4"` / `"Button5"`). Distinct from per-frame `OnMouseDown` +// scripts — these fire even when the click misses any UI frame. +// +// We poll Win32 `GetAsyncKeyState` per frame for the five mouse +// buttons and edge-detect transitions. Gated on WoW being the +// foreground window so alt-tabbed clicks elsewhere don't fire +// spurious events. + +#include "event/Custom.h" +#include "tick/WorldTick.h" + +#include + +namespace Input::GlobalMouse { + +namespace { + +constexpr const char *kDownEvent = "GLOBAL_MOUSE_DOWN"; +constexpr const char *kUpEvent = "GLOBAL_MOUSE_UP"; + +struct Button { + int vk; + const char *name; +}; + +// Order matches modern WoW's button string identifiers. VK_XBUTTON1 +// and VK_XBUTTON2 are the extra side buttons on a 5-button mouse. +constexpr Button kButtons[] = { + {VK_LBUTTON, "LeftButton"}, + {VK_RBUTTON, "RightButton"}, + {VK_MBUTTON, "MiddleButton"}, + {VK_XBUTTON1, "Button4"}, + {VK_XBUTTON2, "Button5"}, +}; + +constexpr int kButtonCount = sizeof(kButtons) / sizeof(kButtons[0]); + +bool g_prev[kButtonCount] = {}; + +bool WoWIsForeground() { + HWND fg = GetForegroundWindow(); + if (fg == nullptr) + return false; + // Cache the WoW window by walking the foreground window's owner- + // process chain on first sight. `GetForegroundWindow` returns the + // window that has focus; for this process, that IS WoW's main + // window (no child window has its own input focus). We compare by + // process ID to avoid false positives if WoW is alt-tabbed. + DWORD fgPid = 0; + GetWindowThreadProcessId(fg, &fgPid); + return fgPid == GetCurrentProcessId(); +} + +void OnWorldTick() { + const bool focused = WoWIsForeground(); + for (int i = 0; i < kButtonCount; ++i) { + // High-order bit of GetAsyncKeyState is "key is currently + // down". Low bit is "key was pressed since last call" — we + // don't use that since we maintain our own edge state. + const bool down = (GetAsyncKeyState(kButtons[i].vk) & 0x8000) != 0; + // Only honor down transitions when WoW has focus, so an + // alt-tabbed click elsewhere doesn't fire. UP transitions + // always fire — we want the matching release event even if + // the user alt-tabbed mid-click, to avoid leaving addons in + // a stuck "button is held" state. + const bool effectiveDown = down && focused; + if (effectiveDown == g_prev[i]) + continue; + g_prev[i] = effectiveDown; + const int slot = Event::Custom::Lookup(effectiveDown ? kDownEvent : kUpEvent); + if (slot >= 0) + Event::Custom::Fire_S(slot, kButtons[i].name); + } +} + +} // namespace + +static const Event::Custom::AutoReserve _reserveDown{kDownEvent}; +static const Event::Custom::AutoReserve _reserveUp{kUpEvent}; +static const Tick::WorldTick::AutoSubscribe _tickSub{&OnWorldTick}; + +} // namespace Input::GlobalMouse diff --git a/src/player/InputEvents.cpp b/src/player/InputEvents.cpp new file mode 100644 index 00000000..84acc1bd --- /dev/null +++ b/src/player/InputEvents.cpp @@ -0,0 +1,145 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU Lesser General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU Lesser General Public License for more details. + +// Modern WoW exposes input-state transitions as events; vanilla 1.12 +// doesn't. One per-frame `Tick::WorldTick` callback reads the engine +// state once, edge-detects each pair, fires whichever transitioned. +// +// PLAYER_STARTED_MOVING / STOPPED_MOVING +// Source: UI-input controller flag bits (WASD / autorun). +// Key-state based — STOPPED fires the instant the movement keys +// release, even mid-air. Matches retail. +// +// PLAYER_STARTED_TURNING / STOPPED_TURNING +// Source: per-frame delta on the player's body yaw at +// `CGPlayer + 0x9C4`. Fires when the character is *actually +// rotating*, not merely on RMB press — matches retail's +// "camera has moved" semantics. The mouselook bit is held +// while RMB is down even if the user isn't dragging; gating +// 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. + +#include "Offsets.h" +#include "event/Custom.h" +#include "tick/WorldTick.h" + +#include +#include + +namespace Player::InputEvents { + +namespace { + +constexpr const char *kStartedMovingEvent = "PLAYER_STARTED_MOVING"; +constexpr const char *kStoppedMovingEvent = "PLAYER_STOPPED_MOVING"; +constexpr const char *kStartedLookingEvent = "PLAYER_STARTED_LOOKING"; +constexpr const char *kStoppedLookingEvent = "PLAYER_STOPPED_LOOKING"; +constexpr const char *kStartedTurningEvent = "PLAYER_STARTED_TURNING"; +constexpr const char *kStoppedTurningEvent = "PLAYER_STOPPED_TURNING"; + +// Below this per-frame delta, treat the yaw as stable. Picks up +// real rotation (smallest user-perceptible RMB-drag is ~0.5°/frame +// = 0.009 rad) while ignoring float drift / single-bit jitter on +// the broadcast-position copy. +constexpr float YAW_EPSILON = 0.001f; + +bool g_wasMoving = false; +bool g_wasLooking = false; +bool g_wasTurning = false; + +float g_prevBodyYaw = 0.0f; +bool g_havePrevYaw = false; + +using ResolveUnitToken_t = void *(__fastcall *)(const char *token); + +const uint8_t *Controller() { + return *reinterpret_cast(Offsets::VAR_UI_INPUT_CONTROLLER); +} + +const uint8_t *Player() { + auto fn = reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); + return static_cast(fn("player")); +} + +void FireTransition(bool now, bool &prev, const char *startedName, + const char *stoppedName) { + if (now == prev) + return; + prev = now; + const int slot = Event::Custom::Lookup(now ? startedName : stoppedName); + if (slot >= 0) + Event::Custom::Fire_None(slot); +} + +void OnWorldTick() { + auto *ctrl = Controller(); + auto *player = Player(); + 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; + 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 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). + // 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( + 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 + } else { + turning = false; // bit held but no drag yet — waiting + } + g_prevBodyYaw = yaw; + g_havePrevYaw = true; + + FireTransition(moving, g_wasMoving, kStartedMovingEvent, kStoppedMovingEvent); + FireTransition(turning, g_wasTurning, kStartedTurningEvent, kStoppedTurningEvent); + FireTransition(looking, g_wasLooking, kStartedLookingEvent, kStoppedLookingEvent); +} + +} // namespace + +static const Event::Custom::AutoReserve _reserveStartedMoving{kStartedMovingEvent}; +static const Event::Custom::AutoReserve _reserveStoppedMoving{kStoppedMovingEvent}; +static const Event::Custom::AutoReserve _reserveStartedLooking{kStartedLookingEvent}; +static const Event::Custom::AutoReserve _reserveStoppedLooking{kStoppedLookingEvent}; +static const Event::Custom::AutoReserve _reserveStartedTurning{kStartedTurningEvent}; +static const Event::Custom::AutoReserve _reserveStoppedTurning{kStoppedTurningEvent}; +static const Tick::WorldTick::AutoSubscribe _tickSub{&OnWorldTick}; + +} // namespace Player::InputEvents diff --git a/src/player/MouseLook.cpp b/src/player/MouseLook.cpp deleted file mode 100644 index c509e7df..00000000 --- a/src/player/MouseLook.cpp +++ /dev/null @@ -1,83 +0,0 @@ -// This file is part of ClassicAPI. -// -// ClassicAPI is free software: you can redistribute it and/or modify it under the terms -// of the GNU Lesser General Public License as published by the Free Software Foundation, either -// version 3 of the License, or (at your option) any later version. -// -// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR -// PURPOSE. See the GNU Lesser General Public License for more details. - -// `PLAYER_STARTED_LOOKING` / `PLAYER_STOPPED_LOOKING` — LMB-drag -// free-look (camera-only rotation, character stays put). -// `PLAYER_STARTED_TURNING` / `PLAYER_STOPPED_TURNING` — RMB-drag / -// `MouselookStart` mouselook mode (character rotates with the -// camera). Mouse-only — modern WoW's TURNING does not fire for -// keyboard turn-in-place, despite what the docs imply; verified -// empirically. -// -// Both pairs are sibling to `PLAYER_STARTED_MOVING` / -// `PLAYER_STOPPED_MOVING`: vanilla 1.12 already maintains the -// underlying flags as part of the UI input controller's master -// state word; modern WoW exposes the transitions as events but -// 1.12 doesn't. We poll the state byte once per frame and edge- -// detect. - -#include "Offsets.h" -#include "event/Custom.h" -#include "tick/WorldTick.h" - -#include - -namespace Player::MouseLook { - -namespace { - -constexpr const char *kStartedLookingEvent = "PLAYER_STARTED_LOOKING"; -constexpr const char *kStoppedLookingEvent = "PLAYER_STOPPED_LOOKING"; -constexpr const char *kStartedTurningEvent = "PLAYER_STARTED_TURNING"; -constexpr const char *kStoppedTurningEvent = "PLAYER_STOPPED_TURNING"; - -bool g_wasLooking = false; -bool g_wasTurning = false; - -const uint8_t *Controller() { - return *reinterpret_cast(Offsets::VAR_UI_INPUT_CONTROLLER); -} - -void FireTransition(bool now, bool &prev, const char *startedName, - const char *stoppedName) { - if (now == prev) - return; - prev = now; - const int slot = Event::Custom::Lookup(now ? startedName : stoppedName); - if (slot >= 0) - Event::Custom::Fire_None(slot); -} - -void OnWorldTick() { - auto *ctrl = Controller(); - if (ctrl == nullptr) { - // Pre-login / teardown — reset so first valid tick doesn't - // fire spurious STOPPED transitions. - g_wasLooking = false; - g_wasTurning = false; - return; - } - const uint32_t flags = *reinterpret_cast( - ctrl + Offsets::OFF_UI_INPUT_FLAGS); - const bool turning = (flags & Offsets::INPUT_FLAG_MOUSELOOK) != 0; - const bool looking = (flags & Offsets::INPUT_FLAG_FREE_LOOK) != 0; - FireTransition(turning, g_wasTurning, kStartedTurningEvent, kStoppedTurningEvent); - FireTransition(looking, g_wasLooking, kStartedLookingEvent, kStoppedLookingEvent); -} - -} // namespace - -static const Event::Custom::AutoReserve _reserveStartedLooking{kStartedLookingEvent}; -static const Event::Custom::AutoReserve _reserveStoppedLooking{kStoppedLookingEvent}; -static const Event::Custom::AutoReserve _reserveStartedTurning{kStartedTurningEvent}; -static const Event::Custom::AutoReserve _reserveStoppedTurning{kStoppedTurningEvent}; -static const Tick::WorldTick::AutoSubscribe _tickSub{&OnWorldTick}; - -} // namespace Player::MouseLook diff --git a/src/player/Movement.cpp b/src/player/Movement.cpp deleted file mode 100644 index b67ec724..00000000 --- a/src/player/Movement.cpp +++ /dev/null @@ -1,87 +0,0 @@ -// This file is part of ClassicAPI. -// -// ClassicAPI is free software: you can redistribute it and/or modify it under the terms -// of the GNU Lesser General Public License as published by the Free Software Foundation, either -// version 3 of the License, or (at your option) any later version. -// -// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR -// PURPOSE. See the GNU Lesser General Public License for more details. - -// `PLAYER_STARTED_MOVING` / `PLAYER_STOPPED_MOVING` — modern events -// vanilla doesn't fire. Per-frame poll of the local player's -// movement-flags word (same word `IsFalling`/`IsSwimming` read) and -// edge-detect on the "translational motion" subset of bits. Fires -// STARTED on `false→true`, STOPPED on `true→false`. -// -// The mask intentionally excludes TURN_LEFT / TURN_RIGHT (turn-in- -// place doesn't count as moving on modern) and the protocol-level -// flags like WALK_MODE, ONTRANSPORT, LEVITATING that don't reflect -// active translation. Jumping and falling DO count — modern fires -// STARTED_MOVING when you jump in place, and the airborne portion of -// any jump/fall keeps the event "moving" until you touch down. -// -// We subscribe to the shared `Tick::WorldTick` registry rather than -// installing our own MinHook on `FUN_WORLD_TICK` — MinHook permits -// only one hook per target address, and `bag/UpdateDelayed.cpp` -// also wants per-frame ticks. - -#include "Offsets.h" -#include "event/Custom.h" -#include "tick/WorldTick.h" - -#include - -namespace Player::Movement { - -namespace { - -constexpr const char *kStartedMovingEvent = "PLAYER_STARTED_MOVING"; -constexpr const char *kStoppedMovingEvent = "PLAYER_STOPPED_MOVING"; - -constexpr uint32_t kMovingMask = - Offsets::MOVEFLAG_FORWARD - | Offsets::MOVEFLAG_BACKWARD - | Offsets::MOVEFLAG_STRAFE_LEFT - | Offsets::MOVEFLAG_STRAFE_RIGHT - | Offsets::MOVEFLAG_JUMPING - | Offsets::MOVEFLAG_FALLING - | Offsets::MOVEFLAG_FALLING_FAR; - -bool g_wasMoving = false; - -using ResolveUnitToken_t = void *(__fastcall *)(const char *token); - -const uint8_t *Player() { - auto fn = reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); - return static_cast(fn("player")); -} - -void OnWorldTick() { - auto *player = Player(); - if (player == nullptr) { - // Pre-world / character select / loading screen. Reset - // state so the next valid tick doesn't fire a spurious - // STOPPED when the player pointer first comes back. - g_wasMoving = false; - return; - } - const uint32_t flags = *reinterpret_cast( - player + Offsets::OFF_PLAYER_MOVEMENT_FLAGS); - const bool moving = (flags & kMovingMask) != 0; - if (moving == g_wasMoving) - return; - g_wasMoving = moving; - const int slot = Event::Custom::Lookup( - moving ? kStartedMovingEvent : kStoppedMovingEvent); - if (slot >= 0) - Event::Custom::Fire_None(slot); -} - -} // namespace - -static const Event::Custom::AutoReserve _reserveStarted{kStartedMovingEvent}; -static const Event::Custom::AutoReserve _reserveStopped{kStoppedMovingEvent}; -static const Tick::WorldTick::AutoSubscribe _tickSub{&OnWorldTick}; - -} // namespace Player::Movement