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
72 // Check if input matches recipe
73 // Takes recipe groups into account
74 static bool inputItemMatchesRecipe(const std::string &inp_name,
75 const std::string &rec_name, IItemDefManager *idef)
78 if (inp_name == rec_name)
82 if (isGroupRecipeStr(rec_name) && idef->isKnown(inp_name)) {
83 const struct ItemDefinition &def = idef->get(inp_name);
84 Strfnd f(rec_name.substr(6));
85 bool all_groups_match = true;
87 std::string check_group = f.next(",");
88 if (itemgroup_get(def.groups, check_group) == 0) {
89 all_groups_match = false;
101 // Deserialize an itemstring then return the name of the item
102 static std::string craftGetItemName(const std::string &itemstring, IGameDef *gamedef)
105 item.deSerialize(itemstring, gamedef->idef());
109 // (mapcar craftGetItemName itemstrings)
110 static std::vector<std::string> craftGetItemNames(
111 const std::vector<std::string> &itemstrings, IGameDef *gamedef)
113 std::vector<std::string> result;
114 for (std::vector<std::string>::const_iterator
115 it = itemstrings.begin();
116 it != itemstrings.end(); it++) {
117 result.push_back(craftGetItemName(*it, gamedef));
122 // Get name of each item, and return them as a new list.
123 static std::vector<std::string> craftGetItemNames(
124 const std::vector<ItemStack> &items, IGameDef *gamedef)
126 std::vector<std::string> result;
127 for (std::vector<ItemStack>::const_iterator
129 it != items.end(); it++) {
130 result.push_back(it->name);
135 // convert a list of item names, to ItemStacks.
136 static std::vector<ItemStack> craftGetItems(
137 const std::vector<std::string> &items, IGameDef *gamedef)
139 std::vector<ItemStack> result;
140 for (std::vector<std::string>::const_iterator
142 it != items.end(); it++) {
143 result.push_back(ItemStack(std::string(*it), (u16)1,
144 (u16)0, "", gamedef->getItemDefManager()));
149 // Compute bounding rectangle given a matrix of items
150 // Returns false if every item is ""
151 static bool craftGetBounds(const std::vector<std::string> &items, unsigned int width,
152 unsigned int &min_x, unsigned int &max_x,
153 unsigned int &min_y, unsigned int &max_y)
155 bool success = false;
158 for (std::vector<std::string>::const_iterator
160 it != items.end(); it++) {
161 // Is this an actual item?
164 // This is the first nonempty item
169 if (x < min_x) min_x = x;
170 if (x > max_x) max_x = x;
171 if (y < min_y) min_y = y;
172 if (y > max_y) max_y = y;
186 // Removes 1 from each item stack
187 static void craftDecrementInput(CraftInput &input, IGameDef *gamedef)
189 for (std::vector<ItemStack>::iterator
190 it = input.items.begin();
191 it != input.items.end(); it++) {
197 // Removes 1 from each item stack with replacement support
198 // Example: if replacements contains the pair ("bucket:bucket_water", "bucket:bucket_empty"),
199 // a water bucket will not be removed but replaced by an empty bucket.
200 static void craftDecrementOrReplaceInput(CraftInput &input,
201 const CraftReplacements &replacements,
204 if (replacements.pairs.empty()) {
205 craftDecrementInput(input, gamedef);
209 // Make a copy of the replacements pair list
210 std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs;
212 for (std::vector<ItemStack>::iterator
213 it = input.items.begin();
214 it != input.items.end(); it++) {
215 if (it->count == 1) {
216 // Find an appropriate replacement
217 bool found_replacement = false;
218 for (std::vector<std::pair<std::string, std::string> >::iterator
220 j != pairs.end(); j++) {
221 if (it->name == craftGetItemName(j->first, gamedef)) {
222 it->deSerialize(j->second, gamedef->idef());
223 found_replacement = true;
228 // No replacement was found, simply decrement count to zero
229 if (!found_replacement)
231 } else if (it->count >= 2) {
232 // Ignore replacements for items with count >= 2
238 // Dump an itemstring matrix
239 static std::string craftDumpMatrix(const std::vector<std::string> &items,
242 std::ostringstream os(std::ios::binary);
245 for(std::vector<std::string>::const_iterator
247 it != items.end(); it++, x++) {
254 os << '"' << (*it) << '"';
260 // Dump an item matrix
261 std::string craftDumpMatrix(const std::vector<ItemStack> &items,
264 std::ostringstream os(std::ios::binary);
267 for (std::vector<ItemStack>::const_iterator
269 it != items.end(); it++, x++) {
276 os << '"' << (it->getItemString()) << '"';
287 std::string CraftInput::dump() const
289 std::ostringstream os(std::ios::binary);
290 os << "(method=" << ((int)method) << ", items="
291 << craftDumpMatrix(items, width) << ")";
299 std::string CraftOutput::dump() const
301 std::ostringstream os(std::ios::binary);
302 os << "(item=\"" << item << "\", time=" << time << ")";
310 std::string CraftReplacements::dump() const
312 std::ostringstream os(std::ios::binary);
314 const char *sep = "";
315 for (std::vector<std::pair<std::string, std::string> >::const_iterator
317 it != pairs.end(); it++) {
318 os << sep << '"' << (it->first) << "\"=>\"" << (it->second) << '"';
326 CraftDefinitionShaped
329 std::string CraftDefinitionShaped::getName() const
334 bool CraftDefinitionShaped::check(const CraftInput &input, IGameDef *gamedef) const
336 if (input.method != CRAFT_METHOD_NORMAL)
339 // Get input item matrix
340 std::vector<std::string> inp_names = craftGetItemNames(input.items, gamedef);
341 unsigned int inp_width = input.width;
344 while (inp_names.size() % inp_width != 0)
345 inp_names.push_back("");
348 unsigned int inp_min_x = 0, inp_max_x = 0, inp_min_y = 0, inp_max_y = 0;
349 if (!craftGetBounds(inp_names, inp_width, inp_min_x, inp_max_x,
350 inp_min_y, inp_max_y))
351 return false; // it was empty
353 std::vector<std::string> rec_names;
355 rec_names = recipe_names;
357 rec_names = craftGetItemNames(recipe, gamedef);
359 // Get recipe item matrix
360 unsigned int rec_width = width;
363 while (rec_names.size() % rec_width != 0)
364 rec_names.push_back("");
367 unsigned int rec_min_x=0, rec_max_x=0, rec_min_y=0, rec_max_y=0;
368 if (!craftGetBounds(rec_names, rec_width, rec_min_x, rec_max_x,
369 rec_min_y, rec_max_y))
370 return false; // it was empty
373 if (inp_max_x - inp_min_x != rec_max_x - rec_min_x ||
374 inp_max_y - inp_min_y != rec_max_y - rec_min_y)
377 // Verify that all item names in the bounding box are equal
378 unsigned int w = inp_max_x - inp_min_x + 1;
379 unsigned int h = inp_max_y - inp_min_y + 1;
381 for (unsigned int y=0; y < h; y++) {
382 unsigned int inp_y = (inp_min_y + y) * inp_width;
383 unsigned int rec_y = (rec_min_y + y) * rec_width;
385 for (unsigned int x=0; x < w; x++) {
386 unsigned int inp_x = inp_min_x + x;
387 unsigned int rec_x = rec_min_x + x;
389 if (!inputItemMatchesRecipe(
390 inp_names[inp_y + inp_x],
391 rec_names[rec_y + rec_x], gamedef->idef())) {
400 CraftOutput CraftDefinitionShaped::getOutput(const CraftInput &input, IGameDef *gamedef) const
402 return CraftOutput(output, 0);
405 CraftInput CraftDefinitionShaped::getInput(const CraftOutput &output, IGameDef *gamedef) const
407 return CraftInput(CRAFT_METHOD_NORMAL,width,craftGetItems(recipe,gamedef));
410 void CraftDefinitionShaped::decrementInput(CraftInput &input, IGameDef *gamedef) const
412 craftDecrementOrReplaceInput(input, replacements, gamedef);
415 CraftHashType CraftDefinitionShaped::getHashType() const
417 assert(hash_inited); // Pre-condition
418 bool has_group = false;
419 for (size_t i = 0; i < recipe_names.size(); i++) {
420 if (isGroupRecipeStr(recipe_names[i])) {
426 return CRAFT_HASH_TYPE_COUNT;
428 return CRAFT_HASH_TYPE_ITEM_NAMES;
431 u64 CraftDefinitionShaped::getHash(CraftHashType type) const
433 assert(hash_inited); // Pre-condition
434 assert((type == CRAFT_HASH_TYPE_ITEM_NAMES)
435 || (type == CRAFT_HASH_TYPE_COUNT)); // Pre-condition
437 std::vector<std::string> rec_names = recipe_names;
438 std::sort(rec_names.begin(), rec_names.end());
439 return getHashForGrid(type, rec_names);
442 void CraftDefinitionShaped::initHash(IGameDef *gamedef)
447 recipe_names = craftGetItemNames(recipe, gamedef);
450 std::string CraftDefinitionShaped::dump() const
452 std::ostringstream os(std::ios::binary);
453 os << "(shaped, output=\"" << output
454 << "\", recipe=" << craftDumpMatrix(recipe, width)
455 << ", replacements=" << replacements.dump() << ")";
460 CraftDefinitionShapeless
463 std::string CraftDefinitionShapeless::getName() const
468 bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef) const
470 if (input.method != CRAFT_METHOD_NORMAL)
473 // Filter empty items out of input
474 std::vector<std::string> input_filtered;
475 for (std::vector<ItemStack>::const_iterator
476 it = input.items.begin();
477 it != input.items.end(); it++) {
479 input_filtered.push_back(it->name);
482 // If there is a wrong number of items in input, no match
483 if (input_filtered.size() != recipe.size()) {
484 /*dstream<<"Number of input items ("<<input_filtered.size()
485 <<") does not match recipe size ("<<recipe.size()<<") "
486 <<"of recipe with output="<<output<<std::endl;*/
490 std::vector<std::string> recipe_copy;
492 recipe_copy = recipe_names;
494 recipe_copy = craftGetItemNames(recipe, gamedef);
495 std::sort(recipe_copy.begin(), recipe_copy.end());
498 // Try with all permutations of the recipe,
499 // start from the lexicographically first permutation (=sorted),
500 // recipe_names is pre-sorted
502 // If all items match, the recipe matches
503 bool all_match = true;
504 //dstream<<"Testing recipe (output="<<output<<"):";
505 for (size_t i=0; i<recipe.size(); i++) {
506 //dstream<<" ("<<input_filtered[i]<<" == "<<recipe_copy[i]<<")";
507 if (!inputItemMatchesRecipe(input_filtered[i], recipe_copy[i],
513 //dstream<<" -> match="<<all_match<<std::endl;
516 } while (std::next_permutation(recipe_copy.begin(), recipe_copy.end()));
521 CraftOutput CraftDefinitionShapeless::getOutput(const CraftInput &input, IGameDef *gamedef) const
523 return CraftOutput(output, 0);
526 CraftInput CraftDefinitionShapeless::getInput(const CraftOutput &output, IGameDef *gamedef) const
528 return CraftInput(CRAFT_METHOD_NORMAL, 0, craftGetItems(recipe, gamedef));
531 void CraftDefinitionShapeless::decrementInput(CraftInput &input, IGameDef *gamedef) const
533 craftDecrementOrReplaceInput(input, replacements, gamedef);
536 CraftHashType CraftDefinitionShapeless::getHashType() const
538 assert(hash_inited); // Pre-condition
539 bool has_group = false;
540 for (size_t i = 0; i < recipe_names.size(); i++) {
541 if (isGroupRecipeStr(recipe_names[i])) {
547 return CRAFT_HASH_TYPE_COUNT;
549 return CRAFT_HASH_TYPE_ITEM_NAMES;
552 u64 CraftDefinitionShapeless::getHash(CraftHashType type) const
554 assert(hash_inited); // Pre-condition
555 assert(type == CRAFT_HASH_TYPE_ITEM_NAMES
556 || type == CRAFT_HASH_TYPE_COUNT); // Pre-condition
557 return getHashForGrid(type, recipe_names);
560 void CraftDefinitionShapeless::initHash(IGameDef *gamedef)
565 recipe_names = craftGetItemNames(recipe, gamedef);
566 std::sort(recipe_names.begin(), recipe_names.end());
569 std::string CraftDefinitionShapeless::dump() const
571 std::ostringstream os(std::ios::binary);
572 os << "(shapeless, output=\"" << output
573 << "\", recipe=" << craftDumpMatrix(recipe, recipe.size())
574 << ", replacements=" << replacements.dump() << ")";
579 CraftDefinitionToolRepair
582 static ItemStack craftToolRepair(
583 const ItemStack &item1,
584 const ItemStack &item2,
585 float additional_wear,
588 IItemDefManager *idef = gamedef->idef();
589 if (item1.count != 1 || item2.count != 1 || item1.name != item2.name
590 || idef->get(item1.name).type != ITEM_TOOL
591 || idef->get(item2.name).type != ITEM_TOOL) {
596 s32 item1_uses = 65536 - (u32) item1.wear;
597 s32 item2_uses = 65536 - (u32) item2.wear;
598 s32 new_uses = item1_uses + item2_uses;
599 s32 new_wear = 65536 - new_uses + floor(additional_wear * 65536 + 0.5);
600 if (new_wear >= 65536)
605 ItemStack repaired = item1;
606 repaired.wear = new_wear;
610 std::string CraftDefinitionToolRepair::getName() const
615 bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef) const
617 if (input.method != CRAFT_METHOD_NORMAL)
622 for (std::vector<ItemStack>::const_iterator
623 it = input.items.begin();
624 it != input.items.end(); it++) {
628 else if (item2.empty())
634 ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
635 return !repaired.empty();
638 CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameDef *gamedef) const
642 for (std::vector<ItemStack>::const_iterator
643 it = input.items.begin();
644 it != input.items.end(); it++) {
648 else if (item2.empty())
652 ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
653 return CraftOutput(repaired.getItemString(), 0);
656 CraftInput CraftDefinitionToolRepair::getInput(const CraftOutput &output, IGameDef *gamedef) const
658 std::vector<ItemStack> stack;
659 stack.push_back(ItemStack());
660 return CraftInput(CRAFT_METHOD_COOKING, additional_wear, stack);
663 void CraftDefinitionToolRepair::decrementInput(CraftInput &input, IGameDef *gamedef) const
665 craftDecrementInput(input, gamedef);
668 std::string CraftDefinitionToolRepair::dump() const
670 std::ostringstream os(std::ios::binary);
671 os << "(toolrepair, additional_wear=" << additional_wear << ")";
676 CraftDefinitionCooking
679 std::string CraftDefinitionCooking::getName() const
684 bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) const
686 if (input.method != CRAFT_METHOD_COOKING)
689 // Filter empty items out of input
690 std::vector<std::string> input_filtered;
691 for (std::vector<ItemStack>::const_iterator
692 it = input.items.begin();
693 it != input.items.end(); it++) {
695 input_filtered.push_back(it->name);
698 // If there is a wrong number of items in input, no match
699 if (input_filtered.size() != 1) {
700 /*dstream<<"Number of input items ("<<input_filtered.size()
701 <<") does not match recipe size (1) "
702 <<"of cooking recipe with output="<<output<<std::endl;*/
706 // Check the single input item
707 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
710 CraftOutput CraftDefinitionCooking::getOutput(const CraftInput &input, IGameDef *gamedef) const
712 return CraftOutput(output, cooktime);
715 CraftInput CraftDefinitionCooking::getInput(const CraftOutput &output, IGameDef *gamedef) const
717 std::vector<std::string> rec;
718 rec.push_back(recipe);
719 return CraftInput(CRAFT_METHOD_COOKING,cooktime,craftGetItems(rec,gamedef));
722 void CraftDefinitionCooking::decrementInput(CraftInput &input, IGameDef *gamedef) const
724 craftDecrementOrReplaceInput(input, replacements, gamedef);
727 CraftHashType CraftDefinitionCooking::getHashType() const
729 if (isGroupRecipeStr(recipe_name))
730 return CRAFT_HASH_TYPE_COUNT;
732 return CRAFT_HASH_TYPE_ITEM_NAMES;
735 u64 CraftDefinitionCooking::getHash(CraftHashType type) const
737 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
738 return getHashForString(recipe_name);
739 } else if (type == CRAFT_HASH_TYPE_COUNT) {
742 //illegal hash type for this CraftDefinition (pre-condition)
748 void CraftDefinitionCooking::initHash(IGameDef *gamedef)
753 recipe_name = craftGetItemName(recipe, gamedef);
756 std::string CraftDefinitionCooking::dump() const
758 std::ostringstream os(std::ios::binary);
759 os << "(cooking, output=\"" << output
760 << "\", recipe=\"" << recipe
761 << "\", cooktime=" << cooktime << ")"
762 << ", replacements=" << replacements.dump() << ")";
770 std::string CraftDefinitionFuel::getName() const
775 bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) const
777 if (input.method != CRAFT_METHOD_FUEL)
780 // Filter empty items out of input
781 std::vector<std::string> input_filtered;
782 for (std::vector<ItemStack>::const_iterator
783 it = input.items.begin();
784 it != input.items.end(); it++) {
786 input_filtered.push_back(it->name);
789 // If there is a wrong number of items in input, no match
790 if (input_filtered.size() != 1) {
791 /*dstream<<"Number of input items ("<<input_filtered.size()
792 <<") does not match recipe size (1) "
793 <<"of fuel recipe with burntime="<<burntime<<std::endl;*/
797 // Check the single input item
798 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
801 CraftOutput CraftDefinitionFuel::getOutput(const CraftInput &input, IGameDef *gamedef) const
803 return CraftOutput("", burntime);
806 CraftInput CraftDefinitionFuel::getInput(const CraftOutput &output, IGameDef *gamedef) const
808 std::vector<std::string> rec;
809 rec.push_back(recipe);
810 return CraftInput(CRAFT_METHOD_COOKING,(int)burntime,craftGetItems(rec,gamedef));
813 void CraftDefinitionFuel::decrementInput(CraftInput &input, IGameDef *gamedef) const
815 craftDecrementOrReplaceInput(input, replacements, gamedef);
818 CraftHashType CraftDefinitionFuel::getHashType() const
820 if (isGroupRecipeStr(recipe_name))
821 return CRAFT_HASH_TYPE_COUNT;
823 return CRAFT_HASH_TYPE_ITEM_NAMES;
826 u64 CraftDefinitionFuel::getHash(CraftHashType type) const
828 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
829 return getHashForString(recipe_name);
830 } else if (type == CRAFT_HASH_TYPE_COUNT) {
833 //illegal hash type for this CraftDefinition (pre-condition)
839 void CraftDefinitionFuel::initHash(IGameDef *gamedef)
844 recipe_name = craftGetItemName(recipe, gamedef);
846 std::string CraftDefinitionFuel::dump() const
848 std::ostringstream os(std::ios::binary);
849 os << "(fuel, recipe=\"" << recipe
850 << "\", burntime=" << burntime << ")"
851 << ", replacements=" << replacements.dump() << ")";
856 Craft definition manager
859 class CCraftDefManager: public IWritableCraftDefManager
864 m_craft_defs.resize(craft_hash_type_max + 1);
867 virtual ~CCraftDefManager()
872 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
873 bool decrementInput, IGameDef *gamedef) const
878 // If all input items are empty, abort.
879 bool all_empty = true;
880 for (std::vector<ItemStack>::const_iterator
881 it = input.items.begin();
882 it != input.items.end(); it++) {
891 std::vector<std::string> input_names;
892 input_names = craftGetItemNames(input.items, gamedef);
893 std::sort(input_names.begin(), input_names.end());
895 // Try hash types with increasing collision rate, and return if found.
896 for (int type = 0; type <= craft_hash_type_max; type++) {
897 u64 hash = getHashForGrid((CraftHashType) type, input_names);
899 /*errorstream << "Checking type " << type << " with hash " << hash << std::endl;*/
901 // We'd like to do "const [...] hash_collisions = m_craft_defs[type][hash];"
902 // but that doesn't compile for some reason. This does.
903 std::map<u64, std::vector<CraftDefinition*> >::const_iterator
904 col_iter = (m_craft_defs[type]).find(hash);
906 if (col_iter == (m_craft_defs[type]).end())
909 const std::vector<CraftDefinition*> &hash_collisions = col_iter->second;
910 // Walk crafting definitions from back to front, so that later
911 // definitions can override earlier ones.
912 for (std::vector<CraftDefinition*>::const_reverse_iterator
913 it = hash_collisions.rbegin();
914 it != hash_collisions.rend(); it++) {
915 CraftDefinition *def = *it;
917 /*errorstream << "Checking " << input.dump() << std::endl
918 << " against " << def->dump() << std::endl;*/
920 if (def->check(input, gamedef)) {
921 // Get output, then decrement input (if requested)
922 output = def->getOutput(input, gamedef);
924 def->decrementInput(input, gamedef);
925 /*errorstream << "Check RETURNS TRUE" << std::endl;*/
933 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
934 IGameDef *gamedef, unsigned limit=0) const
936 std::vector<CraftDefinition*> recipes;
938 std::map<std::string, std::vector<CraftDefinition*> >::const_iterator
939 vec_iter = m_output_craft_definitions.find(output.item);
941 if (vec_iter == m_output_craft_definitions.end())
944 const std::vector<CraftDefinition*> &vec = vec_iter->second;
946 recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size());
948 for (std::vector<CraftDefinition*>::const_reverse_iterator
949 it = vec.rbegin(); it != vec.rend(); ++it) {
950 if (limit && recipes.size() >= limit)
952 recipes.push_back(*it);
957 virtual std::string dump() const
959 std::ostringstream os(std::ios::binary);
960 os << "Crafting definitions:\n";
961 for (int type = 0; type <= craft_hash_type_max; type++) {
962 for (std::map<u64, std::vector<CraftDefinition*> >::const_iterator
963 it = (m_craft_defs[type]).begin();
964 it != (m_craft_defs[type]).end(); it++) {
965 for (std::vector<CraftDefinition*>::const_iterator
966 iit = it->second.begin(); iit != it->second.end(); iit++) {
967 os << "type " << type << " hash " << it->first << (*iit)->dump() << "\n";
973 virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef)
975 verbosestream << "registerCraft: registering craft definition: "
976 << def->dump() << std::endl;
977 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].push_back(def);
980 std::string output_name = craftGetItemName(
981 def->getOutput(input, gamedef).item, gamedef);
982 m_output_craft_definitions[output_name].push_back(def);
986 for (int type = 0; type <= craft_hash_type_max; type++) {
987 for (std::map<u64, std::vector<CraftDefinition*> >::iterator
988 it = m_craft_defs[type].begin();
989 it != m_craft_defs[type].end(); it++) {
990 for (std::vector<CraftDefinition*>::iterator
991 iit = it->second.begin(); iit != it->second.end(); iit++) {
996 m_craft_defs[type].clear();
998 m_output_craft_definitions.clear();
1000 virtual void initHashes(IGameDef *gamedef)
1002 // Move the CraftDefs from the unhashed layer into layers higher up.
1003 for (std::vector<CraftDefinition*>::iterator
1004 it = (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).begin();
1005 it != (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).end(); it++) {
1006 CraftDefinition *def = *it;
1008 // Initialize and get the definition's hash
1009 def->initHash(gamedef);
1010 CraftHashType type = def->getHashType();
1011 u64 hash = def->getHash(type);
1013 // Enter the definition
1014 m_craft_defs[type][hash].push_back(def);
1016 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].clear();
1019 //TODO: change both maps to unordered_map when c++11 can be used
1020 std::vector<std::map<u64, std::vector<CraftDefinition*> > > m_craft_defs;
1021 std::map<std::string, std::vector<CraftDefinition*> > m_output_craft_definitions;
1024 IWritableCraftDefManager* createCraftDefManager()
1026 return new CCraftDefManager();