ItemSlot.cpp 2.1 KB

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