ItemSlot.cpp 2.8 KB

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