Automated update from: http://smariot.hopto.org/translate
[QuestHelper.git] / utility.lua
blob654c4bd891b52871b1ce1ef749f8a132c3eb0a92
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 for k = 1, (math.floor(GetTime() % 1000) + 5) do math.random() end -- it's sort of like seeding. only worse.
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(true, 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") -- we're just going to do away with this entirely, the fact is that a lot of this is going to be ripped to shreds soon anyway
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:Location_RawString(delayed, c, z, x, y)
260 return ("[|cffffffff%s/%s,%s,%s,%s|r]"):format(delayed and "D" or "c", c and string.format("%d", c) or tostring(c), z and string.format("%d", z) or tostring(z), x and string.format("%.3f", x) or tostring(x), y and string.format("%.3f", y) or tostring(y))
262 function QuestHelper:Location_AbsoluteString(delayed, c, x, y)
263 return ("[|cffffffff%s/%s,%s,%s|r]"):format(delayed and "D" or "c", c and string.format("%d", c) or tostring(c), x and string.format("%.3f", x) or tostring(x), y and string.format("%.3f", y) or tostring(y))
266 function QuestHelper:Distance(i1, x1, y1, i2, x2, y2)
267 local p1, p2 = QuestHelper_ZoneLookup[i1], QuestHelper_ZoneLookup[i2]
268 return self.Astrolabe:ComputeDistance(p1[1], p1[2], x1, y1, p2[1], p2[2], x2, y2) or 10000
271 function QuestHelper:AppendPosition(list, index, x, y, w, min_dist)
272 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
273 local nc, nz, nx, ny = self.Astrolabe:GetCurrentPlayerPosition()
274 --self:AppendNotificationError("2008-10-6 nil-position", string.format("nilposition, %s %s %s %s vs %s %s", tostring(nc), tostring(nz), tostring(nx), tostring(ny), tostring(x), tostring(y))) -- We're just not worrying about this too much anymore. Slash and burn.
275 return list -- This isn't a real position.
278 local closest, distance = nil, 0
279 w = w or 1
280 min_dist = min_dist or 200
282 for i, p in ipairs(list) do
283 if index == p[1] then
284 local d = self:Distance(index, x, y, p[1], p[2], p[3])
285 if not closest or d < distance then
286 closest, distance = i, d
291 if closest and distance < min_dist then
292 local p = list[closest]
293 p[2] = (p[2]*p[4]+x*w)/(p[4]+w)
294 p[3] = (p[3]*p[4]+y*w)/(p[4]+w)
295 p[4] = p[4]+w
296 else
297 table.insert(list, {index, x, y, w})
300 return list
303 function QuestHelper:PositionListDistance(list, index, x, y)
304 local closest, distance = nil, 0
305 for i, p in ipairs(list) do
306 local d = self:Distance(index, x, y, p[1], p[2], p[3])
307 if not closest or d < distance then
308 closest, distance = p, d
311 if closest then
312 return distance, closest[1], closest[2], closest[3]
316 function QuestHelper:PositionListDistance2(list, i1, x1, y1, i2, x2, y2)
317 local closest, bd1, bd2, bdt = nil, 0, 0, 0
318 for i, p in ipairs(list) do
319 local d1 = self:Distance(i1, x1, y1, p[1], p[2], p[3])
320 local d2 = self:Distance(i2, x2, y2, p[1], p[2], p[3])
321 local t = d1+d2
322 if not closest or t < bdt then
323 closest, bd1, bd2, bdt = p, d1, d2, t
326 if closest then
327 return d1, d2, closest[1], closest[2], closest[3]
331 function QuestHelper:MergePositions(list1, list2)
332 for i, p in ipairs(list2) do
333 self:AppendPosition(list1, unpack(p))
337 function QuestHelper:MergeDrops(list1, list2)
338 for element, count in pairs(list2) do
339 list1[element] = (list1[element] or 0) + count
343 function QuestHelper: Assert(a, b) -- the space exists so the anti-assert script doesn't find it :D
344 if not a then
345 QuestHelper:Error(b or "Assertion Failed")
349 function QuestHelper:StringizeTable(a)
350 if not a then return "nil" end
351 acu = tostring(self.recycle_tabletyping[a])..": "
352 for i,v in pairs(a) do acu = acu.."["..tostring(i)..","..tostring(v).."] " end
353 return acu
356 function QuestHelper:StringizeTableDouble(a)
357 if not a then return "nil" end
358 acu = tostring(self.recycle_tabletyping[a])..": "
359 for i,v in pairs(a) do acu = acu.."["..self:StringizeTable(i)..","..self:StringizeTable(v).."] " end
360 return acu
363 function QuestHelper:StringizeRecursive(a, d)
364 if not a then return "nil" end
365 if d <= 0 or type(a) ~= "table" then return tostring(a) end
366 acu = tostring(self.recycle_tabletyping[a])..": "
367 for i,v in pairs(a) do acu = acu.."["..self:StringizeRecursive(i, d - 1)..","..self:StringizeRecursive(v, d - 1).."] " end
368 return acu
371 function QuestHelper:TableSize(tbl)
372 local count = 0
373 for k, v in pairs(tbl) do
374 count = count + 1
376 return count
379 function QuestHelper:IsWrath()
380 --return GetBuildInfo():sub(1,1) == '3' or GetBuildInfo() == "0.0.2" -- come on
381 return true -- this had better be true :D
384 function QuestHelper:AppendNotificationError(type, data)
385 local terror = QuestHelper_ErrorPackage(2)
386 terror.data = data
387 QuestHelper_ErrorCatcher_RegisterError(type, terror)