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
509
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
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • 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 Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top