Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
craftdef.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <string>
8#include <iostream>
9#include <vector>
10#include <utility>
11#include "gamedef.h"
12#include "inventory.h"
13
14/*
15 Crafting methods.
16
17 The crafting method depends on the inventory list
18 that the crafting input comes from.
19*/
21{
22 // Crafting grid
24 // Cooking something in a furnace
26 // Using something as fuel for a furnace
28};
29
30/*
31 The type a hash can be. The earlier a type is mentioned in this enum,
32 the earlier it is tried at crafting, and the less likely is a collision.
33 Changing order causes changes in behavior, so know what you do.
34 */
36{
37 // Hashes the normalized names of the recipe's elements.
38 // Only recipes without group usage can be found here,
39 // because groups can't be guessed efficiently.
41
42 // Counts the non-empty slots.
44
45 // This layer both spares an extra variable, and helps to retain (albeit rarely used) functionality. Maps to 0.
46 // Before hashes are "initialized", all hashes reside here, after initialisation, none are.
48
49};
51
52/*
53 Input: The contents of the crafting slots, arranged in matrix form
54*/
56{
58 unsigned int width = 0;
59 std::vector<ItemStack> items;
60
61 CraftInput() = default;
62
63 CraftInput(CraftMethod method_, unsigned int width_,
64 const std::vector<ItemStack> &items_):
65 method(method_), width(width_), items(items_)
66 {}
67
68 // Returns true if all items are empty.
69 bool empty() const;
70
71 std::string dump() const;
72};
73
74/*
75 Output: Result of crafting operation
76*/
78{
79 // Used for normal crafting and cooking, itemstring
80 std::string item = "";
81 // Used for cooking (cook time) and fuel (burn time), seconds
82 float time = 0.0f;
83
84 CraftOutput() = default;
85
86 CraftOutput(const std::string &item_, float time_):
87 item(item_), time(time_)
88 {}
89 std::string dump() const;
90};
91
92/*
93 A list of replacements. A replacement indicates that a specific
94 input item should not be deleted (when crafting) but replaced with
95 a different item. Each replacements is a pair (itemstring to remove,
96 itemstring to replace with)
97
98 Example: If ("bucket:bucket_water", "bucket:bucket_empty") is a
99 replacement pair, the crafting input slot that contained a water
100 bucket will contain an empty bucket after crafting.
101*/
103{
104 // List of replacements
105 std::vector<std::pair<std::string, std::string> > pairs;
106
107 CraftReplacements() = default;
108 CraftReplacements(const std::vector<std::pair<std::string, std::string> > &pairs_):
109 pairs(pairs_)
110 {}
111 std::string dump() const;
112};
113
114/*
115 Crafting definition base class
116*/
118{
119public:
120 /*
121 Craft recipe priorities, from low to high
122
123 Recipes are searched from latest to first.
124 If a recipe with higher priority than a previous found one is
125 encountered, it is selected instead.
126 */
136
137 CraftDefinition() = default;
138 virtual ~CraftDefinition() = default;
139
140 // Returns type of crafting definition
141 virtual std::string getName() const=0;
142
143 // Checks whether the recipe is applicable
144 virtual bool check(const CraftInput &input, IGameDef *gamedef) const=0;
146 {
147 return priority;
148 }
149 // Returns the output structure, meaning depends on crafting method
150 // The implementation can assume that check(input) returns true
151 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const=0;
152 // the inverse of the above
153 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const=0;
154 // Decreases count of every input item
155 virtual void decrementInput(CraftInput &input,
156 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const=0;
157
159 {
160 return hash_type;
161 }
162 virtual u64 getHash(CraftHashType type) const = 0;
163
164 // to be called after all mods are loaded, so that we catch all aliases
165 virtual void initHash(IGameDef *gamedef) = 0;
166
167 virtual std::string dump() const=0;
168
169protected:
172};
173
174/*
175 A plain-jane (shaped) crafting definition
176
177 Supported crafting method: CRAFT_METHOD_NORMAL.
178 Requires the input items to be arranged exactly like in the recipe.
179*/
181{
182public:
185 const std::string &output_,
186 unsigned int width_,
187 const std::vector<std::string> &recipe_,
188 const CraftReplacements &replacements_);
189
190 virtual ~CraftDefinitionShaped() = default;
191
192 virtual std::string getName() const;
193 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
194 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
195 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
196 virtual void decrementInput(CraftInput &input,
197 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
198
199 virtual u64 getHash(CraftHashType type) const;
200
201 virtual void initHash(IGameDef *gamedef);
202
203 virtual std::string dump() const;
204
205private:
206 // Output itemstring
207 std::string output = "";
208 // Width of recipe
209 unsigned int width = 1;
210 // Recipe matrix (itemstrings)
211 std::vector<std::string> recipe;
212 // Recipe matrix (item names)
213 std::vector<std::string> recipe_names;
214 // bool indicating if initHash has been called already
215 bool hash_inited = false;
216 // Replacement items for decrementInput()
218};
219
220/*
221 A shapeless crafting definition
222 Supported crafting method: CRAFT_METHOD_NORMAL.
223 Input items can arranged in any way.
224*/
226{
227public:
230 const std::string &output_,
231 const std::vector<std::string> &recipe_,
232 const CraftReplacements &replacements_);
233
234 virtual ~CraftDefinitionShapeless() = default;
235
236 virtual std::string getName() const;
237 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
238 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
239 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
240 virtual void decrementInput(CraftInput &input,
241 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
242
243 virtual u64 getHash(CraftHashType type) const;
244
245 virtual void initHash(IGameDef *gamedef);
246
247 virtual std::string dump() const;
248
249private:
250 // Output itemstring
251 std::string output;
252 // Recipe list (itemstrings)
253 std::vector<std::string> recipe;
254 // Recipe list (item names), sorted
255 std::vector<std::string> recipe_names;
256 // bool indicating if initHash has been called already
257 bool hash_inited = false;
258 // Replacement items for decrementInput()
260};
261
262/*
263 Tool repair crafting definition
264 Supported crafting method: CRAFT_METHOD_NORMAL.
265 Put two damaged tools into the crafting grid, get one tool back.
266 There should only be one crafting definition of this type.
267*/
269{
270public:
272 CraftDefinitionToolRepair(float additional_wear_);
273
274 virtual ~CraftDefinitionToolRepair() = default;
275
276 virtual std::string getName() const;
277 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
278 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
279 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
280 virtual void decrementInput(CraftInput &input,
281 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
282
283 virtual u64 getHash(CraftHashType type) const { return 2; }
284
285 virtual void initHash(IGameDef *gamedef)
286 {
288 }
289
290 virtual std::string dump() const;
291
292private:
293 // This is a constant that is added to the wear of the result.
294 // May be positive or negative, allowed range [-1,1].
295 // 1 = new tool is completely broken
296 // 0 = simply add remaining uses of both input tools
297 // -1 = new tool is completely pristine
298 float additional_wear = 0.0f;
299};
300
301/*
302 A cooking (in furnace) definition
303 Supported crafting method: CRAFT_METHOD_COOKING.
304*/
306{
307public:
310 const std::string &output_,
311 const std::string &recipe_,
312 float cooktime_,
313 const CraftReplacements &replacements_);
314
315 virtual ~CraftDefinitionCooking() = default;
316
317 virtual std::string getName() const;
318 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
319 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
320 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
321 virtual void decrementInput(CraftInput &input,
322 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
323
324 virtual u64 getHash(CraftHashType type) const;
325
326 virtual void initHash(IGameDef *gamedef);
327
328 virtual std::string dump() const;
329
330private:
331 // Output itemstring
332 std::string output;
333 // Recipe itemstring
334 std::string recipe;
335 // Recipe item name
336 std::string recipe_name;
337 // bool indicating if initHash has been called already
338 bool hash_inited = false;
339 // Time in seconds
340 float cooktime;
341 // Replacement items for decrementInput()
343};
344
345/*
346 A fuel (for furnace) definition
347 Supported crafting method: CRAFT_METHOD_FUEL.
348*/
350{
351public:
354 const std::string &recipe_,
355 float burntime_,
356 const CraftReplacements &replacements_);
357
358 virtual ~CraftDefinitionFuel() = default;
359
360 virtual std::string getName() const;
361 virtual bool check(const CraftInput &input, IGameDef *gamedef) const;
362 virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const;
363 virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const;
364 virtual void decrementInput(CraftInput &input,
365 std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;
366
367 virtual u64 getHash(CraftHashType type) const;
368
369 virtual void initHash(IGameDef *gamedef);
370
371 virtual std::string dump() const;
372
373private:
374 // Recipe itemstring
375 std::string recipe;
376 // Recipe item name
377 std::string recipe_name;
378 // bool indicating if initHash has been called already
379 bool hash_inited = false;
380 // Time in seconds
381 float burntime;
382 // Replacement items for decrementInput()
384};
385
386/*
387 Crafting definition manager
388*/
390{
391public:
392 ICraftDefManager() = default;
393 virtual ~ICraftDefManager() = default;
394
407 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
408 std::vector<ItemStack> &output_replacements,
409 bool decrementInput, IGameDef *gamedef) const=0;
410
411 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
412 IGameDef *gamedef, unsigned limit=0) const=0;
413
414 // Print crafting recipes for debugging
415 virtual std::string dump() const=0;
416};
417
419{
420public:
422 virtual ~IWritableCraftDefManager() = default;
423
424 // The main crafting function
425 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
426 std::vector<ItemStack> &output_replacements,
427 bool decrementInput, IGameDef *gamedef) const=0;
428 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
429 IGameDef *gamedef, unsigned limit=0) const=0;
430
431 virtual bool clearCraftsByOutput(const CraftOutput &output, IGameDef *gamedef) = 0;
432 virtual bool clearCraftsByInput(const CraftInput &input, IGameDef *gamedef) = 0;
433
434 // Print crafting recipes for debugging
435 virtual std::string dump() const=0;
436
437 // Add a crafting definition.
438 // After calling this, the pointer belongs to the manager.
439 virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) = 0;
440
441 // Delete all crafting definitions
442 virtual void clear()=0;
443
444 // To be called after all mods are loaded, so that we catch all aliases
445 virtual void initHashes(IGameDef *gamedef) = 0;
446};
447
Definition craftdef.h:306
CraftDefinitionCooking()=delete
std::string recipe
Definition craftdef.h:334
virtual ~CraftDefinitionCooking()=default
virtual std::string dump() const
Definition craftdef.cpp:907
std::string recipe_name
Definition craftdef.h:336
std::string output
Definition craftdef.h:332
virtual void initHash(IGameDef *gamedef)
Definition craftdef.cpp:894
virtual u64 getHash(CraftHashType type) const
Definition craftdef.cpp:879
virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const
Definition craftdef.cpp:866
float cooktime
Definition craftdef.h:340
virtual bool check(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:835
CraftReplacements replacements
Definition craftdef.h:342
virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:861
bool hash_inited
Definition craftdef.h:338
virtual std::string getName() const
Definition craftdef.cpp:830
virtual void decrementInput(CraftInput &input, std::vector< ItemStack > &output_replacements, IGameDef *gamedef) const
Definition craftdef.cpp:873
Definition craftdef.h:350
virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:964
virtual void decrementInput(CraftInput &input, std::vector< ItemStack > &output_replacements, IGameDef *gamedef) const
Definition craftdef.cpp:976
std::string recipe
Definition craftdef.h:375
virtual ~CraftDefinitionFuel()=default
std::string recipe_name
Definition craftdef.h:377
virtual u64 getHash(CraftHashType type) const
Definition craftdef.cpp:982
virtual void initHash(IGameDef *gamedef)
Definition craftdef.cpp:997
CraftReplacements replacements
Definition craftdef.h:383
virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const
Definition craftdef.cpp:969
virtual std::string dump() const
Definition craftdef.cpp:1010
virtual bool check(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:938
float burntime
Definition craftdef.h:381
CraftDefinitionFuel()=delete
virtual std::string getName() const
Definition craftdef.cpp:933
bool hash_inited
Definition craftdef.h:379
Definition craftdef.h:181
unsigned int width
Definition craftdef.h:209
CraftReplacements replacements
Definition craftdef.h:217
bool hash_inited
Definition craftdef.h:215
virtual std::string dump() const
Definition craftdef.cpp:452
virtual void initHash(IGameDef *gamedef)
Definition craftdef.cpp:439
std::vector< std::string > recipe_names
Definition craftdef.h:213
std::vector< std::string > recipe
Definition craftdef.h:211
virtual std::string getName() const
Definition craftdef.cpp:341
virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const
Definition craftdef.cpp:417
virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:412
std::string output
Definition craftdef.h:207
CraftDefinitionShaped()=delete
virtual u64 getHash(CraftHashType type) const
Definition craftdef.cpp:428
virtual bool check(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:346
virtual void decrementInput(CraftInput &input, std::vector< ItemStack > &output_replacements, IGameDef *gamedef) const
Definition craftdef.cpp:422
virtual ~CraftDefinitionShaped()=default
Definition craftdef.h:226
virtual ~CraftDefinitionShapeless()=default
virtual bool check(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:589
std::vector< std::string > recipe_names
Definition craftdef.h:255
virtual std::string getName() const
Definition craftdef.cpp:477
virtual void initHash(IGameDef *gamedef)
Definition craftdef.cpp:690
virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const
Definition craftdef.cpp:671
bool hash_inited
Definition craftdef.h:257
virtual std::string dump() const
Definition craftdef.cpp:704
virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:666
virtual void decrementInput(CraftInput &input, std::vector< ItemStack > &output_replacements, IGameDef *gamedef) const
Definition craftdef.cpp:676
std::vector< std::string > recipe
Definition craftdef.h:253
CraftReplacements replacements
Definition craftdef.h:259
virtual u64 getHash(CraftHashType type) const
Definition craftdef.cpp:682
std::string output
Definition craftdef.h:251
Definition craftdef.h:269
virtual u64 getHash(CraftHashType type) const
Definition craftdef.h:283
virtual ~CraftDefinitionToolRepair()=default
virtual std::string getName() const
Definition craftdef.cpp:751
float additional_wear
Definition craftdef.h:298
virtual void decrementInput(CraftInput &input, std::vector< ItemStack > &output_replacements, IGameDef *gamedef) const
Definition craftdef.cpp:800
virtual void initHash(IGameDef *gamedef)
Definition craftdef.h:285
virtual std::string dump() const
Definition craftdef.cpp:806
virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:777
virtual bool check(const CraftInput &input, IGameDef *gamedef) const
Definition craftdef.cpp:756
virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const
Definition craftdef.cpp:793
Definition craftdef.h:118
RecipePriority
Definition craftdef.h:128
@ PRIORITY_TOOLREPAIR
Definition craftdef.h:130
@ PRIORITY_SHAPELESS
Definition craftdef.h:132
@ PRIORITY_NO_RECIPE
Definition craftdef.h:129
@ PRIORITY_SHAPED_AND_GROUPS
Definition craftdef.h:133
@ PRIORITY_SHAPELESS_AND_GROUPS
Definition craftdef.h:131
@ PRIORITY_SHAPED
Definition craftdef.h:134
RecipePriority getPriority() const
Definition craftdef.h:145
virtual bool check(const CraftInput &input, IGameDef *gamedef) const =0
CraftHashType hash_type
Definition craftdef.h:170
CraftDefinition()=default
CraftHashType getHashType() const
Definition craftdef.h:158
virtual std::string dump() const =0
virtual void initHash(IGameDef *gamedef)=0
virtual ~CraftDefinition()=default
virtual u64 getHash(CraftHashType type) const =0
virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const =0
virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const =0
virtual std::string getName() const =0
RecipePriority priority
Definition craftdef.h:171
virtual void decrementInput(CraftInput &input, std::vector< ItemStack > &output_replacements, IGameDef *gamedef) const =0
Definition craftdef.h:390
virtual bool getCraftResult(CraftInput &input, CraftOutput &output, std::vector< ItemStack > &output_replacements, bool decrementInput, IGameDef *gamedef) const =0
The main crafting function.
virtual std::vector< CraftDefinition * > getCraftRecipes(CraftOutput &output, IGameDef *gamedef, unsigned limit=0) const =0
virtual ~ICraftDefManager()=default
ICraftDefManager()=default
virtual std::string dump() const =0
Definition gamedef.h:36
Definition craftdef.h:419
virtual bool clearCraftsByOutput(const CraftOutput &output, IGameDef *gamedef)=0
virtual ~IWritableCraftDefManager()=default
virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef)=0
virtual std::string dump() const =0
virtual void initHashes(IGameDef *gamedef)=0
IWritableCraftDefManager()=default
virtual bool clearCraftsByInput(const CraftInput &input, IGameDef *gamedef)=0
virtual bool getCraftResult(CraftInput &input, CraftOutput &output, std::vector< ItemStack > &output_replacements, bool decrementInput, IGameDef *gamedef) const =0
The main crafting function.
virtual std::vector< CraftDefinition * > getCraftRecipes(CraftOutput &output, IGameDef *gamedef, unsigned limit=0) const =0
virtual void clear()=0
CraftHashType
Definition craftdef.h:36
@ CRAFT_HASH_TYPE_ITEM_NAMES
Definition craftdef.h:40
@ CRAFT_HASH_TYPE_UNHASHED
Definition craftdef.h:47
@ CRAFT_HASH_TYPE_COUNT
Definition craftdef.h:43
IWritableCraftDefManager * createCraftDefManager()
Definition craftdef.cpp:1241
CraftMethod
Definition craftdef.h:21
@ CRAFT_METHOD_NORMAL
Definition craftdef.h:23
@ CRAFT_METHOD_COOKING
Definition craftdef.h:25
@ CRAFT_METHOD_FUEL
Definition craftdef.h:27
const int craft_hash_type_max
Definition craftdef.h:50
Definition craftdef.h:56
unsigned int width
Definition craftdef.h:58
CraftInput()=default
bool empty() const
Definition craftdef.cpp:277
CraftMethod method
Definition craftdef.h:57
std::string dump() const
Definition craftdef.cpp:286
CraftInput(CraftMethod method_, unsigned int width_, const std::vector< ItemStack > &items_)
Definition craftdef.h:63
std::vector< ItemStack > items
Definition craftdef.h:59
Definition craftdef.h:78
CraftOutput(const std::string &item_, float time_)
Definition craftdef.h:86
std::string dump() const
Definition craftdef.cpp:298
CraftOutput()=default
std::string item
Definition craftdef.h:80
float time
Definition craftdef.h:82
Definition craftdef.h:103
std::string dump() const
Definition craftdef.cpp:309
CraftReplacements(const std::vector< std::pair< std::string, std::string > > &pairs_)
Definition craftdef.h:108
CraftReplacements()=default
std::vector< std::pair< std::string, std::string > > pairs
Definition craftdef.h:105