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.
20 #ifndef CRAFTDEF_HEADER
21 #define CRAFTDEF_HEADER
28 #include "inventory.h"
33 The crafting method depends on the inventory list
34 that the crafting input comes from.
40 // Cooking something in a furnace
42 // Using something as fuel for a furnace
47 The type a hash can be. The earlier a type is mentioned in this enum,
48 the earlier it is tried at crafting, and the less likely is a collision.
49 Changing order causes changes in behaviour, so know what you do.
53 // Hashes the normalized names of the recipe's elements.
54 // Only recipes without group usage can be found here,
55 // because groups can't be guessed efficiently.
56 CRAFT_HASH_TYPE_ITEM_NAMES,
58 // Counts the non-empty slots.
59 CRAFT_HASH_TYPE_COUNT,
61 // This layer both spares an extra variable, and helps to retain (albeit rarely used) functionality. Maps to 0.
62 // Before hashes are "initialized", all hashes reside here, after initialisation, none are.
63 CRAFT_HASH_TYPE_UNHASHED
66 const int craft_hash_type_max = (int) CRAFT_HASH_TYPE_UNHASHED;
69 Input: The contents of the crafting slots, arranged in matrix form
75 std::vector<ItemStack> items;
78 method(CRAFT_METHOD_NORMAL), width(0), items()
80 CraftInput(CraftMethod method_, unsigned int width_,
81 const std::vector<ItemStack> &items_):
82 method(method_), width(width_), items(items_)
84 std::string dump() const;
88 Output: Result of crafting operation
92 // Used for normal crafting and cooking, itemstring
94 // Used for cooking (cook time) and fuel (burn time), seconds
100 CraftOutput(std::string item_, float time_):
101 item(item_), time(time_)
103 std::string dump() const;
107 A list of replacements. A replacement indicates that a specific
108 input item should not be deleted (when crafting) but replaced with
109 a different item. Each replacements is a pair (itemstring to remove,
110 itemstring to replace with)
112 Example: If ("bucket:bucket_water", "bucket:bucket_empty") is a
113 replacement pair, the crafting input slot that contained a water
114 bucket will contain an empty bucket after crafting.
116 Note: replacements only work correctly when stack_max of the item
117 to be replaced is 1. It is up to the mod writer to ensure this.
119 struct CraftReplacements
121 // List of replacements
122 std::vector<std::pair<std::string, std::string> > pairs;
127 CraftReplacements(std::vector<std::pair<std::string, std::string> > pairs_):
130 std::string dump() const;
131 void serialize(std::ostream &os) const;
132 void deSerialize(std::istream &is);
136 Crafting definition base class
138 class CraftDefinition
142 virtual ~CraftDefinition(){}
144 void serialize(std::ostream &os) const;
145 static CraftDefinition* deSerialize(std::istream &is);
147 // Returns type of crafting definition
148 virtual std::string getName() const=0;
150 // Checks whether the recipe is applicable
151 virtual bool check(const CraftInput &input, IGameDef *gamedef) const=0;
152 // Returns the output structure, meaning depends on crafting method
153 // The implementation can assume that check(input) returns true
154 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const=0;
155 // the inverse of the above
156 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const=0;
157 // Decreases count of every input item
158 virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const=0;
160 virtual CraftHashType getHashType() const = 0;
161 virtual u64 getHash(CraftHashType type) const = 0;
163 // to be called after all mods are loaded, so that we catch all aliases
164 virtual void initHash(IGameDef *gamedef) = 0;
166 virtual std::string dump() const=0;
168 virtual void serializeBody(std::ostream &os) const=0;
169 virtual void deSerializeBody(std::istream &is, int version)=0;
173 A plain-jane (shaped) crafting definition
175 Supported crafting method: CRAFT_METHOD_NORMAL.
176 Requires the input items to be arranged exactly like in the recipe.
178 class CraftDefinitionShaped: public CraftDefinition
181 CraftDefinitionShaped():
182 output(""), width(1), recipe(), hash_inited(false), replacements()
184 CraftDefinitionShaped(
185 const std::string &output_,
187 const std::vector<std::string> &recipe_,
188 const CraftReplacements &replacements_):
189 output(output_), width(width_), recipe(recipe_),
190 hash_inited(false), replacements(replacements_)
192 virtual ~CraftDefinitionShaped(){}
194 virtual std::string getName() const;
195 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
196 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
197 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
198 virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const;
200 virtual CraftHashType getHashType() const;
201 virtual u64 getHash(CraftHashType type) const;
203 virtual void initHash(IGameDef *gamedef);
205 virtual std::string dump() const;
208 virtual void serializeBody(std::ostream &os) const;
209 virtual void deSerializeBody(std::istream &is, int version);
216 // Recipe matrix (itemstrings)
217 std::vector<std::string> recipe;
218 // Recipe matrix (item names)
219 std::vector<std::string> recipe_names;
220 // bool indicating if initHash has been called already
222 // Replacement items for decrementInput()
223 CraftReplacements replacements;
227 A shapeless crafting definition
228 Supported crafting method: CRAFT_METHOD_NORMAL.
229 Input items can arranged in any way.
231 class CraftDefinitionShapeless: public CraftDefinition
234 CraftDefinitionShapeless():
235 output(""), recipe(), hash_inited(false), replacements()
237 CraftDefinitionShapeless(
238 const std::string &output_,
239 const std::vector<std::string> &recipe_,
240 const CraftReplacements &replacements_):
241 output(output_), recipe(recipe_),
242 hash_inited(false), replacements(replacements_)
244 virtual ~CraftDefinitionShapeless(){}
246 virtual std::string getName() const;
247 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
248 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
249 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
250 virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const;
252 virtual CraftHashType getHashType() const;
253 virtual u64 getHash(CraftHashType type) const;
255 virtual void initHash(IGameDef *gamedef);
257 virtual std::string dump() const;
260 virtual void serializeBody(std::ostream &os) const;
261 virtual void deSerializeBody(std::istream &is, int version);
266 // Recipe list (itemstrings)
267 std::vector<std::string> recipe;
268 // Recipe list (item names)
269 std::vector<std::string> recipe_names;
270 // bool indicating if initHash has been called already
272 // Replacement items for decrementInput()
273 CraftReplacements replacements;
277 Tool repair crafting definition
278 Supported crafting method: CRAFT_METHOD_NORMAL.
279 Put two damaged tools into the crafting grid, get one tool back.
280 There should only be one crafting definition of this type.
282 class CraftDefinitionToolRepair: public CraftDefinition
285 CraftDefinitionToolRepair():
288 CraftDefinitionToolRepair(float additional_wear_):
289 additional_wear(additional_wear_)
291 virtual ~CraftDefinitionToolRepair(){}
293 virtual std::string getName() const;
294 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
295 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
296 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
297 virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const;
299 virtual CraftHashType getHashType() const { return CRAFT_HASH_TYPE_COUNT; }
300 virtual u64 getHash(CraftHashType type) const { return 2; }
302 virtual void initHash(IGameDef *gamedef) {}
304 virtual std::string dump() const;
307 virtual void serializeBody(std::ostream &os) const;
308 virtual void deSerializeBody(std::istream &is, int version);
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;
320 A cooking (in furnace) definition
321 Supported crafting method: CRAFT_METHOD_COOKING.
323 class CraftDefinitionCooking: public CraftDefinition
326 CraftDefinitionCooking():
327 output(""), recipe(""), hash_inited(false), cooktime()
329 CraftDefinitionCooking(
330 const std::string &output_,
331 const std::string &recipe_,
333 const CraftReplacements &replacements_):
334 output(output_), recipe(recipe_), hash_inited(false),
335 cooktime(cooktime_), replacements(replacements_)
337 virtual ~CraftDefinitionCooking(){}
339 virtual std::string getName() const;
340 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
341 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
342 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
343 virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const;
345 virtual CraftHashType getHashType() const;
346 virtual u64 getHash(CraftHashType type) const;
348 virtual void initHash(IGameDef *gamedef);
350 virtual std::string dump() const;
353 virtual void serializeBody(std::ostream &os) const;
354 virtual void deSerializeBody(std::istream &is, int version);
362 std::string recipe_name;
363 // bool indicating if initHash has been called already
367 // Replacement items for decrementInput()
368 CraftReplacements replacements;
372 A fuel (for furnace) definition
373 Supported crafting method: CRAFT_METHOD_FUEL.
375 class CraftDefinitionFuel: public CraftDefinition
378 CraftDefinitionFuel():
379 recipe(""), hash_inited(false), burntime()
381 CraftDefinitionFuel(std::string recipe_,
383 const CraftReplacements &replacements_):
384 recipe(recipe_), hash_inited(false), burntime(burntime_), replacements(replacements_)
386 virtual ~CraftDefinitionFuel(){}
388 virtual std::string getName() const;
389 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
390 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
391 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
392 virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const;
394 virtual CraftHashType getHashType() const;
395 virtual u64 getHash(CraftHashType type) const;
397 virtual void initHash(IGameDef *gamedef);
399 virtual std::string dump() const;
402 virtual void serializeBody(std::ostream &os) const;
403 virtual void deSerializeBody(std::istream &is, int version);
409 std::string recipe_name;
410 // bool indicating if initHash has been called already
414 // Replacement items for decrementInput()
415 CraftReplacements replacements;
419 Crafting definition manager
421 class ICraftDefManager
425 virtual ~ICraftDefManager(){}
427 // The main crafting function
428 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
429 bool decrementInput, IGameDef *gamedef) const=0;
430 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
431 IGameDef *gamedef, unsigned limit=0) const=0;
433 // Print crafting recipes for debugging
434 virtual std::string dump() const=0;
437 class IWritableCraftDefManager : public ICraftDefManager
440 IWritableCraftDefManager(){}
441 virtual ~IWritableCraftDefManager(){}
443 // The main crafting function
444 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
445 bool decrementInput, IGameDef *gamedef) const=0;
446 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
447 IGameDef *gamedef, unsigned limit=0) const=0;
449 // Print crafting recipes for debugging
450 virtual std::string dump() const=0;
452 // Add a crafting definition.
453 // After calling this, the pointer belongs to the manager.
454 virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) = 0;
456 // Delete all crafting definitions
457 virtual void clear()=0;
459 // To be called after all mods are loaded, so that we catch all aliases
460 virtual void initHashes(IGameDef *gamedef) = 0;
463 IWritableCraftDefManager* createCraftDefManager();