diff options
author | golem galvanize <golemgalvanize@github.com> | 2018-03-30 16:49:08 -0400 |
---|---|---|
committer | golem galvanize <golemgalvanize@github.com> | 2018-03-30 16:49:08 -0400 |
commit | 069a7e71fc8bd0886fe2d5c39bb96d8fee0d798b (patch) | |
tree | 191d39f28270fb43feaca1d128274ef317968869 /src/gym_leader_rematch.c | |
parent | 3006ff177b1020c0270ff33f48b3216c9d5381b1 (diff) | |
parent | a5cd6e8ef4717aad4a055d5acb6ef250e359fc45 (diff) |
Merge branch 'master' of https://github.com/pret/pokeemerald into decompile_pokedex
Diffstat (limited to 'src/gym_leader_rematch.c')
-rw-r--r-- | src/gym_leader_rematch.c | 106 |
1 files changed, 106 insertions, 0 deletions
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; +} |