//This is the core of the system. It handles perk indexing in order to allow easy use of arrays elsewhere.
library UnitPerk initializer in
globals
hashtable PerkTable = InitHashtable()
integer array UnitPerkType
unit array UnitPerkOwner
unit array UnitPerkTarget
integer array UnitPerkCount
integer array UnitPerkPointer
integer array UnitPerkNext
integer array UnitPerkPrev
integer array UnDex
integer UnDexes
integer ToDestroy
constant integer PERK_DESTROY = 1
endglobals
function DestroyPerk takes integer index returns nothing
set ToDestroy = index
call TriggerEvaluate(LoadTriggerHandle(PerkTable,UnitPerkType[index],PERK_DESTROY))
endfunction
function InitPerk takes integer perktype,unit target,unit owner returns integer
//Necessary data
local integer ID = GetUnitUserData(target)
//Taking a perk ID from the stack and unallocating the taken ID to prevent overlap
local integer index = UnDex[UnDexes]
set UnDexes = UnDexes - 1
//Initializing the perk
set UnitPerkType[index] = perktype
set UnitPerkOwner[index] = owner
set UnitPerkTarget[index] = target
//Incrementing the buff counter(ability level)
if GetUnitAbilityLevel(target,perktype) > 0 then
call BJDebugMsg(I2S(GetUnitAbilityLevel(target,perktype)))
call IncUnitAbilityLevel(target,perktype)
//call SetUnitAbilityLevel(target,perktype,GetUnitAbilityLevel(target,perktype)+1)
call BJDebugMsg(I2S(GetUnitAbilityLevel(target,perktype)))
//call IncUnitAbilityLevel(target,perktype)
else
call UnitAddAbility(target,perktype)
endif
if UnitPerkCount[ID] == 0 then
//Starting a new doubly linked list
set UnitPerkCount[ID] = 1
set UnitPerkPointer[ID] = index
set UnitPerkNext[index] = index
set UnitPerkPrev[index] = index
else
set UnitPerkCount[ID] = UnitPerkCount[ID] + 1
//Swapping the new perk with the one at the pointer.
set UnitPerkPrev[UnitPerkPointer[ID]] = index
set UnitPerkNext[UnitPerkPrev[UnitPerkPointer[ID]]] = index
//
set UnitPerkPrev[index] = UnitPerkPrev[UnitPerkPointer[ID]]
set UnitPerkNext[index] = UnitPerkPointer[ID]
//Setting the pointer to the new perk.
set UnitPerkPointer[ID] = index
endif
//Return the new perk in order to allow attaching data to it.
debug call BJDebugMsg("Array index of new perk is: "+I2S(index))
return index
endfunction
function ClearPerk takes integer index returns nothing
//Necessary for cleaning up
local unit target = UnitPerkTarget[index]
local integer perktype = UnitPerkType[index]
local integer ID = GetUnitUserData(target)
//Decrementing the buff counter(ability level)
if GetUnitAbilityLevel(target,perktype) > 1 then
call BJDebugMsg(I2S(GetUnitAbilityLevel(target,perktype)))
call SetUnitAbilityLevel(target,perktype,GetUnitAbilityLevel(target,perktype)-1)
call BJDebugMsg(I2S(GetUnitAbilityLevel(target,perktype)))
else
call UnitRemoveAbility(target,perktype)
endif
if UnitPerkCount[ID] == 1 then
set UnitPerkCount[ID] = 0
set UnitPerkPointer[ID] = -1
else
//Pointing next and previous perk to eachother
set UnitPerkPrev[UnitPerkNext[index]] = UnitPerkPrev[index]
set UnitPerkNext[UnitPerkPrev[index]] = UnitPerkNext[index]
set UnitPerkCount[ID] = UnitPerkCount[ID] - 1
set UnitPerkPointer[ID] = UnitPerkNext[index]
endif
set UnDexes = UnDexes + 1
set UnDex[UnDexes] = index
set UnitPerkType[index] = -1
endfunction
private function in takes nothing returns nothing
set UnDexes = 1
loop
set UnDex[UnDexes] = 8191-UnDexes
exitwhen UnDexes == 8190
set UnDexes = UnDexes + 1
endloop
endfunction
endlibrary