[System] Effect

Level 6
Joined
Oct 23, 2011
Messages
182
I thought it might be a good idea to use one effect array.. since you likely won't have 8191 effects in map at once.


JASS:
library EffectList /* v1.0.0.4
*************************************************************************************
*
*    This system allows you to easily manage and link effects.
*
*************************************************************************************
*
*   struct Effect extends array
*
*       Methods
*       ------------------
*       static method create takes nothing returns thistype
*       - Creates a list of effects.
*
*           method addPoint takes string s, real x, real y returns thistype
*           - Add a point effect to list.
*           method addTarget takes string s, unit u, string attachment returns thistype
*           - Add a target effect to list.
*
*       static method createPoint takes string s, real x, real y returns thistype
*       - Creates an effect on point.
*
*       static method createTarget takes string s, unit u, string attachment returns thistype
*       - Creates an effect on target.
*
*       method destroy takes nothing returns nothing
*       - Destroys effect and any effects linked to the instance
*
**************************************************************************************/

    struct Effect extends array
    
        readonly thistype prev
        readonly thistype next
        readonly effect eff
        readonly boolean head
        readonly boolean linked
        
        private static integer index = 0
        
        method destroy takes nothing returns nothing
            local thistype node
            
            if .head then
                set .head = false
                set node = .next
                loop
                    exitwhen node == this
                    call DestroyEffect(node.eff)
                    set node.eff = null
                    set node.linked = false
                    set node = node.next
                endloop
                set .next.prev = thistype(0).prev
            else
                if .linked then
                    set .linked = false
                    set .prev.next = .next
                    set .next.prev = .prev
                endif
                set .prev = thistype(0).prev
                call DestroyEffect(.eff)
                set .eff = null
            endif
            
            set thistype(0).prev = this
        endmethod
        
        private static method allocate takes nothing returns thistype
            local thistype this = thistype(0).prev
            if this == 0 then
                set index = index + 1
                set this = index
            else
                set thistype(0).prev = .prev
            endif
            return this
        endmethod
        
        private method linkNode takes thistype node returns thistype
            set node.prev = .prev
            set .prev.next = node
            set .prev = node
            set node.next = this
            set node.linked = true
            return node
        endmethod
        private static method createNode takes effect e returns thistype
            local thistype this = thistype.allocate()
            set .eff = e
            return this
        endmethod
                
        static method createPoint takes string s, real x, real y returns thistype
            return createNode(AddSpecialEffect(s, x, y))
        endmethod
        static method createTarget takes string s, unit u, string a returns thistype
            return createNode(AddSpecialEffectTarget(s, u, a))
        endmethod
        
        method addPoint takes string s, real x, real y returns thistype
            return .linkNode(createPoint(s, x, y))
        endmethod
        method addTarget takes string s, unit u, string a returns thistype
            return .linkNode(createTarget(s, u, a))
        endmethod
        static method create takes nothing returns thistype
            local thistype this = thistype.allocate()
            set .prev = this
            set .next = this
            set .head = true
            return this
        endmethod
    endstruct
    
endlibrary

edit: fixed error

Demo:
JASS:
struct Demo extends array

    static Effect e1
    static Effect e2
    static Effect e3

    private static method onInit takes nothing returns nothing
        local unit u = CreateUnit(Player(0), 'hpea', 0, 0, 0)
        
        set e1 = Effect.createTarget("Blabla.mdl", u, "origin") //Single effect
        set e2 = Effect.create()                                //Multiple effect
        set e3 = Effect.create()                                //Multiple effect
        
        call e2.addTarget("Blabla.mdl", u, "hand right")
        call e2.addTarget("Blabla.mdl", u, "hand left")
        call e3.addTarget("Blabla.mdl", u, "hand right").addTarget("Blabla.mdl", u, "hand left")
        
        call e1.destroy() //Single effect destroyed in this case
        call e2.destroy() //Multiple effect destroyed in this case
        call e3.destroy() //Multiple effect destroyed in this case
    endmethod
endstruct
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,307
these should be readonly

JASS:
thistype prev
thistype next
effect eff
boolean head

These should be private

JASS:
boolean linked
static integer index = 0

Everything to do with this should be debug only

boolean linked

This should be in a private struct

implement CT32

This should be readonly

JASS:
thistype prev
thistype next
UnitIndex caster
integer order

This should be a separate resource

struct ChannelEffect extends array

that's all for now

edit
see that link isn't for debug only, so nvm on the link debug only part, lol

linked should still be readonly though ; p
 
Last edited by a moderator:
You might want to include a sample of usage (it is just good practice, especially if it is a practical use of the system).

I like this system though! It is very convenient for spell making. My only qualm is that the library is named "Effect". Even though it works, it doesn't really say much about the system. I'd go with "EffectList". This, of course, is completely optional--but those are my 2 cents.
 
Level 6
Joined
Oct 23, 2011
Messages
182
You might want to include a sample of usage (it is just good practice, especially if it is a practical use of the system).

I like this system though! It is very convenient for spell making. My only qualm is that the library is named "Effect". Even though it works, it doesn't really say much about the system. I'd go with "EffectList". This, of course, is completely optional--but those are my 2 cents.

Ok I'll rename the library and adds some examples later. However, I wanted to keep the struct name as it is because I wanted it to be easy-to-remember/type replacement for effects
 
Top