From 70b50ab7301421e17b35160660e574724662fd37 Mon Sep 17 00:00:00 2001 From: Foghrye4 Date: Sun, 24 Apr 2016 23:23:48 +0300 Subject: [PATCH] New LuaAPI function 'minetest.clear_craft' --- doc/lua_api.txt | 17 ++++++-- src/craftdef.cpp | 75 ++++++++++++++++++++++++++++++++ src/craftdef.h | 3 ++ src/script/lua_api/l_craft.cpp | 78 +++++++++++++++++++++++++++++++++- src/script/lua_api/l_craft.h | 1 + 5 files changed, 170 insertions(+), 4 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index f277dff5f..3bb75ba04 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1816,6 +1816,14 @@ Call these functions only at load time! * `minetest.register_craftitem(name, item definition)` * `minetest.register_alias(name, convert_to)` * `minetest.register_craft(recipe)` + * Check recipe table syntax for different types below. +* `minetest.clear_craft(recipe)` + * Will erase existing craft based either on output item or on input recipe. + * Use a same recipe table syntax as for `minetest.register_craft(recipe)`, + but specify either output or input only. If you specify both, input will be ignored. + * If there is no erase candidate founded, Lua exception will be thrown. + * Warning! Type field ("shaped","cooking" or any other) will be ignored if recipe + contain output. Will be erased all possible crafting recipe independent from crafting method. * `minetest.register_ore(ore definition)` * `minetest.register_decoration(decoration definition)` * `minetest.override_item(name, redefinition)` @@ -1921,7 +1929,8 @@ Call these functions only at load time! * Called when any node metadata inventory receive item. * 'pos' - {'x','y','z'} - absolute position of affected node with metadata. * `minetest.register_on_nodemeta_inventory_change_item(func(pos, list_name, slot, old_item, new_item))` - * Called when any node metadata inventory change it content in slot number 'slot' from 'old_item' ItemStack to 'new_item' ItemStack. + * Called when any node metadata inventory change it content in slot number 'slot' from 'old_item' + ItemStack to 'new_item' ItemStack. * 'pos' - {'x','y','z'} - absolute position of affected node with metadata. * `minetest.register_on_nodemeta_inventory_remove_item(func(pos, list_name, stack))` * Called when any node metadata inventory loose item. @@ -1930,7 +1939,8 @@ Call these functions only at load time! * Called when any detached inventory receive item. * 'name' - String, name of detached inventory. * `minetest.register_on_detached_inventory_change_item(func(name, list_name, slot, old_item, new_item))` - * Called when any detached inventory change it content in slot number 'slot' from 'old_item' ItemStack to 'new_item' ItemStack. + * Called when any detached inventory change it content in slot number 'slot' from 'old_item' + ItemStack to 'new_item' ItemStack. * 'name' - String, name of detached inventory. * `minetest.register_on_detached_inventory_remove_item(func(name, list_name, stack))` * Called when any detached inventory loose item. @@ -1938,7 +1948,8 @@ Call these functions only at load time! * `minetest.register_on_player_inventory_add_item(func(player, list_name, slot, stack))` * Called when any player receive item. * `minetest.register_on_player_inventory_change_item(func(player, list_name, slot, old_item, new_item))` - * Called when any players' inventory change it content in slot number 'slot' from 'old_item' ItemStack to 'new_item' ItemStack. + * Called when any players' inventory change it content in slot number 'slot' from 'old_item' + ItemStack to 'new_item' ItemStack. * `minetest.register_on_player_inventory_remove_item(func(player, list_name, stack))` * Called when any player loose item. diff --git a/src/craftdef.cpp b/src/craftdef.cpp index d3f1edaf9..d65285ee5 100644 --- a/src/craftdef.cpp +++ b/src/craftdef.cpp @@ -960,6 +960,81 @@ public: return recipes; } + + virtual bool clearCraftRecipesByOutput(CraftOutput &output, IGameDef *gamedef) + { + std::map >::iterator vec_iter = m_output_craft_definitions.find(output.item); + + if (vec_iter == m_output_craft_definitions.end()) + return false; + + std::vector &vec = vec_iter->second; + for (std::vector::iterator i = vec.begin(); + i != vec.end(); ++i) { + CraftDefinition *def = *i; + std::vector &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]; // Recipes are not yet hashed at this point + std::vector new_vec_by_input; + for (std::vector::iterator i2 = unhashed_inputs_vec.begin(); + i2 != unhashed_inputs_vec.end(); ++i2) { + if(def != *i2) + { + new_vec_by_input.push_back(*i2); + } + } + m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input); + } + m_output_craft_definitions.erase(output.item); + return true; + } + + virtual bool clearCraftRecipesByInput(CraftMethod craft_method, unsigned int craft_grid_width, const std::vector &recipe, IGameDef *gamedef) + { + bool all_empty = true; + for (std::vector::size_type i = 0; + i < recipe.size(); i++) { + if (!recipe[i].empty()) { + all_empty = false; + break; + } + } + if (all_empty) + return false; + + CraftInput input(craft_method, craft_grid_width, craftGetItems(recipe, gamedef)); + + std::vector &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]; // Recipes are not yet hashed at this point + std::vector new_vec_by_input; + bool got_hit = false; + for (std::vector::size_type + i = unhashed_inputs_vec.size(); i > 0; i--) { + CraftDefinition *def = unhashed_inputs_vec[i - 1]; + CraftOutput output = def->getOutput(input, gamedef); // What first argument for? + //If check are not passed, skip 'CraftDefinition' and add element to a new vector. + if (!def->check(input, gamedef)){ + new_vec_by_input.push_back(def); + continue; + } + got_hit = true; + std::map >::iterator + vec_iter = m_output_craft_definitions.find(output.item); + if (vec_iter == m_output_craft_definitions.end()) + continue; + std::vector &vec = vec_iter->second; + std::vector new_vec_by_output; + for (std::vector::iterator i = vec.begin(); + i != vec.end(); ++i) { + if(def != *i){ // If pointers from map by input and output are not same, we will add 'CraftDefinition*' to a new vector. + new_vec_by_output.push_back(*i); // Adding dereferenced iterator value (which are 'CraftDefinition' reference) to a new vector. + } + } + m_output_craft_definitions[output.item].swap(new_vec_by_output); // Swaps assigned to current key value with new vector for output map. + } + if(got_hit) + m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input); // Swaps value with new vector for input map. + + return got_hit; + } + virtual std::string dump() const { std::ostringstream os(std::ios::binary); diff --git a/src/craftdef.h b/src/craftdef.h index cebb2d7ae..aeb1ff950 100644 --- a/src/craftdef.h +++ b/src/craftdef.h @@ -426,6 +426,9 @@ public: virtual std::vector getCraftRecipes(CraftOutput &output, IGameDef *gamedef, unsigned limit=0) const=0; + virtual bool clearCraftRecipesByOutput(CraftOutput &output, IGameDef *gamedef) = 0; + virtual bool clearCraftRecipesByInput(CraftMethod craft_method, unsigned int craft_grid_width, const std::vector &recipe, IGameDef *gamedef) = 0; + // Print crafting recipes for debugging virtual std::string dump() const=0; diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp index 391a0133d..59089aa88 100644 --- a/src/script/lua_api/l_craft.cpp +++ b/src/script/lua_api/l_craft.cpp @@ -34,7 +34,6 @@ struct EnumString ModApiCraft::es_CraftMethod[] = {0, NULL}, }; - // helper for register_craft bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index, int &width, std::vector &recipe) @@ -281,6 +280,82 @@ int ModApiCraft::l_register_craft(lua_State *L) return 0; /* number of results */ } +// clear_craft({[output=item], [recipe={{item00,item10},{item01,item11}}]) +int ModApiCraft::l_clear_craft(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + luaL_checktype(L, 1, LUA_TTABLE); + int table = 1; + + // Get the writable craft definition manager from the server + IWritableCraftDefManager *craftdef = + getServer(L)->getWritableCraftDefManager(); + + std::string output = getstringfield_default(L, table, "output", ""); + std::string type = getstringfield_default(L, table, "type", "shaped"); + CraftOutput c_output(output, 0); + if(output != ""){ + if(craftdef->clearCraftRecipesByOutput(c_output, getServer(L))) + return 0; + else + throw LuaError("No crafting specified for output" + " (output=\"" + output + "\")"); + } + std::vector recipe; + int width =0; + CraftMethod method = CRAFT_METHOD_NORMAL; + /* + CraftDefinitionShaped + */ + if(type == "shaped"){ + lua_getfield(L, table, "recipe"); + if(lua_isnil(L, -1)) + throw LuaError("Either output or recipe should be defined"); + if(!readCraftRecipeShaped(L, -1, width, recipe)) + throw LuaError("Invalid crafting recipe"); + } + /* + CraftDefinitionShapeless + */ + else if(type == "shapeless"){ + lua_getfield(L, table, "recipe"); + if(lua_isnil(L, -1)) + throw LuaError("Either output or recipe should be defined"); + if(!readCraftRecipeShapeless(L, -1, recipe)) + throw LuaError("Invalid crafting recipe"); + } + /* + CraftDefinitionCooking + */ + else if(type == "cooking"){ + method = CRAFT_METHOD_COOKING; + std::string rec = getstringfield_default(L, table, "recipe", ""); + if(rec == "") + throw LuaError("Crafting definition (cooking)" + " is missing a recipe"); + recipe.push_back(rec); + } + /* + CraftDefinitionFuel + */ + else if(type == "fuel"){ + method = CRAFT_METHOD_FUEL; + std::string rec = getstringfield_default(L, table, "recipe", ""); + if(rec == "") + throw LuaError("Crafting definition (fuel)" + " is missing a recipe"); + recipe.push_back(rec); + } + else + { + throw LuaError("Unknown crafting definition type: \"" + type + "\""); + } + if(!craftdef->clearCraftRecipesByInput(method, width, recipe, getServer(L))) + throw LuaError("No crafting specified for input"); + lua_pop(L, 1); + return 0; +} + // get_craft_result(input) int ModApiCraft::l_get_craft_result(lua_State *L) { @@ -431,4 +506,5 @@ void ModApiCraft::Initialize(lua_State *L, int top) API_FCT(get_craft_recipe); API_FCT(get_craft_result); API_FCT(register_craft); + API_FCT(clear_craft); } diff --git a/src/script/lua_api/l_craft.h b/src/script/lua_api/l_craft.h index 548608776..eb2bce706 100644 --- a/src/script/lua_api/l_craft.h +++ b/src/script/lua_api/l_craft.h @@ -33,6 +33,7 @@ private: static int l_get_craft_recipe(lua_State *L); static int l_get_all_craft_recipes(lua_State *L); static int l_get_craft_result(lua_State *L); + static int l_clear_craft(lua_State *L); static bool readCraftReplacements(lua_State *L, int index, CraftReplacements &replacements);