X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fcraftdef.h;h=7c14e702a1fb2ff2176fc814f4803a6d499b03b5;hb=74125a74d34e9b1a003107d4ef6b95b8483d2464;hp=f62ad3313b7a50504a3c38864aedc76f0cbfecd5;hpb=27e3a353d84c974dfea113df0eaa0aa7e93b7c6b;p=dragonfireclient.git diff --git a/src/craftdef.h b/src/craftdef.h index f62ad3313..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,29 +132,58 @@ 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; // the inverse of the above virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const=0; // Decreases count of every input item - virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const=0; + 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; }; /* @@ -170,26 +195,22 @@ 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; virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const; virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const; - virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const; + 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); @@ -198,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; }; @@ -219,25 +240,21 @@ 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; virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const; virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const; - virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const; + 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); @@ -252,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; }; @@ -266,24 +283,24 @@ 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; virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const; virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const; - virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const; + 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; @@ -293,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; }; /* @@ -303,26 +320,22 @@ 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; virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const; virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const; - virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const; + 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); @@ -337,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() @@ -351,23 +364,21 @@ 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; virtual CraftOutput getOutput(const CraftInput &input, IGameDef *gamedef) const; virtual CraftInput getInput(const CraftOutput &output, IGameDef *gamedef) const; - virtual void decrementInput(CraftInput &input, IGameDef *gamedef) const; + 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); @@ -380,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() @@ -393,12 +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; @@ -409,15 +433,19 @@ 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, + std::vector &output_replacements, bool decrementInput, IGameDef *gamedef) const=0; 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; @@ -433,6 +461,3 @@ class IWritableCraftDefManager : public ICraftDefManager }; IWritableCraftDefManager* createCraftDefManager(); - -#endif -