vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Name: Banzai-1.0
3 Revision: $Rev: 14544 $
4 Author(s): Rabbit (rabbit.magtheridon@gmail.com), maia
5 Documentation: http://www.wowace.com/index.php/Banzai-1.0_API_Documentation
6 SVN: http://svn.wowace.com/root/trunk/BanzaiLib/Banzai-1.0
7 Description: Aggro notification library.
8 Dependencies: AceLibrary, AceEvent-2.0, RosterLib-2.0
9 ]]
10  
11 -------------------------------------------------------------------------------
12 -- Locals
13 -------------------------------------------------------------------------------
14  
15 local MAJOR_VERSION = "Banzai-1.0"
16 local MINOR_VERSION = "$Revision: 14544 $"
17  
18 if not AceLibrary then error(MAJOR_VERSION .. " requires AceLibrary") end
19 if not AceLibrary:HasInstance("RosterLib-2.0") then error(MAJOR_VERSION .. " requires RosterLib-2.0.") end
20 if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then return end
21  
22 local lib = {}
23 AceLibrary("AceEvent-2.0"):embed(lib)
24  
25 local RL = nil
26 local roster = nil
27 local playerName = nil
28  
29 -------------------------------------------------------------------------------
30 -- Compost Heap, courtesy of Tekkub/SEEA.
31 -------------------------------------------------------------------------------
32  
33 local table_setn
34 do
35 local version = GetBuildInfo()
36 if string.find(version, "^2%.") then
37 -- 2.0.0
38 table_setn = function() end
39 else
40 table_setn = table.setn
41 end
42 end
43  
44 local heap = {}
45 setmetatable(heap, {__mode = "kv"})
46  
47 local function acquire()
48 local t = next(heap)
49 if t then
50 heap[t] = nil
51 assert(not next(t), "A table in the compost heap has been modified!")
52 end
53 t = t or {}
54 return t
55 end
56  
57  
58 local function reclaim(t, d)
59 if type(t) ~= "table" then return end
60 if d and d > 0 then
61 for i in pairs(t) do
62 if type(t[i]) == "table" then reclaim(t[i], d - 1) end
63 end
64 end
65 for i in pairs(t) do t[i] = nil end
66 table_setn(t, 0)
67 heap[t] = true
68 end
69  
70 -------------------------------------------------------------------------------
71 -- Initialization
72 -------------------------------------------------------------------------------
73  
74 -- Activate a new instance of this library
75 function activate(self, oldLib, oldDeactivate)
76 if oldLib then
77 self.vars = oldLib.vars
78 if oldLib:IsEventScheduled("UpdateAggroList") then
79 oldLib:CancelScheduledEvent("UpdateAggroList")
80 end
81 end
82  
83 RL = AceLibrary("RosterLib-2.0")
84 roster = RL.roster
85 playerName = UnitName("player")
86 self:ScheduleRepeatingEvent("UpdateAggroList", self.UpdateAggroList, 0.2, self)
87  
88 if oldDeactivate then oldDeactivate(oldLib) end
89 end
90  
91 -------------------------------------------------------------------------------
92 -- Library
93 -------------------------------------------------------------------------------
94  
95 function lib:UpdateAggroList()
96 local oldBanzai = acquire()
97  
98 for i, unit in pairs(roster) do
99 oldBanzai[unit.unitid] = unit.banzai
100  
101 -- deduct aggro for all, increase it later for everyone with aggro
102 if not unit.banzaiModifier then unit.banzaiModifier = 0 end
103 unit.banzaiModifier = math.max(0, unit.banzaiModifier - 5)
104  
105 -- check for aggro
106 local targetId = unit.unitid .. "target"
107 local targetName = UnitName(targetId .. "target")
108 if roster[targetName] and UnitCanAttack("player", targetId) and UnitCanAttack(targetId, "player") then
109 if not roster[targetName].banzaiModifier then roster[targetName].banzaiModifier = 0 end
110 roster[targetName].banzaiModifier = roster[targetName].banzaiModifier + 10
111 if not roster[targetName].banzaiTarget then roster[targetName].banzaiTarget = acquire() end
112 table.insert(roster[targetName].banzaiTarget, targetId)
113 end
114  
115 -- cleanup
116 unit.banzaiModifier = math.max(0, unit.banzaiModifier)
117 unit.banzaiModifier = math.min(25, unit.banzaiModifier)
118  
119 -- set aggro
120 unit.banzai = (unit.banzaiModifier > 15)
121 end
122  
123 for i, unit in pairs(roster) do
124 if oldBanzai[unit.unitid] ~= nil and oldBanzai[unit.unitid] ~= unit.banzai then
125 -- Aggro status has changed.
126 if unit.banzai == true and unit.banzaiTarget then
127 -- Unit has aggro
128 self:TriggerEvent("Banzai_UnitGainedAggro", unit.unitid, unit.banzaiTarget)
129 if unit.name == playerName then
130 self:TriggerEvent("Banzai_PlayerGainedAggro", unit.banzaiTarget)
131 end
132 elseif unit.banzai == false then
133 -- Unit lost aggro
134 self:TriggerEvent("Banzai_UnitLostAggro", unit.unitid)
135 if unit.name == playerName then
136 self:TriggerEvent("Banzai_PlayerLostAggro", unit.unitid)
137 end
138 end
139 end
140 reclaim(unit.banzaiTarget)
141 unit.banzaiTarget = nil
142 end
143  
144 reclaim(oldBanzai)
145 oldBanzai = nil
146 end
147  
148 -------------------------------------------------------------------------------
149 -- API
150 -------------------------------------------------------------------------------
151  
152 function lib:GetUnitAggroByUnitId( unitId )
153 local rosterUnit = RL:GetUnitObjectFromUnit(unitId)
154 if not rosterUnit then return nil end
155 return rosterUnit.banzai
156 end
157  
158 function lib:GetUnitAggroByUnitName( unitName )
159 local rosterUnit = RL:GetUnitObjectFromName(unitName)
160 if not rosterUnit then return nil end
161 return rosterUnit.banzai
162 end
163  
164 -------------------------------------------------------------------------------
165 -- Register
166 -------------------------------------------------------------------------------
167 AceLibrary:Register(lib, MAJOR_VERSION, MINOR_VERSION, activate)
168