X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fcraftdef.cpp;h=c05a0cfb75c57984121425015e35c0ffdf7d0563;hb=14c283a623a1c9c0015f04865655df7a225bedf9;hp=fb82bb3961781aeb8c41c0f6fd84620f3db52a76;hpb=395b1b33462c6de9801b77457d347bf62024c2de;p=minetest.git diff --git a/src/craftdef.cpp b/src/craftdef.cpp index fb82bb396..c05a0cfb7 100644 --- a/src/craftdef.cpp +++ b/src/craftdef.cpp @@ -287,6 +287,15 @@ std::string craftDumpMatrix(const std::vector &items, CraftInput */ +bool CraftInput::empty() const +{ + for (const auto &item : items) { + if (!item.empty()) + return false; + } + return true; +} + std::string CraftInput::dump() const { std::ostringstream os(std::ios::binary); @@ -337,9 +346,9 @@ CraftDefinitionShaped::CraftDefinitionShaped( output(output_), width(width_), recipe(recipe_), replacements(replacements_) { if (hasGroupItem(recipe)) - priority = SHAPED_AND_GROUPS; + priority = PRIORITY_SHAPED_AND_GROUPS; else - priority = SHAPED; + priority = PRIORITY_SHAPED; } std::string CraftDefinitionShaped::getName() const @@ -473,9 +482,9 @@ CraftDefinitionShapeless::CraftDefinitionShapeless( output(output_), recipe(recipe_), replacements(replacements_) { if (hasGroupItem(recipe)) - priority = SHAPELESS_AND_GROUPS; + priority = PRIORITY_SHAPELESS_AND_GROUPS; else - priority = SHAPELESS; + priority = PRIORITY_SHAPELESS; } std::string CraftDefinitionShapeless::getName() const @@ -588,7 +597,7 @@ std::string CraftDefinitionShapeless::dump() const CraftDefinitionToolRepair::CraftDefinitionToolRepair(float additional_wear_): additional_wear(additional_wear_) { - priority = TOOLREPAIR; + priority = PRIORITY_TOOLREPAIR; } static ItemStack craftToolRepair( @@ -693,9 +702,9 @@ CraftDefinitionCooking::CraftDefinitionCooking( output(output_), recipe(recipe_), cooktime(cooktime_), replacements(replacements_) { if (isGroupRecipeStr(recipe)) - priority = SHAPELESS_AND_GROUPS; + priority = PRIORITY_SHAPELESS_AND_GROUPS; else - priority = SHAPELESS; + priority = PRIORITY_SHAPELESS; } std::string CraftDefinitionCooking::getName() const @@ -725,7 +734,8 @@ bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) c } // Check the single input item - return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef()); + std::string rec_name = craftGetItemName(recipe, gamedef); + return inputItemMatchesRecipe(input_filtered[0], rec_name, gamedef->idef()); } CraftOutput CraftDefinitionCooking::getOutput(const CraftInput &input, IGameDef *gamedef) const @@ -795,9 +805,9 @@ CraftDefinitionFuel::CraftDefinitionFuel( recipe(recipe_), burntime(burntime_), replacements(replacements_) { if (isGroupRecipeStr(recipe_name)) - priority = SHAPELESS_AND_GROUPS; + priority = PRIORITY_SHAPELESS_AND_GROUPS; else - priority = SHAPELESS; + priority = PRIORITY_SHAPELESS; } std::string CraftDefinitionFuel::getName() const @@ -827,7 +837,8 @@ bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) cons } // Check the single input item - return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef()); + std::string rec_name = craftGetItemName(recipe, gamedef); + return inputItemMatchesRecipe(input_filtered[0], rec_name, gamedef->idef()); } CraftOutput CraftDefinitionFuel::getOutput(const CraftInput &input, IGameDef *gamedef) const @@ -906,18 +917,7 @@ class CCraftDefManager: public IWritableCraftDefManager std::vector &output_replacement, bool decrementInput, IGameDef *gamedef) const { - output.item = ""; - output.time = 0; - - // If all input items are empty, abort. - bool all_empty = true; - for (const auto &item : input.items) { - if (!item.empty()) { - all_empty = false; - break; - } - } - if (all_empty) + if (input.empty()) return false; std::vector input_names; @@ -927,7 +927,7 @@ class CCraftDefManager: public IWritableCraftDefManager // Try hash types with increasing collision rate // while remembering the latest, highest priority recipe. CraftDefinition::RecipePriority priority_best = - CraftDefinition::NO_RECIPE; + CraftDefinition::PRIORITY_NO_RECIPE; CraftDefinition *def_best = nullptr; for (int type = 0; type <= craft_hash_type_max; type++) { u64 hash = getHashForGrid((CraftHashType) type, input_names); @@ -970,7 +970,7 @@ class CCraftDefManager: public IWritableCraftDefManager } } } - if (priority_best == CraftDefinition::NO_RECIPE) + if (priority_best == CraftDefinition::PRIORITY_NO_RECIPE) return false; if (decrementInput) def_best->decrementInput(input, output_replacement, gamedef); @@ -1002,84 +1002,48 @@ class CCraftDefManager: public IWritableCraftDefManager return recipes; } - virtual bool clearCraftRecipesByOutput(const CraftOutput &output, IGameDef *gamedef) + virtual bool clearCraftsByOutput(const CraftOutput &output, IGameDef *gamedef) { - auto vec_iter = m_output_craft_definitions.find(output.item); + auto to_clear = m_output_craft_definitions.find(output.item); - if (vec_iter == m_output_craft_definitions.end()) + if (to_clear == m_output_craft_definitions.end()) return false; - std::vector &vec = vec_iter->second; - for (auto def : vec) { + for (auto def : to_clear->second) { // Recipes are not yet hashed at this point - std::vector &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]; - std::vector 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 (auto &i2 : unhashed_inputs_vec) { - if (def != i2) { - new_vec_by_input.push_back(i2); - } - } - m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input); + std::vector &defs = m_craft_defs[(int)CRAFT_HASH_TYPE_UNHASHED][0]; + defs.erase(std::remove(defs.begin(), defs.end(), def), defs.end()); + delete def; } - m_output_craft_definitions.erase(output.item); + m_output_craft_definitions.erase(to_clear); return true; } - virtual bool clearCraftRecipesByInput(CraftMethod craft_method, unsigned int craft_grid_width, - const std::vector &recipe, IGameDef *gamedef) + virtual bool clearCraftsByInput(const CraftInput &input, IGameDef *gamedef) { - bool all_empty = true; - for (const auto &i : recipe) { - if (!i.empty()) { - all_empty = false; - break; - } - } - if (all_empty) + if (input.empty()) return false; - CraftInput input(craft_method, craft_grid_width, craftGetItems(recipe, gamedef)); // Recipes are not yet hashed at this point - std::vector &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]; - std::vector new_vec_by_input; + std::vector &defs = m_craft_defs[(int)CRAFT_HASH_TYPE_UNHASHED][0]; + std::vector new_defs; bool got_hit = false; - for (std::vector::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. */ + for (auto def : defs) { if (!def->check(input, gamedef)) { - new_vec_by_input.push_back(def); + new_defs.push_back(def); continue; } - CraftOutput output = def->getOutput(input, gamedef); got_hit = true; - auto vec_iter = m_output_craft_definitions.find(output.item); - if (vec_iter == m_output_craft_definitions.end()) + std::string output = def->getOutput(input, gamedef).item; + delete def; + auto it = m_output_craft_definitions.find(craftGetItemName(output, gamedef)); + if (it == m_output_craft_definitions.end()) continue; - std::vector &vec = vec_iter->second; - std::vector 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 (auto &vec_i : vec) { - /* If pointers from map by input and output are not same, - we will add 'CraftDefinition*' to a new vector. */ - if (def != vec_i) { - /* Adding dereferenced iterator value (which are - 'CraftDefinition' reference) to a new vector. */ - new_vec_by_output.push_back(vec_i); - } - } - // Swaps assigned to current key value with new vector for output map. - m_output_craft_definitions[output.item].swap(new_vec_by_output); + std::vector &outdefs = it->second; + outdefs.erase(std::remove(outdefs.begin(), outdefs.end(), def), outdefs.end()); } 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); + defs.swap(new_defs); return got_hit; } @@ -1104,8 +1068,8 @@ class CCraftDefManager: public IWritableCraftDefManager } virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) { - verbosestream << "registerCraft: registering craft definition: " - << def->dump() << std::endl; + TRACESTREAM(<< "registerCraft: registering craft definition: " + << def->dump() << std::endl); m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].push_back(def); CraftInput input;