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;
78 CraftInput(CraftMethod method_, unsigned int width_,
79 const std::vector<ItemStack> &items_):
80 method(method_), width(width_), items(items_)
83 std::string dump() const;
87 Output: Result of crafting operation
91 // Used for normal crafting and cooking, itemstring
92 std::string item = "";
93 // Used for cooking (cook time) and fuel (burn time), seconds
98 CraftOutput(const std::string &item_, float time_):
99 item(item_), time(time_)
101 std::string dump() const;
105 A list of replacements. A replacement indicates that a specific
106 input item should not be deleted (when crafting) but replaced with
107 a different item. Each replacements is a pair (itemstring to remove,
108 itemstring to replace with)
110 Example: If ("bucket:bucket_water", "bucket:bucket_empty") is a
111 replacement pair, the crafting input slot that contained a water
112 bucket will contain an empty bucket after crafting.
114 Note: replacements only work correctly when stack_max of the item
115 to be replaced is 1. It is up to the mod writer to ensure this.
117 struct CraftReplacements
119 // List of replacements
120 std::vector<std::pair<std::string, std::string> > pairs;
125 CraftReplacements(const std::vector<std::pair<std::string, std::string> > &pairs_):
128 std::string dump() const;
132 Crafting definition base class
134 class CraftDefinition
138 virtual ~CraftDefinition(){}
140 // Returns type of crafting definition
141 virtual std::string getName() const=0;
143 // Checks whether the recipe is applicable
144 virtual bool check(const CraftInput &input, IGameDef *gamedef) const=0;
145 // Returns the output structure, meaning depends on crafting method
146 // The implementation can assume that check(input) returns true
147 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const=0;
148 // the inverse of the above
149 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const=0;
150 // Decreases count of every input item
151 virtual void decrementInput(CraftInput &input,
152 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const=0;
154 virtual CraftHashType getHashType() const = 0;
155 virtual u64 getHash(CraftHashType type) const = 0;
157 // to be called after all mods are loaded, so that we catch all aliases
158 virtual void initHash(IGameDef *gamedef) = 0;
160 virtual std::string dump() const=0;
164 A plain-jane (shaped) crafting definition
166 Supported crafting method: CRAFT_METHOD_NORMAL.
167 Requires the input items to be arranged exactly like in the recipe.
169 class CraftDefinitionShaped: public CraftDefinition
172 CraftDefinitionShaped() {}
174 CraftDefinitionShaped(
175 const std::string &output_,
177 const std::vector<std::string> &recipe_,
178 const CraftReplacements &replacements_):
179 output(output_), width(width_), recipe(recipe_),
180 replacements(replacements_)
182 virtual ~CraftDefinitionShaped(){}
184 virtual std::string getName() const;
185 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
186 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
187 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
188 virtual void decrementInput(CraftInput &input,
189 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
191 virtual CraftHashType getHashType() const;
192 virtual u64 getHash(CraftHashType type) const;
194 virtual void initHash(IGameDef *gamedef);
196 virtual std::string dump() const;
200 std::string output = "";
202 unsigned int width = 1;
203 // Recipe matrix (itemstrings)
204 std::vector<std::string> recipe;
205 // Recipe matrix (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;
214 A shapeless crafting definition
215 Supported crafting method: CRAFT_METHOD_NORMAL.
216 Input items can arranged in any way.
218 class CraftDefinitionShapeless: public CraftDefinition
221 CraftDefinitionShapeless():
222 output(""), recipe(), hash_inited(false), replacements()
224 CraftDefinitionShapeless(
225 const std::string &output_,
226 const std::vector<std::string> &recipe_,
227 const CraftReplacements &replacements_):
228 output(output_), recipe(recipe_),
229 hash_inited(false), replacements(replacements_)
231 virtual ~CraftDefinitionShapeless(){}
233 virtual std::string getName() const;
234 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
235 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
236 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
237 virtual void decrementInput(CraftInput &input,
238 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
240 virtual CraftHashType getHashType() const;
241 virtual u64 getHash(CraftHashType type) const;
243 virtual void initHash(IGameDef *gamedef);
245 virtual std::string dump() const;
250 // Recipe list (itemstrings)
251 std::vector<std::string> recipe;
252 // Recipe list (item names)
253 std::vector<std::string> recipe_names;
254 // bool indicating if initHash has been called already
256 // Replacement items for decrementInput()
257 CraftReplacements replacements;
261 Tool repair crafting definition
262 Supported crafting method: CRAFT_METHOD_NORMAL.
263 Put two damaged tools into the crafting grid, get one tool back.
264 There should only be one crafting definition of this type.
266 class CraftDefinitionToolRepair: public CraftDefinition
269 CraftDefinitionToolRepair():
272 CraftDefinitionToolRepair(float additional_wear_):
273 additional_wear(additional_wear_)
275 virtual ~CraftDefinitionToolRepair(){}
277 virtual std::string getName() const;
278 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
279 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
280 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
281 virtual void decrementInput(CraftInput &input,
282 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
284 virtual CraftHashType getHashType() const { return CRAFT_HASH_TYPE_COUNT; }
285 virtual u64 getHash(CraftHashType type) const { return 2; }
287 virtual void initHash(IGameDef *gamedef) {}
289 virtual std::string dump() const;
292 // This is a constant that is added to the wear of the result.
293 // May be positive or negative, allowed range [-1,1].
294 // 1 = new tool is completely broken
295 // 0 = simply add remaining uses of both input tools
296 // -1 = new tool is completely pristine
297 float additional_wear;
301 A cooking (in furnace) definition
302 Supported crafting method: CRAFT_METHOD_COOKING.
304 class CraftDefinitionCooking: public CraftDefinition
307 CraftDefinitionCooking():
308 output(""), recipe(""), hash_inited(false), cooktime()
310 CraftDefinitionCooking(
311 const std::string &output_,
312 const std::string &recipe_,
314 const CraftReplacements &replacements_):
315 output(output_), recipe(recipe_), hash_inited(false),
316 cooktime(cooktime_), replacements(replacements_)
318 virtual ~CraftDefinitionCooking(){}
320 virtual std::string getName() const;
321 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
322 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
323 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
324 virtual void decrementInput(CraftInput &input,
325 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
327 virtual CraftHashType getHashType() const;
328 virtual u64 getHash(CraftHashType type) const;
330 virtual void initHash(IGameDef *gamedef);
332 virtual std::string dump() const;
340 std::string recipe_name;
341 // bool indicating if initHash has been called already
345 // Replacement items for decrementInput()
346 CraftReplacements replacements;
350 A fuel (for furnace) definition
351 Supported crafting method: CRAFT_METHOD_FUEL.
353 class CraftDefinitionFuel: public CraftDefinition
356 CraftDefinitionFuel():
357 recipe(""), hash_inited(false), burntime()
359 CraftDefinitionFuel(const std::string &recipe_,
361 const CraftReplacements &replacements_):
365 replacements(replacements_)
367 virtual ~CraftDefinitionFuel(){}
369 virtual std::string getName() const;
370 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
371 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
372 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
373 virtual void decrementInput(CraftInput &input,
374 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
376 virtual CraftHashType getHashType() const;
377 virtual u64 getHash(CraftHashType type) const;
379 virtual void initHash(IGameDef *gamedef);
381 virtual std::string dump() const;
387 std::string recipe_name;
388 // bool indicating if initHash has been called already
392 // Replacement items for decrementInput()
393 CraftReplacements replacements;
397 Crafting definition manager
399 class ICraftDefManager
403 virtual ~ICraftDefManager(){}
405 // The main crafting function
406 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
407 std::vector<ItemStack> &output_replacements,
408 bool decrementInput, IGameDef *gamedef) const=0;
409 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
410 IGameDef *gamedef, unsigned limit=0) const=0;
412 // Print crafting recipes for debugging
413 virtual std::string dump() const=0;
416 class IWritableCraftDefManager : public ICraftDefManager
419 IWritableCraftDefManager(){}
420 virtual ~IWritableCraftDefManager(){}
422 // The main crafting function
423 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
424 std::vector<ItemStack> &output_replacements,
425 bool decrementInput, IGameDef *gamedef) const=0;
426 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
427 IGameDef *gamedef, unsigned limit=0) const=0;
429 virtual bool clearCraftRecipesByOutput(const CraftOutput &output, IGameDef *gamedef) = 0;
430 virtual bool clearCraftRecipesByInput(CraftMethod craft_method,
431 unsigned int craft_grid_width, const std::vector<std::string> &recipe, IGameDef *gamedef) = 0;
433 // Print crafting recipes for debugging
434 virtual std::string dump() const=0;
436 // Add a crafting definition.
437 // After calling this, the pointer belongs to the manager.
438 virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) = 0;
440 // Delete all crafting definitions
441 virtual void clear()=0;
443 // To be called after all mods are loaded, so that we catch all aliases
444 virtual void initHashes(IGameDef *gamedef) = 0;
447 IWritableCraftDefManager* createCraftDefManager();