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 const CraftReplacements &replacements,
205 if (replacements.pairs.empty()) {
206 craftDecrementInput(input, gamedef);
210 // Make a copy of the replacements pair list
211 std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs;
213 for (std::vector<ItemStack>::iterator
214 it = input.items.begin();
215 it != input.items.end(); it++) {
216 if (it->count == 1) {
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 it->deSerialize(j->second, gamedef->idef());
224 found_replacement = true;
229 // No replacement was found, simply decrement count to zero
230 if (!found_replacement)
232 } else if (it->count >= 2) {
233 // Ignore replacements for items with count >= 2
239 // Dump an itemstring matrix
240 static std::string craftDumpMatrix(const std::vector<std::string> &items,
243 std::ostringstream os(std::ios::binary);
246 for(std::vector<std::string>::const_iterator
248 it != items.end(); it++, x++) {
255 os << '"' << (*it) << '"';
261 // Dump an item matrix
262 std::string craftDumpMatrix(const std::vector<ItemStack> &items,
265 std::ostringstream os(std::ios::binary);
268 for (std::vector<ItemStack>::const_iterator
270 it != items.end(); it++, x++) {
277 os << '"' << (it->getItemString()) << '"';
288 std::string CraftInput::dump() const
290 std::ostringstream os(std::ios::binary);
291 os << "(method=" << ((int)method) << ", items="
292 << craftDumpMatrix(items, width) << ")";
300 std::string CraftOutput::dump() const
302 std::ostringstream os(std::ios::binary);
303 os << "(item=\"" << item << "\", time=" << time << ")";
311 std::string CraftReplacements::dump() const
313 std::ostringstream os(std::ios::binary);
315 const char *sep = "";
316 for (std::vector<std::pair<std::string, std::string> >::const_iterator
318 it != pairs.end(); it++) {
319 os << sep << '"' << (it->first) << "\"=>\"" << (it->second) << '"';
327 CraftDefinitionShaped
330 std::string CraftDefinitionShaped::getName() const
335 bool CraftDefinitionShaped::check(const CraftInput &input, IGameDef *gamedef) const
337 if (input.method != CRAFT_METHOD_NORMAL)
340 // Get input item matrix
341 std::vector<std::string> inp_names = craftGetItemNames(input.items, gamedef);
342 unsigned int inp_width = input.width;
345 while (inp_names.size() % inp_width != 0)
346 inp_names.push_back("");
349 unsigned int inp_min_x = 0, inp_max_x = 0, inp_min_y = 0, inp_max_y = 0;
350 if (!craftGetBounds(inp_names, inp_width, inp_min_x, inp_max_x,
351 inp_min_y, inp_max_y))
352 return false; // it was empty
354 std::vector<std::string> rec_names;
356 rec_names = recipe_names;
358 rec_names = craftGetItemNames(recipe, gamedef);
360 // Get recipe item matrix
361 unsigned int rec_width = width;
364 while (rec_names.size() % rec_width != 0)
365 rec_names.push_back("");
368 unsigned int rec_min_x=0, rec_max_x=0, rec_min_y=0, rec_max_y=0;
369 if (!craftGetBounds(rec_names, rec_width, rec_min_x, rec_max_x,
370 rec_min_y, rec_max_y))
371 return false; // it was empty
374 if (inp_max_x - inp_min_x != rec_max_x - rec_min_x ||
375 inp_max_y - inp_min_y != rec_max_y - rec_min_y)
378 // Verify that all item names in the bounding box are equal
379 unsigned int w = inp_max_x - inp_min_x + 1;
380 unsigned int h = inp_max_y - inp_min_y + 1;
382 for (unsigned int y=0; y < h; y++) {
383 unsigned int inp_y = (inp_min_y + y) * inp_width;
384 unsigned int rec_y = (rec_min_y + y) * rec_width;
386 for (unsigned int x=0; x < w; x++) {
387 unsigned int inp_x = inp_min_x + x;
388 unsigned int rec_x = rec_min_x + x;
390 if (!inputItemMatchesRecipe(
391 inp_names[inp_y + inp_x],
392 rec_names[rec_y + rec_x], gamedef->idef())) {
401 CraftOutput CraftDefinitionShaped::getOutput(const CraftInput &input, IGameDef *gamedef) const
403 return CraftOutput(output, 0);
406 CraftInput CraftDefinitionShaped::getInput(const CraftOutput &output, IGameDef *gamedef) const
408 return CraftInput(CRAFT_METHOD_NORMAL,width,craftGetItems(recipe,gamedef));
411 void CraftDefinitionShaped::decrementInput(CraftInput &input, IGameDef *gamedef) const
413 craftDecrementOrReplaceInput(input, replacements, gamedef);
416 CraftHashType CraftDefinitionShaped::getHashType() const
418 assert(hash_inited); // Pre-condition
419 bool has_group = false;
420 for (size_t i = 0; i < recipe_names.size(); i++) {
421 if (isGroupRecipeStr(recipe_names[i])) {
427 return CRAFT_HASH_TYPE_COUNT;
429 return CRAFT_HASH_TYPE_ITEM_NAMES;
432 u64 CraftDefinitionShaped::getHash(CraftHashType type) const
434 assert(hash_inited); // Pre-condition
435 assert((type == CRAFT_HASH_TYPE_ITEM_NAMES)
436 || (type == CRAFT_HASH_TYPE_COUNT)); // Pre-condition
438 std::vector<std::string> rec_names = recipe_names;
439 std::sort(rec_names.begin(), rec_names.end());
440 return getHashForGrid(type, rec_names);
443 void CraftDefinitionShaped::initHash(IGameDef *gamedef)
448 recipe_names = craftGetItemNames(recipe, gamedef);
451 std::string CraftDefinitionShaped::dump() const
453 std::ostringstream os(std::ios::binary);
454 os << "(shaped, output=\"" << output
455 << "\", recipe=" << craftDumpMatrix(recipe, width)
456 << ", replacements=" << replacements.dump() << ")";
461 CraftDefinitionShapeless
464 std::string CraftDefinitionShapeless::getName() const
469 bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef) const
471 if (input.method != CRAFT_METHOD_NORMAL)
474 // Filter empty items out of input
475 std::vector<std::string> input_filtered;
476 for (std::vector<ItemStack>::const_iterator
477 it = input.items.begin();
478 it != input.items.end(); it++) {
480 input_filtered.push_back(it->name);
483 // If there is a wrong number of items in input, no match
484 if (input_filtered.size() != recipe.size()) {
485 /*dstream<<"Number of input items ("<<input_filtered.size()
486 <<") does not match recipe size ("<<recipe.size()<<") "
487 <<"of recipe with output="<<output<<std::endl;*/
491 std::vector<std::string> recipe_copy;
493 recipe_copy = recipe_names;
495 recipe_copy = craftGetItemNames(recipe, gamedef);
496 std::sort(recipe_copy.begin(), recipe_copy.end());
499 // Try with all permutations of the recipe,
500 // start from the lexicographically first permutation (=sorted),
501 // recipe_names is pre-sorted
503 // If all items match, the recipe matches
504 bool all_match = true;
505 //dstream<<"Testing recipe (output="<<output<<"):";
506 for (size_t i=0; i<recipe.size(); i++) {
507 //dstream<<" ("<<input_filtered[i]<<" == "<<recipe_copy[i]<<")";
508 if (!inputItemMatchesRecipe(input_filtered[i], recipe_copy[i],
514 //dstream<<" -> match="<<all_match<<std::endl;
517 } while (std::next_permutation(recipe_copy.begin(), recipe_copy.end()));
522 CraftOutput CraftDefinitionShapeless::getOutput(const CraftInput &input, IGameDef *gamedef) const
524 return CraftOutput(output, 0);
527 CraftInput CraftDefinitionShapeless::getInput(const CraftOutput &output, IGameDef *gamedef) const
529 return CraftInput(CRAFT_METHOD_NORMAL, 0, craftGetItems(recipe, gamedef));
532 void CraftDefinitionShapeless::decrementInput(CraftInput &input, IGameDef *gamedef) const
534 craftDecrementOrReplaceInput(input, replacements, gamedef);
537 CraftHashType CraftDefinitionShapeless::getHashType() const
539 assert(hash_inited); // Pre-condition
540 bool has_group = false;
541 for (size_t i = 0; i < recipe_names.size(); i++) {
542 if (isGroupRecipeStr(recipe_names[i])) {
548 return CRAFT_HASH_TYPE_COUNT;
550 return CRAFT_HASH_TYPE_ITEM_NAMES;
553 u64 CraftDefinitionShapeless::getHash(CraftHashType type) const
555 assert(hash_inited); // Pre-condition
556 assert(type == CRAFT_HASH_TYPE_ITEM_NAMES
557 || type == CRAFT_HASH_TYPE_COUNT); // Pre-condition
558 return getHashForGrid(type, recipe_names);
561 void CraftDefinitionShapeless::initHash(IGameDef *gamedef)
566 recipe_names = craftGetItemNames(recipe, gamedef);
567 std::sort(recipe_names.begin(), recipe_names.end());
570 std::string CraftDefinitionShapeless::dump() const
572 std::ostringstream os(std::ios::binary);
573 os << "(shapeless, output=\"" << output
574 << "\", recipe=" << craftDumpMatrix(recipe, recipe.size())
575 << ", replacements=" << replacements.dump() << ")";
580 CraftDefinitionToolRepair
583 static ItemStack craftToolRepair(
584 const ItemStack &item1,
585 const ItemStack &item2,
586 float additional_wear,
589 IItemDefManager *idef = gamedef->idef();
590 if (item1.count != 1 || item2.count != 1 || item1.name != item2.name
591 || idef->get(item1.name).type != ITEM_TOOL
592 || idef->get(item2.name).type != ITEM_TOOL) {
597 s32 item1_uses = 65536 - (u32) item1.wear;
598 s32 item2_uses = 65536 - (u32) item2.wear;
599 s32 new_uses = item1_uses + item2_uses;
600 s32 new_wear = 65536 - new_uses + floor(additional_wear * 65536 + 0.5);
601 if (new_wear >= 65536)
606 ItemStack repaired = item1;
607 repaired.wear = new_wear;
611 std::string CraftDefinitionToolRepair::getName() const
616 bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef) const
618 if (input.method != CRAFT_METHOD_NORMAL)
623 for (std::vector<ItemStack>::const_iterator
624 it = input.items.begin();
625 it != input.items.end(); it++) {
629 else if (item2.empty())
635 ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
636 return !repaired.empty();
639 CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameDef *gamedef) const
643 for (std::vector<ItemStack>::const_iterator
644 it = input.items.begin();
645 it != input.items.end(); it++) {
649 else if (item2.empty())
653 ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
654 return CraftOutput(repaired.getItemString(), 0);
657 CraftInput CraftDefinitionToolRepair::getInput(const CraftOutput &output, IGameDef *gamedef) const
659 std::vector<ItemStack> stack;
660 stack.push_back(ItemStack());
661 return CraftInput(CRAFT_METHOD_COOKING, additional_wear, stack);
664 void CraftDefinitionToolRepair::decrementInput(CraftInput &input, IGameDef *gamedef) const
666 craftDecrementInput(input, gamedef);
669 std::string CraftDefinitionToolRepair::dump() const
671 std::ostringstream os(std::ios::binary);
672 os << "(toolrepair, additional_wear=" << additional_wear << ")";
677 CraftDefinitionCooking
680 std::string CraftDefinitionCooking::getName() const
685 bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) const
687 if (input.method != CRAFT_METHOD_COOKING)
690 // Filter empty items out of input
691 std::vector<std::string> input_filtered;
692 for (std::vector<ItemStack>::const_iterator
693 it = input.items.begin();
694 it != input.items.end(); it++) {
696 input_filtered.push_back(it->name);
699 // If there is a wrong number of items in input, no match
700 if (input_filtered.size() != 1) {
701 /*dstream<<"Number of input items ("<<input_filtered.size()
702 <<") does not match recipe size (1) "
703 <<"of cooking recipe with output="<<output<<std::endl;*/
707 // Check the single input item
708 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
711 CraftOutput CraftDefinitionCooking::getOutput(const CraftInput &input, IGameDef *gamedef) const
713 return CraftOutput(output, cooktime);
716 CraftInput CraftDefinitionCooking::getInput(const CraftOutput &output, IGameDef *gamedef) const
718 std::vector<std::string> rec;
719 rec.push_back(recipe);
720 return CraftInput(CRAFT_METHOD_COOKING,cooktime,craftGetItems(rec,gamedef));
723 void CraftDefinitionCooking::decrementInput(CraftInput &input, IGameDef *gamedef) const
725 craftDecrementOrReplaceInput(input, replacements, gamedef);
728 CraftHashType CraftDefinitionCooking::getHashType() const
730 if (isGroupRecipeStr(recipe_name))
731 return CRAFT_HASH_TYPE_COUNT;
733 return CRAFT_HASH_TYPE_ITEM_NAMES;
736 u64 CraftDefinitionCooking::getHash(CraftHashType type) const
738 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
739 return getHashForString(recipe_name);
740 } else if (type == CRAFT_HASH_TYPE_COUNT) {
743 //illegal hash type for this CraftDefinition (pre-condition)
749 void CraftDefinitionCooking::initHash(IGameDef *gamedef)
754 recipe_name = craftGetItemName(recipe, gamedef);
757 std::string CraftDefinitionCooking::dump() const
759 std::ostringstream os(std::ios::binary);
760 os << "(cooking, output=\"" << output
761 << "\", recipe=\"" << recipe
762 << "\", cooktime=" << cooktime << ")"
763 << ", replacements=" << replacements.dump() << ")";
771 std::string CraftDefinitionFuel::getName() const
776 bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) const
778 if (input.method != CRAFT_METHOD_FUEL)
781 // Filter empty items out of input
782 std::vector<std::string> input_filtered;
783 for (std::vector<ItemStack>::const_iterator
784 it = input.items.begin();
785 it != input.items.end(); it++) {
787 input_filtered.push_back(it->name);
790 // If there is a wrong number of items in input, no match
791 if (input_filtered.size() != 1) {
792 /*dstream<<"Number of input items ("<<input_filtered.size()
793 <<") does not match recipe size (1) "
794 <<"of fuel recipe with burntime="<<burntime<<std::endl;*/
798 // Check the single input item
799 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
802 CraftOutput CraftDefinitionFuel::getOutput(const CraftInput &input, IGameDef *gamedef) const
804 return CraftOutput("", burntime);
807 CraftInput CraftDefinitionFuel::getInput(const CraftOutput &output, IGameDef *gamedef) const
809 std::vector<std::string> rec;
810 rec.push_back(recipe);
811 return CraftInput(CRAFT_METHOD_COOKING,(int)burntime,craftGetItems(rec,gamedef));
814 void CraftDefinitionFuel::decrementInput(CraftInput &input, IGameDef *gamedef) const
816 craftDecrementOrReplaceInput(input, replacements, gamedef);
819 CraftHashType CraftDefinitionFuel::getHashType() const
821 if (isGroupRecipeStr(recipe_name))
822 return CRAFT_HASH_TYPE_COUNT;
824 return CRAFT_HASH_TYPE_ITEM_NAMES;
827 u64 CraftDefinitionFuel::getHash(CraftHashType type) const
829 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
830 return getHashForString(recipe_name);
831 } else if (type == CRAFT_HASH_TYPE_COUNT) {
834 //illegal hash type for this CraftDefinition (pre-condition)
840 void CraftDefinitionFuel::initHash(IGameDef *gamedef)
845 recipe_name = craftGetItemName(recipe, gamedef);
847 std::string CraftDefinitionFuel::dump() const
849 std::ostringstream os(std::ios::binary);
850 os << "(fuel, recipe=\"" << recipe
851 << "\", burntime=" << burntime << ")"
852 << ", replacements=" << replacements.dump() << ")";
857 Craft definition manager
860 class CCraftDefManager: public IWritableCraftDefManager
865 m_craft_defs.resize(craft_hash_type_max + 1);
868 virtual ~CCraftDefManager()
873 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
874 bool decrementInput, IGameDef *gamedef) const
879 // If all input items are empty, abort.
880 bool all_empty = true;
881 for (std::vector<ItemStack>::const_iterator
882 it = input.items.begin();
883 it != input.items.end(); it++) {
892 std::vector<std::string> input_names;
893 input_names = craftGetItemNames(input.items, gamedef);
894 std::sort(input_names.begin(), input_names.end());
896 // Try hash types with increasing collision rate, and return if found.
897 for (int type = 0; type <= craft_hash_type_max; type++) {
898 u64 hash = getHashForGrid((CraftHashType) type, input_names);
900 /*errorstream << "Checking type " << type << " with hash " << hash << std::endl;*/
902 // We'd like to do "const [...] hash_collisions = m_craft_defs[type][hash];"
903 // but that doesn't compile for some reason. This does.
904 std::map<u64, std::vector<CraftDefinition*> >::const_iterator
905 col_iter = (m_craft_defs[type]).find(hash);
907 if (col_iter == (m_craft_defs[type]).end())
910 const std::vector<CraftDefinition*> &hash_collisions = col_iter->second;
911 // Walk crafting definitions from back to front, so that later
912 // definitions can override earlier ones.
913 for (std::vector<CraftDefinition*>::const_reverse_iterator
914 it = hash_collisions.rbegin();
915 it != hash_collisions.rend(); it++) {
916 CraftDefinition *def = *it;
918 /*errorstream << "Checking " << input.dump() << std::endl
919 << " against " << def->dump() << std::endl;*/
921 if (def->check(input, gamedef)) {
922 // Get output, then decrement input (if requested)
923 output = def->getOutput(input, gamedef);
925 def->decrementInput(input, gamedef);
926 /*errorstream << "Check RETURNS TRUE" << std::endl;*/
934 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
935 IGameDef *gamedef, unsigned limit=0) const
937 std::vector<CraftDefinition*> recipes;
939 std::map<std::string, std::vector<CraftDefinition*> >::const_iterator
940 vec_iter = m_output_craft_definitions.find(output.item);
942 if (vec_iter == m_output_craft_definitions.end())
945 const std::vector<CraftDefinition*> &vec = vec_iter->second;
947 recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size());
949 for (std::vector<CraftDefinition*>::const_reverse_iterator
950 it = vec.rbegin(); it != vec.rend(); ++it) {
951 if (limit && recipes.size() >= limit)
953 recipes.push_back(*it);
958 virtual std::string dump() const
960 std::ostringstream os(std::ios::binary);
961 os << "Crafting definitions:\n";
962 for (int type = 0; type <= craft_hash_type_max; type++) {
963 for (std::map<u64, std::vector<CraftDefinition*> >::const_iterator
964 it = (m_craft_defs[type]).begin();
965 it != (m_craft_defs[type]).end(); it++) {
966 for (std::vector<CraftDefinition*>::const_iterator
967 iit = it->second.begin(); iit != it->second.end(); iit++) {
968 os << "type " << type << " hash " << it->first << (*iit)->dump() << "\n";
974 virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef)
976 verbosestream << "registerCraft: registering craft definition: "
977 << def->dump() << std::endl;
978 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].push_back(def);
981 std::string output_name = craftGetItemName(
982 def->getOutput(input, gamedef).item, gamedef);
983 m_output_craft_definitions[output_name].push_back(def);
987 for (int type = 0; type <= craft_hash_type_max; type++) {
988 for (std::map<u64, std::vector<CraftDefinition*> >::iterator
989 it = m_craft_defs[type].begin();
990 it != m_craft_defs[type].end(); it++) {
991 for (std::vector<CraftDefinition*>::iterator
992 iit = it->second.begin(); iit != it->second.end(); iit++) {
997 m_craft_defs[type].clear();
999 m_output_craft_definitions.clear();
1001 virtual void initHashes(IGameDef *gamedef)
1003 // Move the CraftDefs from the unhashed layer into layers higher up.
1004 for (std::vector<CraftDefinition*>::iterator
1005 it = (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).begin();
1006 it != (m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]).end(); it++) {
1007 CraftDefinition *def = *it;
1009 // Initialize and get the definition's hash
1010 def->initHash(gamedef);
1011 CraftHashType type = def->getHashType();
1012 u64 hash = def->getHash(type);
1014 // Enter the definition
1015 m_craft_defs[type][hash].push_back(def);
1017 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].clear();
1020 //TODO: change both maps to unordered_map when c++11 can be used
1021 std::vector<std::map<u64, std::vector<CraftDefinition*> > > m_craft_defs;
1022 std::map<std::string, std::vector<CraftDefinition*> > m_output_craft_definitions;
1025 IWritableCraftDefManager* createCraftDefManager()
1027 return new CCraftDefManager();