library GetLearnedAbilities /* v1.1.0.0
*************************************************************************************
*
* Allows one to retrieve all currently learned abilities of a unit
*
*************************************************************************************
*
* */ requires /*
*
* */ UnitIndexer /* hiveworkshop.com/forums/showthread.php?t=172090
* */ Table /* hiveworkshop.com/forums/showthread.php?t=188084
* */ RegisterPlayerUnitEvent /* hiveworkshop.com/forums/showthread.php?t=203338
*
************************************************************************************
*
* struct LearnedAbilities extends array
*
* static method operator [] takes unit u returns LearnedAbilities
* method operator [] takes integer index returns integer abilityId
*
* method operator count takes nothing returns integer abilityCount
*
***********************************************************************************/
globals
private Table abils
private Table marked
private integer array c
endglobals
private function Index takes nothing returns boolean
set abils[GetIndexedUnitId()] = Table.create()
set marked[GetIndexedUnitId()] = Table.create()
return false
endfunction
private function Deindex takes nothing returns boolean
call abils[GetIndexedUnitId()].destroy()
call marked[GetIndexedUnitId()].destroy()
set c[GetIndexedUnitId()] = 0
return false
endfunction
private function Ability takes nothing returns nothing
local integer i = GetUnitUserData(GetTriggerUnit())
if (not marked[i].boolean.has(GetLearnedSkill())) then
set abils[i][c[i]] = GetLearnedSkill()
set marked[i].boolean[GetLearnedSkill()] = true
set c[i] = c[i] + 1
endif
endfunction
private module M
private static method onInit takes nothing returns nothing
set abils = Table.create()
set marked = Table.create()
call RegisterUnitIndexEvent(Filter(function Index), UnitIndexer.INDEX)
call RegisterUnitIndexEvent(Filter(function Deindex), UnitIndexer.DEINDEX)
call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL, function Ability)
endmethod
endmodule
struct LearnedAbilities extends array
static method operator [] takes unit u returns LearnedAbilities
return GetUnitUserData(u)
endmethod
method operator [] takes integer index returns integer
return abils[this][index]
endmethod
method operator count takes nothing returns integer
local integer i = c[this]
local integer m
loop
exitwhen i == 0
set i = i - 1
set m = abils[this][i]
if GetUnitAbilityLevel(GetUnitById(this), m) == 0 then
call marked[this].remove(m)
set c[this] = c[this] - 1
set abils[this][i] = abils[this][c[this]]
call abils[this].remove(c[this])
endif
endloop
return c[this]
endmethod
implement M
endstruct
endlibrary