Shawak
Intermediate OT User
Hello, I've a litle problem and i don't know how to solve it.
Always when i add 21 or more items to a player, and the have 2 bps (bp in bp) the function don't add the last (the 1 item) to the next backpack, it drops the item on the map.
Here is my source code:
Please help me.
Regards,
Shawak
Always when i add 21 or more items to a player, and the have 2 bps (bp in bp) the function don't add the last (the 1 item) to the next backpack, it drops the item on the map.
Here is my source code:
Lua:
int LuaScriptInterface::luaDoPlayerAddItem(lua_State *L)
{
//doPlayerAddItem(cid, itemid, <optional> count/subtype, <optional: default: 1> canDropOnMap)
int32_t parameters = lua_gettop(L);
bool canDropOnMap = true;
if(parameters > 3){
canDropOnMap = (popNumber(L) == 1);
}
uint32_t count = 0;
if(parameters > 2){
count = popNumber(L);
}
uint32_t itemId = (uint32_t)popNumber(L);
uint32_t cid = popNumber(L);
ScriptEnviroment* env = getScriptEnv();
Player* player = env->getPlayerByUID(cid);
if(!player){
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
lua_pushnumber(L, LUA_ERROR);
return 1;
}
const ItemType& it = Item::items[itemId];
if(it.stackable && count > 100){
int32_t subCount = count;
while(subCount > 0){
int32_t stackCount = std::min((int32_t)100, (int32_t)subCount);
Item* newItem = Item::CreateItem(itemId, stackCount);
if(!newItem){
reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
lua_pushnumber(L, LUA_ERROR);
return 1;
}
ReturnValue ret = g_game.internalPlayerAddItem(player, newItem, canDropOnMap);
if(ret != RET_NOERROR){
delete newItem;
reportErrorFunc("Could not add item");
lua_pushnumber(L, LUA_ERROR);
return 1;
}
subCount = subCount - stackCount;
if(subCount == 0){
if(newItem->getParent()){
uint32_t uid = env->addThing((Thing*)newItem);
lua_pushnumber(L, uid);
return 1;
}
else{
//stackable item stacked with existing object, newItem will be released
lua_pushnumber(L, LUA_NULL);
return 1;
}
}
}
}
else{
Item* newItem = Item::CreateItem(itemId, count);
if(!newItem){
reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
lua_pushnumber(L, LUA_ERROR);
return 1;
}
ReturnValue ret = g_game.internalPlayerAddItem(player, newItem, canDropOnMap);
if(ret != RET_NOERROR){
delete newItem;
reportErrorFunc("Could not add item");
lua_pushnumber(L, LUA_ERROR);
return 1;
}
if(newItem->getParent()){
uint32_t uid = env->addThing((Thing*)newItem);
lua_pushnumber(L, uid);
return 1;
}
else{
//stackable item stacked with existing object, newItem will be released
lua_pushnumber(L, LUA_NULL);
return 1;
}
}
return 0;
}
Please help me.
Regards,
Shawak