From 1eb806af58a5b36c17ccbeebc6a7fd943d98ed45 Mon Sep 17 00:00:00 2001 From: Gent Date: Wed, 30 Sep 2020 13:29:56 -0400 Subject: [PATCH] Add item stacking logic --- src/ItemManager.cpp | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/ItemManager.cpp b/src/ItemManager.cpp index 96cd8e6..bec937e 100644 --- a/src/ItemManager.cpp +++ b/src/ItemManager.cpp @@ -98,10 +98,38 @@ void ItemManager::itemMoveHandler(CNSocket* sock, CNPacketData* data) { resp.ToSlotItem = *toItem; resp.FromSlotItem = *fromItem; - // swap items in session - sItemBase temp = *toItem; - *toItem = *fromItem; - *fromItem = temp; + // swap/stack items in session + Item* itemDat = getItemData(toItem->iID, toItem->iType); + if (itemDat->stackSize > 1 && itemDat == getItemData(fromItem->iID, fromItem->iType)) { + // items are stackable and identical, 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; + *toItem = *fromItem; + *fromItem = temp; + resp.iFromSlotNum = itemmove->iToSlotNum; + resp.iToSlotNum = itemmove->iFromSlotNum; + } // send equip change to viewable players if (itemmove->eFrom == (int)SlotType::EQUIP || itemmove->eTo == (int)SlotType::EQUIP) { @@ -130,9 +158,8 @@ void ItemManager::itemMoveHandler(CNSocket* sock, CNPacketData* data) { // send response resp.eTo = itemmove->eFrom; - resp.iToSlotNum = itemmove->iFromSlotNum; 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)); }