Quantcast

Factor out state method to compute the cost of a spell.

Johnny C. Lam [04-06-14 - 18:47]
Factor out state method to compute the cost of a spell.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1285 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
OvalePower.lua
diff --git a/OvalePower.lua b/OvalePower.lua
index bd7d287..c4202c8 100644
--- a/OvalePower.lua
+++ b/OvalePower.lua
@@ -282,9 +282,9 @@ function OvalePower:ApplySpellAfterCast(state, spellId, targetGUID, startCast, e

 	-- Update power using information from GetSpellInfo() if there is no SpellInfo() for the spell's cost.
 	do
-		local _, _, _, cost, _, powerType = API_GetSpellInfo(spellId)
-		if cost and powerType then
-			powerType = self.POWER_TYPE[powerType]
+		local _, _, _, cost, _, powerTypeId = API_GetSpellInfo(spellId)
+		if cost and powerTypeId then
+			powerType = self.POWER_TYPE[powerTypeId]
 			if not si or not si[powerType] then
 				state[powerType] = state[powerType] - cost
 			end
@@ -295,7 +295,7 @@ function OvalePower:ApplySpellAfterCast(state, spellId, targetGUID, startCast, e
 		-- Update power state except for eclipse energy (handled by OvaleEclipse).
 		for powerType, powerInfo in pairs(self.POWER_INFO) do
 			if powerType ~= "eclipse" then
-				local cost = si[powerType]
+				local cost = state:PowerCost(spellId, powerType)
 				local power = state[powerType] or 0
 				if cost then
 					--[[
@@ -308,21 +308,6 @@ function OvalePower:ApplySpellAfterCast(state, spellId, targetGUID, startCast, e
 					else
 						power = power - cost
 					end
-					--[[
-						Add extra resource generated by presence of a buff.
-						"buff_<powerType>" is the spell ID of the buff that causes extra resources to be generated or used.
-						"buff_<powerType>_amount" is the amount of extra resources generated or used, defaulting to -1
-							(one extra resource generated).
-					--]]
-					local buffParam = "buff_" .. tostring(powerType)
-					local buffAmoumtParam = buffParam .. "_amount"
-					if si[buffParam] then
-						local aura = state:GetAura("player", si[buffParam], nil, true)
-						if state:IsActiveAura(aura) then
-							local buffAmount = si[buffAmountParam] or -1
-							power = power - buffAmount
-						end
-					end
 					-- Clamp power to lower and upper limits.
 					local mini = powerInfo.mini or 0
 					local maxi = powerInfo.maxi or self.maxPower[powerType]
@@ -341,6 +326,41 @@ end
 --</public-static-methods>

 --<state-methods>
+statePrototype.PowerCost = function(state, spellId, powerType)
+	local spellCost = nil
+	local si = OvaleData.spellInfo[spellId]
+	if si and si[powerType] then
+		local cost = si[powerType]
+		--[[
+			cost > 0 means that the spell costs resources.
+			cost < 0 means that the spell generates resources.
+			cost == 0 means that the spell uses all of the resources (zeroes it out).
+
+			Add extra resource generated by presence of a buff.
+			"buff_<powerType>" is the spell ID of the buff that causes extra resources to be generated or used.
+			"buff_<powerType>_amount" is the amount of extra resources generated or used, defaulting to -1
+				(one extra resource generated).
+		--]]
+		local buffParam = "buff_" .. tostring(powerType)
+		local buffAmoumtParam = buffParam .. "_amount"
+		if si[buffParam] then
+			local aura = state:GetAura("player", si[buffParam], nil, true)
+			if state:IsActiveAura(aura) then
+				local buffAmount = si[buffAmountParam] or -1
+				cost = cost + buffAmount
+			end
+		end
+		spellCost = cost
+	else
+		-- Determine cost using information from GetSpellInfo() if there is no SpellInfo() for the spell's cost.
+		local _, _, _, cost, _, powerTypeId = API_GetSpellInfo(spellId)
+		if cost and powerTypeId and powerType == OvalePower.POWER_TYPE[powerTypeId] then
+			spellCost = cost
+		end
+	end
+	return spellCost
+end
+
 -- Print out the levels of each power type in the current state.
 statePrototype.DebugPower = function(state)
 	for powerType in pairs(OvalePower.POWER_INFO) do