I am tired of this timing out
[QuestHelper.git] / utility.lua
blob49ab44fb0c2113335a74e12845b8ace7067d3853
1 QuestHelper_File["utility.lua"] = "Development Version"
2 QuestHelper_Loadtime["utility.lua"] = GetTime()
4 local default_colour_theme =
5 {message_prefix={0.4, 0.78, 1},
6 message={1, 0.6, 0.2},
7 tooltip={1, 0.8, 0.5},
8 message_highlight={0.73, 1, 0.84},
9 menu_text={1, 1, 1},
10 menu_text_highlight={0, 0, 0},
11 menu={0, 0, 0},
12 menu_highlight={0.3, 0.5, 0.7},
13 menu_title_text={1, 1, 1},
14 menu_title_text_highlight={1, 1, 1},
15 menu_title={0, 0.2, 0.6},
16 menu_title_highlight={0.1, 0.4, 0.8}}
18 local xmas_colour_theme =
19 {message_prefix={0.0, 0.7, 0.0},
20 message={0.2, 1, 0.2},
21 tooltip={0.4, 1, 0.4},
22 message_highlight={1, 0.3, 0.1},
23 menu_text={1, 1, 1},
24 menu_text_highlight={0, 0, 0},
25 menu={0.2, 0, 0},
26 menu_highlight={1, 0.3, 0.3},
27 menu_title_text={0.8, 1, 0.8},
28 menu_title_text_highlight={1, 1, 1},
29 menu_title={0.2, 0.6, 0.2},
30 menu_title_highlight={0.4, 0.7, 0.4}}
32 function QuestHelper:GetColourTheme()
33 if date("%b%d") == "Dec25" then
34 return xmas_colour_theme
35 end
37 return default_colour_theme
38 end
40 QuestHelper.nop = function () end -- Who wouldn't want a function that does nothing?
42 function QuestHelper:HashString(text)
43 -- Computes an Adler-32 checksum.
44 local a, b = 1, 0
45 for i=1,string.len(text) do
46 a = (a+string.byte(text,i))%65521
47 b = (b+a)%65521
48 end
49 return b*65536+a
50 end
52 function QuestHelper:CreateUID(length)
53 local result = ""
54 local characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
55 math.randomseed(math.random(0, 2147483647)+GetTime()*100000)
56 local base = GetUnitName("player")..":"..GetRealmName()..":"..math.random(0, 2147483647)..":"..GetTime()..":"..time()
58 for c = 1,(length or 32) do
59 local pos = 1+math.floor(self:HashString(result..base..math.random(0, 2147483647))%string.len(characters))
60 result = result .. string.sub(characters, pos, pos)
61 end
63 return result
64 end
66 function QuestHelper:ZoneSanity()
67 local sane = true
69 for c in pairs(self.Astrolabe:GetMapVirtualContinents()) do
70 local pz = self.Astrolabe:GetMapVirtualZones(c)
71 pz[0] = true
72 for z in pairs(pz) do
73 local name
75 if z == 0 then
76 name = self.Astrolabe:GetMapVirtualContinents()[c]
77 else
78 name = self.Astrolabe:GetMapVirtualZones(c)[z]
79 end
81 assert(name)
83 if QuestHelper_Zones[c][z] ~= name then
84 sane = false
85 QuestHelper:TextOut(string.format("'%s' has the wrong ID (should be %d,%d).", name, c, z))
86 end
88 local pair = QuestHelper_ZoneLookup[name]
90 if not pair or c ~= pair[1] or z ~= pair[2] then
91 sane = false
92 QuestHelper:TextOut("ZoneLookup['"..name.."'] maps to wrong pair.")
93 end
95 local index = QuestHelper_IndexLookup[name]
96 if QuestHelper_ZoneLookup[index] ~= pair then
97 sane = false
98 QuestHelper:TextOut("ZoneLookup['"..name.."'] isn't equal to ZoneLookup["..index.."]")
99 end
101 if not index or QuestHelper_NameLookup[index] ~= name then
102 sane = false
103 QuestHelper:TextOut("NameLookup["..(index or "???").."'] doesn't equal '"..name.."'")
108 return sane
111 function QuestHelper:TextOut(text)
112 local theme = self:GetColourTheme()
113 DEFAULT_CHAT_FRAME:AddMessage(string.format("|cff%2x%2x%2xQuestHelper: |r%s", theme.message_prefix[1]*255,
114 theme.message_prefix[2]*255,
115 theme.message_prefix[3]*255, text),
116 theme.message[1],
117 theme.message[2],
118 theme.message[3])
121 function QuestHelper:Error(what)
122 DEFAULT_CHAT_FRAME:AddMessage("QuestHelper Error: "..(what or "Unknown").."\n"..debugstack(2), 1,.5,0)
123 QuestHelper_ErrorCatcher_ExplicitError(what or "Unknown", nil, nil)
124 error("Abort!")
127 function QuestHelper:HighlightText(text)
128 local theme = self:GetColourTheme()
129 return string.format("|cff%2x%2x%2x%s|r", theme.message_highlight[1]*255,
130 theme.message_highlight[2]*255,
131 theme.message_highlight[3]*255, text)
134 function QuestHelper:GetUnitID(unit)
135 local id = UnitGUID(unit)
137 if id then
138 return (string.sub(id, 5, 5) == "3") and tonumber(string.sub(id, 6, 12), 16) or nil
141 return nil
144 function QuestHelper:GetQuestID(index)
145 return tonumber(select(3, string.find(GetQuestLink(index), "|Hquest:(%d+):")))
148 -- For future reference:
149 -- Hearthstone = 6948
150 -- Rune of Teleportation = 17031
151 -- Rune of Portals = 17032
153 function QuestHelper:CountItem(item_id)
154 local count = 0
156 for bag = 0,NUM_BAG_SLOTS do
157 for slot = 1,GetContainerNumSlots(bag) do
158 local link = GetContainerItemLink(bag, slot)
159 if link and string.find(link, string.format("|Hitem:%d:", item_id)) then
160 count = count + (select(2, GetContainerItemInfo(bag, slot)) or 0)
165 return count
168 function QuestHelper:ItemCooldown(item_id)
169 local now = GetTime()
170 local cooldown = nil
172 for bag = 0,NUM_BAG_SLOTS do
173 for slot = 1,GetContainerNumSlots(bag) do
174 local link = GetContainerItemLink(bag, slot)
175 if link and string.find(link, string.format("|Hitem:%d:", item_id)) then
176 local s, d, e = GetContainerItemCooldown(bag, slot)
177 if e then
178 if cooldown then
179 cooldown = math.min(cooldown, math.max(0, d-now+s))
180 else
181 cooldown = math.max(0, d-now+s)
183 else
184 return 0
190 return cooldown
193 function QuestHelper:TimeString(seconds)
194 if not seconds then
195 self:AppendNotificationError("2008-10-8 nil-timestring")
196 return "(unknown)"
199 local seconds = math.ceil(seconds)
200 local h, m, s = math.floor(seconds/(60*60)), math.floor(seconds/60)%60, seconds%60
201 if h > 0 then
202 return string.format("|cffffffff%d|r:|cffffffff%02d|r:|cffffffff%02d|r", h, m, s)
203 else
204 return string.format("|cffffffff%d|r:|cffffffff%02d|r", m, s)
208 function QuestHelper:ProgressString(str, pct)
209 if pct > 1 then
210 return string.format("|cff00ff00%s|r", str)
211 elseif pct < 0 then
212 return string.format("|cffff0000%s|r", str)
213 elseif pct > 0.5 then
214 return string.format("|cff%2xff00%s|r", 510-pct*510, str)
215 else
216 return string.format("|cffff%2x00%s|r", pct*510, str)
220 function QuestHelper:PercentString(pct)
221 if pct > 1 then
222 return string.format("|cff00ff00%.1f%%|r", pct*100)
223 elseif pct < 0 then
224 return string.format("|cffff0000%.1f%%|r", pct*100)
225 elseif pct > 0.5 then
226 return string.format("|cff%2xff00%.1f%%|r", 510-pct*510, pct*100)
227 else
228 return string.format("|cffff%2x00%.1f%%|r", pct*510, pct*100)
232 function QuestHelper:PlayerPosition()
233 return self.i, self.x, self.y
236 function QuestHelper:UnitPosition(unit)
237 local c, z, x, y = self.Astrolabe:GetUnitPosition(unit,true)
238 if c then
239 if z == 0 then
240 SetMapToCurrentZone()
241 z = GetCurrentMapZone()
242 if z ~= 0 then
243 x, y = self.Astrolabe:TranslateWorldMapPosition(c, 0, x, y, c, z)
246 return QuestHelper_IndexLookup[c][z], x, y
247 else
248 return self:PlayerPosition()
252 function QuestHelper:PlayerFaction()
253 return UnitFactionGroup("player") == "Alliance" and 1 or 2
256 function QuestHelper:LocationString(i, x, y)
257 return ("[|cffffffff%s|r:|cffffffff%d,%.3f,%.3f|r]"):format(QuestHelper_NameLookup[i] or "nil", i or -7777, x or -7777, y or -7777)
259 function QuestHelper:LocationStringRaw(c, x, y, rc, rz)
260 return ("[|cffffffff%s,%s,%s,%s,%s|r]"):format(tostring(c), x and string.format("%.3f", x) or tostring(x), y and string.format("%.3f", y) or tostring(y), tostring(rc), tostring(rz))
263 function QuestHelper:Distance(i1, x1, y1, i2, x2, y2)
264 local p1, p2 = QuestHelper_ZoneLookup[i1], QuestHelper_ZoneLookup[i2]
265 return self.Astrolabe:ComputeDistance(p1[1], p1[2], x1, y1, p2[1], p2[2], x2, y2) or 10000
268 function QuestHelper:AppendPosition(list, index, x, y, w, min_dist)
269 if not x or not y or (x == 0 and y == 0) or x <= -0.1 or y <= -0.1 or x >= 1.1 or y >= 1.1 then
270 local nc, nz, nx, ny = self.Astrolabe:GetCurrentPlayerPosition()
271 self:AppendNotificationError("2008-10-6 nil-position", string.format("nilposition, %s %s %s %s", tostring(nc), tostring(nz), tostring(nx), tostring(ny)))
272 return list -- This isn't a real position.
275 local closest, distance = nil, 0
276 w = w or 1
277 min_dist = min_dist or 200
279 for i, p in ipairs(list) do
280 if index == p[1] then
281 local d = self:Distance(index, x, y, p[1], p[2], p[3])
282 if not closest or d < distance then
283 closest, distance = i, d
288 if closest and distance < min_dist then
289 local p = list[closest]
290 p[2] = (p[2]*p[4]+x*w)/(p[4]+w)
291 p[3] = (p[3]*p[4]+y*w)/(p[4]+w)
292 p[4] = p[4]+w
293 else
294 table.insert(list, {index, x, y, w})
297 return list
300 function QuestHelper:PositionListDistance(list, index, x, y)
301 local closest, distance = nil, 0
302 for i, p in ipairs(list) do
303 local d = self:Distance(index, x, y, p[1], p[2], p[3])
304 if not closest or d < distance then
305 closest, distance = p, d
308 if closest then
309 return distance, closest[1], closest[2], closest[3]
313 function QuestHelper:PositionListDistance2(list, i1, x1, y1, i2, x2, y2)
314 local closest, bd1, bd2, bdt = nil, 0, 0, 0
315 for i, p in ipairs(list) do
316 local d1 = self:Distance(i1, x1, y1, p[1], p[2], p[3])
317 local d2 = self:Distance(i2, x2, y2, p[1], p[2], p[3])
318 local t = d1+d2
319 if not closest or t < bdt then
320 closest, bd1, bd2, bdt = p, d1, d2, t
323 if closest then
324 return d1, d2, closest[1], closest[2], closest[3]
328 function QuestHelper:MergePositions(list1, list2)
329 for i, p in ipairs(list2) do
330 self:AppendPosition(list1, unpack(p))
334 function QuestHelper:MergeDrops(list1, list2)
335 for element, count in pairs(list2) do
336 list1[element] = (list1[element] or 0) + count
340 function QuestHelper: Assert(a, b) -- the space exists so the anti-assert script doesn't find it :D
341 if not a then
342 QuestHelper:Error(b or "Assertion Failed")
346 function QuestHelper:StringizeTable(a)
347 if not a then return "nil" end
348 acu = tostring(self.recycle_tabletyping[a])..": "
349 for i,v in pairs(a) do acu = acu.."["..tostring(i)..","..tostring(v).."] " end
350 return acu
353 function QuestHelper:StringizeTableDouble(a)
354 if not a then return "nil" end
355 acu = tostring(self.recycle_tabletyping[a])..": "
356 for i,v in pairs(a) do acu = acu.."["..self:StringizeTable(i)..","..self:StringizeTable(v).."] " end
357 return acu
360 function QuestHelper:StringizeRecursive(a, d)
361 if not a then return "nil" end
362 if d <= 0 or type(a) ~= "table" then return tostring(a) end
363 acu = tostring(self.recycle_tabletyping[a])..": "
364 for i,v in pairs(a) do acu = acu.."["..self:StringizeRecursive(i, d - 1)..","..self:StringizeRecursive(v, d - 1).."] " end
365 return acu
368 function QuestHelper:TableSize(tbl)
369 local count = 0
370 for k, v in pairs(tbl) do
371 count = count + 1
373 return count
376 function QuestHelper:IsWrath()
377 --return GetBuildInfo():sub(1,1) == '3' or GetBuildInfo() == "0.0.2" -- come on
378 return true -- this had better be true :D
381 function QuestHelper:AppendNotificationError(type, data)
382 if not QuestHelper_Errors[type] then QuestHelper_Errors[type] = {} end
383 local terror = QuestHelper_ErrorPackage(2)
384 terror.data = data
385 table.insert(QuestHelper_Errors[type], terror)