- 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.
I would be grateful for any tips and advices.
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
Attachments
Last edited: