-- Purple "!" minimap markers on quest givers offering a dungeon pre-quest you -- can pick up right now. The game's own "!" icons can't be recoloured per quest, -- so these are separate pins placed on top. local _, ns = ... local Pins = LibStub("HereBeDragonsQuestie-Pins-2.0") local ICON = "Interface\\GossipFrame\\AvailableQuestIcon" local PIN_SIZE = 14 local REFRESH_DELAY = 1 local PLAYER_CLEARANCE = 11 local OVERLAP_INTERVAL = 0.1 local active, free = {}, {} local function HasBit(mask, bit) return math.floor(mask / bit) % 2 == 1 end local function Done(id) return C_QuestLog.IsQuestFlaggedCompleted(id) end local function DoneOrTaken(id) return Done(id) or C_QuestLog.IsOnQuest(id) end local function AnyOf(list, test) for _, id in ipairs(list or {}) do if test(id) then return true end end return false end local function AllOf(list, test) for _, id in ipairs(list or {}) do if not test(id) then return false end end return true end local function CanPickUp(id, quest, level, raceBit, classBit) if DoneOrTaken(id) or quest.l > level then return false end if quest.r ~= 0 and raceBit and not HasBit(quest.r, raceBit) then return false end if quest.c ~= 0 and not HasBit(quest.c, classBit) then return false end if quest.p and not AnyOf(quest.p, Done) then return false end if not AllOf(quest.g, Done) or AnyOf(quest.x, DoneOrTaken) then return false end -- Past this step already, by another route. if (quest.nx ~= 0 and DoneOrTaken(quest.nx)) or (quest.b ~= 0 and DoneOrTaken(quest.b)) then return false end return true end local function ShowTooltip(pin) local names = ns.DungeonsFor(pin.questID) GameTooltip:SetOwner(pin, "ANCHOR_RIGHT") GameTooltip:SetText(ns.pinQuests[pin.questID].n) if names then GameTooltip:AddLine(ns.Describe(names), 1, 0.82, 0, true) end GameTooltip:Show() end local function HideTooltip() GameTooltip:Hide() end local function AcquirePin() local pin = table.remove(free) if pin then return pin end pin = CreateFrame("Frame", nil, Minimap) pin:Hide() pin:SetSize(PIN_SIZE, PIN_SIZE) pin:SetFrameLevel(Minimap:GetFrameLevel() + 5) local texture = pin:CreateTexture(nil, "ARTWORK") texture:SetAllPoints() texture:SetTexture(ICON) texture:SetDesaturated(true) texture:SetVertexColor(0.75, 0.35, 1) pin:EnableMouse(true) pin:SetScript("OnEnter", ShowTooltip) pin:SetScript("OnLeave", HideTooltip) return pin end local function Refresh() Pins:RemoveAllMinimapIcons(ns) for i = #active, 1, -1 do free[#free + 1] = table.remove(active, i) end local level = UnitLevel("player") local raceBit = ns.raceBits[select(3, UnitRace("player"))] local classBit = 2 ^ (select(3, UnitClass("player")) - 1) for id, quest in pairs(ns.pinQuests) do if ns.DungeonsFor(id) and CanPickUp(id, quest, level, raceBit, classBit) then local spawns = quest.s for i = 1, #spawns, 3 do local pin = AcquirePin() pin.questID = id active[#active + 1] = pin Pins:AddMinimapIconMap(ns, pin, spawns[i], spawns[i + 1], spawns[i + 2], false, false) end end end end -- The player arrow is drawn by the minimap itself, below anything an addon can -- add to it, so markers step aside instead. Alpha rather than Hide, because the -- pin library shows its pins again on every update. local function UpdateOverlap() local centerX, centerY = Minimap:GetCenter() if not centerX then return end for _, pin in ipairs(active) do if pin:IsShown() then local x, y = pin:GetCenter() local overlapping = x ~= nil and (x - centerX) ^ 2 + (y - centerY) ^ 2 < PLAYER_CLEARANCE ^ 2 if overlapping ~= pin.overlapping then pin.overlapping = overlapping pin:SetAlpha(overlapping and 0 or 1) pin:EnableMouse(not overlapping) end end end end C_Timer.NewTicker(OVERLAP_INTERVAL, UpdateOverlap) -- Turning a quest in reports several events, and completion lags slightly behind. local queued = false local function QueueRefresh() if not queued then queued = true C_Timer.After(REFRESH_DELAY, function() queued = false Refresh() end) end end local frame = CreateFrame("Frame") for _, event in ipairs({ "PLAYER_ENTERING_WORLD", "PLAYER_LEVEL_UP", "QUEST_ACCEPTED", "QUEST_REMOVED", "QUEST_TURNED_IN" }) do if not C_EventUtils or C_EventUtils.IsEventValid(event) then frame:RegisterEvent(event) end end frame:SetScript("OnEvent", QueueRefresh)