diff --git a/Source/inv.cpp b/Source/inv.cpp index 53fc8d515..fbee6a9f8 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -864,7 +864,7 @@ void CheckInvCut(int pnum, Point cursorPosition, bool automaticMove, bool dropIt } } -void UpdateBookLevel(Player &player, Item &book) +void UpdateBookLevel(const Player &player, Item &book) { if (book._iMiscId != IMISC_BOOK) return; @@ -1611,7 +1611,7 @@ void CheckInvScrn(bool isShiftHeld, bool isCtrlHeld) } } -void InvGetItem(int pnum, int ii) +void InvGetItem(Player &player, int ii) { auto &item = Items[ii]; if (dropGoldFlag) { @@ -1622,22 +1622,26 @@ void InvGetItem(int pnum, int ii) if (dItem[item.position.x][item.position.y] == 0) return; - auto &player = Players[pnum]; + item._iCreateInfo &= ~CF_PREGEN; + CheckQuestItem(player, item); + UpdateBookLevel(player, item); + item._iStatFlag = player.CanUseItem(item); - if (MyPlayerId == pnum && !player.HoldItem.isEmpty()) - NetSendCmdPItem(true, CMD_SYNCPUTITEM, player.position.tile, player.HoldItem); + if (item._itype != ItemType::Gold || !GoldAutoPlace(player, item)) { + // The item needs to go into the players hand + if (MyPlayer == &player && !player.HoldItem.isEmpty()) { + // drop whatever the player is currently holding + NetSendCmdPItem(true, CMD_SYNCPUTITEM, player.position.tile, player.HoldItem); + } - item._iCreateInfo &= ~CF_PREGEN; - player.HoldItem = item; - CheckQuestItem(player, player.HoldItem); - UpdateBookLevel(player, player.HoldItem); - player.HoldItem._iStatFlag = player.CanUseItem(player.HoldItem); - if (player.HoldItem._itype == ItemType::Gold && GoldAutoPlace(player, player.HoldItem)) - player.HoldItem._itype == ItemType::None; + // need to copy here instead of move so CleanupItems still has access to the position + player.HoldItem = item; + NewCursor(player.HoldItem); + } + + // This potentially moves items in memory so must be done after we've made a copy CleanupItems(ii); pcursitem = -1; - if (!player.HoldItem.isEmpty()) - NewCursor(player.HoldItem); } void AutoGetItem(int pnum, Item *itemPointer, int ii) diff --git a/Source/inv.h b/Source/inv.h index b99805279..68d402871 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -187,7 +187,7 @@ void CheckInvItem(bool isShiftHeld = false, bool isCtrlHeld = false); * Check for interactions with belt */ void CheckInvScrn(bool isShiftHeld, bool isCtrlHeld); -void InvGetItem(int pnum, int ii); +void InvGetItem(Player &player, int ii); void AutoGetItem(int pnum, Item *item, int ii); /** diff --git a/Source/msg.cpp b/Source/msg.cpp index 87c0f9b13..08aa0bb42 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -719,7 +719,7 @@ DWORD OnRequestGetItem(const TCmd *pCmd, Player &player) if (message.bPnum != MyPlayerId) SyncGetItem(position, message.dwSeed, message.wIndx, message.wCI); else - InvGetItem(MyPlayerId, ii); + InvGetItem(*MyPlayer, ii); SetItemRecord(message.dwSeed, message.wCI, message.wIndx); } else if (!NetSendCmdReq2(CMD_REQUESTGITEM, MyPlayerId, message.bPnum, message)) { NetSendCmdExtra(message); @@ -742,13 +742,12 @@ DWORD OnGetItem(const TCmd *pCmd, int pnum) if ((currlevel == message.bLevel || message.bPnum == MyPlayerId) && message.bMaster != MyPlayerId) { if (message.bPnum == MyPlayerId) { if (currlevel != message.bLevel) { - auto &player = Players[MyPlayerId]; - int ii = SyncPutItem(player, player.position.tile, message.wIndx, message.wCI, message.dwSeed, message.bId, message.bDur, message.bMDur, message.bCh, message.bMCh, message.wValue, message.dwBuff, message.wToHit, message.wMaxDam, message.bMinStr, message.bMinMag, message.bMinDex, message.bAC); + int ii = SyncPutItem(*MyPlayer, MyPlayer->position.tile, message.wIndx, message.wCI, message.dwSeed, message.bId, message.bDur, message.bMDur, message.bCh, message.bMCh, message.wValue, message.dwBuff, message.wToHit, message.wMaxDam, message.bMinStr, message.bMinMag, message.bMinDex, message.bAC); if (ii != -1) - InvGetItem(MyPlayerId, ii); + InvGetItem(*MyPlayer, ii); } else { int activeItemIndex = FindGetItem(message.dwSeed, message.wIndx, message.wCI); - InvGetItem(MyPlayerId, ActiveItems[activeItemIndex]); + InvGetItem(*MyPlayer, ActiveItems[activeItemIndex]); } } else { SyncGetItem(position, message.dwSeed, message.wIndx, message.wCI);