Files

215 lines
9.8 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Octo Spec Profiles: Goblin Brainwashing Device integration (GNS + SimpleActionSets behaviour).
-- The Turtle/Octo server keeps 4 talent specs per character and exposes them only as the Device's gossip lines
-- "Save Nth Specialization" and "Activate Nth Specialization (a/b/c)". This file reads those lines, renames the
-- Activate lines with the GNS names, and reacts to the player's choice:
-- Save N -> the current action bars are saved to profile Brainwasher<N>, the equipped set is linked if none is,
-- and (GNS) a name prompt follows once the gossip closes.
-- Activate N -> the bars of the spec being left are saved first (option), then once the server has swapped the
-- talents and spells, profile Brainwasher<N> is applied (bars + linked equipment set).
-- Choices are observed with post-hooks on C_GossipInfo (no Blizzard function is replaced).
local P = OctoSpecProfiles
local Print = P.Print
local ACTIVATE = "Activate (%d)(..) Specialization%s*%(([%d/]+)%)"
local SAVE = "Save (%d)(..) Specialization"
local BUY = "Buy (%d)(..) Specialization tab"
P.device = {open = false, options = {}}
local device = P.device
local function Parse(name)
if type(name) ~= "string" then return end
local n, _, talents = name:match(ACTIVATE)
if n then return "load", tonumber(n), talents end
n = name:match(SAVE)
if n then return "save", tonumber(n) end
end
-- Device detection by its gossip lines (works for the game-object gossip, whatever the title shows).
local function ReadOptions()
wipe(device.options)
local found = false
local _, char = P.DB()
char.deviceTalents = char.deviceTalents or {}
for _, option in ipairs(C_GossipInfo.GetOptions() or {}) do
local action, n, talents = Parse(option.name)
if action and n and n >= 1 and n <= P.DEVICE_SPECS then
found = true
device.options[action .. n] = option
if talents then char.deviceTalents[n] = talents end
end
end
-- Bought slots (server GetTabAmount): 1-2 always; 3/4 have Save lines once bought, otherwise the Device offers
-- "Buy 3rd Specialization tab" (then "Buy 4th ...").
if found then
local slots = 2
for n = 3, P.DEVICE_SPECS do if device.options["save" .. n] then slots = n end end
for _, option in ipairs(C_GossipInfo.GetOptions() or {}) do
local buy = type(option.name) == "string" and tonumber((option.name:match(BUY)))
if buy then slots = math.min(slots, buy - 1) end
end
char.deviceSlots = slots
end
device.open = found
return found
end
-- Slots this character can use: the count read at the Device; before the first visit the two free slots (level 10+).
function P.DeviceSlots()
local _, char = P.DB()
if char.deviceSlots then return char.deviceSlots end
return (UnitLevel("player") or 0) >= 10 and 2 or 0
end
local function OptionByOrder(orderIndex)
for _, option in pairs(device.options) do if option.orderIndex == orderIndex then return option end end
end
local function OptionByID(optionID)
for _, option in pairs(device.options) do if option.gossipOptionID == optionID then return option end end
end
function P.ActiveSpec()
local _, char = P.DB()
local split = P.TalentSplit()
if split and char.deviceTalents then
for n = 1, P.DEVICE_SPECS do if char.deviceTalents[n] == split then return n end end
-- Talents differ from every saved spec (e.g. after "Reset my talents"): none is active.
local any = false
for n = 1, P.DEVICE_SPECS do if char.deviceTalents[n] then any = true end end
if any then return nil end
end
return char.activeSpec
end
-------------------------------------------------------------------------------- gossip line text (GNS)
local function Relabel(button, elementData)
local info = elementData and elementData.info
if not info or not info.name or type(button.SetText) ~= "function" then return end
local action, n, talents = Parse(info.name)
if not action then return end
local active = P.ActiveSpec() == n
local text
if action == "load" then
-- Same length as the server line (one row in the gossip list; a wrapped label overlapped the next
-- option). The active spec is marked by colour only.
text = active and string.format("|cff0a6e0a*Active* %s (%s)|r", P.SpecName(n), talents)
or string.format("Activate %s (%s)", P.SpecName(n), talents)
else
-- Only a player-chosen name adds anything; the default "Nth Specialization" just repeated the
-- line (and light grey on the parchment read as a smudged overlap). Dark brown stays readable.
local _, char = P.DB()
local custom = char.specNames and char.specNames[n]
if not custom or custom == P.ORDINALS[n] .. " Specialization" then return end
text = string.format("%s |cff5c3a14(%s)|r", info.name, custom)
end
button:SetText(text)
if type(button.Resize) == "function" then button:Resize() end
end
local function RelabelAll()
local scroll = GossipFrame and GossipFrame.GreetingPanel and GossipFrame.GreetingPanel.ScrollBox
if scroll and device.open and type(scroll.ForEachFrame) == "function" then
scroll:ForEachFrame(function(button, elementData) pcall(Relabel, button, elementData) end)
end
end
-------------------------------------------------------------------------------- choices
local settle
local function ApplyAfterSwap(n)
-- The server swaps talents and spells after the choice; wait until spell/talent events stop for 1 s (max 10 s).
local name, started = P.DeviceProfileName(n), GetTime()
if settle then settle:Cancel() end
local function Finish()
settle = nil
P.WaitFrame:UnregisterAllEvents()
if P.Get(name) then P.Apply(name)
else P.Save(name, true); Print(string.format("%s has no saved bars yet; saved your current bars to it.", P.DisplayName(name)), 1, 0.8, 0.2) end
local _, char = P.DB(); char.activeSpec = n
char.deviceTalents = char.deviceTalents or {}; char.deviceTalents[n] = P.TalentSplit() or char.deviceTalents[n]
if P.RecordSnapshot then P.RecordSnapshot(n) end
if P.OnChanged then P.OnChanged() end
end
local function Restart()
if settle then settle:Cancel() end
settle = C_Timer.NewTimer(GetTime() - started > 9 and 0 or 1.0, Finish)
end
P.WaitFrame:SetScript("OnEvent", Restart)
for _, event in ipairs({"SPELLS_CHANGED", "PLAYER_TALENT_UPDATE", "CHARACTER_POINTS_CHANGED", "TRAIT_CONFIG_UPDATED", "ACTIONBAR_SLOT_CHANGED"}) do
pcall(P.WaitFrame.RegisterEvent, P.WaitFrame, event)
end
settle = C_Timer.NewTimer(2.0, Finish)
end
P.WaitFrame = CreateFrame("Frame")
local namePromptFor
local function OnChoice(option)
if not option then return end
local action, n = Parse(option.name)
if not action then return end
local _, char, o = P.DB()
if action == "save" then
local provider = P.TalentProvider()
if provider and provider:AvailablePoints() > 0 then
Print("the Device does not save a spec while you have unspent talent points; bars not saved.", 1, 0.5, 0.2)
return
end
-- The Device lists this spec's split only on its next menu; record it now so later talent changes
-- are never attributed to this spec.
char.deviceTalents = char.deviceTalents or {}
char.deviceTalents[n] = P.TalentSplit()
if P.RecordSnapshot then P.RecordSnapshot(n) end
local profile = P.Save(P.DeviceProfileName(n), true)
if profile and not profile.equipmentSet then profile.equipmentSet = P.EquippedSetName() end
char.activeSpec = n
Print(string.format("saved your bars to %s%s.", P.DisplayName(P.DeviceProfileName(n)),
profile and profile.equipmentSet and (" (equipment set '" .. profile.equipmentSet .. "')") or ""), 0.3, 1, 0.3)
if o.namePrompt then namePromptFor = n end
else
local current = P.ActiveSpec()
if o.autoSave and current and current ~= n and P.Get(P.DeviceProfileName(current)) then
P.Save(P.DeviceProfileName(current), true)
end
ApplyAfterSwap(n)
end
if P.OnChanged then P.OnChanged() end
end
hooksecurefunc(C_GossipInfo, "SelectOptionByIndex", function(orderIndex) if device.open then OnChoice(OptionByOrder(orderIndex)) end end)
hooksecurefunc(C_GossipInfo, "SelectOption", function(optionID) if device.open then OnChoice(OptionByID(optionID)) end end)
-- Panel buttons: choose the Device's own line (only while its gossip is open).
function P.DeviceChoose(action, n)
local option = device.open and device.options[action .. n]
if not option then Print("talk to a Goblin Brainwashing Device first.", 1, 0.5, 0.2); return end
C_GossipInfo.SelectOptionByIndex(option.orderIndex)
end
local events = CreateFrame("Frame")
events:RegisterEvent("GOSSIP_SHOW")
events:RegisterEvent("GOSSIP_CLOSED")
events:SetScript("OnEvent", function(_, event)
if event == "GOSSIP_SHOW" then
ReadOptions()
C_Timer.After(0, RelabelAll)
else
device.open = false
if namePromptFor then
local n = namePromptFor; namePromptFor = nil
C_Timer.After(0.5, function() P.PromptSpecName(n) end)
end
end
if P.OnChanged then P.OnChanged() end
end)
local function HookGossip()
if GossipFrame and type(GossipFrame.Update) == "function" and not device.hooked then
hooksecurefunc(GossipFrame, "Update", function() if ReadOptions() then RelabelAll() end end)
device.hooked = true
end
end
HookGossip()
events:RegisterEvent("PLAYER_LOGIN")
events:HookScript("OnEvent", HookGossip)