#include "Recipie.h" #include "CraftingStorage.h" #include "Item.h" Recipie::Recipie() : ReferenceCounter() {} void Recipie::addOutput(Item* item, int amount) { outputs.add(item); outputAmount.add(amount); } Framework::Array Recipie::getOutput(CraftingStorage* zStorage) const { Framework::Array result; int index = 0; for (const Item* output : outputs) { ItemInfo info; info.count = outputAmount.get(index); info.type = output->zItemType()->getId(); info.hp = output->getHp(); info.durability = output->getDurability(); info.maxHp = output->getMaxHp(); info.maxDurability = output->getMaxDurability(); result.add(info); index++; } return result; } bool Recipie::createsOutput(ItemType* zType) { for (const Item* output : outputs) { if (output->zItemType() == zType) { return 1; } } return 0; } UnshapedRecipie::UnshapedRecipie() : Recipie() {} void UnshapedRecipie::addIngredient( ItemFilter* filter, int amount, ItemModifier* modifier) { filters.add(filter); modifiers.add(modifier); inputAmount.add(amount); } bool UnshapedRecipie::testApplicability(CraftingStorage* zStorage) { for (int i = 0; i < outputs.getEintragAnzahl(); i++) { if (!zStorage->hasFreeSpace(outputs.z(i), outputAmount.get(i))) return 0; } return zStorage->isAllAvailable(filters, inputAmount); } void UnshapedRecipie::apply(CraftingStorage* zStorage) { zStorage->consume(filters, modifiers, inputAmount); for (int i = 0; i < outputs.getEintragAnzahl(); i++) { ItemStack* stack = new ItemStack(outputs.z(i)->zItemType()->cloneItem(outputs.z(i)), outputAmount.get(i)); zStorage->addCraftingResult(stack); stack->release(); } } Framework::Text UnshapedRecipie::getRecipieUIML() { Framework::Text result = ""; for (int i = 0; i < filters.getEintragAnzahl(); i++) { result += ""; result += filters.z(i)->getImageUIML(); result += ""; result += filters.z(i)->getLogicUIML(); result += ""; // todo: add modifiers } result += ""; for (int i = 0; i < outputs.getEintragAnzahl(); i++) { result += "zItemType()->getId(); result += "\" hp=\""; result += outputs.z(i)->getHp(); result += "\" durability=\""; result += outputs.z(i)->getDurability(); result += "\" maxHp=\""; result += outputs.z(i)->getMaxHp(); result += "\" maxDurability=\""; result += outputs.z(i)->getMaxDurability(); result += "\">"; result += outputs.z(i)->getTooltipUIML(); result += ""; } result += ""; return result; } ShapedRecipie::ShapedRecipie(int width, int height) : ReferenceCounter(), width(width), height(height) { for (int i = 0; i < width * height; i++) { filters.add(0); inputAmount.add(0); modifiers.add(0); } } void ShapedRecipie::setIngredient( int x, int y, ItemFilter* filter, ItemModifier* modifier) { filters.set(filter, width * y + x); inputAmount.set(1, width * y + x); // TODO: make this configurable modifiers.set(modifier, width * y + x); } bool ShapedRecipie::testApplicability(CraftingStorage* zStorage) { ShapedCraftingStorage* zShapedStorage = dynamic_cast(zStorage); if (!zShapedStorage) return 0; for (int i = 0; i < outputs.getEintragAnzahl(); i++) { if (!zShapedStorage->hasFreeSpace(outputs.z(i), outputAmount.get(i))) return 0; } return zShapedStorage->isAllAvailable(filters, inputAmount, width, height); } void ShapedRecipie::apply(CraftingStorage* zStorage) { ShapedCraftingStorage* zShapedStorage = dynamic_cast(zStorage); zShapedStorage->consume(filters, modifiers, inputAmount, width, height); for (int i = 0; i < outputs.getEintragAnzahl(); i++) { ItemStack* stack = new ItemStack(outputs.z(i)->zItemType()->cloneItem(outputs.z(i)), outputAmount.get(i)); zStorage->addCraftingResult(stack); stack->release(); } } Framework::Text ShapedRecipie::getRecipieUIML() { Framework::Text result = ""; for (int i = 0; i < filters.getEintragAnzahl(); i++) { result += ""; result += filters.z(i)->getImageUIML(); result += ""; result += filters.z(i)->getLogicUIML(); result += ""; // todo: add modifiers } result += ""; for (int i = 0; i < outputs.getEintragAnzahl(); i++) { result += "zItemType()->getId(); result += "\" hp=\""; result += outputs.z(i)->getHp(); result += "\" durability=\""; result += outputs.z(i)->getDurability(); result += "\" maxHp=\""; result += outputs.z(i)->getMaxHp(); result += "\" maxDurability=\""; result += outputs.z(i)->getMaxDurability(); result += "\">"; result += outputs.z(i)->getTooltipUIML(); result += ""; } result += ""; return result; }