craftdef.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <string>
  18. #include <iostream>
  19. #include <vector>
  20. #include <utility>
  21. #include "gamedef.h"
  22. #include "inventory.h"
  23. /*
  24. Crafting methods.
  25. The crafting method depends on the inventory list
  26. that the crafting input comes from.
  27. */
  28. enum CraftMethod
  29. {
  30. // Crafting grid
  31. CRAFT_METHOD_NORMAL,
  32. // Cooking something in a furnace
  33. CRAFT_METHOD_COOKING,
  34. // Using something as fuel for a furnace
  35. CRAFT_METHOD_FUEL,
  36. };
  37. /*
  38. The type a hash can be. The earlier a type is mentioned in this enum,
  39. the earlier it is tried at crafting, and the less likely is a collision.
  40. Changing order causes changes in behaviour, so know what you do.
  41. */
  42. enum CraftHashType
  43. {
  44. // Hashes the normalized names of the recipe's elements.
  45. // Only recipes without group usage can be found here,
  46. // because groups can't be guessed efficiently.
  47. CRAFT_HASH_TYPE_ITEM_NAMES,
  48. // Counts the non-empty slots.
  49. CRAFT_HASH_TYPE_COUNT,
  50. // This layer both spares an extra variable, and helps to retain (albeit rarely used) functionality. Maps to 0.
  51. // Before hashes are "initialized", all hashes reside here, after initialisation, none are.
  52. CRAFT_HASH_TYPE_UNHASHED
  53. };
  54. const int craft_hash_type_max = (int) CRAFT_HASH_TYPE_UNHASHED;
  55. /*
  56. Input: The contents of the crafting slots, arranged in matrix form
  57. */
  58. struct CraftInput
  59. {
  60. CraftMethod method = CRAFT_METHOD_NORMAL;
  61. unsigned int width = 0;
  62. std::vector<ItemStack> items;
  63. CraftInput() = default;
  64. CraftInput(CraftMethod method_, unsigned int width_,
  65. const std::vector<ItemStack> &items_):
  66. method(method_), width(width_), items(items_)
  67. {}
  68. std::string dump() const;
  69. };
  70. /*
  71. Output: Result of crafting operation
  72. */
  73. struct CraftOutput
  74. {
  75. // Used for normal crafting and cooking, itemstring
  76. std::string item = "";
  77. // Used for cooking (cook time) and fuel (burn time), seconds
  78. float time = 0.0f;
  79. CraftOutput() = default;
  80. CraftOutput(const std::string &item_, float time_):
  81. item(item_), time(time_)
  82. {}
  83. std::string dump() const;
  84. };
  85. /*
  86. A list of replacements. A replacement indicates that a specific
  87. input item should not be deleted (when crafting) but replaced with
  88. a different item. Each replacements is a pair (itemstring to remove,
  89. itemstring to replace with)
  90. Example: If ("bucket:bucket_water", "bucket:bucket_empty") is a
  91. replacement pair, the crafting input slot that contained a water
  92. bucket will contain an empty bucket after crafting.
  93. Note: replacements only work correctly when stack_max of the item
  94. to be replaced is 1. It is up to the mod writer to ensure this.
  95. */
  96. struct CraftReplacements
  97. {
  98. // List of replacements
  99. std::vector<std::pair<std::string, std::string> > pairs;
  100. CraftReplacements() = default;
  101. CraftReplacements(const std::vector<std::pair<std::string, std::string> > &pairs_):
  102. pairs(pairs_)
  103. {}
  104. std::string dump() const;
  105. };
  106. /*
  107. Crafting definition base class
  108. */
  109. class CraftDefinition
  110. {
  111. public:
  112. CraftDefinition() = default;
  113. virtual ~CraftDefinition() = default;
  114. // Returns type of crafting definition
  115. virtual std::string getName() const=0;
  116. // Checks whether the recipe is applicable
  117. virtual bool check(const CraftInput &input, IGameDef *gamedef) const=0;
  118. // Returns the output structure, meaning depends on crafting method
  119. // The implementation can assume that check(input) returns true
  120. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const=0;
  121. // the inverse of the above
  122. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const=0;
  123. // Decreases count of every input item
  124. virtual void decrementInput(CraftInput &input,
  125. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const=0;
  126. virtual CraftHashType getHashType() const = 0;
  127. virtual u64 getHash(CraftHashType type) const = 0;
  128. // to be called after all mods are loaded, so that we catch all aliases
  129. virtual void initHash(IGameDef *gamedef) = 0;
  130. virtual std::string dump() const=0;
  131. };
  132. /*
  133. A plain-jane (shaped) crafting definition
  134. Supported crafting method: CRAFT_METHOD_NORMAL.
  135. Requires the input items to be arranged exactly like in the recipe.
  136. */
  137. class CraftDefinitionShaped: public CraftDefinition
  138. {
  139. public:
  140. CraftDefinitionShaped() = delete;
  141. CraftDefinitionShaped(
  142. const std::string &output_,
  143. unsigned int width_,
  144. const std::vector<std::string> &recipe_,
  145. const CraftReplacements &replacements_):
  146. output(output_), width(width_), recipe(recipe_),
  147. replacements(replacements_)
  148. {}
  149. virtual ~CraftDefinitionShaped() = default;
  150. virtual std::string getName() const;
  151. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  152. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  153. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  154. virtual void decrementInput(CraftInput &input,
  155. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  156. virtual CraftHashType getHashType() const;
  157. virtual u64 getHash(CraftHashType type) const;
  158. virtual void initHash(IGameDef *gamedef);
  159. virtual std::string dump() const;
  160. private:
  161. // Output itemstring
  162. std::string output = "";
  163. // Width of recipe
  164. unsigned int width = 1;
  165. // Recipe matrix (itemstrings)
  166. std::vector<std::string> recipe;
  167. // Recipe matrix (item names)
  168. std::vector<std::string> recipe_names;
  169. // bool indicating if initHash has been called already
  170. bool hash_inited = false;
  171. // Replacement items for decrementInput()
  172. CraftReplacements replacements;
  173. };
  174. /*
  175. A shapeless crafting definition
  176. Supported crafting method: CRAFT_METHOD_NORMAL.
  177. Input items can arranged in any way.
  178. */
  179. class CraftDefinitionShapeless: public CraftDefinition
  180. {
  181. public:
  182. CraftDefinitionShapeless() = delete;
  183. CraftDefinitionShapeless(
  184. const std::string &output_,
  185. const std::vector<std::string> &recipe_,
  186. const CraftReplacements &replacements_):
  187. output(output_), recipe(recipe_), replacements(replacements_)
  188. {}
  189. virtual ~CraftDefinitionShapeless() = default;
  190. virtual std::string getName() const;
  191. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  192. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  193. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  194. virtual void decrementInput(CraftInput &input,
  195. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  196. virtual CraftHashType getHashType() const;
  197. virtual u64 getHash(CraftHashType type) const;
  198. virtual void initHash(IGameDef *gamedef);
  199. virtual std::string dump() const;
  200. private:
  201. // Output itemstring
  202. std::string output;
  203. // Recipe list (itemstrings)
  204. std::vector<std::string> recipe;
  205. // Recipe list (item names)
  206. std::vector<std::string> recipe_names;
  207. // bool indicating if initHash has been called already
  208. bool hash_inited = false;
  209. // Replacement items for decrementInput()
  210. CraftReplacements replacements;
  211. };
  212. /*
  213. Tool repair crafting definition
  214. Supported crafting method: CRAFT_METHOD_NORMAL.
  215. Put two damaged tools into the crafting grid, get one tool back.
  216. There should only be one crafting definition of this type.
  217. */
  218. class CraftDefinitionToolRepair: public CraftDefinition
  219. {
  220. public:
  221. CraftDefinitionToolRepair() = delete;
  222. CraftDefinitionToolRepair(float additional_wear_):
  223. additional_wear(additional_wear_)
  224. {}
  225. virtual ~CraftDefinitionToolRepair() = default;
  226. virtual std::string getName() const;
  227. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  228. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  229. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  230. virtual void decrementInput(CraftInput &input,
  231. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  232. virtual CraftHashType getHashType() const { return CRAFT_HASH_TYPE_COUNT; }
  233. virtual u64 getHash(CraftHashType type) const { return 2; }
  234. virtual void initHash(IGameDef *gamedef) {}
  235. virtual std::string dump() const;
  236. private:
  237. // This is a constant that is added to the wear of the result.
  238. // May be positive or negative, allowed range [-1,1].
  239. // 1 = new tool is completely broken
  240. // 0 = simply add remaining uses of both input tools
  241. // -1 = new tool is completely pristine
  242. float additional_wear = 0.0f;
  243. };
  244. /*
  245. A cooking (in furnace) definition
  246. Supported crafting method: CRAFT_METHOD_COOKING.
  247. */
  248. class CraftDefinitionCooking: public CraftDefinition
  249. {
  250. public:
  251. CraftDefinitionCooking() = delete;
  252. CraftDefinitionCooking(
  253. const std::string &output_,
  254. const std::string &recipe_,
  255. float cooktime_,
  256. const CraftReplacements &replacements_):
  257. output(output_), recipe(recipe_), cooktime(cooktime_), replacements(replacements_)
  258. {}
  259. virtual ~CraftDefinitionCooking() = default;
  260. virtual std::string getName() const;
  261. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  262. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  263. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  264. virtual void decrementInput(CraftInput &input,
  265. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  266. virtual CraftHashType getHashType() const;
  267. virtual u64 getHash(CraftHashType type) const;
  268. virtual void initHash(IGameDef *gamedef);
  269. virtual std::string dump() const;
  270. private:
  271. // Output itemstring
  272. std::string output;
  273. // Recipe itemstring
  274. std::string recipe;
  275. // Recipe item name
  276. std::string recipe_name;
  277. // bool indicating if initHash has been called already
  278. bool hash_inited = false;
  279. // Time in seconds
  280. float cooktime;
  281. // Replacement items for decrementInput()
  282. CraftReplacements replacements;
  283. };
  284. /*
  285. A fuel (for furnace) definition
  286. Supported crafting method: CRAFT_METHOD_FUEL.
  287. */
  288. class CraftDefinitionFuel: public CraftDefinition
  289. {
  290. public:
  291. CraftDefinitionFuel() = delete;
  292. CraftDefinitionFuel(const std::string &recipe_,
  293. float burntime_,
  294. const CraftReplacements &replacements_):
  295. recipe(recipe_), burntime(burntime_), replacements(replacements_)
  296. {}
  297. virtual ~CraftDefinitionFuel() = default;
  298. virtual std::string getName() const;
  299. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  300. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  301. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  302. virtual void decrementInput(CraftInput &input,
  303. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  304. virtual CraftHashType getHashType() const;
  305. virtual u64 getHash(CraftHashType type) const;
  306. virtual void initHash(IGameDef *gamedef);
  307. virtual std::string dump() const;
  308. private:
  309. // Recipe itemstring
  310. std::string recipe;
  311. // Recipe item name
  312. std::string recipe_name;
  313. // bool indicating if initHash has been called already
  314. bool hash_inited = false;
  315. // Time in seconds
  316. float burntime;
  317. // Replacement items for decrementInput()
  318. CraftReplacements replacements;
  319. };
  320. /*
  321. Crafting definition manager
  322. */
  323. class ICraftDefManager
  324. {
  325. public:
  326. ICraftDefManager() = default;
  327. virtual ~ICraftDefManager() = default;
  328. // The main crafting function
  329. virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
  330. std::vector<ItemStack> &output_replacements,
  331. bool decrementInput, IGameDef *gamedef) const=0;
  332. virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
  333. IGameDef *gamedef, unsigned limit=0) const=0;
  334. // Print crafting recipes for debugging
  335. virtual std::string dump() const=0;
  336. };
  337. class IWritableCraftDefManager : public ICraftDefManager
  338. {
  339. public:
  340. IWritableCraftDefManager() = default;
  341. virtual ~IWritableCraftDefManager() = default;
  342. // The main crafting function
  343. virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
  344. std::vector<ItemStack> &output_replacements,
  345. bool decrementInput, IGameDef *gamedef) const=0;
  346. virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
  347. IGameDef *gamedef, unsigned limit=0) const=0;
  348. virtual bool clearCraftRecipesByOutput(const CraftOutput &output, IGameDef *gamedef) = 0;
  349. virtual bool clearCraftRecipesByInput(CraftMethod craft_method,
  350. unsigned int craft_grid_width, const std::vector<std::string> &recipe, IGameDef *gamedef) = 0;
  351. // Print crafting recipes for debugging
  352. virtual std::string dump() const=0;
  353. // Add a crafting definition.
  354. // After calling this, the pointer belongs to the manager.
  355. virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) = 0;
  356. // Delete all crafting definitions
  357. virtual void clear()=0;
  358. // To be called after all mods are loaded, so that we catch all aliases
  359. virtual void initHashes(IGameDef *gamedef) = 0;
  360. };
  361. IWritableCraftDefManager* createCraftDefManager();