• 🏆 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!

[JASS] A timers helper

Status
Not open for further replies.
Level 2
Joined
Jul 23, 2010
Messages
11
I first want to mention that this code is technically vJass. However, I only used scope benefits. Take that out, and you have plain JASS code.
This is basically a library which makes it easier to attach data to timers via hashtables.
JASS:
// Uses one hashtable
library Timers initializer Init
    globals
        private constant integer HKEY_FREE_TIMERS_HIDS = 0

        private constant integer HKEY_FREETIMER_FUNC_DATA = 1
        private constant integer HVAL_FREETIMER_FUNC_TIMERS_TO_FREE = 0
        private constant integer HVAL_FREETIMER_FUNC_DATA_OFFSET = 1

        private constant integer HVAL_TIMER_HANDLE = 0
        private constant integer HVAL_TIMER_FREETIMER_INDEX = 1
        private constant integer HVAL_TIMER_DATA_OFFSET = 2

        // Holds timers' handles, and attached data
        // Timer's data is accessed by it's handle id
        private hashtable data = InitHashtable()
        // The timer used for the FreeTimer function
        private timer freetimer_func_loop_timer = CreateTimer()
        // The index of a last freed timer; used for optimisation
        private integer last_freed_index = 0
        // The number of created timers
        private integer timers_created = 0
        // The number of free timers in the pool
        private integer timers_free = 0
    endglobals

    function Init takes nothing returns nothing
        call SaveInteger(data, HKEY_FREETIMER_FUNC_DATA, HVAL_FREETIMER_FUNC_TIMERS_TO_FREE, 0)
    endfunction

    function NewTimer takes nothing returns timer
        local timer t
        local integer t_hid
        local integer i
        local integer index

        set i = 0
        loop
            if(i < timers_free) then
                set t_hid = LoadInteger(data, HKEY_FREE_TIMERS_HIDS, i)
                if(t_hid != 0) then
                    set index = i
                endif

                if(t_hid == 0) then
                    set index = last_freed_index
                    set t_hid = LoadInteger(data, HKEY_FREE_TIMERS_HIDS, index)
                endif

                if(t_hid == 0) then
                    if(last_freed_index < timers_created - 1) then
                        set index = last_freed_index + 1
                        set t_hid = LoadInteger(data, HKEY_FREE_TIMERS_HIDS, index)
                    endif
                endif

                if(t_hid == 0) then
                    if(last_freed_index > 0) then
                        set index = last_freed_index - 1
                        set t_hid = LoadInteger(data, HKEY_FREE_TIMERS_HIDS, index)
                    endif
                endif

                if(t_hid != 0) then
                    set t = LoadTimerHandle(data, t_hid, HVAL_TIMER_HANDLE)
                    set timers_free = timers_free - 1
                    set i = index
                    exitwhen true
                endif
            else
                set t = CreateTimer()
                call SaveTimerHandle(data, GetHandleId(t), HVAL_TIMER_HANDLE, t)
                set timers_created = timers_created + 1
                set i = timers_created - 1
                exitwhen true
            endif

            set i = i + 1
        endloop

        call SaveInteger(data, HKEY_FREE_TIMERS_HIDS, i, 0)

        return t
    endfunction

    function FreeTimer_Loop takes nothing returns nothing
        local integer timers_to_free
        local integer t_hid
        local integer t_i
        local integer hid
        local integer index

        set timers_to_free = LoadInteger(data, HKEY_FREETIMER_FUNC_DATA, HVAL_FREETIMER_FUNC_TIMERS_TO_FREE)
        set t_hid = LoadInteger(data, HKEY_FREETIMER_FUNC_DATA, HVAL_FREETIMER_FUNC_DATA_OFFSET + (timers_to_free - 1))
        set t_i = LoadInteger(data, t_hid, HVAL_TIMER_FREETIMER_INDEX)

        set hid = LoadInteger(data, HKEY_FREE_TIMERS_HIDS, t_i)
        if(hid == 0) then
            set index = t_i
        endif

        if(hid != 0) then
            if(last_freed_index < timers_created - 1) then
                set index = last_freed_index + 1
                set hid = LoadInteger(data, HKEY_FREE_TIMERS_HIDS, index)
            endif
        endif

        if(hid != 0) then
            if(last_freed_index > 0) then
                set index = last_freed_index - 1
                set hid = LoadInteger(data, HKEY_FREE_TIMERS_HIDS, index)
            endif
        endif

        if(hid == 0) then
            set timers_free = timers_free + 1
            set timers_to_free = timers_to_free - 1
            call SaveInteger(data, HKEY_FREE_TIMERS_HIDS, index, t_hid)
            call SaveInteger(data, HKEY_FREETIMER_FUNC_DATA, HVAL_FREETIMER_FUNC_TIMERS_TO_FREE, timers_to_free)

            debug call DisplayTextToForce(GetPlayersAll(), "t_hid = " + I2S(t_hid) + "; timers_to_free = " + I2S(timers_to_free) + "; timers_free = " + I2S(timers_free) + "; timers_created = " + I2S(timers_created))

            if(index != 0) then
                set last_freed_index = index
            endif
        else
            set t_i = t_i + 1
            call SaveInteger(data, t_hid, HVAL_TIMER_FREETIMER_INDEX, t_i)
        endif

        if(timers_to_free > 0) then
            call TimerStart(freetimer_func_loop_timer, 0, false, function FreeTimer_Loop)
        endif
    endfunction

    // Add a timer to the freeing queue
    function FreeTimer takes timer t returns nothing
        local integer timers_to_free
        local integer t_hid

        call PauseTimer(t)
        set t_hid = GetHandleId(t)
        set timers_to_free = LoadInteger(data, HKEY_FREETIMER_FUNC_DATA, HVAL_FREETIMER_FUNC_TIMERS_TO_FREE)
        set timers_to_free = timers_to_free + 1
        call SaveInteger(data, t_hid, HVAL_TIMER_FREETIMER_INDEX, 0)
        call SaveInteger(data, HKEY_FREETIMER_FUNC_DATA, HVAL_FREETIMER_FUNC_DATA_OFFSET + (timers_to_free - 1), GetHandleId(t))
        call SaveInteger(data, HKEY_FREETIMER_FUNC_DATA, HVAL_FREETIMER_FUNC_TIMERS_TO_FREE, timers_to_free)

        call TimerStart(freetimer_func_loop_timer, 0, false, function FreeTimer_Loop)
    endfunction

    // Data attachment functions
    function TimerAttachAgentHandle takes timer t, integer i, agent which_agent returns nothing
        call SaveAgentHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_agent)
    endfunction

    function TimerAttachBoolean takes timer t, integer i, boolean value returns nothing
        call SaveBoolean(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, value)
    endfunction

    function TimerAttachButtonHandle takes timer t, integer i, button which_button returns nothing
        call SaveButtonHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_button)
    endfunction

    function TimerAttachDefeatConditionHandle takes timer t, integer i, defeatcondition which_defeatcondition returns nothing
        call SaveDefeatConditionHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_defeatcondition)
    endfunction

    function TimerAttachDestructableHandle takes timer t, integer i, destructable which_destructable returns nothing
        call SaveDestructableHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_destructable)
    endfunction

    function TimerAttachDialogHandle takes timer t, integer i, dialog which_dialog returns nothing
        call SaveDialogHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_dialog)
    endfunction

    function TimerAttachEffectHandle takes timer t, integer i, effect which_effect returns nothing
        call SaveEffectHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_effect)
    endfunction

    function TimerAttachFogModifierHandle takes timer t, integer i, fogmodifier which_fogmodifier returns nothing
        call SaveFogModifierHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_fogmodifier)
    endfunction

    function TimerAttachFogStateHandle takes timer t, integer i, fogstate which_fogstate returns nothing
        call SaveFogStateHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_fogstate)
    endfunction

    function TimerAttachForceHandle takes timer t, integer i, force which_force returns nothing
        call SaveForceHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_force)
    endfunction

    function TimerAttachGroupHandle takes timer t, integer i, group which_group returns nothing
        call SaveGroupHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_group)
    endfunction

    function TimerAttachHashtableHandle takes timer t, integer i, hashtable which_hashtable returns nothing
        call SaveHashtableHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_hashtable)
    endfunction

    function TimerAttachImageHandle takes timer t, integer i, image which_image returns nothing
        call SaveImageHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_image)
    endfunction

    function TimerAttachInteger takes timer t, integer i, integer value returns nothing
        call SaveInteger(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, value)
    endfunction

    function TimerAttachItemHandle takes timer t, integer i, item which_item returns nothing
        call SaveItemHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_item)
    endfunction

    function TimerAttachItemPoolHandle takes timer t, integer i, itempool which_itempool returns nothing
        call SaveItemPoolHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_itempool)
    endfunction

    function TimerAttachLeaderboardHandle takes timer t, integer i, leaderboard which_leaderboard returns nothing
        call SaveLeaderboardHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_leaderboard)
    endfunction

    function TimerAttachLightningHandle takes timer t, integer i, lightning which_lightning returns nothing
        call SaveLightningHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_lightning)
    endfunction

    function TimerAttachLocationHandle takes timer t, integer i, location which_location returns nothing
        call SaveLocationHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_location)
    endfunction

    function TimerAttachMultiboardHandle takes timer t, integer i, multiboard which_multiboard returns nothing
        call SaveMultiboardHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_multiboard)
    endfunction

    function TimerAttachMultiboardItemHandle takes timer t, integer i, multiboarditem which_multiboarditem returns nothing
        call SaveMultiboardItemHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_multiboarditem)
    endfunction

    function TimerAttachPlayerHandle takes timer t, integer i, player which_player returns nothing
        call SavePlayerHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_player)
    endfunction

    function TimerAttachQuestHandle takes timer t, integer i, quest which_quest returns nothing
        call SaveQuestHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_quest)
    endfunction

    function TimerAttachQuestItemHandle takes timer t, integer i, questitem which_questitem returns nothing
        call SaveQuestItemHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_questitem)
    endfunction

    function TimerAttachReal takes timer t, integer i, real value returns nothing
        call SaveReal(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, value)
    endfunction

    function TimerAttachRectHandle takes timer t, integer i, rect which_rect returns nothing
        call SaveRectHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_rect)
    endfunction

    function TimerAttachRegionHandle takes timer t, integer i, region which_region returns nothing
        call SaveRegionHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_region)
    endfunction

    function TimerAttachSoundHandle takes timer t, integer i, sound which_sound returns nothing
        call SaveSoundHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_sound)
    endfunction

    function TimerAttachStr takes timer t, integer i, string value returns nothing
        call SaveStr(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, value)
    endfunction

    function TimerAttachTextTagHandle takes timer t, integer i, texttag which_texttag returns nothing
        call SaveTextTagHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_texttag)
    endfunction

    function TimerAttachTimerDialogHandle takes timer t, integer i, timerdialog which_timerdialog returns nothing
        call SaveTimerDialogHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_timerdialog)
    endfunction

    function TimerAttachTimerHandle takes timer t, integer i, timer which_timer returns nothing
        call SaveTimerHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_timer)
    endfunction

    function TimerAttachTrackableHandle takes timer t, integer i, trackable which_trackable returns nothing
        call SaveTrackableHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_trackable)
    endfunction

    function TimerAttachTriggerActionHandle takes timer t, integer i, triggeraction which_triggeraction returns nothing
        call SaveTriggerActionHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_triggeraction)
    endfunction

    function TimerAttachTriggerConditionHandle takes timer t, integer i, triggercondition which_triggercondition returns nothing
        call SaveTriggerConditionHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_triggercondition)
    endfunction

    function TimerAttachTriggerEventHandle takes timer t, integer i, event which_event returns nothing
        call SaveTriggerEventHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_event)
    endfunction

    function TimerAttachTriggerHandle takes timer t, integer i, trigger which_trigger returns nothing
        call SaveTriggerHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_trigger)
    endfunction

    function TimerAttachUbersplatHandle takes timer t, integer i, ubersplat which_ubersplat returns nothing
        call SaveUbersplatHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_ubersplat)
    endfunction

    function TimerAttachUnitHandle takes timer t, integer i, unit which_unit returns nothing
        call SaveUnitHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_unit)
    endfunction

    function TimerAttachUnitPoolHandle takes timer t, integer i, unitpool which_unitpool returns nothing
        call SaveUnitPoolHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_unitpool)
    endfunction

    function TimerAttachWidgetHandle takes timer t, integer i, widget which_widget returns nothing
        call SaveWidgetHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i, which_widget)
    endfunction

    // Data retrieval functions
    function TimerGetBoolean takes timer t, integer i returns boolean
        return LoadBoolean(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetButtonHandle takes timer t, integer i returns button
        return LoadButtonHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetDefeatConditionHandle takes timer t, integer i returns defeatcondition
        return LoadDefeatConditionHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetDestructableHandle takes timer t, integer i returns destructable
        return LoadDestructableHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetDialogHandle takes timer t, integer i returns dialog
        return LoadDialogHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetEffectHandle takes timer t, integer i returns effect
        return LoadEffectHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetFogModifierHandle takes timer t, integer i returns fogmodifier
        return LoadFogModifierHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetFogStateHandle takes timer t, integer i returns fogstate
        return LoadFogStateHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetForceHandle takes timer t, integer i returns force
        return LoadForceHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetGroupHandle takes timer t, integer i returns group
        return LoadGroupHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetHashtableHandle takes timer t, integer i returns hashtable
        return LoadHashtableHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetImageHandle takes timer t, integer i returns image
        return LoadImageHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetInteger takes timer t, integer i returns integer
        return LoadInteger(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetItemHandle takes timer t, integer i returns item
        return LoadItemHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetItemPoolHandle takes timer t, integer i returns itempool
        return LoadItemPoolHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetLeaderboardHandle takes timer t, integer i returns leaderboard
        return LoadLeaderboardHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetLightningHandle takes timer t, integer i returns lightning
        return LoadLightningHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetLocationHandle takes timer t, integer i returns location
        return LoadLocationHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetMultiboardHandle takes timer t, integer i returns multiboard
        return LoadMultiboardHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetMultiboardItemHandle takes timer t, integer i returns multiboarditem
        return LoadMultiboardItemHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetPlayerHandle takes timer t, integer i returns player
        return LoadPlayerHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetQuestHandle takes timer t, integer i returns quest
        return LoadQuestHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetQuestItemHandle takes timer t, integer i returns questitem
        return LoadQuestItemHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetReal takes timer t, integer i returns real
        return LoadReal(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetRectHandle takes timer t, integer i returns rect
        return LoadRectHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetRegionHandle takes timer t, integer i returns region
        return LoadRegionHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetSoundHandle takes timer t, integer i returns sound
        return LoadSoundHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetStr takes timer t, integer i returns string
        return LoadStr(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetTextTagHandle takes timer t, integer i returns texttag
        return LoadTextTagHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetTimerDialogHandle takes timer t, integer i returns timerdialog
        return LoadTimerDialogHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetTimerHandle takes timer t, integer i returns timer
        return LoadTimerHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetTrackableHandle takes timer t, integer i returns trackable
        return LoadTrackableHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetTriggerActionHandle takes timer t, integer i returns triggeraction
        return LoadTriggerActionHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetTriggerConditionHandle takes timer t, integer i returns triggercondition
        return LoadTriggerConditionHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetTriggerEventHandle takes timer t, integer i returns event
        return LoadTriggerEventHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetTriggerHandle takes timer t, integer i returns trigger
        return LoadTriggerHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetUbersplatHandle takes timer t, integer i returns ubersplat
        return LoadUbersplatHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetUnitHandle takes timer t, integer i returns unit
        return LoadUnitHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetUnitPoolHandle takes timer t, integer i returns unitpool
        return LoadUnitPoolHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction

    function TimerGetWidgetHandle takes timer t, integer i returns widget
        return LoadWidgetHandle(data, GetHandleId(t), HVAL_TIMER_DATA_OFFSET + i)
    endfunction
endlibrary
I would be grateful for any tips and advices.
 

Attachments

  • timers_example_map.w3x
    27.8 KB · Views: 111
Last edited:
Level 29
Joined
Mar 10, 2009
Messages
5,016
struct instance data saved to timerhandle is better to avoid complications...
JASS:
struct T
   //make variables
   static method looper takes nothing returns nothing
      local timer t = GetExpiredTimer()
      local thistype this = LoadInterger(ht,GetHandleId(t),0)
      //conditions here, if it encounters false, then destroy instance and timer
   endmethod

   static method on takes nothing returns nothing
      local thistype this = allocate()
      local timer t = CreateTimer()
      //declare instance
      call SaveInteger(ht,GetHandleId(t),0,this)
      call TimerStart(t,#,true,function thistype.looper)
   endmethod
endstruct

the thing is, if timer is not destroyed when instance is not running then it is a leak...
 
Level 2
Joined
Jul 23, 2010
Messages
11
I've update the library. It's much better now.
Here's an example:
JASS:
library UnitJump initializer Init requires Timers
    globals
        private constant integer DATA_UNIT_HANDLE = 0
        private constant integer DATA_FACING = 1
        private constant integer DATA_TIME = 2
        private constant integer DATA_STEP = 3
        private constant integer DATA_HEIGHT_RATE = 4
        private constant integer DATA_TIMER = 5

        // Step update interval -- the lower the value, the smoother it will look, but
        // will require more processing power
        private constant real step_interval = 10
        // Forward references to functions passed to timers
        private code Move_f
        private code Step_f
        private code Land_f
    endglobals

    // Makes a unit jump
    // [u] - The unit who will perform the jump
    // [distance] - The distance of the jump
    // [height] - The height of the jump
    // [time] - The duration of the jump in milliseconds
    function UnitJump takes unit u, real distance, real height, real time returns nothing
        local timer t
        local real facing_a
        local real height_rate
        local real step

        set facing_a = GetUnitFacing(u)
        // The rate at which the unit will ascend and descend
        set height_rate = (height / (time / 1000)) * 2
        // The distance the unit will move every step_interval
        set step = distance / (time / step_interval)

        // This ability enables non-flying units to change height
        call UnitAddAbility(u, 'Amrf')
        call SetUnitFlyHeight(u, height, height_rate)
        call UnitRemoveAbility(u, 'Amrf')

        // Create a timer that will initiate moving
        set t = NewTimer()
        call TimerAttachUnitHandle(t, DATA_UNIT_HANDLE, u)
        call TimerAttachReal(t, DATA_FACING, facing_a)
        call TimerAttachReal(t, DATA_TIME, time)
        call TimerAttachReal(t, DATA_STEP, step)
        call TimerAttachTimerHandle(t, DATA_TIMER, NewTimer())
        call TimerStart(t, 0, false, Move_f)

        // This timer will execute after half of the [duration] and will
        // begin descending the unit
        set t = NewTimer()
        call TimerAttachUnitHandle(t, DATA_UNIT_HANDLE, u)
        call TimerAttachReal(t, DATA_HEIGHT_RATE, height_rate)
        call TimerStart(t, (time / 1000) / 2, false, Land_f)
    endfunction

    // Set's up a timer which will execute in [step_interval]
    private function Move takes nothing returns nothing
        local timer t
        local timer t_step

        set t = GetExpiredTimer()
        set t_step = TimerGetTimerHandle(t, DATA_TIMER)

        call TimerAttachUnitHandle(t_step, DATA_UNIT_HANDLE, TimerGetUnitHandle(t, DATA_UNIT_HANDLE))
        call TimerAttachReal(t_step, DATA_FACING, TimerGetReal(t, DATA_FACING))
        call TimerAttachReal(t_step, DATA_TIME, TimerGetReal(t, DATA_TIME))
        call TimerAttachReal(t_step, DATA_STEP, TimerGetReal(t, DATA_STEP))
        call TimerAttachTimerHandle(t_step, DATA_TIMER, t)

        call TimerStart(t_step, 0, false, Step_f)
    endfunction

    // Moves the unit for [step] and reduces [time] for [step_interval].
    // If [time] is above zero, call Move again.
    private function Step takes nothing returns nothing
        local timer t
        local timer t_move
        local unit u
        local real facing_a
        local real distance
        local real time
        local real step
        local location u_loc
        local location u_loc_new

        set t = GetExpiredTimer()
        set u = TimerGetUnitHandle(t, DATA_UNIT_HANDLE)
        set facing_a = TimerGetReal(t, DATA_FACING)
        set time = TimerGetReal(t, DATA_TIME)
        set step = TimerGetReal(t, DATA_STEP)

        set u_loc = GetUnitLoc(u)
        set u_loc_new = PolarProjectionBJ(u_loc, step, facing_a)
        call SetUnitPositionLoc(u, u_loc_new)
        call RemoveLocation(u_loc)
        call RemoveLocation(u_loc_new)

        set t_move = TimerGetTimerHandle(t, DATA_TIMER)
        set time = time - step_interval
        if(time > 0) then
            call TimerAttachUnitHandle(t_move, DATA_UNIT_HANDLE, u)
            call TimerAttachReal(t_move, DATA_FACING, facing_a)
            call TimerAttachReal(t_move, DATA_TIME, time)
            call TimerAttachReal(t_move, DATA_STEP, step)
            call TimerAttachTimerHandle(t_move, DATA_TIMER, t)
            call TimerStart(t_move, step_interval / 1000, false, Move_f)
        else
            call FreeTimer(t)
            call FreeTimer(t_move)
        endif
    endfunction

    private function Land takes nothing returns nothing
        local timer t
        local unit u
        local real height_rate

        set t = GetExpiredTimer()
        set u = TimerGetUnitHandle(t, DATA_UNIT_HANDLE)
        set height_rate = TimerGetReal(t, DATA_HEIGHT_RATE)
        call FreeTimer(t)

        call UnitAddAbility(u, 'Amrf')
        call SetUnitFlyHeight(u, 0, height_rate)
        call UnitRemoveAbility(u, 'Amrf')
    endfunction

    private function Init takes nothing returns nothing
        set Move_f = function Move
        set Step_f = function Step
        set Land_f = function Land
    endfunction
endlibrary
I'm not sure if this is JASS section worthy.
 
Status
Not open for further replies.
Top