#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(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; }