123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #include "ItemSlot.h"
- ItemSlot::ItemSlot( int maxSize, int pullPriority, int pushPriority, int allowedPullSide, int allowedPushSides, bool allowHigherStackSize )
- : ReferenceCounter(),
- items( 0 ),
- maxSize( maxSize ),
- allowedPullSide( allowedPullSide ),
- allowedPushSides( allowedPushSides ),
- pullPriority( pullPriority ),
- pushPriority( pushPriority ),
- allowHigherStackSize( allowHigherStackSize )
- {}
- ItemSlot::~ItemSlot()
- {
- if( items )
- items->release();
- }
- ItemStack* ItemSlot::takeItemsOut( int count, Direction dir )
- {
- if( !items )
- return 0;
- if( (dir | allowedPullSide) == allowedPullSide )
- {
- ItemStack* result = items->split( count );
- if( items->getSize() == 0 )
- {
- items->release();
- items = 0;
- }
- return result;
- }
- return 0;
- }
- void ItemSlot::addItems( ItemStack* zStack, Direction dir )
- {
- if( (dir | allowedPushSides) == allowedPushSides )
- {
- if( !items )
- {
- if( allowHigherStackSize )
- {
- items = zStack->split( maxSize );
- items->setMaxSize( maxSize );
- }
- else
- {
- items = zStack->split( MIN( maxSize, zStack->zItem()->getMaxStackSize() ) );
- items->setMaxSize( MIN( maxSize, items->zItem()->getMaxStackSize() ) );
- }
- }
- else
- items->addItemStack( zStack );
- }
- }
- int ItemSlot::numberOfAddableItems( const ItemStack* zStack, Direction dir ) const
- {
- if( (dir | allowedPushSides) == allowedPushSides )
- {
- if( !items )
- {
- if( allowHigherStackSize )
- return maxSize;
- else
- return MIN( maxSize, zStack->zItem()->getMaxStackSize() );
- }
- else
- return items->getMaxSize() - items->getSize();
- }
- return 0;
- }
- const ItemStack* ItemSlot::zStack() const
- {
- return items;
- }
- int ItemSlot::getPullPriority() const
- {
- return pullPriority;
- }
- int ItemSlot::getPushPriority() const
- {
- return pushPriority;
- }
- bool ItemSlot::isFull() const
- {
- return items ? items->getSize() >= items->getMaxSize() : maxSize == 0;
- }
- int ItemSlot::getFreeSpace() const
- {
- return items ? items->getMaxSize() - items->getSize() : maxSize;
- }
- bool ItemSlot::isEmpty() const
- {
- return !items;
- }
- int ItemSlot::getNumberOfItems() const
- {
- return items ? items->getSize() : 0;
- }
|