ItemSlot.cpp 2.8 KB

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