All pastes #181124 Raw Edit

Someone

public text v1 · immutable
#181124 ·published 2006-09-24 15:18 UTC
rendered paste body
Index: Banzai-1.0.lua
===================================================================
--- Banzai-1.0.lua	(revision 11830)
+++ Banzai-1.0.lua	(working copy)
@@ -22,10 +22,49 @@
 local lib = {}
 AceLibrary("AceEvent-2.0"):embed(lib)
 
+local RL = nil
 local roster = nil
 local playerName = nil
 
 -------------------------------------------------------------------------------
+-- Utility
+-------------------------------------------------------------------------------
+
+local function acquire(a1,a2,a3,a4,a5)
+	local t = next(heap)
+	if t then
+		heap[t] = nil
+		assert(not next(t), "A table in the compost heap has been modified!")
+	end
+	t = t or {}
+
+	if a1 ~= nil then table.insert(t, a1) end
+	if a2 ~= nil then table.insert(t, a2) end
+	if a3 ~= nil then table.insert(t, a3) end
+	if a4 ~= nil then table.insert(t, a4) end
+	if a5 ~= nil then table.insert(t, a5) end
+	return t
+end
+
+
+local function reclaim(t, d)
+	if type(t) ~= "table" then return end
+
+	if d and d > 0 then
+		for i in pairs(t) do
+			if type(t[i]) == "table" then reclaim(t[i], d - 1) end
+		end
+	end
+
+	for i in pairs(t) do t[i] = nil end
+	t.reset = 1
+	t.reset = nil
+	table.setn(t, 0)
+
+	heap[t] = true
+end
+
+-------------------------------------------------------------------------------
 -- Initialization
 -------------------------------------------------------------------------------
 
@@ -38,9 +77,10 @@
 		end
 	end
 
-	roster = AceLibrary("RosterLib-2.0").roster
+	RL = AceLibrary("RosterLib-2.0")
+	roster = RL.roster
 	playerName = UnitName("player")
-	self:ScheduleRepeatingEvent("UpdateAggroList", self.UpdateAggroList, 0.5, self)
+	self:ScheduleRepeatingEvent("UpdateAggroList", self.UpdateAggroList, 0.2, self)
 
 	if oldDeactivate then oldDeactivate(oldLib) end
 end
@@ -50,8 +90,10 @@
 -------------------------------------------------------------------------------
 
 function lib:UpdateAggroList()
+	local oldBanzai = acquire()
+
 	for i, unit in pairs(roster) do
-		local oldbanzai = unit.banzai
+		oldBanzai[unit.unitid] = unit.banzai
 
 		-- deduct aggro for all, increase it later for everyone with aggro
 		if not unit.banzaiModifier then unit.banzaiModifier = 0 end
@@ -59,9 +101,13 @@
 
 		-- check for aggro
 		local targetName = UnitName(unit.unitid .. "targettarget")
+		-- is this guy targetting something we can kill?
 		if roster[targetName] and UnitCanAttack("player", unit.unitid .. "target") then
 			if not roster[targetName].banzaiModifier then roster[targetName].banzaiModifier = 0 end
 			roster[targetName].banzaiModifier = roster[targetName].banzaiModifier + 10
+			roster[targetName].banzaiTarget = unit.unitid .. "target"
+		else
+			roster[targetName].banzaiTarget = nil
 		end
 
 		-- cleanup
@@ -70,14 +116,46 @@
 
 		-- set aggro
 		unit.banzai = (unit.banzaiModifier > 15)
+	end
 
-		if oldbanzai ~= unit.banzai and targetName == playerName then
-			self:TriggerEvent("Banzai_PlayerAggro", unit.unitid .. "target")
+	for i, unit in pairs(roster) do
+		if oldBanzai[unit.unitId] ~= unit.banzai then
+			-- Aggro status has changed.
+			if unit.banzai then
+				-- Unit has aggro
+				self:TriggerEvent("Banzai_UnitGainedAggro", unit.banzaiTarget)
+				if unit.name == playerName then
+					self:TriggerEvent("Banzai_PlayerGainedAggro", unit.banzaiTarget)
+				end
+			else
+				-- Unit lost aggro
+				self:TriggerEvent("Banzai_UnitLostAggro", unit.unitid)
+				if unit.name == playerName then
+					self:TriggerEvent("Banzai_PlayerLostAggro", unit.unitid)
+				end
+			end
 		end
 	end
+
 end
 
 -------------------------------------------------------------------------------
+-- API
+-------------------------------------------------------------------------------
+
+function lib:GetUnitAggroByUnitId( unitId )
+	local rosterUnit = RL:GetUnitObjectFromUnit(unitId)
+	if not rosterUnit then return nil end
+	return rosterUnit.banzai
+end
+
+function lib:GetUnitAggroByUnitName( unitName )
+	local rosterUnit = RL:GetUnitObjectFromName(unitName)
+	if not rosterUnit then return nil end
+	return rosterUnit.banzai
+end
+
+-------------------------------------------------------------------------------
 -- Register
 -------------------------------------------------------------------------------
 AceLibrary:Register(lib, MAJOR_VERSION, MINOR_VERSION, activate)