Displays the change log in-game when upgraded.
[QuestHelper.git] / utility.lua
blobf403a3ff679ed413172af1a51c7803bdf9d92229
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=1, select("#", GetMapContinents()) do
69 for z=0, select("#", GetMapZones(c)) do
70 local name
72 if z == 0 then
73 name = select(c, GetMapContinents())
74 else
75 name = select(z, GetMapZones(c))
76 end
78 assert(name)
80 if QuestHelper_Zones[c][z] ~= name then
81 sane = false
82 QuestHelper:TextOut("'"..name.."' has the wrong ID.")
83 end
85 local pair = QuestHelper_ZoneLookup[name]
87 if not pair or c ~= pair[1] or z ~= pair[2] then
88 sane = false
89 QuestHelper:TextOut("ZoneLookup['"..name.."'] maps to wrong pair.")
90 end
92 local index = QuestHelper_IndexLookup[name]
93 if QuestHelper_ZoneLookup[index] ~= pair then
94 sane = false
95 QuestHelper:TextOut("ZoneLookup['"..name.."'] isn't equal to ZoneLookup["..index.."]")
96 end
98 if not index or QuestHelper_NameLookup[index] ~= name then
99 sane = false
100 QuestHelper:TextOut("NameLookup["..(index or "???").."'] doesn't equal '"..name.."'")
105 return sane
108 function QuestHelper:TextOut(text)
109 local theme = self:GetColourTheme()
110 DEFAULT_CHAT_FRAME:AddMessage(string.format("|cff%2x%2x%2xQuestHelper: |r%s", theme.message_prefix[1]*255,
111 theme.message_prefix[2]*255,
112 theme.message_prefix[3]*255, text),
113 theme.message[1],
114 theme.message[2],
115 theme.message[3])
118 function QuestHelper:Error(what)
119 DEFAULT_CHAT_FRAME:AddMessage("QuestHelper Error: "..(what or "Unknown").."\n"..debugstack(2), 1,.5,0)
120 error("Abort!")
123 function QuestHelper:HighlightText(text)
124 local theme = self:GetColourTheme()
125 return string.format("|cff%2x%2x%2x%s|r", theme.message_highlight[1]*255,
126 theme.message_highlight[2]*255,
127 theme.message_highlight[3]*255, text)
130 function QuestHelper:GetUnitID(unit)
131 local id = UnitGUID(unit)
133 if id then
134 return (string.sub(id, 5, 5) == "3") and tonumber(string.sub(id, 6, 12), 16) or nil
137 return nil
140 function QuestHelper:GetQuestID(index)
141 return tonumber(select(3, string.find(GetQuestLink(index), "|Hquest:(%d+):")))
144 -- For future reference:
145 -- Hearthstone = 6948
146 -- Rune of Teleportation = 17031
147 -- Rune of Portals = 17032
149 function QuestHelper:CountItem(item_id)
150 local count = 0
152 for bag = 0,NUM_BAG_SLOTS do
153 for slot = 1,GetContainerNumSlots(bag) do
154 local link = GetContainerItemLink(bag, slot)
155 if link and string.find(link, string.format("|Hitem:%d:", item_id)) then
156 count = count + (select(2, GetContainerItemInfo(bag, slot)) or 0)
161 return count
164 function QuestHelper:ItemCooldown(item_id)
165 local now = GetTime()
166 local cooldown = nil
168 for bag = 0,NUM_BAG_SLOTS do
169 for slot = 1,GetContainerNumSlots(bag) do
170 local link = GetContainerItemLink(bag, slot)
171 if link and string.find(link, string.format("|Hitem:%d:", item_id)) then
172 local s, d, e = GetContainerItemCooldown(bag, slot)
173 if e then
174 if cooldown then
175 cooldown = math.min(cooldown, math.max(0, d-now+s))
176 else
177 cooldown = math.max(0, d-now+s)
179 else
180 return 0
186 return cooldown
189 function QuestHelper:TimeString(seconds)
190 local seconds = math.ceil(seconds)
191 local h, m, s = math.floor(seconds/(60*60)), math.floor(seconds/60)%60, seconds%60
192 if h > 0 then
193 return string.format("|cffffffff%d|r:|cffffffff%02d|r:|cffffffff%02d|r", h, m, s)
194 else
195 return string.format("|cffffffff%d|r:|cffffffff%02d|r", m, s)
199 function QuestHelper:ProgressString(str, pct)
200 if pct > 1 then
201 return string.format("|cff00ff00%s|r", str)
202 elseif pct < 0 then
203 return string.format("|cffff0000%s|r", str)
204 elseif pct > 0.5 then
205 return string.format("|cff%2xff00%s|r", 510-pct*510, str)
206 else
207 return string.format("|cffff%2x00%s|r", pct*510, str)
211 function QuestHelper:PercentString(pct)
212 if pct > 1 then
213 return string.format("|cff00ff00%.1f%%|r", pct*100)
214 elseif pct < 0 then
215 return string.format("|cffff0000%.1f%%|r", pct*100)
216 elseif pct > 0.5 then
217 return string.format("|cff%2xff00%.1f%%|r", 510-pct*510, pct*100)
218 else
219 return string.format("|cffff%2x00%.1f%%|r", pct*510, pct*100)
223 function QuestHelper:PlayerPosition()
224 return self.i, self.x, self.y
227 function QuestHelper:UnitPosition(unit)
228 local c, z, x, y = self.Astrolabe:GetUnitPosition(unit,true)
229 if c then
230 if z == 0 then
231 SetMapToCurrentZone()
232 z = GetCurrentMapZone()
233 if z ~= 0 then
234 x, y = self.Astrolabe:TranslateWorldMapPosition(c, 0, x, y, c, z)
237 return QuestHelper_IndexLookup[c][z], x, y
238 else
239 return self:PlayerPosition()
243 function QuestHelper:LocationString(i, x, y)
244 return ("[|cffffffff%s|r:|cffffffff%d,%.3f,%.3f|r]"):format(QuestHelper_NameLookup[i] or "nil", i, x, y)
247 function QuestHelper:Distance(i1, x1, y1, i2, x2, y2)
248 local p1, p2 = QuestHelper_ZoneLookup[i1], QuestHelper_ZoneLookup[i2]
249 return self.Astrolabe:ComputeDistance(p1[1], p1[2], x1, y1, p2[1], p2[2], x2, y2) or 10000
252 function QuestHelper:AppendPosition(list, index, x, y, w, min_dist)
253 if (x == 0 and y == 0) or x <= -0.1 or y <= -0.1 or x >= 1.1 or y >= 1.1 then
254 return list -- This isn't a real position.
257 local closest, distance = nil, 0
258 w = w or 1
259 min_dist = min_dist or 200
261 for i, p in ipairs(list) do
262 if index == p[1] then
263 local d = self:Distance(index, x, y, p[1], p[2], p[3])
264 if not closest or d < distance then
265 closest, distance = i, d
270 if closest and distance < min_dist then
271 local p = list[closest]
272 p[2] = (p[2]*p[4]+x*w)/(p[4]+w)
273 p[3] = (p[3]*p[4]+y*w)/(p[4]+w)
274 p[4] = p[4]+w
275 else
276 table.insert(list, {index, x, y, w})
279 return list
282 function QuestHelper:PositionListDistance(list, index, x, y)
283 local closest, distance = nil, 0
284 for i, p in ipairs(list) do
285 local d = self:Distance(index, x, y, p[1], p[2], p[3])
286 if not closest or d < distance then
287 closest, distance = p, d
290 if closest then
291 return distance, closest[1], closest[2], closest[3]
295 function QuestHelper:PositionListDistance2(list, i1, x1, y1, i2, x2, y2)
296 local closest, bd1, bd2, bdt = nil, 0, 0, 0
297 for i, p in ipairs(list) do
298 local d1 = self:Distance(i1, x1, y1, p[1], p[2], p[3])
299 local d2 = self:Distance(i2, x2, y2, p[1], p[2], p[3])
300 local t = d1+d2
301 if not closest or t < bdt then
302 closest, bd1, bd2, bdt = p, d1, d2, t
305 if closest then
306 return d1, d2, closest[1], closest[2], closest[3]
310 function QuestHelper:MergePositions(list1, list2)
311 for i, p in ipairs(list2) do
312 self:AppendPosition(list1, unpack(p))
316 function QuestHelper:MergeDrops(list1, list2)
317 for element, count in pairs(list2) do
318 list1[element] = (list1[element] or 0) + count