Recipie.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "Recipie.h"
  2. #include "CraftingStorage.h"
  3. #include "Item.h"
  4. Recipie::Recipie()
  5. : ReferenceCounter()
  6. {}
  7. void Recipie::addOutput(Item* item, int amount)
  8. {
  9. outputs.add(item);
  10. outputAmount.add(amount);
  11. }
  12. Framework::Array<ItemInfo> Recipie::getOutput(CraftingStorage* zStorage) const
  13. {
  14. Framework::Array<ItemInfo> result;
  15. int index = 0;
  16. for (const Item* output : outputs)
  17. {
  18. ItemInfo info;
  19. info.count = outputAmount.get(index);
  20. info.type = output->zItemType()->getId();
  21. info.hp = output->getHp();
  22. info.durability = output->getDurability();
  23. info.maxHp = output->getMaxHp();
  24. info.maxDurability = output->getMaxDurability();
  25. result.add(info);
  26. index++;
  27. }
  28. return result;
  29. }
  30. bool Recipie::createsOutput(ItemType* zType)
  31. {
  32. for (const Item* output : outputs)
  33. {
  34. if (output->zItemType() == zType)
  35. {
  36. return 1;
  37. }
  38. }
  39. return 0;
  40. }
  41. UnshapedRecipie::UnshapedRecipie()
  42. : Recipie()
  43. {}
  44. void UnshapedRecipie::addIngredient(
  45. ItemFilter* filter, int amount, ItemModifier* modifier)
  46. {
  47. filters.add(filter);
  48. modifiers.add(modifier);
  49. inputAmount.add(amount);
  50. }
  51. bool UnshapedRecipie::testApplicability(CraftingStorage* zStorage)
  52. {
  53. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  54. {
  55. if (!zStorage->hasFreeSpace(outputs.z(i), outputAmount.get(i)))
  56. return 0;
  57. }
  58. return zStorage->isAllAvailable(filters, inputAmount);
  59. }
  60. void UnshapedRecipie::apply(CraftingStorage* zStorage)
  61. {
  62. zStorage->consume(filters, modifiers, inputAmount);
  63. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  64. {
  65. ItemStack* stack
  66. = new ItemStack(outputs.z(i)->zItemType()->cloneItem(outputs.z(i)),
  67. outputAmount.get(i));
  68. zStorage->addCraftingResult(stack);
  69. stack->release();
  70. }
  71. }
  72. Framework::Text UnshapedRecipie::getRecipieUIML()
  73. {
  74. Framework::Text result = "<recipie type=\"unshaped\"><ingredients>";
  75. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  76. {
  77. result.append() << "<ingredient amount=\"" << inputAmount.get(i)
  78. << "\">";
  79. if (filters.z(i))
  80. {
  81. result.append()
  82. << "<logic>" << filters.z(i)->getLogicUIML() << "</logic>";
  83. }
  84. result += "</ingredient>";
  85. // TODO: add modifiers
  86. }
  87. result += "</ingredients><outputs>";
  88. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  89. {
  90. result.append() << "<output amount=\"" << outputAmount.get(i)
  91. << "\" itemType=\""
  92. << outputs.z(i)->zItemType()->getId() << "\" hp=\""
  93. << outputs.z(i)->getHp() << "\" durability=\""
  94. << outputs.z(i)->getDurability() << "\" maxHp=\""
  95. << outputs.z(i)->getMaxHp() << "\" maxDurability=\""
  96. << outputs.z(i)->getMaxDurability() << "\">"
  97. << outputs.z(i)->getTooltipUIML() << "</output>";
  98. }
  99. result += "</outputs></recipie>";
  100. return result;
  101. }
  102. ShapedRecipie::ShapedRecipie(int width, int height)
  103. : ReferenceCounter(),
  104. width(width),
  105. height(height)
  106. {
  107. for (int i = 0; i < width * height; i++)
  108. {
  109. filters.add(0);
  110. inputAmount.add(0);
  111. modifiers.add(0);
  112. }
  113. }
  114. void ShapedRecipie::setIngredient(
  115. int x, int y, ItemFilter* filter, ItemModifier* modifier)
  116. {
  117. filters.set(filter, width * y + x);
  118. inputAmount.set(1, width * y + x); // TODO: make this configurable
  119. modifiers.set(modifier, width * y + x);
  120. }
  121. bool ShapedRecipie::testApplicability(CraftingStorage* zStorage)
  122. {
  123. ShapedCraftingStorage* zShapedStorage
  124. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  125. if (!zShapedStorage) return 0;
  126. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  127. {
  128. if (!zShapedStorage->hasFreeSpace(outputs.z(i), outputAmount.get(i)))
  129. return 0;
  130. }
  131. return zShapedStorage->isAllAvailable(filters, inputAmount, width, height);
  132. }
  133. void ShapedRecipie::apply(CraftingStorage* zStorage)
  134. {
  135. ShapedCraftingStorage* zShapedStorage
  136. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  137. zShapedStorage->consume(filters, modifiers, inputAmount, width, height);
  138. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  139. {
  140. ItemStack* stack
  141. = new ItemStack(outputs.z(i)->zItemType()->cloneItem(outputs.z(i)),
  142. outputAmount.get(i));
  143. zStorage->addCraftingResult(stack);
  144. stack->release();
  145. }
  146. }
  147. Framework::Text ShapedRecipie::getRecipieUIML()
  148. {
  149. Framework::Text result = "<recipie type=\"shaped\" width=\"";
  150. result.append() << width << "\" height=\"" << height << "\"><ingredients>";
  151. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  152. {
  153. result.append() << "<ingredient amount=\"" << inputAmount.get(i)
  154. << "\">";
  155. if (filters.z(i))
  156. {
  157. result.append()
  158. << "<logic>" << filters.z(i)->getLogicUIML() << "</logic>";
  159. }
  160. result += "</ingredient>";
  161. // TODO: add modifiers
  162. }
  163. result += "</ingredients><outputs>";
  164. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  165. {
  166. result.append() << "<output amount=\"" << outputAmount.get(i)
  167. << "\" itemType=\""
  168. << outputs.z(i)->zItemType()->getId() << "\" hp=\""
  169. << outputs.z(i)->getHp() << "\" durability=\""
  170. << outputs.z(i)->getDurability() << "\" maxHp=\""
  171. << outputs.z(i)->getMaxHp() << "\" maxDurability=\""
  172. << outputs.z(i)->getMaxDurability() << "\">"
  173. << outputs.z(i)->getTooltipUIML() << "</output>";
  174. }
  175. result += "</outputs></recipie>";
  176. return result;
  177. }