HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

TimerFuncs

01-09-2008, 09:31 PM#1
xombie
Hey sorry I'm at school right now I just wrote something quickly but could somebody please check for me to see if this compiles properly - would really care for some feedback on its operation too.

Collapse JASS:
library TimerFuncs initializer init

    //============================================================================
    globals
        private timer T = CreateTimer()
        private timer E = CreateTimer()

        private integer DATA_MAX = 0
        private timerfunc array DATA
    endglobals
    
    //============================================================================
    function TimerStart takes real timeout, boolean periodic, code actionFunc, integer tag returns nothing
        call timerfunc.create(actionFunc, tag, periodic, timeout)
    endfunction
    //============================================================================
    function TimerStop takes nothing returns boolean
        if timerfunc.curr!=0 then
            call timerfunc.curr.destroy()
            return true
        endif
        return false
    endfunction    
    //============================================================================
    constant function TimerGetData takes nothing returns integer
        return timerfunc.curr.tag
    endfunction

    //============================================================================
    private function findNext takes nothing returns timerfunc
        local integer i=0
        local timerfunc next
        
        set next=DATA[i]
        loop
            exitwhen i==DATA_MAX
            if DATA[i].time < next then    
                set next = DATA[i]
            endif
            set i=i+1
        endloop
        return next
    endfunction

    //============================================================================
    private function handler takes nothing returns nothing
        local integer i=0
        local integer n=0
        local timerfunc next
        local timerfunc array destroy
        local trigger t

        loop
            exitwhen i==DATA_MAX
            if DATA[i].time<=TimerGetElapsed(T) then

                set timerfunc.curr=DATA[i]

                set t=CreateTrigger()
                call TriggerAddAction(t, DATA[i].actionFunc)
                call TriggerExecute(t)
                call DestroyTrigger(t)

                if DATA[i].periodic then
                    set DATA[i].time=TimerGetElapsed(T)+DATA[i].timeout
                else
                    set destroy[n]=DATA[i]
                    set n=n+1        
                endif
            endif
            set i=i+1
        endloop
        set timerfunc.curr=0
        set i=0
        loop
            exitwhen i==n then
            call destroy[i].destroy()
            set i=i+1
        endloop

        set next=findNext()
        call PauseTimer(E)
        if next != 0 then
            call TimerStart(E, next.time-TimerGetElapsed(T), false, function handler)
        endif

        set t=null
    endfunction

    //============================================================================
    struct timerfunc
        code actionFunc
        integer tag
        real time
        real timeout

        private boolean periodic

        static timerfunc curr

        static method create takes code actionFunc, integer tag, boolean periodic, real timeout returns timerfunc
            local timerfunc dat=timerfunc.allocate()
                        local timerfunc next

            set dat.actionFunc=actionFunc
            set dat.tag=tag
            set dat.time=TimerGetElapsed(T)+timeout
            set dat.timeout=timeout
            set dat.periodic=periodic
            
            set next=findNext()
            call PauseTimer(E)
            call TimerStart(E, next.time-TimerGetElapsed(T), false, function handler)

            return dat
        endmethod

        method onDestroy takes nothing returns nothing
            set DATA_MAX=DATA_MAX-1
            set DATA[.index]=DATA_MAX
            set DATA[.index].index=.index
        endmethod
    endstruct

    //============================================================================
    private function init takes nothing returns nothing
        call TimerStart(T, 1000000000, false, null)
    endfunction

endlibrary
01-09-2008, 09:53 PM#2
Vexorian
It can't compile because blizzard does not allow code arrays and your code would make jasshelper generate one.
01-09-2008, 10:05 PM#3
xombie
Hmmm good point. What would you suggest that I use to replace 'code'? I mean, I could use strings for ExecuteFunc but that is not a plus.

Would a function interface work? Err... I should just wait until I get home so I don't have to bother everybody.
01-09-2008, 10:12 PM#4
cohadar
triggercondition

But I already made that one :P
01-10-2008, 12:15 AM#5
xombie
You smell bad cohadar. Eh, I'm just kind of experamenting with this stuff I'm not very good with JASS. I'm trying to make a decent vector system too at the moment but I'm having trouble with it.