Quantcast

Adapt GetAura() to store results in a table passed to the method.

Johnny C. Lam [07-07-13 - 04:43]
Adapt GetAura() to store results in a table passed to the method.

This affects the GetAura method in OvaleAura, OvaleState and
OvaleCondition.

This change allows for efficient lookup of any of the properties of the
aura found by GetAura if needed, while preserving the base information in
the return values for GetAura (start, ending, count, gain).

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@942 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
OvaleAura.lua
OvaleCondition.lua
OvaleState.lua
diff --git a/OvaleAura.lua b/OvaleAura.lua
index 66e7fd0..0b5f421 100644
--- a/OvaleAura.lua
+++ b/OvaleAura.lua
@@ -26,6 +26,7 @@ local select = select
 local strfind = string.find
 local tinsert = table.insert
 local tsort = table.sort
+local wipe = table.wipe
 local API_UnitAura = UnitAura

 -- aura pool
@@ -40,6 +41,10 @@ local self_aura = {}
 -- self_serial[guid] = aura age
 local self_serial = {}

+-- Static properties used by "GetAura" method.
+local aura_GetAura = {}
+local newAura_GetAura = {}
+
 local OVALE_UNKNOWN_GUID = 0

 local OVALE_AURA_DEBUG = "aura"
@@ -371,7 +376,7 @@ function OvaleAura:Ovale_InactiveUnit(event, guid)
 	RemoveAurasForGUID(guid)
 end

-function OvaleAura:GetAuraByGUID(guid, spellId, filter, mine, unitId)
+function OvaleAura:GetAuraByGUID(guid, spellId, filter, mine, unitId, auraFound)
 	if not guid then
 		Ovale:Log("nil guid does not exist in OvaleAura")
 		return nil
@@ -444,28 +449,48 @@ function OvaleAura:GetAuraByGUID(guid, spellId, filter, mine, unitId)
 		end
 	end

-	if not aura then return nil end
-	return aura.start, aura.ending, aura.stacks, aura.tick, aura.value, aura.gain
+	if aura then
+		-- If auraFound is a table, then copy the found aura into auraFound.
+		if auraFound then
+			for k, v in pairs(aura) do
+				auraFound[k] = v
+			end
+		end
+		return aura.start, aura.ending, aura.stacks, aura.gain
+	else
+		return nil
+	end
 end

-function OvaleAura:GetAura(unitId, spellId, filter, mine)
+function OvaleAura:GetAura(unitId, spellId, filter, mine, auraFound)
 	local guid = OvaleGUID:GetGUID(unitId)
 	if OvaleData.buffSpellList[spellId] then
-		local newStart, newEnding, newStacks, newTick, newValue, newGain
+		if auraFound then wipe(newAura_GetAura) end
+		local newStart, newEnding, newStacks, newGain
 		for auraId in pairs(OvaleData.buffSpellList[spellId]) do
-			local start, ending, stacks, tick, value, gain = self:GetAuraByGUID(guid, auraId, filter, mine, unitId)
+			if auraFound then wipe(aura_GetAura) end
+			local start, ending, stacks, gain = self:GetAuraByGUID(guid, auraId, filter, mine, unitId, aura_GetAura)
 			if start and (not newStart or stacks > newStacks) then
 				newStart = start
 				newEnding = ending
 				newStacks = stacks
-				newTick = tick
-				newValue = value
 				newGain = gain
+				if auraFound then
+					wipe(newAura_GetAura)
+					for k, v in pairs(aura_GetAura) do
+						newAura_GetAura[k] = v
+					end
+				end
+			end
+		end
+		if auraFound then
+			for k, v in pairs(newAura_GetAura) do
+				auraFound[k] = v
 			end
 		end
 		return newStart, newEnding, newStacks, newTick, newValue, newGain
 	else
-		return self:GetAuraByGUID(guid, spellId, filter, mine, unitId)
+		return self:GetAuraByGUID(guid, spellId, filter, mine, unitId, auraFound)
 	end
 end

diff --git a/OvaleCondition.lua b/OvaleCondition.lua
index 0a23a02..5eec31e 100644
--- a/OvaleCondition.lua
+++ b/OvaleCondition.lua
@@ -79,6 +79,9 @@ local self_lastTTDHealth = {}
 local self_lastTTDguid = {}
 local self_lastTTDdps = {}

+-- static property for conditions that use GetAura()
+local self_auraFound = {}
+
 local OVALE_POWERTYPE_ENERGY = OvaleData.power.energy.id
 local OVALE_POWERTYPE_MANA = OvaleData.power.mana.id

@@ -319,8 +322,8 @@ local function GetRunesCooldown(condition)
 end

 -- Front-end for OvaleState:GetAura() using condition parameters.
--- return start, ending, stacks, tick, value, gain
-local function GetAura(condition)
+-- return start, ending, stacks, gain
+local function GetAura(condition, auraFound)
 	local unitId = GetTarget(condition)
 	local spellId = condition[1]
 	local filter = GetFilter(condition)
@@ -330,7 +333,7 @@ local function GetAura(condition)
 		Ovale:Log("GetAura: nil spellId")
 		return nil
 	end
-	local start, ending, stacks, tick, value, gain = OvaleState:GetAura(unitId, spellId, filter, mine)
+	local start, ending, stacks, gain = OvaleState:GetAura(unitId, spellId, filter, mine, auraFound)

 	if not start then
 		Ovale:Logf("GetAura: aura %s not found on %s filter=%s mine=%s", spellId, unitId, filter, mine)
@@ -342,7 +345,7 @@ local function GetAura(condition)
 		return nil
 	end
 	Ovale:Logf("GetAura: aura %s found on %s start=%s ending=%s stacks=%s/%d", spellId, unitId, start, ending, stacks, conditionStacks)
-	return start, ending, stacks, tick, value, gain
+	return start, ending, stacks, gain
 end

 -- Front-end for OvaleState:GetAuraOnAnyTarget() using condition parameters.
@@ -653,7 +656,7 @@ OvaleCondition.conditions.debuffremains = OvaleCondition.conditions.buffremains
 OvaleCondition.conditions.buffgain = function(condition)
 	Ovale:Error("not implemented")
 	if true then return nil end
-	local gain = select(6, GetAura(condition)) or 0
+	local gain = select(4, GetAura(condition)) or 0
 	return 0, nil, 0, 0, 1
 end
 OvaleCondition.conditions.debuffgain = OvaleCondition.conditions.buffgain
@@ -2147,7 +2150,9 @@ end
 -- @see Ticks, TicksRemain, TickTime

 OvaleCondition.conditions.nexttick = function(condition)
-	local start, ending, _, tick = GetAura(condition)
+	self_auraFound.tick = nil
+	local start, ending = GetAura(condition, self_auraFound)
+	local tick = self_auraFound.tick
 	if ending and tick then
 		while ending - tick > OvaleState.currentTime do
 			ending = ending - tick
@@ -2766,7 +2771,9 @@ end
 --     Spell(purifying_brew)

 OvaleCondition.conditions.tickvalue = function(condition)
-	local value = select(5, GetAura(condition)) or 0
+	self_auraFound.value = nil
+	local start, ending = GetAura(condition, self_auraFound)
+	local value = self_auraFound.value or 0
 	return Compare(value, condition[2], condition[3])
 end

@@ -2781,7 +2788,9 @@ end
 -- @see NextTick, TicksRemain, TickTime

 OvaleCondition.conditions.ticks = function(condition)
-	local start, ending, _, tick = GetAura(condition)
+	self_auraFound.tick = nil
+	local start, ending = GetAura(condition, self_auraFound)
+	local tick = self_auraFound.tick
 	local duration, numTicks
 	if start then
 		-- Aura exists on the target
@@ -2827,7 +2836,9 @@ end
 --     Spell(shadow_word_pain)

 OvaleCondition.conditions.ticksremain = function(condition)
-	local start, ending, _, tick = GetAura(condition)
+	self_auraFound.tick = nil
+	local start, ending = GetAura(condition, self_auraFound)
+	local tick = self_auraFound.tick
 	if ending and tick and tick > 0 then
 		return 0, nil, 1, ending, -1/tick
 	end
@@ -2851,7 +2862,9 @@ end
 -- @see NextTick, Ticks, TicksRemain

 OvaleCondition.conditions.ticktime = function(condition)
-	local start, ending, _, tick = GetAura(condition)
+	self_auraFound.tick = nil
+	local start, ending = GetAura(condition, self_auraFound)
+	local tick = self_auraFound.tick
 	if not tick then
 		tick = OvaleData:GetTickLength(condition[1])
 	end
diff --git a/OvaleState.lua b/OvaleState.lua
index 8e08af8..ac472e4 100644
--- a/OvaleState.lua
+++ b/OvaleState.lua
@@ -27,6 +27,7 @@ local floor = math.floor
 local pairs = pairs
 local select = select
 local tostring = tostring
+local wipe = table.wipe
 local API_GetEclipseDirection = GetEclipseDirection
 local API_GetRuneCooldown = GetRuneCooldown
 local API_GetRuneType = GetRuneType
@@ -41,6 +42,10 @@ local self_damageMultiplier = 1
 local self_runes = {}
 local self_runesCD = {}

+-- Static properties used by "GetAura" method.
+local aura_GetAura = {}
+local newAura_GetAura = {}
+
 -- Aura IDs for Eclipse buffs.
 local LUNAR_ECLIPSE = 48518
 local SOLAR_ECLIPSE = 48517
@@ -545,7 +550,7 @@ function OvaleState:GetComputedSpellCD(spellId)
 	return actionCooldownStart, actionCooldownDuration, actionEnable
 end

-function OvaleState:GetAuraByGUID(guid, spellId, filter, mine, unitId)
+function OvaleState:GetAuraByGUID(guid, spellId, filter, mine, unitId, auraFound)
 	local aura
 	if mine then
 		local auraTable = self.aura[guid]
@@ -574,31 +579,47 @@ function OvaleState:GetAuraByGUID(guid, spellId, filter, mine, unitId)
 		else
 			Ovale:Logf("Found %s aura %s on %s (removed)", filter, spellId, guid)
 		end
-		return aura.start, aura.ending, aura.stacks, aura.tick, aura.value, aura.gain
+		if auraFound then
+			for k, v in pairs(aura) do
+				auraFound[k] = v
+			end
+		end
+		return aura.start, aura.ending, aura.stacks, aura.gain
 	else
 		Ovale:Logf("Aura %s not found in state for %s", spellId, guid)
-		return OvaleAura:GetAuraByGUID(guid, spellId, filter, mine, unitId)
+		return OvaleAura:GetAuraByGUID(guid, spellId, filter, mine, unitId, auraFound)
 	end
 end

-function OvaleState:GetAura(unitId, spellId, filter, mine)
+function OvaleState:GetAura(unitId, spellId, filter, mine, auraFound)
 	local guid = OvaleGUID:GetGUID(unitId)
 	if OvaleData.buffSpellList[spellId] then
-		local newStart, newEnding, newStacks, newTick, newValue, newGain
+		if auraFound then wipe(newAura_GetAura) end
+		local newStart, newEnding, newStacks, newGain
 		for auraId in pairs(OvaleData.buffSpellList[spellId]) do
-			local start, ending, stacks, tick, value, gain = self:GetAuraByGUID(guid, auraId, filter, mine, unitId)
+			if auraFound then wipe(aura_GetAura) end
+			local start, ending, stacks, gain = self:GetAuraByGUID(guid, auraId, filter, mine, unitId, aura_GetAura)
 			if start and (not newStart or stacks > newStacks) then
 				newStart = start
 				newEnding = ending
 				newStacks = stacks
-				newTick = tick
-				newValue = value
 				newGain = gain
+				if auraFound then
+					wipe(newAura_GetAura)
+					for k, v in pairs(aura_GetAura) do
+						newAura_GetAura[k] = v
+					end
+				end
+			end
+		end
+		if auraFound then
+			for k, v in pairs(newAura_GetAura) do
+				auraFound[k] = v
 			end
 		end
-		return newStart, newEnding, newStacks, newTick, newValue, newGain
+		return newStart, newEnding, newStacks, newGain
 	else
-		return self:GetAuraByGUID(guid, spellId, filter, mine, unitId)
+		return self:GetAuraByGUID(guid, spellId, filter, mine, unitId, auraFound)
 	end
 end