View Single Post
10-18-15, 11:30 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, assuming you only care about your own spells, then rather than hand-maintaining a list of spells that are on the GCD, you can just scan the spellbook for spells with a cooldown, and assume everything else is on the GCD:

lua Code:
  1. -- Top of your file:
  2. local gcdSpells = {}
  3. local gcdMasterSpell

lua Code:
  1. -- In response to SPELLS_CHANGED event:
  2. wipe(gcdSpells)
  3. local _, _, offset, numSpells = GetSpellTabInfo(2)
  4. for i = 1, offset + numSpells do
  5.      local slotType, slotID = GetSpellBookItemInfo(i, "spell")
  6.      if slotType == "SPELL" then
  7.           local slotName = GetSpellBookItemName(i, "spell")
  8.           local spellName, _, _, _, _, _, spellID = GetSpellInfo(slotName)
  9.           local spellCD = GetSpellBaseCooldown(spellID or 0) -- spellID can be nil during loading
  10.           if spellCD == 0 then
  11.                gcdSpells[spellID] = true
  12.           end
  13.      end
  14. end
  15. gcdMasterSpell = next(gcdSpells)

lua Code:
  1. -- To get the real GCD at any given time:
  2. local start, duration = GetSpellCooldown(gcdMasterSpell)

Note that the ID and name from the GetSpellBookItem* functions may not be the real ID and/or name of the spell, in cases where the spell changes based on spec, talents, or other conditions. Doing a reverse lookup on the provided name with GetSpellInfo will get you correct information at any given time.

Then to get the actual GCD time, you only need to look at the current cooldown of one spell; it doesn't matter which one, so the above code just picks one basically at random.

If you only need to know the GCD at any given moment, and don't need a list of spells, it's even easier:

lua Code:
  1. local gcdSpell
  2.  
  3. local _, _, offset, numSpells = GetSpellTabInfo(2)
  4. for i = 1, offset + numSpells do
  5.      local slotType, slotID = GetSpellBookItemInfo(i, "spell")
  6.      if slotType == "SPELL" then
  7.           local slotName = GetSpellBookItemName(i, "spell")
  8.           local spellName, _, _, _, _, _, spellID = GetSpellInfo(slotName)
  9.           local spellCD = GetSpellBaseCooldown(spellID or 0) -- spellID can be nil during loading
  10.           if spellCD == 0 then
  11.                gcdSpell = spellID
  12.                break
  13.           end
  14.      end
  15. end
  16.  
  17. local start, duration = GetSpellCooldown(gcdSpell)

Items are slightly trickier, but as long as you know the GCD you can just compare the cooldown of the item to find out if the item is on the GCD or has its own cooldown. You could alternatively use tooltip parsing but that may not always be accurate.

Trying to guess the GCD of enemy players is probably a lost cause.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote