ItemSlot.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "ItemSlot.h"
  2. ItemSlot::ItemSlot(Framework::Text name, 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. name(name),
  12. id(0)
  13. {}
  14. ItemSlot::~ItemSlot()
  15. {
  16. if (items)
  17. items->release();
  18. }
  19. void ItemSlot::setId(int id)
  20. {
  21. this->id = id;
  22. }
  23. ItemStack* ItemSlot::takeItemsOut(int count, Direction dir)
  24. {
  25. if (!items)
  26. return 0;
  27. if ((dir | allowedPullSide) == allowedPullSide)
  28. {
  29. ItemStack* result = items->split(count);
  30. if (items->getSize() == 0)
  31. {
  32. items->release();
  33. items = 0;
  34. }
  35. return result;
  36. }
  37. return 0;
  38. }
  39. void ItemSlot::addItems(ItemStack* zStack, Direction dir)
  40. {
  41. if ((dir | allowedPushSides) == allowedPushSides)
  42. {
  43. if (!items)
  44. {
  45. if (allowHigherStackSize)
  46. {
  47. items = zStack->split(maxSize);
  48. items->setMaxSize(maxSize);
  49. }
  50. else
  51. {
  52. items = zStack->split(MIN(maxSize, zStack->zItem()->getMaxStackSize()));
  53. items->setMaxSize(MIN(maxSize, items->zItem()->getMaxStackSize()));
  54. }
  55. }
  56. else
  57. items->addItemStack(zStack);
  58. }
  59. }
  60. int ItemSlot::numberOfAddableItems(const ItemStack* zStack, Direction dir) const
  61. {
  62. if ((dir | allowedPushSides) == allowedPushSides)
  63. {
  64. if (!items)
  65. {
  66. if (allowHigherStackSize)
  67. return maxSize;
  68. else
  69. return MIN(maxSize, zStack->zItem()->getMaxStackSize());
  70. }
  71. else if(zStack->zItem() && items->zItem()->canBeStackedWith(zStack->zItem()))
  72. return items->getMaxSize() - items->getSize();
  73. }
  74. return 0;
  75. }
  76. const ItemStack* ItemSlot::zStack() const
  77. {
  78. return items;
  79. }
  80. int ItemSlot::getPullPriority() const
  81. {
  82. return pullPriority;
  83. }
  84. int ItemSlot::getPushPriority() const
  85. {
  86. return pushPriority;
  87. }
  88. bool ItemSlot::isFull() const
  89. {
  90. return items ? items->getSize() >= items->getMaxSize() : maxSize == 0;
  91. }
  92. int ItemSlot::getFreeSpace() const
  93. {
  94. return items ? items->getMaxSize() - items->getSize() : maxSize;
  95. }
  96. bool ItemSlot::isEmpty() const
  97. {
  98. return !items;
  99. }
  100. int ItemSlot::getNumberOfItems() const
  101. {
  102. return items ? items->getSize() : 0;
  103. }
  104. const Framework::Text& ItemSlot::getName() const
  105. {
  106. return name;
  107. }
  108. int ItemSlot::getId() const
  109. {
  110. return id;
  111. }