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

[Snippet] Unit-Type Indexer

Level 31
Joined
Jul 10, 2007
Messages
6,306
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