]> git.lizzy.rs Git - minetest.git/blobdiff - src/craftdef.cpp
Shaders: Remove special handling for liquids. (#4670)
[minetest.git] / src / craftdef.cpp
index 409481e64ad402019810ec557601f889a7636e10..45d3018a7b6bef1692bb1d5aad7b6334c9b64c08 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)
@@ -90,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;
        }
@@ -214,7 +214,7 @@ static void craftDecrementOrReplaceInput(CraftInput &input,
                for (std::vector<std::pair<std::string, std::string> >::iterator
                                j = pairs.begin();
                                j != pairs.end(); ++j) {
-                       if (item.name == craftGetItemName(j->first, gamedef)) {
+                       if (inputItemMatchesRecipe(item.name, j->first, gamedef->idef())) {
                                if (item.count == 1) {
                                        item.deSerialize(j->second, gamedef->idef());
                                        found_replacement = true;
@@ -960,14 +960,104 @@ class CCraftDefManager: public IWritableCraftDefManager
 
                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++) {
+                                       it != (m_craft_defs[type]).end(); ++it) {
                                for (std::vector<CraftDefinition*>::size_type i = 0;
                                                i < it->second.size(); i++) {
                                        os << "type " << type
@@ -992,10 +1082,10 @@ class CCraftDefManager: public IWritableCraftDefManager
        }
        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) {