From 5243285ab2cbf895f28e9866e3f5671326422348 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Thu, 2 Nov 2017 18:19:49 +0100 Subject: [PATCH] start working on battle 7 --- asm/battle_7.s | 827 ------------------------------- include/battle.h | 6 + include/battle_script_commands.h | 2 +- include/malloc.h | 6 + ld_script.txt | 1 + src/battle_7.c | 284 +++++++++++ 6 files changed, 298 insertions(+), 828 deletions(-) create mode 100644 src/battle_7.c diff --git a/asm/battle_7.s b/asm/battle_7.s index 94cc1ba8e7..8d9321af09 100644 --- a/asm/battle_7.s +++ b/asm/battle_7.s @@ -5,834 +5,7 @@ .text - thumb_func_start AllocateBattleSpritesData -AllocateBattleSpritesData: @ 805D118 - push {r4,lr} - ldr r4, =gBattleSpritesDataPtr - movs r0, 0x10 - bl AllocZeroed - str r0, [r4] - movs r0, 0x10 - bl AllocZeroed - ldr r1, [r4] - str r0, [r1] - movs r0, 0x30 - bl AllocZeroed - ldr r1, [r4] - str r0, [r1, 0x4] - movs r0, 0x10 - bl AllocZeroed - ldr r1, [r4] - str r0, [r1, 0x8] - movs r0, 0x50 - bl AllocZeroed - ldr r1, [r4] - str r0, [r1, 0xC] - pop {r4} - pop {r0} - bx r0 - .pool - thumb_func_end AllocateBattleSpritesData - thumb_func_start FreeBattleSpritesData -FreeBattleSpritesData: @ 805D158 - push {r4,r5,lr} - ldr r5, =gBattleSpritesDataPtr - ldr r0, [r5] - cmp r0, 0 - beq _0805D192 - ldr r0, [r0, 0xC] - bl Free - ldr r0, [r5] - movs r4, 0 - str r4, [r0, 0xC] - ldr r0, [r0, 0x8] - bl Free - ldr r0, [r5] - str r4, [r0, 0x8] - ldr r0, [r0, 0x4] - bl Free - ldr r0, [r5] - str r4, [r0, 0x4] - ldr r0, [r0] - bl Free - ldr r0, [r5] - str r4, [r0] - bl Free - str r4, [r5] -_0805D192: - pop {r4,r5} - pop {r0} - bx r0 - .pool - thumb_func_end FreeBattleSpritesData - - thumb_func_start ChooseMoveAndTargetInBattlePalace -ChooseMoveAndTargetInBattlePalace: @ 805D19C - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - movs r0, 0x1 - negs r0, r0 - mov r9, r0 - ldr r4, =gActiveBank - ldrb r0, [r4] - lsls r2, r0, 9 - ldr r1, =gBattleBufferA + 4 - adds r2, r1 - mov r10, r2 - movs r1, 0 - movs r2, 0xFF - bl CheckMoveLimitations - lsls r0, 24 - lsrs r0, 24 - mov r8, r0 - bl Random - lsls r0, 16 - lsrs r0, 16 - movs r1, 0x64 - bl __umodsi3 - lsls r0, 16 - lsrs r7, r0, 16 - ldr r0, =gBattleStruct - ldr r0, [r0] - adds r0, 0x92 - ldrb r1, [r0] - ldr r2, =gBitTable - ldrb r0, [r4] - lsls r0, 2 - adds r0, r2 - ldr r0, [r0] - ands r1, r0 - negs r0, r1 - orrs r0, r1 - asrs r4, r0, 31 - movs r0, 0x2 - ands r4, r0 - adds r6, r4, 0 - adds r5, r4, 0x2 - b _0805D20E - .pool -_0805D20C: - adds r4, 0x1 -_0805D20E: - cmp r4, r5 - bge _0805D234 - ldr r0, =gActiveBank - ldrb r1, [r0] - movs r0, 0x58 - muls r0, r1 - ldr r1, =gBattleMons + 0x48 - adds r0, r1 - ldr r0, [r0] - bl GetNatureFromPersonality - lsls r0, 24 - lsrs r0, 22 - adds r0, r4, r0 - ldr r2, =gUnknown_0831C494 - adds r0, r2 - ldrb r0, [r0] - cmp r0, r7 - ble _0805D20C -_0805D234: - subs r7, r4, r6 - cmp r4, r5 - bne _0805D23C - movs r7, 0x2 -_0805D23C: - movs r6, 0 - movs r4, 0 - mov r1, r10 - ldrh r0, [r1] - cmp r0, 0 - beq _0805D27C - mov r5, r10 -_0805D24A: - ldrh r0, [r5] - bl sub_805D4A8 - lsls r0, 24 - lsrs r0, 24 - cmp r7, r0 - bne _0805D26E - mov r0, r10 - adds r0, 0x8 - adds r0, r4 - ldrb r0, [r0] - cmp r0, 0 - beq _0805D26E - ldr r0, =gBitTable - lsls r1, r4, 2 - adds r1, r0 - ldr r0, [r1] - orrs r6, r0 -_0805D26E: - adds r5, 0x2 - adds r4, 0x1 - cmp r4, 0x3 - bgt _0805D27C - ldrh r0, [r5] - cmp r0, 0 - bne _0805D24A -_0805D27C: - cmp r6, 0 - beq _0805D2AC - ldr r3, =gBattleStruct - ldr r1, [r3] - adds r1, 0x92 - ldrb r2, [r1] - movs r0, 0xF - ands r0, r2 - strb r0, [r1] - ldr r1, [r3] - adds r1, 0x92 - lsls r0, r6, 4 - ldrb r2, [r1] - orrs r0, r2 - strb r0, [r1] - lsls r0, r6, 24 - lsrs r0, 24 - bl BattleAI_SetupAIData - bl BattleAI_ChooseMoveOrAction - lsls r0, 24 - lsrs r0, 24 - mov r9, r0 -_0805D2AC: - movs r0, 0x1 - negs r0, r0 - cmp r9, r0 - beq _0805D2B6 - b _0805D40C -_0805D2B6: - mov r2, r8 - cmp r2, 0xF - bne _0805D2BE - b _0805D3EA -_0805D2BE: - movs r5, 0 - movs r6, 0 - ldr r7, =gBitTable - mov r4, r10 -_0805D2C6: - ldrh r0, [r4] - bl sub_805D4A8 - lsls r0, 24 - cmp r0, 0 - bne _0805D2DE - ldr r0, [r7] - mov r1, r8 - ands r0, r1 - cmp r0, 0 - bne _0805D2DE - adds r5, 0x1 -_0805D2DE: - ldrh r0, [r4] - bl sub_805D4A8 - lsls r0, 24 - lsrs r0, 24 - cmp r0, 0x1 - bne _0805D2F8 - ldr r0, [r7] - mov r2, r8 - ands r0, r2 - cmp r0, 0 - bne _0805D2F8 - adds r5, 0x10 -_0805D2F8: - ldrh r0, [r4] - bl sub_805D4A8 - lsls r0, 24 - lsrs r0, 24 - cmp r0, 0x2 - bne _0805D316 - ldr r0, [r7] - mov r1, r8 - ands r0, r1 - cmp r0, 0 - bne _0805D316 - movs r2, 0x80 - lsls r2, 1 - adds r5, r2 -_0805D316: - adds r7, 0x4 - adds r4, 0x2 - ldr r0, =gBitTable - adds r0, 0xC - cmp r7, r0 - ble _0805D2C6 - movs r1, 0xF - ands r1, r5 - cmp r1, 0x1 - ble _0805D32C - adds r6, 0x1 -_0805D32C: - movs r0, 0xF0 - ands r0, r5 - cmp r0, 0x1F - ble _0805D336 - adds r6, 0x1 -_0805D336: - ldr r2, =0x000001ff - cmp r0, r2 - ble _0805D33E - adds r6, 0x1 -_0805D33E: - cmp r6, 0x1 - bgt _0805D346 - cmp r6, 0 - bne _0805D388 -_0805D346: - ldr r5, =gBitTable -_0805D348: - bl Random - lsls r0, 16 - movs r1, 0xC0 - lsls r1, 10 - ands r1, r0 - lsrs r4, r1, 16 - lsls r0, r4, 2 - adds r0, r5 - ldr r0, [r0] - mov r1, r8 - ands r0, r1 - cmp r0, 0 - bne _0805D366 - mov r9, r4 -_0805D366: - movs r0, 0x1 - negs r0, r0 - cmp r9, r0 - beq _0805D348 - b _0805D3D4 - .pool -_0805D388: - cmp r1, 0x1 - ble _0805D38E - movs r6, 0 -_0805D38E: - cmp r0, 0x1F - ble _0805D394 - movs r6, 0x1 -_0805D394: - cmp r0, r2 - ble _0805D39A - movs r6, 0x2 -_0805D39A: - bl Random - lsls r0, 16 - movs r1, 0xC0 - lsls r1, 10 - ands r1, r0 - lsrs r4, r1, 16 - ldr r1, =gBitTable - lsls r0, r4, 2 - adds r0, r1 - ldr r0, [r0] - mov r2, r8 - ands r0, r2 - cmp r0, 0 - bne _0805D3CC - lsls r0, r4, 1 - add r0, r10 - ldrh r0, [r0] - bl sub_805D4A8 - lsls r0, 24 - lsrs r0, 24 - cmp r6, r0 - bne _0805D3CC - mov r9, r4 -_0805D3CC: - movs r0, 0x1 - negs r0, r0 - cmp r9, r0 - beq _0805D39A -_0805D3D4: - bl Random - lsls r0, 16 - lsrs r0, 16 - movs r1, 0x64 - bl __umodsi3 - lsls r0, 16 - lsrs r0, 16 - cmp r0, 0x31 - bls _0805D40C -_0805D3EA: - ldr r2, =gProtectStructs - ldr r0, =gActiveBank - ldrb r1, [r0] - lsls r1, 4 - adds r1, r2 - ldrb r0, [r1, 0x2] - movs r2, 0x10 - orrs r0, r2 - strb r0, [r1, 0x2] - movs r0, 0 - b _0805D496 - .pool -_0805D40C: - mov r1, r9 - lsls r0, r1, 1 - mov r2, r10 - adds r1, r2, r0 - ldrh r0, [r1] - cmp r0, 0xAE - bne _0805D434 - ldrb r0, [r2, 0x12] - movs r5, 0 - cmp r0, 0x7 - beq _0805D442 - ldrb r1, [r2, 0x13] - movs r0, 0x7 - eors r1, r0 - negs r0, r1 - orrs r0, r1 - asrs r5, r0, 31 - movs r0, 0x10 - ands r5, r0 - b _0805D442 -_0805D434: - ldr r2, =gBattleMoves - ldrh r1, [r1] - lsls r0, r1, 1 - adds r0, r1 - lsls r0, 2 - adds r0, r2 - ldrb r5, [r0, 0x6] -_0805D442: - movs r0, 0x10 - ands r0, r5 - cmp r0, 0 - beq _0805D45C - ldr r0, =gActiveBank - ldrb r0, [r0] - lsls r0, 8 - b _0805D48A - .pool -_0805D45C: - cmp r5, 0 - bne _0805D470 - bl sub_805D5F4 - lsls r0, 16 - lsrs r0, 16 - mov r2, r9 - orrs r2, r0 - mov r9, r2 - b _0805D490 -_0805D470: - ldr r0, =gActiveBank - ldrb r0, [r0] - bl GetBankIdentity - adds r1, r0, 0 - movs r2, 0x1 - movs r0, 0x1 - ands r0, r1 - eors r0, r2 - bl GetBankByIdentity - lsls r0, 24 - lsrs r0, 16 -_0805D48A: - mov r1, r9 - orrs r1, r0 - mov r9, r1 -_0805D490: - mov r2, r9 - lsls r0, r2, 16 - lsrs r0, 16 -_0805D496: - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end ChooseMoveAndTargetInBattlePalace - - thumb_func_start sub_805D4A8 -sub_805D4A8: @ 805D4A8 - push {lr} - lsls r0, 16 - lsrs r2, r0, 16 - ldr r1, =gBattleMoves - lsls r0, r2, 1 - adds r0, r2 - lsls r0, 2 - adds r0, r1 - ldrb r0, [r0, 0x6] - adds r3, r1, 0 - cmp r0, 0x40 - bls _0805D4C2 - b _0805D5EE -_0805D4C2: - lsls r0, 2 - ldr r1, =_0805D4D4 - adds r0, r1 - ldr r0, [r0] - mov pc, r0 - .pool - .align 2, 0 -_0805D4D4: - .4byte _0805D5D8 - .4byte _0805D5E6 - .4byte _0805D5D8 - .4byte _0805D5EE - .4byte _0805D5D8 - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5D8 - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EA - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5D8 - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5EE - .4byte _0805D5E6 -_0805D5D8: - lsls r0, r2, 1 - adds r0, r2 - lsls r0, 2 - adds r0, r3 - ldrb r0, [r0, 0x1] - cmp r0, 0 - bne _0805D5EE -_0805D5E6: - movs r0, 0x2 - b _0805D5F0 -_0805D5EA: - movs r0, 0x1 - b _0805D5F0 -_0805D5EE: - movs r0, 0 -_0805D5F0: - pop {r1} - bx r1 - thumb_func_end sub_805D4A8 - - thumb_func_start sub_805D5F4 -sub_805D5F4: @ 805D5F4 - push {r4-r7,lr} - mov r7, r9 - mov r6, r8 - push {r6,r7} - ldr r0, =gBattleTypeFlags - ldr r0, [r0] - movs r1, 0x1 - ands r0, r1 - cmp r0, 0 - beq _0805D6FA - ldr r0, =gActiveBank - ldrb r0, [r0] - bl GetBankSide - lsls r0, 24 - cmp r0, 0 - bne _0805D630 - movs r0, 0x1 - bl GetBankByIdentity - lsls r0, 24 - lsrs r0, 24 - mov r8, r0 - movs r0, 0x3 - b _0805D63E - .pool -_0805D630: - movs r0, 0 - bl GetBankByIdentity - lsls r0, 24 - lsrs r0, 24 - mov r8, r0 - movs r0, 0x2 -_0805D63E: - bl GetBankByIdentity - lsls r0, 24 - lsrs r7, r0, 24 - ldr r1, =gBattleMons - movs r2, 0x58 - mov r0, r8 - muls r0, r2 - adds r5, r0, r1 - adds r0, r7, 0 - muls r0, r2 - adds r6, r0, r1 - ldrh r0, [r5, 0x28] - ldrh r3, [r6, 0x28] - cmp r0, r3 - bne _0805D680 - bl Random - adds r3, r0, 0 - ldr r0, =gActiveBank - ldrb r1, [r0] - movs r2, 0x1 - movs r0, 0x1 - ands r0, r1 - eors r0, r2 - movs r1, 0x2 - ands r1, r3 - adds r0, r1 - b _0805D702 - .pool -_0805D680: - ldr r4, =gUnknown_0831C604 - ldr r0, =gActiveBank - mov r9, r0 - ldrb r0, [r0] - muls r0, r2 - adds r1, 0x48 - adds r0, r1 - ldr r0, [r0] - bl GetNatureFromPersonality - lsls r0, 24 - lsrs r0, 24 - adds r0, r4 - ldrb r4, [r0] - cmp r4, 0x1 - beq _0805D6CC - cmp r4, 0x1 - bgt _0805D6B4 - cmp r4, 0 - beq _0805D6BA - b _0805D6FA - .pool -_0805D6B4: - cmp r4, 0x2 - beq _0805D6DE - b _0805D6FA -_0805D6BA: - ldrh r0, [r5, 0x28] - ldrh r6, [r6, 0x28] - cmp r0, r6 - bls _0805D6C8 - mov r1, r8 - lsls r0, r1, 8 - b _0805D704 -_0805D6C8: - lsls r0, r7, 8 - b _0805D704 -_0805D6CC: - ldrh r0, [r5, 0x28] - ldrh r6, [r6, 0x28] - cmp r0, r6 - bcs _0805D6DA - mov r3, r8 - lsls r0, r3, 8 - b _0805D704 -_0805D6DA: - lsls r0, r7, 8 - b _0805D704 -_0805D6DE: - bl Random - adds r3, r0, 0 - mov r0, r9 - ldrb r1, [r0] - movs r2, 0x1 - movs r0, 0x1 - ands r0, r1 - eors r0, r2 - ands r4, r3 - adds r0, r4 - lsls r0, 24 - lsrs r0, 16 - b _0805D704 -_0805D6FA: - ldr r0, =gActiveBank - ldrb r1, [r0] - movs r0, 0x1 - eors r0, r1 -_0805D702: - lsls r0, 8 -_0805D704: - pop {r3,r4} - mov r8, r3 - mov r9, r4 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end sub_805D5F4 - - thumb_func_start sub_805D714 -sub_805D714: @ 805D714 - push {r4,lr} - adds r4, r0, 0 - ldrh r1, [r4, 0x30] - lsls r1, 24 - lsrs r1, 24 - ldr r2, =gSprites - lsls r0, r1, 4 - adds r0, r1 - lsls r0, 2 - adds r1, r0, r2 - adds r0, r1, 0 - adds r0, 0x3F - ldrb r3, [r0] - lsls r0, r3, 26 - cmp r0, 0 - bge _0805D766 - adds r0, r1, 0 - adds r0, 0x3E - ldrb r0, [r0] - lsls r0, 29 - cmp r0, 0 - blt _0805D766 - adds r2, r1, 0 - adds r2, 0x2C - ldrb r1, [r2] - lsls r0, r1, 25 - cmp r0, 0 - bge _0805D75C - movs r0, 0x41 - negs r0, r0 - ands r0, r1 - strb r0, [r2] - b _0805D766 - .pool -_0805D75C: - lsls r0, r3, 27 - cmp r0, 0 - bge _0805D766 - ldr r0, =SpriteCallbackDummy - str r0, [r4, 0x1C] -_0805D766: - pop {r4} - pop {r0} - bx r0 - .pool - thumb_func_end sub_805D714 - - thumb_func_start sub_805D770 -sub_805D770: @ 805D770 - push {r4,lr} - adds r4, r0, 0 - lsls r1, 24 - adds r3, r4, 0 - adds r3, 0x2C - ldrb r0, [r3] - movs r2, 0x40 - orrs r0, r2 - strb r0, [r3] - ldr r0, =SpriteCallbackDummy - str r0, [r4, 0x1C] - cmp r1, 0 - bne _0805D798 - adds r0, r4, 0 - movs r1, 0x1 - bl StartSpriteAffineAnim - b _0805D7A0 - .pool -_0805D798: - adds r0, r4, 0 - movs r1, 0x1 - bl StartSpriteAffineAnim -_0805D7A0: - adds r0, r4, 0 - bl AnimateSprite - pop {r4} - pop {r0} - bx r0 - thumb_func_end sub_805D770 - - thumb_func_start sub_805D7AC -sub_805D7AC: @ 805D7AC - push {lr} - adds r2, r0, 0 - ldr r0, =gUnknown_020243FC - ldrh r1, [r0] - movs r0, 0x1 - ands r0, r1 - cmp r0, 0 - bne _0805D7E4 - ldrh r0, [r2, 0x2E] - ldrh r1, [r2, 0x24] - adds r0, r1 - strh r0, [r2, 0x24] - lsls r0, 16 - cmp r0, 0 - bne _0805D7E4 - movs r1, 0x26 - ldrsh r0, [r2, r1] - cmp r0, 0 - beq _0805D7E0 - ldr r0, =sub_805D7EC - b _0805D7E2 - .pool -_0805D7E0: - ldr r0, =SpriteCallbackDummy -_0805D7E2: - str r0, [r2, 0x1C] -_0805D7E4: - pop {r0} - bx r0 - .pool - thumb_func_end sub_805D7AC - - thumb_func_start sub_805D7EC -sub_805D7EC: @ 805D7EC - push {lr} - adds r1, r0, 0 - ldrh r0, [r1, 0x26] - subs r0, 0x2 - strh r0, [r1, 0x26] - lsls r0, 16 - cmp r0, 0 - bne _0805D800 - ldr r0, =SpriteCallbackDummy - str r0, [r1, 0x1C] -_0805D800: - pop {r0} - bx r0 - .pool - thumb_func_end sub_805D7EC thumb_func_start DoStatusAnimation DoStatusAnimation: @ 805D808 diff --git a/include/battle.h b/include/battle.h index 1916f450ab..d958294b16 100644 --- a/include/battle.h +++ b/include/battle.h @@ -944,6 +944,12 @@ struct BattleAnimationInfo u8 field_9_x20 : 1; u8 field_9_x40 : 1; u8 field_9_x80 : 1; + u8 field_A; + u8 field_B; + u8 field_C; + u8 field_D; + u8 field_E; + u8 field_F; }; struct BattleHealthboxInfo diff --git a/include/battle_script_commands.h b/include/battle_script_commands.h index a466abc041..ebfeabcccc 100644 --- a/include/battle_script_commands.h +++ b/include/battle_script_commands.h @@ -57,6 +57,6 @@ void HandleBattleWindow(u8 xStart, u8 yStart, u8 xEnd, u8 yEnd, u8 flags); bool8 UproarWakeUpCheck(u8 bank); extern void (* const gBattleScriptingCommandsTable[])(void); -extern const u8 gUnknown_0831C494[]; +extern const u8 gUnknown_0831C494[][4]; #endif // GUARD_BATTLE_SCRIPT_COMMANDS_H diff --git a/include/malloc.h b/include/malloc.h index f3a7317975..003ee1f033 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -5,6 +5,12 @@ #define calloc(ct, sz) AllocZeroed((ct) * (sz)) #define free Free +#define FREE_AND_SET_NULL(ptr) \ +{ \ + free(ptr); \ + ptr = NULL; \ +} + extern u8 gHeap[]; void *Alloc(u32 size); void *AllocZeroed(u32 size); diff --git a/ld_script.txt b/ld_script.txt index 6ea68b1c71..3cec6fd8f9 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -62,6 +62,7 @@ SECTIONS { src/battle_script_commands.o(.text); asm/battle_5.o(.text); src/battle_controller_player.o(.text); + src/battle_7.o(.text); asm/battle_7.o(.text); src/battle_controller_opponent.o(.text); src/battle_ai_switch_items.o(.text); diff --git a/src/battle_7.c b/src/battle_7.c new file mode 100644 index 0000000000..8e21f9df5f --- /dev/null +++ b/src/battle_7.c @@ -0,0 +1,284 @@ +#include "global.h" +#include "battle.h" +#include "battle_controllers.h" +#include "battle_ai_script_commands.h" +#include "malloc.h" +#include "rng.h" +#include "util.h" +#include "pokemon.h" +#include "moves.h" + +extern u8 gBattleBufferA[BATTLE_BANKS_COUNT][0x200]; +extern u8 gActiveBank; +extern u16 gUnknown_020243FC; +extern struct BattlePokemon gBattleMons[BATTLE_BANKS_COUNT]; + +extern const struct BattleMove gBattleMoves[]; +extern const u8 gUnknown_0831C604[]; + +// this file's functions +u8 sub_805D4A8(u16 move); +u16 BattlePalaceGetTargetRetValue(void); +void sub_805D7EC(struct Sprite *sprite); + +void AllocateBattleSpritesData(void) +{ + gBattleSpritesDataPtr = AllocZeroed(sizeof(struct BattleSpriteData)); + gBattleSpritesDataPtr->bankData = AllocZeroed(sizeof(struct BattleSpriteInfo) * BATTLE_BANKS_COUNT); + gBattleSpritesDataPtr->healthBoxesData = AllocZeroed(sizeof(struct BattleHealthboxInfo) * BATTLE_BANKS_COUNT); + gBattleSpritesDataPtr->animationData = AllocZeroed(sizeof(struct BattleAnimationInfo)); + gBattleSpritesDataPtr->battleBars = AllocZeroed(sizeof(struct BattleBarInfo) * BATTLE_BANKS_COUNT); +} + +void FreeBattleSpritesData(void) +{ + if (gBattleSpritesDataPtr == NULL) + return; + + FREE_AND_SET_NULL(gBattleSpritesDataPtr->battleBars); + FREE_AND_SET_NULL(gBattleSpritesDataPtr->animationData); + FREE_AND_SET_NULL(gBattleSpritesDataPtr->healthBoxesData); + FREE_AND_SET_NULL(gBattleSpritesDataPtr->bankData); + FREE_AND_SET_NULL(gBattleSpritesDataPtr); +} + +u16 ChooseMoveAndTargetInBattlePalace(void) +{ + s32 i, var1, var2; + s32 chosenMoveId = -1; + struct ChooseMoveStruct *moveInfo = (struct ChooseMoveStruct*)(&gBattleBufferA[gActiveBank][4]); + u8 unusableMovesBits = CheckMoveLimitations(gActiveBank, 0, 0xFF); + s32 percent = Random() % 100; + + i = (gBattleStruct->field_92 & gBitTable[gActiveBank]) ? 2 : 0; + var2 = i; + var1 = i + 2; + + for (; i < var1; i++) + { + if (gUnknown_0831C494[GetNatureFromPersonality(gBattleMons[gActiveBank].personality)][i] > percent) + break; + } + + percent = i - var2; + if (i == var1) + percent = 2; + + for (var2 = 0, i = 0; i < 4; i++) + { + if (moveInfo->moves[i] == MOVE_NONE) + break; + if (percent == sub_805D4A8(moveInfo->moves[i]) && moveInfo->currentPp[i] != 0) + var2 |= gBitTable[i]; + } + + if (var2 != 0) + { + gBattleStruct->field_92 &= 0xF; + gBattleStruct->field_92 |= (var2 << 4); + BattleAI_SetupAIData(var2); + chosenMoveId = BattleAI_ChooseMoveOrAction(); + } + + if (chosenMoveId == -1) + { + if (unusableMovesBits != 0xF) + { + var1 = 0, var2 = 0; + + for (i = 0; i < 4; i++) + { + if (sub_805D4A8(moveInfo->moves[i]) == 0 && !(gBitTable[i] & unusableMovesBits)) + var1 += 0x1; + if (sub_805D4A8(moveInfo->moves[i]) == 1 && !(gBitTable[i] & unusableMovesBits)) + var1 += 0x10; + if (sub_805D4A8(moveInfo->moves[i]) == 2 && !(gBitTable[i] & unusableMovesBits)) + var1 += 0x100; + } + + if ((var1 & 0xF) > 1) + var2++; + if ((var1 & 0xF0) > 0x1F) + var2++; + if ((var1 & 0xF0) > 0x1FF) + var2++; + + if (var2 > 1 || var2 == 0) + { + do + { + i = Random() % 4; + if (!(gBitTable[i] & unusableMovesBits)) + chosenMoveId = i; + } while (chosenMoveId == -1); + } + else + { + if ((var1 & 0xF) > 1) + var2 = 0; + if ((var1 & 0xF0) > 0x1F) + var2 = 1; + if ((var1 & 0xF0) > 0x1FF) + var2 = 2; + + do + { + i = Random() % 4; + if (!(gBitTable[i] & unusableMovesBits) && var2 == sub_805D4A8(moveInfo->moves[i])) + chosenMoveId = i; + } while (chosenMoveId == -1); + } + + if (Random() % 100 > 49) + { + gProtectStructs[gActiveBank].flag_x10 = 1; + return 0; + } + } + else + { + gProtectStructs[gActiveBank].flag_x10 = 1; + return 0; + } + } + + if (moveInfo->moves[chosenMoveId] == MOVE_CURSE) + { + if (moveInfo->monType1 != TYPE_GHOST && moveInfo->monType2 != TYPE_GHOST) + var1 = MOVE_TARGET_x10; + else + var1 = MOVE_TARGET_SELECTED; + } + else + { + var1 = gBattleMoves[moveInfo->moves[chosenMoveId]].target; + } + + if (var1 & MOVE_TARGET_x10) + chosenMoveId |= (gActiveBank << 8); + else if (var1 == MOVE_TARGET_SELECTED) + chosenMoveId |= (BattlePalaceGetTargetRetValue()); + else + chosenMoveId |= (GetBankByIdentity((GetBankIdentity(gActiveBank) & BIT_SIDE) ^ BIT_SIDE) << 8); + + return chosenMoveId; +} + +u8 sub_805D4A8(u16 move) +{ + switch (gBattleMoves[move].target) + { + case MOVE_TARGET_SELECTED: + case MOVE_TARGET_USER: + case MOVE_TARGET_RANDOM: + case MOVE_TARGET_BOTH: + case MOVE_TARGET_FOES_AND_ALLY: + if (gBattleMoves[move].power == 0) + return 2; + else + return 0; + break; + case MOVE_TARGET_DEPENDS: + case MOVE_TARGET_OPPONENTS_FIELD: + return 2; + case MOVE_TARGET_x10: + return 1; + default: + return 0; + } +} + +u16 BattlePalaceGetTargetRetValue(void) +{ + if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) + { + u8 opposing1, opposing2; + + if (GetBankSide(gActiveBank) == SIDE_PLAYER) + { + opposing1 = GetBankByIdentity(IDENTITY_OPPONENT_MON1); + opposing2 = GetBankByIdentity(IDENTITY_OPPONENT_MON2); + } + else + { + opposing1 = GetBankByIdentity(IDENTITY_PLAYER_MON1); + opposing2 = GetBankByIdentity(IDENTITY_PLAYER_MON2); + } + + if (gBattleMons[opposing1].hp == gBattleMons[opposing2].hp) + return (((gActiveBank & BIT_SIDE) ^ BIT_SIDE) + (Random() & 2)) << 8; + + switch (gUnknown_0831C604[GetNatureFromPersonality(gBattleMons[gActiveBank].personality)]) + { + case 0: + if (gBattleMons[opposing1].hp > gBattleMons[opposing2].hp) + return opposing1 << 8; + else + return opposing2 << 8; + case 1: + if (gBattleMons[opposing1].hp < gBattleMons[opposing2].hp) + return opposing1 << 8; + else + return opposing2 << 8; + case 2: + return (((gActiveBank & BIT_SIDE) ^ BIT_SIDE) + (Random() & 2)) << 8; + } + } + + return (gActiveBank ^ BIT_SIDE) << 8; +} + +void sub_805D714(struct Sprite *sprite) +{ + u8 spriteId = sprite->data1; + + if (!gSprites[spriteId].affineAnimEnded) + return; + if (gSprites[spriteId].invisible) + return; + + if (gSprites[spriteId].animPaused) + { + gSprites[spriteId].animPaused = 0; + } + else + { + if (gSprites[spriteId].animEnded) + sprite->callback = SpriteCallbackDummy; + } +} + +void sub_805D770(struct Sprite *sprite, bool8 arg1) +{ + sprite->animPaused = 1; + sprite->callback = SpriteCallbackDummy; + + if (!arg1) + StartSpriteAffineAnim(sprite, 1); + else + StartSpriteAffineAnim(sprite, 1); + + AnimateSprite(sprite); +} + +void sub_805D7AC(struct Sprite *sprite) +{ + if (!(gUnknown_020243FC & 1)) + { + sprite->pos2.x += sprite->data0; + if (sprite->pos2.x == 0) + { + if (sprite->pos2.y != 0) + sprite->callback = sub_805D7EC; + else + sprite->callback = SpriteCallbackDummy; + } + } +} + +void sub_805D7EC(struct Sprite *sprite) +{ + sprite->pos2.y -= 2; + if (sprite->pos2.y == 0) + sprite->callback = SpriteCallbackDummy; +}