library BagSystem initializer Init
//
// The_Witcher 's
// Bag System
//
// With this System you're able to give as many bags as you want to EVERY unit you want!
// The unit only needs to have the Inventory(hero) ability for an inventory with 6 slots
// you just need to create two useable items: one for next bag and one for previous bag
//
// you can add and remove bags whenever you want
//
//functions:
// InitBackpackForUnit takes unit u, integer maxbags
// if you want a unit to use bags, you need to init that with this function
//
// AddBagsToUnit takes unit towhich, integer bags
// with this function you can higher or lower the amount of bags of a unit (add a negative number for the 'bags' value to lower it)
// if there are items in the removed bag they will be dropped
//
// RemoveBackpack takes unit u
// this function simply removes every bag from the unit
// if you want the unit to use bags again, you have to use InitBackpackForUnit once again
//
//
// the SETUP part
globals
// this is the rawcode of the item for next bag
private constant integer NEXT_BAG_ID = 'I001'
// this is the rawcode of the item for previous bag
private constant integer PREV_BAG_ID = 'I002'
//false= on previous bag is the number of the previous and on next bag the number of the next bag
//true = on previous and next bag is the number of the current bag
private constant boolean DISPLAY_CURRENT_BAG = true
endglobals
//---------------------------------------------------------
//-----------Don't modify anything below this line---------
//---------------------------------------------------------
private struct data
unit u
integer array first[99]
integer array firstc[99]
integer array second[99]
integer array secondc[99]
integer array third[99]
integer array thirdc[99]
integer array fourth[99]
integer array fourthc[99]
integer max
integer bag = 1
endstruct
globals
private hashtable h = InitHashtable()
private integer Key = StringHash("Data")
endglobals
private function BagChange takes nothing returns nothing
local unit u = GetTriggerUnit()
local data dat = LoadInteger(h,GetHandleId(u),Key)
local item ite = GetManipulatedItem()
local integer itemID = GetItemTypeId(ite)
if (itemID == PREV_BAG_ID) or (itemID == NEXT_BAG_ID) then
call RemoveItem(UnitItemInSlot(u,4))
call RemoveItem(UnitItemInSlot(u,5))
set dat.first[dat.bag] = GetItemTypeId(UnitItemInSlot(u, 0))
set dat.firstc[dat.bag] = GetItemCharges(UnitItemInSlot(u, 0))
call RemoveItem(UnitItemInSlot(u,0))
set dat.second[dat.bag] = GetItemTypeId(UnitItemInSlot(u, 1))
set dat.secondc[dat.bag] = GetItemCharges(UnitItemInSlot(u, 1))
call RemoveItem(UnitItemInSlot(u,1))
set dat.third[dat.bag] = GetItemTypeId(UnitItemInSlot(u, 2))
set dat.thirdc[dat.bag] = GetItemCharges(UnitItemInSlot(u, 2))
call RemoveItem(UnitItemInSlot(u,2))
set dat.fourth[dat.bag] = GetItemTypeId(UnitItemInSlot(u, 3))
set dat.fourthc[dat.bag] = GetItemCharges(UnitItemInSlot(u, 3))
call RemoveItem(UnitItemInSlot(u,3))
if itemID == PREV_BAG_ID then
set dat.bag = dat.bag - 1
if dat.bag <= 0 then
set dat.bag = dat.max
endif
else
set dat.bag = dat.bag + 1
if dat.bag >= dat.max + 1 then
set dat.bag = 1
endif
endif
call UnitAddItemToSlotById(u,dat.first[dat.bag],0)
call SetItemCharges(UnitItemInSlot(u, 0),dat.firstc[dat.bag])
call UnitAddItemToSlotById(u,dat.second[dat.bag],1)
call SetItemCharges(UnitItemInSlot(u, 1),dat.secondc[dat.bag])
call UnitAddItemToSlotById(u,dat.third[dat.bag],2)
call SetItemCharges(UnitItemInSlot(u, 2),dat.thirdc[dat.bag])
call UnitAddItemToSlotById(u,dat.fourth[dat.bag],3)
call SetItemCharges(UnitItemInSlot(u, 3),dat.fourthc[dat.bag])
call UnitAddItemToSlotById(u,PREV_BAG_ID,4)
call UnitAddItemToSlotById(u,NEXT_BAG_ID,5)
if DISPLAY_CURRENT_BAG then
call SetItemCharges(UnitItemInSlot(u, 4),dat.bag)
call SetItemCharges(UnitItemInSlot(u, 5),dat.bag)
else
if dat.bag == dat.max then
call SetItemCharges(UnitItemInSlot(u, 4),dat.bag-1)
call SetItemCharges(UnitItemInSlot(u, 5),1)
elseif dat.bag == 1 then
call SetItemCharges(UnitItemInSlot(u, 4),dat.max)
call SetItemCharges(UnitItemInSlot(u, 5),dat.bag+1)
else
call SetItemCharges(UnitItemInSlot(u, 4),dat.bag-1)
call SetItemCharges(UnitItemInSlot(u, 5),dat.bag+1)
endif
endif
endif
set u = null
set ite = null
endfunction
function InitBackpackForUnit takes unit u, integer maxbags returns nothing
local data dat = data.create()
set dat.u = u
set dat.max = maxbags
call UnitRemoveItemFromSlot(u,4)
call UnitRemoveItemFromSlot(u,5)
call UnitAddItemToSlotById(u,PREV_BAG_ID,4)
call UnitAddItemToSlotById(u,NEXT_BAG_ID,5)
if DISPLAY_CURRENT_BAG then
call SetItemCharges(UnitItemInSlot(u, 4),1)
call SetItemCharges(UnitItemInSlot(u, 5),1)
else
call SetItemCharges(UnitItemInSlot(u, 4),dat.max)
call SetItemCharges(UnitItemInSlot(u, 5),2)
endif
call SaveInteger(h,GetHandleId(u),Key,dat)
endfunction
function AddBagsToUnit takes unit towhich, integer bags returns nothing
local data dat = LoadInteger(h,GetHandleId(towhich),Key)
local integer s = dat.max + bags
local item ite
if s < 0 then
set s = 0
endif
if bags < 0 then
loop
exitwhen dat.max == s
set ite = CreateItem(dat.first[dat.max],GetUnitX(towhich),GetUnitY(towhich))
call SetItemCharges(ite, dat.firstc[dat.max])
set ite = CreateItem(dat.second[dat.max],GetUnitX(towhich),GetUnitY(towhich))
call SetItemCharges(ite, dat.secondc[dat.max])
set ite = CreateItem(dat.third[dat.max],GetUnitX(towhich),GetUnitY(towhich))
call SetItemCharges(ite, dat.thirdc[dat.max])
set ite = CreateItem(dat.fourth[dat.max],GetUnitX(towhich),GetUnitY(towhich))
call SetItemCharges(ite, dat.fourthc[dat.max])
set dat.first[dat.max] = 0
set dat.second[dat.max] = 0
set dat.third[dat.max] = 0
set dat.fourth[dat.max] = 0
set dat.firstc[dat.max] = 0
set dat.secondc[dat.max] = 0
set dat.thirdc[dat.max] = 0
set dat.fourthc[dat.max] = 0
set dat.max = dat.max - 1
endloop
else
set dat.max = dat.max + bags
endif
set ite = null
endfunction
function RemoveBackpack takes unit u returns nothing
local data dat = LoadInteger(h,GetHandleId(u),Key)
local integer i = 0
local integer ii = 0
call RemoveItem(UnitItemInSlot(u, 4))
call RemoveItem(UnitItemInSlot(u, 5))
set ii = 0
loop
exitwhen i > 3
call UnitRemoveItemFromSlot(dat.u,i)
set i = i + 1
endloop
set i = 1
loop
exitwhen i > dat.max
set i = i + 1
call UnitAddItemToSlotById(u,dat.first[i],0)
call SetItemCharges(UnitItemInSlot(u, 0),dat.firstc[i])
call UnitAddItemToSlotById(u,dat.second[i],1)
call SetItemCharges(UnitItemInSlot(u, 1),dat.secondc[i])
call UnitAddItemToSlotById(u,dat.third[i],2)
call SetItemCharges(UnitItemInSlot(u, 2),dat.thirdc[i])
call UnitAddItemToSlotById(u,dat.fourth[i],3)
call SetItemCharges(UnitItemInSlot(u, 3),dat.fourthc[i])
set ii = 0
loop
exitwhen ii == 4
call UnitRemoveItemFromSlot(dat.u,ii)
set ii = ii + 1
endloop
endloop
call dat.destroy()
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger( )
local integer i = 0
loop
call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_USE_ITEM, null)
set i = i + 1
exitwhen i == bj_MAX_PLAYER_SLOTS
endloop
call TriggerAddAction(t, function BagChange)
endfunction
endlibrary