From 3de338e5b6e6b759a52e5cb367e53988ece49424 Mon Sep 17 00:00:00 2001 From: Juliano Leal Goncalves Date: Sat, 27 Feb 2021 19:30:05 -0300 Subject: [PATCH] :recycle: Pass item referece on SpecialAutoPlace This simplifies and decouples the logic for determining the size of the item from ambient variables and makes the function easier to use. --- Source/inv.cpp | 25 +++++++++++++++++++++---- Source/inv.h | 2 +- Source/stores.cpp | 4 ++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Source/inv.cpp b/Source/inv.cpp index 649f29a4a..c26be968c 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -580,6 +580,21 @@ void DrawInvBelt() } } +/** + * @brief Gets the size, in inventory cells, of the given item. + * @param item The item whose size is to be determined. + * @return The size, in inventory cells, of the item. + */ +InvXY GetInventorySize(const ItemStruct &item) +{ + int itemSizeIndex = item._iCurs + CURSOR_FIRSTITEM; + + return { + InvItemWidth[itemSizeIndex] / INV_SLOT_SIZE_PX, + InvItemHeight[itemSizeIndex] / INV_SLOT_SIZE_PX, + }; +} + BOOL AutoPlace(int pnum, int ii, int sx, int sy, BOOL saveflag) { int i, j, xx, yy; @@ -635,7 +650,7 @@ BOOL AutoPlace(int pnum, int ii, int sx, int sy, BOOL saveflag) return done; } -BOOL SpecialAutoPlace(int pnum, int ii, int sx, int sy) +BOOL SpecialAutoPlace(int pnum, int ii, const ItemStruct &item) { int i, j, xx, yy; BOOL done; @@ -645,7 +660,9 @@ BOOL SpecialAutoPlace(int pnum, int ii, int sx, int sy) if (yy < 0) { yy = 0; } - for (j = 0; j < sy && done; j++) { + + InvXY itemSize = GetInventorySize(item); + for (j = 0; j < itemSize.Y && done; j++) { if (yy >= NUM_INV_GRID_ELEM) { done = FALSE; } @@ -653,7 +670,7 @@ BOOL SpecialAutoPlace(int pnum, int ii, int sx, int sy) if (xx < 0) { xx = 0; } - for (i = 0; i < sx && done; i++) { + for (i = 0; i < itemSize.X && done; i++) { if (xx >= 10) { done = FALSE; } else { @@ -664,7 +681,7 @@ BOOL SpecialAutoPlace(int pnum, int ii, int sx, int sy) yy += 10; } if (!done) { - if (sx > 1 || sy > 1) { + if (itemSize.X > 1 || itemSize.Y > 1) { done = FALSE; } else { for (i = 0; i < MAXBELTITEMS; i++) { diff --git a/Source/inv.h b/Source/inv.h index 7ea96d254..3b5a25cb6 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -21,7 +21,7 @@ void InitInv(); void DrawInv(); void DrawInvBelt(); BOOL AutoPlace(int pnum, int ii, int sx, int sy, BOOL saveflag); -BOOL SpecialAutoPlace(int pnum, int ii, int sx, int sy); +BOOL SpecialAutoPlace(int pnum, int ii, const ItemStruct &item); BOOL GoldAutoPlace(int pnum); void CheckInvSwap(int pnum, BYTE bLoc, int idx, WORD wCI, int seed, BOOL bId); void inv_update_rem_item(int pnum, BYTE iv); diff --git a/Source/stores.cpp b/Source/stores.cpp index afb9a1389..55ee3aceb 100644 --- a/Source/stores.cpp +++ b/Source/stores.cpp @@ -2270,7 +2270,7 @@ void S_WBuyEnter() done = FALSE; for (i = 0; i < NUM_INV_GRID_ELEM && !done; i++) { - done = SpecialAutoPlace(myplr, i, cursW / 28, cursH / 28); + done = SpecialAutoPlace(myplr, i, plr[myplr].HoldItem); } if (done) @@ -2571,7 +2571,7 @@ void S_HBuyEnter() done = FALSE; for (i = 0; i < NUM_INV_GRID_ELEM && !done; i++) { - done = SpecialAutoPlace(myplr, i, cursW / 28, cursH / 28); + done = SpecialAutoPlace(myplr, i, plr[myplr].HoldItem); } if (done)