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
  • 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