Recipie.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "Recipie.h"
  2. #include "CraftingStorage.h"
  3. Recipie::Recipie()
  4. : ReferenceCounter()
  5. {}
  6. void Recipie::addIngredient(ItemFilter* filter, int amount)
  7. {
  8. filters.add(filter);
  9. inputAmount.add(amount);
  10. }
  11. void Recipie::addOutput(Item* item, int amount)
  12. {
  13. outputs.add(item);
  14. outputAmount.add(amount);
  15. }
  16. bool Recipie::testApplicability(CraftingStorage* zStorage)
  17. {
  18. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  19. {
  20. if (!zStorage->hasFreeSpace(outputs.z(i), outputAmount.get(i)))
  21. return 0;
  22. }
  23. return zStorage->isAllAvailable(filters, inputAmount);
  24. }
  25. void Recipie::apply(CraftingStorage* zStorage)
  26. {
  27. zStorage->consume(filters, inputAmount);
  28. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  29. {
  30. ItemStack* stack = new ItemStack(outputs.z(i)->zItemType()->cloneItem(outputs.z(i)), outputAmount.get(i));
  31. zStorage->addCraftingResult(stack);
  32. stack->release();
  33. }
  34. }
  35. ShapedRecipie::ShapedRecipie(int width, int height)
  36. :ReferenceCounter(),
  37. width(width),
  38. height(height),
  39. output(0),
  40. outputAmount(0)
  41. {
  42. for (int i = 0; i < width * height; i++)
  43. filters.add(0);
  44. }
  45. ShapedRecipie::~ShapedRecipie()
  46. {
  47. if (output)
  48. output->release();
  49. }
  50. void ShapedRecipie::setIngredient(int x, int y, ItemFilter* filter)
  51. {
  52. filters.set(filter, width * x + y);
  53. }
  54. void ShapedRecipie::setOutput(Item* item, int amount)
  55. {
  56. if (output)
  57. output->release();
  58. output = item;
  59. outputAmount = amount;
  60. }
  61. bool ShapedRecipie::testApplicability(ShapedCraftingStorage* zStorage)
  62. {
  63. return zStorage->isAllAvailable(filters, width, height) && zStorage->hasFreeSpace(output, outputAmount);
  64. }
  65. void ShapedRecipie::apply(ShapedCraftingStorage* zStorage)
  66. {
  67. zStorage->consume(filters, width, height);
  68. ItemStack* stack = new ItemStack(output->zItemType()->cloneItem(output), outputAmount);
  69. zStorage->addCraftingResult(stack);
  70. stack->release();
  71. }
  72. Framework::Array<ItemInfo> ShapedRecipie::getOutput(ShapedCraftingStorage* zStorage)
  73. {
  74. Framework::Array<ItemInfo> result;
  75. ItemInfo info;
  76. info.count = outputAmount;
  77. info.type = output->zItemType()->getId();
  78. info.damage = output->getDamage();
  79. info.durability = output->getDurability();
  80. info.maxDamage = output->getMaxDamage();
  81. info.maxDurability = output->getMaxDurability();
  82. result.add(info);
  83. return result;
  84. }