forked from rh-hideout/pokeemerald-expansion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecorded_battle.c
842 lines (719 loc) · 28.5 KB
/
recorded_battle.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
#include "global.h"
#include "battle.h"
#include "battle_anim.h"
#include "battle_controllers.h"
#include "recorded_battle.h"
#include "main.h"
#include "pokemon.h"
#include "random.h"
#include "event_data.h"
#include "link.h"
#include "string_util.h"
#include "palette.h"
#include "save.h"
#include "malloc.h"
#include "util.h"
#include "task.h"
#include "test_runner.h"
#include "text.h"
#include "battle_setup.h"
#include "frontier_util.h"
#include "constants/trainers.h"
#include "constants/rgb.h"
struct PlayerInfo
{
u32 trainerId;
u8 name[PLAYER_NAME_LENGTH + 1];
u8 gender;
u16 battlerId;
u16 language;
};
// Save data using TryWriteSpecialSaveSector is allowed to exceed SECTOR_DATA_SIZE (up to the counter field)
STATIC_ASSERT(sizeof(struct RecordedBattleSave) <= SECTOR_COUNTER_OFFSET, RecordedBattleSaveFreeSpace);
EWRAM_DATA rng_value_t gRecordedBattleRngSeed = RNG_VALUE_EMPTY;
EWRAM_DATA rng_value_t gBattlePalaceMoveSelectionRngValue = RNG_VALUE_EMPTY;
EWRAM_DATA static u8 sBattleRecords[MAX_BATTLERS_COUNT][BATTLER_RECORD_SIZE] = {0};
EWRAM_DATA static u16 sBattlerRecordSizes[MAX_BATTLERS_COUNT] = {0};
EWRAM_DATA static u16 sBattlerPrevRecordSizes[MAX_BATTLERS_COUNT] = {0};
EWRAM_DATA static u16 sBattlerSavedRecordSizes[MAX_BATTLERS_COUNT] = {0};
EWRAM_DATA static u8 sRecordMode = 0;
EWRAM_DATA static u8 sLvlMode = 0;
EWRAM_DATA static u8 sFrontierFacility = 0;
EWRAM_DATA static u8 sFrontierBrainSymbol = 0;
EWRAM_DATA static MainCallback sCallback2_AfterRecordedBattle = NULL;
EWRAM_DATA u8 gRecordedBattleMultiplayerId = 0;
EWRAM_DATA static u8 sFrontierPassFlag = 0;
EWRAM_DATA static u8 sBattleScene = 0;
EWRAM_DATA static u8 sTextSpeed = 0;
EWRAM_DATA static u32 sBattleFlags = 0;
EWRAM_DATA static u32 sAI_Scripts = 0;
EWRAM_DATA static struct Pokemon sSavedPlayerParty[PARTY_SIZE] = {0};
EWRAM_DATA static struct Pokemon sSavedOpponentParty[PARTY_SIZE] = {0};
EWRAM_DATA static u16 sPlayerMonMoves[MAX_BATTLERS_COUNT / 2][MAX_MON_MOVES] = {0};
EWRAM_DATA static struct PlayerInfo sPlayers[MAX_LINK_PLAYERS] = {0};
EWRAM_DATA static bool8 sIsPlaybackFinished = 0;
EWRAM_DATA static u8 sRecordMixFriendName[PLAYER_NAME_LENGTH + 1] = {0};
EWRAM_DATA static u8 sRecordMixFriendClass = 0;
EWRAM_DATA static u8 sApprenticeId = 0;
EWRAM_DATA static u16 sEasyChatSpeech[EASY_CHAT_BATTLE_WORDS_COUNT] = {0};
EWRAM_DATA static u8 sBattleOutcome = 0;
static u8 sRecordMixFriendLanguage;
static u8 sApprenticeLanguage;
static u8 GetNextRecordedDataByte(u8 *, u8 *, u8 *);
static bool32 CopyRecordedBattleFromSave(struct RecordedBattleSave *);
static void RecordedBattle_RestoreSavedParties(void);
static void CB2_RecordedBattle(void);
void RecordedBattle_Init(u8 mode)
{
s32 i, j;
sRecordMode = mode;
sIsPlaybackFinished = FALSE;
for (i = 0; i < MAX_BATTLERS_COUNT; i++)
{
sBattlerRecordSizes[i] = 0;
sBattlerPrevRecordSizes[i] = 0;
sBattlerSavedRecordSizes[i] = 0;
if (mode == B_RECORD_MODE_RECORDING)
{
for (j = 0; j < BATTLER_RECORD_SIZE; j++)
sBattleRecords[i][j] = 0xFF;
sBattleFlags = gBattleTypeFlags;
sAI_Scripts = gBattleResources->ai->aiFlags[B_POSITION_OPPONENT_LEFT];
}
}
}
void RecordedBattle_SetTrainerInfo(void)
{
s32 i, j;
if (sRecordMode == B_RECORD_MODE_RECORDING)
{
gRecordedBattleRngSeed = gRngValue;
sFrontierFacility = VarGet(VAR_FRONTIER_FACILITY);
sFrontierBrainSymbol = GetFronterBrainSymbol();
}
else if (sRecordMode == B_RECORD_MODE_PLAYBACK)
{
gRngValue = gRecordedBattleRngSeed;
}
if (gBattleTypeFlags & BATTLE_TYPE_LINK)
{
// Link recorded battle, record info for all trainers
u8 linkPlayersCount;
u8 text[30];
gRecordedBattleMultiplayerId = GetMultiplayerId();
linkPlayersCount = GetLinkPlayerCount();
for (i = 0; i < MAX_LINK_PLAYERS; i++)
{
sPlayers[i].trainerId = gLinkPlayers[i].trainerId;
sPlayers[i].gender = gLinkPlayers[i].gender;
sPlayers[i].battlerId = gLinkPlayers[i].id;
sPlayers[i].language = gLinkPlayers[i].language;
// Record names
if (i < linkPlayersCount)
{
StringCopy(text, gLinkPlayers[i].name);
StripExtCtrlCodes(text);
StringCopy(sPlayers[i].name, text);
}
else
{
for (j = 0; j < PLAYER_NAME_LENGTH + 1; j++)
sPlayers[i].name[j] = gLinkPlayers[i].name[j];
}
}
}
else
{
// Local battle, just record own info
sPlayers[0].trainerId = (gSaveBlock2Ptr->playerTrainerId[0])
| (gSaveBlock2Ptr->playerTrainerId[1] << 8)
| (gSaveBlock2Ptr->playerTrainerId[2] << 16)
| (gSaveBlock2Ptr->playerTrainerId[3] << 24);
sPlayers[0].gender = gSaveBlock2Ptr->playerGender;
sPlayers[0].battlerId = 0;
sPlayers[0].language = gGameLanguage;
for (i = 0; i < PLAYER_NAME_LENGTH + 1; i++)
sPlayers[0].name[i] = gSaveBlock2Ptr->playerName[i];
}
}
void RecordedBattle_SetBattlerAction(u8 battlerId, u8 action)
{
if (sBattlerRecordSizes[battlerId] < BATTLER_RECORD_SIZE && sRecordMode != B_RECORD_MODE_PLAYBACK)
sBattleRecords[battlerId][sBattlerRecordSizes[battlerId]++] = action;
}
void RecordedBattle_ClearBattlerAction(u8 battlerId, u8 bytesToClear)
{
s32 i;
for (i = 0; i < bytesToClear; i++)
{
sBattlerRecordSizes[battlerId]--;
sBattleRecords[battlerId][sBattlerRecordSizes[battlerId]] = 0xFF;
if (sBattlerRecordSizes[battlerId] == 0)
break;
}
}
u8 RecordedBattle_GetBattlerAction(u32 actionType, u8 battlerId)
{
if (gTestRunnerEnabled)
TestRunner_Battle_CheckBattleRecordActionType(battlerId, sBattlerRecordSizes[battlerId], actionType);
// Trying to read past array or invalid action byte, battle is over.
if (sBattlerRecordSizes[battlerId] >= BATTLER_RECORD_SIZE || sBattleRecords[battlerId][sBattlerRecordSizes[battlerId]] == 0xFF)
{
gSpecialVar_Result = gBattleOutcome = B_OUTCOME_PLAYER_TELEPORTED; // hah
ResetPaletteFadeControl();
BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 16, RGB_BLACK);
SetMainCallback2(CB2_QuitRecordedBattle);
return 0xFF;
}
else
{
return sBattleRecords[battlerId][sBattlerRecordSizes[battlerId]++];
}
}
static u8 UNUSED GetRecordedBattleMode(void)
{
return sRecordMode;
}
u8 RecordedBattle_BufferNewBattlerData(u8 *dst)
{
u8 i, j;
u8 idx = 0;
for (i = 0; i < MAX_BATTLERS_COUNT; i++)
{
if (sBattlerRecordSizes[i] != sBattlerPrevRecordSizes[i])
{
dst[idx++] = i;
dst[idx++] = sBattlerRecordSizes[i] - sBattlerPrevRecordSizes[i];
for (j = 0; j < sBattlerRecordSizes[i] - sBattlerPrevRecordSizes[i]; j++)
dst[idx++] = sBattleRecords[i][sBattlerPrevRecordSizes[i] + j];
sBattlerPrevRecordSizes[i] = sBattlerRecordSizes[i];
}
}
return idx;
}
void RecordedBattle_RecordAllBattlerData(u8 *src)
{
s32 i;
u8 idx = 2;
u8 size;
if (!(gBattleTypeFlags & BATTLE_TYPE_LINK))
return;
for (i = 0; i < GetLinkPlayerCount(); i++)
{
if ((gLinkPlayers[i].version & 0xFF) != VERSION_EMERALD)
return;
}
if (!(gBattleTypeFlags & BATTLE_TYPE_IS_MASTER))
{
for (size = *src; size != 0;)
{
u8 battlerId = GetNextRecordedDataByte(src, &idx, &size);
u8 numActions = GetNextRecordedDataByte(src, &idx, &size);
for (i = 0; i < numActions; i++)
sBattleRecords[battlerId][sBattlerSavedRecordSizes[battlerId]++] = GetNextRecordedDataByte(src, &idx, &size);
}
}
}
static u8 GetNextRecordedDataByte(u8 *data, u8 *idx, u8 *size)
{
(*size)--;
return data[(*idx)++];
}
bool32 CanCopyRecordedBattleSaveData(void)
{
struct RecordedBattleSave *dst = AllocZeroed(sizeof(struct RecordedBattleSave));
bool32 ret = CopyRecordedBattleFromSave(dst);
Free(dst);
return ret;
}
static bool32 IsRecordedBattleSaveValid(struct RecordedBattleSave *save)
{
if (save->battleFlags == 0)
return FALSE;
if (save->battleFlags & BATTLE_TYPE_RECORDED_INVALID)
return FALSE;
if (CalcByteArraySum((void *)(save), sizeof(*save) - 4) != save->checksum)
return FALSE;
return TRUE;
}
static bool32 RecordedBattleToSave(struct RecordedBattleSave *battleSave, struct RecordedBattleSave *saveSector)
{
memset(saveSector, 0, SECTOR_SIZE);
memcpy(saveSector, battleSave, sizeof(*battleSave));
saveSector->checksum = CalcByteArraySum((void *)(saveSector), sizeof(*saveSector) - 4);
if (TryWriteSpecialSaveSector(SECTOR_ID_RECORDED_BATTLE, (void *)(saveSector)) != SAVE_STATUS_OK)
return FALSE;
else
return TRUE;
}
bool32 MoveRecordedBattleToSaveData(void)
{
s32 i, j;
bool32 ret;
struct RecordedBattleSave *battleSave, *savSection;
u8 saveAttempts;
saveAttempts = 0;
battleSave = AllocZeroed(sizeof(struct RecordedBattleSave));
savSection = AllocZeroed(SECTOR_SIZE);
for (i = 0; i < PARTY_SIZE; i++)
{
battleSave->playerParty[i] = sSavedPlayerParty[i];
battleSave->opponentParty[i] = sSavedOpponentParty[i];
}
for (i = 0; i < MAX_LINK_PLAYERS; i++)
{
for (j = 0; j < PLAYER_NAME_LENGTH + 1; j++)
battleSave->playersName[i][j] = sPlayers[i].name[j];
battleSave->playersGender[i] = sPlayers[i].gender;
battleSave->playersLanguage[i] = sPlayers[i].language;
battleSave->playersBattlers[i] = sPlayers[i].battlerId;
battleSave->playersTrainerId[i] = sPlayers[i].trainerId;
}
battleSave->rngSeed = gRecordedBattleRngSeed;
if (sBattleFlags & BATTLE_TYPE_LINK)
{
battleSave->battleFlags = (sBattleFlags & ~(BATTLE_TYPE_LINK | BATTLE_TYPE_LINK_IN_BATTLE)) | BATTLE_TYPE_RECORDED_LINK;
// BATTLE_TYPE_RECORDED_IS_MASTER set indicates battle will play
// out from player's perspective (i.e. player with back to camera)
// Otherwise player will appear on "opponent" side
if (sBattleFlags & BATTLE_TYPE_IS_MASTER)
{
battleSave->battleFlags |= BATTLE_TYPE_RECORDED_IS_MASTER;
}
else if (sBattleFlags & BATTLE_TYPE_MULTI)
{
switch (sPlayers[0].battlerId)
{
case 0:
case 2:
if (!(sPlayers[gRecordedBattleMultiplayerId].battlerId & 1))
battleSave->battleFlags |= BATTLE_TYPE_RECORDED_IS_MASTER;
break;
case 1:
case 3:
if ((sPlayers[gRecordedBattleMultiplayerId].battlerId & 1))
battleSave->battleFlags |= BATTLE_TYPE_RECORDED_IS_MASTER;
break;
}
}
}
else
{
battleSave->battleFlags = sBattleFlags;
}
battleSave->opponentA = TRAINER_BATTLE_PARAM.opponentA;
battleSave->opponentB = TRAINER_BATTLE_PARAM.opponentB;
battleSave->partnerId = gPartnerTrainerId;
battleSave->multiplayerId = gRecordedBattleMultiplayerId;
battleSave->lvlMode = gSaveBlock2Ptr->frontier.lvlMode;
battleSave->frontierFacility = sFrontierFacility;
battleSave->frontierBrainSymbol = sFrontierBrainSymbol;
battleSave->battleScene = gSaveBlock2Ptr->optionsBattleSceneOff;
battleSave->textSpeed = OPTIONS_TEXT_SPEED_FAST;
battleSave->AI_scripts = sAI_Scripts;
if (TRAINER_BATTLE_PARAM.opponentA >= TRAINER_RECORD_MIXING_FRIEND && TRAINER_BATTLE_PARAM.opponentA < TRAINER_RECORD_MIXING_APPRENTICE)
{
for (i = 0; i < PLAYER_NAME_LENGTH + 1; i++)
battleSave->recordMixFriendName[i] = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentA - TRAINER_RECORD_MIXING_FRIEND].name[i];
battleSave->recordMixFriendClass = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentA - TRAINER_RECORD_MIXING_FRIEND].facilityClass;
if (sBattleOutcome == B_OUTCOME_WON)
{
for (i = 0; i < EASY_CHAT_BATTLE_WORDS_COUNT; i++)
battleSave->easyChatSpeech[i] = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentA - TRAINER_RECORD_MIXING_FRIEND].speechLost[i];
}
else
{
for (i = 0; i < EASY_CHAT_BATTLE_WORDS_COUNT; i++)
battleSave->easyChatSpeech[i] = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentA - TRAINER_RECORD_MIXING_FRIEND].speechWon[i];
}
battleSave->recordMixFriendLanguage = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentA - TRAINER_RECORD_MIXING_FRIEND].language;
}
else if (TRAINER_BATTLE_PARAM.opponentB >= TRAINER_RECORD_MIXING_FRIEND && TRAINER_BATTLE_PARAM.opponentB < TRAINER_RECORD_MIXING_APPRENTICE)
{
for (i = 0; i < PLAYER_NAME_LENGTH + 1; i++)
battleSave->recordMixFriendName[i] = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentB - TRAINER_RECORD_MIXING_FRIEND].name[i];
battleSave->recordMixFriendClass = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentB - TRAINER_RECORD_MIXING_FRIEND].facilityClass;
if (sBattleOutcome == B_OUTCOME_WON)
{
for (i = 0; i < EASY_CHAT_BATTLE_WORDS_COUNT; i++)
battleSave->easyChatSpeech[i] = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentB - TRAINER_RECORD_MIXING_FRIEND].speechLost[i];
}
else
{
for (i = 0; i < EASY_CHAT_BATTLE_WORDS_COUNT; i++)
battleSave->easyChatSpeech[i] = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentB - TRAINER_RECORD_MIXING_FRIEND].speechWon[i];
}
battleSave->recordMixFriendLanguage = gSaveBlock2Ptr->frontier.towerRecords[TRAINER_BATTLE_PARAM.opponentB - TRAINER_RECORD_MIXING_FRIEND].language;
}
else if (gPartnerTrainerId >= TRAINER_RECORD_MIXING_FRIEND && gPartnerTrainerId < TRAINER_RECORD_MIXING_APPRENTICE)
{
for (i = 0; i < PLAYER_NAME_LENGTH + 1; i++)
battleSave->recordMixFriendName[i] = gSaveBlock2Ptr->frontier.towerRecords[gPartnerTrainerId - TRAINER_RECORD_MIXING_FRIEND].name[i];
battleSave->recordMixFriendClass = gSaveBlock2Ptr->frontier.towerRecords[gPartnerTrainerId - TRAINER_RECORD_MIXING_FRIEND].facilityClass;
battleSave->recordMixFriendLanguage = gSaveBlock2Ptr->frontier.towerRecords[gPartnerTrainerId - TRAINER_RECORD_MIXING_FRIEND].language;
}
// if (TRAINER_BATTLE_PARAM.opponentA >= TRAINER_RECORD_MIXING_APPRENTICE)
// {
// battleSave->apprenticeId = gSaveBlock2Ptr->apprentices[TRAINER_BATTLE_PARAM.opponentA - TRAINER_RECORD_MIXING_APPRENTICE].id;
// for (i = 0; i < EASY_CHAT_BATTLE_WORDS_COUNT; i++)
// battleSave->easyChatSpeech[i] = gSaveBlock2Ptr->apprentices[TRAINER_BATTLE_PARAM.opponentA - TRAINER_RECORD_MIXING_APPRENTICE].speechWon[i];
// battleSave->apprenticeLanguage = gSaveBlock2Ptr->apprentices[TRAINER_BATTLE_PARAM.opponentA - TRAINER_RECORD_MIXING_APPRENTICE].language;
// }
// else if (gTrainerBattleOpponent_B >= TRAINER_RECORD_MIXING_APPRENTICE)
// {
// battleSave->apprenticeId = gSaveBlock2Ptr->apprentices[gTrainerBattleOpponent_B - TRAINER_RECORD_MIXING_APPRENTICE].id;
// for (i = 0; i < EASY_CHAT_BATTLE_WORDS_COUNT; i++)
// battleSave->easyChatSpeech[i] = gSaveBlock2Ptr->apprentices[gTrainerBattleOpponent_B - TRAINER_RECORD_MIXING_APPRENTICE].speechWon[i];
// battleSave->apprenticeLanguage = gSaveBlock2Ptr->apprentices[gTrainerBattleOpponent_B - TRAINER_RECORD_MIXING_APPRENTICE].language;
// }
// else if (gPartnerTrainerId >= TRAINER_RECORD_MIXING_APPRENTICE)
// {
// battleSave->apprenticeId = gSaveBlock2Ptr->apprentices[gPartnerTrainerId - TRAINER_RECORD_MIXING_APPRENTICE].id;
// battleSave->apprenticeLanguage = gSaveBlock2Ptr->apprentices[gPartnerTrainerId - TRAINER_RECORD_MIXING_APPRENTICE].language;
// }
for (i = 0; i < MAX_BATTLERS_COUNT; i++)
for (j = 0; j < BATTLER_RECORD_SIZE; j++)
battleSave->battleRecord[i][j] = sBattleRecords[i][j];
while (1)
{
ret = RecordedBattleToSave(battleSave, savSection);
if (ret == TRUE)
break;
saveAttempts++;
if (saveAttempts >= 3)
break;
}
Free(battleSave);
Free(savSection);
return ret;
}
static bool32 TryCopyRecordedBattleSaveData(struct RecordedBattleSave *dst, struct SaveSector *saveBuffer)
{
if (TryReadSpecialSaveSector(SECTOR_ID_RECORDED_BATTLE, (void *)(saveBuffer)) != SAVE_STATUS_OK)
return FALSE;
memcpy(dst, saveBuffer, sizeof(struct RecordedBattleSave));
if (!IsRecordedBattleSaveValid(dst))
return FALSE;
return TRUE;
}
static bool32 CopyRecordedBattleFromSave(struct RecordedBattleSave *dst)
{
struct SaveSector *savBuffer = AllocZeroed(SECTOR_SIZE);
bool32 ret = TryCopyRecordedBattleSaveData(dst, savBuffer);
Free(savBuffer);
return ret;
}
static void CB2_RecordedBattleEnd(void)
{
gSaveBlock2Ptr->frontier.lvlMode = sLvlMode;
gBattleOutcome = 0;
gBattleTypeFlags = 0;
TRAINER_BATTLE_PARAM.opponentA = 0;
TRAINER_BATTLE_PARAM.opponentB = 0;
gPartnerTrainerId = 0;
RecordedBattle_RestoreSavedParties();
SetMainCallback2(sCallback2_AfterRecordedBattle);
}
#define tFramesToWait data[0]
static void Task_StartAfterCountdown(u8 taskId)
{
if (--gTasks[taskId].tFramesToWait == 0)
{
gMain.savedCallback = CB2_RecordedBattleEnd;
SetMainCallback2(CB2_InitBattle);
DestroyTask(taskId);
}
}
void SetPartiesFromRecordedSave(struct RecordedBattleSave *src)
{
s32 i;
ZeroPlayerPartyMons();
ZeroEnemyPartyMons();
for (i = 0; i < PARTY_SIZE; i++)
{
gPlayerParty[i] = src->playerParty[i];
gEnemyParty[i] = src->opponentParty[i];
}
}
void SetVariablesForRecordedBattle(struct RecordedBattleSave *src)
{
bool8 var;
s32 i, j;
SetPartiesFromRecordedSave(src);
for (i = 0; i < MAX_LINK_PLAYERS; i++)
{
for (var = FALSE, j = 0; j < PLAYER_NAME_LENGTH + 1; j++)
{
gLinkPlayers[i].name[j] = src->playersName[i][j];
if (src->playersName[i][j] == EOS)
var = TRUE;
}
gLinkPlayers[i].gender = src->playersGender[i];
gLinkPlayers[i].language = src->playersLanguage[i];
gLinkPlayers[i].id = src->playersBattlers[i];
gLinkPlayers[i].trainerId = src->playersTrainerId[i];
if (var)
ConvertInternationalString(gLinkPlayers[i].name, gLinkPlayers[i].language);
}
gRecordedBattleRngSeed = src->rngSeed;
gBattleTypeFlags = src->battleFlags | BATTLE_TYPE_RECORDED;
TRAINER_BATTLE_PARAM.opponentA = src->opponentA;
TRAINER_BATTLE_PARAM.opponentB = src->opponentB;
gPartnerTrainerId = src->partnerId;
gRecordedBattleMultiplayerId = src->multiplayerId;
sLvlMode = gSaveBlock2Ptr->frontier.lvlMode;
sFrontierFacility = src->frontierFacility;
sFrontierBrainSymbol = src->frontierBrainSymbol;
sBattleScene = src->battleScene;
sTextSpeed = src->textSpeed;
sAI_Scripts = src->AI_scripts;
for (i = 0; i < PLAYER_NAME_LENGTH + 1; i++)
sRecordMixFriendName[i] = src->recordMixFriendName[i];
sRecordMixFriendClass = src->recordMixFriendClass;
sApprenticeId = src->apprenticeId;
sRecordMixFriendLanguage = src->recordMixFriendLanguage;
sApprenticeLanguage = src->apprenticeLanguage;
for (i = 0; i < EASY_CHAT_BATTLE_WORDS_COUNT; i++)
sEasyChatSpeech[i] = src->easyChatSpeech[i];
gSaveBlock2Ptr->frontier.lvlMode = src->lvlMode;
for (i = 0; i < MAX_BATTLERS_COUNT; i++)
for (j = 0; j < BATTLER_RECORD_SIZE; j++)
sBattleRecords[i][j] = src->battleRecord[i][j];
}
void PlayRecordedBattle(void (*CB2_After)(void))
{
struct RecordedBattleSave *battleSave = AllocZeroed(sizeof(struct RecordedBattleSave));
if (CopyRecordedBattleFromSave(battleSave) == TRUE)
{
u8 taskId;
RecordedBattle_SaveParties();
SetVariablesForRecordedBattle(battleSave);
taskId = CreateTask(Task_StartAfterCountdown, 1);
gTasks[taskId].tFramesToWait = 128;
sCallback2_AfterRecordedBattle = CB2_After;
PlayMapChosenOrBattleBGM(FALSE);
SetMainCallback2(CB2_RecordedBattle);
}
Free(battleSave);
}
#undef tFramesToWait
static void CB2_RecordedBattle(void)
{
AnimateSprites();
BuildOamBuffer();
RunTasks();
}
u8 GetRecordedBattleFrontierFacility(void)
{
return sFrontierFacility;
}
u8 GetRecordedBattleFronterBrainSymbol(void)
{
return sFrontierBrainSymbol;
}
void RecordedBattle_SaveParties(void)
{
s32 i;
for (i = 0; i < PARTY_SIZE; i++)
{
sSavedPlayerParty[i] = gPlayerParty[i];
sSavedOpponentParty[i] = gEnemyParty[i];
}
}
static void RecordedBattle_RestoreSavedParties(void)
{
s32 i;
for (i = 0; i < PARTY_SIZE; i++)
{
gPlayerParty[i] = sSavedPlayerParty[i];
gEnemyParty[i] = sSavedOpponentParty[i];
}
}
u8 GetBattlerLinkPlayerGender(u32 battler)
{
s32 i;
for (i = 0; i < MAX_LINK_PLAYERS; i++)
{
if (gLinkPlayers[i].id == battler)
break;
}
if (i != MAX_LINK_PLAYERS)
return gLinkPlayers[i].gender;
return 0;
}
void RecordedBattle_ClearFrontierPassFlag(void)
{
sFrontierPassFlag = 0;
}
// Set sFrontierPassFlag to received state of FLAG_SYS_FRONTIER_PASS
void RecordedBattle_SetFrontierPassFlagFromHword(u16 flags)
{
sFrontierPassFlag |= (flags & (1 << 15)) >> 15;
}
u8 RecordedBattle_GetFrontierPassFlag(void)
{
return sFrontierPassFlag;
}
u8 GetBattleSceneInRecordedBattle(void)
{
return sBattleScene;
}
u8 GetTextSpeedInRecordedBattle(void)
{
return sTextSpeed;
}
void RecordedBattle_CopyBattlerMoves(u32 battler)
{
s32 i;
if (GetBattlerSide(battler) == B_SIDE_OPPONENT)
return;
if (gBattleTypeFlags & (BATTLE_TYPE_LINK | BATTLE_TYPE_RECORDED_LINK))
return;
if (sRecordMode == B_RECORD_MODE_PLAYBACK)
return;
for (i = 0; i < MAX_MON_MOVES; i++)
sPlayerMonMoves[battler / 2][i] = gBattleMons[battler].moves[i];
}
// This is a special battle action only used by this function
// It shares a value with B_ACTION_SAFARI_POKEBLOCK, which can never occur in a recorded battle.
#define ACTION_MOVE_CHANGE 6
void RecordedBattle_CheckMovesetChanges(u8 mode)
{
s32 battlerId, j, k;
if (gBattleTypeFlags & (BATTLE_TYPE_LINK | BATTLE_TYPE_RECORDED_LINK))
return;
for (battlerId = 0; battlerId < gBattlersCount; battlerId++)
{
// Player's side only
if (GetBattlerSide(battlerId) != B_SIDE_OPPONENT)
{
if (mode == B_RECORD_MODE_RECORDING)
{
// Check if any of the battler's moves have changed.
for (j = 0; j < MAX_MON_MOVES; j++)
{
if (gBattleMons[battlerId].moves[j] != sPlayerMonMoves[battlerId / 2][j])
break;
}
if (j != MAX_MON_MOVES)
{
// At least one of the moves has been changed
RecordedBattle_SetBattlerAction(battlerId, ACTION_MOVE_CHANGE);
for (j = 0; j < MAX_MON_MOVES; j++)
{
for (k = 0; k < MAX_MON_MOVES; k++)
{
if (gBattleMons[battlerId].moves[j] == sPlayerMonMoves[battlerId / 2][k])
{
RecordedBattle_SetBattlerAction(battlerId, k);
break;
}
}
}
}
}
else // B_RECORD_MODE_PLAYBACK
{
if (sBattleRecords[battlerId][sBattlerRecordSizes[battlerId]] == ACTION_MOVE_CHANGE)
{
u8 ppBonuses[MAX_MON_MOVES];
u8 moveSlots[MAX_MON_MOVES];
u8 mimickedMoveSlots[MAX_MON_MOVES];
struct ChooseMoveStruct movePp;
u8 ppBonusSet;
// We know the current action is ACTION_MOVE_CHANGE, retrieve
// it without saving it to move on to the next action.
RecordedBattle_GetBattlerAction(RECORDED_BYTE, battlerId);
for (j = 0; j < MAX_MON_MOVES; j++)
ppBonuses[j] = ((gBattleMons[battlerId].ppBonuses & (3 << (j << 1))) >> (j << 1));
for (j = 0; j < MAX_MON_MOVES; j++)
{
moveSlots[j] = RecordedBattle_GetBattlerAction(RECORDED_BYTE, battlerId);
movePp.moves[j] = gBattleMons[battlerId].moves[moveSlots[j]];
movePp.currentPp[j] = gBattleMons[battlerId].pp[moveSlots[j]];
movePp.maxPp[j] = ppBonuses[moveSlots[j]];
mimickedMoveSlots[j] = (gDisableStructs[battlerId].mimickedMoves & (1u << j)) >> j;
}
for (j = 0; j < MAX_MON_MOVES; j++)
{
gBattleMons[battlerId].moves[j] = movePp.moves[j];
gBattleMons[battlerId].pp[j] = movePp.currentPp[j];
}
gBattleMons[battlerId].ppBonuses = 0;
gDisableStructs[battlerId].mimickedMoves = 0;
for (j = 0; j < MAX_MON_MOVES; j++)
{
gBattleMons[battlerId].ppBonuses |= movePp.maxPp[j] << (j << 1);
gDisableStructs[battlerId].mimickedMoves |= mimickedMoveSlots[j] << j;
}
if (!(gBattleMons[battlerId].status2 & STATUS2_TRANSFORMED))
{
for (j = 0; j < MAX_MON_MOVES; j++)
ppBonuses[j] = (GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP_BONUSES, NULL) & ((3 << (j << 1)))) >> (j << 1);
for (j = 0; j < MAX_MON_MOVES; j++)
{
movePp.moves[j] = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_MOVE1 + moveSlots[j], NULL);
movePp.currentPp[j] = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP1 + moveSlots[j], NULL);
movePp.maxPp[j] = ppBonuses[moveSlots[j]];
}
for (j = 0; j < MAX_MON_MOVES; j++)
{
SetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_MOVE1 + j, &movePp.moves[j]);
SetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP1 + j, &movePp.currentPp[j]);
}
ppBonusSet = 0;
for (j = 0; j < MAX_MON_MOVES; j++)
ppBonusSet |= movePp.maxPp[j] << (j << 1);
SetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP_BONUSES, &ppBonusSet);
}
gChosenMoveByBattler[battlerId] = gBattleMons[battlerId].moves[gBattleStruct->chosenMovePositions[battlerId]];
}
}
}
}
}
u32 GetAiScriptsInRecordedBattle(void)
{
return sAI_Scripts;
}
// Used to determine when the player is allowed to press B to end a recorded battle's playback
void RecordedBattle_SetPlaybackFinished(void)
{
sIsPlaybackFinished = TRUE;
}
bool8 RecordedBattle_CanStopPlayback(void)
{
return (sIsPlaybackFinished == FALSE);
}
void GetRecordedBattleRecordMixFriendName(u8 *dst)
{
s32 i;
for (i = 0; i < PLAYER_NAME_LENGTH + 1; i++)
dst[i] = sRecordMixFriendName[i];
dst[PLAYER_NAME_LENGTH] = EOS;
ConvertInternationalString(dst, sRecordMixFriendLanguage);
}
u8 GetRecordedBattleRecordMixFriendClass(void)
{
return sRecordMixFriendClass;
}
u8 GetRecordedBattleApprenticeId(void)
{
return sApprenticeId;
}
u8 GetRecordedBattleRecordMixFriendLanguage(void)
{
return sRecordMixFriendLanguage;
}
u8 GetRecordedBattleApprenticeLanguage(void)
{
return sApprenticeLanguage;
}
void RecordedBattle_SaveBattleOutcome(void)
{
sBattleOutcome = gBattleOutcome;
}
u16 *GetRecordedBattleEasyChatSpeech(void)
{
return sEasyChatSpeech;
}