ItemSlot.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "ItemSlot.h"
  2. ItemSlot::ItemSlot( int maxSize, int pullPriority, int pushPriority, int allowedPullSide, int allowedPushSides, bool allowHigherStackSize )
  3. : ReferenceCounter(),
  4. items( 0 ),
  5. maxSize( maxSize ),
  6. allowedPullSide( allowedPullSide ),
  7. allowedPushSides( allowedPushSides ),
  8. pullPriority( pullPriority ),
  9. pushPriority( pushPriority ),
  10. allowHigherStackSize( allowHigherStackSize )
  11. {}
  12. ItemSlot::~ItemSlot()
  13. {
  14. if( items )
  15. items->release();
  16. }
  17. ItemStack* ItemSlot::takeItemsOut( int count, Direction dir )
  18. {
  19. if( !items )
  20. return 0;
  21. if( (dir | allowedPullSide) == allowedPullSide )
  22. {
  23. ItemStack* result = items->split( count );
  24. if( items->getSize() == 0 )
  25. {
  26. items->release();
  27. items = 0;
  28. }
  29. return result;
  30. }
  31. return 0;
  32. }
  33. void ItemSlot::addItems( ItemStack* zStack, Direction dir )
  34. {
  35. if( (dir | allowedPushSides) == allowedPushSides )
  36. {
  37. if( !items )
  38. {
  39. if( allowHigherStackSize )
  40. {
  41. items = zStack->split( maxSize );
  42. items->setMaxSize( maxSize );
  43. }
  44. else
  45. {
  46. items = zStack->split( MIN( maxSize, items->zItem()->getMaxStackSize() ) );
  47. items->setMaxSize( MIN( maxSize, items->zItem()->getMaxStackSize() ) );
  48. }
  49. }
  50. else
  51. items->addItemStack( zStack );
  52. }
  53. }
  54. int ItemSlot::numberOfAddableItems( const ItemStack* zStack, Direction dir ) const
  55. {
  56. if( (dir | allowedPushSides) == allowedPushSides )
  57. {
  58. if( !items )
  59. {
  60. if( allowHigherStackSize )
  61. return maxSize;
  62. else
  63. return MIN( maxSize, zStack->zItem()->getMaxStackSize() );
  64. }
  65. else
  66. return items->getMaxSize() - items->getSize();
  67. }
  68. return 0;
  69. }
  70. const ItemStack* ItemSlot::zStack() const
  71. {
  72. return items;
  73. }
  74. int ItemSlot::getPullPriority() const
  75. {
  76. return pullPriority;
  77. }
  78. int ItemSlot::getPushPriority() const
  79. {
  80. return pushPriority;
  81. }
  82. bool ItemSlot::isFull() const
  83. {
  84. return items ? items->getSize() < items->getMaxSize() : 0;
  85. }