• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Snippet] Unit-Type Indexer

JASS:
library UnitTypeIndexer /* v1.0.0.0
*************************************************************************************
*
*   A snippet that indexes unit types of indexed units
*
*************************************************************************************
*   */uses/*
*   
*       */ UnitIndexer /*       hiveworkshop.com/forums/jass-functions-413/unit-indexer-172090/
*       */ Table /*             hiveworkshop.com/forums/jass-functions-413/snippet-new-table-188084/
*
************************************************************************************
*
*   function GetUnitTypeIdIndexById takes integer unitId returns integer
*
*       Retrieves a unit's unit type index by its id (GetUnitId(u))
*
*   function GetUnitTypeIdIndex takes unit u returns integer
*
*       Given a unit, returns the unit's hashed unit type id (u)
*
*   function IndexUnitTypeId takes integer unitTypeId returns integer
*
*       Indexes a unit type id given a unit type id. Slower than
*       GetUnitTypeIdIndex, but useful if working directly with unit type
*       ids rather than units. (GetUnitTypeId(u))
************************************************************************************/
    globals
        private Table t = 0
        private integer o = 0
        private integer array i
    endglobals
    function GetUnitTypeIdIndexById takes integer u returns integer
        return i[u]
    endfunction
    function GetUnitTypeIdIndex takes unit u returns integer
        return i[GetUnitId(u)]
    endfunction
    function IndexUnitTypeId takes integer u returns integer
        local integer z = t[u]
        if (z == 0) then
            set o = o + 1
            set z = o
            set t[u] = z
        endif
        return z
    endfunction
    private function H takes nothing returns boolean
        local integer z = t[GetUnitTypeId(GetIndexedUnit())]
        if (z == 0) then
            set o = o + 1
            set z = o
            set t[GetUnitTypeId(GetIndexedUnit())] = z
        endif
        set i[GetIndexedUnitId()] = z
        return false
    endfunction
    private function D takes nothing returns boolean
        set i[GetIndexedUnitId()] = 0
        return false
    endfunction
    private module Init
        private static method onInit takes nothing returns nothing
            set t = Table.create()
            call RegisterUnitIndexEvent(Condition(function H), UnitIndexer.INDEX)
            call RegisterUnitIndexEvent(Condition(function D), UnitIndexer.DEINDEX)
        endmethod
    endmodule
    private struct Inits extends array
        implement Init
    endstruct
endlibrary
 
Last edited:
Top