craftdef.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. // Returns true if all items are empty.
  69. bool empty() const;
  70. std::string dump() const;
  71. };
  72. /*
  73. Output: Result of crafting operation
  74. */
  75. struct CraftOutput
  76. {
  77. // Used for normal crafting and cooking, itemstring
  78. std::string item = "";
  79. // Used for cooking (cook time) and fuel (burn time), seconds
  80. float time = 0.0f;
  81. CraftOutput() = default;
  82. CraftOutput(const std::string &item_, float time_):
  83. item(item_), time(time_)
  84. {}
  85. std::string dump() const;
  86. };
  87. /*
  88. A list of replacements. A replacement indicates that a specific
  89. input item should not be deleted (when crafting) but replaced with
  90. a different item. Each replacements is a pair (itemstring to remove,
  91. itemstring to replace with)
  92. Example: If ("bucket:bucket_water", "bucket:bucket_empty") is a
  93. replacement pair, the crafting input slot that contained a water
  94. bucket will contain an empty bucket after crafting.
  95. Note: replacements only work correctly when stack_max of the item
  96. to be replaced is 1. It is up to the mod writer to ensure this.
  97. */
  98. struct CraftReplacements
  99. {
  100. // List of replacements
  101. std::vector<std::pair<std::string, std::string> > pairs;
  102. CraftReplacements() = default;
  103. CraftReplacements(const std::vector<std::pair<std::string, std::string> > &pairs_):
  104. pairs(pairs_)
  105. {}
  106. std::string dump() const;
  107. };
  108. /*
  109. Crafting definition base class
  110. */
  111. class CraftDefinition
  112. {
  113. public:
  114. /*
  115. Craft recipe priorities, from low to high
  116. Recipes are searched from latest to first.
  117. If a recipe with higher priority than a previous found one is
  118. encountered, it is selected instead.
  119. */
  120. enum RecipePriority
  121. {
  122. PRIORITY_NO_RECIPE,
  123. PRIORITY_TOOLREPAIR,
  124. PRIORITY_SHAPELESS_AND_GROUPS,
  125. PRIORITY_SHAPELESS,
  126. PRIORITY_SHAPED_AND_GROUPS,
  127. PRIORITY_SHAPED,
  128. };
  129. CraftDefinition() = default;
  130. virtual ~CraftDefinition() = default;
  131. // Returns type of crafting definition
  132. virtual std::string getName() const=0;
  133. // Checks whether the recipe is applicable
  134. virtual bool check(const CraftInput &input, IGameDef *gamedef) const=0;
  135. RecipePriority getPriority() const
  136. {
  137. return priority;
  138. }
  139. // Returns the output structure, meaning depends on crafting method
  140. // The implementation can assume that check(input) returns true
  141. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const=0;
  142. // the inverse of the above
  143. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const=0;
  144. // Decreases count of every input item
  145. virtual void decrementInput(CraftInput &input,
  146. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const=0;
  147. CraftHashType getHashType() const
  148. {
  149. return hash_type;
  150. }
  151. virtual u64 getHash(CraftHashType type) const = 0;
  152. // to be called after all mods are loaded, so that we catch all aliases
  153. virtual void initHash(IGameDef *gamedef) = 0;
  154. virtual std::string dump() const=0;
  155. protected:
  156. CraftHashType hash_type;
  157. RecipePriority priority;
  158. };
  159. /*
  160. A plain-jane (shaped) crafting definition
  161. Supported crafting method: CRAFT_METHOD_NORMAL.
  162. Requires the input items to be arranged exactly like in the recipe.
  163. */
  164. class CraftDefinitionShaped: public CraftDefinition
  165. {
  166. public:
  167. CraftDefinitionShaped() = delete;
  168. CraftDefinitionShaped(
  169. const std::string &output_,
  170. unsigned int width_,
  171. const std::vector<std::string> &recipe_,
  172. const CraftReplacements &replacements_);
  173. virtual ~CraftDefinitionShaped() = default;
  174. virtual std::string getName() const;
  175. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  176. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  177. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  178. virtual void decrementInput(CraftInput &input,
  179. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  180. virtual u64 getHash(CraftHashType type) const;
  181. virtual void initHash(IGameDef *gamedef);
  182. virtual std::string dump() const;
  183. private:
  184. // Output itemstring
  185. std::string output = "";
  186. // Width of recipe
  187. unsigned int width = 1;
  188. // Recipe matrix (itemstrings)
  189. std::vector<std::string> recipe;
  190. // Recipe matrix (item names)
  191. std::vector<std::string> recipe_names;
  192. // bool indicating if initHash has been called already
  193. bool hash_inited = false;
  194. // Replacement items for decrementInput()
  195. CraftReplacements replacements;
  196. };
  197. /*
  198. A shapeless crafting definition
  199. Supported crafting method: CRAFT_METHOD_NORMAL.
  200. Input items can arranged in any way.
  201. */
  202. class CraftDefinitionShapeless: public CraftDefinition
  203. {
  204. public:
  205. CraftDefinitionShapeless() = delete;
  206. CraftDefinitionShapeless(
  207. const std::string &output_,
  208. const std::vector<std::string> &recipe_,
  209. const CraftReplacements &replacements_);
  210. virtual ~CraftDefinitionShapeless() = default;
  211. virtual std::string getName() const;
  212. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  213. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  214. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  215. virtual void decrementInput(CraftInput &input,
  216. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  217. virtual u64 getHash(CraftHashType type) const;
  218. virtual void initHash(IGameDef *gamedef);
  219. virtual std::string dump() const;
  220. private:
  221. // Output itemstring
  222. std::string output;
  223. // Recipe list (itemstrings)
  224. std::vector<std::string> recipe;
  225. // Recipe list (item names)
  226. std::vector<std::string> recipe_names;
  227. // bool indicating if initHash has been called already
  228. bool hash_inited = false;
  229. // Replacement items for decrementInput()
  230. CraftReplacements replacements;
  231. };
  232. /*
  233. Tool repair crafting definition
  234. Supported crafting method: CRAFT_METHOD_NORMAL.
  235. Put two damaged tools into the crafting grid, get one tool back.
  236. There should only be one crafting definition of this type.
  237. */
  238. class CraftDefinitionToolRepair: public CraftDefinition
  239. {
  240. public:
  241. CraftDefinitionToolRepair() = delete;
  242. CraftDefinitionToolRepair(float additional_wear_);
  243. virtual ~CraftDefinitionToolRepair() = default;
  244. virtual std::string getName() const;
  245. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  246. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  247. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  248. virtual void decrementInput(CraftInput &input,
  249. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  250. virtual u64 getHash(CraftHashType type) const { return 2; }
  251. virtual void initHash(IGameDef *gamedef)
  252. {
  253. hash_type = CRAFT_HASH_TYPE_COUNT;
  254. }
  255. virtual std::string dump() const;
  256. private:
  257. // This is a constant that is added to the wear of the result.
  258. // May be positive or negative, allowed range [-1,1].
  259. // 1 = new tool is completely broken
  260. // 0 = simply add remaining uses of both input tools
  261. // -1 = new tool is completely pristine
  262. float additional_wear = 0.0f;
  263. };
  264. /*
  265. A cooking (in furnace) definition
  266. Supported crafting method: CRAFT_METHOD_COOKING.
  267. */
  268. class CraftDefinitionCooking: public CraftDefinition
  269. {
  270. public:
  271. CraftDefinitionCooking() = delete;
  272. CraftDefinitionCooking(
  273. const std::string &output_,
  274. const std::string &recipe_,
  275. float cooktime_,
  276. const CraftReplacements &replacements_);
  277. virtual ~CraftDefinitionCooking() = default;
  278. virtual std::string getName() const;
  279. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  280. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  281. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  282. virtual void decrementInput(CraftInput &input,
  283. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  284. virtual u64 getHash(CraftHashType type) const;
  285. virtual void initHash(IGameDef *gamedef);
  286. virtual std::string dump() const;
  287. private:
  288. // Output itemstring
  289. std::string output;
  290. // Recipe itemstring
  291. std::string recipe;
  292. // Recipe item name
  293. std::string recipe_name;
  294. // bool indicating if initHash has been called already
  295. bool hash_inited = false;
  296. // Time in seconds
  297. float cooktime;
  298. // Replacement items for decrementInput()
  299. CraftReplacements replacements;
  300. };
  301. /*
  302. A fuel (for furnace) definition
  303. Supported crafting method: CRAFT_METHOD_FUEL.
  304. */
  305. class CraftDefinitionFuel: public CraftDefinition
  306. {
  307. public:
  308. CraftDefinitionFuel() = delete;
  309. CraftDefinitionFuel(
  310. const std::string &recipe_,
  311. float burntime_,
  312. const CraftReplacements &replacements_);
  313. virtual ~CraftDefinitionFuel() = default;
  314. virtual std::string getName() const;
  315. virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
  316. virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
  317. virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
  318. virtual void decrementInput(CraftInput &input,
  319. std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
  320. virtual u64 getHash(CraftHashType type) const;
  321. virtual void initHash(IGameDef *gamedef);
  322. virtual std::string dump() const;
  323. private:
  324. // Recipe itemstring
  325. std::string recipe;
  326. // Recipe item name
  327. std::string recipe_name;
  328. // bool indicating if initHash has been called already
  329. bool hash_inited = false;
  330. // Time in seconds
  331. float burntime;
  332. // Replacement items for decrementInput()
  333. CraftReplacements replacements;
  334. };
  335. /*
  336. Crafting definition manager
  337. */
  338. class ICraftDefManager
  339. {
  340. public:
  341. ICraftDefManager() = default;
  342. virtual ~ICraftDefManager() = default;
  343. // The main crafting function
  344. virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
  345. std::vector<ItemStack> &output_replacements,
  346. bool decrementInput, IGameDef *gamedef) const=0;
  347. virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
  348. IGameDef *gamedef, unsigned limit=0) const=0;
  349. // Print crafting recipes for debugging
  350. virtual std::string dump() const=0;
  351. };
  352. class IWritableCraftDefManager : public ICraftDefManager
  353. {
  354. public:
  355. IWritableCraftDefManager() = default;
  356. virtual ~IWritableCraftDefManager() = default;
  357. // The main crafting function
  358. virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
  359. std::vector<ItemStack> &output_replacements,
  360. bool decrementInput, IGameDef *gamedef) const=0;
  361. virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
  362. IGameDef *gamedef, unsigned limit=0) const=0;
  363. virtual bool clearCraftsByOutput(const CraftOutput &output, IGameDef *gamedef) = 0;
  364. virtual bool clearCraftsByInput(const CraftInput &input, IGameDef *gamedef) = 0;
  365. // Print crafting recipes for debugging
  366. virtual std::string dump() const=0;
  367. // Add a crafting definition.
  368. // After calling this, the pointer belongs to the manager.
  369. virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) = 0;
  370. // Delete all crafting definitions
  371. virtual void clear()=0;
  372. // To be called after all mods are loaded, so that we catch all aliases
  373. virtual void initHashes(IGameDef *gamedef) = 0;
  374. };
  375. IWritableCraftDefManager* createCraftDefManager();