Files
ClassToolkit/Spells.lua
T
notforever-ship-it 81f6c5abc8 Class Toolkit 1.0.0
Swing timer for every class (melee, Auto Shot, wand), buff reminder, spell ready icons, reagent and ammo warnings, and the hunter range icon and feed reminder moved over from PokeHuntLog.
2026-09-19 15:44:42 +01:00

133 lines
4.2 KiB
Lua

-- Class Toolkit: what the player knows and where it sits on the action bars.
--
-- Spellbook: each spell's highest rank, its icon, and its cost (only in the tooltip, "25 Mana").
-- Action bars: which slot holds a spell, found by reading each button's tooltip. Range checks need this,
-- because in 1.12 only action slots can say whether the target is in range (IsActionInRange).
local CTK = ClassToolkit
local BOOK = "spell"
local POWER_WORDS = { Mana = true, Rage = true, Energy = true }
local spells = {} -- [lowercase name] = { name, index, texture }
local costs = {} -- [lowercase name] = number, read from the tooltip on first use
local actionSlots = {} -- [lowercase name] = slot
local slotsDirty = true
local lastSlotScan = 0
local function ScanSpellbook()
spells = {}
costs = {}
local i = 1
while i < 500 do
local name = GetSpellName(i, BOOK)
if not name then break end
-- Ranks are listed lowest first, so the last one seen is the highest.
spells[string.lower(name)] = { name = name, index = i, texture = GetSpellTexture(i, BOOK) }
i = i + 1
end
CTK.Debug("spellbook: " .. (i - 1) .. " entries")
end
local function ScanActionSlots()
slotsDirty = false
lastSlotScan = GetTime()
actionSlots = {}
local tip = CTK.ScanTooltip()
for slot = 1, 120 do
if HasAction(slot) then
tip:SetOwner(WorldFrame, "ANCHOR_NONE")
tip:SetAction(slot)
local text = CTK.ScanLine("Left", 1)
if text and not actionSlots[string.lower(text)] then
actionSlots[string.lower(text)] = slot
end
tip:Hide()
end
end
end
-- The spell as the spellbook spells it, or nil if the player doesn't know it.
function CTK.KnownSpellName(name)
local s = name and spells[string.lower(name)]
return s and s.name
end
function CTK.KnowsSpell(name)
return name and spells[string.lower(name)] ~= nil
end
function CTK.SpellIndex(name)
local s = name and spells[string.lower(name)]
return s and s.index
end
function CTK.SpellTexture(name)
local s = name and spells[string.lower(name)]
return s and s.texture
end
-- Seconds left on the spell's cooldown (0 when ready), and the cooldown's full length.
function CTK.SpellCooldown(name)
local index = CTK.SpellIndex(name)
if not index then return 0, 0 end
local start, duration = GetSpellCooldown(index, BOOK)
if not start or start == 0 or not duration then return 0, 0 end
local left = start + duration - GetTime()
if left < 0 then left = 0 end
return left, duration
end
-- Mana, rage or energy the spell costs, or nil if it has none (or the tooltip doesn't say).
function CTK.SpellCost(name)
local key = name and string.lower(name)
if not key or not spells[key] then return nil end
if costs[key] == nil then
costs[key] = false
local tip = CTK.ScanTooltip()
tip:SetSpell(spells[key].index, BOOK)
local text = CTK.ScanLine("Left", 2)
if text then
local _, _, amount, word = string.find(text, "^(%d+) (%a+)")
if amount and POWER_WORDS[word] then costs[key] = tonumber(amount) end
end
tip:Hide()
end
return costs[key] or nil
end
-- The action slot holding this spell, or nil. The bars are re-read a second after they change.
function CTK.ActionSlotFor(name)
if slotsDirty and GetTime() - lastSlotScan > 1 then ScanActionSlots() end
return name and actionSlots[string.lower(name)]
end
-- Is the target in range of this spell? true / false, or nil when it can't be told (not on a bar,
-- no target, or a spell with no range).
function CTK.SpellInRange(name)
local slot = CTK.ActionSlotFor(name)
if not slot then return nil end
local r = IsActionInRange(slot)
if r == 1 then return true end
if r == 0 then return false end
return nil
end
function CTK.InitSpells()
local f = CreateFrame("Frame")
local events = { "SPELLS_CHANGED", "LEARNED_SPELL_IN_TAB", "PLAYER_ENTERING_WORLD", "ACTIONBAR_SLOT_CHANGED" }
for i = 1, table.getn(events) do
pcall(f.RegisterEvent, f, events[i])
end
f:SetScript("OnEvent", function()
if event == "ACTIONBAR_SLOT_CHANGED" then
slotsDirty = true
else
ScanSpellbook()
slotsDirty = true
if event ~= "PLAYER_ENTERING_WORLD" then CTK.UpdateAll() end
end
end)
ScanSpellbook()
end