-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
item_menu.c
executable file
·2597 lines (2382 loc) · 83.3 KB
/
item_menu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "global.h"
#include "item_menu.h"
#include "battle.h"
#include "battle_controllers.h"
#include "battle_pyramid.h"
#include "frontier_util.h"
#include "battle_pyramid_bag.h"
#include "berry_tag_screen.h"
#include "bg.h"
#include "data.h"
#include "decompress.h"
#include "event_data.h"
#include "event_object_movement.h"
#include "event_scripts.h"
#include "field_player_avatar.h"
#include "field_specials.h"
#include "graphics.h"
#include "gpu_regs.h"
#include "international_string_util.h"
#include "item.h"
#include "item_menu_icons.h"
#include "item_use.h"
#include "lilycove_lady.h"
#include "list_menu.h"
#include "link.h"
#include "mail.h"
#include "main.h"
#include "malloc.h"
#include "map_name_popup.h"
#include "menu.h"
#include "money.h"
#include "overworld.h"
#include "palette.h"
#include "party_menu.h"
#include "player_pc.h"
#include "pokemon.h"
#include "pokemon_summary_screen.h"
#include "scanline_effect.h"
#include "script.h"
#include "shop.h"
#include "sound.h"
#include "sprite.h"
#include "strings.h"
#include "string_util.h"
#include "task.h"
#include "text_window.h"
#include "menu_helpers.h"
#include "window.h"
#include "apprentice.h"
#include "battle_pike.h"
#include "constants/items.h"
#include "constants/rgb.h"
#include "constants/songs.h"
#define TAG_POCKET_SCROLL_ARROW 110
#define TAG_BAG_SCROLL_ARROW 111
// The buffer for the bag item list needs to be large enough to hold the maximum
// number of item slots that could fit in a single pocket, + 1 for Cancel.
// This constant picks the max of the existing pocket sizes.
// By default, the largest pocket is BAG_TMHM_COUNT at 64.
#define MAX_POCKET_ITEMS ((max(BAG_TMHM_COUNT, \
max(BAG_BERRIES_COUNT, \
max(BAG_ITEMS_COUNT, \
max(BAG_KEYITEMS_COUNT, \
BAG_POKEBALLS_COUNT))))) + 1)
// Up to 8 item slots can be visible at a time
#define MAX_ITEMS_SHOWN 8
enum {
SWITCH_POCKET_NONE,
SWITCH_POCKET_LEFT,
SWITCH_POCKET_RIGHT
};
enum {
ACTION_USE,
ACTION_TOSS,
ACTION_REGISTER,
ACTION_GIVE,
ACTION_CANCEL,
ACTION_BATTLE_USE,
ACTION_CHECK,
ACTION_WALK,
ACTION_DESELECT,
ACTION_CHECK_TAG,
ACTION_CONFIRM,
ACTION_SHOW,
ACTION_GIVE_FAVOR_LADY,
ACTION_CONFIRM_QUIZ_LADY,
ACTION_DUMMY,
};
enum {
WIN_ITEM_LIST,
WIN_DESCRIPTION,
WIN_POCKET_NAME,
WIN_TMHM_INFO_ICONS,
WIN_TMHM_INFO,
WIN_MESSAGE, // Identical to ITEMWIN_MESSAGE. Unused?
};
// Item list ID for toSwapPos to indicate an item is not currently being swapped
#define NOT_SWAPPING 0xFF
struct ListBuffer1 {
struct ListMenuItem subBuffers[MAX_POCKET_ITEMS];
};
struct ListBuffer2 {
u8 name[MAX_POCKET_ITEMS][ITEM_NAME_LENGTH + 10];
};
struct TempWallyBag {
struct ItemSlot bagPocket_Items[BAG_ITEMS_COUNT];
struct ItemSlot bagPocket_PokeBalls[BAG_POKEBALLS_COUNT];
u16 cursorPosition[POCKETS_COUNT];
u16 scrollPosition[POCKETS_COUNT];
u16 unused;
u16 pocket;
};
static void CB2_Bag(void);
static bool8 SetupBagMenu(void);
static void BagMenu_InitBGs(void);
static bool8 LoadBagMenu_Graphics(void);
static void LoadBagMenuTextWindows(void);
static void AllocateBagItemListBuffers(void);
static void LoadBagItemListBuffers(u8);
static void PrintPocketNames(const u8 *, const u8 *);
static void CopyPocketNameToWindow(u32);
static void DrawPocketIndicatorSquare(u8, bool8);
static void CreatePocketScrollArrowPair(void);
static void CreatePocketSwitchArrowPair(void);
static void DestroyPocketSwitchArrowPair(void);
static void PrepareTMHMMoveWindow(void);
static bool8 IsWallysBag(void);
static void Task_WallyTutorialBagMenu(u8);
static void Task_BagMenu_HandleInput(u8);
static void GetItemName(u8 *, u16);
static void PrintItemDescription(int);
static void BagMenu_PrintCursorAtPos(u8, u8);
static void BagMenu_Print(u8, u8, const u8 *, u8, u8, u8, u8, u8, u8);
static void Task_CloseBagMenu(u8);
static u8 AddItemMessageWindow(u8);
static void RemoveItemMessageWindow(u8);
static void ReturnToItemList(u8);
static void PrintItemQuantity(u8, s16);
static u8 BagMenu_AddWindow(u8);
static u8 GetSwitchBagPocketDirection(void);
static void SwitchBagPocket(u8, s16, bool16);
static bool8 CanSwapItems(void);
static void StartItemSwap(u8 taskId);
static void Task_SwitchBagPocket(u8);
static void Task_HandleSwappingItemsInput(u8);
static void DoItemSwap(u8);
static void CancelItemSwap(u8);
static void PrintTMHMMoveData(u16);
static void PrintContextMenuItems(u8);
static void PrintContextMenuItemGrid(u8, u8, u8);
static void Task_ItemContext_SingleRow(u8);
static void Task_ItemContext_MultipleRows(u8);
static bool8 IsValidContextMenuPos(s8);
static void BagMenu_RemoveWindow(u8);
static void PrintThereIsNoPokemon(u8);
static void Task_ChooseHowManyToToss(u8);
static void AskTossItems(u8);
static void Task_RemoveItemFromBag(u8);
static void ItemMenu_Cancel(u8);
static void HandleErrorMessage(u8);
static void PrintItemCantBeHeld(u8);
static void DisplayCurrentMoneyWindow(void);
static void DisplaySellItemPriceAndConfirm(u8);
static void InitSellHowManyInput(u8);
static void AskSellItems(u8);
static void RemoveMoneyWindow(void);
static void Task_ChooseHowManyToSell(u8);
static void SellItem(u8);
static void WaitAfterItemSell(u8);
static void TryDepositItem(u8);
static void Task_ChooseHowManyToDeposit(u8 taskId);
static void WaitDepositErrorMessage(u8);
static void CB2_ApprenticeExitBagMenu(void);
static void CB2_FavorLadyExitBagMenu(void);
static void CB2_QuizLadyExitBagMenu(void);
static void UpdatePocketItemLists(void);
static void InitPocketListPositions(void);
static void InitPocketScrollPositions(void);
static u8 CreateBagInputHandlerTask(u8);
static void DrawItemListBgRow(u8);
static void BagMenu_MoveCursorCallback(s32, bool8, struct ListMenu *);
static void BagMenu_ItemPrintCallback(u8, u32, u8);
static void ItemMenu_UseOutOfBattle(u8);
static void ItemMenu_Toss(u8);
static void ItemMenu_Register(u8);
static void ItemMenu_Give(u8);
static void ItemMenu_Cancel(u8);
static void ItemMenu_UseInBattle(u8);
static void ItemMenu_CheckTag(u8);
static void ItemMenu_Show(u8);
static void ItemMenu_GiveFavorLady(u8);
static void ItemMenu_ConfirmQuizLady(u8);
static void Task_ItemContext_Normal(u8);
static void Task_ItemContext_GiveToParty(u8);
static void Task_ItemContext_Sell(u8);
static void Task_ItemContext_Deposit(u8);
static void Task_ItemContext_GiveToPC(u8);
static void ConfirmToss(u8);
static void CancelToss(u8);
static void ConfirmSell(u8);
static void CancelSell(u8);
static const struct BgTemplate sBgTemplates_ItemMenu[] =
{
{
.bg = 0,
.charBaseIndex = 0,
.mapBaseIndex = 31,
.screenSize = 0,
.paletteMode = 0,
.priority = 1,
.baseTile = 0,
},
{
.bg = 1,
.charBaseIndex = 0,
.mapBaseIndex = 30,
.screenSize = 0,
.paletteMode = 0,
.priority = 0,
.baseTile = 0,
},
{
.bg = 2,
.charBaseIndex = 3,
.mapBaseIndex = 29,
.screenSize = 0,
.paletteMode = 0,
.priority = 2,
.baseTile = 0,
},
};
static const struct ListMenuTemplate sItemListMenu =
{
.items = NULL,
.moveCursorFunc = BagMenu_MoveCursorCallback,
.itemPrintFunc = BagMenu_ItemPrintCallback,
.totalItems = 0,
.maxShowed = 0,
.windowId = WIN_ITEM_LIST,
.header_X = 0,
.item_X = 8,
.cursor_X = 0,
.upText_Y = 1,
.cursorPal = 1,
.fillValue = 0,
.cursorShadowPal = 3,
.lettersSpacing = 0,
.itemVerticalPadding = 0,
.scrollMultiple = LIST_NO_MULTIPLE_SCROLL,
.fontId = FONT_NARROW,
.cursorKind = CURSOR_BLACK_ARROW
};
static const struct MenuAction sItemMenuActions[] = {
[ACTION_USE] = {gMenuText_Use, {ItemMenu_UseOutOfBattle}},
[ACTION_TOSS] = {gMenuText_Toss, {ItemMenu_Toss}},
[ACTION_REGISTER] = {gMenuText_Register, {ItemMenu_Register}},
[ACTION_GIVE] = {gMenuText_Give, {ItemMenu_Give}},
[ACTION_CANCEL] = {gText_Cancel2, {ItemMenu_Cancel}},
[ACTION_BATTLE_USE] = {gMenuText_Use, {ItemMenu_UseInBattle}},
[ACTION_CHECK] = {gMenuText_Check, {ItemMenu_UseOutOfBattle}},
[ACTION_WALK] = {gMenuText_Walk, {ItemMenu_UseOutOfBattle}},
[ACTION_DESELECT] = {gMenuText_Deselect, {ItemMenu_Register}},
[ACTION_CHECK_TAG] = {gMenuText_CheckTag, {ItemMenu_CheckTag}},
[ACTION_CONFIRM] = {gMenuText_Confirm, {Task_FadeAndCloseBagMenu}},
[ACTION_SHOW] = {gMenuText_Show, {ItemMenu_Show}},
[ACTION_GIVE_FAVOR_LADY] = {gMenuText_Give2, {ItemMenu_GiveFavorLady}},
[ACTION_CONFIRM_QUIZ_LADY] = {gMenuText_Confirm, {ItemMenu_ConfirmQuizLady}},
[ACTION_DUMMY] = {gText_EmptyString2, {NULL}}
};
// these are all 2D arrays with a width of 2 but are represented as 1D arrays
// ACTION_DUMMY is used to represent blank spaces
static const u8 sContextMenuItems_ItemsPocket[] = {
ACTION_USE, ACTION_GIVE,
ACTION_TOSS, ACTION_CANCEL
};
static const u8 sContextMenuItems_KeyItemsPocket[] = {
ACTION_USE, ACTION_REGISTER,
ACTION_DUMMY, ACTION_CANCEL
};
static const u8 sContextMenuItems_BallsPocket[] = {
ACTION_GIVE, ACTION_DUMMY,
ACTION_TOSS, ACTION_CANCEL
};
static const u8 sContextMenuItems_TmHmPocket[] = {
ACTION_USE, ACTION_GIVE,
ACTION_DUMMY, ACTION_CANCEL
};
static const u8 sContextMenuItems_BerriesPocket[] = {
ACTION_CHECK_TAG, ACTION_DUMMY,
ACTION_USE, ACTION_GIVE,
ACTION_TOSS, ACTION_CANCEL
};
static const u8 sContextMenuItems_BattleUse[] = {
ACTION_BATTLE_USE, ACTION_CANCEL
};
static const u8 sContextMenuItems_Give[] = {
ACTION_GIVE, ACTION_CANCEL
};
static const u8 sContextMenuItems_Cancel[] = {
ACTION_CANCEL
};
static const u8 sContextMenuItems_BerryBlenderCrush[] = {
ACTION_CONFIRM, ACTION_CHECK_TAG,
ACTION_DUMMY, ACTION_CANCEL
};
static const u8 sContextMenuItems_Apprentice[] = {
ACTION_SHOW, ACTION_CANCEL
};
static const u8 sContextMenuItems_FavorLady[] = {
ACTION_GIVE_FAVOR_LADY, ACTION_CANCEL
};
static const u8 sContextMenuItems_QuizLady[] = {
ACTION_CONFIRM_QUIZ_LADY, ACTION_CANCEL
};
static const TaskFunc sContextMenuFuncs[] = {
[ITEMMENULOCATION_FIELD] = Task_ItemContext_Normal,
[ITEMMENULOCATION_BATTLE] = Task_ItemContext_Normal,
[ITEMMENULOCATION_PARTY] = Task_ItemContext_GiveToParty,
[ITEMMENULOCATION_SHOP] = Task_ItemContext_Sell,
[ITEMMENULOCATION_BERRY_TREE] = Task_FadeAndCloseBagMenu,
[ITEMMENULOCATION_BERRY_BLENDER_CRUSH] = Task_ItemContext_Normal,
[ITEMMENULOCATION_ITEMPC] = Task_ItemContext_Deposit,
[ITEMMENULOCATION_FAVOR_LADY] = Task_ItemContext_Normal,
[ITEMMENULOCATION_QUIZ_LADY] = Task_ItemContext_Normal,
[ITEMMENULOCATION_APPRENTICE] = Task_ItemContext_Normal,
[ITEMMENULOCATION_WALLY] = NULL,
[ITEMMENULOCATION_PCBOX] = Task_ItemContext_GiveToPC
};
static const struct YesNoFuncTable sYesNoTossFunctions = {ConfirmToss, CancelToss};
static const struct YesNoFuncTable sYesNoSellItemFunctions = {ConfirmSell, CancelSell};
static const struct ScrollArrowsTemplate sBagScrollArrowsTemplate = {
.firstArrowType = SCROLL_ARROW_LEFT,
.firstX = 28,
.firstY = 16,
.secondArrowType = SCROLL_ARROW_RIGHT,
.secondX = 100,
.secondY = 16,
.fullyUpThreshold = -1,
.fullyDownThreshold = -1,
.tileTag = TAG_BAG_SCROLL_ARROW,
.palTag = TAG_BAG_SCROLL_ARROW,
.palNum = 0,
};
static const u8 sRegisteredSelect_Gfx[] = INCBIN_U8("graphics/bag/select_button.4bpp");
enum {
COLORID_NORMAL,
COLORID_POCKET_NAME,
COLORID_GRAY_CURSOR,
COLORID_UNUSED,
COLORID_TMHM_INFO,
COLORID_NONE = 0xFF
};
static const u8 sFontColorTable[][3] = {
// bgColor, textColor, shadowColor
[COLORID_NORMAL] = {TEXT_COLOR_TRANSPARENT, TEXT_COLOR_WHITE, TEXT_COLOR_LIGHT_GRAY},
[COLORID_POCKET_NAME] = {TEXT_COLOR_TRANSPARENT, TEXT_COLOR_WHITE, TEXT_COLOR_RED},
[COLORID_GRAY_CURSOR] = {TEXT_COLOR_TRANSPARENT, TEXT_COLOR_LIGHT_GRAY, TEXT_COLOR_GREEN},
[COLORID_UNUSED] = {TEXT_COLOR_DARK_GRAY, TEXT_COLOR_WHITE, TEXT_COLOR_LIGHT_GRAY},
[COLORID_TMHM_INFO] = {TEXT_COLOR_TRANSPARENT, TEXT_DYNAMIC_COLOR_5, TEXT_DYNAMIC_COLOR_1}
};
static const struct WindowTemplate sDefaultBagWindows[] =
{
[WIN_ITEM_LIST] = {
.bg = 0,
.tilemapLeft = 14,
.tilemapTop = 2,
.width = 15,
.height = 16,
.paletteNum = 1,
.baseBlock = 0x27,
},
[WIN_DESCRIPTION] = {
.bg = 0,
.tilemapLeft = 0,
.tilemapTop = 13,
.width = 14,
.height = 6,
.paletteNum = 1,
.baseBlock = 0x117,
},
[WIN_POCKET_NAME] = {
.bg = 0,
.tilemapLeft = 4,
.tilemapTop = 1,
.width = 8,
.height = 2,
.paletteNum = 1,
.baseBlock = 0x1A1,
},
[WIN_TMHM_INFO_ICONS] = {
.bg = 0,
.tilemapLeft = 1,
.tilemapTop = 13,
.width = 5,
.height = 6,
.paletteNum = 12,
.baseBlock = 0x16B,
},
[WIN_TMHM_INFO] = {
.bg = 0,
.tilemapLeft = 7,
.tilemapTop = 13,
.width = 4,
.height = 6,
.paletteNum = 12,
.baseBlock = 0x189,
},
[WIN_MESSAGE] = {
.bg = 1,
.tilemapLeft = 2,
.tilemapTop = 15,
.width = 27,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x1B1,
},
DUMMY_WIN_TEMPLATE,
};
static const struct WindowTemplate sContextMenuWindowTemplates[] =
{
[ITEMWIN_1x1] = {
.bg = 1,
.tilemapLeft = 22,
.tilemapTop = 17,
.width = 7,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x21D,
},
[ITEMWIN_1x2] = {
.bg = 1,
.tilemapLeft = 22,
.tilemapTop = 15,
.width = 7,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x21D,
},
[ITEMWIN_2x2] = {
.bg = 1,
.tilemapLeft = 15,
.tilemapTop = 15,
.width = 14,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x21D,
},
[ITEMWIN_2x3] = {
.bg = 1,
.tilemapLeft = 15,
.tilemapTop = 13,
.width = 14,
.height = 6,
.paletteNum = 15,
.baseBlock = 0x21D,
},
[ITEMWIN_MESSAGE] = {
.bg = 1,
.tilemapLeft = 2,
.tilemapTop = 15,
.width = 27,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x1B1,
},
[ITEMWIN_YESNO_LOW] = { // Yes/No tucked in corner, for toss confirm
.bg = 1,
.tilemapLeft = 24,
.tilemapTop = 15,
.width = 5,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x21D,
},
[ITEMWIN_YESNO_HIGH] = { // Yes/No higher up, positioned above a lower message box
.bg = 1,
.tilemapLeft = 21,
.tilemapTop = 9,
.width = 5,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x21D,
},
[ITEMWIN_QUANTITY] = { // Used for quantity of items to Toss/Deposit
.bg = 1,
.tilemapLeft = 24,
.tilemapTop = 17,
.width = 5,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x21D,
},
[ITEMWIN_QUANTITY_WIDE] = { // Used for quantity and price of items to Sell
.bg = 1,
.tilemapLeft = 18,
.tilemapTop = 11,
.width = 10,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x245,
},
[ITEMWIN_MONEY] = {
.bg = 1,
.tilemapLeft = 1,
.tilemapTop = 1,
.width = 10,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x231,
},
};
EWRAM_DATA struct BagMenu *gBagMenu = 0;
EWRAM_DATA struct BagPosition gBagPosition = {0};
static EWRAM_DATA struct ListBuffer1 *sListBuffer1 = 0;
static EWRAM_DATA struct ListBuffer2 *sListBuffer2 = 0;
EWRAM_DATA u16 gSpecialVar_ItemId = 0;
static EWRAM_DATA struct TempWallyBag *sTempWallyBag = 0;
void ResetBagScrollPositions(void)
{
gBagPosition.pocket = ITEMS_POCKET;
memset(gBagPosition.cursorPosition, 0, sizeof(gBagPosition.cursorPosition));
memset(gBagPosition.scrollPosition, 0, sizeof(gBagPosition.scrollPosition));
}
void CB2_BagMenuFromStartMenu(void)
{
GoToBagMenu(ITEMMENULOCATION_FIELD, POCKETS_COUNT, CB2_ReturnToFieldWithOpenMenu);
}
void CB2_BagMenuFromBattle(void)
{
if (!InBattlePyramid())
GoToBagMenu(ITEMMENULOCATION_BATTLE, POCKETS_COUNT, CB2_SetUpReshowBattleScreenAfterMenu2);
else
GoToBattlePyramidBagMenu(PYRAMIDBAG_LOC_BATTLE, CB2_SetUpReshowBattleScreenAfterMenu2);
}
// Choosing berry to plant
void CB2_ChooseBerry(void)
{
GoToBagMenu(ITEMMENULOCATION_BERRY_TREE, BERRIES_POCKET, CB2_ReturnToFieldContinueScript);
}
// Choosing berry for Berry Blender or Berry Crush
void ChooseBerryForMachine(void (*exitCallback)(void))
{
GoToBagMenu(ITEMMENULOCATION_BERRY_BLENDER_CRUSH, BERRIES_POCKET, exitCallback);
}
void CB2_GoToSellMenu(void)
{
GoToBagMenu(ITEMMENULOCATION_SHOP, POCKETS_COUNT, CB2_ExitSellMenu);
}
void CB2_GoToItemDepositMenu(void)
{
GoToBagMenu(ITEMMENULOCATION_ITEMPC, POCKETS_COUNT, CB2_PlayerPCExitBagMenu);
}
void ApprenticeOpenBagMenu(void)
{
GoToBagMenu(ITEMMENULOCATION_APPRENTICE, POCKETS_COUNT, CB2_ApprenticeExitBagMenu);
gSpecialVar_0x8005 = ITEM_NONE;
gSpecialVar_Result = FALSE;
}
void FavorLadyOpenBagMenu(void)
{
GoToBagMenu(ITEMMENULOCATION_FAVOR_LADY, POCKETS_COUNT, CB2_FavorLadyExitBagMenu);
gSpecialVar_Result = FALSE;
}
void QuizLadyOpenBagMenu(void)
{
GoToBagMenu(ITEMMENULOCATION_QUIZ_LADY, POCKETS_COUNT, CB2_QuizLadyExitBagMenu);
gSpecialVar_Result = FALSE;
}
void GoToBagMenu(u8 location, u8 pocket, void ( *exitCallback)())
{
gBagMenu = AllocZeroed(sizeof(*gBagMenu));
if (gBagMenu == NULL)
{
// Alloc failed, exit
SetMainCallback2(exitCallback);
}
else
{
if (location != ITEMMENULOCATION_LAST)
gBagPosition.location = location;
if (exitCallback)
gBagPosition.exitCallback = exitCallback;
if (pocket < POCKETS_COUNT)
gBagPosition.pocket = pocket;
if (gBagPosition.location == ITEMMENULOCATION_BERRY_TREE ||
gBagPosition.location == ITEMMENULOCATION_BERRY_BLENDER_CRUSH)
gBagMenu->pocketSwitchDisabled = TRUE;
gBagMenu->newScreenCallback = NULL;
gBagMenu->toSwapPos = NOT_SWAPPING;
gBagMenu->pocketScrollArrowsTask = TASK_NONE;
gBagMenu->pocketSwitchArrowsTask = TASK_NONE;
memset(gBagMenu->spriteIds, SPRITE_NONE, sizeof(gBagMenu->spriteIds));
memset(gBagMenu->windowIds, WINDOW_NONE, sizeof(gBagMenu->windowIds));
SetMainCallback2(CB2_Bag);
}
}
void CB2_BagMenuRun(void)
{
RunTasks();
AnimateSprites();
BuildOamBuffer();
DoScheduledBgTilemapCopiesToVram();
UpdatePaletteFade();
}
void VBlankCB_BagMenuRun(void)
{
LoadOam();
ProcessSpriteCopyRequests();
TransferPlttBuffer();
}
#define tListTaskId data[0]
#define tListPosition data[1]
#define tQuantity data[2]
#define tNeverRead data[3]
#define tItemCount data[8]
#define tMsgWindowId data[10]
#define tPocketSwitchDir data[11]
#define tPocketSwitchTimer data[12]
#define tPocketSwitchState data[13]
static void CB2_Bag(void)
{
while(MenuHelpers_ShouldWaitForLinkRecv() != TRUE && SetupBagMenu() != TRUE && MenuHelpers_IsLinkActive() != TRUE)
{};
}
static bool8 SetupBagMenu(void)
{
u8 taskId;
switch (gMain.state)
{
case 0:
SetVBlankHBlankCallbacksToNull();
ClearScheduledBgCopiesToVram();
gMain.state++;
break;
case 1:
ScanlineEffect_Stop();
gMain.state++;
break;
case 2:
FreeAllSpritePalettes();
gMain.state++;
break;
case 3:
ResetPaletteFade();
gPaletteFade.bufferTransferDisabled = TRUE;
gMain.state++;
break;
case 4:
ResetSpriteData();
gMain.state++;
break;
case 5:
gMain.state++;
break;
case 6:
if (!MenuHelpers_IsLinkActive())
ResetTasks();
gMain.state++;
break;
case 7:
BagMenu_InitBGs();
gBagMenu->graphicsLoadState = 0;
gMain.state++;
break;
case 8:
if (!LoadBagMenu_Graphics())
break;
gMain.state++;
break;
case 9:
LoadBagMenuTextWindows();
gMain.state++;
break;
case 10:
UpdatePocketItemLists();
InitPocketListPositions();
InitPocketScrollPositions();
gMain.state++;
break;
case 11:
AllocateBagItemListBuffers();
gMain.state++;
break;
case 12:
LoadBagItemListBuffers(gBagPosition.pocket);
gMain.state++;
break;
case 13:
PrintPocketNames(gPocketNamesStringsTable[gBagPosition.pocket], 0);
CopyPocketNameToWindow(0);
DrawPocketIndicatorSquare(gBagPosition.pocket, TRUE);
gMain.state++;
break;
case 14:
taskId = CreateBagInputHandlerTask(gBagPosition.location);
gTasks[taskId].tListTaskId = ListMenuInit(&gMultiuseListMenuTemplate, gBagPosition.scrollPosition[gBagPosition.pocket], gBagPosition.cursorPosition[gBagPosition.pocket]);
gTasks[taskId].tNeverRead = 0;
gTasks[taskId].tItemCount = 0;
gMain.state++;
break;
case 15:
AddBagVisualSprite(gBagPosition.pocket);
gMain.state++;
break;
case 16:
CreateItemMenuSwapLine();
gMain.state++;
break;
case 17:
CreatePocketScrollArrowPair();
CreatePocketSwitchArrowPair();
gMain.state++;
break;
case 18:
PrepareTMHMMoveWindow();
gMain.state++;
break;
case 19:
BlendPalettes(PALETTES_ALL, 16, 0);
gMain.state++;
break;
case 20:
BeginNormalPaletteFade(PALETTES_ALL, 0, 16, 0, RGB_BLACK);
gPaletteFade.bufferTransferDisabled = FALSE;
gMain.state++;
break;
default:
SetVBlankCallback(VBlankCB_BagMenuRun);
SetMainCallback2(CB2_BagMenuRun);
return TRUE;
}
return FALSE;
}
static void BagMenu_InitBGs(void)
{
ResetVramOamAndBgCntRegs();
memset(gBagMenu->tilemapBuffer, 0, sizeof(gBagMenu->tilemapBuffer));
ResetBgsAndClearDma3BusyFlags(0);
InitBgsFromTemplates(0, sBgTemplates_ItemMenu, ARRAY_COUNT(sBgTemplates_ItemMenu));
SetBgTilemapBuffer(2, gBagMenu->tilemapBuffer);
ResetAllBgsCoordinates();
ScheduleBgCopyTilemapToVram(2);
SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_ON | DISPCNT_OBJ_1D_MAP);
ShowBg(0);
ShowBg(1);
ShowBg(2);
SetGpuReg(REG_OFFSET_BLDCNT, 0);
}
static bool8 LoadBagMenu_Graphics(void)
{
switch (gBagMenu->graphicsLoadState)
{
case 0:
ResetTempTileDataBuffers();
DecompressAndCopyTileDataToVram(2, gBagScreen_Gfx, 0, 0, 0);
gBagMenu->graphicsLoadState++;
break;
case 1:
if (FreeTempTileDataBuffersIfPossible() != TRUE)
{
LZDecompressWram(gBagScreen_GfxTileMap, gBagMenu->tilemapBuffer);
gBagMenu->graphicsLoadState++;
}
break;
case 2:
if (!IsWallysBag() && gSaveBlock2Ptr->playerGender != MALE)
LoadCompressedPalette(gBagScreenFemale_Pal, BG_PLTT_ID(0), 2 * PLTT_SIZE_4BPP);
else
LoadCompressedPalette(gBagScreenMale_Pal, BG_PLTT_ID(0), 2 * PLTT_SIZE_4BPP);
gBagMenu->graphicsLoadState++;
break;
case 3:
if (IsWallysBag() == TRUE || gSaveBlock2Ptr->playerGender == MALE)
LoadCompressedSpriteSheet(&gBagMaleSpriteSheet);
else
LoadCompressedSpriteSheet(&gBagFemaleSpriteSheet);
gBagMenu->graphicsLoadState++;
break;
case 4:
LoadCompressedSpritePalette(&gBagPaletteTable);
gBagMenu->graphicsLoadState++;
break;
default:
LoadListMenuSwapLineGfx();
gBagMenu->graphicsLoadState = 0;
return TRUE;
}
return FALSE;
}
static u8 CreateBagInputHandlerTask(u8 location)
{
u8 taskId;
if (location == ITEMMENULOCATION_WALLY)
taskId = CreateTask(Task_WallyTutorialBagMenu, 0);
else
taskId = CreateTask(Task_BagMenu_HandleInput, 0);
return taskId;
}
static void AllocateBagItemListBuffers(void)
{
sListBuffer1 = Alloc(sizeof(*sListBuffer1));
sListBuffer2 = Alloc(sizeof(*sListBuffer2));
}
static void LoadBagItemListBuffers(u8 pocketId)
{
u16 i;
struct BagPocket *pocket = &gBagPockets[pocketId];
struct ListMenuItem *subBuffer;
if (!gBagMenu->hideCloseBagText)
{
for (i = 0; i < gBagMenu->numItemStacks[pocketId] - 1; i++)
{
GetItemName(sListBuffer2->name[i], pocket->itemSlots[i].itemId);
subBuffer = sListBuffer1->subBuffers;
subBuffer[i].name = sListBuffer2->name[i];
subBuffer[i].id = i;
}
StringCopy(sListBuffer2->name[i], gText_CloseBag);
subBuffer = sListBuffer1->subBuffers;
subBuffer[i].name = sListBuffer2->name[i];
subBuffer[i].id = LIST_CANCEL;
}
else
{
for (i = 0; i < gBagMenu->numItemStacks[pocketId]; i++)
{
GetItemName(sListBuffer2->name[i], pocket->itemSlots[i].itemId);
subBuffer = sListBuffer1->subBuffers;
subBuffer[i].name = sListBuffer2->name[i];
subBuffer[i].id = i;
}
}
gMultiuseListMenuTemplate = sItemListMenu;
gMultiuseListMenuTemplate.totalItems = gBagMenu->numItemStacks[pocketId];
gMultiuseListMenuTemplate.items = sListBuffer1->subBuffers;
gMultiuseListMenuTemplate.maxShowed = gBagMenu->numShownItems[pocketId];
}
static void GetItemName(u8 *dest, u16 itemId)
{
switch (gBagPosition.pocket)
{
case TMHM_POCKET:
StringCopy(gStringVar2, gMoveNames[ItemIdToBattleMoveId(itemId)]);
if (itemId >= ITEM_HM01)
{
// Get HM number
ConvertIntToDecimalStringN(gStringVar1, itemId - ITEM_HM01 + 1, STR_CONV_MODE_LEADING_ZEROS, 1);
StringExpandPlaceholders(dest, gText_NumberItem_HM);
}
else
{
// Get TM number
ConvertIntToDecimalStringN(gStringVar1, itemId - ITEM_TM01 + 1, STR_CONV_MODE_LEADING_ZEROS, 2);
StringExpandPlaceholders(dest, gText_NumberItem_TMBerry);
}
break;
case BERRIES_POCKET:
ConvertIntToDecimalStringN(gStringVar1, itemId - FIRST_BERRY_INDEX + 1, STR_CONV_MODE_LEADING_ZEROS, 2);
CopyItemName(itemId, gStringVar2);
StringExpandPlaceholders(dest, gText_NumberItem_TMBerry);
break;
default:
CopyItemName(itemId, dest);
break;
}
}
static void BagMenu_MoveCursorCallback(s32 itemIndex, bool8 onInit, struct ListMenu *list)
{
if (onInit != TRUE)
{
PlaySE(SE_SELECT);
ShakeBagSprite();
}
if (gBagMenu->toSwapPos == NOT_SWAPPING)
{
RemoveBagItemIconSprite(gBagMenu->itemIconSlot ^ 1);
if (itemIndex != LIST_CANCEL)
AddBagItemIconSprite(BagGetItemIdByPocketPosition(gBagPosition.pocket + 1, itemIndex), gBagMenu->itemIconSlot);
else
AddBagItemIconSprite(ITEM_LIST_END, gBagMenu->itemIconSlot);
gBagMenu->itemIconSlot ^= 1;
if (!gBagMenu->inhibitItemDescriptionPrint)
PrintItemDescription(itemIndex);
}
}
static void BagMenu_ItemPrintCallback(u8 windowId, u32 itemIndex, u8 y)
{
u16 itemId;
u16 itemQuantity;
int offset;
if (itemIndex != LIST_CANCEL)
{
if (gBagMenu->toSwapPos != NOT_SWAPPING)
{
// Swapping items, draw cursor at original item's location
if (gBagMenu->toSwapPos == (u8)itemIndex)
BagMenu_PrintCursorAtPos(y, COLORID_GRAY_CURSOR);
else
BagMenu_PrintCursorAtPos(y, COLORID_NONE);
}
itemId = BagGetItemIdByPocketPosition(gBagPosition.pocket + 1, itemIndex);
itemQuantity = BagGetQuantityByPocketPosition(gBagPosition.pocket + 1, itemIndex);
// Draw HM icon
if (itemId >= ITEM_HM01 && itemId <= ITEM_HM08)
BlitBitmapToWindow(windowId, gBagMenuHMIcon_Gfx, 8, y - 1, 16, 16);
if (gBagPosition.pocket == BERRIES_POCKET)
{
// Print berry quantity
ConvertIntToDecimalStringN(gStringVar1, itemQuantity, STR_CONV_MODE_RIGHT_ALIGN, BERRY_CAPACITY_DIGITS);
StringExpandPlaceholders(gStringVar4, gText_xVar1);
offset = GetStringRightAlignXOffset(FONT_NARROW, gStringVar4, 119);
BagMenu_Print(windowId, FONT_NARROW, gStringVar4, offset, y, 0, 0, TEXT_SKIP_DRAW, COLORID_NORMAL);
}
else if (gBagPosition.pocket != KEYITEMS_POCKET && ItemId_GetImportance(itemId) == FALSE)
{
// Print item quantity
ConvertIntToDecimalStringN(gStringVar1, itemQuantity, STR_CONV_MODE_RIGHT_ALIGN, BAG_ITEM_CAPACITY_DIGITS);
StringExpandPlaceholders(gStringVar4, gText_xVar1);
offset = GetStringRightAlignXOffset(FONT_NARROW, gStringVar4, 119);
BagMenu_Print(windowId, FONT_NARROW, gStringVar4, offset, y, 0, 0, TEXT_SKIP_DRAW, COLORID_NORMAL);
}
else
{
// Print registered icon
if (gSaveBlock1Ptr->registeredItem != ITEM_NONE && gSaveBlock1Ptr->registeredItem == itemId)
BlitBitmapToWindow(windowId, sRegisteredSelect_Gfx, 96, y - 1, 24, 16);
}
}
}
static void PrintItemDescription(int itemIndex)
{
const u8 *str;
if (itemIndex != LIST_CANCEL)
{