Recipie.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 += "<ingredient amount=\"";
  78. result += inputAmount.get(i);
  79. result += "\">";
  80. if (filters.z(i))
  81. {
  82. result += "<logic>";
  83. result += filters.z(i)->getLogicUIML();
  84. result += "</logic>";
  85. }
  86. result += "</ingredient>";
  87. // TODO: add modifiers
  88. }
  89. result += "</ingredients><outputs>";
  90. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  91. {
  92. result += "<output amount=\"";
  93. result += outputAmount.get(i);
  94. result += "\" itemType=\"";
  95. result += outputs.z(i)->zItemType()->getId();
  96. result += "\" hp=\"";
  97. result += outputs.z(i)->getHp();
  98. result += "\" durability=\"";
  99. result += outputs.z(i)->getDurability();
  100. result += "\" maxHp=\"";
  101. result += outputs.z(i)->getMaxHp();
  102. result += "\" maxDurability=\"";
  103. result += outputs.z(i)->getMaxDurability();
  104. result += "\">";
  105. result += outputs.z(i)->getTooltipUIML();
  106. result += "</output>";
  107. }
  108. result += "</outputs></recipie>";
  109. return result;
  110. }
  111. ShapedRecipie::ShapedRecipie(int width, int height)
  112. : ReferenceCounter(),
  113. width(width),
  114. height(height)
  115. {
  116. for (int i = 0; i < width * height; i++)
  117. {
  118. filters.add(0);
  119. inputAmount.add(0);
  120. modifiers.add(0);
  121. }
  122. }
  123. void ShapedRecipie::setIngredient(
  124. int x, int y, ItemFilter* filter, ItemModifier* modifier)
  125. {
  126. filters.set(filter, width * y + x);
  127. inputAmount.set(1, width * y + x); // TODO: make this configurable
  128. modifiers.set(modifier, width * y + x);
  129. }
  130. bool ShapedRecipie::testApplicability(CraftingStorage* zStorage)
  131. {
  132. ShapedCraftingStorage* zShapedStorage
  133. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  134. if (!zShapedStorage) return 0;
  135. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  136. {
  137. if (!zShapedStorage->hasFreeSpace(outputs.z(i), outputAmount.get(i)))
  138. return 0;
  139. }
  140. return zShapedStorage->isAllAvailable(filters, inputAmount, width, height);
  141. }
  142. void ShapedRecipie::apply(CraftingStorage* zStorage)
  143. {
  144. ShapedCraftingStorage* zShapedStorage
  145. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  146. zShapedStorage->consume(filters, modifiers, inputAmount, width, height);
  147. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  148. {
  149. ItemStack* stack
  150. = new ItemStack(outputs.z(i)->zItemType()->cloneItem(outputs.z(i)),
  151. outputAmount.get(i));
  152. zStorage->addCraftingResult(stack);
  153. stack->release();
  154. }
  155. }
  156. Framework::Text ShapedRecipie::getRecipieUIML()
  157. {
  158. Framework::Text result = "<recipie type=\"shaped\" width=\"";
  159. result += width;
  160. result += "\" height=\"";
  161. result += height;
  162. result += "\"><ingredients>";
  163. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  164. {
  165. result += "<ingredient amount=\"";
  166. result += inputAmount.get(i);
  167. result += "\">";
  168. if (filters.z(i))
  169. {
  170. result += "<logic>";
  171. result += filters.z(i)->getLogicUIML();
  172. result += "</logic>";
  173. }
  174. result += "</ingredient>";
  175. // TODO: add modifiers
  176. }
  177. result += "</ingredients><outputs>";
  178. for (int i = 0; i < outputs.getEintragAnzahl(); i++)
  179. {
  180. result += "<output amount=\"";
  181. result += outputAmount.get(i);
  182. result += "\" itemType=\"";
  183. result += outputs.z(i)->zItemType()->getId();
  184. result += "\" hp=\"";
  185. result += outputs.z(i)->getHp();
  186. result += "\" durability=\"";
  187. result += outputs.z(i)->getDurability();
  188. result += "\" maxHp=\"";
  189. result += outputs.z(i)->getMaxHp();
  190. result += "\" maxDurability=\"";
  191. result += outputs.z(i)->getMaxDurability();
  192. result += "\">";
  193. result += outputs.z(i)->getTooltipUIML();
  194. result += "</output>";
  195. }
  196. result += "</outputs></recipie>";
  197. return result;
  198. }