Exemple #1
0
// get_all_craft_recipes(result item)
int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
{
    NO_MAP_LOCK_REQUIRED;

    std::string o_item = luaL_checkstring(L,1);
    IGameDef *gdef = getServer(L);
    ICraftDefManager *cdef = gdef->cdef();
    CraftInput input;
    CraftOutput output(o_item,0);
    std::vector<CraftDefinition*> recipes_list;
    recipes_list = cdef->getCraftRecipes(output, gdef);
    if (recipes_list.empty()) {
        lua_pushnil(L);
        return 1;
    }

    lua_createtable(L, recipes_list.size(), 0);
    std::vector<CraftDefinition*>::const_iterator iter = recipes_list.begin();
    for (u16 i = 0; iter != recipes_list.end(); iter++) {
        CraftOutput tmpout;
        tmpout.item = "";
        tmpout.time = 0;
        tmpout = (*iter)->getOutput(input, gdef);
        std::string query = tmpout.item;
        char *fmtpos, *fmt = &query[0];
        if (strtok_r(fmt, " ", &fmtpos) == output.item) {
            input = (*iter)->getInput(output, gdef);
            lua_newtable(L);
            lua_newtable(L); // items
            std::vector<ItemStack>::const_iterator iter = input.items.begin();
            for (u16 j = 0; iter != input.items.end(); iter++) {
                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);
            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");
            lua_rawseti(L, -2, ++i);
        }
    }
    return 1;
}
Exemple #2
0
// get_craft_recipe(result item)
int ModApiCraft::l_get_craft_recipe(lua_State *L)
{
	NO_MAP_LOCK_REQUIRED;

	int k = 1;
	int input_i = 1;
	std::string o_item = luaL_checkstring(L,input_i);

	IGameDef *gdef = getServer(L);
	ICraftDefManager *cdef = gdef->cdef();
	CraftInput input;
	CraftOutput output(o_item,0);
	bool got = cdef->getCraftRecipe(input, output, gdef);
	lua_newtable(L); // output table
	if(got){
		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");
	} else {
		lua_pushnil(L);
		lua_setfield(L, -2, "items");
		setintfield(L, -1, "width", 0);
	}
	return 1;
}
Exemple #3
0
// get_craft_result(input)
int ModApiCraft::l_get_craft_result(lua_State *L)
{
	NO_MAP_LOCK_REQUIRED;

	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);
	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");
	std::vector<ItemStack> items = read_items(L, -1,getServer(L));
	lua_pop(L, 1); // items

	IGameDef *gdef = getServer(L);
	ICraftDefManager *cdef = gdef->cdef();
	CraftInput input(method, width, items);
	CraftOutput output;
	std::vector<ItemStack> output_replacements;
	bool got = cdef->getCraftResult(input, output, output_replacements, true, gdef);
	lua_newtable(L); // output table
	if (got) {
		ItemStack item;
		item.deSerialize(output.item, gdef->idef());
		LuaItemStack::create(L, item);
		lua_setfield(L, -2, "item");
		setintfield(L, -1, "time", output.time);
		push_items(L, output_replacements);
		lua_setfield(L, -2, "replacements");
	} else {
		LuaItemStack::create(L, ItemStack());
		lua_setfield(L, -2, "item");
		setintfield(L, -1, "time", 0);
		lua_newtable(L);
		lua_setfield(L, -2, "replacements");
	}
	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;
}
Exemple #4
0
// get_node_def(nodename)
int ModApiClient::l_get_node_def(lua_State *L)
{
	IGameDef *gdef = getGameDef(L);
	assert(gdef);

	INodeDefManager *ndef = gdef->ndef();
	assert(ndef);

	if (!lua_isstring(L, 1))
		return 0;

	const std::string &name = lua_tostring(L, 1);
	const ContentFeatures &cf = ndef->get(ndef->getId(name));
	if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
		return 0;

	push_content_features(L, cf);

	return 1;
}
Exemple #5
0
// get_item_def(itemstring)
int ModApiClient::l_get_item_def(lua_State *L)
{
	IGameDef *gdef = getGameDef(L);
	assert(gdef);

	IItemDefManager *idef = gdef->idef();
	assert(idef);

	if (!lua_isstring(L, 1))
		return 0;

	const std::string &name(lua_tostring(L, 1));
	if (!idef->isKnown(name))
		return 0;
	const ItemDefinition &def = idef->get(name);

	push_item_definition_full(L, def);

	return 1;
}
Exemple #6
0
// get_item_def(itemstring)
int ModApiClient::l_get_item_def(lua_State *L)
{
	IGameDef *gdef = getGameDef(L);
	assert(gdef);

	IItemDefManager *idef = gdef->idef();
	assert(idef);

	if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_ITEMDEFS))
		return 0;

	if (!lua_isstring(L, 1))
		return 0;

	const std::string &name(lua_tostring(L, 1));
	if (!idef->isKnown(name))
		return 0;
	const ItemDefinition &def = idef->get(name);

	push_item_definition_full(L, def);

	return 1;
}
Exemple #7
0
// get_all_craft_recipes(result item)
int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
{
	NO_MAP_LOCK_REQUIRED;

	std::string o_item = luaL_checkstring(L,1);
	IGameDef *gdef = getServer(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);
		}
	}
	return 1;
}