System Buff Generator

bills

New Member
Reaction score
0
Buff Generator version 3.1.9

This system allows rapid and easy buff development.

Requirements:
Unit Indexer by Nestharus
Event by Nestharus
CTL by Nestharus
TimerUtilsEx by Magtheridon96
Table by Bribe


System code:
JASS:
library BuffGen /* v3.1.9.0
************************************************************************************************
*    BUFF GENERATOR by Bills
*
*	 This system allows rapid and easy buff development.
*
************************************************************************************************
*
*   */ uses /*
*   
*       */ UnitIndexer /*   hiveworkshop.com/forums/jass-resources-412/system-unit-indexer-172090/
*       */ Event /*         hiveworkshop.com/forums/jass-resources-412/snippet-event-186555/
*       */ CTL /*           hiveworkshop.com/forums/jass-resources-412/snippet-constant-timer-loop-32-a-201381/
*       */ TimerUtilsEx /*  hiveworkshop.com/forums/graveyard-418/system-timerutilsex-204500/
*       */ Table /*         hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
*
***************************************************************************************************
*
*    Functions
*
*        function AddBuff takes unit whichUnit, Buff whichBuff, real duration returns boolean
*            - returns true if added the buff
*
*        function RemoveBuff takes unit wichUnit, Buff whichBuff returns boolean
*            - returns true if removed the buff
*
*        function RemoveAllBuffs takes unit wichUnit returns nothing
*            - removes all buffs of unit
*
*        function UnitHasBuff takes unit wichUnit, Buff whichBuff returns boolean
*            - returns true if unit has the buff
*
*        function SetBuffDuration takes unit whichUnit, Buff whichBuff, real duration returns nothing
*            - sets the duration of buff
*
*        function AddBuffDuration takes unit whichUnit, Buff whichBuff, real duration returns nothing
*            - increments the duration of buff
*
*        function GetBuffDuration takes unit whichUnit, Buff whichBuff, real duration returns real
*            - returns the duration of buff
*
*        function CountBuffs takes unit whichUnit returns integer
*            - returns de amount of buffs that unit have
*
*******************************************************************************************
*
*   struct Buff extends array
*
*        static constant real TIMEOUT=0.03125
*            - Useful to the method onPeriodic
*        static Event START
*            - fires when a buff is added
*        static Event FINISH
*            - fires when a buff is removed
*
*        Event Responses
*            - function GetEventBuff takes nothing returns Buff
*            - function GetBuffEventUnit takes nothing returns unit
*            - function GetBuffEventUnitId takes nothing returns integer
*
*        function TriggerRegisterBuffEvent takes trigger t, Event e returns nothing
*            - Jass wrapper of EVENT.registerTrigger(trigger)
*
*        function RegisterBuffEvent takes boolexpr b, Event e returns nothing
*            - Jass wrapper of EVENT.register(boolexpr)
*
*****************************************************************************
*
*    module BuffStruct
*
*        static method operator buff takes nothing returns Buff
*            - returns the Buff generated by implementation of this module
*
*        (interface)
*
*            private static constant integer AURAID = 'AURA'
*                (required) - this is a tornado slow aura
*            private static constant integer BUFFID = 'BUFF'
*                (required) - this is the buff of aura
*
*            private method onApply takes nothing returns nothing
*                (optional) - called all times that AddBuff is called
*
*            private method onRemove takes nothing returns nothing
*                (optional) - called all times that RemoveBuff is called
*
*            private method onFinish takes nothing returns nothing
*                (optional) - called only when the buff finish completely your duration
*
*            private method onPeriodic takes nothing returns nothing
*                (optional) - called 32 times per second
*                
*            Notes:
*                - "thistype this" represents the index of buffed unit in all methods above.
*                - If you do not want to use AURAID or/and BUFFID, just set it with 0 (zero).
*
********************************************************************************
*
*    ForEachBuff Modules
*
*        allows the method ForEachBuff takes unit enumUnit returns nothing
*
*        module ForEachBuff
*            - declare local variables here
*        module ForEachBuffLoop
*            - run loop code
*        module ForEachBuffNull
*            - null locals
*        module ForEachBuffEnd
*
*        Exemple of ForEachBuff
*            - struct Test extends array
*                    unit targ
*                    implement ForEachBuff
*                        local unit target=thistype(GetUnitUserData(enumUnit)).targ
*                    implement ForEachBuffLoop
*                        // deals 50 damage on target for each buff on enumUnit
*                        call UnitDamageTarget(enumUnit, target, 50, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_MAGIC, WEAPON_TYPE_WHOKNOWS)
*                    implement ForEachBuffNull
*                        set target=null
*                    implement ForEachBuffEnd
*
*                    static method onCast takes nothing returns boolean
*                        set thistype(GetUnitUserData(GetTriggerUnit())).targ=GetSpellTargetUnit()
*                        call ForEachBuff(GetTriggerUnit())
*                        return false
*                    endmethod
*              endstruct
*
************************************************************************************/
    globals
        private integer total=0 //Buff indexer and counter
        private integer array aid //ability id
        private integer array bid //buff id
        private boolean array bhp //buff has periodic method?
        private integer array bc
        private Table array t
        private Event array e
        private integer array ed1 // event data
        private integer array ed2 // other event data
        private boolean ee=true
        private unit eu=null
        private Buff eb=0
    endglobals
    
    private module M
        private static method onInit takes nothing returns nothing
            set START=Event.create()
            set FINISH=Event.create()
        endmethod
    endmodule
    
    struct Buff extends array
        readonly static constant real TIMEOUT=0.031250000
        readonly static Event START
        readonly static Event FINISH
        implement M
    endstruct

    private function InitBuff takes integer auraid, integer buffid, code func returns integer
        local integer id=total+1
        set total=id
        set e[id]=Event.create()
        call e[id].register(Filter(func))
        set t[id]=Table.create()
        set aid[id]=auraid
        set bid[id]=buffid
        return id
    endfunction
    
    function UnitHasBuff takes unit u, Buff b returns boolean
        return t<b>.boolean[GetUnitUserData(u)]
    endfunction

    function RemoveBuff takes unit u, Buff b returns boolean
        debug if u==null or b&lt;=0 or integer(b)&gt;total then
            debug call BJDebugMsg(&quot;[Buff Generator] Error: invalid arguments on RemoveBuff&quot;)
			debug return false
		debug endif
		if UnitHasBuff(u,b) then
            set t<b>.boolean[GetUnitUserData(u)]=false
            if UnitMakeAbilityPermanent(u,false,aid<b>) then
                call UnitRemoveAbility(u,aid<b>)
                call UnitRemoveAbility(u,bid<b>)
            endif
            set ed1<b>=GetUnitUserData(u)
            set ed2<b>=2
            call e<b>.fire()
            if ee then
                set eu=u
                set eb=b
                call Buff.FINISH.fire()
            endif
            return true
        endif
        return false
    endfunction
    
    function AddBuff takes unit u, Buff b, real dur returns boolean
        local integer i
        debug if u==null or b&lt;=0 or integer(b)&gt;total then
            debug call BJDebugMsg(&quot;[Buff Generator] Error: invalid arguments on AddBuff&quot;)
            debug return false
        debug endif
		if UnitHasBuff(u,b) then
            call RemoveBuff(u,b)
            call AddBuff(u,b,dur)
        else
            set i=GetUnitUserData(u)
            set t<b>.boolean<i>=true
            if UnitAddAbility(u,aid<b>) then
                call UnitMakeAbilityPermanent(u,true,aid<b>)
            endif
            set t<b>.real<i>=dur
            set ed1<b>=i
            set ed2<b>=1
            call e<b>.fire()
            if ee then
                set eu=u
                set eb=b
                call Buff.START.fire()
            endif
            return true
        endif
        return false
    endfunction

    function RemoveAllBuffs takes unit u returns nothing
        local Buff i=total
        loop
            exitwhen i==0
            call RemoveBuff(u,i)
            set i=i-1
        endloop
    endfunction
    
    function SetBuffDuration takes unit u, Buff b, real dur returns nothing
        if UnitHasBuff(u,b) then
            set t<b>.real[GetUnitUserData(u)]=dur
            set ed1<b>=GetUnitUserData(u)
            set ed2<b>=3
            call e<b>.fire()
        endif
    endfunction

    function GetBuffDuration takes unit u, Buff b returns real
        if UnitHasBuff(u,b) then
            if bhp<b> then
                return t<b>.real[GetUnitUserData(u)]
            else
                return TimerGetRemaining(t<b>.timer[GetUnitUserData(u)])
            endif
        endif
        return 0.
    endfunction

    function AddBuffDuration takes unit u, Buff b, real dur returns nothing
        call SetBuffDuration(u,b,GetBuffDuration(u,b)+dur)
    endfunction
    
    function CountBuffs takes unit u returns integer
        return bc[GetUnitUserData(u)]
    endfunction
    
    function TriggerRegisterBuffEvent takes trigger t, Event e returns nothing
        call e.registerTrigger(t)
    endfunction

    function RegisterBuffEvent takes boolexpr t, Event e returns nothing
        call e.register(t)
    endfunction
    
    function GetBuffEventUnit takes nothing returns unit
        return eu
    endfunction

    function GetBuffEventUnitId takes nothing returns integer
        return GetUnitUserData(eu)
    endfunction
    
    function GetEventBuff takes nothing returns Buff
        return eb
    endfunction
    
    module BuffStruct
        private static Buff id //id of this buff
        
        static method operator buff takes nothing returns Buff
            return id
        endmethod
        
        static if thistype.onPeriodic.exists then
            private integer index
            private thistype T
            implement CTL
                local real d
            implement CTLExpire
                set d=t[id].real[index]-Buff.TIMEOUT
                call thistype(index).onPeriodic()
                if d&lt;=0.00 then
                    call RemoveBuff(GetUnitById(index),id)
                    static if thistype.onFinish.exists then
                        call thistype(index).onFinish()
                    endif
                else
                    set t[id].real[index]=d
                endif
            implement CTLNull
            implement CTLEnd
        else
            private static method exp takes nothing returns nothing
                local integer i=GetTimerData(GetExpiredTimer())
                call RemoveBuff(GetUnitById(i),id)
				static if thistype.onFinish.exists then
                    call thistype(i).onFinish()
                endif
            endmethod
        endif
        
        private static method m takes nothing returns nothing
            local integer i=ed1[id] //unit id
            local integer j=ed2[id] //data
            local thistype this=thistype(i)
            if j==1 then
                static if thistype.onPeriodic.exists then
                    set T=create()
                    set T.index=i
                else
                    set t[id].timer<i>=NewTimerEx(i)
                    call TimerStart(t[id].timer<i>,t[id].real<i>,false,function thistype.exp)
                endif
                static if thistype.onApply.exists then
                    call onApply()
                endif
                set bc<i>=bc<i>+1
            elseif j==2 then
                static if thistype.onPeriodic.exists then
                    call T.destroy()
                else
                    call ReleaseTimer(t[id].timer<i>)
                endif
                static if thistype.onRemove.exists then
                    call onRemove()
                endif
                set bc<i>=bc<i>-1
            elseif j==3 then
                static if not thistype.onPeriodic.exists then
                    set t[id].timer<i>=NewTimerEx(ReleaseTimer(t[id].timer<i>))
                    call TimerStart(t[id].timer<i>,t[id].real<i>,false,function thistype.exp)
                endif
            endif
        endmethod

        private static method onInit takes nothing returns nothing
            set id=InitBuff(AURAID,BUFFID,function thistype.m)
            static if thistype.onPeriodic.exists then
                set bhp[id]=true
            endif
        endmethod
    endmodule

	// ForEachBuff Modules
    module ForEachBuff
        static method ForEachBuff takes unit enumUnit returns nothing
            local Buff this=total
    endmodule
    module ForEachBuffLoop
            loop
                exitwhen this==0
                if UnitHasBuff(enumUnit,this) then
    endmodule
    module ForEachBuffNull
                endif
                set this=this-1
            endloop
    endmodule
    module ForEachBuffEnd
        endmethod
    endmodule
endlibrary</i></i></i></i></i></i></i></i></i></i></i></i></b></b></b></b></b></b></b></b></b></b></i></b></b></b></i></b></b></b></b></b></b></b></b></b>


Demos
Struct Example
JASS:
library BuffGenExemple uses BuffGen, Bonus
    /* Bloodlust
    ** Increase attack speed by 100%, when finishs duration apply beserk buff */

    struct Berserk extends array
        private static constant integer AURAID = &#039;A001&#039;
        private static constant integer BUFFID = &#039;B001&#039;

        private method onApply takes nothing returns nothing
            call AddUnitBonus(GetUnitById(this),BONUS_ATTACK_SPEED,300)
        endmethod

        private method onRemove takes nothing returns nothing
            call AddUnitBonus(GetUnitById(this),BONUS_ATTACK_SPEED,-300)
        endmethod

        private method onPeriodic takes nothing returns nothing
            //consumes 30 hp per second
            call SetWidgetLife(GetUnitById(this),GetWidgetLife(GetUnitById(this))-(30*Buff.TIMEOUT))
        endmethod

        implement BuffStruct
    endstruct

    struct Bloodlust extends array
        private static constant integer AURAID = &#039;A000&#039; 
        private static constant integer BUFFID = &#039;B000&#039; 

        private method onApply takes nothing returns nothing 
            call AddUnitBonus(GetUnitById(this),BONUS_ATTACK_SPEED,100)
        endmethod

        private method onRemove takes nothing returns nothing
            call AddUnitBonus(GetUnitById(this),BONUS_ATTACK_SPEED,-100)
        endmethod

        private method onFinish takes nothing returns nothing
            call AddBuff(GetUnitById(this),Berserk.buff,10) // adds berserk for 10 sec.
        endmethod
        
        implement BuffStruct

        // code of spell
        private static method a takes nothing returns boolean
            call AddBuff(GetTriggerUnit(),thistype.buff,10) // adds bloddlust for 10 sec.
            return false
        endmethod

        //initialization
        private static method onInit takes nothing returns nothing
            local trigger t=CreateTrigger()
            call TriggerAddCondition(t, Condition(function thistype.a))
            call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
            set t=null
        endmethod
    endstruct
endlibrary


Exemple of ForEachBuff
JASS:
struct Exemple extends array
	implement ForEachBuff
	implement ForEachBuffLoop
		// decreases the duration of each buff in 10 seconds
		call AddBuffDuration(enumUnit,this,-10)
	implement ForEachBuffNull
	implement ForEachBuffEnd

	static method onCast takes nothing returns boolean
		call ForEachBuff(GetTriggerUnit())
		return false
	endmethod
endstruct
 

Attachments

  • BuffGenerator 3.1.9.w3x
    63.7 KB · Views: 400

bills

New Member
Reaction score
0
Updated (v3.1.8).

- function GetBuffDuration fixed

@edit
Updated (v3.1.9).

- real TIMEOUT is now a static constant member of struct Buff -> Buff.TIMEOUT
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top