diff options
Diffstat (limited to 'src/craftdef.cpp')
-rw-r--r-- | src/craftdef.cpp | 368 |
1 files changed, 188 insertions, 180 deletions
diff --git a/src/craftdef.cpp b/src/craftdef.cpp index 210605198..e821a4620 100644 --- a/src/craftdef.cpp +++ b/src/craftdef.cpp @@ -55,24 +55,26 @@ inline u64 getHashForString(const std::string &recipe_str) static u64 getHashForGrid(CraftHashType type, const std::vector<std::string> &grid_names) { switch (type) { - case CRAFT_HASH_TYPE_ITEM_NAMES: { - std::ostringstream os; - bool is_first = true; - for (const std::string &grid_name : grid_names) { - if (!grid_name.empty()) { - os << (is_first ? "" : "\n") << grid_name; - is_first = false; - } + case CRAFT_HASH_TYPE_ITEM_NAMES: { + std::ostringstream os; + bool is_first = true; + for (const std::string &grid_name : grid_names) { + if (!grid_name.empty()) { + os << (is_first ? "" : "\n") << grid_name; + is_first = false; } - return getHashForString(os.str()); - } case CRAFT_HASH_TYPE_COUNT: { - u64 cnt = 0; - for (const std::string &grid_name : grid_names) - if (!grid_name.empty()) - cnt++; - return cnt; - } case CRAFT_HASH_TYPE_UNHASHED: - return 0; + } + return getHashForString(os.str()); + } + case CRAFT_HASH_TYPE_COUNT: { + u64 cnt = 0; + for (const std::string &grid_name : grid_names) + if (!grid_name.empty()) + cnt++; + return cnt; + } + case CRAFT_HASH_TYPE_UNHASHED: + return 0; } // invalid CraftHashType assert(false); @@ -147,8 +149,8 @@ static std::vector<ItemStack> craftGetItems( std::vector<ItemStack> result; result.reserve(items.size()); for (const auto &item : items) { - result.emplace_back(std::string(item), (u16)1, - (u16)0, gamedef->getItemDefManager()); + result.emplace_back(std::string(item), (u16)1, (u16)0, + gamedef->getItemDefManager()); } return result; } @@ -156,8 +158,8 @@ static std::vector<ItemStack> craftGetItems( // Compute bounding rectangle given a matrix of items // Returns false if every item is "" static bool craftGetBounds(const std::vector<std::string> &items, unsigned int width, - unsigned int &min_x, unsigned int &max_x, - unsigned int &min_y, unsigned int &max_y) + unsigned int &min_x, unsigned int &max_x, unsigned int &min_y, + unsigned int &max_y) { bool success = false; unsigned int x = 0; @@ -171,10 +173,14 @@ static bool craftGetBounds(const std::vector<std::string> &items, unsigned int w min_y = max_y = y; success = true; } else { - if (x < min_x) min_x = x; - if (x > max_x) max_x = x; - if (y < min_y) min_y = y; - if (y > max_y) max_y = y; + if (x < min_x) + min_x = x; + if (x > max_x) + max_x = x; + if (y < min_y) + min_y = y; + if (y > max_y) + max_y = y; } } @@ -198,12 +204,12 @@ static void craftDecrementInput(CraftInput &input, IGameDef *gamedef) } // Removes 1 from each item stack with replacement support -// Example: if replacements contains the pair ("bucket:bucket_water", "bucket:bucket_empty"), +// 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) + const CraftReplacements &replacements, IGameDef *gamedef) { if (replacements.pairs.empty()) { craftDecrementInput(input, gamedef); @@ -211,13 +217,14 @@ static void craftDecrementOrReplaceInput(CraftInput &input, } // Make a copy of the replacements pair list - std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs; + std::vector<std::pair<std::string, std::string>> pairs = replacements.pairs; for (auto &item : input.items) { // Find an appropriate replacement bool found_replacement = false; for (auto j = pairs.begin(); j != pairs.end(); ++j) { - if (inputItemMatchesRecipe(item.name, j->first, gamedef->idef())) { + if (inputItemMatchesRecipe( + item.name, j->first, gamedef->idef())) { if (item.count == 1) { item.deSerialize(j->second, gamedef->idef()); found_replacement = true; @@ -231,7 +238,6 @@ static void craftDecrementOrReplaceInput(CraftInput &input, found_replacement = true; output_replacements.push_back(rep); break; - } } // No replacement was found, simply decrement count by one @@ -241,14 +247,13 @@ static void craftDecrementOrReplaceInput(CraftInput &input, } // Dump an itemstring matrix -static std::string craftDumpMatrix(const std::vector<std::string> &items, - unsigned int width) +static std::string craftDumpMatrix( + const std::vector<std::string> &items, unsigned int width) { std::ostringstream os(std::ios::binary); os << "{ "; unsigned int x = 0; - for(std::vector<std::string>::size_type i = 0; - i < items.size(); i++, x++) { + for (std::vector<std::string>::size_type i = 0; i < items.size(); i++, x++) { if (x == width) { os << "; "; x = 0; @@ -262,14 +267,12 @@ static std::string craftDumpMatrix(const std::vector<std::string> &items, } // Dump an item matrix -std::string craftDumpMatrix(const std::vector<ItemStack> &items, - unsigned int width) +std::string craftDumpMatrix(const std::vector<ItemStack> &items, unsigned int width) { std::ostringstream os(std::ios::binary); os << "{ "; unsigned int x = 0; - for (std::vector<ItemStack>::size_type i = 0; - i < items.size(); i++, x++) { + for (std::vector<ItemStack>::size_type i = 0; i < items.size(); i++, x++) { if (x == width) { os << "; "; x = 0; @@ -282,7 +285,6 @@ std::string craftDumpMatrix(const std::vector<ItemStack> &items, return os.str(); } - /* CraftInput */ @@ -299,8 +301,8 @@ bool CraftInput::empty() const std::string CraftInput::dump() const { std::ostringstream os(std::ios::binary); - os << "(method=" << ((int)method) << ", items=" - << craftDumpMatrix(items, width) << ")"; + os << "(method=" << ((int)method) << ", items=" << craftDumpMatrix(items, width) + << ")"; return os.str(); } @@ -322,12 +324,10 @@ std::string CraftOutput::dump() const std::string CraftReplacements::dump() const { std::ostringstream os(std::ios::binary); - os<<"{"; + os << "{"; const char *sep = ""; for (const auto &repl_p : pairs) { - os << sep - << '"' << (repl_p.first) - << "\"=>\"" << (repl_p.second) << '"'; + os << sep << '"' << (repl_p.first) << "\"=>\"" << (repl_p.second) << '"'; sep = ","; } os << "}"; @@ -338,12 +338,11 @@ std::string CraftReplacements::dump() const CraftDefinitionShaped */ -CraftDefinitionShaped::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_) +CraftDefinitionShaped::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_) { if (hasGroupItem(recipe)) priority = PRIORITY_SHAPED_AND_GROUPS; @@ -371,9 +370,9 @@ bool CraftDefinitionShaped::check(const CraftInput &input, IGameDef *gamedef) co // Get input bounds unsigned int inp_min_x = 0, inp_max_x = 0, inp_min_y = 0, inp_max_y = 0; - if (!craftGetBounds(inp_names, inp_width, inp_min_x, inp_max_x, - inp_min_y, inp_max_y)) - return false; // it was empty + if (!craftGetBounds(inp_names, inp_width, inp_min_x, inp_max_x, inp_min_y, + inp_max_y)) + return false; // it was empty std::vector<std::string> rec_names; if (hash_inited) @@ -389,10 +388,10 @@ bool CraftDefinitionShaped::check(const CraftInput &input, IGameDef *gamedef) co rec_names.emplace_back(""); // Get recipe bounds - unsigned int rec_min_x=0, rec_max_x=0, rec_min_y=0, rec_max_y=0; - if (!craftGetBounds(rec_names, rec_width, rec_min_x, rec_max_x, - rec_min_y, rec_max_y)) - return false; // it was empty + unsigned int rec_min_x = 0, rec_max_x = 0, rec_min_y = 0, rec_max_y = 0; + if (!craftGetBounds(rec_names, rec_width, rec_min_x, rec_max_x, rec_min_y, + rec_max_y)) + return false; // it was empty // Different sizes? if (inp_max_x - inp_min_x != rec_max_x - rec_min_x || @@ -403,17 +402,16 @@ bool CraftDefinitionShaped::check(const CraftInput &input, IGameDef *gamedef) co unsigned int w = inp_max_x - inp_min_x + 1; unsigned int h = inp_max_y - inp_min_y + 1; - for (unsigned int y=0; y < h; y++) { + for (unsigned int y = 0; y < h; y++) { unsigned int inp_y = (inp_min_y + y) * inp_width; unsigned int rec_y = (rec_min_y + y) * rec_width; - for (unsigned int x=0; x < w; x++) { + for (unsigned int x = 0; x < w; x++) { unsigned int inp_x = inp_min_x + x; unsigned int rec_x = rec_min_x + x; - if (!inputItemMatchesRecipe( - inp_names[inp_y + inp_x], - rec_names[rec_y + rec_x], gamedef->idef())) { + if (!inputItemMatchesRecipe(inp_names[inp_y + inp_x], + rec_names[rec_y + rec_x], gamedef->idef())) { return false; } } @@ -422,18 +420,20 @@ bool CraftDefinitionShaped::check(const CraftInput &input, IGameDef *gamedef) co return true; } -CraftOutput CraftDefinitionShaped::getOutput(const CraftInput &input, IGameDef *gamedef) const +CraftOutput CraftDefinitionShaped::getOutput( + const CraftInput &input, IGameDef *gamedef) const { return CraftOutput(output, 0); } -CraftInput CraftDefinitionShaped::getInput(const CraftOutput &output, IGameDef *gamedef) const +CraftInput CraftDefinitionShaped::getInput( + const CraftOutput &output, IGameDef *gamedef) const { - return CraftInput(CRAFT_METHOD_NORMAL,width,craftGetItems(recipe,gamedef)); + return CraftInput(CRAFT_METHOD_NORMAL, width, craftGetItems(recipe, gamedef)); } -void CraftDefinitionShaped::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements, - IGameDef *gamedef) const +void CraftDefinitionShaped::decrementInput(CraftInput &input, + std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const { craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef); } @@ -441,8 +441,8 @@ void CraftDefinitionShaped::decrementInput(CraftInput &input, std::vector<ItemSt u64 CraftDefinitionShaped::getHash(CraftHashType type) const { assert(hash_inited); // Pre-condition - assert((type == CRAFT_HASH_TYPE_ITEM_NAMES) - || (type == CRAFT_HASH_TYPE_COUNT)); // Pre-condition + assert((type == CRAFT_HASH_TYPE_ITEM_NAMES) || + (type == CRAFT_HASH_TYPE_COUNT)); // Pre-condition std::vector<std::string> rec_names = recipe_names; std::sort(rec_names.begin(), rec_names.end()); @@ -466,8 +466,8 @@ std::string CraftDefinitionShaped::dump() const { std::ostringstream os(std::ios::binary); os << "(shaped, output=\"" << output - << "\", recipe=" << craftDumpMatrix(recipe, width) - << ", replacements=" << replacements.dump() << ")"; + << "\", recipe=" << craftDumpMatrix(recipe, width) + << ", replacements=" << replacements.dump() << ")"; return os.str(); } @@ -475,11 +475,11 @@ std::string CraftDefinitionShaped::dump() const CraftDefinitionShapeless */ -CraftDefinitionShapeless::CraftDefinitionShapeless( - const std::string &output_, +CraftDefinitionShapeless::CraftDefinitionShapeless(const std::string &output_, const std::vector<std::string> &recipe_, - const CraftReplacements &replacements_): - output(output_), recipe(recipe_), replacements(replacements_) + const CraftReplacements &replacements_) : + output(output_), + recipe(recipe_), replacements(replacements_) { if (hasGroupItem(recipe)) priority = PRIORITY_SHAPELESS_AND_GROUPS; @@ -526,16 +526,16 @@ bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef) do { // If all items match, the recipe matches bool all_match = true; - //dstream<<"Testing recipe (output="<<output<<"):"; - for (size_t i=0; i<recipe.size(); i++) { - //dstream<<" ("<<input_filtered[i]<<" == "<<recipe_copy[i]<<")"; + // dstream<<"Testing recipe (output="<<output<<"):"; + for (size_t i = 0; i < recipe.size(); i++) { + // dstream<<" ("<<input_filtered[i]<<" == "<<recipe_copy[i]<<")"; if (!inputItemMatchesRecipe(input_filtered[i], recipe_copy[i], - gamedef->idef())) { + gamedef->idef())) { all_match = false; break; } } - //dstream<<" -> match="<<all_match<<std::endl; + // dstream<<" -> match="<<all_match<<std::endl; if (all_match) return true; } while (std::next_permutation(recipe_copy.begin(), recipe_copy.end())); @@ -543,18 +543,20 @@ bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef) return false; } -CraftOutput CraftDefinitionShapeless::getOutput(const CraftInput &input, IGameDef *gamedef) const +CraftOutput CraftDefinitionShapeless::getOutput( + const CraftInput &input, IGameDef *gamedef) const { return CraftOutput(output, 0); } -CraftInput CraftDefinitionShapeless::getInput(const CraftOutput &output, IGameDef *gamedef) const +CraftInput CraftDefinitionShapeless::getInput( + const CraftOutput &output, IGameDef *gamedef) const { return CraftInput(CRAFT_METHOD_NORMAL, 0, craftGetItems(recipe, gamedef)); } -void CraftDefinitionShapeless::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements, - IGameDef *gamedef) const +void CraftDefinitionShapeless::decrementInput(CraftInput &input, + std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const { craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef); } @@ -562,8 +564,8 @@ void CraftDefinitionShapeless::decrementInput(CraftInput &input, std::vector<Ite u64 CraftDefinitionShapeless::getHash(CraftHashType type) const { assert(hash_inited); // Pre-condition - assert(type == CRAFT_HASH_TYPE_ITEM_NAMES - || type == CRAFT_HASH_TYPE_COUNT); // Pre-condition + assert(type == CRAFT_HASH_TYPE_ITEM_NAMES || + type == CRAFT_HASH_TYPE_COUNT); // Pre-condition return getHashForGrid(type, recipe_names); } @@ -585,8 +587,8 @@ std::string CraftDefinitionShapeless::dump() const { std::ostringstream os(std::ios::binary); os << "(shapeless, output=\"" << output - << "\", recipe=" << craftDumpMatrix(recipe, recipe.size()) - << ", replacements=" << replacements.dump() << ")"; + << "\", recipe=" << craftDumpMatrix(recipe, recipe.size()) + << ", replacements=" << replacements.dump() << ")"; return os.str(); } @@ -594,28 +596,26 @@ std::string CraftDefinitionShapeless::dump() const CraftDefinitionToolRepair */ -CraftDefinitionToolRepair::CraftDefinitionToolRepair(float additional_wear_): - additional_wear(additional_wear_) +CraftDefinitionToolRepair::CraftDefinitionToolRepair(float additional_wear_) : + additional_wear(additional_wear_) { priority = PRIORITY_TOOLREPAIR; } -static ItemStack craftToolRepair( - const ItemStack &item1, - const ItemStack &item2, - float additional_wear, - IGameDef *gamedef) +static ItemStack craftToolRepair(const ItemStack &item1, const ItemStack &item2, + float additional_wear, IGameDef *gamedef) { IItemDefManager *idef = gamedef->idef(); - if (item1.count != 1 || item2.count != 1 || item1.name != item2.name - || idef->get(item1.name).type != ITEM_TOOL - || itemgroup_get(idef->get(item1.name).groups, "disable_repair") == 1) { + if (item1.count != 1 || item2.count != 1 || item1.name != item2.name || + idef->get(item1.name).type != ITEM_TOOL || + itemgroup_get(idef->get(item1.name).groups, "disable_repair") == + 1) { // Failure return ItemStack(); } - s32 item1_uses = 65536 - (u32) item1.wear; - s32 item2_uses = 65536 - (u32) item2.wear; + s32 item1_uses = 65536 - (u32)item1.wear; + s32 item2_uses = 65536 - (u32)item2.wear; s32 new_uses = item1_uses + item2_uses; s32 new_wear = 65536 - new_uses + floor(additional_wear * 65536 + 0.5); if (new_wear >= 65536) @@ -654,7 +654,8 @@ bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef return !repaired.empty(); } -CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameDef *gamedef) const +CraftOutput CraftDefinitionToolRepair::getOutput( + const CraftInput &input, IGameDef *gamedef) const { ItemStack item1; ItemStack item2; @@ -670,15 +671,16 @@ CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameD return CraftOutput(repaired.getItemString(), 0); } -CraftInput CraftDefinitionToolRepair::getInput(const CraftOutput &output, IGameDef *gamedef) const +CraftInput CraftDefinitionToolRepair::getInput( + const CraftOutput &output, IGameDef *gamedef) const { std::vector<ItemStack> stack; stack.emplace_back(); return CraftInput(CRAFT_METHOD_COOKING, additional_wear, stack); } -void CraftDefinitionToolRepair::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements, - IGameDef *gamedef) const +void CraftDefinitionToolRepair::decrementInput(CraftInput &input, + std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const { craftDecrementInput(input, gamedef); } @@ -694,12 +696,11 @@ std::string CraftDefinitionToolRepair::dump() const CraftDefinitionCooking */ -CraftDefinitionCooking::CraftDefinitionCooking( - const std::string &output_, - const std::string &recipe_, - float cooktime_, - const CraftReplacements &replacements_): - output(output_), recipe(recipe_), cooktime(cooktime_), replacements(replacements_) +CraftDefinitionCooking::CraftDefinitionCooking(const std::string &output_, + const std::string &recipe_, float cooktime_, + const CraftReplacements &replacements_) : + output(output_), + recipe(recipe_), cooktime(cooktime_), replacements(replacements_) { if (isGroupRecipeStr(recipe)) priority = PRIORITY_SHAPELESS_AND_GROUPS; @@ -737,20 +738,22 @@ bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) c return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef()); } -CraftOutput CraftDefinitionCooking::getOutput(const CraftInput &input, IGameDef *gamedef) const +CraftOutput CraftDefinitionCooking::getOutput( + const CraftInput &input, IGameDef *gamedef) const { return CraftOutput(output, cooktime); } -CraftInput CraftDefinitionCooking::getInput(const CraftOutput &output, IGameDef *gamedef) const +CraftInput CraftDefinitionCooking::getInput( + const CraftOutput &output, IGameDef *gamedef) const { std::vector<std::string> rec; rec.push_back(recipe); - return CraftInput(CRAFT_METHOD_COOKING,cooktime,craftGetItems(rec,gamedef)); + return CraftInput(CRAFT_METHOD_COOKING, cooktime, craftGetItems(rec, gamedef)); } -void CraftDefinitionCooking::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements, - IGameDef *gamedef) const +void CraftDefinitionCooking::decrementInput(CraftInput &input, + std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const { craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef); } @@ -786,10 +789,9 @@ void CraftDefinitionCooking::initHash(IGameDef *gamedef) std::string CraftDefinitionCooking::dump() const { std::ostringstream os(std::ios::binary); - os << "(cooking, output=\"" << output - << "\", recipe=\"" << recipe - << "\", cooktime=" << cooktime << ")" - << ", replacements=" << replacements.dump() << ")"; + os << "(cooking, output=\"" << output << "\", recipe=\"" << recipe + << "\", cooktime=" << cooktime << ")" + << ", replacements=" << replacements.dump() << ")"; return os.str(); } @@ -797,11 +799,10 @@ std::string CraftDefinitionCooking::dump() const CraftDefinitionFuel */ -CraftDefinitionFuel::CraftDefinitionFuel( - const std::string &recipe_, - float burntime_, - const CraftReplacements &replacements_): - recipe(recipe_), burntime(burntime_), replacements(replacements_) +CraftDefinitionFuel::CraftDefinitionFuel(const std::string &recipe_, float burntime_, + const CraftReplacements &replacements_) : + recipe(recipe_), + burntime(burntime_), replacements(replacements_) { if (isGroupRecipeStr(recipe_name)) priority = PRIORITY_SHAPELESS_AND_GROUPS; @@ -839,20 +840,23 @@ bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) cons return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef()); } -CraftOutput CraftDefinitionFuel::getOutput(const CraftInput &input, IGameDef *gamedef) const +CraftOutput CraftDefinitionFuel::getOutput( + const CraftInput &input, IGameDef *gamedef) const { return CraftOutput("", burntime); } -CraftInput CraftDefinitionFuel::getInput(const CraftOutput &output, IGameDef *gamedef) const +CraftInput CraftDefinitionFuel::getInput( + const CraftOutput &output, IGameDef *gamedef) const { std::vector<std::string> rec; rec.push_back(recipe); - return CraftInput(CRAFT_METHOD_COOKING,(int)burntime,craftGetItems(rec,gamedef)); + return CraftInput( + CRAFT_METHOD_COOKING, (int)burntime, craftGetItems(rec, gamedef)); } -void CraftDefinitionFuel::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements, - IGameDef *gamedef) const +void CraftDefinitionFuel::decrementInput(CraftInput &input, + std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const { craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef); } @@ -888,9 +892,8 @@ void CraftDefinitionFuel::initHash(IGameDef *gamedef) std::string CraftDefinitionFuel::dump() const { std::ostringstream os(std::ios::binary); - os << "(fuel, recipe=\"" << recipe - << "\", burntime=" << burntime << ")" - << ", replacements=" << replacements.dump() << ")"; + os << "(fuel, recipe=\"" << recipe << "\", burntime=" << burntime << ")" + << ", replacements=" << replacements.dump() << ")"; return os.str(); } @@ -898,18 +901,12 @@ std::string CraftDefinitionFuel::dump() const Craft definition manager */ -class CCraftDefManager: public IWritableCraftDefManager +class CCraftDefManager : public IWritableCraftDefManager { public: - CCraftDefManager() - { - m_craft_defs.resize(craft_hash_type_max + 1); - } + CCraftDefManager() { m_craft_defs.resize(craft_hash_type_max + 1); } - virtual ~CCraftDefManager() - { - clear(); - } + virtual ~CCraftDefManager() { clear(); } virtual bool getCraftResult(CraftInput &input, CraftOutput &output, std::vector<ItemStack> &output_replacement, bool decrementInput, @@ -925,40 +922,48 @@ public: // Try hash types with increasing collision rate // while remembering the latest, highest priority recipe. CraftDefinition::RecipePriority priority_best = - CraftDefinition::PRIORITY_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); + u64 hash = getHashForGrid((CraftHashType)type, input_names); - /*errorstream << "Checking type " << type << " with hash " << hash << std::endl;*/ + /*errorstream << "Checking type " << type << " with hash " << hash + * << std::endl;*/ - // We'd like to do "const [...] hash_collisions = m_craft_defs[type][hash];" - // but that doesn't compile for some reason. This does. + // We'd like to do "const [...] hash_collisions = + // m_craft_defs[type][hash];" but that doesn't compile for some + // reason. This does. auto col_iter = (m_craft_defs[type]).find(hash); if (col_iter == (m_craft_defs[type]).end()) continue; - const std::vector<CraftDefinition*> &hash_collisions = col_iter->second; + 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*>::size_type - i = hash_collisions.size(); i > 0; i--) { + 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;*/ - CraftDefinition::RecipePriority priority = def->getPriority(); - if (priority > priority_best - && def->check(input, gamedef)) { + CraftDefinition::RecipePriority priority = + def->getPriority(); + if (priority > priority_best && + def->check(input, gamedef)) { // Check if the crafted node/item exists CraftOutput out = def->getOutput(input, gamedef); ItemStack is; is.deSerialize(out.item, gamedef->idef()); if (!is.isKnown(gamedef->idef())) { - infostream << "trying to craft non-existent " - << out.item << ", ignoring recipe" << std::endl; + infostream << "trying to craft " + "non-existent " + << out.item + << ", ignoring recipe" + << std::endl; continue; } @@ -975,22 +980,22 @@ public: return true; } - virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output, - IGameDef *gamedef, unsigned limit=0) const + virtual std::vector<CraftDefinition *> getCraftRecipes( + CraftOutput &output, IGameDef *gamedef, unsigned limit = 0) const { - std::vector<CraftDefinition*> recipes; + std::vector<CraftDefinition *> recipes; auto vec_iter = m_output_craft_definitions.find(output.item); if (vec_iter == m_output_craft_definitions.end()) return recipes; - const std::vector<CraftDefinition*> &vec = vec_iter->second; + const std::vector<CraftDefinition *> &vec = vec_iter->second; recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size()); - for (std::vector<CraftDefinition*>::size_type i = vec.size(); - i > 0; i--) { + for (std::vector<CraftDefinition *>::size_type i = vec.size(); i > 0; + i--) { CraftDefinition *def = vec[i - 1]; if (limit && recipes.size() >= limit) break; @@ -1009,8 +1014,10 @@ public: for (auto def : to_clear->second) { // Recipes are not yet hashed at this point - std::vector<CraftDefinition *> &defs = m_craft_defs[(int)CRAFT_HASH_TYPE_UNHASHED][0]; - defs.erase(std::remove(defs.begin(), defs.end(), def), defs.end()); + std::vector<CraftDefinition *> &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(to_clear); @@ -1023,7 +1030,8 @@ public: return false; // Recipes are not yet hashed at this point - std::vector<CraftDefinition *> &defs = m_craft_defs[(int)CRAFT_HASH_TYPE_UNHASHED][0]; + std::vector<CraftDefinition *> &defs = + m_craft_defs[(int)CRAFT_HASH_TYPE_UNHASHED][0]; std::vector<CraftDefinition *> new_defs; bool got_hit = false; for (auto def : defs) { @@ -1034,11 +1042,13 @@ public: got_hit = true; std::string output = def->getOutput(input, gamedef).item; delete def; - auto it = m_output_craft_definitions.find(craftGetItemName(output, gamedef)); + auto it = m_output_craft_definitions.find( + craftGetItemName(output, gamedef)); if (it == m_output_craft_definitions.end()) continue; std::vector<CraftDefinition *> &outdefs = it->second; - outdefs.erase(std::remove(outdefs.begin(), outdefs.end(), def), outdefs.end()); + outdefs.erase(std::remove(outdefs.begin(), outdefs.end(), def), + outdefs.end()); } if (got_hit) defs.swap(new_defs); @@ -1053,12 +1063,10 @@ public: for (int type = 0; type <= craft_hash_type_max; ++type) { for (auto it = m_craft_defs[type].begin(); it != m_craft_defs[type].end(); ++it) { - for (std::vector<CraftDefinition*>::size_type i = 0; + 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"; + os << "type " << type << " hash " << it->first + << " def " << it->second[i]->dump() << "\n"; } } } @@ -1067,8 +1075,8 @@ public: virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) { TRACESTREAM(<< "registerCraft: registering craft definition: " - << def->dump() << std::endl); - m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].push_back(def); + << def->dump() << std::endl); + m_craft_defs[(int)CRAFT_HASH_TYPE_UNHASHED][0].push_back(def); CraftInput input; std::string output_name = craftGetItemName( @@ -1092,7 +1100,7 @@ public: { // Move the CraftDefs from the unhashed layer into layers higher up. std::vector<CraftDefinition *> &unhashed = - m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]; + m_craft_defs[(int)CRAFT_HASH_TYPE_UNHASHED][0]; for (auto def : unhashed) { // Initialize and get the definition's hash def->initHash(gamedef); @@ -1104,14 +1112,14 @@ public: } unhashed.clear(); } + private: - std::vector<std::unordered_map<u64, std::vector<CraftDefinition*> > > - m_craft_defs; - std::unordered_map<std::string, std::vector<CraftDefinition*> > - m_output_craft_definitions; + std::vector<std::unordered_map<u64, std::vector<CraftDefinition *>>> m_craft_defs; + std::unordered_map<std::string, std::vector<CraftDefinition *>> + m_output_craft_definitions; }; -IWritableCraftDefManager* createCraftDefManager() +IWritableCraftDefManager *createCraftDefManager() { return new CCraftDefManager(); } |