Snippet Damage Mod

Dirac

22710180
Reaction score
147
JASS:
library DamageMod /* v1.0.7

*/uses/*
*/  Damage  /* thehelper.net/forums/showthread.php/168144-Damage-Struct

A useful tool for damage modification.

***********************************************************************
*
*   struct DamageMod
*
*       -   Creates global damage mods and executes them on priority
*       -   order
*
*       static method create takes code func, integer priority returns DamageMod
*           -   Every time damage is recorded the function provided is
*           -   executed. Must return false. This function fires before
*           -   any particular unit damage registration. Fires by a
*           -   priority list, from lowest to greatest.
*       method destroy takes nothing returns nothing
*           -   Destroys the damage mod, the function will no longer
*           -   be excecuted
*       static method turnOff takes nothing returns nothing
*           -   Turns off the entire system.
*       static method turnOn takes nothing returns nothing
*           -   Turns on the entire system.
*       method disable takes nothing returns nothing
*           -   Disables the mod.
*       method enable takes nothing returns nothing
*           -   Enables the mod.
*
**********************************************************************
    
This needs to be a textmacro in order to keep the encapsulation correct
and private within the Damage library. Consider this textmacro private.

**********************************************************************/

//! textmacro DAMAGE_MOD_EX1
    globals
        private trigger       DamageModTrig     =CreateTrigger()
        private boolean       DamageModEnabled  =true
    endglobals

    struct DamageMod extends array
        implement LinkedList
        
        private boolexpr func
        private integer  priority
        
        
        static method turnOn takes nothing returns nothing
            set DamageModEnabled=true
        endmethod
        
        static method turnOff takes nothing returns nothing
            set DamageModEnabled=false
        endmethod
        
        //*********************************************************************
        //  As it seems w3 evaluates the trigger condition in the order they
        //  were added to the trigger, so basically this refreshes the trigger
        //  by clearing it's conditions and adding them again in the priority
        //  order. Uses a linked list to keep track of conditions.
        private static method refresh takes nothing returns nothing
            local thistype this=base.next
            call TriggerClearConditions(DamageModTrig)
            loop
                exitwhen head
                call TriggerAddCondition(DamageModTrig,func)
                set this=next
            endloop
        endmethod
        
        //*********************************************************************
        //  Bubbles down the mod's priority until the correct place in the list
        //  is found (the "head"). Then creates a new node previous to it.
        method enable takes nothing returns nothing
            local integer  priority = this.priority
            local thistype node = base.next
            local thistype last = base
            loop
                exitwhen node.head
                if priority>=node.priority then
                    set last=node
                endif
                set node=node.next
            endloop
            call last.insertNode(this)
            call refresh()
        endmethod
        
        //*********************************************************************
        //  Deletes the node from the linked list and refreshes the trigger's
        //  condition to keep the priority order correct. Notice that since the
        //  node is no longer in the list, the condition added to the trigger
        //  will not survive the refresh method.
        method disable takes nothing returns nothing
            call this.removeNode()
            call refresh()
        endmethod
        
        static method create takes code func, integer priority returns thistype
            local thistype this=allocate()
            
            set this.func=Filter(func)
            set this.priority=priority
            
            call this.enable()
            
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            call this.disable()
            call this.deallocate()
        endmethod
    endstruct
//! endtextmacro

//! textmacro DAMAGE_MOD_EX2

    //*********************************************************************
    //  Conditions will be evaluated in the order they were added, so there
    //  is nothing to worry.
    if DamageModEnabled then
        call TriggerEvaluate(DamageModTrig)
    endif
//! endtextmacro
endlibrary
 

PurgeandFire

zxcvmkgdfg
Reaction score
510
Nice. :)

Do you need a destroy method though? Not sure when someone would destroy an instance. Also, I think you can just do this with a single-linked list, instead of a doubly. (just use 'next' members, since all you really need it for is forward iteration)
 

Dirac

22710180
Reaction score
147
Yes i wouldn't need a doubly linked list if there were no destroy method. But adding one isn't really that much of a problem, so i'll just let it be.
One thing i really don't need and didn't include is instance recycling, it makes no sense that someone would create 8190 damage mods.
 

Dirac

22710180
Reaction score
147
v1.02
-Added the "turnOn" and "turnOff" static methods to disable all damage mods.
 

Dirac

22710180
Reaction score
147
v1.03
-Added support for custom damage types
-Added data attachment to mods
-Fixed encapsulation issues
-Added the GetDamageMod function
 

Dirac

22710180
Reaction score
147
v1.0.6
-Added support for the newest version of LinkedList and improved the code.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • jonas jonas:
    where did you go?
  • The Helper The Helper:
    Jefferson TX on a Paranormal Investigation of a haunted bed and breakfast - I got some friends that are paranormal investigators and they have an RV and do YouTubes
    +1
  • The Helper The Helper:
    It was a lot of fun. The RV was bad ass
  • jonas jonas:
    That sounds like fun!
    +1
  • The Helper The Helper:
    it was a blast!
  • The Helper The Helper:
    I am going to post the Youtube of the investigation in the forums when it is ready
    +1
  • jonas jonas:
    cool!
  • vypur85 vypur85:
    Sounds cool TH.
  • tom_mai78101 tom_mai78101:
    I was on a Legend of Zelda marathon...
  • tom_mai78101 tom_mai78101:
    Am still doing it now
    +1
  • jonas jonas:
    which one(s) are you playing?
  • jonas jonas:
    I played a little bit of the switch title two weeks ago and found it quite boring
  • The Helper The Helper:
    just got back from San Antonio this weekend had the best Buffalo Chicken Cheesesteak sandwhich in Universal City, TX - place was called Yous Guys freaking awesome! Hope everyone had a fantastic weekend!
    +1
  • The Helper The Helper:
    Happy Tuesday!
  • The Helper The Helper:
    We have been getting crazy numbers reported by the forum of people online the bots are going crazy on us I think it is AI training bots going at it at least that is what it looks like to me.
  • The Helper The Helper:
    Most legit traffic is tracked on multiple Analytics and we have Cloud Flare setup to block a ton of stuff but still there is large amount of bots that seem to escape detection and show up in the user list of the forum. I have been watching this bullshit for a year and still cannot figure it out it is drving me crazy lol.
    +1
  • Ghan Ghan:
    Beep boop
    +1
  • The Helper The Helper:
    hears robot sounds while 250 bots are on the forum lol
  • The Helper The Helper:
    Happy Saturday!
    +1
  • The Helper The Helper:
    and then it was Thursday...
    +2
  • tom_mai78101 tom_mai78101:
    And then Monday
    +1
  • The Helper The Helper:
    I got the day off today!
    +1
  • tom_mai78101 tom_mai78101:
    How...? (T-T)
  • The Helper The Helper:
    I took the day off. I work for myself so I can do that.
    +1
  • Varine Varine:
    Well I'm already over summer

    The Helper Discord

    Members online

    No members online now.

    Affiliates

    Hive Workshop NUON Dome World Editor Tutorials

    Network Sponsors

    Apex Steel Pipe - Buys and sells Steel Pipe.
    Top