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