forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuse_pokeblock.c
1677 lines (1517 loc) · 47.5 KB
/
use_pokeblock.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 "main.h"
#include "dma3.h"
#include "pokeblock.h"
#include "malloc.h"
#include "decompress.h"
#include "graphics.h"
#include "palette.h"
#include "pokenav.h"
#include "menu_specialized.h"
#include "scanline_effect.h"
#include "text.h"
#include "bg.h"
#include "window.h"
#include "text_window.h"
#include "constants/rgb.h"
#include "sound.h"
#include "constants/songs.h"
#include "sprite.h"
#include "string_util.h"
#include "strings.h"
#include "menu.h"
#include "gpu_regs.h"
#include "graphics.h"
#include "pokemon_summary_screen.h"
#include "item_menu.h"
/*
This file handles the screen where the player chooses
which pokemon to give a pokeblock to. The subsequent scene
of feeding the pokeblock to the pokemon is handled by
pokeblock_feed.c, and the rest of the pokeblock menu (and
other pokeblock-related functions) are in pokeblock.c
*/
enum {
WIN_NAME,
WIN_NATURE,
WIN_TEXT,
WIN_COUNT
};
#define TAG_UP_DOWN 0
#define TAG_CONDITION 1
// At any one time, the currently selected mon and its two adjacent neighbors can be loaded
// IDs to refer to one of these 3 are called "load id" in this file
#define NUM_SELECTIONS_LOADED 3
struct UsePokeblockSession
{
void (*callback)(void);
void (*exitCallback)(void);
struct Pokeblock *pokeblock;
struct Pokemon *mon;
u8 stringBuffer[64];
u8 mainState;
u8 unused1;
u8 timer;
u8 condition;
u8 numEnhancements;
u8 unused2;
bool8 monInTopHalf;
u8 conditionsBeforeBlock[CONDITION_COUNT];
u8 conditionsAfterBlock[CONDITION_COUNT];
u8 enhancements[CONDITION_COUNT];
s16 pokeblockStatBoosts[CONDITION_COUNT];
u8 numSelections; // num in party + 1 (for Cancel)
u8 curSelection;
bool8 (*loadNewSelection)(void);
u8 helperState;
u8 unused3;
u8 natureText[34];
};
// This struct is identical to PokenavMonListItem, the struct used for managing lists of Pokémon in the PokéNav
// Given that this screen is essentially duplicated in the poknav, this struct was probably the same one with
// a more general name/purpose
// TODO: Once the PokéNav conditions screens are documented, resolve the above
struct UsePokeblockMenuPokemon
{
u8 boxId; // Because this screen is never used for the PC this is always set to TOTAL_BOXES_COUNT to refer to party
u8 monId;
u16 data; // never read
};
struct UsePokeblockMenu
{
u32 unused;
u16 partyPalettes[PARTY_SIZE][0x40];
u8 partySheets[NUM_SELECTIONS_LOADED][MON_PIC_SIZE * MAX_MON_PIC_FRAMES];
u8 unusedBuffer[0x1000];
u8 tilemapBuffer[BG_SCREEN_SIZE + 2];
u8 selectionIconSpriteIds[PARTY_SIZE + 1];
s16 curMonXOffset;
u8 curMonSpriteId;
u16 curMonPalette;
u16 curMonSheet;
u8 *curMonTileStart;
struct Sprite *sparkles[MAX_CONDITION_SPARKLES];
struct Sprite *condition[2];
u8 toLoadSelection;
u8 locationStrings[NUM_SELECTIONS_LOADED][24]; // Gets an "in party" or "in box #" string that never gets printed
u8 monNameStrings[NUM_SELECTIONS_LOADED][64];
struct ConditionGraph graph;
u8 numSparkles[NUM_SELECTIONS_LOADED];
s8 curLoadId;
s8 nextLoadId;
s8 prevLoadId;
s8 toLoadId;
struct UsePokeblockMenuPokemon party[PARTY_SIZE];
struct UsePokeblockSession info;
};
static void SetUsePokeblockCallback(void (*func)(void));
static void LoadUsePokeblockMenu(void);
static void CB2_UsePokeblockMenu(void);
static void CB2_ReturnToUsePokeblockMenu(void);
static void ShowUsePokeblockMenu(void);
static void CB2_ShowUsePokeblockMenuForResults(void);
static void ShowUsePokeblockMenuForResults(void);
static void LoadPartyInfo(void);
static void LoadAndCreateSelectionIcons(void);
static u8 GetSelectionIdFromPartyId(u8);
static bool8 LoadConditionTitle(void);
static bool8 LoadUsePokeblockMenuGfx(void);
static void UpdateMonPic(u8);
static void UpdateMonInfoText(u16, bool8);
static void UsePokeblockMenu(void);
static void UpdateSelection(bool8);
static void CloseUsePokeblockMenu(void);
static void AskUsePokeblock(void);
static s8 HandleAskUsePokeblockInput(void);
static bool8 IsSheenMaxed(void);
static void PrintWontEatAnymore(void);
static void FeedPokeblockToMon(void);
static void EraseMenuWindow(void);
static u8 GetPartyIdFromSelectionId(u8);
static void ShowPokeblockResults(void);
static void CalculateConditionEnhancements(void);
static void LoadAndCreateUpDownSprites(void);
static void CalculateNumAdditionalSparkles(u8);
static void PrintFirstEnhancement(void);
static bool8 TryPrintNextEnhancement(void);
static void BufferEnhancedText(u8 *, u8, s16);
static void PrintMenuWindowText(const u8 *);
static void CalculatePokeblockEffectiveness(struct Pokeblock *, struct Pokemon *);
static void SpriteCB_UpDown(struct Sprite *);
static void LoadInitialMonInfo(void);
static void LoadMonInfo(s16, u8);
static bool8 LoadNewSelection_CancelToMon(void);
static bool8 LoadNewSelection_MonToCancel(void);
static bool8 LoadNewSelection_MonToMon(void);
static void SpriteCB_SelectionIconPokeball(struct Sprite *);
static void SpriteCB_SelectionIconCancel(struct Sprite *);
static void SpriteCB_MonPic(struct Sprite *);
static void SpriteCB_Condition(struct Sprite *);
extern const u16 gConditionGraphData_Pal[];
extern const u16 gConditionText_Pal[];
// The below 3 are saved for returning to the screen after feeding a pokeblock to a mon
// so that the rest of the data can be freed
static EWRAM_DATA struct UsePokeblockSession *sInfo = NULL;
static EWRAM_DATA void (*sExitCallback)(void) = NULL;
static EWRAM_DATA struct Pokeblock *sPokeblock = NULL;
EWRAM_DATA u8 gPokeblockMonId = 0;
EWRAM_DATA s16 gPokeblockGain = 0;
static EWRAM_DATA u8 *sGraph_Tilemap = NULL;
static EWRAM_DATA u8 *sGraph_Gfx = NULL;
static EWRAM_DATA u8 *sMonFrame_TilemapPtr = NULL;
static EWRAM_DATA struct UsePokeblockMenu *sMenu = NULL;
static const u32 sMonFrame_Pal[] = INCBIN_U32("graphics/pokeblock/use_screen/mon_frame_pal.bin");
static const u32 sMonFrame_Gfx[] = INCBIN_U32("graphics/pokeblock/use_screen/mon_frame.4bpp");
static const u32 sMonFrame_Tilemap[] = INCBIN_U32("graphics/pokeblock/use_screen/mon_frame.bin.lz");
static const u32 sGraphData_Tilemap[] = INCBIN_U32("graphics/pokeblock/use_screen/graph_data.bin.lz");
// The condition/flavors aren't listed in their normal order in this file, they're listed as shown on the graph going counter-clockwise
// Normally they would go Cool/Spicy, Beauty/Dry, Cute/Sweet, Smart/Bitter, Tough/Sour (also graph order, but clockwise)
static const u32 sConditionToMonData[CONDITION_COUNT] =
{
[CONDITION_COOL] = MON_DATA_COOL,
[CONDITION_TOUGH] = MON_DATA_TOUGH,
[CONDITION_SMART] = MON_DATA_SMART,
[CONDITION_CUTE] = MON_DATA_CUTE,
[CONDITION_BEAUTY] = MON_DATA_BEAUTY
};
static const u8 sConditionToFlavor[CONDITION_COUNT] =
{
[CONDITION_COOL] = FLAVOR_SPICY,
[CONDITION_TOUGH] = FLAVOR_SOUR,
[CONDITION_SMART] = FLAVOR_BITTER,
[CONDITION_CUTE] = FLAVOR_SWEET,
[CONDITION_BEAUTY] = FLAVOR_DRY
};
static const u8 sNatureTextColors[] =
{
TEXT_COLOR_TRANSPARENT,
TEXT_COLOR_BLUE,
TEXT_COLOR_WHITE
};
static const struct BgTemplate sBgTemplates[4] =
{
{
.bg = 0,
.charBaseIndex = 2,
.mapBaseIndex = 0x1F,
.screenSize = 0,
.paletteMode = 0,
.priority = 0,
.baseTile = 0
},
{
.bg = 1,
.charBaseIndex = 0,
.mapBaseIndex = 0x1E,
.screenSize = 0,
.paletteMode = 0,
.priority = 3,
.baseTile = 0
},
{
.bg = 3,
.charBaseIndex = 3,
.mapBaseIndex = 0x1D,
.screenSize = 0,
.paletteMode = 0,
.priority = 2,
.baseTile = 0x100
},
{
.bg = 2,
.charBaseIndex = 0,
.mapBaseIndex = 0x17,
.screenSize = 0,
.paletteMode = 0,
.priority = 1,
.baseTile = 0
}
};
static const struct WindowTemplate sWindowTemplates[WIN_COUNT + 1] =
{
[WIN_NAME] = {
.bg = 0,
.tilemapLeft = 13,
.tilemapTop = 1,
.width = 13,
.height = 4,
.paletteNum = 15,
.baseBlock = 1
},
[WIN_NATURE] = {
.bg = 0,
.tilemapLeft = 0,
.tilemapTop = 14,
.width = 11,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x35
},
[WIN_TEXT] = {
.bg = 0,
.tilemapLeft = 1,
.tilemapTop = 17,
.width = 28,
.height = 2,
.paletteNum = 15,
.baseBlock = 0x4B
},
DUMMY_WIN_TEMPLATE
};
static const struct WindowTemplate sUsePokeblockYesNoWinTemplate =
{
.bg = 0,
.tilemapLeft = 24,
.tilemapTop = 11,
.width = 5,
.height = 4,
.paletteNum = 15,
.baseBlock = 0x83
};
static const u8 *const sConditionNames[CONDITION_COUNT] =
{
[CONDITION_COOL] = gText_Coolness,
[CONDITION_TOUGH] = gText_Toughness,
[CONDITION_SMART] = gText_Smartness,
[CONDITION_CUTE] = gText_Cuteness,
[CONDITION_BEAUTY] = gText_Beauty3
};
static const struct SpriteSheet sSpriteSheet_UpDown =
{
gUsePokeblockUpDown_Gfx, 0x200, TAG_UP_DOWN
};
static const struct SpritePalette sSpritePalette_UpDown =
{
gUsePokeblockUpDown_Pal, TAG_UP_DOWN
};
static const s16 sUpDownCoordsOnGraph[CONDITION_COUNT][2] =
{
[CONDITION_COOL] = {156, 36},
[CONDITION_TOUGH] = {117, 59},
[CONDITION_SMART] = {117, 118},
[CONDITION_CUTE] = {197, 118},
[CONDITION_BEAUTY] = {197, 59}
};
static const struct OamData sOam_UpDown =
{
.y = 0,
.affineMode = ST_OAM_AFFINE_OFF,
.objMode = ST_OAM_OBJ_NORMAL,
.bpp = ST_OAM_4BPP,
.shape = SPRITE_SHAPE(32x16),
.x = 0,
.size = SPRITE_SIZE(32x16),
.tileNum = 0,
.priority = 1,
.paletteNum = 0,
};
static const union AnimCmd sAnim_Up[] =
{
ANIMCMD_FRAME(0, 5),
ANIMCMD_END
};
static const union AnimCmd sAnim_Down[] =
{
ANIMCMD_FRAME(8, 5),
ANIMCMD_END
};
static const union AnimCmd *const sAnims_UpDown[] =
{
sAnim_Up,
sAnim_Down
};
static const struct SpriteTemplate sSpriteTemplate_UpDown =
{
.tileTag = TAG_UP_DOWN,
.paletteTag = TAG_UP_DOWN,
.oam = &sOam_UpDown,
.anims = sAnims_UpDown,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
static const struct OamData sOam_Condition =
{
.y = 0,
.affineMode = ST_OAM_AFFINE_OFF,
.objMode = ST_OAM_OBJ_NORMAL,
.bpp = ST_OAM_4BPP,
.shape = SPRITE_SHAPE(64x32),
.x = 0,
.size = SPRITE_SIZE(64x32),
.tileNum = 0,
.priority = 1,
.paletteNum = 0,
};
static const union AnimCmd sAnim_Condition_0[] =
{
ANIMCMD_FRAME(0, 5),
ANIMCMD_END
};
static const union AnimCmd sAnim_Condition_1[] =
{
ANIMCMD_FRAME(32, 5),
ANIMCMD_END
};
static const union AnimCmd sAnim_Condition_2[] =
{
ANIMCMD_FRAME(64, 5),
ANIMCMD_END
};
static const union AnimCmd *const sAnims_Condition[] =
{
sAnim_Condition_0,
sAnim_Condition_1,
sAnim_Condition_2
};
static const struct SpriteTemplate sSpriteTemplate_Condition =
{
.tileTag = TAG_CONDITION,
.paletteTag = TAG_CONDITION,
.oam = &sOam_Condition,
.anims = sAnims_Condition,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCB_Condition,
};
static const struct SpritePalette sSpritePalette_Condition =
{
gUsePokeblockCondition_Pal, TAG_CONDITION
};
// When first opening the selection screen
void ChooseMonToGivePokeblock(struct Pokeblock *pokeblock, void (*callback)(void))
{
sMenu = AllocZeroed(sizeof(*sMenu));
sInfo = &sMenu->info;
sInfo->pokeblock = pokeblock;
sInfo->exitCallback = callback;
SetUsePokeblockCallback(LoadUsePokeblockMenu);
SetMainCallback2(CB2_UsePokeblockMenu);
}
// When returning to the selection screen after feeding a pokeblock to a mon
static void CB2_ReturnAndChooseMonToGivePokeblock(void)
{
sMenu = AllocZeroed(sizeof(*sMenu));
sInfo = &sMenu->info;
sInfo->pokeblock = sPokeblock;
sInfo->exitCallback = sExitCallback;
gPokeblockMonId = GetSelectionIdFromPartyId(gPokeblockMonId);
sInfo->monInTopHalf = (gPokeblockMonId <= PARTY_SIZE / 2) ? FALSE : TRUE;
SetUsePokeblockCallback(LoadUsePokeblockMenu);
SetMainCallback2(CB2_ReturnToUsePokeblockMenu);
}
static void CB2_ReturnToUsePokeblockMenu(void)
{
sInfo->callback();
AnimateSprites();
BuildOamBuffer();
UpdatePaletteFade();
if (sInfo->callback == ShowUsePokeblockMenu)
{
sInfo->mainState = 0;
SetMainCallback2(CB2_ShowUsePokeblockMenuForResults);
}
}
static void CB2_ShowUsePokeblockMenuForResults(void)
{
ShowUsePokeblockMenuForResults();
AnimateSprites();
BuildOamBuffer();
UpdatePaletteFade();
}
static void CB2_UsePokeblockMenu(void)
{
sInfo->callback();
AnimateSprites();
BuildOamBuffer();
RunTextPrinters();
UpdatePaletteFade();
}
static void VBlankCB_UsePokeblockMenu(void)
{
LoadOam();
ProcessSpriteCopyRequests();
TransferPlttBuffer();
ConditionGraph_Draw(&sMenu->graph);
ScanlineEffect_InitHBlankDmaTransfer();
}
static void SetUsePokeblockCallback(void (*func)(void))
{
sInfo->callback = func;
sInfo->mainState = 0;
}
static void LoadUsePokeblockMenu(void)
{
switch (sInfo->mainState)
{
case 0:
sMenu->curMonSpriteId = SPRITE_NONE;
ConditionGraph_Init(&sMenu->graph);
sInfo->mainState++;
break;
case 1:
ResetSpriteData();
FreeAllSpritePalettes();
sInfo->mainState++;
break;
case 2:
SetVBlankCallback(NULL);
CpuFill32(0, (void *)(VRAM), VRAM_SIZE);
sInfo->mainState++;
break;
case 3:
ResetBgsAndClearDma3BusyFlags(0);
InitBgsFromTemplates(0, sBgTemplates, ARRAY_COUNT(sBgTemplates));
InitWindows(sWindowTemplates);
DeactivateAllTextPrinters();
LoadUserWindowBorderGfx(0, 0x97, BG_PLTT_ID(14));
sInfo->mainState++;
break;
case 4:
sInfo->mainState++;
break;
case 5:
if (!LoadConditionTitle())
sInfo->mainState++;
break;
case 6:
gKeyRepeatStartDelay = 20;
LoadPartyInfo();
sInfo->mainState++;
break;
case 7:
if (!LoadUsePokeblockMenuGfx())
sInfo->mainState++;
break;
case 8:
UpdateMonPic(0);
LoadAndCreateSelectionIcons();
sInfo->mainState++;
break;
case 9:
if (!MoveConditionMonOnscreen(&sMenu->curMonXOffset))
sInfo->mainState++;
break;
case 10:
sInfo->mainState++;
break;
case 11:
ConditionGraph_CalcPositions(sMenu->graph.conditions[0], sMenu->graph.savedPositions[0]);
ConditionGraph_InitResetScanline(&sMenu->graph);
sInfo->mainState++;
break;
case 12:
if (!ConditionGraph_ResetScanline(&sMenu->graph))
{
ConditionGraph_SetNewPositions(&sMenu->graph, sMenu->graph.savedPositions[0], sMenu->graph.savedPositions[0]);
sInfo->mainState++;
}
break;
case 13:
ConditionGraph_Update(&sMenu->graph);
sInfo->mainState++;
break;
case 14:
PutWindowTilemap(WIN_NAME);
PutWindowTilemap(WIN_NATURE);
UpdateMonInfoText(0, TRUE);
sInfo->mainState++;
break;
case 15:
SetUsePokeblockCallback(ShowUsePokeblockMenu);
break;
}
}
static void ShowUsePokeblockMenu(void)
{
switch (sInfo->mainState)
{
case 0:
BeginNormalPaletteFade(PALETTES_ALL, 0, 16, 0, RGB_BLACK);
SetVBlankCallback(VBlankCB_UsePokeblockMenu);
ShowBg(0);
ShowBg(1);
ShowBg(3);
ShowBg(2);
sInfo->mainState++;
break;
case 1:
if (!gPaletteFade.active)
{
ResetConditionSparkleSprites(sMenu->sparkles);
if (sMenu->info.curSelection != sMenu->info.numSelections - 1)
{
u8 numSparkles = sMenu->numSparkles[sMenu->curLoadId];
CreateConditionSparkleSprites(sMenu->sparkles, sMenu->curMonSpriteId, numSparkles);
}
SetUsePokeblockCallback(UsePokeblockMenu);
}
break;
}
}
enum {
STATE_HANDLE_INPUT,
STATE_UPDATE_SELECTION,
STATE_2, // unused state
STATE_CLOSE,
STATE_4, // unused state
STATE_CONFIRM_SELECTION,
STATE_HANDLE_CONFIRMATION,
STATE_WAIT_MSG,
};
static void UsePokeblockMenu(void)
{
bool8 loading;
switch (sInfo->mainState)
{
case STATE_HANDLE_INPUT:
if (JOY_HELD(DPAD_UP))
{
PlaySE(SE_SELECT);
UpdateSelection(TRUE);
DestroyConditionSparkleSprites(sMenu->sparkles);
sInfo->mainState = STATE_UPDATE_SELECTION;
}
else if (JOY_HELD(DPAD_DOWN))
{
PlaySE(SE_SELECT);
UpdateSelection(FALSE);
DestroyConditionSparkleSprites(sMenu->sparkles);
sInfo->mainState = STATE_UPDATE_SELECTION;
}
else if (JOY_NEW(B_BUTTON))
{
PlaySE(SE_SELECT);
sInfo->mainState = STATE_CLOSE;
}
else if (JOY_NEW(A_BUTTON))
{
PlaySE(SE_SELECT);
// If last item, selected Cancel. Otherwise selected mon
if (sMenu->info.curSelection == sMenu->info.numSelections - 1)
sInfo->mainState = STATE_CLOSE;
else
sInfo->mainState = STATE_CONFIRM_SELECTION;
}
break;
case STATE_UPDATE_SELECTION:
loading = sMenu->info.loadNewSelection();
if (!loading)
sInfo->mainState = STATE_HANDLE_INPUT;
break;
case STATE_2:
break;
case STATE_CLOSE:
SetUsePokeblockCallback(CloseUsePokeblockMenu);
break;
case STATE_4:
break;
case STATE_CONFIRM_SELECTION:
AskUsePokeblock();
sInfo->mainState++;
break;
case STATE_HANDLE_CONFIRMATION:
switch (HandleAskUsePokeblockInput())
{
case 1: // NO
case MENU_B_PRESSED:
sInfo->mainState = STATE_HANDLE_INPUT;
break;
case 0: // YES
if (IsSheenMaxed())
{
PrintWontEatAnymore();
sInfo->mainState = STATE_WAIT_MSG;
}
else
{
SetUsePokeblockCallback(FeedPokeblockToMon);
}
break;
}
break;
case STATE_WAIT_MSG:
if (JOY_NEW(A_BUTTON | B_BUTTON))
{
EraseMenuWindow();
sInfo->mainState = STATE_HANDLE_INPUT;
}
break;
}
}
static void FeedPokeblockToMon(void)
{
switch (sInfo->mainState)
{
case 0:
gPokeblockMonId = GetPartyIdFromSelectionId(sMenu->info.curSelection);
sExitCallback = sInfo->exitCallback;
sPokeblock = sInfo->pokeblock;
BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 16, RGB_BLACK);
sInfo->mainState++;
break;
case 1:
if (!gPaletteFade.active)
{
SetVBlankCallback(NULL);
FREE_AND_SET_NULL(sGraph_Tilemap);
FREE_AND_SET_NULL(sGraph_Gfx);
FREE_AND_SET_NULL(sMonFrame_TilemapPtr);
FREE_AND_SET_NULL(sMenu);
FreeAllWindowBuffers();
gMain.savedCallback = CB2_ReturnAndChooseMonToGivePokeblock;
PreparePokeblockFeedScene();
}
break;
}
}
static void ShowUsePokeblockMenuForResults(void)
{
bool8 loading;
switch (sInfo->mainState)
{
case 0:
if (sMenu->info.curSelection != gPokeblockMonId)
{
UpdateSelection(sInfo->monInTopHalf);
sInfo->mainState++;
}
else
{
sInfo->mainState = 3;
}
break;
case 1:
loading = sMenu->info.loadNewSelection();
if (!loading)
sInfo->mainState = 0;
break;
case 2:
break;
case 3:
BlendPalettes(PALETTES_ALL, 16, RGB_BLACK);
sInfo->mainState++;
break;
case 4:
ShowBg(0);
ShowBg(1);
ShowBg(3);
ShowBg(2);
sInfo->mainState++;
break;
case 5:
SetVBlankCallback(VBlankCB_UsePokeblockMenu);
BeginNormalPaletteFade(PALETTES_ALL, 0, 16, 0, RGB_BLACK);
sInfo->mainState++;
break;
case 6:
if (!gPaletteFade.active)
{
ResetConditionSparkleSprites(sMenu->sparkles);
SetUsePokeblockCallback(ShowPokeblockResults);
SetMainCallback2(CB2_UsePokeblockMenu);
}
break;
}
}
static void ShowPokeblockResults(void)
{
switch (sInfo->mainState)
{
case 0:
sInfo->mon = gPlayerParty;
sInfo->mon += sMenu->party[sMenu->info.curSelection].monId;
DestroyConditionSparkleSprites(sMenu->sparkles);
sInfo->mainState++;
break;
case 1:
if (JOY_NEW(A_BUTTON | B_BUTTON))
sInfo->mainState++;
break;
case 2:
CalculateConditionEnhancements();
ConditionGraph_CalcPositions(sInfo->conditionsAfterBlock, sMenu->graph.savedPositions[CONDITION_GRAPH_LOAD_MAX - 1]);
ConditionGraph_SetNewPositions(&sMenu->graph, sMenu->graph.savedPositions[sMenu->curLoadId], sMenu->graph.savedPositions[CONDITION_GRAPH_LOAD_MAX - 1]);
LoadAndCreateUpDownSprites();
sInfo->mainState++;
break;
case 3:
if (!ConditionGraph_TryUpdate(&sMenu->graph))
{
CalculateNumAdditionalSparkles(GetPartyIdFromSelectionId(sMenu->info.curSelection));
if (sMenu->info.curSelection != sMenu->info.numSelections - 1)
{
u8 numSparkles = sMenu->numSparkles[sMenu->curLoadId];
CreateConditionSparkleSprites(sMenu->sparkles, sMenu->curMonSpriteId, numSparkles);
}
sInfo->timer = 0;
sInfo->mainState++;
}
break;
case 4:
if (++sInfo->timer > 16)
{
PrintFirstEnhancement();
sInfo->mainState++;
}
break;
case 5:
if (JOY_NEW(A_BUTTON | B_BUTTON) && !TryPrintNextEnhancement())
{
TryClearPokeblock((u8)gSpecialVar_ItemId);
SetUsePokeblockCallback(CloseUsePokeblockMenu);
}
break;
}
}
static void CloseUsePokeblockMenu(void)
{
u8 i;
switch (sInfo->mainState)
{
case 0:
BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 16, RGB_BLACK);
sInfo->mainState++;
break;
case 1:
if (!gPaletteFade.active)
sInfo->mainState = 2;
break;
case 2:
gScanlineEffect.state = 3;
ScanlineEffect_InitHBlankDmaTransfer();
sInfo->mainState++;
break;
case 3:
SetMainCallback2(sInfo->exitCallback);
FreeConditionSparkles(sMenu->sparkles);
for (i = 0; i < ARRAY_COUNT(sMenu->selectionIconSpriteIds); i++)
DestroySprite(&gSprites[sMenu->selectionIconSpriteIds[i]]);
FreeSpriteTilesByTag(TAG_UP_DOWN);
FreeSpriteTilesByTag(TAG_CONDITION);
FreeSpritePaletteByTag(TAG_UP_DOWN);
FreeSpritePaletteByTag(TAG_CONDITION);
for (i = 0; i < ARRAY_COUNT(sMenu->condition); i++)
DestroySprite(sMenu->condition[i]);
if (sMenu->curMonSpriteId != SPRITE_NONE)
DestroySprite(&gSprites[sMenu->curMonSpriteId]);
SetVBlankCallback(NULL);
FREE_AND_SET_NULL(sGraph_Tilemap);
FREE_AND_SET_NULL(sGraph_Gfx);
FREE_AND_SET_NULL(sMonFrame_TilemapPtr);
FREE_AND_SET_NULL(sMenu);
FreeAllWindowBuffers();
break;
}
}
static void AskUsePokeblock(void)
{
u8 stringBuffer[0x40];
GetMonData(&gPlayerParty[GetPartyIdFromSelectionId(sMenu->info.curSelection)], MON_DATA_NICKNAME, stringBuffer);
StringGet_Nickname(stringBuffer);
StringAppend(stringBuffer, gText_GetsAPokeBlockQuestion);
StringCopy(gStringVar4, stringBuffer);
FillWindowPixelBuffer(WIN_TEXT, 17);
DrawTextBorderOuter(WIN_TEXT, 151, 14);
AddTextPrinterParameterized(WIN_TEXT, FONT_NORMAL, gStringVar4, 0, 1, 0, NULL);
PutWindowTilemap(WIN_TEXT);
CopyWindowToVram(WIN_TEXT, COPYWIN_FULL);
CreateYesNoMenu(&sUsePokeblockYesNoWinTemplate, 151, 14, 0);
}
static s8 HandleAskUsePokeblockInput(void)
{
s8 menuItem = Menu_ProcessInputNoWrapClearOnChoose();
switch (menuItem)
{
case 0: // YES
break;
case MENU_B_PRESSED:
case 1: // NO
PlaySE(SE_SELECT);
rbox_fill_rectangle(2);
ClearWindowTilemap(2);
break;
}
return menuItem;
}
static void PrintFirstEnhancement(void)
{
DrawTextBorderOuter(WIN_TEXT, 151, 14);
FillWindowPixelBuffer(WIN_TEXT, 17);
for (sInfo->condition = 0; sInfo->condition < CONDITION_COUNT; sInfo->condition++)
{
if (sInfo->enhancements[sInfo->condition] != 0)
break;
}
if (sInfo->condition < CONDITION_COUNT)
BufferEnhancedText(gStringVar4, sInfo->condition, sInfo->enhancements[sInfo->condition]);
else
BufferEnhancedText(gStringVar4, sInfo->condition, 0);
PrintMenuWindowText(gStringVar4);
PutWindowTilemap(WIN_TEXT);
CopyWindowToVram(WIN_TEXT, COPYWIN_FULL);
}
static bool8 TryPrintNextEnhancement(void)
{
FillWindowPixelBuffer(WIN_TEXT, 17);
while (1)
{
sInfo->condition++;
if (sInfo->condition < CONDITION_COUNT)
{
if (sInfo->enhancements[sInfo->condition] != 0)
break;
}
else
{
sInfo->condition = CONDITION_COUNT;
return FALSE;
}
}
BufferEnhancedText(gStringVar4, sInfo->condition, sInfo->enhancements[sInfo->condition]);
PrintMenuWindowText(gStringVar4);
CopyWindowToVram(WIN_TEXT, COPYWIN_GFX);
return TRUE;
}
static void PrintWontEatAnymore(void)
{
FillWindowPixelBuffer(WIN_TEXT, 17);
DrawTextBorderOuter(WIN_TEXT, 151, 14);
AddTextPrinterParameterized(WIN_TEXT, FONT_NORMAL, gText_WontEatAnymore, 0, 1, 0, NULL);
PutWindowTilemap(WIN_TEXT);
CopyWindowToVram(WIN_TEXT, COPYWIN_FULL);
}
static void EraseMenuWindow(void)
{
rbox_fill_rectangle(WIN_TEXT);
ClearWindowTilemap(WIN_TEXT);
CopyWindowToVram(WIN_TEXT, COPYWIN_FULL);
}
static void PrintMenuWindowText(const u8 *message)
{
AddTextPrinterParameterized(WIN_TEXT, FONT_NORMAL, gStringVar4, 0, 1, 0, NULL);
}
static void BufferEnhancedText(u8 *dest, u8 condition, s16 enhancement)
{
switch (enhancement)
{
case 1 ... 32767: // if > 0
enhancement = 0;
// fallthrough
case -32768 ... -1: // if < 0
if (enhancement)
dest[(u16)enhancement] += 0; // something you can't imagine
StringCopy(dest, sConditionNames[condition]);
StringAppend(dest, gText_WasEnhanced);
break;
case 0:
StringCopy(dest, gText_NothingChanged);
break;
}
}
static void GetMonConditions(struct Pokemon *mon, u8 *data)
{
u16 i;
for (i = 0; i < CONDITION_COUNT; i++)
data[i] = GetMonData(mon, sConditionToMonData[i]);
}
static void AddPokeblockToConditions(struct Pokeblock *pokeblock, struct Pokemon *mon)
{
u16 i;
s16 stat;
u8 data;