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: 407

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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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