diff --git a/Source/control.cpp b/Source/control.cpp index ca4b473d8..f8429003a 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -658,7 +658,7 @@ void OpenCharPanel() { QuestLogIsOpen = false; CloseGoldWithdraw(); - IsStashOpen = false; + CloseStash(); chrflag = true; } @@ -1032,7 +1032,7 @@ void CheckBtnUp() case PanelButtonQlog: CloseCharPanel(); CloseGoldWithdraw(); - IsStashOpen = false; + CloseStash(); if (!QuestLogIsOpen) StartQuestlog(); else @@ -1049,7 +1049,7 @@ void CheckBtnUp() case PanelButtonInventory: sbookflag = false; CloseGoldWithdraw(); - IsStashOpen = false; + CloseStash(); invflag = !invflag; if (dropGoldFlag) { CloseGoldDrop(); diff --git a/Source/controls/plrctrls.cpp b/Source/controls/plrctrls.cpp index 947dc1308..6256980f2 100644 --- a/Source/controls/plrctrls.cpp +++ b/Source/controls/plrctrls.cpp @@ -1627,7 +1627,7 @@ void ProcessGameAction(const GameAction &action) QuestLogIsOpen = false; sbookflag = false; CloseGoldWithdraw(); - IsStashOpen = false; + CloseStash(); } break; case GameActionType_TOGGLE_CHARACTER_INFO: @@ -1644,7 +1644,7 @@ void ProcessGameAction(const GameAction &action) StartQuestlog(); CloseCharPanel(); CloseGoldWithdraw(); - IsStashOpen = false; + CloseStash(); spselflag = false; } else { QuestLogIsOpen = false; diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 40ac6c149..92d31932e 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -227,7 +227,7 @@ void LeftMouseCmd(bool bShift) if (leveltype == DTYPE_TOWN) { CloseGoldWithdraw(); - IsStashOpen = false; + CloseStash(); if (pcursitem != -1 && pcurs == CURSOR_HAND) NetSendCmdLocParam1(true, invflag ? CMD_GOTOGETITEM : CMD_GOTOAGETITEM, cursPosition, pcursitem); if (pcursmonst != -1) @@ -1482,7 +1482,7 @@ void InventoryKeyPressed() } sbookflag = false; CloseGoldWithdraw(); - IsStashOpen = false; + CloseStash(); } void CharacterSheetKeyPressed() @@ -1525,7 +1525,7 @@ void QuestLogKeyPressed() } CloseCharPanel(); CloseGoldWithdraw(); - IsStashOpen = false; + CloseStash(); } void DisplaySpellsKeyPressed() diff --git a/Source/inv.cpp b/Source/inv.cpp index 26770c7c1..87907739a 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -2081,10 +2081,37 @@ bool UseInvItem(int cii) void CloseInventory() { CloseGoldWithdraw(); - IsStashOpen = false; + CloseStash(); invflag = false; } +void CloseStash() +{ + if (!IsStashOpen) + return; + + Player &myPlayer = *MyPlayer; + if (!myPlayer.HoldItem.isEmpty()) { + std::optional itemTile = FindAdjacentPositionForItem(myPlayer.position.future, myPlayer._pdir); + if (itemTile) { + NetSendCmdPItem(true, CMD_PUTITEM, *itemTile, myPlayer.HoldItem); + } else { + if (!AutoPlaceItemInBelt(myPlayer, myPlayer.HoldItem, true) + && !AutoPlaceItemInInventory(myPlayer, myPlayer.HoldItem, true) + && !AutoPlaceItemInStash(myPlayer, myPlayer.HoldItem, true)) { + // This can fail for max gold, arena potions and a stash that has been arranged + // to not have room for the item all 3 cases are extremely unlikely + app_fatal(_("No room for item")); + } + PlaySFX(ItemInvSnds[ItemCAnimTbl[myPlayer.HoldItem._iCurs]]); + } + myPlayer.HoldItem.clear(); + NewCursor(CURSOR_HAND); + } + + IsStashOpen = false; +} + void DoTelekinesis() { if (ObjectUnderCursor != nullptr && !ObjectUnderCursor->IsDisabled()) diff --git a/Source/inv.h b/Source/inv.h index 4915e9059..122175fb6 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -96,6 +96,7 @@ bool CanBePlacedOnBelt(const Item &item); using ItemFunc = void (*)(Item &); void CloseInventory(); +void CloseStash(); void FreeInvGFX(); void InitInv();