Restore visual_scale support for nodeboxes (and allfaces) (#9906)
[oweals/minetest.git] / src / craftdef.h
index eb3cd7e391486e8a9530d2043ed1c5bf270ba5ce..7c14e702a1fb2ff2176fc814f4803a6d499b03b5 100644 (file)
@@ -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 <string>
 #include <iostream>
@@ -43,22 +42,47 @@ enum CraftMethod
        CRAFT_METHOD_FUEL,
 };
 
+/*
+       The type a hash can be. The earlier a type is mentioned in this enum,
+       the earlier it is tried at crafting, and the less likely is a collision.
+       Changing order causes changes in behaviour, so know what you do.
+ */
+enum CraftHashType
+{
+       // Hashes the normalized names of the recipe's elements.
+       // Only recipes without group usage can be found here,
+       // because groups can't be guessed efficiently.
+       CRAFT_HASH_TYPE_ITEM_NAMES,
+
+       // Counts the non-empty slots.
+       CRAFT_HASH_TYPE_COUNT,
+
+       // This layer both spares an extra variable, and helps to retain (albeit rarely used) functionality. Maps to 0.
+       // Before hashes are "initialized", all hashes reside here, after initialisation, none are.
+       CRAFT_HASH_TYPE_UNHASHED
+
+};
+const int craft_hash_type_max = (int) CRAFT_HASH_TYPE_UNHASHED;
+
 /*
        Input: The contents of the crafting slots, arranged in matrix form
 */
 struct CraftInput
 {
-       CraftMethod method;
-       unsigned int width;
+       CraftMethod method = CRAFT_METHOD_NORMAL;
+       unsigned int width = 0;
        std::vector<ItemStack> items;
 
-       CraftInput():
-               method(CRAFT_METHOD_NORMAL), width(0), items()
-       {}
+       CraftInput() = default;
+
        CraftInput(CraftMethod method_, unsigned int width_,
                        const std::vector<ItemStack> &items_):
                method(method_), width(width_), items(items_)
        {}
+
+       // Returns true if all items are empty.
+       bool empty() const;
+
        std::string dump() const;
 };
 
@@ -68,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;
@@ -90,24 +113,17 @@ 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<std::pair<std::string, std::string> > pairs;
 
-       CraftReplacements():
-               pairs()
-       {}
-       CraftReplacements(std::vector<std::pair<std::string, std::string> > pairs_):
+       CraftReplacements() = default;
+       CraftReplacements(const std::vector<std::pair<std::string, std::string> > &pairs_):
                pairs(pairs_)
        {}
        std::string dump() const;
-       void serialize(std::ostream &os) const;
-       void deSerialize(std::istream &is);
 };
 
 /*
@@ -116,30 +132,58 @@ struct CraftReplacements
 class CraftDefinition
 {
 public:
-       CraftDefinition(){}
-       virtual ~CraftDefinition(){}
-
-       void serialize(std::ostream &os) const;
-       static CraftDefinition* deSerialize(std::istream &is);
+       /*
+               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<ItemStack> &output_replacements, IGameDef *gamedef) 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:
-       virtual void serializeBody(std::ostream &os) const=0;
-       virtual void deSerializeBody(std::istream &is, int version)=0;
+       CraftHashType hash_type;
+       RecipePriority priority;
 };
 
 /*
@@ -151,37 +195,39 @@ protected:
 class CraftDefinitionShaped: public CraftDefinition
 {
 public:
-       CraftDefinitionShaped():
-               output(""), width(1), recipe(), replacements()
-       {}
+       CraftDefinitionShaped() = delete;
        CraftDefinitionShaped(
-                       const std::string &output_,
-                       unsigned int width_,
-                       const std::vector<std::string> &recipe_,
-                       const CraftReplacements &replacements_):
-               output(output_), width(width_), recipe(recipe_), replacements(replacements_)
-       {}
-       virtual ~CraftDefinitionShaped(){}
+               const std::string &output_,
+               unsigned int width_,
+               const std::vector<std::string> &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<ItemStack> &output_replacements, IGameDef *gamedef) const;
 
-       virtual std::string dump() const;
+       virtual u64 getHash(CraftHashType type) const;
 
-protected:
-       virtual void serializeBody(std::ostream &os) const;
-       virtual void deSerializeBody(std::istream &is, int version);
+       virtual void initHash(IGameDef *gamedef);
+
+       virtual std::string dump() const;
 
 private:
        // Output itemstring
-       std::string output;
+       std::string output = "";
        // Width of recipe
-       unsigned int width;
+       unsigned int width = 1;
        // Recipe matrix (itemstrings)
        std::vector<std::string> recipe;
+       // Recipe matrix (item names)
+       std::vector<std::string> recipe_names;
+       // bool indicating if initHash has been called already
+       bool hash_inited = false;
        // Replacement items for decrementInput()
        CraftReplacements replacements;
 };
@@ -194,34 +240,36 @@ private:
 class CraftDefinitionShapeless: public CraftDefinition
 {
 public:
-       CraftDefinitionShapeless():
-               output(""), recipe(), replacements()
-       {}
+       CraftDefinitionShapeless() = delete;
        CraftDefinitionShapeless(
-                       const std::string &output_,
-                       const std::vector<std::string> &recipe_,
-                       const CraftReplacements &replacements_):
-               output(output_), recipe(recipe_), replacements(replacements_)
-       {}
-       virtual ~CraftDefinitionShapeless(){}
+               const std::string &output_,
+               const std::vector<std::string> &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<ItemStack> &output_replacements, IGameDef *gamedef) const;
 
-       virtual std::string dump() const;
+       virtual u64 getHash(CraftHashType type) const;
 
-protected:
-       virtual void serializeBody(std::ostream &os) const;
-       virtual void deSerializeBody(std::istream &is, int version);
+       virtual void initHash(IGameDef *gamedef);
+
+       virtual std::string dump() const;
 
 private:
        // Output itemstring
        std::string output;
        // Recipe list (itemstrings)
        std::vector<std::string> recipe;
+       // Recipe list (item names)
+       std::vector<std::string> recipe_names;
+       // bool indicating if initHash has been called already
+       bool hash_inited = false;
        // Replacement items for decrementInput()
        CraftReplacements replacements;
 };
@@ -235,25 +283,26 @@ private:
 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<ItemStack> &output_replacements, IGameDef *gamedef) const;
 
-       virtual std::string dump() const;
+       virtual u64 getHash(CraftHashType type) const { return 2; }
 
-protected:
-       virtual void serializeBody(std::ostream &os) const;
-       virtual void deSerializeBody(std::istream &is, int version);
+       virtual void initHash(IGameDef *gamedef)
+       {
+               hash_type = CRAFT_HASH_TYPE_COUNT;
+       }
+
+       virtual std::string dump() const;
 
 private:
        // This is a constant that is added to the wear of the result.
@@ -261,7 +310,7 @@ private:
        // 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;
 };
 
 /*
@@ -271,35 +320,37 @@ private:
 class CraftDefinitionCooking: public CraftDefinition
 {
 public:
-       CraftDefinitionCooking():
-               output(""), recipe(""), cooktime()
-       {}
+       CraftDefinitionCooking() = delete;
        CraftDefinitionCooking(
-                       const std::string &output_,
-                       const std::string &recipe_,
-                       float cooktime_,
-                       const CraftReplacements &replacements_):
-               output(output_), recipe(recipe_), 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<ItemStack> &output_replacements, IGameDef *gamedef) const;
 
-       virtual std::string dump() const;
+       virtual u64 getHash(CraftHashType type) const;
 
-protected:
-       virtual void serializeBody(std::ostream &os) const;
-       virtual void deSerializeBody(std::istream &is, int version);
+       virtual void initHash(IGameDef *gamedef);
+
+       virtual std::string dump() const;
 
 private:
        // Output itemstring
        std::string output;
        // Recipe itemstring
        std::string recipe;
+       // Recipe item name
+       std::string recipe_name;
+       // bool indicating if initHash has been called already
+       bool hash_inited = false;
        // Time in seconds
        float cooktime;
        // Replacement items for decrementInput()
@@ -313,31 +364,34 @@ private:
 class CraftDefinitionFuel: public CraftDefinition
 {
 public:
-       CraftDefinitionFuel():
-               recipe(""), burntime()
-       {}
-       CraftDefinitionFuel(std::string recipe_,
-                       float burntime_,
-                       const CraftReplacements &replacements_):
-               recipe(recipe_), 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<ItemStack> &output_replacements, IGameDef *gamedef) const;
 
-       virtual std::string dump() const;
+       virtual u64 getHash(CraftHashType type) const;
 
-protected:
-       virtual void serializeBody(std::ostream &os) const;
-       virtual void deSerializeBody(std::istream &is, int version);
+       virtual void initHash(IGameDef *gamedef);
+
+       virtual std::string dump() const;
 
 private:
        // Recipe itemstring
        std::string recipe;
+       // Recipe item name
+       std::string recipe_name;
+       // bool indicating if initHash has been called already
+       bool hash_inited = false;
        // Time in seconds
        float burntime;
        // Replacement items for decrementInput()
@@ -350,47 +404,60 @@ private:
 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<ItemStack> &output_replacements,
                        bool decrementInput, IGameDef *gamedef) const=0;
-       virtual bool getCraftRecipe(CraftInput &input, CraftOutput &output,
-                       IGameDef *gamedef) const=0;
-       
+
+       virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
+                       IGameDef *gamedef, unsigned limit=0) const=0;
+
        // Print crafting recipes for debugging
        virtual std::string dump() const=0;
-
-       virtual void serialize(std::ostream &os) const=0;
 };
 
 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<ItemStack> &output_replacements,
                        bool decrementInput, IGameDef *gamedef) const=0;
-       virtual bool getCraftRecipe(CraftInput &input, CraftOutput &output,
-                       IGameDef *gamedef) const=0;
+       virtual std::vector<CraftDefinition*> 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;
 
        // Add a crafting definition.
        // After calling this, the pointer belongs to the manager.
-       virtual void registerCraft(CraftDefinition *def)=0;
+       virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) = 0;
+
        // Delete all crafting definitions
        virtual void clear()=0;
 
-       virtual void serialize(std::ostream &os) const=0;
-       virtual void deSerialize(std::istream &is)=0;
+       // To be called after all mods are loaded, so that we catch all aliases
+       virtual void initHashes(IGameDef *gamedef) = 0;
 };
 
 IWritableCraftDefManager* createCraftDefManager();
-
-#endif
-