From f46e941f306a3f9b6a527e33f3657657da0436cb Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Mon, 26 Feb 2018 08:23:17 -0500 Subject: Name functions, variables, and that one flag constant --- src/battle_setup.c | 6 +-- src/gym_leader_rematch.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++ src/trainer_rematch.c | 105 ---------------------------------------------- 3 files changed, 109 insertions(+), 108 deletions(-) create mode 100644 src/gym_leader_rematch.c delete mode 100644 src/trainer_rematch.c (limited to 'src') diff --git a/src/battle_setup.c b/src/battle_setup.c index fcc0b2e0e..fa1eb7edc 100644 --- a/src/battle_setup.c +++ b/src/battle_setup.c @@ -31,6 +31,7 @@ #include "string_util.h" #include "overworld.h" #include "field_weather.h" +#include "gym_leader_rematch.h" enum { @@ -66,7 +67,6 @@ extern void Overworld_ClearSavedMusic(void); extern void CB2_WhiteOut(void); extern void sub_80AF6F0(void); extern void PlayBattleBGM(void); -extern void sub_81DA57C(void); extern u8 Overworld_GetFlashLevel(void); extern u16 sub_81A9AA8(u8 localId); extern u16 sub_81D6180(u8 localId); @@ -945,13 +945,13 @@ static void CB2_EndFirstBattle(void) static void sub_80B1218(void) { if (GetGameStat(GAME_STAT_WILD_BATTLES) % 60 == 0) - sub_81DA57C(); + UpdateGymLeaderRematch(); } static void sub_80B1234(void) { if (GetGameStat(GAME_STAT_TRAINER_BATTLES) % 20 == 0) - sub_81DA57C(); + UpdateGymLeaderRematch(); } // why not just use the macros? maybe its because they didnt want to uncast const every time? diff --git a/src/gym_leader_rematch.c b/src/gym_leader_rematch.c new file mode 100644 index 000000000..355ae5534 --- /dev/null +++ b/src/gym_leader_rematch.c @@ -0,0 +1,106 @@ +#include "global.h" +#include "constants/flags.h" +#include "random.h" +#include "event_data.h" +#include "battle_setup.h" +#include "gym_leader_rematch.h" + +static void UpdateGymLeaderRematchFromArray(const u16 *data, size_t size, u32 maxRematch); +static s32 GetRematchIndex(u32 trainerIdx); + +static const u16 GymLeaderRematches_AfterNewMauville[] = { + REMATCH_ROXANNE, + REMATCH_BRAWLY, + REMATCH_WATTSON, + REMATCH_FLANNERY, + REMATCH_NORMAN, + REMATCH_WINONA, + REMATCH_TATE_AND_LIZA, + REMATCH_JUAN +}; + +static const u16 GymLeaderRematches_BeforeNewMauville[] = { + REMATCH_ROXANNE, + REMATCH_BRAWLY, + // Wattson isn't available at this time + REMATCH_FLANNERY, + REMATCH_NORMAN, + REMATCH_WINONA, + REMATCH_TATE_AND_LIZA, + REMATCH_JUAN +}; + +void UpdateGymLeaderRematch(void) +{ + if (FlagGet(FLAG_SYS_GAME_CLEAR) && (Random() % 100) <= 30) + { + if (FlagGet(FLAG_WATTSON_REMATCH_AVAILABLE)) + UpdateGymLeaderRematchFromArray(GymLeaderRematches_AfterNewMauville, ARRAY_COUNT(GymLeaderRematches_AfterNewMauville), 5); + else + UpdateGymLeaderRematchFromArray(GymLeaderRematches_BeforeNewMauville, ARRAY_COUNT(GymLeaderRematches_BeforeNewMauville), 1); + } +} + +static void UpdateGymLeaderRematchFromArray(const u16 *data, size_t size, u32 maxRematch) +{ + s32 whichLeader = 0; + s32 lowestRematchIndex = 5; + u32 i; + s32 rematchIndex; + for (i = 0; i < size; i++) + { + if (!gSaveBlock1Ptr->trainerRematches[data[i]]) + { + rematchIndex = GetRematchIndex(data[i]); + if (lowestRematchIndex > rematchIndex) + lowestRematchIndex = rematchIndex; + whichLeader++; + } + } + if (whichLeader != 0 && lowestRematchIndex <= maxRematch) + { + whichLeader = 0; + for (i = 0; i < size; i++) + { + if (!gSaveBlock1Ptr->trainerRematches[data[i]]) + { + rematchIndex = GetRematchIndex(data[i]); + if (rematchIndex == lowestRematchIndex) + whichLeader++; + } + } + if (whichLeader != 0) + { + whichLeader = Random() % whichLeader; + for (i = 0; i < size; i++) + { + if (!gSaveBlock1Ptr->trainerRematches[data[i]]) + { + rematchIndex = GetRematchIndex(data[i]); + if (rematchIndex == lowestRematchIndex) + { + if (whichLeader == 0) + { + gSaveBlock1Ptr->trainerRematches[data[i]] = lowestRematchIndex; + break; + } + whichLeader--; + } + } + } + } + } +} + +static s32 GetRematchIndex(u32 trainerIdx) +{ + s32 i; + for (i = 0; i < 5; i++) + { + if (!HasTrainerBeenFought(gRematchTable[trainerIdx].trainerIds[i])) + { + return i; + } + } + return 5; +} diff --git a/src/trainer_rematch.c b/src/trainer_rematch.c deleted file mode 100644 index 676cfa087..000000000 --- a/src/trainer_rematch.c +++ /dev/null @@ -1,105 +0,0 @@ -#include "global.h" -#include "constants/flags.h" -#include "random.h" -#include "event_data.h" -#include "battle_setup.h" -#include "trainer_rematch.h" - -static void sub_81DA5D4(const u16 *data, size_t size, u32 a2); -static s32 sub_81DA6CC(u32 trainerIdx); - -static const u16 GymLeaderRematches_AfterNewMauville[] = { - REMATCH_ROXANNE, - REMATCH_BRAWLY, - REMATCH_WATTSON, - REMATCH_FLANNERY, - REMATCH_NORMAN, - REMATCH_WINONA, - REMATCH_TATE_AND_LIZA, - REMATCH_JUAN -}; - -static const u16 GymLeaderRematches_BeforeNewMauville[] = { - REMATCH_ROXANNE, - REMATCH_BRAWLY, - // Wattson isn't available at this time - REMATCH_FLANNERY, - REMATCH_NORMAN, - REMATCH_WINONA, - REMATCH_TATE_AND_LIZA, - REMATCH_JUAN -}; - -void sub_81DA57C(void) -{ - if (FlagGet(FLAG_SYS_GAME_CLEAR) && (Random() % 100) <= 30) - { - if (FlagGet(FLAG_0x05B)) - sub_81DA5D4(GymLeaderRematches_AfterNewMauville, ARRAY_COUNT(GymLeaderRematches_AfterNewMauville), 5); - else - sub_81DA5D4(GymLeaderRematches_BeforeNewMauville, ARRAY_COUNT(GymLeaderRematches_BeforeNewMauville), 1); - } -} - -static void sub_81DA5D4(const u16 *data, size_t size, u32 a2) -{ - s32 r6 = 0; - s32 r8 = 5; - u32 i; - for (i = 0; i < size; i++) - { - if (!gSaveBlock1Ptr->trainerRematches[data[i]]) - { - s32 val = sub_81DA6CC(data[i]); - if (r8 > val) - r8 = val; - r6++; - } - } - if (r6 != 0 && r8 <= a2) - { - r6 = 0; - for (i = 0; i < size; i++) - { - if (!gSaveBlock1Ptr->trainerRematches[data[i]]) - { - s32 val = sub_81DA6CC(data[i]); - if (val == r8) - r6++; - } - } - if (r6 != 0) - { - r6 = Random() % r6; - for (i = 0; i < size; i++) - { - if (!gSaveBlock1Ptr->trainerRematches[data[i]]) - { - s32 val = sub_81DA6CC(data[i]); - if (val == r8) - { - if (r6 == 0) - { - gSaveBlock1Ptr->trainerRematches[data[i]] = r8; - break; - } - r6--; - } - } - } - } - } -} - -static s32 sub_81DA6CC(u32 trainerIdx) -{ - s32 i; - for (i = 0; i < 5; i++) - { - if (!HasTrainerBeenFought(gRematchTable[trainerIdx].trainerIds[i])) - { - return i; - } - } - return 5; -} -- cgit v1.2.3