1
0
mirror of https://github.com/minetest/minetest.git synced 2024-11-15 23:10:28 +01:00

New LuaAPI function 'minetest.clear_craft'

This commit is contained in:
Foghrye4 2016-04-24 23:23:48 +03:00
parent 513d96c928
commit 70b50ab730
5 changed files with 170 additions and 4 deletions

View File

@ -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.

View File

@ -960,6 +960,81 @@ public:
return recipes;
}
virtual bool clearCraftRecipesByOutput(CraftOutput &output, IGameDef *gamedef)
{
std::map<std::string, std::vector<CraftDefinition*> >::iterator vec_iter = m_output_craft_definitions.find(output.item);
if (vec_iter == m_output_craft_definitions.end())
return false;
std::vector<CraftDefinition*> &vec = vec_iter->second;
for (std::vector<CraftDefinition*>::iterator i = vec.begin();
i != vec.end(); ++i) {
CraftDefinition *def = *i;
std::vector<CraftDefinition*> &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]; // Recipes are not yet hashed at this point
std::vector<CraftDefinition*> new_vec_by_input;
for (std::vector<CraftDefinition*>::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<std::string> &recipe, IGameDef *gamedef)
{
bool all_empty = true;
for (std::vector<std::string>::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<CraftDefinition*> &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]; // Recipes are not yet hashed at this point
std::vector<CraftDefinition*> new_vec_by_input;
bool got_hit = false;
for (std::vector<CraftDefinition*>::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<std::string, std::vector<CraftDefinition*> >::iterator
vec_iter = m_output_craft_definitions.find(output.item);
if (vec_iter == m_output_craft_definitions.end())
continue;
std::vector<CraftDefinition*> &vec = vec_iter->second;
std::vector<CraftDefinition*> new_vec_by_output;
for (std::vector<CraftDefinition*>::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);

View File

@ -426,6 +426,9 @@ public:
virtual std::vector<CraftDefinition*> 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<std::string> &recipe, IGameDef *gamedef) = 0;
// Print crafting recipes for debugging
virtual std::string dump() const=0;

View File

@ -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<std::string> &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<std::string> 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);
}

View File

@ -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);