wc3campaigns
WC3C Homepage - www.wc3c.netUser Control Panel (Requires Log-In)Engage in discussions with other users and join contests in the WC3C forums!Read one of our many tutorials, ranging in difficulty from beginner to advanced!Show off your artistic talents in the WC3C Gallery!Download quality models, textures, spells (vJASS/JASS), systems, and scripts!Download maps that have passed through our rigorous approval process!

Go Back   Wc3C.net > Resources > - Submit a resource -
User Name
Password
Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar



Reply
 
Thread Tools Search this Thread
Old 10-27-2011, 09:32 AM   #1
Bribe
User
 
Bribe's Avatar
 
Join Date: Mar 2010
Posts: 233

Submissions (1)

Bribe will become famous soon enough (30)Bribe will become famous soon enough (30)

Send a message via AIM to Bribe
Default TimerUtilsEx

This is a fixed version of Vexorian's TimerUtils.
It allows you to call NewTimer inside module initializers (Useful for elapsed game time events and other things)

Collapse JASS:
library TimerUtilsEx
/*************************************************
*
*   TimerUtilsEx
*   v2.1.0.3
*   By Vexorian, Bribe & Magtheridon96
*
*   Original version by Vexorian.
*
*   Flavors:
*       Hashtable:
*           - RAM:              Minimal
*           - TimerData:        Slow
*
*       Array:
*           - RAM:              Maximal
*           - TimerData:        Fast
*
*   All the functions have O(1) complexity.
*   The Array version is the fastest, but the hashtable
*   version is the safest. The Array version is still
*   quite safe though, and I would recommend using it.
*   The system is much slower in debug mode.
*
*   API:
*   ----
*       - function NewTimer takes nothing returns timer
*           - Returns a new timer from the stack.
*       - function NewTimerEx takes integer i returns timer
*           - Returns a new timer from the stack and attaches a value to it.
*       - function ReleaseTimer takes timer t returns integer
*           - Throws a timer back into the stack. Also returns timer data.
*       - function SetTimerData takes timer t, integer value returns nothing
*           - Attaches a value to a timer.
*       - function GetTimerData takes timer t returns integer
*           - Returns the attached value.
*
*************************************************/

    globals //Calibration
        //Use hashtable, or fast array?
        private constant boolean USE_HASH = false
        //Max Number of Timers Held in Stack
        private constant integer QUANTITY = 256
    endglobals
    
    globals
        private timer array tT
        private integer tN = 0
        private hashtable ht = InitHashtable()
    endglobals
    
    private module Init
        static if not USE_HASH then
            private static method onInit takes nothing returns nothing
                local integer i = QUANTITY
                loop
                    set i = i - 1
                    set tT[i] = CreateTimer()
                    exitwhen i == 0
                endloop
                
                set tN = QUANTITY
            endmethod
        endif
    endmodule
    
    // JassHelper doesn't support static ifs for globals.
    private struct Data extends array
        static if not USE_HASH then
            static integer array data
        endif
        implement Init
    endstruct
    
    // Double free protection
    private function ValidTimer takes integer i returns boolean
        return LoadBoolean(ht, i, 1)
    endfunction
    
    private function Get takes integer id returns integer
        debug if not ValidTimer(id) then
            debug call BJDebugMsg("[TimerUtils]Error: Tried to get data from invalid timer.")
        debug endif
        static if USE_HASH then
            return LoadInteger(ht, id, 0)
        else
            return Data.data[id - 0x100000]
        endif
    endfunction
    
    private function Set takes integer id, integer data returns nothing
        debug if not ValidTimer(id) then
            debug call BJDebugMsg("[TimerUtils]Error: Tried to attach data to invalid timer.")
        debug endif
        static if USE_HASH then
            call SaveInteger(ht, id, 0, data)
        else
            set Data.data[id - 0x100000] = data
        endif
    endfunction
    
    function SetTimerData takes timer t, integer data returns nothing
        call Set(GetHandleId(t), data)
    endfunction
    
    function GetTimerData takes timer t returns integer
        return Get(GetHandleId(t))
    endfunction
    
    function NewTimerEx takes integer data returns timer
        local integer id
        if tN == 0 then
            static if USE_HASH then
                set tT[0] = CreateTimer()
            else
                debug call BJDebugMsg("[TimerUtils]Error: No Timers In The Stack! You must increase 'QUANTITY'")
                return null
            endif
        else
            set tN = tN - 1
        endif
        set id = GetHandleId(tT[tN])
        call SaveBoolean(ht, id, 1, true)
        call Set(id, data)
        return tT[tN]
    endfunction
    
    function NewTimer takes nothing returns timer
        return NewTimerEx(0)
    endfunction
    
    function ReleaseTimer takes timer t returns integer
        local integer id = GetHandleId(t)
        local integer data = 0
        
        // Pause the timer just in case.
        call PauseTimer(t)
        
        // Make sure the timer is valid.
        if ValidTimer(id) then
            // Get the timer's data.
            set data = Get(id)
            
            // Unmark handle id as a valid timer.
            call RemoveSavedBoolean(ht, id, 1)
            
            //If it's not run in USE_HASH mode, this next block is useless.
            static if USE_HASH then
            
                //At least clear hash memory while it's in the recycle stack.
                call RemoveSavedInteger(ht, id, 0)
                
                // If the recycle limit is reached
                if tN == QUANTITY then
                    // then we destroy the timer.
                    call DestroyTimer(t)
                    return data
                endif
            endif
            
            //Recycle the timer.
            set tT[tN] = t
            set tN = tN + 1
            
        //Tried to pass a bad timer.
        debug else
            debug call BJDebugMsg("[TimerUtils]Error: Tried to release non-active timer!")
        endif
        
        //Return Timer Data.
        return data
    endfunction

endlibrary

library TimerUtils requires TimerUtilsEx
endlibrary

Last edited by Bribe : 10-27-2011 at 01:59 PM.
Bribe is offline   Reply With Quote
Sponsored Links - Login to hide this ad!
Old 10-27-2011, 11:56 AM   #2
Vexorian
Free Software Terrorist
 
Vexorian's Avatar


Technical Director
 
Join Date: Apr 2003
Posts: 14,898

Submissions (37)

Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)

Hero Contest #3 - 2nd Place

Default

Quote:
It allows you to call NewTimer inside module initializers
What do you mean by that? I cannot really think of any reason it wouldn't work before. So you would have to explain it.

It doesn't seem like you are the author of this.

The rest seems rather unnecessary and probably even slower. Except for NewTimerEx which can easily be added to the real timer utils anyway.
__________________
Zoom (requires log in)Wc3 map optimizer 5.0
Someone should fix .wav sound in this thing.
Zoom (requires log in)JassHelper 0.A.2.A
Turns your simple code into something that is complicated enough to work.
Faster != more useful
Vexorian is offline   Reply With Quote
Old 10-27-2011, 12:33 PM   #3
Bribe
User
 
Bribe's Avatar
 
Join Date: Mar 2010
Posts: 233

Submissions (1)

Bribe will become famous soon enough (30)Bribe will become famous soon enough (30)

Send a message via AIM to Bribe
Default

Vexorian, nice to see you around I must say. It's inspiring.

Magtheridon96 and I wrote this revised script, it is a co-authorship of sorts. I suppose I could write "by Vexorian, Magtheridon96 and Bribe" but I'd really prefer if you just updated TimerUtils.

The reason it doesn't work in module initializers is because when you use hashtable a new timer isn't created because tN is equal to 0 (because you initialize in a library Init instead of a module init and TimerUtils bugs in hashtable mode when timers overflow).

If I had my wishes I would really like it if you updated your TimerUtils library. Then we can stop with this unpleasant trend of re-writing systems.

In fact, while we are on this subject it would make the most sense humanly possible if JassHelper were updated so that initializers, whether be module, struct or library, initialized before any library requiring it.

The rest are not needed features, though nice to have, but don't slow anything down except in ReleaseTimer. I made the change in ReleaseTimer so that more than QUANTITY timers could not be recycled, because there are sometimes spikes in the game where lots of timers might be needed but not all would need to be recycled.

You can view the development on this crap-tastic subject here: http://www.hiveworkshop.com/forums/j...00/index5.html

Last edited by Bribe : 10-27-2011 at 01:25 PM.
Bribe is offline   Reply With Quote
Old 10-29-2011, 02:12 AM   #4
Vexorian
Free Software Terrorist
 
Vexorian's Avatar


Technical Director
 
Join Date: Apr 2003
Posts: 14,898

Submissions (37)

Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)

Hero Contest #3 - 2nd Place

Default

I figured out about the thing with onInit when I wasn't looking at the site. Quite honestly, I think the best thing I could do is update jassHelper to remove struct and module onInit altogether, because it is really ridiculous how every library wants to initialize before modules.. But then modules wouldn't work very great, I guess.

I will update TimerUtils to have NewTimerEx and a module onInit. sigh.
__________________
Zoom (requires log in)Wc3 map optimizer 5.0
Someone should fix .wav sound in this thing.
Zoom (requires log in)JassHelper 0.A.2.A
Turns your simple code into something that is complicated enough to work.
Faster != more useful
Vexorian is offline   Reply With Quote
Old 10-29-2011, 09:51 AM   #5
Anitarf
Procrastination Incarnate


Development Director
 
Join Date: Feb 2004
Posts: 8,177

Submissions (19)

Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)

2008 Spell olympics - Fire - SilverApproved Map: Old School Alliance TacticsHero Contest #2 - 3rd PlaceSpell making session 2 winner

Default

Quote:
Originally Posted by Vexorian
I figured out about the thing with onInit when I wasn't looking at the site. Quite honestly, I think the best thing I could do is update jassHelper to remove struct and module onInit altogether, because it is really ridiculous how every library wants to initialize before modules.. But then modules wouldn't work very great, I guess.
That's quite an understatement. Such a change would also break a ton of libraries that are widely used, yet their maintainers are no longer active to make the necessary changes (if those changes are even possible, as you say this would make modules less functional). We'd have a huge surge of new people rewriting old libraries again, as if that isn't enough of a problem already.

I agree about the widespread use of module initializers being ridiculous, though. Most libraries don't need to do it.

Are the edits Vex made to TimerUtils adequate for me to graveyard this, Bribe?
__________________
Anitarf is offline   Reply With Quote
Old 10-29-2011, 10:00 AM   #6
Bribe
User
 
Bribe's Avatar
 
Join Date: Mar 2010
Posts: 233

Submissions (1)

Bribe will become famous soon enough (30)Bribe will become famous soon enough (30)

Send a message via AIM to Bribe
Default

There is still the issue that you and I have addressed where overflow timers in hashtable-mode bug because the "set tT[0] = CreateTimer()" line is only there if hashtable mode is "off".

You can go ahead and ditch this resource as I never wanted it in the first place, what I wanted was a system that can be linked to that works 100%.

On an side note Vex now uses .evaluate() on the init function plus a textmacro instead of a wrapper, which unfortunately makes the code way longer than before while being completely avoidable by doing the approach I have here.
Bribe is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT. The time now is 02:16 PM.


Affiliates
The Hubb The JASS Vault Clan WEnW Campaign Creations Clan CBS GamesModding Flixreel Videos

Powered by vBulletin (Copyright ©2000 - 2017, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services