From 5757562341ab70d62bb4e7588104a2e5d8b8ab1b Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Fri, 25 Mar 2022 21:14:26 +0100 Subject: [PATCH] Clean up inventory code --- Source/control.cpp | 4 +- Source/cursor.cpp | 2 +- Source/inv.cpp | 114 +++++++++++++++++------------------- Source/inv.h | 2 +- Source/options.h | 6 ++ Source/panels/charpanel.cpp | 2 +- 6 files changed, 64 insertions(+), 66 deletions(-) diff --git a/Source/control.cpp b/Source/control.cpp index 4a08420bd..44243f93c 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -737,7 +737,7 @@ void CheckPanelInfo() } } if (MousePosition.x > 190 + mainPanelPosition.x && MousePosition.x < 437 + mainPanelPosition.x && MousePosition.y > 4 + mainPanelPosition.y && MousePosition.y < 33 + mainPanelPosition.y) - pcursinvitem = CheckInvHLight(); + pcursinvitem = CheckInvHLight(MousePosition); if (CheckXPBarInfo()) { panelflag = true; @@ -1073,7 +1073,7 @@ void control_drop_gold(char vkey) CloseGoldDrop(); dropGoldValue = 0; } else if (vkey == DVL_VK_BACK) { - dropGoldValue = dropGoldValue / 10; + dropGoldValue /= 10; } } diff --git a/Source/cursor.cpp b/Source/cursor.cpp index 77e05cf7c..054b82299 100644 --- a/Source/cursor.cpp +++ b/Source/cursor.cpp @@ -393,7 +393,7 @@ void CheckCursMove() return; } if (invflag && GetRightPanel().Contains(MousePosition)) { - pcursinvitem = CheckInvHLight(); + pcursinvitem = CheckInvHLight(MousePosition); return; } if (IsStashOpen && GetLeftPanel().Contains(MousePosition)) { diff --git a/Source/inv.cpp b/Source/inv.cpp index 7a3da47d4..e2abdc855 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -146,7 +146,7 @@ std::optional pInvCels; */ void AddItemToInvGrid(Player &player, int invGridIndex, int invListIndex, Size itemSize) { - const int pitch = 10; + constexpr int pitch = 10; for (int y = 0; y < itemSize.height; y++) { for (int x = 0; x < itemSize.width; x++) { if (x == 0 && y == itemSize.height - 1) @@ -284,10 +284,8 @@ bool AutoEquip(int playerId, const Item &item, inv_body_loc bodyLocation, bool p return true; } -void CheckInvPaste(int pnum, Point cursorPosition) +void CheckInvPaste(Player &player, Point cursorPosition) { - auto &player = Players[pnum]; - SetICursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); int i = cursorPosition.x + (IsHardwareCursor() ? 0 : (icursSize.width / 2)); int j = cursorPosition.y + (IsHardwareCursor() ? 0 : (icursSize.height / 2)); @@ -321,40 +319,35 @@ void CheckInvPaste(int pnum, Point cursorPosition) return; item_equip_type il = ILOC_UNEQUIPABLE; - if (r >= SLOTXY_HEAD_FIRST && r <= SLOTXY_HEAD_LAST) + if (r >= SLOTXY_HEAD_FIRST && r <= SLOTXY_HEAD_LAST) { il = ILOC_HELM; - if (r >= SLOTXY_RING_LEFT && r <= SLOTXY_RING_RIGHT) + } else if (r >= SLOTXY_RING_LEFT && r <= SLOTXY_RING_RIGHT) { il = ILOC_RING; - if (r == SLOTXY_AMULET) + } else if (r == SLOTXY_AMULET) { il = ILOC_AMULET; - if (r >= SLOTXY_HAND_LEFT_FIRST && r <= SLOTXY_HAND_RIGHT_LAST) - il = ILOC_ONEHAND; - if (r >= SLOTXY_CHEST_FIRST && r <= SLOTXY_CHEST_LAST) + } else if (r >= SLOTXY_HAND_LEFT_FIRST && r <= SLOTXY_HAND_RIGHT_LAST) { + il = player.GetItemLocation(player.HoldItem); + } else if (r >= SLOTXY_CHEST_FIRST && r <= SLOTXY_CHEST_LAST) { il = ILOC_ARMOR; - if (r >= SLOTXY_BELT_FIRST && r <= SLOTXY_BELT_LAST) + } else if (r >= SLOTXY_BELT_FIRST && r <= SLOTXY_BELT_LAST) { il = ILOC_BELT; - - done = player.GetItemLocation(player.HoldItem) == il; - - if (il == ILOC_ONEHAND && player.GetItemLocation(player.HoldItem) == ILOC_TWOHAND) { - il = ILOC_TWOHAND; - done = true; - } - if (player.GetItemLocation(player.HoldItem) == ILOC_UNEQUIPABLE && il == ILOC_BELT) { - if (itemSize == Size { 1, 1 }) { - done = true; - if (!AllItemsList[player.HoldItem.IDidx].iUsable) - done = false; - if (!player.CanUseItem(player.HoldItem)) - done = false; - if (player.HoldItem._itype == ItemType::Gold) - done = false; - } } int8_t it = 0; - if (il == ILOC_UNEQUIPABLE) { - done = true; + switch (il) { + case ILOC_BELT: + if (player.HoldItem._iLoc != ILOC_UNEQUIPABLE) + return; + if (itemSize != Size { 1, 1 }) + return; + if (!AllItemsList[player.HoldItem.IDidx].iUsable) + return; + if (!player.CanUseItem(player.HoldItem)) + return; + if (player.HoldItem._itype == ItemType::Gold) + return; + break; + case ILOC_UNEQUIPABLE: { int ii = r - SLOTXY_INV_FIRST; if (player.HoldItem._itype == ItemType::Gold) { if (player.InvGrid[ii] != 0) { @@ -368,23 +361,22 @@ void CheckInvPaste(int pnum, Point cursorPosition) } } } else { - int yy = std::max(INV_ROW_SLOT_SIZE * ((ii / INV_ROW_SLOT_SIZE) - ((itemSize.height - 1) / 2)), 0); - for (j = 0; j < itemSize.height && done; j++) { + int yy = std::max(INV_ROW_SLOT_SIZE * (ii / INV_ROW_SLOT_SIZE - ((itemSize.height - 1) / 2)), 0); + for (j = 0; j < itemSize.height; j++) { if (yy >= NUM_INV_GRID_ELEM) - done = false; + return; int xx = std::max((ii % INV_ROW_SLOT_SIZE) - ((itemSize.width - 1) / 2), 0); - for (i = 0; i < itemSize.width && done; i++) { + for (i = 0; i < itemSize.width; i++) { if (xx >= INV_ROW_SLOT_SIZE) { - done = false; - } else { - if (player.InvGrid[xx + yy] != 0) { - int8_t iv = abs(player.InvGrid[xx + yy]); - if (it != 0) { - if (it != iv) - done = false; - } else { - it = iv; - } + return; + } + if (player.InvGrid[xx + yy] != 0) { + int8_t iv = abs(player.InvGrid[xx + yy]); + if (it != 0) { + if (it != iv) + return; + } else { + it = iv; } } xx++; @@ -392,23 +384,23 @@ void CheckInvPaste(int pnum, Point cursorPosition) yy += INV_ROW_SLOT_SIZE; } } + } break; + default: + if (player.HoldItem._iLoc != il) { + return; + } + break; } - if (!done) - return; - if (IsNoneOf(il, ILOC_UNEQUIPABLE, ILOC_BELT) && !player.CanUseItem(player.HoldItem)) { - done = false; player.Say(HeroSpeech::ICantUseThisYet); - } - - if (!done) return; + } if (player._pmode > PM_WALK3 && IsNoneOf(il, ILOC_UNEQUIPABLE, ILOC_BELT)) return; - if (pnum == MyPlayerId) + if (&player == MyPlayer) PlaySFX(ItemInvSnds[ItemCAnimTbl[player.HoldItem._iCurs]]); int cn = CURSOR_HAND; @@ -499,13 +491,13 @@ void CheckInvPaste(int pnum, Point cursorPosition) player.HoldItem = player.InvBody[INVLOC_HAND_RIGHT]; else player.HoldItem = player.InvBody[INVLOC_HAND_LEFT]; - if (pnum == MyPlayerId) + if (&player == MyPlayer) NewCursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); else SetICursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); bool done2h = AutoPlaceItemInInventory(player, player.HoldItem, true); player.HoldItem = tempitem; - if (pnum == MyPlayerId) + if (&player == MyPlayer) NewCursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); else SetICursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); @@ -603,7 +595,7 @@ void CheckInvPaste(int pnum, Point cursorPosition) break; } CalcPlrInv(player, true); - if (pnum == MyPlayerId) { + if (&player == MyPlayer) { if (cn == CURSOR_HAND && !IsHardwareCursor()) SetCursorPos(MousePosition + Displacement(cursSize / 2)); NewCursor(cn); @@ -1606,7 +1598,7 @@ void inv_update_rem_item(Player &player, inv_body_loc iv) void CheckInvItem(bool isShiftHeld, bool isCtrlHeld) { if (pcurs >= CURSOR_FIRSTITEM) { - CheckInvPaste(MyPlayerId, MousePosition); + CheckInvPaste(*MyPlayer, MousePosition); } else { CheckInvCut(MyPlayerId, MousePosition, isShiftHeld, isCtrlHeld); } @@ -1895,7 +1887,7 @@ int SyncDropItem(Point position, int idx, uint16_t icreateinfo, int iseed, int i return ii; } -int8_t CheckInvHLight() +int8_t CheckInvHLight(Point mousePosition) { int8_t r = 0; for (; r < NUM_XY_SLOTS; r++) { @@ -1906,10 +1898,10 @@ int8_t CheckInvHLight() yo = GetMainPanel().position.y; } - if (MousePosition.x >= InvRect[r].x + xo - && MousePosition.x < InvRect[r].x + xo + (InventorySlotSizeInPixels.width + 1) - && MousePosition.y >= InvRect[r].y + yo - (InventorySlotSizeInPixels.height + 1) - && MousePosition.y < InvRect[r].y + yo) { + if (mousePosition.x >= InvRect[r].x + xo + && mousePosition.x < InvRect[r].x + xo + (InventorySlotSizeInPixels.width + 1) + && mousePosition.y >= InvRect[r].y + yo - (InventorySlotSizeInPixels.height + 1) + && mousePosition.y < InvRect[r].y + yo) { break; } } diff --git a/Source/inv.h b/Source/inv.h index 3c3149301..9733a67ae 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -195,7 +195,7 @@ bool TryInvPut(); int InvPutItem(Player &player, Point position); int SyncPutItem(Player &player, Point position, int idx, uint16_t icreateinfo, int iseed, int Id, int dur, int mdur, int ch, int mch, int ivalue, uint32_t ibuff, int toHit, int maxDam, int minStr, int minMag, int minDex, int ac); int SyncDropItem(Point position, int idx, uint16_t icreateinfo, int iseed, int id, int dur, int mdur, int ch, int mch, int ivalue, uint32_t ibuff, int toHit, int maxDam, int minStr, int minMag, int minDex, int ac); -int8_t CheckInvHLight(); +int8_t CheckInvHLight(Point mousePosition); void RemoveScroll(Player &player); bool UseScroll(); void UseStaffCharge(Player &player); diff --git a/Source/options.h b/Source/options.h index 2ac3e33a7..12060ea73 100644 --- a/Source/options.h +++ b/Source/options.h @@ -1,3 +1,9 @@ +/** + * @file options.h + * + * Load and save options from the diablo.ini file. + */ + #pragma once #include diff --git a/Source/panels/charpanel.cpp b/Source/panels/charpanel.cpp index 11512268b..5c765cb21 100644 --- a/Source/panels/charpanel.cpp +++ b/Source/panels/charpanel.cpp @@ -296,7 +296,7 @@ void FreeCharPanel() void DrawChr(const Surface &out) { - Point pos = GetPanelPosition(UiPanels::Character, { 0, 0 }); + Point pos = GetPanelPosition(UiPanels::Character); DrawArt(out, pos, &PanelFull); for (auto &entry : panelEntries) { if (entry.statDisplayFunc != nullptr) {