forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdodrio_berry_picking.c
5233 lines (4789 loc) · 153 KB
/
dodrio_berry_picking.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 "malloc.h"
#include "bg.h"
#include "dodrio_berry_picking.h"
#include "dynamic_placeholder_text_util.h"
#include "event_data.h"
#include "gpu_regs.h"
#include "international_string_util.h"
#include "item.h"
#include "link.h"
#include "link_rfu.h"
#include "m4a.h"
#include "main.h"
#include "palette.h"
#include "minigame_countdown.h"
#include "random.h"
#include "save.h"
#include "script.h"
#include "sound.h"
#include "string_util.h"
#include "strings.h"
#include "task.h"
#include "text_window.h"
#include "window.h"
#include "constants/items.h"
#include "constants/songs.h"
// Note that in this file 'Dodrio Berry Picking' is often
// shortened to DodrioGame or just Game for convenience
#define MAX_SCORE 999990
#define MAX_BERRIES 9999
// The minimum score needed to receive a prize
#define PRIZE_SCORE 3000
// Difficulty increases as berries are eaten. The rate of new berries increases and the types of berries changes
// When the max difficulty is reached it starts again from the beginning
#define NUM_DIFFICULTIES 7
#define MAX_FALL_DIST 10 // The number of times a berry needs to fall before hitting the ground
#define EAT_FALL_DIST 7 // The number of times a berry needs to fall to be available to eat
#if FRENCH
#define RESULTS_TITLE_FONT FONT_NORMAL
#elif ITALIAN
#define RESULTS_TITLE_FONT FONT_NARROW
#else //ENGLISH
#define RESULTS_TITLE_FONT FONT_NORMAL
#endif
enum {
BG_INTERFACE,
BG_TREE_LEFT,
BG_TREE_RIGHT,
BG_SCENERY
};
enum {
FUNC_INTRO,
FUNC_INIT_COUNTDOWN,
FUNC_COUNTDOWN,
FUNC_WAIT_START,
FUNC_PLAY_GAME,
FUNC_INIT_RESULTS,
FUNC_RESULTS,
FUNC_ASK_PLAY_AGAIN,
FUNC_END_LINK,
FUNC_EXIT,
FUNC_RESET_GAME,
FUNC_WAIT_END_GAME,
};
enum {
GFXFUNC_LOAD,
GFXFUNC_SHOW_NAMES,
GFXFUNC_SHOW_RESULTS,
GFXFUNC_MSG_PLAY_AGAIN,
GFXFUNC_MSG_SAVING,
GFXFUNC_MSG_COMM_STANDBY,
GFXFUNC_ERASE_MSG,
GFXFUNC_MSG_PLAYER_DROPPED,
GFXFUNC_STOP,
GFXFUNC_IDLE,
};
enum {
PACKET_READY_START = 1,
PACKET_GAME_STATE,
PACKET_PICK_STATE,
PACKET_READY_END,
};
enum {
PLAY_AGAIN_NONE,
PLAY_AGAIN_YES,
PLAY_AGAIN_NO,
PLAY_AGAIN_DROPPED = 5,
};
enum {
PICK_NONE, // Dodrio standing still
PICK_RIGHT, // Dodrio reaching right
PICK_MIDDLE, // Dodrio reaching up
PICK_LEFT, // Dodrio reaching left
PICK_DISABLED, // Dodrio down after game over
};
enum {
BERRY_BLUE,
BERRY_GREEN,
BERRY_GOLD,
BERRY_MISSED,
BERRY_PRIZE,
BERRY_IN_ROW,
NUM_BERRY_IDS
};
#define NUM_BERRY_TYPES 4 // Blue, Green, Gold, and 'missed'
// Eaten anim comes after the normal & missed versions of other berries
#define ANIM_EATEN (BERRY_MISSED * 2)
enum {
BERRYSTATE_NONE,
BERRYSTATE_PICKED, // Berry has been picked by a Dodrio, replaced with blue hit sprite (still falling)
BERRYSTATE_EATEN, // Berry has been eaten (after being picked), berry is gone now
BERRYSTATE_SQUISHED, // Berry has hit the ground
};
enum {
INPUTSTATE_NONE,
INPUTSTATE_TRY_PICK,
INPUTSTATE_PICKED,
INPUTSTATE_ATE_BERRY,
INPUTSTATE_BAD_MISS,
};
// Colors for status bar squares
// Colored gray when a berry is missed
// Flash red when few yellow squares remain
enum {
STATUS_YELLOW,
STATUS_GRAY,
STATUS_RED,
};
#define NUM_STATUS_SQUARES 10
// Berries fall in predefined columns.
// A total of 10 are available, though fewer will be used with < 5 players
// The 11th column is a repeat of the 1st column wrapped around, so only
// the values 0-9 are unique 'valid' columns
#define NUM_BERRY_COLUMNS 11
#define GFXTAG_DODRIO 0
#define GFXTAG_STATUS 1
#define GFXTAG_BERRIES 2
#define GFXTAG_CLOUD 5
#define GFXTAG_COUNTDOWN 7
#define PALTAG_DODRIO_NORMAL 0
#define PALTAG_DODRIO_SHINY 1
#define PALTAG_STATUS 2
#define PALTAG_BERRIES 3
#define PALTAG_CLOUD 6
#define PALTAG_COUNTDOWN 8
#define NUM_CLOUDS 2
#define PLAYER_NONE 0xFF
struct DodrioGame_Gfx
{
u16 ALIGNED(4) tilemapBuffers[3][BG_SCREEN_SIZE];
bool32 finished;
u8 ALIGNED(4) taskId;
u8 ALIGNED(4) windowIds[MAX_RFU_PLAYERS + 5]; // The latter 5 are never used
u8 ALIGNED(4) state;
u8 ALIGNED(4) loadState;
u16 ALIGNED(4) timer;
u8 ALIGNED(4) cursorSelection;
u8 ALIGNED(4) playAgainState;
void (*func)(void);
}; // size = 0x302C
struct StatusBar
{
u8 unused[12];
bool8 entered[NUM_STATUS_SQUARES];
s16 yChange[NUM_STATUS_SQUARES];
u16 spriteIds[NUM_STATUS_SQUARES];
u16 flashTimer;
}; // size = 0x40
struct DodrioGame_Berries
{
u8 ids[NUM_BERRY_COLUMNS];
u8 fallDist[NUM_BERRY_COLUMNS];
};
struct DodrioGame_PlayerCommData
{
u8 pickState;
bool8 ALIGNED(4) ateBerry;
bool8 ALIGNED(4) missedBerry;
};
struct DodrioGame_Player
{
u8 name[16];
bool32 receivedGameStatePacket; // Never read
struct DodrioGame_Berries berries;
struct DodrioGame_PlayerCommData comm;
u32 unused;
}; // size = 0x3C
// Because Dodrio is required for this minigame,
// the only relevant information about the selected
// Pokémon is whether or not it's shiny
struct DodrioGame_MonInfo
{
bool8 isShiny;
};
struct DodrioGame_ScoreResults
{
u8 ranking;
u32 score;
};
struct DodrioGame
{
/*0x0000*/ void (*exitCallback)(void);
/*0x0004*/ u8 ALIGNED(4) taskId;
/*0x0008*/ u8 ALIGNED(4) playersReceived;
/*0x000C*/ u8 ALIGNED(4) startState;
/*0x0010*/ u8 ALIGNED(4) state;
/*0x0014*/ u8 ALIGNED(4) timer;
/*0x0018*/ u8 ALIGNED(4) funcId;
/*0x001C*/ u8 ALIGNED(4) prevFuncId; // Set, never read
/*0x0020*/ bool8 ALIGNED(4) isLeader;
/*0x0024*/ u8 ALIGNED(4) numPlayers;
/*0x0028*/ u8 ALIGNED(4) multiplayerId;
/*0x0029*/ u8 unused1[7];
/*0x0030*/ u8 ALIGNED(4) countdownEndDelay;
/*0x0034*/ u8 ALIGNED(4) posToPlayerId[MAX_RFU_PLAYERS];
/*0x003C*/ u8 ALIGNED(4) unused2; // Set to 0, never read
/*0x0040*/ u8 ALIGNED(4) numGraySquares;
/*0x0044*/ u8 ALIGNED(4) berryColStart;
/*0x0048*/ u8 ALIGNED(4) berryColEnd;
/*0x004A*/ u16 berryResults[MAX_RFU_PLAYERS][NUM_BERRY_IDS];
/*0x0086*/ u16 berriesEaten[MAX_RFU_PLAYERS];
/*0x0090*/ u8 ALIGNED(4) difficulty[MAX_RFU_PLAYERS];
/*0x0098*/ u8 ALIGNED(4) pickStateQueue[4];
/*0x009C*/ u8 ALIGNED(4) eatTimer[NUM_BERRY_COLUMNS];
/*0x00A8*/ u8 ALIGNED(4) inputState[MAX_RFU_PLAYERS];
/*0x00B0*/ u8 ALIGNED(4) inputDelay[MAX_RFU_PLAYERS];
/*0x00B8*/ u8 ALIGNED(4) berryEatenBy[NUM_BERRY_COLUMNS];
/*0x00C4*/ u8 ALIGNED(4) berryState[NUM_BERRY_COLUMNS];
/*0x00D0*/ u8 ALIGNED(4) fallTimer[NUM_BERRY_COLUMNS];
/*0x00DC*/ u8 ALIGNED(4) newBerryTimer[NUM_BERRY_COLUMNS];
/*0x00E8*/ u8 ALIGNED(4) prevBerryIds[NUM_BERRY_COLUMNS];
/*0x00F4*/ u8 ALIGNED(4) playersAttemptingPick[NUM_BERRY_COLUMNS][2];
/*0x010C*/ u8 ALIGNED(4) playAgainStates[MAX_RFU_PLAYERS];
/*0x0112*/ u16 berriesPickedInRow;
/*0x0114*/ u16 maxBerriesPickedInRow;
/*0x0118*/ bool32 startCountdown; // Never read
/*0x011C*/ bool32 startGame;
/*0x0120*/ bool32 berriesFalling;
/*0x0124*/ u8 ALIGNED(4) clearRecvCmdTimer;
/*0x0128*/ bool8 ALIGNED(4) clearRecvCmds;
/*0x012C*/ bool32 allReadyToEnd;
/*0x0130*/ bool32 readyToEnd[MAX_RFU_PLAYERS];
/*0x0144*/ bool8 ALIGNED(4) playingPickSound;
/*0x0148*/ bool8 ALIGNED(4) playingSquishSound[NUM_BERRY_COLUMNS];
/*0x0154*/ u8 ALIGNED(4) endSoundState;
/*0x0158*/ bool8 ALIGNED(4) readyToStart[MAX_RFU_PLAYERS];
/*0x0160*/ struct DodrioGame_Gfx gfx;
/*0x318C*/ struct DodrioGame_MonInfo monInfo[MAX_RFU_PLAYERS];
/*0x31A0*/ struct DodrioGame_Player players[MAX_RFU_PLAYERS];
/*0x32CC*/ struct DodrioGame_Player player;
/*0x3308*/ struct DodrioGame_ScoreResults scoreResults[MAX_RFU_PLAYERS];
}; // size = 0x3330
EWRAM_DATA static struct DodrioGame * sGame = NULL;
EWRAM_DATA static u16 * sDodrioSpriteIds[MAX_RFU_PLAYERS] = {NULL};
EWRAM_DATA static u16 * sCloudSpriteIds[NUM_CLOUDS] = {NULL};
EWRAM_DATA static u16 * sBerrySpriteIds[NUM_BERRY_COLUMNS] = {NULL};
EWRAM_DATA static u16 * sBerryIconSpriteIds[NUM_BERRY_TYPES] = {NULL};
EWRAM_DATA static struct StatusBar * sStatusBar = NULL;
EWRAM_DATA static struct DodrioGame_Gfx * sGfx = NULL;
static bool32 sExitingGame;
static void ResetTasksAndSprites(void);
static void InitDodrioGame(struct DodrioGame *);
static void Task_StartDodrioGame(u8);
static void DoGameIntro(void);
static void InitCountdown(void);
static void DoCountdown(void);
static void WaitGameStart(void);
static void PlayGame_Leader(void);
static void PlayGame_Member(void);
static void WaitEndGame_Leader(void);
static void WaitEndGame_Member(void);
static void InitResults_Leader(void);
static void InitResults_Member(void);
static void DoResults(void);
static void AskPlayAgain(void);
static void EndLink(void);
static void ExitGame(void);
static void ResetGame(void);
static void Task_NewGameIntro(u8);
static void Task_CommunicateMonInfo(u8);
static void RecvLinkData_Leader(void);
static void SendLinkData_Leader(void);
static void RecvLinkData_Member(void);
static void SendLinkData_Member(void);
static void HandleSound_Leader(void);
static void HandleSound_Member(void);
static void CB2_DodrioGame(void);
static void VBlankCB_DodrioGame(void);
static void InitMonInfo(struct DodrioGame_MonInfo *, struct Pokemon *);
static void CreateTask_(TaskFunc, u8);
static void CreateDodrioGameTask(TaskFunc);
static void SetGameFunc(u8);
static bool32 SlideTreeBordersOut(void);
static void InitFirstWaveOfBerries(void);
static bool32 TryPickBerry(u8, u8, u8);
static void UpdateFallingBerries(void);
static void UpdateGame_Leader(void);
static void UpdateGame_Member(void);
static void GetActiveBerryColumns(u8, u8 *, u8 *);
static bool32 AllPlayersReadyToStart(void);
static void ResetReadyToStart(void);
static bool32 ReadyToEndGame_Leader(void);
static bool32 ReadyToEndGame_Member(void);
static void TryIncrementDifficulty(u8);
static u8 GetPlayerIdAtColumn(u8);
static u8 GetNewBerryId(u8, u8);
static void IncrementBerryResult(u8, u8, u8);
static void UpdateBerriesPickedInRow(bool32);
static void SetMaxBerriesPickedInRow(void);
static void ResetForPlayAgainPrompt(void);
static void SetRandomPrize(void);
static void TryUpdateRecords(void);
static u8 UpdatePickStateQueue(u8);
static void HandleWaitPlayAgainInput(void);
static void ResetPickState(void);
static u32 GetHighestScore(void);
static void SendPacket_ReadyToStart(bool32);
static void SendPacket_GameState(struct DodrioGame_Player *,
struct DodrioGame_PlayerCommData *,
struct DodrioGame_PlayerCommData *,
struct DodrioGame_PlayerCommData *,
struct DodrioGame_PlayerCommData *,
struct DodrioGame_PlayerCommData *,
u8 , bool32 , bool32 );
static bool32 RecvPacket_GameState(u32,
struct DodrioGame_Player *,
struct DodrioGame_PlayerCommData *,
struct DodrioGame_PlayerCommData *,
struct DodrioGame_PlayerCommData *,
struct DodrioGame_PlayerCommData *,
struct DodrioGame_PlayerCommData *,
u8 *, bool32 *, bool32 *);
static void SendPacket_PickState(u8);
static bool32 RecvPacket_PickState(u32, u8 *);
static void SendPacket_ReadyToEnd(bool32);
static bool32 RecvPacket_ReadyToEnd(u32);
static void LoadDodrioGfx(void);
static void CreateDodrioSprite(struct DodrioGame_MonInfo *, u8, u8, u8);
static void StartDodrioMissedAnim(u8);
static void StartDodrioIntroAnim(u8);
static void FreeDodrioSprites(u8);
static void SetAllDodrioInvisibility(bool8, u8);
static void CreateStatusBarSprites(void);
static void FreeStatusBar(void);
static void SetStatusBarInvisibility(bool8);
static void InitStatusBarPos(void);
static bool32 DoStatusBarIntro(void);
static void LoadBerryGfx(void);
static void CreateBerrySprites(void);
static void FreeBerrySprites(void);
static void CreateCloudSprites(void);
static void ResetCloudPos(void);
static void StartCloudMovement(void);
static void FreeCloudSprites(void);
static void SetCloudInvisibility(bool8);
static void ResetBerryAndStatusBarSprites(void);
static void ResetGfxState(void);
static void InitGameGfx(struct DodrioGame_Gfx *);
static void SetGfxFuncById(u8);
static bool32 IsGfxFuncActive(void);
static u8 GetPlayAgainState(void);
static void SetBerryInvisibility(u8, bool8);
static void SetBerryIconsInvisibility(bool8);
static void SetBerryAnim(u16, u8);
static void SetBerryYPos(u8, u8);
static void SetDodrioAnim(u8, u8);
static u8 GetNewBerryIdByDifficulty(u8, u8);
static void UpdateStatusBarAnim(u8);
static u32 RecvPacket_ReadyToStart(u32);
static u32 IncrementWithLimit(u32, u32);
static u32 Min(u32, u32);
static u32 GetScore(u8);
static void Task_ShowDodrioBerryPickingRecords(u8);
static void Task_TryRunGfxFunc(u8);
static void PrintRecordsText(u8, s32);
static void SpriteCB_Status(struct Sprite *);
static void SpriteCB_Dodrio(struct Sprite *);
static u32 DoDodrioMissedAnim(struct Sprite *);
static u32 DoDodrioIntroAnim(struct Sprite *);
static s16 GetDodrioXPos(u8, u8);
static void SetDodrioInvisibility(bool8, u8);
static void LoadGfx(void);
static bool32 LoadBgGfx(void);
static void InitBgs(void);
static void SetGfxFunc(void (*func)(void));
static void (*GetGfxFunc(void))(void);
static void ShowNames(void);
static void ShowResults(void);
static void Msg_WantToPlayAgain(void);
static void Msg_SavingDontTurnOff(void);
static void Msg_CommunicationStandby(void);
static void EraseMessage(void);
static void Msg_SomeoneDroppedOut(void);
static void StopGfxFuncs(void);
static void GfxIdle(void);
// For each player, the array is a list of all the columns starting with the column to their left
// Only the range of active columns is read from the array (dependent on the number of players),
// so the arrays are spaced such that the numbers in the center are where the data that's read starts and end.
static const u8 sActiveColumnMap[MAX_RFU_PLAYERS][MAX_RFU_PLAYERS][NUM_BERRY_COLUMNS] =
{
{ // 1 player (never used), columns 4-6.
// Sometimes read to get default order regardless of the current number of players
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
},
{ // 2 players (never used), columns 3-6
{0, 1, 2, 3, 4, 5, 6, 3, 8, 9, 0},
{0, 1, 2, 5, 6, 3, 4, 5, 8, 9, 0},
},
{ // 3 players, columns 2-7
{0, 1, 2, 3, 4, 5, 6, 7, 2, 9, 0},
{0, 1, 4, 5, 6, 7, 2, 3, 4, 9, 0},
{0, 1, 6, 7, 2, 3, 4, 5, 6, 9, 0},
},
{ // 4 players, columns 1-8
{0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 0},
{0, 3, 4, 5, 6, 7, 8, 1, 2, 3, 0},
{0, 5, 6, 7, 8, 1, 2, 3, 4, 5, 0},
{0, 7, 8, 1, 2, 3, 4, 5, 6, 7, 0},
},
{ // 5 players, all columns (0-9)
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 },
{ 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2 },
{ 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4 },
{ 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
{ 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8 },
},
};
// A table for which falling berry column corresponds to which Dodrio head for each player
// The numbers in each array are the column number for each head, {left, middle, right}
// Dependent on the number of players
static const u8 sDodrioHeadToColumnMap[MAX_RFU_PLAYERS][MAX_RFU_PLAYERS][3] =
{
{ // 1 player (never used)
{4, 5, 6},
},
{ // 2 players (never used)
{3, 4, 5},
{5, 6, 3},
},
{ // 3 players
{4, 5, 6},
{6, 7, 2},
{2, 3, 4},
},
{ // 4 players
{3, 4, 5},
{5, 6, 7},
{7, 8, 1},
{1, 2, 3},
},
{ // 5 players
{4, 5, 6},
{6, 7, 8},
{8, 9, 0},
{0, 1, 2},
{2, 3, 4},
},
};
// A table of player ids and their neighbor, dependent on the total number of players
// {L, M, R}, where M is the player in question, L is their neighbor to the left, and R is their neighbor to the right
static const u8 sDodrioNeighborMap[MAX_RFU_PLAYERS][MAX_RFU_PLAYERS][3] =
{
{ // 1 player (never used)
{1, 0, 1},
},
{ // 2 players (never used)
{1, 0, 1},
{0, 1, 0},
},
{ // 3 players
{2, 0, 1},
{0, 1, 2},
{1, 2, 0},
},
{ // 4 players
{3, 0, 1},
{0, 1, 2},
{1, 2, 3},
{2, 3, 0},
},
{ // 5 players
{4, 0, 1},
{0, 1, 2},
{1, 2, 3},
{2, 3, 4},
{3, 4, 0},
},
};
#define x 9 // No player at this column. This may go out of bounds if this is returned
// Takes the number of players and a column and returns the player id at that column.
// Note that the assignment is somewhat arbitrary as players share neighboring columns.
ALIGNED(4)
static const u8 sPlayerIdAtColumn[MAX_RFU_PLAYERS][NUM_BERRY_COLUMNS] =
{
{x, x, x, x, 1, 1, 1, x, x, x, x}, // 1 player
{x, x, x, 0, 0, 1, 1, 0, x, x, x}, // 2 players
{x, x, 2, 2, 0, 0, 1, 1, 1, x, x}, // 3 players
{x, 3, 3, 0, 0, 1, 1, 2, 2, 3, x}, // 4 players
{3, 3, 4, 4, 0, 0, 1, 1, 2, 2, 3}, // 5 players
};
#undef x
// Each array contains the columns that belong solely to one player, dependent on the number of players
// When determing how difficult the berries in a column should be, the highest
// difficulty of the players sharing that column is used.
// This table is used to skip that check, and instead automatically use the
// difficulty of the only player who can use the column.
static const u8 sUnsharedColumns[MAX_RFU_PLAYERS][MAX_RFU_PLAYERS] =
{
{5},
{4, 6},
{3, 5, 7},
{2, 4, 6, 8},
#ifndef BUGFIX
{1, 3, 5, 6, 9}, // BUG: Column 6 is shared, 7 is not. As a result, the player in column 7 will have their difficulty influenced by their neighbors
#else
{1, 3, 5, 7, 9},
#endif
};
// Duplicate and unused gfx. Feel free to remove.
static const u32 sDuplicateGfx[] = INCBIN_U32("graphics/dodrio_berry_picking/tree_border.gbapal",
"graphics/dodrio_berry_picking/bg.gbapal",
"graphics/dodrio_berry_picking/dodrio.gbapal",
"graphics/dodrio_berry_picking/shiny.gbapal",
"graphics/dodrio_berry_picking/status.gbapal",
"graphics/dodrio_berry_picking/berries.gbapal",
"graphics/dodrio_berry_picking/berries.4bpp.lz",
"graphics/dodrio_berry_picking/cloud.gbapal",
"graphics/dodrio_berry_picking/bg.4bpp.lz",
"graphics/dodrio_berry_picking/tree_border.4bpp.lz",
"graphics/dodrio_berry_picking/status.4bpp.lz",
"graphics/dodrio_berry_picking/cloud.4bpp.lz",
"graphics/dodrio_berry_picking/dodrio.4bpp.lz",
"graphics/dodrio_berry_picking/bg.bin.lz",
"graphics/dodrio_berry_picking/tree_border_right.bin.lz",
"graphics/dodrio_berry_picking/tree_border_left.bin.lz");
static const u8 sBerryFallDelays[][3] =
{
{ [BERRY_BLUE] = 40, [BERRY_GREEN] = 24, [BERRY_GOLD] = 13 },
{ [BERRY_BLUE] = 32, [BERRY_GREEN] = 19, [BERRY_GOLD] = 10 },
{ [BERRY_BLUE] = 22, [BERRY_GREEN] = 13, [BERRY_GOLD] = 7 },
};
// How far the outer tree borders should slide to reveal the game screen.
// Dependent on how many players are playing.
// Curiously the 2-player screen is narrower than the 1-player, though neither
// gets used as there's a 3 player minimum
ALIGNED(4)
static const u8 sTreeBorderXPos[MAX_RFU_PLAYERS] = {8, 5, 8, 11, 15};
// The number of berries eaten needed to progress to the next difficulty
ALIGNED(4)
static const u8 sDifficultyThresholds[NUM_DIFFICULTIES] = {5, 10, 20, 30, 50, 70, 100};
ALIGNED(4)
static const u8 sPrizeBerryIds[][10] =
{
{ // Possible prizes with 3 players
ITEM_TO_BERRY(ITEM_RAZZ_BERRY) - 1,
ITEM_TO_BERRY(ITEM_BLUK_BERRY) - 1,
ITEM_TO_BERRY(ITEM_NANAB_BERRY) - 1,
ITEM_TO_BERRY(ITEM_WEPEAR_BERRY) - 1,
ITEM_TO_BERRY(ITEM_PINAP_BERRY) - 1,
ITEM_TO_BERRY(ITEM_PINAP_BERRY) - 1,
ITEM_TO_BERRY(ITEM_WEPEAR_BERRY) - 1,
ITEM_TO_BERRY(ITEM_NANAB_BERRY) - 1,
ITEM_TO_BERRY(ITEM_BLUK_BERRY) - 1,
ITEM_TO_BERRY(ITEM_RAZZ_BERRY) - 1
},
{ // Possible prizes with 4 players
ITEM_TO_BERRY(ITEM_POMEG_BERRY) - 1,
ITEM_TO_BERRY(ITEM_KELPSY_BERRY) - 1,
ITEM_TO_BERRY(ITEM_QUALOT_BERRY) - 1,
ITEM_TO_BERRY(ITEM_HONDEW_BERRY) - 1,
ITEM_TO_BERRY(ITEM_GREPA_BERRY) - 1,
ITEM_TO_BERRY(ITEM_TAMATO_BERRY) - 1,
ITEM_TO_BERRY(ITEM_CORNN_BERRY) - 1,
ITEM_TO_BERRY(ITEM_MAGOST_BERRY) - 1,
ITEM_TO_BERRY(ITEM_RABUTA_BERRY) - 1,
ITEM_TO_BERRY(ITEM_NOMEL_BERRY) - 1
},
{ // Possible prizes with 5 players
ITEM_TO_BERRY(ITEM_SPELON_BERRY) - 1,
ITEM_TO_BERRY(ITEM_PAMTRE_BERRY) - 1,
ITEM_TO_BERRY(ITEM_WATMEL_BERRY) - 1,
ITEM_TO_BERRY(ITEM_DURIN_BERRY) - 1,
ITEM_TO_BERRY(ITEM_BELUE_BERRY) - 1,
ITEM_TO_BERRY(ITEM_BELUE_BERRY) - 1,
ITEM_TO_BERRY(ITEM_DURIN_BERRY) - 1,
ITEM_TO_BERRY(ITEM_WATMEL_BERRY) - 1,
ITEM_TO_BERRY(ITEM_PAMTRE_BERRY) - 1,
ITEM_TO_BERRY(ITEM_SPELON_BERRY) - 1
},
};
static void (*const sLeaderFuncs[])(void) =
{
[FUNC_INTRO] = DoGameIntro,
[FUNC_INIT_COUNTDOWN] = InitCountdown,
[FUNC_COUNTDOWN] = DoCountdown,
[FUNC_WAIT_START] = WaitGameStart,
[FUNC_PLAY_GAME] = PlayGame_Leader,
[FUNC_INIT_RESULTS] = InitResults_Leader,
[FUNC_RESULTS] = DoResults,
[FUNC_ASK_PLAY_AGAIN] = AskPlayAgain,
[FUNC_END_LINK] = EndLink,
[FUNC_EXIT] = ExitGame,
[FUNC_RESET_GAME] = ResetGame,
[FUNC_WAIT_END_GAME] = WaitEndGame_Leader
};
static void (*const sMemberFuncs[])(void) =
{
[FUNC_INTRO] = DoGameIntro,
[FUNC_INIT_COUNTDOWN] = InitCountdown,
[FUNC_COUNTDOWN] = DoCountdown,
[FUNC_WAIT_START] = WaitGameStart,
[FUNC_PLAY_GAME] = PlayGame_Member,
[FUNC_INIT_RESULTS] = InitResults_Member,
[FUNC_RESULTS] = DoResults,
[FUNC_ASK_PLAY_AGAIN] = AskPlayAgain,
[FUNC_END_LINK] = EndLink,
[FUNC_EXIT] = ExitGame,
[FUNC_RESET_GAME] = ResetGame,
[FUNC_WAIT_END_GAME] = WaitEndGame_Member
};
void StartDodrioBerryPicking(u16 partyId, void (*exitCallback)(void))
{
sExitingGame = FALSE;
if (gReceivedRemoteLinkPlayers && (sGame = AllocZeroed(sizeof(*sGame))))
{
ResetTasksAndSprites();
InitDodrioGame(sGame);
sGame->exitCallback = exitCallback;
sGame->multiplayerId = GetMultiplayerId();
sGame->player = sGame->players[sGame->multiplayerId];
InitMonInfo(&sGame->monInfo[sGame->multiplayerId], &gPlayerParty[partyId]);
CreateTask(Task_StartDodrioGame, 1);
SetMainCallback2(CB2_DodrioGame);
SetRandomPrize();
GetActiveBerryColumns(sGame->numPlayers, &sGame->berryColStart, &sGame->berryColEnd);
StopMapMusic();
PlayNewMapMusic(MUS_RG_BERRY_PICK);
}
else
{
// Exit - Alloc failed, or players not connected
SetMainCallback2(exitCallback);
return;
}
}
static void ResetTasksAndSprites(void)
{
ResetTasks();
ResetSpriteData();
FreeAllSpritePalettes();
}
static void InitDodrioGame(struct DodrioGame * game)
{
u8 i;
game->startState = 0;
game->state = 0;
game->timer = 0;
game->funcId = FUNC_INTRO;
game->prevFuncId = FUNC_INTRO;
game->startGame = FALSE;
game->berriesFalling = FALSE;
game->countdownEndDelay = 0;
game->numGraySquares = 0;
game->unused2 = 0;
game->allReadyToEnd = FALSE;
for (i = 0; i < ARRAY_COUNT(game->pickStateQueue); i++)
game->pickStateQueue[i] = PICK_NONE;
for (i = 0; i < MAX_RFU_PLAYERS; i++)
{
game->inputState[i] = INPUTSTATE_NONE;
game->inputDelay[i] = 0;
game->berryResults[i][BERRY_BLUE] = 0;
game->berryResults[i][BERRY_GREEN] = 0;
game->berryResults[i][BERRY_GOLD] = 0;
game->berryResults[i][BERRY_MISSED] = 0;
game->berryResults[i][BERRY_IN_ROW] = 0;
game->playAgainStates[i] = PLAY_AGAIN_NONE;
game->readyToEnd[i] = FALSE;
}
for (i = 0; i < NUM_BERRY_COLUMNS; i++)
{
game->fallTimer[i] = 0;
game->newBerryTimer[i] = 0;
game->berryState[i] = BERRYSTATE_NONE;
game->playersAttemptingPick[i][0] = PLAYER_NONE;
game->playersAttemptingPick[i][1] = PLAYER_NONE;
}
game->isLeader = GetMultiplayerId() == 0 ? TRUE : FALSE;
game->numPlayers = GetLinkPlayerCount();
game->posToPlayerId[0] = GetMultiplayerId();
for (i = 1; i < game->numPlayers; i++)
{
game->posToPlayerId[i] = game->posToPlayerId[i - 1] + 1;
if (game->posToPlayerId[i] > game->numPlayers - 1)
game->posToPlayerId[i] %= game->numPlayers;
}
}
static void Task_StartDodrioGame(u8 taskId)
{
u8 i, numPlayers;
switch (sGame->startState)
{
case 0:
SetVBlankCallback(NULL);
CreateTask_(Task_CommunicateMonInfo, 4);
sGame->startState++;
break;
case 1:
if (!FuncIsActiveTask(Task_CommunicateMonInfo))
{
InitGameGfx(&sGame->gfx);
sGame->startState++;
}
break;
case 2:
if (!IsGfxFuncActive())
{
Rfu_SetLinkStandbyCallback();
sGame->startState++;
}
break;
case 3:
if (IsLinkTaskFinished())
{
if (gReceivedRemoteLinkPlayers)
{
LoadWirelessStatusIndicatorSpriteGfx();
CreateWirelessStatusIndicatorSprite(0, 0);
}
sGame->startState++;
}
break;
case 4:
numPlayers = sGame->numPlayers;
LoadDodrioGfx();
for (i = 0; i < numPlayers; i++)
CreateDodrioSprite(&sGame->monInfo[sGame->posToPlayerId[i]], i, sGame->posToPlayerId[i], sGame->numPlayers);
SetAllDodrioInvisibility(FALSE, sGame->numPlayers);
sGame->startState++;
break;
case 5:
LoadBerryGfx();
CreateBerrySprites();
CreateCloudSprites();
CreateStatusBarSprites();
sGame->startState++;
break;
case 6:
BlendPalettes(PALETTES_ALL, 0x10, 0x00);
BeginNormalPaletteFade(PALETTES_ALL, 0, 16, 0, 0);
SetVBlankCallback(VBlankCB_DodrioGame);
sGame->startState++;
break;
case 7:
UpdatePaletteFade();
if (!gPaletteFade.active)
sGame->startState++;
break;
default:
DestroyTask(taskId);
CreateDodrioGameTask(Task_NewGameIntro);
break;
}
}
static void Task_DodrioGame_Leader(u8 taskId)
{
RecvLinkData_Leader();
sLeaderFuncs[sGame->funcId]();
if (!sExitingGame)
UpdateGame_Leader();
SendLinkData_Leader();
}
static void Task_DodrioGame_Member(u8 taskId)
{
RecvLinkData_Member();
sMemberFuncs[sGame->funcId]();
if (!sExitingGame)
UpdateGame_Member();
SendLinkData_Member();
}
static void DoGameIntro(void)
{
switch (sGame->state)
{
case 0:
StartDodrioIntroAnim(1);
SetGfxFuncById(GFXFUNC_SHOW_NAMES);
sGame->state++;
break;
case 1:
if (!IsGfxFuncActive())
SetGameFunc(FUNC_INIT_COUNTDOWN);
break;
}
}
static void InitCountdown(void)
{
switch (sGame->state)
{
case 0:
InitFirstWaveOfBerries();
sGame->state++;
break;
default:
sGame->startCountdown = TRUE;
SetGameFunc(FUNC_COUNTDOWN);
break;
}
}
static void DoCountdown(void)
{
switch (sGame->state)
{
case 0:
StartMinigameCountdown(GFXTAG_COUNTDOWN, PALTAG_COUNTDOWN, 120, 80, 0);
sGame->state++;
break;
case 1:
Rfu_SetLinkStandbyCallback();
sGame->state++;
break;
case 2:
if (IsLinkTaskFinished())
{
sGame->state++;
sGame->countdownEndDelay = 0;
}
break;
case 3:
if (!IsMinigameCountdownRunning())
sGame->state++;
break;
case 4:
if (++sGame->countdownEndDelay > 5)
{
Rfu_SetLinkStandbyCallback();
sGame->state++;
}
break;
case 5:
if (IsLinkTaskFinished())
SetGameFunc(FUNC_WAIT_START);
break;
}
}
static void WaitGameStart(void)
{
switch (sGame->state)
{
case 0:
if (sGame->startGame)
SetGameFunc(FUNC_PLAY_GAME);
break;
}
}
static void PlayGame_Leader(void)
{
switch (sGame->state)
{
case 0:
if (sGame->numGraySquares < NUM_STATUS_SQUARES)
{
if (sGame->inputState[0] == INPUTSTATE_NONE)
{
if (JOY_NEW(DPAD_UP))
{
if (sGame->players[0].comm.pickState == PICK_NONE)
{
sGame->players[0].comm.ateBerry = FALSE;
sGame->players[0].comm.pickState = UpdatePickStateQueue(PICK_MIDDLE);
}
}
else if (JOY_NEW(DPAD_RIGHT))
{
if (sGame->players[0].comm.pickState == PICK_NONE)
{
sGame->players[0].comm.ateBerry = FALSE;
sGame->players[0].comm.pickState = UpdatePickStateQueue(PICK_RIGHT);
}
}
else if (JOY_NEW(DPAD_LEFT))
{
if (sGame->players[0].comm.pickState == PICK_NONE)
{
sGame->players[0].comm.ateBerry = FALSE;
sGame->players[0].comm.pickState = UpdatePickStateQueue(PICK_LEFT);
}
}
else
{
sGame->players[0].comm.pickState = UpdatePickStateQueue(PICK_NONE);
}
}
}
else
{
SetGameFunc(FUNC_WAIT_END_GAME);
}
UpdateFallingBerries();
HandleSound_Leader();
break;
}
}
static void PlayGame_Member(void)
{
if (sGame->numGraySquares < NUM_STATUS_SQUARES)
{
if (JOY_NEW(DPAD_UP))
{
if (sGame->players[sGame->multiplayerId].comm.pickState == PICK_NONE)
{
sGame->player.comm.pickState = PICK_MIDDLE;
}
}
else if (JOY_NEW(DPAD_RIGHT))
{
if (sGame->players[sGame->multiplayerId].comm.pickState == PICK_NONE)
{
sGame->player.comm.pickState = PICK_RIGHT;
}
}
else if (JOY_NEW(DPAD_LEFT))
{
if (sGame->players[sGame->multiplayerId].comm.pickState == PICK_NONE)
{
sGame->player.comm.pickState = PICK_LEFT;
}
}