Browse Source

Introduce StashStruct.GetPage/SetPage

pull/4202/head^2
obligaron 4 years ago committed by Anders Jenbo
parent
commit
481f15ded5
  1. 4
      Source/controls/plrctrls.cpp
  2. 4
      Source/diablo.cpp
  3. 40
      Source/qol/stash.cpp
  4. 14
      Source/qol/stash.h

4
Source/controls/plrctrls.cpp

@ -680,7 +680,7 @@ Point FindFirstStashSlotOnItem(uint16_t itemInvId)
return InvalidStashPoint; return InvalidStashPoint;
for (auto point : PointsInRectangleRange({ { 0, 0 }, { 10, 10 } })) { for (auto point : PointsInRectangleRange({ { 0, 0 }, { 10, 10 } })) {
if (Stash.stashGrids[Stash.page][point.x][point.y] == itemInvId) if (Stash.stashGrids[Stash.GetPage()][point.x][point.y] == itemInvId)
return point; return point;
} }
@ -1574,7 +1574,7 @@ void PerformPrimaryAction()
Point slotUnderCursor = stashSlot + Displacement { x, y }; Point slotUnderCursor = stashSlot + Displacement { x, y };
if (slotUnderCursor.x >= 10 || slotUnderCursor.y >= 10) if (slotUnderCursor.x >= 10 || slotUnderCursor.y >= 10)
continue; continue;
uint16_t itemId = Stash.stashGrids[Stash.page][slotUnderCursor.x][slotUnderCursor.y]; uint16_t itemId = Stash.stashGrids[Stash.GetPage()][slotUnderCursor.x][slotUnderCursor.y];
if (itemId != 0) if (itemId != 0)
return itemId; return itemId;
} }

4
Source/diablo.cpp

@ -510,7 +510,7 @@ void PressKey(int vkey)
} else if (AutomapActive) { } else if (AutomapActive) {
AutomapUp(); AutomapUp();
} else if (IsStashOpen) { } else if (IsStashOpen) {
Stash.page = clamp(Stash.page - 1, 0, 49); Stash.SetPage(Stash.GetPage() - 1);
} }
} else if (vkey == DVL_VK_DOWN) { } else if (vkey == DVL_VK_DOWN) {
if (stextflag != STORE_NONE) { if (stextflag != STORE_NONE) {
@ -524,7 +524,7 @@ void PressKey(int vkey)
} else if (AutomapActive) { } else if (AutomapActive) {
AutomapDown(); AutomapDown();
} else if (IsStashOpen) { } else if (IsStashOpen) {
Stash.page = clamp(Stash.page + 1, 0, 49); Stash.SetPage(Stash.GetPage() + 1);
} }
} else if (vkey == DVL_VK_PRIOR) { } else if (vkey == DVL_VK_PRIOR) {
if (stextflag != STORE_NONE) { if (stextflag != STORE_NONE) {

40
Source/qol/stash.cpp

@ -28,6 +28,8 @@ int WithdrawGoldValue;
namespace { namespace {
constexpr int CountStashPages = 50;
int InitialWithdrawGoldValue; int InitialWithdrawGoldValue;
/** Contains mappings for the buttons in the stash (2 navigation buttons, withdraw gold buttons, 2 navigation buttons) */ /** Contains mappings for the buttons in the stash (2 navigation buttons, withdraw gold buttons, 2 navigation buttons) */
@ -109,7 +111,7 @@ void CheckStashPaste(Point cursorPosition)
// Check that no more then 1 item is replaced by the move // Check that no more then 1 item is replaced by the move
uint16_t it = 0; uint16_t it = 0;
for (auto point : PointsInRectangleRange({ { 0, 0 }, itemSize })) { for (auto point : PointsInRectangleRange({ { 0, 0 }, itemSize })) {
uint16_t iv = Stash.stashGrids[Stash.page][firstSlot.x + point.x][firstSlot.y + point.y]; uint16_t iv = Stash.stashGrids[Stash.GetPage()][firstSlot.x + point.x][firstSlot.y + point.y];
if (iv == 0 || it == iv) if (iv == 0 || it == iv)
continue; continue;
if (it == 0) { if (it == 0) {
@ -131,7 +133,7 @@ void CheckStashPaste(Point cursorPosition)
} else { } else {
stashIndex = it - 1; stashIndex = it - 1;
cn = SwapItem(Stash.stashList[stashIndex], player.HoldItem); cn = SwapItem(Stash.stashList[stashIndex], player.HoldItem);
for (auto &row : Stash.stashGrids[Stash.page]) { for (auto &row : Stash.stashGrids[Stash.GetPage()]) {
for (auto &itemId : row) { for (auto &itemId : row) {
if (itemId == it) if (itemId == it)
itemId = 0; itemId = 0;
@ -139,7 +141,7 @@ void CheckStashPaste(Point cursorPosition)
} }
} }
AddItemToStashGrid(Stash.page, firstSlot, stashIndex, itemSize); AddItemToStashGrid(Stash.GetPage(), firstSlot, stashIndex, itemSize);
Stash.dirty = true; Stash.dirty = true;
@ -182,7 +184,7 @@ void CheckStashCut(Point cursorPosition, bool automaticMove, bool dropItem)
bool automaticallyMoved = false; bool automaticallyMoved = false;
bool automaticallyEquipped = false; bool automaticallyEquipped = false;
uint16_t ii = Stash.stashGrids[Stash.page][slot.x][slot.y]; uint16_t ii = Stash.stashGrids[Stash.GetPage()][slot.x][slot.y];
if (ii != 0) { if (ii != 0) {
uint16_t iv = ii - 1; uint16_t iv = ii - 1;
@ -306,25 +308,24 @@ void CheckStashButtonRelease(Point mousePosition)
if (stashButton.Contains(mousePosition)) { if (stashButton.Contains(mousePosition)) {
switch (StashButtonPressed) { switch (StashButtonPressed) {
case 0: case 0:
Stash.page -= 10; Stash.SetPage(Stash.GetPage() - 10);
break; break;
case 1: case 1:
Stash.page -= 1; Stash.SetPage(Stash.GetPage() - 1);
break; break;
case 2: case 2:
StartGoldWithdraw(); StartGoldWithdraw();
break; break;
case 3: case 3:
Stash.page += 1; Stash.SetPage(Stash.GetPage() + 1);
break; break;
case 4: case 4:
Stash.page += 10; Stash.SetPage(Stash.GetPage() + 10);
break; break;
} }
} }
StashButtonPressed = -1; StashButtonPressed = -1;
Stash.page = clamp(Stash.page, 0, 49);
} }
void CheckStashButtonPress(Point mousePosition) void CheckStashButtonPress(Point mousePosition)
@ -355,17 +356,17 @@ void DrawStash(const Surface &out)
constexpr Displacement offset { 0, INV_SLOT_SIZE_PX - 1 }; constexpr Displacement offset { 0, INV_SLOT_SIZE_PX - 1 };
for (auto slot : PointsInRectangleRange({ { 0, 0 }, { 10, 10 } })) { for (auto slot : PointsInRectangleRange({ { 0, 0 }, { 10, 10 } })) {
if (Stash.stashGrids[Stash.page][slot.x][slot.y] != 0) { if (Stash.stashGrids[Stash.GetPage()][slot.x][slot.y] != 0) {
InvDrawSlotBack(out, GetStashSlotCoord(slot) + offset, InventorySlotSizeInPixels); InvDrawSlotBack(out, GetStashSlotCoord(slot) + offset, InventorySlotSizeInPixels);
} }
} }
for (auto slot : PointsInRectangleRange({ { 0, 0 }, { 10, 10 } })) { for (auto slot : PointsInRectangleRange({ { 0, 0 }, { 10, 10 } })) {
if (Stash.stashGrids[Stash.page][slot.x][slot.y] == 0) { if (Stash.stashGrids[Stash.GetPage()][slot.x][slot.y] == 0) {
continue; // No item in the given slot continue; // No item in the given slot
} }
uint16_t itemId = Stash.stashGrids[Stash.page][slot.x][slot.y] - 1; uint16_t itemId = Stash.stashGrids[Stash.GetPage()][slot.x][slot.y] - 1;
if (Stash.stashList[itemId].position != slot) { if (Stash.stashList[itemId].position != slot) {
continue; // Not the first slot of the item continue; // Not the first slot of the item
} }
@ -388,7 +389,7 @@ void DrawStash(const Surface &out)
Point position = GetPanelPosition(UiPanels::Stash); Point position = GetPanelPosition(UiPanels::Stash);
UiFlags style = UiFlags::VerticalCenter | UiFlags::ColorWhite; UiFlags style = UiFlags::VerticalCenter | UiFlags::ColorWhite;
DrawString(out, fmt::format("{:d}", Stash.page + 1), { position + Displacement { 132, 0 }, { 57, 11 } }, UiFlags::AlignCenter | style); DrawString(out, fmt::format("{:d}", Stash.GetPage() + 1), { position + Displacement { 132, 0 }, { 57, 11 } }, UiFlags::AlignCenter | style);
DrawString(out, fmt::format("{:d}", Stash.gold), { position + Displacement { 122, 19 }, { 107, 13 } }, UiFlags::AlignRight | style); DrawString(out, fmt::format("{:d}", Stash.gold), { position + Displacement { 122, 19 }, { 107, 13 } }, UiFlags::AlignRight | style);
} }
@ -423,7 +424,7 @@ uint16_t CheckStashHLight(Point mousePosition)
ClearPanel(); ClearPanel();
uint16_t itemId = abs(Stash.stashGrids[Stash.page][slot.x][slot.y]); uint16_t itemId = abs(Stash.stashGrids[Stash.GetPage()][slot.x][slot.y]);
if (itemId == 0) if (itemId == 0)
return -1; return -1;
@ -510,7 +511,7 @@ bool UseStashItem(uint16_t c)
void StashStruct::RemoveStashItem(uint16_t iv) void StashStruct::RemoveStashItem(uint16_t iv)
{ {
// Iterate through stashGrid and remove every reference to item // Iterate through stashGrid and remove every reference to item
for (auto &row : Stash.stashGrids[Stash.page]) { for (auto &row : Stash.stashGrids[Stash.GetPage()]) {
for (uint16_t &itemId : row) { for (uint16_t &itemId : row) {
if (itemId - 1 == iv) { if (itemId - 1 == iv) {
itemId = 0; itemId = 0;
@ -542,6 +543,11 @@ void StashStruct::RemoveStashItem(uint16_t iv)
Stash.dirty = true; Stash.dirty = true;
} }
void StashStruct::SetPage(int newPage)
{
page = clamp(newPage, 0, CountStashPages - 1);
}
void WithdrawGoldKeyPress(char vkey) void WithdrawGoldKeyPress(char vkey)
{ {
auto &myPlayer = Players[MyPlayerId]; auto &myPlayer = Players[MyPlayerId];
@ -625,11 +631,9 @@ bool AutoPlaceItemInStash(Player &player, const Item &item, bool persistItem)
Size itemSize = GetInventorySize(item); Size itemSize = GetInventorySize(item);
constexpr int CountStashPages = 50;
// Try to add the item to the current active page and if it's not possible move forward // Try to add the item to the current active page and if it's not possible move forward
for (int pageCounter = 0; pageCounter < CountStashPages; pageCounter++) { for (int pageCounter = 0; pageCounter < CountStashPages; pageCounter++) {
int pageIndex = Stash.page + pageCounter; int pageIndex = Stash.GetPage() + pageCounter;
// Wrap around if needed // Wrap around if needed
if (pageIndex >= CountStashPages) if (pageIndex >= CountStashPages)
pageIndex -= CountStashPages; pageIndex -= CountStashPages;

14
Source/qol/stash.h

@ -14,14 +14,24 @@
namespace devilution { namespace devilution {
struct StashStruct { class StashStruct {
public:
void RemoveStashItem(uint16_t iv); void RemoveStashItem(uint16_t iv);
std::map<int, std::array<std::array<uint16_t, 10>, 10>> stashGrids; std::map<int, std::array<std::array<uint16_t, 10>, 10>> stashGrids;
std::vector<Item> stashList; std::vector<Item> stashList;
int gold; int gold;
bool dirty = false;
int GetPage() const
{
return page;
}
void SetPage(int newPage);
private:
/** Current Page */ /** Current Page */
int page; int page;
bool dirty = false;
}; };
constexpr Point InvalidStashPoint { -1, -1 }; constexpr Point InvalidStashPoint { -1, -1 };

Loading…
Cancel
Save