| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- #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;
- }
- ItemSlotFactory::ItemSlotFactory()
- : ObjectTypeFactory()
- {}
- JSONObjectValidationBuilder* ItemSlotFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- Framework::JSON::JSONArray* defaultSides = new Framework::JSON::JSONArray();
- defaultSides->addValue(new Framework::JSON::JSONString("TOP"));
- defaultSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
- defaultSides->addValue(new Framework::JSON::JSONString("NORTH"));
- defaultSides->addValue(new Framework::JSON::JSONString("EAST"));
- defaultSides->addValue(new Framework::JSON::JSONString("SOUTH"));
- defaultSides->addValue(new Framework::JSON::JSONString("WEST"));
- return builder->withRequiredString("category")
- ->withDefault("Inventory")
- ->finishString()
- ->withRequiredNumber("maxSize")
- ->withDefault(50.0)
- ->finishNumber()
- ->withRequiredNumber("pullPriority")
- ->whichIsGreaterThen(0.0)
- ->withDefault(1.0)
- ->finishNumber()
- ->withRequiredNumber("pushPriority")
- ->whichIsGreaterThen(0.0)
- ->withDefault(1.0)
- ->finishNumber()
- ->withRequiredArray("allowedPullSides")
- ->withDefault(
- dynamic_cast<Framework::JSON::JSONArray*>(defaultSides->getThis()))
- ->addAcceptedStringInArray()
- ->whichIsOneOf({"TOP", "BOTTOM", "NORTH", "EAST", "SOUTH", "WEST"})
- ->finishString()
- ->finishArray()
- ->withRequiredArray("allowedPushSides")
- ->withDefault(defaultSides)
- ->addAcceptedStringInArray()
- ->whichIsOneOf({"TOP", "BOTTOM", "NORTH", "EAST", "SOUTH", "WEST"})
- ->finishString()
- ->finishArray()
- ->withRequiredBool("allowHigherStackSize")
- ->withDefault(false)
- ->finishBool();
- }
- ItemSlot* ItemSlotFactory::fromJson(Framework::JSON::JSONObject* zJson) const
- {
- Framework::Text category
- = zJson->zValue("category")->asString()->getString();
- int maxSize = (int)zJson->zValue("maxSize")->asNumber()->getNumber();
- int pullPriority
- = (int)zJson->zValue("pullPriority")->asNumber()->getNumber();
- int pushPriority
- = (int)zJson->zValue("pushPriority")->asNumber()->getNumber();
- int allowedPullSides = 0;
- int allowedPushSides = 0;
- for (Framework::JSON::JSONValue* side :
- *zJson->zValue("allowedPullSides")->asArray())
- {
- Framework::Text sideText = side->asString()->getString();
- if (sideText.istGleich("TOP"))
- {
- allowedPullSides |= TOP;
- }
- else if (sideText.istGleich("BOTTOM"))
- {
- allowedPullSides |= BOTTOM;
- }
- else if (sideText.istGleich("NORTH"))
- {
- allowedPullSides |= NORTH;
- }
- else if (sideText.istGleich("EAST"))
- {
- allowedPullSides |= EAST;
- }
- else if (sideText.istGleich("SOUTH"))
- {
- allowedPullSides |= SOUTH;
- }
- else if (sideText.istGleich("WEST"))
- {
- allowedPullSides |= WEST;
- }
- }
- for (Framework::JSON::JSONValue* side :
- *zJson->zValue("allowedPushSides")->asArray())
- {
- Framework::Text sideText = side->asString()->getString();
- if (sideText.istGleich("TOP"))
- {
- allowedPushSides |= TOP;
- }
- else if (sideText.istGleich("BOTTOM"))
- {
- allowedPushSides |= BOTTOM;
- }
- else if (sideText.istGleich("NORTH"))
- {
- allowedPushSides |= NORTH;
- }
- else if (sideText.istGleich("EAST"))
- {
- allowedPushSides |= EAST;
- }
- else if (sideText.istGleich("SOUTH"))
- {
- allowedPushSides |= SOUTH;
- }
- else if (sideText.istGleich("WEST"))
- {
- allowedPushSides |= WEST;
- }
- }
- bool allowHigherStackSize
- = zJson->zValue("allowHigherStackSize")->asBool()->getBool();
- return new ItemSlot(category,
- maxSize,
- pullPriority,
- pushPriority,
- allowedPullSides,
- allowedPushSides,
- allowHigherStackSize);
- }
- Framework::JSON::JSONObject* ItemSlotFactory::toJsonObject(
- ItemSlot* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue(
- "category", new Framework::JSON::JSONString(zObject->getName()));
- result->addValue(
- "maxSize", new Framework::JSON::JSONNumber((double)zObject->maxSize));
- result->addValue("pullPriority",
- new Framework::JSON::JSONNumber((double)zObject->getPullPriority()));
- result->addValue("pushPriority",
- new Framework::JSON::JSONNumber((double)zObject->getPushPriority()));
- Framework::JSON::JSONArray* allowedPullSides
- = new Framework::JSON::JSONArray();
- if (zObject->allowedPullSide & TOP)
- allowedPullSides->addValue(new Framework::JSON::JSONString("TOP"));
- if (zObject->allowedPullSide & BOTTOM)
- allowedPullSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
- if (zObject->allowedPullSide & NORTH)
- allowedPullSides->addValue(new Framework::JSON::JSONString("NORTH"));
- if (zObject->allowedPullSide & EAST)
- allowedPullSides->addValue(new Framework::JSON::JSONString("EAST"));
- if (zObject->allowedPullSide & SOUTH)
- allowedPullSides->addValue(new Framework::JSON::JSONString("SOUTH"));
- if (zObject->allowedPullSide & WEST)
- allowedPullSides->addValue(new Framework::JSON::JSONString("WEST"));
- result->addValue("allowedPullSides", allowedPullSides);
- Framework::JSON::JSONArray* allowedPushSides
- = new Framework::JSON::JSONArray();
- if (zObject->allowedPushSides & TOP)
- allowedPushSides->addValue(new Framework::JSON::JSONString("TOP"));
- if (zObject->allowedPushSides & BOTTOM)
- allowedPullSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
- if (zObject->allowedPushSides & NORTH)
- allowedPushSides->addValue(new Framework::JSON::JSONString("NORTH"));
- if (zObject->allowedPushSides & EAST)
- allowedPushSides->addValue(new Framework::JSON::JSONString("EAST"));
- if (zObject->allowedPushSides & SOUTH)
- allowedPushSides->addValue(new Framework::JSON::JSONString("SOUTH"));
- if (zObject->allowedPushSides & WEST)
- allowedPushSides->addValue(new Framework::JSON::JSONString("WEST"));
- result->addValue("allowedPushSides", allowedPushSides);
- result->addValue("allowHigherStackSize",
- new Framework::JSON::JSONBool(zObject->allowHigherStackSize));
- return result;
- }
|