disable the flight stuff for now, we'll fix it after thanksgiving
[QuestHelper.git] / utility.lua
blob23b6676f74f5bf892af6696e38c21e7bdf7ccf7e
1 QuestHelper_File["utility.lua"] = "Development Version"
3 local default_colour_theme =
4 {message_prefix={0.4, 0.78, 1},
5 message={1, 0.6, 0.2},
6 tooltip={1, 0.8, 0.5},
7 message_highlight={0.73, 1, 0.84},
8 menu_text={1, 1, 1},
9 menu_text_highlight={0, 0, 0},
10 menu={0, 0, 0},
11 menu_highlight={0.3, 0.5, 0.7},
12 menu_title_text={1, 1, 1},
13 menu_title_text_highlight={1, 1, 1},
14 menu_title={0, 0.2, 0.6},
15 menu_title_highlight={0.1, 0.4, 0.8}}
17 local xmas_colour_theme =
18 {message_prefix={0.0, 0.7, 0.0},
19 message={0.2, 1, 0.2},
20 tooltip={0.4, 1, 0.4},
21 message_highlight={1, 0.3, 0.1},
22 menu_text={1, 1, 1},
23 menu_text_highlight={0, 0, 0},
24 menu={0.2, 0, 0},
25 menu_highlight={1, 0.3, 0.3},
26 menu_title_text={0.8, 1, 0.8},
27 menu_title_text_highlight={1, 1, 1},
28 menu_title={0.2, 0.6, 0.2},
29 menu_title_highlight={0.4, 0.7, 0.4}}
31 function QuestHelper:GetColourTheme()
32 if date("%b%d") == "Dec25" then
33 return xmas_colour_theme
34 end
36 return default_colour_theme
37 end
39 QuestHelper.nop = function () end -- Who wouldn't want a function that does nothing?
41 function QuestHelper:HashString(text)
42 -- Computes an Adler-32 checksum.
43 local a, b = 1, 0
44 for i=1,string.len(text) do
45 a = (a+string.byte(text,i))%65521
46 b = (b+a)%65521
47 end
48 return b*65536+a
49 end
51 function QuestHelper:CreateUID(length)
52 local result = ""
53 local characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
54 math.randomseed(math.random(0, 2147483647)+GetTime()*100000)
55 local base = GetUnitName("player")..":"..GetRealmName()..":"..math.random(0, 2147483647)..":"..GetTime()..":"..time()
57 for c = 1,(length or 32) do
58 local pos = 1+math.floor(self:HashString(result..base..math.random(0, 2147483647))%string.len(characters))
59 result = result .. string.sub(characters, pos, pos)
60 end
62 return result
63 end
65 function QuestHelper:ZoneSanity()
66 local sane = true
68 for c in pairs(self.Astrolabe:GetMapVirtualContinents()) do
69 local pz = self.Astrolabe:GetMapVirtualZones(c)
70 pz[0] = true
71 for z in pairs(pz) do
72 local name
74 if z == 0 then
75 name = self.Astrolabe:GetMapVirtualContinents()[c]
76 else
77 name = self.Astrolabe:GetMapVirtualZones(c)[z]
78 end
80 assert(name)
82 if QuestHelper_Zones[c][z] ~= name then
83 sane = false
84 QuestHelper:TextOut(string.format("'%s' has the wrong ID (should be %d,%d).", name, c, z))
85 end
87 local pair = QuestHelper_ZoneLookup[name]
89 if not pair or c ~= pair[1] or z ~= pair[2] then
90 sane = false
91 QuestHelper:TextOut("ZoneLookup['"..name.."'] maps to wrong pair.")
92 end
94 local index = QuestHelper_IndexLookup[name]
95 if QuestHelper_ZoneLookup[index] ~= pair then
96 sane = false
97 QuestHelper:TextOut("ZoneLookup['"..name.."'] isn't equal to ZoneLookup["..index.."]")
98 end
100 if not index or QuestHelper_NameLookup[index] ~= name then
101 sane = false
102 QuestHelper:TextOut("NameLookup["..(index or "???").."'] doesn't equal '"..name.."'")
107 return sane
110 function QuestHelper:TextOut(text)
111 local theme = self:GetColourTheme()
112 DEFAULT_CHAT_FRAME:AddMessage(string.format("|cff%2x%2x%2xQuestHelper: |r%s", theme.message_prefix[1]*255,
113 theme.message_prefix[2]*255,
114 theme.message_prefix[3]*255, text),
115 theme.message[1],
116 theme.message[2],
117 theme.message[3])
120 function QuestHelper:Error(what)
121 DEFAULT_CHAT_FRAME:AddMessage("QuestHelper Error: "..(what or "Unknown").."\n"..debugstack(2), 1,.5,0)
122 QuestHelper_ErrorCatcher_ExplicitError(what or "Unknown", nil, nil)
123 error("Abort!")
126 function QuestHelper:HighlightText(text)
127 local theme = self:GetColourTheme()
128 return string.format("|cff%2x%2x%2x%s|r", theme.message_highlight[1]*255,
129 theme.message_highlight[2]*255,
130 theme.message_highlight[3]*255, text)
133 function QuestHelper:GetUnitID(unit)
134 local id = UnitGUID(unit)
136 if id then
137 return (string.sub(id, 5, 5) == "3") and tonumber(string.sub(id, 6, 12), 16) or nil
140 return nil
143 function QuestHelper:GetQuestID(index)
144 return tonumber(select(3, string.find(GetQuestLink(index), "|Hquest:(%d+):")))
147 -- For future reference:
148 -- Hearthstone = 6948
149 -- Rune of Teleportation = 17031
150 -- Rune of Portals = 17032
152 function QuestHelper:CountItem(item_id)
153 local count = 0
155 for bag = 0,NUM_BAG_SLOTS do
156 for slot = 1,GetContainerNumSlots(bag) do
157 local link = GetContainerItemLink(bag, slot)
158 if link and string.find(link, string.format("|Hitem:%d:", item_id)) then
159 count = count + (select(2, GetContainerItemInfo(bag, slot)) or 0)
164 return count
167 function QuestHelper:ItemCooldown(item_id)
168 local now = GetTime()
169 local cooldown = nil
171 for bag = 0,NUM_BAG_SLOTS do
172 for slot = 1,GetContainerNumSlots(bag) do
173 local link = GetContainerItemLink(bag, slot)
174 if link and string.find(link, string.format("|Hitem:%d:", item_id)) then
175 local s, d, e = GetContainerItemCooldown(bag, slot)
176 if e then
177 if cooldown then
178 cooldown = math.min(cooldown, math.max(0, d-now+s))
179 else
180 cooldown = math.max(0, d-now+s)
182 else
183 return 0
189 return cooldown
192 function QuestHelper:TimeString(seconds)
193 if not seconds then
194 self:AppendNotificationError("2008-10-8 nil-timestring")
195 return "(unknown)"
198 local seconds = math.ceil(seconds)
199 local h, m, s = math.floor(seconds/(60*60)), math.floor(seconds/60)%60, seconds%60
200 if h > 0 then
201 return string.format("|cffffffff%d|r:|cffffffff%02d|r:|cffffffff%02d|r", h, m, s)
202 else
203 return string.format("|cffffffff%d|r:|cffffffff%02d|r", m, s)
207 function QuestHelper:ProgressString(str, pct)
208 if pct > 1 then
209 return string.format("|cff00ff00%s|r", str)
210 elseif pct < 0 then
211 return string.format("|cffff0000%s|r", str)
212 elseif pct > 0.5 then
213 return string.format("|cff%2xff00%s|r", 510-pct*510, str)
214 else
215 return string.format("|cffff%2x00%s|r", pct*510, str)
219 function QuestHelper:PercentString(pct)
220 if pct > 1 then
221 return string.format("|cff00ff00%.1f%%|r", pct*100)
222 elseif pct < 0 then
223 return string.format("|cffff0000%.1f%%|r", pct*100)
224 elseif pct > 0.5 then
225 return string.format("|cff%2xff00%.1f%%|r", 510-pct*510, pct*100)
226 else
227 return string.format("|cffff%2x00%.1f%%|r", pct*510, pct*100)
231 function QuestHelper:PlayerPosition()
232 return self.i, self.x, self.y
235 function QuestHelper:UnitPosition(unit)
236 local c, z, x, y = self.Astrolabe:GetUnitPosition(unit,true)
237 if c then
238 if z == 0 then
239 SetMapToCurrentZone()
240 z = GetCurrentMapZone()
241 if z ~= 0 then
242 x, y = self.Astrolabe:TranslateWorldMapPosition(c, 0, x, y, c, z)
245 return QuestHelper_IndexLookup[c][z], x, y
246 else
247 return self:PlayerPosition()
251 function QuestHelper:PlayerFaction()
252 return UnitFactionGroup("player") == "Alliance" and 1 or 2
255 function QuestHelper:LocationString(i, x, y)
256 return ("[|cffffffff%s|r:|cffffffff%d,%.3f,%.3f|r]"):format(QuestHelper_NameLookup[i] or "nil", i or -7777, x or -7777, y or -7777)
258 function QuestHelper:LocationStringRaw(c, x, y, rc, rz)
259 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))
262 function QuestHelper:Distance(i1, x1, y1, i2, x2, y2)
263 local p1, p2 = QuestHelper_ZoneLookup[i1], QuestHelper_ZoneLookup[i2]
264 return self.Astrolabe:ComputeDistance(p1[1], p1[2], x1, y1, p2[1], p2[2], x2, y2) or 10000
267 function QuestHelper:AppendPosition(list, index, x, y, w, min_dist)
268 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
269 local nc, nz, nx, ny = self.Astrolabe:GetCurrentPlayerPosition()
270 self:AppendNotificationError("2008-10-6 nil-position", string.format("nilposition, %s %s %s %s", tostring(nc), tostring(nz), tostring(nx), tostring(ny)))
271 return list -- This isn't a real position.
274 local closest, distance = nil, 0
275 w = w or 1
276 min_dist = min_dist or 200
278 for i, p in ipairs(list) do
279 if index == p[1] then
280 local d = self:Distance(index, x, y, p[1], p[2], p[3])
281 if not closest or d < distance then
282 closest, distance = i, d
287 if closest and distance < min_dist then
288 local p = list[closest]
289 p[2] = (p[2]*p[4]+x*w)/(p[4]+w)
290 p[3] = (p[3]*p[4]+y*w)/(p[4]+w)
291 p[4] = p[4]+w
292 else
293 table.insert(list, {index, x, y, w})
296 return list
299 function QuestHelper:PositionListDistance(list, index, x, y)
300 local closest, distance = nil, 0
301 for i, p in ipairs(list) do
302 local d = self:Distance(index, x, y, p[1], p[2], p[3])
303 if not closest or d < distance then
304 closest, distance = p, d
307 if closest then
308 return distance, closest[1], closest[2], closest[3]
312 function QuestHelper:PositionListDistance2(list, i1, x1, y1, i2, x2, y2)
313 local closest, bd1, bd2, bdt = nil, 0, 0, 0
314 for i, p in ipairs(list) do
315 local d1 = self:Distance(i1, x1, y1, p[1], p[2], p[3])
316 local d2 = self:Distance(i2, x2, y2, p[1], p[2], p[3])
317 local t = d1+d2
318 if not closest or t < bdt then
319 closest, bd1, bd2, bdt = p, d1, d2, t
322 if closest then
323 return d1, d2, closest[1], closest[2], closest[3]
327 function QuestHelper:MergePositions(list1, list2)
328 for i, p in ipairs(list2) do
329 self:AppendPosition(list1, unpack(p))
333 function QuestHelper:MergeDrops(list1, list2)
334 for element, count in pairs(list2) do
335 list1[element] = (list1[element] or 0) + count
339 function QuestHelper: Assert(a, b) -- the space exists so the anti-assert script doesn't find it :D
340 if not a then
341 QuestHelper:Error(b or "Assertion Failed")
345 function QuestHelper:StringizeTable(a)
346 if not a then return "nil" end
347 acu = tostring(self.recycle_tabletyping[a])..": "
348 for i,v in pairs(a) do acu = acu.."["..tostring(i)..","..tostring(v).."] " end
349 return acu
352 function QuestHelper:StringizeTableDouble(a)
353 if not a then return "nil" end
354 acu = tostring(self.recycle_tabletyping[a])..": "
355 for i,v in pairs(a) do acu = acu.."["..self:StringizeTable(i)..","..self:StringizeTable(v).."] " end
356 return acu
359 function QuestHelper:StringizeRecursive(a, d)
360 if not a then return "nil" end
361 if d <= 0 or type(a) ~= "table" then return tostring(a) end
362 acu = tostring(self.recycle_tabletyping[a])..": "
363 for i,v in pairs(a) do acu = acu.."["..self:StringizeRecursive(i, d - 1)..","..self:StringizeRecursive(v, d - 1).."] " end
364 return acu
367 function QuestHelper:TableSize(tbl)
368 local count = 0
369 for k, v in pairs(tbl) do
370 count = count + 1
372 return count
375 function QuestHelper:IsWrath()
376 --return GetBuildInfo():sub(1,1) == '3' or GetBuildInfo() == "0.0.2" -- come on
377 return true -- this had better be true :D
380 function QuestHelper:AppendNotificationError(type, data)
381 if not QuestHelper_Errors[type] then QuestHelper_Errors[type] = {} end
382 local terror = QuestHelper_ErrorPackage(2)
383 terror.data = data
384 table.insert(QuestHelper_Errors[type], terror)