View Single Post
02-19-14, 09:26 AM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
In the interests of reducing code duplication, and reducing the number of function calls and global lookups (checking the class and spec on every power update is quite wasteful) I would rewrite that like so:

Lua Code:
  1. caelUI.powersound = caelUI.createModule("PowerSound")
  2.  
  3. -- Use RegisterUnitEvent, not RegisterEvent, to filter out irrelevant events.
  4. caelUI.powersound:RegisterUnitEvent("UNIT_COMBO_POINTS", "player")
  5. caelUI.powersound:RegisterUnitEvent("UNIT_POWER", "player")
  6.  
  7. -- Look these things up once at load; they're not going to change mid-session.
  8. local POWER_TYPE, POWER_DIVISOR
  9. if caelUI.playerClass == "MONK" then
  10.     POWER_TYPE = "CHI"
  11. elseif caelUI.playerClass == "PALADIN" then
  12.     POWER_TYPE = "HOLY_POWER"
  13. elseif caelUI.playerClass == "PRIEST" then
  14.     POWER_TYPE = "SHADOW_ORBS"
  15.     MAX_POWER = PRIEST_BAR_NUM_ORBS
  16. elseif caelUI.playerClass == "WARLOCK" then
  17.     -- Only register these events on a class that needs it.
  18.     caelUI.powersound:RegisterEvent("PLAYER_LOGIN")
  19.     caelUI.powersound:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
  20. end
  21.  
  22. local lastCount = 0
  23. caelUI.powersound:SetScript("OnEvent", function(self, event, unit, powerType)
  24.     -- This will quickly get out of irrelevant power updates, which are the most frequent case.
  25.     if event == "UNIT_POWER" and powerType ~= POWER_TYPE then return end
  26.  
  27.     -- This is the least frequent case, but the other events share code, so check for this one first.
  28.     if event == "PLAYER_SPECIALIZATION_CHANGED" then
  29.         local spec = GetSpecialization()
  30.         if spec == SPEC_WARLOCK_AFFLICTION then
  31.             POWER_TYPE = "SOUL_SHARDS"
  32.             POWER_DIVISOR = 1
  33.         elseif spec == SPEC_WARLOCK_DEMONOLOGY then
  34.             POWER_TYPE = "DEMONIC_FURY"
  35.             POWER_DIVISOR = 250
  36.         elseif spec == SPEC_WARLOCK_DESTRUCTION then
  37.             POWER_TYPE = "BURNING_EMBERS"
  38.             POWER_DIVISOR = 10
  39.         end
  40.         return
  41.     end
  42.  
  43.     local count, maxCount
  44.     if event == "UNIT_COMBO_POINTS" then
  45.         count = UnitComboPoints("player", "target")
  46.         maxCount = MAX_COMBO_POINTS
  47.     elseif POWER_DIVISOR then
  48.         -- Only use this code path for warlocks to avoid the 2 function calls if they're not needed.
  49.         count = floor(UnitPower(unit, powerType) / POWER_DIVISOR)
  50.         maxCount = MAX_POWER or floor(UnitPowerMax(unit, powerType) / POWER_DIVISOR)
  51.     else
  52.         count = UnitPower(unit, powerType)
  53.         maxCount = MAX_POWER or UnitPowerMax(unit, powerType)
  54.     end
  55.  
  56.     if count > lastCount then
  57.         -- count can't be < 0, so neither can lastCount, and 0 is not > 0, so no need to check count > 0
  58.         if count == maxCount then
  59.             -- Check this first so you can just "else" straight into the other thing.
  60.             PlaySoundFile(caelMedia.files.soundComboMax, "Master")
  61.         else
  62.             PlaySoundFile(caelMedia.files.soundCombo, "Master")
  63.         end
  64.     end
  65.  
  66.     lastCount = count
  67. end)

Also you may want to upvalue UnitPower, UnitPowerMax, and math.floor to speed things up.
__________________
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.

Last edited by Phanx : 02-19-14 at 03:37 PM.
  Reply With Quote