3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "lua_api/l_craft.h"
22 #include "common/c_internal.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
26 #include "lua_api/l_item.h"
32 ModApiCraft::ModApiCraft()
37 struct EnumString ModApiCraft::es_CraftMethod[] =
39 {CRAFT_METHOD_NORMAL, "normal"},
40 {CRAFT_METHOD_COOKING, "cooking"},
41 {CRAFT_METHOD_FUEL, "fuel"},
46 // helper for register_craft
47 bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index,
48 int &width, std::vector<std::string> &recipe)
51 index = lua_gettop(L) + 1 + index;
53 if(!lua_istable(L, index))
58 while(lua_next(L, index) != 0){
60 // key at index -2 and value at index -1
61 if(!lua_istable(L, -1))
63 int table2 = lua_gettop(L);
65 while(lua_next(L, table2) != 0){
66 // key at index -2 and value at index -1
67 if(!lua_isstring(L, -1))
69 recipe.push_back(lua_tostring(L, -1));
70 // removes value, keeps key for next iteration
80 // removes value, keeps key for next iteration
87 // helper for register_craft
88 bool ModApiCraft::readCraftRecipeShapeless(lua_State *L, int index,
89 std::vector<std::string> &recipe)
92 index = lua_gettop(L) + 1 + index;
94 if(!lua_istable(L, index))
98 while(lua_next(L, index) != 0){
99 // key at index -2 and value at index -1
100 if(!lua_isstring(L, -1))
102 recipe.push_back(lua_tostring(L, -1));
103 // removes value, keeps key for next iteration
109 // helper for register_craft
110 bool ModApiCraft::readCraftReplacements(lua_State *L, int index,
111 CraftReplacements &replacements)
114 index = lua_gettop(L) + 1 + index;
116 if(!lua_istable(L, index))
120 while(lua_next(L, index) != 0){
121 // key at index -2 and value at index -1
122 if(!lua_istable(L, -1))
124 lua_rawgeti(L, -1, 1);
125 if(!lua_isstring(L, -1))
127 std::string replace_from = lua_tostring(L, -1);
129 lua_rawgeti(L, -1, 2);
130 if(!lua_isstring(L, -1))
132 std::string replace_to = lua_tostring(L, -1);
134 replacements.pairs.push_back(
135 std::make_pair(replace_from, replace_to));
136 // removes value, keeps key for next iteration
141 // register_craft({output=item, recipe={{item00,item10},{item01,item11}})
142 int ModApiCraft::l_register_craft(lua_State *L)
144 NO_MAP_LOCK_REQUIRED;
145 //infostream<<"register_craft"<<std::endl;
146 luaL_checktype(L, 1, LUA_TTABLE);
149 // Get the writable craft definition manager from the server
150 IWritableCraftDefManager *craftdef =
151 getServer(L)->getWritableCraftDefManager();
153 std::string type = getstringfield_default(L, table, "type", "shaped");
156 CraftDefinitionShaped
158 if(type == "shaped"){
159 std::string output = getstringfield_default(L, table, "output", "");
161 throw LuaError(L, "Crafting definition is missing an output");
164 std::vector<std::string> recipe;
165 lua_getfield(L, table, "recipe");
167 throw LuaError(L, "Crafting definition is missing a recipe"
168 " (output=\"" + output + "\")");
169 if(!readCraftRecipeShaped(L, -1, width, recipe))
170 throw LuaError(L, "Invalid crafting recipe"
171 " (output=\"" + output + "\")");
173 CraftReplacements replacements;
174 lua_getfield(L, table, "replacements");
175 if(!lua_isnil(L, -1))
177 if(!readCraftReplacements(L, -1, replacements))
178 throw LuaError(L, "Invalid replacements"
179 " (output=\"" + output + "\")");
182 CraftDefinition *def = new CraftDefinitionShaped(
183 output, width, recipe, replacements);
184 craftdef->registerCraft(def);
187 CraftDefinitionShapeless
189 else if(type == "shapeless"){
190 std::string output = getstringfield_default(L, table, "output", "");
192 throw LuaError(L, "Crafting definition (shapeless)"
193 " is missing an output");
195 std::vector<std::string> recipe;
196 lua_getfield(L, table, "recipe");
198 throw LuaError(L, "Crafting definition (shapeless)"
199 " is missing a recipe"
200 " (output=\"" + output + "\")");
201 if(!readCraftRecipeShapeless(L, -1, recipe))
202 throw LuaError(L, "Invalid crafting recipe"
203 " (output=\"" + output + "\")");
205 CraftReplacements replacements;
206 lua_getfield(L, table, "replacements");
207 if(!lua_isnil(L, -1))
209 if(!readCraftReplacements(L, -1, replacements))
210 throw LuaError(L, "Invalid replacements"
211 " (output=\"" + output + "\")");
214 CraftDefinition *def = new CraftDefinitionShapeless(
215 output, recipe, replacements);
216 craftdef->registerCraft(def);
219 CraftDefinitionToolRepair
221 else if(type == "toolrepair"){
222 float additional_wear = getfloatfield_default(L, table,
223 "additional_wear", 0.0);
225 CraftDefinition *def = new CraftDefinitionToolRepair(
227 craftdef->registerCraft(def);
230 CraftDefinitionCooking
232 else if(type == "cooking"){
233 std::string output = getstringfield_default(L, table, "output", "");
235 throw LuaError(L, "Crafting definition (cooking)"
236 " is missing an output");
238 std::string recipe = getstringfield_default(L, table, "recipe", "");
240 throw LuaError(L, "Crafting definition (cooking)"
241 " is missing a recipe"
242 " (output=\"" + output + "\")");
244 float cooktime = getfloatfield_default(L, table, "cooktime", 3.0);
246 CraftReplacements replacements;
247 lua_getfield(L, table, "replacements");
248 if(!lua_isnil(L, -1))
250 if(!readCraftReplacements(L, -1, replacements))
251 throw LuaError(L, "Invalid replacements"
252 " (cooking output=\"" + output + "\")");
255 CraftDefinition *def = new CraftDefinitionCooking(
256 output, recipe, cooktime, replacements);
257 craftdef->registerCraft(def);
262 else if(type == "fuel"){
263 std::string recipe = getstringfield_default(L, table, "recipe", "");
265 throw LuaError(L, "Crafting definition (fuel)"
266 " is missing a recipe");
268 float burntime = getfloatfield_default(L, table, "burntime", 1.0);
270 CraftReplacements replacements;
271 lua_getfield(L, table, "replacements");
272 if(!lua_isnil(L, -1))
274 if(!readCraftReplacements(L, -1, replacements))
275 throw LuaError(L, "Invalid replacements"
276 " (fuel recipe=\"" + recipe + "\")");
279 CraftDefinition *def = new CraftDefinitionFuel(
280 recipe, burntime, replacements);
281 craftdef->registerCraft(def);
285 throw LuaError(L, "Unknown crafting definition type: \"" + type + "\"");
289 return 0; /* number of results */
292 // get_craft_result(input)
293 int ModApiCraft::l_get_craft_result(lua_State *L)
295 NO_MAP_LOCK_REQUIRED;
298 std::string method_s = getstringfield_default(L, input_i, "method", "normal");
299 enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method",
300 es_CraftMethod, CRAFT_METHOD_NORMAL);
302 lua_getfield(L, input_i, "width");
303 if(lua_isnumber(L, -1))
304 width = luaL_checkinteger(L, -1);
306 lua_getfield(L, input_i, "items");
307 std::vector<ItemStack> items = read_items(L, -1,getServer(L));
308 lua_pop(L, 1); // items
310 IGameDef *gdef = getServer(L);
311 ICraftDefManager *cdef = gdef->cdef();
312 CraftInput input(method, width, items);
314 bool got = cdef->getCraftResult(input, output, true, gdef);
315 lua_newtable(L); // output table
318 item.deSerialize(output.item, gdef->idef());
319 LuaItemStack::create(L, item);
320 lua_setfield(L, -2, "item");
321 setintfield(L, -1, "time", output.time);
323 LuaItemStack::create(L, ItemStack());
324 lua_setfield(L, -2, "item");
325 setintfield(L, -1, "time", 0);
327 lua_newtable(L); // decremented input table
328 lua_pushstring(L, method_s.c_str());
329 lua_setfield(L, -2, "method");
330 lua_pushinteger(L, width);
331 lua_setfield(L, -2, "width");
332 push_items(L, input.items);
333 lua_setfield(L, -2, "items");
337 // get_craft_recipe(result item)
338 int ModApiCraft::l_get_craft_recipe(lua_State *L)
340 NO_MAP_LOCK_REQUIRED;
344 std::string o_item = luaL_checkstring(L,input_i);
346 IGameDef *gdef = getServer(L);
347 ICraftDefManager *cdef = gdef->cdef();
349 CraftOutput output(o_item,0);
350 bool got = cdef->getCraftRecipe(input, output, gdef);
351 lua_newtable(L); // output table
354 for(std::vector<ItemStack>::const_iterator
355 i = input.items.begin();
356 i != input.items.end(); i++, k++)
362 lua_pushinteger(L,k);
363 lua_pushstring(L,i->name.c_str());
366 lua_setfield(L, -2, "items");
367 setintfield(L, -1, "width", input.width);
368 switch (input.method) {
369 case CRAFT_METHOD_NORMAL:
370 lua_pushstring(L,"normal");
372 case CRAFT_METHOD_COOKING:
373 lua_pushstring(L,"cooking");
375 case CRAFT_METHOD_FUEL:
376 lua_pushstring(L,"fuel");
379 lua_pushstring(L,"unknown");
381 lua_setfield(L, -2, "type");
384 lua_setfield(L, -2, "items");
385 setintfield(L, -1, "width", 0);
390 // get_all_craft_recipes(result item)
391 int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
393 NO_MAP_LOCK_REQUIRED;
395 std::string o_item = luaL_checkstring(L,1);
396 IGameDef *gdef = getServer(L);
397 ICraftDefManager *cdef = gdef->cdef();
399 CraftOutput output(o_item,0);
400 std::vector<CraftDefinition*> recipes_list = cdef->getCraftRecipes(output, gdef);
401 if (recipes_list.empty())
406 // Get the table insert function
407 lua_getglobal(L, "table");
408 lua_getfield(L, -1, "insert");
409 int table_insert = lua_gettop(L);
411 int table = lua_gettop(L);
412 for (std::vector<CraftDefinition*>::const_iterator
413 i = recipes_list.begin();
414 i != recipes_list.end(); i++)
419 CraftDefinition *def = *i;
420 tmpout = def->getOutput(input, gdef);
421 std::string query = tmpout.item;
422 char *fmtpos, *fmt = &query[0];
423 if (strtok_r(fmt, " ", &fmtpos) == output.item)
425 input = def->getInput(output, gdef);
426 lua_pushvalue(L, table_insert);
427 lua_pushvalue(L, table);
431 for(std::vector<ItemStack>::const_iterator
432 i = input.items.begin();
433 i != input.items.end(); i++, k++)
437 lua_pushinteger(L,k);
438 lua_pushstring(L,i->name.c_str());
441 lua_setfield(L, -2, "items");
442 setintfield(L, -1, "width", input.width);
443 switch (input.method) {
444 case CRAFT_METHOD_NORMAL:
445 lua_pushstring(L,"normal");
447 case CRAFT_METHOD_COOKING:
448 lua_pushstring(L,"cooking");
450 case CRAFT_METHOD_FUEL:
451 lua_pushstring(L,"fuel");
454 lua_pushstring(L,"unknown");
456 lua_setfield(L, -2, "type");
457 lua_pushstring(L, &tmpout.item[0]);
458 lua_setfield(L, -2, "output");
459 if (lua_pcall(L, 2, 0, 0))
460 script_error(L, "error: %s", lua_tostring(L, -1));
466 bool ModApiCraft::Initialize(lua_State* L, int top) {
469 retval &= API_FCT(get_all_craft_recipes);
470 retval &= API_FCT(get_craft_recipe);
471 retval &= API_FCT(get_craft_result);
472 retval &= API_FCT(register_craft);
477 ModApiCraft modapicraft_prototype;