// This file is part of ClassicAPI. // // ClassicAPI is free software: you can redistribute it and/or modify it under the terms // of the GNU 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 General Public License for more details. // // You should have received a copy of the GNU General Public License along with // ClassicAPI. If not, see . // Cast / channel info — `UnitCastingInfo`, `CastingInfo`, // `UnitChannelInfo`, `ChannelInfo`. // // Vanilla 1.12 stores NO cast start/end times anywhere readable (unlike // 3.3.5+, which keeps them per-unit on the CGUnit). The cast bar is // Lua-driven off `SPELLCAST_START(duration)` + `GetTime()`. So we // self-track the LOCAL PLAYER's cast/channel with a HYBRID of two sources: // - Normal casts (client path): co-hook the engine's cast-start writer // `FUN_CAST_START_SET` and stamp from its spellID argument the instant // the cast begins (startMs = now, endMs = now + effective cast time) — // zero latency. `g_castFromServer = false`; cleared on `WorldTick` when // `VAR_CURRENT_CAST_SPELL` returns to 0 (normal end / cancel / interrupt). // - Chained same-spell recasts (server path): the engine SHORT-CIRCUITS // `Spell_C_CastSpell` at `spellID == VAR_CURRENT_CAST_SPELL` — it never // runs the client cast path, never sets the global, never fires // SPELLCAST_START. So a chained recast is invisible client-side; the // ONLY witness is the server's `SMSG_SPELL_START` (= nampower's // SPELL_START_SELF). We stamp `g_cast` from that packet (server cast // time), dedup'd against a just-made client stamp so a normal cast's // confirming packet doesn't restart its bar. `g_castFromServer = true`; // since the global was never set for these, the WorldTick VAR==0 clear // must skip them — they expire on their computed endMs, or earlier via // the abort co-hooks (ClearCastingSpell / the failure packets) when the // engine learns the cast died. // - channel: stamped from `SMSG_SPELL_START` at packet receipt (instant // channels; engine-computed duration) and from `MSG_CHANNEL_START` // (server duration — and the ONLY start signal for cast-then-channel // spells like Mind Control, whose SpellStart covers just the cast // phase). The broadcast +0x228 field is NOT polled for the player — // it lags the channel start by ~1 tick. // `now` is the engine ms clock (`FUN_OS_TICKCOUNT_MS`), the same source // Lua's `GetTime()` scales by 0.001 — so startMs/endMs are directly // comparable to `GetTime()*1000` in addon progress math. // // Other units have no engine-stored cast TIMES (the engine does keep the // current-cast spellID at [unit+0xC8C], but nothing temporal), so we // capture their casts from the `SMSG_SPELL_START` packet (the only place // the client sees another unit's cast + a server-authoritative cast time) // into a per-caster cache — see the SpellStart co-hook below. Scope: // - Player casts/channels: full timing (self-tracked, exact engine math). // - Other units' regular CASTS: full timing while within the cast window, // from the SpellStart cache. Aborts (interrupt, death, movement, // cancel) evict the entry the moment the engine stops the unit's cast // — the CGUnit_C::ClearCastingSpell co-hook below, backstopped by // co-hooks on both failure packets — so no lingering ghost bar. // - Other units' CHANNELS: the SpellStart cache gives real start/end times // from the moment the channel begins. The broadcast +0x228 field lags // that start by up to ~1 s, so it is latched like the player's (see // PollRemoteChannels): trusted only once it has shown the spell, after // which its clearing is the channel's early end. A channel we didn't see // begin reports spellID/name/texture from +0x228 with nil times. // - Other units' PUSHBACK is invisible: SMSG_SPELL_DELAYED goes only to // the affected caster (empirically confirmed — during a melee-pushback // test only the victim's client received it), so a remote bar can't // stretch. Inherent 1.12 protocol gap, not recoverable client-side. // // The other modern-signature fields: `castID` is the cast's castGUID // (Spell::CastEvents), `isTradeskill` is SPELL_ATTR_TRADESPELL, `delayTimeMs` // the accumulated pushback, `notInterruptible` the 3.3.5 client's own // player-relative predicate on 1.12 data (Spell::Interruptible). `castBarID` // has no 1.12 source and stays nil. #include "Game.h" #include "Offsets.h" #include "aura/Source.h" #include "dbc/Lookup.h" #include "duration/Object.h" #include "net/PacketDispatch.h" #include "net/PacketReader.h" #include "object/Resolve.h" #include "player/Info.h" #include "spell/Interruptible.h" #include "spell/Lookup.h" #include "spell/CastEvents.h" #include "tick/WorldTick.h" #include "time/Clock.h" #include "unit/Focus.h" #include "unit/Identity.h" #include "unit/IncomingResurrection.h" #include namespace Spell::Cast { namespace { using GetCastTime_t = uint32_t(__fastcall *)(int spellID, int pet, int allowNegative); using GetDuration_t = int(__fastcall *)(const uint8_t *spellRecord, int pet, int skipMod); // SPELL_ATTR_RANGED — every ranged ability carries it; the server adds a // flat +500ms "aim time" to these (SpellEntry::GetCastTime). Auto-repeat // shots (Auto Shot / Shoot, AttributesEx2 bit 0x20) are the one exclusion. constexpr uint32_t SPELL_ATTR_RANGED = 0x2; // SMSG_SPELL_START target block: SpellCastTargets writes a u16 target mask, // then a packed GUID for the unit target when this bit is set (1.12 server // SpellCastTargets::write). Self / ground / item targets carry no unit GUID — // a cast with none reports no target. Per project convention TARGET_* flags // stay local (single-use, no drift). constexpr uint16_t TARGET_FLAG_UNIT = 0x2; struct TrackedSpell { int spellID; // 0 = not casting / channeling int startMs; int endMs; int delayMs; // accumulated pushback (SMSG_SPELL_DELAYED), 0 = none }; TrackedSpell g_cast{0, 0, 0, 0}; TrackedSpell g_channel{0, 0, 0, 0}; // The unit GUID the player is currently casting / channeling AT (from the // SMSG_SPELL_START target block), or 0 for a self / ground / untargeted cast. // Reset on each fresh client-side stamp; filled when the confirming packet's // target lands. Read by `UnitSpellTargetName` and gated there on g_cast / // g_channel being active, so a stale value from a finished cast is never read. uint64_t g_castTargetGuid = 0; // True when g_cast was stamped from SMSG_SPELL_START (a chained same-spell // recast the client cast path bailed on) rather than the client-side // FUN_CAST_START_SET hook. Such casts never set VAR_CURRENT_CAST_SPELL, so // the WorldTick VAR==0 clear must not touch them. bool g_castFromServer = false; // Set when a movement-immune cast's engine cast-state was dropped by the player // MOVING (issue #23 — grenades). Such a cast isn't really interrupted; the // server completes it. So we keep tracking it instead of ending it, and clear // it on its SPELL_GO completion (→ STOP, no INTERRUPTED) or a grace past its end. // A real cancel / interrupt happens with the player stationary, so it clears // normally. Reset on every fresh cast stamp. bool g_castMoveDropped = false; // Armed once the broadcast UNIT_FIELD_CHANNEL_SPELL (+0x228) has been seen to // reflect g_channel's spell — the enable for OnWorldTick's channel-stop poll. // StampChannel re-arms it (false) on every channel stamp so that field's // ~1-tick start lag can't false-clear a channel we just stamped. bool g_channelConfirmed = false; // True for a channel the client shows no bar for (SPELL_ATTR_EX3_HIDE_CHANNEL_BAR). // The engine fires no SPELLCAST_CHANNEL_START for it, and 4.3.4 records no UI // channel, so neither do we: no channel info and no channel events. bool HidesChannelBar(int spellID) { const uint8_t *rec = Spell::Lookup::RecordForID(spellID); return rec != nullptr && (Game::Read(rec, Offsets::OFF_SPELL_RECORD_ATTRIBUTES_EX3) & Offsets::SPELL_ATTR_EX3_HIDE_CHANNEL_BAR) != 0; } // Stamp the player's channel and re-arm the stop latch. A channel supersedes // any in-flight cast. Both channel-stamp sites (SMSG_SPELL_START's instant- // channel path and the MSG_CHANNEL_START handler) route through here so // neither can forget to reset the latch. void StampChannel(int spellID, int startMs, int endMs) { if (HidesChannelBar(spellID)) { // No channel bar, so no channel info or events — as the engine does. g_channel.spellID = 0; g_cast.spellID = 0; return; } g_channel = TrackedSpell{spellID, startMs, endMs, 0}; g_channelConfirmed = false; g_cast.spellID = 0; } // Cast tracking keeps ticks in a signed int internally by design — see PushMs // for why that's safe (small deltas cancel; the Lua boundary re-casts to // uint32 so it matches GetTime()*1000 across the wrap). The reader itself // still comes from the one canonical source. int NowMs() { return static_cast(Time::Clock::NowMs()); } // Wrap-safe "has `endMs` arrived?" on the int-stored ticks — `Time::Clock`'s // signed-difference compare. A raw `now >= endMs` inverts for a cast that // straddles 2^31, where `endMs` reads negative and `now` does not. bool Ended(int nowMs, int endMs) { return Time::Clock::Reached(static_cast(nowMs), static_cast(endMs)); } bool Ended(int endMs) { return Ended(NowMs(), endMs); } // A cast is interrupted by the caster moving only if its Spell.dbc InterruptFlags // carry SPELL_INTERRUPT_FLAG_MOVEMENT (the server's HandleMovementOpcodes and the // client's own CheckCast both gate the movement interrupt on this bit). Thrown // items — grenades — lack it. Unknown spell → assume interruptible (the safe // default: never keeps a phantom bar alive). bool MovementInterruptible(int spellID) { const uint8_t *rec = Spell::Lookup::RecordForID(spellID); if (rec == nullptr) return true; return (Game::Read( rec, Offsets::OFF_SPELL_RECORD_INTERRUPT_FLAGS) & Offsets::SPELL_INTERRUPT_FLAG_MOVEMENT) != 0; } // Grace past a move-dropped cast's computed end before we give up on its // SPELL_GO and end it anyway (covers the SPELL_GO landing ~1 RTT after the // bar's computed end). constexpr int kMoveDroppedGraceMs = 750; // Is the local player moving in a way that drops an in-progress cast? Mirrors // the engine's own cast-drop movement test (CheckCast masks the player movement // flags with MOVEFLAG_MASK_CAST_DROP). Used to tell a spurious movement drop of // a movement-immune cast (grenades) from a real cancel/interrupt (stationary). // False (the safe answer — clear normally) when the player object or its // movement block is missing. bool PlayerMoving() { const uint8_t *player = Unit::Identity::PlayerObject(); if (player == nullptr) return false; const uint8_t *move = Game::Read( player, Offsets::OFF_UNIT_MOVEMENT_INFO_PTR); if (move == nullptr) return false; return (Game::Read(move, Offsets::OFF_MOVEMENT_FLAGS) & Offsets::MOVEFLAG_MASK_CAST_DROP) != 0; } // Pushes an engine-ms timestamp to Lua as an UNSIGNED 32-bit value. // // `FUN_OS_TICKCOUNT_MS` returns a 64-bit ms tick from one of two backends // (GetTickCount, or a scaled rdtsc — the latter survives warm reboots, so // the value can be well past 2^31 even right after a "restart"). We keep // ticks in a signed int internally, which is fine for the small deltas and // window compares (they cancel), but a direct int->double push surfaces a // NEGATIVE startTimeMs/endTimeMs once the tick's high bit is set. // // The contract is that these match `GetTime()*1000`. Script_GetTime // (0x00515EA0) does `(double)(tick & 0xFFFFFFFF) * 0.001` — i.e. it masks // the tick to the low 32 bits UNSIGNED, then scales. So reinterpreting our // stored tick as uint32 reproduces GetTime()'s exact value for the whole // range, including across the ~49.7-day 2^32 wrap (both wrap together). // Carrying 64-bit precision here would be WRONG: it would diverge from // GetTime() past 2^32 and break addons' (now-start)/(end-start) math. void PushMs(void *L, int ms) { Game::Lua::PushNumber(L, static_cast(static_cast(ms))); } void *Resolve(const char *token) { return Game::ResolveUnitToken(token); } // Effective cast time (ms) for the local player, 0 if instant. Uses the // engine's own cast-time helper (base + level scaling + cast-time SpellMod // op 10 + the caster's haste/cast-speed multiplier), so our end time // matches the engine's cast bar exactly — including talents like a Mage's // faster casts. Pet arg 0 = local player; allowNegative 0 clamps negatives to 0. int CastTimeMs(int spellID) { int ms = static_cast( reinterpret_cast(Offsets::FUN_GET_CAST_TIME)(spellID, 0, 0)); // FUN_006e3340 (also the spell tooltip's cast-time source) does NOT add the // flat +500ms "aim time" the server applies to ranged, non-autorepeat // spells — SpellEntry::GetCastTime does `castTime += 500` for // SPELL_ATTR_RANGED. So our zero-latency prediction runs ~500ms short of // the server for Volley / Aimed Shot / Multi-Shot; the confirming // SMSG_SPELL_START then snaps the bar out ~1 RTT later. Mirror the server // here so the prediction is right from frame 1 (only for spells that // already have a cast time — instant ranged abilities that the server // gives a 500ms aim time still surface through the packet path). The // cast-time reconcile in HandleSpellStart remains the backstop. if (ms > 0) { const uint8_t *rec = Spell::Lookup::RecordForID(spellID); if (rec != nullptr) { const uint32_t attr = Game::Read(rec, Offsets::OFF_SPELL_RECORD_ATTRIBUTES); const uint32_t attrEx2 = Game::Read(rec, Offsets::OFF_SPELL_RECORD_ATTRIBUTES_EX2); if ((attr & SPELL_ATTR_RANGED) && !(attrEx2 & Offsets::SPELL_ATTR_EX2_AUTOREPEAT_FLAG)) ms += 500; } } return ms; } // Effective channel duration (ms) for the local player, 0 if none. Uses // the engine's own duration helper (SpellDuration base + level scaling + // duration SpellMod op 1), mirroring how the cast time is sourced, so the // channel end time matches the engine. Pet arg 0 = local player. int ChannelDurationMs(int spellID) { const uint8_t *rec = Spell::Lookup::RecordForID(spellID); if (rec == nullptr) return 0; const int ms = reinterpret_cast(Offsets::FUN_GET_SPELL_DURATION)(rec, 0, 0); return ms > 0 ? ms : 0; // negative = "infinite"; channels are finite } const char *SpellName(const uint8_t *rec) { const int locale = Game::Read(Offsets::VAR_LOCALE_INDEX); return Game::Read(rec, Offsets::OFF_SPELL_NAMES + locale * 4); } bool IsTradeskill(const uint8_t *rec) { return (Game::Read(rec, Offsets::OFF_SPELL_RECORD_ATTRIBUTES) & Offsets::SPELL_ATTR_TRADESPELL) != 0; } // Spell icon texture path, or "" if there's no icon record. SpellIcon.dbc // already stores the full "Interface\Icons\..." path (unlike item icons, // which are bare filenames) — so it's used verbatim, no prefix. const char *SpellIconPath(const uint8_t *rec) { const int iconID = Game::Read(rec, Offsets::OFF_SPELL_RECORD_ICON_ID); const uint8_t *iconRec = DBC::Record(Offsets::VAR_SPELL_ICON_RECORDS, Offsets::VAR_SPELL_ICON_COUNT, static_cast(iconID)); if (iconRec == nullptr) return ""; const char *path = Game::Read(iconRec, Offsets::OFF_SPELLICON_PATH); return (path != nullptr) ? path : ""; } // Pushes UnitCastingInfo's 11-tuple from a tracked cast, or nothing (nil) // if there's no active cast. `casterGuid` identifies whose cast this is (the // player's or a remote unit's) so `castID` can be pulled from Spell::CastEvents // — the same castGUID the cast's UNIT_SPELLCAST_* events carry. `casterUnit` // is the caster's CGUnit (null when unresolvable) for the immunity-aura half // of `notInterruptible`. // The spell record of a cast the getters report, or null for none. // Self-expire: once the cast window has elapsed, report nothing even if // the slot wasn't explicitly cleared. Keeps a stale cast (e.g. one whose // clear we missed) from lingering on an addon's cast bar, and lets a // fresh stamp for the next cast take over cleanly. const uint8_t *ShownCastRecord(const TrackedSpell &c) { if (c.spellID == 0 || Ended(c.endMs)) return nullptr; return Spell::Lookup::RecordForID(c.spellID); } int PushCastInfo(void *L, const TrackedSpell &c, uint64_t casterGuid, const uint8_t *casterUnit) { const uint8_t *rec = ShownCastRecord(c); if (rec == nullptr) return 0; const char *name = SpellName(rec); Game::Lua::PushString(L, name); // 1 name Game::Lua::PushString(L, name); // 2 displayName Game::Lua::PushString(L, SpellIconPath(rec)); // 3 textureID (path) PushMs(L, c.startMs); // 4 startTimeMs PushMs(L, c.endMs); // 5 endTimeMs Game::Lua::PushBool(L, IsTradeskill(rec)); // 6 isTradeskill char castGuid[48]; // 7 castID if (Spell::CastEvents::CurrentCastGuid(casterGuid, c.spellID, castGuid, sizeof castGuid)) Game::Lua::PushString(L, castGuid); else Game::Lua::PushNil(L); Game::Lua::PushBool(L, Spell::Interruptible::NotInterruptible( casterUnit, rec, /*isChannel*/ false)); // 8 notInterruptible Game::Lua::PushNumber(L, static_cast(c.spellID)); // 9 castingSpellID Game::Lua::PushNil(L); // 10 castBarID Game::Lua::PushNumber(L, static_cast(c.delayMs)); // 11 delayTimeMs return 11; } // Pushes UnitChannelInfo's 8-tuple. `haveTimes` is false for remote units // (we only track the local player's channel start), pushing nil times. // `casterUnit` is the channeling CGUnit (null when unresolvable), for the // immunity-aura half of `notInterruptible`. // The spell record of a channel the getters report, or null for none. // Self-expire a timed channel once its window elapses (mirrors // ShownCastRecord) — this is how the player's channel clears, since we no // longer poll the +0x228 field. Remote callers gate on endMs before // passing haveTimes, so this is a no-op for them. const uint8_t *ShownChannelRecord(int spellID, int endMs, bool haveTimes) { if (spellID == 0 || HidesChannelBar(spellID)) return nullptr; if (haveTimes && endMs != 0 && Ended(endMs)) return nullptr; return Spell::Lookup::RecordForID(spellID); } int PushChannelInfo(void *L, int spellID, int startMs, int endMs, bool haveTimes, const uint8_t *casterUnit) { const uint8_t *rec = ShownChannelRecord(spellID, endMs, haveTimes); if (rec == nullptr) return 0; const char *name = SpellName(rec); Game::Lua::PushString(L, name); // 1 name Game::Lua::PushString(L, name); // 2 text / displayName Game::Lua::PushString(L, SpellIconPath(rec)); // 3 textureID (path) if (haveTimes) { PushMs(L, startMs); // 4 startTimeMs PushMs(L, endMs); // 5 endTimeMs } else { Game::Lua::PushNil(L); Game::Lua::PushNil(L); } Game::Lua::PushBool(L, IsTradeskill(rec)); // 6 isTradeskill Game::Lua::PushBool(L, Spell::Interruptible::NotInterruptible( casterUnit, rec, /*isChannel*/ true)); // 7 notInterruptible Game::Lua::PushNumber(L, static_cast(spellID)); // 8 spellID return 8; } // ---- Player cast detection (FUN_CAST_START_SET co-hook) ------------------ // The engine's cast-start writer — the client path for a NORMAL cast. We // stamp from its spellID argument the instant the cast begins (zero // latency), *before* the original so any SPELLCAST_START it triggers sees // fresh times. This does NOT fire for a chained same-spell recast — the // caller (Spell_C_CastSpell) bails at `spellID == VAR_CURRENT_CAST_SPELL` // before ever reaching this writer; those are caught by the SMSG_SPELL_START // path instead. The `spellID == 0` (queued-restore / clear) path is left to // the OnWorldTick VAR==0 poll so a restore doesn't prematurely wipe a bar. using CastStartSet_t = void(__fastcall *)(int spellID, int targetState); CastStartSet_t g_origCastStartSet = nullptr; void __fastcall CastStartSet_h(int spellID, int targetState) { if (spellID != 0) { const int dur = CastTimeMs(spellID); if (dur > 0) { const int now = NowMs(); g_cast = TrackedSpell{spellID, now, now + dur, 0}; g_castFromServer = false; // client-tracked; VAR==0 clears it g_castMoveDropped = false; // fresh cast — clear the move-drop latch g_channel.spellID = 0; // a cast supersedes any channel g_castTargetGuid = 0; // target arrives with the confirming packet } // dur == 0: instant (no bar) or channel (handled via +0x228 poll); // leave g_cast — don't clobber an unrelated active cast. } g_origCastStartSet(spellID, targetState); } const Game::HookAutoRegister _castStartHook{ Offsets::FUN_CAST_START_SET, reinterpret_cast(&CastStartSet_h), reinterpret_cast(&g_origCastStartSet)}; // ---- Remote-unit cast tracking (SMSG_SPELL_START) ------------------------ // Vanilla stores no cast record for other units, so we capture their casts // from SMSG_SPELL_START — the only packet carrying another unit's cast with // a server-authoritative cast time. Keyed by caster GUID (a unit casts one // thing at a time). Regular casts back `UnitCastingInfo`; channels add real // times to `UnitChannelInfo` (validated against the live +0x228 field). bool IsChannelSpell(const uint8_t *rec) { return (Game::Read(rec, Offsets::OFF_SPELL_RECORD_ATTRIBUTES_EX) & Offsets::SPELL_ATTR_EX_CHANNELED) != 0; } // Channel duration for a non-player caster — base (skipMod=1), since we // only have the local player's spell-mod tables. int RemoteChannelDurationMs(const uint8_t *rec) { const int ms = reinterpret_cast( Offsets::FUN_GET_SPELL_DURATION)(rec, 0, /*skipMod*/ 1); return ms > 0 ? ms : 0; } struct RemoteCast { uint64_t casterGuid; uint64_t targetGuid; // unit the caster is casting at, 0 if none int spellID; int startMs; int endMs; bool isChannel; bool used; bool confirmed; // channel: +0x228 has shown spellID (PollRemoteChannels) }; constexpr int kRemoteCastSlots = 64; RemoteCast g_remoteCasts[kRemoteCastSlots]; int g_remoteCursor = 0; void StoreRemoteCast(uint64_t caster, uint64_t targetGuid, int spellID, int startMs, int endMs, bool isChannel) { const RemoteCast entry{caster, targetGuid, spellID, startMs, endMs, isChannel, true}; // One active cast per unit — replace any existing entry for this caster. for (auto &e : g_remoteCasts) { if (e.used && e.casterGuid == caster) { e = entry; return; } } const int now = NowMs(); for (auto &e : g_remoteCasts) { if (!e.used || Ended(now, e.endMs)) { e = entry; return; } } g_remoteCasts[g_remoteCursor] = entry; g_remoteCursor = (g_remoteCursor + 1) % kRemoteCastSlots; } const RemoteCast *FindRemoteCast(uint64_t caster) { if (caster == 0) return nullptr; for (const auto &e : g_remoteCasts) { if (e.used && e.casterGuid == caster) return &e; } return nullptr; } // How recently the client-side FUN_CAST_START_SET hook must have stamped the // same spell for an SMSG_SPELL_START to count as that cast's confirming // packet (skip — don't restart the bar) rather than a new chained recast // (stamp). Must exceed worst-case latency but stay under a GCD / min cast // time so genuine chained casts aren't wrongly deduped. constexpr int kCastStartDedupMs = 500; void HandleSpellStart(uint64_t caster, int spellID, uint32_t castTime, uint64_t targetGuid) { if (caster == 0 || spellID == 0) return; const uint8_t *rec = Spell::Lookup::RecordForID(spellID); if (rec == nullptr) return; const int now = NowMs(); const bool channel = IsChannelSpell(rec); if (caster == Unit::Identity::PlayerGuid()) { if (channel && castTime == 0) { // Player channel start. The broadcast +0x228 field that signals // "channeling" is server-pushed and lands ~1 tick (~1s) after the // channel actually begins, so a +0x228 poll shows the bar far too // late — the addon already polled nil at SPELLCAST_CHANNEL_START // and cleared. This packet IS the channel start: stamp g_channel // now with the engine-computed duration. We run before the // original handler that fires SPELLCAST_CHANNEL_START, so the data // is fresh when addons poll. Cleared by self-expiry (PushChannel- // Info) at endMs. (MSG_CHANNEL_START re-stamps moments later with // the server's duration — see ChannelStart_h.) const int dur = ChannelDurationMs(spellID); if (dur > 0) { StampChannel(spellID, now, now + dur); g_castTargetGuid = targetGuid; } return; } // channel && castTime > 0 → a CAST-THEN-CHANNEL spell (Mind Control: // 3 s cast, then a 60 s channel). This packet is the cast phase — // the channel begins only when the cast completes, signaled by // MSG_CHANNEL_START (ChannelStart_h). Fall through to regular cast // handling so the bar shows the 3 s cast, not a premature 60 s // channel (pfUI #9). if (castTime == 0) return; // instant — no cast bar // Backstop for chained same-spell recasts: the engine short-circuits // Spell_C_CastSpell at `spellID == VAR_CURRENT_CAST_SPELL`, so the // client path (FUN_CAST_START_SET hook) never fires for them — this // server packet is the only signal. Dedup against a fresh client stamp // (same spell, started within ~latency): a normal cast's confirming // packet mustn't RESTART its bar; otherwise it's a chained recast the // client bailed on — take it, and flag it so the VAR==0 poll (which // never saw it) won't clear it. if (g_cast.spellID == spellID && !Ended(now, g_cast.endMs) && now - g_cast.startMs < kCastStartDedupMs) { // Confirming packet. Keep startMs (no visual restart), but SNAP the // end time to the server's authoritative castTime. The client stamp // came from FUN_006e3340, whose only caster factor is the cast-speed // multiplier at descriptor +0x22c (UNIT_MOD_CAST_SPEED — spell // haste); the server additionally scales ranged ABILITIES by ranged // attack speed (SpellEntry::GetCastTime's IsRangedSpell branch, // m_modAttackSpeedPct[RANGED_ATTACK] — quiver/Quick Shots), which // the prediction can't see. Two real divergences so far: Volley // (client +0x22c hastes it, server doesn't → bar ended early) and // Steady/Aimed Shot (server ranged-hastes it, client doesn't → bar // ran long, pfUI#43). The server's castTime is reality; reconcile // to it (preserving accumulated pushback). A no-op when they agree; // when they differ, PollPlayer sees the end move and fires // UNIT_SPELLCAST_DELAYED so cast bars re-read the corrected times. g_cast.endMs = g_cast.startMs + static_cast(castTime) + g_cast.delayMs; g_castTargetGuid = targetGuid; // confirming packet carries the target return; } g_cast = TrackedSpell{spellID, now, now + static_cast(castTime), 0}; g_castFromServer = true; g_castMoveDropped = false; // fresh cast — clear the move-drop latch g_channel.spellID = 0; // a cast supersedes any channel g_castTargetGuid = targetGuid; return; } // A channeled spell with a cast time (Mind Control) is in its CAST phase // here — store it as a regular cast so observers see the 3 s bar. The // remote channel phase has no packet (MSG_CHANNEL_START is caster-only); // it surfaces through the unit's broadcast +0x228 field as usual. const bool instantChannel = channel && castTime == 0; if (instantChannel && HidesChannelBar(spellID)) return; // no channel bar — see HidesChannelBar const int endMs = instantChannel ? now + RemoteChannelDurationMs(rec) : now + static_cast(castTime); StoreRemoteCast(caster, targetGuid, spellID, now, endMs, instantChannel); // Phase 2 events: START (cast) or CHANNEL_START (channel). Skip pure // instants (non-channel, castTime 0 → no bar); their SUCCEEDED still fires // from the SPELL_GO hook. if (castTime > 0 || instantChannel) Spell::CastEvents::OnRemoteCastStart(caster, spellID, endMs, instantChannel); } // SMSG_SPELL_START parse. Body (mirrored from nampower's SpellStartHandler): // itemGuid(packed), casterGuid(packed), spellId(u32), castFlags(u16), // castTime(u32), then the SpellCastTargets block: targetMask(u16) and — when // TARGET_FLAG_UNIT is set — a packed target GUID (the unit the cast is aimed // at; the field that backs UnitSpellTargetName). Runs from the // Net::PacketDispatch funnel with the cursor already positioned at the body. void ParseSpellStart(Net::CDataStore *packet) { Net::ReadPackedGuid(packet); // itemGuid (unused) const uint64_t caster = Net::ReadPackedGuid(packet); const int spellID = static_cast(Net::Read(packet)); Net::Read(packet); // castFlags (unused) const uint32_t castTime = Net::Read(packet); // TARGET_FLAG_UNIT is the first field the server writes after the mask, so // the packed GUID sits immediately after it (SpellCastTargets::write). Other // target kinds (ground coords, item) carry no unit GUID → target is 0. const uint16_t targetMask = Net::Read(packet); const uint64_t targetGuid = (targetMask & TARGET_FLAG_UNIT) ? Net::ReadPackedGuid(packet) : 0; HandleSpellStart(caster, spellID, castTime, targetGuid); } // SMSG_SPELL_DELAYED — cast pushback. The server only sends it to the // affected caster, so it's effectively always the local player; extend the // tracked cast's end (and report the accumulated delay as delayTimeMs) so an // addon's bar stretches on damage. Body: guid(u64), delay(u32). void ParseSpellDelayed(Net::CDataStore *packet) { const uint64_t guid = Net::Read(packet); const uint32_t delay = Net::Read(packet); if (delay != 0 && g_cast.spellID != 0 && guid == Unit::Identity::PlayerGuid()) { g_cast.endMs += static_cast(delay); g_cast.delayMs += static_cast(delay); } } // MSG_CHANNEL_START — the server tells the caster a channel has begun, with // the server-authoritative (modifier-applied) duration. Sent only to the // caster, so always the local player. This is what makes CAST-THEN-CHANNEL // spells (Mind Control: 3 s cast, then a 60 s channel) show both phases: // their SMSG_SPELL_START carries the cast (castTime > 0, handled as a regular // cast in HandleSpellStart) and only this packet marks the channel starting. // For instant channels it re-stamps what the SpellStart path just wrote (same // moment, server duration) — harmless. Running from the dispatch funnel keeps // the stamp ahead of the engine's leaf handler (which fires // SPELLCAST_CHANNEL_START), so the data is fresh when addons poll. Body: // spellId(u32), durationMs(u32). void ParseChannelStart(Net::CDataStore *packet) { const int spellID = static_cast(Net::Read(packet)); const uint32_t duration = Net::Read(packet); if (spellID != 0 && duration > 0) { const int now = NowMs(); StampChannel(spellID, now, now + static_cast(duration)); } } // MSG_CHANNEL_UPDATE — sent to the channeling PLAYER on damage pushback // (vanilla channels SHORTEN when hit) and once more at the channel's end // (remaining == 0). Body is a single u32 = the new remaining time (ms); the // spell is the in-progress channel (the packet carries no spellID). The engine // stores the new end nowhere, so re-anchor g_channel.endMs to the server's // remaining time so UnitChannelInfo (and the OnWorldTick endMs self-expiry) // track pushback; on remaining == 0 clear the channel so CHANNEL_STOP fires // promptly (ahead of the ~1 s-lagged +0x228 field). The pushback also // shortened the channel's aura on each hit target server-side // (Spell::DelayedChannel → DelaySpellAuraHolder), so re-anchor the cached // target-side expirations too — else C_UnitAuras reads a pushed-back channel // debuff (Dark Harvest, the drains) late by the accumulated pushback. void ParseChannelUpdate(Net::CDataStore *packet) { if (g_channel.spellID == 0) return; const uint32_t remaining = Net::Read(packet); const int spellID = g_channel.spellID; if (remaining == 0) { g_channel.spellID = 0; // authoritative channel end } else { g_channel.endMs = NowMs() + static_cast(remaining); Aura::Source::RestampPlayerChannel(static_cast(spellID), remaining); Spell::CastEvents::OnPlayerChannelUpdate(spellID); } } // Shared abort handling for the two sibling failure packets. Servers // derived from (v)mangos broadcast BOTH `SMSG_SPELL_FAILURE` and // `SMSG_SPELL_FAILED_OTHER` from Spell::SendInterrupted, but which one a // given core emits per abort path varies — so both handlers are co-hooked // and both feed this. Idempotent, so the both-arrive case is harmless. // // `castSlotOnly` is set for the ClearCastingSpell route: that method clears // the unit's CAST slot (+0xC8C), which the engine's own SPELL_GO handler does // for every spell — including a channel at the moment it STARTS (it passes // notify = `+0x228 == 0`, i.e. "not a stop while the channel field is set"). // A channel's lifetime is the broadcast +0x228 field, which UnitChannelInfo // already gates on, so this route must leave channel entries alone; evicting // them killed the times of every remote instant channel (bandages, issue // #102). The failure packets are real aborts and still evict channels. void HandleCastAborted(uint64_t guid, int spellID, bool castSlotOnly = false) { if (guid == 0 || spellID == 0) return; Unit::IncomingResurrection::OnCastEnded(guid, spellID); // Remote units: invalidate the cached cast/channel so // UnitCastingInfo/UnitChannelInfo stop reporting it this tick. for (auto &e : g_remoteCasts) { if (e.used && e.casterGuid == guid && e.spellID == spellID) { if (castSlotOnly && e.isChannel) break; e.used = false; break; } } // Local player: a server-stamped chained recast (g_castFromServer) // never set VAR_CURRENT_CAST_SPELL, so the WorldTick VAR==0 poll can't // clear it on interrupt — this packet is the only signal. Whether the // caster receives their own broadcast is server-dependent; if it never // fires, the endMs self-expiry backstop still applies. if (guid == Unit::Identity::PlayerGuid()) { // Cast interrupts are surfaced by PollPlayer / SpellFailed_h. Player // CHANNELS never fire INTERRUPTED (retail only fires CHANNEL_STOP for // them, interrupted or not), so there's nothing to do for a channel. if (g_cast.spellID == spellID) { // A movement-immune cast (grenade) whose cast-state the movement // path just cleared isn't really interrupted — the server completes // it (issue #23). Keep tracking it (mark it move-dropped); OnWorldTick // ends it on its SPELL_GO or a grace past its end. A real cancel / // interrupt happens with the player NOT moving, so it clears here. if (!MovementInterruptible(spellID) && PlayerMoving()) g_castMoveDropped = true; else g_cast.spellID = 0; } return; } // Phase 2: remote unit — the poll fires INTERRUPTED + STOP for it. Spell::CastEvents::OnRemoteAborted(guid, spellID); } // SMSG_SPELL_FAILURE / SMSG_SPELL_FAILED_OTHER — sibling abort packets. The // server broadcasts one (which varies by core / abort path) to observers when // a started cast aborts (interrupt, death, movement, fizzle); handling both // makes eviction independent of which the server chose. Both share the head // casterGuid(u64), spellId(u32) — plain, not packed (SpellFailure has a // trailing result(u8) we don't read). Drops a remote cast the moment it dies // instead of letting the ghost bar run to its computed end. void ParseCastAborted(Net::CDataStore *packet) { const uint64_t guid = Net::Read(packet); const int spellID = static_cast(Net::Read(packet)); HandleCastAborted(guid, spellID); } // Co-hook on CGUnit_C::ClearCastingSpell — the engine choke point every // "unit stopped casting" path funnels through (failure packets, SPELL_GO, // movement, death, animation events, local cancel — see Offsets.h). The // original is gated on `spellID == [unit+0xC8C]` (the unit's live // current-cast field), so a real stop only happens when the gate passes; // we mirror the gate and evict then. This is the PRIMARY interrupt signal // on Turtle: its core doesn't broadcast either failure packet — interrupt // propagation to observers happens via SuperWoW, whose code calls this // method directly (verified by return-address tracing: interrupted remote // casts clear with a caller inside a sibling DLL, natural completions // clear from the SPELL_GO handler). Verified in-game: Kick / Earth Shock / // Counterspell each evict the victim's cast, while non-interrupting damage // (pushback flinch) does NOT route through here — no false eviction. using ClearCastingSpell_t = void(__fastcall *)(void *unit, void *edx, int spellID, char notify, char cleanup); ClearCastingSpell_t g_origClearCastingSpell = nullptr; void __fastcall ClearCastingSpell_h(void *unit, void *edx, int spellID, char notify, char cleanup) { if (unit != nullptr && spellID != 0) { const int current = Game::Read(unit, Offsets::OFF_UNIT_CAST_SPELL); if (current != 0 && current == spellID) HandleCastAborted(Unit::Identity::GuidForObject(unit), spellID, /*castSlotOnly*/ true); } g_origClearCastingSpell(unit, edx, spellID, notify, cleanup); } const Game::HookAutoRegister _clearCastingHook{ Offsets::FUN_UNIT_CLEAR_CASTING_SPELL, reinterpret_cast(&ClearCastingSpell_h), reinterpret_cast(&g_origClearCastingSpell)}; // One subscriber on the SMSG dispatch funnel, replacing the six per-opcode // co-hooks these spell packets used to install (SpellStart / Delayed / // ChannelStart / ChannelUpdate / Failure / FailedOther) — every one of which // nampower also detours. The dispatcher hands us the cursor at the body (the // opcode already consumed). CastStartSet and ClearCastingSpell stay direct // hooks: they're engine methods, not SMSG handlers, so the funnel can't reach // them (and ClearCastingSpell is the SuperWoW-shared interrupt choke point). void SpellPacketSub(uint32_t opcode, Net::CDataStore *packet) { if (packet == nullptr) return; switch (opcode) { case Offsets::SMSG_SPELL_START: ParseSpellStart(packet); break; case Offsets::SMSG_SPELL_DELAYED: ParseSpellDelayed(packet); break; case Offsets::SMSG_SPELL_CHANNEL_START: ParseChannelStart(packet); break; case Offsets::SMSG_SPELL_CHANNEL_UPDATE: ParseChannelUpdate(packet); break; case Offsets::SMSG_SPELL_FAILURE: case Offsets::SMSG_SPELL_FAILED_OTHER: ParseCastAborted(packet); break; default: break; } } const Net::PacketDispatch::AutoSubscribe _spellPacketSub{&SpellPacketSub}; // The remote analog of the player's +0x228 latch in OnWorldTick (see there). // The field lags the channel start by up to ~1 s, so until it has shown the // spell the SMSG_SPELL_START stamp is the truth — UnitChannelInfo reporting // nothing in that window made it disagree with the CHANNEL_START event // observers had just received. Once confirmed, the field leaving the spell // before endMs is the channel ending early (interrupt, cancel, movement), // which no packet reports to observers on Turtle: drop the entry and let the // events poll fire CHANNEL_STOP. Past endMs the entry is left to the natural // end, since the field's own clear trails it. Casters out of view are // skipped and self-expire at endMs. void PollRemoteChannels() { const int now = NowMs(); for (auto &e : g_remoteCasts) { if (!e.used || !e.isChannel || now - e.endMs >= 0) continue; const auto *unit = static_cast( Object::ByGuid(Offsets::TYPEMASK_UNIT, e.casterGuid)); if (unit == nullptr) continue; const auto *desc = Game::Read(unit, Offsets::OFF_UNIT_DESCRIPTOR); if (desc == nullptr) continue; const int chan = Game::Read(desc, Offsets::OFF_UNIT_FIELD_CHANNEL_SPELL); if (chan == e.spellID) { e.confirmed = true; } else if (e.confirmed) { e.used = false; Spell::CastEvents::OnRemoteAborted(e.casterGuid, e.spellID); } } } // UNIT_SPELLCAST_INTERRUPTIBLE / _NOT_INTERRUPTIBLE. 4.3.4 re-runs its // not-interruptible test when the target's or focus's auras change mid-cast and // signals when the answer flips (CGUnit_C aura update → ProcessCastNotInterruptible // → Script_SendUnitSignal). `Spell::Interruptible` answers lazily from the // caster's live auras, so the same flip is seen by comparing the answer for one // cast from frame to frame. A new cast only sets the baseline: its first answer // is read from UnitCastingInfo at START, not signalled. struct InterruptWatch { uint64_t guid; int spellID; int startMs; bool notInterruptible; }; InterruptWatch g_interruptWatch[2]; // target, focus // The cast or channel `guid` is in now: spell, start (the cast's identity) and // whether it is a channel. False when it isn't casting. bool CurrentCastOf(uint64_t guid, const uint8_t *unit, int *spellID, int *startMs, bool *isChannel) { const int now = NowMs(); if (guid == Unit::Identity::PlayerGuid()) { if (g_cast.spellID != 0 && !Ended(now, g_cast.endMs)) { *spellID = g_cast.spellID, *startMs = g_cast.startMs, *isChannel = false; return true; } if (g_channel.spellID != 0 && (g_channel.endMs == 0 || !Ended(now, g_channel.endMs))) { *spellID = g_channel.spellID, *startMs = g_channel.startMs, *isChannel = true; return true; } return false; } if (const RemoteCast *rc = FindRemoteCast(guid); rc != nullptr && !Ended(now, rc->endMs)) { *spellID = rc->spellID, *startMs = rc->startMs, *isChannel = rc->isChannel; return true; } // A channel seen only through the broadcast field: no start time, so the // spell alone identifies it. auto *desc = Game::Read(unit, Offsets::OFF_UNIT_DESCRIPTOR); const int chan = desc != nullptr ? Game::Read(desc, Offsets::OFF_UNIT_FIELD_CHANNEL_SPELL) : 0; if (chan == 0) return false; *spellID = chan, *startMs = 0, *isChannel = true; return true; } void PollInterruptible() { if (!Spell::CastEvents::InterruptibleWatched()) { for (auto &w : g_interruptWatch) w = InterruptWatch{}; return; } const uint64_t guids[2] = { Game::Read(static_cast(Offsets::VAR_CURRENT_SELECTION_GUID_LO)), Unit::Focus::Get()}; for (int i = 0; i < 2; ++i) { InterruptWatch &w = g_interruptWatch[i]; const uint64_t guid = guids[i]; // A focus that is also the target is watched once, under the target. const auto *unit = (guid != 0 && !(i == 1 && guid == guids[0])) ? static_cast( Object::ByGuid(Offsets::TYPEMASK_UNIT, guid)) : nullptr; int spellID = 0, startMs = 0; bool isChannel = false; const uint8_t *rec = nullptr; if (unit != nullptr && CurrentCastOf(guid, unit, &spellID, &startMs, &isChannel)) rec = Spell::Lookup::RecordForID(spellID); if (rec == nullptr) { w = InterruptWatch{}; continue; } const bool notInterruptible = Spell::Interruptible::NotInterruptible(unit, rec, isChannel); const bool sameCast = w.guid == guid && w.spellID == spellID && w.startMs == startMs; if (sameCast && w.notInterruptible != notInterruptible) Spell::CastEvents::OnInterruptibleChanged(guid, notInterruptible); w = InterruptWatch{guid, spellID, startMs, notInterruptible}; } } } // namespace // Per-frame upkeep for the player. The regular cast is stamped by the // FUN_CAST_START_SET hook; here we clear it when the engine's cast global // drops to 0, and poll the broadcast +0x228 field for the channel (which // has no comparable client-side start hook). void OnWorldTick() { // Clear a CLIENT-tracked cast when the cast global returns to 0 — covering // normal end, cancel, and interrupt. No grace window is needed: the // FUN_CAST_START_SET hook stamps g_cast in the same call that writes the // global non-zero, so it's never transiently 0 right after a stamp. // Server-stamped chained casts (g_castFromServer) never set the global, // so they're exempt — they expire on their computed endMs (self-expiry in // PushCastInfo). if (g_cast.spellID != 0 && !g_castFromServer && Game::Read(Offsets::VAR_CURRENT_CAST_SPELL) == 0) g_cast.spellID = 0; // End a move-dropped cast (issue #23): the engine cleared its cast-state on // movement, but the server completes it. HandleCastAborted kept it tracked; // end it once its SPELL_GO has landed (→ PollPlayer STOP, no INTERRUPTED) or, // if none ever comes, a grace past its computed end (→ honest INTERRUPTED + // STOP). Gated on the latch, so normal casts are untouched. if (g_cast.spellID != 0 && g_castMoveDropped && (Spell::CastEvents::PlayerCastSucceeded() || Time::Clock::Reached(static_cast(g_cast.endMs + kMoveDroppedGraceMs)))) { g_cast.spellID = 0; g_castMoveDropped = false; } // A target-selection spell (Disenchant, ground-target AoE, …) stamps // g_cast at the green-cursor step, NOT when the cast actually begins: // Spell_C_CastSpell (0x006E4B60) calls the cast-start writer // (FUN_CAST_START_SET) BEFORE Spell_C_TargetSpell raises the reticle, and // the later item/ground click re-enters Spell_C_CastSpell only to hit its // "already this spell" short-circuit — so the writer never re-fires for the // real start. Left alone, g_cast would surface a premature cast (UnitCasting- // Info + UNIT_SPELLCAST_START) during targeting, seconds early, and a cancel // would even fire a phantom START/INTERRUPTED/STOP. So drop the client stamp // while the reticle is up for that spell: on selection the server's // SMSG_SPELL_START re-stamps it cleanly (g_cast is 0, so HandleSpellStart // takes the fresh-stamp path and START fires with the correct time + the // SENT castGUID); on cancel it simply stays cleared. Gated on // !g_castFromServer so it can never touch that server re-stamp. if (g_cast.spellID != 0 && !g_castFromServer && Game::Read(Offsets::VAR_SPELL_TARGETING_FLAGS) != 0 && g_cast.spellID == Game::Read(Offsets::VAR_PENDING_CAST_SPELL)) g_cast.spellID = 0; // Detect a player CHANNEL that ended before its computed endMs — the // summon-ritual case (UNIT_FIELD_CHANNEL_SPELL = 698, cleared when the // summon fills), and equally an interrupted / cancelled / LoS-broken // channel. Without this, g_channel only clears at endMs (self-expiry in // PushChannelInfo) or when a later TIMED cast supersedes it — so an early // end followed by an INSTANT cast (which supersedes nothing) leaves a // ghost bar counting down to a duration that no longer reflects reality. // // We deliberately don't use +0x228 to START the bar — it's a broadcast // UpdateField that lags the channel start by ~1 tick (HandleSpellStart // stamps from the packet instead). For the STOP that lag is harmless, but a // naive "clear when field == 0" would wipe a channel we just stamped during // the lag window. Guard with a latch: only honor the 0 once the field has // been seen to reflect our channel spell at least once. A channel too short // to ever appear in the field just self-expires at endMs. if (g_channel.spellID != 0) { const uint8_t *desc = Unit::Identity::PlayerDescriptor(); if (desc != nullptr) { const int chan = Game::Read( desc, Offsets::OFF_UNIT_FIELD_CHANNEL_SPELL); if (chan == g_channel.spellID) g_channelConfirmed = true; else if (chan == 0 && g_channelConfirmed) g_channel.spellID = 0; } // Self-expiry backstop for the poll: the +0x228 broadcast clears ~1 // tick (~1s) late, so a channel that runs its full duration would // otherwise linger until then — the poll's CHANNEL_STOP (and any // g_channel reader) would trail the real end by ~1s. Clear at the // server-computed endMs too, so the effective stop is the EARLIER of // (computed end, early +0x228 clear — the summon-ritual / interrupt // case). Mirrors PushChannelInfo's endMs guard, which already hides // the channel from UnitChannelInfo at this same instant. // if (g_channel.spellID != 0 && g_channel.endMs != 0 && Ended(g_channel.endMs)) g_channel.spellID = 0; } // Derive the player's UNIT_SPELLCAST_* events from the cast/channel // state above, now that this tick's clears have been applied. Poll- // based so it needs no changes to the many stamp/clear sites; ~1 frame // latency is imperceptible for a cast bar, and every fire is // listener-gated (near-free when no addon uses these). Spell::CastEvents::PollPlayer(g_cast.spellID, g_cast.startMs, g_cast.endMs, g_channel.spellID, g_channel.startMs); PollRemoteChannels(); Spell::CastEvents::PollRemote(); Spell::CastEvents::PollReticle(); PollInterruptible(); } static const Tick::WorldTick::AutoSubscribe _tickSub{&OnWorldTick}; // `CastingInfo()` — local player's cast, no token lookup. static int __fastcall Script_CastingInfo(void *L) { return PushCastInfo(L, g_cast, Unit::Identity::PlayerGuid(), Unit::Identity::PlayerObject()); } // The unit a `(unit)` getter names in argument 1, or null when it names // none. Raises `usage` for a non-string. static void *UnitArg(void *L, const char *usage) { if (!Game::Lua::IsString(L, 1)) { Game::Lua::Error(L, usage); return nullptr; } const char *token = Game::Lua::ToString(L, 1); return (token != nullptr) ? Resolve(token) : nullptr; } // The regular cast unit `u` has, as UnitCastingInfo reports it: the local // player from self-tracking; other units from the SMSG_SPELL_START cache // (regular casts still within their cast window; aborted casts are evicted by // the ClearCastingSpell / failure-packet co-hooks). spellID 0 when none. struct UnitCast { TrackedSpell spell; uint64_t guid; const uint8_t *unit; }; static UnitCast FindUnitCast(void *u) { if (u == Resolve("player")) return {g_cast, Unit::Identity::PlayerGuid(), Unit::Identity::PlayerObject()}; const uint64_t guid = Unit::Identity::GuidForObject(u); const RemoteCast *rc = FindRemoteCast(guid); if (rc != nullptr && !rc->isChannel && !Ended(rc->endMs)) return {TrackedSpell{rc->spellID, rc->startMs, rc->endMs}, guid, static_cast(u)}; return {TrackedSpell{0, 0, 0, 0}, guid, static_cast(u)}; } // `UnitCastingInfo(unit)`. static int __fastcall Script_UnitCastingInfo(void *L) { void *u = UnitArg(L, "Usage: C_Spell.UnitCastingInfo(\"unit\")"); if (u == nullptr) return 0; const UnitCast c = FindUnitCast(u); return PushCastInfo(L, c.spell, c.guid, c.unit); } // Pushes a duration object for [startMs, endMs] in GetTime() seconds. static int PushDuration(void *L, int startMs, int endMs) { Duration::Object::PushFromStart(L, static_cast(static_cast(startMs)) * 0.001, static_cast(endMs - startMs) * 0.001); return 1; } // `UnitCastingDuration(unit)` — the cast UnitCastingInfo reports, as a new // duration object each call, or nothing. The object is a snapshot: retail's // keeps the times it was made with after the cast ends (probe 2026-11-01), so // pushback reaches only a later call. Pushback taken so far is in it, as in // UnitCastingInfo's end time. static int __fastcall Script_UnitCastingDuration(void *L) { void *u = UnitArg(L, "Usage: UnitCastingDuration(\"unit\")"); if (u == nullptr) return 0; const TrackedSpell c = FindUnitCast(u).spell; if (ShownCastRecord(c) == nullptr) return 0; return PushDuration(L, c.startMs, c.endMs); } // `ChannelInfo()` — local player's channel, no token lookup. static int __fastcall Script_ChannelInfo(void *L) { return PushChannelInfo(L, g_channel.spellID, g_channel.startMs, g_channel.endMs, true, Unit::Identity::PlayerObject()); } // The channel unit `u` has, as UnitChannelInfo reports it: full timing for // the player; for other units, full timing from the SpellStart cache, else // spellID (no times) from the broadcast +0x228 field. spellID 0 when none. struct UnitChannel { int spellID; int startMs; int endMs; bool haveTimes; const uint8_t *unit; }; static UnitChannel FindUnitChannel(void *u) { if (u == Resolve("player")) return {g_channel.spellID, g_channel.startMs, g_channel.endMs, true, Unit::Identity::PlayerObject()}; auto *unit = static_cast(u); auto *desc = Game::Read(unit, Offsets::OFF_UNIT_DESCRIPTOR); if (desc == nullptr) return {0, 0, 0, false, unit}; // A cached channel stands while +0x228 shows it, or reads 0 because it // hasn't caught up yet (unconfirmed — PollRemoteChannels drops the entry // once a confirmed field clears). Otherwise the field alone answers. const int spellID = Game::Read(desc, Offsets::OFF_UNIT_FIELD_CHANNEL_SPELL); const RemoteCast *rc = FindRemoteCast(Unit::Identity::GuidForObject(unit)); if (rc != nullptr && rc->isChannel && !Ended(rc->endMs) && (spellID == rc->spellID || (spellID == 0 && !rc->confirmed))) return {rc->spellID, rc->startMs, rc->endMs, true, unit}; return {spellID, 0, 0, false, unit}; } // `UnitChannelInfo(unit)`. static int __fastcall Script_UnitChannelInfo(void *L) { void *u = UnitArg(L, "Usage: C_Spell.UnitChannelInfo(\"unit\")"); if (u == nullptr) return 0; const UnitChannel c = FindUnitChannel(u); return PushChannelInfo(L, c.spellID, c.startMs, c.endMs, c.haveTimes, c.unit); } // `UnitChannelDuration(unit)` — the channel UnitChannelInfo reports, as a // new snapshot duration object each call (see UnitCastingDuration), or // nothing. Also nothing for a unit whose channel we did not see begin, which // has no times. static int __fastcall Script_UnitChannelDuration(void *L) { void *u = UnitArg(L, "Usage: UnitChannelDuration(\"unit\")"); if (u == nullptr) return 0; const UnitChannel c = FindUnitChannel(u); if (!c.haveTimes || c.endMs == 0 || ShownChannelRecord(c.spellID, c.endMs, c.haveTimes) == nullptr) return 0; return PushDuration(L, c.startMs, c.endMs); } // The unit GUID `caster` is currently casting / channeling AT, or 0 when it // isn't casting or the spell has no unit target (self / ground / item). The // player comes from g_cast / g_channel + g_castTargetGuid, gated on an active // cast so a stale target from a finished cast is never returned; other units // come from the SMSG_SPELL_START cache while inside their cast window. static uint64_t TargetGuidForCaster(uint64_t caster) { if (caster == 0) return 0; if (caster == Unit::Identity::PlayerGuid()) { const int now = NowMs(); const bool casting = g_cast.spellID != 0 && !Ended(now, g_cast.endMs); const bool channeling = g_channel.spellID != 0 && (g_channel.endMs == 0 || !Ended(now, g_channel.endMs)); return (casting || channeling) ? g_castTargetGuid : 0; } const RemoteCast *rc = FindRemoteCast(caster); return (rc != nullptr && !Ended(rc->endMs)) ? rc->targetGuid : 0; } // `UnitSpellTargetName(unit)` — name of the unit that `unit` is currently // casting / channeling a spell at, or nil when `unit` isn't casting, the spell // has no unit target (self / ground / item), or the target's name can't be // resolved (an off-screen stranger). ClassicAPI extension; the cast's target // comes from the SMSG_SPELL_START target block, captured for the player and // any remote unit whose cast we've observed. static int __fastcall Script_UnitSpellTargetName(void *L) { if (!Game::Lua::IsString(L, 1)) { Game::Lua::Error(L, "Usage: UnitSpellTargetName(\"unit\")"); return 0; } const char *token = Game::Lua::ToString(L, 1); const uint64_t caster = (token != nullptr) ? Unit::Identity::GuidForToken(token) : 0; const uint64_t targetGuid = TargetGuidForCaster(caster); char name[64]; if (targetGuid == 0 || !Player::Info::NameFromGuid(targetGuid, name, sizeof name)) { Game::Lua::PushNil(L); return 1; } Game::Lua::PushString(L, name); return 1; } // --- Documentation ---------------------------------------------------------- static const Game::Doc::Field kUnitArgs[] = { Game::Doc::Req("unit", "UnitToken", "The unit to read."), }; // Both cast getters push the same 11 values, and push nothing at all when // there is no cast — so every return is nilable. static const Game::Doc::Field kCastingInfoRets[] = { Game::Doc::Opt("name", "string", nullptr, "Localized spell name; all returns are nil when the unit is not casting."), Game::Doc::Opt("displayName", "string", nullptr, "Same as name."), Game::Doc::Opt("textureID", "string", nullptr, "Icon texture path."), Game::Doc::Opt("startTimeMs", "number", nullptr, "When the cast began, on the same clock as GetTime times 1000."), Game::Doc::Opt("endTimeMs", "number", nullptr, "When the cast ends, on the same clock."), Game::Doc::Opt("isTradeskill", "bool", nullptr, "True for a profession recipe cast."), Game::Doc::Opt("castID", "string", nullptr, "The cast GUID the UNIT_SPELLCAST events carry for this cast."), Game::Doc::Opt("notInterruptible", "bool", nullptr, "True when no interrupt or silence you know can stop the cast."), Game::Doc::Opt("castingSpellID", "number", nullptr, "The spell being cast."), Game::Doc::Opt("castBarID", "number", nullptr, "Always nil."), Game::Doc::Opt("delayTimeMs", "number", nullptr, "Pushback the cast has taken so far, in milliseconds."), }; static const Game::Doc::Function kUnitCastingInfo{ "The regular cast a unit has in progress, with its start and end times.", kUnitArgs, kCastingInfoRets}; static const Game::Doc::Function kCastingInfo{ "The regular cast the player has in progress, with its start and end times.", {}, kCastingInfoRets}; // Both channel getters push the same 8 values, and nothing when there is no // channel. Remote units whose channel we did not see begin report nil times. static const Game::Doc::Field kChannelInfoRets[] = { Game::Doc::Opt("name", "string", nullptr, "Localized spell name; all returns are nil when the unit is not channeling."), Game::Doc::Opt("displayName", "string", nullptr, "Same as name."), Game::Doc::Opt("textureID", "string", nullptr, "Icon texture path."), Game::Doc::Opt("startTimeMs", "number", nullptr, "When the channel began, on the same clock as GetTime times 1000; " "nil for a unit whose channel you did not see begin."), Game::Doc::Opt("endTimeMs", "number", nullptr, "When the channel ends, on the same clock."), Game::Doc::Opt("isTradeskill", "bool", nullptr, "True for a profession recipe cast."), Game::Doc::Opt("notInterruptible", "bool", nullptr, "True when no interrupt or silence you know can stop the channel."), Game::Doc::Opt("spellID", "number", nullptr, "The spell being channeled."), }; static const Game::Doc::Function kUnitChannelInfo{ "The channel a unit has in progress, with its start and end times.", kUnitArgs, kChannelInfoRets}; static const Game::Doc::Function kChannelInfo{ "The channel the player has in progress, with its start and end times.", {}, kChannelInfoRets}; static const Game::Doc::Field kCastingDurationRets[] = { Game::Doc::Req("duration", "LuaDurationObject", "A new object each call that keeps these times; nothing is returned when the unit is not " "casting."), }; static const Game::Doc::Function kUnitCastingDuration{ "The time span of the regular cast a unit has in progress.", kUnitArgs, kCastingDurationRets, "UnitGlobals"}; static const Game::Doc::Field kChannelDurationRets[] = { Game::Doc::Req("duration", "LuaDurationObject", "A new object each call that keeps these times; nothing is returned when the unit is not " "channeling, or for a unit whose channel you did not see begin."), }; static const Game::Doc::Function kUnitChannelDuration{ "The time span of the channel a unit has in progress.", kUnitArgs, kChannelDurationRets, "UnitGlobals"}; static const Game::Doc::Field kTargetNameRets[] = { Game::Doc::Opt("targetName", "string", nullptr, "Nil when the unit is not casting, the spell has no unit target, " "or the name cannot be resolved."), }; static const Game::Doc::Function kUnitSpellTargetName{ "The name of the unit that a unit is casting or channeling a spell at.", kUnitArgs, kTargetNameRets, "SpellGlobals", true}; static void RegisterLuaFunctions() { // Registered under C_Spell rather than as globals to avoid clobbering // the global `UnitCastingInfo` / `UnitChannelInfo` names. Addons that // ship their own vanilla cast-tracking libraries use the // `_G.UnitCastingInfo or ` idiom (e.g. ShaguTweaks' // libcast.lua scrapes the combat log for remote/enemy casts the 1.12 // engine never exposes). Occupying the global makes them adopt our // player-only version and drop their superior fallback — so we cede // the global names and expose the functions here instead. Game::Lua::RegisterTableFunction("C_Spell", "UnitCastingInfo", &Script_UnitCastingInfo, &kUnitCastingInfo); Game::Lua::RegisterTableFunction("C_Spell", "CastingInfo", &Script_CastingInfo, &kCastingInfo); Game::Lua::RegisterTableFunction("C_Spell", "UnitChannelInfo", &Script_UnitChannelInfo, &kUnitChannelInfo); Game::Lua::RegisterTableFunction("C_Spell", "ChannelInfo", &Script_ChannelInfo, &kChannelInfo); // Retail's names. No 1.12 addon ships its own, so they take the globals. Game::Lua::RegisterGlobalFunction("UnitCastingDuration", &Script_UnitCastingDuration, &kUnitCastingDuration); Game::Lua::RegisterGlobalFunction("UnitChannelDuration", &Script_UnitChannelDuration, &kUnitChannelDuration); // A novel ClassicAPI name (no addon ships its own), so it's safe as a // global — and it matches the `UnitSpellTargetName(unit)` call shape. Game::Lua::RegisterGlobalFunction("UnitSpellTargetName", &Script_UnitSpellTargetName, &kUnitSpellTargetName); } static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; } // namespace Spell::Cast