X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fcraftdef.h;h=7c14e702a1fb2ff2176fc814f4803a6d499b03b5;hb=0e88176db8ce9f3fb631feb56bdd29d4ada054f5;hp=fddff6cef8c5e240e8a1e1c7eba5e26bb21e3c44;hpb=76be103a91d6987527af19e87d93007be8ba8a67;p=dragonfireclient.git diff --git a/src/craftdef.h b/src/craftdef.h index fddff6cef..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 @@ -74,13 +73,16 @@ struct CraftInput unsigned int width = 0; std::vector items; - CraftInput() {} + 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; }; @@ -94,7 +96,7 @@ struct CraftOutput // Used for cooking (cook time) and fuel (burn time), seconds float time = 0.0f; - CraftOutput() {} + CraftOutput() = default; CraftOutput(const std::string &item_, float time_): item(item_), time(time_) @@ -111,18 +113,13 @@ 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() = default; CraftReplacements(const std::vector > &pairs_): pairs(pairs_) {} @@ -135,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; @@ -152,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; }; /* @@ -170,17 +195,14 @@ class CraftDefinition class CraftDefinitionShaped: public CraftDefinition { public: - CraftDefinitionShaped() {} - + CraftDefinitionShaped() = delete; CraftDefinitionShaped( - const std::string &output_, - unsigned int width_, - const std::vector &recipe_, - const CraftReplacements &replacements_): - output(output_), width(width_), recipe(recipe_), - 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; @@ -189,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); @@ -219,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; @@ -238,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); @@ -253,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; }; @@ -267,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; @@ -282,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; @@ -295,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; }; /* @@ -305,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; @@ -325,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); @@ -340,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() @@ -354,18 +364,13 @@ class CraftDefinitionCooking: public CraftDefinition class CraftDefinitionFuel: public CraftDefinition { public: - CraftDefinitionFuel(): - recipe(""), hash_inited(false), burntime() - {} - CraftDefinitionFuel(const 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; @@ -374,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); @@ -387,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() @@ -400,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; @@ -417,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, @@ -427,9 +443,8 @@ class IWritableCraftDefManager : public ICraftDefManager virtual std::vector getCraftRecipes(CraftOutput &output, IGameDef *gamedef, unsigned limit=0) const=0; - virtual bool clearCraftRecipesByOutput(const CraftOutput &output, IGameDef *gamedef) = 0; - virtual bool clearCraftRecipesByInput(CraftMethod craft_method, - unsigned int craft_grid_width, const std::vector &recipe, IGameDef *gamedef) = 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; @@ -446,6 +461,3 @@ class IWritableCraftDefManager : public ICraftDefManager }; IWritableCraftDefManager* createCraftDefManager(); - -#endif -