3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "inventory.h"
32 The crafting method depends on the inventory list
33 that the crafting input comes from.
39 // Cooking something in a furnace
41 // Using something as fuel for a furnace
46 The type a hash can be. The earlier a type is mentioned in this enum,
47 the earlier it is tried at crafting, and the less likely is a collision.
48 Changing order causes changes in behaviour, so know what you do.
52 // Hashes the normalized names of the recipe's elements.
53 // Only recipes without group usage can be found here,
54 // because groups can't be guessed efficiently.
55 CRAFT_HASH_TYPE_ITEM_NAMES,
57 // Counts the non-empty slots.
58 CRAFT_HASH_TYPE_COUNT,
60 // This layer both spares an extra variable, and helps to retain (albeit rarely used) functionality. Maps to 0.
61 // Before hashes are "initialized", all hashes reside here, after initialisation, none are.
62 CRAFT_HASH_TYPE_UNHASHED
65 const int craft_hash_type_max = (int) CRAFT_HASH_TYPE_UNHASHED;
68 Input: The contents of the crafting slots, arranged in matrix form
72 CraftMethod method = CRAFT_METHOD_NORMAL;
73 unsigned int width = 0;
74 std::vector<ItemStack> items;
76 CraftInput() = default;
78 CraftInput(CraftMethod method_, unsigned int width_,
79 const std::vector<ItemStack> &items_):
80 method(method_), width(width_), items(items_)
83 // Returns true if all items are empty.
86 std::string dump() const;
90 Output: Result of crafting operation
94 // Used for normal crafting and cooking, itemstring
95 std::string item = "";
96 // Used for cooking (cook time) and fuel (burn time), seconds
99 CraftOutput() = default;
101 CraftOutput(const std::string &item_, float time_):
102 item(item_), time(time_)
104 std::string dump() const;
108 A list of replacements. A replacement indicates that a specific
109 input item should not be deleted (when crafting) but replaced with
110 a different item. Each replacements is a pair (itemstring to remove,
111 itemstring to replace with)
113 Example: If ("bucket:bucket_water", "bucket:bucket_empty") is a
114 replacement pair, the crafting input slot that contained a water
115 bucket will contain an empty bucket after crafting.
117 Note: replacements only work correctly when stack_max of the item
118 to be replaced is 1. It is up to the mod writer to ensure this.
120 struct CraftReplacements
122 // List of replacements
123 std::vector<std::pair<std::string, std::string> > pairs;
125 CraftReplacements() = default;
126 CraftReplacements(const std::vector<std::pair<std::string, std::string> > &pairs_):
129 std::string dump() const;
133 Crafting definition base class
135 class CraftDefinition
139 Craft recipe priorities, from low to high
141 Recipes are searched from latest to first.
142 If a recipe with higher priority than a previous found one is
143 encountered, it is selected instead.
149 PRIORITY_SHAPELESS_AND_GROUPS,
151 PRIORITY_SHAPED_AND_GROUPS,
155 CraftDefinition() = default;
156 virtual ~CraftDefinition() = default;
158 // Returns type of crafting definition
159 virtual std::string getName() const=0;
161 // Checks whether the recipe is applicable
162 virtual bool check(const CraftInput &input, IGameDef *gamedef) const=0;
163 RecipePriority getPriority() const
167 // Returns the output structure, meaning depends on crafting method
168 // The implementation can assume that check(input) returns true
169 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const=0;
170 // the inverse of the above
171 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const=0;
172 // Decreases count of every input item
173 virtual void decrementInput(CraftInput &input,
174 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const=0;
176 CraftHashType getHashType() const
180 virtual u64 getHash(CraftHashType type) const = 0;
182 // to be called after all mods are loaded, so that we catch all aliases
183 virtual void initHash(IGameDef *gamedef) = 0;
185 virtual std::string dump() const=0;
188 CraftHashType hash_type;
189 RecipePriority priority;
193 A plain-jane (shaped) crafting definition
195 Supported crafting method: CRAFT_METHOD_NORMAL.
196 Requires the input items to be arranged exactly like in the recipe.
198 class CraftDefinitionShaped: public CraftDefinition
201 CraftDefinitionShaped() = delete;
202 CraftDefinitionShaped(
203 const std::string &output_,
205 const std::vector<std::string> &recipe_,
206 const CraftReplacements &replacements_);
208 virtual ~CraftDefinitionShaped() = default;
210 virtual std::string getName() const;
211 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
212 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
213 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
214 virtual void decrementInput(CraftInput &input,
215 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
217 virtual u64 getHash(CraftHashType type) const;
219 virtual void initHash(IGameDef *gamedef);
221 virtual std::string dump() const;
225 std::string output = "";
227 unsigned int width = 1;
228 // Recipe matrix (itemstrings)
229 std::vector<std::string> recipe;
230 // Recipe matrix (item names)
231 std::vector<std::string> recipe_names;
232 // bool indicating if initHash has been called already
233 bool hash_inited = false;
234 // Replacement items for decrementInput()
235 CraftReplacements replacements;
239 A shapeless crafting definition
240 Supported crafting method: CRAFT_METHOD_NORMAL.
241 Input items can arranged in any way.
243 class CraftDefinitionShapeless: public CraftDefinition
246 CraftDefinitionShapeless() = delete;
247 CraftDefinitionShapeless(
248 const std::string &output_,
249 const std::vector<std::string> &recipe_,
250 const CraftReplacements &replacements_);
252 virtual ~CraftDefinitionShapeless() = default;
254 virtual std::string getName() const;
255 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
256 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
257 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
258 virtual void decrementInput(CraftInput &input,
259 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
261 virtual u64 getHash(CraftHashType type) const;
263 virtual void initHash(IGameDef *gamedef);
265 virtual std::string dump() const;
270 // Recipe list (itemstrings)
271 std::vector<std::string> recipe;
272 // Recipe list (item names)
273 std::vector<std::string> recipe_names;
274 // bool indicating if initHash has been called already
275 bool hash_inited = false;
276 // Replacement items for decrementInput()
277 CraftReplacements replacements;
281 Tool repair crafting definition
282 Supported crafting method: CRAFT_METHOD_NORMAL.
283 Put two damaged tools into the crafting grid, get one tool back.
284 There should only be one crafting definition of this type.
286 class CraftDefinitionToolRepair: public CraftDefinition
289 CraftDefinitionToolRepair() = delete;
290 CraftDefinitionToolRepair(float additional_wear_);
292 virtual ~CraftDefinitionToolRepair() = default;
294 virtual std::string getName() const;
295 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
296 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
297 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
298 virtual void decrementInput(CraftInput &input,
299 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
301 virtual u64 getHash(CraftHashType type) const { return 2; }
303 virtual void initHash(IGameDef *gamedef)
305 hash_type = CRAFT_HASH_TYPE_COUNT;
308 virtual std::string dump() const;
311 // This is a constant that is added to the wear of the result.
312 // May be positive or negative, allowed range [-1,1].
313 // 1 = new tool is completely broken
314 // 0 = simply add remaining uses of both input tools
315 // -1 = new tool is completely pristine
316 float additional_wear = 0.0f;
320 A cooking (in furnace) definition
321 Supported crafting method: CRAFT_METHOD_COOKING.
323 class CraftDefinitionCooking: public CraftDefinition
326 CraftDefinitionCooking() = delete;
327 CraftDefinitionCooking(
328 const std::string &output_,
329 const std::string &recipe_,
331 const CraftReplacements &replacements_);
333 virtual ~CraftDefinitionCooking() = default;
335 virtual std::string getName() const;
336 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
337 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
338 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
339 virtual void decrementInput(CraftInput &input,
340 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
342 virtual u64 getHash(CraftHashType type) const;
344 virtual void initHash(IGameDef *gamedef);
346 virtual std::string dump() const;
354 std::string recipe_name;
355 // bool indicating if initHash has been called already
356 bool hash_inited = false;
359 // Replacement items for decrementInput()
360 CraftReplacements replacements;
364 A fuel (for furnace) definition
365 Supported crafting method: CRAFT_METHOD_FUEL.
367 class CraftDefinitionFuel: public CraftDefinition
370 CraftDefinitionFuel() = delete;
372 const std::string &recipe_,
374 const CraftReplacements &replacements_);
376 virtual ~CraftDefinitionFuel() = default;
378 virtual std::string getName() const;
379 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
380 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
381 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
382 virtual void decrementInput(CraftInput &input,
383 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
385 virtual u64 getHash(CraftHashType type) const;
387 virtual void initHash(IGameDef *gamedef);
389 virtual std::string dump() const;
395 std::string recipe_name;
396 // bool indicating if initHash has been called already
397 bool hash_inited = false;
400 // Replacement items for decrementInput()
401 CraftReplacements replacements;
405 Crafting definition manager
407 class ICraftDefManager
410 ICraftDefManager() = default;
411 virtual ~ICraftDefManager() = default;
413 // The main crafting function
414 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
415 std::vector<ItemStack> &output_replacements,
416 bool decrementInput, IGameDef *gamedef) const=0;
417 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
418 IGameDef *gamedef, unsigned limit=0) const=0;
420 // Print crafting recipes for debugging
421 virtual std::string dump() const=0;
424 class IWritableCraftDefManager : public ICraftDefManager
427 IWritableCraftDefManager() = default;
428 virtual ~IWritableCraftDefManager() = default;
430 // The main crafting function
431 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
432 std::vector<ItemStack> &output_replacements,
433 bool decrementInput, IGameDef *gamedef) const=0;
434 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
435 IGameDef *gamedef, unsigned limit=0) const=0;
437 virtual bool clearCraftsByOutput(const CraftOutput &output, IGameDef *gamedef) = 0;
438 virtual bool clearCraftsByInput(const CraftInput &input, IGameDef *gamedef) = 0;
440 // Print crafting recipes for debugging
441 virtual std::string dump() const=0;
443 // Add a crafting definition.
444 // After calling this, the pointer belongs to the manager.
445 virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) = 0;
447 // Delete all crafting definitions
448 virtual void clear()=0;
450 // To be called after all mods are loaded, so that we catch all aliases
451 virtual void initHashes(IGameDef *gamedef) = 0;
454 IWritableCraftDefManager* createCraftDefManager();