• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

How to use mag's spell models?

Status
Not open for further replies.
Level 14
Joined
Apr 20, 2009
Messages
1,543
I'm kind of confused as to how to use the following code:
JASS:
struct Spell

    private static constant integer ABIL_CODE = 'A000'
    private static constant integer BUFF_CODE = 'B000'
    private static constant integer UNIT_CODE = 'u000'

    private static constant real TIMEOUT = 0.03125

    private thistype next
    private thistype prev

    private static timer iterator = CreateTimer()
    private static integer count = 0

    private unit caster
    private unit target
    private real damage

    private method destroy takes nothing returns nothing
        /*
        *   Deallocate this instance.
        */
        call this.deallocate()

        /*
        *   We remove the instance from the linked list.
        */
        set this.next.prev = this.prev
        set this.prev.next = this.next

        /*
        *   We decrease the count by 1.
        *   If the count is 0, we pause the timer.
        */
        set count = count - 1
        if count == 0 then
            call PauseTimer(iterator)
        endif

        /*
        *   We null the data in the struct.
        *   This is completely optional. It doesn't really make a difference
        *   at all. (Unless you're casting the spell some hundreds of times.)
        *   If you have some real memory intense systems in your map, 
        *   you might want to do this, especially if your struct has a lot of data.
        *
        *   These are global variables, so they will be recycled eventually.
        *   It's all up to you, my friend.
        */
        set this.caster = null
        set this.target = null

        // Code.

    endmethod

    private static method periodic takes nothing returns nothing
        /*
        *   Starting from the first instance, we loop
        *   over all the instance in the list until we hit
        *   a dead-end.
        */
        local thistype this = thistype(0).next
        loop
            exitwhen this == 0

            // Code.

            set this = this.next
        endloop
    endmethod

    private static method run takes nothing returns boolean
        /*
        *   We allocate an instance.
        */
        local thistype this = thistype.allocate()

        /*
        *   We add the instance to the linked list.
        */
        set this.next = 0
        set this.prev = thistype(0).prev
        set thistype(0).prev.next = this
        set thistype(0).prev = this

        /*
        *   We increase the count by 1.
        *   If the count is 1, we start the timer to loop through 
        *   the instances. This is because recasting the spell while 
        *   an instance is already running shouldn't restart the timer.
        */
        set count = count + 1
        if count == 1 then
            call TimerStart(iterator, TIMEOUT, true, function thistype.periodic)
        endif

        /*
        *   We set our struct data.
        */
        set this.caster = GetTriggerUnit()
        set this.target = GetSpellTargetUnit()

        // Code.

        return false
    endmethod

    private static method onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
        call TriggerAddCondition(t, Condition(function thistype.run))
        set t = null
    endmethod
endstruct

I need to make a spell that damages a group of units every 1 sec for 20 secs.
For this I need a timer but I'm not sure how to use a tick inside the following code in order to accomplish it.
Could someone perhaps provide me with an example on how this is done?
I am quite used to using TimerUtils in the past. I want to learn how it is done correctly using structs instead.
I heared that TimerTools is more efficient but I'm not quite sure how to use it yet if I even need it when using this instead.
Since I'm using a struct I should be able to create a timer for each instance right?
 
Last edited:
Level 14
Joined
Apr 20, 2009
Messages
1,543
That's what I was thinking too, but then what about the FPS? The timer has an interval of 0.03125 which means it needs some tick in order to calculate a full second has passed right?
How would I go about doing that?
I'm sure there is more to it in this code but I can't figure out what...

I thought that the timer is used for looping through all the instances of the struct untill there are none left. Making it so that the timer repeats constantly for each and every instance.
But then I don't understand what exactly to do with it...

EDIT: I think I got it, it's 32 ticks per second. All that I'd need to do is set this.tick = this.tick + 1 and then if tick == 32 do the damage. Then if the tick is 32*20 destroy.
Seems simple enough right?
 
Last edited:
Status
Not open for further replies.
Top