Move l_get_all_craft_recipes to scriptapi_craft
authorsapier <Sapier at GMX dot net>
Thu, 7 Mar 2013 23:22:54 +0000 (23:22 +0000)
committerkwolekr <kwolekr@minetest.net>
Mon, 11 Mar 2013 23:27:13 +0000 (19:27 -0400)
src/scriptapi.cpp
src/scriptapi_craft.cpp
src/scriptapi_craft.h

index 04af4eb2257d978daf927d57ff05ab6ca993411b..f95f5a29de908a48170e7cd9e119c90408c3403d 100644 (file)
@@ -1000,79 +1000,6 @@ static int l_notify_authentication_modified(lua_State *L)
        return 0;
 }
 
-// get_craft_recipes(result item)
-static int l_get_all_craft_recipes(lua_State *L)
-{
-       char tmp[20];
-       int input_i = 1;
-       std::string o_item = luaL_checkstring(L,input_i);
-       IGameDef *gdef = get_server(L);
-       ICraftDefManager *cdef = gdef->cdef();
-       CraftInput input;
-       CraftOutput output(o_item,0);
-       std::vector<CraftDefinition*> recipes_list = cdef->getCraftRecipes(output, gdef);
-       if (recipes_list.empty())
-       {
-               lua_pushnil(L);
-               return 1;
-       }
-       // Get the table insert function
-       lua_getglobal(L, "table");
-       lua_getfield(L, -1, "insert");
-       int table_insert = lua_gettop(L);
-       lua_newtable(L);
-       int table = lua_gettop(L);
-       for(std::vector<CraftDefinition*>::const_iterator
-               i = recipes_list.begin();
-               i != recipes_list.end(); i++)
-       {
-               CraftOutput tmpout;
-               tmpout.item = "";
-               tmpout.time = 0;
-               CraftDefinition *def = *i;
-               tmpout = def->getOutput(input, gdef);
-               if(tmpout.item.substr(0,output.item.length()) == output.item)
-               {
-                       input = def->getInput(output, gdef);
-                       lua_pushvalue(L, table_insert);
-                       lua_pushvalue(L, table);
-                       lua_newtable(L);
-                       int k = 0;
-                       lua_newtable(L);
-                       for(std::vector<ItemStack>::const_iterator
-                               i = input.items.begin();
-                               i != input.items.end(); i++, k++)
-                       {
-                               if (i->empty()) continue;
-                               sprintf(tmp,"%d",k);
-                               lua_pushstring(L,tmp);
-                               lua_pushstring(L,i->name.c_str());
-                               lua_settable(L, -3);
-                       }
-                       lua_setfield(L, -2, "items");
-                       setintfield(L, -1, "width", input.width);
-                       switch (input.method)
-                               {
-                               case CRAFT_METHOD_NORMAL:
-                                       lua_pushstring(L,"normal");
-                                       break;
-                               case CRAFT_METHOD_COOKING:
-                                       lua_pushstring(L,"cooking");
-                                       break;
-                               case CRAFT_METHOD_FUEL:
-                                       lua_pushstring(L,"fuel");
-                                       break;
-                               default:
-                                       lua_pushstring(L,"unknown");
-                               }
-                       lua_setfield(L, -2, "type");
-                       if(lua_pcall(L, 2, 0, 0))
-                       script_error(L, "error: %s", lua_tostring(L, -1));
-               }
-       }
-       return 1;
-}
-
 // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
 static int l_rollback_get_last_node_actor(lua_State *L)
 {
index 617bb86a30beb50ef56813fa046abb5b49f7a0f6..183eeb84055759ed1f4c534728632df688088c8d 100644 (file)
@@ -379,3 +379,76 @@ int l_get_craft_recipe(lua_State *L)
        }
        return 1;
 }
+
+// get_all_craft_recipes(result item)
+int l_get_all_craft_recipes(lua_State *L)
+{
+       char tmp[20];
+       int input_i = 1;
+       std::string o_item = luaL_checkstring(L,input_i);
+       IGameDef *gdef = get_server(L);
+       ICraftDefManager *cdef = gdef->cdef();
+       CraftInput input;
+       CraftOutput output(o_item,0);
+       std::vector<CraftDefinition*> recipes_list = cdef->getCraftRecipes(output, gdef);
+       if (recipes_list.empty())
+       {
+               lua_pushnil(L);
+               return 1;
+       }
+       // Get the table insert function
+       lua_getglobal(L, "table");
+       lua_getfield(L, -1, "insert");
+       int table_insert = lua_gettop(L);
+       lua_newtable(L);
+       int table = lua_gettop(L);
+       for(std::vector<CraftDefinition*>::const_iterator
+               i = recipes_list.begin();
+               i != recipes_list.end(); i++)
+       {
+               CraftOutput tmpout;
+               tmpout.item = "";
+               tmpout.time = 0;
+               CraftDefinition *def = *i;
+               tmpout = def->getOutput(input, gdef);
+               if(tmpout.item.substr(0,output.item.length()) == output.item)
+               {
+                       input = def->getInput(output, gdef);
+                       lua_pushvalue(L, table_insert);
+                       lua_pushvalue(L, table);
+                       lua_newtable(L);
+                       int k = 0;
+                       lua_newtable(L);
+                       for(std::vector<ItemStack>::const_iterator
+                               i = input.items.begin();
+                               i != input.items.end(); i++, k++)
+                       {
+                               if (i->empty()) continue;
+                               sprintf(tmp,"%d",k);
+                               lua_pushstring(L,tmp);
+                               lua_pushstring(L,i->name.c_str());
+                               lua_settable(L, -3);
+                       }
+                       lua_setfield(L, -2, "items");
+                       setintfield(L, -1, "width", input.width);
+                       switch (input.method)
+                               {
+                               case CRAFT_METHOD_NORMAL:
+                                       lua_pushstring(L,"normal");
+                                       break;
+                               case CRAFT_METHOD_COOKING:
+                                       lua_pushstring(L,"cooking");
+                                       break;
+                               case CRAFT_METHOD_FUEL:
+                                       lua_pushstring(L,"fuel");
+                                       break;
+                               default:
+                                       lua_pushstring(L,"unknown");
+                               }
+                       lua_setfield(L, -2, "type");
+                       if(lua_pcall(L, 2, 0, 0))
+                       script_error(L, "error: %s", lua_tostring(L, -1));
+               }
+       }
+       return 1;
+}
index 2e209d4aef62369183a9bc047bbfbb52e1e50618..f28989fd96b501e67500495e18ed5ca91f027ebc 100644 (file)
@@ -33,6 +33,7 @@ extern "C" {
 /*****************************************************************************/
 int l_register_craft(lua_State *L);
 int l_get_craft_recipe(lua_State *L);
+int l_get_all_craft_recipes(lua_State *L);
 int l_get_craft_result(lua_State *L);
 
 /*****************************************************************************/