pokeemerald/src/gym_leader_rematch.c

107 lines
3.0 KiB
C
Raw Normal View History

2018-02-26 03:56:09 +01:00
#include "global.h"
#include "constants/flags.h"
#include "random.h"
#include "event_data.h"
#include "battle_setup.h"
#include "gym_leader_rematch.h"
2018-02-26 03:56:09 +01:00
static void UpdateGymLeaderRematchFromArray(const u16 *data, size_t size, u32 maxRematch);
static s32 GetRematchIndex(u32 trainerIdx);
2018-02-26 03:56:09 +01:00
static const u16 GymLeaderRematches_AfterNewMauville[] = {
REMATCH_ROXANNE,
REMATCH_BRAWLY,
REMATCH_WATTSON,
REMATCH_FLANNERY,
REMATCH_NORMAN,
REMATCH_WINONA,
REMATCH_TATE_AND_LIZA,
REMATCH_JUAN
};
2018-02-26 03:56:09 +01:00
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
};
2018-02-26 03:56:09 +01:00
void UpdateGymLeaderRematch(void)
2018-02-26 03:56:09 +01:00
{
if (FlagGet(FLAG_SYS_GAME_CLEAR) && (Random() % 100) <= 30)
{
if (FlagGet(FLAG_WATTSON_REMATCH_AVAILABLE))
UpdateGymLeaderRematchFromArray(GymLeaderRematches_AfterNewMauville, ARRAY_COUNT(GymLeaderRematches_AfterNewMauville), 5);
2018-02-26 03:56:09 +01:00
else
UpdateGymLeaderRematchFromArray(GymLeaderRematches_BeforeNewMauville, ARRAY_COUNT(GymLeaderRematches_BeforeNewMauville), 1);
2018-02-26 03:56:09 +01:00
}
}
static void UpdateGymLeaderRematchFromArray(const u16 *data, size_t size, u32 maxRematch)
2018-02-26 03:56:09 +01:00
{
s32 whichLeader = 0;
s32 lowestRematchIndex = 5;
2018-02-26 03:56:09 +01:00
u32 i;
s32 rematchIndex;
2018-02-26 03:56:09 +01:00
for (i = 0; i < size; i++)
{
if (!gSaveBlock1Ptr->trainerRematches[data[i]])
{
rematchIndex = GetRematchIndex(data[i]);
if (lowestRematchIndex > rematchIndex)
lowestRematchIndex = rematchIndex;
whichLeader++;
2018-02-26 03:56:09 +01:00
}
}
if (whichLeader != 0 && lowestRematchIndex <= maxRematch)
2018-02-26 03:56:09 +01:00
{
whichLeader = 0;
2018-02-26 03:56:09 +01:00
for (i = 0; i < size; i++)
{
if (!gSaveBlock1Ptr->trainerRematches[data[i]])
{
rematchIndex = GetRematchIndex(data[i]);
if (rematchIndex == lowestRematchIndex)
whichLeader++;
2018-02-26 03:56:09 +01:00
}
}
if (whichLeader != 0)
2018-02-26 03:56:09 +01:00
{
whichLeader = Random() % whichLeader;
2018-02-26 03:56:09 +01:00
for (i = 0; i < size; i++)
{
if (!gSaveBlock1Ptr->trainerRematches[data[i]])
{
rematchIndex = GetRematchIndex(data[i]);
if (rematchIndex == lowestRematchIndex)
2018-02-26 03:56:09 +01:00
{
if (whichLeader == 0)
2018-02-26 03:56:09 +01:00
{
gSaveBlock1Ptr->trainerRematches[data[i]] = lowestRematchIndex;
2018-02-26 03:56:09 +01:00
break;
}
whichLeader--;
2018-02-26 03:56:09 +01:00
}
}
}
}
}
}
static s32 GetRematchIndex(u32 trainerIdx)
2018-02-26 03:56:09 +01:00
{
s32 i;
for (i = 0; i < 5; i++)
{
if (!HasTrainerBeenFought(gRematchTable[trainerIdx].trainerIds[i]))
{
return i;
}
}
return 5;
}