ItemSlot.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, zStack->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() : maxSize == 0;
  85. }
  86. int ItemSlot::getFreeSpace() const
  87. {
  88. return items ? items->getMaxSize() - items->getSize() : maxSize;
  89. }
  90. bool ItemSlot::isEmpty() const
  91. {
  92. return !items;
  93. }
  94. int ItemSlot::getNumberOfItems() const
  95. {
  96. return items ? items->getSize() : 0;
  97. }