#include "ItemStack.h" #include "Item.h" ItemStack::ItemStack( Item* item, int currentSize, int maxSize ) : ReferenceCounter(), item( item ), size( currentSize ), maxSize( maxSize ) {} ItemStack::ItemStack( Item* item, int currentSize ) : ItemStack( item, currentSize, item->getMaxStackSize() ) {} ItemStack::~ItemStack() { if( item ) item->release(); } void ItemStack::setMaxSize( int size ) { maxSize = size; } ItemStack* ItemStack::split( int size ) { size = MIN( size, this->size ); this->size -= size; return new ItemStack( dynamic_cast(item->getThis()), size, item->getMaxStackSize() ); } Item* ItemStack::extractFromStack() { if( size == 0 ) return 0; --size; return item->zItemType()->cloneItem( item ); } bool ItemStack::addToStack( Item* item ) { if( size < maxSize && this->item->canBeStackedWith( item ) ) { ++size; item->release(); return true; } item->release(); return false; } void ItemStack::addItemStack( ItemStack* zItemStack ) { if( zItemStack->zItem()->canBeStackedWith( item ) ) { int numToAdd = MIN( zItemStack->getSize(), maxSize - size ); size += numToAdd; zItemStack->size -= numToAdd; } } bool ItemStack::isStackable( Item* zItem ) const { return item->canBeStackedWith( zItem ); } const Item* ItemStack::zItem() const { return item; } int ItemStack::getSize() const { return size; } int ItemStack::getMaxSize() const { return maxSize; }