Mercurial > wow > degaine
comparison Degaine.lua @ 63:3daeb57da1a9 tip
Automated merge with ssh://hg.wowace.com/wow/degaine/mainline
author | contrebasse |
---|---|
date | Wed, 01 Jun 2011 19:16:37 +0200 |
parents | 16828b1f9f9d |
children |
comparison
equal
deleted
inserted
replaced
61:60dfeef23620 | 63:3daeb57da1a9 |
---|---|
1 local AddonName, T = ... | |
2 -- Draw your weapons automagically | |
3 | |
4 | |
5 --------------------------------------------------------------- | |
6 -- Known bugs | |
7 --------------------------------------------------------------- | |
8 -- If you stand up by jumping or walking it won't draw your weapon | |
9 -- (it's not possible to know if you were sitting or standing before) | |
10 | |
11 --@debug@ | |
12 local debug = false | |
13 local debugAll = false | |
14 --@end-debug@ | |
15 | |
16 | |
17 --------------------------------------------------------------- | |
18 -- Globals to local | |
19 --------------------------------------------------------------- | |
20 -- These need to be globals : | |
21 -- GLOBALS: Degaine_ToggleAuto, Degaine_isAuto, BINDING_HEADER_DEGAINE, BINDING_NAME_DEGAINE_TOGGLE | |
22 | |
23 -- It doesn't matter if they are globals, rarely unsed : | |
24 -- GLOBALS: DEFAULT_CHAT_FRAME | |
25 | |
26 -- Lua functions | |
27 local pairs = pairs; | |
28 local type = type; | |
29 local select = select | |
30 local wipe = wipe | |
31 | |
32 -- Wow functions | |
33 local GetTime = GetTime | |
34 local InCombatLockdown = InCombatLockdown | |
35 local UnitName = UnitName | |
36 local IsMounted = IsMounted | |
37 local UnitCastingInfo = UnitCastingInfo | |
38 local GetUnitSpeed = GetUnitSpeed | |
39 local UnitBuff = UnitBuff | |
40 -- The hook has to be done before registering the local reference | |
41 local t_lastDegaine = 0 -- Time of the last draw | |
42 hooksecurefunc("ToggleSheath",function() | |
43 t_lastDegaine = GetTime() | |
44 end); | |
45 local ToggleSheath = ToggleSheath | |
46 | |
47 | |
48 --------------------------------------------------------------- | |
49 -- Local vars | |
50 --------------------------------------------------------------- | |
51 -- const | |
52 local DegaineFrame = CreateFrame("Frame") | |
53 local playername = UnitName("player") | |
54 local delay = 2.5 -- seconds | |
55 local delay_short = 0.5 -- seconds | |
56 | |
57 -- vars | |
58 local t_left = -1 | |
59 local isGossipClosed = true | |
60 local wasGossipOpened = false | |
61 local t_lastGossipClosed = 0 | |
62 --local wasMounted = IsMounted() -- handle mounts with UNIT_AURA | |
63 local isTradeSkill = false | |
64 | |
65 | |
66 --------------------------------------------------------------- | |
67 -- Some stuff... | |
68 --------------------------------------------------------------- | |
69 -- Print status | |
70 local printState = function() | |
71 DEFAULT_CHAT_FRAME:AddMessage(Degaine_isAuto and T.L["PRINT_ENABLED"] or T.L["PRINT_DISABLED"]) | |
72 end | |
73 | |
74 | |
75 --------------------------------------------------------------- | |
76 -- Launch drawing weapons | |
77 --------------------------------------------------------------- | |
78 | |
79 -- Wait a few seconds before drawing, to be able to see the curren animation (and avoid bugs) | |
80 local Degaine_OnUpdate = function(self,t_elapsed) | |
81 t_left = t_left - t_elapsed; | |
82 if t_left<=0 then | |
83 ToggleSheath() | |
84 DegaineFrame:SetScript("OnUpdate", nil) | |
85 end | |
86 end | |
87 local start = function(arg_delay) | |
88 | |
89 --@debug@ | |
90 --DEFAULT_CHAT_FRAME:AddMessage("Go !") | |
91 --@end-debug@ | |
92 | |
93 if Degaine_isAuto and (GetTime()-t_lastDegaine)>1 and not InCombatLockdown() then | |
94 t_left = arg_delay or delay | |
95 DegaineFrame:SetScript("OnUpdate", Degaine_OnUpdate) | |
96 end | |
97 end | |
98 | |
99 | |
100 --------------------------------------------------------------- | |
101 -- Events config | |
102 --------------------------------------------------------------- | |
103 -- Events to watch | |
104 local events = { -- no args to watch | |
105 GOSSIP_SHOW = function() | |
106 -- isGossipClosed : | |
107 -- if the gossip is already opened, no need to draw weapon | |
108 -- UnitName("npc")==UnitName("target") : | |
109 -- hack gossips again because there's no emote with non npc | |
110 -- you have to target the npc to interact with it | |
111 -- things such as boards can't be targeted and start no emote | |
112 local returnvalue = isGossipClosed and UnitName("npc")==UnitName("target") | |
113 isGossipClosed = false | |
114 return returnvalue | |
115 end, | |
116 MERCHANT_SHOW = function() | |
117 -- hack for the cases where the merchant's frame is opened from a gossip frame : | |
118 -- the gossip is closed and the merchant opened but the weapon is not stealthed | |
119 if (GetTime() - t_lastGossipClosed) < 0.5 then | |
120 return not wasGossipOpened | |
121 else | |
122 return isGossipClosed | |
123 end | |
124 end, | |
125 QUEST_PROGRESS = true, | |
126 BANKFRAME_OPENED = true, --function() return GossipFrame:IsShown() end, | |
127 AUCTION_HOUSE_SHOW = true, | |
128 } | |
129 local events1player = { -- the first arg is "player" | |
130 UNIT_SPELLCAST_FAILED = true, | |
131 UNIT_SPELLCAST_INTERRUPTED = true, | |
132 UNIT_SPELLCAST_SUCCEEDED = true, | |
133 UNIT_SPELLCAST_START = true, -- to ckeck tradeskill | |
134 } | |
135 | |
136 --[[ | |
137 -- Was used when moving prevented to play the emote when talking | |
138 -- Was needed for CHAT_MSG_SAY and CHAT_MSG_YELL | |
139 local function playerIsNotMoving() | |
140 return GetUnitSpeed("player") == 0 | |
141 end | |
142 --]] | |
143 local events2playername = { -- the 2nd arg is playername | |
144 CHAT_MSG_SAY = true, | |
145 CHAT_MSG_YELL = true, | |
146 CHAT_MSG_TEXT_EMOTE = true, -- the emote is not shown, but the weapon is stealthed | |
147 } | |
148 | |
149 | |
150 --------------------------------------------------------------- | |
151 -- Handle mounts with OnUpdate | |
152 --------------------------------------------------------------- | |
153 -- works with GupPet but not with the default interface... | |
154 -- I guess that's because a specific /cancelaura or alike is done | |
155 -- hooksecurefunc("Dismount",start); | |
156 | |
157 -- With OnUpdate | |
158 local handleMountsFrame = CreateFrame("frame") | |
159 local handleMountsOnUpdate | |
160 do | |
161 local IsMounted = IsMounted | |
162 local wasMounted = IsMounted() | |
163 local buffsIDs = {} | |
164 function handleMountsOnUpdate(self,t_elapsed) | |
165 if IsMounted() then | |
166 wasMounted = true | |
167 else | |
168 if wasMounted then | |
169 start(delay_short) -- no animation for unmounting | |
170 wasMounted = false | |
171 end | |
172 end | |
173 | |
174 -- hack to watch auras too | |
175 -- Loop over all buffs | |
176 -- name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, | |
177 -- shouldConsolidate, spellId = UnitBuff("unit", index or ["name", "rank"][, "filter"]) | |
178 local i = 1 | |
179 -- list player's buffs | |
180 local buffID = select(11,UnitBuff("player", i)); -- spell id | |
181 while buffID do | |
182 buffsIDs[buffID] = true | |
183 i = i + 1; | |
184 buffID = select(11,UnitBuff("player", i)); | |
185 end | |
186 --[[ | |
187 for i=1,40 do | |
188 buffID = select(11,UnitBuff("player", i)); | |
189 if buffID then | |
190 buffsIDs[buffID] = true | |
191 end | |
192 end | |
193 --]] | |
194 --@debug@ | |
195 --if debug then | |
196 -- DEFAULT_CHAT_FRAME:AddMessage(""..i-1.."Buff(s) found") | |
197 --end | |
198 --@end-debug@ | |
199 | |
200 -- loop over watched buffs to check if it is present or not | |
201 -- we have to loop over T.LoseAura (and not buffsIDs) to be able to check for buff disappearance | |
202 for id,state in pairs(T.LoseAura) do | |
203 if buffsIDs[id] then | |
204 if not T.LoseAura[id] then | |
205 -- buff gained | |
206 --@debug@ | |
207 if debug then | |
208 DEFAULT_CHAT_FRAME:AddMessage("Buff gained: "..id) | |
209 end | |
210 --@end-debug@ | |
211 T.LoseAura[id] = true | |
212 end | |
213 else | |
214 if T.LoseAura[id] then | |
215 -- buff lost | |
216 --@debug@ | |
217 if debug then | |
218 DEFAULT_CHAT_FRAME:AddMessage("Buff lost: "..id) | |
219 end | |
220 --@end-debug@ | |
221 T.LoseAura[id] = false | |
222 | |
223 -- draw weapon | |
224 start() | |
225 end | |
226 end | |
227 end | |
228 | |
229 -- empty temporary table | |
230 wipe(buffsIDs) | |
231 end | |
232 end | |
233 | |
234 | |
235 --------------------------------------------------------------- | |
236 -- Activation functions | |
237 --------------------------------------------------------------- | |
238 local activate = function() | |
239 if (not Degaine_isAuto) or InCombatLockdown() then return end | |
240 | |
241 --DegaineFrame:SetScript("OnUpdate", Degaine_OnUpdate); | |
242 --@debug@ | |
243 if debugAll then | |
244 DegaineFrame:RegisterAllEvents() | |
245 else | |
246 --@end-debug@ | |
247 for k,_ in pairs(events) do DegaineFrame:RegisterEvent(k) end | |
248 for k,_ in pairs(events1player) do DegaineFrame:RegisterEvent(k) end | |
249 for k,_ in pairs(events2playername) do DegaineFrame:RegisterEvent(k) end | |
250 --DegaineFrame:RegisterEvent("UNIT_AURA") | |
251 handleMountsFrame:SetScript("OnUpdate",handleMountsOnUpdate) | |
252 --@debug@ | |
253 end | |
254 --@end-debug@ | |
255 end | |
256 local desactivate = function() | |
257 DegaineFrame:SetScript("OnUpdate", nil) | |
258 t_left = -1 | |
259 isTradeSkill = false | |
260 for k,_ in pairs(events) do DegaineFrame:UnregisterEvent(k) end | |
261 for k,_ in pairs(events1player) do DegaineFrame:UnregisterEvent(k) end | |
262 for k,_ in pairs(events2playername) do DegaineFrame:UnregisterEvent(k) end | |
263 --DegaineFrame:UnregisterEvent("UNIT_AURA") | |
264 handleMountsFrame:SetScript("OnUpdate",nil) | |
265 end | |
266 Degaine_ToggleAuto = function() | |
267 if Degaine_isAuto then | |
268 Degaine_isAuto = false | |
269 desactivate() | |
270 else | |
271 Degaine_isAuto = true | |
272 activate() | |
273 end | |
274 printState() | |
275 end | |
276 | |
277 | |
278 | |
279 --------------------------------------------------------------- | |
280 -- Event/hooks functions | |
281 --------------------------------------------------------------- | |
282 local Degaine_OnEvent = function(self,event, arg1, arg2, _, _, arg5, ...) | |
283 --@debug@ | |
284 if debug then | |
285 DEFAULT_CHAT_FRAME:AddMessage(event) | |
286 if arg1 then DEFAULT_CHAT_FRAME:AddMessage("arg1 = "..arg1) end | |
287 if arg2 then DEFAULT_CHAT_FRAME:AddMessage("arg2 = "..arg2) end | |
288 end | |
289 --@end-debug@ | |
290 | |
291 --[[ IsMounted() renvoie toujours false ici, on doit passer par OnUpdate | |
292 if event=="UNIT_AURA" then | |
293 if arg1=="player" then | |
294 DEFAULT_CHAT_FRAME:AddMessage("player") | |
295 if IsMounted() then | |
296 DEFAULT_CHAT_FRAME:AddMessage("ismounted") | |
297 wasMounted = true | |
298 else | |
299 DEFAULT_CHAT_FRAME:AddMessage("notmounted") | |
300 if wasMounted then | |
301 DEFAULT_CHAT_FRAME:AddMessage("wasmounted : start") | |
302 wasMounted = false | |
303 start() | |
304 end | |
305 end | |
306 end | |
307 else | |
308 --]] | |
309 if events[event] then | |
310 if type(events[event])~="function" or events[event]() then | |
311 start() | |
312 end | |
313 elseif events1player[event] then | |
314 if arg1=="player" then | |
315 if event == "UNIT_SPELLCAST_START" then | |
316 --name, nameSubtext, text, texture, startTime, endTime, isTradeSkill, castID, notInterruptible = UnitCastingInfo("unit") | |
317 isTradeSkill = select(7,UnitCastingInfo("player")) | |
318 --DEFAULT_CHAT_FRAME:AddMessage(name) | |
319 --DEFAULT_CHAT_FRAME:AddMessage(isTradeSkill and "True" or "False") | |
320 --if isTradeSkill then | |
321 | |
322 --end | |
323 --elseif arg5 and not T.SpellBlackList[arg5] then -- arg5 is SpellID | |
324 elseif (arg5 and T.SpellWhiteList[arg5]) or isTradeSkill then -- arg5 is SpellID | |
325 isTradeSkill = false -- for next time | |
326 start(type(T.SpellWhiteList[arg5])=="number" and T.SpellWhiteList[arg5] or delay_short) | |
327 end | |
328 end | |
329 elseif events2playername[event] then | |
330 if arg2==playername and (type(events2playername[event])~="function" or events2playername[event]()) then | |
331 start() | |
332 end | |
333 elseif event == "GOSSIP_CLOSED" then | |
334 wasGossipOpened = not isGossipClosed | |
335 isGossipClosed = true | |
336 t_lastGossipClosed = GetTime() | |
337 | |
338 elseif event == "PLAYER_REGEN_ENABLED" then | |
339 for k,_ in pairs(events1player) do DegaineFrame:UnregisterEvent(k) end | |
340 --desactivate() | |
341 elseif event == "PLAYER_REGEN_DISABLED" then | |
342 for k,_ in pairs(events1player) do DegaineFrame:RegisterEvent(k) end | |
343 --activate() | |
344 | |
345 elseif event == "ADDON_LOADED" and arg1==AddonName then | |
346 if Degaine_isAuto==nil then | |
347 Degaine_isAuto = true | |
348 end | |
349 if Degaine_isAuto then activate() else desactivate() end | |
350 printState() | |
351 | |
352 -- Not needed anymore | |
353 DegaineFrame:UnregisterEvent("ADDON_LOADED") | |
354 | |
355 elseif event == "PLAYER_LOGIN" then | |
356 -- BLacklis alomst all Companions and mounts | |
357 T.DoBlackListCrittersAndMount() | |
358 end | |
359 end | |
360 | |
361 -- Hook when standing up | |
362 -- (Didn't work, see http://forums.wowace.com/showthread.php?p=310547#post310547) | |
363 hooksecurefunc("SitStandOrDescendStart",start); | |
364 | |
365 | |
366 --------------------------------------------------------------- | |
367 -- Commands | |
368 --------------------------------------------------------------- | |
369 -- Bindings | |
370 BINDING_HEADER_DEGAINE = T.L["BINDING_HEADER"] | |
371 BINDING_NAME_DEGAINE_TOGGLE = T.L["BINDING_NAME_DEGAINE_TOGGLE"] | |
372 | |
373 -- Slash command | |
374 -- GLOBALS: SLASH_DEGAINE1 | |
375 SLASH_DEGAINE1 = "/degaine" | |
376 SlashCmdList["DEGAINE"] = Degaine_ToggleAuto | |
377 | |
378 | |
379 --------------------------------------------------------------- | |
380 -- Initialization | |
381 --------------------------------------------------------------- | |
382 DegaineFrame:SetScript("OnEvent",Degaine_OnEvent) | |
383 | |
384 -- Global events | |
385 DegaineFrame:RegisterEvent("GOSSIP_CLOSED") | |
386 DegaineFrame:RegisterEvent("PLAYER_REGEN_ENABLED") | |
387 DegaineFrame:RegisterEvent("PLAYER_REGEN_DISABLED") | |
388 DegaineFrame:RegisterEvent("ADDON_LOADED") | |
389 DegaineFrame:RegisterEvent("PLAYER_LOGIN") | |
390 activate() |