Merge pull request #123 from gsemaj/items

Add simple item stacking logic
This commit is contained in:
CPunch 2020-09-30 18:37:31 -05:00 committed by GitHub
commit 8ff97ec0b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 7 deletions

View File

@ -98,10 +98,38 @@ void ItemManager::itemMoveHandler(CNSocket* sock, CNPacketData* data) {
resp.ToSlotItem = *toItem; resp.ToSlotItem = *toItem;
resp.FromSlotItem = *fromItem; resp.FromSlotItem = *fromItem;
// swap items in session // swap/stack items in session
Item* itemDat = getItemData(toItem->iID, toItem->iType);
if (itemDat->stackSize > 1 && itemDat == getItemData(fromItem->iID, fromItem->iType) && fromItem->iOpt < itemDat->stackSize && toItem->iOpt < itemDat->stackSize) {
// items are stackable, identical, and not maxed, so run stacking logic
toItem->iOpt += fromItem->iOpt; // sum counts
fromItem->iOpt = 0; // deplete from item
if (toItem->iOpt > itemDat->stackSize) {
// handle overflow
fromItem->iOpt += (toItem->iOpt - itemDat->stackSize); // add overflow to fromItem
toItem->iOpt = itemDat->stackSize; // set toItem count to max
}
if (fromItem->iOpt == 0) { // from item count depleted
// delete item
fromItem->iID = 0;
fromItem->iType = 0;
fromItem->iTimeLimit = 0;
}
resp.iFromSlotNum = itemmove->iFromSlotNum;
resp.iToSlotNum = itemmove->iToSlotNum;
resp.FromSlotItem = *fromItem;
resp.ToSlotItem = *toItem;
} else {
// items not stackable; just swap them
sItemBase temp = *toItem; sItemBase temp = *toItem;
*toItem = *fromItem; *toItem = *fromItem;
*fromItem = temp; *fromItem = temp;
resp.iFromSlotNum = itemmove->iToSlotNum;
resp.iToSlotNum = itemmove->iFromSlotNum;
}
// send equip change to viewable players // send equip change to viewable players
if (itemmove->eFrom == (int)SlotType::EQUIP || itemmove->eTo == (int)SlotType::EQUIP) { if (itemmove->eFrom == (int)SlotType::EQUIP || itemmove->eTo == (int)SlotType::EQUIP) {
@ -130,9 +158,8 @@ void ItemManager::itemMoveHandler(CNSocket* sock, CNPacketData* data) {
// send response // send response
resp.eTo = itemmove->eFrom; resp.eTo = itemmove->eFrom;
resp.iToSlotNum = itemmove->iFromSlotNum;
resp.eFrom = itemmove->eTo; resp.eFrom = itemmove->eTo;
resp.iFromSlotNum = itemmove->iToSlotNum;
sock->sendPacket((void*)&resp, P_FE2CL_PC_ITEM_MOVE_SUCC, sizeof(sP_FE2CL_PC_ITEM_MOVE_SUCC)); sock->sendPacket((void*)&resp, P_FE2CL_PC_ITEM_MOVE_SUCC, sizeof(sP_FE2CL_PC_ITEM_MOVE_SUCC));
} }

View File

@ -193,7 +193,7 @@ void NPCManager::npcVendorBuy(CNSocket* sock, CNPacketData* data) {
return; return;
} }
int itemCost = item->sellPrice * (item->stackSize > 1 ? req->Item.iOpt : 1); int itemCost = item->buyPrice * (item->stackSize > 1 ? req->Item.iOpt : 1);
int slot = ItemManager::findFreeSlot(plr); int slot = ItemManager::findFreeSlot(plr);
if (itemCost > plr->money || slot == -1) { if (itemCost > plr->money || slot == -1) {
// NOTE: VENDOR_ITEM_BUY_FAIL is not actually handled client-side. // NOTE: VENDOR_ITEM_BUY_FAIL is not actually handled client-side.