Recipie.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. {
  44. filters.add(0);
  45. modifiers.add(0);
  46. }
  47. }
  48. ShapedRecipie::~ShapedRecipie()
  49. {
  50. if (output)
  51. output->release();
  52. }
  53. void ShapedRecipie::setIngredient(int x, int y, ItemFilter* filter, ItemModifier* modifier)
  54. {
  55. filters.set(filter, width * y + x);
  56. modifiers.set(modifier, width * y + x);
  57. }
  58. void ShapedRecipie::setOutput(Item* item, int amount)
  59. {
  60. if (output)
  61. output->release();
  62. output = item;
  63. outputAmount = amount;
  64. }
  65. bool ShapedRecipie::testApplicability(ShapedCraftingStorage* zStorage)
  66. {
  67. return zStorage->isAllAvailable(filters, width, height) && zStorage->hasFreeSpace(output, outputAmount);
  68. }
  69. void ShapedRecipie::apply(ShapedCraftingStorage* zStorage)
  70. {
  71. zStorage->consume(filters, modifiers, width, height);
  72. ItemStack* stack = new ItemStack(output->zItemType()->cloneItem(output), outputAmount);
  73. zStorage->addCraftingResult(stack);
  74. stack->release();
  75. }
  76. Framework::Array<ItemInfo> ShapedRecipie::getOutput(ShapedCraftingStorage* zStorage)
  77. {
  78. Framework::Array<ItemInfo> result;
  79. ItemInfo info;
  80. info.count = outputAmount;
  81. info.type = output->zItemType()->getId();
  82. info.hp = output->getHp();
  83. info.durability = output->getDurability();
  84. info.maxHp = output->getMaxHp();
  85. info.maxDurability = output->getMaxDurability();
  86. result.add(info);
  87. return result;
  88. }