From fd3893190fc6822ed5676a81feab0a1e72f2452c Mon Sep 17 00:00:00 2001 From: qndel Date: Thu, 1 Jul 2021 19:21:10 +0200 Subject: [PATCH] inv cleanup --- Source/inv.cpp | 142 ++++++++++++++++------------------------------ Source/inv.h | 5 +- Source/stores.cpp | 8 +-- 3 files changed, 55 insertions(+), 100 deletions(-) diff --git a/Source/inv.cpp b/Source/inv.cpp index 5055f1145..e2e1ec6da 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -226,11 +226,11 @@ void DrawInv(const CelOutputBuffer &out) // calc item offsets for weapons smaller than 2x3 slots if (slot == INVLOC_HAND_LEFT) { - screenX += frameSize.width == InventorySlotSizeInPixels.width ? 14 : 0; - screenY += frameSize.height == (3 * InventorySlotSizeInPixels.height) ? 0 : -14; + screenX += frameSize.width == InventorySlotSizeInPixels.width ? INV_SLOT_HALF_SIZE_PX : 0; + screenY += frameSize.height == (3 * InventorySlotSizeInPixels.height) ? 0 : -INV_SLOT_HALF_SIZE_PX; } else if (slot == INVLOC_HAND_RIGHT) { - screenX += frameSize.width == InventorySlotSizeInPixels.width ? 13 : 1; - screenY += frameSize.height == (3 * InventorySlotSizeInPixels.height) ? 0 : -14; + screenX += frameSize.width == InventorySlotSizeInPixels.width ? (INV_SLOT_HALF_SIZE_PX - 1) : 1; + screenY += frameSize.height == (3 * InventorySlotSizeInPixels.height) ? 0 : -INV_SLOT_HALF_SIZE_PX; } const auto &cel = GetInvItemSprite(frame); @@ -252,7 +252,7 @@ void DrawInv(const CelOutputBuffer &out) light_table_index = 0; cel_transparency_active = true; - const int dstX = RIGHT_PANEL_X + slotPos[INVLOC_HAND_RIGHT].x + (frameSize.width == InventorySlotSizeInPixels.width ? 13 : -1); + const int dstX = RIGHT_PANEL_X + slotPos[INVLOC_HAND_RIGHT].x + (frameSize.width == InventorySlotSizeInPixels.width ? INV_SLOT_HALF_SIZE_PX : 0) - 1; const int dstY = slotPos[INVLOC_HAND_RIGHT].y; CelClippedBlitLightTransTo(out, { dstX, dstY }, cel, celFrame); @@ -758,17 +758,14 @@ bool GoldAutoPlace(PlayerStruct &player) bool GoldAutoPlaceInInventorySlot(PlayerStruct &player, int slotIndex) { - int yy = 10 * (slotIndex / 10); - int xx = slotIndex % 10; - - if (player.InvGrid[xx + yy] != 0) { + if (player.InvGrid[slotIndex] != 0) { return false; } int ii = player._pNumInv; player.InvList[ii] = player.HoldItem; - player._pNumInv = player._pNumInv + 1; - player.InvGrid[xx + yy] = player._pNumInv; + player._pNumInv++; + player.InvGrid[slotIndex] = player._pNumInv; GetPlrHandSeed(&player.InvList[ii]); int gold = player.HoldItem._ivalue; @@ -804,7 +801,7 @@ void CheckInvPaste(int pnum, Point cursorPosition) Size itemSize { icursW28, icursH28 }; bool done = false; int r = 0; - for (; r < NUM_XY_SLOTS && !done; r++) { + for (; r < NUM_XY_SLOTS; r++) { int xo = RIGHT_PANEL; int yo = 0; if (r >= SLOTXY_BELT_FIRST) { @@ -815,17 +812,19 @@ void CheckInvPaste(int pnum, Point cursorPosition) if (i >= InvRect[r].x + xo && i <= InvRect[r].x + xo + InventorySlotSizeInPixels.width) { if (j >= InvRect[r].y + yo - InventorySlotSizeInPixels.height - 1 && j < InvRect[r].y + yo) { done = true; - r--; } } - if (r == SLOTXY_CHEST_LAST) { + if (r == SLOTXY_INV_FIRST) { if ((itemSize.width & 1) == 0) - i -= 14; + i -= INV_SLOT_HALF_SIZE_PX; if ((itemSize.height & 1) == 0) - j -= 14; + j -= INV_SLOT_HALF_SIZE_PX; } if (r == SLOTXY_INV_LAST && (itemSize.height & 1) == 0) - j += 14; + j += INV_SLOT_HALF_SIZE_PX; + + if (done) // found a slot + break; } if (!done) return; @@ -871,10 +870,8 @@ void CheckInvPaste(int pnum, Point cursorPosition) done = true; int ii = r - SLOTXY_INV_FIRST; if (player.HoldItem._itype == ITYPE_GOLD) { - int yy = 10 * (ii / 10); - int xx = ii % 10; - if (player.InvGrid[xx + yy] != 0) { - int iv = player.InvGrid[xx + yy]; + if (player.InvGrid[ii] != 0) { + int iv = player.InvGrid[ii]; if (iv > 0) { if (player.InvList[iv - 1]._itype != ITYPE_GOLD) { it = iv; @@ -884,17 +881,17 @@ void CheckInvPaste(int pnum, Point cursorPosition) } } } else { - int yy = 10 * ((ii / 10) - ((itemSize.height - 1) / 2)); + int yy = INV_ROW_SLOT_SIZE * ((ii / INV_ROW_SLOT_SIZE) - ((itemSize.height - 1) / 2)); if (yy < 0) yy = 0; for (j = 0; j < itemSize.height && done; j++) { if (yy >= NUM_INV_GRID_ELEM) done = false; - int xx = (ii % 10) - ((itemSize.width - 1) / 2); + int xx = (ii % INV_ROW_SLOT_SIZE) - ((itemSize.width - 1) / 2); if (xx < 0) xx = 0; for (i = 0; i < itemSize.width && done; i++) { - if (xx >= 10) { + if (xx >= INV_ROW_SLOT_SIZE) { done = false; } else { if (player.InvGrid[xx + yy] != 0) { @@ -911,7 +908,7 @@ void CheckInvPaste(int pnum, Point cursorPosition) } xx++; } - yy += 10; + yy += INV_ROW_SLOT_SIZE; } } } @@ -933,34 +930,29 @@ void CheckInvPaste(int pnum, Point cursorPosition) int cn = CURSOR_HAND; switch (il) { case ILOC_HELM: - NetSendCmdChItem(false, INVLOC_HEAD); - if (player.InvBody[INVLOC_HEAD].isEmpty()) - player.InvBody[INVLOC_HEAD] = player.HoldItem; - else - cn = SwapItem(&player.InvBody[INVLOC_HEAD], &player.HoldItem); - break; case ILOC_RING: - if (r == SLOTXY_RING_LEFT) { - NetSendCmdChItem(false, INVLOC_RING_LEFT); - if (player.InvBody[INVLOC_RING_LEFT].isEmpty()) - player.InvBody[INVLOC_RING_LEFT] = player.HoldItem; - else - cn = SwapItem(&player.InvBody[INVLOC_RING_LEFT], &player.HoldItem); - } else { - NetSendCmdChItem(false, INVLOC_RING_RIGHT); - if (player.InvBody[INVLOC_RING_RIGHT].isEmpty()) - player.InvBody[INVLOC_RING_RIGHT] = player.HoldItem; - else - cn = SwapItem(&player.InvBody[INVLOC_RING_RIGHT], &player.HoldItem); - } - break; case ILOC_AMULET: - NetSendCmdChItem(false, INVLOC_AMULET); - if (player.InvBody[INVLOC_AMULET].isEmpty()) - player.InvBody[INVLOC_AMULET] = player.HoldItem; + case ILOC_ARMOR: { + auto iLocToInvLoc = [&r](item_equip_type loc) { + switch (loc) { + case ILOC_HELM: + return INVLOC_HEAD; + case ILOC_RING: + return (r == SLOTXY_RING_LEFT ? INVLOC_RING_LEFT : INVLOC_RING_RIGHT); + case ILOC_AMULET: + return INVLOC_AMULET; + case ILOC_ARMOR: + return INVLOC_CHEST; + } + }; + inv_body_loc slot = iLocToInvLoc(il); + NetSendCmdChItem(false, INVLOC_CHEST); + if (player.InvBody[slot].isEmpty()) + player.InvBody[slot] = player.HoldItem; else - cn = SwapItem(&player.InvBody[INVLOC_AMULET], &player.HoldItem); + cn = SwapItem(&player.InvBody[slot], &player.HoldItem); break; + } case ILOC_ONEHAND: if (r <= SLOTXY_HAND_LEFT_LAST) { if (player.InvBody[INVLOC_HAND_LEFT].isEmpty()) { @@ -1057,20 +1049,11 @@ void CheckInvPaste(int pnum, Point cursorPosition) force_redraw = 255; } break; - case ILOC_ARMOR: - NetSendCmdChItem(false, INVLOC_CHEST); - if (player.InvBody[INVLOC_CHEST].isEmpty()) - player.InvBody[INVLOC_CHEST] = player.HoldItem; - else - cn = SwapItem(&player.InvBody[INVLOC_CHEST], &player.HoldItem); - break; case ILOC_UNEQUIPABLE: if (player.HoldItem._itype == ITYPE_GOLD && it == 0) { int ii = r - SLOTXY_INV_FIRST; - int yy = 10 * (ii / 10); - int xx = ii % 10; - if (player.InvGrid[yy + xx] > 0) { - int il = player.InvGrid[yy + xx] - 1; + if (player.InvGrid[ii] > 0) { + int il = player.InvGrid[ii] - 1; int gt = player.InvList[il]._ivalue; int ig = player.HoldItem._ivalue + gt; if (ig <= MaxGold) { @@ -1091,7 +1074,7 @@ void CheckInvPaste(int pnum, Point cursorPosition) int il = player._pNumInv; player.InvList[il] = player.HoldItem; player._pNumInv++; - player.InvGrid[yy + xx] = player._pNumInv; + player.InvGrid[ii] = player._pNumInv; player._pGold += player.HoldItem._ivalue; SetPlrHandGoldCurs(&player.InvList[il]); } @@ -1118,41 +1101,14 @@ void CheckInvPaste(int pnum, Point cursorPosition) // Calculate top-left position of item for InvGrid and then add item to InvGrid - int xx = std::max(ii % 10 - ((itemSize.width - 1) / 2), 0); - int yy = std::max(10 * (ii / 10 - ((itemSize.height - 1) / 2)), 0); + int xx = std::max(ii % INV_ROW_SLOT_SIZE - ((itemSize.width - 1) / 2), 0); + int yy = std::max(INV_ROW_SLOT_SIZE * (ii / INV_ROW_SLOT_SIZE - ((itemSize.height - 1) / 2)), 0); AddItemToInvGrid(player, xx + yy, it, itemSize); } break; case ILOC_BELT: { int ii = r - SLOTXY_BELT_FIRST; - if (player.HoldItem._itype == ITYPE_GOLD) { - if (!player.SpdList[ii].isEmpty()) { - if (player.SpdList[ii]._itype == ITYPE_GOLD) { - i = player.HoldItem._ivalue + player.SpdList[ii]._ivalue; - if (i <= MaxGold) { - player.SpdList[ii]._ivalue = i; - player._pGold += player.HoldItem._ivalue; - SetPlrHandGoldCurs(&player.SpdList[ii]); - } else { - i = MaxGold - player.SpdList[ii]._ivalue; - player._pGold += i; - player.HoldItem._ivalue -= i; - player.SpdList[ii]._ivalue = MaxGold; - player.SpdList[ii]._iCurs = ICURS_GOLD_LARGE; - - // BUGFIX: incorrect values here are leftover from beta (fixed) - cn = GetGoldCursor(player.HoldItem._ivalue); - cn += CURSOR_FIRSTITEM; - } - } else { - player._pGold += player.HoldItem._ivalue; - cn = SwapItem(&player.SpdList[ii], &player.HoldItem); - } - } else { - player.SpdList[ii] = player.HoldItem; - player._pGold += player.HoldItem._ivalue; - } - } else if (player.SpdList[ii].isEmpty()) { + if (player.SpdList[ii].isEmpty()) { player.SpdList[ii] = player.HoldItem; } else { cn = SwapItem(&player.SpdList[ii], &player.HoldItem); @@ -1215,7 +1171,7 @@ void CheckInvCut(int pnum, Point cursorPosition, bool automaticMove) bool done = false; int r = 0; - for (; (DWORD)r < NUM_XY_SLOTS && !done; r++) { + for (; (DWORD)r < NUM_XY_SLOTS; r++) { int xo = RIGHT_PANEL; int yo = 0; if (r >= SLOTXY_BELT_FIRST) { @@ -1229,7 +1185,7 @@ void CheckInvCut(int pnum, Point cursorPosition, bool automaticMove) && cursorPosition.y >= InvRect[r].y + yo - (InventorySlotSizeInPixels.height + 1) && cursorPosition.y < InvRect[r].y + yo) { done = true; - r--; + break; } } diff --git a/Source/inv.h b/Source/inv.h index b19530d59..34634cae9 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -14,9 +14,10 @@ namespace devilution { -constexpr Size InventorySlotSizeInPixels { 28, 28 }; - +#define INV_SLOT_SIZE_PX 28 +#define INV_SLOT_HALF_SIZE_PX (INV_SLOT_SIZE_PX / 2) #define INV_ROW_SLOT_SIZE 10 +constexpr Size InventorySlotSizeInPixels { INV_SLOT_SIZE_PX, INV_SLOT_SIZE_PX }; enum inv_item : int8_t { // clang-format off diff --git a/Source/stores.cpp b/Source/stores.cpp index 0c30e3bbe..cd89b2638 100644 --- a/Source/stores.cpp +++ b/Source/stores.cpp @@ -1460,15 +1460,13 @@ void PlaceStoreGold(int v) { auto &myPlayer = plr[myplr]; - for (int i = 0; i < NUM_INV_GRID_ELEM; i++) { - int xx = i % 10; - int yy = 10 * (i / 10); - if (myPlayer.InvGrid[xx + yy] == 0) { + for (int gridNum = 0; gridNum < NUM_INV_GRID_ELEM; gridNum++) { + if (myPlayer.InvGrid[gridNum] == 0) { int ii = myPlayer._pNumInv; GetGoldSeed(myplr, &golditem); myPlayer.InvList[ii] = golditem; myPlayer._pNumInv++; - myPlayer.InvGrid[xx + yy] = myPlayer._pNumInv; + myPlayer.InvGrid[gridNum] = myPlayer._pNumInv; myPlayer.InvList[ii]._ivalue = v; SetPlrHandGoldCurs(&myPlayer.InvList[ii]); return;