forked from rh-hideout/pokeemerald-expansion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshop.c
2031 lines (1840 loc) · 65.3 KB
/
shop.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 "bg.h"
#include "data.h"
#include "decompress.h"
#include "decoration.h"
#include "decoration_inventory.h"
#include "event_object_movement.h"
#include "field_player_avatar.h"
#include "field_screen_effect.h"
#include "field_weather.h"
#include "fieldmap.h"
#include "gpu_regs.h"
#include "graphics.h"
#include "international_string_util.h"
#include "item.h"
#include "item_icon.h"
#include "item_menu.h"
#include "list_menu.h"
#include "main.h"
#include "malloc.h"
#include "menu.h"
#include "menu_helpers.h"
#include "money.h"
#include "move.h"
#include "overworld.h"
#include "palette.h"
#include "party_menu.h"
#include "scanline_effect.h"
#include "script.h"
#include "shop.h"
#include "sound.h"
#include "sprite.h"
#include "string_util.h"
#include "strings.h"
#include "text_window.h"
#include "tv.h"
#include "constants/decorations.h"
#include "constants/event_objects.h"
#include "constants/items.h"
#include "constants/metatile_behaviors.h"
#include "constants/rgb.h"
#include "constants/songs.h"
#include "constants/party_menu.h"
#include "event_data.h"
#include "pokemon_icon.h"
#define TAG_SCROLL_ARROW 2100
#define TAG_ITEM_ICON_BASE 2110
#define MARTMOVE sMartInfo.martType == MART_TYPE_MOVE_TUTOR
#define MARTBP sMartInfo.martType == MART_TYPE_BP
#define MAX_ITEMS_SHOWN 8
const u8 gTextSelect[] = _("{SELECT_BUTTON}");
enum {
WIN_BUY_SELL_QUIT,
WIN_BUY_QUIT,
};
enum {
WIN_MONEY,
WIN_BP,
WIN_ITEM_LIST,
WIN_ITEM_DESCRIPTION,
WIN_QUANTITY_IN_BAG,
WIN_QUANTITY_PRICE,
WIN_MESSAGE,
WIN_BATTLE_MOVE_DESC,
};
enum {
COLORID_NORMAL, // Item descriptions, quantity in bag, and quantity/price
COLORID_ITEM_LIST, // The text in the item list, and the cursor normally
COLORID_GRAY_CURSOR, // When the cursor has selected an item to purchase
};
enum {
MART_TYPE_NORMAL,
MART_TYPE_DECOR,
MART_TYPE_DECOR2,
MART_TYPE_BP,
MART_TYPE_MOVE_TUTOR,
};
// shop view window NPC info enum
enum
{
OBJ_EVENT_ID,
X_COORD,
Y_COORD,
ANIM_NUM,
LAYER_TYPE
};
struct MartInfo
{
void (*callback)(void);
const struct MenuAction *menuActions;
const u16 *itemList;
u16 itemCount;
u8 windowId;
u8 martType;
u8 moveDesc;
};
struct ShopData
{
u16 tilemapBuffers[4][0x400];
u32 totalCost;
u16 itemsShowed;
u16 selectedRow;
u16 scrollOffset;
u16 maxQuantity;
u8 scrollIndicatorsTaskId;
u8 iconSlot;
u8 itemSpriteIds[2];
s16 viewportObjects[OBJECT_EVENTS_COUNT][5];
};
static EWRAM_DATA struct MartInfo sMartInfo = {0};
static EWRAM_DATA struct ShopData *sShopData = NULL;
static EWRAM_DATA struct ListMenuItem *sListMenuItems = NULL;
static EWRAM_DATA u8 (*sItemNames)[ITEM_NAME_LENGTH + 8] = {0};
static EWRAM_DATA u8 sPurchaseHistoryId = 0;
EWRAM_DATA struct ItemSlot gMartPurchaseHistory[SMARTSHOPPER_NUM_ITEMS] = {0};
static EWRAM_DATA u16 sScrollOffset = 0;
static EWRAM_DATA u16 sSelectedRow = 0;
static EWRAM_DATA u8 sNarrowerText = 0;
static EWRAM_DATA u8 sShowMonIcons = 0;
static void Task_ShopMenu(u8 taskId);
static void Task_HandleShopMenuQuit(u8 taskId);
static void CB2_InitBuyMenu(void);
static void Task_GoToBuyOrSellMenu(u8 taskId);
static void MapPostLoadHook_ReturnToShopMenu(void);
static void Task_ReturnToShopMenu(u8 taskId);
static void ShowShopMenuAfterExitingBuyOrSellMenu(u8 taskId);
static void BuyMenuDrawGraphics(void);
static void BuyMenuAddScrollIndicatorArrows(void);
static void Task_BuyMenu(u8 taskId);
static void BuyMenuBuildListMenuTemplate(void);
static void BuyMenuInitBgs(void);
static void BuyMenuInitWindows(void);
static void BuyMenuDecompressBgGraphics(void);
static void BuyMenuSetListEntry(struct ListMenuItem *, u16, u8 *);
static void BuyMenuAddItemIcon(u16, u8);
static void BuyMenuRemoveItemIcon(u16, u8);
static void BuyMenuPrint(u8 windowId, const u8 *text, u8 x, u8 y, s8 speed, u8 colorSet);
static void BuyMenuDrawMapGraphics(void);
static void BuyMenuCopyMenuBgToBg1TilemapBuffer(void);
static void BuyMenuCollectObjectEventData(void);
static void BuyMenuDrawObjectEvents(void);
static void BuyMenuDrawMapBg(void);
static bool8 BuyMenuCheckForOverlapWithMenuBg(int, int);
static void BuyMenuDrawMapMetatile(s16, s16, const u16 *, u8);
static void BuyMenuDrawMapMetatileLayer(u16 *dest, s16 offset1, s16 offset2, const u16 *src);
static bool8 BuyMenuCheckIfObjectEventOverlapsMenuBg(s16 *);
static void ExitBuyMenu(u8 taskId);
static void Task_ExitBuyMenu(u8 taskId);
static void BuyMenuTryMakePurchase(u8 taskId);
static void BuyMenuReturnToItemList(u8 taskId);
static void Task_BuyHowManyDialogueInit(u8 taskId);
static void BuyMenuConfirmPurchase(u8 taskId);
static void BuyMenuPrintItemQuantityAndPrice(u8 taskId);
static void Task_BuyHowManyDialogueHandleInput(u8 taskId);
static void BuyMenuSubtractMoney(u8 taskId);
static void RecordItemPurchase(u8 taskId);
static void Task_ReturnToItemListAfterItemPurchase(u8 taskId);
static void Task_ReturnToItemListAfterDecorationPurchase(u8 taskId);
static void Task_HandleShopMenuBuy(u8 taskId);
static void Task_HandleShopMenuSell(u8 taskId);
static void BuyMenuPrintItemDescriptionAndShowItemIcon(s32 item, bool8 onInit, struct ListMenu *list);
static void BuyMenuPrintPriceInList(u8 windowId, u32 itemId, u8 y);
static void FormatTextByWidth(u8*, s32, u8, const u8*, s16);
static void BuyMenuStartTutor(u8 taskId);
static void Task_BuyMenuTutor(u8 taskId);
static void CB2_InitBuyMenuAfterTutor(void);
static void DrawPartyMonIcons(void);
static void TintPartyMonIcons(u16 tm);
static void DestroyPartyMonIcons(void);
static const struct YesNoFuncTable sShopPurchaseYesNoFuncs =
{
BuyMenuTryMakePurchase,
BuyMenuReturnToItemList
};
static const struct MenuAction sShopMenuActions_BuySellQuit[] =
{
{ gText_ShopBuy, {.void_u8=Task_HandleShopMenuBuy} },
{ gText_ShopSell, {.void_u8=Task_HandleShopMenuSell} },
{ gText_ShopQuit, {.void_u8=Task_HandleShopMenuQuit} }
};
static const struct MenuAction sShopMenuActions_BuyQuit[] =
{
{ gText_ShopBuy, {.void_u8=Task_HandleShopMenuBuy} },
{ gText_ShopQuit, {.void_u8=Task_HandleShopMenuQuit} }
};
static const struct MenuAction sShopMenuActions_ExchangeQuit[] =
{
{ gText_Exchange, {.void_u8=Task_HandleShopMenuBuy} },
{ gText_ShopQuit, {.void_u8=Task_HandleShopMenuQuit} }
};
static const struct MenuAction sShopMenuActions_TeachMoveQuit[] =
{
{ gText_TeachMove, {.void_u8=Task_HandleShopMenuBuy} },
{ gText_ShopQuit, {.void_u8=Task_HandleShopMenuQuit} }
};
static const struct WindowTemplate sShopMenuWindowTemplates[] =
{
[WIN_BUY_SELL_QUIT] = {
.bg = 0,
.tilemapLeft = 2,
.tilemapTop = 1,
.width = 9,
.height = 6,
.paletteNum = 15,
.baseBlock = 0x0008,
},
// Separate shop menu window for decorations, which can't be sold
[WIN_BUY_QUIT] = {
.bg = 0,
.tilemapLeft = 2,
.tilemapTop = 1,
.width = 9,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x0008,
}
};
static const struct ListMenuTemplate sShopBuyMenuListTemplate =
{
.items = NULL,
.moveCursorFunc = BuyMenuPrintItemDescriptionAndShowItemIcon,
.itemPrintFunc = BuyMenuPrintPriceInList,
.totalItems = 0,
.maxShowed = 0,
.windowId = WIN_ITEM_LIST,
.header_X = 0,
.item_X = 8,
.cursor_X = 0,
.upText_Y = 1,
.cursorPal = 2,
.fillValue = 0,
.cursorShadowPal = 3,
.lettersSpacing = 0,
.itemVerticalPadding = 0,
.scrollMultiple = LIST_NO_MULTIPLE_SCROLL,
.fontId = FONT_NARROW,
.cursorKind = CURSOR_BLACK_ARROW,
.textNarrowWidth = 84,
};
static const struct BgTemplate sShopBuyMenuBgTemplates[] =
{
{
.bg = 0,
.charBaseIndex = 2,
.mapBaseIndex = 31,
.screenSize = 0,
.paletteMode = 0,
.priority = 0,
.baseTile = 0
},
{
.bg = 1,
.charBaseIndex = 0,
.mapBaseIndex = 30,
.screenSize = 0,
.paletteMode = 0,
.priority = 1,
.baseTile = 0
},
{
.bg = 2,
.charBaseIndex = 0,
.mapBaseIndex = 29,
.screenSize = 0,
.paletteMode = 0,
.priority = 2,
.baseTile = 0
},
{
.bg = 3,
.charBaseIndex = 0,
.mapBaseIndex = 28,
.screenSize = 0,
.paletteMode = 0,
.priority = 3,
.baseTile = 0
}
};
static const struct WindowTemplate sShopBuyMenuWindowTemplates[] =
{
[WIN_MONEY] = {
.bg = 0,
.tilemapLeft = 1,
.tilemapTop = 1,
.width = 10,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x001E,
},
[WIN_BP] = {
.bg = 0,
.tilemapLeft = 1,
.tilemapTop = 1,
.width = 6,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x001E,
},
[WIN_ITEM_LIST] = {
.bg = 0,
.tilemapLeft = 14,
.tilemapTop = 2,
.width = 15,
.height = 16,
.paletteNum = 15,
.baseBlock = 0x0032,
},
[WIN_ITEM_DESCRIPTION] = {
.bg = 0,
.tilemapLeft = 0,
.tilemapTop = 13,
.width = 14,
.height = 6,
.paletteNum = 15,
.baseBlock = 0x0122,
},
[WIN_QUANTITY_IN_BAG] = {
.bg = 0,
.tilemapLeft = 1,
.tilemapTop = 11,
.width = 12,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x0176,
},
[WIN_QUANTITY_PRICE] = {
.bg = 0,
.tilemapLeft = 18,
.tilemapTop = 11,
.width = 10,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x018E,
},
[WIN_MESSAGE] = {
.bg = 0,
.tilemapLeft = 2,
.tilemapTop = 15,
.width = 27,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x01A2,
},
[WIN_BATTLE_MOVE_DESC] = {
.bg = 0,
.tilemapLeft = 1,
.tilemapTop = 5,
.width = 11,
.height = 7,
.paletteNum = 15,
.baseBlock = 0x0222,
},
DUMMY_WIN_TEMPLATE
};
static const struct WindowTemplate sShopBuyMenuYesNoWindowTemplates =
{
.bg = 0,
.tilemapLeft = 21,
.tilemapTop = 9,
.width = 5,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x020E,
};
static const u8 sShopBuyMenuTextColors[][3] =
{
[COLORID_NORMAL] = {1, 2, 3},
[COLORID_ITEM_LIST] = {0, 2, 3},
[COLORID_GRAY_CURSOR] = {0, 3, 2},
};
static u8 CreateShopMenu(u8 martType)
{
int numMenuItems;
LockPlayerFieldControls();
sShowMonIcons = 0;
sMartInfo.martType = martType;
gSpecialVar_Result = FALSE;
if (martType == MART_TYPE_NORMAL)
{
struct WindowTemplate winTemplate = sShopMenuWindowTemplates[WIN_BUY_SELL_QUIT];
winTemplate.width = GetMaxWidthInMenuTable(sShopMenuActions_BuySellQuit, ARRAY_COUNT(sShopMenuActions_BuySellQuit));
sMartInfo.windowId = AddWindow(&winTemplate);
sMartInfo.menuActions = sShopMenuActions_BuySellQuit;
numMenuItems = ARRAY_COUNT(sShopMenuActions_BuySellQuit);
}
else if (MARTBP)
{
struct WindowTemplate winTemplate = sShopMenuWindowTemplates[WIN_BUY_QUIT];
winTemplate.width = GetMaxWidthInMenuTable(sShopMenuActions_ExchangeQuit, ARRAY_COUNT(sShopMenuActions_ExchangeQuit));
sMartInfo.windowId = AddWindow(&winTemplate);
sMartInfo.menuActions = sShopMenuActions_ExchangeQuit;
numMenuItems = ARRAY_COUNT(sShopMenuActions_ExchangeQuit);
}
else if (MARTMOVE)
{
struct WindowTemplate winTemplate = sShopMenuWindowTemplates[WIN_BUY_QUIT];
winTemplate.width = GetMaxWidthInMenuTable(sShopMenuActions_TeachMoveQuit, ARRAY_COUNT(sShopMenuActions_TeachMoveQuit));
sMartInfo.windowId = AddWindow(&winTemplate);
sMartInfo.menuActions = sShopMenuActions_TeachMoveQuit;
numMenuItems = ARRAY_COUNT(sShopMenuActions_TeachMoveQuit);
}
else
{
struct WindowTemplate winTemplate = sShopMenuWindowTemplates[WIN_BUY_QUIT];
winTemplate.width = GetMaxWidthInMenuTable(sShopMenuActions_BuyQuit, ARRAY_COUNT(sShopMenuActions_BuyQuit));
sMartInfo.windowId = AddWindow(&winTemplate);
sMartInfo.menuActions = sShopMenuActions_BuyQuit;
numMenuItems = ARRAY_COUNT(sShopMenuActions_BuyQuit);
}
SetStandardWindowBorderStyle(sMartInfo.windowId, FALSE);
PrintMenuTable(sMartInfo.windowId, numMenuItems, sMartInfo.menuActions);
InitMenuInUpperLeftCornerNormal(sMartInfo.windowId, numMenuItems, 0);
PutWindowTilemap(sMartInfo.windowId);
CopyWindowToVram(sMartInfo.windowId, COPYWIN_MAP);
return CreateTask(Task_ShopMenu, 8);
}
static void SetShopMenuCallback(void (* callback)(void))
{
sMartInfo.callback = callback;
}
static void SetShopItemsForSale(const u16 *items)
{
u16 i = 0;
sMartInfo.itemList = items;
sMartInfo.itemCount = 0;
// Read items until ITEM_NONE / DECOR_NONE is reached
while (sMartInfo.itemList[i])
{
sMartInfo.itemCount++;
i++;
}
}
static void Task_ShopMenu(u8 taskId)
{
s8 inputCode = Menu_ProcessInputNoWrap();
switch (inputCode)
{
case MENU_NOTHING_CHOSEN:
break;
case MENU_B_PRESSED:
PlaySE(SE_SELECT);
Task_HandleShopMenuQuit(taskId);
break;
default:
sMartInfo.menuActions[inputCode].func.void_u8(taskId);
break;
}
}
#define tItemCount data[1]
#define tItemId data[5]
#define tListTaskId data[7]
#define tCallbackHi data[8]
#define tCallbackLo data[9]
static void Task_HandleShopMenuBuy(u8 taskId)
{
s16 *data = gTasks[taskId].data;
tCallbackHi = (u32)CB2_InitBuyMenu >> 16;
tCallbackLo = (u32)CB2_InitBuyMenu;
gTasks[taskId].func = Task_GoToBuyOrSellMenu;
FadeScreen(FADE_TO_BLACK, 0);
}
static void Task_HandleShopMenuSell(u8 taskId)
{
s16 *data = gTasks[taskId].data;
tCallbackHi = (u32)CB2_GoToSellMenu >> 16;
tCallbackLo = (u32)CB2_GoToSellMenu;
gTasks[taskId].func = Task_GoToBuyOrSellMenu;
FadeScreen(FADE_TO_BLACK, 0);
}
void CB2_ExitSellMenu(void)
{
gFieldCallback = MapPostLoadHook_ReturnToShopMenu;
SetMainCallback2(CB2_ReturnToField);
}
static void Task_HandleShopMenuQuit(u8 taskId)
{
ClearStdWindowAndFrameToTransparent(sMartInfo.windowId, 2); // Incorrect use, making it not copy it to vram.
RemoveWindow(sMartInfo.windowId);
TryPutSmartShopperOnAir();
UnlockPlayerFieldControls();
DestroyTask(taskId);
if (sMartInfo.callback)
sMartInfo.callback();
}
static void Task_GoToBuyOrSellMenu(u8 taskId)
{
s16 *data = gTasks[taskId].data;
if (!gPaletteFade.active)
{
DestroyTask(taskId);
SetMainCallback2((void *)((u16)tCallbackHi << 16 | (u16)tCallbackLo));
}
}
static void MapPostLoadHook_ReturnToShopMenu(void)
{
FadeInFromBlack();
CreateTask(Task_ReturnToShopMenu, 8);
}
static void Task_ReturnToShopMenu(u8 taskId)
{
if (IsWeatherNotFadingIn() == TRUE)
{
if (sMartInfo.martType == MART_TYPE_DECOR2)
DisplayItemMessageOnField(taskId, gText_CanIHelpWithAnythingElse, ShowShopMenuAfterExitingBuyOrSellMenu);
else
DisplayItemMessageOnField(taskId, gText_AnythingElseICanHelp, ShowShopMenuAfterExitingBuyOrSellMenu);
}
}
static void ShowShopMenuAfterExitingBuyOrSellMenu(u8 taskId)
{
CreateShopMenu(sMartInfo.martType);
DestroyTask(taskId);
}
static void CB2_BuyMenu(void)
{
RunTasks();
AnimateSprites();
BuildOamBuffer();
DoScheduledBgTilemapCopiesToVram();
UpdatePaletteFade();
}
static void VBlankCB_BuyMenu(void)
{
LoadOam();
ProcessSpriteCopyRequests();
TransferPlttBuffer();
}
static void CB2_InitBuyMenu(void)
{
u8 taskId;
switch (gMain.state)
{
case 0:
SetVBlankHBlankCallbacksToNull();
CpuFastFill(0, (void *)OAM, OAM_SIZE);
ScanlineEffect_Stop();
ResetTempTileDataBuffers();
FreeAllSpritePalettes();
ResetPaletteFade();
ResetSpriteData();
ResetTasks();
ClearScheduledBgCopiesToVram();
sShopData = AllocZeroed(sizeof(struct ShopData));
sShopData->scrollIndicatorsTaskId = TASK_NONE;
sShopData->itemSpriteIds[0] = SPRITE_NONE;
sShopData->itemSpriteIds[1] = SPRITE_NONE;
BuyMenuBuildListMenuTemplate();
BuyMenuInitBgs();
FillBgTilemapBufferRect_Palette0(0, 0, 0, 0, 0x20, 0x20);
FillBgTilemapBufferRect_Palette0(1, 0, 0, 0, 0x20, 0x20);
FillBgTilemapBufferRect_Palette0(2, 0, 0, 0, 0x20, 0x20);
FillBgTilemapBufferRect_Palette0(3, 0, 0, 0, 0x20, 0x20);
BuyMenuInitWindows();
BuyMenuDecompressBgGraphics();
gMain.state++;
break;
case 1:
if (!FreeTempTileDataBuffersIfPossible())
gMain.state++;
break;
default:
BuyMenuDrawGraphics();
BuyMenuAddScrollIndicatorArrows();
taskId = CreateTask(Task_BuyMenu, 8);
gTasks[taskId].tListTaskId = ListMenuInit(&gMultiuseListMenuTemplate, 0, 0);
BlendPalettes(PALETTES_ALL, 16, RGB_BLACK);
BeginNormalPaletteFade(PALETTES_ALL, 0, 16, 0, RGB_BLACK);
SetVBlankCallback(VBlankCB_BuyMenu);
SetMainCallback2(CB2_BuyMenu);
break;
}
}
static void CB2_InitBuyMenuAfterTutor(void)
{
u8 taskId;
if (gSpecialVar_Result == TRUE)
{
gSaveBlock2Ptr->frontier.battlePoints -= gSpecialVar_0x8008;
gSpecialVar_0x8008 = 0;
SetMainCallback2(CB2_ReturnToFieldContinueScriptPlayMapMusic);
}
switch (gMain.state)
{
case 0:
SetVBlankHBlankCallbacksToNull();
CpuFastFill(0, (void *)OAM, OAM_SIZE);
ScanlineEffect_Stop();
ResetTempTileDataBuffers();
FreeAllSpritePalettes();
ResetPaletteFade();
ResetSpriteData();
ResetTasks();
ClearScheduledBgCopiesToVram();
CopyPrimaryTilesetToVram(gMapHeader.mapLayout);
CopySecondaryTilesetToVram(gMapHeader.mapLayout);
LoadMapTilesetPalettes(gMapHeader.mapLayout);
sShopData = AllocZeroed(sizeof(struct ShopData));
sShopData->scrollIndicatorsTaskId = TASK_NONE;
sShopData->itemSpriteIds[0] = SPRITE_NONE;
sShopData->itemSpriteIds[1] = SPRITE_NONE;
BuyMenuBuildListMenuTemplate();
BuyMenuInitBgs();
FillBgTilemapBufferRect_Palette0(0, 0, 0, 0, 0x20, 0x20);
FillBgTilemapBufferRect_Palette0(1, 0, 0, 0, 0x20, 0x20);
FillBgTilemapBufferRect_Palette0(2, 0, 0, 0, 0x20, 0x20);
FillBgTilemapBufferRect_Palette0(3, 0, 0, 0, 0x20, 0x20);
BuyMenuInitWindows();
BuyMenuDecompressBgGraphics();
gMain.state++;
break;
case 1:
if (!FreeTempTileDataBuffersIfPossible())
gMain.state++;
break;
default:
BuyMenuDrawGraphics();
BuyMenuAddScrollIndicatorArrows();
taskId = CreateTask(Task_BuyMenu, 8);
gTasks[taskId].tListTaskId = ListMenuInit(&gMultiuseListMenuTemplate, sScrollOffset, sSelectedRow);
BlendPalettes(PALETTES_ALL, 16, RGB_BLACK);
BeginNormalPaletteFade(PALETTES_ALL, 0, 16, 0, RGB_BLACK);
SetVBlankCallback(VBlankCB_BuyMenu);
SetMainCallback2(CB2_BuyMenu);
break;
}
}
static void BuyMenuFreeMemory(void)
{
Free(sShopData);
Free(sListMenuItems);
Free(sItemNames);
FreeAllWindowBuffers();
}
static void BuyMenuBuildListMenuTemplate(void)
{
u16 i;
sListMenuItems = Alloc((sMartInfo.itemCount + 1) * sizeof(*sListMenuItems));
sItemNames = Alloc((sMartInfo.itemCount + 1) * sizeof(*sItemNames));
for (i = 0; i < sMartInfo.itemCount; i++)
BuyMenuSetListEntry(&sListMenuItems[i], sMartInfo.itemList[i], sItemNames[i]);
StringCopy(sItemNames[i], gText_Cancel2);
sListMenuItems[i].name = sItemNames[i];
sListMenuItems[i].id = LIST_CANCEL;
gMultiuseListMenuTemplate = sShopBuyMenuListTemplate;
gMultiuseListMenuTemplate.items = sListMenuItems;
gMultiuseListMenuTemplate.totalItems = sMartInfo.itemCount + 1;
if (gMultiuseListMenuTemplate.totalItems > MAX_ITEMS_SHOWN)
gMultiuseListMenuTemplate.maxShowed = MAX_ITEMS_SHOWN;
else
gMultiuseListMenuTemplate.maxShowed = gMultiuseListMenuTemplate.totalItems;
sShopData->itemsShowed = gMultiuseListMenuTemplate.maxShowed;
}
static void BuyMenuSetListEntry(struct ListMenuItem *menuItem, u16 item, u8 *name)
{
if (sMartInfo.martType == MART_TYPE_NORMAL || MARTBP)
CopyItemName(item, name);
else if (MARTMOVE)
StringCopy(name, gMovesInfo[item].name);
else
StringCopy(name, gDecorations[item].name);
menuItem->name = name;
menuItem->id = item;
}
static void MoveTutorLoadMonIcons(u32 item)
{
DestroyPartyMonIcons();
FillWindowPixelBuffer(WIN_BATTLE_MOVE_DESC, PIXEL_FILL(1));
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, gTextSelect, 64, 0, TEXT_SKIP_DRAW, NULL); // adds "PP" text
DrawPartyMonIcons();
TintPartyMonIcons(item);
CopyWindowToVram(WIN_BATTLE_MOVE_DESC, COPYWIN_GFX);
}
static void MoveTutorLoadMoveInfo(u32 item)
{
s32 x;
const struct MoveInfo *move;
u8 buffer[32];
const u8 *str;
extern const struct TypeInfo gTypesInfo[NUMBER_OF_MON_TYPES];
FillWindowPixelBuffer(WIN_BATTLE_MOVE_DESC, PIXEL_FILL(1));
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, gTextSelect, 64, 0, TEXT_SKIP_DRAW, NULL); // adds "PP" text
str = gText_MoveRelearnerPower;
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, str, 0, 0, TEXT_SKIP_DRAW, NULL); // adds "Power" text
str = gText_MoveRelearnerAccuracy;
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, str, 0, 13, TEXT_SKIP_DRAW, NULL); // adds "Accuracy" text
str = gText_MoveRelearnerPP;
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, str, 0, 26, TEXT_SKIP_DRAW, NULL); // adds "PP" text
if (item == LIST_CANCEL)
{
CopyWindowToVram(WIN_BATTLE_MOVE_DESC, COPYWIN_GFX);
return;
}
move = &gMovesInfo[item];
str = gTypesInfo[move->type].name;
x = GetStringRightAlignXOffset(FONT_NARROW, str, 0);
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, str, x, 39, TEXT_SKIP_DRAW, NULL); // adds Type name
str = gTypesInfo[move->type].name;
x = GetStringWidth(FONT_NARROW, str, 0) + GetStringRightAlignXOffset(FONT_NARROW, str, 0);
switch (move->category)
{
case DAMAGE_CATEGORY_PHYSICAL:
str = gText_TutorPhysical;
break;
case DAMAGE_CATEGORY_SPECIAL:
str = gText_TutorSpecial;
break;
case DAMAGE_CATEGORY_STATUS:
str = gText_TutorStatus;
break;
}
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, str, x, 39, TEXT_SKIP_DRAW, NULL); // adds Physical/Special/Status text
x = 2 + GetStringWidth(FONT_NARROW, gText_MoveRelearnerPP, 0);
ConvertIntToDecimalStringN(buffer, move->pp, STR_CONV_MODE_LEFT_ALIGN, 2);
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, buffer, x, 26, TEXT_SKIP_DRAW, NULL); // adds PP value
if (move->power < 2)
{
str = gText_ThreeDashes;
}
else
{
ConvertIntToDecimalStringN(buffer, move->power, STR_CONV_MODE_LEFT_ALIGN, 3);
str = buffer;
}
x = 2 + GetStringWidth(FONT_NARROW, gText_MoveRelearnerPower, 0);
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, str, x, 0, TEXT_SKIP_DRAW, NULL); // adds Power value
if (move->accuracy == 0)
{
str = gText_ThreeDashes;
}
else
{
ConvertIntToDecimalStringN(buffer, move->accuracy, STR_CONV_MODE_LEFT_ALIGN, 3);
str = buffer;
}
x = 2 + GetStringWidth(FONT_NARROW, gText_MoveRelearnerAccuracy, 0);
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, str, x, 13, TEXT_SKIP_DRAW, NULL); // adds Accuracy value
str = gMovesInfo[item].description;
AddTextPrinterParameterized(WIN_BATTLE_MOVE_DESC, FONT_NARROW, str, 0, 65, 0, NULL);
}
static void BuyMenuPrintItemDescriptionAndShowItemIcon(s32 item, bool8 onInit, struct ListMenu *list)
{
const u8 *description;
if (onInit != TRUE)
PlaySE(SE_SELECT);
if (MARTMOVE && I_MOVE_TUTOR_INFO_BOX == TRUE)
{
if(sShowMonIcons == FALSE)
{
MoveTutorLoadMoveInfo(item);
}
else
{
MoveTutorLoadMonIcons(item);
}
}
if (sMartInfo.martType != MART_TYPE_MOVE_TUTOR)
{
if (item != LIST_CANCEL)
BuyMenuAddItemIcon(item, sShopData->iconSlot);
else
BuyMenuAddItemIcon(ITEM_LIST_END, sShopData->iconSlot);
BuyMenuRemoveItemIcon(item, sShopData->iconSlot ^ 1);
sShopData->iconSlot ^= 1;
}
if (item != LIST_CANCEL)
{
if (sMartInfo.martType == MART_TYPE_NORMAL || MARTBP)
description = ItemId_GetDescription(item);
else if (MARTMOVE)
{
FormatTextByWidth(gStringVar3, 101, FONT_NARROW, gMovesInfo[item].description, GetFontAttribute(FONT_NARROW, FONTATTR_LETTER_SPACING));
if (sNarrowerText)
FormatTextByWidth(gStringVar3, 101, FONT_NARROWER, gMovesInfo[item].description, GetFontAttribute(FONT_NARROWER, FONTATTR_LETTER_SPACING));
description = gStringVar3;
}
else
description = gDecorations[item].description;
if(item == ITEM_TM_FRUSTRATION)
description = gText_FrustrationTM;
if(item == ITEM_TM_RETURN)
description = gText_ReturnTM;
if(item == ITEM_TM_LIGHT_SCREEN)
description = gText_LightScreenTM;
if(item == ITEM_TM_REFLECT)
description = gText_ReflectTM;
if(item == ITEM_TM_SAFEGUARD)
description = gText_SafeguardTM;
if(item == ITEM_TM_FOCUS_PUNCH)
description = gText_FocusPunchTm;
if(item == ITEM_TM_BLIZZARD)
description = gText_BlizzardTm;
if(item == ITEM_TM_HYPER_BEAM)
description = gText_HyperBeamTm;
if(item == ITEM_TM_SOLAR_BEAM)
description = gText_SolarBeamTm;
if(item == ITEM_TM_THUNDER)
description = gText_ThunderTm;
if(item == ITEM_TM_FIRE_BLAST)
description = gText_FireBlastTm;
if(item == ITEM_TM_CALM_MIND)
description = gText_CalmMindTm;
if(item == ITEM_TM_ROAR)
description = gText_RoarTm;
if(item == ITEM_TM_BULK_UP)
description = gText_BulkUpTm;
if(item == ITEM_TM_PROTECT)
description = gText_ProtectTm;
if(item == ITEM_TM_ATTRACT)
description = gText_AttractTm;
}
else
{
description = gText_QuitShopping;
}
FillWindowPixelBuffer(WIN_ITEM_DESCRIPTION, PIXEL_FILL(0));
if (MARTMOVE)
BuyMenuPrint(WIN_ITEM_DESCRIPTION, description, 4, 4, 0, COLORID_NORMAL);
else
BuyMenuPrint(WIN_ITEM_DESCRIPTION, description, 3, 1, 0, COLORID_NORMAL);
}
static u16 SanitizeItemId(u16 itemId)
{
if (itemId >= ITEMS_COUNT)
return ITEM_NONE;
else
return itemId;
}
static u16 ItemId_GetBpPrice(u16 itemId)
{
if (MARTMOVE)
return gMovesInfo[itemId].bpCost;
else
return gItemsInfo[SanitizeItemId(itemId)].bpCost;
}
static void BuyMenuPrintPriceInList(u8 windowId, u32 itemId, u8 y)
{
u8 x;
if (itemId != LIST_CANCEL)
{
if (sMartInfo.martType == MART_TYPE_NORMAL)
{
ConvertIntToDecimalStringN(
gStringVar1,
ItemId_GetPrice(itemId) >> IsPokeNewsActive(POKENEWS_SLATEPORT),
STR_CONV_MODE_LEFT_ALIGN,
6);
}
else if (MARTBP)
{
ConvertIntToDecimalStringN(
gStringVar1,
ItemId_GetBpPrice(itemId),
STR_CONV_MODE_LEFT_ALIGN,
6);
}
else if (MARTMOVE)
{
ConvertIntToDecimalStringN(
gStringVar1,
ItemId_GetBpPrice(itemId),
STR_CONV_MODE_LEFT_ALIGN,
6);
}
else
{
ConvertIntToDecimalStringN(
gStringVar1,
gDecorations[itemId].price,
STR_CONV_MODE_LEFT_ALIGN,
6);
}
if (ItemId_GetImportance(itemId) && sMartInfo.martType != MART_TYPE_MOVE_TUTOR && (CheckBagHasItem(itemId, 1) || CheckPCHasItem(itemId, 1)))
StringCopy(gStringVar4, gText_SoldOut);
else if (itemId == ITEM_PROCESSOR && !CheckBagHasItem(ITEM_MEGA_RING, 1))
StringCopy(gStringVar4, gText_SoldOut);
else
{
if (MARTBP || MARTMOVE)
StringCopy(ConvertIntToDecimalStringN(gStringVar4, ItemId_GetBpPrice(itemId), STR_CONV_MODE_RIGHT_ALIGN, 4), gText_BP);
else
StringExpandPlaceholders(gStringVar4, gText_PokedollarVar1);
}
x = GetStringRightAlignXOffset(FONT_NARROW, gStringVar4, 120);
AddTextPrinterParameterized4(windowId, FONT_NARROW, x, y, 0, 0, sShopBuyMenuTextColors[COLORID_ITEM_LIST], TEXT_SKIP_DRAW, gStringVar4);
}
}
static void BuyMenuAddScrollIndicatorArrows(void)
{
if (sShopData->scrollIndicatorsTaskId == TASK_NONE && sMartInfo.itemCount + 1 > MAX_ITEMS_SHOWN)
{
sShopData->scrollIndicatorsTaskId = AddScrollIndicatorArrowPairParameterized(
SCROLL_ARROW_UP,
172,
12,
148,
sMartInfo.itemCount - (MAX_ITEMS_SHOWN - 1),
TAG_SCROLL_ARROW,
TAG_SCROLL_ARROW,
&sShopData->scrollOffset);
}
}
static void BuyMenuRemoveScrollIndicatorArrows(void)
{
if (sShopData->scrollIndicatorsTaskId != TASK_NONE)
{
RemoveScrollIndicatorArrowPair(sShopData->scrollIndicatorsTaskId);
sShopData->scrollIndicatorsTaskId = TASK_NONE;
}
}
static void BuyMenuPrintCursor(u8 scrollIndicatorsTaskId, u8 colorSet)
{
u8 y = ListMenuGetYCoordForPrintingArrowCursor(scrollIndicatorsTaskId);
BuyMenuPrint(WIN_ITEM_LIST, gText_SelectorArrow2, 0, y, 0, colorSet);
}
static void BuyMenuAddItemIcon(u16 item, u8 iconSlot)
{
u8 spriteId;
u8 *spriteIdPtr = &sShopData->itemSpriteIds[iconSlot];