library InventoryRules initializer init
globals
private code on_item_pickup_handler
endglobals
private function init takes nothing returns nothing
local trigger t
// setup the event
set t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
// and its handler
call ExecuteFunc(SCOPE_PRIVATE + "set_on_item_pickup_handler")
call TriggerAddAction(t, on_item_pickup_handler)
call ExecuteFunc(SCOPE_PRIVATE + "carry_1_of_items_init")
endfunction
globals
private integer array carry_1_of_items
private integer carry_1_of_items_count
endglobals
private function carry_1_of_items_init takes nothing returns nothing
set carry_1_of_items[1] = 'ratf' // claws of attack +15
set carry_1_of_items[2] = 'ckng' // crown of kings +5
set carry_1_of_items[3] = 'modt' // mask of death
set carry_1_of_items_count = 3
endfunction
private function on_item_pickup takes nothing returns nothing
local unit u = GetTriggerUnit()
local item picked_it = GetManipulatedItem()
local integer picked_it_id = GetItemTypeId(picked_it)
local item curr_it
local integer curr_it_id
local integer inv_size = UnitInventorySize(u)
local integer i
local integer j
local real x
local real y
set i = 0
loop
exitwhen i >= inv_size
set curr_it = UnitItemInSlot(u, i)
// skip over empty slots and the item we've just picked
if curr_it != null and curr_it != picked_it then
set curr_it_id = GetItemTypeId(curr_it)
// we drop the item we've just picked if we already have an item of the same type
if picked_it_id == curr_it_id then
set j = 1
loop
exitwhen j > carry_1_of_items_count
if curr_it_id == carry_1_of_items[j] then
exitwhen true
endif
set j = j + 1
endloop
// but only if its in the carry-1-of-items list
if j <= carry_1_of_items_count then
set x = GetItemX(picked_it)
set y = GetItemY(picked_it)
// call UnitDropItemPoint(u, picked_it, GetUnitX(u), GetUnitY(u))
// call SetItemPosition(picked_it, x, y)
// call IssueImmediateOrder(u, "stop")
// SetItemPosition seems to remove the item from the unit's inventory on its own
call SetItemPosition(picked_it, x, y)
// TODO: error message, sound?
exitwhen true
endif
endif
//
endif
set i = i + 1
endloop
set u = null
set picked_it = null
set curr_it = null
endfunction
private function set_on_item_pickup_handler takes nothing returns nothing
set on_item_pickup_handler = function on_item_pickup
endfunction
endlibrary