summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFoghrye4 <foghrye4@gmail.com>2016-04-25 18:50:10 +0300
committerest31 <MTest31@outlook.com>2016-07-05 21:40:13 +0200
commit281e9f39fdffd106e79122ae23e2b5443a5daea3 (patch)
tree38456eaaed63d4d4bd9fea72438e9dd64d9d5706 /src
parent7a532056e29d00006382caad9ccde66e54ca8aa5 (diff)
downloadminetest-281e9f39fdffd106e79122ae23e2b5443a5daea3.tar.gz
minetest-281e9f39fdffd106e79122ae23e2b5443a5daea3.tar.bz2
minetest-281e9f39fdffd106e79122ae23e2b5443a5daea3.zip
Adding minetest.clear_craft
Modifications by est31: grammar fixes in doc + error messages and a little style fix, no functional change.
Diffstat (limited to 'src')
-rw-r--r--src/craftdef.cpp90
-rw-r--r--src/craftdef.h4
-rw-r--r--src/script/lua_api/l_craft.cpp76
-rw-r--r--src/script/lua_api/l_craft.h1
4 files changed, 170 insertions, 1 deletions
diff --git a/src/craftdef.cpp b/src/craftdef.cpp
index d3f1edaf9..45d3018a7 100644
--- a/src/craftdef.cpp
+++ b/src/craftdef.cpp
@@ -960,6 +960,96 @@ public:
return recipes;
}
+
+ virtual bool clearCraftRecipesByOutput(const 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;
+ // Recipes are not yet hashed at this point
+ std::vector<CraftDefinition*> &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
+ std::vector<CraftDefinition*> new_vec_by_input;
+ /* We will preallocate necessary memory addresses, so we don't need to reallocate them later.
+ This would save us some performance. */
+ new_vec_by_input.reserve(unhashed_inputs_vec.size());
+ 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));
+ // Recipes are not yet hashed at this point
+ std::vector<CraftDefinition*> &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
+ 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];
+ /* If the input doesn't match the recipe definition, this recipe definition later
+ will be added back in source map. */
+ if (!def->check(input, gamedef)) {
+ new_vec_by_input.push_back(def);
+ continue;
+ }
+ CraftOutput output = def->getOutput(input, gamedef);
+ 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;
+ /* We will preallocate necessary memory addresses, so we don't need
+ to reallocate them later. This would save us some performance. */
+ new_vec_by_output.reserve(vec.size());
+ for (std::vector<CraftDefinition*>::iterator i = vec.begin();
+ i != vec.end(); ++i) {
+ /* If pointers from map by input and output are not same,
+ we will add 'CraftDefinition*' to a new vector. */
+ if (def != *i) {
+ /* Adding dereferenced iterator value (which are
+ 'CraftDefinition' reference) to a new vector. */
+ new_vec_by_output.push_back(*i);
+ }
+ }
+ // Swaps assigned to current key value with new vector for output map.
+ m_output_craft_definitions[output.item].swap(new_vec_by_output);
+ }
+ if (got_hit)
+ // Swaps value with new vector for input map.
+ m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input);
+
+ 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..695ee0c2c 100644
--- a/src/craftdef.h
+++ b/src/craftdef.h
@@ -426,6 +426,10 @@ public:
virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
IGameDef *gamedef, unsigned limit=0) const=0;
+ virtual bool clearCraftRecipesByOutput(const 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;
diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp
index 391a0133d..d135c689f 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<std::string> &recipe)
@@ -281,6 +280,80 @@ 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 craft recipe known 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 has to 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 has to 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 +504,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);