diff --git a/asm/trainer_rematch.s b/asm/trainer_rematch.s deleted file mode 100644 index 1c874f28f..000000000 --- a/asm/trainer_rematch.s +++ /dev/null @@ -1,206 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - thumb_func_start sub_81DA57C -sub_81DA57C: @ 81DA57C - push {lr} - ldr r0, =0x00000864 - bl FlagGet - lsls r0, 24 - cmp r0, 0 - beq _081DA5CA - bl Random - lsls r0, 16 - lsrs r0, 16 - movs r1, 0x64 - bl __umodsi3 - lsls r0, 16 - lsrs r0, 16 - cmp r0, 0x1E - bhi _081DA5CA - movs r0, 0x5B - bl FlagGet - lsls r0, 24 - cmp r0, 0 - beq _081DA5C0 - ldr r0, =gUnknown_0862AD34 - movs r1, 0x8 - movs r2, 0x5 - bl sub_81DA5D4 - b _081DA5CA - .pool -_081DA5C0: - ldr r0, =gUnknown_0862AD44 - movs r1, 0x7 - movs r2, 0x1 - bl sub_81DA5D4 -_081DA5CA: - pop {r0} - bx r0 - .pool - thumb_func_end sub_81DA57C - - thumb_func_start sub_81DA5D4 -sub_81DA5D4: @ 81DA5D4 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x8 - mov r9, r0 - adds r7, r1, 0 - mov r10, r2 - movs r6, 0 - movs r0, 0x5 - mov r8, r0 - movs r5, 0 - cmp r5, r7 - bcs _081DA61C - mov r4, r9 -_081DA5F4: - ldr r0, =gSaveBlock1Ptr - ldr r0, [r0] - ldrh r1, [r4] - ldr r2, =0x000009ca - adds r0, r2 - adds r0, r1 - ldrb r0, [r0] - cmp r0, 0 - bne _081DA614 - adds r0, r1, 0 - bl sub_81DA6CC - cmp r8, r0 - ble _081DA612 - mov r8, r0 -_081DA612: - adds r6, 0x1 -_081DA614: - adds r4, 0x2 - adds r5, 0x1 - cmp r5, r7 - bcc _081DA5F4 -_081DA61C: - cmp r6, 0 - beq _081DA6BA - cmp r8, r10 - bhi _081DA6BA - movs r6, 0 - movs r5, 0 - cmp r5, r7 - bcs _081DA654 - mov r4, r9 -_081DA62E: - ldr r0, =gSaveBlock1Ptr - ldr r0, [r0] - ldrh r1, [r4] - ldr r2, =0x000009ca - adds r0, r2 - adds r0, r1 - ldrb r0, [r0] - cmp r0, 0 - bne _081DA64C - adds r0, r1, 0 - bl sub_81DA6CC - cmp r0, r8 - bne _081DA64C - adds r6, 0x1 -_081DA64C: - adds r4, 0x2 - adds r5, 0x1 - cmp r5, r7 - bcc _081DA62E -_081DA654: - cmp r6, 0 - beq _081DA6BA - bl Random - lsls r0, 16 - lsrs r0, 16 - adds r1, r6, 0 - bl __modsi3 - adds r6, r0, 0 - movs r5, 0 - cmp r5, r7 - bcs _081DA6BA - ldr r2, =gSaveBlock1Ptr - ldr r3, =0x000009ca - mov r4, r9 -_081DA674: - ldr r0, [r2] - ldrh r1, [r4] - adds r0, r3 - adds r0, r1 - ldrb r0, [r0] - cmp r0, 0 - bne _081DA6B2 - adds r0, r1, 0 - str r2, [sp] - str r3, [sp, 0x4] - bl sub_81DA6CC - ldr r2, [sp] - ldr r3, [sp, 0x4] - cmp r0, r8 - bne _081DA6B2 - cmp r6, 0 - bne _081DA6B0 - ldr r0, [r2] - adds r0, r3 - ldrh r4, [r4] - adds r0, r4 - mov r1, r8 - strb r1, [r0] - b _081DA6BA - .pool -_081DA6B0: - subs r6, 0x1 -_081DA6B2: - adds r4, 0x2 - adds r5, 0x1 - cmp r5, r7 - bcc _081DA674 -_081DA6BA: - add sp, 0x8 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r0} - bx r0 - thumb_func_end sub_81DA5D4 - - thumb_func_start sub_81DA6CC -sub_81DA6CC: @ 81DA6CC - push {r4-r6,lr} - adds r5, r0, 0 - movs r4, 0 - ldr r6, =gRematchTable -_081DA6D4: - lsls r0, r4, 1 - lsls r1, r5, 4 - adds r0, r1 - adds r0, r6 - ldrh r0, [r0] - bl HasTrainerBeenFought - lsls r0, 24 - cmp r0, 0 - bne _081DA6F0 - adds r0, r4, 0 - b _081DA6F8 - .pool -_081DA6F0: - adds r4, 0x1 - cmp r4, 0x4 - ble _081DA6D4 - movs r0, 0x5 -_081DA6F8: - pop {r4-r6} - pop {r1} - bx r1 - thumb_func_end sub_81DA6CC - - .align 2, 0 @ Don't pad with nop. diff --git a/data/trainer_rematch.s b/data/trainer_rematch.s deleted file mode 100644 index e3a05ba8b..000000000 --- a/data/trainer_rematch.s +++ /dev/null @@ -1,10 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .section .rodata - -gUnknown_0862AD34:: @ 862AD34 - .2byte 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048 - -gUnknown_0862AD44:: @ 862AD44 - .2byte 0x0041, 0x0042, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0000 diff --git a/data/unk_transition.s b/data/unk_transition.s index 2dc54e22d..db5744d08 100644 --- a/data/unk_transition.s +++ b/data/unk_transition.s @@ -3,6 +3,7 @@ .section .rodata + .align 2, 0 gUnknown_0862AD54:: @ 862AD54 .incbin "graphics/battle_transitions/frontier_transition.4bpp.lz" diff --git a/ld_script.txt b/ld_script.txt index 4c7b40dd9..43ff3468a 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); - asm/trainer_rematch.o(.text); + src/trainer_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); - data/trainer_rematch.o(.rodata); + src/trainer_rematch.o(.rodata); data/unk_transition.o(.rodata); src/unk_transition.o(.rodata); data/text_input_strings.o(.rodata); diff --git a/src/trainer_rematch.c b/src/trainer_rematch.c new file mode 100644 index 000000000..8056396a0 --- /dev/null +++ b/src/trainer_rematch.c @@ -0,0 +1,97 @@ + +// Includes +#include "global.h" +#include "constants/flags.h" +#include "random.h" +#include "event_data.h" +#include "battle_setup.h" + +// Static type declarations + +// Static RAM declarations + +// Static ROM declarations + +void sub_81DA5D4(const u16 *data, size_t size, u32 a2); +s32 sub_81DA6CC(u32 trainerIdx); + +// .rodata + +const u16 gUnknown_0862AD34[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48}; +const u16 gUnknown_0862AD44[] = {0x41, 0x42, 0x44, 0x45, 0x46, 0x47, 0x48}; + +// .text + +void sub_81DA57C(void) +{ + if (FlagGet(FLAG_SYS_GAME_CLEAR) && (Random() % 100) <= 30) + { + if (FlagGet(FLAG_0x05B)) + sub_81DA5D4(gUnknown_0862AD34, ARRAY_COUNT(gUnknown_0862AD34), 5); + else + sub_81DA5D4(gUnknown_0862AD44, ARRAY_COUNT(gUnknown_0862AD44), 1); + } +} + +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--; + } + } + } + } + } +} + +s32 sub_81DA6CC(u32 trainerIdx) +{ + s32 i; + for (i = 0; i < 5; i++) + { + if (!HasTrainerBeenFought(gRematchTable[trainerIdx].trainerIds[i])) + { + return i; + } + } + return 5; +}