forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbattle_gfx_sfx_util.c
1340 lines (1169 loc) · 47.3 KB
/
battle_gfx_sfx_util.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 "battle.h"
#include "battle_controllers.h"
#include "battle_ai_script_commands.h"
#include "battle_anim.h"
#include "constants/battle_anim.h"
#include "battle_interface.h"
#include "main.h"
#include "dma3.h"
#include "malloc.h"
#include "graphics.h"
#include "random.h"
#include "util.h"
#include "pokemon.h"
#include "constants/moves.h"
#include "task.h"
#include "sprite.h"
#include "sound.h"
#include "party_menu.h"
#include "m4a.h"
#include "decompress.h"
#include "data.h"
#include "palette.h"
#include "contest.h"
#include "constants/songs.h"
#include "constants/rgb.h"
#include "constants/battle_palace.h"
extern const u8 gBattlePalaceNatureToMoveTarget[];
extern const u8 * const gBattleAnims_General[];
extern const u8 * const gBattleAnims_Special[];
extern const struct CompressedSpriteSheet gSpriteSheet_EnemyShadow;
extern const struct SpriteTemplate gSpriteTemplate_EnemyShadow;
// this file's functions
static u8 GetBattlePalaceMoveGroup(u16 move);
static u16 GetBattlePalaceTarget(void);
static void SpriteCB_TrainerSlideVertical(struct Sprite *sprite);
static bool8 ShouldAnimBeDoneRegardlessOfSubstitute(u8 animId);
static void Task_ClearBitWhenBattleTableAnimDone(u8 taskId);
static void Task_ClearBitWhenSpecialAnimDone(u8 taskId);
static void ClearSpritesBattlerHealthboxAnimData(void);
// const rom data
static const struct CompressedSpriteSheet sSpriteSheet_SinglesPlayerHealthbox =
{
gHealthboxSinglesPlayerGfx, 0x1000, TAG_HEALTHBOX_PLAYER1_TILE
};
static const struct CompressedSpriteSheet sSpriteSheet_SinglesOpponentHealthbox =
{
gHealthboxSinglesOpponentGfx, 0x1000, TAG_HEALTHBOX_OPPONENT1_TILE
};
static const struct CompressedSpriteSheet sSpriteSheets_DoublesPlayerHealthbox[2] =
{
{gHealthboxDoublesPlayerGfx, 0x800, TAG_HEALTHBOX_PLAYER1_TILE},
{gHealthboxDoublesPlayerGfx, 0x800, TAG_HEALTHBOX_PLAYER2_TILE}
};
static const struct CompressedSpriteSheet sSpriteSheets_DoublesOpponentHealthbox[2] =
{
{gHealthboxDoublesOpponentGfx, 0x800, TAG_HEALTHBOX_OPPONENT1_TILE},
{gHealthboxDoublesOpponentGfx, 0x800, TAG_HEALTHBOX_OPPONENT2_TILE}
};
static const struct CompressedSpriteSheet sSpriteSheet_SafariHealthbox =
{
gHealthboxSafariGfx, 0x1000, TAG_HEALTHBOX_SAFARI_TILE
};
static const struct CompressedSpriteSheet sSpriteSheets_HealthBar[MAX_BATTLERS_COUNT] =
{
{gBlankGfxCompressed, 0x0100, TAG_HEALTHBAR_PLAYER1_TILE},
{gBlankGfxCompressed, 0x0120, TAG_HEALTHBAR_OPPONENT1_TILE},
{gBlankGfxCompressed, 0x0100, TAG_HEALTHBAR_PLAYER2_TILE},
{gBlankGfxCompressed, 0x0120, TAG_HEALTHBAR_OPPONENT2_TILE}
};
static const struct SpritePalette sSpritePalettes_HealthBoxHealthBar[2] =
{
{gBattleInterface_BallStatusBarPal, TAG_HEALTHBOX_PAL},
{gBattleInterface_BallDisplayPal, TAG_HEALTHBAR_PAL}
};
// code
void AllocateBattleSpritesData(void)
{
gBattleSpritesDataPtr = AllocZeroed(sizeof(struct BattleSpriteData));
gBattleSpritesDataPtr->battlerData = AllocZeroed(sizeof(struct BattleSpriteInfo) * MAX_BATTLERS_COUNT);
gBattleSpritesDataPtr->healthBoxesData = AllocZeroed(sizeof(struct BattleHealthboxInfo) * MAX_BATTLERS_COUNT);
gBattleSpritesDataPtr->animationData = AllocZeroed(sizeof(struct BattleAnimationInfo));
gBattleSpritesDataPtr->battleBars = AllocZeroed(sizeof(struct BattleBarInfo) * MAX_BATTLERS_COUNT);
}
void FreeBattleSpritesData(void)
{
if (gBattleSpritesDataPtr == NULL)
return;
FREE_AND_SET_NULL(gBattleSpritesDataPtr->battleBars);
FREE_AND_SET_NULL(gBattleSpritesDataPtr->animationData);
FREE_AND_SET_NULL(gBattleSpritesDataPtr->healthBoxesData);
FREE_AND_SET_NULL(gBattleSpritesDataPtr->battlerData);
FREE_AND_SET_NULL(gBattleSpritesDataPtr);
}
// Pokémon chooses move to use in Battle Palace rather than player
u16 ChooseMoveAndTargetInBattlePalace(void)
{
s32 i, var1, var2;
s32 chosenMoveId = -1;
struct ChooseMoveStruct *moveInfo = (struct ChooseMoveStruct *)(&gBattleBufferA[gActiveBattler][4]);
u8 unusableMovesBits = CheckMoveLimitations(gActiveBattler, 0, MOVE_LIMITATIONS_ALL);
s32 percent = Random() % 100;
// Heavy variable re-use here makes this hard to read without defines
// Possibly just optimization? might still match with additional vars
#define maxGroupNum var1
#define minGroupNum var2
#define selectedGroup percent
#define selectedMoves var2
#define moveTarget var1
#define numMovesPerGroup var1
#define numMultipleMoveGroups var2
#define randSelectGroup var2
// If battler is < 50% HP and not asleep, use second set of move group likelihoods
// otherwise use first set
i = (gBattleStruct->palaceFlags & gBitTable[gActiveBattler]) ? 2 : 0;
minGroupNum = i;
maxGroupNum = i + 2; // + 2 because there are two percentages per set of likelihoods
// Each nature has a different percent chance to select a move from one of 3 move groups
// If percent is less than 1st check, use move from "Attack" group
// If percent is less than 2nd check, use move from "Defense" group
// Otherwise use move from "Support" group
for (; i < maxGroupNum; i++)
{
if (gBattlePalaceNatureToMoveGroupLikelihood[GetNatureFromPersonality(gBattleMons[gActiveBattler].personality)][i] > percent)
break;
}
selectedGroup = i - minGroupNum;
if (i == maxGroupNum)
selectedGroup = PALACE_MOVE_GROUP_SUPPORT;
// Flag moves that match selected group, to be passed to AI
for (selectedMoves = 0, i = 0; i < MAX_MON_MOVES; i++)
{
if (moveInfo->moves[i] == MOVE_NONE)
break;
if (selectedGroup == GetBattlePalaceMoveGroup(moveInfo->moves[i]) && moveInfo->currentPp[i] != 0)
selectedMoves |= gBitTable[i];
}
// Pass selected moves to AI, pick one
if (selectedMoves != 0)
{
// Lower 4 bits of palaceFlags are flags for each battler.
// Clear the rest of palaceFlags, then set the selected moves in the upper 4 bits.
gBattleStruct->palaceFlags &= (1 << MAX_BATTLERS_COUNT) - 1;
gBattleStruct->palaceFlags |= (selectedMoves << MAX_BATTLERS_COUNT);
BattleAI_SetupAIData(selectedMoves);
chosenMoveId = BattleAI_ChooseMoveOrAction();
}
// If no moves matched the selected group, pick a new move from groups the Pokémon has
// In this case the AI is not checked again, so the choice may be worse
// If a move is chosen this way, there's a 50% chance that it will be unable to use it anyway
if (chosenMoveId == -1)
{
if (unusableMovesBits != ALL_MOVES_MASK)
{
numMovesPerGroup = 0, numMultipleMoveGroups = 0;
for (i = 0; i < MAX_MON_MOVES; i++)
{
// Count the number of usable moves the battler has in each move group.
// The totals will be stored separately in 3 groups of 4 bits each in numMovesPerGroup.
if (GetBattlePalaceMoveGroup(moveInfo->moves[i]) == PALACE_MOVE_GROUP_ATTACK && !(gBitTable[i] & unusableMovesBits))
numMovesPerGroup += (1 << 0);
if (GetBattlePalaceMoveGroup(moveInfo->moves[i]) == PALACE_MOVE_GROUP_DEFENSE && !(gBitTable[i] & unusableMovesBits))
numMovesPerGroup += (1 << 4);
if (GetBattlePalaceMoveGroup(moveInfo->moves[i]) == PALACE_MOVE_GROUP_SUPPORT && !(gBitTable[i] & unusableMovesBits))
numMovesPerGroup += (1 << 8);
}
// Count the number of move groups for which the battler has at least 2 usable moves.
// This is a roundabout way to determine if there is a move group that should be
// preferred, because it has multiple move options and the others do not.
// The condition intended to check the total for the Support group is accidentally
// checking the Defense total, and is never true. As a result the preferences for
// random move selection here will skew away from the Support move group.
if ((numMovesPerGroup & 0xF) >= 2)
numMultipleMoveGroups++;
if ((numMovesPerGroup & (0xF << 4)) >= (2 << 4))
numMultipleMoveGroups++;
#ifdef BUGFIX
if ((numMovesPerGroup & (0xF << 8)) >= (2 << 8))
#else
if ((numMovesPerGroup & (0xF << 4)) >= (2 << 8))
#endif
numMultipleMoveGroups++;
// By this point we already know the battler only has usable moves from at most 2 of the 3 move groups,
// because they had no usable moves from the move group that was selected based on Nature.
//
// The below condition is effectively 'numMultipleMoveGroups != 1'.
// There is no stand-out group with multiple moves to choose from, so we pick randomly.
// Note that because of the bug above the battler may actually have any number of Support moves.
if (numMultipleMoveGroups > 1 || numMultipleMoveGroups == 0)
{
do
{
i = Random() % MAX_MON_MOVES;
if (!(gBitTable[i] & unusableMovesBits))
chosenMoveId = i;
} while (chosenMoveId == -1);
}
else
{
// The battler has just 1 move group with multiple move options to choose from.
// Choose a move randomly from this group.
// Same bug as the previous set of conditions (the condition for Support is never true).
// This bug won't cause a softlock below, because if Support is the only group with multiple
// moves then it won't have been counted, and the 'numMultipleMoveGroups == 0' above will be true.
if ((numMovesPerGroup & 0xF) >= 2)
randSelectGroup = PALACE_MOVE_GROUP_ATTACK;
if ((numMovesPerGroup & (0xF << 4)) >= (2 << 4))
randSelectGroup = PALACE_MOVE_GROUP_DEFENSE;
#ifdef BUGFIX
if ((numMovesPerGroup & (0xF << 8)) >= (2 << 8))
#else
if ((numMovesPerGroup & (0xF << 4)) >= (2 << 8))
#endif
randSelectGroup = PALACE_MOVE_GROUP_SUPPORT;
do
{
i = Random() % MAX_MON_MOVES;
if (!(gBitTable[i] & unusableMovesBits) && randSelectGroup == GetBattlePalaceMoveGroup(moveInfo->moves[i]))
chosenMoveId = i;
} while (chosenMoveId == -1);
}
// Because the selected move was not from the Nature-chosen move group there's a 50% chance
// that it will be unable to use it. This could have been checked earlier to avoid the above work.
if (Random() % 100 >= 50)
{
gProtectStructs[gActiveBattler].palaceUnableToUseMove = TRUE;
return 0;
}
}
else
{
// All the battler's moves were flagged as unusable.
gProtectStructs[gActiveBattler].palaceUnableToUseMove = TRUE;
return 0;
}
}
if (moveInfo->moves[chosenMoveId] == MOVE_CURSE)
{
if (moveInfo->monTypes[0] != TYPE_GHOST && moveInfo->monTypes[1] != TYPE_GHOST)
moveTarget = MOVE_TARGET_USER;
else
moveTarget = MOVE_TARGET_SELECTED;
}
else
{
moveTarget = gBattleMoves[moveInfo->moves[chosenMoveId]].target;
}
if (moveTarget & MOVE_TARGET_USER)
chosenMoveId |= (gActiveBattler << 8);
else if (moveTarget == MOVE_TARGET_SELECTED)
chosenMoveId |= GetBattlePalaceTarget();
else
chosenMoveId |= (GetBattlerAtPosition(BATTLE_OPPOSITE(GET_BATTLER_SIDE(gActiveBattler))) << 8);
return chosenMoveId;
}
#undef maxGroupNum
#undef minGroupNum
#undef selectedGroup
#undef selectedMoves
#undef moveTarget
#undef numMovesPerGroup
#undef numMultipleMoveGroups
#undef randSelectGroup
static u8 GetBattlePalaceMoveGroup(u16 move)
{
switch (gBattleMoves[move].target)
{
case MOVE_TARGET_SELECTED:
case MOVE_TARGET_USER_OR_SELECTED:
case MOVE_TARGET_RANDOM:
case MOVE_TARGET_BOTH:
case MOVE_TARGET_FOES_AND_ALLY:
if (gBattleMoves[move].power == 0)
return PALACE_MOVE_GROUP_SUPPORT;
else
return PALACE_MOVE_GROUP_ATTACK;
break;
case MOVE_TARGET_DEPENDS:
case MOVE_TARGET_OPPONENTS_FIELD:
return PALACE_MOVE_GROUP_SUPPORT;
case MOVE_TARGET_USER:
return PALACE_MOVE_GROUP_DEFENSE;
default:
return PALACE_MOVE_GROUP_ATTACK;
}
}
static u16 GetBattlePalaceTarget(void)
{
if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE)
{
u8 opposing1, opposing2;
if (GetBattlerSide(gActiveBattler) == B_SIDE_PLAYER)
{
opposing1 = GetBattlerAtPosition(B_POSITION_OPPONENT_LEFT);
opposing2 = GetBattlerAtPosition(B_POSITION_OPPONENT_RIGHT);
}
else
{
opposing1 = GetBattlerAtPosition(B_POSITION_PLAYER_LEFT);
opposing2 = GetBattlerAtPosition(B_POSITION_PLAYER_RIGHT);
}
if (gBattleMons[opposing1].hp == gBattleMons[opposing2].hp)
return (BATTLE_OPPOSITE(gActiveBattler & BIT_SIDE) + (Random() & 2)) << 8;
switch (gBattlePalaceNatureToMoveTarget[GetNatureFromPersonality(gBattleMons[gActiveBattler].personality)])
{
case PALACE_TARGET_STRONGER:
if (gBattleMons[opposing1].hp > gBattleMons[opposing2].hp)
return opposing1 << 8;
else
return opposing2 << 8;
case PALACE_TARGET_WEAKER:
if (gBattleMons[opposing1].hp < gBattleMons[opposing2].hp)
return opposing1 << 8;
else
return opposing2 << 8;
case PALACE_TARGET_RANDOM:
return (BATTLE_OPPOSITE(gActiveBattler & BIT_SIDE) + (Random() & 2)) << 8;
}
}
return BATTLE_OPPOSITE(gActiveBattler) << 8;
}
// Wait for the Pokémon to finish appearing out from the Poké Ball on send out
void SpriteCB_WaitForBattlerBallReleaseAnim(struct Sprite *sprite)
{
u8 spriteId = sprite->data[1];
if (!gSprites[spriteId].affineAnimEnded)
return;
if (gSprites[spriteId].invisible)
return;
if (gSprites[spriteId].animPaused)
{
gSprites[spriteId].animPaused = 0;
}
else
{
if (gSprites[spriteId].animEnded)
sprite->callback = SpriteCallbackDummy;
}
}
static void UNUSED UnusedDoBattleSpriteAffineAnim(struct Sprite *sprite, bool8 pointless)
{
sprite->animPaused = TRUE;
sprite->callback = SpriteCallbackDummy;
if (!pointless)
StartSpriteAffineAnim(sprite, 1);
else
StartSpriteAffineAnim(sprite, 1);
AnimateSprite(sprite);
}
#define sSpeedX data[0]
void SpriteCB_TrainerSlideIn(struct Sprite *sprite)
{
if (!(gIntroSlideFlags & 1))
{
sprite->x2 += sprite->sSpeedX;
if (sprite->x2 == 0)
{
if (sprite->y2 != 0)
sprite->callback = SpriteCB_TrainerSlideVertical;
else
sprite->callback = SpriteCallbackDummy;
}
}
}
// Slide up to 0 if necessary (used by multi battle intro)
static void SpriteCB_TrainerSlideVertical(struct Sprite *sprite)
{
sprite->y2 -= 2;
if (sprite->y2 == 0)
sprite->callback = SpriteCallbackDummy;
}
#undef sSpeedX
void InitAndLaunchChosenStatusAnimation(bool8 isStatus2, u32 status)
{
gBattleSpritesDataPtr->healthBoxesData[gActiveBattler].statusAnimActive = 1;
if (!isStatus2)
{
if (status == STATUS1_FREEZE)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_FRZ);
else if (status == STATUS1_POISON || status & STATUS1_TOXIC_POISON)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_PSN);
else if (status == STATUS1_BURN)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_BRN);
else if (status & STATUS1_SLEEP)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_SLP);
else if (status == STATUS1_PARALYSIS)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_PRZ);
else // no animation
gBattleSpritesDataPtr->healthBoxesData[gActiveBattler].statusAnimActive = 0;
}
else
{
if (status & STATUS2_INFATUATION)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_INFATUATION);
else if (status & STATUS2_CONFUSION)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_CONFUSION);
else if (status & STATUS2_CURSED)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_CURSED);
else if (status & STATUS2_NIGHTMARE)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_NIGHTMARE);
else if (status & STATUS2_WRAPPED)
LaunchStatusAnimation(gActiveBattler, B_ANIM_STATUS_WRAPPED); // this animation doesn't actually exist
else // no animation
gBattleSpritesDataPtr->healthBoxesData[gActiveBattler].statusAnimActive = 0;
}
}
#define tBattlerId data[0]
bool8 TryHandleLaunchBattleTableAnimation(u8 activeBattler, u8 atkBattler, u8 defBattler, u8 tableId, u16 argument)
{
u8 taskId;
if (tableId == B_ANIM_CASTFORM_CHANGE && (argument & CASTFORM_SUBSTITUTE))
{
// If Castform is behind substitute, set the new form but skip the animation
gBattleMonForms[activeBattler] = (argument & ~CASTFORM_SUBSTITUTE);
return TRUE;
}
if (gBattleSpritesDataPtr->battlerData[activeBattler].behindSubstitute
&& !ShouldAnimBeDoneRegardlessOfSubstitute(tableId))
{
return TRUE;
}
if (gBattleSpritesDataPtr->battlerData[activeBattler].behindSubstitute
&& tableId == B_ANIM_SUBSTITUTE_FADE
&& gSprites[gBattlerSpriteIds[activeBattler]].invisible)
{
LoadBattleMonGfxAndAnimate(activeBattler, TRUE, gBattlerSpriteIds[activeBattler]);
ClearBehindSubstituteBit(activeBattler);
return TRUE;
}
gBattleAnimAttacker = atkBattler;
gBattleAnimTarget = defBattler;
gBattleSpritesDataPtr->animationData->animArg = argument;
LaunchBattleAnimation(gBattleAnims_General, tableId, FALSE);
taskId = CreateTask(Task_ClearBitWhenBattleTableAnimDone, 10);
gTasks[taskId].tBattlerId = activeBattler;
gBattleSpritesDataPtr->healthBoxesData[gTasks[taskId].tBattlerId].animFromTableActive = 1;
return FALSE;
}
static void Task_ClearBitWhenBattleTableAnimDone(u8 taskId)
{
gAnimScriptCallback();
if (!gAnimScriptActive)
{
gBattleSpritesDataPtr->healthBoxesData[gTasks[taskId].tBattlerId].animFromTableActive = 0;
DestroyTask(taskId);
}
}
#undef tBattlerId
static bool8 ShouldAnimBeDoneRegardlessOfSubstitute(u8 animId)
{
switch (animId)
{
case B_ANIM_SUBSTITUTE_FADE:
case B_ANIM_RAIN_CONTINUES:
case B_ANIM_SUN_CONTINUES:
case B_ANIM_SANDSTORM_CONTINUES:
case B_ANIM_HAIL_CONTINUES:
case B_ANIM_SNATCH_MOVE:
return TRUE;
default:
return FALSE;
}
}
#define tBattlerId data[0]
void InitAndLaunchSpecialAnimation(u8 activeBattler, u8 atkBattler, u8 defBattler, u8 tableId)
{
u8 taskId;
gBattleAnimAttacker = atkBattler;
gBattleAnimTarget = defBattler;
LaunchBattleAnimation(gBattleAnims_Special, tableId, FALSE);
taskId = CreateTask(Task_ClearBitWhenSpecialAnimDone, 10);
gTasks[taskId].tBattlerId = activeBattler;
gBattleSpritesDataPtr->healthBoxesData[gTasks[taskId].tBattlerId].specialAnimActive = 1;
}
static void Task_ClearBitWhenSpecialAnimDone(u8 taskId)
{
gAnimScriptCallback();
if (!gAnimScriptActive)
{
gBattleSpritesDataPtr->healthBoxesData[gTasks[taskId].tBattlerId].specialAnimActive = 0;
DestroyTask(taskId);
}
}
#undef tBattlerId
// Great function to include newly added moves that don't have animation yet.
bool8 IsMoveWithoutAnimation(u16 moveId, u8 animationTurn)
{
return FALSE;
}
// Check if SE has finished or 30 calls, whichever comes first
bool8 IsBattleSEPlaying(u8 battlerId)
{
u8 zero = 0;
if (IsSEPlaying())
{
gBattleSpritesDataPtr->healthBoxesData[battlerId].soundTimer++;
if (gBattleSpritesDataPtr->healthBoxesData[gActiveBattler].soundTimer < 30)
return TRUE;
m4aMPlayStop(&gMPlayInfo_SE1);
m4aMPlayStop(&gMPlayInfo_SE2);
}
if (zero == 0)
{
gBattleSpritesDataPtr->healthBoxesData[battlerId].soundTimer = 0;
return FALSE;
}
// Never reached
return TRUE;
}
void BattleLoadOpponentMonSpriteGfx(struct Pokemon *mon, u8 battlerId)
{
u32 monsPersonality, currentPersonality, otId;
u16 species;
u8 position;
u16 paletteOffset;
const void *lzPaletteData;
monsPersonality = GetMonData(mon, MON_DATA_PERSONALITY);
if (gBattleSpritesDataPtr->battlerData[battlerId].transformSpecies == SPECIES_NONE)
{
species = GetMonData(mon, MON_DATA_SPECIES);
currentPersonality = monsPersonality;
}
else
{
species = gBattleSpritesDataPtr->battlerData[battlerId].transformSpecies;
currentPersonality = gTransformedPersonalities[battlerId];
}
otId = GetMonData(mon, MON_DATA_OT_ID);
position = GetBattlerPosition(battlerId);
HandleLoadSpecialPokePic_DontHandleDeoxys(&gMonFrontPicTable[species],
gMonSpritesGfxPtr->sprites.ptr[position],
species, currentPersonality);
paletteOffset = OBJ_PLTT_ID(battlerId);
if (gBattleSpritesDataPtr->battlerData[battlerId].transformSpecies == SPECIES_NONE)
lzPaletteData = GetMonFrontSpritePal(mon);
else
lzPaletteData = GetMonSpritePalFromSpeciesAndPersonality(species, otId, monsPersonality);
LZDecompressWram(lzPaletteData, gDecompressionBuffer);
LoadPalette(gDecompressionBuffer, paletteOffset, PLTT_SIZE_4BPP);
LoadPalette(gDecompressionBuffer, BG_PLTT_ID(8) + BG_PLTT_ID(battlerId), PLTT_SIZE_4BPP);
if (species == SPECIES_CASTFORM)
{
paletteOffset = OBJ_PLTT_ID(battlerId);
LZDecompressWram(lzPaletteData, gBattleStruct->castformPalette);
LoadPalette(gBattleStruct->castformPalette[gBattleMonForms[battlerId]], paletteOffset, PLTT_SIZE_4BPP);
}
// transform's pink color
if (gBattleSpritesDataPtr->battlerData[battlerId].transformSpecies != SPECIES_NONE)
{
BlendPalette(paletteOffset, 16, 6, RGB_WHITE);
CpuCopy32(&gPlttBufferFaded[paletteOffset], &gPlttBufferUnfaded[paletteOffset], PLTT_SIZEOF(16));
}
}
void BattleLoadPlayerMonSpriteGfx(struct Pokemon *mon, u8 battlerId)
{
u32 monsPersonality, currentPersonality, otId;
u16 species;
u8 position;
u16 paletteOffset;
const void *lzPaletteData;
monsPersonality = GetMonData(mon, MON_DATA_PERSONALITY);
if (gBattleSpritesDataPtr->battlerData[battlerId].transformSpecies == SPECIES_NONE)
{
species = GetMonData(mon, MON_DATA_SPECIES);
currentPersonality = monsPersonality;
}
else
{
species = gBattleSpritesDataPtr->battlerData[battlerId].transformSpecies;
currentPersonality = gTransformedPersonalities[battlerId];
}
otId = GetMonData(mon, MON_DATA_OT_ID);
position = GetBattlerPosition(battlerId);
if (ShouldIgnoreDeoxysForm(1, battlerId) == TRUE || gBattleSpritesDataPtr->battlerData[battlerId].transformSpecies != SPECIES_NONE)
{
HandleLoadSpecialPokePic_DontHandleDeoxys(&gMonBackPicTable[species],
gMonSpritesGfxPtr->sprites.ptr[position],
species, currentPersonality);
}
else
{
HandleLoadSpecialPokePic(&gMonBackPicTable[species],
gMonSpritesGfxPtr->sprites.ptr[position],
species, currentPersonality);
}
paletteOffset = OBJ_PLTT_ID(battlerId);
if (gBattleSpritesDataPtr->battlerData[battlerId].transformSpecies == SPECIES_NONE)
lzPaletteData = GetMonFrontSpritePal(mon);
else
lzPaletteData = GetMonSpritePalFromSpeciesAndPersonality(species, otId, monsPersonality);
LZDecompressWram(lzPaletteData, gDecompressionBuffer);
LoadPalette(gDecompressionBuffer, paletteOffset, PLTT_SIZE_4BPP);
LoadPalette(gDecompressionBuffer, BG_PLTT_ID(8) + BG_PLTT_ID(battlerId), PLTT_SIZE_4BPP);
if (species == SPECIES_CASTFORM)
{
paletteOffset = OBJ_PLTT_ID(battlerId);
LZDecompressWram(lzPaletteData, gBattleStruct->castformPalette);
LoadPalette(gBattleStruct->castformPalette[gBattleMonForms[battlerId]], paletteOffset, PLTT_SIZE_4BPP);
}
// transform's pink color
if (gBattleSpritesDataPtr->battlerData[battlerId].transformSpecies != SPECIES_NONE)
{
BlendPalette(paletteOffset, 16, 6, RGB_WHITE);
CpuCopy32(&gPlttBufferFaded[paletteOffset], &gPlttBufferUnfaded[paletteOffset], PLTT_SIZEOF(16));
}
}
static void UNUSED BattleGfxSfxDummy1(void)
{
}
void BattleGfxSfxDummy2(u16 species)
{
}
void DecompressTrainerFrontPic(u16 frontPicId, u8 battlerId)
{
u8 position = GetBattlerPosition(battlerId);
DecompressPicFromTable_2(&gTrainerFrontPicTable[frontPicId],
gMonSpritesGfxPtr->sprites.ptr[position],
SPECIES_NONE);
LoadCompressedSpritePalette(&gTrainerFrontPicPaletteTable[frontPicId]);
}
void DecompressTrainerBackPic(u16 backPicId, u8 battlerId)
{
u8 position = GetBattlerPosition(battlerId);
DecompressPicFromTable_2(&gTrainerBackPicTable[backPicId],
gMonSpritesGfxPtr->sprites.ptr[position],
SPECIES_NONE);
LoadCompressedPalette(gTrainerBackPicPaletteTable[backPicId].data,
OBJ_PLTT_ID(battlerId), PLTT_SIZE_4BPP);
}
void BattleGfxSfxDummy3(u8 gender)
{
}
void FreeTrainerFrontPicPalette(u16 frontPicId)
{
FreeSpritePaletteByTag(gTrainerFrontPicPaletteTable[frontPicId].tag);
}
// Unused.
void BattleLoadAllHealthBoxesGfxAtOnce(void)
{
u8 numberOfBattlers = 0;
u8 i;
LoadSpritePalette(&sSpritePalettes_HealthBoxHealthBar[0]);
LoadSpritePalette(&sSpritePalettes_HealthBoxHealthBar[1]);
if (!IsDoubleBattle())
{
LoadCompressedSpriteSheet(&sSpriteSheet_SinglesPlayerHealthbox);
LoadCompressedSpriteSheet(&sSpriteSheet_SinglesOpponentHealthbox);
numberOfBattlers = 2;
}
else
{
LoadCompressedSpriteSheet(&sSpriteSheets_DoublesPlayerHealthbox[0]);
LoadCompressedSpriteSheet(&sSpriteSheets_DoublesPlayerHealthbox[1]);
LoadCompressedSpriteSheet(&sSpriteSheets_DoublesOpponentHealthbox[0]);
LoadCompressedSpriteSheet(&sSpriteSheets_DoublesOpponentHealthbox[1]);
numberOfBattlers = MAX_BATTLERS_COUNT;
}
for (i = 0; i < numberOfBattlers; i++)
LoadCompressedSpriteSheet(&sSpriteSheets_HealthBar[gBattlerPositions[i]]);
}
bool8 BattleLoadAllHealthBoxesGfx(u8 state)
{
bool8 retVal = FALSE;
if (state != 0)
{
if (state == 1)
{
LoadSpritePalette(&sSpritePalettes_HealthBoxHealthBar[0]);
LoadSpritePalette(&sSpritePalettes_HealthBoxHealthBar[1]);
}
else if (!IsDoubleBattle())
{
if (state == 2)
{
if (gBattleTypeFlags & BATTLE_TYPE_SAFARI)
LoadCompressedSpriteSheet(&sSpriteSheet_SafariHealthbox);
else
LoadCompressedSpriteSheet(&sSpriteSheet_SinglesPlayerHealthbox);
}
else if (state == 3)
{
LoadCompressedSpriteSheet(&sSpriteSheet_SinglesOpponentHealthbox);
}
else if (state == 4)
{
LoadCompressedSpriteSheet(&sSpriteSheets_HealthBar[gBattlerPositions[0]]);
}
else if (state == 5)
{
LoadCompressedSpriteSheet(&sSpriteSheets_HealthBar[gBattlerPositions[1]]);
}
else
{
retVal = TRUE;
}
}
else
{
if (state == 2)
LoadCompressedSpriteSheet(&sSpriteSheets_DoublesPlayerHealthbox[0]);
else if (state == 3)
LoadCompressedSpriteSheet(&sSpriteSheets_DoublesPlayerHealthbox[1]);
else if (state == 4)
LoadCompressedSpriteSheet(&sSpriteSheets_DoublesOpponentHealthbox[0]);
else if (state == 5)
LoadCompressedSpriteSheet(&sSpriteSheets_DoublesOpponentHealthbox[1]);
else if (state == 6)
LoadCompressedSpriteSheet(&sSpriteSheets_HealthBar[gBattlerPositions[0]]);
else if (state == 7)
LoadCompressedSpriteSheet(&sSpriteSheets_HealthBar[gBattlerPositions[1]]);
else if (state == 8)
LoadCompressedSpriteSheet(&sSpriteSheets_HealthBar[gBattlerPositions[2]]);
else if (state == 9)
LoadCompressedSpriteSheet(&sSpriteSheets_HealthBar[gBattlerPositions[3]]);
else
retVal = TRUE;
}
}
return retVal;
}
void LoadBattleBarGfx(u8 unused)
{
LZDecompressWram(gBattleInterfaceGfx_BattleBar, gMonSpritesGfxPtr->barFontGfx);
}
bool8 BattleInitAllSprites(u8 *state1, u8 *battlerId)
{
bool8 retVal = FALSE;
switch (*state1)
{
case 0:
ClearSpritesBattlerHealthboxAnimData();
(*state1)++;
break;
case 1:
if (!BattleLoadAllHealthBoxesGfx(*battlerId))
{
(*battlerId)++;
}
else
{
*battlerId = 0;
(*state1)++;
}
break;
case 2:
(*state1)++;
break;
case 3:
if ((gBattleTypeFlags & BATTLE_TYPE_SAFARI) && *battlerId == 0)
gHealthboxSpriteIds[*battlerId] = CreateSafariPlayerHealthboxSprites();
else
gHealthboxSpriteIds[*battlerId] = CreateBattlerHealthboxSprites(*battlerId);
(*battlerId)++;
if (*battlerId == gBattlersCount)
{
*battlerId = 0;
(*state1)++;
}
break;
case 4:
InitBattlerHealthboxCoords(*battlerId);
if (gBattlerPositions[*battlerId] <= B_POSITION_OPPONENT_LEFT)
DummyBattleInterfaceFunc(gHealthboxSpriteIds[*battlerId], FALSE);
else
DummyBattleInterfaceFunc(gHealthboxSpriteIds[*battlerId], TRUE);
(*battlerId)++;
if (*battlerId == gBattlersCount)
{
*battlerId = 0;
(*state1)++;
}
break;
case 5:
if (GetBattlerSide(*battlerId) == B_SIDE_PLAYER)
{
if (!(gBattleTypeFlags & BATTLE_TYPE_SAFARI))
UpdateHealthboxAttribute(gHealthboxSpriteIds[*battlerId], &gPlayerParty[gBattlerPartyIndexes[*battlerId]], HEALTHBOX_ALL);
}
else
{
UpdateHealthboxAttribute(gHealthboxSpriteIds[*battlerId], &gEnemyParty[gBattlerPartyIndexes[*battlerId]], HEALTHBOX_ALL);
}
SetHealthboxSpriteInvisible(gHealthboxSpriteIds[*battlerId]);
(*battlerId)++;
if (*battlerId == gBattlersCount)
{
*battlerId = 0;
(*state1)++;
}
break;
case 6:
LoadAndCreateEnemyShadowSprites();
BufferBattlePartyCurrentOrder();
retVal = TRUE;
break;
}
return retVal;
}
void ClearSpritesHealthboxAnimData(void)
{
memset(gBattleSpritesDataPtr->healthBoxesData, 0, sizeof(struct BattleHealthboxInfo) * MAX_BATTLERS_COUNT);
memset(gBattleSpritesDataPtr->animationData, 0, sizeof(struct BattleAnimationInfo));
}
static void ClearSpritesBattlerHealthboxAnimData(void)
{
ClearSpritesHealthboxAnimData();
memset(gBattleSpritesDataPtr->battlerData, 0, sizeof(struct BattleSpriteInfo) * MAX_BATTLERS_COUNT);
}
void CopyAllBattleSpritesInvisibilities(void)
{
s32 i;
for (i = 0; i < gBattlersCount; i++)
gBattleSpritesDataPtr->battlerData[i].invisible = gSprites[gBattlerSpriteIds[i]].invisible;
}
void CopyBattleSpriteInvisibility(u8 battlerId)
{
gBattleSpritesDataPtr->battlerData[battlerId].invisible = gSprites[gBattlerSpriteIds[battlerId]].invisible;
}
void HandleSpeciesGfxDataChange(u8 battlerAtk, u8 battlerDef, bool8 castform)
{
u16 paletteOffset;
u32 personalityValue;
u32 otId;
u8 position;
const u32 *lzPaletteData;
if (castform)
{
StartSpriteAnim(&gSprites[gBattlerSpriteIds[battlerAtk]], gBattleSpritesDataPtr->animationData->animArg);
paletteOffset = OBJ_PLTT_ID(battlerAtk);
LoadPalette(gBattleStruct->castformPalette[gBattleSpritesDataPtr->animationData->animArg], paletteOffset, PLTT_SIZE_4BPP);
gBattleMonForms[battlerAtk] = gBattleSpritesDataPtr->animationData->animArg;
if (gBattleSpritesDataPtr->battlerData[battlerAtk].transformSpecies != SPECIES_NONE)
{
BlendPalette(paletteOffset, 16, 6, RGB_WHITE);
CpuCopy32(&gPlttBufferFaded[paletteOffset], &gPlttBufferUnfaded[paletteOffset], PLTT_SIZEOF(16));
}
gSprites[gBattlerSpriteIds[battlerAtk]].y = GetBattlerSpriteDefault_Y(battlerAtk);
}
else
{
const void *src;
void *dst;
u16 targetSpecies;
if (IsContest())
{
position = B_POSITION_PLAYER_LEFT;
targetSpecies = gContestResources->moveAnim->targetSpecies;
personalityValue = gContestResources->moveAnim->personality;
otId = gContestResources->moveAnim->otId;
HandleLoadSpecialPokePic_DontHandleDeoxys(&gMonBackPicTable[targetSpecies],
gMonSpritesGfxPtr->sprites.ptr[position],
targetSpecies,
gContestResources->moveAnim->targetPersonality);
}
else
{
position = GetBattlerPosition(battlerAtk);
if (GetBattlerSide(battlerDef) == B_SIDE_OPPONENT)
targetSpecies = GetMonData(&gEnemyParty[gBattlerPartyIndexes[battlerDef]], MON_DATA_SPECIES);
else
targetSpecies = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerDef]], MON_DATA_SPECIES);
if (GetBattlerSide(battlerAtk) == B_SIDE_PLAYER)
{
personalityValue = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerAtk]], MON_DATA_PERSONALITY);
otId = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerAtk]], MON_DATA_OT_ID);
HandleLoadSpecialPokePic_DontHandleDeoxys(&gMonBackPicTable[targetSpecies],
gMonSpritesGfxPtr->sprites.ptr[position],
targetSpecies,
gTransformedPersonalities[battlerAtk]);
}
else
{
personalityValue = GetMonData(&gEnemyParty[gBattlerPartyIndexes[battlerAtk]], MON_DATA_PERSONALITY);
otId = GetMonData(&gEnemyParty[gBattlerPartyIndexes[battlerAtk]], MON_DATA_OT_ID);
HandleLoadSpecialPokePic_DontHandleDeoxys(&gMonFrontPicTable[targetSpecies],
gMonSpritesGfxPtr->sprites.ptr[position],
targetSpecies,
gTransformedPersonalities[battlerAtk]);
}
}
src = gMonSpritesGfxPtr->sprites.ptr[position];
dst = (void *)(OBJ_VRAM0 + gSprites[gBattlerSpriteIds[battlerAtk]].oam.tileNum * 32);
DmaCopy32(3, src, dst, MON_PIC_SIZE);
paletteOffset = OBJ_PLTT_ID(battlerAtk);