diff --git a/README.md b/README.md index 271b3ed7..2b1b157d 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Full per-function reference: **[docs/API.md](docs/API.md)**. | NameCache | `C_CreatureInfo.GetCreatureID`, `C_PlayerCache.GetPlayerInfoByName`, `C_PlayerCache.IsEnabled`, `C_PlayerCache.IsScanEnabled`, `C_PlayerCache.RememberPlayer`, `C_PlayerCache.SetEnabled`, `C_PlayerCache.SetScanEnabled`, `C_PlayerInfo.GUIDIsCreature`, `C_PlayerInfo.GUIDIsGameObject`, `C_PlayerInfo.GUIDIsPet`, `C_PlayerInfo.GUIDIsPlayer`, `GetPlayerInfoByGUID` | | Quest | `C_QuestLog.GetQuestIDForLogIndex`, `C_QuestLog.GetTitleForQuestID`, `C_QuestLog.RequestLoadQuestByID` | | Spell | `C_Spell.GetSpellDescription`, `C_Spell.GetSpellInfo`, `C_Spell.GetSpellLink`, `C_Spell.GetSpellName`, `C_Spell.GetSpellTexture`, `C_Spell.IsSpellHarmful`, `C_Spell.IsSpellHelpful`, `C_Spell.IsSpellPassive`, `C_Spell.IsSpellUsable`, `C_SpellBook.GetCurrentLevelSpells`, `C_SpellBook.GetSpellLevelLearned`, `C_SpellBook.GetSpellSkillLine`, `CastSpellNoToggle`, `FindSpellBookSlotByID`, `GetCraftSpellID`, `GetSpellInfo`, `GetSpellLink`, `GetSpellSchool`, `IsHarmfulSpell`, `IsHelpfulSpell`, `IsPassiveSpell`, `IsPlayerSpell`, `IsSpellKnown`, `IsUsableSpell` | -| State | `GetMirrorTimerInfo`, `GetMirrorTimerProgress`, `IsAssistingRitual`, `IsFalling`, `IsMounted`, `IsStealthed`, `IsSwimming` | +| State | `GetMirrorTimerInfo`, `GetMirrorTimerProgress`, `GetShapeshiftFormID`, `IsAssistingRitual`, `IsFalling`, `IsMounted`, `IsStealthed`, `IsSwimming` | | Table | `table.wipe` | | Talent | `GetTalentIDByIndex`, `GetTalentSpellID` | | Time | `C_DateAndTime.AdjustTimeByDays`, `C_DateAndTime.AdjustTimeByMinutes`, `C_DateAndTime.CompareCalendarTime`, `C_DateAndTime.GetCalendarTimeFromEpoch`, `C_DateAndTime.GetCurrentCalendarTime`, `C_DateAndTime.GetSecondsUntilDailyReset`, `C_DateAndTime.GetServerTimeLocal`, `C_Timer.After`, `C_Timer.NewTicker`, `C_Timer.NewTimer`, `GetServerTime` | diff --git a/docs/API.md b/docs/API.md index 1b8703e4..95749e13 100644 --- a/docs/API.md +++ b/docs/API.md @@ -216,6 +216,7 @@ build instructions. - [`IsSwimming()`](#isswimming) - [`IsAssistingRitual()`](#isassistingritual) - [`GetMirrorTimerInfo(index)` / `GetMirrorTimerProgress(label)`](#getmirrortimerinfoindex--getmirrortimerprogresslabel) + - [`GetShapeshiftFormID()`](#getshapeshiftformid) - [Table](#table) - [`table.wipe(t)`](#tablewipet) @@ -4866,6 +4867,51 @@ end > to modern's name. Addons backporting `"FATIGUE"`-keyed code need to > handle either string. +### `GetShapeshiftFormID()` + +Returns the player's current shapeshift form as the integer ID from +vanilla 1.12.1's `SpellShapeshiftForm.dbc`. Returns `0` when the +player isn't shifted. + +| Class | Form IDs | +|-------|----------| +| Druid | `1` Cat, `2` Tree, `3` Travel, `4` Aquatic, `5` Bear, `8` Dire Bear, `31` Moonkin | +| Warrior | `17` Battle, `18` Defensive, `19` Berserker | +| Shaman | `16` Ghost Wolf | +| Rogue | `30` Stealth | +| Priest | `28` Shadowform, `32` Spirit of Redemption | + +**Turtle WoW** extends the DBC with custom rows that aren't present +on Blizzard 1.12.1: + +| ID | Name | Notes | +|----|------|-------| +| `9` | Tree of Life Form | Restoration druid endgame form | +| `11` | Swift Travel Form | Mounted-speed travel form | + +> **Vanilla numbering ≠ modern numbering.** Modern WoW renumbered the +> table — e.g. modern uses `17` for Travel and `35` for Tree of Life, +> where 1.12.1 uses `3` (Travel) and Turtle uses `9` (Tree of Life). +> Don't copy named constants from a modern addon and expect them to +> match. + +```lua +if GetShapeshiftFormID() == 0 then + CastSpellByName("Cat Form") +end +``` + +Reads byte 2 of `UNIT_BYTES_1` (descriptor `+0x212`) on the local +player — the same byte vanilla's `Script_GetShapeshiftFormInfo` +compares against each form-spell's effect-encoded form ID to answer +"is this form active". The engine updates the byte from +`SMSG_UPDATE_OBJECT` aura/form packets, so the value is live without +needing an event listener. + +Vanilla 1.12 has only `GetShapeshiftFormInfo(index)` (1-based bar +index); `GetShapeshiftFormID` exposes the DBC ID directly so callers +can write `if formID == CAT_FORM` without iterating the bar. + ## Table ### `table.wipe(t)` diff --git a/src/unit/ShapeshiftForm.cpp b/src/unit/ShapeshiftForm.cpp new file mode 100644 index 00000000..3fef47ad --- /dev/null +++ b/src/unit/ShapeshiftForm.cpp @@ -0,0 +1,78 @@ +// 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. +// +// You should have received a copy of the GNU Lesser General Public License along with +// ClassicAPI. If not, see . + +#include "Game.h" +#include "Offsets.h" + +#include + +namespace Unit::ShapeshiftForm { + +namespace { + +using ResolveUnitToken_t = void *(__fastcall *)(const char *token); + +// `GetShapeshiftFormID()` — returns the player's current shapeshift +// form ID from vanilla 1.12.1's `SpellShapeshiftForm.dbc`: +// Druid: 1 Cat, 2 Tree, 3 Travel, 4 Aquatic, 5 Bear, +// 8 Dire Bear, 31 Moonkin +// Warrior: 17 Battle, 18 Defensive, 19 Berserker +// Shaman: 16 Ghost Wolf +// Rogue: 30 Stealth +// Priest: 28 Shadowform, 32 Spirit of Redemption +// Turtle WoW extends the DBC with custom rows: 9 Tree of Life Form, +// 11 Swift Travel Form. Both slots are empty on Blizzard 1.12.1. +// Returns `0` when the player isn't shapeshifted. **These are the +// 1.12 values; modern WoW renumbered the table** (e.g. modern +// Travel=17, Tree=35) — addons backporting modern code shouldn't +// reuse the constants verbatim. +// +// Reads byte 2 of `UNIT_BYTES_1` (descriptor `+0x212`) on the local +// player. Derived from `Script_GetShapeshiftFormInfo` at `0x004B45C0`, +// which uses the same byte to compare against each form-spell's +// effect-encoded form ID when answering "is this form active". The +// engine sets it from `SMSG_UPDATE_OBJECT` aura/form updates, so it +// tracks live state without us needing to listen on any event. +// +// Vanilla 1.12 has no native `GetShapeshiftFormID` — only the +// 1-based bar-index `GetShapeshiftFormInfo`. Modern API lets callers +// key behavior on the DBC ID directly (`if formID == CAT_FORM then`) +// without needing to iterate the bar. +int __fastcall Script_GetShapeshiftFormID(void *L) { + auto resolve = reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); + auto *player = static_cast(resolve("player")); + if (player == nullptr) { + Game::Lua::PushNumber(L, 0); + return 1; + } + auto *fields = *reinterpret_cast( + player + Offsets::OFF_UNIT_DESCRIPTOR); + if (fields == nullptr) { + Game::Lua::PushNumber(L, 0); + return 1; + } + const uint32_t bytes1 = *reinterpret_cast( + fields + Offsets::OFF_UNIT_FIELD_BYTES_1); + Game::Lua::PushNumber(L, static_cast((bytes1 >> 16) & 0xFF)); + return 1; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterGlobalFunction("GetShapeshiftFormID", &Script_GetShapeshiftFormID); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +} // namespace Unit::ShapeshiftForm