objectives work, I think
[QuestHelper.git] / collect_flight.lua
blob1d6933be1aeeb5181ed0d8d249058b77c307d798
1 QuestHelper_File["collect_flight.lua"] = "Development Version"
2 QuestHelper_Loadtime["collect_flight.lua"] = GetTime()
4 local debug_output = false
5 if QuestHelper_File["collect_flight.lua"] == "Development Version" then debug_output = true end
7 local QHCFM
8 local QHCFT
10 local IsMonsterGUID
11 local GetMonsterType
13 local function GetRoute(currentname, endnode)
14 local path = currentname .. "@@" .. TaxiNodeName(endnode)
16 local links = GetNumRoutes(endnode)
17 if links > 50 then -- links is apparently sometimes 999998. Why? I could not say. It is a mystery. Hopefully this will show up in the datafiles and I'll be able to debug it.
18 return path, string.format("@@@ WACKYLAND %s %d @@@", TaxiNodeGetType(endnode), links)
19 end
21 local route = ""
22 for j = 1, links - 1 do
23 if #route > 0 then route = route .. "@@" end
24 route = route .. string.format("%f:%f", TaxiGetDestX(endnode, j), TaxiGetDestY(endnode, j))
25 end
27 return path, route
28 end
30 local function GetCurrentname()
31 QuestHelper: Assert(NumTaxiNodes() < 100)
32 for i = 1, NumTaxiNodes() do if TaxiNodeGetType(i) == "CURRENT" then return TaxiNodeName(i) end end
33 end
35 local path, route
36 local start_time
37 local phase = "IDLE"
39 local real_TakeTaxiNode = TakeTaxiNode
40 TakeTaxiNode = function(id)
41 path, route = GetRoute(GetCurrentname(), id)
43 phase = "PENDING"
45 start_time = GetTime()
47 real_TakeTaxiNode(id)
48 end
50 local function TaximapOpened()
51 -- Figure out who we have targeted and what our location name is
52 local currentname = GetCurrentname()
53 if not currentname then return end -- must not actually have opened the map
55 QuestHelper: Assert(NumTaxiNodes() < 100)
56 for i = 1, NumTaxiNodes() do
57 local name, type = TaxiNodeName(i), TaxiNodeGetType(i)
58 if not QHCFM[name] then QHCFM[name] = {} end
59 QHCFM[name].x, QHCFM[name].y = TaxiNodePosition(i)
60 if type == "CURRENT" then
61 local guid = UnitGUID("target")
62 if guid and IsMonsterGUID(guid) then QHCFM[name].master = GetMonsterType(guid) end
63 end
65 if type ~= "CURRENT" then
66 local path, route = GetRoute(currentname, i)
67 if not QHCFT[path] then QHCFT[path] = {} end
68 if not QHCFT[path][route] then QHCFT[path][route] = true end
69 end
70 end
71 end
73 local function OnUpdate()
74 if UnitOnTaxi("player") then
75 if phase == "PENDING" then
76 start_time = GetTime()
77 phase = "FLYING"
78 end
79 else
80 if phase == "PENDING" then
81 if GetTime() > start_time + 5 then -- something has gone wrong, abort
82 phase = "IDLE"
83 path, route, start_time = nil, nil, nil
84 end
85 elseif phase == "FLYING" then
86 -- yaay
87 QHCFT[path][route] = (type(QHCFT[path][route]) == "number" and QHCFT[path][route] or 0) + (GetTime() - start_time)
88 QHCFT[path][route .. "##count"] = (QHCFT[path][route .. "##count"] or 0) + 1
89 phase = "IDLE"
90 path, route, start_time = nil, nil, nil
91 end
92 end
93 end
95 function QH_Collect_Flight_Init(QHCData, API)
96 if not QHCData.flight_master then QHCData.flight_master = {} end
97 if not QHCData.flight_times then QHCData.flight_times = {} end
98 QHCFM, QHCFT = QHCData.flight_master, QHCData.flight_times
100 IsMonsterGUID = API.Utility_IsMonsterGUID
101 GetMonsterType = API.Utility_GetMonsterType
102 QuestHelper: Assert(IsMonsterGUID)
103 QuestHelper: Assert(GetMonsterType)
105 API.Registrar_EventHook("TAXIMAP_OPENED", TaximapOpened)
107 API.Registrar_OnUpdateHook(OnUpdate)