Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper Function: auraDuration( aura, duration, unit ) #4412

Open
wants to merge 1 commit into
base: thewarwithin
Choose a base branch
from

Conversation

syrifgit
Copy link
Collaborator

Another helper function for changing duration of buffs/debuffs, whether extended (very common) or reduced (like warlock arts or devastation evoker fire breath)

Features

  • Buffs and debuffs
    • Checks buff table first
    • if not found then will check debuffs
    • If neither are found, return 0
  • Extensions and reductions (via negative values)
  • removeBuff() if extension brings an aura to 0 or less (it checks .remains)
  • Useful return values
    • 1: Aura was modified successfully (it was up, and the extension was applied, and the aura is not removed because of it)
      • This could instead return aura.remains as it would always be a positive value? Would pass all the same test cases as 1, but maybe add additional functionality?
    • 0: No operations were done, most likely due to aura not being up or the function call supplied a 0 in the duration parameter
    • -1: Aura was negatively extended, and will be removed due to it
  • Instead of passing the table buff.buff_name, we should stick with passing the aura name as a string
    • While this is technically more CPU work, it keeps naming conventions in-line with all other similar functions such as applyBuff() and addStack()
    • If you are concerned with checking both tables, I could split this into 2 functions, buffDuration( aura, duration ) and debuffDuration( aura, duration, unit )
  • Unit parameter is optional, will default to "target". It is not considered when checking buffs.

Fully Commented Code

-- Modify aura remaining duration function.
-- Extends or reduces the duration of an aura. 
-- Returns:
  --  1: Successfully extended (positive or negative value).
  --  0: Aura not active, no operations performed.
  -- -1: Aura removed due to a negative extension that reduced its duration to 0 or less.
local function auraDuration( aura, duration, unit )
    if not aura then
        Error( "Invalid arguments passed to auraDuration: '%s'.\n\n%s", tostring( aura ), debugstack() )
        return 0
    end

    -- Default parameters.
    duration = duration or 1 -- Defaults to +1 second if no value supplied.
    if duration == 0 then return 0 end -- Don't perform operations for a zero duration.
    unit = unit or "target" -- Default to "target" for debuffs if no unit is specified.

    -- Buff handling.
    local b = state.buff[ aura ]
    if b and b.remains > 0 then
        -- Modify the buff's duration.
        b.expires = b.expires + duration

        if b.remains <= 0 then
            state.removeBuff( aura )
            return -1 -- Buff removed.
        end

        return 1 -- Buff extended successfully.
    end

    -- Debuff handling.
    local d = state.debuff[ aura ]
    if d and d.remains > 0 then
        -- Modify the debuff's duration.
        d.expires = d.expires + duration

        if d.remains <= 0 then
            state.removeDebuff( unit, aura )
            return -1 -- Debuff removed.
        end

        return 1 -- Debuff extended successfully.
    end

    -- Aura not active (neither buff nor debuff).
    return 0
end

state.auraDuration = auraDuration

Real Use Cases

Marksmanship Hunter

        if tensileTrueshotExtension == 4 then
            -- If consuming 2 stacks @ 4, it still caps at 5. Don't roll over to 6
            buff.trueshot.expires = buff.trueshot.expires + 1
        else
            buff.trueshot.expires = buff.trueshot.expires + count
        end
        if tensileTrueshotExtension == 4 then
            -- If consuming 2 stacks @ 4, it still caps at 5. Don't roll over to 6
            auraDuration( "trueshot" )
        else
           auraDuration( "trueshot", count )
        end

Devastation Evoker

if talent.consume_flame.enabled and debuff.fire_breath.up then debuff.fire_breath.expires = debuff.fire_breath.expires - 2 end
if talent.consume_flame.enabled then auraDuration( "fire_breath", -2, "target" ) end

This would also work due to "target" default

if talent.consume_flame.enabled then auraDuration( "fire_breath", -2 ) end

Shadow Priest

            if talent.expiation.enabled then
                    if debuff.shadow_word_pain.remains <= 6 then
                        removeDebuff( "shadow_word_pain" )
                    else
                        debuff.shadow_word_pain.expires = debuff.shadow_word_pain.expires - 6
                    end
            end
 if talent.expiation.enabled then auraDuration( "shadow_word_pain", -6 )

Havoc Demon Hunter

(yes I know the actual code here has changed since I wrote the example, hopefully it still illustrates)

local TriggerDemonic = setfenv( function( )

    if buff.metamorphosis.up then
        buff.metamorphosis.duration = buff.metamorphosis.duration + 7
        buff.metamorphosis.expires = buff.metamorphosis.expires + 7
    else
        applyBuff( "metamorphosis", 7 )
        if talent.inner_demon.enabled then
            applyBuff( "inner_demon" )
        end
        stat.haste = stat.haste + 10
        -- Fel-Scarred
        if talent.demonsurge.enabled then
            applyBuff( "demonsurge_annihilation", buff.metamorphosis.remains )
            applyBuff( "demonsurge_death_sweep", buff.metamorphosis.remains )
        end
    end

end, state )
local TriggerDemonic = setfenv( function( )

    local transform = auraDuration( "metamorphosis", 7 )

    if transform == 0 then
        applyBuff( "metamorphosis", 7 )
        if talent.inner_demon.enabled then
            applyBuff( "inner_demon" )
        end
        stat.haste = stat.haste + 10
        -- Fel-Scarred
        if talent.demonsurge.enabled then
            applyBuff( "demonsurge_annihilation", buff.metamorphosis.remains )
            applyBuff( "demonsurge_death_sweep", buff.metamorphosis.remains )
        end
    end

end, state )

@syrifgit syrifgit requested a review from Hekili February 27, 2025 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant