From 3003b1129e544dfe458698eb18eda9977c06e7cc Mon Sep 17 00:00:00 2001
From: Brues <5278969+brues-code@users.noreply.github.com>
Date: Sun, 21 Jun 2026 16:20:30 -0500
Subject: [PATCH] Add C_Spell.GetSpellPowerCost(spellIdentifier)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Returns a SpellPowerCostInfo[] (one entry in vanilla — spells have a
single power cost), or nil for an unknown / no-cost spell. The `cost` is
the player-effective value via the engine's own cost helper FUN_006e31b0
(base + level + ManaCostPercent-of-resource + descriptor power-cost mods
+ the cost SpellMod, op 0xE) — so it matches what casting actually
deducts, talents included.
Verified in-game: Blizzard (10187) on a Mage with 3/3 Frost Channeling
returns cost 1190 = 1400 base x 0.85 (15% reduction).
Keeps C_Spell.GetSpellInfo's `cost` field as the base ManaCost (intrinsic
spell data); GetSpellPowerCost is the player-modified counterpart — same
base/modified split as GetSpellRadius. Calling the full engine helper
beats replicating op 14 via Spell::Mod, since the helper also folds in
the descriptor power-cost modifiers.
Fields: type (Enum.PowerType), name (power token), cost, minCost (==cost,
no optional cost in vanilla), costPercent, costPerSec 0, requiredAuraID 0,
hasRequiredAura false.
Docs: API.md section + README Spell row.
---
README.md | 2 +-
docs/API.md | 38 ++++++++++++++
src/Offsets.h | 10 ++++
src/spell/PowerCost.cpp | 109 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 158 insertions(+), 1 deletion(-)
create mode 100644 src/spell/PowerCost.cpp
diff --git a/README.md b/README.md
index 9124b776..5617ebf1 100644
--- a/README.md
+++ b/README.md
@@ -47,7 +47,7 @@ Full per-function reference: **[docs/API.md](docs/API.md)**.
| [NamePlate](docs/API.md#nameplate) | `C_NamePlate.GetNamePlateForGUID`, `C_NamePlate.GetNamePlateForUnit`, `C_NamePlate.GetNamePlateGUIDs`, `C_NamePlate.GetNamePlates` |
| [NameCache](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`, `UnitNameFromGUID` |
| [Quest](docs/API.md#quest) | `C_QuestLog.GetQuestDetails`, `C_QuestLog.GetQuestIDForLogIndex`, `C_QuestLog.GetTitleForQuestID`, `C_QuestLog.IsOnQuest`, `C_QuestLog.IsQuestDataCachedByID`, `C_QuestLog.IsUnitOnQuest`, `C_QuestLog.RequestLoadQuestByID`, `GetQuestLogLeaderBoardID` |
-| [Spell](docs/API.md#spell) | `C_Spell.CancelSpellByID`, `C_Spell.CastAtCursor`, `C_Spell.DoesSpellExist`, `C_Spell.GetSchoolString`, `C_Spell.GetSpellCooldown`, `C_Spell.GetSpellDescription`, `C_Spell.GetSpellInfo`, `C_Spell.GetSpellLink`, `C_Spell.GetSpellMechanicByID`, `C_Spell.GetSpellName`, `C_Spell.GetSpellRadius`, `C_Spell.GetSpellReagents`, `C_Spell.GetSpellSubtext`, `C_Spell.GetSpellTexture`, `C_Spell.IsAutoAttackSpell`, `C_Spell.IsCurrentSpell`, `C_Spell.IsRangedAutoAttackSpell`, `C_Spell.IsSelfBuff`, `C_Spell.IsSpellHarmful`, `C_Spell.IsSpellHelpful`, `C_Spell.IsSpellPassive`, `C_Spell.IsSpellUsable`, `C_Spell.SpellHasRange`, `CancelSpellByName`, `CastingInfo`, `CastSpellNoToggle`, `ChannelInfo`, `GetCraftSpellID`, `GetSpellInfo`, `GetSpellLink`, `GetSpellRadius`, `GetSpellSchool`, `IsHarmfulSpell`, `IsHelpfulSpell`, `IsPassiveSpell`, `IsPlayerSpell`, `IsSpellKnown`, `IsUsableSpell`, `SpellHasRange`, `UnitCastingInfo`, `UnitChannelInfo` |
+| [Spell](docs/API.md#spell) | `C_Spell.CancelSpellByID`, `C_Spell.CastAtCursor`, `C_Spell.DoesSpellExist`, `C_Spell.GetSchoolString`, `C_Spell.GetSpellCooldown`, `C_Spell.GetSpellDescription`, `C_Spell.GetSpellInfo`, `C_Spell.GetSpellLink`, `C_Spell.GetSpellMechanicByID`, `C_Spell.GetSpellName`, `C_Spell.GetSpellPowerCost`, `C_Spell.GetSpellRadius`, `C_Spell.GetSpellReagents`, `C_Spell.GetSpellSubtext`, `C_Spell.GetSpellTexture`, `C_Spell.IsAutoAttackSpell`, `C_Spell.IsCurrentSpell`, `C_Spell.IsRangedAutoAttackSpell`, `C_Spell.IsSelfBuff`, `C_Spell.IsSpellHarmful`, `C_Spell.IsSpellHelpful`, `C_Spell.IsSpellPassive`, `C_Spell.IsSpellUsable`, `C_Spell.SpellHasRange`, `CancelSpellByName`, `CastingInfo`, `CastSpellNoToggle`, `ChannelInfo`, `GetCraftSpellID`, `GetSpellInfo`, `GetSpellLink`, `GetSpellRadius`, `GetSpellSchool`, `IsHarmfulSpell`, `IsHelpfulSpell`, `IsPassiveSpell`, `IsPlayerSpell`, `IsSpellKnown`, `IsUsableSpell`, `SpellHasRange`, `UnitCastingInfo`, `UnitChannelInfo` |
| [SpellBook](docs/API.md#spellbook) | `C_SpellBook.GetCurrentLevelSpells`, `C_SpellBook.GetSpellLevelLearned`, `C_SpellBook.GetSpellSkillLine`, `C_SpellBook.IsAutoAttackSpellBookItem`, `C_SpellBook.IsRangedAutoAttackSpellBookItem`, `FindSpellBookSlotByID` |
| [State](docs/API.md#state) | `CancelShapeshiftForm`, `Dismount`, `GetMirrorTimerInfo`, `GetMirrorTimerProgress`, `GetShapeshiftFormID`, `IsAssistingRitual`, `IsFalling`, `IsInGroup`, `IsInRaid`, `IsLoggedIn`, `IsMounted`, `IsStealthed`, `IsSwimming` |
| [Table](docs/API.md#table) | `table.wipe` |
diff --git a/docs/API.md b/docs/API.md
index 87c5838b..a39abe76 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -281,6 +281,7 @@ build instructions.
- [`C_Spell.GetSpellDescription(spellID)`](#c_spellgetspelldescriptionspellid)
- [`C_Spell.GetSpellMechanicByID(spellID)`](#c_spellgetspellmechanicbyidspellid)
- [`C_Spell.GetSpellRadius(spellID)` / `GetSpellRadius(slot, bookType)`](#c_spellgetspellradiusspellid--getspellradiusslot-booktype)
+ - [`C_Spell.GetSpellPowerCost(spellIdentifier)`](#c_spellgetspellpowercostspellidentifier)
- [`C_Spell.GetSpellReagents(spellID)`](#c_spellgetspellreagentsspellid)
- [`C_Spell.GetSpellSubtext(spellIdentifier)`](#c_spellgetspellsubtextspellidentifier)
- [`IsPassiveSpell(spellID)` / `IsPassiveSpell(slot, bookType)`](#ispassivespellspellid--ispassivespellslot-booktype)
@@ -6928,6 +6929,43 @@ across the three effects is returned.
> another caster's talents). All DBCs / mod tables are resident from
> boot, so the call is synchronous with no caching.
+### `C_Spell.GetSpellPowerCost(spellIdentifier)`
+
+Returns an array of `SpellPowerCostInfo` tables, or `nil` if the spell
+isn't found or has no resource cost. Vanilla spells have exactly one
+power cost, so the array holds at most one entry.
+
+```lua
+C_Spell.GetSpellPowerCost(10187) -- Blizzard, Mage with 3/3 Frost Channeling
+-- { [1] = { type = 0, name = "MANA", cost = 1190, minCost = 1190,
+-- costPercent = 0, costPerSec = 0, requiredAuraID = 0,
+-- hasRequiredAura = false } }
+-- base cost 1400, 15% reduction applied -> 1190
+```
+
+| Field | Meaning |
+|-------|---------|
+| `type` | `Enum.PowerType` — `0` mana, `1` rage, `2` focus, `3` energy, `4` happiness (the spell's `PowerType`) |
+| `name` | power token (`"MANA"`, `"RAGE"`, …) |
+| `cost` | **effective** cost for the local player |
+| `minCost` | `== cost` (vanilla has no optional-cost component) |
+| `costPercent` | the spell's `%`-of-base-resource cost, or `0` for a flat cost |
+| `costPerSec` | `0` (vanilla doesn't expose a per-second channel cost in these terms) |
+| `requiredAuraID` | `0` (no form/aura-conditional costs in 1.12) |
+| `hasRequiredAura` | `false` |
+
+`cost` is the value the engine actually charges — base + level scaling +
+`ManaCostPercent`-of-resource + descriptor power-cost mods + the cost
+SpellMod (talents like Frost Channeling). It comes from the engine's own
+cost helper (`FUN_006e31b0`), so it stays in lockstep with what casting
+the spell deducts.
+
+> **Effective vs. base.** This returns the player-modified cost.
+> [`C_Spell.GetSpellInfo`](#c_spellgetspellinfospellid)'s `cost` field is
+> the **base** `ManaCost` (intrinsic spell data, caster-independent) —
+> same base/modified split as
+> [`GetSpellRadius`](#c_spellgetspellradiusspellid--getspellradiusslot-booktype).
+
### `C_Spell.GetSpellReagents(spellID)`
Returns the spell's reagent list as an array of
diff --git a/src/Offsets.h b/src/Offsets.h
index 213f18d7..c9722092 100644
--- a/src/Offsets.h
+++ b/src/Offsets.h
@@ -639,6 +639,16 @@ enum Offsets {
// instant / no-cast-time spells.
FUN_GET_CAST_TIME = 0x006E3340,
+ // Effective power cost (mana/rage/energy/…) for a spell:
+ // `__fastcall uint32_t(int spellID, int unit /*0 = local player*/)`.
+ // Computes ManaCost base + per-level + ManaCostPercent-of-resource,
+ // applies the descriptor power-cost modifiers, then the cost
+ // SpellMod (op 0xE / 14). 0xFFFFFFFF on invalid input / no player.
+ // Read by C_Spell.GetSpellPowerCost (the full helper, like the cast
+ // time one — beats replicating op 14 via Spell::Mod since it also
+ // folds in the descriptor cost mods).
+ FUN_GET_SPELL_COST = 0x006E31B0,
+
// Spell.dbc `m_durationIndex` field — pointer into SpellDuration.dbc.
// Verified via `FUN_004E44B0` (`0x004e44b0`) and `FUN_006EA000`
// (`0x006ea000`), both of which read `[spellRec + 0x78]` and use
diff --git a/src/spell/PowerCost.cpp b/src/spell/PowerCost.cpp
new file mode 100644
index 00000000..29a2e90a
--- /dev/null
+++ b/src/spell/PowerCost.cpp
@@ -0,0 +1,109 @@
+// 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 .
+
+// `C_Spell.GetSpellPowerCost(spellIdentifier)` -> SpellPowerCostInfo[]
+//
+// Returns an array of power-cost tables, or nil if the spell isn't found
+// or has no resource cost. Vanilla spells have exactly one power cost, so
+// the array always holds at most one entry.
+//
+// `cost` is the **effective** cost for the local player — base + level
+// scaling + ManaCostPercent-of-resource + the descriptor power-cost
+// modifiers + the cost SpellMod (op 14) — via the engine's own cost
+// helper `FUN_006e31b0` (the same value the engine charges). So talents
+// like reduced-cost spells are reflected. Per-entry fields:
+//
+// type Enum.PowerType (0 mana, 1 rage, 2 focus, 3 energy,
+// 4 happiness) — the spell's PowerType (+0x7C)
+// name power token ("MANA", "RAGE", …)
+// cost effective cost (see above)
+// minCost == cost; vanilla has no "optional" cost component
+// costPercent ManaCostPercent (+0x270) — the spell's %-of-base-
+// resource cost, or 0 for a flat cost
+// costPerSec 0 — vanilla doesn't expose a per-second channel cost
+// in these terms
+// requiredAuraID 0 — vanilla has no form/aura-conditional costs here
+// hasRequiredAura false
+
+#include "Game.h"
+#include "Offsets.h"
+#include "spell/Arg.h"
+#include "spell/Lookup.h"
+
+#include
+
+namespace Spell::PowerCost {
+
+namespace {
+
+using GetSpellCost_t = uint32_t(__fastcall *)(int spellID, int unit);
+
+constexpr int OFF_POWER_TYPE = 0x7C; // int PowerType
+constexpr int OFF_MANA_COST_PERCENT = 0x270; // int, % of base resource (0 = flat)
+
+// Power token by PowerType, matching the engine's POWER_TYPE strings.
+const char *PowerToken(int powerType) {
+ switch (powerType) {
+ case 0: return "MANA";
+ case 1: return "RAGE";
+ case 2: return "FOCUS";
+ case 3: return "ENERGY";
+ case 4: return "HAPPINESS";
+ default: return ""; // e.g. health-cost spells (PowerType -2)
+ }
+}
+
+uint32_t EffectiveCost(int spellID) {
+ return reinterpret_cast(Offsets::FUN_GET_SPELL_COST)(spellID, 0);
+}
+
+int __fastcall Script_GetSpellPowerCost(void *L) {
+ const int spellID = Spell::Arg::ResolveSpellID(L, 1);
+ const uint8_t *rec = Spell::Lookup::RecordForID(spellID);
+ if (rec == nullptr)
+ return 0; // nil — spell not found
+
+ const uint32_t cost = EffectiveCost(spellID);
+ if (cost == 0xFFFFFFFF)
+ return 0; // engine error (no player context, etc.)
+
+ const int powerType = *reinterpret_cast(rec + OFF_POWER_TYPE);
+ const int costPercent = *reinterpret_cast(rec + OFF_MANA_COST_PERCENT);
+ if (cost == 0 && costPercent == 0)
+ return 0; // nil — no resource cost
+
+ // SpellPowerCostInfo[] — a one-element array: outer[1] = entry.
+ Game::Lua::NewTable(L); // outer
+ Game::Lua::PushNumber(L, 1.0); // key 1
+ Game::Lua::NewTable(L); // entry (top)
+ Game::Lua::SetFieldNumber(L, "type", static_cast(powerType));
+ Game::Lua::SetFieldString(L, "name", PowerToken(powerType));
+ Game::Lua::SetFieldNumber(L, "cost", static_cast(cost));
+ Game::Lua::SetFieldNumber(L, "minCost", static_cast(cost));
+ Game::Lua::SetFieldNumber(L, "costPercent", static_cast(costPercent));
+ Game::Lua::SetFieldNumber(L, "costPerSec", 0);
+ Game::Lua::SetFieldNumber(L, "requiredAuraID", 0);
+ Game::Lua::SetFieldBool(L, "hasRequiredAura", false);
+ Game::Lua::SetTable(L, -3); // outer[1] = entry
+ return 1;
+}
+
+} // namespace
+
+static void RegisterLuaFunctions() {
+ Game::Lua::RegisterTableFunction("C_Spell", "GetSpellPowerCost", &Script_GetSpellPowerCost);
+}
+
+static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
+
+} // namespace Spell::PowerCost