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>::size_type i = 0;
116 i < itemstrings.size(); i++) {
117 result.push_back(craftGetItemName(itemstrings[i], 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>::size_type i = 0;
128 i < items.size(); i++) {
129 result.push_back(items[i].name);
134 // convert a list of item names, to ItemStacks.
135 static std::vector<ItemStack> craftGetItems(
136 const std::vector<std::string> &items, IGameDef *gamedef)
138 std::vector<ItemStack> result;
139 for (std::vector<std::string>::size_type i = 0;
140 i < items.size(); i++) {
141 result.push_back(ItemStack(std::string(items[i]), (u16)1,
142 (u16)0, "", gamedef->getItemDefManager()));
147 // Compute bounding rectangle given a matrix of items
148 // Returns false if every item is ""
149 static bool craftGetBounds(const std::vector<std::string> &items, unsigned int width,
150 unsigned int &min_x, unsigned int &max_x,
151 unsigned int &min_y, unsigned int &max_y)
153 bool success = false;
156 for (std::vector<std::string>::size_type i = 0;
157 i < items.size(); i++) {
158 // Is this an actual item?
159 if (items[i] != "") {
161 // This is the first nonempty item
166 if (x < min_x) min_x = x;
167 if (x > max_x) max_x = x;
168 if (y < min_y) min_y = y;
169 if (y > max_y) max_y = y;
183 // Removes 1 from each item stack
184 static void craftDecrementInput(CraftInput &input, IGameDef *gamedef)
186 for (std::vector<ItemStack>::size_type i = 0;
187 i < input.items.size(); i++) {
188 if (input.items[i].count != 0)
189 input.items[i].remove(1);
193 // Removes 1 from each item stack with replacement support
194 // Example: if replacements contains the pair ("bucket:bucket_water", "bucket:bucket_empty"),
195 // a water bucket will not be removed but replaced by an empty bucket.
196 static void craftDecrementOrReplaceInput(CraftInput &input,
197 std::vector<ItemStack> &output_replacements,
198 const CraftReplacements &replacements,
201 if (replacements.pairs.empty()) {
202 craftDecrementInput(input, gamedef);
206 // Make a copy of the replacements pair list
207 std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs;
209 for (std::vector<ItemStack>::size_type i = 0;
210 i < input.items.size(); i++) {
211 ItemStack &item = input.items[i];
212 // Find an appropriate replacement
213 bool found_replacement = false;
214 for (std::vector<std::pair<std::string, std::string> >::iterator
216 j != pairs.end(); ++j) {
217 if (item.name == craftGetItemName(j->first, gamedef)) {
218 if (item.count == 1) {
219 item.deSerialize(j->second, gamedef->idef());
220 found_replacement = true;
225 rep.deSerialize(j->second, gamedef->idef());
227 found_replacement = true;
228 output_replacements.push_back(rep);
233 // No replacement was found, simply decrement count by one
234 if (!found_replacement && item.count > 0)
239 // Dump an itemstring matrix
240 static std::string craftDumpMatrix(const std::vector<std::string> &items,
243 std::ostringstream os(std::ios::binary);
245 for(std::vector<std::string>::size_type i = 0;
246 i < items.size(); i++) {
253 os << '"' << items[i] << '"';
259 // Dump an item matrix
260 std::string craftDumpMatrix(const std::vector<ItemStack> &items,
263 std::ostringstream os(std::ios::binary);
265 for (std::vector<ItemStack>::size_type i = 0;
266 i < items.size(); i++) {
273 os << '"' << (items[i].getItemString()) << '"';
284 std::string CraftInput::dump() const
286 std::ostringstream os(std::ios::binary);
287 os << "(method=" << ((int)method) << ", items="
288 << craftDumpMatrix(items, width) << ")";
296 std::string CraftOutput::dump() const
298 std::ostringstream os(std::ios::binary);
299 os << "(item=\"" << item << "\", time=" << time << ")";
307 std::string CraftReplacements::dump() const
309 std::ostringstream os(std::ios::binary);
311 const char *sep = "";
312 for (std::vector<std::pair<std::string, std::string> >::size_type i = 0;
313 i < pairs.size(); i++) {
314 const std::pair<std::string, std::string> &repl_p = pairs[i];
316 << '"' << (repl_p.first)
317 << "\"=>\"" << (repl_p.second) << '"';
325 CraftDefinitionShaped
328 std::string CraftDefinitionShaped::getName() const
333 bool CraftDefinitionShaped::check(const CraftInput &input, IGameDef *gamedef) const
335 if (input.method != CRAFT_METHOD_NORMAL)
338 // Get input item matrix
339 std::vector<std::string> inp_names = craftGetItemNames(input.items, gamedef);
340 unsigned int inp_width = input.width;
343 while (inp_names.size() % inp_width != 0)
344 inp_names.push_back("");
347 unsigned int inp_min_x = 0, inp_max_x = 0, inp_min_y = 0, inp_max_y = 0;
348 if (!craftGetBounds(inp_names, inp_width, inp_min_x, inp_max_x,
349 inp_min_y, inp_max_y))
350 return false; // it was empty
352 std::vector<std::string> rec_names;
354 rec_names = recipe_names;
356 rec_names = craftGetItemNames(recipe, gamedef);
358 // Get recipe item matrix
359 unsigned int rec_width = width;
362 while (rec_names.size() % rec_width != 0)
363 rec_names.push_back("");
366 unsigned int rec_min_x=0, rec_max_x=0, rec_min_y=0, rec_max_y=0;
367 if (!craftGetBounds(rec_names, rec_width, rec_min_x, rec_max_x,
368 rec_min_y, rec_max_y))
369 return false; // it was empty
372 if (inp_max_x - inp_min_x != rec_max_x - rec_min_x ||
373 inp_max_y - inp_min_y != rec_max_y - rec_min_y)
376 // Verify that all item names in the bounding box are equal
377 unsigned int w = inp_max_x - inp_min_x + 1;
378 unsigned int h = inp_max_y - inp_min_y + 1;
380 for (unsigned int y=0; y < h; y++) {
381 unsigned int inp_y = (inp_min_y + y) * inp_width;
382 unsigned int rec_y = (rec_min_y + y) * rec_width;
384 for (unsigned int x=0; x < w; x++) {
385 unsigned int inp_x = inp_min_x + x;
386 unsigned int rec_x = rec_min_x + x;
388 if (!inputItemMatchesRecipe(
389 inp_names[inp_y + inp_x],
390 rec_names[rec_y + rec_x], gamedef->idef())) {
399 CraftOutput CraftDefinitionShaped::getOutput(const CraftInput &input, IGameDef *gamedef) const
401 return CraftOutput(output, 0);
404 CraftInput CraftDefinitionShaped::getInput(const CraftOutput &output, IGameDef *gamedef) const
406 return CraftInput(CRAFT_METHOD_NORMAL,width,craftGetItems(recipe,gamedef));
409 void CraftDefinitionShaped::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
410 IGameDef *gamedef) const
412 craftDecrementOrReplaceInput(input, output_replacements, 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>::size_type i = 0;
476 i < input.items.size(); i++) {
477 const ItemStack &item = input.items[i];
479 input_filtered.push_back(item.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, std::vector<ItemStack> &output_replacements,
532 IGameDef *gamedef) const
534 craftDecrementOrReplaceInput(input, output_replacements, 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>::size_type i = 0;
624 i < input.items.size(); i++) {
625 const ItemStack &item = input.items[i];
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>::size_type i = 0;
644 i < input.items.size(); i++) {
645 const ItemStack &item = input.items[i];
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, std::vector<ItemStack> &output_replacements,
665 IGameDef *gamedef) const
667 craftDecrementInput(input, gamedef);
670 std::string CraftDefinitionToolRepair::dump() const
672 std::ostringstream os(std::ios::binary);
673 os << "(toolrepair, additional_wear=" << additional_wear << ")";
678 CraftDefinitionCooking
681 std::string CraftDefinitionCooking::getName() const
686 bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) const
688 if (input.method != CRAFT_METHOD_COOKING)
691 // Filter empty items out of input
692 std::vector<std::string> input_filtered;
693 for (std::vector<ItemStack>::size_type i = 0;
694 i < input.items.size(); i++) {
695 const std::string &name = input.items[i].name;
697 input_filtered.push_back(name);
700 // If there is a wrong number of items in input, no match
701 if (input_filtered.size() != 1) {
702 /*dstream<<"Number of input items ("<<input_filtered.size()
703 <<") does not match recipe size (1) "
704 <<"of cooking recipe with output="<<output<<std::endl;*/
708 // Check the single input item
709 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
712 CraftOutput CraftDefinitionCooking::getOutput(const CraftInput &input, IGameDef *gamedef) const
714 return CraftOutput(output, cooktime);
717 CraftInput CraftDefinitionCooking::getInput(const CraftOutput &output, IGameDef *gamedef) const
719 std::vector<std::string> rec;
720 rec.push_back(recipe);
721 return CraftInput(CRAFT_METHOD_COOKING,cooktime,craftGetItems(rec,gamedef));
724 void CraftDefinitionCooking::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
725 IGameDef *gamedef) const
727 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
730 CraftHashType CraftDefinitionCooking::getHashType() const
732 if (isGroupRecipeStr(recipe_name))
733 return CRAFT_HASH_TYPE_COUNT;
735 return CRAFT_HASH_TYPE_ITEM_NAMES;
738 u64 CraftDefinitionCooking::getHash(CraftHashType type) const
740 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
741 return getHashForString(recipe_name);
742 } else if (type == CRAFT_HASH_TYPE_COUNT) {
745 //illegal hash type for this CraftDefinition (pre-condition)
751 void CraftDefinitionCooking::initHash(IGameDef *gamedef)
756 recipe_name = craftGetItemName(recipe, gamedef);
759 std::string CraftDefinitionCooking::dump() const
761 std::ostringstream os(std::ios::binary);
762 os << "(cooking, output=\"" << output
763 << "\", recipe=\"" << recipe
764 << "\", cooktime=" << cooktime << ")"
765 << ", replacements=" << replacements.dump() << ")";
773 std::string CraftDefinitionFuel::getName() const
778 bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) const
780 if (input.method != CRAFT_METHOD_FUEL)
783 // Filter empty items out of input
784 std::vector<std::string> input_filtered;
785 for (std::vector<ItemStack>::size_type i = 0;
786 i < input.items.size(); i++) {
787 const std::string &name = input.items[i].name;
789 input_filtered.push_back(name);
792 // If there is a wrong number of items in input, no match
793 if (input_filtered.size() != 1) {
794 /*dstream<<"Number of input items ("<<input_filtered.size()
795 <<") does not match recipe size (1) "
796 <<"of fuel recipe with burntime="<<burntime<<std::endl;*/
800 // Check the single input item
801 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
804 CraftOutput CraftDefinitionFuel::getOutput(const CraftInput &input, IGameDef *gamedef) const
806 return CraftOutput("", burntime);
809 CraftInput CraftDefinitionFuel::getInput(const CraftOutput &output, IGameDef *gamedef) const
811 std::vector<std::string> rec;
812 rec.push_back(recipe);
813 return CraftInput(CRAFT_METHOD_COOKING,(int)burntime,craftGetItems(rec,gamedef));
816 void CraftDefinitionFuel::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
817 IGameDef *gamedef) const
819 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
822 CraftHashType CraftDefinitionFuel::getHashType() const
824 if (isGroupRecipeStr(recipe_name))
825 return CRAFT_HASH_TYPE_COUNT;
827 return CRAFT_HASH_TYPE_ITEM_NAMES;
830 u64 CraftDefinitionFuel::getHash(CraftHashType type) const
832 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
833 return getHashForString(recipe_name);
834 } else if (type == CRAFT_HASH_TYPE_COUNT) {
837 //illegal hash type for this CraftDefinition (pre-condition)
843 void CraftDefinitionFuel::initHash(IGameDef *gamedef)
848 recipe_name = craftGetItemName(recipe, gamedef);
850 std::string CraftDefinitionFuel::dump() const
852 std::ostringstream os(std::ios::binary);
853 os << "(fuel, recipe=\"" << recipe
854 << "\", burntime=" << burntime << ")"
855 << ", replacements=" << replacements.dump() << ")";
860 Craft definition manager
863 class CCraftDefManager: public IWritableCraftDefManager
868 m_craft_defs.resize(craft_hash_type_max + 1);
871 virtual ~CCraftDefManager()
876 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
877 std::vector<ItemStack> &output_replacement, bool decrementInput,
878 IGameDef *gamedef) const
883 // If all input items are empty, abort.
884 bool all_empty = true;
885 for (std::vector<ItemStack>::size_type i = 0;
886 i < input.items.size(); i++) {
887 if (!input.items[i].empty()) {
895 std::vector<std::string> input_names;
896 input_names = craftGetItemNames(input.items, gamedef);
897 std::sort(input_names.begin(), input_names.end());
899 // Try hash types with increasing collision rate, and return if found.
900 for (int type = 0; type <= craft_hash_type_max; type++) {
901 u64 hash = getHashForGrid((CraftHashType) type, input_names);
903 /*errorstream << "Checking type " << type << " with hash " << hash << std::endl;*/
905 // We'd like to do "const [...] hash_collisions = m_craft_defs[type][hash];"
906 // but that doesn't compile for some reason. This does.
907 std::map<u64, std::vector<CraftDefinition*> >::const_iterator
908 col_iter = (m_craft_defs[type]).find(hash);
910 if (col_iter == (m_craft_defs[type]).end())
913 const std::vector<CraftDefinition*> &hash_collisions = col_iter->second;
914 // Walk crafting definitions from back to front, so that later
915 // definitions can override earlier ones.
916 for (std::vector<CraftDefinition*>::size_type
917 i = hash_collisions.size(); i > 0; i--) {
918 CraftDefinition *def = hash_collisions[i - 1];
920 /*errorstream << "Checking " << input.dump() << std::endl
921 << " against " << def->dump() << std::endl;*/
923 if (def->check(input, gamedef)) {
924 // Get output, then decrement input (if requested)
925 output = def->getOutput(input, gamedef);
927 def->decrementInput(input, output_replacement, gamedef);
928 /*errorstream << "Check RETURNS TRUE" << std::endl;*/
936 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
937 IGameDef *gamedef, unsigned limit=0) const
939 std::vector<CraftDefinition*> recipes;
941 std::map<std::string, std::vector<CraftDefinition*> >::const_iterator
942 vec_iter = m_output_craft_definitions.find(output.item);
944 if (vec_iter == m_output_craft_definitions.end())
947 const std::vector<CraftDefinition*> &vec = vec_iter->second;
949 recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size());
951 for (std::vector<CraftDefinition*>::size_type i = vec.size();
953 CraftDefinition *def = vec[i - 1];
954 if (limit && recipes.size() >= limit)
956 recipes.push_back(def);
961 virtual std::string dump() const
963 std::ostringstream os(std::ios::binary);
964 os << "Crafting definitions:\n";
965 for (int type = 0; type <= craft_hash_type_max; type++) {
966 for (std::map<u64, std::vector<CraftDefinition*> >::const_iterator
967 it = (m_craft_defs[type]).begin();
968 it != (m_craft_defs[type]).end(); it++) {
969 for (std::vector<CraftDefinition*>::size_type i = 0;
970 i < it->second.size(); i++) {
971 os << "type " << type
972 << " hash " << it->first
973 << " def " << it->second[i]->dump()
980 virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef)
982 verbosestream << "registerCraft: registering craft definition: "
983 << def->dump() << std::endl;
984 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].push_back(def);
987 std::string output_name = craftGetItemName(
988 def->getOutput(input, gamedef).item, gamedef);
989 m_output_craft_definitions[output_name].push_back(def);
993 for (int type = 0; type <= craft_hash_type_max; type++) {
994 for (std::map<u64, std::vector<CraftDefinition*> >::iterator
995 it = m_craft_defs[type].begin();
996 it != m_craft_defs[type].end(); it++) {
997 for (std::vector<CraftDefinition*>::iterator
998 iit = it->second.begin();
999 iit != it->second.end(); ++iit) {
1004 m_craft_defs[type].clear();
1006 m_output_craft_definitions.clear();
1008 virtual void initHashes(IGameDef *gamedef)
1010 // Move the CraftDefs from the unhashed layer into layers higher up.
1011 std::vector<CraftDefinition *> &unhashed =
1012 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
1013 for (std::vector<CraftDefinition*>::size_type i = 0;
1014 i < unhashed.size(); i++) {
1015 CraftDefinition *def = unhashed[i];
1017 // Initialize and get the definition's hash
1018 def->initHash(gamedef);
1019 CraftHashType type = def->getHashType();
1020 u64 hash = def->getHash(type);
1022 // Enter the definition
1023 m_craft_defs[type][hash].push_back(def);
1028 //TODO: change both maps to unordered_map when c++11 can be used
1029 std::vector<std::map<u64, std::vector<CraftDefinition*> > > m_craft_defs;
1030 std::map<std::string, std::vector<CraftDefinition*> > m_output_craft_definitions;
1033 IWritableCraftDefManager* createCraftDefManager()
1035 return new CCraftDefManager();