X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fcraftdef.h;h=7c14e702a1fb2ff2176fc814f4803a6d499b03b5;hb=0e88176db8ce9f3fb631feb56bdd29d4ada054f5;hp=cebb2d7ae2d82771b1d174f8704c757ed7f44763;hpb=17ba584fe254eeaee3489cc20e03810a59f3ef9b;p=dragonfireclient.git diff --git a/src/craftdef.h b/src/craftdef.h index cebb2d7ae..7c14e702a 100644 --- a/src/craftdef.h +++ b/src/craftdef.h @@ -17,8 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#ifndef CRAFTDEF_HEADER -#define CRAFTDEF_HEADER +#pragma once #include #include @@ -70,17 +69,20 @@ const int craft_hash_type_max = (int) CRAFT_HASH_TYPE_UNHASHED; */ struct CraftInput { - CraftMethod method; - unsigned int width; + CraftMethod method = CRAFT_METHOD_NORMAL; + unsigned int width = 0; std::vector items; - CraftInput(): - method(CRAFT_METHOD_NORMAL), width(0), items() - {} + CraftInput() = default; + CraftInput(CraftMethod method_, unsigned int width_, const std::vector &items_): method(method_), width(width_), items(items_) {} + + // Returns true if all items are empty. + bool empty() const; + std::string dump() const; }; @@ -90,14 +92,13 @@ struct CraftInput struct CraftOutput { // Used for normal crafting and cooking, itemstring - std::string item; + std::string item = ""; // Used for cooking (cook time) and fuel (burn time), seconds - float time; + float time = 0.0f; - CraftOutput(): - item(""), time(0) - {} - CraftOutput(std::string item_, float time_): + CraftOutput() = default; + + CraftOutput(const std::string &item_, float time_): item(item_), time(time_) {} std::string dump() const; @@ -112,19 +113,14 @@ struct CraftOutput Example: If ("bucket:bucket_water", "bucket:bucket_empty") is a replacement pair, the crafting input slot that contained a water bucket will contain an empty bucket after crafting. - - Note: replacements only work correctly when stack_max of the item - to be replaced is 1. It is up to the mod writer to ensure this. */ struct CraftReplacements { // List of replacements std::vector > pairs; - CraftReplacements(): - pairs() - {} - CraftReplacements(std::vector > pairs_): + CraftReplacements() = default; + CraftReplacements(const std::vector > &pairs_): pairs(pairs_) {} std::string dump() const; @@ -136,14 +132,35 @@ struct CraftReplacements class CraftDefinition { public: - CraftDefinition(){} - virtual ~CraftDefinition(){} + /* + Craft recipe priorities, from low to high + + Recipes are searched from latest to first. + If a recipe with higher priority than a previous found one is + encountered, it is selected instead. + */ + enum RecipePriority + { + PRIORITY_NO_RECIPE, + PRIORITY_TOOLREPAIR, + PRIORITY_SHAPELESS_AND_GROUPS, + PRIORITY_SHAPELESS, + PRIORITY_SHAPED_AND_GROUPS, + PRIORITY_SHAPED, + }; + + CraftDefinition() = default; + virtual ~CraftDefinition() = default; // Returns type of crafting definition virtual std::string getName() const=0; // Checks whether the recipe is applicable virtual bool check(const CraftInput &input, IGameDef *gamedef) const=0; + RecipePriority getPriority() const + { + return priority; + } // Returns the output structure, meaning depends on crafting method // The implementation can assume that check(input) returns true virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const=0; @@ -153,13 +170,20 @@ class CraftDefinition virtual void decrementInput(CraftInput &input, std::vector &output_replacements, IGameDef *gamedef) const=0; - virtual CraftHashType getHashType() const = 0; + CraftHashType getHashType() const + { + return hash_type; + } virtual u64 getHash(CraftHashType type) const = 0; // to be called after all mods are loaded, so that we catch all aliases virtual void initHash(IGameDef *gamedef) = 0; virtual std::string dump() const=0; + +protected: + CraftHashType hash_type; + RecipePriority priority; }; /* @@ -171,18 +195,14 @@ class CraftDefinition class CraftDefinitionShaped: public CraftDefinition { public: - CraftDefinitionShaped(): - output(""), width(1), recipe(), hash_inited(false), replacements() - {} + CraftDefinitionShaped() = delete; CraftDefinitionShaped( - const std::string &output_, - unsigned int width_, - const std::vector &recipe_, - const CraftReplacements &replacements_): - output(output_), width(width_), recipe(recipe_), - hash_inited(false), replacements(replacements_) - {} - virtual ~CraftDefinitionShaped(){} + const std::string &output_, + unsigned int width_, + const std::vector &recipe_, + const CraftReplacements &replacements_); + + virtual ~CraftDefinitionShaped() = default; virtual std::string getName() const; virtual bool check(const CraftInput &input, IGameDef *gamedef) const; @@ -191,7 +211,6 @@ class CraftDefinitionShaped: public CraftDefinition virtual void decrementInput(CraftInput &input, std::vector &output_replacements, IGameDef *gamedef) const; - virtual CraftHashType getHashType() const; virtual u64 getHash(CraftHashType type) const; virtual void initHash(IGameDef *gamedef); @@ -200,15 +219,15 @@ class CraftDefinitionShaped: public CraftDefinition private: // Output itemstring - std::string output; + std::string output = ""; // Width of recipe - unsigned int width; + unsigned int width = 1; // Recipe matrix (itemstrings) std::vector recipe; // Recipe matrix (item names) std::vector recipe_names; // bool indicating if initHash has been called already - bool hash_inited; + bool hash_inited = false; // Replacement items for decrementInput() CraftReplacements replacements; }; @@ -221,17 +240,13 @@ class CraftDefinitionShaped: public CraftDefinition class CraftDefinitionShapeless: public CraftDefinition { public: - CraftDefinitionShapeless(): - output(""), recipe(), hash_inited(false), replacements() - {} + CraftDefinitionShapeless() = delete; CraftDefinitionShapeless( - const std::string &output_, - const std::vector &recipe_, - const CraftReplacements &replacements_): - output(output_), recipe(recipe_), - hash_inited(false), replacements(replacements_) - {} - virtual ~CraftDefinitionShapeless(){} + const std::string &output_, + const std::vector &recipe_, + const CraftReplacements &replacements_); + + virtual ~CraftDefinitionShapeless() = default; virtual std::string getName() const; virtual bool check(const CraftInput &input, IGameDef *gamedef) const; @@ -240,7 +255,6 @@ class CraftDefinitionShapeless: public CraftDefinition virtual void decrementInput(CraftInput &input, std::vector &output_replacements, IGameDef *gamedef) const; - virtual CraftHashType getHashType() const; virtual u64 getHash(CraftHashType type) const; virtual void initHash(IGameDef *gamedef); @@ -255,7 +269,7 @@ class CraftDefinitionShapeless: public CraftDefinition // Recipe list (item names) std::vector recipe_names; // bool indicating if initHash has been called already - bool hash_inited; + bool hash_inited = false; // Replacement items for decrementInput() CraftReplacements replacements; }; @@ -269,13 +283,10 @@ class CraftDefinitionShapeless: public CraftDefinition class CraftDefinitionToolRepair: public CraftDefinition { public: - CraftDefinitionToolRepair(): - additional_wear(0) - {} - CraftDefinitionToolRepair(float additional_wear_): - additional_wear(additional_wear_) - {} - virtual ~CraftDefinitionToolRepair(){} + CraftDefinitionToolRepair() = delete; + CraftDefinitionToolRepair(float additional_wear_); + + virtual ~CraftDefinitionToolRepair() = default; virtual std::string getName() const; virtual bool check(const CraftInput &input, IGameDef *gamedef) const; @@ -284,10 +295,12 @@ class CraftDefinitionToolRepair: public CraftDefinition virtual void decrementInput(CraftInput &input, std::vector &output_replacements, IGameDef *gamedef) const; - virtual CraftHashType getHashType() const { return CRAFT_HASH_TYPE_COUNT; } virtual u64 getHash(CraftHashType type) const { return 2; } - virtual void initHash(IGameDef *gamedef) {} + virtual void initHash(IGameDef *gamedef) + { + hash_type = CRAFT_HASH_TYPE_COUNT; + } virtual std::string dump() const; @@ -297,7 +310,7 @@ class CraftDefinitionToolRepair: public CraftDefinition // 1 = new tool is completely broken // 0 = simply add remaining uses of both input tools // -1 = new tool is completely pristine - float additional_wear; + float additional_wear = 0.0f; }; /* @@ -307,18 +320,14 @@ class CraftDefinitionToolRepair: public CraftDefinition class CraftDefinitionCooking: public CraftDefinition { public: - CraftDefinitionCooking(): - output(""), recipe(""), hash_inited(false), cooktime() - {} + CraftDefinitionCooking() = delete; CraftDefinitionCooking( - const std::string &output_, - const std::string &recipe_, - float cooktime_, - const CraftReplacements &replacements_): - output(output_), recipe(recipe_), hash_inited(false), - cooktime(cooktime_), replacements(replacements_) - {} - virtual ~CraftDefinitionCooking(){} + const std::string &output_, + const std::string &recipe_, + float cooktime_, + const CraftReplacements &replacements_); + + virtual ~CraftDefinitionCooking() = default; virtual std::string getName() const; virtual bool check(const CraftInput &input, IGameDef *gamedef) const; @@ -327,7 +336,6 @@ class CraftDefinitionCooking: public CraftDefinition virtual void decrementInput(CraftInput &input, std::vector &output_replacements, IGameDef *gamedef) const; - virtual CraftHashType getHashType() const; virtual u64 getHash(CraftHashType type) const; virtual void initHash(IGameDef *gamedef); @@ -342,7 +350,7 @@ class CraftDefinitionCooking: public CraftDefinition // Recipe item name std::string recipe_name; // bool indicating if initHash has been called already - bool hash_inited; + bool hash_inited = false; // Time in seconds float cooktime; // Replacement items for decrementInput() @@ -356,15 +364,13 @@ class CraftDefinitionCooking: public CraftDefinition class CraftDefinitionFuel: public CraftDefinition { public: - CraftDefinitionFuel(): - recipe(""), hash_inited(false), burntime() - {} - CraftDefinitionFuel(std::string recipe_, - float burntime_, - const CraftReplacements &replacements_): - recipe(recipe_), hash_inited(false), burntime(burntime_), replacements(replacements_) - {} - virtual ~CraftDefinitionFuel(){} + CraftDefinitionFuel() = delete; + CraftDefinitionFuel( + const std::string &recipe_, + float burntime_, + const CraftReplacements &replacements_); + + virtual ~CraftDefinitionFuel() = default; virtual std::string getName() const; virtual bool check(const CraftInput &input, IGameDef *gamedef) const; @@ -373,7 +379,6 @@ class CraftDefinitionFuel: public CraftDefinition virtual void decrementInput(CraftInput &input, std::vector &output_replacements, IGameDef *gamedef) const; - virtual CraftHashType getHashType() const; virtual u64 getHash(CraftHashType type) const; virtual void initHash(IGameDef *gamedef); @@ -386,7 +391,7 @@ class CraftDefinitionFuel: public CraftDefinition // Recipe item name std::string recipe_name; // bool indicating if initHash has been called already - bool hash_inited; + bool hash_inited = false; // Time in seconds float burntime; // Replacement items for decrementInput() @@ -399,13 +404,25 @@ class CraftDefinitionFuel: public CraftDefinition class ICraftDefManager { public: - ICraftDefManager(){} - virtual ~ICraftDefManager(){} - - // The main crafting function + ICraftDefManager() = default; + virtual ~ICraftDefManager() = default; + + /** + * The main crafting function. + * + * @param input The input grid. + * @param output CraftOutput where the result is placed. + * @param output_replacements A vector of ItemStacks where replacements are + * placed if they cannot be placed in the input. Replacements can be placed + * in the input if the stack of the replaced item has a count of 1. + * @param decrementInput If true, consume or replace input items. + * @param gamedef + * @return true if a result was found, otherwise false. + */ virtual bool getCraftResult(CraftInput &input, CraftOutput &output, std::vector &output_replacements, bool decrementInput, IGameDef *gamedef) const=0; + virtual std::vector getCraftRecipes(CraftOutput &output, IGameDef *gamedef, unsigned limit=0) const=0; @@ -416,8 +433,8 @@ class ICraftDefManager class IWritableCraftDefManager : public ICraftDefManager { public: - IWritableCraftDefManager(){} - virtual ~IWritableCraftDefManager(){} + IWritableCraftDefManager() = default; + virtual ~IWritableCraftDefManager() = default; // The main crafting function virtual bool getCraftResult(CraftInput &input, CraftOutput &output, @@ -426,6 +443,9 @@ class IWritableCraftDefManager : public ICraftDefManager virtual std::vector getCraftRecipes(CraftOutput &output, IGameDef *gamedef, unsigned limit=0) const=0; + virtual bool clearCraftsByOutput(const CraftOutput &output, IGameDef *gamedef) = 0; + virtual bool clearCraftsByInput(const CraftInput &input, IGameDef *gamedef) = 0; + // Print crafting recipes for debugging virtual std::string dump() const=0; @@ -441,6 +461,3 @@ class IWritableCraftDefManager : public ICraftDefManager }; IWritableCraftDefManager* createCraftDefManager(); - -#endif -