diff options
Diffstat (limited to 'src/script/lua_api/l_craft.cpp')
-rw-r--r-- | src/script/lua_api/l_craft.cpp | 188 |
1 files changed, 97 insertions, 91 deletions
diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp index 07b45eaff..18622ee00 100644 --- a/src/script/lua_api/l_craft.cpp +++ b/src/script/lua_api/l_craft.cpp @@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + #include "lua_api/l_craft.h" #include "lua_api/l_internal.h" #include "lua_api/l_item.h" @@ -25,45 +26,46 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "server.h" #include "craftdef.h" -struct EnumString ModApiCraft::es_CraftMethod[] = { - {CRAFT_METHOD_NORMAL, "normal"}, - {CRAFT_METHOD_COOKING, "cooking"}, - {CRAFT_METHOD_FUEL, "fuel"}, - {0, NULL}, +struct EnumString ModApiCraft::es_CraftMethod[] = +{ + {CRAFT_METHOD_NORMAL, "normal"}, + {CRAFT_METHOD_COOKING, "cooking"}, + {CRAFT_METHOD_FUEL, "fuel"}, + {0, NULL}, }; // helper for register_craft -bool ModApiCraft::readCraftRecipeShaped( - lua_State *L, int index, int &width, std::vector<std::string> &recipe) +bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index, + int &width, std::vector<std::string> &recipe) { - if (index < 0) + if(index < 0) index = lua_gettop(L) + 1 + index; - if (!lua_istable(L, index)) + if(!lua_istable(L, index)) return false; lua_pushnil(L); int rowcount = 0; - while (lua_next(L, index) != 0) { + while(lua_next(L, index) != 0){ int colcount = 0; // key at index -2 and value at index -1 - if (!lua_istable(L, -1)) + if(!lua_istable(L, -1)) return false; int table2 = lua_gettop(L); lua_pushnil(L); - while (lua_next(L, table2) != 0) { + while(lua_next(L, table2) != 0){ // key at index -2 and value at index -1 - if (!lua_isstring(L, -1)) + if(!lua_isstring(L, -1)) return false; recipe.emplace_back(readParam<std::string>(L, -1)); // removes value, keeps key for next iteration lua_pop(L, 1); colcount++; } - if (rowcount == 0) { + if(rowcount == 0){ width = colcount; } else { - if (colcount != width) + if(colcount != width) return false; } // removes value, keeps key for next iteration @@ -74,19 +76,19 @@ bool ModApiCraft::readCraftRecipeShaped( } // helper for register_craft -bool ModApiCraft::readCraftRecipeShapeless( - lua_State *L, int index, std::vector<std::string> &recipe) +bool ModApiCraft::readCraftRecipeShapeless(lua_State *L, int index, + std::vector<std::string> &recipe) { - if (index < 0) + if(index < 0) index = lua_gettop(L) + 1 + index; - if (!lua_istable(L, index)) + if(!lua_istable(L, index)) return false; lua_pushnil(L); - while (lua_next(L, index) != 0) { + while(lua_next(L, index) != 0){ // key at index -2 and value at index -1 - if (!lua_isstring(L, -1)) + if(!lua_isstring(L, -1)) return false; recipe.emplace_back(readParam<std::string>(L, -1)); // removes value, keeps key for next iteration @@ -96,27 +98,27 @@ bool ModApiCraft::readCraftRecipeShapeless( } // helper for register_craft -bool ModApiCraft::readCraftReplacements( - lua_State *L, int index, CraftReplacements &replacements) +bool ModApiCraft::readCraftReplacements(lua_State *L, int index, + CraftReplacements &replacements) { - if (index < 0) + if(index < 0) index = lua_gettop(L) + 1 + index; - if (!lua_istable(L, index)) + if(!lua_istable(L, index)) return false; lua_pushnil(L); - while (lua_next(L, index) != 0) { + while(lua_next(L, index) != 0){ // key at index -2 and value at index -1 - if (!lua_istable(L, -1)) + if(!lua_istable(L, -1)) return false; lua_rawgeti(L, -1, 1); - if (!lua_isstring(L, -1)) + if(!lua_isstring(L, -1)) return false; std::string replace_from = readParam<std::string>(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 2); - if (!lua_isstring(L, -1)) + if(!lua_isstring(L, -1)) return false; std::string replace_to = readParam<std::string>(L, -1); lua_pop(L, 1); @@ -130,19 +132,20 @@ bool ModApiCraft::readCraftReplacements( int ModApiCraft::l_register_craft(lua_State *L) { NO_MAP_LOCK_REQUIRED; - // infostream<<"register_craft"<<std::endl; + //infostream<<"register_craft"<<std::endl; luaL_checktype(L, 1, LUA_TTABLE); int table = 1; // Get the writable craft definition manager from the server - IWritableCraftDefManager *craftdef = getServer(L)->getWritableCraftDefManager(); + IWritableCraftDefManager *craftdef = + getServer(L)->getWritableCraftDefManager(); std::string type = getstringfield_default(L, table, "type", "shaped"); /* CraftDefinitionShaped */ - if (type == "shaped") { + if(type == "shaped"){ std::string output = getstringfield_default(L, table, "output", ""); if (output.empty()) throw LuaError("Crafting definition is missing an output"); @@ -150,22 +153,20 @@ int ModApiCraft::l_register_craft(lua_State *L) int width = 0; std::vector<std::string> recipe; lua_getfield(L, table, "recipe"); - if (lua_isnil(L, -1)) + if(lua_isnil(L, -1)) throw LuaError("Crafting definition is missing a recipe" - " (output=\"" + - output + "\")"); - if (!readCraftRecipeShaped(L, -1, width, recipe)) + " (output=\"" + output + "\")"); + if(!readCraftRecipeShaped(L, -1, width, recipe)) throw LuaError("Invalid crafting recipe" - " (output=\"" + - output + "\")"); + " (output=\"" + output + "\")"); CraftReplacements replacements; lua_getfield(L, table, "replacements"); - if (!lua_isnil(L, -1)) { - if (!readCraftReplacements(L, -1, replacements)) + if(!lua_isnil(L, -1)) + { + if(!readCraftReplacements(L, -1, replacements)) throw LuaError("Invalid replacements" - " (output=\"" + - output + "\")"); + " (output=\"" + output + "\")"); } CraftDefinition *def = new CraftDefinitionShaped( @@ -175,31 +176,29 @@ int ModApiCraft::l_register_craft(lua_State *L) /* CraftDefinitionShapeless */ - else if (type == "shapeless") { + else if(type == "shapeless"){ std::string output = getstringfield_default(L, table, "output", ""); if (output.empty()) throw LuaError("Crafting definition (shapeless)" - " is missing an output"); + " is missing an output"); std::vector<std::string> recipe; lua_getfield(L, table, "recipe"); - if (lua_isnil(L, -1)) + if(lua_isnil(L, -1)) throw LuaError("Crafting definition (shapeless)" - " is missing a recipe" - " (output=\"" + - output + "\")"); - if (!readCraftRecipeShapeless(L, -1, recipe)) + " is missing a recipe" + " (output=\"" + output + "\")"); + if(!readCraftRecipeShapeless(L, -1, recipe)) throw LuaError("Invalid crafting recipe" - " (output=\"" + - output + "\")"); + " (output=\"" + output + "\")"); CraftReplacements replacements; lua_getfield(L, table, "replacements"); - if (!lua_isnil(L, -1)) { - if (!readCraftReplacements(L, -1, replacements)) + if(!lua_isnil(L, -1)) + { + if(!readCraftReplacements(L, -1, replacements)) throw LuaError("Invalid replacements" - " (output=\"" + - output + "\")"); + " (output=\"" + output + "\")"); } CraftDefinition *def = new CraftDefinitionShapeless( @@ -209,38 +208,38 @@ int ModApiCraft::l_register_craft(lua_State *L) /* CraftDefinitionToolRepair */ - else if (type == "toolrepair") { - float additional_wear = - getfloatfield_default(L, table, "additional_wear", 0.0); + else if(type == "toolrepair"){ + float additional_wear = getfloatfield_default(L, table, + "additional_wear", 0.0); - CraftDefinition *def = new CraftDefinitionToolRepair(additional_wear); + CraftDefinition *def = new CraftDefinitionToolRepair( + additional_wear); craftdef->registerCraft(def, getServer(L)); } /* CraftDefinitionCooking */ - else if (type == "cooking") { + else if(type == "cooking"){ std::string output = getstringfield_default(L, table, "output", ""); if (output.empty()) throw LuaError("Crafting definition (cooking)" - " is missing an output"); + " is missing an output"); std::string recipe = getstringfield_default(L, table, "recipe", ""); if (recipe.empty()) throw LuaError("Crafting definition (cooking)" - " is missing a recipe" - " (output=\"" + - output + "\")"); + " is missing a recipe" + " (output=\"" + output + "\")"); float cooktime = getfloatfield_default(L, table, "cooktime", 3.0); CraftReplacements replacements; lua_getfield(L, table, "replacements"); - if (!lua_isnil(L, -1)) { - if (!readCraftReplacements(L, -1, replacements)) + if(!lua_isnil(L, -1)) + { + if(!readCraftReplacements(L, -1, replacements)) throw LuaError("Invalid replacements" - " (cooking output=\"" + - output + "\")"); + " (cooking output=\"" + output + "\")"); } CraftDefinition *def = new CraftDefinitionCooking( @@ -250,27 +249,29 @@ int ModApiCraft::l_register_craft(lua_State *L) /* CraftDefinitionFuel */ - else if (type == "fuel") { + else if(type == "fuel"){ std::string recipe = getstringfield_default(L, table, "recipe", ""); if (recipe.empty()) throw LuaError("Crafting definition (fuel)" - " is missing a recipe"); + " is missing a recipe"); float burntime = getfloatfield_default(L, table, "burntime", 1.0); CraftReplacements replacements; lua_getfield(L, table, "replacements"); - if (!lua_isnil(L, -1)) { - if (!readCraftReplacements(L, -1, replacements)) + if(!lua_isnil(L, -1)) + { + if(!readCraftReplacements(L, -1, replacements)) throw LuaError("Invalid replacements" - " (fuel recipe=\"" + - recipe + "\")"); + " (fuel recipe=\"" + recipe + "\")"); } - CraftDefinition *def = - new CraftDefinitionFuel(recipe, burntime, replacements); + CraftDefinition *def = new CraftDefinitionFuel( + recipe, burntime, replacements); craftdef->registerCraft(def, getServer(L)); - } else { + } + else + { throw LuaError("Unknown crafting definition type: \"" + type + "\""); } @@ -286,7 +287,8 @@ int ModApiCraft::l_clear_craft(lua_State *L) int table = 1; // Get the writable craft definition manager from the server - IWritableCraftDefManager *craftdef = getServer(L)->getWritableCraftDefManager(); + IWritableCraftDefManager *craftdef = + getServer(L)->getWritableCraftDefManager(); std::string output = getstringfield_default(L, table, "output", ""); std::string type = getstringfield_default(L, table, "type", "shaped"); @@ -332,7 +334,7 @@ int ModApiCraft::l_clear_craft(lua_State *L) std::string rec = getstringfield_default(L, table, "recipe", ""); if (rec.empty()) throw LuaError("Crafting definition (cooking)" - " is missing a recipe"); + " is missing a recipe"); recipe.push_back(rec); } /* @@ -343,7 +345,7 @@ int ModApiCraft::l_clear_craft(lua_State *L) std::string rec = getstringfield_default(L, table, "recipe", ""); if (rec.empty()) throw LuaError("Crafting definition (fuel)" - " is missing a recipe"); + " is missing a recipe"); recipe.push_back(rec); } else { throw LuaError("Unknown crafting definition type: \"" + type + "\""); @@ -372,15 +374,15 @@ int ModApiCraft::l_get_craft_result(lua_State *L) int input_i = 1; std::string method_s = getstringfield_default(L, input_i, "method", "normal"); - enum CraftMethod method = (CraftMethod)getenumfield( - L, input_i, "method", es_CraftMethod, CRAFT_METHOD_NORMAL); + enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method", + es_CraftMethod, CRAFT_METHOD_NORMAL); int width = 1; lua_getfield(L, input_i, "width"); - if (lua_isnumber(L, -1)) + if(lua_isnumber(L, -1)) width = luaL_checkinteger(L, -1); lua_pop(L, 1); lua_getfield(L, input_i, "items"); - std::vector<ItemStack> items = read_items(L, -1, getServer(L)); + std::vector<ItemStack> items = read_items(L, -1,getServer(L)); lua_pop(L, 1); // items IGameDef *gdef = getServer(L); @@ -415,7 +417,9 @@ int ModApiCraft::l_get_craft_result(lua_State *L) return 2; } -static void push_craft_recipe(lua_State *L, IGameDef *gdef, const CraftDefinition *recipe, + +static void push_craft_recipe(lua_State *L, IGameDef *gdef, + const CraftDefinition *recipe, const CraftOutput &tmpout) { CraftInput input = recipe->getInput(tmpout, gdef); @@ -458,7 +462,8 @@ static void push_craft_recipe(lua_State *L, IGameDef *gdef, const CraftDefinitio } static void push_craft_recipes(lua_State *L, IGameDef *gdef, - const std::vector<CraftDefinition *> &recipes, const CraftOutput &output) + const std::vector<CraftDefinition*> &recipes, + const CraftOutput &output) { lua_createtable(L, recipes.size(), 0); @@ -467,7 +472,7 @@ static void push_craft_recipes(lua_State *L, IGameDef *gdef, return; } - std::vector<CraftDefinition *>::const_iterator it = recipes.begin(); + std::vector<CraftDefinition*>::const_iterator it = recipes.begin(); for (unsigned i = 0; it != recipes.end(); ++it) { lua_newtable(L); push_craft_recipe(L, gdef, *it, output); @@ -475,6 +480,7 @@ static void push_craft_recipes(lua_State *L, IGameDef *gdef, } } + // get_craft_recipe(result item) int ModApiCraft::l_get_craft_recipe(lua_State *L) { @@ -483,8 +489,8 @@ int ModApiCraft::l_get_craft_recipe(lua_State *L) std::string item = luaL_checkstring(L, 1); Server *server = getServer(L); CraftOutput output(item, 0); - std::vector<CraftDefinition *> recipes = - server->cdef()->getCraftRecipes(output, server, 1); + std::vector<CraftDefinition*> recipes = server->cdef() + ->getCraftRecipes(output, server, 1); lua_createtable(L, 1, 0); @@ -506,8 +512,8 @@ int ModApiCraft::l_get_all_craft_recipes(lua_State *L) std::string item = luaL_checkstring(L, 1); Server *server = getServer(L); CraftOutput output(item, 0); - std::vector<CraftDefinition *> recipes = - server->cdef()->getCraftRecipes(output, server); + std::vector<CraftDefinition*> recipes = server->cdef() + ->getCraftRecipes(output, server); push_craft_recipes(L, server, recipes, output); return 1; |