Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raised the limit of max items per stack to 999 #3923

Merged
merged 14 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions include/constants/items.h
Original file line number Diff line number Diff line change
Expand Up @@ -1022,13 +1022,12 @@
#define NUM_TECHNICAL_MACHINES 100
#define NUM_HIDDEN_MACHINES 8

#define MAX_BAG_ITEM_CAPACITY 99
#define MAX_PC_ITEM_CAPACITY 999
#define MAX_BERRY_CAPACITY 999
#define MAX_BAG_ITEM_CAPACITY 999
#define MAX_PC_ITEM_CAPACITY 999
#define MAX_PYRAMID_BAG_ITEM_CAPACITY 99 // Values higher than 255 require free SaveBlock2 space.

#define BAG_ITEM_CAPACITY_DIGITS 2
#define BERRY_CAPACITY_DIGITS 3
#define MAX_ITEM_DIGITS BERRY_CAPACITY_DIGITS
#define MAX_ITEM_DIGITS ((MAX_BAG_ITEM_CAPACITY > 99) ? 3 : 2)
#define MAX_PYRAMID_ITEM_DIGITS ((MAX_PYRAMID_BAG_ITEM_CAPACITY > 99) ? 3 : 2)

// Secondary IDs for rods
#define OLD_ROD 0
Expand Down
5 changes: 5 additions & 0 deletions include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "constants/pokemon.h"
#include "constants/easy_chat.h"
#include "constants/trainer_hill.h"
#include "constants/items.h"

// Prevent cross-jump optimization.
#define BLOCK_CROSS_JUMP asm("");
Expand Down Expand Up @@ -225,7 +226,11 @@ struct BerryPickingResults
struct PyramidBag
{
u16 itemId[FRONTIER_LVL_MODE_COUNT][PYRAMID_BAG_ITEMS_COUNT];
#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 quantity[FRONTIER_LVL_MODE_COUNT][PYRAMID_BAG_ITEMS_COUNT];
#else
u8 quantity[FRONTIER_LVL_MODE_COUNT][PYRAMID_BAG_ITEMS_COUNT];
#endif
};

struct BerryCrush
Expand Down
28 changes: 22 additions & 6 deletions src/battle_pyramid_bag.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ static void CopyBagItemName(u8 *dst, u16 itemId)
{
if (ItemId_GetPocket(itemId) == POCKET_BERRIES)
{
ConvertIntToDecimalStringN(gStringVar1, ITEM_TO_BERRY(itemId), STR_CONV_MODE_LEADING_ZEROS, 2);
ConvertIntToDecimalStringN(gStringVar1, ITEM_TO_BERRY(itemId), STR_CONV_MODE_LEADING_ZEROS, MAX_PYRAMID_ITEM_DIGITS);
CopyItemName(itemId, gStringVar2);
StringExpandPlaceholders(dst, gText_NumberItem_TMBerry);
}
Expand Down Expand Up @@ -670,7 +670,7 @@ static void PrintItemQuantity(u8 windowId, u32 itemIndex, u8 y)
ConvertIntToDecimalStringN(gStringVar1,
gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode][itemIndex],
STR_CONV_MODE_RIGHT_ALIGN,
2);
MAX_PYRAMID_ITEM_DIGITS);
StringExpandPlaceholders(gStringVar4, gText_xVar1);
xAlign = GetStringRightAlignXOffset(FONT_NARROW, gStringVar4, 119);
PyramidBagPrint_Quantity(windowId, gStringVar4, xAlign, y, 0, 0, TEXT_SKIP_DRAW, COLORID_DARK_GRAY);
Expand Down Expand Up @@ -727,7 +727,11 @@ static void SwapItems(u8 id1, u8 id2)
{
u16 temp;
u16 *itemIds = gSaveBlock2Ptr->frontier.pyramidBag.itemId[gSaveBlock2Ptr->frontier.lvlMode];
#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#else
u8 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#endif

SWAP(itemIds[id1], itemIds[id2], temp);
SWAP(quantities[id1], quantities[id2], temp);
Expand All @@ -736,7 +740,11 @@ static void SwapItems(u8 id1, u8 id2)
static void MovePyramidBagItemSlotInList(u8 from, u8 to)
{
u16 *itemIds = gSaveBlock2Ptr->frontier.pyramidBag.itemId[gSaveBlock2Ptr->frontier.lvlMode];
#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#else
u8 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#endif

if (from != to)
{
Expand Down Expand Up @@ -770,7 +778,11 @@ static void CompactItems(void)
{
u8 i, j;
u16 *itemIds = gSaveBlock2Ptr->frontier.pyramidBag.itemId[gSaveBlock2Ptr->frontier.lvlMode];
#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#else
u8 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#endif

for (i = 0; i < PYRAMID_BAG_ITEMS_COUNT; i++)
{
Expand Down Expand Up @@ -1148,7 +1160,7 @@ static void AskConfirmToss(u8 taskId)
s16 *data = gTasks[taskId].data;

CopyItemName(gSpecialVar_ItemId, gStringVar1);
ConvertIntToDecimalStringN(gStringVar2, tNumToToss, STR_CONV_MODE_LEFT_ALIGN, 2);
ConvertIntToDecimalStringN(gStringVar2, tNumToToss, STR_CONV_MODE_LEFT_ALIGN, MAX_PYRAMID_ITEM_DIGITS);
StringExpandPlaceholders(gStringVar4, gText_ConfirmTossItems);
FillWindowPixelBuffer(WIN_INFO, PIXEL_FILL(0));
PyramidBagPrint(WIN_INFO, gStringVar4, 3, 0, 0, 1, 0, COLORID_DARK_GRAY);
Expand All @@ -1167,7 +1179,7 @@ static void DontTossItem(u8 taskId)
static void ShowNumToToss(void)
{
s32 x;
ConvertIntToDecimalStringN(gStringVar1, 1, STR_CONV_MODE_LEADING_ZEROS, 2);
ConvertIntToDecimalStringN(gStringVar1, 1, STR_CONV_MODE_LEADING_ZEROS, MAX_PYRAMID_ITEM_DIGITS);
StringExpandPlaceholders(gStringVar4, gText_xVar1);
DrawTossNumberWindow(WIN_TOSS_NUM);
x = GetStringCenterAlignXOffset(FONT_NORMAL, gStringVar4, 0x28);
Expand All @@ -1177,7 +1189,7 @@ static void ShowNumToToss(void)
static void UpdateNumToToss(s16 num)
{
s32 x;
ConvertIntToDecimalStringN(gStringVar1, num, STR_CONV_MODE_LEADING_ZEROS, 2);
ConvertIntToDecimalStringN(gStringVar1, num, STR_CONV_MODE_LEADING_ZEROS, MAX_PYRAMID_ITEM_DIGITS);
StringExpandPlaceholders(gStringVar4, gText_xVar1);
x = GetStringCenterAlignXOffset(FONT_NORMAL, gStringVar4, 0x28);
AddTextPrinterParameterized(WIN_TOSS_NUM, FONT_NORMAL, gStringVar4, x, 2, 0, NULL);
Expand Down Expand Up @@ -1216,7 +1228,7 @@ static void TossItem(u8 taskId)
s16 *data = gTasks[taskId].data;

CopyItemName(gSpecialVar_ItemId, gStringVar1);
ConvertIntToDecimalStringN(gStringVar2, tNumToToss, STR_CONV_MODE_LEFT_ALIGN, 2);
ConvertIntToDecimalStringN(gStringVar2, tNumToToss, STR_CONV_MODE_LEFT_ALIGN, MAX_PYRAMID_ITEM_DIGITS);
StringExpandPlaceholders(gStringVar4, gText_ThrewAwayVar2Var1s);
FillWindowPixelBuffer(WIN_INFO, PIXEL_FILL(0));
PyramidBagPrint(WIN_INFO, gStringVar4, 3, 0, 0, 1, 0, COLORID_DARK_GRAY);
Expand Down Expand Up @@ -1412,7 +1424,11 @@ void TryStoreHeldItemsInPyramidBag(void)
u8 i;
struct Pokemon *party = gPlayerParty;
u16 *newItems = Alloc(PYRAMID_BAG_ITEMS_COUNT * sizeof(*newItems));
#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 *newQuantities = Alloc(PYRAMID_BAG_ITEMS_COUNT * sizeof(*newQuantities));
#else
u8 *newQuantities = Alloc(PYRAMID_BAG_ITEMS_COUNT * sizeof(*newQuantities));
#endif
u16 heldItem;

memcpy(newItems, gSaveBlock2Ptr->frontier.pyramidBag.itemId[gSaveBlock2Ptr->frontier.lvlMode], PYRAMID_BAG_ITEMS_COUNT * sizeof(*newItems));
Expand Down
11 changes: 5 additions & 6 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -3012,10 +3012,9 @@ static void DebugAction_Give_Item_SelectQuantity(u8 taskId)

if (JOY_NEW(DPAD_UP))
{
u32 maxCapacity = (ItemId_GetPocket(itemId) - 1 == BERRIES_POCKET) ? MAX_BERRY_CAPACITY : MAX_BAG_ITEM_CAPACITY;
gTasks[taskId].tInput += sPowersOfTen[gTasks[taskId].tDigit];
if (gTasks[taskId].tInput > maxCapacity)
gTasks[taskId].tInput = maxCapacity;
if (gTasks[taskId].tInput > MAX_BAG_ITEM_CAPACITY)
gTasks[taskId].tInput = MAX_BAG_ITEM_CAPACITY;
}
if (JOY_NEW(DPAD_DOWN))
{
Expand All @@ -3030,7 +3029,7 @@ static void DebugAction_Give_Item_SelectQuantity(u8 taskId)
}
if (JOY_NEW(DPAD_RIGHT))
{
if (gTasks[taskId].tDigit < 2)
if (gTasks[taskId].tDigit < MAX_ITEM_DIGITS)
gTasks[taskId].tDigit += 1;
}

Expand Down Expand Up @@ -4182,8 +4181,8 @@ static void DebugAction_PCBag_Fill_PocketBerries(u8 taskId)

for (itemId = FIRST_BERRY_INDEX; itemId < LAST_BERRY_INDEX; itemId++)
{
if (CheckBagHasSpace(itemId, MAX_BERRY_CAPACITY))
AddBagItem(itemId, MAX_BERRY_CAPACITY);
if (CheckBagHasSpace(itemId, MAX_BAG_ITEM_CAPACITY))
AddBagItem(itemId, MAX_BAG_ITEM_CAPACITY);
}
}

Expand Down
80 changes: 43 additions & 37 deletions src/item.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,35 +177,26 @@ bool8 HasAtLeastOneBerry(void)
bool8 CheckBagHasSpace(u16 itemId, u16 count)
{
u8 i;
u8 pocket;
u16 slotCapacity;
u8 pocket = ItemId_GetPocket(itemId) - 1;
u16 ownedCount;

if (ItemId_GetPocket(itemId) == POCKET_NONE)
return FALSE;

if (InBattlePyramid() || FlagGet(FLAG_STORING_ITEMS_IN_PYRAMID_BAG) == TRUE)
{
return CheckPyramidBagHasSpace(itemId, count);
}

pocket = ItemId_GetPocket(itemId) - 1;
if (pocket != BERRIES_POCKET)
slotCapacity = MAX_BAG_ITEM_CAPACITY;
else
slotCapacity = MAX_BERRY_CAPACITY;

// Check space in any existing item slots that already contain this item
for (i = 0; i < gBagPockets[pocket].capacity; i++)
{
if (gBagPockets[pocket].itemSlots[i].itemId == itemId)
{
ownedCount = GetBagItemQuantity(&gBagPockets[pocket].itemSlots[i].quantity);
if (ownedCount + count <= slotCapacity)
if (ownedCount + count <= MAX_BAG_ITEM_CAPACITY)
return TRUE;
if (pocket == TMHM_POCKET || pocket == BERRIES_POCKET)
return FALSE;
count -= (slotCapacity - ownedCount);
count -= (MAX_BAG_ITEM_CAPACITY - ownedCount);
if (count == 0)
break; //should be return TRUE, but that doesn't match
}
Expand All @@ -218,11 +209,11 @@ bool8 CheckBagHasSpace(u16 itemId, u16 count)
{
if (gBagPockets[pocket].itemSlots[i].itemId == 0)
{
if (count > slotCapacity)
if (count > MAX_BAG_ITEM_CAPACITY)
{
if (pocket == TMHM_POCKET || pocket == BERRIES_POCKET)
return FALSE;
count -= slotCapacity;
count -= MAX_BAG_ITEM_CAPACITY;
}
else
{
Expand Down Expand Up @@ -254,26 +245,20 @@ bool8 AddBagItem(u16 itemId, u16 count)
{
struct BagPocket *itemPocket;
struct ItemSlot *newItems;
u16 slotCapacity;
u16 ownedCount;
u8 pocket = ItemId_GetPocket(itemId) - 1;

itemPocket = &gBagPockets[pocket];
newItems = AllocZeroed(itemPocket->capacity * sizeof(struct ItemSlot));
memcpy(newItems, itemPocket->itemSlots, itemPocket->capacity * sizeof(struct ItemSlot));

if (pocket != BERRIES_POCKET)
slotCapacity = MAX_BAG_ITEM_CAPACITY;
else
slotCapacity = MAX_BERRY_CAPACITY;

for (i = 0; i < itemPocket->capacity; i++)
{
if (newItems[i].itemId == itemId)
{
ownedCount = GetBagItemQuantity(&newItems[i].quantity);
// check if won't exceed max slot capacity
if (ownedCount + count <= slotCapacity)
if (ownedCount + count <= MAX_BAG_ITEM_CAPACITY)
{
// successfully added to already existing item's count
SetBagItemQuantity(&newItems[i].quantity, ownedCount + count);
Expand All @@ -291,8 +276,8 @@ bool8 AddBagItem(u16 itemId, u16 count)
}
else
{
count -= slotCapacity - ownedCount;
SetBagItemQuantity(&newItems[i].quantity, slotCapacity);
count -= MAX_BAG_ITEM_CAPACITY - ownedCount;
SetBagItemQuantity(&newItems[i].quantity, MAX_BAG_ITEM_CAPACITY);
// don't create another instance of the item if it's at max slot capacity and count is equal to 0
if (count == 0)
{
Expand All @@ -312,16 +297,16 @@ bool8 AddBagItem(u16 itemId, u16 count)
if (newItems[i].itemId == ITEM_NONE)
{
newItems[i].itemId = itemId;
if (count > slotCapacity)
if (count > MAX_BAG_ITEM_CAPACITY)
{
// try creating a new slot with max capacity if duplicates are possible
if (pocket == TMHM_POCKET || pocket == BERRIES_POCKET)
{
Free(newItems);
return FALSE;
}
count -= slotCapacity;
SetBagItemQuantity(&newItems[i].quantity, slotCapacity);
count -= MAX_BAG_ITEM_CAPACITY;
SetBagItemQuantity(&newItems[i].quantity, MAX_BAG_ITEM_CAPACITY);
}
else
{
Expand Down Expand Up @@ -690,7 +675,11 @@ static bool8 CheckPyramidBagHasItem(u16 itemId, u16 count)
{
u8 i;
u16 *items = gSaveBlock2Ptr->frontier.pyramidBag.itemId[gSaveBlock2Ptr->frontier.lvlMode];
#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#else
u8 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#endif

for (i = 0; i < PYRAMID_BAG_ITEMS_COUNT; i++)
{
Expand All @@ -712,16 +701,20 @@ static bool8 CheckPyramidBagHasSpace(u16 itemId, u16 count)
{
u8 i;
u16 *items = gSaveBlock2Ptr->frontier.pyramidBag.itemId[gSaveBlock2Ptr->frontier.lvlMode];
#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#else
u8 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#endif

for (i = 0; i < PYRAMID_BAG_ITEMS_COUNT; i++)
{
if (items[i] == itemId || items[i] == ITEM_NONE)
{
if (quantities[i] + count <= MAX_BAG_ITEM_CAPACITY)
if (quantities[i] + count <= MAX_PYRAMID_BAG_ITEM_CAPACITY)
return TRUE;

count = (quantities[i] + count) - MAX_BAG_ITEM_CAPACITY;
count = (quantities[i] + count) - MAX_PYRAMID_BAG_ITEM_CAPACITY;
if (count == 0)
return TRUE;
}
Expand All @@ -735,23 +728,28 @@ bool8 AddPyramidBagItem(u16 itemId, u16 count)
u16 i;

u16 *items = gSaveBlock2Ptr->frontier.pyramidBag.itemId[gSaveBlock2Ptr->frontier.lvlMode];
u8 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];

u16 *newItems = Alloc(PYRAMID_BAG_ITEMS_COUNT * sizeof(*newItems));

#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
u16 *newQuantities = Alloc(PYRAMID_BAG_ITEMS_COUNT * sizeof(*newQuantities));
#else
u8 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
u8 *newQuantities = Alloc(PYRAMID_BAG_ITEMS_COUNT * sizeof(*newQuantities));
#endif

memcpy(newItems, items, PYRAMID_BAG_ITEMS_COUNT * sizeof(*newItems));
memcpy(newQuantities, quantities, PYRAMID_BAG_ITEMS_COUNT * sizeof(*newQuantities));

for (i = 0; i < PYRAMID_BAG_ITEMS_COUNT; i++)
{
if (newItems[i] == itemId && newQuantities[i] < MAX_BAG_ITEM_CAPACITY)
if (newItems[i] == itemId && newQuantities[i] < MAX_PYRAMID_BAG_ITEM_CAPACITY)
{
newQuantities[i] += count;
if (newQuantities[i] > MAX_BAG_ITEM_CAPACITY)
if (newQuantities[i] > MAX_PYRAMID_BAG_ITEM_CAPACITY)
{
count = newQuantities[i] - MAX_BAG_ITEM_CAPACITY;
newQuantities[i] = MAX_BAG_ITEM_CAPACITY;
count = newQuantities[i] - MAX_PYRAMID_BAG_ITEM_CAPACITY;
newQuantities[i] = MAX_PYRAMID_BAG_ITEM_CAPACITY;
}
else
{
Expand All @@ -771,10 +769,10 @@ bool8 AddPyramidBagItem(u16 itemId, u16 count)
{
newItems[i] = itemId;
newQuantities[i] = count;
if (newQuantities[i] > MAX_BAG_ITEM_CAPACITY)
if (newQuantities[i] > MAX_PYRAMID_BAG_ITEM_CAPACITY)
{
count = newQuantities[i] - MAX_BAG_ITEM_CAPACITY;
newQuantities[i] = MAX_BAG_ITEM_CAPACITY;
count = newQuantities[i] - MAX_PYRAMID_BAG_ITEM_CAPACITY;
newQuantities[i] = MAX_PYRAMID_BAG_ITEM_CAPACITY;
}
else
{
Expand Down Expand Up @@ -808,7 +806,11 @@ bool8 RemovePyramidBagItem(u16 itemId, u16 count)
u16 i;

u16 *items = gSaveBlock2Ptr->frontier.pyramidBag.itemId[gSaveBlock2Ptr->frontier.lvlMode];
#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#else
u8 *quantities = gSaveBlock2Ptr->frontier.pyramidBag.quantity[gSaveBlock2Ptr->frontier.lvlMode];
#endif

i = gPyramidBagMenuState.cursorPosition + gPyramidBagMenuState.scrollPosition;
if (items[i] == itemId && quantities[i] >= count)
Expand All @@ -821,7 +823,11 @@ bool8 RemovePyramidBagItem(u16 itemId, u16 count)
else
{
u16 *newItems = Alloc(PYRAMID_BAG_ITEMS_COUNT * sizeof(*newItems));
#if MAX_PYRAMID_BAG_ITEM_CAPACITY > 255
u16 *newQuantities = Alloc(PYRAMID_BAG_ITEMS_COUNT * sizeof(*newQuantities));
#else
u8 *newQuantities = Alloc(PYRAMID_BAG_ITEMS_COUNT * sizeof(*newQuantities));
#endif

memcpy(newItems, items, PYRAMID_BAG_ITEMS_COUNT * sizeof(*newItems));
memcpy(newQuantities, quantities, PYRAMID_BAG_ITEMS_COUNT * sizeof(*newQuantities));
Expand Down
Loading
Loading