2013-02-23 19:06:57 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
#include "lua_api/l_craft.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "lua_api/l_internal.h"
|
|
|
|
#include "lua_api/l_item.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "common/c_converter.h"
|
|
|
|
#include "common/c_content.h"
|
|
|
|
#include "server.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "craftdef.h"
|
2013-02-23 19:06:57 +01:00
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
struct EnumString ModApiCraft::es_CraftMethod[] =
|
2013-02-23 19:06:57 +01:00
|
|
|
{
|
|
|
|
{CRAFT_METHOD_NORMAL, "normal"},
|
|
|
|
{CRAFT_METHOD_COOKING, "cooking"},
|
|
|
|
{CRAFT_METHOD_FUEL, "fuel"},
|
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
// helper for register_craft
|
2013-05-25 00:51:02 +02:00
|
|
|
bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index,
|
2013-02-23 19:06:57 +01:00
|
|
|
int &width, std::vector<std::string> &recipe)
|
|
|
|
{
|
|
|
|
if(index < 0)
|
|
|
|
index = lua_gettop(L) + 1 + index;
|
|
|
|
|
|
|
|
if(!lua_istable(L, index))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
int rowcount = 0;
|
|
|
|
while(lua_next(L, index) != 0){
|
|
|
|
int colcount = 0;
|
|
|
|
// key at index -2 and value at index -1
|
|
|
|
if(!lua_istable(L, -1))
|
|
|
|
return false;
|
|
|
|
int table2 = lua_gettop(L);
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(lua_next(L, table2) != 0){
|
|
|
|
// key at index -2 and value at index -1
|
|
|
|
if(!lua_isstring(L, -1))
|
|
|
|
return false;
|
2018-06-30 17:11:38 +02:00
|
|
|
recipe.emplace_back(readParam<std::string>(L, -1));
|
2013-02-23 19:06:57 +01:00
|
|
|
// removes value, keeps key for next iteration
|
|
|
|
lua_pop(L, 1);
|
|
|
|
colcount++;
|
|
|
|
}
|
|
|
|
if(rowcount == 0){
|
|
|
|
width = colcount;
|
|
|
|
} else {
|
|
|
|
if(colcount != width)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// removes value, keeps key for next iteration
|
|
|
|
lua_pop(L, 1);
|
|
|
|
rowcount++;
|
|
|
|
}
|
|
|
|
return width != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper for register_craft
|
2013-05-25 00:51:02 +02:00
|
|
|
bool ModApiCraft::readCraftRecipeShapeless(lua_State *L, int index,
|
2013-02-23 19:06:57 +01:00
|
|
|
std::vector<std::string> &recipe)
|
|
|
|
{
|
|
|
|
if(index < 0)
|
|
|
|
index = lua_gettop(L) + 1 + index;
|
|
|
|
|
|
|
|
if(!lua_istable(L, index))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(lua_next(L, index) != 0){
|
|
|
|
// key at index -2 and value at index -1
|
|
|
|
if(!lua_isstring(L, -1))
|
|
|
|
return false;
|
2018-06-30 17:11:38 +02:00
|
|
|
recipe.emplace_back(readParam<std::string>(L, -1));
|
2013-02-23 19:06:57 +01:00
|
|
|
// removes value, keeps key for next iteration
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper for register_craft
|
2013-05-25 00:51:02 +02:00
|
|
|
bool ModApiCraft::readCraftReplacements(lua_State *L, int index,
|
2013-02-23 19:06:57 +01:00
|
|
|
CraftReplacements &replacements)
|
|
|
|
{
|
|
|
|
if(index < 0)
|
|
|
|
index = lua_gettop(L) + 1 + index;
|
|
|
|
|
|
|
|
if(!lua_istable(L, index))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(lua_next(L, index) != 0){
|
|
|
|
// key at index -2 and value at index -1
|
|
|
|
if(!lua_istable(L, -1))
|
|
|
|
return false;
|
|
|
|
lua_rawgeti(L, -1, 1);
|
|
|
|
if(!lua_isstring(L, -1))
|
|
|
|
return false;
|
2018-06-30 17:11:38 +02:00
|
|
|
std::string replace_from = readParam<std::string>(L, -1);
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_rawgeti(L, -1, 2);
|
|
|
|
if(!lua_isstring(L, -1))
|
|
|
|
return false;
|
2018-06-30 17:11:38 +02:00
|
|
|
std::string replace_to = readParam<std::string>(L, -1);
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_pop(L, 1);
|
2017-08-19 22:23:47 +02:00
|
|
|
replacements.pairs.emplace_back(replace_from, replace_to);
|
2013-02-23 19:06:57 +01:00
|
|
|
// removes value, keeps key for next iteration
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// register_craft({output=item, recipe={{item00,item10},{item01,item11}})
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiCraft::l_register_craft(lua_State *L)
|
2013-02-23 19:06:57 +01:00
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
NO_MAP_LOCK_REQUIRED;
|
2013-02-23 19:06:57 +01:00
|
|
|
//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 =
|
2013-05-25 00:51:02 +02:00
|
|
|
getServer(L)->getWritableCraftDefManager();
|
2013-02-23 19:06:57 +01:00
|
|
|
|
|
|
|
std::string type = getstringfield_default(L, table, "type", "shaped");
|
|
|
|
|
|
|
|
/*
|
|
|
|
CraftDefinitionShaped
|
|
|
|
*/
|
|
|
|
if(type == "shaped"){
|
|
|
|
std::string output = getstringfield_default(L, table, "output", "");
|
2017-08-19 22:23:47 +02:00
|
|
|
if (output.empty())
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Crafting definition is missing an output");
|
2013-02-23 19:06:57 +01:00
|
|
|
|
|
|
|
int width = 0;
|
|
|
|
std::vector<std::string> recipe;
|
|
|
|
lua_getfield(L, table, "recipe");
|
|
|
|
if(lua_isnil(L, -1))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Crafting definition is missing a recipe"
|
2013-02-23 19:06:57 +01:00
|
|
|
" (output=\"" + output + "\")");
|
2013-05-25 00:51:02 +02:00
|
|
|
if(!readCraftRecipeShaped(L, -1, width, recipe))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Invalid crafting recipe"
|
2013-02-23 19:06:57 +01:00
|
|
|
" (output=\"" + output + "\")");
|
|
|
|
|
|
|
|
CraftReplacements replacements;
|
|
|
|
lua_getfield(L, table, "replacements");
|
|
|
|
if(!lua_isnil(L, -1))
|
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
if(!readCraftReplacements(L, -1, replacements))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Invalid replacements"
|
2013-02-23 19:06:57 +01:00
|
|
|
" (output=\"" + output + "\")");
|
|
|
|
}
|
|
|
|
|
|
|
|
CraftDefinition *def = new CraftDefinitionShaped(
|
|
|
|
output, width, recipe, replacements);
|
2015-01-26 14:46:55 +01:00
|
|
|
craftdef->registerCraft(def, getServer(L));
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
CraftDefinitionShapeless
|
|
|
|
*/
|
|
|
|
else if(type == "shapeless"){
|
|
|
|
std::string output = getstringfield_default(L, table, "output", "");
|
2017-08-19 22:23:47 +02:00
|
|
|
if (output.empty())
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Crafting definition (shapeless)"
|
2013-02-23 19:06:57 +01:00
|
|
|
" is missing an output");
|
|
|
|
|
|
|
|
std::vector<std::string> recipe;
|
|
|
|
lua_getfield(L, table, "recipe");
|
|
|
|
if(lua_isnil(L, -1))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Crafting definition (shapeless)"
|
2013-02-23 19:06:57 +01:00
|
|
|
" is missing a recipe"
|
|
|
|
" (output=\"" + output + "\")");
|
2013-05-25 00:51:02 +02:00
|
|
|
if(!readCraftRecipeShapeless(L, -1, recipe))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Invalid crafting recipe"
|
2013-02-23 19:06:57 +01:00
|
|
|
" (output=\"" + output + "\")");
|
|
|
|
|
|
|
|
CraftReplacements replacements;
|
|
|
|
lua_getfield(L, table, "replacements");
|
|
|
|
if(!lua_isnil(L, -1))
|
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
if(!readCraftReplacements(L, -1, replacements))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Invalid replacements"
|
2013-02-23 19:06:57 +01:00
|
|
|
" (output=\"" + output + "\")");
|
|
|
|
}
|
|
|
|
|
|
|
|
CraftDefinition *def = new CraftDefinitionShapeless(
|
|
|
|
output, recipe, replacements);
|
2015-01-26 14:46:55 +01:00
|
|
|
craftdef->registerCraft(def, getServer(L));
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
CraftDefinitionToolRepair
|
|
|
|
*/
|
|
|
|
else if(type == "toolrepair"){
|
|
|
|
float additional_wear = getfloatfield_default(L, table,
|
|
|
|
"additional_wear", 0.0);
|
|
|
|
|
|
|
|
CraftDefinition *def = new CraftDefinitionToolRepair(
|
|
|
|
additional_wear);
|
2015-01-26 14:46:55 +01:00
|
|
|
craftdef->registerCraft(def, getServer(L));
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
CraftDefinitionCooking
|
|
|
|
*/
|
|
|
|
else if(type == "cooking"){
|
|
|
|
std::string output = getstringfield_default(L, table, "output", "");
|
2017-08-19 22:23:47 +02:00
|
|
|
if (output.empty())
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Crafting definition (cooking)"
|
2013-02-23 19:06:57 +01:00
|
|
|
" is missing an output");
|
|
|
|
|
|
|
|
std::string recipe = getstringfield_default(L, table, "recipe", "");
|
2017-08-19 22:23:47 +02:00
|
|
|
if (recipe.empty())
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Crafting definition (cooking)"
|
2013-02-23 19:06:57 +01:00
|
|
|
" 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))
|
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
if(!readCraftReplacements(L, -1, replacements))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Invalid replacements"
|
2013-02-23 19:06:57 +01:00
|
|
|
" (cooking output=\"" + output + "\")");
|
|
|
|
}
|
|
|
|
|
|
|
|
CraftDefinition *def = new CraftDefinitionCooking(
|
|
|
|
output, recipe, cooktime, replacements);
|
2015-01-26 14:46:55 +01:00
|
|
|
craftdef->registerCraft(def, getServer(L));
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
CraftDefinitionFuel
|
|
|
|
*/
|
|
|
|
else if(type == "fuel"){
|
|
|
|
std::string recipe = getstringfield_default(L, table, "recipe", "");
|
2017-08-19 22:23:47 +02:00
|
|
|
if (recipe.empty())
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Crafting definition (fuel)"
|
2013-02-23 19:06:57 +01:00
|
|
|
" 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))
|
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
if(!readCraftReplacements(L, -1, replacements))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Invalid replacements"
|
2013-02-23 19:06:57 +01:00
|
|
|
" (fuel recipe=\"" + recipe + "\")");
|
|
|
|
}
|
|
|
|
|
|
|
|
CraftDefinition *def = new CraftDefinitionFuel(
|
|
|
|
recipe, burntime, replacements);
|
2015-01-26 14:46:55 +01:00
|
|
|
craftdef->registerCraft(def, getServer(L));
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Unknown crafting definition type: \"" + type + "\"");
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return 0; /* number of results */
|
|
|
|
}
|
|
|
|
|
2016-04-25 17:50:10 +02:00
|
|
|
// 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);
|
2017-08-19 22:23:47 +02:00
|
|
|
if (!output.empty()) {
|
2019-08-10 23:28:00 +02:00
|
|
|
if (craftdef->clearCraftsByOutput(c_output, getServer(L))) {
|
2018-10-24 16:03:21 +02:00
|
|
|
lua_pushboolean(L, true);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-19 22:23:47 +02:00
|
|
|
|
2018-10-24 16:03:21 +02:00
|
|
|
warningstream << "No craft recipe known for output" << std::endl;
|
|
|
|
lua_pushboolean(L, false);
|
|
|
|
return 1;
|
2016-04-25 17:50:10 +02:00
|
|
|
}
|
|
|
|
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", "");
|
2017-08-19 22:23:47 +02:00
|
|
|
if (rec.empty())
|
2016-04-25 17:50:10 +02:00
|
|
|
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", "");
|
2017-08-19 22:23:47 +02:00
|
|
|
if (rec.empty())
|
2016-04-25 17:50:10 +02:00
|
|
|
throw LuaError("Crafting definition (fuel)"
|
|
|
|
" is missing a recipe");
|
|
|
|
recipe.push_back(rec);
|
|
|
|
} else {
|
|
|
|
throw LuaError("Unknown crafting definition type: \"" + type + "\"");
|
|
|
|
}
|
2018-10-24 16:03:21 +02:00
|
|
|
|
2019-08-10 23:28:00 +02:00
|
|
|
std::vector<ItemStack> items;
|
|
|
|
items.reserve(recipe.size());
|
|
|
|
for (const auto &item : recipe)
|
|
|
|
items.emplace_back(item, 1, 0, getServer(L)->idef());
|
|
|
|
CraftInput input(method, width, items);
|
|
|
|
|
|
|
|
if (!craftdef->clearCraftsByInput(input, getServer(L))) {
|
2018-10-24 16:03:21 +02:00
|
|
|
warningstream << "No craft recipe matches input" << std::endl;
|
|
|
|
lua_pushboolean(L, false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushboolean(L, true);
|
|
|
|
return 1;
|
2016-04-25 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 19:06:57 +01:00
|
|
|
// get_craft_result(input)
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiCraft::l_get_craft_result(lua_State *L)
|
2013-02-23 19:06:57 +01:00
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
NO_MAP_LOCK_REQUIRED;
|
2022-04-09 14:47:59 +02:00
|
|
|
IGameDef *gdef = getGameDef(L);
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2022-04-09 14:47:59 +02:00
|
|
|
const int input_i = 1;
|
2013-02-23 19:06:57 +01:00
|
|
|
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);
|
|
|
|
int width = 1;
|
|
|
|
lua_getfield(L, input_i, "width");
|
|
|
|
if(lua_isnumber(L, -1))
|
|
|
|
width = luaL_checkinteger(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_getfield(L, input_i, "items");
|
2022-04-09 14:47:59 +02:00
|
|
|
std::vector<ItemStack> items = read_items(L, -1, gdef);
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_pop(L, 1); // items
|
|
|
|
|
|
|
|
ICraftDefManager *cdef = gdef->cdef();
|
|
|
|
CraftInput input(method, width, items);
|
|
|
|
CraftOutput output;
|
2015-06-02 20:30:04 +02:00
|
|
|
std::vector<ItemStack> output_replacements;
|
|
|
|
bool got = cdef->getCraftResult(input, output, output_replacements, true, gdef);
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_newtable(L); // output table
|
2015-06-02 20:30:04 +02:00
|
|
|
if (got) {
|
2013-02-23 19:06:57 +01:00
|
|
|
ItemStack item;
|
|
|
|
item.deSerialize(output.item, gdef->idef());
|
|
|
|
LuaItemStack::create(L, item);
|
|
|
|
lua_setfield(L, -2, "item");
|
|
|
|
setintfield(L, -1, "time", output.time);
|
2015-06-02 20:30:04 +02:00
|
|
|
push_items(L, output_replacements);
|
|
|
|
lua_setfield(L, -2, "replacements");
|
2013-02-23 19:06:57 +01:00
|
|
|
} else {
|
|
|
|
LuaItemStack::create(L, ItemStack());
|
|
|
|
lua_setfield(L, -2, "item");
|
|
|
|
setintfield(L, -1, "time", 0);
|
2015-06-02 20:30:04 +02:00
|
|
|
lua_newtable(L);
|
|
|
|
lua_setfield(L, -2, "replacements");
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
lua_newtable(L); // decremented input table
|
|
|
|
lua_pushstring(L, method_s.c_str());
|
|
|
|
lua_setfield(L, -2, "method");
|
|
|
|
lua_pushinteger(L, width);
|
|
|
|
lua_setfield(L, -2, "width");
|
|
|
|
push_items(L, input.items);
|
|
|
|
lua_setfield(L, -2, "items");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2015-01-26 14:46:55 +01:00
|
|
|
|
2015-07-25 07:54:56 +02:00
|
|
|
static void push_craft_recipe(lua_State *L, IGameDef *gdef,
|
2015-01-26 14:46:55 +01:00
|
|
|
const CraftDefinition *recipe,
|
|
|
|
const CraftOutput &tmpout)
|
|
|
|
{
|
|
|
|
CraftInput input = recipe->getInput(tmpout, gdef);
|
|
|
|
CraftOutput output = recipe->getOutput(input, gdef);
|
|
|
|
|
|
|
|
lua_newtable(L); // items
|
|
|
|
std::vector<ItemStack>::const_iterator iter = input.items.begin();
|
2017-04-25 10:17:53 +02:00
|
|
|
for (u16 j = 1; iter != input.items.end(); ++iter, j++) {
|
2015-01-26 14:46:55 +01:00
|
|
|
if (iter->empty())
|
|
|
|
continue;
|
|
|
|
lua_pushstring(L, iter->name.c_str());
|
|
|
|
lua_rawseti(L, -2, j);
|
|
|
|
}
|
|
|
|
lua_setfield(L, -2, "items");
|
|
|
|
setintfield(L, -1, "width", input.width);
|
2016-07-30 12:50:22 +02:00
|
|
|
|
|
|
|
std::string method_s;
|
2015-01-26 14:46:55 +01:00
|
|
|
switch (input.method) {
|
|
|
|
case CRAFT_METHOD_NORMAL:
|
2016-07-30 12:50:22 +02:00
|
|
|
method_s = "normal";
|
2015-01-26 14:46:55 +01:00
|
|
|
break;
|
|
|
|
case CRAFT_METHOD_COOKING:
|
2016-07-30 12:50:22 +02:00
|
|
|
method_s = "cooking";
|
2015-01-26 14:46:55 +01:00
|
|
|
break;
|
|
|
|
case CRAFT_METHOD_FUEL:
|
2016-07-30 12:50:22 +02:00
|
|
|
method_s = "fuel";
|
2015-01-26 14:46:55 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-07-30 12:50:22 +02:00
|
|
|
method_s = "unknown";
|
2015-01-26 14:46:55 +01:00
|
|
|
}
|
2016-07-30 12:50:22 +02:00
|
|
|
lua_pushstring(L, method_s.c_str());
|
|
|
|
lua_setfield(L, -2, "method");
|
|
|
|
|
|
|
|
// Deprecated, only for compatibility's sake
|
|
|
|
lua_pushstring(L, method_s.c_str());
|
2015-01-26 14:46:55 +01:00
|
|
|
lua_setfield(L, -2, "type");
|
2016-07-30 12:50:22 +02:00
|
|
|
|
2015-07-25 07:54:56 +02:00
|
|
|
lua_pushstring(L, output.item.c_str());
|
2015-01-26 14:46:55 +01:00
|
|
|
lua_setfield(L, -2, "output");
|
|
|
|
}
|
|
|
|
|
2015-07-25 07:54:56 +02:00
|
|
|
static void push_craft_recipes(lua_State *L, IGameDef *gdef,
|
2015-01-26 14:46:55 +01:00
|
|
|
const std::vector<CraftDefinition*> &recipes,
|
|
|
|
const CraftOutput &output)
|
|
|
|
{
|
|
|
|
if (recipes.empty()) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-09 14:47:59 +02:00
|
|
|
lua_createtable(L, recipes.size(), 0);
|
|
|
|
|
2015-01-26 14:46:55 +01:00
|
|
|
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);
|
|
|
|
lua_rawseti(L, -2, ++i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-23 19:06:57 +01:00
|
|
|
// get_craft_recipe(result item)
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiCraft::l_get_craft_recipe(lua_State *L)
|
2013-02-23 19:06:57 +01:00
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
|
2015-01-26 14:46:55 +01:00
|
|
|
std::string item = luaL_checkstring(L, 1);
|
2022-04-09 14:47:59 +02:00
|
|
|
IGameDef *gdef = getGameDef(L);
|
2015-01-26 14:46:55 +01:00
|
|
|
CraftOutput output(item, 0);
|
2022-04-09 14:47:59 +02:00
|
|
|
auto recipes = gdef->cdef()->getCraftRecipes(output, gdef, 1);
|
2013-02-23 19:06:57 +01:00
|
|
|
|
2015-03-23 02:38:53 +01:00
|
|
|
lua_createtable(L, 1, 0);
|
|
|
|
|
2015-01-26 14:46:55 +01:00
|
|
|
if (recipes.empty()) {
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_pushnil(L);
|
|
|
|
lua_setfield(L, -2, "items");
|
|
|
|
setintfield(L, -1, "width", 0);
|
2015-01-26 14:46:55 +01:00
|
|
|
return 1;
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
2022-04-09 14:47:59 +02:00
|
|
|
push_craft_recipe(L, gdef, recipes[0], output);
|
2013-02-23 19:06:57 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2013-03-08 00:22:54 +01:00
|
|
|
|
|
|
|
// get_all_craft_recipes(result item)
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
|
2013-03-08 00:22:54 +01:00
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
|
2015-01-26 14:46:55 +01:00
|
|
|
std::string item = luaL_checkstring(L, 1);
|
2022-04-09 14:47:59 +02:00
|
|
|
IGameDef *gdef = getGameDef(L);
|
2015-01-26 14:46:55 +01:00
|
|
|
CraftOutput output(item, 0);
|
2022-04-09 14:47:59 +02:00
|
|
|
auto recipes = gdef->cdef()->getCraftRecipes(output, gdef);
|
2013-11-30 18:11:07 +01:00
|
|
|
|
2022-04-09 14:47:59 +02:00
|
|
|
push_craft_recipes(L, gdef, recipes, output);
|
2013-03-08 00:22:54 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
void ModApiCraft::Initialize(lua_State *L, int top)
|
|
|
|
{
|
|
|
|
API_FCT(get_all_craft_recipes);
|
|
|
|
API_FCT(get_craft_recipe);
|
|
|
|
API_FCT(get_craft_result);
|
|
|
|
API_FCT(register_craft);
|
2016-04-25 17:50:10 +02:00
|
|
|
API_FCT(clear_craft);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
2022-05-02 20:55:04 +02:00
|
|
|
|
|
|
|
void ModApiCraft::InitializeAsync(lua_State *L, int top)
|
|
|
|
{
|
|
|
|
// all read-only functions
|
|
|
|
API_FCT(get_all_craft_recipes);
|
|
|
|
API_FCT(get_craft_recipe);
|
|
|
|
API_FCT(get_craft_result);
|
|
|
|
}
|