3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "irrlichttypes.h"
28 #include "inventory.h"
29 #include "util/serialize.h"
30 #include "util/string.h"
31 #include "util/numeric.h"
33 #include "exceptions.h"
35 inline bool isGroupRecipeStr(const std::string &rec_name)
37 return str_starts_with(rec_name, std::string("group:"));
40 inline u64 getHashForString(const std::string &recipe_str)
42 /*errorstream << "Hashing craft string \"" << recipe_str << '"';*/
43 return murmur_hash_64_ua(recipe_str.data(), recipe_str.length(), 0xdeadbeef);
46 static u64 getHashForGrid(CraftHashType type, const std::vector<std::string> &grid_names)
49 case CRAFT_HASH_TYPE_ITEM_NAMES: {
50 std::ostringstream os;
52 for (size_t i = 0; i < grid_names.size(); i++) {
53 if (grid_names[i] != "") {
54 os << (is_first ? "" : "\n") << grid_names[i];
58 return getHashForString(os.str());
59 } case CRAFT_HASH_TYPE_COUNT: {
61 for (size_t i = 0; i < grid_names.size(); i++)
62 if (grid_names[i] != "")
65 } case CRAFT_HASH_TYPE_UNHASHED:
68 // invalid CraftHashType
73 // Check if input matches recipe
74 // Takes recipe groups into account
75 static bool inputItemMatchesRecipe(const std::string &inp_name,
76 const std::string &rec_name, IItemDefManager *idef)
79 if (inp_name == rec_name)
83 if (isGroupRecipeStr(rec_name) && idef->isKnown(inp_name)) {
84 const struct ItemDefinition &def = idef->get(inp_name);
85 Strfnd f(rec_name.substr(6));
86 bool all_groups_match = true;
88 std::string check_group = f.next(",");
89 if (itemgroup_get(def.groups, check_group) == 0) {
90 all_groups_match = false;
102 // Deserialize an itemstring then return the name of the item
103 static std::string craftGetItemName(const std::string &itemstring, IGameDef *gamedef)
106 item.deSerialize(itemstring, gamedef->idef());
110 // (mapcar craftGetItemName itemstrings)
111 static std::vector<std::string> craftGetItemNames(
112 const std::vector<std::string> &itemstrings, IGameDef *gamedef)
114 std::vector<std::string> result;
115 for (std::vector<std::string>::const_iterator
116 it = itemstrings.begin();
117 it != itemstrings.end(); it++) {
118 result.push_back(craftGetItemName(*it, gamedef));
123 // Get name of each item, and return them as a new list.
124 static std::vector<std::string> craftGetItemNames(
125 const std::vector<ItemStack> &items, IGameDef *gamedef)
127 std::vector<std::string> result;
128 for (std::vector<ItemStack>::const_iterator
130 it != items.end(); it++) {
131 result.push_back(it->name);
136 // convert a list of item names, to ItemStacks.
137 static std::vector<ItemStack> craftGetItems(
138 const std::vector<std::string> &items, IGameDef *gamedef)
140 std::vector<ItemStack> result;
141 for (std::vector<std::string>::const_iterator
143 it != items.end(); it++) {
144 result.push_back(ItemStack(std::string(*it), (u16)1,
145 (u16)0, "", gamedef->getItemDefManager()));
150 // Compute bounding rectangle given a matrix of items
151 // Returns false if every item is ""
152 static bool craftGetBounds(const std::vector<std::string> &items, unsigned int width,
153 unsigned int &min_x, unsigned int &max_x,
154 unsigned int &min_y, unsigned int &max_y)
156 bool success = false;
159 for (std::vector<std::string>::const_iterator
161 it != items.end(); it++) {
162 // Is this an actual item?
165 // This is the first nonempty item
170 if (x < min_x) min_x = x;
171 if (x > max_x) max_x = x;
172 if (y < min_y) min_y = y;
173 if (y > max_y) max_y = y;
187 // Removes 1 from each item stack
188 static void craftDecrementInput(CraftInput &input, IGameDef *gamedef)
190 for (std::vector<ItemStack>::iterator
191 it = input.items.begin();
192 it != input.items.end(); it++) {
198 // Removes 1 from each item stack with replacement support
199 // Example: if replacements contains the pair ("bucket:bucket_water", "bucket:bucket_empty"),
200 // a water bucket will not be removed but replaced by an empty bucket.
201 static void craftDecrementOrReplaceInput(CraftInput &input,
202 std::vector<ItemStack> &output_replacements,
203 const CraftReplacements &replacements,
206 if (replacements.pairs.empty()) {
207 craftDecrementInput(input, gamedef);
211 // Make a copy of the replacements pair list
212 std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs;
214 for (std::vector<ItemStack>::iterator
215 it = input.items.begin();
216 it != input.items.end(); it++) {
217 // Find an appropriate replacement
218 bool found_replacement = false;
219 for (std::vector<std::pair<std::string, std::string> >::iterator
221 j != pairs.end(); j++) {
222 if (it->name == craftGetItemName(j->first, gamedef)) {
223 if (it->count == 1) {
224 it->deSerialize(j->second, gamedef->idef());
225 found_replacement = true;
230 rep.deSerialize(j->second, gamedef->idef());
232 found_replacement = true;
233 output_replacements.push_back(rep);
238 // No replacement was found, simply decrement count by one
239 if (!found_replacement && it->count > 0)
244 // Dump an itemstring matrix
245 static std::string craftDumpMatrix(const std::vector<std::string> &items,
248 std::ostringstream os(std::ios::binary);
251 for(std::vector<std::string>::const_iterator
253 it != items.end(); it++, x++) {
260 os << '"' << (*it) << '"';
266 // Dump an item matrix
267 std::string craftDumpMatrix(const std::vector<ItemStack> &items,
270 std::ostringstream os(std::ios::binary);
273 for (std::vector<ItemStack>::const_iterator
275 it != items.end(); it++, x++) {
282 os << '"' << (it->getItemString()) << '"';
293 std::string CraftInput::dump() const
295 std::ostringstream os(std::ios::binary);
296 os << "(method=" << ((int)method) << ", items="
297 << craftDumpMatrix(items, width) << ")";
305 std::string CraftOutput::dump() const
307 std::ostringstream os(std::ios::binary);
308 os << "(item=\"" << item << "\", time=" << time << ")";
316 std::string CraftReplacements::dump() const
318 std::ostringstream os(std::ios::binary);
320 const char *sep = "";
321 for (std::vector<std::pair<std::string, std::string> >::const_iterator
323 it != pairs.end(); it++) {
324 os << sep << '"' << (it->first) << "\"=>\"" << (it->second) << '"';
332 CraftDefinitionShaped
335 std::string CraftDefinitionShaped::getName() const
340 bool CraftDefinitionShaped::check(const CraftInput &input, IGameDef *gamedef) const
342 if (input.method != CRAFT_METHOD_NORMAL)
345 // Get input item matrix
346 std::vector<std::string> inp_names = craftGetItemNames(input.items, gamedef);
347 unsigned int inp_width = input.width;
350 while (inp_names.size() % inp_width != 0)
351 inp_names.push_back("");
354 unsigned int inp_min_x = 0, inp_max_x = 0, inp_min_y = 0, inp_max_y = 0;
355 if (!craftGetBounds(inp_names, inp_width, inp_min_x, inp_max_x,
356 inp_min_y, inp_max_y))
357 return false; // it was empty
359 std::vector<std::string> rec_names;
361 rec_names = recipe_names;
363 rec_names = craftGetItemNames(recipe, gamedef);
365 // Get recipe item matrix
366 unsigned int rec_width = width;
369 while (rec_names.size() % rec_width != 0)
370 rec_names.push_back("");
373 unsigned int rec_min_x=0, rec_max_x=0, rec_min_y=0, rec_max_y=0;
374 if (!craftGetBounds(rec_names, rec_width, rec_min_x, rec_max_x,
375 rec_min_y, rec_max_y))
376 return false; // it was empty
379 if (inp_max_x - inp_min_x != rec_max_x - rec_min_x ||
380 inp_max_y - inp_min_y != rec_max_y - rec_min_y)
383 // Verify that all item names in the bounding box are equal
384 unsigned int w = inp_max_x - inp_min_x + 1;
385 unsigned int h = inp_max_y - inp_min_y + 1;
387 for (unsigned int y=0; y < h; y++) {
388 unsigned int inp_y = (inp_min_y + y) * inp_width;
389 unsigned int rec_y = (rec_min_y + y) * rec_width;
391 for (unsigned int x=0; x < w; x++) {
392 unsigned int inp_x = inp_min_x + x;
393 unsigned int rec_x = rec_min_x + x;
395 if (!inputItemMatchesRecipe(
396 inp_names[inp_y + inp_x],
397 rec_names[rec_y + rec_x], gamedef->idef())) {
406 CraftOutput CraftDefinitionShaped::getOutput(const CraftInput &input, IGameDef *gamedef) const
408 return CraftOutput(output, 0);
411 CraftInput CraftDefinitionShaped::getInput(const CraftOutput &output, IGameDef *gamedef) const
413 return CraftInput(CRAFT_METHOD_NORMAL,width,craftGetItems(recipe,gamedef));
416 void CraftDefinitionShaped::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
417 IGameDef *gamedef) const
419 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
422 CraftHashType CraftDefinitionShaped::getHashType() const
424 assert(hash_inited); // Pre-condition
425 bool has_group = false;
426 for (size_t i = 0; i < recipe_names.size(); i++) {
427 if (isGroupRecipeStr(recipe_names[i])) {
433 return CRAFT_HASH_TYPE_COUNT;
435 return CRAFT_HASH_TYPE_ITEM_NAMES;
438 u64 CraftDefinitionShaped::getHash(CraftHashType type) const
440 assert(hash_inited); // Pre-condition
441 assert((type == CRAFT_HASH_TYPE_ITEM_NAMES)
442 || (type == CRAFT_HASH_TYPE_COUNT)); // Pre-condition
444 std::vector<std::string> rec_names = recipe_names;
445 std::sort(rec_names.begin(), rec_names.end());
446 return getHashForGrid(type, rec_names);
449 void CraftDefinitionShaped::initHash(IGameDef *gamedef)
454 recipe_names = craftGetItemNames(recipe, gamedef);
457 std::string CraftDefinitionShaped::dump() const
459 std::ostringstream os(std::ios::binary);
460 os << "(shaped, output=\"" << output
461 << "\", recipe=" << craftDumpMatrix(recipe, width)
462 << ", replacements=" << replacements.dump() << ")";
467 CraftDefinitionShapeless
470 std::string CraftDefinitionShapeless::getName() const
475 bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef) const
477 if (input.method != CRAFT_METHOD_NORMAL)
480 // Filter empty items out of input
481 std::vector<std::string> input_filtered;
482 for (std::vector<ItemStack>::const_iterator
483 it = input.items.begin();
484 it != input.items.end(); it++) {
486 input_filtered.push_back(it->name);
489 // If there is a wrong number of items in input, no match
490 if (input_filtered.size() != recipe.size()) {
491 /*dstream<<"Number of input items ("<<input_filtered.size()
492 <<") does not match recipe size ("<<recipe.size()<<") "
493 <<"of recipe with output="<<output<<std::endl;*/
497 std::vector<std::string> recipe_copy;
499 recipe_copy = recipe_names;
501 recipe_copy = craftGetItemNames(recipe, gamedef);
502 std::sort(recipe_copy.begin(), recipe_copy.end());
505 // Try with all permutations of the recipe,
506 // start from the lexicographically first permutation (=sorted),
507 // recipe_names is pre-sorted
509 // If all items match, the recipe matches
510 bool all_match = true;
511 //dstream<<"Testing recipe (output="<<output<<"):";
512 for (size_t i=0; i<recipe.size(); i++) {
513 //dstream<<" ("<<input_filtered[i]<<" == "<<recipe_copy[i]<<")";
514 if (!inputItemMatchesRecipe(input_filtered[i], recipe_copy[i],
520 //dstream<<" -> match="<<all_match<<std::endl;
523 } while (std::next_permutation(recipe_copy.begin(), recipe_copy.end()));
528 CraftOutput CraftDefinitionShapeless::getOutput(const CraftInput &input, IGameDef *gamedef) const
530 return CraftOutput(output, 0);
533 CraftInput CraftDefinitionShapeless::getInput(const CraftOutput &output, IGameDef *gamedef) const
535 return CraftInput(CRAFT_METHOD_NORMAL, 0, craftGetItems(recipe, gamedef));
538 void CraftDefinitionShapeless::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
539 IGameDef *gamedef) const
541 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
544 CraftHashType CraftDefinitionShapeless::getHashType() const
546 assert(hash_inited); // Pre-condition
547 bool has_group = false;
548 for (size_t i = 0; i < recipe_names.size(); i++) {
549 if (isGroupRecipeStr(recipe_names[i])) {
555 return CRAFT_HASH_TYPE_COUNT;
557 return CRAFT_HASH_TYPE_ITEM_NAMES;
560 u64 CraftDefinitionShapeless::getHash(CraftHashType type) const
562 assert(hash_inited); // Pre-condition
563 assert(type == CRAFT_HASH_TYPE_ITEM_NAMES
564 || type == CRAFT_HASH_TYPE_COUNT); // Pre-condition
565 return getHashForGrid(type, recipe_names);
568 void CraftDefinitionShapeless::initHash(IGameDef *gamedef)
573 recipe_names = craftGetItemNames(recipe, gamedef);
574 std::sort(recipe_names.begin(), recipe_names.end());
577 std::string CraftDefinitionShapeless::dump() const
579 std::ostringstream os(std::ios::binary);
580 os << "(shapeless, output=\"" << output
581 << "\", recipe=" << craftDumpMatrix(recipe, recipe.size())
582 << ", replacements=" << replacements.dump() << ")";
587 CraftDefinitionToolRepair
590 static ItemStack craftToolRepair(
591 const ItemStack &item1,
592 const ItemStack &item2,
593 float additional_wear,
596 IItemDefManager *idef = gamedef->idef();
597 if (item1.count != 1 || item2.count != 1 || item1.name != item2.name
598 || idef->get(item1.name).type != ITEM_TOOL
599 || idef->get(item2.name).type != ITEM_TOOL) {
604 s32 item1_uses = 65536 - (u32) item1.wear;
605 s32 item2_uses = 65536 - (u32) item2.wear;
606 s32 new_uses = item1_uses + item2_uses;
607 s32 new_wear = 65536 - new_uses + floor(additional_wear * 65536 + 0.5);
608 if (new_wear >= 65536)
613 ItemStack repaired = item1;
614 repaired.wear = new_wear;
618 std::string CraftDefinitionToolRepair::getName() const
623 bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef) const
625 if (input.method != CRAFT_METHOD_NORMAL)
630 for (std::vector<ItemStack>::const_iterator
631 it = input.items.begin();
632 it != input.items.end(); it++) {
636 else if (item2.empty())
642 ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
643 return !repaired.empty();
646 CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameDef *gamedef) const
650 for (std::vector<ItemStack>::const_iterator
651 it = input.items.begin();
652 it != input.items.end(); it++) {
656 else if (item2.empty())
660 ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
661 return CraftOutput(repaired.getItemString(), 0);
664 CraftInput CraftDefinitionToolRepair::getInput(const CraftOutput &output, IGameDef *gamedef) const
666 std::vector<ItemStack> stack;
667 stack.push_back(ItemStack());
668 return CraftInput(CRAFT_METHOD_COOKING, additional_wear, stack);
671 void CraftDefinitionToolRepair::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
672 IGameDef *gamedef) const
674 craftDecrementInput(input, gamedef);
677 std::string CraftDefinitionToolRepair::dump() const
679 std::ostringstream os(std::ios::binary);
680 os << "(toolrepair, additional_wear=" << additional_wear << ")";
685 CraftDefinitionCooking
688 std::string CraftDefinitionCooking::getName() const
693 bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) const
695 if (input.method != CRAFT_METHOD_COOKING)
698 // Filter empty items out of input
699 std::vector<std::string> input_filtered;
700 for (std::vector<ItemStack>::const_iterator
701 it = input.items.begin();
702 it != input.items.end(); it++) {
704 input_filtered.push_back(it->name);
707 // If there is a wrong number of items in input, no match
708 if (input_filtered.size() != 1) {
709 /*dstream<<"Number of input items ("<<input_filtered.size()
710 <<") does not match recipe size (1) "
711 <<"of cooking recipe with output="<<output<<std::endl;*/
715 // Check the single input item
716 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
719 CraftOutput CraftDefinitionCooking::getOutput(const CraftInput &input, IGameDef *gamedef) const
721 return CraftOutput(output, cooktime);
724 CraftInput CraftDefinitionCooking::getInput(const CraftOutput &output, IGameDef *gamedef) const
726 std::vector<std::string> rec;
727 rec.push_back(recipe);
728 return CraftInput(CRAFT_METHOD_COOKING,cooktime,craftGetItems(rec,gamedef));
731 void CraftDefinitionCooking::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
732 IGameDef *gamedef) const
734 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
737 CraftHashType CraftDefinitionCooking::getHashType() const
739 if (isGroupRecipeStr(recipe_name))
740 return CRAFT_HASH_TYPE_COUNT;
742 return CRAFT_HASH_TYPE_ITEM_NAMES;
745 u64 CraftDefinitionCooking::getHash(CraftHashType type) const
747 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
748 return getHashForString(recipe_name);
749 } else if (type == CRAFT_HASH_TYPE_COUNT) {
752 //illegal hash type for this CraftDefinition (pre-condition)
758 void CraftDefinitionCooking::initHash(IGameDef *gamedef)
763 recipe_name = craftGetItemName(recipe, gamedef);
766 std::string CraftDefinitionCooking::dump() const
768 std::ostringstream os(std::ios::binary);
769 os << "(cooking, output=\"" << output
770 << "\", recipe=\"" << recipe
771 << "\", cooktime=" << cooktime << ")"
772 << ", replacements=" << replacements.dump() << ")";
780 std::string CraftDefinitionFuel::getName() const
785 bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) const
787 if (input.method != CRAFT_METHOD_FUEL)
790 // Filter empty items out of input
791 std::vector<std::string> input_filtered;
792 for (std::vector<ItemStack>::const_iterator
793 it = input.items.begin();
794 it != input.items.end(); it++) {
796 input_filtered.push_back(it->name);
799 // If there is a wrong number of items in input, no match
800 if (input_filtered.size() != 1) {
801 /*dstream<<"Number of input items ("<<input_filtered.size()
802 <<") does not match recipe size (1) "
803 <<"of fuel recipe with burntime="<<burntime<<std::endl;*/
807 // Check the single input item
808 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
811 CraftOutput CraftDefinitionFuel::getOutput(const CraftInput &input, IGameDef *gamedef) const
813 return CraftOutput("", burntime);
816 CraftInput CraftDefinitionFuel::getInput(const CraftOutput &output, IGameDef *gamedef) const
818 std::vector<std::string> rec;
819 rec.push_back(recipe);
820 return CraftInput(CRAFT_METHOD_COOKING,(int)burntime,craftGetItems(rec,gamedef));
823 void CraftDefinitionFuel::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
824 IGameDef *gamedef) const
826 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
829 CraftHashType CraftDefinitionFuel::getHashType() const
831 if (isGroupRecipeStr(recipe_name))
832 return CRAFT_HASH_TYPE_COUNT;
834 return CRAFT_HASH_TYPE_ITEM_NAMES;
837 u64 CraftDefinitionFuel::getHash(CraftHashType type) const
839 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
840 return getHashForString(recipe_name);
841 } else if (type == CRAFT_HASH_TYPE_COUNT) {
844 //illegal hash type for this CraftDefinition (pre-condition)
850 void CraftDefinitionFuel::initHash(IGameDef *gamedef)
855 recipe_name = craftGetItemName(recipe, gamedef);
857 std::string CraftDefinitionFuel::dump() const
859 std::ostringstream os(std::ios::binary);
860 os << "(fuel, recipe=\"" << recipe
861 << "\", burntime=" << burntime << ")"
862 << ", replacements=" << replacements.dump() << ")";
867 Craft definition manager
870 class CCraftDefManager: public IWritableCraftDefManager
875 m_craft_defs.resize(craft_hash_type_max + 1);
878 virtual ~CCraftDefManager()
883 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
884 std::vector<ItemStack> &output_replacement, bool decrementInput,
885 IGameDef *gamedef) const
890 // If all input items are empty, abort.
891 bool all_empty = true;
892 for (std::vector<ItemStack>::const_iterator
893 it = input.items.begin();
894 it != input.items.end(); it++) {
903 std::vector<std::string> input_names;
904 input_names = craftGetItemNames(input.items, gamedef);
905 std::sort(input_names.begin(), input_names.end());
907 // Try hash types with increasing collision rate, and return if found.
908 for (int type = 0; type <= craft_hash_type_max; type++) {
909 u64 hash = getHashForGrid((CraftHashType) type, input_names);
911 /*errorstream << "Checking type " << type << " with hash " << hash << std::endl;*/
913 // We'd like to do "const [...] hash_collisions = m_craft_defs[type][hash];"
914 // but that doesn't compile for some reason. This does.
915 std::map<u64, std::vector<CraftDefinition*> >::const_iterator
916 col_iter = (m_craft_defs[type]).find(hash);
918 if (col_iter == (m_craft_defs[type]).end())
921 const std::vector<CraftDefinition*> &hash_collisions = col_iter->second;
922 // Walk crafting definitions from back to front, so that later
923 // definitions can override earlier ones.
924 for (std::vector<CraftDefinition*>::const_reverse_iterator
925 it = hash_collisions.rbegin();
926 it != hash_collisions.rend(); it++) {
927 CraftDefinition *def = *it;
929 /*errorstream << "Checking " << input.dump() << std::endl
930 << " against " << def->dump() << std::endl;*/
932 if (def->check(input, gamedef)) {
933 // Get output, then decrement input (if requested)
934 output = def->getOutput(input, gamedef);
936 def->decrementInput(input, output_replacement, gamedef);
937 /*errorstream << "Check RETURNS TRUE" << std::endl;*/
945 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
946 IGameDef *gamedef, unsigned limit=0) const
948 std::vector<CraftDefinition*> recipes;
950 std::map<std::string, std::vector<CraftDefinition*> >::const_iterator
951 vec_iter = m_output_craft_definitions.find(output.item);
953 if (vec_iter == m_output_craft_definitions.end())
956 const std::vector<CraftDefinition*> &vec = vec_iter->second;
958 recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size());
960 for (std::vector<CraftDefinition*>::const_reverse_iterator
961 it = vec.rbegin(); it != vec.rend(); ++it) {
962 if (limit && recipes.size() >= limit)
964 recipes.push_back(*it);
969 virtual std::string dump() const
971 std::ostringstream os(std::ios::binary);
972 os << "Crafting definitions:\n";
973 for (int type = 0; type <= craft_hash_type_max; type++) {
974 for (std::map<u64, std::vector<CraftDefinition*> >::const_iterator
975 it = (m_craft_defs[type]).begin();
976 it != (m_craft_defs[type]).end(); it++) {
977 for (std::vector<CraftDefinition*>::const_iterator
978 iit = it->second.begin(); iit != it->second.end(); iit++) {
979 os << "type " << type << " hash " << it->first << (*iit)->dump() << "\n";
985 virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef)
987 verbosestream << "registerCraft: registering craft definition: "
988 << def->dump() << std::endl;
989 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].push_back(def);
992 std::string output_name = craftGetItemName(
993 def->getOutput(input, gamedef).item, gamedef);
994 m_output_craft_definitions[output_name].push_back(def);
998 for (int type = 0; type <= craft_hash_type_max; type++) {
999 for (std::map<u64, std::vector<CraftDefinition*> >::iterator
1000 it = m_craft_defs[type].begin();
1001 it != m_craft_defs[type].end(); it++) {
1002 for (std::vector<CraftDefinition*>::iterator
1003 iit = it->second.begin(); iit != it->second.end(); iit++) {
1008 m_craft_defs[type].clear();
1010 m_output_craft_definitions.clear();
1012 virtual void initHashes(IGameDef *gamedef)
1014 // Move the CraftDefs from the unhashed layer into layers higher up.
1015 for (std::vector<CraftDefinition*>::iterator
1016 it = (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).begin();
1017 it != (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).end(); it++) {
1018 CraftDefinition *def = *it;
1020 // Initialize and get the definition's hash
1021 def->initHash(gamedef);
1022 CraftHashType type = def->getHashType();
1023 u64 hash = def->getHash(type);
1025 // Enter the definition
1026 m_craft_defs[type][hash].push_back(def);
1028 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].clear();
1031 //TODO: change both maps to unordered_map when c++11 can be used
1032 std::vector<std::map<u64, std::vector<CraftDefinition*> > > m_craft_defs;
1033 std::map<std::string, std::vector<CraftDefinition*> > m_output_craft_definitions;
1036 IWritableCraftDefManager* createCraftDefManager()
1038 return new CCraftDefManager();