diff --git a/asm/battle_tower.s b/asm/battle_tower.s index eb89d4d79..6cee2dc0a 100644 --- a/asm/battle_tower.s +++ b/asm/battle_tower.s @@ -3134,7 +3134,7 @@ _081639FC: bl __umodsi3 cmp r0, 0 bne _08163A78 - bl sub_81DA57C + bl UpdateGymLeaderRematch b _08163A78 .pool _08163A2C: diff --git a/data/scripts/maps/MauvilleCity.inc b/data/scripts/maps/MauvilleCity.inc index 1c48e9710..af30c7057 100644 --- a/data/scripts/maps/MauvilleCity.inc +++ b/data/scripts/maps/MauvilleCity.inc @@ -16,7 +16,7 @@ MauvilleCity_MapScript1_1DF38B: @ 81DF38B MauvilleCity_EventScript_1DF3A9:: @ 81DF3A9 clearflag FLAG_0x391 setflag FLAG_0x390 - setflag FLAG_0x05B + setflag FLAG_WATTSON_REMATCH_AVAILABLE return MauvilleCity_EventScript_1DF3B3:: @ 81DF3B3 diff --git a/include/constants/flags.h b/include/constants/flags.h index 63e053cbd..2e40a8eba 100644 --- a/include/constants/flags.h +++ b/include/constants/flags.h @@ -91,7 +91,7 @@ #define FLAG_0x058 0x58 #define FLAG_0x059 0x59 #define FLAG_0x05A 0x5A -#define FLAG_0x05B 0x5B +#define FLAG_WATTSON_REMATCH_AVAILABLE 0x5B #define FLAG_0x05C 0x5C #define FLAG_0x05D 0x5D #define FLAG_0x05E 0x5E diff --git a/include/trainer_rematch.h b/include/gym_leader_rematch.h similarity index 97% rename from include/trainer_rematch.h rename to include/gym_leader_rematch.h index faf459846..ef0e5fef1 100644 --- a/include/trainer_rematch.h +++ b/include/gym_leader_rematch.h @@ -82,6 +82,6 @@ enum { REMATCH_WALLACE }; -void sub_81DA57C(void); +void UpdateGymLeaderRematch(void); #endif //GUARD_TRAINER_REMATCH_H diff --git a/ld_script.txt b/ld_script.txt index 43ff3468a..3b828766f 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -267,7 +267,7 @@ SECTIONS { src/rayquaza_scene.o(.text); src/walda_phrase.o(.text); asm/contest_link_81D9DE4.o(.text); - src/trainer_rematch.o(.text); + src/gym_leader_rematch.o(.text); src/unk_transition.o(.text); src/international_string_util.o(.text); asm/international_string_util.o(.text); @@ -536,7 +536,7 @@ SECTIONS { data/pokenav.o(.rodata); src/rayquaza_scene.o(.rodata); src/walda_phrase.o(.rodata); - src/trainer_rematch.o(.rodata); + src/gym_leader_rematch.o(.rodata); data/unk_transition.o(.rodata); src/unk_transition.o(.rodata); data/text_input_strings.o(.rodata); 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/trainer_rematch.c b/src/gym_leader_rematch.c similarity index 51% rename from src/trainer_rematch.c rename to src/gym_leader_rematch.c index 676cfa087..355ae5534 100644 --- a/src/trainer_rematch.c +++ b/src/gym_leader_rematch.c @@ -3,10 +3,10 @@ #include "random.h" #include "event_data.h" #include "battle_setup.h" -#include "trainer_rematch.h" +#include "gym_leader_rematch.h" -static void sub_81DA5D4(const u16 *data, size_t size, u32 a2); -static s32 sub_81DA6CC(u32 trainerIdx); +static void UpdateGymLeaderRematchFromArray(const u16 *data, size_t size, u32 maxRematch); +static s32 GetRematchIndex(u32 trainerIdx); static const u16 GymLeaderRematches_AfterNewMauville[] = { REMATCH_ROXANNE, @@ -30,60 +30,61 @@ static const u16 GymLeaderRematches_BeforeNewMauville[] = { REMATCH_JUAN }; -void sub_81DA57C(void) +void UpdateGymLeaderRematch(void) { if (FlagGet(FLAG_SYS_GAME_CLEAR) && (Random() % 100) <= 30) { - if (FlagGet(FLAG_0x05B)) - sub_81DA5D4(GymLeaderRematches_AfterNewMauville, ARRAY_COUNT(GymLeaderRematches_AfterNewMauville), 5); + if (FlagGet(FLAG_WATTSON_REMATCH_AVAILABLE)) + UpdateGymLeaderRematchFromArray(GymLeaderRematches_AfterNewMauville, ARRAY_COUNT(GymLeaderRematches_AfterNewMauville), 5); else - sub_81DA5D4(GymLeaderRematches_BeforeNewMauville, ARRAY_COUNT(GymLeaderRematches_BeforeNewMauville), 1); + UpdateGymLeaderRematchFromArray(GymLeaderRematches_BeforeNewMauville, ARRAY_COUNT(GymLeaderRematches_BeforeNewMauville), 1); } } -static void sub_81DA5D4(const u16 *data, size_t size, u32 a2) +static void UpdateGymLeaderRematchFromArray(const u16 *data, size_t size, u32 maxRematch) { - s32 r6 = 0; - s32 r8 = 5; + s32 whichLeader = 0; + s32 lowestRematchIndex = 5; u32 i; + s32 rematchIndex; for (i = 0; i < size; i++) { if (!gSaveBlock1Ptr->trainerRematches[data[i]]) { - s32 val = sub_81DA6CC(data[i]); - if (r8 > val) - r8 = val; - r6++; + rematchIndex = GetRematchIndex(data[i]); + if (lowestRematchIndex > rematchIndex) + lowestRematchIndex = rematchIndex; + whichLeader++; } } - if (r6 != 0 && r8 <= a2) + if (whichLeader != 0 && lowestRematchIndex <= maxRematch) { - r6 = 0; + whichLeader = 0; for (i = 0; i < size; i++) { if (!gSaveBlock1Ptr->trainerRematches[data[i]]) { - s32 val = sub_81DA6CC(data[i]); - if (val == r8) - r6++; + rematchIndex = GetRematchIndex(data[i]); + if (rematchIndex == lowestRematchIndex) + whichLeader++; } } - if (r6 != 0) + if (whichLeader != 0) { - r6 = Random() % r6; + whichLeader = Random() % whichLeader; for (i = 0; i < size; i++) { if (!gSaveBlock1Ptr->trainerRematches[data[i]]) { - s32 val = sub_81DA6CC(data[i]); - if (val == r8) + rematchIndex = GetRematchIndex(data[i]); + if (rematchIndex == lowestRematchIndex) { - if (r6 == 0) + if (whichLeader == 0) { - gSaveBlock1Ptr->trainerRematches[data[i]] = r8; + gSaveBlock1Ptr->trainerRematches[data[i]] = lowestRematchIndex; break; } - r6--; + whichLeader--; } } } @@ -91,7 +92,7 @@ static void sub_81DA5D4(const u16 *data, size_t size, u32 a2) } } -static s32 sub_81DA6CC(u32 trainerIdx) +static s32 GetRematchIndex(u32 trainerIdx) { s32 i; for (i = 0; i < 5; i++)