123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #include "ItemSlot.h"
- #include "ItemStack.h"
- ItemSlot::ItemSlot(Framework::Text name,
- int maxSize,
- int pullPriority,
- int pushPriority,
- int allowedPullSide,
- int allowedPushSides,
- bool allowHigherStackSize)
- : ReferenceCounter(),
- items(0),
- maxSize(maxSize),
- allowedPullSide(allowedPullSide),
- allowedPushSides(allowedPushSides),
- pullPriority(pullPriority),
- pushPriority(pushPriority),
- allowHigherStackSize(allowHigherStackSize),
- name(name),
- id(0)
- {}
- ItemSlot::~ItemSlot()
- {
- if (items) items->release();
- }
- void ItemSlot::setId(int id)
- {
- this->id = id;
- }
- ItemStack* ItemSlot::takeItemsOut(int count, Direction dir)
- {
- if (!items) return 0;
- if ((dir | allowedPullSide) == allowedPullSide)
- {
- ItemStack* result = items->split(count);
- if (items->getSize() == 0)
- {
- items->release();
- items = 0;
- }
- return result;
- }
- return 0;
- }
- void ItemSlot::addItems(ItemStack* zStack, Direction dir)
- {
- if ((dir | allowedPushSides) == allowedPushSides)
- {
- if (!items)
- {
- if (allowHigherStackSize)
- {
- items = zStack->split(maxSize);
- items->setMaxSize(maxSize);
- }
- else
- {
- items = zStack->split(
- MIN(maxSize, zStack->zItem()->getMaxStackSize()));
- items->setMaxSize(
- MIN(maxSize, items->zItem()->getMaxStackSize()));
- }
- }
- else
- items->addItemStack(zStack);
- }
- }
- void ItemSlot::update() {
- if (items && items->getSize() == 0)
- {
- items->release();
- items = 0;
- }
- }
- int ItemSlot::numberOfAddableItems(const ItemStack* zStack, Direction dir) const
- {
- if ((dir | allowedPushSides) == allowedPushSides)
- {
- if (!items)
- {
- if (allowHigherStackSize)
- return maxSize;
- else
- return MIN(maxSize, zStack->zItem()->getMaxStackSize());
- }
- else if (zStack->zItem()
- && items->zItem()->canBeStackedWith(zStack->zItem()))
- return items->getMaxSize() - items->getSize();
- }
- return 0;
- }
- const ItemStack* ItemSlot::zStack() const
- {
- return items;
- }
- int ItemSlot::getPullPriority() const
- {
- return pullPriority;
- }
- int ItemSlot::getPushPriority() const
- {
- return pushPriority;
- }
- bool ItemSlot::isFull() const
- {
- return items ? items->getSize() >= items->getMaxSize() : maxSize == 0;
- }
- int ItemSlot::getFreeSpace() const
- {
- return items ? items->getMaxSize() - items->getSize() : maxSize;
- }
- bool ItemSlot::isEmpty() const
- {
- return !items;
- }
- int ItemSlot::getNumberOfItems() const
- {
- return items ? items->getSize() : 0;
- }
- const Framework::Text& ItemSlot::getName() const
- {
- return name;
- }
- int ItemSlot::getId() const
- {
- return id;
- }
|