123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #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<ItemInfo> Recipie::getOutput(CraftingStorage* zStorage) const
- {
- Framework::Array<ItemInfo> 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 = "<recipie type=\"unshaped\"><ingredients>";
- for (int i = 0; i < filters.getEintragAnzahl(); i++)
- {
- result += "<ingredient amount=\"";
- result += inputAmount.get(i);
- result += "\">";
- if (filters.z(i))
- {
- result += "<logic>";
- result += filters.z(i)->getLogicUIML();
- result += "</logic>";
- }
- result += "</ingredient>";
- // TODO: add modifiers
- }
- result += "</ingredients><outputs>";
- for (int i = 0; i < outputs.getEintragAnzahl(); i++)
- {
- result += "<output amount=\"";
- result += outputAmount.get(i);
- result += "\" itemType=\"";
- result += outputs.z(i)->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 += "</output>";
- }
- result += "</outputs></recipie>";
- 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<ShapedCraftingStorage*>(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<ShapedCraftingStorage*>(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 = "<recipie type=\"shaped\" width=\"";
- result += width;
- result += "\" height=\"";
- result += height;
- result += "\"><ingredients>";
- for (int i = 0; i < filters.getEintragAnzahl(); i++)
- {
- result += "<ingredient amount=\"";
- result += inputAmount.get(i);
- result += "\">";
- if (filters.z(i))
- {
- result += "<logic>";
- result += filters.z(i)->getLogicUIML();
- result += "</logic>";
- }
- result += "</ingredient>";
- // TODO: add modifiers
- }
- result += "</ingredients><outputs>";
- for (int i = 0; i < outputs.getEintragAnzahl(); i++)
- {
- result += "<output amount=\"";
- result += outputAmount.get(i);
- result += "\" itemType=\"";
- result += outputs.z(i)->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 += "</output>";
- }
- result += "</outputs></recipie>";
- return result;
- }
|