From 0c91a0d59db70f3f502004d4c37fecd4e10c9401 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Thu, 26 Jul 2012 13:49:13 +0300 Subject: [PATCH] Working group-shapeless and multigroup recipes --- doc/lua_api.txt | 10 +++- src/craftdef.cpp | 120 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 100 insertions(+), 30 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 35b89021d..d47281b2d 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -423,7 +423,7 @@ effective towards. Groups in crafting recipes --------------------------- -An example: +An example: Make meat soup from any meat, any water and any bowl { output = 'food:meat_soup_raw', recipe = { @@ -431,7 +431,13 @@ An example: {'group:water'}, {'group:bowl'}, }, - preserve = {'group:bowl'}, -- Not implemented yet (TODO) + -- preserve = {'group:bowl'}, -- Not implemented yet (TODO) +} +An another example: Make red wool from white wool and red dye +{ + type = 'shapeless', + output = 'wool:red', + recipe = {'wool:white', 'group:dye,basecolor_red'}, } Special groups diff --git a/src/craftdef.cpp b/src/craftdef.cpp index ab78e7560..b15443607 100644 --- a/src/craftdef.cpp +++ b/src/craftdef.cpp @@ -23,9 +23,11 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "log.h" #include #include +#include #include "gamedef.h" #include "inventory.h" #include "util/serialize.h" +#include "strfnd.h" // Check if input matches recipe // Takes recipe groups into account @@ -38,9 +40,17 @@ static bool inputItemMatchesRecipe(const std::string &inp_name, // Group if(rec_name.substr(0,6) == "group:" && idef->isKnown(inp_name)){ - std::string rec_group = rec_name.substr(6); const struct ItemDefinition &def = idef->get(inp_name); - if(itemgroup_get(def.groups, rec_group) != 0) + Strfnd f(rec_name.substr(6)); + bool all_groups_match = true; + do{ + std::string check_group = f.next(","); + if(itemgroup_get(def.groups, check_group) == 0){ + all_groups_match = false; + break; + } + }while(!f.atend()); + if(all_groups_match) return true; } @@ -140,6 +150,8 @@ static bool craftGetBounds(const std::vector &items, unsigned int w return success; } +#if 0 +// This became useless when group support was added to shapeless recipes // Convert a list of item names to a multiset static std::multiset craftMakeMultiset(const std::vector &names) { @@ -153,6 +165,7 @@ static std::multiset craftMakeMultiset(const std::vector input_filtered; + for(std::vector::const_iterator + i = input.items.begin(); + i != input.items.end(); i++) + { + if(i->name != "") + input_filtered.push_back(i->name); + } - // Get input item multiset - std::vector inp_names = craftGetItemNames(input.items, gamedef); - std::multiset inp_names_multiset = craftMakeMultiset(inp_names); + // If there is a wrong number of items in input, no match + if(input_filtered.size() != recipe.size()){ + /*dstream<<"Number of input items ("<idef())){ + all_match = false; + break; + } + } + //dstream<<" -> match="< inp_names = craftGetItemNames(input.items, gamedef); - std::multiset inp_names_multiset = craftMakeMultiset(inp_names); - - // Get recipe item multiset - std::multiset rec_names_multiset; - rec_names_multiset.insert(craftGetItemName(recipe, gamedef)); + // Filter empty items out of input + std::vector input_filtered; + for(std::vector::const_iterator + i = input.items.begin(); + i != input.items.end(); i++) + { + if(i->name != "") + input_filtered.push_back(i->name); + } - // Recipe is matched when the multisets coincide - return inp_names_multiset == rec_names_multiset; + // If there is a wrong number of items in input, no match + if(input_filtered.size() != 1){ + /*dstream<<"Number of input items ("<name); + } - // Recipe is matched when the multisets coincide - return inp_names_multiset == rec_names_multiset; + // If there is a wrong number of items in input, no match + if(input_filtered.size() != 1){ + /*dstream<<"Number of input items ("<