Quantcast

Fix SpellCooldown(spell) if spell is a shared cooldown name string.

Johnny C. Lam [05-22-14 - 12:37]
Fix SpellCooldown(spell) if spell is a shared cooldown name string.

Make a list of spells associated with each sharedcd name during script
compilation and loop through those spells to get the cooldown information.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1487 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
OvaleCompile.lua
OvaleCooldown.lua
diff --git a/OvaleCompile.lua b/OvaleCompile.lua
index 7ff066c..4132976 100644
--- a/OvaleCompile.lua
+++ b/OvaleCompile.lua
@@ -19,6 +19,7 @@ local OvaleTimeSpan = Ovale.OvaleTimeSpan

 -- Forward declarations for module dependencies.
 local OvaleCondition = nil
+local OvaleCooldown = nil
 local OvaleData = nil
 local OvaleEquipement = nil
 local OvaleOptions = nil
@@ -416,6 +417,7 @@ local function ParseSpellInfo(params)
 				end
 				OvaleData.buffSpellList[v][spellId] = true
 			elseif k == "sharedcd" then
+				OvaleCooldown:AddSharedCooldown(v, spellId)
 				self_sharedCooldownNames[v] = true
 			else
 				si[k] = v
@@ -849,6 +851,7 @@ local function CompileScript(text)
 	wipe(self_missingSpellList)
 	wipe(self_functionCalls)
 	wipe(self.customFunctionNode)
+	OvaleCooldown:ResetSharedCooldowns()

 	-- Return all existing nodes to the node pool.
 	for i, node in pairs(self_node) do
@@ -917,6 +920,7 @@ end
 function OvaleCompile:OnInitialize()
 	-- Resolve module dependencies.
 	OvaleCondition = Ovale.OvaleCondition
+	OvaleCooldown = Ovale.OvaleCooldown
 	OvaleData = Ovale.OvaleData
 	OvaleEquipement = Ovale.OvaleEquipement
 	OvaleOptions = Ovale.OvaleOptions
diff --git a/OvaleCooldown.lua b/OvaleCooldown.lua
index 18dc6d3..d0ab5d3 100644
--- a/OvaleCooldown.lua
+++ b/OvaleCooldown.lua
@@ -30,6 +30,8 @@ local API_UnitClass = UnitClass
 local _, self_class = API_UnitClass("player")
 -- Current age of cooldown state.
 local self_serial = 0
+-- Shared cooldown name (sharedcd) to spell table mapping.
+local self_sharedCooldownSpells = {}
 --</private-static-properties>

 --<public-static-methods>
@@ -69,6 +71,36 @@ function OvaleCooldown:Update()
 	self_serial = self_serial + 1
 end

+-- Empty out the sharedcd table.
+function OvaleCooldown:ResetSharedCooldowns()
+	for name, spellTable in pairs(self_sharedCooldownSpells) do
+		for spellId in pairs(spellTable) do
+			spellTable[spellId] = nil
+		end
+	end
+end
+
+function OvaleCooldown:AddSharedCooldown(name, spellId)
+	self_sharedCooldownSpells[name] = self_sharedCooldownSpells[name] or {}
+	self_sharedCooldownSpells[name][spellId] = true
+end
+
+-- Get the cooldown information for the given spell ID.  If given a shared cooldown name,
+-- then cycle through all spells associated with that spell ID to find the cooldown
+-- information.
+function OvaleCooldown:GetSpellCooldown(spellId)
+	local start, duration, enable
+	if self_sharedCooldownSpells[spellId] then
+		for id in pairs(self_sharedCooldownSpells[spellId]) do
+			start, duration, enable = API_GetSpellCooldown(id)
+			if start then break end
+		end
+	else
+		start, duration, enable = API_GetSpellCooldown(spellId)
+	end
+	return start, duration, enable
+end
+
 -- Return the GCD after the given spellId is cast.
 -- If no spellId is given, then returns the GCD after a "yellow-hit" ability has been cast.
 function OvaleCooldown:GetGCD(spellId)
@@ -256,13 +288,13 @@ statePrototype.GetCD = function(state, spellId)
 	-- Populate the cooldown information from the current game state if it is outdated.
 	local cd = state.cd[cdName]
 	if not cd.start or not cd.serial or cd.serial < self_serial then
-		local start, duration, enable = API_GetSpellCooldown(spellId)
+		local start, duration, enable = OvaleCooldown:GetSpellCooldown(spellId)
 		if start and start > 0 then
 			charges = 0
 		end
 		if si and si.forcecd then
 			if si.forcecd then
-				start, duration = API_GetSpellCooldown(si.forcecd)
+				start, duration = OvaleCooldown:GetSpellCooldown(si.forcecd)
 			end
 		end
 		cd.serial = self_serial