]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/scriptapi_craft.cpp
Pre-select current game in world creation dialog
[dragonfireclient.git] / src / scriptapi_craft.cpp
index 617bb86a30beb50ef56813fa046abb5b49f7a0f6..459908237e84c10bcaae43d20f9599ace79426f3 100644 (file)
@@ -330,8 +330,7 @@ int l_get_craft_result(lua_State *L)
 // get_craft_recipe(result item)
 int l_get_craft_recipe(lua_State *L)
 {
-       int k = 0;
-       char tmp[20];
+       int k = 1;
        int input_i = 1;
        std::string o_item = luaL_checkstring(L,input_i);
 
@@ -351,8 +350,7 @@ int l_get_craft_recipe(lua_State *L)
                        {
                                continue;
                        }
-                       sprintf(tmp,"%d",k);
-                       lua_pushstring(L,tmp);
+                       lua_pushinteger(L,k);
                        lua_pushstring(L,i->name.c_str());
                        lua_settable(L, -3);
                }
@@ -379,3 +377,77 @@ 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)
+{
+       std::string o_item = luaL_checkstring(L,1);
+       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);
+               std::string query = tmpout.item;
+               char *fmtpos, *fmt = &query[0];
+               if (strtok_r(fmt, " ", &fmtpos) == output.item)
+               {
+                       input = def->getInput(output, gdef);
+                       lua_pushvalue(L, table_insert);
+                       lua_pushvalue(L, table);
+                       lua_newtable(L);
+                       int k = 1;
+                       lua_newtable(L);
+                       for(std::vector<ItemStack>::const_iterator
+                               i = input.items.begin();
+                               i != input.items.end(); i++, k++)
+                       {
+                               if (i->empty())
+                                       continue;
+                               lua_pushinteger(L, k);
+                               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");
+                       lua_pushstring(L, &tmpout.item[0]);
+                       lua_setfield(L, -2, "output");
+                       if (lua_pcall(L, 2, 0, 0))
+                               script_error(L, "error: %s", lua_tostring(L, -1));
+               }
+       }
+       return 1;
+}