Add cancel button to password change menu. (#5720)
[oweals/minetest.git] / src / craftdef.cpp
index d3ac8a4588fbdb7e8be699f02d1fe3e847b1fc1a..286d1eadaf8dc699ec4c492f23b09b284d1ea5f2 100644 (file)
@@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/serialize.h"
 #include "util/string.h"
 #include "util/numeric.h"
-#include "strfnd.h"
+#include "util/strfnd.h"
 #include "exceptions.h"
 
 inline bool isGroupRecipeStr(const std::string &rec_name)
@@ -67,6 +67,7 @@ static u64 getHashForGrid(CraftHashType type, const std::vector<std::string> &gr
        }
        // invalid CraftHashType
        assert(false);
+       return 0;
 }
 
 // Check if input matches recipe
@@ -89,7 +90,7 @@ static bool inputItemMatchesRecipe(const std::string &inp_name,
                                all_groups_match = false;
                                break;
                        }
-               } while (!f.atend());
+               } while (!f.at_end());
                if (all_groups_match)
                        return true;
        }
@@ -111,10 +112,9 @@ static std::vector<std::string> craftGetItemNames(
                const std::vector<std::string> &itemstrings, IGameDef *gamedef)
 {
        std::vector<std::string> result;
-       for (std::vector<std::string>::const_iterator
-                       it = itemstrings.begin();
-                       it != itemstrings.end(); it++) {
-               result.push_back(craftGetItemName(*it, gamedef));
+       for (std::vector<std::string>::size_type i = 0;
+                       i < itemstrings.size(); i++) {
+               result.push_back(craftGetItemName(itemstrings[i], gamedef));
        }
        return result;
 }
@@ -124,10 +124,9 @@ static std::vector<std::string> craftGetItemNames(
                const std::vector<ItemStack> &items, IGameDef *gamedef)
 {
        std::vector<std::string> result;
-       for (std::vector<ItemStack>::const_iterator
-                       it = items.begin();
-                       it != items.end(); it++) {
-               result.push_back(it->name);
+       for (std::vector<ItemStack>::size_type i = 0;
+                       i < items.size(); i++) {
+               result.push_back(items[i].name);
        }
        return result;
 }
@@ -137,11 +136,10 @@ static std::vector<ItemStack> craftGetItems(
                const std::vector<std::string> &items, IGameDef *gamedef)
 {
        std::vector<ItemStack> result;
-       for (std::vector<std::string>::const_iterator
-                       it = items.begin();
-                       it != items.end(); it++) {
-               result.push_back(ItemStack(std::string(*it), (u16)1,
-                       (u16)0, "", gamedef->getItemDefManager()));
+       for (std::vector<std::string>::size_type i = 0;
+                       i < items.size(); i++) {
+               result.push_back(ItemStack(std::string(items[i]), (u16)1,
+                       (u16)0, gamedef->getItemDefManager()));
        }
        return result;
 }
@@ -155,11 +153,10 @@ static bool craftGetBounds(const std::vector<std::string> &items, unsigned int w
        bool success = false;
        unsigned int x = 0;
        unsigned int y = 0;
-       for (std::vector<std::string>::const_iterator
-                       it = items.begin();
-                       it != items.end(); it++) {
+       for (std::vector<std::string>::size_type i = 0;
+                       i < items.size(); i++) {
                // Is this an actual item?
-               if (*it != "") {
+               if (items[i] != "") {
                        if (!success) {
                                // This is the first nonempty item
                                min_x = max_x = x;
@@ -186,11 +183,10 @@ static bool craftGetBounds(const std::vector<std::string> &items, unsigned int w
 // Removes 1 from each item stack
 static void craftDecrementInput(CraftInput &input, IGameDef *gamedef)
 {
-       for (std::vector<ItemStack>::iterator
-                       it = input.items.begin();
-                       it != input.items.end(); it++) {
-               if (it->count != 0)
-                       it->remove(1);
+       for (std::vector<ItemStack>::size_type i = 0;
+                       i < input.items.size(); i++) {
+               if (input.items[i].count != 0)
+                       input.items[i].remove(1);
        }
 }
 
@@ -198,6 +194,7 @@ static void craftDecrementInput(CraftInput &input, IGameDef *gamedef)
 // Example: if replacements contains the pair ("bucket:bucket_water", "bucket:bucket_empty"),
 //   a water bucket will not be removed but replaced by an empty bucket.
 static void craftDecrementOrReplaceInput(CraftInput &input,
+               std::vector<ItemStack> &output_replacements,
                const CraftReplacements &replacements,
                IGameDef *gamedef)
 {
@@ -209,29 +206,33 @@ static void craftDecrementOrReplaceInput(CraftInput &input,
        // Make a copy of the replacements pair list
        std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs;
 
-       for (std::vector<ItemStack>::iterator
-                       it = input.items.begin();
-                       it != input.items.end(); it++) {
-               if (it->count == 1) {
-                       // Find an appropriate replacement
-                       bool found_replacement = false;
-                       for (std::vector<std::pair<std::string, std::string> >::iterator
-                                       j = pairs.begin();
-                                       j != pairs.end(); j++) {
-                               if (it->name == craftGetItemName(j->first, gamedef)) {
-                                       it->deSerialize(j->second, gamedef->idef());
+       for (std::vector<ItemStack>::size_type i = 0;
+                       i < input.items.size(); i++) {
+               ItemStack &item = input.items[i];
+               // Find an appropriate replacement
+               bool found_replacement = false;
+               for (std::vector<std::pair<std::string, std::string> >::iterator
+                               j = pairs.begin();
+                               j != pairs.end(); ++j) {
+                       if (inputItemMatchesRecipe(item.name, j->first, gamedef->idef())) {
+                               if (item.count == 1) {
+                                       item.deSerialize(j->second, gamedef->idef());
                                        found_replacement = true;
                                        pairs.erase(j);
                                        break;
+                               } else {
+                                       ItemStack rep;
+                                       rep.deSerialize(j->second, gamedef->idef());
+                                       item.remove(1);
+                                       found_replacement = true;
+                                       output_replacements.push_back(rep);
+                                       break;
                                }
                        }
-                       // No replacement was found, simply decrement count to zero
-                       if (!found_replacement)
-                               it->remove(1);
-               } else if (it->count >= 2) {
-                       // Ignore replacements for items with count >= 2
-                       it->remove(1);
                }
+               // No replacement was found, simply decrement count by one
+               if (!found_replacement && item.count > 0)
+                       item.remove(1);
        }
 }
 
@@ -240,18 +241,17 @@ static std::string craftDumpMatrix(const std::vector<std::string> &items,
                unsigned int width)
 {
        std::ostringstream os(std::ios::binary);
-       os<<"{ ";
+       os << "{ ";
        unsigned int x = 0;
-       for(std::vector<std::string>::const_iterator
-                       it = items.begin();
-                       it != items.end(); it++, x++) {
+       for(std::vector<std::string>::size_type i = 0;
+                       i < items.size(); i++, x++) {
                if (x == width) {
-                       os<<"; ";
+                       os << "; ";
                        x = 0;
                } else if (x != 0) {
-                       os<<",";
+                       os << ",";
                }
-               os << '"' << (*it) << '"';
+               os << '"' << items[i] << '"';
        }
        os << " }";
        return os.str();
@@ -264,16 +264,15 @@ std::string craftDumpMatrix(const std::vector<ItemStack> &items,
        std::ostringstream os(std::ios::binary);
        os << "{ ";
        unsigned int x = 0;
-       for (std::vector<ItemStack>::const_iterator
-                       it = items.begin();
-                       it != items.end(); it++, x++) {
+       for (std::vector<ItemStack>::size_type i = 0;
+                       i < items.size(); i++, x++) {
                if (x == width) {
                        os << "; ";
                        x = 0;
                } else if (x != 0) {
-                       os<<",";
+                       os << ",";
                }
-               os << '"' << (it->getItemString()) << '"';
+               os << '"' << (items[i].getItemString()) << '"';
        }
        os << " }";
        return os.str();
@@ -312,73 +311,18 @@ std::string CraftReplacements::dump() const
        std::ostringstream os(std::ios::binary);
        os<<"{";
        const char *sep = "";
-       for (std::vector<std::pair<std::string, std::string> >::const_iterator
-                       it = pairs.begin();
-                       it != pairs.end(); it++) {
-               os << sep << '"' << (it->first) << "\"=>\"" << (it->second) << '"';
+       for (std::vector<std::pair<std::string, std::string> >::size_type i = 0;
+                       i < pairs.size(); i++) {
+               const std::pair<std::string, std::string> &repl_p = pairs[i];
+               os << sep
+                       << '"' << (repl_p.first)
+                       << "\"=>\"" << (repl_p.second) << '"';
                sep = ",";
        }
        os << "}";
        return os.str();
 }
 
-void CraftReplacements::serialize(std::ostream &os) const
-{
-       writeU16(os, pairs.size());
-       for (u32 i=0; i<pairs.size(); i++) {
-               os << serializeString(pairs[i].first);
-               os << serializeString(pairs[i].second);
-       }
-}
-
-void CraftReplacements::deSerialize(std::istream &is)
-{
-       pairs.clear();
-       u32 count = readU16(is);
-       for (u32 i=0; i<count; i++) {
-               std::string first = deSerializeString(is);
-               std::string second = deSerializeString(is);
-               pairs.push_back(std::make_pair(first, second));
-       }
-}
-
-/*
-       CraftDefinition
-*/
-
-void CraftDefinition::serialize(std::ostream &os) const
-{
-       writeU8(os, 1); // version
-       os << serializeString(getName());
-       serializeBody(os);
-}
-
-CraftDefinition* CraftDefinition::deSerialize(std::istream &is)
-{
-       int version = readU8(is);
-       if (version != 1) throw SerializationError(
-                       "unsupported CraftDefinition version");
-       std::string name = deSerializeString(is);
-       CraftDefinition *def = NULL;
-       if (name == "shaped") {
-               def = new CraftDefinitionShaped;
-       } else if (name == "shapeless") {
-               def = new CraftDefinitionShapeless;
-       } else if (name == "toolrepair") {
-               def = new CraftDefinitionToolRepair;
-       } else if (name == "cooking") {
-               def = new CraftDefinitionCooking;
-       } else if (name == "fuel") {
-               def = new CraftDefinitionFuel;
-       } else {
-               infostream << "Unknown CraftDefinition name=\""
-                       << name << '"' << std::endl;
-               throw SerializationError("Unknown CraftDefinition name");
-       }
-       def->deSerializeBody(is, version);
-       return def;
-}
-
 /*
        CraftDefinitionShaped
 */
@@ -464,9 +408,10 @@ CraftInput CraftDefinitionShaped::getInput(const CraftOutput &output, IGameDef *
        return CraftInput(CRAFT_METHOD_NORMAL,width,craftGetItems(recipe,gamedef));
 }
 
-void CraftDefinitionShaped::decrementInput(CraftInput &input, IGameDef *gamedef) const
+void CraftDefinitionShaped::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
+        IGameDef *gamedef) const
 {
-       craftDecrementOrReplaceInput(input, replacements, gamedef);
+       craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
 }
 
 CraftHashType CraftDefinitionShaped::getHashType() const
@@ -513,29 +458,6 @@ std::string CraftDefinitionShaped::dump() const
        return os.str();
 }
 
-void CraftDefinitionShaped::serializeBody(std::ostream &os) const
-{
-       os << serializeString(output);
-       writeU16(os, width);
-       writeU16(os, recipe.size());
-       for (u32 i=0; i<recipe.size(); i++)
-               os << serializeString(recipe[i]);
-       replacements.serialize(os);
-}
-
-void CraftDefinitionShaped::deSerializeBody(std::istream &is, int version)
-{
-       if (version != 1) throw SerializationError(
-                       "unsupported CraftDefinitionShaped version");
-       output = deSerializeString(is);
-       width = readU16(is);
-       recipe.clear();
-       u32 count = readU16(is);
-       for (u32 i=0; i<count; i++)
-               recipe.push_back(deSerializeString(is));
-       replacements.deSerialize(is);
-}
-
 /*
        CraftDefinitionShapeless
 */
@@ -552,11 +474,11 @@ bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef)
 
        // Filter empty items out of input
        std::vector<std::string> input_filtered;
-       for (std::vector<ItemStack>::const_iterator
-                       it = input.items.begin();
-                       it != input.items.end(); it++) {
-               if (it->name != "")
-                       input_filtered.push_back(it->name);
+       for (std::vector<ItemStack>::size_type i = 0;
+                       i < input.items.size(); i++) {
+               const ItemStack &item = input.items[i];
+               if (item.name != "")
+                       input_filtered.push_back(item.name);
        }
 
        // If there is a wrong number of items in input, no match
@@ -608,9 +530,10 @@ CraftInput CraftDefinitionShapeless::getInput(const CraftOutput &output, IGameDe
        return CraftInput(CRAFT_METHOD_NORMAL, 0, craftGetItems(recipe, gamedef));
 }
 
-void CraftDefinitionShapeless::decrementInput(CraftInput &input, IGameDef *gamedef) const
+void CraftDefinitionShapeless::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
+       IGameDef *gamedef) const
 {
-       craftDecrementOrReplaceInput(input, replacements, gamedef);
+       craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
 }
 
 CraftHashType CraftDefinitionShapeless::getHashType() const
@@ -655,27 +578,6 @@ std::string CraftDefinitionShapeless::dump() const
        return os.str();
 }
 
-void CraftDefinitionShapeless::serializeBody(std::ostream &os) const
-{
-       os << serializeString(output);
-       writeU16(os, recipe.size());
-       for (u32 i=0; i<recipe.size(); i++)
-               os << serializeString(recipe[i]);
-       replacements.serialize(os);
-}
-
-void CraftDefinitionShapeless::deSerializeBody(std::istream &is, int version)
-{
-       if (version != 1) throw SerializationError(
-                       "unsupported CraftDefinitionShapeless version");
-       output = deSerializeString(is);
-       recipe.clear();
-       u32 count = readU16(is);
-       for (u32 i=0; i<count; i++)
-               recipe.push_back(deSerializeString(is));
-       replacements.deSerialize(is);
-}
-
 /*
        CraftDefinitionToolRepair
 */
@@ -720,14 +622,14 @@ bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef
 
        ItemStack item1;
        ItemStack item2;
-       for (std::vector<ItemStack>::const_iterator
-                       it = input.items.begin();
-                       it != input.items.end(); it++) {
-               if (!it->empty()) {
+       for (std::vector<ItemStack>::size_type i = 0;
+                       i < input.items.size(); i++) {
+               const ItemStack &item = input.items[i];
+               if (!item.empty()) {
                        if (item1.empty())
-                               item1 = *it;
+                               item1 = item;
                        else if (item2.empty())
-                               item2 = *it;
+                               item2 = item;
                        else
                                return false;
                }
@@ -740,14 +642,14 @@ CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameD
 {
        ItemStack item1;
        ItemStack item2;
-       for (std::vector<ItemStack>::const_iterator
-                       it = input.items.begin();
-                       it != input.items.end(); it++) {
-               if (!it->empty()) {
+       for (std::vector<ItemStack>::size_type i = 0;
+                       i < input.items.size(); i++) {
+               const ItemStack &item = input.items[i];
+               if (!item.empty()) {
                        if (item1.empty())
-                               item1 = *it;
+                               item1 = item;
                        else if (item2.empty())
-                               item2 = *it;
+                               item2 = item;
                }
        }
        ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
@@ -761,7 +663,8 @@ CraftInput CraftDefinitionToolRepair::getInput(const CraftOutput &output, IGameD
        return CraftInput(CRAFT_METHOD_COOKING, additional_wear, stack);
 }
 
-void CraftDefinitionToolRepair::decrementInput(CraftInput &input, IGameDef *gamedef) const
+void CraftDefinitionToolRepair::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
+       IGameDef *gamedef) const
 {
        craftDecrementInput(input, gamedef);
 }
@@ -773,18 +676,6 @@ std::string CraftDefinitionToolRepair::dump() const
        return os.str();
 }
 
-void CraftDefinitionToolRepair::serializeBody(std::ostream &os) const
-{
-       writeF1000(os, additional_wear);
-}
-
-void CraftDefinitionToolRepair::deSerializeBody(std::istream &is, int version)
-{
-       if (version != 1) throw SerializationError(
-                       "unsupported CraftDefinitionToolRepair version");
-       additional_wear = readF1000(is);
-}
-
 /*
        CraftDefinitionCooking
 */
@@ -801,11 +692,11 @@ bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) c
 
        // Filter empty items out of input
        std::vector<std::string> input_filtered;
-       for (std::vector<ItemStack>::const_iterator
-                       it = input.items.begin();
-                       it != input.items.end(); it++) {
-               if (it->name != "")
-                       input_filtered.push_back(it->name);
+       for (std::vector<ItemStack>::size_type i = 0;
+                       i < input.items.size(); i++) {
+               const std::string &name = input.items[i].name;
+               if (name != "")
+                       input_filtered.push_back(name);
        }
 
        // If there is a wrong number of items in input, no match
@@ -832,9 +723,10 @@ CraftInput CraftDefinitionCooking::getInput(const CraftOutput &output, IGameDef
        return CraftInput(CRAFT_METHOD_COOKING,cooktime,craftGetItems(rec,gamedef));
 }
 
-void CraftDefinitionCooking::decrementInput(CraftInput &input, IGameDef *gamedef) const
+void CraftDefinitionCooking::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
+       IGameDef *gamedef) const
 {
-       craftDecrementOrReplaceInput(input, replacements, gamedef);
+       craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
 }
 
 CraftHashType CraftDefinitionCooking::getHashType() const
@@ -876,24 +768,6 @@ std::string CraftDefinitionCooking::dump() const
        return os.str();
 }
 
-void CraftDefinitionCooking::serializeBody(std::ostream &os) const
-{
-       os << serializeString(output);
-       os << serializeString(recipe);
-       writeF1000(os, cooktime);
-       replacements.serialize(os);
-}
-
-void CraftDefinitionCooking::deSerializeBody(std::istream &is, int version)
-{
-       if (version != 1) throw SerializationError(
-                       "unsupported CraftDefinitionCooking version");
-       output = deSerializeString(is);
-       recipe = deSerializeString(is);
-       cooktime = readF1000(is);
-       replacements.deSerialize(is);
-}
-
 /*
        CraftDefinitionFuel
 */
@@ -910,11 +784,11 @@ bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) cons
 
        // Filter empty items out of input
        std::vector<std::string> input_filtered;
-       for (std::vector<ItemStack>::const_iterator
-                       it = input.items.begin();
-                       it != input.items.end(); it++) {
-               if (it->name != "")
-                       input_filtered.push_back(it->name);
+       for (std::vector<ItemStack>::size_type i = 0;
+                       i < input.items.size(); i++) {
+               const std::string &name = input.items[i].name;
+               if (name != "")
+                       input_filtered.push_back(name);
        }
 
        // If there is a wrong number of items in input, no match
@@ -941,9 +815,10 @@ CraftInput CraftDefinitionFuel::getInput(const CraftOutput &output, IGameDef *ga
        return CraftInput(CRAFT_METHOD_COOKING,(int)burntime,craftGetItems(rec,gamedef));
 }
 
-void CraftDefinitionFuel::decrementInput(CraftInput &input, IGameDef *gamedef) const
+void CraftDefinitionFuel::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
+       IGameDef *gamedef) const
 {
-       craftDecrementOrReplaceInput(input, replacements, gamedef);
+       craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
 }
 
 CraftHashType CraftDefinitionFuel::getHashType() const
@@ -983,22 +858,6 @@ std::string CraftDefinitionFuel::dump() const
        return os.str();
 }
 
-void CraftDefinitionFuel::serializeBody(std::ostream &os) const
-{
-       os << serializeString(recipe);
-       writeF1000(os, burntime);
-       replacements.serialize(os);
-}
-
-void CraftDefinitionFuel::deSerializeBody(std::istream &is, int version)
-{
-       if (version != 1) throw SerializationError(
-                       "unsupported CraftDefinitionFuel version");
-       recipe = deSerializeString(is);
-       burntime = readF1000(is);
-       replacements.deSerialize(is);
-}
-
 /*
        Craft definition manager
 */
@@ -1017,17 +876,17 @@ public:
        }
 
        virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
-                       bool decrementInput, IGameDef *gamedef) const
+                       std::vector<ItemStack> &output_replacement, bool decrementInput,
+                       IGameDef *gamedef) const
        {
                output.item = "";
                output.time = 0;
 
                // If all input items are empty, abort.
                bool all_empty = true;
-               for (std::vector<ItemStack>::const_iterator
-                               it = input.items.begin();
-                               it != input.items.end(); it++) {
-                       if (!it->empty()) {
+               for (std::vector<ItemStack>::size_type i = 0;
+                       i < input.items.size(); i++) {
+                       if (!input.items[i].empty()) {
                                all_empty = false;
                                break;
                        }
@@ -1056,10 +915,9 @@ public:
                        const std::vector<CraftDefinition*> &hash_collisions = col_iter->second;
                        // Walk crafting definitions from back to front, so that later
                        // definitions can override earlier ones.
-                       for (std::vector<CraftDefinition*>::const_reverse_iterator
-                                       it = hash_collisions.rbegin();
-                                       it != hash_collisions.rend(); it++) {
-                               CraftDefinition *def = *it;
+                       for (std::vector<CraftDefinition*>::size_type
+                                       i = hash_collisions.size(); i > 0; i--) {
+                               CraftDefinition *def = hash_collisions[i - 1];
 
                                /*errorstream << "Checking " << input.dump() << std::endl
                                        << " against " << def->dump() << std::endl;*/
@@ -1068,7 +926,7 @@ public:
                                        // Get output, then decrement input (if requested)
                                        output = def->getOutput(input, gamedef);
                                        if (decrementInput)
-                                               def->decrementInput(input, gamedef);
+                                               def->decrementInput(input, output_replacement, gamedef);
                                        /*errorstream << "Check RETURNS TRUE" << std::endl;*/
                                        return true;
                                }
@@ -1092,26 +950,120 @@ public:
 
                recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size());
 
-               for (std::vector<CraftDefinition*>::const_reverse_iterator
-                               it = vec.rbegin(); it != vec.rend(); ++it) {
+               for (std::vector<CraftDefinition*>::size_type i = vec.size();
+                               i > 0; i--) {
+                       CraftDefinition *def = vec[i - 1];
                        if (limit && recipes.size() >= limit)
                                break;
-                       recipes.push_back(*it);
+                       recipes.push_back(def);
                }
 
                return recipes;
        }
+
+       virtual bool clearCraftRecipesByOutput(const CraftOutput &output, IGameDef *gamedef)
+       {
+               std::map<std::string, std::vector<CraftDefinition*> >::iterator vec_iter =
+                       m_output_craft_definitions.find(output.item);
+
+               if (vec_iter == m_output_craft_definitions.end())
+                       return false;
+
+               std::vector<CraftDefinition*> &vec = vec_iter->second;
+               for (std::vector<CraftDefinition*>::iterator i = vec.begin();
+                               i != vec.end(); ++i) {
+                       CraftDefinition *def = *i;
+                       // Recipes are not yet hashed at this point
+                       std::vector<CraftDefinition*> &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
+                       std::vector<CraftDefinition*> new_vec_by_input;
+                       /* We will preallocate necessary memory addresses, so we don't need to reallocate them later.
+                               This would save us some performance. */
+                       new_vec_by_input.reserve(unhashed_inputs_vec.size());
+                       for (std::vector<CraftDefinition*>::iterator i2 = unhashed_inputs_vec.begin();
+                                       i2 != unhashed_inputs_vec.end(); ++i2) {
+                               if (def != *i2) {
+                                       new_vec_by_input.push_back(*i2);
+                               }
+                       }
+                       m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input);
+               }
+               m_output_craft_definitions.erase(output.item);
+               return true;
+       }
+
+       virtual bool clearCraftRecipesByInput(CraftMethod craft_method, unsigned int craft_grid_width,
+               const std::vector<std::string> &recipe, IGameDef *gamedef)
+       {
+               bool all_empty = true;
+               for (std::vector<std::string>::size_type i = 0;
+                               i < recipe.size(); i++) {
+                       if (!recipe[i].empty()) {
+                               all_empty = false;
+                               break;
+                       }
+               }
+               if (all_empty)
+                       return false;
+
+               CraftInput input(craft_method, craft_grid_width, craftGetItems(recipe, gamedef));
+               // Recipes are not yet hashed at this point
+               std::vector<CraftDefinition*> &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
+               std::vector<CraftDefinition*> new_vec_by_input;
+               bool got_hit = false;
+               for (std::vector<CraftDefinition*>::size_type
+                               i = unhashed_inputs_vec.size(); i > 0; i--) {
+                       CraftDefinition *def = unhashed_inputs_vec[i - 1];
+                       /* If the input doesn't match the recipe definition, this recipe definition later
+                               will be added back in source map. */
+                       if (!def->check(input, gamedef)) {
+                               new_vec_by_input.push_back(def);
+                               continue;
+                       }
+                       CraftOutput output = def->getOutput(input, gamedef);
+                       got_hit = true;
+                       std::map<std::string, std::vector<CraftDefinition*> >::iterator
+                               vec_iter = m_output_craft_definitions.find(output.item);
+                       if (vec_iter == m_output_craft_definitions.end())
+                               continue;
+                       std::vector<CraftDefinition*> &vec = vec_iter->second;
+                       std::vector<CraftDefinition*> new_vec_by_output;
+                       /* We will preallocate necessary memory addresses, so we don't need
+                               to reallocate them later. This would save us some performance. */
+                       new_vec_by_output.reserve(vec.size());
+                       for (std::vector<CraftDefinition*>::iterator i = vec.begin();
+                                       i != vec.end(); ++i) {
+                               /* If pointers from map by input and output are not same,
+                                       we will add 'CraftDefinition*' to a new vector. */
+                               if (def != *i) {
+                                       /* Adding dereferenced iterator value (which are
+                                               'CraftDefinition' reference) to a new vector. */
+                                       new_vec_by_output.push_back(*i);
+                               }
+                       }
+                       // Swaps assigned to current key value with new vector for output map.
+                       m_output_craft_definitions[output.item].swap(new_vec_by_output);
+               }
+               if (got_hit)
+                       // Swaps value with new vector for input map.
+                       m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input);
+
+               return got_hit;
+       }
+
        virtual std::string dump() const
        {
                std::ostringstream os(std::ios::binary);
                os << "Crafting definitions:\n";
-               for (int type = 0; type <= craft_hash_type_max; type++) {
+               for (int type = 0; type <= craft_hash_type_max; ++type) {
                        for (std::map<u64, std::vector<CraftDefinition*> >::const_iterator
                                        it = (m_craft_defs[type]).begin();
-                                       it != (m_craft_defs[type]).end(); it++) {
-                               for (std::vector<CraftDefinition*>::const_iterator
-                                               iit = it->second.begin(); iit != it->second.end(); iit++) {
-                                       os << "type " << type << " hash " << it->first << (*iit)->dump() << "\n";
+                                       it != (m_craft_defs[type]).end(); ++it) {
+                               for (std::vector<CraftDefinition*>::size_type i = 0;
+                                               i < it->second.size(); i++) {
+                                       os << "type " << type
+                                               << " hash " << it->first
+                                               << " def " << it->second[i]->dump()
+                                               << "\n";
                                }
                        }
                }
@@ -1130,12 +1082,13 @@ public:
        }
        virtual void clear()
        {
-               for (int type = 0; type <= craft_hash_type_max; type++) {
+               for (int type = 0; type <= craft_hash_type_max; ++type) {
                        for (std::map<u64, std::vector<CraftDefinition*> >::iterator
                                        it = m_craft_defs[type].begin();
-                                       it != m_craft_defs[type].end(); it++) {
+                                       it != m_craft_defs[type].end(); ++it) {
                                for (std::vector<CraftDefinition*>::iterator
-                                               iit = it->second.begin(); iit != it->second.end(); iit++) {
+                                               iit = it->second.begin();
+                                               iit != it->second.end(); ++iit) {
                                        delete *iit;
                                }
                                it->second.clear();
@@ -1147,10 +1100,11 @@ public:
        virtual void initHashes(IGameDef *gamedef)
        {
                // Move the CraftDefs from the unhashed layer into layers higher up.
-               for (std::vector<CraftDefinition*>::iterator
-                       it = (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).begin();
-                       it != (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).end(); it++) {
-                       CraftDefinition *def = *it;
+               std::vector<CraftDefinition *> &unhashed =
+                       m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
+               for (std::vector<CraftDefinition*>::size_type i = 0;
+                       i < unhashed.size(); i++) {
+                       CraftDefinition *def = unhashed[i];
 
                        // Initialize and get the definition's hash
                        def->initHash(gamedef);
@@ -1160,7 +1114,7 @@ public:
                        // Enter the definition
                        m_craft_defs[type][hash].push_back(def);
                }
-               m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].clear();
+               unhashed.clear();
        }
 private:
        //TODO: change both maps to unordered_map when c++11 can be used
@@ -1172,4 +1126,3 @@ IWritableCraftDefManager* createCraftDefManager()
 {
        return new CCraftDefManager();
 }
-