module ItemStorageHelper
def self.pbQuantity(items,maxsize,item)
ret=0
for i in 0...maxsize
itemslot=items[i]
if itemslot && itemslot[0]==item
ret+=itemslot[1]
end
end
return ret
end
def self.pbDeleteItem(items,maxsize,item,qty)
raise "Invalid value for qty: #{qty}" if qty<0
return true if qty==0
ret=false
for i in 0...maxsize
itemslot=items[i]
if itemslot && itemslot[0]==item
amount=[qty,itemslot[1]].min
itemslot[1]-=amount
qty-=amount
items[i]=nil if itemslot[1]==0
if qty==0
ret=true
break
end
end
end
items.compact!
return ret
end
def self.pbCanStore?(items,maxsize,maxPerSlot,item,qty)
raise "Invalid value for qty: #{qty}" if qty<0
return true if qty==0
for i in 0...maxsize
itemslot=items[i]
if !itemslot
qty-=[qty,maxPerSlot].min
return true if qty==0
elsif itemslot[0]==item && itemslot[1]<maxPerSlot
newamt=itemslot[1]
newamt=[newamt+qty,maxPerSlot].min
qty-=(newamt-itemslot[1])
return true if qty==0
end
end
return false
end
def self.pbStoreItem(items,maxsize,maxPerSlot,item,qty)
raise "Invalid value for qty: #{qty}" if qty<0
return true if qty==0
for i in 0...maxsize
itemslot=items[i]
if !itemslot
items[i]=[item,[qty,maxPerSlot].min]
qty-=items[i][1]
return true if qty==0
elsif itemslot[0]==item && itemslot[1]<maxPerSlot
newamt=itemslot[1]
newamt=[newamt+qty,maxPerSlot].min
qty-=(newamt-itemslot[1])
itemslot[1]=newamt
return true if qty==0
end
end
return false
end
end
####################################
####################################
class GenericItem
attr_reader :id,:type
def initialize(id,type)
@id=id
@type=type
end
def ==(other)
return other.id==@id && other.type==@type
end
end
class ItemStorage
MAXSIZE=100
MAXPERSLOT=99
def initialize
@items=[]
end
def each
@items.each {|i| yield i }
end
def toGenericItem(item)
case item
when RPG::Item
return GenericItem(item.id,0)
when RPG::Weapon
return GenericItem(item.id,1)
when RPG::Armor
return GenericItem(item.id,2)
else
return nil
end
end
# _item_ is RPG::Item, RPG::Weapon, or RPG::Armor
def quantity(item)
genericItem=toGenericItem(item)
return ItemStorageHelper.pbQuantity(@items,MAXSIZE,genericItem(item))
end
# _item_ is RPG::Item, RPG::Weapon, or RPG::Armor
def deposit(item,count=1)
genericItem=toGenericItem(item)
count=[$game_party.item_number(item),count].min
if count>0
$game_party.lose_item(item,count)
return ItemStorageHelper.pbStoreItem(
@items,MAXSIZE,MAXPERSLOT,genericItem,count)
end
end
# _item_ is RPG::Item, RPG::Weapon, or RPG::Armor
def withdraw(item,count=1)
genericItem=toGenericItem(item)
count=[quantity(item),count].min
if count>0
$game_party.gain_item(item,count)
return ItemStorageHelper.pbDeleteItem(
@items,MAXSIZE,toGenericItem(item),count)
end
end
end