this may or may not work on 3.1, we'll test
[QuestHelper.git] / collect_quest.lua
blob3cee4b94d5b69871393ba01cdeccf9a32f8fb8b2
1 QuestHelper_File["collect_quest.lua"] = "Development Version"
2 QuestHelper_Loadtime["collect_quest.lua"] = GetTime()
4 local debug_output = false
5 if QuestHelper_File["collect_quest.lua"] == "Development Version" then debug_output = true end
7 local IsMonsterGUID
8 local GetMonsterType
10 local GetQuestType
11 local GetItemType
13 local GetLoc
15 local QHCQ
17 local deebey
19 local function RegisterQuestData(category, location, GetQuestLogWhateverInfo)
20 local index = 1
21 local localspot
22 while true do
23 local ilink = GetQuestLogItemLink(category, index)
24 if not ilink then break end
26 if not localspot then if not location["items_" .. category] then location["items_" .. category] = {} end localspot = location["items_" .. category] end
28 local name, tex, num, qual, usa = GetQuestLogWhateverInfo(index)
29 localspot[GetItemType(ilink)] = num
31 --QuestHelper:TextOut(string.format("%s:%d - %d %s %s", category, index, num, tostring(ilink), tostring(name)))
33 index = index + 1
34 end
35 end
37 local complete_suffix = string.gsub(string.gsub(string.gsub(ERR_QUEST_OBJECTIVE_COMPLETE_S, "%%s", ""), "%)", "%%)"), "%(", "%%(")
38 function pin()
39 QuestHelper:TextOut("^.*: (%d+)/(%d+)(" .. complete_suffix .. ")?$")
40 end
42 -- qlookup[questname][objectivename] = {{qid = qid, objid = objid}}
43 local qlookups = {}
45 local function ScanQuests()
47 local selected
48 local index = 1
50 local dbx = {}
52 qlookups = {}
54 while true do
55 if not GetQuestLogTitle(index) then break end
57 local qlink = GetQuestLink(index)
58 if qlink then
59 --QuestHelper:TextOut(qlink)
60 --QuestHelper:TextOut(string.gsub(qlink, "|", "||"))
61 local id, level = GetQuestType(qlink)
62 local title, _, tag, groupcount, _, _, _, daily = GetQuestLogTitle(index)
64 if not qlookups[title] then qlookups[title] = {} end -- gronk
66 --QuestHelper:TextOut(string.format("%s - %d %d", qlink, id, level))
68 if not QHCQ[id] then
69 --if true then
70 if not selected then selected = GetQuestLogSelection() end
71 SelectQuestLogEntry(index)
73 QHCQ[id] = {}
74 QHCQ[id].level = level
76 RegisterQuestData("reward", QHCQ[id], GetQuestLogRewardInfo)
77 RegisterQuestData("choice", QHCQ[id], GetQuestLogChoiceInfo)
79 --QuestHelper:TextOut(string.format("%d", GetNumQuestLeaderBoards(index)))
80 for i = 1, GetNumQuestLeaderBoards(index) do
81 local desc, type = GetQuestLogLeaderBoard(i, index)
82 QHCQ[id][string.format("criteria_%d_text", i)] = desc
83 QHCQ[id][string.format("criteria_%d_type", i)] = type
84 --QuestHelper:TextOut(string.format("%s, %s", desc, type))
85 end
87 QHCQ[id].name = title
88 QHCQ[id].tag = tag
89 QHCQ[id].groupcount = (groupcount or -1)
90 QHCQ[id].daily = (not not daily)
92 if GetQuestLogSpecialItemInfo then
93 local typ = GetQuestLogSpecialItemInfo(index)
94 if typ then typ = GetItemType(typ) end
95 QHCQ[id].special_item = typ or false
96 end
97 end
99 dbx[id] = {}
102 --QuestHelper:TextOut(string.format("%d", GetNumQuestLeaderBoards(index)))
104 for i = 1, GetNumQuestLeaderBoards(index) do
105 local desc, _, done = GetQuestLogLeaderBoard(i, index)
107 if not qlookups[title][desc] then qlookups[title][desc] = {} end
108 table.insert(qlookups[title][desc], {qid = id, obj = i})
110 -- If we wanted to parse everything here, we'd do something very complicated.
111 -- Fundamentally, we don't. We only care if numeric values change or if something goes from "not done" to "done".
112 -- Luckily, the patterns are identical in all cases for this (I think.)
113 local have, needed = string.match(desc, "^.*: (%d+)/(%d+)$")
114 have = tonumber(have)
115 needed = tonumber(needed)
117 --[[QuestHelper:TextOut(desc)
118 QuestHelper:TextOut("^.*: (%d+)/(%d+)(" .. complete_suffix .. ")?$")
119 QuestHelper:TextOut(string.gsub(desc, complete_suffix, ""))
120 QuestHelper:TextOut(string.format("%s %s", tostring(have), tostring(needed)))]]
121 if not have or not needed then
122 have = done and 1 or 0
123 needed = 1 -- okay so we don't really use this unless we're debugging, shut up >:(
126 dbx[id][i] = have
130 index = index + 1
133 if selected then SelectQuestLogEntry(selected) end -- abort abort bzzt bzzt bzzt awoooooooga dive, dive, dive
135 return dbx
138 local eventy = {}
140 local function Looted(message)
141 local ltype = GetItemType(message, true)
142 table.insert(eventy, {time = GetTime(), event = string.format("I%di", ltype)})
143 --if debug_output then QuestHelper:TextOut(string.format("Added event %s", string.format("I%di", ltype))) end
146 local function Combat(_, event, _, _, _, guid)
147 if event ~= "UNIT_DIED" then return end
148 if not IsMonsterGUID(guid) then return end
149 local mtype = GetMonsterType(guid, true)
150 table.insert(eventy, {time = GetTime(), event = string.format("M%dm", mtype)})
151 --if debug_output then QuestHelper:TextOut(string.format("Added event %s", string.format("M%dm", mtype))) end
154 local changed = false
155 local first = true
157 local function Init()
158 first = true
161 local function LogChanged()
162 changed = true
165 local function WatchUpdate() -- we're currently ignoring the ID of the quest that was updated for simplicity's sake.
166 changed = true
169 local function AppendMember(tab, key, dat)
170 tab[key] = (tab[key] or "") .. dat
173 local function StartOrEnd(se, id)
174 local targuid = UnitGUID("target")
175 local chunk = ""
176 if targuid and IsMonsterGUID(targuid) then
177 chunk = string.format("M%dm", GetMonsterType(targuid))
179 chunk = chunk .. GetLoc()
181 AppendMember(QHCQ[id], se, chunk)
184 local abandoncomplete = ""
185 local abandoncomplete_timestamp = nil
187 local GetQuestReward_Orig = GetQuestReward
188 GetQuestReward = function (...)
189 abandoncomplete = "complete"
190 abandoncomplete_timestamp = GetTime()
191 GetQuestReward_Orig(...)
194 local AbandonQuest_Orig = AbandonQuest
195 AbandonQuest = function ()
196 abandoncomplete = "abandon"
197 abandoncomplete_timestamp = GetTime()
198 AbandonQuest_Orig()
201 local function UpdateQuests()
203 do -- this should once and for all fix this issue
204 local foverride = true
205 for _, _ in pairs(deebey) do
206 foverride = false
208 if UnitLevel("player") == 1 then
209 foverride = false
211 if foverride then foverride = true end
214 if first then deebey = ScanQuests() first = false end
215 if not changed then return end
216 changed = false
218 local tim = GetTime()
220 local noobey = ScanQuests()
222 local traverse = {}
224 local dsize, nsize = QuestHelper:TableSize(deebey), QuestHelper:TableSize(noobey)
226 for k, _ in pairs(deebey) do traverse[k] = true end
227 for k, _ in pairs(noobey) do traverse[k] = true end
229 --[[
230 if QuestHelper:TableSize(deebey) ~= QuestHelper:TableSize(noobey) then
231 QuestHelper:TextOut(string.format("%d %d", QuestHelper:TableSize(deebey), QuestHelper:TableSize(noobey)))
232 end]]
234 while #eventy > 0 and eventy[1].time < GetTime() - 1 do table.remove(eventy, 1) end -- slurp
235 local token
236 local debugtok
238 local diffs = 0
240 for k, _ in pairs(traverse) do
241 if not deebey[k] then
242 -- Quest was acquired
243 if debug_output then QuestHelper:TextOut(string.format("Acquired! Questid %d", k)) end
244 StartOrEnd("start", k)
245 diffs = diffs + 1
247 elseif not noobey[k] then
248 -- Quest was dropped or completed
249 if abandoncomplete == "complete" and abandoncomplete_timestamp + 30 >= GetTime() then
250 if debug_output then QuestHelper:TextOut(string.format("Completed! Questid %d", k)) end
251 StartOrEnd("end", k)
252 abandoncomplete = ""
253 else
254 if debug_output then QuestHelper:TextOut(string.format("Dropped! Questid %d", k)) end
257 diffs = diffs + 1
259 else
260 QuestHelper: Assert(#deebey[k] == #noobey[k])
261 for i = 1, #deebey[k] do
263 --[[
264 if not (noobey[k][i] >= deebey[k][i]) then
265 QuestHelper:TextOut(string.format("%s, %s", type(noobey[k][i]), type(deebey[k][i])))
266 QuestHelper:TextOut(string.format("%d, %d", noobey[k][i], deebey[k][i]))
267 for index = 1, 100 do
268 local qlink = GetQuestLink(index)
269 if qlink then qlink = GetQuestType(qlink) end
270 if qlink == k then
271 QuestHelper:TextOut(GetQuestLogLeaderBoard(i, index))
275 QuestHelper: Assert(noobey[k][i] >= deebey[k][i]) -- man I hope this is true]] -- This entire section can fail if people throw away quest items, or if quest items have a duration that expires. Sigh.
277 if noobey[k][i] > deebey[k][i] then
278 if not token then
279 token = ""
280 for k, v in pairs(eventy) do token = token .. v.event end
281 debugtok = token
282 token = token .. "L" .. GetLoc() .. "l"
285 local ttok = token
286 if noobey[k][i] - 1 ~= deebey[k][i] then
287 ttok = string.format("C%dc", noobey[k][i] - deebey[k][i]) .. ttok
290 AppendMember(QHCQ[k], string.format("criteria_%d_satisfied", i), ttok)
292 if debug_output then QuestHelper:TextOut(string.format("Updated! Questid %d item %d count %d tok %s", k, i, noobey[k][i] - deebey[k][i], debugtok)) end
293 diffs = diffs + 1
299 deebey = noobey
301 --QuestHelper: Assert(diffs <= 5, string.format("excessive quest diffs - delta is %d, went from %d to %d", diffs, dsize, nsize))
302 --QuestHelper:TextOut(string.format("done in %f", GetTime() - tim))
305 local enable_quest_hints = GetBuildInfo():match("0%.1%..*") or (GetBuildInfo():match("3%..*") and not GetBuildInfo():match("3%.0%..*"))
306 QH_filter_hints = false
308 local function MouseoverUnit()
309 QH_filter_hints = false
310 if not enable_quest_hints then return end
312 if GameTooltip:GetUnit() and UnitExists("mouseover") and UnitIsVisible("mouseover") and not UnitIsPlayer("mouseover") and not UnitPlayerControlled("mouseover") then
313 local guid = UnitGUID("mouseover")
315 if not IsMonsterGUID(guid) then return end
317 guid = GetMonsterType(guid)
319 if GetQuestLogSpecialItemInfo then
320 for _, v in pairs(qlookups) do
321 for _, block in pairs(v) do
322 for _, tv in ipairs(block) do
323 if not QHCQ[tv.qid][string.format("criteria_%d_monster_true", tv.obj)] then
324 QHCQ[tv.qid][string.format("criteria_%d_monster_true", tv.obj)] = {}
325 QHCQ[tv.qid][string.format("criteria_%d_monster_false", tv.obj)] = {}
328 QHCQ[tv.qid][string.format("criteria_%d_monster_false", tv.obj)][guid] = (QHCQ[tv.qid][string.format("criteria_%d_monster_false", tv.obj)][guid] or 0) + 1
333 local line = 2
334 local qs
335 local qe
337 while _G["GameTooltipTextLeft" .. line] and _G["GameTooltipTextLeft" .. line]:IsShown() do
338 local r, g, b, a = _G["GameTooltipTextLeft" .. line]:GetTextColor()
339 r, g, b, a = math.floor(r * 255 + 0.5), math.floor(g * 255 + 0.5), math.floor(b * 255 + 0.5), math.floor(a * 255 + 0.5)
340 --print(r, g, b, a)
342 if r == 255 and g == 210 and b == 0 and a == 255 then
343 if not qs then qs = line end
344 else
345 if qs and not qe then qe = line end
347 line = line + 1
350 if qs and not qe then qe = line end
351 if qe then qe = qe - 1 end
353 if qs and qe then
354 local cquest = nil
356 QH_filter_hints = true
358 for i = qs, qe do
359 local lin = _G["GameTooltipTextLeft" .. i]:GetText()
361 if cquest and cquest[lin] then
362 local titem_block = cquest[lin]
363 for _, titem in pairs(titem_block) do
364 QHCQ[titem.qid][string.format("criteria_%d_monster_false", titem.obj)][guid] = (QHCQ[titem.qid][string.format("criteria_%d_monster_false", titem.obj)][guid] or 0) - 1
365 QHCQ[titem.qid][string.format("criteria_%d_monster_true", titem.obj)][guid] = (QHCQ[titem.qid][string.format("criteria_%d_monster_true", titem.obj)][guid] or 0) + 1
367 elseif qlookups[lin] then
368 cquest = qlookups[lin]
369 else
370 QH_filter_hints = false
371 --QuestHelper: Assert()
379 function QH_Collect_Quest_Init(QHCData, API)
380 if not QHCData.quest then QHCData.quest = {} end
381 QHCQ = QHCData.quest
383 GetQuestType = API.Utility_GetQuestType
384 GetItemType = API.Utility_GetItemType
385 IsMonsterGUID = API.Utility_IsMonsterGUID
386 GetMonsterType = API.Utility_GetMonsterType
387 QuestHelper: Assert(GetQuestType)
388 QuestHelper: Assert(GetItemType)
389 QuestHelper: Assert(IsMonsterGUID)
390 QuestHelper: Assert(GetMonsterType)
392 GetLoc = API.Callback_LocationBolusCurrent
393 QuestHelper: Assert(GetLoc)
395 deebey = ScanQuests()
397 QH_Event("UNIT_QUEST_LOG_CHANGED", LogChanged)
398 QH_Event("QUEST_LOG_UPDATE", UpdateQuests)
399 QH_Event("QUEST_WATCH_UPDATE", WatchUpdate)
401 QH_Event("CHAT_MSG_LOOT", Looted)
402 QH_Event("COMBAT_LOG_EVENT_UNFILTERED", Combat)
404 API.Registrar_TooltipHook(MouseoverUnit)
406 -- Here's a pile of events that seem to trigger during startup that also don't seem like would trigger while questing.
407 -- We'll lose a few quest updates from this, but that's OK.
408 QH_Event("PLAYER_ENTERING_WORLD", Init)
409 QH_Event("UNIT_MODEL_CHANGED", Init)
410 QH_Event("GUILDBANK_UPDATE_WITHDRAWMONEY", Init)
411 QH_Event("UPDATE_TICKET", Init)