Quantcast

Allow SpellInfo to specify a function to compute Damage().

Johnny C. Lam [07-05-13 - 07:02]
Allow SpellInfo to specify a function to compute Damage().

A SpellInfo() declaration can include a "damage=FunctionName" parameter,
where FunctionName is an Ovale script function defined using AddFunction
that is used directly by the Damage() script condition to compute the
estimated damage of a spell.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@935 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
OvaleCompile.lua
OvaleCondition.lua
diff --git a/OvaleCompile.lua b/OvaleCompile.lua
index 8d93078..8608858 100644
--- a/OvaleCompile.lua
+++ b/OvaleCompile.lua
@@ -852,6 +852,18 @@ function OvaleCompile:Compile()
 	Ovale:UpdateFrame()
 end

+function OvaleCompile:GetFunctionNode(name)
+	name = strlower(name)
+	local nodeName = self_customFunctions[name]
+	if nodeName then
+		local nodeId = strmatch(nodeName, "node(%d+)")
+		if nodeId then
+			return self_node[tonumber(nodeId)]
+		end
+	end
+	return nil
+end
+
 function OvaleCompile:Debug()
 	self_pool:Debug()
 	Ovale:Print(self:DebugNode(self.masterNodes[1]))
diff --git a/OvaleCondition.lua b/OvaleCondition.lua
index 787fbad..2f96be5 100644
--- a/OvaleCondition.lua
+++ b/OvaleCondition.lua
@@ -15,6 +15,8 @@ Ovale.OvaleCondition = OvaleCondition
 local LBCT = LibStub("LibBabble-CreatureType-3.0"):GetLookupTable()
 local LRC = LibStub("LibRangeCheck-2.0", true)
 local OvaleAura = Ovale.OvaleAura
+local OvaleBestAction = nil	-- forward declaration
+local OvaleCompile = nil	-- forward declaration
 local OvaleDamageTaken = Ovale.OvaleDamageTaken
 local OvaleData = Ovale.OvaleData
 local OvaleEnemies = Ovale.OvaleEnemies
@@ -425,6 +427,27 @@ local function TimeToDie(unitId)
 	end
 	return timeToDie, health, maxHealth
 end
+
+local function ComputeFunctionParam(spellId, paramName)
+	local si = OvaleData.spellInfo[spellId]
+	if si and si[paramName] then
+		-- Resolve forward declarations.
+		OvaleBestAction = OvaleBestAction or Ovale.OvaleBestAction
+		OvaleCompile = OvaleCompile or Ovale.OvaleCompile
+		if OvaleBestAction and OvaleCompile then
+			local element = OvaleCompile:GetFunctionNode(si[paramName])
+			if element then
+				local element = select(4, OvaleBestAction:Compute(element))
+				local element = element.result
+				if element and element.type == "value" then
+					return element.value, element.origin, element.rate
+				end
+			end
+			return 0, 0, 0
+		end
+	end
+	return nil
+end
 --</private-static-methods>

 --<public-static-properties>
@@ -1063,9 +1086,10 @@ end
 -- @see Damage, LastSpellDamage, LastSpellEstimatedDamage

 OvaleCondition.conditions.critdamage = function(condition)
-	local spellId = condition[1]
-	local ret = OvaleData:GetDamage(spellId, OvalePaperDoll.attackPower, OvalePaperDoll.spellBonusDamage, OvaleState.state.combo)
-	return 0, nil, 2 * ret * OvaleState:GetDamageMultiplier(spellId), 0, 0
+	-- TODO: Need to account for increased crit effect from meta-gems.
+	local critFactor = 2
+	local start, ending, value, origin, rate = OvaleCondition.conditions.damage(condition)
+	return start, ending, critFactor * value, critFactor * origin, critFactor * rate
 end

 --- Get the current estimated damage of a spell.
@@ -1084,8 +1108,13 @@ end

 OvaleCondition.conditions.damage = function(condition)
 	local spellId = condition[1]
-	local ret = OvaleData:GetDamage(spellId, OvalePaperDoll.attackPower, OvalePaperDoll.spellBonusDamage, OvaleState.state.combo)
-	return 0, nil, ret * OvaleState:GetDamageMultiplier(spellId), 0, 0
+	local value, origin, rate = ComputeFunctionParam(spellId, "damage")
+	if value then
+		return 0, nil, value, origin, rate
+	else
+		value = OvaleData:GetDamage(spellId, OvalePaperDoll.attackPower, OvalePaperDoll.spellBonusDamage, OvaleState.state.combo)
+		return 0, nil, value * OvaleState:GetDamageMultiplier(spellId), 0, 0
+	end
 end

 --- Get the current damage multiplier of a spell.