Advertisement
IsaacSin

Untitled

Oct 3rd, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.31 KB | None | 0 0
  1. local addonName, addon = ...
  2. local hbd = addon.hbd
  3.  
  4. local enableClicks = true       -- True if waypoint-clicking is enabled to set points
  5. local enableClosest = true      -- True if 'Automatic' quest waypoints are enabled
  6. local modifier                  -- A string representing click-modifiers "CAS", etc.
  7.  
  8. local modTbl = {
  9.     C = IsControlKeyDown,
  10.     A = IsAltKeyDown,
  11.     S = IsShiftKeyDown,
  12. }
  13.  
  14. local L = TomTomLocals
  15.  
  16. -- This function and the related events/hooks are used to automatically
  17. -- update the crazy arrow to the closest quest waypoint.
  18. local lastWaypoint
  19. local scanning          -- This function is not re-entrant, stop that
  20.  
  21. local function getQIDFromIndex(questIndex)
  22.     return (select(8, GetQuestLogTitle(questIndex)))
  23. end
  24.  
  25. local function ObjectivesChanged()
  26.     -- This function should only run if enableClosest is set
  27.     if not enableClosest then
  28.         return
  29.     end
  30.  
  31.     -- This function may be called while we are processing this function
  32.     -- so stop that from happening.
  33.     if scanning then
  34.         return
  35.     else
  36.         scanning = true
  37.     end
  38.  
  39.     local map, floor = GetCurrentMapAreaID()
  40.     local floors = hbd:GetNumFloors(map)
  41.     floor = (floors == 0 and 0 or 1)
  42.  
  43.     local px, py = GetPlayerMapPosition("player")
  44.  
  45.     -- Bail out if we can't get the player's position
  46.     if not px or not py or px <= 0 or py <= 0 then
  47.         scanning = false
  48.         return
  49.     end
  50.  
  51.     -- THIS CVAR MUST BE CHANGED BACK!
  52.     local cvar = GetCVarBool("questPOI")
  53.     SetCVar("questPOI", 1)
  54.  
  55.     local closest
  56.     local closestdist = math.huge
  57.  
  58.     -- This function relies on the above CVar being set, and updates the icon
  59.     -- position information so it can be queries via the API
  60.     QuestPOIUpdateIcons()
  61.  
  62.     -- Scan through every quest that is tracked, and find the closest one
  63.     local watchIndex = 1
  64.     while true do
  65.         local questIndex = GetQuestIndexForWatch(watchIndex)
  66.  
  67.         if not questIndex then
  68.             break
  69.         end
  70.  
  71.         local qid = getQIDFromIndex(questIndex)
  72.         local completed, x, y, objective = QuestPOIGetIconInfo(qid)
  73.  
  74.         if x and y then
  75.             local dist = hbd:GetZoneDistance(map, floor, px, py, map, floor, x, y)
  76.             if dist < closestdist then
  77.                 closest = watchIndex
  78.                 closestdist = dist
  79.             end
  80.         end
  81.         watchIndex = watchIndex + 1
  82.     end
  83.  
  84.     if closest then
  85.         local questIndex = GetQuestIndexForWatch(closest)
  86.         local title = GetQuestLogTitle(questIndex)
  87.         local qid = getQIDFromIndex(questIndex)
  88.         local completed, x, y, objective = QuestPOIGetIconInfo(qid)
  89.  
  90.         if completed then
  91.             title = "Turn in: " .. title
  92.         end
  93.  
  94.         local setWaypoint = true
  95.         if lastWaypoint then
  96.             -- This is a hack that relies on the UID format, do not use this
  97.             -- in your addons, please.
  98.             local pm, pf, px, py = unpack(lastWaypoint)
  99.             if map == pm and floor == pf and x == px and y == py and lastWaypoint.title == title then
  100.                 -- This is the same waypoint, do nothing
  101.                 setWaypoint = false
  102.             else
  103.                 -- This is a new waypoint, clear the previous one
  104.                 TomTom:RemoveWaypoint(lastWaypoint)
  105.             end
  106.         end
  107.  
  108.         if setWaypoint then
  109.             -- Set the new waypoint
  110.             lastWaypoint = TomTom:AddMFWaypoint(map, floor, x, y, {
  111.                 title = title,
  112.                 persistent = false,
  113.                 arrivaldistance = TomTom.profile.poi.arrival,
  114.             })
  115.  
  116.             -- Check and see if the Crazy arrow is empty, and use it if so
  117.             if TomTom:IsCrazyArrowEmpty() then
  118.                 TomTom:SetCrazyArrow(lastWaypoint, TomTom.profile.poi.arrival, title)
  119.             end
  120.         end
  121.     else
  122.         -- No closest waypoint was found, so remove one if its already set
  123.         if lastWaypoint then
  124.             TomTom:RemoveWaypoint(lastWaypoint)
  125.             lastWaypoint = nil
  126.         end
  127.     end
  128.  
  129.     SetCVar("questPOI", cvar and 1 or 0)
  130.     scanning = false
  131. end
  132.  
  133. local eventFrame = CreateFrame("Frame")
  134. eventFrame:RegisterEvent("QUEST_POI_UPDATE")
  135. eventFrame:RegisterEvent("QUEST_LOG_UPDATE")
  136.  
  137. eventFrame:SetScript("OnEvent", function(self, event, ...)
  138.     if event == "QUEST_POI_UPDATE" then
  139.         ObjectivesChanged()
  140.     elseif event == "QUEST_LOG_UPDATE" then
  141.         ObjectivesChanged()
  142.     end
  143. end)
  144.  
  145. local poiclickwaypoints = {}
  146. local function poi_OnClick(self, button)
  147.     if not enableClicks then
  148.         return
  149.     end
  150.  
  151.     if button == "LeftButton" then
  152.         for i = 1, #modifier do
  153.             local mod = modifier:sub(i, i)
  154.             local func = modTbl[mod]
  155.             if not func() then
  156.                 return
  157.             end
  158.         end
  159.     else
  160.         return
  161.     end
  162.  
  163.     local cvar = GetCVarBool("questPOI")
  164.     SetCVar("questPOI", 1)
  165.  
  166.     -- Run our logic, and set a waypoint for this button
  167.     local m, f = GetCurrentMapAreaID()
  168.  
  169.     local questIndex = self.quest and self.quest.questLogIndex
  170.     if not questIndex and self.questId then
  171.         -- Lookup the questIndex for the given questId
  172.         for idx = 1, GetNumQuestLogEntries(), 1 do
  173.             local qid = getQIDFromIndex(idx)
  174.             if qid == self.questId then
  175.                 questIndex = idx
  176.             end
  177.         end
  178.     end
  179.  
  180.     if not questIndex and self.index then
  181.         questIndex = GetQuestIndexForWatch(self.index)
  182.     end
  183.  
  184.     QuestPOIUpdateIcons()
  185.  
  186.     local title = GetQuestLogTitle(questIndex)
  187.     local qid = getQIDFromIndex(questIndex)
  188.     local completed, x, y, objective = QuestPOIGetIconInfo(qid)
  189.     if completed then
  190.         title = "Turn in: " .. title
  191.     end
  192.  
  193.     if not x or not y then
  194.         -- No coordinate information for this quest/objective
  195.         local header = "|cFF33FF99TomTom|r"
  196.         print(L["%s: No coordinate information found for '%s' at this map level"]:format(header, title))
  197.         return
  198.     end
  199.  
  200.     local key = TomTom:GetKeyArgs(m, f, x, y, title)
  201.  
  202.     local alreadySet = false
  203.     if poiclickwaypoints[key] then
  204.         local uid = poiclickwaypoints[key]
  205.         -- Check to see if it has been removed by the user
  206.         if TomTom:IsValidWaypoint(uid) then
  207.             alreadySet = true
  208.         end
  209.     end
  210.  
  211.     if not alreadySet then
  212.         local uid = TomTom:AddMFWaypoint(m, f, x, y, {
  213.             title = title,
  214.             arrivaldistance = TomTom.profile.poi.arrival,
  215.         })
  216.         poiclickwaypoints[key] = uid
  217.     else
  218.         local uid = poiclickwaypoints[key]
  219.         TomTom:SetCrazyArrow(uid, TomTom.profile.poi.arrival, title)
  220.     end
  221.  
  222.     SetCVar("questPOI", cvar and 1 or 0)
  223. end
  224.  
  225. hooksecurefunc("TaskPOI_OnClick", function(self, button)
  226.     poi_OnClick(self, button)
  227. end)
  228.  
  229. hooksecurefunc("QuestPOIButton_OnClick", function(self, button)
  230.     poi_OnClick(self, button)
  231. end)
  232.  
  233. function TomTom:EnableDisablePOIIntegration()
  234.     enableClicks= TomTom.profile.poi.enable
  235.     modifier = TomTom.profile.poi.modifier
  236.     enableClosest = TomTom.profile.poi.setClosest
  237.  
  238.     if not enableClosest and lastWaypoint then
  239.         TomTom:RemoveWaypoint(lastWaypoint)
  240.         lastWaypoint = nil
  241.     elseif enableClosest then
  242.         ObjectivesChanged()
  243.     end
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement