library MaxItemStacks initializer Init
globals
// This dummy is created and hidden once. It requires an inventory with at least 2 slots.
private constant integer DUMMY_UNIT_TYPE_MAX_CHECKS = 'Hpal'
// Warcraft III has a limit of number of stacks for the field "Stats - Max Stacks".
private constant integer MAX_STACKS_ALLOWED = 1000
private unit stackItemDummy = null
private integer stackCounter = 0
private hashtable stackHashTable = InitHashtable()
endglobals
public function GetStackItemDummy takes nothing returns unit
return stackItemDummy
endfunction
public function GetMaxStacksByItemTypeId takes integer itemTypeId returns integer
local integer i = 0
local item tmpItem = null
if (HaveSavedInteger(stackHashTable, itemTypeId, 0)) then
return LoadInteger(stackHashTable, itemTypeId, 0)
endif
set stackCounter = 1
set tmpItem = CreateItem(itemTypeId, 0.0, 0.0)
call SetItemCharges(tmpItem, 1)
call UnitAddItem(stackItemDummy, tmpItem)
set i = 1
loop
set tmpItem = CreateItem(itemTypeId, 0.0, 0.0)
call SetItemCharges(tmpItem, 1)
call UnitAddItem(stackItemDummy, tmpItem)
exitwhen (stackCounter <= i)
set i = i + 1
exitwhen (i >= MAX_STACKS_ALLOWED)
endloop
if (UnitItemInSlot(stackItemDummy, 0) != null) then
call RemoveItem(UnitItemInSlot(stackItemDummy, 0))
endif
if (UnitItemInSlot(stackItemDummy, 1) != null) then
call RemoveItem(UnitItemInSlot(stackItemDummy, 1))
endif
call SaveInteger(stackHashTable, itemTypeId, 0, stackCounter)
return stackCounter
endfunction
private function TriggerActionStack takes nothing returns nothing
set stackCounter = stackCounter + 1
endfunction
private function Init takes nothing returns nothing
set stackItemDummy = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), DUMMY_UNIT_TYPE_MAX_CHECKS, 0.0, 0.0, 0.0)
call SetUnitInvulnerable(stackItemDummy, true)
if (IsUnitType(stackItemDummy, UNIT_TYPE_HERO)) then
call SuspendHeroXP(stackItemDummy, true)
endif
call SetUnitUseFood(stackItemDummy, false)
call ShowUnit(stackItemDummy, false)
call BlzSetUnitWeaponBooleanField(stackItemDummy, UNIT_WEAPON_BF_ATTACKS_ENABLED, 0, false)
call BlzSetUnitWeaponBooleanField(stackItemDummy, UNIT_WEAPON_BF_ATTACKS_ENABLED, 1, false)
call TriggerRegisterUnitEvent(stackItemTrigger, stackItemDummy, EVENT_UNIT_STACK_ITEM)
call TriggerAddAction(stackItemTrigger, function TriggerActionStack)
endfunction
endlibrary