diff --git a/asm/script_movement.s b/asm/script_movement.s deleted file mode 100644 index 8c6a6257c..000000000 --- a/asm/script_movement.s +++ /dev/null @@ -1,577 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - thumb_func_start ScriptMovement_StartObjectMovementScript -ScriptMovement_StartObjectMovementScript: @ 80D32E8 - push {r4,lr} - sub sp, 0x4 - adds r4, r3, 0 - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - lsrs r1, 24 - lsls r2, 24 - lsrs r2, 24 - mov r3, sp - bl TryGetFieldObjectIdByLocalIdAndMap - lsls r0, 24 - cmp r0, 0 - beq _080D330A - movs r0, 0x1 - b _080D3332 -_080D330A: - ldr r0, =sub_80D3660 - bl FuncIsActiveTask - lsls r0, 24 - cmp r0, 0 - bne _080D331C - movs r0, 0x32 - bl sub_80D33AC -_080D331C: - bl sub_80D33F4 - lsls r0, 24 - lsrs r0, 24 - mov r1, sp - ldrb r1, [r1] - adds r2, r4, 0 - bl sub_80D3408 - lsls r0, 24 - lsrs r0, 24 -_080D3332: - add sp, 0x4 - pop {r4} - pop {r1} - bx r1 - .pool - thumb_func_end ScriptMovement_StartObjectMovementScript - - thumb_func_start ScriptMovement_IsObjectMovementFinished -ScriptMovement_IsObjectMovementFinished: @ 80D3340 - push {r4,lr} - sub sp, 0x4 - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - lsrs r1, 24 - lsls r2, 24 - lsrs r2, 24 - mov r3, sp - bl TryGetFieldObjectIdByLocalIdAndMap - lsls r0, 24 - cmp r0, 0 - bne _080D3382 - bl sub_80D33F4 - lsls r0, 24 - lsrs r4, r0, 24 - mov r0, sp - ldrb r1, [r0] - adds r0, r4, 0 - bl sub_80D3474 - lsls r0, 24 - lsrs r1, r0, 24 - cmp r1, 0x10 - beq _080D3382 - adds r0, r4, 0 - bl sub_80D3584 - lsls r0, 24 - lsrs r0, 24 - b _080D3384 -_080D3382: - movs r0, 0x1 -_080D3384: - add sp, 0x4 - pop {r4} - pop {r1} - bx r1 - thumb_func_end ScriptMovement_IsObjectMovementFinished - - thumb_func_start sub_80D338C -sub_80D338C: @ 80D338C - push {r4,lr} - bl sub_80D33F4 - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0xFF - beq _080D33A6 - adds r0, r4, 0 - bl sub_80D361C - adds r0, r4, 0 - bl DestroyTask -_080D33A6: - pop {r4} - pop {r0} - bx r0 - thumb_func_end sub_80D338C - - thumb_func_start sub_80D33AC -sub_80D33AC: @ 80D33AC - push {r4,r5,lr} - adds r1, r0, 0 - lsls r1, 24 - lsrs r1, 24 - ldr r0, =sub_80D3660 - bl CreateTask - lsls r0, 24 - lsrs r0, 24 - movs r2, 0x1 - lsls r1, r0, 2 - adds r1, r0 - lsls r3, r1, 3 - ldr r5, =gTasks + 0x8 - ldr r0, =0x0000ffff - adds r4, r0, 0 -_080D33CC: - lsls r0, r2, 1 - adds r0, r3 - adds r0, r5 - ldrh r1, [r0] - orrs r1, r4 - strh r1, [r0] - adds r0, r2, 0x1 - lsls r0, 24 - lsrs r2, r0, 24 - cmp r2, 0xF - bls _080D33CC - pop {r4,r5} - pop {r0} - bx r0 - .pool - thumb_func_end sub_80D33AC - - thumb_func_start sub_80D33F4 -sub_80D33F4: @ 80D33F4 - push {lr} - ldr r0, =sub_80D3660 - bl FindTaskIdByFunc - lsls r0, 24 - lsrs r0, 24 - pop {r1} - bx r1 - .pool - thumb_func_end sub_80D33F4 - - thumb_func_start sub_80D3408 -sub_80D3408: @ 80D3408 - push {r4-r7,lr} - mov r7, r9 - mov r6, r8 - push {r6,r7} - mov r9, r2 - lsls r0, 24 - lsrs r5, r0, 24 - adds r7, r5, 0 - lsls r1, 24 - lsrs r6, r1, 24 - mov r8, r6 - adds r0, r5, 0 - adds r1, r6, 0 - bl sub_80D3474 - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0x10 - beq _080D3444 - adds r0, r5, 0 - adds r1, r4, 0 - bl sub_80D3584 - lsls r0, 24 - cmp r0, 0 - beq _080D3464 - adds r0, r5, 0 - adds r1, r4, 0 - adds r2, r6, 0 - b _080D345A -_080D3444: - adds r0, r7, 0 - movs r1, 0xFF - bl sub_80D3474 - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0x10 - beq _080D3464 - adds r0, r7, 0 - adds r1, r4, 0 - mov r2, r8 -_080D345A: - mov r3, r9 - bl sub_80D35DC - movs r0, 0 - b _080D3466 -_080D3464: - movs r0, 0x1 -_080D3466: - pop {r3,r4} - mov r8, r3 - mov r9, r4 - pop {r4-r7} - pop {r1} - bx r1 - thumb_func_end sub_80D3408 - - thumb_func_start sub_80D3474 -sub_80D3474: @ 80D3474 - push {lr} - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - lsrs r3, r1, 24 - lsls r1, r0, 2 - adds r1, r0 - lsls r1, 3 - ldr r0, =gTasks + 0x8 - adds r1, r0 - adds r1, 0x2 - movs r2, 0 -_080D348C: - ldrb r0, [r1] - cmp r0, r3 - bne _080D349C - adds r0, r2, 0 - b _080D34AA - .pool -_080D349C: - adds r0, r2, 0x1 - lsls r0, 24 - lsrs r2, r0, 24 - adds r1, 0x1 - cmp r2, 0xF - bls _080D348C - movs r0, 0x10 -_080D34AA: - pop {r1} - bx r1 - thumb_func_end sub_80D3474 - - thumb_func_start sub_80D34B0 -sub_80D34B0: @ 80D34B0 - push {lr} - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - lsrs r3, r1, 24 - lsls r1, r0, 2 - adds r1, r0 - lsls r1, 3 - ldr r0, =gTasks + 0x8 - adds r1, r0 - adds r1, 0x2 - str r1, [r2] - movs r0, 0 - cmp r0, r3 - bcs _080D34DC -_080D34CE: - adds r0, 0x1 - lsls r0, 24 - lsrs r0, 24 - adds r1, 0x1 - cmp r0, r3 - bcc _080D34CE - str r1, [r2] -_080D34DC: - pop {r0} - bx r0 - .pool - thumb_func_end sub_80D34B0 - - thumb_func_start sub_80D34E4 -sub_80D34E4: @ 80D34E4 - push {r4,lr} - sub sp, 0x4 - adds r4, r2, 0 - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - lsrs r1, 24 - lsls r4, 24 - lsrs r4, 24 - mov r2, sp - bl sub_80D34B0 - ldr r0, [sp] - strb r4, [r0] - add sp, 0x4 - pop {r4} - pop {r0} - bx r0 - thumb_func_end sub_80D34E4 - - thumb_func_start sub_80D3508 -sub_80D3508: @ 80D3508 - push {r4,lr} - sub sp, 0x4 - adds r4, r2, 0 - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - lsrs r1, 24 - mov r2, sp - bl sub_80D34B0 - ldr r0, [sp] - ldrb r0, [r0] - strb r0, [r4] - add sp, 0x4 - pop {r4} - pop {r0} - bx r0 - thumb_func_end sub_80D3508 - - thumb_func_start sub_80D352C -sub_80D352C: @ 80D352C - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - ldr r2, =gBitTable - lsrs r1, 22 - adds r1, r2 - ldr r2, [r1] - mvns r2, r2 - lsls r2, 16 - lsrs r2, 16 - ldr r3, =gTasks - lsls r1, r0, 2 - adds r1, r0 - lsls r1, 3 - adds r1, r3 - ldrh r0, [r1, 0x8] - ands r2, r0 - strh r2, [r1, 0x8] - bx lr - .pool - thumb_func_end sub_80D352C - - thumb_func_start sub_80D355C -sub_80D355C: @ 80D355C - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - ldr r3, =gTasks - lsls r2, r0, 2 - adds r2, r0 - lsls r2, 3 - adds r2, r3 - ldr r0, =gBitTable - lsrs r1, 22 - adds r1, r0 - ldr r0, [r1] - ldrh r1, [r2, 0x8] - orrs r0, r1 - strh r0, [r2, 0x8] - bx lr - .pool - thumb_func_end sub_80D355C - - thumb_func_start sub_80D3584 -sub_80D3584: @ 80D3584 - push {lr} - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - ldr r3, =gTasks - lsls r2, r0, 2 - adds r2, r0 - lsls r2, 3 - adds r2, r3 - ldr r0, =gBitTable - lsrs r1, 22 - adds r1, r0 - ldr r0, [r1] - ldrh r1, [r2, 0x8] - ands r0, r1 - cmp r0, 0 - bne _080D35B4 - movs r0, 0 - b _080D35B6 - .pool -_080D35B4: - movs r0, 0x1 -_080D35B6: - pop {r1} - bx r1 - thumb_func_end sub_80D3584 - - thumb_func_start npc_obj_offscreen_culling_and_flag_update -npc_obj_offscreen_culling_and_flag_update: @ 80D35BC - lsls r0, 24 - ldr r2, =gUnknown_02039D90 - lsrs r0, 22 - adds r0, r2 - str r1, [r0] - bx lr - .pool - thumb_func_end npc_obj_offscreen_culling_and_flag_update - - thumb_func_start sub_80D35CC -sub_80D35CC: @ 80D35CC - lsls r0, 24 - ldr r1, =gUnknown_02039D90 - lsrs r0, 22 - adds r0, r1 - ldr r0, [r0] - bx lr - .pool - thumb_func_end sub_80D35CC - - thumb_func_start sub_80D35DC -sub_80D35DC: @ 80D35DC - push {r4-r6,lr} - mov r6, r8 - push {r6} - adds r5, r0, 0 - adds r4, r1, 0 - adds r6, r2, 0 - mov r8, r3 - lsls r5, 24 - lsrs r5, 24 - lsls r4, 24 - lsrs r4, 24 - lsls r6, 24 - lsrs r6, 24 - adds r0, r5, 0 - adds r1, r4, 0 - bl sub_80D352C - adds r0, r4, 0 - mov r1, r8 - bl npc_obj_offscreen_culling_and_flag_update - adds r0, r5, 0 - adds r1, r4, 0 - adds r2, r6, 0 - bl sub_80D34E4 - pop {r3} - mov r8, r3 - pop {r4-r6} - pop {r0} - bx r0 - thumb_func_end sub_80D35DC - - thumb_func_start sub_80D361C -sub_80D361C: @ 80D361C - push {r4,r5,lr} - lsls r0, 24 - lsrs r0, 24 - lsls r1, r0, 2 - adds r1, r0 - lsls r1, 3 - ldr r0, =gTasks + 0x8 - adds r1, r0 - adds r4, r1, 0x2 - movs r5, 0 -_080D3630: - ldrb r0, [r4] - cmp r0, 0xFF - beq _080D3646 - adds r1, r0, 0 - lsls r0, r1, 3 - adds r0, r1 - lsls r0, 2 - ldr r1, =gMapObjects - adds r0, r1 - bl npc_sync_anim_pause_bits -_080D3646: - adds r0, r5, 0x1 - lsls r0, 24 - lsrs r5, r0, 24 - adds r4, 0x1 - cmp r5, 0xF - bls _080D3630 - pop {r4,r5} - pop {r0} - bx r0 - .pool - thumb_func_end sub_80D361C - - thumb_func_start sub_80D3660 -sub_80D3660: @ 80D3660 - push {r4-r7,lr} - sub sp, 0x4 - lsls r0, 24 - lsrs r7, r0, 24 - movs r5, 0 - mov r6, sp -_080D366C: - adds r0, r7, 0 - adds r1, r5, 0 - mov r2, sp - bl sub_80D3508 - ldrb r0, [r6] - cmp r0, 0xFF - beq _080D3690 - adds r4, r0, 0 - adds r0, r5, 0 - bl sub_80D35CC - adds r3, r0, 0 - adds r0, r7, 0 - adds r1, r5, 0 - adds r2, r4, 0 - bl sub_80D36A4 -_080D3690: - adds r0, r5, 0x1 - lsls r0, 24 - lsrs r5, r0, 24 - cmp r5, 0xF - bls _080D366C - add sp, 0x4 - pop {r4-r7} - pop {r0} - bx r0 - thumb_func_end sub_80D3660 - - thumb_func_start sub_80D36A4 -sub_80D36A4: @ 80D36A4 - push {r4-r7,lr} - mov r7, r8 - push {r7} - adds r5, r3, 0 - lsls r0, 24 - lsrs r7, r0, 24 - lsls r1, 24 - lsrs r6, r1, 24 - mov r8, r6 - lsls r2, 24 - lsrs r2, 24 - lsls r0, r2, 3 - adds r0, r2 - lsls r0, 2 - ldr r1, =gMapObjects - adds r4, r0, r1 - adds r0, r4, 0 - bl FieldObjectIsSpecialAnimActive - lsls r0, 24 - cmp r0, 0 - beq _080D36DC - adds r0, r4, 0 - bl FieldObjectClearAnimIfSpecialAnimFinished - lsls r0, 24 - cmp r0, 0 - beq _080D370E -_080D36DC: - ldrb r1, [r5] - cmp r1, 0xFE - bne _080D36F8 - adds r0, r7, 0 - adds r1, r6, 0 - bl sub_80D355C - adds r0, r4, 0 - bl FreezeMapObject - b _080D370E - .pool -_080D36F8: - adds r0, r4, 0 - bl FieldObjectSetSpecialAnim - lsls r0, 24 - cmp r0, 0 - bne _080D370E - adds r5, 0x1 - mov r0, r8 - adds r1, r5, 0 - bl npc_obj_offscreen_culling_and_flag_update -_080D370E: - pop {r3} - mov r8, r3 - pop {r4-r7} - pop {r0} - bx r0 - thumb_func_end sub_80D36A4 - - .align 2, 0 @ Don't pad with nop. diff --git a/ld_script.txt b/ld_script.txt index 5df04c0c3..96717763d 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -131,7 +131,7 @@ SECTIONS { asm/pokemon_storage_system.o(.text); src/pokemon_icon.o(.text); asm/pokemon_icon.o(.text); - asm/script_movement.o(.text); + src/script_movement.o(.text); asm/fldeff_cut.o(.text); src/mail_data.o(.text); src/map_name_popup.o(.text); diff --git a/src/script_movement.c b/src/script_movement.c new file mode 100644 index 000000000..bbeb0eedc --- /dev/null +++ b/src/script_movement.c @@ -0,0 +1,233 @@ +#include "global.h" +#include "script_movement.h" +#include "field_map_obj.h" +#include "field_map_obj_helpers.h" +#include "task.h" +#include "util.h" + +// static functions +static void sub_80D33AC(u8); +static u8 sub_80D33F4(void); +static bool8 sub_80D3408(u8, u8, const u8 *); +static u8 sub_80D3474(u8, u8); +static bool8 sub_80D3584(u8, u8); +static void sub_80D35DC(u8, u8, u8, const u8 *); +static void UnfreezeObjects(u8); +static void sub_80D3660(u8); +static void sub_80A2490(u8, u8, u8, const u8 *); + +// EWRAM_DATA +static EWRAM_DATA const u8 *gUnknown_02039D90[16] = {0}; + +// text +bool8 ScriptMovement_StartObjectMovementScript(u8 localId, u8 mapNum, u8 mapGroup, const u8 *movementScript) +{ + u8 mapObjId; + + if (TryGetFieldObjectIdByLocalIdAndMap(localId, mapNum, mapGroup, &mapObjId)) + return TRUE; + if (!FuncIsActiveTask(sub_80D3660)) + sub_80D33AC(50); + return sub_80D3408(sub_80D33F4(), mapObjId, movementScript); +} + +bool8 ScriptMovement_IsObjectMovementFinished(u8 localId, u8 mapNum, u8 mapBank) +{ + u8 mapObjId; + u8 r4; + u8 r1; + + if (TryGetFieldObjectIdByLocalIdAndMap(localId, mapNum, mapBank, &mapObjId)) + return TRUE; + r4 = sub_80D33F4(); + r1 = sub_80D3474(r4, mapObjId); + if (r1 == 16) + return TRUE; + return sub_80D3584(r4, r1); +} + +void sub_80D338C(void) +{ + u8 taskId; + + taskId = sub_80D33F4(); + if (taskId != 0xFF) + { + UnfreezeObjects(taskId); + DestroyTask(taskId); + } +} + + +static void sub_80D33AC(u8 priority) +{ + u8 taskId; + u8 i; + + taskId = CreateTask(sub_80D3660, priority); + for (i = 1; i < 16; i++) + gTasks[taskId].data[i] = 0xFFFF; +} + +static u8 sub_80D33F4(void) +{ + return FindTaskIdByFunc(sub_80D3660); +} + +static bool8 sub_80D3408(u8 taskId, u8 mapObjId, const u8 *movementScript) +{ + u8 r4; + + r4 = sub_80D3474(taskId, mapObjId); + if (r4 != 16) + { + if (sub_80D3584(taskId, r4) == 0) + { + return TRUE; + } + else + { + sub_80D35DC(taskId, r4, mapObjId, movementScript); + return FALSE; + } + } + r4 = sub_80D3474(taskId, 0xFF); + if (r4 == 16) + { + return TRUE; + } + else + { + sub_80D35DC(taskId, r4, mapObjId, movementScript); + return FALSE; + } +} + +static u8 sub_80D3474(u8 taskId, u8 b) +{ + u8 *ptr; + u8 i; + + ptr = (u8 *)&gTasks[taskId].data[1]; + for (i = 0; i < 16; i++, ptr++) + { + if (*ptr == b) + return i; + } + return 16; +} + +static void sub_80D34B0(u8 taskId, u8 b, u8 **c) +{ + u8 i; + + *c = (u8 *)&gTasks[taskId].data[1]; + for (i = 0; i < b; i++, (*c)++) + ; +} + +static void sub_80D34E4(u8 taskId, u8 b, u8 c) +{ + u8 *ptr; + + sub_80D34B0(taskId, b, &ptr); + *ptr = c; //what is this supposed to do? +} + +static void sub_80D3508(u8 taskId, u8 b, u8 *c) +{ + u8 *ptr; + + sub_80D34B0(taskId, b, &ptr); + *c = *ptr; +} + +static void sub_80D352C(u8 a, u8 b) +{ + u16 var = ~gBitTable[b]; + + gTasks[a].data[0] &= var; +} + +static void sub_80D355C(u8 taskId, u8 b) +{ + gTasks[taskId].data[0] |= gBitTable[b]; +} + +static bool8 sub_80D3584(u8 taskId, u8 b) +{ + u16 var = (u16)gTasks[taskId].data[0] & gBitTable[b]; + + if (var != 0) + return TRUE; + else + return FALSE; +} + +static void npc_obj_offscreen_culling_and_flag_update(u8 a, const u8 *movementScript) +{ + gUnknown_02039D90[a] = movementScript; +} + +static const u8 *sub_80D35CC(u8 a) +{ + return gUnknown_02039D90[a]; +} + +static void sub_80D35DC(u8 taskId, u8 b, u8 mapObjId, const u8 *movementScript) +{ + sub_80D352C(taskId, b); + npc_obj_offscreen_culling_and_flag_update(b, movementScript); + sub_80D34E4(taskId, b, mapObjId); +} + +static void UnfreezeObjects(u8 taskId) +{ + u8 *pMapObjId; + u8 i; + + pMapObjId = (u8 *)&gTasks[taskId].data[1]; + for (i = 0; i < 16; i++, pMapObjId++) + { + if (*pMapObjId != 0xFF) + npc_sync_anim_pause_bits(&gMapObjects[*pMapObjId]); + } +} + +static void sub_80D3660(u8 taskId) +{ + u8 i; + u8 var; + + for (i = 0; i < 16; i++) + { + sub_80D3508(taskId, i, &var); + if (var != 0xFF) + sub_80A2490(taskId, i, var, sub_80D35CC(i)); + } +} + +static void sub_80A2490(u8 taskId, u8 b, u8 mapObjId, const u8 *d) +{ + u8 var; + + if (FieldObjectIsSpecialAnimActive(&gMapObjects[mapObjId]) + && !FieldObjectClearAnimIfSpecialAnimFinished(&gMapObjects[mapObjId])) + return; + + var = *d; + if (var == 0xFE) + { + sub_80D355C(taskId, b); + FreezeMapObject(&gMapObjects[mapObjId]); + } + else + { + if (!FieldObjectSetSpecialAnim(&gMapObjects[mapObjId], var)) + { + d++; + npc_obj_offscreen_culling_and_flag_update(b, d); + } + } +} + diff --git a/sym_ewram.txt b/sym_ewram.txt index 4d43ac3da..b7f3ebdf3 100644 --- a/sym_ewram.txt +++ b/sym_ewram.txt @@ -408,8 +408,7 @@ gUnknown_02039D88: @ 2039D88 gUnknown_02039D8C: @ 2039D8C .space 0x4 -gUnknown_02039D90: @ 2039D90 - .space 0x40 + .include "src/script_movement.o" gUnknown_02039DD0: @ 2039DD0 .space 0x4