wow-addons/DungeonPreQuest/DungeonPreQuest.lua
rope 01a351a865 DungeonPreQuest: mark dungeon quests too, refresh QuestieDB data
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 10:13:45 +00:00

185 lines
5.9 KiB
Lua

-- Dungeon Pre-Quest: marks dungeon quests and quests whose chain leads to one,
-- in the quest log and on the quest offer window. The client has no chain data, so
-- Data.lua is generated ahead of time.
local _, ns = ...
local COLOR = "|cffffd100"
local LABEL = "Dungeon pre-quest"
local DUNGEON_LABEL = "Dungeon quest"
local ROW_MARK = "|cffc77dff[D]|r"
local ROW_MARK_GAP = 5
local function HasClass(mask, classID)
return mask == 0 or math.floor(mask / 2 ^ (classID - 1)) % 2 == 1
end
-- Dungeon names from one of the generated tables that apply to this
-- character, or nil if none. An empty list means a dungeon that isn't known.
local function Match(tbl, questID)
local row = questID and tbl and tbl[questID]
if not row then
return nil
end
local faction = UnitFactionGroup("player")
local factionBit = faction == "Alliance" and 1 or faction == "Horde" and 2
local classID = select(3, UnitClass("player"))
if not factionBit or not classID then
return nil
end
local names, seen, matched = {}, {}, false
for i = 1, #row, 3 do
local dungeon, factions, classes = row[i], row[i + 1], row[i + 2]
if (factions == 3 or factions == factionBit) and HasClass(classes, classID) then
matched = true
local name = ns.dungeons[dungeon]
if name and not seen[name] then
seen[name] = true
names[#names + 1] = name
end
end
end
return matched and names or nil
end
local function Label(label, names)
if #names == 0 then
return label
end
return label .. ": " .. table.concat(names, ", ")
end
-- What to say about a quest for this character, or nil if it isn't one of
-- ours: a quest done inside a dungeon, a step towards one, or both.
local function Describe(questID)
local own = Match(ns.dungeonQuests, questID)
local leads = Match(ns.quests, questID)
if own and leads then
return Label(DUNGEON_LABEL, own) .. "\n" .. Label(LABEL, leads)
end
return own and Label(DUNGEON_LABEL, own) or leads and Label(LABEL, leads) or nil
end
ns.Describe = Describe
local function MarkRow(button)
if not Describe(button.questID) then
if button.dpqMark then
button.dpqMark:Hide()
end
return
end
if not button.dpqMark then
button.dpqMark = button:CreateFontString(nil, "ARTWORK", "GameFontNormalLeft")
button.dpqMark:SetText(ROW_MARK)
end
-- Follows the title rather than sitting in the left gutter, which belongs
-- to the map icons. A wrapped title reports its unwrapped length.
local offset = button.Text:GetStringWidth()
local boxWidth = button.Text:GetWidth()
if boxWidth > 0 then
offset = math.min(offset, boxWidth)
end
button.dpqMark:ClearAllPoints()
button.dpqMark:SetPoint("TOPLEFT", button.Text, "TOPLEFT", offset + ROW_MARK_GAP, 0)
button.dpqMark:Show()
end
local function UpdateRows()
for button in QuestScrollFrame.titleFramePool:EnumerateActive() do
MarkRow(button)
end
end
-- Purple, because the gold used elsewhere is hard to read on parchment.
local LIST_MARK = "|cff8a2be2[D]|r "
local function MarkListButton(button, questID)
if not Describe(questID) then
return false
end
local text = button:GetText()
if not text or text:find(LIST_MARK, 1, true) then
return false
end
button:SetText(LIST_MARK .. text)
return true
end
local function MarkGreetingList()
for button in QuestFrameGreetingPanel.titleButtonPool:EnumerateActive() do
if button.isActive == 0 then
local questID = select(5, GetAvailableQuestInfo(button:GetID()))
if MarkListButton(button, questID) then
button:SetHeight(math.max(button:GetTextHeight() + 2, button.Icon:GetHeight()))
end
end
end
end
local banner
local function UpdateBanner()
if not banner then
return
end
local text = Describe(GetQuestID())
if text then
banner:SetText(COLOR .. text .. "|r")
banner:Show()
else
banner:Hide()
end
end
local function Setup()
hooksecurefunc("QuestLogQuests_Update", UpdateRows)
EventRegistry:RegisterCallback("QuestMapLogTitleButton.OnEnter", function(_, button, questID)
local text = Describe(questID or (button and button.questID))
if text then
GameTooltip:AddLine(text, 1, 0.82, 0, true)
GameTooltip:Show()
end
end, ns)
-- Sits above the window, where it can't disturb Blizzard's layout. Parented
-- to the detail panel so it goes away with that panel.
banner = QuestFrameDetailPanel:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
banner:SetPoint("BOTTOM", QuestFrame, "TOP", 0, 6)
QuestFrameDetailPanel:HookScript("OnShow", UpdateBanner)
-- Gossip buttons copy this mixin when they are first created, after login.
hooksecurefunc(GossipAvailableQuestButtonMixin, "Setup", function(button, questInfo)
if MarkListButton(button, questInfo and questInfo.questID) then
button:Resize()
end
end)
-- Showing the panel runs Blizzard's XML-bound handler, while refreshing an
-- open panel calls the global by name, so both need covering.
QuestFrameGreetingPanel:HookScript("OnShow", MarkGreetingList)
hooksecurefunc("QuestFrameGreetingPanel_OnShow", MarkGreetingList)
end
local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("QUEST_DETAIL")
frame:SetScript("OnEvent", function(_, event)
if event == "PLAYER_LOGIN" then
Setup()
else
UpdateBanner()
end
end)
-- /dpq 123 says what is known about a quest ID.
SLASH_DUNGEONPREQUEST1 = "/dpq"
SlashCmdList.DUNGEONPREQUEST = function(text)
local id = tonumber(text)
local text = Describe(id)
DEFAULT_CHAT_FRAME:AddMessage(COLOR .. "DungeonPreQuest:|r " .. (id and (text or "not a dungeon quest for you") or "usage: /dpq questID"))
end