forked from pret/pokefirered
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattle_tower.c
1425 lines (1240 loc) · 44.3 KB
/
battle_tower.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_tower.h"
#include "random.h"
#include "text.h"
#include "event_data.h"
#include "string_util.h"
#include "battle_message.h"
#include "data.h"
#include "pokedex.h"
#include "overworld.h"
#include "battle_transition.h"
#include "easy_chat.h"
#include "battle_setup.h"
#include "battle_main.h"
#include "task.h"
#include "battle.h"
#include "party_menu.h"
#include "new_game.h"
#include "save.h"
#include "item.h"
#include "script_pokemon_util.h"
#include "constants/items.h"
#include "constants/moves.h"
#include "constants/pokemon.h"
#include "constants/event_objects.h"
static EWRAM_DATA u16 sSpecialVar_0x8004_Copy = 0;
#define TakeBravoTrainerBattleTowerOffTheAir()
#define ewram160FB (*((u8 *)&gBattleStruct->field_DA))
static void ResetBattleTowerStreak(u8 levelType);
static void SetBattleTowerTrainerGfxId(u8 trainerClass);
static void CheckMonBattleTowerBanlist(u16 species, u16 heldItem, u16 hp, u8 battleTowerLevelType, u8 monLevel, u16 *validPartySpecies, u16 *validPartyHeldItems, u8 *numValid);
static void SaveCurrentWinStreak(void);
static void ValidateBattleTowerRecordChecksums(void);
static void SetBattleTowerRecordChecksum(struct BattleTowerRecord * record);
static void ClearBattleTowerRecord(struct BattleTowerRecord * record);
static void PopulateBravoTrainerBattleTowerLostData(void);
static u16 GetCurrentBattleTowerWinStreak(u8 levelType);
static void SetEReaderTrainerChecksum(struct BattleTowerEReaderTrainer * eReaderTrainer);
static void PrintEReaderTrainerFarewellMessage(void);
// unknown unused data
static const u8 sUnused[] = {
5, 4, 1, 16, 4, 2, 5, 6,
3, 16, 6, 4, 0, 1, 2, 3,
0, 2, 3
};
static const u8 sText_100[] = _("100");
static const struct BattleTowerTrainer sBattleTowerTrainers[] =
{
};
static const u16 sBattleTowerHeldItems[] = {
ITEM_NONE,
ITEM_KINGS_ROCK,
ITEM_SITRUS_BERRY,
ITEM_ORAN_BERRY,
ITEM_CHESTO_BERRY,
ITEM_HARD_STONE,
ITEM_FOCUS_BAND,
ITEM_PERSIM_BERRY,
ITEM_MIRACLE_SEED,
ITEM_BERRY_JUICE,
ITEM_MACHO_BRACE,
ITEM_SILVER_POWDER,
ITEM_CHERI_BERRY,
ITEM_BLACK_GLASSES,
ITEM_BLACK_BELT,
ITEM_SOUL_DEW,
ITEM_CHOICE_BAND,
ITEM_MAGNET,
ITEM_SILK_SCARF,
ITEM_WHITE_HERB,
ITEM_DEEP_SEA_SCALE,
ITEM_DEEP_SEA_TOOTH,
ITEM_MYSTIC_WATER,
ITEM_SHARP_BEAK,
ITEM_QUICK_CLAW,
ITEM_LEFTOVERS,
ITEM_RAWST_BERRY,
ITEM_LIGHT_BALL,
ITEM_POISON_BARB,
ITEM_NEVER_MELT_ICE,
ITEM_ASPEAR_BERRY,
ITEM_SPELL_TAG,
ITEM_BRIGHT_POWDER,
ITEM_LEPPA_BERRY,
ITEM_SCOPE_LENS,
ITEM_TWISTED_SPOON,
ITEM_METAL_COAT,
ITEM_MENTAL_HERB,
ITEM_CHARCOAL,
ITEM_PECHA_BERRY,
ITEM_SOFT_SAND,
ITEM_LUM_BERRY,
ITEM_DRAGON_SCALE,
ITEM_DRAGON_FANG,
ITEM_IAPAPA_BERRY,
ITEM_WIKI_BERRY,
ITEM_SEA_INCENSE,
ITEM_SHELL_BELL,
ITEM_SALAC_BERRY,
ITEM_LANSAT_BERRY,
ITEM_APICOT_BERRY,
ITEM_STARF_BERRY,
ITEM_LIECHI_BERRY,
ITEM_STICK,
ITEM_LAX_INCENSE,
ITEM_AGUAV_BERRY,
ITEM_FIGY_BERRY,
ITEM_THICK_CLUB,
ITEM_MAGO_BERRY,
ITEM_METAL_POWDER,
ITEM_PETAYA_BERRY,
ITEM_LUCKY_PUNCH,
ITEM_GANLON_BERRY
};
#include "data/battle_tower/level_50_mons.h"
#include "data/battle_tower/level_100_mons.h"
static const u8 sMaleTrainerClasses[] =
{
};
static const u8 sFemaleTrainerClasses[] =
{
};
static const u8 sMaleTrainerGfx[] =
{
};
static const u8 sFemaleTrainerGfx[] =
{
};
const u16 gBattleTowerBannedSpecies[] = {
SPECIES_MEW,
SPECIES_MEWTWO,
SPECIES_HO_OH,
SPECIES_LUGIA,
SPECIES_CELEBI,
SPECIES_KYOGRE,
SPECIES_GROUDON,
SPECIES_RAYQUAZA,
SPECIES_JIRACHI,
SPECIES_DEOXYS,
0xFFFF
};
static const u16 sShortStreakPrizes[] = {
ITEM_HP_UP,
ITEM_PROTEIN,
ITEM_IRON,
ITEM_CALCIUM,
ITEM_CARBOS,
ITEM_ZINC
};
static const u16 sLongStreakPrizes[] = {
ITEM_BRIGHT_POWDER,
ITEM_WHITE_HERB,
ITEM_QUICK_CLAW,
ITEM_LEFTOVERS,
ITEM_MENTAL_HERB,
ITEM_KINGS_ROCK,
ITEM_FOCUS_BAND,
ITEM_SCOPE_LENS,
ITEM_CHOICE_BAND
};
void BattleTowerMapScript2(void)
{
u8 count = 0;
s32 levelType;
for (levelType = 0; levelType < 2; levelType++)
{
switch (gSaveBlock2Ptr->battleTower.var_4AE[levelType])
{
default:
case 0:
ResetBattleTowerStreak(levelType);
if (count == 0)
VarSet(VAR_TEMP_0, 5);
break;
case 1:
ResetBattleTowerStreak(levelType);
VarSet(VAR_TEMP_0, BTSPECIAL_RESULT_SAVE_SCUM);
count++;
break;
case 3:
break;
case 4:
VarSet(VAR_TEMP_0, BTSPECIAL_RESULT_WON7);
count++;
break;
case 5:
VarSet(VAR_TEMP_0, BTSPECIAL_RESULT_LOST);
count++;
break;
case 6:
break;
case 2:
VarSet(VAR_TEMP_0, BTSPECIAL_RESULT_QUICKSAVE);
count++;
break;
}
}
if (gSaveBlock2Ptr->battleTower.var_4AE[0] == 3 && gSaveBlock2Ptr->battleTower.var_4AE[1] == 3)
VarSet(VAR_TEMP_0, BTSPECIAL_RESULT_INACTIVE);
ValidateBattleTowerRecordChecksums();
}
static void ResetBattleTowerStreak(u8 levelType)
{
gSaveBlock2Ptr->battleTower.var_4AE[levelType] = 0;
gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] = 1;
gSaveBlock2Ptr->battleTower.curStreakChallengesNum[levelType] = 1;
}
static bool8 ShouldBattleEReaderTrainer(u8 levelType, u16 winStreak)
{
u8 trainerTeamLevel;
u8 monLevel;
s32 i;
u16 validPartySpecies[6];
u16 validPartyHeldItems[6];
u8 numValid;
numValid = 0;
ValidateEReaderTrainer();
if (gSpecialVar_Result != 0 || gSaveBlock2Ptr->battleTower.ereaderTrainer.winStreak != winStreak)
return FALSE;
if (levelType != 0)
trainerTeamLevel = 100;
else
trainerTeamLevel = 50;
for (i = 0; i < 3; i++)
{
monLevel = gSaveBlock2Ptr->battleTower.ereaderTrainer.party[i].level;
if (gSaveBlock2Ptr->battleTower.ereaderTrainer.party[i].level != trainerTeamLevel)
return FALSE;
CheckMonBattleTowerBanlist(
gSaveBlock2Ptr->battleTower.ereaderTrainer.party[i].species,
gSaveBlock2Ptr->battleTower.ereaderTrainer.party[i].heldItem,
1,
levelType,
monLevel,
validPartySpecies,
validPartyHeldItems,
&numValid);
}
return (numValid == 3);
}
static bool8 ChooseSpecialBattleTowerTrainer(void)
{
s32 recordIndex, i;
u8 battleTowerLevelType;
u16 winStreak;
bool8 retVal;
s32 numCandidates;
u32 trainerIds[5];
numCandidates = 0;
battleTowerLevelType = gSaveBlock2Ptr->battleTower.battleTowerLevelType;
winStreak = GetCurrentBattleTowerWinStreak(battleTowerLevelType);
if (ShouldBattleEReaderTrainer(battleTowerLevelType, winStreak))
{
gSaveBlock2Ptr->battleTower.battleTowerTrainerId = BATTLE_TOWER_EREADER_TRAINER_ID;
retVal = TRUE;
}
else
{
// Check if one of the battle tower trainers from record mixing should be the next trainer.
for (recordIndex = 0; recordIndex < 5; recordIndex++)
{
struct BattleTowerRecord *record = &gSaveBlock2Ptr->battleTower.records[recordIndex];
u32 recordHasData = 0;
u32 checksum = 0;
for (i = 0; i < sizeof(struct BattleTowerRecord) / sizeof(u32) - 1; i++)
{
recordHasData |= ((u32 *) record)[i];
checksum += ((u32 *) record)[i];
}
if (gSaveBlock2Ptr->battleTower.records[recordIndex].winStreak == winStreak
&& gSaveBlock2Ptr->battleTower.records[recordIndex].battleTowerLevelType == battleTowerLevelType
&& recordHasData
&& gSaveBlock2Ptr->battleTower.records[recordIndex].checksum == checksum)
{
trainerIds[numCandidates] = recordIndex;
numCandidates++;
}
}
if (numCandidates == 0)
{
retVal = FALSE;
}
else
{
gSaveBlock2Ptr->battleTower.battleTowerTrainerId =
trainerIds[Random() % numCandidates] + BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID;
retVal = TRUE;
}
}
return retVal;
}
void ChooseNextBattleTowerTrainer(void)
{
int i;
u16 trainerId;
bool8 levelType;
levelType = gSaveBlock2Ptr->battleTower.battleTowerLevelType;
if (ChooseSpecialBattleTowerTrainer())
{
SetBattleTowerTrainerGfxId(gSaveBlock2Ptr->battleTower.battleTowerTrainerId);
gSaveBlock2Ptr->battleTower.battledTrainerIds[gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] - 1] = gSaveBlock2Ptr->battleTower.battleTowerTrainerId;
return;
}
if (gSaveBlock2Ptr->battleTower.curStreakChallengesNum[levelType] <= 7)
{
if (gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] == 7)
{
do
{
trainerId = ((Random() & 0xFF) * 5) >> 7;
trainerId += (gSaveBlock2Ptr->battleTower.curStreakChallengesNum[levelType] - 1) * 10 + 20;
// Ensure trainer wasn't previously fought in this challenge.
for (i = 0; i < gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] - 1 && gSaveBlock2Ptr->battleTower.battledTrainerIds[i] != trainerId; i++);
}
while (i != gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] - 1);
gSaveBlock2Ptr->battleTower.battleTowerTrainerId = trainerId;
}
else
{
do
{
trainerId = (((Random() & 0xFF) * 5) >> 6);
trainerId += (gSaveBlock2Ptr->battleTower.curStreakChallengesNum[levelType] - 1) * 10;
// Ensure trainer wasn't previously fought in this challenge.
for (i = 0; i < gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] - 1 && gSaveBlock2Ptr->battleTower.battledTrainerIds[i] != trainerId; i++);
}
while (i != gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] - 1);
gSaveBlock2Ptr->battleTower.battleTowerTrainerId = trainerId;
}
}
else
{
do
{
trainerId = (((Random() & 0xFF) * 30) >> 8) + 70;
// Ensure trainer wasn't previously fought in this challenge.
for (i = 0; i < gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] - 1 && gSaveBlock2Ptr->battleTower.battledTrainerIds[i] != trainerId; i++);
}
while (i != gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] - 1);
gSaveBlock2Ptr->battleTower.battleTowerTrainerId = trainerId;
}
SetBattleTowerTrainerGfxId(gSaveBlock2Ptr->battleTower.battleTowerTrainerId);
// Don't bother saving this trainer, since it's the last one in the challenge.
if (gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] < 7)
gSaveBlock2Ptr->battleTower.battledTrainerIds[gSaveBlock2Ptr->battleTower.curChallengeBattleNum[levelType] - 1] = gSaveBlock2Ptr->battleTower.battleTowerTrainerId;
}
static void SetBattleTowerTrainerGfxId(u8 trainerClass)
{
VarSet(VAR_OBJ_GFX_ID_0, OBJ_EVENT_GFX_YOUNGSTER);
}
void SetEReaderTrainerGfxId(void)
{
SetBattleTowerTrainerGfxId(BATTLE_TOWER_EREADER_TRAINER_ID);
}
// Unreferenced; formerly used by Record Mixing in R/S
static void UpdateOrInsertReceivedBattleTowerRecord(struct BattleTowerRecord * record0)
{
s32 i;
s32 j;
s32 k;
s32 l = 0;
struct BattleTowerRecord * record = record0;
u16 winStreaks[6];
u16 indices[6];
for (i = 0; i < 5; i++)
{
k = 0;
for (j = 0; j < 4; j++)
{
if (gSaveBlock2Ptr->battleTower.records[i].trainerId[j] != record->trainerId[j])
break;
}
if (j == 4)
{
for (k = 0; k < 7; k++)
{
if (gSaveBlock2Ptr->battleTower.records[i].name[j] != record->name[j])
break;
if (record->name[j] == EOS)
{
k = 7;
break;
}
}
}
if (k == 7)
break;
}
if (i < 5)
{
gSaveBlock2Ptr->battleTower.records[i] = *record;
return;
}
for (i = 0; i < 5; i++)
{
if (gSaveBlock2Ptr->battleTower.records[i].winStreak == 0)
break;
}
if (i < 5)
{
gSaveBlock2Ptr->battleTower.records[i] = *record;
return;
}
winStreaks[0] = gSaveBlock2Ptr->battleTower.records[0].winStreak;
indices[0] = 0;
l++;
for (i = 1; i < 5; i++)
{
for (j = 0; j < l; j++)
{
if (gSaveBlock2Ptr->battleTower.records[i].winStreak < winStreaks[j])
{
j = 0;
l = 1;
winStreaks[0] = gSaveBlock2Ptr->battleTower.records[i].winStreak;
indices[0] = i;
break;
}
if (gSaveBlock2Ptr->battleTower.records[i].winStreak > winStreaks[j])
break;
}
if (j == l)
{
winStreaks[l] = gSaveBlock2Ptr->battleTower.records[i].winStreak;
indices[l] = i;
l++;
}
}
i = Random() % l;
gSaveBlock2Ptr->battleTower.records[indices[i]] = *record;
}
u8 GetBattleTowerTrainerFrontSpriteId(void)
{
if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId == BATTLE_TOWER_EREADER_TRAINER_ID)
{
return gFacilityClassToPicIndex[gSaveBlock2Ptr->battleTower.ereaderTrainer.trainerClass];
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID)
{
return gFacilityClassToPicIndex[sBattleTowerTrainers[gSaveBlock2Ptr->battleTower.battleTowerTrainerId].trainerClass];
}
else
{
return gFacilityClassToPicIndex[gSaveBlock2Ptr->battleTower.records[gSaveBlock2Ptr->battleTower.battleTowerTrainerId - BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID].trainerClass];
}
}
u8 GetBattleTowerTrainerClassNameId(void)
{
if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId == BATTLE_TOWER_EREADER_TRAINER_ID)
{
return gFacilityClassToTrainerClass[gSaveBlock2Ptr->battleTower.ereaderTrainer.trainerClass];
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId >= BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID)
{
return gFacilityClassToTrainerClass[gSaveBlock2Ptr->battleTower.records[gSaveBlock2Ptr->battleTower.battleTowerTrainerId - BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID].trainerClass];
}
else
{
return gFacilityClassToTrainerClass[sBattleTowerTrainers[gSaveBlock2Ptr->battleTower.battleTowerTrainerId].trainerClass];
}
}
void GetBattleTowerTrainerName(u8 *dest)
{
s32 i;
if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId == BATTLE_TOWER_EREADER_TRAINER_ID)
{
for (i = 0; i < 7; i++)
dest[i] = gSaveBlock2Ptr->battleTower.ereaderTrainer.name[i];
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID)
{
for (i = 0; i < 3; i++)
dest[i] = sBattleTowerTrainers[gSaveBlock2Ptr->battleTower.battleTowerTrainerId].name[i];
}
else
{
for (i = 0; i < 7; i++)
dest[i] = gSaveBlock2Ptr->battleTower.records[gSaveBlock2Ptr->battleTower.battleTowerTrainerId - BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID].name[i];
}
dest[i] = EOS;
}
static void FillBattleTowerTrainerParty(void)
{
s32 partyIndex;
s32 i;
u16 chosenMonIndices[3];
u8 friendship;
u8 level;
u8 fixedIV;
u8 battleMonsOffset;
u8 monPoolSize;
u8 teamFlags;
const struct BattleTowerPokemonTemplate *battleTowerMons;
battleMonsOffset = 0;
monPoolSize = 60;
friendship = 255;
ZeroEnemyPartyMons();
// Different trainers have access to different sets of pokemon to use in battle.
// The pokemon later in gBattleTowerLevel100Mons or gBattleTowerLevel50Mons are
// stronger. Additionally, the later trainers' pokemon are granted higher IVs.
if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < 20)
{
fixedIV = 6;
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < 30)
{
fixedIV = 9;
battleMonsOffset = 30;
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < 40)
{
fixedIV = 12;
battleMonsOffset = 60;
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < 50)
{
fixedIV = 15;
battleMonsOffset = 90;
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < 60)
{
fixedIV = 18;
battleMonsOffset = 120;
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < 70)
{
fixedIV = 21;
battleMonsOffset = 150;
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < 80)
{
fixedIV = 31;
battleMonsOffset = 180;
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID)
{
fixedIV = 31;
battleMonsOffset = 200;
monPoolSize = 100;
}
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId == BATTLE_TOWER_EREADER_TRAINER_ID)
{
// Load E-Reader trainer's party.
do
{
for (partyIndex = 0; partyIndex < 3; partyIndex++)
CreateBattleTowerMon(&gEnemyParty[partyIndex], &gSaveBlock2Ptr->battleTower.ereaderTrainer.party[partyIndex]);
return;
} while (0);
}
else
{
// Load a battle tower record's party. (From record mixing)
for (partyIndex = 0; partyIndex < 3; partyIndex++)
{
CreateBattleTowerMon(
&gEnemyParty[partyIndex],
&gSaveBlock2Ptr->battleTower.records[gSaveBlock2Ptr->battleTower.battleTowerTrainerId - BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID].party[partyIndex]);
}
return;
}
// Use the appropriate list of pokemon and level depending on the
// current challenge type. (level 50 or level 100 challenge)
if (gSaveBlock2Ptr->battleTower.battleTowerLevelType != 0)
{
battleTowerMons = gBattleTowerLevel100Mons;
level = 100;
}
else
{
battleTowerMons = gBattleTowerLevel50Mons;
level = 50;
}
teamFlags = sBattleTowerTrainers[gSaveBlock2Ptr->battleTower.battleTowerTrainerId].teamFlags;
// Attempt to fill the trainer's party with random Pokemon until 3 have been
// successfully chosen. The trainer's party may not have duplicate pokemon species
// or duplicate held items. Each pokemon must have all of the trainer's team flags
// set, as well. If any of those conditions are not met, then the loop starts over
// and another pokemon is chosen at random.
partyIndex = 0;
while (partyIndex != 3)
{
// Pick a random pokemon index based on the number of pokemon available to choose from
// and the starting offset in the battle tower pokemon array.
s32 battleMonIndex = ((Random() & 0xFF) * monPoolSize) / 256 + battleMonsOffset;
// Ensure the chosen pokemon has compatible team flags with the trainer.
if (teamFlags == 0 || (battleTowerMons[battleMonIndex].teamFlags & teamFlags) == teamFlags)
{
// Ensure this pokemon species isn't a duplicate.
for (i = 0; i < partyIndex; i++)
{
if (GetMonData(&gEnemyParty[i], MON_DATA_SPECIES, NULL) == battleTowerMons[battleMonIndex].species)
break;
}
if (i != partyIndex)
continue;
// Ensure this pokemon's held item isn't a duplicate.
for (i = 0; i < partyIndex; i++)
{
if (GetMonData(&gEnemyParty[i], MON_DATA_HELD_ITEM, NULL) != ITEM_NONE
&& GetMonData(&gEnemyParty[i], MON_DATA_HELD_ITEM, NULL) == sBattleTowerHeldItems[battleTowerMons[battleMonIndex].heldItem])
break;
}
if (i != partyIndex)
continue;
// Ensure this exact pokemon index isn't a duplicate. This check doesn't seem necessary
// because the species and held items were already checked directly above. Perhaps this
// is leftover code before the logic for duplicate species and held items was added.
//for (i = 0; i < partyIndex && chosenMonIndices[i] != battleMonIndex; i++);
for (i = 0; i < partyIndex; i++)
{
if (chosenMonIndices[i] == battleMonIndex)
break;
}
if (i != partyIndex)
continue;
chosenMonIndices[partyIndex] = battleMonIndex;
// Place the chosen pokemon into the trainer's party.
CreateMonWithEVSpread(
&gEnemyParty[partyIndex],
battleTowerMons[battleMonIndex].species,
level,
fixedIV,
battleTowerMons[battleMonIndex].evSpread);
// Give the chosen pokemon its specified moves.
for (i = 0; i < 4; i++)
{
SetMonMoveSlot(&gEnemyParty[partyIndex], battleTowerMons[battleMonIndex].moves[i], i);
if (battleTowerMons[battleMonIndex].moves[i] == MOVE_FRUSTRATION)
friendship = 0; // MOVE_FRUSTRATION is more powerful the lower the pokemon's friendship is.
}
SetMonData(&gEnemyParty[partyIndex], MON_DATA_FRIENDSHIP, &friendship);
SetMonData(&gEnemyParty[partyIndex], MON_DATA_HELD_ITEM, &sBattleTowerHeldItems[battleTowerMons[battleMonIndex].heldItem]);
// The pokemon was successfully added to the trainer's party, so it's safe to move on to
// the next party slot.
partyIndex++;
}
}
}
static u8 AppendBattleTowerBannedSpeciesName(u16 species, u8 count)
{
if (GetSetPokedexFlag(SpeciesToNationalPokedexNum(species), FLAG_GET_CAUGHT))
{
if (count == 0)
StringAppend(gStringVar1, gText_BattleTowerBan_Space);
count++;
StringAppend(gStringVar1, gSpeciesNames[species]);
switch (count)
{
case 2:
StringAppend(gStringVar1, gText_BattleTowerBan_Newline2);
break;
case 5:
case 8:
case 11:
StringAppend(gStringVar1, gText_BattleTowerBan_Newline1);
break;
default:
StringAppend(gStringVar1, gText_BattleTowerBan_Space);
break;
}
}
return count;
}
static void CheckMonBattleTowerBanlist(u16 species, u16 heldItem, u16 hp, u8 battleTowerLevelType, u8 monLevel, u16 *validPartySpecies, u16 *validPartyHeldItems, u8 *numValid)
{
s32 i;
u32 counter = 0;
if (species == SPECIES_EGG || species == SPECIES_NONE)
return;
while (1)
{
if (gBattleTowerBannedSpecies[counter] == 0xFFFF)
break;
if (gBattleTowerBannedSpecies[counter] == species)
break;
counter++;
}
if (gBattleTowerBannedSpecies[counter] != 0xFFFF)
return;
if (battleTowerLevelType == 0 && monLevel > 50)
return;
for (i = 0; i < *numValid && validPartySpecies[i] != species ; i++);
if (i != *numValid)
return;
if (heldItem != ITEM_NONE)
{
for (i = 0; i < *numValid && validPartyHeldItems[i] != heldItem ; i++);
if (i != *numValid)
return;
}
validPartySpecies[*numValid] = species;
validPartyHeldItems[*numValid] = heldItem;
*numValid = *numValid + 1;
}
void CheckPartyBattleTowerBanlist(void)
{
s32 i;
u16 species2;
u16 heldItem;
u8 level;
u16 hp;
u32 numBanlistCaught;
u16 validPartySpecies[6];
u16 validPartyHeldItems[6];
u8 counter;
counter = 0;
for (i = 0; i < PARTY_SIZE; i++)
{
species2 = GetMonData(&gPlayerParty[i], MON_DATA_SPECIES_OR_EGG);
heldItem = GetMonData(&gPlayerParty[i], MON_DATA_HELD_ITEM);
level = GetMonData(&gPlayerParty[i], MON_DATA_LEVEL);
hp = GetMonData(&gPlayerParty[i], MON_DATA_HP);
CheckMonBattleTowerBanlist(species2, heldItem, hp, gSpecialVar_Result, level, validPartySpecies, validPartyHeldItems, &counter);
}
if (counter < 3)
{
gStringVar1[0] = EOS;
gSpecialVar_0x8004 = 1;
counter = 0;
for (i = 0; gBattleTowerBannedSpecies[i] != 0xFFFF; i++)
counter = AppendBattleTowerBannedSpeciesName(gBattleTowerBannedSpecies[i], counter);
i = StringLength(gStringVar1);
gStringVar1[i - 1] = EOS;
if (counter < 3)
StringAppend(gStringVar1, gText_BattleTowerBan_Is1);
else
StringAppend(gStringVar1, gText_BattleTowerBan_Is2);
}
else
{
gSpecialVar_0x8004 = 0;
gSaveBlock2Ptr->battleTower.battleTowerLevelType = gSpecialVar_Result;
}
}
static void BufferBattleTowerTrainerMessage(const u16 *greeting)
{
s32 i;
if (EC_DoesEasyChatStringFitOnLine(greeting, 3, 2, 18))
{
ConvertEasyChatWordsToString(gStringVar4, greeting, 2, 3);
i = 0;
while (gStringVar4[i++] != CHAR_NEWLINE)
;
while (gStringVar4[i] != CHAR_NEWLINE)
i++;
gStringVar4[i] = CHAR_PROMPT_SCROLL;
}
else
{
ConvertEasyChatWordsToString(gStringVar4, greeting, 3, 2);
}
}
void PrintBattleTowerTrainerGreeting(void)
{
if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId == BATTLE_TOWER_EREADER_TRAINER_ID)
BufferBattleTowerTrainerMessage(gSaveBlock2Ptr->battleTower.ereaderTrainer.greeting);
else if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId < BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID)
BufferBattleTowerTrainerMessage(sBattleTowerTrainers[gSaveBlock2Ptr->battleTower.battleTowerTrainerId].greeting);
else
BufferBattleTowerTrainerMessage(gSaveBlock2Ptr->battleTower.records[gSaveBlock2Ptr->battleTower.battleTowerTrainerId - BATTLE_TOWER_RECORD_MIXING_TRAINER_BASE_ID].greeting);
}
static void CB2_FinishEReaderBattle(void)
{
s32 i;
u16 heldItem;
switch (sSpecialVar_0x8004_Copy)
{
case 0:
break;
case 1:
for (i = 0; i < PARTY_SIZE; i++)
{
heldItem = GetMonData(&gSaveBlock1Ptr->playerParty[i], MON_DATA_HELD_ITEM);
SetMonData(&gPlayerParty[i], MON_DATA_HELD_ITEM, &heldItem);
}
break;
case 2:
PrintEReaderTrainerFarewellMessage();
break;
}
SetMainCallback2(CB2_ReturnToFieldContinueScriptPlayMapMusic);
}
static void Task_WaitBT(u8 taskId)
{
if (IsBattleTransitionDone() == TRUE)
{
gMain.savedCallback = CB2_FinishEReaderBattle;
CleanupOverworldWindowsAndTilemaps();
SetMainCallback2(CB2_InitBattle);
DestroyTask(taskId);
}
}
void StartSpecialBattle(void)
{
s32 i;
u16 heldItem;
u8 transition;
sSpecialVar_0x8004_Copy = gSpecialVar_0x8004;
switch (sSpecialVar_0x8004_Copy)
{
case 0: // battle tower battle
gBattleTypeFlags = (BATTLE_TYPE_BATTLE_TOWER | BATTLE_TYPE_TRAINER);
gTrainerBattleOpponent_A = 0;
FillBattleTowerTrainerParty();
CreateTask(Task_WaitBT, 1);
PlayMapChosenOrBattleBGM(0);
transition = BattleSetup_GetBattleTowerBattleTransition();
BattleTransition_StartOnField(transition);
break;
case 1: // secret base battle
for (i = 0; i < PARTY_SIZE; i++)
{
heldItem = GetMonData(&gPlayerParty[i], MON_DATA_HELD_ITEM);
SetMonData(&gSaveBlock1Ptr->playerParty[i], MON_DATA_HELD_ITEM, &heldItem);
}
CreateTask(Task_WaitBT, 1);
PlayMapChosenOrBattleBGM(0);
transition = BattleSetup_GetBattleTowerBattleTransition();
BattleTransition_StartOnField(transition);
break;
case 2: // e-reader trainer battle
ZeroEnemyPartyMons();
for (i = 0; i < 3; i++)
CreateBattleTowerMon(&gEnemyParty[i], &gSaveBlock2Ptr->battleTower.ereaderTrainer.party[i]);
gBattleTypeFlags = (BATTLE_TYPE_EREADER_TRAINER | BATTLE_TYPE_TRAINER);
gTrainerBattleOpponent_A = 0;
CreateTask(Task_WaitBT, 1);
PlayMapChosenOrBattleBGM(0);
transition = BattleSetup_GetBattleTowerBattleTransition();
BattleTransition_StartOnField(transition);
break;
}
}
void SetBattleTowerProperty(void)
{
s32 i;
u8 battleTowerLevelType = gSaveBlock2Ptr->battleTower.battleTowerLevelType;
switch (gSpecialVar_0x8004)
{
case 0:
ewram160FB = gSaveBlock2Ptr->battleTower.var_4AE[battleTowerLevelType];
gSaveBlock2Ptr->battleTower.var_4AE[battleTowerLevelType] = gSpecialVar_0x8005;
break;
case 1:
gSaveBlock2Ptr->battleTower.battleTowerLevelType = gSpecialVar_0x8005;
break;
case 2:
gSaveBlock2Ptr->battleTower.curChallengeBattleNum[battleTowerLevelType] = gSpecialVar_0x8005;
break;
case 3:
gSaveBlock2Ptr->battleTower.curStreakChallengesNum[battleTowerLevelType] = gSpecialVar_0x8005;
break;
case 4:
gSaveBlock2Ptr->battleTower.battleTowerTrainerId = gSpecialVar_0x8005;
break;
case 5:
for (i = 0; i < 3; i++)
gSaveBlock2Ptr->battleTower.selectedPartyMons[i] = gSelectedOrderFromParty[i];
break;
case 6:
if (gSaveBlock2Ptr->battleTower.battleTowerTrainerId == BATTLE_TOWER_EREADER_TRAINER_ID)
ClearEReaderTrainer(&gSaveBlock2Ptr->battleTower.ereaderTrainer);
if (gSaveBlock2Ptr->battleTower.totalBattleTowerWins < 9999)
gSaveBlock2Ptr->battleTower.totalBattleTowerWins++;
gSaveBlock2Ptr->battleTower.curChallengeBattleNum[battleTowerLevelType]++;
SaveCurrentWinStreak();
gSpecialVar_Result = gSaveBlock2Ptr->battleTower.curChallengeBattleNum[battleTowerLevelType];
gStringVar1[0] = gSaveBlock2Ptr->battleTower.curChallengeBattleNum[battleTowerLevelType] + 0xA1;
gStringVar1[1] = 0xFF;
break;
case 7:
if (gSaveBlock2Ptr->battleTower.curStreakChallengesNum[battleTowerLevelType] < 1430)
gSaveBlock2Ptr->battleTower.curStreakChallengesNum[battleTowerLevelType]++;
SaveCurrentWinStreak();
gSpecialVar_Result = gSaveBlock2Ptr->battleTower.curStreakChallengesNum[battleTowerLevelType];
break;
case 8:
gSaveBlock2Ptr->battleTower.unk_554 = gSpecialVar_0x8005;
break;
case 9:
break;
case 10:
SetGameStat(GAME_STAT_BATTLE_TOWER_BEST_STREAK, gSaveBlock2Ptr->battleTower.bestBattleTowerWinStreak);
break;
case 11:
if (gSaveBlock2Ptr->battleTower.var_4AE[battleTowerLevelType] != 3)
ResetBattleTowerStreak(battleTowerLevelType);
break;
case 12: