Recipie.cpp 5.9 KB

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