From 57e5624e0619b8d96fb43ff68d9eeefe734b1a4f Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Tue, 10 Oct 2017 18:01:45 +0200 Subject: [PATCH 1/3] battle9 is being worked on --- asm/battle_9.s | 1487 +-------------------- asm/battle_controller_opponent.s | 10 +- asm/battle_controller_player.s | 20 +- asm/battle_controller_player_partner.s | 6 +- asm/battle_controller_recorded_opponent.s | 6 +- asm/battle_controller_recorded_player.s | 8 +- asm/battle_controller_safari.s | 4 +- asm/battle_controller_wally.s | 6 +- asm/rom3.s | 6 +- include/battle_controllers.h | 1 + include/battle_script_commands.h | 2 +- ld_script.txt | 1 + src/battle_9.c | 525 ++++++++ src/battle_script_commands.c | 8 +- 14 files changed, 568 insertions(+), 1522 deletions(-) create mode 100644 src/battle_9.c diff --git a/asm/battle_9.s b/asm/battle_9.s index 0dfc105ec..6d77d1593 100644 --- a/asm/battle_9.s +++ b/asm/battle_9.s @@ -5,1487 +5,6 @@ .text - thumb_func_start ai_switch_perish_song -ai_switch_perish_song: @ 8062BF8 - push {lr} - ldr r1, =gStatuses3 - ldr r0, =gActiveBank - ldrb r2, [r0] - lsls r0, r2, 2 - adds r0, r1 - ldr r0, [r0] - movs r1, 0x20 - ands r0, r1 - cmp r0, 0 - beq _08062C50 - ldr r0, =gDisableStructs - lsls r1, r2, 3 - subs r1, r2 - lsls r1, 2 - adds r1, r0 - ldrb r0, [r1, 0xF] - lsls r0, 28 - cmp r0, 0 - bne _08062C50 - ldr r0, =gBattleStruct - ldr r0, [r0] - adds r0, r2, r0 - movs r1, 0xA5 - lsls r1, 2 - adds r0, r1 - movs r1, 0x6 - strb r1, [r0] - movs r0, 0x1 - movs r1, 0x2 - movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb - movs r0, 0x1 - b _08062C52 - .pool -_08062C50: - movs r0, 0 -_08062C52: - pop {r1} - bx r1 - thumb_func_end ai_switch_perish_song - - thumb_func_start sub_8062C58 -sub_8062C58: @ 8062C58 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x4 - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - movs r1, 0x1 - ands r0, r1 - cmp r0, 0 - beq _08062C72 - b _08062E2C -_08062C72: - ldr r0, =gActiveBank - ldrb r0, [r0] - bl GetBankIdentity - movs r1, 0x1 - eors r0, r1 - lsls r0, 24 - lsrs r0, 24 - mov r10, r0 - ldr r4, =gBattleMons - bl GetBankByIdentity - lsls r0, 24 - lsrs r0, 24 - movs r1, 0x58 - muls r0, r1 - adds r0, r4 - adds r0, 0x20 - ldrb r0, [r0] - cmp r0, 0x19 - beq _08062C9E - b _08062E2C -_08062C9E: - mov r0, r10 - bl GetBankByIdentity - lsls r0, 24 - lsrs r2, r0, 24 - movs r6, 0 - mov r8, r4 - movs r7, 0x58 - adds r0, r2, 0 - muls r0, r7 - adds r4, r0, r4 - movs r0, 0x20 - adds r0, r4 - mov r9, r0 -_08062CBA: - lsls r1, r6, 1 - ldr r5, =gActiveBank - ldrb r0, [r5] - muls r0, r7 - adds r1, r0 - mov r0, r8 - adds r0, 0xC - adds r1, r0 - ldrh r0, [r1] - cmp r0, 0 - beq _08062CE8 - ldrh r1, [r4] - mov r3, r9 - ldrb r2, [r3] - bl AI_TypeCalc - lsls r0, 24 - lsrs r1, r0, 24 - movs r0, 0x2 - ands r1, r0 - cmp r1, 0 - beq _08062CE8 - b _08062E2C -_08062CE8: - adds r6, 0x1 - cmp r6, 0x3 - ble _08062CBA - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - ldr r1, =0x00808000 - ands r0, r1 - cmp r0, 0 - beq _08062D50 - ldrb r1, [r5] - movs r0, 0x2 - ands r0, r1 - adds r1, r5, 0 - cmp r0, 0 - bne _08062D20 - movs r4, 0 - movs r0, 0x3 - str r0, [sp] - b _08062D58 - .pool -_08062D20: - movs r4, 0x3 - movs r3, 0x6 - str r3, [sp] - b _08062D58 -_08062D28: - ldr r0, =gActiveBank - ldrb r0, [r0] - ldr r1, =gBattleStruct - ldr r1, [r1] - adds r0, r1 - movs r1, 0xA5 - lsls r1, 2 - adds r0, r1 - strb r6, [r0] - movs r0, 0x1 - movs r1, 0x2 - movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb - movs r0, 0x1 - b _08062E2E - .pool -_08062D50: - movs r4, 0 - movs r3, 0x6 - str r3, [sp] - adds r1, r5, 0 -_08062D58: - ldrb r0, [r1] - bl GetBankSide - lsls r0, 24 - ldr r1, =gEnemyParty - mov r9, r1 - cmp r0, 0 - bne _08062D6C - ldr r3, =gPlayerParty - mov r9, r3 -_08062D6C: - adds r6, r4, 0 - ldr r0, [sp] - cmp r6, r0 - bge _08062E2C -_08062D74: - movs r0, 0x64 - adds r5, r6, 0 - muls r5, r0 - mov r1, r9 - adds r4, r1, r5 - adds r0, r4, 0 - movs r1, 0x39 - bl GetMonData - cmp r0, 0 - beq _08062E24 - adds r0, r4, 0 - movs r1, 0x41 - bl GetMonData - cmp r0, 0 - beq _08062E24 - adds r0, r4, 0 - movs r1, 0x41 - bl GetMonData - movs r1, 0xCE - lsls r1, 1 - cmp r0, r1 - beq _08062E24 - ldr r1, =gBattlePartyID - ldr r0, =gActiveBank - ldrb r0, [r0] - lsls r0, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r6, r0 - beq _08062E24 - adds r0, r4, 0 - movs r1, 0xB - bl GetMonData - adds r0, r4, 0 - movs r1, 0x2E - bl GetMonData - mov r0, r10 - bl GetBankByIdentity - lsls r0, 24 - lsrs r2, r0, 24 - movs r4, 0 - mov r8, r5 - ldr r1, =gBattleMons - movs r0, 0x58 - muls r0, r2 - adds r5, r0, r1 - adds r7, r5, 0 - adds r7, 0x20 -_08062DE0: - adds r1, r4, 0 - adds r1, 0xD - mov r0, r9 - add r0, r8 - bl GetMonData - lsls r0, 16 - lsrs r0, 16 - cmp r0, 0 - beq _08062E1E - ldrh r1, [r5] - ldrb r2, [r7] - bl AI_TypeCalc - lsls r0, 24 - lsrs r1, r0, 24 - movs r0, 0x2 - ands r1, r0 - cmp r1, 0 - beq _08062E1E - bl Random - lsls r0, 16 - lsrs r0, 16 - movs r1, 0x3 - bl __umodsi3 - lsls r0, 16 - lsrs r0, 16 - cmp r0, 0x1 - bls _08062D28 -_08062E1E: - adds r4, 0x1 - cmp r4, 0x3 - ble _08062DE0 -_08062E24: - adds r6, 0x1 - ldr r3, [sp] - cmp r6, r3 - blt _08062D74 -_08062E2C: - movs r0, 0 -_08062E2E: - add sp, 0x4 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8062C58 - - thumb_func_start sub_8062E54 -sub_8062E54: @ 8062E54 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x8 - movs r0, 0x1 - bl ai_has_super_effective_move_on_field - lsls r0, 24 - cmp r0, 0 - beq _08062E82 - bl Random - lsls r0, 16 - lsrs r0, 16 - movs r1, 0x3 - bl __umodsi3 - lsls r0, 16 - cmp r0, 0 - beq _08062E82 - b _080630AC -_08062E82: - ldr r1, =gUnknown_02024250 - ldr r5, =gActiveBank - ldrb r3, [r5] - lsls r0, r3, 1 - adds r4, r0, r1 - ldrh r1, [r4] - cmp r1, 0 - bne _08062E94 - b _080630AC -_08062E94: - ldr r0, =0x0000ffff - cmp r1, r0 - bne _08062E9C - b _080630AC -_08062E9C: - ldr r2, =gBattleMoves - ldrh r1, [r4] - lsls r0, r1, 1 - adds r0, r1 - lsls r0, 2 - adds r0, r2 - ldrb r0, [r0, 0x1] - cmp r0, 0 - bne _08062EB0 - b _080630AC -_08062EB0: - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - movs r1, 0x1 - ands r0, r1 - cmp r0, 0 - beq _08062F1A - mov r8, r3 - mov r0, r8 - bl GetBankIdentity - movs r4, 0x2 - eors r0, r4 - lsls r0, 24 - lsrs r0, 24 - bl GetBankByIdentity - ldr r1, =gAbsentBankFlags - ldrb r1, [r1] - ldr r2, =gBitTable - lsls r0, 24 - lsrs r0, 22 - adds r0, r2 - ldr r0, [r0] - ands r1, r0 - cmp r1, 0 - beq _08062F04 - ldrb r7, [r5] - b _08062F1E - .pool -_08062F04: - ldrb r0, [r5] - bl GetBankIdentity - eors r0, r4 - lsls r0, 24 - lsrs r0, 24 - bl GetBankByIdentity - lsls r0, 24 - lsrs r7, r0, 24 - b _08062F1E -_08062F1A: - adds r7, r3, 0 - mov r8, r7 -_08062F1E: - ldr r3, =gBattleMoves - ldr r1, =gUnknown_02024250 - ldr r2, =gActiveBank - ldrb r0, [r2] - lsls r0, 1 - adds r0, r1 - ldrh r1, [r0] - lsls r0, r1, 1 - adds r0, r1 - lsls r0, 2 - adds r0, r3 - ldrb r0, [r0, 0x2] - cmp r0, 0xA - bne _08062F4C - movs r0, 0x12 - b _08062F5E - .pool -_08062F4C: - cmp r0, 0xB - bne _08062F56 - movs r1, 0xB - str r1, [sp] - b _08062F60 -_08062F56: - cmp r0, 0xD - beq _08062F5C - b _080630AC -_08062F5C: - movs r0, 0xA -_08062F5E: - str r0, [sp] -_08062F60: - ldr r1, =gBattleMons - ldrb r3, [r2] - movs r0, 0x58 - muls r0, r3 - adds r0, r1 - adds r0, 0x20 - ldrb r0, [r0] - ldr r1, [sp] - cmp r0, r1 - bne _08062F76 - b _080630AC -_08062F76: - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - ldr r1, =0x00808000 - ands r0, r1 - cmp r0, 0 - beq _08062FCC - movs r0, 0x2 - ands r3, r0 - cmp r3, 0 - bne _08062F9C - movs r4, 0 - movs r0, 0x3 - b _08062FD0 - .pool -_08062F9C: - movs r4, 0x3 - movs r1, 0x6 - mov r10, r1 - b _08062FD2 -_08062FA4: - ldr r0, =gActiveBank - ldrb r0, [r0] - ldr r1, =gBattleStruct - ldr r1, [r1] - adds r0, r1 - movs r2, 0xA5 - lsls r2, 2 - adds r0, r2 - strb r5, [r0] - movs r0, 0x1 - movs r1, 0x2 - movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb - movs r0, 0x1 - b _080630AE - .pool -_08062FCC: - movs r4, 0 - movs r0, 0x6 -_08062FD0: - mov r10, r0 -_08062FD2: - ldrb r0, [r2] - bl GetBankSide - lsls r0, 24 - ldr r1, =gEnemyParty - str r1, [sp, 0x4] - cmp r0, 0 - bne _08062FE6 - ldr r2, =gPlayerParty - str r2, [sp, 0x4] -_08062FE6: - adds r5, r4, 0 - cmp r5, r10 - bge _080630AC - ldr r0, =gBaseStats - mov r9, r0 -_08062FF0: - movs r0, 0x64 - muls r0, r5 - ldr r1, [sp, 0x4] - adds r6, r1, r0 - adds r0, r6, 0 - movs r1, 0x39 - bl GetMonData - cmp r0, 0 - beq _080630A6 - adds r0, r6, 0 - movs r1, 0x41 - bl GetMonData - cmp r0, 0 - beq _080630A6 - adds r0, r6, 0 - movs r1, 0x41 - bl GetMonData - movs r1, 0xCE - lsls r1, 1 - cmp r0, r1 - beq _080630A6 - ldr r1, =gBattlePartyID - mov r2, r8 - lsls r0, r2, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r5, r0 - beq _080630A6 - lsls r0, r7, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r5, r0 - beq _080630A6 - ldr r0, =gBattleStruct - ldr r1, [r0] - adds r0, r2, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r5, r0 - beq _080630A6 - adds r0, r7, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r5, r0 - beq _080630A6 - adds r0, r6, 0 - movs r1, 0xB - bl GetMonData - lsls r0, 16 - lsrs r4, r0, 16 - adds r0, r6, 0 - movs r1, 0x2E - bl GetMonData - cmp r0, 0 - beq _08063088 - lsls r0, r4, 3 - subs r0, r4 - lsls r0, 2 - add r0, r9 - ldrb r0, [r0, 0x17] - b _08063092 - .pool -_08063088: - lsls r0, r4, 3 - subs r0, r4 - lsls r0, 2 - add r0, r9 - ldrb r0, [r0, 0x16] -_08063092: - ldr r1, [sp] - cmp r1, r0 - bne _080630A6 - bl Random - movs r1, 0x1 - ands r1, r0 - cmp r1, 0 - beq _080630A6 - b _08062FA4 -_080630A6: - adds r5, 0x1 - cmp r5, r10 - blt _08062FF0 -_080630AC: - movs r0, 0 -_080630AE: - add sp, 0x8 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - thumb_func_end sub_8062E54 - - thumb_func_start ai_switchout_natural_cure -ai_switchout_natural_cure: @ 80630C0 - push {r4,r5,lr} - ldr r3, =gBattleMons - ldr r5, =gActiveBank - ldrb r4, [r5] - movs r0, 0x58 - adds r2, r4, 0 - muls r2, r0 - adds r0, r3, 0 - adds r0, 0x4C - adds r0, r2, r0 - ldr r0, [r0] - movs r1, 0x7 - ands r0, r1 - cmp r0, 0 - beq _08063188 - adds r1, r2, r3 - adds r0, r1, 0 - adds r0, 0x20 - ldrb r0, [r0] - cmp r0, 0x1E - bne _08063188 - ldrh r0, [r1, 0x2C] - ldrh r1, [r1, 0x28] - lsrs r0, 1 - cmp r1, r0 - bcc _08063188 - ldr r1, =gUnknown_02024250 - lsls r0, r4, 1 - adds r0, r1 - ldrh r1, [r0] - cmp r1, 0 - beq _08063106 - ldr r0, =0x0000ffff - cmp r1, r0 - bne _08063128 -_08063106: - bl Random - movs r1, 0x1 - ands r1, r0 - cmp r1, 0 - beq _08063128 - ldrb r0, [r5] - b _08063190 - .pool -_08063128: - ldr r2, =gBattleMoves - ldr r1, =gUnknown_02024250 - ldr r4, =gActiveBank - ldrb r0, [r4] - lsls r0, 1 - adds r0, r1 - ldrh r1, [r0] - lsls r0, r1, 1 - adds r0, r1 - lsls r0, 2 - adds r0, r2 - ldrb r0, [r0, 0x1] - cmp r0, 0 - bne _08063160 - bl Random - movs r1, 0x1 - ands r1, r0 - cmp r1, 0 - beq _08063160 - ldrb r0, [r4] - b _08063190 - .pool -_08063160: - movs r0, 0x8 - movs r1, 0x1 - bl ai_switchout_something - lsls r0, 24 - cmp r0, 0 - bne _080631AA - movs r0, 0x4 - movs r1, 0x1 - bl ai_switchout_something - lsls r0, 24 - cmp r0, 0 - bne _080631AA - bl Random - movs r1, 0x1 - ands r1, r0 - cmp r1, 0 - bne _0806318C -_08063188: - movs r0, 0 - b _080631AC -_0806318C: - ldr r0, =gActiveBank - ldrb r0, [r0] -_08063190: - ldr r1, =gBattleStruct - ldr r1, [r1] - adds r0, r1 - movs r1, 0xA5 - lsls r1, 2 - adds r0, r1 - movs r1, 0x6 - strb r1, [r0] - movs r0, 0x1 - movs r1, 0x2 - movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb -_080631AA: - movs r0, 0x1 -_080631AC: - pop {r4,r5} - pop {r1} - bx r1 - .pool - thumb_func_end ai_switchout_natural_cure - - thumb_func_start ai_has_super_effective_move_on_field -ai_has_super_effective_move_on_field: @ 80631BC - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - lsls r0, 24 - lsrs r0, 24 - mov r9, r0 - ldr r0, =gActiveBank - ldrb r0, [r0] - bl GetBankIdentity - movs r1, 0x1 - eors r0, r1 - lsls r0, 24 - lsrs r0, 24 - mov r10, r0 - bl GetBankByIdentity - lsls r0, 24 - lsrs r3, r0, 24 - ldr r0, =gAbsentBankFlags - ldrb r1, [r0] - ldr r2, =gBitTable - lsls r0, r3, 2 - adds r0, r2 - ldr r0, [r0] - ands r1, r0 - cmp r1, 0 - bne _08063256 - movs r4, 0 - ldr r6, =gBattleMons - movs r7, 0x58 - adds r0, r3, 0 - muls r0, r7 - adds r5, r0, r6 - movs r0, 0x20 - adds r0, r5 - mov r8, r0 -_0806320A: - lsls r1, r4, 1 - ldr r0, =gActiveBank - ldrb r0, [r0] - muls r0, r7 - adds r1, r0 - adds r0, r6, 0 - adds r0, 0xC - adds r1, r0 - ldrh r0, [r1] - cmp r0, 0 - beq _08063250 - ldrh r1, [r5] - mov r3, r8 - ldrb r2, [r3] - bl AI_TypeCalc - lsls r0, 24 - lsrs r1, r0, 24 - movs r0, 0x2 - ands r1, r0 - cmp r1, 0 - beq _08063250 - mov r0, r9 - cmp r0, 0 - bne _08063278 - bl Random - lsls r0, 16 - lsrs r0, 16 - movs r1, 0xA - bl __umodsi3 - lsls r0, 16 - cmp r0, 0 - bne _08063278 -_08063250: - adds r4, 0x1 - cmp r4, 0x3 - ble _0806320A -_08063256: - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - movs r1, 0x1 - ands r0, r1 - cmp r0, 0 - bne _0806327C - b _080632FA - .pool -_08063278: - movs r0, 0x1 - b _080632FC -_0806327C: - movs r1, 0x2 - mov r0, r10 - eors r0, r1 - bl GetBankByIdentity - lsls r0, 24 - lsrs r3, r0, 24 - ldr r0, =gAbsentBankFlags - ldrb r1, [r0] - ldr r2, =gBitTable - lsls r0, r3, 2 - adds r0, r2 - ldr r0, [r0] - ands r1, r0 - cmp r1, 0 - bne _080632FA - movs r4, 0 - ldr r6, =gBattleMons - movs r7, 0x58 - adds r0, r3, 0 - muls r0, r7 - adds r5, r0, r6 - movs r3, 0x20 - adds r3, r5 - mov r8, r3 -_080632AE: - lsls r1, r4, 1 - ldr r0, =gActiveBank - ldrb r0, [r0] - muls r0, r7 - adds r1, r0 - adds r0, r6, 0 - adds r0, 0xC - adds r1, r0 - ldrh r0, [r1] - cmp r0, 0 - beq _080632F4 - ldrh r1, [r5] - mov r3, r8 - ldrb r2, [r3] - bl AI_TypeCalc - lsls r0, 24 - lsrs r1, r0, 24 - movs r0, 0x2 - ands r1, r0 - cmp r1, 0 - beq _080632F4 - mov r0, r9 - cmp r0, 0 - bne _08063278 - bl Random - lsls r0, 16 - lsrs r0, 16 - movs r1, 0xA - bl __umodsi3 - lsls r0, 16 - cmp r0, 0 - bne _08063278 -_080632F4: - adds r4, 0x1 - cmp r4, 0x3 - ble _080632AE -_080632FA: - movs r0, 0 -_080632FC: - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end ai_has_super_effective_move_on_field - - thumb_func_start ai_is_too_invested_in_stat_buffs -ai_is_too_invested_in_stat_buffs: @ 806331C - push {r4,lr} - movs r4, 0 - ldr r1, =gBattleMons - ldr r0, =gActiveBank - ldrb r2, [r0] - movs r0, 0x58 - muls r0, r2 - adds r1, 0x18 - adds r2, r0, r1 - movs r3, 0x7 -_08063330: - ldrb r1, [r2] - movs r0, 0 - ldrsb r0, [r2, r0] - cmp r0, 0x6 - ble _08063346 - subs r1, 0x6 - lsls r0, r4, 24 - asrs r0, 24 - adds r0, r1 - lsls r0, 24 - lsrs r4, r0, 24 -_08063346: - adds r2, 0x1 - subs r3, 0x1 - cmp r3, 0 - bge _08063330 - movs r0, 0 - cmp r4, 0x3 - bls _08063356 - movs r0, 0x1 -_08063356: - pop {r4} - pop {r1} - bx r1 - .pool - thumb_func_end ai_is_too_invested_in_stat_buffs - - thumb_func_start ai_switchout_something -ai_switchout_something: @ 8063364 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x10 - lsls r0, 24 - lsrs r0, 24 - str r0, [sp] - lsls r1, 24 - lsrs r1, 24 - str r1, [sp, 0x4] - ldr r2, =gUnknown_02024250 - ldr r1, =gActiveBank - ldrb r3, [r1] - lsls r0, r3, 1 - adds r4, r0, r2 - ldrh r2, [r4] - adds r5, r1, 0 - cmp r2, 0 - bne _08063390 - b _080635EE -_08063390: - ldr r0, =0x0000ffff - cmp r2, r0 - bne _08063398 - b _080635EE -_08063398: - ldr r0, =gUnknown_02024270 - adds r0, r3, r0 - ldrb r0, [r0] - cmp r0, 0xFF - bne _080633A4 - b _080635EE -_080633A4: - ldr r2, =gBattleMoves - ldrh r1, [r4] - lsls r0, r1, 1 - adds r0, r1 - lsls r0, 2 - adds r0, r2 - ldrb r0, [r0, 0x1] - cmp r0, 0 - bne _080633B8 - b _080635EE -_080633B8: - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - movs r1, 0x1 - ands r0, r1 - cmp r0, 0 - beq _0806342C - mov r9, r3 - mov r0, r9 - bl GetBankIdentity - movs r4, 0x2 - eors r0, r4 - lsls r0, 24 - lsrs r0, 24 - bl GetBankByIdentity - ldr r1, =gAbsentBankFlags - ldrb r1, [r1] - ldr r2, =gBitTable - lsls r0, 24 - lsrs r0, 22 - adds r0, r2 - ldr r0, [r0] - ands r1, r0 - cmp r1, 0 - beq _08063414 - ldrb r5, [r5] - mov r10, r5 - b _08063430 - .pool -_08063414: - ldrb r0, [r5] - bl GetBankIdentity - eors r0, r4 - lsls r0, 24 - lsrs r0, 24 - bl GetBankByIdentity - lsls r0, 24 - lsrs r0, 24 - mov r10, r0 - b _08063430 -_0806342C: - mov r9, r3 - mov r10, r9 -_08063430: - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - ldr r1, =0x00808000 - ands r0, r1 - cmp r0, 0 - beq _08063490 - ldr r2, =gActiveBank - ldrb r1, [r2] - movs r0, 0x2 - ands r0, r1 - adds r5, r2, 0 - cmp r0, 0 - bne _08063460 - movs r4, 0 - movs r0, 0x3 - str r0, [sp, 0x8] - b _08063498 - .pool -_08063460: - movs r4, 0x3 - movs r1, 0x6 - str r1, [sp, 0x8] - b _08063498 -_08063468: - ldr r0, =gActiveBank - ldrb r0, [r0] - ldr r1, =gBattleStruct - ldr r1, [r1] - adds r0, r1 - movs r2, 0xA5 - lsls r2, 2 - adds r0, r2 - strb r6, [r0] - movs r0, 0x1 - movs r1, 0x2 - movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb - movs r0, 0x1 - b _080635F0 - .pool -_08063490: - movs r4, 0 - movs r0, 0x6 - str r0, [sp, 0x8] - ldr r5, =gActiveBank -_08063498: - ldrb r0, [r5] - bl GetBankSide - lsls r0, 24 - ldr r1, =gEnemyParty - str r1, [sp, 0xC] - cmp r0, 0 - bne _080634AC - ldr r2, =gPlayerParty - str r2, [sp, 0xC] -_080634AC: - adds r6, r4, 0 - b _080635E6 - .pool -_080634BC: - movs r0, 0x64 - muls r0, r6 - ldr r1, [sp, 0xC] - adds r5, r1, r0 - adds r0, r5, 0 - movs r1, 0x39 - bl GetMonData - cmp r0, 0 - bne _080634D2 - b _080635E4 -_080634D2: - adds r0, r5, 0 - movs r1, 0x41 - bl GetMonData - cmp r0, 0 - bne _080634E0 - b _080635E4 -_080634E0: - adds r0, r5, 0 - movs r1, 0x41 - bl GetMonData - movs r1, 0xCE - lsls r1, 1 - cmp r0, r1 - beq _080635E4 - ldr r1, =gBattlePartyID - mov r2, r9 - lsls r0, r2, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r6, r0 - beq _080635E4 - mov r2, r10 - lsls r0, r2, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r6, r0 - beq _080635E4 - ldr r0, =gBattleStruct - ldr r1, [r0] - mov r2, r9 - adds r0, r2, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r6, r0 - beq _080635E4 - mov r2, r10 - adds r0, r2, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r6, r0 - beq _080635E4 - adds r0, r5, 0 - movs r1, 0xB - bl GetMonData - lsls r0, 16 - lsrs r4, r0, 16 - adds r0, r5, 0 - movs r1, 0x2E - bl GetMonData - cmp r0, 0 - beq _08063558 - lsls r0, r4, 3 - subs r0, r4 - lsls r0, 2 - ldr r1, =gBaseStats - adds r0, r1 - ldrb r2, [r0, 0x17] - b _08063564 - .pool -_08063558: - lsls r0, r4, 3 - subs r0, r4 - lsls r0, 2 - ldr r2, =gBaseStats - adds r0, r2 - ldrb r2, [r0, 0x16] -_08063564: - ldr r1, =gUnknown_02024250 - ldr r5, =gActiveBank - ldrb r0, [r5] - lsls r0, 1 - adds r0, r1 - ldrh r0, [r0] - adds r1, r4, 0 - bl AI_TypeCalc - ldr r1, [sp] - ands r1, r0 - cmp r1, 0 - beq _080635E4 - ldr r1, =gUnknown_02024270 - ldrb r0, [r5] - adds r0, r1 - ldrb r0, [r0] - mov r9, r0 - movs r4, 0 - movs r0, 0x64 - adds r1, r6, 0 - muls r1, r0 - mov r8, r1 - ldr r1, =gBattleMons - movs r0, 0x58 - mov r2, r9 - muls r2, r0 - adds r0, r2, 0 - adds r5, r0, r1 - adds r7, r5, 0 - adds r7, 0x20 -_080635A2: - adds r1, r4, 0 - adds r1, 0xD - ldr r0, [sp, 0xC] - add r0, r8 - bl GetMonData - lsls r0, 16 - lsrs r0, 16 - cmp r0, 0 - beq _080635DE - ldrh r1, [r5] - ldrb r2, [r7] - bl AI_TypeCalc - lsls r0, 24 - lsrs r1, r0, 24 - movs r0, 0x2 - ands r1, r0 - cmp r1, 0 - beq _080635DE - bl Random - lsls r0, 16 - lsrs r0, 16 - ldr r1, [sp, 0x4] - bl __modsi3 - cmp r0, 0 - bne _080635DE - b _08063468 -_080635DE: - adds r4, 0x1 - cmp r4, 0x3 - ble _080635A2 -_080635E4: - adds r6, 0x1 -_080635E6: - ldr r0, [sp, 0x8] - cmp r6, r0 - bge _080635EE - b _080634BC -_080635EE: - movs r0, 0 -_080635F0: - add sp, 0x10 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end ai_switchout_something - - thumb_func_start sub_8063614 -sub_8063614: @ 8063614 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x4 - ldr r6, =gBattleMons - ldr r4, =gActiveBank - ldrb r2, [r4] - movs r5, 0x58 - adds r0, r2, 0 - muls r0, r5 - adds r1, r6, 0 - adds r1, 0x50 - adds r0, r1 - ldr r0, [r0] - ldr r1, =0x0400e000 - ands r0, r1 - cmp r0, 0 - beq _0806363E - b _0806386C -_0806363E: - ldr r0, =gStatuses3 - lsls r1, r2, 2 - adds r1, r0 - ldr r1, [r1] - movs r0, 0x80 - lsls r0, 3 - ands r1, r0 - cmp r1, 0 - beq _08063652 - b _0806386C -_08063652: - str r1, [sp] - movs r0, 0xC - adds r1, r2, 0 - movs r2, 0x17 - movs r3, 0 - bl AbilityBattleEffects - lsls r0, 24 - lsrs r0, 24 - cmp r0, 0 - beq _0806366A - b _0806386C -_0806366A: - ldrb r1, [r4] - str r0, [sp] - movs r0, 0xC - movs r2, 0x47 - movs r3, 0 - bl AbilityBattleEffects - lsls r0, 24 - lsrs r0, 24 - cmp r0, 0 - beq _08063682 - b _0806386C -_08063682: - str r0, [sp] - movs r0, 0xE - movs r1, 0 - movs r2, 0x2A - movs r3, 0 - bl AbilityBattleEffects - lsls r0, 24 - cmp r0, 0 - beq _080636B4 - ldrb r0, [r4] - muls r0, r5 - adds r1, r0, r6 - adds r0, r1, 0 - adds r0, 0x21 - ldrb r0, [r0] - cmp r0, 0x8 - bne _080636A8 - b _0806386C -_080636A8: - adds r0, r1, 0 - adds r0, 0x22 - ldrb r0, [r0] - cmp r0, 0x8 - bne _080636B4 - b _0806386C -_080636B4: - ldr r0, =gBattleTypeFlags - ldr r1, [r0] - movs r0, 0x80 - lsls r0, 11 - ands r0, r1 - cmp r0, 0 - beq _080636C4 - b _0806386C -_080636C4: - movs r0, 0 - mov r10, r0 - movs r0, 0x1 - ands r1, r0 - cmp r1, 0 - beq _0806372E - ldrb r7, [r4] - adds r0, r7, 0 - bl GetBankIdentity - movs r5, 0x2 - eors r0, r5 - lsls r0, 24 - lsrs r0, 24 - bl GetBankByIdentity - ldr r1, =gAbsentBankFlags - ldrb r1, [r1] - ldr r2, =gBitTable - lsls r0, 24 - lsrs r0, 22 - adds r0, r2 - ldr r0, [r0] - ands r1, r0 - cmp r1, 0 - beq _08063718 - ldrb r6, [r4] - b _08063732 - .pool -_08063718: - ldrb r0, [r4] - bl GetBankIdentity - eors r0, r5 - lsls r0, 24 - lsrs r0, 24 - bl GetBankByIdentity - lsls r0, 24 - lsrs r6, r0, 24 - b _08063732 -_0806372E: - ldrb r6, [r4] - adds r7, r6, 0 -_08063732: - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - ldr r1, =0x00808000 - ands r0, r1 - cmp r0, 0 - beq _08063768 - ldr r2, =gActiveBank - ldrb r1, [r2] - movs r0, 0x2 - ands r0, r1 - cmp r0, 0 - bne _08063760 - movs r4, 0 - movs r1, 0x3 - mov r8, r1 - b _08063770 - .pool -_08063760: - movs r4, 0x3 - movs r0, 0x6 - mov r8, r0 - b _08063770 -_08063768: - movs r4, 0 - movs r1, 0x6 - mov r8, r1 - ldr r2, =gActiveBank -_08063770: - ldrb r0, [r2] - bl GetBankSide - lsls r0, 24 - ldr r1, =gEnemyParty - mov r9, r1 - cmp r0, 0 - bne _08063784 - ldr r0, =gPlayerParty - mov r9, r0 -_08063784: - adds r5, r4, 0 - cmp r5, r8 - bge _080637F2 -_0806378A: - movs r0, 0x64 - muls r0, r5 - mov r1, r9 - adds r4, r1, r0 - adds r0, r4, 0 - movs r1, 0x39 - bl GetMonData - cmp r0, 0 - beq _080637EC - adds r0, r4, 0 - movs r1, 0x41 - bl GetMonData - cmp r0, 0 - beq _080637EC - adds r0, r4, 0 - movs r1, 0x41 - bl GetMonData - movs r1, 0xCE - lsls r1, 1 - cmp r0, r1 - beq _080637EC - ldr r1, =gBattlePartyID - lsls r0, r7, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r5, r0 - beq _080637EC - lsls r0, r6, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r5, r0 - beq _080637EC - ldr r0, =gBattleStruct - ldr r1, [r0] - adds r0, r7, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r5, r0 - beq _080637EC - adds r0, r6, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r5, r0 - beq _080637EC - movs r0, 0x1 - add r10, r0 -_080637EC: - adds r5, 0x1 - cmp r5, r8 - blt _0806378A -_080637F2: - mov r1, r10 - cmp r1, 0 - beq _0806386C - bl ai_switch_perish_song - lsls r0, 24 - cmp r0, 0 - bne _08063852 - bl sub_8062C58 - lsls r0, 24 - cmp r0, 0 - bne _08063852 - bl sub_8062E54 - lsls r0, 24 - cmp r0, 0 - bne _08063852 - bl ai_switchout_natural_cure - lsls r0, 24 - cmp r0, 0 - bne _08063852 - movs r0, 0 - bl ai_has_super_effective_move_on_field - lsls r0, 24 - cmp r0, 0 - bne _0806386C - bl ai_is_too_invested_in_stat_buffs - lsls r0, 24 - cmp r0, 0 - bne _0806386C - movs r0, 0x8 - movs r1, 0x2 - bl ai_switchout_something - lsls r0, 24 - cmp r0, 0 - bne _08063852 - movs r0, 0x4 - movs r1, 0x3 - bl ai_switchout_something - lsls r0, 24 - cmp r0, 0 - beq _0806386C -_08063852: - movs r0, 0x1 - b _0806386E - .pool -_0806386C: - movs r0, 0 -_0806386E: - add sp, 0x4 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - thumb_func_end sub_8063614 - thumb_func_start sub_8063880 sub_8063880: @ 8063880 push {r4-r7,lr} @@ -1515,7 +34,7 @@ _080638A8: bne _080638B6 b _080639E6 _080638B6: - bl sub_8063614 + bl ShouldSwitch lsls r0, 24 cmp r0, 0 bne _080638C2 @@ -1659,7 +178,7 @@ _080639E6: lsls r2, 8 movs r0, 0x1 movs r1, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 _080639F8: pop {r3} mov r8, r3 @@ -2802,7 +1321,7 @@ _08064328: movs r0, 0x1 movs r1, 0x1 movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 ldr r0, =gActiveBank ldrb r0, [r0] lsrs r0, 1 diff --git a/asm/battle_controller_opponent.s b/asm/battle_controller_opponent.s index 48b7c7bf4..3fbeac5c1 100644 --- a/asm/battle_controller_opponent.s +++ b/asm/battle_controller_opponent.s @@ -5243,7 +5243,7 @@ sub_8061FB8: @ 8061FB8 movs r0, 0x1 movs r1, 0xA _08061FD6: - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 _08061FDA: bl OpponentBufferExecCompleted b _08062156 @@ -5347,7 +5347,7 @@ _0806209C: movs r0, 0x1 movs r1, 0xA adds r2, r4, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 b _08061FDA .pool _080620C4: @@ -5378,7 +5378,7 @@ _080620C6: movs r0, 0x1 movs r1, 0xA adds r2, r4, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 b _08062152 .pool _08062108: @@ -5401,7 +5401,7 @@ _08062108: orrs r2, r4 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 b _08062152 .pool _0806213C: @@ -5413,7 +5413,7 @@ _0806213C: orrs r2, r4 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 _08062152: bl OpponentBufferExecCompleted _08062156: diff --git a/asm/battle_controller_player.s b/asm/battle_controller_player.s index 94d4cdef0..e5381b4d8 100644 --- a/asm/battle_controller_player.s +++ b/asm/battle_controller_player.s @@ -226,7 +226,7 @@ _0805764C: movs r0, 0x1 movs r1, 0x3 movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 b _080577C2 _08057658: movs r0, 0x20 @@ -393,7 +393,7 @@ _080577B2: movs r1, 0xC _080577BC: movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 _080577C2: bl PlayerBufferExecCompleted b _080577E8 @@ -518,7 +518,7 @@ _080578A0: orrs r2, r0 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 ldrb r0, [r4] movs r1, 0x1 bl dp11b_obj_free @@ -1086,7 +1086,7 @@ _08057E14: _08057E1C: movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl PlayerBufferExecCompleted b _08057F9E .pool @@ -3413,7 +3413,7 @@ _080591FE: lsrs r2, 16 movs r0, 0x1 movs r1, 0xB - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 strb r5, [r4] bl battle_type_is_double lsls r0, 24 @@ -3686,7 +3686,7 @@ _08059430: lsrs r2, 16 movs r0, 0x1 movs r1, 0xB - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 strb r5, [r4] ldr r0, =sub_8059544 str r0, [r6] @@ -4291,14 +4291,14 @@ _08059A2C: movs r0, 0x1 movs r1, 0xE movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 b _08059A76 .pool _08059A6C: movs r0, 0x1 movs r1, 0xD movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 _08059A76: bl PlayerBufferExecCompleted _08059A7A: @@ -8658,7 +8658,7 @@ sub_805C158: @ 805C158 lsrs r2, 16 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl PlayerBufferExecCompleted _0805C194: pop {r0} @@ -9378,7 +9378,7 @@ sub_805C80C: @ 805C80C movs r0, 0x1 movs r1, 0 movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl PlayerBufferExecCompleted pop {r0} bx r0 diff --git a/asm/battle_controller_player_partner.s b/asm/battle_controller_player_partner.s index 0ddd2a924..269e5347b 100644 --- a/asm/battle_controller_player_partner.s +++ b/asm/battle_controller_player_partner.s @@ -645,7 +645,7 @@ _081BB2E2: lsrs r2, 16 movs r0, 0x1 movs r1, 0xB - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 strb r5, [r4] bl battle_type_is_double lsls r0, 24 @@ -918,7 +918,7 @@ _081BB514: lsrs r2, 16 movs r0, 0x1 movs r1, 0xB - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 strb r5, [r4] ldr r0, =sub_81BB628 str r0, [r6] @@ -5068,7 +5068,7 @@ _081BDB3C: orrs r2, r5 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl PlayerPartnerBufferExecCompleted pop {r4-r6} pop {r0} diff --git a/asm/battle_controller_recorded_opponent.s b/asm/battle_controller_recorded_opponent.s index 152dfca9c..c55bf641f 100644 --- a/asm/battle_controller_recorded_opponent.s +++ b/asm/battle_controller_recorded_opponent.s @@ -4754,7 +4754,7 @@ sub_8188EF0: @ 8188EF0 lsrs r1, 24 movs r0, 0x1 movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl RecordedOpponentBufferExecCompleted pop {r0} bx r0 @@ -4785,7 +4785,7 @@ sub_8188F20: @ 8188F20 lsrs r2, 16 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 b _08188F6C .pool _08188F48: @@ -4803,7 +4803,7 @@ _08188F48: orrs r2, r4 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 _08188F6C: bl RecordedOpponentBufferExecCompleted pop {r4,r5} diff --git a/asm/battle_controller_recorded_player.s b/asm/battle_controller_recorded_player.s index a2ea742b7..836ff5b95 100644 --- a/asm/battle_controller_recorded_player.s +++ b/asm/battle_controller_recorded_player.s @@ -4861,7 +4861,7 @@ sub_818C49C: @ 818C49C lsrs r1, 24 movs r0, 0x1 movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl RecordedPlayerBufferExecCompleted _0818C4C6: pop {r0} @@ -4897,7 +4897,7 @@ _0818C508: lsrs r1, 24 movs r0, 0x1 movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl RecordedPlayerBufferExecCompleted _0818C522: pop {r0} @@ -4929,7 +4929,7 @@ sub_818C538: @ 818C538 lsrs r2, 16 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 b _0818C584 .pool _0818C560: @@ -4947,7 +4947,7 @@ _0818C560: orrs r2, r4 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 _0818C584: bl RecordedPlayerBufferExecCompleted pop {r4,r5} diff --git a/asm/battle_controller_safari.s b/asm/battle_controller_safari.s index a5acca8c1..c3fbd0aad 100644 --- a/asm/battle_controller_safari.s +++ b/asm/battle_controller_safari.s @@ -103,13 +103,13 @@ _0815942A: movs r1, 0x7 _0815942E: movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 b _08159440 _08159436: movs r0, 0x1 movs r1, 0x8 movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 _08159440: bl SafariBufferExecCompleted b _0815954E diff --git a/asm/battle_controller_wally.s b/asm/battle_controller_wally.s index 64c8b4442..f80292bcc 100644 --- a/asm/battle_controller_wally.s +++ b/asm/battle_controller_wally.s @@ -153,7 +153,7 @@ _081684CE: movs r1, 0x9 _081684E4: movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl WallyBufferExecCompleted ldr r1, [r4] adds r1, 0x94 @@ -208,7 +208,7 @@ _08168540: movs r0, 0x1 movs r1, 0x1 movs r2, 0 - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl WallyBufferExecCompleted _08168564: pop {r4,r5} @@ -3777,7 +3777,7 @@ _0816A550: lsls r2, 1 movs r0, 0x1 movs r1, 0xA - bl dp01_build_cmdbuf_x21_a_bb + bl EmitCmd_x21 bl WallyBufferExecCompleted _0816A574: pop {r4} diff --git a/asm/rom3.s b/asm/rom3.s index bde493568..649097eb6 100644 --- a/asm/rom3.s +++ b/asm/rom3.s @@ -3259,8 +3259,8 @@ _0803413E: .pool thumb_func_end sub_8034110 - thumb_func_start dp01_build_cmdbuf_x21_a_bb -dp01_build_cmdbuf_x21_a_bb: @ 8034158 + thumb_func_start EmitCmd_x21 +EmitCmd_x21: @ 8034158 push {r4,lr} adds r4, r1, 0 lsls r0, 24 @@ -3280,7 +3280,7 @@ dp01_build_cmdbuf_x21_a_bb: @ 8034158 pop {r0} bx r0 .pool - thumb_func_end dp01_build_cmdbuf_x21_a_bb + thumb_func_end EmitCmd_x21 thumb_func_start dp01_build_cmdbuf_x22_a_three_bytes dp01_build_cmdbuf_x22_a_three_bytes: @ 8034184 diff --git a/include/battle_controllers.h b/include/battle_controllers.h index eea544b55..e36c95fbe 100644 --- a/include/battle_controllers.h +++ b/include/battle_controllers.h @@ -84,5 +84,6 @@ void Emit_x32(u8 bufferId); void EmitPrintString(u8 bufferId, u16 stringId); void EmitResetActionMoveSelection(u8 bufferId, u8 caseId); void EmitBallThrow(u8 bufferId, u8 caseId); +void EmitCmd_x21(u8 bufferId, u8 arg1, u16 arg2); #endif // GUARD_BATTLE_CONTROLLERS_H diff --git a/include/battle_script_commands.h b/include/battle_script_commands.h index ae0d20f28..590944f02 100644 --- a/include/battle_script_commands.h +++ b/include/battle_script_commands.h @@ -43,7 +43,7 @@ void AI_CalcDmg(u8 bankAtk, u8 bankDef); u8 TypeCalc(u16 move, u8 bankAtk, u8 bankDef); -u8 AI_TypeCalc(u16 move, u16 species, u8 ability); +u8 AI_TypeCalc(u16 move, u16 targetSpecies, u8 targetAbility); u8 BankGetTurnOrder(u8 bank); void SetMoveEffect(bool8 primary, u8 certain); void BattleDestroyCursorAt(u8 cursorPosition); diff --git a/ld_script.txt b/ld_script.txt index 3e47864f1..1da43ff9d 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -64,6 +64,7 @@ SECTIONS { asm/battle_controller_player.o(.text); asm/battle_7.o(.text); asm/battle_controller_opponent.o(.text); + src/battle_9.o(.text); asm/battle_9.o(.text); asm/battle_controller_linkopponent.o(.text); src/pokemon_1.o(.text); diff --git a/src/battle_9.c b/src/battle_9.c new file mode 100644 index 000000000..1b1a4ab7c --- /dev/null +++ b/src/battle_9.c @@ -0,0 +1,525 @@ +#include "global.h" +#include "battle.h" +#include "battle_controllers.h" +#include "abilities.h" +#include "moves.h" +#include "pokemon.h" +#include "species.h" +#include "rng.h" +#include "util.h" + +extern u8 gActiveBank; +extern u8 gAbsentBankFlags; +extern u32 gBattleTypeFlags; +extern u32 gStatuses3[BATTLE_BANKS_COUNT]; +extern struct BattlePokemon gBattleMons[BATTLE_BANKS_COUNT]; +extern u16 gBattlePartyID[BATTLE_BANKS_COUNT]; +extern u16 gUnknown_02024250[BATTLE_BANKS_COUNT]; +extern u8 gUnknown_02024270[BATTLE_BANKS_COUNT]; + +extern const struct BattleMove gBattleMoves[]; +extern const struct BaseStats gBaseStats[]; + +// this file's functions +bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng); +bool8 FindMonWithFlagsAndSuperEffective(u8 flags, u8 moduloPercent); + +bool8 ShouldSwitchIfPerishSong(void) +{ + if (gStatuses3[gActiveBank] & STATUS3_PERISH_SONG + && gDisableStructs[gActiveBank].perishSong1 == 0) + { + *(gBattleStruct->field_294 + gActiveBank) = 6; + EmitCmd_x21(1, 2, 0); + return TRUE; + } + + return FALSE; +} + +bool8 ShouldSwitchIfWonderGuard(void) +{ + u8 opposingIdentity; + u8 opposingBank; + u8 moveFlags; + s32 i, j; + s32 firstId; + s32 lastId; // + 1 + struct Pokemon *party = NULL; + u16 move; + + if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) + return FALSE; + + opposingIdentity = GetBankIdentity(gActiveBank) ^ BIT_SIDE; + + if (gBattleMons[GetBankByIdentity(opposingIdentity)].ability != ABILITY_WONDER_GUARD) + return FALSE; + + // check if pokemon has a super effective move + for (opposingBank = GetBankByIdentity(opposingIdentity), i = 0; i < 4; i++) + { + move = gBattleMons[gActiveBank].moves[i]; + if (move == MOVE_NONE) + continue; + + moveFlags = AI_TypeCalc(move, gBattleMons[opposingBank].species, gBattleMons[opposingBank].ability); + if (moveFlags & MOVESTATUS_SUPEREFFECTIVE) + return FALSE; + } + + // get party information + if (gBattleTypeFlags & (BATTLE_TYPE_TWO_OPPONENTS | BATTLE_TYPE_x800000)) + { + if ((gActiveBank & BIT_MON) == 0) + firstId = 0, lastId = 3; + else + firstId = 3, lastId = 6; + } + else + { + firstId = 0, lastId = 6; + } + + if (GetBankSide(gActiveBank) == SIDE_PLAYER) + party = gPlayerParty; + else + party = gEnemyParty; + + // find a pokemon in the party that has a super effective move + for (i = firstId; i < lastId; i++) + { + if (GetMonData(&party[i], MON_DATA_HP) == 0) + continue; + if (GetMonData(&party[i], MON_DATA_SPECIES2) == SPECIES_NONE) + continue; + if (GetMonData(&party[i], MON_DATA_SPECIES2) == SPECIES_EGG) + continue; + if (i == gBattlePartyID[gActiveBank]) + continue; + + GetMonData(&party[i], MON_DATA_SPECIES); // unused return value + GetMonData(&party[i], MON_DATA_ALT_ABILITY); // unused return value + + for (opposingBank = GetBankByIdentity(opposingIdentity), j = 0; j < 4; j++) + { + move = GetMonData(&party[i], MON_DATA_MOVE1 + j); + if (move == MOVE_NONE) + continue; + + moveFlags = AI_TypeCalc(move, gBattleMons[opposingBank].species, gBattleMons[opposingBank].ability); + if (moveFlags & MOVESTATUS_SUPEREFFECTIVE && Random() % 3 < 2) + { + // we found a mon + *(gBattleStruct->field_294 + gActiveBank) = i; + EmitCmd_x21(1, 2, 0); + return TRUE; + } + } + } + + return FALSE; // at this point there is not a single pokemon in the party that has a super effective move against a pokemon with wonder guard +} + +bool8 sub_8062E54(void) +{ + u8 bankIn1, bankIn2; + u8 absorbingTypeAbility; + s32 firstId; + s32 lastId; // + 1 + struct Pokemon *party; + s32 i; + + if (HasSuperEffectiveMoveAgainstOpponents(TRUE) && Random() % 3 != 0) + return FALSE; + if (gUnknown_02024250[gActiveBank] == 0) + return FALSE; + if (gUnknown_02024250[gActiveBank] == 0xFFFF) + return FALSE; + if (gBattleMoves[gUnknown_02024250[gActiveBank]].power == 0) + return FALSE; + + if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) + { + bankIn1 = gActiveBank; + if (gAbsentBankFlags & gBitTable[GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON)]) + bankIn2 = gActiveBank; + else + bankIn2 = GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON); + } + else + { + bankIn1 = gActiveBank; + bankIn2 = gActiveBank; + } + + if (gBattleMoves[gUnknown_02024250[gActiveBank]].type == TYPE_FIRE) + absorbingTypeAbility = ABILITY_FLASH_FIRE; + else if (gBattleMoves[gUnknown_02024250[gActiveBank]].type == TYPE_WATER) + absorbingTypeAbility = ABILITY_WATER_ABSORB; + else if (gBattleMoves[gUnknown_02024250[gActiveBank]].type == TYPE_ELECTRIC) + absorbingTypeAbility = ABILITY_VOLT_ABSORB; + else + return FALSE; + + if (gBattleMons[gActiveBank].ability == absorbingTypeAbility) + return FALSE; + + if (gBattleTypeFlags & (BATTLE_TYPE_TWO_OPPONENTS | BATTLE_TYPE_x800000)) + { + if ((gActiveBank & BIT_MON) == 0) + firstId = 0, lastId = 3; + else + firstId = 3, lastId = 6; + } + else + { + firstId = 0, lastId = 6; + } + + if (GetBankSide(gActiveBank) == SIDE_PLAYER) + party = gPlayerParty; + else + party = gEnemyParty; + + for (i = firstId; i < lastId; i++) + { + u16 species; + u8 monAbility; + + if (GetMonData(&party[i], MON_DATA_HP) == 0) + continue; + if (GetMonData(&party[i], MON_DATA_SPECIES2) == SPECIES_NONE) + continue; + if (GetMonData(&party[i], MON_DATA_SPECIES2) == SPECIES_EGG) + continue; + if (i == gBattlePartyID[bankIn1]) + continue; + if (i == gBattlePartyID[bankIn2]) + continue; + if (i == *(gBattleStruct->field_5C + bankIn1)) + continue; + if (i == *(gBattleStruct->field_5C + bankIn2)) + continue; + + species = GetMonData(&party[i], MON_DATA_SPECIES); + if (GetMonData(&party[i], MON_DATA_ALT_ABILITY) != 0) + monAbility = gBaseStats[species].ability2; + else + monAbility = gBaseStats[species].ability1; + + if (absorbingTypeAbility == monAbility && Random() & 1) + { + // we found a mon + *(gBattleStruct->field_294 + gActiveBank) = i; + EmitCmd_x21(1, 2, 0); + return TRUE; + } + } + + return FALSE; +} + +bool8 ShouldSwitchIfNaturalCure(void) +{ + if (!(gBattleMons[gActiveBank].status1 & STATUS_SLEEP)) + return FALSE; + if (gBattleMons[gActiveBank].ability != ABILITY_NATURAL_CURE) + return FALSE; + if (gBattleMons[gActiveBank].hp < gBattleMons[gActiveBank].maxHP / 2) + return FALSE; + + if ((gUnknown_02024250[gActiveBank] == 0 || gUnknown_02024250[gActiveBank] == 0xFFFF) && Random() & 1) + { + *(gBattleStruct->field_294 + gActiveBank) = 6; + EmitCmd_x21(1, 2, 0); + return TRUE; + } + else if (gBattleMoves[gUnknown_02024250[gActiveBank]].power == 0 && Random() & 1) + { + *(gBattleStruct->field_294 + gActiveBank) = 6; + EmitCmd_x21(1, 2, 0); + return TRUE; + } + + if (FindMonWithFlagsAndSuperEffective(MOVESTATUS_NOTAFFECTED, 1)) + return TRUE; + if (FindMonWithFlagsAndSuperEffective(MOVESTATUS_NOTVERYEFFECTIVE, 1)) + return TRUE; + if (Random() & 1) + { + *(gBattleStruct->field_294 + gActiveBank) = 6; + EmitCmd_x21(1, 2, 0); + return TRUE; + } + + return FALSE; +} + +bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng) +{ + u8 opposingIdentity; + u8 opposingBank; + s32 i; + u8 moveFlags; + u16 move; + + opposingIdentity = GetBankIdentity(gActiveBank) ^ BIT_SIDE; + opposingBank = GetBankByIdentity(opposingIdentity); + + if (!(gAbsentBankFlags & gBitTable[opposingBank])) + { + for (i = 0; i < 4; i++) + { + move = gBattleMons[gActiveBank].moves[i]; + if (move == MOVE_NONE) + continue; + + moveFlags = AI_TypeCalc(move, gBattleMons[opposingBank].species, gBattleMons[opposingBank].ability); + if (moveFlags & MOVESTATUS_SUPEREFFECTIVE) + { + if (noRng) + return TRUE; + if (Random() % 10 != 0) + return TRUE; + } + } + } + if (!(gBattleTypeFlags & BATTLE_TYPE_DOUBLE)) + return FALSE; + + opposingBank = GetBankByIdentity(opposingIdentity ^ BIT_MON); + + if (!(gAbsentBankFlags & gBitTable[opposingBank])) + { + for (i = 0; i < 4; i++) + { + move = gBattleMons[gActiveBank].moves[i]; + if (move == MOVE_NONE) + continue; + + moveFlags = AI_TypeCalc(move, gBattleMons[opposingBank].species, gBattleMons[opposingBank].ability); + if (moveFlags & MOVESTATUS_SUPEREFFECTIVE) + { + if (noRng) + return TRUE; + if (Random() % 10 != 0) + return TRUE; + } + } + } + + return FALSE; +} + +bool8 AreStatsRaised(void) +{ + u8 buffedStatsValue = 0; + s32 i; + + for (i = 0; i < BATTLE_STATS_NO; i++) + { + if (gBattleMons[gActiveBank].statStages[i] > 6) + buffedStatsValue += gBattleMons[gActiveBank].statStages[i] - 6; + } + + return (buffedStatsValue > 3); +} + +bool8 FindMonWithFlagsAndSuperEffective(u8 flags, u8 moduloPercent) +{ + u8 bankIn1, bankIn2; + s32 firstId; + s32 lastId; // + 1 + struct Pokemon *party; + s32 i, j; + u16 move; + u8 moveFlags; + + if (gUnknown_02024250[gActiveBank] == 0) + return FALSE; + if (gUnknown_02024250[gActiveBank] == 0xFFFF) + return FALSE; + if (gUnknown_02024270[gActiveBank] == 0xFF) + return FALSE; + if (gBattleMoves[gUnknown_02024250[gActiveBank]].power == 0) + return FALSE; + + if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) + { + bankIn1 = gActiveBank; + if (gAbsentBankFlags & gBitTable[GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON)]) + bankIn2 = gActiveBank; + else + bankIn2 = GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON); + } + else + { + bankIn1 = gActiveBank; + bankIn2 = gActiveBank; + } + + if (gBattleTypeFlags & (BATTLE_TYPE_TWO_OPPONENTS | BATTLE_TYPE_x800000)) + { + if ((gActiveBank & BIT_MON) == 0) + firstId = 0, lastId = 3; + else + firstId = 3, lastId = 6; + } + else + { + firstId = 0, lastId = 6; + } + + if (GetBankSide(gActiveBank) == SIDE_PLAYER) + party = gPlayerParty; + else + party = gEnemyParty; + + for (i = firstId; i < lastId; i++) + { + u16 species; + u8 monAbility; + + if (GetMonData(&party[i], MON_DATA_HP) == 0) + continue; + if (GetMonData(&party[i], MON_DATA_SPECIES2) == SPECIES_NONE) + continue; + if (GetMonData(&party[i], MON_DATA_SPECIES2) == SPECIES_EGG) + continue; + if (i == gBattlePartyID[bankIn1]) + continue; + if (i == gBattlePartyID[bankIn2]) + continue; + if (i == *(gBattleStruct->field_5C + bankIn1)) + continue; + if (i == *(gBattleStruct->field_5C + bankIn2)) + continue; + + species = GetMonData(&party[i], MON_DATA_SPECIES); + if (GetMonData(&party[i], MON_DATA_ALT_ABILITY) != 0) + monAbility = gBaseStats[species].ability2; + else + monAbility = gBaseStats[species].ability1; + + moveFlags = AI_TypeCalc(gUnknown_02024250[gActiveBank], species, monAbility); + if (moveFlags & flags) + { + bankIn1 = gUnknown_02024270[gActiveBank]; + + for (j = 0; j < 4; j++) + { + move = GetMonData(&party[i], MON_DATA_MOVE1 + j); + if (move == 0) + continue; + + moveFlags = AI_TypeCalc(move, gBattleMons[bankIn1].species, gBattleMons[bankIn1].ability); + if (moveFlags & MOVESTATUS_SUPEREFFECTIVE && Random() % moduloPercent == 0) + { + *(gBattleStruct->field_294 + gActiveBank) = i; + EmitCmd_x21(1, 2, 0); + return TRUE; + } + } + } + } + + return FALSE; +} + +bool8 ShouldSwitch(void) +{ + u8 bankIn1, bankIn2; + s32 firstId; + s32 lastId; // + 1 + struct Pokemon *party; + s32 i; + s32 availableToSwitch; + + if (gBattleMons[gActiveBank].status2 & (STATUS2_WRAPPED | STATUS2_ESCAPE_PREVENTION)) + return FALSE; + if (gStatuses3[gActiveBank] & STATUS3_ROOTED) + return FALSE; + if (AbilityBattleEffects(ABILITYEFFECT_CHECK_OTHER_SIDE, gActiveBank, ABILITY_SHADOW_TAG, 0, 0)) + return FALSE; + if (AbilityBattleEffects(ABILITYEFFECT_CHECK_OTHER_SIDE, gActiveBank, ABILITY_ARENA_TRAP, 0, 0)) + return FALSE; // misses the flying or levitate check + if (AbilityBattleEffects(ABILITYEFFECT_FIELD_SPORT, gActiveBank, ABILITY_MAGNET_PULL, 0, 0) + && (gBattleMons[gActiveBank].type1 == TYPE_STEEL || gBattleMons[gActiveBank].type2 == TYPE_STEEL)) + return FALSE; + if (gBattleTypeFlags & BATTLE_TYPE_ARENA) + return FALSE; + + availableToSwitch = 0; + if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) + { + bankIn1 = gActiveBank; + if (gAbsentBankFlags & gBitTable[GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON)]) + bankIn2 = gActiveBank; + else + bankIn2 = GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON); + } + else + { + bankIn1 = gActiveBank; + bankIn2 = gActiveBank; + } + + if (gBattleTypeFlags & (BATTLE_TYPE_TWO_OPPONENTS | BATTLE_TYPE_x800000)) + { + if ((gActiveBank & BIT_MON) == 0) + firstId = 0, lastId = 3; + else + firstId = 3, lastId = 6; + } + else + { + firstId = 0, lastId = 6; + } + + if (GetBankSide(gActiveBank) == SIDE_PLAYER) + party = gPlayerParty; + else + party = gEnemyParty; + + for (i = firstId; i < lastId; i++) + { + if (GetMonData(&party[i], MON_DATA_HP) == 0) + continue; + if (GetMonData(&party[i], MON_DATA_SPECIES2) == SPECIES_NONE) + continue; + if (GetMonData(&party[i], MON_DATA_SPECIES2) == SPECIES_EGG) + continue; + if (i == gBattlePartyID[bankIn1]) + continue; + if (i == gBattlePartyID[bankIn2]) + continue; + if (i == *(gBattleStruct->field_5C + bankIn1)) + continue; + if (i == *(gBattleStruct->field_5C + bankIn2)) + continue; + + availableToSwitch++; + } + + if (availableToSwitch == 0) + return FALSE; + if (ShouldSwitchIfPerishSong()) + return TRUE; + if (ShouldSwitchIfWonderGuard()) + return TRUE; + if (sub_8062E54()) + return TRUE; + if (ShouldSwitchIfNaturalCure()) + return TRUE; + if (HasSuperEffectiveMoveAgainstOpponents(FALSE)) + return FALSE; + if (AreStatsRaised()) + return FALSE; + if (FindMonWithFlagsAndSuperEffective(MOVESTATUS_NOTAFFECTED, 2)) + return TRUE; + if (FindMonWithFlagsAndSuperEffective(MOVESTATUS_NOTVERYEFFECTIVE, 3)) + return TRUE; + + return FALSE; +} diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 17b0e2ff9..50ae5bafe 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -1772,11 +1772,11 @@ u8 TypeCalc(u16 move, u8 bankAtk, u8 bankDef) return flags; } -u8 AI_TypeCalc(u16 move, u16 species, u8 ability) +u8 AI_TypeCalc(u16 move, u16 targetSpecies, u8 targetAbility) { s32 i = 0; u8 flags = 0; - u8 type1 = gBaseStats[species].type1, type2 = gBaseStats[species].type2; + u8 type1 = gBaseStats[targetSpecies].type1, type2 = gBaseStats[targetSpecies].type2; u8 moveType; if (move == MOVE_STRUGGLE) @@ -1784,7 +1784,7 @@ u8 AI_TypeCalc(u16 move, u16 species, u8 ability) moveType = gBattleMoves[move].type; - if (ability == ABILITY_LEVITATE && moveType == TYPE_GROUND) + if (targetAbility == ABILITY_LEVITATE && moveType == TYPE_GROUND) { flags = MOVESTATUS_MISSED | MOVESTATUS_NOTAFFECTED; } @@ -1809,7 +1809,7 @@ u8 AI_TypeCalc(u16 move, u16 species, u8 ability) i += 3; } } - if (ability == ABILITY_WONDER_GUARD + if (targetAbility == ABILITY_WONDER_GUARD && (!(flags & MOVESTATUS_SUPEREFFECTIVE) || ((flags & (MOVESTATUS_SUPEREFFECTIVE | MOVESTATUS_NOTVERYEFFECTIVE)) == (MOVESTATUS_SUPEREFFECTIVE | MOVESTATUS_NOTVERYEFFECTIVE))) && gBattleMoves[move].power) flags |= MOVESTATUS_NOTAFFECTED; From 6dfe8ced2cb516202f2934bf02762503f55fd8da Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Tue, 10 Oct 2017 21:45:07 +0200 Subject: [PATCH 2/3] ai switching basically done --- asm/battle_9.s | 726 +------------------------ asm/battle_controller_opponent.s | 4 +- asm/battle_controller_player_partner.s | 4 +- include/battle.h | 1 + include/battle_ai_switch_items.h | 7 + src/battle_9.c | 299 +++++++++- 6 files changed, 299 insertions(+), 742 deletions(-) create mode 100644 include/battle_ai_switch_items.h diff --git a/asm/battle_9.s b/asm/battle_9.s index 6d77d1593..0cf529bcc 100644 --- a/asm/battle_9.s +++ b/asm/battle_9.s @@ -5,726 +5,6 @@ .text - thumb_func_start sub_8063880 -sub_8063880: @ 8063880 - push {r4-r7,lr} - mov r7, r8 - push {r7} - ldr r4, =gActiveBank - ldrb r0, [r4] - bl GetBankIdentity - lsls r0, 24 - lsrs r6, r0, 24 - adds r5, r6, 0 - ldrb r0, [r4] - bl GetBankSide - lsls r0, 24 - ldr r1, =gEnemyParty - mov r8, r1 - cmp r0, 0 - bne _080638A8 - ldr r2, =gPlayerParty - mov r8, r2 -_080638A8: - ldr r7, =gBattleTypeFlags - ldr r0, [r7] - movs r1, 0x8 - ands r0, r1 - cmp r0, 0 - bne _080638B6 - b _080639E6 -_080638B6: - bl ShouldSwitch - lsls r0, 24 - cmp r0, 0 - bne _080638C2 - b _080639DC -_080638C2: - ldrb r0, [r4] - ldr r2, =gBattleStruct - ldr r1, [r2] - adds r0, r1 - movs r1, 0xA5 - lsls r1, 2 - adds r0, r1 - ldrb r0, [r0] - cmp r0, 0x6 - bne _080639B8 - bl sub_8063A90 - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0x6 - bne _080639A6 - ldr r0, [r7] - movs r1, 0x1 - ands r0, r1 - cmp r0, 0 - bne _08063910 - adds r0, r6, 0 - bl GetBankByIdentity - lsls r0, 24 - lsrs r5, r0, 24 - adds r6, r5, 0 - b _08063928 - .pool -_08063910: - adds r0, r5, 0 - bl GetBankByIdentity - lsls r0, 24 - lsrs r6, r0, 24 - movs r0, 0x2 - eors r5, r0 - adds r0, r5, 0 - bl GetBankByIdentity - lsls r0, 24 - lsrs r5, r0, 24 -_08063928: - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - ldr r1, =0x00808000 - ands r0, r1 - cmp r0, 0 - beq _08063958 - ldr r0, =gActiveBank - ldrb r1, [r0] - movs r0, 0x2 - ands r0, r1 - cmp r0, 0 - bne _08063954 - movs r0, 0 - movs r7, 0x3 - b _0806395C - .pool -_08063954: - movs r0, 0x3 - b _0806395A -_08063958: - movs r0, 0 -_0806395A: - movs r7, 0x6 -_0806395C: - adds r4, r0, 0 - cmp r4, r7 - bge _080639A6 -_08063962: - movs r0, 0x64 - muls r0, r4 - add r0, r8 - movs r1, 0x39 - bl GetMonData - cmp r0, 0 - beq _080639A0 - ldr r1, =gBattlePartyID - lsls r0, r6, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r4, r0 - beq _080639A0 - lsls r0, r5, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r4, r0 - beq _080639A0 - ldr r0, =gBattleStruct - ldr r1, [r0] - adds r0, r6, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r4, r0 - beq _080639A0 - adds r0, r5, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r4, r0 - bne _080639A6 -_080639A0: - adds r4, 0x1 - cmp r4, r7 - blt _08063962 -_080639A6: - ldr r0, =gActiveBank - ldrb r0, [r0] - ldr r2, =gBattleStruct - ldr r1, [r2] - adds r0, r1 - movs r1, 0xA5 - lsls r1, 2 - adds r0, r1 - strb r4, [r0] -_080639B8: - ldr r0, =gActiveBank - ldrb r1, [r0] - ldr r0, [r2] - adds r0, r1, r0 - adds r1, r0, 0 - adds r1, 0x5C - movs r2, 0xA5 - lsls r2, 2 - adds r0, r2 - ldrb r0, [r0] - strb r0, [r1] - b _080639F8 - .pool -_080639DC: - bl sub_8063EE0 - lsls r0, 24 - cmp r0, 0 - bne _080639F8 -_080639E6: - ldr r0, =gActiveBank - ldrb r0, [r0] - movs r2, 0x1 - eors r2, r0 - lsls r2, 8 - movs r0, 0x1 - movs r1, 0 - bl EmitCmd_x21 -_080639F8: - pop {r3} - mov r8, r3 - pop {r4-r7} - pop {r0} - bx r0 - .pool - thumb_func_end sub_8063880 - - thumb_func_start sub_8063A08 -sub_8063A08: @ 8063A08 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x4 - adds r6, r3, 0 - lsls r0, 24 - lsrs r0, 24 - str r0, [sp] - lsls r1, 24 - lsrs r1, 24 - mov r9, r1 - lsls r2, 24 - lsrs r2, 24 - mov r8, r2 - movs r7, 0 - ldr r0, =gTypeEffectiveness - mov r10, r0 - ldrb r0, [r0] - cmp r0, 0xFF - beq _08063A7A - mov r5, r10 -_08063A36: - ldrb r0, [r5] - cmp r0, 0xFE - beq _08063A6C - ldr r1, [sp] - cmp r0, r1 - bne _08063A6C - ldrb r4, [r5, 0x1] - cmp r4, r9 - bne _08063A56 - ldrb r1, [r6] - ldrb r0, [r5, 0x2] - muls r0, r1 - movs r1, 0xA - bl __divsi3 - strb r0, [r6] -_08063A56: - cmp r4, r8 - bne _08063A6C - cmp r9, r8 - beq _08063A6C - ldrb r1, [r6] - ldrb r0, [r5, 0x2] - muls r0, r1 - movs r1, 0xA - bl __divsi3 - strb r0, [r6] -_08063A6C: - adds r5, 0x3 - adds r7, 0x3 - mov r1, r10 - adds r0, r7, r1 - ldrb r0, [r0] - cmp r0, 0xFF - bne _08063A36 -_08063A7A: - add sp, 0x4 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r0} - bx r0 - .pool - thumb_func_end sub_8063A08 - - thumb_func_start sub_8063A90 -sub_8063A90: @ 8063A90 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x24 - ldr r4, =gActiveBank - ldrb r2, [r4] - ldr r0, =gBattleStruct - ldr r0, [r0] - adds r0, r2, r0 - adds r1, r0, 0 - adds r1, 0x5C - ldrb r0, [r1] - cmp r0, 0x6 - beq _08063AB2 - b _08063E50 -_08063AB2: - ldr r0, =gBattleTypeFlags - ldr r1, [r0] - movs r0, 0x80 - lsls r0, 11 - ands r0, r1 - cmp r0, 0 - beq _08063AE0 - ldr r1, =gBattlePartyID - lsls r0, r2, 1 - adds r0, r1 - ldrb r0, [r0] - adds r0, 0x1 - lsls r0, 24 - lsrs r0, 24 - b _08063E50 - .pool -_08063AE0: - movs r0, 0x1 - ands r1, r0 - cmp r1, 0 - beq _08063B6C - str r2, [sp, 0x10] - adds r0, r2, 0 - bl GetBankIdentity - movs r5, 0x2 - eors r0, r5 - lsls r0, 24 - lsrs r0, 24 - bl GetBankByIdentity - ldr r1, =gAbsentBankFlags - ldrb r1, [r1] - ldr r2, =gBitTable - lsls r0, 24 - lsrs r0, 22 - adds r0, r2 - ldr r0, [r0] - ands r1, r0 - cmp r1, 0 - beq _08063B20 - ldrb r4, [r4] - str r4, [sp, 0x14] - b _08063B36 - .pool -_08063B20: - ldrb r0, [r4] - bl GetBankIdentity - eors r0, r5 - lsls r0, 24 - lsrs r0, 24 - bl GetBankByIdentity - lsls r0, 24 - lsrs r0, 24 - str r0, [sp, 0x14] -_08063B36: - bl Random - movs r3, 0x2 - ands r0, r3 - lsls r0, 24 - lsrs r0, 24 - str r0, [sp, 0x4] - ldr r0, =gAbsentBankFlags - ldrb r1, [r0] - ldr r2, =gBitTable - ldr r4, [sp, 0x4] - lsls r0, r4, 2 - adds r0, r2 - ldr r0, [r0] - ands r1, r0 - cmp r1, 0 - beq _08063B8A - eors r4, r3 - lsls r0, r4, 24 - lsrs r0, 24 - str r0, [sp, 0x4] - b _08063B8A - .pool -_08063B6C: - adds r0, r2, 0 - bl GetBankIdentity - movs r1, 0x1 - eors r0, r1 - lsls r0, 24 - lsrs r0, 24 - bl GetBankByIdentity - lsls r0, 24 - lsrs r0, 24 - str r0, [sp, 0x4] - ldrb r4, [r4] - str r4, [sp, 0x14] - str r4, [sp, 0x10] -_08063B8A: - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - ldr r1, =0x00808000 - ands r0, r1 - cmp r0, 0 - beq _08063BC2 - ldr r2, =gActiveBank - ldrb r1, [r2] - movs r0, 0x2 - ands r0, r1 - cmp r0, 0 - bne _08063BB8 - movs r0, 0 - str r0, [sp, 0x18] - movs r1, 0x3 - str r1, [sp, 0x1C] - b _08063BCC - .pool -_08063BB8: - movs r4, 0x3 - str r4, [sp, 0x18] - movs r0, 0x6 - str r0, [sp, 0x1C] - b _08063BCC -_08063BC2: - movs r1, 0 - str r1, [sp, 0x18] - movs r2, 0x6 - str r2, [sp, 0x1C] - ldr r2, =gActiveBank -_08063BCC: - ldrb r0, [r2] - bl GetBankSide - lsls r0, 24 - ldr r4, =gEnemyParty - str r4, [sp, 0x20] - cmp r0, 0 - bne _08063BE0 - ldr r0, =gPlayerParty - str r0, [sp, 0x20] -_08063BE0: - movs r1, 0 - mov r9, r1 -_08063BE4: - movs r2, 0 - str r2, [sp, 0x8] - movs r4, 0x6 - str r4, [sp, 0xC] - ldr r7, [sp, 0x18] - ldr r0, [sp, 0x1C] - cmp r7, r0 - blt _08063BF6 - b _08063CF2 -_08063BF6: - lsls r0, r7, 2 - ldr r1, =gBitTable - adds r0, r1 - mov r8, r0 - mov r10, sp -_08063C00: - movs r0, 0x64 - muls r0, r7 - ldr r2, [sp, 0x20] - adds r4, r2, r0 - adds r0, r4, 0 - movs r1, 0xB - bl GetMonData - lsls r0, 16 - lsrs r5, r0, 16 - cmp r5, 0 - beq _08063CD8 - adds r0, r4, 0 - movs r1, 0x39 - bl GetMonData - cmp r0, 0 - beq _08063CD8 - mov r4, r8 - ldr r0, [r4] - mov r1, r9 - ands r0, r1 - cmp r0, 0 - bne _08063CD8 - ldr r1, =gBattlePartyID - ldr r2, [sp, 0x10] - lsls r0, r2, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r0, r7 - beq _08063CD8 - ldr r4, [sp, 0x14] - lsls r0, r4, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r0, r7 - beq _08063CD8 - ldr r0, =gBattleStruct - ldr r1, [r0] - adds r0, r2, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r7, r0 - beq _08063CD8 - adds r0, r4, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r7, r0 - beq _08063CD8 - ldr r1, =gBaseStats - lsls r0, r5, 3 - subs r0, r5 - lsls r0, 2 - adds r0, r1 - ldrb r5, [r0, 0x6] - ldrb r6, [r0, 0x7] - movs r0, 0xA - mov r1, r10 - strb r0, [r1] - movs r0, 0x58 - ldr r2, [sp, 0x4] - adds r4, r2, 0 - muls r4, r0 - ldr r0, =gBattleMons - adds r4, r0 - adds r0, r4, 0 - adds r0, 0x21 - ldrb r0, [r0] - adds r1, r5, 0 - adds r2, r6, 0 - mov r3, sp - bl sub_8063A08 - adds r4, 0x22 - ldrb r0, [r4] - adds r1, r5, 0 - adds r2, r6, 0 - mov r3, sp - bl sub_8063A08 - ldr r2, [sp, 0x8] - mov r1, r10 - ldrb r1, [r1] - cmp r2, r1 - bcs _08063CE6 - mov r2, r10 - ldrb r2, [r2] - str r2, [sp, 0x8] - lsls r0, r7, 24 - lsrs r0, 24 - str r0, [sp, 0xC] - b _08063CE6 - .pool -_08063CD8: - mov r4, r8 - ldr r0, [r4] - mov r1, r9 - orrs r1, r0 - lsls r0, r1, 24 - lsrs r0, 24 - mov r9, r0 -_08063CE6: - movs r2, 0x4 - add r8, r2 - adds r7, 0x1 - ldr r4, [sp, 0x1C] - cmp r7, r4 - blt _08063C00 -_08063CF2: - ldr r0, [sp, 0xC] - cmp r0, 0x6 - beq _08063D54 - movs r7, 0 - movs r0, 0x64 - ldr r1, [sp, 0xC] - adds r5, r1, 0 - muls r5, r0 -_08063D02: - adds r1, r7, 0 - adds r1, 0xD - ldr r2, [sp, 0x20] - adds r0, r2, r5 - bl GetMonData - lsls r0, 16 - lsrs r4, r0, 16 - cmp r4, 0 - beq _08063D2A - ldr r0, =gActiveBank - ldrb r1, [r0] - adds r0, r4, 0 - ldr r2, [sp, 0x4] - bl TypeCalc - movs r1, 0x2 - ands r1, r0 - cmp r1, 0 - bne _08063D30 -_08063D2A: - adds r7, 0x1 - cmp r7, 0x3 - ble _08063D02 -_08063D30: - cmp r7, 0x4 - beq _08063D36 - b _08063E4E -_08063D36: - ldr r4, [sp, 0xC] - lsls r0, r4, 2 - ldr r1, =gBitTable - adds r0, r1 - ldr r0, [r0] - mov r2, r9 - orrs r2, r0 - lsls r0, r2, 24 - lsrs r0, 24 - mov r9, r0 - b _08063D58 - .pool -_08063D54: - movs r4, 0x3F - mov r9, r4 -_08063D58: - mov r0, r9 - cmp r0, 0x3F - beq _08063D60 - b _08063BE4 -_08063D60: - ldr r0, =gDynamicBasePower - movs r1, 0 - strh r1, [r0] - ldr r0, =gBattleStruct - ldr r0, [r0] - strb r1, [r0, 0x13] - ldr r0, =gBattleScripting - movs r2, 0x1 - strb r2, [r0, 0xE] - ldr r0, =gBattleMoveFlags - strb r1, [r0] - ldr r0, =gCritMultiplier - strb r2, [r0] - movs r1, 0 - str r1, [sp, 0x8] - movs r2, 0x6 - str r2, [sp, 0xC] - ldr r7, [sp, 0x18] - ldr r4, [sp, 0x1C] - cmp r7, r4 - bge _08063E4E -_08063D8A: - movs r0, 0x64 - adds r6, r7, 0 - muls r6, r0 - ldr r0, [sp, 0x20] - adds r4, r0, r6 - adds r0, r4, 0 - movs r1, 0xB - bl GetMonData - lsls r0, 16 - adds r1, r7, 0x1 - mov r10, r1 - cmp r0, 0 - beq _08063E46 - adds r0, r4, 0 - movs r1, 0x39 - bl GetMonData - cmp r0, 0 - beq _08063E46 - ldr r1, =gBattlePartyID - ldr r2, [sp, 0x10] - lsls r0, r2, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r0, r7 - beq _08063E46 - ldr r4, [sp, 0x14] - lsls r0, r4, 1 - adds r0, r1 - ldrh r0, [r0] - cmp r0, r7 - beq _08063E46 - ldr r0, =gBattleStruct - ldr r1, [r0] - adds r0, r2, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r7, r0 - beq _08063E46 - adds r0, r4, r1 - adds r0, 0x5C - ldrb r0, [r0] - cmp r7, r0 - beq _08063E46 - movs r5, 0 - mov r9, r6 - ldr r0, =gBattleMoveDamage - mov r8, r0 - ldr r6, =gActiveBank -_08063DEE: - adds r1, r5, 0 - adds r1, 0xD - ldr r0, [sp, 0x20] - add r0, r9 - bl GetMonData - lsls r0, 16 - lsrs r4, r0, 16 - movs r0, 0 - mov r1, r8 - str r0, [r1] - cmp r4, 0 - beq _08063E2A - ldr r0, =gBattleMoves - lsls r1, r4, 1 - adds r1, r4 - lsls r1, 2 - adds r1, r0 - ldrb r0, [r1, 0x1] - cmp r0, 0x1 - beq _08063E2A - ldrb r0, [r6] - ldr r1, [sp, 0x4] - bl AI_CalcDmg - ldrb r1, [r6] - adds r0, r4, 0 - ldr r2, [sp, 0x4] - bl TypeCalc -_08063E2A: - mov r2, r8 - ldr r0, [r2] - ldr r4, [sp, 0x8] - cmp r4, r0 - bge _08063E40 - lsls r0, 24 - lsrs r0, 24 - str r0, [sp, 0x8] - lsls r0, r7, 24 - lsrs r0, 24 - str r0, [sp, 0xC] -_08063E40: - adds r5, 0x1 - cmp r5, 0x3 - ble _08063DEE -_08063E46: - mov r7, r10 - ldr r0, [sp, 0x1C] - cmp r7, r0 - blt _08063D8A -_08063E4E: - ldr r0, [sp, 0xC] -_08063E50: - add sp, 0x24 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8063A90 thumb_func_start ai_identify_item_effect ai_identify_item_effect: @ 8063E84 @@ -783,8 +63,8 @@ _08063EDA: bx r1 thumb_func_end ai_identify_item_effect - thumb_func_start sub_8063EE0 -sub_8063EE0: @ 8063EE0 + thumb_func_start ShouldUseItem +ShouldUseItem: @ 8063EE0 push {r4-r7,lr} mov r7, r10 mov r6, r9 @@ -1361,6 +641,6 @@ _0806437E: pop {r4-r7} pop {r1} bx r1 - thumb_func_end sub_8063EE0 + thumb_func_end ShouldUseItem .align 2, 0 @ Don't pad with nop. diff --git a/asm/battle_controller_opponent.s b/asm/battle_controller_opponent.s index 3fbeac5c1..896d86d3a 100644 --- a/asm/battle_controller_opponent.s +++ b/asm/battle_controller_opponent.s @@ -5212,7 +5212,7 @@ sub_8061F90: @ 8061F90 thumb_func_start sub_8061F9C sub_8061F9C: @ 8061F9C push {lr} - bl sub_8063880 + bl AI_TrySwitchOrUseItem bl OpponentBufferExecCompleted pop {r0} bx r0 @@ -5456,7 +5456,7 @@ sub_8062188: @ 8062188 ldrb r0, [r1] cmp r0, 0x6 bne _08062254 - bl sub_8063A90 + bl GetMostSuitableMonToSwitchInto lsls r0, 24 lsrs r4, r0, 24 cmp r4, 0x6 diff --git a/asm/battle_controller_player_partner.s b/asm/battle_controller_player_partner.s index 269e5347b..8c080500e 100644 --- a/asm/battle_controller_player_partner.s +++ b/asm/battle_controller_player_partner.s @@ -4990,7 +4990,7 @@ sub_81BDAA0: @ 81BDAA0 thumb_func_start sub_81BDAAC sub_81BDAAC: @ 81BDAAC push {lr} - bl sub_8063880 + bl AI_TrySwitchOrUseItem bl PlayerPartnerBufferExecCompleted pop {r0} bx r0 @@ -5087,7 +5087,7 @@ sub_81BDB70: @ 81BDB70 thumb_func_start sub_81BDB7C sub_81BDB7C: @ 81BDB7C push {r4-r6,lr} - bl sub_8063A90 + bl GetMostSuitableMonToSwitchInto lsls r0, 24 lsrs r4, r0, 24 cmp r4, 0x6 diff --git a/include/battle.h b/include/battle.h index bb61c7043..0aa778fe2 100644 --- a/include/battle.h +++ b/include/battle.h @@ -5,6 +5,7 @@ #include "battle_util.h" #include "battle_script_commands.h" #include "battle_2.h" +#include "battle_ai_switch_items.h" /* Banks are a name given to what could be called a 'battlerId' or 'monControllerId'. diff --git a/include/battle_ai_switch_items.h b/include/battle_ai_switch_items.h new file mode 100644 index 000000000..5c78a5386 --- /dev/null +++ b/include/battle_ai_switch_items.h @@ -0,0 +1,7 @@ +#ifndef GUARD_BATTLE_AI_SWITCH_ITEMS_H +#define GUARD_BATTLE_AI_SWITCH_ITEMS_H + +void AI_TrySwitchOrUseItem(void); +u8 GetMostSuitableMonToSwitchInto(void); + +#endif // GUARD_BATTLE_AI_SWITCH_ITEMS_H diff --git a/src/battle_9.c b/src/battle_9.c index 1b1a4ab7c..291bfc9e3 100644 --- a/src/battle_9.c +++ b/src/battle_9.c @@ -16,13 +16,19 @@ extern struct BattlePokemon gBattleMons[BATTLE_BANKS_COUNT]; extern u16 gBattlePartyID[BATTLE_BANKS_COUNT]; extern u16 gUnknown_02024250[BATTLE_BANKS_COUNT]; extern u8 gUnknown_02024270[BATTLE_BANKS_COUNT]; +extern u16 gDynamicBasePower; +extern u8 gBattleMoveFlags; +extern u8 gCritMultiplier; +extern s32 gBattleMoveDamage; extern const struct BattleMove gBattleMoves[]; extern const struct BaseStats gBaseStats[]; +extern const u8 gTypeEffectiveness[]; // this file's functions bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng); bool8 FindMonWithFlagsAndSuperEffective(u8 flags, u8 moduloPercent); +bool8 ShouldUseItem(void); bool8 ShouldSwitchIfPerishSong(void) { @@ -121,7 +127,7 @@ bool8 ShouldSwitchIfWonderGuard(void) return FALSE; // at this point there is not a single pokemon in the party that has a super effective move against a pokemon with wonder guard } -bool8 sub_8062E54(void) +bool8 FindMonThatAbsorbsOpponentsMove(void) { u8 bankIn1, bankIn2; u8 absorbingTypeAbility; @@ -430,13 +436,14 @@ bool8 FindMonWithFlagsAndSuperEffective(u8 flags, u8 moduloPercent) bool8 ShouldSwitch(void) { u8 bankIn1, bankIn2; + u8 *activeBankPtr; // needed to match s32 firstId; s32 lastId; // + 1 struct Pokemon *party; s32 i; s32 availableToSwitch; - if (gBattleMons[gActiveBank].status2 & (STATUS2_WRAPPED | STATUS2_ESCAPE_PREVENTION)) + if (gBattleMons[*(activeBankPtr = &gActiveBank)].status2 & (STATUS2_WRAPPED | STATUS2_ESCAPE_PREVENTION)) return FALSE; if (gStatuses3[gActiveBank] & STATUS3_ROOTED) return FALSE; @@ -444,25 +451,29 @@ bool8 ShouldSwitch(void) return FALSE; if (AbilityBattleEffects(ABILITYEFFECT_CHECK_OTHER_SIDE, gActiveBank, ABILITY_ARENA_TRAP, 0, 0)) return FALSE; // misses the flying or levitate check - if (AbilityBattleEffects(ABILITYEFFECT_FIELD_SPORT, gActiveBank, ABILITY_MAGNET_PULL, 0, 0) - && (gBattleMons[gActiveBank].type1 == TYPE_STEEL || gBattleMons[gActiveBank].type2 == TYPE_STEEL)) - return FALSE; + if (AbilityBattleEffects(ABILITYEFFECT_FIELD_SPORT, 0, ABILITY_MAGNET_PULL, 0, 0)) + { + if (gBattleMons[gActiveBank].type1 == TYPE_STEEL) + return FALSE; + if (gBattleMons[gActiveBank].type2 == TYPE_STEEL) + return FALSE; + } if (gBattleTypeFlags & BATTLE_TYPE_ARENA) return FALSE; availableToSwitch = 0; if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) { - bankIn1 = gActiveBank; - if (gAbsentBankFlags & gBitTable[GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON)]) - bankIn2 = gActiveBank; + bankIn1 = *activeBankPtr; + if (gAbsentBankFlags & gBitTable[GetBankByIdentity(GetBankIdentity(*activeBankPtr) ^ BIT_MON)]) + bankIn2 = *activeBankPtr; else - bankIn2 = GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON); + bankIn2 = GetBankByIdentity(GetBankIdentity(*activeBankPtr) ^ BIT_MON); } else { - bankIn1 = gActiveBank; - bankIn2 = gActiveBank; + bankIn1 = *activeBankPtr; + bankIn2 = *activeBankPtr; } if (gBattleTypeFlags & (BATTLE_TYPE_TWO_OPPONENTS | BATTLE_TYPE_x800000)) @@ -508,7 +519,7 @@ bool8 ShouldSwitch(void) return TRUE; if (ShouldSwitchIfWonderGuard()) return TRUE; - if (sub_8062E54()) + if (FindMonThatAbsorbsOpponentsMove()) return TRUE; if (ShouldSwitchIfNaturalCure()) return TRUE; @@ -516,10 +527,268 @@ bool8 ShouldSwitch(void) return FALSE; if (AreStatsRaised()) return FALSE; - if (FindMonWithFlagsAndSuperEffective(MOVESTATUS_NOTAFFECTED, 2)) - return TRUE; - if (FindMonWithFlagsAndSuperEffective(MOVESTATUS_NOTVERYEFFECTIVE, 3)) + if (FindMonWithFlagsAndSuperEffective(MOVESTATUS_NOTAFFECTED, 2) + || FindMonWithFlagsAndSuperEffective(MOVESTATUS_NOTVERYEFFECTIVE, 3)) return TRUE; return FALSE; } + +void AI_TrySwitchOrUseItem(void) +{ + struct Pokemon *party; + u8 bankIn1, bankIn2; + s32 firstId; + s32 lastId; // + 1 + u8 bankIdentity = GetBankIdentity(gActiveBank); + + if (GetBankSide(gActiveBank) == SIDE_PLAYER) + party = gPlayerParty; + else + party = gEnemyParty; + + if (gBattleTypeFlags & BATTLE_TYPE_TRAINER) + { + if (ShouldSwitch()) + { + if (*(gBattleStruct->field_294 + gActiveBank) == 6) + { + s32 monToSwitchId = GetMostSuitableMonToSwitchInto(); + if (monToSwitchId == 6) + { + if (!(gBattleTypeFlags & BATTLE_TYPE_DOUBLE)) + { + bankIn1 = GetBankByIdentity(bankIdentity); + bankIn2 = bankIn1; + } + else + { + bankIn1 = GetBankByIdentity(bankIdentity); + bankIn2 = GetBankByIdentity(bankIdentity ^ BIT_MON); + } + + if (gBattleTypeFlags & (BATTLE_TYPE_TWO_OPPONENTS | BATTLE_TYPE_x800000)) + { + if ((gActiveBank & BIT_MON) == 0) + firstId = 0, lastId = 3; + else + firstId = 3, lastId = 6; + } + else + { + firstId = 0, lastId = 6; + } + + for (monToSwitchId = firstId; monToSwitchId < lastId; monToSwitchId++) + { + if (GetMonData(&party[monToSwitchId], MON_DATA_HP) == 0) + continue; + if (monToSwitchId == gBattlePartyID[bankIn1]) + continue; + if (monToSwitchId == gBattlePartyID[bankIn2]) + continue; + if (monToSwitchId == *(gBattleStruct->field_5C + bankIn1)) + continue; + if (monToSwitchId == *(gBattleStruct->field_5C + bankIn2)) + continue; + + break; + } + } + + *(gBattleStruct->field_294 + gActiveBank) = monToSwitchId; + } + + *(gBattleStruct->field_5C + gActiveBank) = *(gBattleStruct->field_294 + gActiveBank); + return; + } + else if (ShouldUseItem()) + { + return; + } + } + + EmitCmd_x21(1, 0, (gActiveBank ^ BIT_SIDE) << 8); +} + +#define TYPE_FORESIGHT 0xFE +#define TYPE_ENDTABLE 0xFF + +static void ModulateByTypeEffectiveness(u8 atkType, u8 defType1, u8 defType2, u8 *var) +{ + s32 i = 0; + + while (gTypeEffectiveness[i] != TYPE_ENDTABLE) + { + if (gTypeEffectiveness[i] == TYPE_FORESIGHT) + { + i += 3; + continue; + } + else if (gTypeEffectiveness[i] == atkType) + { + // check type1 + if (gTypeEffectiveness[i + 1] == defType1) + *var = (*var * gTypeEffectiveness[i + 2]) / 10; + // check type2 + if (gTypeEffectiveness[i + 1] == defType2 && defType1 != defType2) + *var = (*var * gTypeEffectiveness[i + 2]) / 10; + } + i += 3; + } +} + +u8 GetMostSuitableMonToSwitchInto(void) +{ + u8 opposingBank; + u8 bestDmg; // note : should be changed to u32 for obvious reasons + u8 bestMonId; + u8 bankIn1, bankIn2; + s32 firstId; + s32 lastId; // + 1 + struct Pokemon *party; + s32 i, j; + u8 invalidMons; + u16 move; + + if (*(gBattleStruct->field_5C + gActiveBank) != 6) + return *(gBattleStruct->field_5C + gActiveBank); + if (gBattleTypeFlags & BATTLE_TYPE_ARENA) + return gBattlePartyID[gActiveBank] + 1; + + if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) + { + bankIn1 = gActiveBank; + if (gAbsentBankFlags & gBitTable[GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON)]) + bankIn2 = gActiveBank; + else + bankIn2 = GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_MON); + + // UB: It considers the opponent only player's side even though it can battle alongside player; + opposingBank = Random() & BIT_MON; + if (gAbsentBankFlags & gBitTable[opposingBank]) + opposingBank ^= BIT_MON; + } + else + { + opposingBank = GetBankByIdentity(GetBankIdentity(gActiveBank) ^ BIT_SIDE); + bankIn1 = gActiveBank; + bankIn2 = gActiveBank; + } + + if (gBattleTypeFlags & (BATTLE_TYPE_TWO_OPPONENTS | BATTLE_TYPE_x800000)) + { + if ((gActiveBank & BIT_MON) == 0) + firstId = 0, lastId = 3; + else + firstId = 3, lastId = 6; + } + else + { + firstId = 0, lastId = 6; + } + + if (GetBankSide(gActiveBank) == SIDE_PLAYER) + party = gPlayerParty; + else + party = gEnemyParty; + + invalidMons = 0; + + while (invalidMons != 0x3F) // all mons are invalid + { + bestDmg = 0; + bestMonId = 6; + // find the mon which type is the most suitable offensively + for (i = firstId; i < lastId; i++) + { + u16 species = GetMonData(&party[i], MON_DATA_SPECIES); + if (species != SPECIES_NONE + && GetMonData(&party[i], MON_DATA_HP) != 0 + && !(gBitTable[i] & invalidMons) + && gBattlePartyID[bankIn1] != i + && gBattlePartyID[bankIn2] != i + && i != *(gBattleStruct->field_5C + bankIn1) + && i != *(gBattleStruct->field_5C + bankIn2)) + { + u8 type1 = gBaseStats[species].type1; + u8 type2 = gBaseStats[species].type2; + u8 typeDmg = 10; + ModulateByTypeEffectiveness(gBattleMons[opposingBank].type1, type1, type2, &typeDmg); + ModulateByTypeEffectiveness(gBattleMons[opposingBank].type2, type1, type2, &typeDmg); + if (bestDmg < typeDmg) + { + bestDmg = typeDmg; + bestMonId = i; + } + } + else + { + invalidMons |= gBitTable[i]; + } + } + + // ok, we know the mon has the right typing but does it have at least one super effective move? + if (bestMonId != 6) + { + for (i = 0; i < 4; i++) + { + move = GetMonData(&party[bestMonId], MON_DATA_MOVE1 + i); + if (move != MOVE_NONE && TypeCalc(move, gActiveBank, opposingBank) & MOVESTATUS_SUPEREFFECTIVE) + break; + } + + if (i != 4) + return bestMonId; // has both the typing and at least one super effective move + + invalidMons |= gBitTable[bestMonId]; // sorry buddy, we want something better + } + else + { + invalidMons = 0x3F; // no viable mon to switch + } + } + + gDynamicBasePower = 0; + gBattleStruct->dynamicMoveType = 0; + gBattleScripting.dmgMultiplier = 1; + gBattleMoveFlags = 0; + gCritMultiplier = 1; + bestDmg = 0; + bestMonId = 6; + + // if we couldn't find the best mon in terms of typing, find the one that deals most damage + for (i = firstId; i < lastId; i++) + { + if ((u16)(GetMonData(&party[i], MON_DATA_SPECIES)) == SPECIES_NONE) + continue; + if (GetMonData(&party[i], MON_DATA_HP) == 0) + continue; + if (gBattlePartyID[bankIn1] == i) + continue; + if (gBattlePartyID[bankIn2] == i) + continue; + if (i == *(gBattleStruct->field_5C + bankIn1)) + continue; + if (i == *(gBattleStruct->field_5C + bankIn2)) + continue; + + for (j = 0; j < 4; j++) + { + move = GetMonData(&party[i], MON_DATA_MOVE1 + j); + gBattleMoveDamage = 0; + if (move != MOVE_NONE && gBattleMoves[move].power != 1) + { + AI_CalcDmg(gActiveBank, opposingBank); + TypeCalc(move, gActiveBank, opposingBank); + } + if (bestDmg < gBattleMoveDamage) + { + bestDmg = gBattleMoveDamage; + bestMonId = i; + } + } + } + + return bestMonId; +} From a3b62f43ee4ce02d8b7dd28acf53692567754bd3 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Wed, 11 Oct 2017 12:49:42 +0200 Subject: [PATCH 3/3] battle 9 is decompiled --- asm/battle_9.s | 646 ------------------- include/battle.h | 6 +- include/battle_ai_switch_items.h | 10 + include/pokemon.h | 1 + include/pokemon_item_effects.h | 54 ++ ld_script.txt | 3 +- src/battle_2.c | 28 +- src/battle_ai_script_commands.c | 2 +- src/{battle_9.c => battle_ai_switch_items.c} | 179 ++++- 9 files changed, 252 insertions(+), 677 deletions(-) delete mode 100644 asm/battle_9.s rename src/{battle_9.c => battle_ai_switch_items.c} (78%) diff --git a/asm/battle_9.s b/asm/battle_9.s deleted file mode 100644 index 0cf529bcc..000000000 --- a/asm/battle_9.s +++ /dev/null @@ -1,646 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - - thumb_func_start ai_identify_item_effect -ai_identify_item_effect: @ 8063E84 - push {r4,lr} - adds r2, r1, 0 - lsls r0, 24 - lsrs r0, 24 - cmp r0, 0x13 - bne _08063E94 - movs r0, 0x1 - b _08063EDA -_08063E94: - ldrb r1, [r2, 0x4] - movs r0, 0x4 - ands r0, r1 - cmp r0, 0 - beq _08063EA2 - movs r0, 0x2 - b _08063EDA -_08063EA2: - ldrb r4, [r2, 0x3] - movs r3, 0x3F - adds r0, r3, 0 - ands r0, r4 - cmp r0, 0 - beq _08063EB2 - movs r0, 0x3 - b _08063EDA -_08063EB2: - ldrb r1, [r2] - adds r0, r3, 0 - ands r0, r1 - cmp r0, 0 - bne _08063EC8 - ldrb r0, [r2, 0x1] - cmp r0, 0 - bne _08063EC8 - ldrb r0, [r2, 0x2] - cmp r0, 0 - beq _08063ECC -_08063EC8: - movs r0, 0x4 - b _08063EDA -_08063ECC: - movs r0, 0x80 - ands r0, r4 - cmp r0, 0 - bne _08063ED8 - movs r0, 0x6 - b _08063EDA -_08063ED8: - movs r0, 0x5 -_08063EDA: - pop {r4} - pop {r1} - bx r1 - thumb_func_end ai_identify_item_effect - - thumb_func_start ShouldUseItem -ShouldUseItem: @ 8063EE0 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x4 - movs r0, 0 - mov r10, r0 - movs r7, 0 - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - movs r1, 0x80 - lsls r1, 15 - ands r0, r1 - cmp r0, 0 - beq _08063F12 - ldr r0, =gActiveBank - ldrb r0, [r0] - bl GetBankIdentity - lsls r0, 24 - lsrs r0, 24 - cmp r0, 0x2 - bne _08063F12 - b _0806437C -_08063F12: - ldr r0, =gActiveBank - ldrb r0, [r0] - bl GetBankSide - lsls r0, 24 - ldr r5, =gEnemyParty - cmp r0, 0 - bne _08063F24 - ldr r5, =gPlayerParty -_08063F24: - movs r1, 0 - mov r8, r1 -_08063F28: - movs r0, 0x64 - mov r2, r8 - muls r2, r0 - adds r0, r2, 0 - adds r4, r5, r0 - adds r0, r4, 0 - movs r1, 0x39 - bl GetMonData - cmp r0, 0 - beq _08063F64 - adds r0, r4, 0 - movs r1, 0x41 - bl GetMonData - cmp r0, 0 - beq _08063F64 - adds r0, r4, 0 - movs r1, 0x41 - bl GetMonData - movs r1, 0xCE - lsls r1, 1 - cmp r0, r1 - beq _08063F64 - mov r0, r10 - adds r0, 0x1 - lsls r0, 24 - lsrs r0, 24 - mov r10, r0 -_08063F64: - movs r0, 0x1 - add r8, r0 - mov r1, r8 - cmp r1, 0x5 - ble _08063F28 - movs r2, 0 - mov r8, r2 -_08063F72: - ldr r1, =gBattleResources - mov r0, r8 - cmp r0, 0 - beq _08063F8E - ldr r0, [r1] - ldr r0, [r0, 0x18] - adds r0, 0x50 - ldrb r0, [r0] - mov r2, r8 - subs r0, r2 - adds r0, 0x1 - cmp r10, r0 - ble _08063F8E - b _08064370 -_08063F8E: - ldr r0, [r1] - ldr r0, [r0, 0x18] - mov r2, r8 - lsls r1, r2, 1 - adds r0, 0x48 - adds r0, r1 - ldrh r0, [r0] - mov r9, r0 - str r1, [sp] - cmp r0, 0 - bne _08063FA6 - b _08064370 -_08063FA6: - ldr r1, =gItemEffectTable - subs r0, 0xD - lsls r0, 2 - adds r0, r1 - ldr r1, [r0] - cmp r1, 0 - bne _08063FB6 - b _08064370 -_08063FB6: - mov r0, r9 - cmp r0, 0xAF - bne _08063FE8 - ldr r0, =gSaveBlock1Ptr - ldr r0, [r0] - ldr r1, =0x00003214 - adds r5, r0, r1 - b _08063FEA - .pool -_08063FE8: - adds r5, r1, 0 -_08063FEA: - mov r2, r9 - lsls r0, r2, 24 - lsrs r0, 24 - adds r1, r5, 0 - bl ai_identify_item_effect - ldr r4, =gActiveBank - ldrb r1, [r4] - lsrs r1, 1 - ldr r3, =gBattleStruct - ldr r2, [r3] - adds r1, r2 - adds r1, 0xC4 - strb r0, [r1] - ldrb r0, [r4] - lsrs r0, 1 - ldr r1, [r3] - adds r0, r1 - adds r0, 0xC4 - ldrb r0, [r0] - subs r0, 0x1 - cmp r0, 0x5 - bls _0806401A - b _08064324 -_0806401A: - lsls r0, 2 - ldr r1, =_08064030 - adds r0, r1 - ldr r0, [r0] - mov pc, r0 - .pool - .align 2, 0 -_08064030: - .4byte _08064048 - .4byte _08064074 - .4byte _080640BC - .4byte _08064214 - .4byte _080642F4 - .4byte _0806437C -_08064048: - ldr r2, =gBattleMons - ldr r0, =gActiveBank - ldrb r1, [r0] - movs r0, 0x58 - muls r0, r1 - adds r0, r2 - ldrh r1, [r0, 0x2C] - ldrh r0, [r0, 0x28] - lsrs r1, 2 - cmp r0, r1 - bcc _08064060 - b _08064324 -_08064060: - cmp r0, 0 - bne _08064066 - b _08064324 -_08064066: - movs r7, 0x1 - b _08064328 - .pool -_08064074: - mov r0, r9 - movs r1, 0x4 - movs r2, 0x4 - bl GetItemEffectParamOffset - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0 - bne _08064088 - b _08064324 -_08064088: - ldr r2, =gBattleMons - ldr r0, =gActiveBank - ldrb r1, [r0] - movs r0, 0x58 - muls r0, r1 - adds r1, r0, r2 - ldrh r2, [r1, 0x28] - cmp r2, 0 - bne _0806409C - b _08064324 -_0806409C: - ldrh r3, [r1, 0x2C] - lsrs r0, r3, 2 - cmp r2, r0 - bcc _08064066 - adds r0, r2, 0 - subs r0, r3, r0 - adds r1, r5, r4 - ldrb r1, [r1] - cmp r0, r1 - bgt _080640B2 - b _08064324 -_080640B2: - b _08064066 - .pool -_080640BC: - ldr r3, =gActiveBank - ldrb r0, [r3] - lsrs r0, 1 - ldr r2, =gBattleStruct - ldr r1, [r2] - adds r0, r1 - adds r0, 0xC6 - movs r1, 0 - strb r1, [r0] - ldrb r1, [r5, 0x3] - movs r6, 0x20 - movs r0, 0x20 - ands r0, r1 - cmp r0, 0 - beq _08064100 - ldr r1, =gBattleMons - ldrb r4, [r3] - movs r0, 0x58 - muls r0, r4 - adds r1, 0x4C - adds r0, r1 - ldr r0, [r0] - movs r1, 0x7 - ands r0, r1 - cmp r0, 0 - beq _08064100 - lsrs r1, r4, 1 - ldr r0, [r2] - adds r1, r0 - adds r1, 0xC6 - ldrb r0, [r1] - orrs r0, r6 - strb r0, [r1] - movs r7, 0x1 -_08064100: - ldrb r1, [r5, 0x3] - movs r6, 0x10 - movs r0, 0x10 - ands r0, r1 - cmp r0, 0 - beq _0806413A - ldr r1, =gBattleMons - ldrb r4, [r3] - movs r0, 0x58 - muls r0, r4 - adds r1, 0x4C - adds r0, r1 - ldr r1, [r0] - movs r0, 0x8 - ands r0, r1 - cmp r0, 0 - bne _0806412A - movs r0, 0x80 - ands r1, r0 - cmp r1, 0 - beq _0806413A -_0806412A: - lsrs r1, r4, 1 - ldr r0, [r2] - adds r1, r0 - adds r1, 0xC6 - ldrb r0, [r1] - orrs r0, r6 - strb r0, [r1] - movs r7, 0x1 -_0806413A: - ldrb r1, [r5, 0x3] - movs r6, 0x8 - movs r0, 0x8 - ands r0, r1 - cmp r0, 0 - beq _0806416C - ldr r1, =gBattleMons - ldrb r4, [r3] - movs r0, 0x58 - muls r0, r4 - adds r1, 0x4C - adds r0, r1 - ldr r0, [r0] - movs r1, 0x10 - ands r0, r1 - cmp r0, 0 - beq _0806416C - lsrs r1, r4, 1 - ldr r0, [r2] - adds r1, r0 - adds r1, 0xC6 - ldrb r0, [r1] - orrs r0, r6 - strb r0, [r1] - movs r7, 0x1 -_0806416C: - ldrb r1, [r5, 0x3] - movs r6, 0x4 - movs r0, 0x4 - ands r0, r1 - cmp r0, 0 - beq _0806419E - ldr r1, =gBattleMons - ldrb r4, [r3] - movs r0, 0x58 - muls r0, r4 - adds r1, 0x4C - adds r0, r1 - ldr r0, [r0] - movs r1, 0x20 - ands r0, r1 - cmp r0, 0 - beq _0806419E - lsrs r1, r4, 1 - ldr r0, [r2] - adds r1, r0 - adds r1, 0xC6 - ldrb r0, [r1] - orrs r0, r6 - strb r0, [r1] - movs r7, 0x1 -_0806419E: - ldrb r1, [r5, 0x3] - movs r6, 0x2 - movs r0, 0x2 - ands r0, r1 - cmp r0, 0 - beq _080641D0 - ldr r1, =gBattleMons - ldrb r4, [r3] - movs r0, 0x58 - muls r0, r4 - adds r1, 0x4C - adds r0, r1 - ldr r0, [r0] - movs r1, 0x40 - ands r0, r1 - cmp r0, 0 - beq _080641D0 - lsrs r1, r4, 1 - ldr r0, [r2] - adds r1, r0 - adds r1, 0xC6 - ldrb r0, [r1] - orrs r0, r6 - strb r0, [r1] - movs r7, 0x1 -_080641D0: - ldrb r1, [r5, 0x3] - movs r4, 0x1 - movs r0, 0x1 - ands r0, r1 - cmp r0, 0 - bne _080641DE - b _08064324 -_080641DE: - ldr r1, =gBattleMons - ldrb r3, [r3] - movs r0, 0x58 - muls r0, r3 - adds r1, 0x50 - adds r0, r1 - ldr r0, [r0] - movs r1, 0x7 - ands r0, r1 - cmp r0, 0 - bne _080641F6 - b _08064324 -_080641F6: - lsrs r1, r3, 1 - ldr r0, [r2] - adds r1, r0 - adds r1, 0xC6 - ldrb r0, [r1] - orrs r0, r4 - strb r0, [r1] - b _08064066 - .pool -_08064214: - ldr r4, =gActiveBank - ldrb r0, [r4] - lsrs r0, 1 - ldr r3, =gBattleStruct - ldr r1, [r3] - adds r0, r1 - adds r0, 0xC6 - movs r1, 0 - strb r1, [r0] - ldr r1, =gDisableStructs - ldrb r2, [r4] - lsls r0, r2, 3 - subs r0, r2 - lsls r0, 2 - adds r0, r1 - ldrb r0, [r0, 0x16] - cmp r0, 0 - beq _08064324 - ldrb r1, [r5] - movs r6, 0xF - adds r0, r6, 0 - ands r0, r1 - cmp r0, 0 - beq _08064254 - lsrs r2, 1 - ldr r0, [r3] - adds r2, r0 - adds r2, 0xC6 - ldrb r0, [r2] - movs r1, 0x1 - orrs r0, r1 - strb r0, [r2] -_08064254: - ldrb r1, [r5, 0x1] - movs r7, 0xF0 - adds r0, r7, 0 - ands r0, r1 - cmp r0, 0 - beq _08064272 - ldrb r0, [r4] - lsrs r0, 1 - ldr r1, [r3] - adds r0, r1 - adds r0, 0xC6 - ldrb r1, [r0] - movs r2, 0x2 - orrs r1, r2 - strb r1, [r0] -_08064272: - ldrb r1, [r5, 0x1] - adds r0, r6, 0 - ands r0, r1 - cmp r0, 0 - beq _0806428E - ldrb r0, [r4] - lsrs r0, 1 - ldr r1, [r3] - adds r0, r1 - adds r0, 0xC6 - ldrb r1, [r0] - movs r2, 0x4 - orrs r1, r2 - strb r1, [r0] -_0806428E: - ldrb r1, [r5, 0x2] - adds r0, r6, 0 - ands r0, r1 - cmp r0, 0 - beq _080642AA - ldrb r0, [r4] - lsrs r0, 1 - ldr r1, [r3] - adds r0, r1 - adds r0, 0xC6 - ldrb r1, [r0] - movs r2, 0x8 - orrs r1, r2 - strb r1, [r0] -_080642AA: - ldrb r1, [r5, 0x2] - adds r0, r7, 0 - ands r0, r1 - cmp r0, 0 - beq _080642C6 - ldrb r0, [r4] - lsrs r0, 1 - ldr r1, [r3] - adds r0, r1 - adds r0, 0xC6 - ldrb r1, [r0] - movs r2, 0x20 - orrs r1, r2 - strb r1, [r0] -_080642C6: - ldrb r1, [r5] - movs r0, 0x30 - ands r0, r1 - cmp r0, 0 - bne _080642D2 - b _08064066 -_080642D2: - ldrb r0, [r4] - lsrs r0, 1 - ldr r1, [r3] - adds r0, r1 - adds r0, 0xC6 - ldrb r1, [r0] - movs r2, 0x80 - orrs r1, r2 - strb r1, [r0] - b _08064066 - .pool -_080642F4: - ldr r4, =gActiveBank - ldrb r0, [r4] - bl GetBankSide - lsls r0, 24 - lsrs r3, r0, 24 - ldr r2, =gDisableStructs - ldrb r1, [r4] - lsls r0, r1, 3 - subs r0, r1 - lsls r0, 2 - adds r0, r2 - ldrb r0, [r0, 0x16] - cmp r0, 0 - beq _08064324 - ldr r0, =gSideTimers - lsls r1, r3, 1 - adds r1, r3 - lsls r1, 2 - adds r1, r0 - ldrb r0, [r1, 0x4] - cmp r0, 0 - bne _08064324 - b _08064066 -_08064324: - cmp r7, 0 - beq _08064370 -_08064328: - movs r0, 0x1 - movs r1, 0x1 - movs r2, 0 - bl EmitCmd_x21 - ldr r0, =gActiveBank - ldrb r0, [r0] - lsrs r0, 1 - ldr r1, =gBattleStruct - ldr r1, [r1] - lsls r0, 1 - adds r0, r1 - adds r0, 0xC0 - mov r1, r9 - strb r1, [r0] - ldr r0, =gBattleResources - ldr r0, [r0] - ldr r0, [r0, 0x18] - adds r0, 0x48 - ldr r2, [sp] - adds r0, r2 - movs r1, 0 - strh r1, [r0] - adds r0, r7, 0 - b _0806437E - .pool -_08064370: - movs r0, 0x1 - add r8, r0 - mov r1, r8 - cmp r1, 0x3 - bgt _0806437C - b _08063F72 -_0806437C: - movs r0, 0 -_0806437E: - add sp, 0x4 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - thumb_func_end ShouldUseItem - - .align 2, 0 @ Don't pad with nop. diff --git a/include/battle.h b/include/battle.h index 0aa778fe2..a60f450ce 100644 --- a/include/battle.h +++ b/include/battle.h @@ -477,7 +477,7 @@ struct BattleHistory struct UsedMoves usedMoves[BATTLE_BANKS_COUNT]; u8 abilities[BATTLE_BANKS_COUNT]; u8 itemEffects[BATTLE_BANKS_COUNT]; - u16 TrainerItems[BATTLE_BANKS_COUNT]; + u16 trainerItems[BATTLE_BANKS_COUNT]; u8 itemsNo; }; @@ -633,8 +633,8 @@ struct BattleStruct void (*savedCallback)(void); u16 usedHeldItems[BATTLE_BANKS_COUNT]; u8 field_C0[4]; - u8 field_C4[2]; - u8 field_C6[2]; + u8 AI_itemType[2]; + u8 AI_itemFlags[2]; u16 choicedMove[BATTLE_BANKS_COUNT]; u16 changedItems[BATTLE_BANKS_COUNT]; u8 intimidateBank; diff --git a/include/battle_ai_switch_items.h b/include/battle_ai_switch_items.h index 5c78a5386..0a230e7f6 100644 --- a/include/battle_ai_switch_items.h +++ b/include/battle_ai_switch_items.h @@ -1,6 +1,16 @@ #ifndef GUARD_BATTLE_AI_SWITCH_ITEMS_H #define GUARD_BATTLE_AI_SWITCH_ITEMS_H +enum +{ + AI_ITEM_FULL_RESTORE = 1, + AI_ITEM_HEAL_HP, + AI_ITEM_CURE_CONDITION, + AI_ITEM_X_STAT, + AI_ITEM_GUARD_SPECS, + AI_ITEM_NOT_RECOGNIZABLE +}; + void AI_TrySwitchOrUseItem(void); u8 GetMostSuitableMonToSwitchInto(void); diff --git a/include/pokemon.h b/include/pokemon.h index 263a37426..c26c28afa 100644 --- a/include/pokemon.h +++ b/include/pokemon.h @@ -642,6 +642,7 @@ void UpdatePartyPokerusTime(u16 days); void PartySpreadPokerus(struct Pokemon *party); s8 GetMonFlavourRelation(struct Pokemon *mon, u8 a2); s8 GetFlavourRelationByPersonality(u32 personality, u8 a2); +u8 GetItemEffectParamOffset(u16 itemId, u8 effectByte, u8 effectBit); #include "sprite.h" diff --git a/include/pokemon_item_effects.h b/include/pokemon_item_effects.h index 88f341317..505cc0f25 100644 --- a/include/pokemon_item_effects.h +++ b/include/pokemon_item_effects.h @@ -3,6 +3,60 @@ #include "pokemon.h" +// TODO once pokemon item effects is decompiled +/* +struct PokemonItemEffect +{ + //field 0 + u8 xAtk : 4; // x1, x2, x4, x8 = xF + u8 field_0_x10 : 1; // x10 + u8 critRatioUp : 1; // x20 + u8 field_0_x40 : 1; // x40 + u8 cureInfatuation : 1; // x80 + + /*field 1 + u8 xSpeed : 4; // x1, x2, x4, x8 = xF + u8 xDefense : 4; // x10, x20, x40, xF0 + + /*field 2 + u8 xSpAtk : 4; // x1, x2, x4, x8 = xF + u8 xAccuracy : 4; // x10, x20, x40, xF0 + + /*field 3 + u8 cureConfusion : 1; // x1 + u8 cureParalysis : 1; // x2 + u8 cureFreeze : 1; // x4 + u8 cureBurn : 1; // x8 + u8 curePoison : 1; // x10 + u8 cureSleep : 1; // x20 + u8 field_3_x40 : 1; // x40 + u8 cantLowerStats : 1; // x80 + + /*field 4 + u8 hpEv : 1; // x1 + u8 attackEv : 1; // x2 + u8 healHp : 1; // x4 + u8 field_4_x8 : 1; // x8 + u8 field_4_x10 : 1; // x10 + u8 ppUp : 1; // x20 + u8 levelUp : 1; // x40 + u8 evolutionStone : 1; // x80 + + /*field 5 + u8 defEv: 1; // x1 + u8 speedEv : 1; // x2 + u8 spDefEv : 1; // x4 + u8 spAtkEv : 1; // x8 + u8 ppMax : 1; // x10 + u8 field_5_x20 : 1; // x20 + u8 field_5_x40 : 1; // x40 + u8 field_5_x80 : 1; // x80 + + /*field 6 + u8 value; +}; +*/ + bool8 ExecuteTableBasedItemEffect(struct Pokemon *mon, u16 itemId, u8 partyId, u8 monMoveIndex, u8 a5); #endif // GUARD_POKEMON_ITEM_EFFECTS diff --git a/ld_script.txt b/ld_script.txt index 1da43ff9d..887f24811 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -64,8 +64,7 @@ SECTIONS { asm/battle_controller_player.o(.text); asm/battle_7.o(.text); asm/battle_controller_opponent.o(.text); - src/battle_9.o(.text); - asm/battle_9.o(.text); + src/battle_ai_switch_items.o(.text); asm/battle_controller_linkopponent.o(.text); src/pokemon_1.o(.text); asm/pokemon_1.o(.text); diff --git a/src/battle_2.c b/src/battle_2.c index 92c48c427..c4da02e36 100644 --- a/src/battle_2.c +++ b/src/battle_2.c @@ -5324,30 +5324,30 @@ static void HandleAction_UseItem(void) { gBattleScripting.bank = gBankAttacker; - switch (*(gBattleStruct->field_C4 + (gBankAttacker >> 1))) + switch (*(gBattleStruct->AI_itemType + (gBankAttacker >> 1))) { - case 1: - case 2: + case AI_ITEM_FULL_RESTORE: + case AI_ITEM_HEAL_HP: break; - case 3: + case AI_ITEM_CURE_CONDITION: gBattleCommunication[MULTISTRING_CHOOSER] = 0; - if (*(gBattleStruct->field_C6 + gBankAttacker / 2) & 1) + if (*(gBattleStruct->AI_itemFlags + gBankAttacker / 2) & 1) { - if (*(gBattleStruct->field_C6 + gBankAttacker / 2) & 0x3E) + if (*(gBattleStruct->AI_itemFlags + gBankAttacker / 2) & 0x3E) gBattleCommunication[MULTISTRING_CHOOSER] = 5; } else { - while (!(*(gBattleStruct->field_C6 + gBankAttacker / 2) & 1)) + while (!(*(gBattleStruct->AI_itemFlags + gBankAttacker / 2) & 1)) { - *(gBattleStruct->field_C6 + gBankAttacker / 2) >>= 1; + *(gBattleStruct->AI_itemFlags + gBankAttacker / 2) >>= 1; gBattleCommunication[MULTISTRING_CHOOSER]++; } } break; - case 4: + case AI_ITEM_X_STAT: gBattleCommunication[MULTISTRING_CHOOSER] = 4; - if (*(gBattleStruct->field_C6 + (gBankAttacker >> 1)) & 0x80) + if (*(gBattleStruct->AI_itemFlags + (gBankAttacker >> 1)) & 0x80) { gBattleCommunication[MULTISTRING_CHOOSER] = 5; } @@ -5356,9 +5356,9 @@ static void HandleAction_UseItem(void) PREPARE_STAT_BUFFER(gBattleTextBuff1, STAT_ATK) PREPARE_STRING_BUFFER(gBattleTextBuff2, 0xD2) - while (!((*(gBattleStruct->field_C6 + (gBankAttacker >> 1))) & 1)) + while (!((*(gBattleStruct->AI_itemFlags + (gBankAttacker >> 1))) & 1)) { - *(gBattleStruct->field_C6 + gBankAttacker / 2) >>= 1; + *(gBattleStruct->AI_itemFlags + gBankAttacker / 2) >>= 1; gBattleTextBuff1[2]++; } @@ -5366,7 +5366,7 @@ static void HandleAction_UseItem(void) gBattleScripting.animArg2 = 0; } break; - case 5: + case AI_ITEM_GUARD_SPECS: if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) gBattleCommunication[MULTISTRING_CHOOSER] = 2; else @@ -5374,7 +5374,7 @@ static void HandleAction_UseItem(void) break; } - gBattlescriptCurrInstr = gUnknown_082DBD3C[*(gBattleStruct->field_C4 + gBankAttacker / 2)]; + gBattlescriptCurrInstr = gUnknown_082DBD3C[*(gBattleStruct->AI_itemType + gBankAttacker / 2)]; } gCurrentActionFuncId = ACTION_RUN_BATTLESCRIPT; } diff --git a/src/battle_ai_script_commands.c b/src/battle_ai_script_commands.c index bba66983b..24377eacc 100644 --- a/src/battle_ai_script_commands.c +++ b/src/battle_ai_script_commands.c @@ -331,7 +331,7 @@ void BattleAI_HandleItemUseBeforeAISetup(u8 defaultScoreMoves) { if (gTrainers[gTrainerBattleOpponent_A].items[i] != 0) { - gBattleResources->battleHistory->TrainerItems[gBattleResources->battleHistory->itemsNo] = gTrainers[gTrainerBattleOpponent_A].items[i]; + gBattleResources->battleHistory->trainerItems[gBattleResources->battleHistory->itemsNo] = gTrainers[gTrainerBattleOpponent_A].items[i]; gBattleResources->battleHistory->itemsNo++; } } diff --git a/src/battle_9.c b/src/battle_ai_switch_items.c similarity index 78% rename from src/battle_9.c rename to src/battle_ai_switch_items.c index 291bfc9e3..661759a98 100644 --- a/src/battle_9.c +++ b/src/battle_ai_switch_items.c @@ -7,6 +7,8 @@ #include "species.h" #include "rng.h" #include "util.h" +#include "items.h" +#include "pokemon_item_effects.h" extern u8 gActiveBank; extern u8 gAbsentBankFlags; @@ -24,13 +26,14 @@ extern s32 gBattleMoveDamage; extern const struct BattleMove gBattleMoves[]; extern const struct BaseStats gBaseStats[]; extern const u8 gTypeEffectiveness[]; +extern const u8 * const gItemEffectTable[]; // todo: fix once struct is declared // this file's functions -bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng); -bool8 FindMonWithFlagsAndSuperEffective(u8 flags, u8 moduloPercent); -bool8 ShouldUseItem(void); +static bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng); +static bool8 FindMonWithFlagsAndSuperEffective(u8 flags, u8 moduloPercent); +static bool8 ShouldUseItem(void); -bool8 ShouldSwitchIfPerishSong(void) +static bool8 ShouldSwitchIfPerishSong(void) { if (gStatuses3[gActiveBank] & STATUS3_PERISH_SONG && gDisableStructs[gActiveBank].perishSong1 == 0) @@ -43,7 +46,7 @@ bool8 ShouldSwitchIfPerishSong(void) return FALSE; } -bool8 ShouldSwitchIfWonderGuard(void) +static bool8 ShouldSwitchIfWonderGuard(void) { u8 opposingIdentity; u8 opposingBank; @@ -127,7 +130,7 @@ bool8 ShouldSwitchIfWonderGuard(void) return FALSE; // at this point there is not a single pokemon in the party that has a super effective move against a pokemon with wonder guard } -bool8 FindMonThatAbsorbsOpponentsMove(void) +static bool8 FindMonThatAbsorbsOpponentsMove(void) { u8 bankIn1, bankIn2; u8 absorbingTypeAbility; @@ -226,7 +229,7 @@ bool8 FindMonThatAbsorbsOpponentsMove(void) return FALSE; } -bool8 ShouldSwitchIfNaturalCure(void) +static bool8 ShouldSwitchIfNaturalCure(void) { if (!(gBattleMons[gActiveBank].status1 & STATUS_SLEEP)) return FALSE; @@ -262,7 +265,7 @@ bool8 ShouldSwitchIfNaturalCure(void) return FALSE; } -bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng) +static bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng) { u8 opposingIdentity; u8 opposingBank; @@ -318,7 +321,7 @@ bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng) return FALSE; } -bool8 AreStatsRaised(void) +static bool8 AreStatsRaised(void) { u8 buffedStatsValue = 0; s32 i; @@ -332,7 +335,7 @@ bool8 AreStatsRaised(void) return (buffedStatsValue > 3); } -bool8 FindMonWithFlagsAndSuperEffective(u8 flags, u8 moduloPercent) +static bool8 FindMonWithFlagsAndSuperEffective(u8 flags, u8 moduloPercent) { u8 bankIn1, bankIn2; s32 firstId; @@ -433,7 +436,7 @@ bool8 FindMonWithFlagsAndSuperEffective(u8 flags, u8 moduloPercent) return FALSE; } -bool8 ShouldSwitch(void) +static bool8 ShouldSwitch(void) { u8 bankIn1, bankIn2; u8 *activeBankPtr; // needed to match @@ -792,3 +795,157 @@ u8 GetMostSuitableMonToSwitchInto(void) return bestMonId; } + +// TODO: use PokemonItemEffect struct instead of u8 once it's documented +static u8 GetAI_ItemType(u8 itemId, const u8 *itemEffect) // NOTE: should take u16 as item Id argument +{ + if (itemId == ITEM_FULL_RESTORE) + return AI_ITEM_FULL_RESTORE; + if (itemEffect[4] & 4) + return AI_ITEM_HEAL_HP; + if (itemEffect[3] & 0x3F) + return AI_ITEM_CURE_CONDITION; + if (itemEffect[0] & 0x3F || itemEffect[1] != 0 || itemEffect[2] != 0) + return AI_ITEM_X_STAT; + if (itemEffect[3] & 0x80) + return AI_ITEM_GUARD_SPECS; + + return AI_ITEM_NOT_RECOGNIZABLE; +} + +static bool8 ShouldUseItem(void) +{ + struct Pokemon *party; + s32 i; + u8 validMons = 0; + bool8 shouldUse = FALSE; + + if (gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER && GetBankIdentity(gActiveBank) == IDENTITY_PLAYER_MON2) + return FALSE; + + if (GetBankSide(gActiveBank) == SIDE_PLAYER) + party = gPlayerParty; + else + party = gEnemyParty; + + for (i = 0; i < 6; i++) + { + if (GetMonData(&party[i], MON_DATA_HP) != 0 + && GetMonData(&party[i], MON_DATA_SPECIES2) != SPECIES_NONE + && GetMonData(&party[i], MON_DATA_SPECIES2) != SPECIES_EGG) + { + validMons++; + } + } + + for (i = 0; i < 4; i++) + { + u16 item; + const u8 *itemEffects; + u8 paramOffset; + u8 bankSide; + + if (i != 0 && validMons > (gBattleResources->battleHistory->itemsNo - i) + 1) + continue; + item = gBattleResources->battleHistory->trainerItems[i]; + if (item == ITEM_NONE) + continue; + if (gItemEffectTable[item - 13] == NULL) + continue; + + if (item == ITEM_ENIGMA_BERRY) + itemEffects = gSaveBlock1Ptr->enigmaBerry.itemEffect; + else + itemEffects = gItemEffectTable[item - 13]; + + *(gBattleStruct->AI_itemType + gActiveBank / 2) = GetAI_ItemType(item, itemEffects); + + switch (*(gBattleStruct->AI_itemType + gActiveBank / 2)) + { + case AI_ITEM_FULL_RESTORE: + if (gBattleMons[gActiveBank].hp >= gBattleMons[gActiveBank].maxHP / 4) + break; + if (gBattleMons[gActiveBank].hp == 0) + break; + shouldUse = TRUE; + break; + case AI_ITEM_HEAL_HP: + paramOffset = GetItemEffectParamOffset(item, 4, 4); + if (paramOffset == 0) + break; + if (gBattleMons[gActiveBank].hp == 0) + break; + if (gBattleMons[gActiveBank].hp < gBattleMons[gActiveBank].maxHP / 4 || gBattleMons[gActiveBank].maxHP - gBattleMons[gActiveBank].hp > itemEffects[paramOffset]) + shouldUse = TRUE; + break; + case AI_ITEM_CURE_CONDITION: + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) = 0; + if (itemEffects[3] & 0x20 && gBattleMons[gActiveBank].status1 & STATUS_SLEEP) + { + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x20; + shouldUse = TRUE; + } + if (itemEffects[3] & 0x10 && (gBattleMons[gActiveBank].status1 & STATUS_POISON || gBattleMons[gActiveBank].status1 & STATUS_TOXIC_POISON)) + { + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x10; + shouldUse = TRUE; + } + if (itemEffects[3] & 0x8 && gBattleMons[gActiveBank].status1 & STATUS_BURN) + { + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x8; + shouldUse = TRUE; + } + if (itemEffects[3] & 0x4 && gBattleMons[gActiveBank].status1 & STATUS_FREEZE) + { + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x4; + shouldUse = TRUE; + } + if (itemEffects[3] & 0x2 && gBattleMons[gActiveBank].status1 & STATUS_PARALYSIS) + { + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x2; + shouldUse = TRUE; + } + if (itemEffects[3] & 0x1 && gBattleMons[gActiveBank].status2 & STATUS2_CONFUSION) + { + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x1; + shouldUse = TRUE; + } + break; + case AI_ITEM_X_STAT: + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) = 0; + if (gDisableStructs[gActiveBank].isFirstTurn == 0) + break; + if (itemEffects[0] & 0xF) + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x1; + if (itemEffects[1] & 0xF0) + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x2; + if (itemEffects[1] & 0xF) + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x4; + if (itemEffects[2] & 0xF) + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x8; + if (itemEffects[2] & 0xF0) + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x20; + if (itemEffects[0] & 0x30) + *(gBattleStruct->AI_itemFlags + gActiveBank / 2) |= 0x80; + shouldUse = TRUE; + break; + case AI_ITEM_GUARD_SPECS: + bankSide = GetBankSide(gActiveBank); + if (gDisableStructs[gActiveBank].isFirstTurn != 0 && gSideTimers[bankSide].mistTimer == 0) + shouldUse = TRUE; + break; + case AI_ITEM_NOT_RECOGNIZABLE: + return FALSE; + } + + if (shouldUse) + { + EmitCmd_x21(1, 1, 0); + *(gBattleStruct->field_C0 + (gActiveBank / 2) * 2) = item; + gBattleResources->battleHistory->trainerItems[i] = 0; + return shouldUse; + } + } + + return FALSE; +}