Recipie.cpp 2.5 KB

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