From 06b909bcd80e5b3f882273a317ac957cd57f07a5 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Sun, 25 Apr 2021 12:07:08 -0400 Subject: [PATCH] Document player pc --- include/menu_helpers.h | 4 +- include/menu_specialized.h | 20 +- include/player_pc.h | 71 +- src/decoration.c | 2 +- src/international_string_util.c | 4 +- src/item_menu.c | 4 +- src/menu_helpers.c | 30 +- src/menu_specialized.c | 92 +-- src/party_menu.c | 2 +- src/player_pc.c | 1186 ++++++++++++++++--------------- src/secret_base.c | 2 +- 11 files changed, 711 insertions(+), 706 deletions(-) diff --git a/include/menu_helpers.h b/include/menu_helpers.h index c4401354b7..fe223cfb45 100644 --- a/include/menu_helpers.h +++ b/include/menu_helpers.h @@ -32,8 +32,8 @@ bool8 sub_8122148(u16 itemId); bool8 itemid_80BF6D8_mail_related(u16 itemId); bool8 MenuHelpers_LinkSomething(void); bool8 MenuHelpers_CallLinkSomething(void); -void sub_812220C(struct ItemSlot *slots, u8 count, u8 *arg2, u8 *usedSlotsCount, u8 maxUsedSlotsCount); -void sub_812225C(u16 *scrollOffset, u16 *cursorPos, u8 maxShownItems, u8 numItems); +void SetItemListPerPageCount(struct ItemSlot *slots, u8 slotsCount, u8 *pageItems, u8 *totalItems, u8 maxPerPage); +void SetCursorWithinListBounds(u16 *scrollOffset, u16 *cursorPos, u8 maxShownItems, u8 totalItems); void sub_8122298(u16 *arg0, u16 *arg1, u8 arg2, u8 arg3, u8 arg4); void LoadListMenuSwapLineGfx(void); void CreateSwapLineSprites(u8 *spriteIds, u8 count); diff --git a/include/menu_specialized.h b/include/menu_specialized.h index 987fca9fc2..c29110662b 100644 --- a/include/menu_specialized.h +++ b/include/menu_specialized.h @@ -15,6 +15,14 @@ #define MAX_CONDITION_SPARKLES 10 +// Window IDs for the Player PC Mailbox +enum { + MAILBOXWIN_TITLE, + MAILBOXWIN_LIST, + MAILBOXWIN_OPTIONS, + MAILBOXWIN_COUNT +}; + struct UnknownSubStruct_81D1ED4 { u16 unk0; @@ -35,12 +43,12 @@ struct ConditionGraph /*0x355*/ u8 state; }; -bool8 sub_81D1C44(u8 count); -u8 sub_81D1C84(u8 a0); -u8 sub_81D1DC0(struct PlayerPCItemPageStruct *page); -void sub_81D1E90(struct PlayerPCItemPageStruct *page); -void sub_81D1EC0(void); -void sub_81D1D04(u8 a0); +bool8 MailboxMenu_Alloc(u8 count); +u8 MailboxMenu_AddWindow(u8 windowIdx); +u8 MailboxMenu_CreateList(struct PlayerPCItemPageStruct *page); +void MailboxMenu_AddScrollArrows(struct PlayerPCItemPageStruct *page); +void MailboxMenu_Free(void); +void MailboxMenu_RemoveWindow(u8 windowIdx); void InitConditionGraphData(struct ConditionGraph *graph); void sub_81D2108(struct ConditionGraph *graph); void SetConditionGraphIOWindows(u8 bg); diff --git a/include/player_pc.h b/include/player_pc.h index bdf4a4e0ef..2521eac131 100644 --- a/include/player_pc.h +++ b/include/player_pc.h @@ -3,69 +3,6 @@ #include "menu.h" -// local task defines -#define PAGE_INDEX data[0] -#define ITEMS_ABOVE_TOP data[1] -#define NUM_ITEMS data[1] -#define NUM_QUANTITY_ROLLER data[3] -#define NUM_PAGE_ITEMS data[4] -// not used -#define CURRENT_ITEM_STORAGE_MENU data[3] -// not used -#define SWAP_ITEM_INDEX data[8] -#define SWITCH_MODE_ACTIVE data[9] - -// this is potentially an ewram access occuring in high ewram. TODO: investigate this further. -#define NEW_GAME_PC_ITEMS(i, type) ((u16)((u16 *)gNewGamePCItems + type)[i * 2]) - -// defined and used in the above macro -enum -{ - PC_ITEM_ID, - PC_QUANTITY -}; - -// player PC menu options -enum -{ - PLAYERPC_MENU_ITEMSTORAGE, - PLAYERPC_MENU_MAILBOX, - PLAYERPC_MENU_DECORATION, - PLAYERPC_MENU_TURNOFF -}; - -// item storage menus -enum -{ - ITEMPC_MENU_WITHDRAW, - ITEMPC_MENU_DEPOSIT, - ITEMPC_MENU_TOSS, - ITEMPC_MENU_EXIT -}; - -// mailbox mail options -enum -{ - MAILBOX_READ, - MAILBOX_MOVE_TO_BAG, - MAILBOX_GIVE, - MAILBOX_CANCEL, -}; - -// special item description handlers -enum -{ - ITEMPC_SWITCH_WHICH_ITEM = 0xFFF7, - ITEMPC_OKAY_TO_THROW_AWAY, - ITEMPC_TOO_IMPORTANT, - ITEMPC_NO_MORE_ROOM, - ITEMPC_THREW_AWAY_ITEM, - ITEMPC_HOW_MANY_TO_TOSS, - ITEMPC_WITHDREW_THING, - ITEMPC_HOW_MANY_TO_WITHDRAW, - ITEMPC_GO_BACK_TO_PREV -}; - struct PlayerPCItemPageStruct { u16 cursorPos; @@ -76,16 +13,12 @@ struct PlayerPCItemPageStruct u8 scrollIndicatorTaskId; }; -// Exported type declarations +extern struct PlayerPCItemPageStruct gPlayerPCItemPageInfo; -// Exported RAM declarations -extern struct PlayerPCItemPageStruct playerPCItemPageInfo; - -// Exported ROM declarations extern const struct MenuAction gMailboxMailOptions[]; void ReshowPlayerPC(u8 taskId); -void sub_816B31C(void); +void CB2_PlayerPCExitBagMenu(void); void Mailbox_ReturnToMailListAfterDeposit(void); void NewGameInitPCItems(void); diff --git a/src/decoration.c b/src/decoration.c index b9a2437088..c0dd18bfeb 100644 --- a/src/decoration.c +++ b/src/decoration.c @@ -862,7 +862,7 @@ static void InitDecorationItemsMenuLimits(void) static void InitDecorationItemsMenuScrollAndCursor(void) { - sub_812225C(&sDecorationsScrollOffset, &sDecorationsCursorPos, sDecorationItemsMenu->maxShownItems, sDecorationItemsMenu->numMenuItems); + SetCursorWithinListBounds(&sDecorationsScrollOffset, &sDecorationsCursorPos, sDecorationItemsMenu->maxShownItems, sDecorationItemsMenu->numMenuItems); } static void InitDecorationItemsMenuScrollAndCursor2(void) diff --git a/src/international_string_util.c b/src/international_string_util.c index ed1e178161..73d9907e5d 100644 --- a/src/international_string_util.c +++ b/src/international_string_util.c @@ -34,11 +34,11 @@ int GetStringWidthDifference(int fontId, const u8 *str, int totalWidth, int lett return 0; } -int GetMaxWidthInMenuTable(const struct MenuAction *str, int arg1) +int GetMaxWidthInMenuTable(const struct MenuAction *str, int numActions) { int i, var; - for (var = 0, i = 0; i < arg1; i++) + for (var = 0, i = 0; i < numActions; i++) { int stringWidth = GetStringWidth(1, str[i].text, 0); if (stringWidth > var) diff --git a/src/item_menu.c b/src/item_menu.c index 865f8d8d16..4634d03562 100755 --- a/src/item_menu.c +++ b/src/item_menu.c @@ -552,7 +552,7 @@ void CB2_GoToSellMenu(void) void CB2_GoToItemDepositMenu(void) { - GoToBagMenu(ITEMMENULOCATION_ITEMPC, POCKETS_COUNT, sub_816B31C); + GoToBagMenu(ITEMMENULOCATION_ITEMPC, POCKETS_COUNT, CB2_PlayerPCExitBagMenu); } void ApprenticeOpenBagMenu(void) @@ -1078,7 +1078,7 @@ void All_CalculateNItemsAndMaxShowed(void) void SetInitialScrollAndCursorPositions(u8 pocketId) { - sub_812225C(&gBagPositionStruct.scrollPosition[pocketId], &gBagPositionStruct.cursorPosition[pocketId], gBagMenu->numShownItems[pocketId], gBagMenu->numItemStacks[pocketId]); + SetCursorWithinListBounds(&gBagPositionStruct.scrollPosition[pocketId], &gBagPositionStruct.cursorPosition[pocketId], gBagMenu->numShownItems[pocketId], gBagMenu->numItemStacks[pocketId]); } static void SetPocketListPositions(void) diff --git a/src/menu_helpers.c b/src/menu_helpers.c index 5a6ac83947..7da4f939b8 100644 --- a/src/menu_helpers.c +++ b/src/menu_helpers.c @@ -323,36 +323,38 @@ bool8 MenuHelpers_CallLinkSomething(void) return TRUE; } -void sub_812220C(struct ItemSlot *slots, u8 count, u8 *arg2, u8 *usedSlotsCount, u8 maxUsedSlotsCount) +void SetItemListPerPageCount(struct ItemSlot *slots, u8 slotsCount, u8 *pageItems, u8 *totalItems, u8 maxPerPage) { u16 i; struct ItemSlot *slots_ = slots; - (*usedSlotsCount) = 0; - for (i = 0; i < count; i++) + // Count the number of non-empty item slots + *totalItems = 0; + for (i = 0; i < slotsCount; i++) { if (slots_[i].itemId != ITEM_NONE) - (*usedSlotsCount)++; + (*totalItems)++; } + (*totalItems)++; // + 1 for 'Cancel' - (*usedSlotsCount)++; - if ((*usedSlotsCount) > maxUsedSlotsCount) - *arg2 = maxUsedSlotsCount; + // Set number of items per page + if (*totalItems > maxPerPage) + *pageItems = maxPerPage; else - *arg2 = (*usedSlotsCount); + *pageItems = *totalItems; } -void sub_812225C(u16 *scrollOffset, u16 *cursorPos, u8 maxShownItems, u8 numItems) +void SetCursorWithinListBounds(u16 *scrollOffset, u16 *cursorPos, u8 maxShownItems, u8 totalItems) { - if (*scrollOffset != 0 && *scrollOffset + maxShownItems > numItems) - *scrollOffset = numItems - maxShownItems; + if (*scrollOffset != 0 && *scrollOffset + maxShownItems > totalItems) + *scrollOffset = totalItems - maxShownItems; - if (*scrollOffset + *cursorPos >= numItems) + if (*scrollOffset + *cursorPos >= totalItems) { - if (numItems == 0) + if (totalItems == 0) *cursorPos = 0; else - *cursorPos = numItems - 1; + *cursorPos = totalItems - 1; } } diff --git a/src/menu_specialized.c b/src/menu_specialized.c index 4c4be57f04..ab17c64a5e 100644 --- a/src/menu_specialized.c +++ b/src/menu_specialized.c @@ -28,10 +28,10 @@ extern const struct CompressedSpriteSheet gMonFrontPicTable[]; -EWRAM_DATA static u8 sUnknown_0203CF48[3] = {0}; -EWRAM_DATA static struct ListMenuItem *sUnknown_0203CF4C = NULL; +EWRAM_DATA static u8 sMailboxWindowIds[MAILBOXWIN_COUNT] = {0}; +EWRAM_DATA static struct ListMenuItem *sMailboxList = NULL; -static void sub_81D1E7C(s32 itemIndex, bool8 onInit, struct ListMenu *list); +static void MailboxMenu_MoveCursorFunc(s32 itemIndex, bool8 onInit, struct ListMenu *list); static void sub_81D24A4(struct ConditionGraph *a0); static void sub_81D2634(struct ConditionGraph *a0); static void MoveRelearnerCursorCallback(s32 itemIndex, bool8 onInit, struct ListMenu *list); @@ -40,33 +40,33 @@ static void SetNextConditionSparkle(struct Sprite *sprite); static void SpriteCB_ConditionSparkle(struct Sprite *sprite); static void ShowAllConditionSparkles(struct Sprite *sprite); -static const struct WindowTemplate sUnknown_086253E8[] = +static const struct WindowTemplate sWindowTemplates_MailboxMenu[MAILBOXWIN_COUNT] = { - { + [MAILBOXWIN_TITLE] = { .bg = 0, .tilemapLeft = 1, .tilemapTop = 1, .width = 8, .height = 2, - .paletteNum = 0xF, + .paletteNum = 15, .baseBlock = 0x8 }, - { + [MAILBOXWIN_LIST] = { .bg = 0, .tilemapLeft = 21, .tilemapTop = 1, .width = 8, .height = 18, - .paletteNum = 0xF, + .paletteNum = 15, .baseBlock = 0x18 }, - { + [MAILBOXWIN_OPTIONS] = { .bg = 0, .tilemapLeft = 1, .tilemapTop = 1, .width = 11, .height = 8, - .paletteNum = 0xF, + .paletteNum = 15, .baseBlock = 0x18 } }; @@ -207,53 +207,55 @@ static const struct ListMenuTemplate sMoveRelearnerMovesListTemplate = .cursorKind = 0 }; -bool8 sub_81D1C44(u8 count) +bool8 MailboxMenu_Alloc(u8 count) { u8 i; - sUnknown_0203CF4C = Alloc(count * sizeof(*sUnknown_0203CF4C) + sizeof(*sUnknown_0203CF4C)); - if (sUnknown_0203CF4C == NULL) + // + 1 to count for 'Cancel' + sMailboxList = Alloc((count + 1) * sizeof(*sMailboxList)); + if (sMailboxList == NULL) return FALSE; - for (i = 0; i < ARRAY_COUNT(sUnknown_0203CF48); i++) - sUnknown_0203CF48[i] = WINDOW_NONE; + for (i = 0; i < ARRAY_COUNT(sMailboxWindowIds); i++) + sMailboxWindowIds[i] = WINDOW_NONE; return TRUE; } -u8 sub_81D1C84(u8 a0) +u8 MailboxMenu_AddWindow(u8 windowIdx) { - if (sUnknown_0203CF48[a0] == WINDOW_NONE) + if (sMailboxWindowIds[windowIdx] == WINDOW_NONE) { - if (a0 == 2) + if (windowIdx == MAILBOXWIN_OPTIONS) { - struct WindowTemplate template = sUnknown_086253E8[2]; + struct WindowTemplate template = sWindowTemplates_MailboxMenu[windowIdx]; template.width = GetMaxWidthInMenuTable(&gMailboxMailOptions[0], 4); - sUnknown_0203CF48[2] = AddWindow(&template); + sMailboxWindowIds[windowIdx] = AddWindow(&template); } - else + else // MAILBOXWIN_TITLE or MAILBOXWIN_LIST { - sUnknown_0203CF48[a0] = AddWindow(&sUnknown_086253E8[a0]); + sMailboxWindowIds[windowIdx] = AddWindow(&sWindowTemplates_MailboxMenu[windowIdx]); } - SetStandardWindowBorderStyle(sUnknown_0203CF48[a0], 0); + SetStandardWindowBorderStyle(sMailboxWindowIds[windowIdx], 0); } - return sUnknown_0203CF48[a0]; + return sMailboxWindowIds[windowIdx]; } -void sub_81D1D04(u8 a0) +void MailboxMenu_RemoveWindow(u8 windowIdx) { - ClearStdWindowAndFrameToTransparent(sUnknown_0203CF48[a0], 0); - ClearWindowTilemap(sUnknown_0203CF48[a0]); - RemoveWindow(sUnknown_0203CF48[a0]); - sUnknown_0203CF48[a0] = WINDOW_NONE; + ClearStdWindowAndFrameToTransparent(sMailboxWindowIds[windowIdx], 0); + ClearWindowTilemap(sMailboxWindowIds[windowIdx]); + RemoveWindow(sMailboxWindowIds[windowIdx]); + sMailboxWindowIds[windowIdx] = WINDOW_NONE; } -static u8 sub_81D1D34(u8 a0) +// Unused +static u8 MailboxMenu_GetWindowId(u8 windowIdx) { - return sUnknown_0203CF48[a0]; + return sMailboxWindowIds[windowIdx]; } -static void sub_81D1D44(u8 windowId, s32 itemId, u8 y) +static void MailboxMenu_ItemPrintFunc(u8 windowId, s32 itemId, u8 y) { u8 buffer[30]; u16 length; @@ -269,21 +271,21 @@ static void sub_81D1D44(u8 windowId, s32 itemId, u8 y) AddTextPrinterParameterized4(windowId, 1, 8, y, 0, 0, sPlayerNameTextColors, -1, buffer); } -u8 sub_81D1DC0(struct PlayerPCItemPageStruct *page) +u8 MailboxMenu_CreateList(struct PlayerPCItemPageStruct *page) { u16 i; for (i = 0; i < page->count; i++) { - sUnknown_0203CF4C[i].name = sEmptyItemName; - sUnknown_0203CF4C[i].id = i; + sMailboxList[i].name = sEmptyItemName; + sMailboxList[i].id = i; } - sUnknown_0203CF4C[i].name = gText_Cancel2; - sUnknown_0203CF4C[i].id = LIST_CANCEL; + sMailboxList[i].name = gText_Cancel2; + sMailboxList[i].id = LIST_CANCEL; - gMultiuseListMenuTemplate.items = sUnknown_0203CF4C; + gMultiuseListMenuTemplate.items = sMailboxList; gMultiuseListMenuTemplate.totalItems = page->count + 1; - gMultiuseListMenuTemplate.windowId = sUnknown_0203CF48[1]; + gMultiuseListMenuTemplate.windowId = sMailboxWindowIds[MAILBOXWIN_LIST]; gMultiuseListMenuTemplate.header_X = 0; gMultiuseListMenuTemplate.item_X = 8; gMultiuseListMenuTemplate.cursor_X = 0; @@ -292,8 +294,8 @@ u8 sub_81D1DC0(struct PlayerPCItemPageStruct *page) gMultiuseListMenuTemplate.cursorPal = 2; gMultiuseListMenuTemplate.fillValue = 1; gMultiuseListMenuTemplate.cursorShadowPal = 3; - gMultiuseListMenuTemplate.moveCursorFunc = sub_81D1E7C; - gMultiuseListMenuTemplate.itemPrintFunc = sub_81D1D44; + gMultiuseListMenuTemplate.moveCursorFunc = MailboxMenu_MoveCursorFunc; + gMultiuseListMenuTemplate.itemPrintFunc = MailboxMenu_ItemPrintFunc; gMultiuseListMenuTemplate.fontId = 1; gMultiuseListMenuTemplate.cursorKind = 0; gMultiuseListMenuTemplate.lettersSpacing = 0; @@ -302,20 +304,20 @@ u8 sub_81D1DC0(struct PlayerPCItemPageStruct *page) return ListMenuInit(&gMultiuseListMenuTemplate, page->itemsAbove, page->cursorPos); } -static void sub_81D1E7C(s32 itemIndex, bool8 onInit, struct ListMenu *list) +static void MailboxMenu_MoveCursorFunc(s32 itemIndex, bool8 onInit, struct ListMenu *list) { if (onInit != TRUE) PlaySE(SE_SELECT); } -void sub_81D1E90(struct PlayerPCItemPageStruct *page) +void MailboxMenu_AddScrollArrows(struct PlayerPCItemPageStruct *page) { page->scrollIndicatorTaskId = AddScrollIndicatorArrowPairParameterized(2, 0xC8, 12, 0x94, page->count - page->pageItems + 1, 0x6E, 0x6E, &page->itemsAbove); } -void sub_81D1EC0(void) +void MailboxMenu_Free(void) { - Free(sUnknown_0203CF4C); + Free(sMailboxList); } void InitConditionGraphData(struct ConditionGraph *graph) diff --git a/src/party_menu.c b/src/party_menu.c index 81c39949bf..b59567ae87 100755 --- a/src/party_menu.c +++ b/src/party_menu.c @@ -5471,7 +5471,7 @@ static void TryGiveMailToSelectedMon(u8 taskId) struct MailStruct *mail; gPartyMenuUseExitCallback = FALSE; - mail = &gSaveBlock1Ptr->mail[playerPCItemPageInfo.itemsAbove + 6 + playerPCItemPageInfo.cursorPos]; + mail = &gSaveBlock1Ptr->mail[gPlayerPCItemPageInfo.itemsAbove + PARTY_SIZE + gPlayerPCItemPageInfo.cursorPos]; if (GetMonData(mon, MON_DATA_HELD_ITEM) != ITEM_NONE) { DisplayPartyMenuMessage(gText_PkmnHoldingItemCantHoldMail, TRUE); diff --git a/src/player_pc.c b/src/player_pc.c index e5c3c5a184..b880671c18 100644 --- a/src/player_pc.c +++ b/src/player_pc.c @@ -30,24 +30,79 @@ #include "window.h" #include "menu_specialized.h" -// structures -struct Struct203BCC4 -{ - struct ListMenuItem unk0[51]; - u8 unk198[51][0x18]; - u8 windowIds[6]; - u8 unk666; - u8 spriteId; - u8 spriteIds[7]; +// Top level PC menu options +enum { + MENU_ITEMSTORAGE, + MENU_MAILBOX, + MENU_DECORATION, + MENU_TURNOFF +}; + +// Item storage menu options +enum { + MENU_WITHDRAW, + MENU_DEPOSIT, + MENU_TOSS, + MENU_EXIT +}; + +// Windows for the main menus (top level menu, and item storage menu) +enum { + WIN_MAIN_MENU, + WIN_MAIN_MENU_BEDROOM, + WIN_ITEM_STORAGE_MENU, +}; + +// Windows for item storage (while viewing the PC's item inventory) +enum { + ITEMWIN_LIST, + ITEMWIN_MESSAGE, + ITEMWIN_ICON, + ITEMWIN_TITLE, + ITEMWIN_QUANTITY, + ITEMWIN_YESNO, + ITEMWIN_COUNT +}; + // When showing the main list, the first window to this window are drawn +#define ITEMWIN_LIST_END ITEMWIN_TITLE + +// Message IDs for Item Storage +enum { + MSG_SWITCH_WHICH_ITEM = 0xFFF7, + MSG_OKAY_TO_THROW_AWAY, + MSG_TOO_IMPORTANT, + MSG_NO_MORE_ROOM, + MSG_THREW_AWAY_ITEM, + MSG_HOW_MANY_TO_TOSS, + MSG_WITHDREW_ITEM, + MSG_HOW_MANY_TO_WITHDRAW, + MSG_GO_BACK_TO_PREV +}; + +#define TAG_ITEM_ICON 5110 +#define TAG_SCROLL_ARROW 5112 + +// Item list ID for toSwapPos to indicate an item is not currently being swapped +#define NOT_SWAPPING 0xFF + +#define SWAP_LINE_LENGTH 7 + +struct ItemStorageMenu +{ + struct ListMenuItem listItems[PC_ITEMS_COUNT + 1]; + u8 itemNames[PC_ITEMS_COUNT + 1][ITEM_NAME_LENGTH + 10]; + u8 windowIds[ITEMWIN_COUNT]; + u8 toSwapPos; + u8 spriteId; + u8 swapLineSpriteIds[SWAP_LINE_LENGTH]; }; -// static functions static void InitPlayerPCMenu(u8 taskId); static void PlayerPCProcessMenuInput(u8 taskId); static void InitItemStorageMenu(u8 taskId, u8 var); static u8 GetMailboxMailCount(void); -static void Mailbox_UpdateMailList(void); +static void Mailbox_CompactMailList(void); static void Mailbox_DrawMailboxMenu(u8 taskId); static void Mailbox_ProcessInput(u8 taskId); static void Mailbox_PrintWhatToDoWithPlayerMailText(u8 taskId); @@ -67,15 +122,14 @@ static void Mailbox_Give(u8 taskId); static void Mailbox_Cancel(u8 taskId); static void Mailbox_CancelMoveToBag(u8 taskId); -static void Mailbox_MoveToBagYesNoPrompt(u8 taskId); -static void Mailbox_DrawYesNoBeforeMove(u8 taskId); +static void Mailbox_HandleConfirmMoveToBag(u8 taskId); +static void Mailbox_AskConfirmMoveToBag(u8 taskId); static void Mailbox_DoGiveMailPokeMenu(u8 taskId); static void Mailbox_NoPokemonForMail(u8 taskId); static void Mailbox_FadeAndReadMail(u8 taskId); static void Mailbox_ReturnToFieldFromReadMail(void); -static void Mailbox_DoRedrawMailboxMenuAfterReturn(void); -static void pal_fill_for_maplights_or_black(void); +static void Mailbox_ReshowAfterMail(void); static void Mailbox_HandleReturnToProcessInput(u8 taskId); static void Mailbox_UpdateMailListAfterDeposit(void); @@ -83,91 +137,92 @@ static void ItemStorage_Withdraw(u8 taskId); static void ItemStorage_Deposit(u8 taskId); static void ItemStorage_Toss(u8 taskId); static void ItemStorage_Exit(u8 taskId); -static void ItemStorage_ResumeInputFromYesToss(u8 taskId); -static void ItemStorage_ResumeInputFromNoToss(u8 taskId); +static void ItemStorage_TossItemYes(u8 taskId); +static void ItemStorage_TossItemNo(u8 taskId); static void ItemStorageMenuPrint(const u8 *); static void ItemStorageMenuProcessInput(u8 taskId); -static void ItemStorage_ProcessWithdrawTossInput(u8 taskId); -static void ItemStorage_SetItemAndMailCount(u8); +static void SetPlayerPCListCount(u8); static void ItemStorage_HandleReturnToProcessInput(u8 taskId); -static void ItemStorage_WithdrawToss_Helper(u8 taskId, bool8 toss); +static void ItemStorage_Enter(u8 taskId, bool8 toss); +static void ItemStorage_CreateListMenu(u8 taskId); +static void ItemStorage_ProcessInput(u8 taskId); static void Task_ItemStorage_Deposit(u8 taskId); +static void ItemStorage_ReshowAfterBagMenu(void); static void ItemStorage_DoItemWithdraw(u8 taskId); static void ItemStorage_DoItemToss(u8 taskid); static void ItemStorage_HandleQuantityRolling(u8 taskid); -static void ItemStorage_GoBackToPlayerPCMenu(u8 taskId); -static void ItemStorage_ItemSwapChoosePrompt(u8 taskId); +static void ItemStorage_ExitItemList(u8 taskId); +static void ItemStorage_StartItemSwap(u8 taskId); static void ItemStorage_DoItemAction(u8 taskId); -static void ItemStorage_ProcessInput(u8 taskId); -static void ItemStorage_DoItemSwap(u8 taskId, bool8 a); +static void ItemStorage_FinishItemSwap(u8 taskId, bool8 a); static void ItemStorage_HandleRemoveItem(u8 taskId); -static void ItemStorage_WaitPressHandleResumeProcessInput(u8 taskId); -static void ItemStorage_StartScrollIndicatorAndProcessInput(u8 taskId); +static void ItemStorage_HandleErrorMessageInput(u8 taskId); +static void ItemStorage_ReturnToListInput(u8 taskId); -static const u8* ItemStorage_GetItemPcResponse(u16); +static const u8* ItemStorage_GetMessage(u16); static void CopyItemName_PlayerPC(u8 *string, u16 itemId); -static void sub_816BC14(void); -static void sub_816BFE0(u8 y, u8, u8 speed); -static void sub_816BCC4(u8); -static void UpdateSwapLinePos(u8); -static void sub_816C4FC(u8 taskId); -static void sub_816C0C8(void); -static void sub_816C060(u16 itemId); -static void sub_816BEF0(s32 id); -static void sub_816B4DC(u8 taskId); +static void ItemStorage_Init(void); +static void ItemStorage_DrawSwapArrow(u8 y, u8, u8 speed); +static void ItemStorage_RemoveWindow(u8); +static void ItemStorage_UpdateSwapLinePos(u8); +static void ItemStorage_ProcessItemSwapInput(u8 taskId); +static void ItemStorage_EraseItemIcon(void); +static void ItemStorage_DrawItemIcon(u16 itemId); +static void ItemStorage_PrintDescription(s32 id); +static void ItemStorage_EraseMainMenu(u8 taskId); static void ItemStorage_MoveCursor(s32 id, bool8 b, struct ListMenu * thisMenu); -static void fish4_goto_x5_or_x6(u8 windowId, s32 id, u8 yOffset); +static void ItemStorage_PrintMenuItem(u8 windowId, s32 id, u8 yOffset); -// EWRAM -static EWRAM_DATA const u8 *gPcItemMenuOptionOrder = NULL; -static EWRAM_DATA u8 gPcItemMenuOptionsNum = 0; -EWRAM_DATA struct PlayerPCItemPageStruct playerPCItemPageInfo = {0, 0, 0, 0, {0, 0, 0}, 0}; -static EWRAM_DATA struct Struct203BCC4 *gUnknown_0203BCC4 = NULL; +static EWRAM_DATA const u8 *sTopMenuOptionOrder = NULL; +static EWRAM_DATA u8 sTopMenuNumOptions = 0; +EWRAM_DATA struct PlayerPCItemPageStruct gPlayerPCItemPageInfo = {}; +static EWRAM_DATA struct ItemStorageMenu *sItemStorageMenu = NULL; -// .rodata -static const u8 *const gPCText_OptionDescList[] = +static const u8 *const sItemStorage_OptionDescriptions[] = { - gText_TakeOutItemsFromPC, - gText_StoreItemsInPC, - gText_ThrowAwayItemsInPC, - gText_GoBackPrevMenu, + [MENU_WITHDRAW] = gText_TakeOutItemsFromPC, + [MENU_DEPOSIT] = gText_StoreItemsInPC, + [MENU_TOSS] = gText_ThrowAwayItemsInPC, + [MENU_EXIT] = gText_GoBackPrevMenu, }; static const struct MenuAction sPlayerPCMenuActions[] = { - { gText_ItemStorage, PlayerPC_ItemStorage }, - { gText_Mailbox, PlayerPC_Mailbox }, - { gText_Decoration, PlayerPC_Decoration }, - { gText_TurnOff, PlayerPC_TurnOff } + [MENU_ITEMSTORAGE] = { gText_ItemStorage, PlayerPC_ItemStorage }, + [MENU_MAILBOX] = { gText_Mailbox, PlayerPC_Mailbox }, + [MENU_DECORATION] = { gText_Decoration, PlayerPC_Decoration }, + [MENU_TURNOFF] = { gText_TurnOff, PlayerPC_TurnOff } }; -static const u8 gBedroomPC_OptionOrder[] = +static const u8 sBedroomPC_OptionOrder[] = { - PLAYERPC_MENU_ITEMSTORAGE, - PLAYERPC_MENU_MAILBOX, - PLAYERPC_MENU_DECORATION, - PLAYERPC_MENU_TURNOFF + MENU_ITEMSTORAGE, + MENU_MAILBOX, + MENU_DECORATION, + MENU_TURNOFF }; +#define NUM_BEDROOM_PC_OPTIONS ARRAY_COUNT(sBedroomPC_OptionOrder) -static const u8 gPlayerPC_OptionOrder[] = +static const u8 sPlayerPC_OptionOrder[] = { - PLAYERPC_MENU_ITEMSTORAGE, - PLAYERPC_MENU_MAILBOX, - PLAYERPC_MENU_TURNOFF + MENU_ITEMSTORAGE, + MENU_MAILBOX, + MENU_TURNOFF }; +#define NUM_PLAYER_PC_OPTIONS ARRAY_COUNT(sPlayerPC_OptionOrder) -static const struct MenuAction gPCText_ItemPCOptionsText[] = +static const struct MenuAction sItemStorage_MenuActions[] = { - { gText_WithdrawItem, ItemStorage_Withdraw }, - { gText_DepositItem, ItemStorage_Deposit }, - { gText_TossItem, ItemStorage_Toss }, - { gText_Cancel, ItemStorage_Exit } + [MENU_WITHDRAW] = { gText_WithdrawItem, ItemStorage_Withdraw }, + [MENU_DEPOSIT] = { gText_DepositItem, ItemStorage_Deposit }, + [MENU_TOSS] = { gText_TossItem, ItemStorage_Toss }, + [MENU_EXIT] = { gText_Cancel, ItemStorage_Exit } }; -static const struct ItemSlot gNewGamePCItems[] = +static const struct ItemSlot sNewGamePCItems[] = { { ITEM_POTION, 1 }, { ITEM_NONE, 0 } @@ -175,15 +230,15 @@ static const struct ItemSlot gNewGamePCItems[] = const struct MenuAction gMailboxMailOptions[] = { - { gText_Read, Mailbox_DoMailRead }, + { gText_Read, Mailbox_DoMailRead }, { gText_MoveToBag, Mailbox_MoveToBag }, - { gText_Give2, Mailbox_Give }, - { gText_Cancel2, Mailbox_Cancel } + { gText_Give2, Mailbox_Give }, + { gText_Cancel2, Mailbox_Cancel } }; -static const struct WindowTemplate gUnknown_085DFF24[3] = +static const struct WindowTemplate sWindowTemplates_MainMenus[] = { - { + [WIN_MAIN_MENU] = { .bg = 0, .tilemapLeft = 1, .tilemapTop = 1, @@ -192,7 +247,7 @@ static const struct WindowTemplate gUnknown_085DFF24[3] = .paletteNum = 15, .baseBlock = 1 }, - { + [WIN_MAIN_MENU_BEDROOM] = { .bg = 0, .tilemapLeft = 1, .tilemapTop = 1, @@ -201,7 +256,7 @@ static const struct WindowTemplate gUnknown_085DFF24[3] = .paletteNum = 15, .baseBlock = 1 }, - { + [WIN_ITEM_STORAGE_MENU] = { .bg = 0, .tilemapLeft = 1, .tilemapTop = 1, @@ -212,17 +267,17 @@ static const struct WindowTemplate gUnknown_085DFF24[3] = } }; -static const struct YesNoFuncTable ResumeFromWithdrawYesNoFuncList = // ResumeFromWithdrawYesNoFuncList +static const struct YesNoFuncTable ItemTossYesNoFuncs = { - ItemStorage_ResumeInputFromYesToss, - ItemStorage_ResumeInputFromNoToss + ItemStorage_TossItemYes, + ItemStorage_TossItemNo }; -static const struct ListMenuTemplate gUnknown_085DFF44 = +static const struct ListMenuTemplate sListMenuTemplate_ItemStorage = { .items = NULL, .moveCursorFunc = ItemStorage_MoveCursor, - .itemPrintFunc = fish4_goto_x5_or_x6, + .itemPrintFunc = ItemStorage_PrintMenuItem, .totalItems = 0, .maxShowed = 0, .windowId = 0, @@ -239,9 +294,9 @@ static const struct ListMenuTemplate gUnknown_085DFF44 = .fontId = 7 }; -static const struct WindowTemplate gUnknown_085DFF5C[5] = +static const struct WindowTemplate sWindowTemplates_ItemStorage[ITEMWIN_COUNT] = { - { + [ITEMWIN_LIST] = { .bg = 0, .tilemapLeft = 16, .tilemapTop = 1, @@ -250,7 +305,7 @@ static const struct WindowTemplate gUnknown_085DFF5C[5] = .paletteNum = 15, .baseBlock = 0x0001 }, - { + [ITEMWIN_MESSAGE] = { .bg = 0, .tilemapLeft = 1, .tilemapTop = 13, @@ -259,7 +314,7 @@ static const struct WindowTemplate gUnknown_085DFF5C[5] = .paletteNum = 15, .baseBlock = 0x00EB }, - { + [ITEMWIN_ICON] = { .bg = 0, .tilemapLeft = 1, .tilemapTop = 8, @@ -268,7 +323,7 @@ static const struct WindowTemplate gUnknown_085DFF5C[5] = .paletteNum = 15, .baseBlock = 0x0153 }, - { + [ITEMWIN_TITLE] = { .bg = 0, .tilemapLeft = 1, .tilemapTop = 1, @@ -277,7 +332,7 @@ static const struct WindowTemplate gUnknown_085DFF5C[5] = .paletteNum = 15, .baseBlock = 0x0139 }, - { + [ITEMWIN_QUANTITY] = { .bg = 0, .tilemapLeft = 8, .tilemapTop = 9, @@ -285,61 +340,67 @@ static const struct WindowTemplate gUnknown_085DFF5C[5] = .height = 2, .paletteNum = 15, .baseBlock = 0x015C + }, + [ITEMWIN_YESNO] = { + .bg = 0, + .tilemapLeft = 9, + .tilemapTop = 7, + .width = 5, + .height = 4, + .paletteNum = 15, + .baseBlock = 0x0168 } }; -static const struct WindowTemplate gUnknown_085DFF84 = -{ - .bg = 0, - .tilemapLeft = 9, - .tilemapTop = 7, - .width = 5, - .height = 4, - .paletteNum = 15, - .baseBlock = 0x0168 -}; +static const u8 sSwapArrowTextColors[] = {TEXT_COLOR_WHITE, TEXT_COLOR_LIGHT_GRAY, TEXT_COLOR_DARK_GRAY}; -static const u8 gUnknown_085DFF8C[] = {0x01, 0x03, 0x02, 0x00}; - -// text +// Macro below is likely a fakematch, equivalent to sNewGamePCItems[i].quantity +#define GET_QUANTITY(i) ((u16)((u16 *)sNewGamePCItems + 1)[i * 2]) void NewGameInitPCItems(void) { - u8 i; - - // because Game Freak don't know how to use a struct or a 2d array - for(i = 0, ClearItemSlots(gSaveBlock1Ptr->pcItems, ARRAY_COUNT(gSaveBlock1Ptr->pcItems)); NEW_GAME_PC_ITEMS(i, PC_ITEM_ID) && NEW_GAME_PC_ITEMS(i, PC_QUANTITY) && - AddPCItem(NEW_GAME_PC_ITEMS(i, PC_ITEM_ID), NEW_GAME_PC_ITEMS(i, PC_QUANTITY)) == TRUE; i++); + u8 i = 0; + ClearItemSlots(gSaveBlock1Ptr->pcItems, PC_ITEMS_COUNT); + for(; sNewGamePCItems[i].itemId != ITEM_NONE && GET_QUANTITY(i) && + AddPCItem(sNewGamePCItems[i].itemId, GET_QUANTITY(i)) == TRUE; i++); } +#undef GET_QUANTITY void BedroomPC(void) { - gPcItemMenuOptionOrder = gBedroomPC_OptionOrder; - gPcItemMenuOptionsNum = 4; + sTopMenuOptionOrder = sBedroomPC_OptionOrder; + sTopMenuNumOptions = NUM_BEDROOM_PC_OPTIONS; DisplayItemMessageOnField(CreateTask(TaskDummy, 0), gText_WhatWouldYouLike, InitPlayerPCMenu); } void PlayerPC(void) { - gPcItemMenuOptionOrder = gPlayerPC_OptionOrder; - gPcItemMenuOptionsNum = 3; + sTopMenuOptionOrder = sPlayerPC_OptionOrder; + sTopMenuNumOptions = NUM_PLAYER_PC_OPTIONS; DisplayItemMessageOnField(CreateTask(TaskDummy, 0), gText_WhatWouldYouLike, InitPlayerPCMenu); } +#define tUsedSlots data[1] +#define tQuantity data[2] +#define tInTossMenu data[3] +#define tWindowId data[4] +#define tListTaskId data[5] + static void InitPlayerPCMenu(u8 taskId) { u16 *data; struct WindowTemplate windowTemplate; - data = gTasks[taskId].data; - if (gPcItemMenuOptionsNum == 3) - windowTemplate = gUnknown_085DFF24[0]; - else - windowTemplate = gUnknown_085DFF24[1]; - windowTemplate.width = sub_81DB3D8(sPlayerPCMenuActions, gPcItemMenuOptionOrder, gPcItemMenuOptionsNum); - data[4] = AddWindow(&windowTemplate); - SetStandardWindowBorderStyle(data[4], 0); - sub_81995E4(data[4], gPcItemMenuOptionsNum, sPlayerPCMenuActions, gPcItemMenuOptionOrder); - InitMenuInUpperLeftCornerPlaySoundWhenAPressed(data[4], gPcItemMenuOptionsNum, 0); + + if (sTopMenuNumOptions == NUM_PLAYER_PC_OPTIONS) + windowTemplate = sWindowTemplates_MainMenus[WIN_MAIN_MENU]; + else // Bedroom PC + windowTemplate = sWindowTemplates_MainMenus[WIN_MAIN_MENU_BEDROOM]; + + windowTemplate.width = sub_81DB3D8(sPlayerPCMenuActions, sTopMenuOptionOrder, sTopMenuNumOptions); + tWindowId = AddWindow(&windowTemplate); + SetStandardWindowBorderStyle(tWindowId, 0); + sub_81995E4(tWindowId, sTopMenuNumOptions, sPlayerPCMenuActions, sTopMenuOptionOrder); + InitMenuInUpperLeftCornerPlaySoundWhenAPressed(tWindowId, sTopMenuNumOptions, 0); ScheduleBgCopyTilemapToVram(0); gTasks[taskId].func = PlayerPCProcessMenuInput; } @@ -350,30 +411,30 @@ static void PlayerPCProcessMenuInput(u8 taskId) s8 inputOptionId; data = gTasks[taskId].data; - if (gPcItemMenuOptionsNum > 3) + if (sTopMenuNumOptions > 3) inputOptionId = Menu_ProcessInput(); else inputOptionId = Menu_ProcessInputNoWrap(); switch (inputOptionId) { - case MENU_NOTHING_CHOSEN: - break; - case MENU_B_PRESSED: - PlaySE(SE_SELECT); - ClearStdWindowAndFrameToTransparent(data[4], FALSE); - ClearWindowTilemap(data[4]); - RemoveWindow(data[4]); - ScheduleBgCopyTilemapToVram(0); - gTasks[taskId].func = PlayerPC_TurnOff; - break; - default: - ClearStdWindowAndFrameToTransparent(data[4], FALSE); - ClearWindowTilemap(data[4]); - RemoveWindow(data[4]); - ScheduleBgCopyTilemapToVram(0); - gTasks[taskId].func = sPlayerPCMenuActions[gPcItemMenuOptionOrder[inputOptionId]].func.void_u8; - break; + case MENU_NOTHING_CHOSEN: + break; + case MENU_B_PRESSED: + PlaySE(SE_SELECT); + ClearStdWindowAndFrameToTransparent(tWindowId, FALSE); + ClearWindowTilemap(tWindowId); + RemoveWindow(tWindowId); + ScheduleBgCopyTilemapToVram(0); + gTasks[taskId].func = PlayerPC_TurnOff; + break; + default: + ClearStdWindowAndFrameToTransparent(tWindowId, FALSE); + ClearWindowTilemap(tWindowId); + RemoveWindow(tWindowId); + ScheduleBgCopyTilemapToVram(0); + gTasks[taskId].func = sPlayerPCMenuActions[sTopMenuOptionOrder[inputOptionId]].func.void_u8; + break; } } @@ -384,31 +445,37 @@ void ReshowPlayerPC(u8 var) static void PlayerPC_ItemStorage(u8 taskId) { - InitItemStorageMenu(taskId, ITEMPC_MENU_WITHDRAW); + InitItemStorageMenu(taskId, MENU_WITHDRAW); gTasks[taskId].func = ItemStorageMenuProcessInput; } static void PlayerPC_Mailbox(u8 taskId) { - playerPCItemPageInfo.count = GetMailboxMailCount(); + gPlayerPCItemPageInfo.count = GetMailboxMailCount(); - if (playerPCItemPageInfo.count == 0) + if (gPlayerPCItemPageInfo.count == 0) + { + // Mailbox cannot be opened if no mail is in PC DisplayItemMessageOnField(taskId, gText_NoMailHere, ReshowPlayerPC); + } else { - playerPCItemPageInfo.cursorPos = 0; - playerPCItemPageInfo.itemsAbove = 0; - playerPCItemPageInfo.scrollIndicatorTaskId = TASK_NONE; - Mailbox_UpdateMailList(); - ItemStorage_SetItemAndMailCount(taskId); - if (sub_81D1C44(playerPCItemPageInfo.count) == TRUE) + gPlayerPCItemPageInfo.cursorPos = 0; + gPlayerPCItemPageInfo.itemsAbove = 0; + gPlayerPCItemPageInfo.scrollIndicatorTaskId = TASK_NONE; + Mailbox_CompactMailList(); + SetPlayerPCListCount(taskId); + if (MailboxMenu_Alloc(gPlayerPCItemPageInfo.count) == TRUE) { ClearDialogWindowAndFrame(0, 0); Mailbox_DrawMailboxMenu(taskId); gTasks[taskId].func = Mailbox_ProcessInput; } - else + else + { + // Alloc failed, exit Mailbox DisplayItemMessageOnField(taskId, gText_NoMailHere, ReshowPlayerPC); + } } } @@ -419,7 +486,7 @@ static void PlayerPC_Decoration(u8 taskId) static void PlayerPC_TurnOff(u8 taskId) { - if (gPcItemMenuOptionsNum == 4) // if the option count is 4, we are at the bedroom PC, so do gender specific handling. + if (sTopMenuNumOptions == NUM_BEDROOM_PC_OPTIONS) // Flimsy way to determine if Bedroom PC is in use { if (gSaveBlock2Ptr->playerGender == MALE) ScriptContext1_SetupScript(LittlerootTown_BrendansHouse_2F_EventScript_TurnOffPlayerPC); @@ -439,14 +506,14 @@ static void InitItemStorageMenu(u8 taskId, u8 var) struct WindowTemplate windowTemplate; data = gTasks[taskId].data; - windowTemplate = gUnknown_085DFF24[2]; - windowTemplate.width = GetMaxWidthInMenuTable(gPCText_ItemPCOptionsText, 4); - data[4] = AddWindow(&windowTemplate); - SetStandardWindowBorderStyle(data[4], 0); - PrintMenuTable(data[4], ARRAY_COUNT(gPCText_ItemPCOptionsText), gPCText_ItemPCOptionsText); - InitMenuInUpperLeftCornerPlaySoundWhenAPressed(data[4], 4, var); + windowTemplate = sWindowTemplates_MainMenus[WIN_ITEM_STORAGE_MENU]; + windowTemplate.width = GetMaxWidthInMenuTable(sItemStorage_MenuActions, ARRAY_COUNT(sItemStorage_MenuActions)); + tWindowId = AddWindow(&windowTemplate); + SetStandardWindowBorderStyle(tWindowId, 0); + PrintMenuTable(tWindowId, ARRAY_COUNT(sItemStorage_MenuActions), sItemStorage_MenuActions); + InitMenuInUpperLeftCornerPlaySoundWhenAPressed(tWindowId, 4, var); ScheduleBgCopyTilemapToVram(0); - ItemStorageMenuPrint(gPCText_OptionDescList[var]); + ItemStorageMenuPrint(sItemStorage_OptionDescriptions[var]); } static void ItemStorageMenuPrint(const u8 *textPtr) @@ -457,27 +524,26 @@ static void ItemStorageMenuPrint(const u8 *textPtr) static void ItemStorageMenuProcessInput(u8 taskId) { - s8 r5; - s8 r2; + s8 oldPos, newPos; s8 inputOptionId; - r5 = Menu_GetCursorPos(); + oldPos = Menu_GetCursorPos(); inputOptionId = Menu_ProcessInput(); - r2 = Menu_GetCursorPos(); + newPos = Menu_GetCursorPos(); switch (inputOptionId) { - case MENU_NOTHING_CHOSEN: - if (r5 != r2) - ItemStorageMenuPrint(gPCText_OptionDescList[r2]); - break; - case MENU_B_PRESSED: - PlaySE(SE_SELECT); - ItemStorage_Exit(taskId); - break; - default: - PlaySE(SE_SELECT); - gPCText_ItemPCOptionsText[inputOptionId].func.void_u8(taskId); - break; + case MENU_NOTHING_CHOSEN: + if (oldPos != newPos) + ItemStorageMenuPrint(sItemStorage_OptionDescriptions[newPos]); + break; + case MENU_B_PRESSED: + PlaySE(SE_SELECT); + ItemStorage_Exit(taskId); + break; + default: + PlaySE(SE_SELECT); + sItemStorage_MenuActions[inputOptionId].func.void_u8(taskId); + break; } } @@ -497,13 +563,13 @@ static void Task_ItemStorage_Deposit(u8 taskId) } } -void sub_816B31C(void) +void CB2_PlayerPCExitBagMenu(void) { - gFieldCallback = Mailbox_DoRedrawMailboxMenuAfterReturn; + gFieldCallback = ItemStorage_ReshowAfterBagMenu; SetMainCallback2(CB2_ReturnToField); } -void Mailbox_DoRedrawMailboxMenuAfterReturn(void) +static void ItemStorage_ReshowAfterBagMenu(void) { LoadMessageBoxAndBorderGfx(); DrawDialogueFrame(0, 1); @@ -521,12 +587,15 @@ static void ItemStorage_Withdraw(u8 taskId) { s16 *data = gTasks[taskId].data; - NUM_ITEMS = CountUsedPCItemSlots(); - if (NUM_ITEMS != 0) - ItemStorage_WithdrawToss_Helper(taskId, FALSE); + tUsedSlots = CountUsedPCItemSlots(); + if (tUsedSlots != 0) + { + ItemStorage_Enter(taskId, FALSE); + } else { - sub_816B4DC(taskId); + // Can't withdraw, no items in PC + ItemStorage_EraseMainMenu(taskId); DisplayItemMessageOnField(taskId, gText_NoItems, PlayerPC_ItemStorage); } @@ -536,136 +605,133 @@ static void ItemStorage_Toss(u8 taskId) { s16 *data = gTasks[taskId].data; - NUM_ITEMS = CountUsedPCItemSlots(); - if (NUM_ITEMS != 0) - ItemStorage_WithdrawToss_Helper(taskId, TRUE); + tUsedSlots = CountUsedPCItemSlots(); + if (tUsedSlots != 0) + { + ItemStorage_Enter(taskId, TRUE); + } else { - sub_816B4DC(taskId); + // Can't toss, no items in PC + ItemStorage_EraseMainMenu(taskId); DisplayItemMessageOnField(taskId, gText_NoItems, PlayerPC_ItemStorage); } } -static void ItemStorage_WithdrawToss_Helper(u8 taskId, bool8 toss) +static void ItemStorage_Enter(u8 taskId, bool8 toss) { u16 *data = gTasks[taskId].data; - data[3] = toss; - sub_816B4DC(taskId); - playerPCItemPageInfo.cursorPos = 0; - playerPCItemPageInfo.itemsAbove = 0; - playerPCItemPageInfo.scrollIndicatorTaskId = TASK_NONE; - ItemStorage_SetItemAndMailCount(taskId); - sub_816BC14(); + tInTossMenu = toss; + ItemStorage_EraseMainMenu(taskId); + gPlayerPCItemPageInfo.cursorPos = 0; + gPlayerPCItemPageInfo.itemsAbove = 0; + gPlayerPCItemPageInfo.scrollIndicatorTaskId = TASK_NONE; + SetPlayerPCListCount(taskId); + ItemStorage_Init(); FreeAndReserveObjectSpritePalettes(); LoadListMenuSwapLineGfx(); - CreateSwapLineSprites(gUnknown_0203BCC4->spriteIds, 7); + CreateSwapLineSprites(sItemStorageMenu->swapLineSpriteIds, SWAP_LINE_LENGTH); ClearDialogWindowAndFrame(0,0); - gTasks[taskId].func = ItemStorage_ProcessWithdrawTossInput; + gTasks[taskId].func = ItemStorage_CreateListMenu; } static void ItemStorage_Exit(u8 taskId) { - sub_816B4DC(taskId); + ItemStorage_EraseMainMenu(taskId); ReshowPlayerPC(taskId); } - -static void ItemStorage_SetItemAndMailCount(u8 taskId) +// Used by Item Storage and the Mailbox +static void SetPlayerPCListCount(u8 taskId) { - if (playerPCItemPageInfo.count > 7) - playerPCItemPageInfo.pageItems = 8; + if (gPlayerPCItemPageInfo.count > 7) + gPlayerPCItemPageInfo.pageItems = 8; else - playerPCItemPageInfo.pageItems = playerPCItemPageInfo.count + 1; + gPlayerPCItemPageInfo.pageItems = gPlayerPCItemPageInfo.count + 1; } -static void sub_816B4DC(u8 taskId) +static void ItemStorage_EraseMainMenu(u8 taskId) { u16 *data = gTasks[taskId].data; - - ClearStdWindowAndFrameToTransparent(data[4], FALSE); - ClearWindowTilemap(data[4]); - RemoveWindow(data[4]); + ClearStdWindowAndFrameToTransparent(tWindowId, FALSE); + ClearWindowTilemap(tWindowId); + RemoveWindow(tWindowId); ScheduleBgCopyTilemapToVram(0); } static u8 GetMailboxMailCount(void) { - u8 i, j; + u8 mailInPC, i; - for(i = 0, j = PARTY_SIZE; j < MAIL_COUNT; j++) - if (gSaveBlock1Ptr->mail[j].itemId != ITEM_NONE) - i++; + // Count mail in PC (by first skipping over mail in party) + for (mailInPC = 0, i = PARTY_SIZE; i < MAIL_COUNT; i++) + if (gSaveBlock1Ptr->mail[i].itemId != ITEM_NONE) + mailInPC++; - return i; + return mailInPC; } -static void Mailbox_UpdateMailList(void) +static void Mailbox_CompactMailList(void) { - struct MailStruct mailBuffer; + struct MailStruct temp; u8 i, j; for (i = PARTY_SIZE; i < MAIL_COUNT - 1; i++) { for (j = i + 1; j < MAIL_COUNT; j++) { - if (gSaveBlock1Ptr->mail[i].itemId == 0) - { - mailBuffer = gSaveBlock1Ptr->mail[i]; - gSaveBlock1Ptr->mail[i] = gSaveBlock1Ptr->mail[j]; - gSaveBlock1Ptr->mail[j] = mailBuffer; - } + if (gSaveBlock1Ptr->mail[i].itemId == ITEM_NONE) + SWAP(gSaveBlock1Ptr->mail[i], gSaveBlock1Ptr->mail[j], temp); } } } static void Mailbox_DrawMailboxMenu(u8 taskId) { - u8 windowId; - - windowId = sub_81D1C84(0); - sub_81D1C84(1); + u8 windowId = MailboxMenu_AddWindow(MAILBOXWIN_TITLE); + MailboxMenu_AddWindow(MAILBOXWIN_LIST); AddTextPrinterParameterized(windowId, 1, gText_Mailbox, GetStringCenterAlignXOffset(1, gText_Mailbox, 0x40), 1, 0, NULL); ScheduleBgCopyTilemapToVram(0); - gTasks[taskId].data[5] = sub_81D1DC0(&playerPCItemPageInfo); - sub_81D1E90(&playerPCItemPageInfo); + gTasks[taskId].tListTaskId = MailboxMenu_CreateList(&gPlayerPCItemPageInfo); + MailboxMenu_AddScrollArrows(&gPlayerPCItemPageInfo); } static void Mailbox_ProcessInput(u8 taskId) { u16 *data = gTasks[taskId].data; - s32 inputOptionId; if (!gPaletteFade.active) { - inputOptionId = ListMenu_ProcessInput(data[5]); - ListMenuGetScrollAndRow(data[5], &(playerPCItemPageInfo.itemsAbove), &(playerPCItemPageInfo.cursorPos)); + s32 inputOptionId = ListMenu_ProcessInput(tListTaskId); + ListMenuGetScrollAndRow(tListTaskId, &gPlayerPCItemPageInfo.itemsAbove, &gPlayerPCItemPageInfo.cursorPos); switch (inputOptionId) { - case LIST_NOTHING_CHOSEN: - break; - case LIST_CANCEL: - PlaySE(SE_SELECT); - RemoveScrollIndicatorArrowPair(playerPCItemPageInfo.scrollIndicatorTaskId); - Mailbox_ReturnToPlayerPC(taskId); - break; - default: - PlaySE(SE_SELECT); - sub_81D1D04(0); - sub_81D1D04(1); - DestroyListMenuTask(data[5], &(playerPCItemPageInfo.itemsAbove), &(playerPCItemPageInfo.cursorPos)); - ScheduleBgCopyTilemapToVram(0); - RemoveScrollIndicatorArrowPair(playerPCItemPageInfo.scrollIndicatorTaskId); - gTasks[taskId].func = Mailbox_PrintWhatToDoWithPlayerMailText; - break; + case LIST_NOTHING_CHOSEN: + break; + case LIST_CANCEL: + PlaySE(SE_SELECT); + RemoveScrollIndicatorArrowPair(gPlayerPCItemPageInfo.scrollIndicatorTaskId); + Mailbox_ReturnToPlayerPC(taskId); + break; + default: + // Selected mail, ask what to do with it + PlaySE(SE_SELECT); + MailboxMenu_RemoveWindow(MAILBOXWIN_TITLE); + MailboxMenu_RemoveWindow(MAILBOXWIN_LIST); + DestroyListMenuTask(tListTaskId, &gPlayerPCItemPageInfo.itemsAbove, &gPlayerPCItemPageInfo.cursorPos); + ScheduleBgCopyTilemapToVram(0); + RemoveScrollIndicatorArrowPair(gPlayerPCItemPageInfo.scrollIndicatorTaskId); + gTasks[taskId].func = Mailbox_PrintWhatToDoWithPlayerMailText; + break; } } } static void Mailbox_PrintWhatToDoWithPlayerMailText(u8 taskId) { - StringCopy(gStringVar1, gSaveBlock1Ptr->mail[playerPCItemPageInfo.itemsAbove + PARTY_SIZE + playerPCItemPageInfo.cursorPos].playerName); + StringCopy(gStringVar1, gSaveBlock1Ptr->mail[gPlayerPCItemPageInfo.itemsAbove + PARTY_SIZE + gPlayerPCItemPageInfo.cursorPos].playerName); ConvertInternationalPlayerNameStripChar(gStringVar1, CHAR_SPACE); StringExpandPlaceholders(gStringVar4, gText_WhatToDoWithVar1sMail); DisplayItemMessageOnField(taskId, gStringVar4, Mailbox_PrintMailOptions); @@ -675,19 +741,19 @@ static void Mailbox_ReturnToPlayerPC(u8 taskId) { s16 *data = gTasks[taskId].data; - sub_81D1D04(0); - sub_81D1D04(1); - DestroyListMenuTask(data[5], NULL, NULL); + MailboxMenu_RemoveWindow(MAILBOXWIN_TITLE); + MailboxMenu_RemoveWindow(MAILBOXWIN_LIST); + DestroyListMenuTask(tListTaskId, NULL, NULL); ScheduleBgCopyTilemapToVram(0); - sub_81D1EC0(); + MailboxMenu_Free(); ReshowPlayerPC(taskId); } static void Mailbox_PrintMailOptions(u8 taskId) { - u8 r4 = sub_81D1C84(2); - PrintMenuTable(r4, ARRAY_COUNT(gMailboxMailOptions), gMailboxMailOptions); - InitMenuInUpperLeftCornerPlaySoundWhenAPressed(r4, 4, 0); + u8 windowId = MailboxMenu_AddWindow(MAILBOXWIN_OPTIONS); + PrintMenuTable(windowId, ARRAY_COUNT(gMailboxMailOptions), gMailboxMailOptions); + InitMenuInUpperLeftCornerPlaySoundWhenAPressed(windowId, 4, 0); ScheduleBgCopyTilemapToVram(0); gTasks[taskId].func = Mailbox_MailOptionsProcessInput; } @@ -696,19 +762,18 @@ static void Mailbox_MailOptionsProcessInput(u8 taskId) { s8 inputOptionId = ProcessMenuInput_other(); - switch(inputOptionId) + switch (inputOptionId) { - case -2: - break; - case -1: - PlaySE(SE_SELECT); - Mailbox_Cancel(taskId); - break; - - default: - PlaySE(SE_SELECT); - gMailboxMailOptions[inputOptionId].func.void_u8(taskId); - break; + case MENU_NOTHING_CHOSEN: + break; + case MENU_B_PRESSED: + PlaySE(SE_SELECT); + Mailbox_Cancel(taskId); + break; + default: + PlaySE(SE_SELECT); + gMailboxMailOptions[inputOptionId].func.void_u8(taskId); + break; } } @@ -722,26 +787,26 @@ static void Mailbox_FadeAndReadMail(u8 taskId) { if (!gPaletteFade.active) { - sub_81D1EC0(); + MailboxMenu_Free(); CleanupOverworldWindowsAndTilemaps(); - ReadMail(&(gSaveBlock1Ptr->mail[playerPCItemPageInfo.itemsAbove + PARTY_SIZE + playerPCItemPageInfo.cursorPos]), Mailbox_ReturnToFieldFromReadMail, TRUE); + ReadMail(&gSaveBlock1Ptr->mail[gPlayerPCItemPageInfo.itemsAbove + PARTY_SIZE + gPlayerPCItemPageInfo.cursorPos], Mailbox_ReturnToFieldFromReadMail, TRUE); DestroyTask(taskId); } } static void Mailbox_ReturnToFieldFromReadMail(void) { - gFieldCallback = pal_fill_for_maplights_or_black; + gFieldCallback = Mailbox_ReshowAfterMail; SetMainCallback2(CB2_ReturnToField); } -static void pal_fill_for_maplights_or_black(void) +static void Mailbox_ReshowAfterMail(void) { u8 taskId; LoadMessageBoxAndBorderGfx(); taskId = CreateTask(Mailbox_HandleReturnToProcessInput, 0); - if (sub_81D1C44(playerPCItemPageInfo.count) == TRUE) + if (MailboxMenu_Alloc(gPlayerPCItemPageInfo.count) == TRUE) Mailbox_DrawMailboxMenu(taskId); else DestroyTask(taskId); @@ -756,49 +821,49 @@ static void Mailbox_HandleReturnToProcessInput(u8 taskId) static void Mailbox_MoveToBag(u8 taskId) { - DisplayItemMessageOnField(taskId, gText_MessageWillBeLost, Mailbox_DrawYesNoBeforeMove); + DisplayItemMessageOnField(taskId, gText_MessageWillBeLost, Mailbox_AskConfirmMoveToBag); } -static void Mailbox_DrawYesNoBeforeMove(u8 taskId) +static void Mailbox_AskConfirmMoveToBag(u8 taskId) { DisplayYesNoMenuDefaultYes(); - gTasks[taskId].func = Mailbox_MoveToBagYesNoPrompt; + gTasks[taskId].func = Mailbox_HandleConfirmMoveToBag; } -static void Mailbox_MoveToBagYesNoPrompt(u8 taskId) +static void Mailbox_HandleConfirmMoveToBag(u8 taskId) { - switch(Menu_ProcessInputNoWrapClearOnChoose()) + switch (Menu_ProcessInputNoWrapClearOnChoose()) { - case 0: - Mailbox_DoMailMoveToBag(taskId); - break; - case -1: - PlaySE(SE_SELECT); - case 1: - Mailbox_CancelMoveToBag(taskId); - break; - case -2: - default: - break; + case 0: // Yes + Mailbox_DoMailMoveToBag(taskId); + break; + case MENU_B_PRESSED: + PlaySE(SE_SELECT); + case 1: // No + Mailbox_CancelMoveToBag(taskId); + break; + case MENU_NOTHING_CHOSEN: + default: + break; } } static void Mailbox_DoMailMoveToBag(u8 taskId) { - struct MailStruct *mailStruct = &(gSaveBlock1Ptr->mail[playerPCItemPageInfo.itemsAbove + PARTY_SIZE + playerPCItemPageInfo.cursorPos]); - if (!AddBagItem(mailStruct->itemId, 1)) + struct MailStruct *mail = &gSaveBlock1Ptr->mail[gPlayerPCItemPageInfo.itemsAbove + PARTY_SIZE + gPlayerPCItemPageInfo.cursorPos]; + if (!AddBagItem(mail->itemId, 1)) { DisplayItemMessageOnField(taskId, gText_BagIsFull, Mailbox_Cancel); } else { DisplayItemMessageOnField(taskId, gText_MailToBagMessageErased, Mailbox_Cancel); - ClearMailStruct(mailStruct); - Mailbox_UpdateMailList(); - playerPCItemPageInfo.count--; - if (playerPCItemPageInfo.count < (playerPCItemPageInfo.pageItems + playerPCItemPageInfo.itemsAbove) && playerPCItemPageInfo.itemsAbove != 0) - playerPCItemPageInfo.itemsAbove--; - ItemStorage_SetItemAndMailCount(taskId); + ClearMailStruct(mail); + Mailbox_CompactMailList(); + gPlayerPCItemPageInfo.count--; + if (gPlayerPCItemPageInfo.count < (gPlayerPCItemPageInfo.pageItems + gPlayerPCItemPageInfo.itemsAbove) && gPlayerPCItemPageInfo.itemsAbove != 0) + gPlayerPCItemPageInfo.itemsAbove--; + SetPlayerPCListCount(taskId); } } @@ -822,7 +887,7 @@ static void Mailbox_DoGiveMailPokeMenu(u8 taskId) { if (!gPaletteFade.active) { - sub_81D1EC0(); + MailboxMenu_Free(); CleanupOverworldWindowsAndTilemaps(); ChooseMonToGiveMailFromMailbox(); DestroyTask(taskId); @@ -840,15 +905,15 @@ static void Mailbox_UpdateMailListAfterDeposit(void) u8 taskId; u8 prevCount; taskId = CreateTask(Mailbox_HandleReturnToProcessInput, 0); - prevCount = playerPCItemPageInfo.count; - playerPCItemPageInfo.count = GetMailboxMailCount(); - Mailbox_UpdateMailList(); - if (prevCount != playerPCItemPageInfo.count && (playerPCItemPageInfo.count < (playerPCItemPageInfo.pageItems + playerPCItemPageInfo.itemsAbove)) - && playerPCItemPageInfo.itemsAbove != 0) - playerPCItemPageInfo.itemsAbove--; - ItemStorage_SetItemAndMailCount(taskId); + prevCount = gPlayerPCItemPageInfo.count; + gPlayerPCItemPageInfo.count = GetMailboxMailCount(); + Mailbox_CompactMailList(); + if (prevCount != gPlayerPCItemPageInfo.count && (gPlayerPCItemPageInfo.count < (gPlayerPCItemPageInfo.pageItems + gPlayerPCItemPageInfo.itemsAbove)) + && gPlayerPCItemPageInfo.itemsAbove != 0) + gPlayerPCItemPageInfo.itemsAbove--; + SetPlayerPCListCount(taskId); LoadMessageBoxAndBorderGfx(); - if (sub_81D1C44(playerPCItemPageInfo.count) == TRUE) + if (MailboxMenu_Alloc(gPlayerPCItemPageInfo.count) == TRUE) Mailbox_DrawMailboxMenu(taskId); else DestroyTask(taskId); @@ -862,45 +927,44 @@ static void Mailbox_NoPokemonForMail(u8 taskId) static void Mailbox_Cancel(u8 taskId) { - sub_81D1D04(2); + MailboxMenu_RemoveWindow(MAILBOXWIN_OPTIONS); ClearDialogWindowAndFrame(0, 0); Mailbox_DrawMailboxMenu(taskId); ScheduleBgCopyTilemapToVram(0); gTasks[taskId].func = Mailbox_ProcessInput; } -static void sub_816BC14(void) +static void ItemStorage_Init(void) { - gUnknown_0203BCC4 = AllocZeroed(sizeof(struct Struct203BCC4)); - memset(gUnknown_0203BCC4->windowIds, 0xFF, 0x6); - gUnknown_0203BCC4->unk666 = 0xFF; - gUnknown_0203BCC4->spriteId = SPRITE_NONE; + sItemStorageMenu = AllocZeroed(sizeof(*sItemStorageMenu)); + memset(sItemStorageMenu->windowIds, WINDOW_NONE, ITEMWIN_COUNT); + sItemStorageMenu->toSwapPos = NOT_SWAPPING; + sItemStorageMenu->spriteId = SPRITE_NONE; } -static void sub_816BC58(void) +static void ItemStorage_Free(void) { u32 i; - - for(i = 0; i < 6; i++) - sub_816BCC4(i); - Free(gUnknown_0203BCC4); + for (i = 0; i < ITEMWIN_COUNT; i++) + ItemStorage_RemoveWindow(i); + Free(sItemStorageMenu); } -static u8 sub_816BC7C(u8 a) +static u8 ItemStorage_AddWindow(u8 i) { - u8 *windowIdLoc = &(gUnknown_0203BCC4->windowIds[a]); + u8 *windowIdLoc = &sItemStorageMenu->windowIds[i]; if (*windowIdLoc == WINDOW_NONE) { - *windowIdLoc = AddWindow(&gUnknown_085DFF5C[a]); + *windowIdLoc = AddWindow(&sWindowTemplates_ItemStorage[i]); DrawStdFrameWithCustomTileAndPalette(*windowIdLoc, FALSE, 0x214, 0xE); ScheduleBgCopyTilemapToVram(0); } return *windowIdLoc; } -static void sub_816BCC4(u8 a) +static void ItemStorage_RemoveWindow(u8 i) { - u8 *windowIdLoc = &(gUnknown_0203BCC4->windowIds[a]); + u8 *windowIdLoc = &sItemStorageMenu->windowIds[i]; if (*windowIdLoc != WINDOW_NONE) { ClearStdWindowAndFrameToTransparent(*windowIdLoc, FALSE); @@ -915,20 +979,25 @@ void ItemStorage_RefreshListMenu(void) { u16 i; - for(i = 0; i < playerPCItemPageInfo.count - 1; i++) + // Copy item names for all entries but the last (which is Cancel) + for(i = 0; i < gPlayerPCItemPageInfo.count - 1; i++) { - CopyItemName_PlayerPC(&(gUnknown_0203BCC4->unk198[i][0]), gSaveBlock1Ptr->pcItems[i].itemId); - gUnknown_0203BCC4->unk0[i].name = &(gUnknown_0203BCC4->unk198[i][0]); - gUnknown_0203BCC4->unk0[i].id = i; + CopyItemName_PlayerPC(&sItemStorageMenu->itemNames[i][0], gSaveBlock1Ptr->pcItems[i].itemId); + sItemStorageMenu->listItems[i].name = &sItemStorageMenu->itemNames[i][0]; + sItemStorageMenu->listItems[i].id = i; } - StringCopy(&(gUnknown_0203BCC4->unk198[i][0]) ,gText_Cancel2); - gUnknown_0203BCC4->unk0[i].name = &(gUnknown_0203BCC4->unk198[i][0]); - gUnknown_0203BCC4->unk0[i].id = -2; - gMultiuseListMenuTemplate = gUnknown_085DFF44; - gMultiuseListMenuTemplate.windowId = sub_816BC7C(0); - gMultiuseListMenuTemplate.totalItems = playerPCItemPageInfo.count; - gMultiuseListMenuTemplate.items = gUnknown_0203BCC4->unk0; - gMultiuseListMenuTemplate.maxShowed = playerPCItemPageInfo.pageItems; + + // Set up Cancel entry + StringCopy(&sItemStorageMenu->itemNames[i][0], gText_Cancel2); + sItemStorageMenu->listItems[i].name = &sItemStorageMenu->itemNames[i][0]; + sItemStorageMenu->listItems[i].id = LIST_CANCEL; + + // Set list menu data + gMultiuseListMenuTemplate = sListMenuTemplate_ItemStorage; + gMultiuseListMenuTemplate.windowId = ItemStorage_AddWindow(ITEMWIN_LIST); + gMultiuseListMenuTemplate.totalItems = gPlayerPCItemPageInfo.count; + gMultiuseListMenuTemplate.items = sItemStorageMenu->listItems; + gMultiuseListMenuTemplate.maxShowed = gPlayerPCItemPageInfo.pageItems; } void CopyItemName_PlayerPC(u8 *string, u16 itemId) @@ -936,31 +1005,31 @@ void CopyItemName_PlayerPC(u8 *string, u16 itemId) CopyItemName(itemId, string); } -static void ItemStorage_MoveCursor(s32 id, bool8 b, struct ListMenu *thisMenu) +static void ItemStorage_MoveCursor(s32 id, bool8 onInit, struct ListMenu *list) { - if (b != TRUE) + if (onInit != TRUE) PlaySE(SE_SELECT); - if (gUnknown_0203BCC4->unk666 == 0xFF) + if (sItemStorageMenu->toSwapPos == NOT_SWAPPING) { - sub_816C0C8(); - if (id != -2) - sub_816C060(gSaveBlock1Ptr->pcItems[id].itemId); + ItemStorage_EraseItemIcon(); + if (id != LIST_CANCEL) + ItemStorage_DrawItemIcon(gSaveBlock1Ptr->pcItems[id].itemId); else - sub_816C060(ITEMPC_GO_BACK_TO_PREV); - sub_816BEF0(id); + ItemStorage_DrawItemIcon(MSG_GO_BACK_TO_PREV); + ItemStorage_PrintDescription(id); } } -static void fish4_goto_x5_or_x6(u8 windowId, s32 id, u8 yOffset) +static void ItemStorage_PrintMenuItem(u8 windowId, s32 id, u8 yOffset) { - if (id != -2) + if (id != LIST_CANCEL) { - if (gUnknown_0203BCC4->unk666 != 0xFF) + if (sItemStorageMenu->toSwapPos != NOT_SWAPPING) { - if (gUnknown_0203BCC4->unk666 == (u8)id) - sub_816BFE0(yOffset, 0, 0xFF); + if (sItemStorageMenu->toSwapPos == (u8)id) + ItemStorage_DrawSwapArrow(yOffset, 0, TEXT_SPEED_FF); else - sub_816BFE0(yOffset, 0xFF, 0xFF); + ItemStorage_DrawSwapArrow(yOffset, 0xFF, TEXT_SPEED_FF); } ConvertIntToDecimalStringN(gStringVar1, gSaveBlock1Ptr->pcItems[id].quantity, STR_CONV_MODE_RIGHT_ALIGN, 3); StringExpandPlaceholders(gStringVar4, gText_xVar1); @@ -968,58 +1037,64 @@ static void fish4_goto_x5_or_x6(u8 windowId, s32 id, u8 yOffset) } } -static void sub_816BEF0(s32 id) +static void ItemStorage_PrintDescription(s32 id) { const u8* description; - u8 windowId = gUnknown_0203BCC4->windowIds[1]; + u8 windowId = sItemStorageMenu->windowIds[ITEMWIN_MESSAGE]; - if (id != -2) + // Get item description (or Cancel text) + if (id != LIST_CANCEL) description = (u8 *)ItemId_GetDescription(gSaveBlock1Ptr->pcItems[id].itemId); else - description = ItemStorage_GetItemPcResponse(ITEMPC_GO_BACK_TO_PREV); + description = ItemStorage_GetMessage(MSG_GO_BACK_TO_PREV); + FillWindowPixelBuffer(windowId, PIXEL_FILL(1)); AddTextPrinterParameterized(windowId, 1, description, 0, 1, 0, NULL); } -static void ItemStorage_StartScrollIndicator(void) +static void ItemStorage_AddScrollIndicator(void) { - if (playerPCItemPageInfo.scrollIndicatorTaskId == TASK_NONE) - playerPCItemPageInfo.scrollIndicatorTaskId = AddScrollIndicatorArrowPairParameterized(SCROLL_ARROW_UP, 0xB0, 0xC, 0x94, playerPCItemPageInfo.count - playerPCItemPageInfo.pageItems, 0x13F8, 0x13F8, &(playerPCItemPageInfo.itemsAbove)); + if (gPlayerPCItemPageInfo.scrollIndicatorTaskId == TASK_NONE) + gPlayerPCItemPageInfo.scrollIndicatorTaskId = AddScrollIndicatorArrowPairParameterized(SCROLL_ARROW_UP, 176, 12, 148, + gPlayerPCItemPageInfo.count - gPlayerPCItemPageInfo.pageItems, + TAG_SCROLL_ARROW, + TAG_SCROLL_ARROW, + &gPlayerPCItemPageInfo.itemsAbove); } static void ItemStorage_RemoveScrollIndicator(void) { - if (playerPCItemPageInfo.scrollIndicatorTaskId != TASK_NONE) + if (gPlayerPCItemPageInfo.scrollIndicatorTaskId != TASK_NONE) { - RemoveScrollIndicatorArrowPair(playerPCItemPageInfo.scrollIndicatorTaskId); - playerPCItemPageInfo.scrollIndicatorTaskId = TASK_NONE; + RemoveScrollIndicatorArrowPair(gPlayerPCItemPageInfo.scrollIndicatorTaskId); + gPlayerPCItemPageInfo.scrollIndicatorTaskId = TASK_NONE; } } -static void sub_816BFB8(u8 a, u8 b, u8 speed) +static void ItemStorage_SetSwapArrow(u8 listTaskId, u8 b, u8 speed) { - sub_816BFE0(ListMenuGetYCoordForPrintingArrowCursor(a), b, speed); + ItemStorage_DrawSwapArrow(ListMenuGetYCoordForPrintingArrowCursor(listTaskId), b, speed); } -static void sub_816BFE0(u8 y, u8 b, u8 speed) +static void ItemStorage_DrawSwapArrow(u8 y, u8 b, u8 speed) { - u8 windowId = gUnknown_0203BCC4->windowIds[0]; + u8 windowId = sItemStorageMenu->windowIds[ITEMWIN_LIST]; if (b == 0xFF) FillWindowPixelRect(windowId, PIXEL_FILL(1), 0, y, GetMenuCursorDimensionByFont(1, 0), GetMenuCursorDimensionByFont(1, 1)); else - AddTextPrinterParameterized4(windowId, 1, 0, y, 0, 0, gUnknown_085DFF8C, speed, gText_SelectorArrow2); + AddTextPrinterParameterized4(windowId, 1, 0, y, 0, 0, sSwapArrowTextColors, speed, gText_SelectorArrow2); } -static void sub_816C060(u16 itemId) +static void ItemStorage_DrawItemIcon(u16 itemId) { u8 spriteId; - u8* spriteIdLoc = &(gUnknown_0203BCC4->spriteId); + u8* spriteIdLoc = &sItemStorageMenu->spriteId; if (*spriteIdLoc == SPRITE_NONE) { - FreeSpriteTilesByTag(0x13F6); - FreeSpritePaletteByTag(0x13F6); - spriteId = AddItemIconSprite(0x13F6, 0x13F6, itemId); + FreeSpriteTilesByTag(TAG_ITEM_ICON); + FreeSpritePaletteByTag(TAG_ITEM_ICON); + spriteId = AddItemIconSprite(TAG_ITEM_ICON, TAG_ITEM_ICON, itemId); if (spriteId != MAX_SPRITES) { *spriteIdLoc = spriteId; @@ -1030,30 +1105,30 @@ static void sub_816C060(u16 itemId) } } -static void sub_816C0C8(void) +static void ItemStorage_EraseItemIcon(void) { - u8* spriteIdLoc = &(gUnknown_0203BCC4->spriteId); + u8* spriteIdLoc = &sItemStorageMenu->spriteId; if (*spriteIdLoc != SPRITE_NONE) { - FreeSpriteTilesByTag(0x13F6); - FreeSpritePaletteByTag(0x13F6); - DestroySprite(&(gSprites[*spriteIdLoc])); + FreeSpriteTilesByTag(TAG_ITEM_ICON); + FreeSpritePaletteByTag(TAG_ITEM_ICON); + DestroySprite(&gSprites[*spriteIdLoc]); *spriteIdLoc = SPRITE_NONE; } } -static void sub_816C110(void) +static void ItemStorage_CompactList(void) { CompactPCItems(); - sub_812220C(gSaveBlock1Ptr->pcItems, 50, &(playerPCItemPageInfo.pageItems), &(playerPCItemPageInfo.count), 0x8); + SetItemListPerPageCount(gSaveBlock1Ptr->pcItems, PC_ITEMS_COUNT, &gPlayerPCItemPageInfo.pageItems, &gPlayerPCItemPageInfo.count, 8); } -static void sub_816C140(void) +static void ItemStorage_CompactCursor(void) { - sub_812225C(&(playerPCItemPageInfo.itemsAbove), &(playerPCItemPageInfo.cursorPos), playerPCItemPageInfo.pageItems, playerPCItemPageInfo.count); + SetCursorWithinListBounds(&gPlayerPCItemPageInfo.itemsAbove, &gPlayerPCItemPageInfo.cursorPos, gPlayerPCItemPageInfo.pageItems, gPlayerPCItemPageInfo.count); } -static void ItemStorage_ProcessWithdrawTossInput(u8 taskId) +static void ItemStorage_CreateListMenu(u8 taskId) { s16 *data; bool32 toss; @@ -1061,98 +1136,97 @@ static void ItemStorage_ProcessWithdrawTossInput(u8 taskId) const u8* text; data = gTasks[taskId].data; - for(i = 0; i <=3; i++) - sub_816BC7C(i); - toss = data[3]; + for (i = 0; i <= ITEMWIN_LIST_END; i++) + ItemStorage_AddWindow(i); + toss = tInTossMenu; text = gText_TossItem; if (!toss) text = gText_WithdrawItem; x = GetStringCenterAlignXOffset(1, text, 104); - AddTextPrinterParameterized(gUnknown_0203BCC4->windowIds[3], 1, text, x, 1, 0, NULL); - CopyWindowToVram(gUnknown_0203BCC4->windowIds[2], 2); - sub_816C110(); - sub_816C140(); + AddTextPrinterParameterized(sItemStorageMenu->windowIds[ITEMWIN_TITLE], 1, text, x, 1, 0, NULL); + CopyWindowToVram(sItemStorageMenu->windowIds[ITEMWIN_ICON], 2); + ItemStorage_CompactList(); + ItemStorage_CompactCursor(); ItemStorage_RefreshListMenu(); - data[5] = ListMenuInit(&gMultiuseListMenuTemplate, playerPCItemPageInfo.itemsAbove, playerPCItemPageInfo.cursorPos); - ItemStorage_StartScrollIndicator(); + tListTaskId = ListMenuInit(&gMultiuseListMenuTemplate, gPlayerPCItemPageInfo.itemsAbove, gPlayerPCItemPageInfo.cursorPos); + ItemStorage_AddScrollIndicator(); ScheduleBgCopyTilemapToVram(0); gTasks[taskId].func = ItemStorage_ProcessInput; } -static const u8* ItemStorage_GetItemPcResponse(u16 itemId) +static const u8* ItemStorage_GetMessage(u16 itemId) { const u8 *string; switch(itemId) { - case ITEMPC_GO_BACK_TO_PREV: - string = gText_GoBackPrevMenu; - break; - case ITEMPC_HOW_MANY_TO_WITHDRAW: - string = gText_WithdrawHowManyItems; - break; - case ITEMPC_WITHDREW_THING: - string = gText_WithdrawXItems; - break; - case ITEMPC_HOW_MANY_TO_TOSS: - string = gText_TossHowManyVar1s; - break; - case ITEMPC_THREW_AWAY_ITEM: - string = gText_ThrewAwayVar2Var1s; - break; - case ITEMPC_NO_MORE_ROOM: - string = gText_NoRoomInBag; - break; - case ITEMPC_TOO_IMPORTANT: - string = gText_TooImportantToToss; - break; - case ITEMPC_OKAY_TO_THROW_AWAY: - string = gText_ConfirmTossItems; - break; - case ITEMPC_SWITCH_WHICH_ITEM: - string = gText_MoveVar1Where; - break; - default: - string = ItemId_GetDescription(itemId); - break; + case MSG_GO_BACK_TO_PREV: + string = gText_GoBackPrevMenu; + break; + case MSG_HOW_MANY_TO_WITHDRAW: + string = gText_WithdrawHowManyItems; + break; + case MSG_WITHDREW_ITEM: + string = gText_WithdrawXItems; + break; + case MSG_HOW_MANY_TO_TOSS: + string = gText_TossHowManyVar1s; + break; + case MSG_THREW_AWAY_ITEM: + string = gText_ThrewAwayVar2Var1s; + break; + case MSG_NO_MORE_ROOM: + string = gText_NoRoomInBag; + break; + case MSG_TOO_IMPORTANT: + string = gText_TooImportantToToss; + break; + case MSG_OKAY_TO_THROW_AWAY: + string = gText_ConfirmTossItems; + break; + case MSG_SWITCH_WHICH_ITEM: + string = gText_MoveVar1Where; + break; + default: + string = ItemId_GetDescription(itemId); + break; } return string; } -static void ItemStorage_PrintItemPcResponse(const u8 *string) +static void ItemStorage_PrintMessage(const u8 *string) { - u8 windowId = gUnknown_0203BCC4->windowIds[1]; + u8 windowId = sItemStorageMenu->windowIds[ITEMWIN_MESSAGE]; FillWindowPixelBuffer(windowId, PIXEL_FILL(1)); StringExpandPlaceholders(gStringVar4, string); AddTextPrinterParameterized(windowId, 1, gStringVar4, 0, 1, 0, NULL); } +// Process input while on the item storage's item list static void ItemStorage_ProcessInput(u8 taskId) { - s16 *data; - s32 id; - - data = gTasks[taskId].data; + s16 *data = gTasks[taskId].data; if (JOY_NEW(SELECT_BUTTON)) { - ListMenuGetScrollAndRow(data[5], &(playerPCItemPageInfo.itemsAbove), &(playerPCItemPageInfo.cursorPos)); - if ((playerPCItemPageInfo.itemsAbove + playerPCItemPageInfo.cursorPos) != (playerPCItemPageInfo.count - 1)) + // 'Select' starts input for swapping items if not on Cancel + ListMenuGetScrollAndRow(tListTaskId, &gPlayerPCItemPageInfo.itemsAbove, &gPlayerPCItemPageInfo.cursorPos); + if (gPlayerPCItemPageInfo.itemsAbove + gPlayerPCItemPageInfo.cursorPos != gPlayerPCItemPageInfo.count - 1) { PlaySE(SE_SELECT); - ItemStorage_ItemSwapChoosePrompt(taskId); + ItemStorage_StartItemSwap(taskId); } } else { - id = ListMenu_ProcessInput(data[5]); - ListMenuGetScrollAndRow(data[5], &(playerPCItemPageInfo.itemsAbove), &(playerPCItemPageInfo.cursorPos)); - switch(id) + s32 id = ListMenu_ProcessInput(tListTaskId); + ListMenuGetScrollAndRow(tListTaskId, &gPlayerPCItemPageInfo.itemsAbove, &gPlayerPCItemPageInfo.cursorPos); + switch (id) { case LIST_NOTHING_CHOSEN: break; case LIST_CANCEL: PlaySE(SE_SELECT); - ItemStorage_GoBackToPlayerPCMenu(taskId); + ItemStorage_ExitItemList(taskId); break; default: PlaySE(SE_SELECT); @@ -1162,50 +1236,46 @@ static void ItemStorage_ProcessInput(u8 taskId) } } -static void ItemStorage_GoBackToPlayerPCMenu_InitStorage(u8 taskId) +static void ItemStorage_ReturnToMenuSelect(u8 taskId) { - s16 *data; - - data = gTasks[taskId].data; + s16 *data = gTasks[taskId].data; if (!IsDma3ManagerBusyWithBgCopy()) { DrawDialogueFrame(0, 0); - if (!data[3]) - InitItemStorageMenu(taskId, ITEMPC_MENU_WITHDRAW); + + // Select Withdraw/Toss by default depending on which was just exited + if (!tInTossMenu) + InitItemStorageMenu(taskId, MENU_WITHDRAW); else - InitItemStorageMenu(taskId, ITEMPC_MENU_TOSS); + InitItemStorageMenu(taskId, MENU_TOSS); gTasks[taskId].func = ItemStorageMenuProcessInput; } } -static void ItemStorage_GoBackToPlayerPCMenu(u8 taskId) +static void ItemStorage_ExitItemList(u8 taskId) { - s16 *data; - - data = gTasks[taskId].data; - sub_816C0C8(); + s16 *data = gTasks[taskId].data; + ItemStorage_EraseItemIcon(); ItemStorage_RemoveScrollIndicator(); - DestroyListMenuTask(data[5], NULL, NULL); - DestroySwapLineSprites(gUnknown_0203BCC4->spriteIds, 7); - sub_816BC58(); - gTasks[taskId].func = ItemStorage_GoBackToPlayerPCMenu_InitStorage; + DestroyListMenuTask(tListTaskId, NULL, NULL); + DestroySwapLineSprites(sItemStorageMenu->swapLineSpriteIds, SWAP_LINE_LENGTH); + ItemStorage_Free(); + gTasks[taskId].func = ItemStorage_ReturnToMenuSelect; } -static void ItemStorage_ItemSwapChoosePrompt(u8 taskId) +static void ItemStorage_StartItemSwap(u8 taskId) { - s16 *data; - - data = gTasks[taskId].data; - ListMenuSetUnkIndicatorsStructField(data[5], 16, 1); - gUnknown_0203BCC4->unk666 = (playerPCItemPageInfo.itemsAbove + playerPCItemPageInfo.cursorPos); - sub_816BFB8(data[5], 0, 0); - UpdateSwapLinePos(gUnknown_0203BCC4->unk666); - CopyItemName(gSaveBlock1Ptr->pcItems[gUnknown_0203BCC4->unk666].itemId, gStringVar1); - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(ITEMPC_SWITCH_WHICH_ITEM)); - gTasks[taskId].func = sub_816C4FC; + s16 *data = gTasks[taskId].data; + ListMenuSetUnkIndicatorsStructField(tListTaskId, 16, 1); + sItemStorageMenu->toSwapPos = gPlayerPCItemPageInfo.itemsAbove + gPlayerPCItemPageInfo.cursorPos; + ItemStorage_SetSwapArrow(tListTaskId, 0, 0); + ItemStorage_UpdateSwapLinePos(sItemStorageMenu->toSwapPos); + CopyItemName(gSaveBlock1Ptr->pcItems[sItemStorageMenu->toSwapPos].itemId, gStringVar1); + ItemStorage_PrintMessage(ItemStorage_GetMessage(MSG_SWITCH_WHICH_ITEM)); + gTasks[taskId].func = ItemStorage_ProcessItemSwapInput; } -static void sub_816C4FC(u8 taskId) +static void ItemStorage_ProcessItemSwapInput(u8 taskId) { s16 *data; s32 id; @@ -1213,230 +1283,220 @@ static void sub_816C4FC(u8 taskId) data = gTasks[taskId].data; if (JOY_NEW(SELECT_BUTTON)) { - ListMenuGetScrollAndRow(data[5], &(playerPCItemPageInfo.itemsAbove), &(playerPCItemPageInfo.cursorPos)); - ItemStorage_DoItemSwap(taskId, FALSE); + ListMenuGetScrollAndRow(tListTaskId, &gPlayerPCItemPageInfo.itemsAbove, &gPlayerPCItemPageInfo.cursorPos); + ItemStorage_FinishItemSwap(taskId, FALSE); return; } - id = ListMenu_ProcessInput(data[5]); - ListMenuGetScrollAndRow(data[5], &(playerPCItemPageInfo.itemsAbove), &(playerPCItemPageInfo.cursorPos)); - SetSwapLineSpritesInvisibility(gUnknown_0203BCC4->spriteIds, 7, FALSE); - UpdateSwapLinePos(playerPCItemPageInfo.cursorPos); - switch(id) + id = ListMenu_ProcessInput(tListTaskId); + ListMenuGetScrollAndRow(tListTaskId, &gPlayerPCItemPageInfo.itemsAbove, &gPlayerPCItemPageInfo.cursorPos); + SetSwapLineSpritesInvisibility(sItemStorageMenu->swapLineSpriteIds, SWAP_LINE_LENGTH, FALSE); + ItemStorage_UpdateSwapLinePos(gPlayerPCItemPageInfo.cursorPos); + switch (id) { case LIST_NOTHING_CHOSEN: break; case LIST_CANCEL: if (JOY_NEW(A_BUTTON)) - { - ItemStorage_DoItemSwap(taskId, FALSE); - } + ItemStorage_FinishItemSwap(taskId, FALSE); else - { - ItemStorage_DoItemSwap(taskId, TRUE); - } + ItemStorage_FinishItemSwap(taskId, TRUE); break; default: - ItemStorage_DoItemSwap(taskId, FALSE); + ItemStorage_FinishItemSwap(taskId, FALSE); break; } } -static void ItemStorage_DoItemSwap(u8 taskId, bool8 a) +static void ItemStorage_FinishItemSwap(u8 taskId, bool8 canceled) { - s16 *data; - u16 b; - u8 c; - - data = gTasks[taskId].data; - b = (playerPCItemPageInfo.itemsAbove + playerPCItemPageInfo.cursorPos); + s16 *data = gTasks[taskId].data; + u16 newPos = gPlayerPCItemPageInfo.itemsAbove + gPlayerPCItemPageInfo.cursorPos; PlaySE(SE_SELECT); - DestroyListMenuTask(data[5], &(playerPCItemPageInfo.itemsAbove), &(playerPCItemPageInfo.cursorPos)); - if (!a) + DestroyListMenuTask(tListTaskId, &gPlayerPCItemPageInfo.itemsAbove, &gPlayerPCItemPageInfo.cursorPos); + + if (!canceled && sItemStorageMenu->toSwapPos != newPos && sItemStorageMenu->toSwapPos != newPos - 1) { - c = gUnknown_0203BCC4->unk666; - if (c != b) - { - if (c != b - 1) - { - MoveItemSlotInList(gSaveBlock1Ptr->pcItems, c, b); - ItemStorage_RefreshListMenu(); - } - } - else - goto LABEL_SKIP_CURSOR_DECREMENT; + MoveItemSlotInList(gSaveBlock1Ptr->pcItems, sItemStorageMenu->toSwapPos, newPos); + ItemStorage_RefreshListMenu(); } - if (gUnknown_0203BCC4->unk666 < b) - playerPCItemPageInfo.cursorPos--; - LABEL_SKIP_CURSOR_DECREMENT: - SetSwapLineSpritesInvisibility(gUnknown_0203BCC4->spriteIds, 7, TRUE); - gUnknown_0203BCC4->unk666 = 0xFF; - data[5] = ListMenuInit(&gMultiuseListMenuTemplate, playerPCItemPageInfo.itemsAbove, playerPCItemPageInfo.cursorPos); + if (sItemStorageMenu->toSwapPos < newPos) + gPlayerPCItemPageInfo.cursorPos--; + + SetSwapLineSpritesInvisibility(sItemStorageMenu->swapLineSpriteIds, SWAP_LINE_LENGTH, TRUE); + sItemStorageMenu->toSwapPos = NOT_SWAPPING; + tListTaskId = ListMenuInit(&gMultiuseListMenuTemplate, gPlayerPCItemPageInfo.itemsAbove, gPlayerPCItemPageInfo.cursorPos); ScheduleBgCopyTilemapToVram(0); gTasks[taskId].func = ItemStorage_ProcessInput; } -static void UpdateSwapLinePos(u8 y) +static void ItemStorage_UpdateSwapLinePos(u8 y) { - UpdateSwapLineSpritesPos(gUnknown_0203BCC4->spriteIds, 7, 128, ((y+1) * 16)); + UpdateSwapLineSpritesPos(sItemStorageMenu->swapLineSpriteIds, SWAP_LINE_LENGTH, 128, (y+1) * 16); } -static void sub_816C6BC(u8 windowId, u16 value, u32 mode, u8 x, u8 y, u8 n) +static void ItemStorage_PrintItemQuantity(u8 windowId, u16 value, u32 mode, u8 x, u8 y, u8 n) { ConvertIntToDecimalStringN(gStringVar1, value, mode, n); StringExpandPlaceholders(gStringVar4, gText_xVar1); AddTextPrinterParameterized(windowId, 1, gStringVar4, GetStringCenterAlignXOffset(1, gStringVar4, 48), y, 0, NULL); } +// Start an item Withdraw/Toss static void ItemStorage_DoItemAction(u8 taskId) { - s16 *data; - u16 b; - - data = gTasks[taskId].data; - b = (playerPCItemPageInfo.cursorPos + playerPCItemPageInfo.itemsAbove); + s16 *data = gTasks[taskId].data; + u16 pos = gPlayerPCItemPageInfo.cursorPos + gPlayerPCItemPageInfo.itemsAbove; ItemStorage_RemoveScrollIndicator(); - data[2] = 1; - if (!data[3]) + tQuantity = 1; + + if (!tInTossMenu) { - if (gSaveBlock1Ptr->pcItems[b].quantity == 1) + if (gSaveBlock1Ptr->pcItems[pos].quantity == 1) { + // Withdrawing 1 item, do it automatically ItemStorage_DoItemWithdraw(taskId); return; } - CopyItemName(gSaveBlock1Ptr->pcItems[b].itemId, gStringVar1); - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(ITEMPC_HOW_MANY_TO_WITHDRAW)); + + // Withdrawing multiple items, show "how many" message + CopyItemName(gSaveBlock1Ptr->pcItems[pos].itemId, gStringVar1); + ItemStorage_PrintMessage(ItemStorage_GetMessage(MSG_HOW_MANY_TO_WITHDRAW)); } else { - if (gSaveBlock1Ptr->pcItems[b].quantity == 1) + if (gSaveBlock1Ptr->pcItems[pos].quantity == 1) { + // Tossing 1 item, do it automatically ItemStorage_DoItemToss(taskId); return; } - CopyItemName(gSaveBlock1Ptr->pcItems[b].itemId, gStringVar1); - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(ITEMPC_HOW_MANY_TO_TOSS)); + + // Tossing multiple items, show "how many" message + CopyItemName(gSaveBlock1Ptr->pcItems[pos].itemId, gStringVar1); + ItemStorage_PrintMessage(ItemStorage_GetMessage(MSG_HOW_MANY_TO_TOSS)); } - sub_816C6BC(sub_816BC7C(4), data[2], STR_CONV_MODE_LEADING_ZEROS, 8, 1, 3); + + // Set up "how many" prompt + ItemStorage_PrintItemQuantity(ItemStorage_AddWindow(ITEMWIN_QUANTITY), tQuantity, STR_CONV_MODE_LEADING_ZEROS, 8, 1, 3); gTasks[taskId].func = ItemStorage_HandleQuantityRolling; } static void ItemStorage_HandleQuantityRolling(u8 taskId) { - s16 *data; - u16 b; + s16 *data = gTasks[taskId].data; + u16 pos = gPlayerPCItemPageInfo.cursorPos + gPlayerPCItemPageInfo.itemsAbove; - data = gTasks[taskId].data; - b = (playerPCItemPageInfo.cursorPos + playerPCItemPageInfo.itemsAbove); - if (AdjustQuantityAccordingToDPadInput(&(data[2]), gSaveBlock1Ptr->pcItems[b].quantity) == TRUE) - sub_816C6BC(sub_816BC7C(4), data[2], STR_CONV_MODE_LEADING_ZEROS, 8, 1, 3); + if (AdjustQuantityAccordingToDPadInput(&tQuantity, gSaveBlock1Ptr->pcItems[pos].quantity) == TRUE) + { + ItemStorage_PrintItemQuantity(ItemStorage_AddWindow(ITEMWIN_QUANTITY), tQuantity, STR_CONV_MODE_LEADING_ZEROS, 8, 1, 3); + } else { if (JOY_NEW(A_BUTTON)) { + // Quantity confirmed, perform action PlaySE(SE_SELECT); - sub_816BCC4(4); - if (!data[3]) + ItemStorage_RemoveWindow(ITEMWIN_QUANTITY); + if (!tInTossMenu) ItemStorage_DoItemWithdraw(taskId); else ItemStorage_DoItemToss(taskId); } else if (JOY_NEW(B_BUTTON)) { + // Canceled action PlaySE(SE_SELECT); - sub_816BCC4(4); - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(gSaveBlock1Ptr->pcItems[b].itemId)); - ItemStorage_StartScrollIndicatorAndProcessInput(taskId); + ItemStorage_RemoveWindow(ITEMWIN_QUANTITY); + ItemStorage_PrintMessage(ItemStorage_GetMessage(gSaveBlock1Ptr->pcItems[pos].itemId)); + ItemStorage_ReturnToListInput(taskId); } } } static void ItemStorage_DoItemWithdraw(u8 taskId) { - s16 *data; - u16 b; + s16 *data = gTasks[taskId].data; + u16 pos = gPlayerPCItemPageInfo.cursorPos + gPlayerPCItemPageInfo.itemsAbove; - data = gTasks[taskId].data; - b = (playerPCItemPageInfo.cursorPos + playerPCItemPageInfo.itemsAbove); - if (AddBagItem(gSaveBlock1Ptr->pcItems[b].itemId, data[2]) == TRUE) + if (AddBagItem(gSaveBlock1Ptr->pcItems[pos].itemId, tQuantity) == TRUE) { - CopyItemName(gSaveBlock1Ptr->pcItems[b].itemId, gStringVar1); - ConvertIntToDecimalStringN(gStringVar2, data[2], STR_CONV_MODE_LEFT_ALIGN, 3); - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(ITEMPC_WITHDREW_THING)); + // Item withdrawn + CopyItemName(gSaveBlock1Ptr->pcItems[pos].itemId, gStringVar1); + ConvertIntToDecimalStringN(gStringVar2, tQuantity, STR_CONV_MODE_LEFT_ALIGN, 3); + ItemStorage_PrintMessage(ItemStorage_GetMessage(MSG_WITHDREW_ITEM)); gTasks[taskId].func = ItemStorage_HandleRemoveItem; } else { - data[2] = 0; - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(ITEMPC_NO_MORE_ROOM)); - gTasks[taskId].func = ItemStorage_WaitPressHandleResumeProcessInput; + // No room to withdraw items + tQuantity = 0; + ItemStorage_PrintMessage(ItemStorage_GetMessage(MSG_NO_MORE_ROOM)); + gTasks[taskId].func = ItemStorage_HandleErrorMessageInput; } } static void ItemStorage_DoItemToss(u8 taskId) { - s16 *data; - u16 b; + s16 *data = gTasks[taskId].data; + u16 pos = gPlayerPCItemPageInfo.cursorPos + gPlayerPCItemPageInfo.itemsAbove; - data = gTasks[taskId].data; - b = (playerPCItemPageInfo.cursorPos + playerPCItemPageInfo.itemsAbove); - if (!ItemId_GetImportance(gSaveBlock1Ptr->pcItems[b].itemId)) + if (!ItemId_GetImportance(gSaveBlock1Ptr->pcItems[pos].itemId)) { - CopyItemName(gSaveBlock1Ptr->pcItems[b].itemId, gStringVar1); - ConvertIntToDecimalStringN(gStringVar2, data[2], STR_CONV_MODE_LEFT_ALIGN, 3); - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(ITEMPC_OKAY_TO_THROW_AWAY)); - CreateYesNoMenuWithCallbacks(taskId, &gUnknown_085DFF84, 1, 0, 1, 0x214, 0xE, &ResumeFromWithdrawYesNoFuncList); + // Show toss confirmation prompt + CopyItemName(gSaveBlock1Ptr->pcItems[pos].itemId, gStringVar1); + ConvertIntToDecimalStringN(gStringVar2, tQuantity, STR_CONV_MODE_LEFT_ALIGN, 3); + ItemStorage_PrintMessage(ItemStorage_GetMessage(MSG_OKAY_TO_THROW_AWAY)); + CreateYesNoMenuWithCallbacks(taskId, &sWindowTemplates_ItemStorage[ITEMWIN_YESNO], 1, 0, 1, 0x214, 0xE, &ItemTossYesNoFuncs); } else { - data[2] = 0; - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(ITEMPC_TOO_IMPORTANT)); - gTasks[taskId].func = ItemStorage_WaitPressHandleResumeProcessInput; + // Can't toss important items + tQuantity = 0; + ItemStorage_PrintMessage(ItemStorage_GetMessage(MSG_TOO_IMPORTANT)); + gTasks[taskId].func = ItemStorage_HandleErrorMessageInput; } } -static void ItemStorage_ResumeInputFromYesToss(u8 taskId) +static void ItemStorage_TossItemYes(u8 taskId) { - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(ITEMPC_THREW_AWAY_ITEM)); + ItemStorage_PrintMessage(ItemStorage_GetMessage(MSG_THREW_AWAY_ITEM)); gTasks[taskId].func = ItemStorage_HandleRemoveItem; } -static void ItemStorage_ResumeInputFromNoToss(u8 taskId) +static void ItemStorage_TossItemNo(u8 taskId) { - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(gSaveBlock1Ptr->pcItems[(playerPCItemPageInfo.itemsAbove + playerPCItemPageInfo.cursorPos)].itemId)); - ItemStorage_StartScrollIndicatorAndProcessInput(taskId); + ItemStorage_PrintMessage(ItemStorage_GetMessage(gSaveBlock1Ptr->pcItems[gPlayerPCItemPageInfo.itemsAbove + gPlayerPCItemPageInfo.cursorPos].itemId)); + ItemStorage_ReturnToListInput(taskId); } +// Remove item from PC (was either Tossed or Withdrawn) static void ItemStorage_HandleRemoveItem(u8 taskId) { - s16 *data; - - data = gTasks[taskId].data; + s16 *data = gTasks[taskId].data; if (JOY_NEW(A_BUTTON | B_BUTTON)) { - RemovePCItem((playerPCItemPageInfo.cursorPos + playerPCItemPageInfo.itemsAbove), data[2]); - DestroyListMenuTask(data[5], &(playerPCItemPageInfo.itemsAbove), &(playerPCItemPageInfo.cursorPos)); - sub_816C110(); - sub_816C140(); + RemovePCItem(gPlayerPCItemPageInfo.cursorPos + gPlayerPCItemPageInfo.itemsAbove, tQuantity); + DestroyListMenuTask(tListTaskId, &gPlayerPCItemPageInfo.itemsAbove, &gPlayerPCItemPageInfo.cursorPos); + ItemStorage_CompactList(); + ItemStorage_CompactCursor(); ItemStorage_RefreshListMenu(); - data[5] = ListMenuInit(&gMultiuseListMenuTemplate, playerPCItemPageInfo.itemsAbove, playerPCItemPageInfo.cursorPos); - ItemStorage_StartScrollIndicatorAndProcessInput(taskId); + tListTaskId = ListMenuInit(&gMultiuseListMenuTemplate, gPlayerPCItemPageInfo.itemsAbove, gPlayerPCItemPageInfo.cursorPos); + ItemStorage_ReturnToListInput(taskId); } } -static void ItemStorage_WaitPressHandleResumeProcessInput(u8 taskId) +static void ItemStorage_HandleErrorMessageInput(u8 taskId) { - s16 *data; - - data = gTasks[taskId].data; + s16 *data = gTasks[taskId].data; if (JOY_NEW(A_BUTTON | B_BUTTON)) { - ItemStorage_PrintItemPcResponse(ItemStorage_GetItemPcResponse(gSaveBlock1Ptr->pcItems[(playerPCItemPageInfo.itemsAbove + playerPCItemPageInfo.cursorPos)].itemId)); - ItemStorage_StartScrollIndicatorAndProcessInput(taskId); + ItemStorage_PrintMessage(ItemStorage_GetMessage(gSaveBlock1Ptr->pcItems[gPlayerPCItemPageInfo.itemsAbove + gPlayerPCItemPageInfo.cursorPos].itemId)); + ItemStorage_ReturnToListInput(taskId); } } -static void ItemStorage_StartScrollIndicatorAndProcessInput(u8 taskId) +static void ItemStorage_ReturnToListInput(u8 taskId) { - ItemStorage_StartScrollIndicator(); + ItemStorage_AddScrollIndicator(); gTasks[taskId].func = ItemStorage_ProcessInput; } diff --git a/src/secret_base.c b/src/secret_base.c index ddc051dca7..a3ac84649f 100644 --- a/src/secret_base.c +++ b/src/secret_base.c @@ -1075,7 +1075,7 @@ void DeleteRegistry_Yes_Callback(u8 taskId) DestroyListMenuTask(tListTaskId, &tScrollOffset, &tSelectedRow); gSaveBlock1Ptr->secretBases[tSelectedBaseId].registryStatus = UNREGISTERED; BuildRegistryMenuItems(taskId); - sub_812225C(&tScrollOffset, &tSelectedRow, tMaxShownItems, tNumBases); + SetCursorWithinListBounds(&tScrollOffset, &tSelectedRow, tMaxShownItems, tNumBases); FinalizeRegistryMenu(taskId); gTasks[taskId].func = HandleRegistryMenuInput; }