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

ExtensionMethods

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
JASS:
/*****************************************************************************
*
*    ExtensionMethods
*
*    General purpose functions that extend native jass interface.
*
******************************************************************************
*
*    Item handle extension methods:
*
*       function GetUnitItemCount takes unit whichUnit returns integer
*          Returns the number of items equipped.
*
*       function IsUnitInventoryFull takes unit whichUnit returns boolean
*          Checks if unit inventory is full.
*
*       function GetUnitItemSlot takes unit whichUnit, item whichItem returns integer
*          Retrieves slot number of specified item equiped by unit whichUnit or -1 if not found.
*
*       function IsItemAlive takes item whichItem returns boolean
*          Returns value indicating whether specified item is alive.
*
*       function IsItemPickupable takes item whichItem returns boolean
*          Returns value indicating whether specified item can be picked up.
*
*****************************************************************************/
library ExtensionMethods

function GetUnitItemCount takes unit whichUnit returns integer
    local integer size = UnitInventorySize(whichUnit)
    local integer slot = 0
    local integer result = 0
    loop
        exitwhen slot >= size
        if UnitItemInSlot(whichUnit, slot) != null then
            set result = result + 1
        endif
        set slot = slot + 1
    endloop

    return result
endfunction

function IsUnitInventoryFull takes unit whichUnit returns boolean
    return GetUnitItemCount(whichUnit) == UnitInventorySize(whichUnit)
endfunction

function GetUnitItemSlot takes unit whichUnit, item whichItem returns integer
    local integer slot = 0
    local integer size

    if UnitHasItem(whichUnit, whichItem) then
        set size = UnitInventorySize(whichUnit)
        loop
            if UnitItemInSlot(whichUnit, slot) == whichItem then
                return slot
            endif
            set slot = slot + 1
            exitwhen slot >= size
        endloop
    endif

    return -1 // NOT_FOUND
endfunction

function IsItemAlive takes item whichItem returns boolean
    return GetItemTypeId(whichItem) != 0 and GetWidgetLife(whichItem) > 0.405
endfunction

function IsItemPickupable takes item whichItem returns boolean
    return IsItemAlive(whichItem) and not IsItemOwned(whichItem) and IsItemVisible(whichItem)
endfunction

endlibrary
Last edited:
Top