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);
246 for(std::vector<std::string>::size_type i = 0;
247 i < items.size(); i++, x++) {
254 os << '"' << items[i] << '"';
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>::size_type i = 0;
268 i < items.size(); i++, x++) {
275 os << '"' << (items[i].getItemString()) << '"';
286 std::string CraftInput::dump() const
288 std::ostringstream os(std::ios::binary);
289 os << "(method=" << ((int)method) << ", items="
290 << craftDumpMatrix(items, width) << ")";
298 std::string CraftOutput::dump() const
300 std::ostringstream os(std::ios::binary);
301 os << "(item=\"" << item << "\", time=" << time << ")";
309 std::string CraftReplacements::dump() const
311 std::ostringstream os(std::ios::binary);
313 const char *sep = "";
314 for (std::vector<std::pair<std::string, std::string> >::size_type i = 0;
315 i < pairs.size(); i++) {
316 const std::pair<std::string, std::string> &repl_p = pairs[i];
318 << '"' << (repl_p.first)
319 << "\"=>\"" << (repl_p.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, std::vector<ItemStack> &output_replacements,
412 IGameDef *gamedef) const
414 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
417 CraftHashType CraftDefinitionShaped::getHashType() const
419 assert(hash_inited); // Pre-condition
420 bool has_group = false;
421 for (size_t i = 0; i < recipe_names.size(); i++) {
422 if (isGroupRecipeStr(recipe_names[i])) {
428 return CRAFT_HASH_TYPE_COUNT;
430 return CRAFT_HASH_TYPE_ITEM_NAMES;
433 u64 CraftDefinitionShaped::getHash(CraftHashType type) const
435 assert(hash_inited); // Pre-condition
436 assert((type == CRAFT_HASH_TYPE_ITEM_NAMES)
437 || (type == CRAFT_HASH_TYPE_COUNT)); // Pre-condition
439 std::vector<std::string> rec_names = recipe_names;
440 std::sort(rec_names.begin(), rec_names.end());
441 return getHashForGrid(type, rec_names);
444 void CraftDefinitionShaped::initHash(IGameDef *gamedef)
449 recipe_names = craftGetItemNames(recipe, gamedef);
452 std::string CraftDefinitionShaped::dump() const
454 std::ostringstream os(std::ios::binary);
455 os << "(shaped, output=\"" << output
456 << "\", recipe=" << craftDumpMatrix(recipe, width)
457 << ", replacements=" << replacements.dump() << ")";
462 CraftDefinitionShapeless
465 std::string CraftDefinitionShapeless::getName() const
470 bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef) const
472 if (input.method != CRAFT_METHOD_NORMAL)
475 // Filter empty items out of input
476 std::vector<std::string> input_filtered;
477 for (std::vector<ItemStack>::size_type i = 0;
478 i < input.items.size(); i++) {
479 const ItemStack &item = input.items[i];
481 input_filtered.push_back(item.name);
484 // If there is a wrong number of items in input, no match
485 if (input_filtered.size() != recipe.size()) {
486 /*dstream<<"Number of input items ("<<input_filtered.size()
487 <<") does not match recipe size ("<<recipe.size()<<") "
488 <<"of recipe with output="<<output<<std::endl;*/
492 std::vector<std::string> recipe_copy;
494 recipe_copy = recipe_names;
496 recipe_copy = craftGetItemNames(recipe, gamedef);
497 std::sort(recipe_copy.begin(), recipe_copy.end());
500 // Try with all permutations of the recipe,
501 // start from the lexicographically first permutation (=sorted),
502 // recipe_names is pre-sorted
504 // If all items match, the recipe matches
505 bool all_match = true;
506 //dstream<<"Testing recipe (output="<<output<<"):";
507 for (size_t i=0; i<recipe.size(); i++) {
508 //dstream<<" ("<<input_filtered[i]<<" == "<<recipe_copy[i]<<")";
509 if (!inputItemMatchesRecipe(input_filtered[i], recipe_copy[i],
515 //dstream<<" -> match="<<all_match<<std::endl;
518 } while (std::next_permutation(recipe_copy.begin(), recipe_copy.end()));
523 CraftOutput CraftDefinitionShapeless::getOutput(const CraftInput &input, IGameDef *gamedef) const
525 return CraftOutput(output, 0);
528 CraftInput CraftDefinitionShapeless::getInput(const CraftOutput &output, IGameDef *gamedef) const
530 return CraftInput(CRAFT_METHOD_NORMAL, 0, craftGetItems(recipe, gamedef));
533 void CraftDefinitionShapeless::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
534 IGameDef *gamedef) const
536 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
539 CraftHashType CraftDefinitionShapeless::getHashType() const
541 assert(hash_inited); // Pre-condition
542 bool has_group = false;
543 for (size_t i = 0; i < recipe_names.size(); i++) {
544 if (isGroupRecipeStr(recipe_names[i])) {
550 return CRAFT_HASH_TYPE_COUNT;
552 return CRAFT_HASH_TYPE_ITEM_NAMES;
555 u64 CraftDefinitionShapeless::getHash(CraftHashType type) const
557 assert(hash_inited); // Pre-condition
558 assert(type == CRAFT_HASH_TYPE_ITEM_NAMES
559 || type == CRAFT_HASH_TYPE_COUNT); // Pre-condition
560 return getHashForGrid(type, recipe_names);
563 void CraftDefinitionShapeless::initHash(IGameDef *gamedef)
568 recipe_names = craftGetItemNames(recipe, gamedef);
569 std::sort(recipe_names.begin(), recipe_names.end());
572 std::string CraftDefinitionShapeless::dump() const
574 std::ostringstream os(std::ios::binary);
575 os << "(shapeless, output=\"" << output
576 << "\", recipe=" << craftDumpMatrix(recipe, recipe.size())
577 << ", replacements=" << replacements.dump() << ")";
582 CraftDefinitionToolRepair
585 static ItemStack craftToolRepair(
586 const ItemStack &item1,
587 const ItemStack &item2,
588 float additional_wear,
591 IItemDefManager *idef = gamedef->idef();
592 if (item1.count != 1 || item2.count != 1 || item1.name != item2.name
593 || idef->get(item1.name).type != ITEM_TOOL
594 || idef->get(item2.name).type != ITEM_TOOL) {
599 s32 item1_uses = 65536 - (u32) item1.wear;
600 s32 item2_uses = 65536 - (u32) item2.wear;
601 s32 new_uses = item1_uses + item2_uses;
602 s32 new_wear = 65536 - new_uses + floor(additional_wear * 65536 + 0.5);
603 if (new_wear >= 65536)
608 ItemStack repaired = item1;
609 repaired.wear = new_wear;
613 std::string CraftDefinitionToolRepair::getName() const
618 bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef) const
620 if (input.method != CRAFT_METHOD_NORMAL)
625 for (std::vector<ItemStack>::size_type i = 0;
626 i < input.items.size(); i++) {
627 const ItemStack &item = input.items[i];
631 else if (item2.empty())
637 ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
638 return !repaired.empty();
641 CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameDef *gamedef) const
645 for (std::vector<ItemStack>::size_type i = 0;
646 i < input.items.size(); i++) {
647 const ItemStack &item = input.items[i];
651 else if (item2.empty())
655 ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
656 return CraftOutput(repaired.getItemString(), 0);
659 CraftInput CraftDefinitionToolRepair::getInput(const CraftOutput &output, IGameDef *gamedef) const
661 std::vector<ItemStack> stack;
662 stack.push_back(ItemStack());
663 return CraftInput(CRAFT_METHOD_COOKING, additional_wear, stack);
666 void CraftDefinitionToolRepair::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
667 IGameDef *gamedef) const
669 craftDecrementInput(input, gamedef);
672 std::string CraftDefinitionToolRepair::dump() const
674 std::ostringstream os(std::ios::binary);
675 os << "(toolrepair, additional_wear=" << additional_wear << ")";
680 CraftDefinitionCooking
683 std::string CraftDefinitionCooking::getName() const
688 bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) const
690 if (input.method != CRAFT_METHOD_COOKING)
693 // Filter empty items out of input
694 std::vector<std::string> input_filtered;
695 for (std::vector<ItemStack>::size_type i = 0;
696 i < input.items.size(); i++) {
697 const std::string &name = input.items[i].name;
699 input_filtered.push_back(name);
702 // If there is a wrong number of items in input, no match
703 if (input_filtered.size() != 1) {
704 /*dstream<<"Number of input items ("<<input_filtered.size()
705 <<") does not match recipe size (1) "
706 <<"of cooking recipe with output="<<output<<std::endl;*/
710 // Check the single input item
711 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
714 CraftOutput CraftDefinitionCooking::getOutput(const CraftInput &input, IGameDef *gamedef) const
716 return CraftOutput(output, cooktime);
719 CraftInput CraftDefinitionCooking::getInput(const CraftOutput &output, IGameDef *gamedef) const
721 std::vector<std::string> rec;
722 rec.push_back(recipe);
723 return CraftInput(CRAFT_METHOD_COOKING,cooktime,craftGetItems(rec,gamedef));
726 void CraftDefinitionCooking::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
727 IGameDef *gamedef) const
729 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
732 CraftHashType CraftDefinitionCooking::getHashType() const
734 if (isGroupRecipeStr(recipe_name))
735 return CRAFT_HASH_TYPE_COUNT;
737 return CRAFT_HASH_TYPE_ITEM_NAMES;
740 u64 CraftDefinitionCooking::getHash(CraftHashType type) const
742 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
743 return getHashForString(recipe_name);
744 } else if (type == CRAFT_HASH_TYPE_COUNT) {
747 //illegal hash type for this CraftDefinition (pre-condition)
753 void CraftDefinitionCooking::initHash(IGameDef *gamedef)
758 recipe_name = craftGetItemName(recipe, gamedef);
761 std::string CraftDefinitionCooking::dump() const
763 std::ostringstream os(std::ios::binary);
764 os << "(cooking, output=\"" << output
765 << "\", recipe=\"" << recipe
766 << "\", cooktime=" << cooktime << ")"
767 << ", replacements=" << replacements.dump() << ")";
775 std::string CraftDefinitionFuel::getName() const
780 bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) const
782 if (input.method != CRAFT_METHOD_FUEL)
785 // Filter empty items out of input
786 std::vector<std::string> input_filtered;
787 for (std::vector<ItemStack>::size_type i = 0;
788 i < input.items.size(); i++) {
789 const std::string &name = input.items[i].name;
791 input_filtered.push_back(name);
794 // If there is a wrong number of items in input, no match
795 if (input_filtered.size() != 1) {
796 /*dstream<<"Number of input items ("<<input_filtered.size()
797 <<") does not match recipe size (1) "
798 <<"of fuel recipe with burntime="<<burntime<<std::endl;*/
802 // Check the single input item
803 return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef());
806 CraftOutput CraftDefinitionFuel::getOutput(const CraftInput &input, IGameDef *gamedef) const
808 return CraftOutput("", burntime);
811 CraftInput CraftDefinitionFuel::getInput(const CraftOutput &output, IGameDef *gamedef) const
813 std::vector<std::string> rec;
814 rec.push_back(recipe);
815 return CraftInput(CRAFT_METHOD_COOKING,(int)burntime,craftGetItems(rec,gamedef));
818 void CraftDefinitionFuel::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements,
819 IGameDef *gamedef) const
821 craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
824 CraftHashType CraftDefinitionFuel::getHashType() const
826 if (isGroupRecipeStr(recipe_name))
827 return CRAFT_HASH_TYPE_COUNT;
829 return CRAFT_HASH_TYPE_ITEM_NAMES;
832 u64 CraftDefinitionFuel::getHash(CraftHashType type) const
834 if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
835 return getHashForString(recipe_name);
836 } else if (type == CRAFT_HASH_TYPE_COUNT) {
839 //illegal hash type for this CraftDefinition (pre-condition)
845 void CraftDefinitionFuel::initHash(IGameDef *gamedef)
850 recipe_name = craftGetItemName(recipe, gamedef);
852 std::string CraftDefinitionFuel::dump() const
854 std::ostringstream os(std::ios::binary);
855 os << "(fuel, recipe=\"" << recipe
856 << "\", burntime=" << burntime << ")"
857 << ", replacements=" << replacements.dump() << ")";
862 Craft definition manager
865 class CCraftDefManager: public IWritableCraftDefManager
870 m_craft_defs.resize(craft_hash_type_max + 1);
873 virtual ~CCraftDefManager()
878 virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
879 std::vector<ItemStack> &output_replacement, bool decrementInput,
880 IGameDef *gamedef) const
885 // If all input items are empty, abort.
886 bool all_empty = true;
887 for (std::vector<ItemStack>::size_type i = 0;
888 i < input.items.size(); i++) {
889 if (!input.items[i].empty()) {
897 std::vector<std::string> input_names;
898 input_names = craftGetItemNames(input.items, gamedef);
899 std::sort(input_names.begin(), input_names.end());
901 // Try hash types with increasing collision rate, and return if found.
902 for (int type = 0; type <= craft_hash_type_max; type++) {
903 u64 hash = getHashForGrid((CraftHashType) type, input_names);
905 /*errorstream << "Checking type " << type << " with hash " << hash << std::endl;*/
907 // We'd like to do "const [...] hash_collisions = m_craft_defs[type][hash];"
908 // but that doesn't compile for some reason. This does.
909 std::map<u64, std::vector<CraftDefinition*> >::const_iterator
910 col_iter = (m_craft_defs[type]).find(hash);
912 if (col_iter == (m_craft_defs[type]).end())
915 const std::vector<CraftDefinition*> &hash_collisions = col_iter->second;
916 // Walk crafting definitions from back to front, so that later
917 // definitions can override earlier ones.
918 for (std::vector<CraftDefinition*>::size_type
919 i = hash_collisions.size(); i > 0; i--) {
920 CraftDefinition *def = hash_collisions[i - 1];
922 /*errorstream << "Checking " << input.dump() << std::endl
923 << " against " << def->dump() << std::endl;*/
925 if (def->check(input, gamedef)) {
926 // Get output, then decrement input (if requested)
927 output = def->getOutput(input, gamedef);
929 def->decrementInput(input, output_replacement, gamedef);
930 /*errorstream << "Check RETURNS TRUE" << std::endl;*/
938 virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
939 IGameDef *gamedef, unsigned limit=0) const
941 std::vector<CraftDefinition*> recipes;
943 std::map<std::string, std::vector<CraftDefinition*> >::const_iterator
944 vec_iter = m_output_craft_definitions.find(output.item);
946 if (vec_iter == m_output_craft_definitions.end())
949 const std::vector<CraftDefinition*> &vec = vec_iter->second;
951 recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size());
953 for (std::vector<CraftDefinition*>::size_type i = vec.size();
955 CraftDefinition *def = vec[i - 1];
956 if (limit && recipes.size() >= limit)
958 recipes.push_back(def);
963 virtual std::string dump() const
965 std::ostringstream os(std::ios::binary);
966 os << "Crafting definitions:\n";
967 for (int type = 0; type <= craft_hash_type_max; type++) {
968 for (std::map<u64, std::vector<CraftDefinition*> >::const_iterator
969 it = (m_craft_defs[type]).begin();
970 it != (m_craft_defs[type]).end(); it++) {
971 for (std::vector<CraftDefinition*>::size_type i = 0;
972 i < it->second.size(); i++) {
973 os << "type " << type
974 << " hash " << it->first
975 << " def " << it->second[i]->dump()
982 virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef)
984 verbosestream << "registerCraft: registering craft definition: "
985 << def->dump() << std::endl;
986 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].push_back(def);
989 std::string output_name = craftGetItemName(
990 def->getOutput(input, gamedef).item, gamedef);
991 m_output_craft_definitions[output_name].push_back(def);
995 for (int type = 0; type <= craft_hash_type_max; type++) {
996 for (std::map<u64, std::vector<CraftDefinition*> >::iterator
997 it = m_craft_defs[type].begin();
998 it != m_craft_defs[type].end(); it++) {
999 for (std::vector<CraftDefinition*>::iterator
1000 iit = it->second.begin();
1001 iit != it->second.end(); ++iit) {
1006 m_craft_defs[type].clear();
1008 m_output_craft_definitions.clear();
1010 virtual void initHashes(IGameDef *gamedef)
1012 // Move the CraftDefs from the unhashed layer into layers higher up.
1013 std::vector<CraftDefinition *> &unhashed =
1014 m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
1015 for (std::vector<CraftDefinition*>::size_type i = 0;
1016 i < unhashed.size(); i++) {
1017 CraftDefinition *def = unhashed[i];
1019 // Initialize and get the definition's hash
1020 def->initHash(gamedef);
1021 CraftHashType type = def->getHashType();
1022 u64 hash = def->getHash(type);
1024 // Enter the definition
1025 m_craft_defs[type][hash].push_back(def);
1030 //TODO: change both maps to unordered_map when c++11 can be used
1031 std::vector<std::map<u64, std::vector<CraftDefinition*> > > m_craft_defs;
1032 std::map<std::string, std::vector<CraftDefinition*> > m_output_craft_definitions;
1035 IWritableCraftDefManager* createCraftDefManager()
1037 return new CCraftDefManager();