ItemSlot.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. void ItemSlot::update() {
  68. if (items && items->getSize() == 0)
  69. {
  70. items->release();
  71. items = 0;
  72. }
  73. }
  74. int ItemSlot::numberOfAddableItems(const ItemStack* zStack, Direction dir) const
  75. {
  76. if ((dir | allowedPushSides) == allowedPushSides)
  77. {
  78. if (!items)
  79. {
  80. if (allowHigherStackSize)
  81. return maxSize;
  82. else
  83. return MIN(maxSize, zStack->zItem()->getMaxStackSize());
  84. }
  85. else if (zStack->zItem()
  86. && items->zItem()->canBeStackedWith(zStack->zItem()))
  87. return items->getMaxSize() - items->getSize();
  88. }
  89. return 0;
  90. }
  91. const ItemStack* ItemSlot::zStack() const
  92. {
  93. return items;
  94. }
  95. int ItemSlot::getPullPriority() const
  96. {
  97. return pullPriority;
  98. }
  99. int ItemSlot::getPushPriority() const
  100. {
  101. return pushPriority;
  102. }
  103. bool ItemSlot::isFull() const
  104. {
  105. return items ? items->getSize() >= items->getMaxSize() : maxSize == 0;
  106. }
  107. int ItemSlot::getFreeSpace() const
  108. {
  109. return items ? items->getMaxSize() - items->getSize() : maxSize;
  110. }
  111. bool ItemSlot::isEmpty() const
  112. {
  113. return !items;
  114. }
  115. int ItemSlot::getNumberOfItems() const
  116. {
  117. return items ? items->getSize() : 0;
  118. }
  119. const Framework::Text& ItemSlot::getName() const
  120. {
  121. return name;
  122. }
  123. int ItemSlot::getId() const
  124. {
  125. return id;
  126. }