From 9498dfa71c811b09f319bc9c9656c08f3182c9dc Mon Sep 17 00:00:00 2001 From: scnorton Date: Wed, 6 Sep 2017 14:25:17 -0400 Subject: [PATCH] InitFieldObjectStateFromTemplate (nonmatching, same as ruby) --- asm/field_map_obj.s | 192 ------------------------------- include/field_map_obj.h | 1 + include/global.fieldmap.h | 3 +- include/global.h | 1 + src/field_map_obj.c | 230 +++++++++++++++++++++++++++++++++++++- 5 files changed, 233 insertions(+), 194 deletions(-) diff --git a/asm/field_map_obj.s b/asm/field_map_obj.s index 01e04f04b0..8afd73a019 100644 --- a/asm/field_map_obj.s +++ b/asm/field_map_obj.s @@ -5,198 +5,6 @@ .text - thumb_func_start GetFieldObjectIdByLocalId -@ u8 GetFieldObjectIdByLocalId(u8 localId) -GetFieldObjectIdByLocalId: @ 808D608 - push {r4,lr} - lsls r0, 24 - lsrs r3, r0, 24 - movs r2, 0 - ldr r4, =gMapObjects -_0808D612: - lsls r0, r2, 3 - adds r0, r2 - lsls r0, 2 - adds r1, r0, r4 - ldrb r0, [r1] - lsls r0, 31 - cmp r0, 0 - beq _0808D630 - ldrb r0, [r1, 0x8] - cmp r0, r3 - bne _0808D630 - adds r0, r2, 0 - b _0808D63C - .pool -_0808D630: - adds r0, r2, 0x1 - lsls r0, 24 - lsrs r2, r0, 24 - cmp r2, 0xF - bls _0808D612 - movs r0, 0x10 -_0808D63C: - pop {r4} - pop {r1} - bx r1 - thumb_func_end GetFieldObjectIdByLocalId - - thumb_func_start InitFieldObjectStateFromTemplate -@ u8 InitFieldObjectStateFromTemplate(struct FieldObjectTemplate *fieldObjectTemplate, u8 mapId, u8 mapGroupId) -InitFieldObjectStateFromTemplate: @ 808D644 - push {r4-r7,lr} - mov r7, r9 - mov r6, r8 - push {r6,r7} - sub sp, 0x4 - adds r5, r0, 0 - lsls r1, 24 - lsrs r6, r1, 24 - lsls r2, 24 - lsrs r7, r2, 24 - ldrb r0, [r5] - adds r1, r6, 0 - adds r2, r7, 0 - mov r3, sp - bl GetAvailableFieldObjectSlot - lsls r0, 24 - cmp r0, 0 - beq _0808D66E - movs r0, 0x10 - b _0808D762 -_0808D66E: - mov r0, sp - ldrb r1, [r0] - lsls r0, r1, 3 - adds r0, r1 - lsls r0, 2 - ldr r1, =gMapObjects - adds r4, r0, r1 - adds r0, r4, 0 - bl npc_clear_ids_and_state - ldrh r3, [r5, 0x4] - adds r3, 0x7 - lsls r3, 16 - lsrs r3, 16 - ldrh r2, [r5, 0x6] - adds r2, 0x7 - lsls r2, 16 - lsrs r2, 16 - ldrb r0, [r4] - movs r1, 0x1 - orrs r0, r1 - movs r1, 0x4 - orrs r0, r1 - strb r0, [r4] - ldrb r0, [r5, 0x1] - strb r0, [r4, 0x5] - ldrb r0, [r5, 0x9] - strb r0, [r4, 0x6] - ldrb r0, [r5] - strb r0, [r4, 0x8] - strb r6, [r4, 0x9] - strb r7, [r4, 0xA] - strh r3, [r4, 0xC] - strh r2, [r4, 0xE] - strh r3, [r4, 0x10] - strh r2, [r4, 0x12] - strh r3, [r4, 0x14] - strh r2, [r4, 0x16] - ldrb r0, [r5, 0x8] - movs r7, 0xF - adds r1, r7, 0 - ands r1, r0 - ldrb r2, [r4, 0xB] - movs r0, 0x10 - negs r0, r0 - mov r8, r0 - ands r0, r2 - orrs r0, r1 - strb r0, [r4, 0xB] - ldrb r1, [r5, 0x8] - lsls r1, 4 - ands r0, r7 - orrs r0, r1 - strb r0, [r4, 0xB] - ldrb r1, [r5, 0xA] - lsls r1, 28 - movs r0, 0xF - mov r9, r0 - lsrs r1, 28 - ldrb r2, [r4, 0x19] - mov r0, r8 - ands r0, r2 - orrs r0, r1 - strb r0, [r4, 0x19] - ldrb r1, [r5, 0xA] - lsrs r1, 4 - lsls r1, 4 - ands r0, r7 - orrs r0, r1 - strb r0, [r4, 0x19] - ldrh r0, [r5, 0xC] - strb r0, [r4, 0x7] - ldrh r0, [r5, 0xE] - strb r0, [r4, 0x1D] - ldr r1, =gUnknown_085055CD - ldrb r0, [r5, 0x9] - adds r0, r1 - ldrb r1, [r0] - adds r0, r4, 0 - adds r0, 0x20 - strb r1, [r0] - ldrb r1, [r0] - adds r0, r4, 0 - bl FieldObjectSetDirection - adds r0, r4, 0 - bl FieldObjectHandleDynamicGraphicsId - ldr r1, =gUnknown_0850557C - ldrb r0, [r4, 0x6] - adds r0, r1 - ldrb r0, [r0] - cmp r0, 0 - beq _0808D75E - ldrb r2, [r4, 0x19] - adds r0, r7, 0 - ands r0, r2 - cmp r0, 0 - bne _0808D746 - lsls r0, r2, 28 - lsrs r0, 28 - adds r0, 0x1 - mov r1, r9 - ands r0, r1 - mov r1, r8 - ands r1, r2 - orrs r1, r0 - strb r1, [r4, 0x19] -_0808D746: - ldrb r2, [r4, 0x19] - movs r0, 0xF0 - ands r0, r2 - cmp r0, 0 - bne _0808D75E - lsrs r1, r2, 4 - adds r1, 0x1 - lsls r1, 4 - adds r0, r7, 0 - ands r0, r2 - orrs r0, r1 - strb r0, [r4, 0x19] -_0808D75E: - mov r0, sp - ldrb r0, [r0] -_0808D762: - add sp, 0x4 - pop {r3,r4} - mov r8, r3 - mov r9, r4 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end InitFieldObjectStateFromTemplate - thumb_func_start sub_808D77C sub_808D77C: @ 808D77C push {r4-r7,lr} diff --git a/include/field_map_obj.h b/include/field_map_obj.h index 93a90d2c8e..3381d847be 100644 --- a/include/field_map_obj.h +++ b/include/field_map_obj.h @@ -11,6 +11,7 @@ void sub_808D438(void); u8 GetFieldObjectIdByLocalIdAndMap(u8, u8, u8); bool8 TryGetFieldObjectIdByLocalIdAndMap(u8, u8, u8, u8 *); u8 GetFieldObjectIdByXY(s16, s16); +void FieldObjectSetDirection(struct MapObject *, u8); // Exported data declarations diff --git a/include/global.fieldmap.h b/include/global.fieldmap.h index 3e0c7729a5..b0a36aa55a 100644 --- a/include/global.fieldmap.h +++ b/include/global.fieldmap.h @@ -226,7 +226,8 @@ struct MapObject /*0x14*/ struct Coords16 coords3; /*0x18*/ u8 mapobj_unk_18:4; //current direction? /*0x18*/ u8 placeholder18:4; - /*0x19*/ u8 mapobj_unk_19; + /*0x19*/ u8 mapobj_unk_19:4; + /*0x19*/ u8 mapobj_unk_19b:4; /*0x1A*/ u8 mapobj_unk_1A; /*0x1B*/ u8 mapobj_unk_1B; /*0x1C*/ u8 mapobj_unk_1C; diff --git a/include/global.h b/include/global.h index f9c5f68bea..0c0f864700 100644 --- a/include/global.h +++ b/include/global.h @@ -8,6 +8,7 @@ // to help in decompiling #define asm_comment(x) asm volatile("@ -- " x " -- ") +#define asm_unified(x) asm(".syntax unified\n" x "\n.syntax divided") #ifdef __APPLE__ void memset(void *, int, size_t); diff --git a/src/field_map_obj.c b/src/field_map_obj.c index 1b4ba0b7f6..b97d5c0dc9 100644 --- a/src/field_map_obj.c +++ b/src/field_map_obj.c @@ -15,14 +15,19 @@ /*static*/ void sub_808D450(void); /*static*/ u8 GetFieldObjectIdByLocalId(u8); /*static*/ u8 GetFieldObjectIdByLocalIdAndMapInternal(u8, u8, u8); +/*static*/ bool8 GetAvailableFieldObjectSlot(u16, u8, u8, u8 *); +/*static*/ void FieldObjectHandleDynamicGraphicsId(struct MapObject *); // ROM data +const u8 gUnknown_085055CD[0x53]; +const u8 gUnknown_0850557C[0x51]; + // Code /*static*/ void npc_clear_ids_and_state(struct MapObject *mapObject) { - memset(mapObject, 0, sizeof(struct MapObject)); + *mapObject = (struct MapObject){}; mapObject->localId = 0xFF; mapObject->mapNum = -1; mapObject->mapGroup = -1; @@ -124,3 +129,226 @@ u8 GetFieldObjectIdByLocalIdAndMapInternal(u8 localId, u8 mapId, u8 mapGroupId) } return ARRAY_COUNT(gMapObjects); } + +u8 GetFieldObjectIdByLocalId(u8 localId) +{ + u8 i; + + for (i = 0; i < ARRAY_COUNT(gMapObjects); i ++) + { + if (gMapObjects[i].active && gMapObjects[i].localId == localId) + { + return i; + } + } + return ARRAY_COUNT(gMapObjects); +} + +// This function has the same nonmatching quirk as in Ruby/Sapphire. +#ifdef NONMATCHING +u8 InitFieldObjectStateFromTemplate(struct MapObjectTemplate *template, u8 mapId, u8 mapGroupId) +{ + u8 slot; + struct MapObject *mapObject; + u16 x; + u16 y; + + if (GetAvailableFieldObjectSlot(template->localId, mapId, mapGroupId, &slot)) + { + return ARRAY_COUNT(gMapObjects); + } + mapObject = &gMapObjects[slot]; + npc_clear_ids_and_state(mapObject); + x = template->x + 7; + y = template->y + 7; + mapObject->active = TRUE; + mapObject->mapobj_bit_2 = TRUE; + mapObject->graphicsId = template->graphicsId; + mapObject->animPattern = template->movementType; + mapObject->localId = template->localId; + mapObject->mapNum = mapId; + mapObject->mapGroup = mapGroupId; + mapObject->coords1.x = x; + mapObject->coords1.y = y; + mapObject->coords2.x = x; + mapObject->coords2.y = y; + mapObject->coords3.x = x; + mapObject->coords3.y = y; + mapObject->mapobj_unk_0B_0 = template->elevation; + mapObject->elevation = template->elevation; + mapObject->mapobj_unk_19 = template->unkA_0; + mapObject->mapobj_unk_19b = template->unkA_4; + mapObject->trainerType = template->unkC; + mapObject->trainerRange_berryTreeId = template->unkE; + mapObject->mapobj_unk_20 = gUnknown_085055CD[template->movementType]; + FieldObjectSetDirection(mapObject, mapObject->mapobj_unk_20); + FieldObjectHandleDynamicGraphicsId(mapObject); + if (gUnknown_0850557C[mapObject->animPattern]) + { + if (mapObject->mapobj_unk_19 == 0) + { + mapObject->mapobj_unk_19 ++; + } + if (mapObject->mapobj_unk_19b == 0) + { + mapObject->mapobj_unk_19b ++; + } + } + return slot; +} +#else +__attribute__((naked)) u8 InitFieldObjectStateFromTemplate(struct MapObjectTemplate *template, u8 mapId, u8 mapGroupId) +{ + asm_unified("\tpush {r4-r7,lr}\n" + "\tmov r7, r9\n" + "\tmov r6, r8\n" + "\tpush {r6,r7}\n" + "\tsub sp, 0x4\n" + "\tadds r5, r0, 0\n" + "\tlsls r1, 24\n" + "\tlsrs r6, r1, 24\n" + "\tlsls r2, 24\n" + "\tlsrs r7, r2, 24\n" + "\tldrb r0, [r5]\n" + "\tadds r1, r6, 0\n" + "\tadds r2, r7, 0\n" + "\tmov r3, sp\n" + "\tbl GetAvailableFieldObjectSlot\n" + "\tlsls r0, 24\n" + "\tcmp r0, 0\n" + "\tbeq _0808D66E\n" + "\tmovs r0, 0x10\n" + "\tb _0808D762\n" + "_0808D66E:\n" + "\tmov r0, sp\n" + "\tldrb r1, [r0]\n" + "\tlsls r0, r1, 3\n" + "\tadds r0, r1\n" + "\tlsls r0, 2\n" + "\tldr r1, =gMapObjects\n" + "\tadds r4, r0, r1\n" + "\tadds r0, r4, 0\n" + "\tbl npc_clear_ids_and_state\n" + "\tldrh r3, [r5, 0x4]\n" + "\tadds r3, 0x7\n" + "\tlsls r3, 16\n" + "\tlsrs r3, 16\n" + "\tldrh r2, [r5, 0x6]\n" + "\tadds r2, 0x7\n" + "\tlsls r2, 16\n" + "\tlsrs r2, 16\n" + "\tldrb r0, [r4]\n" + "\tmovs r1, 0x1\n" + "\torrs r0, r1\n" + "\tmovs r1, 0x4\n" + "\torrs r0, r1\n" + "\tstrb r0, [r4]\n" + "\tldrb r0, [r5, 0x1]\n" + "\tstrb r0, [r4, 0x5]\n" + "\tldrb r0, [r5, 0x9]\n" + "\tstrb r0, [r4, 0x6]\n" + "\tldrb r0, [r5]\n" + "\tstrb r0, [r4, 0x8]\n" + "\tstrb r6, [r4, 0x9]\n" + "\tstrb r7, [r4, 0xA]\n" + "\tstrh r3, [r4, 0xC]\n" + "\tstrh r2, [r4, 0xE]\n" + "\tstrh r3, [r4, 0x10]\n" + "\tstrh r2, [r4, 0x12]\n" + "\tstrh r3, [r4, 0x14]\n" + "\tstrh r2, [r4, 0x16]\n" + "\tldrb r0, [r5, 0x8]\n" + "\tmovs r7, 0xF\n" + "\tadds r1, r7, 0\n" + "\tands r1, r0\n" + "\tldrb r2, [r4, 0xB]\n" + "\tmovs r0, 0x10\n" + "\tnegs r0, r0\n" + "\tmov r8, r0\n" + "\tands r0, r2\n" + "\torrs r0, r1\n" + "\tstrb r0, [r4, 0xB]\n" + "\tldrb r1, [r5, 0x8]\n" + "\tlsls r1, 4\n" + "\tands r0, r7\n" + "\torrs r0, r1\n" + "\tstrb r0, [r4, 0xB]\n" + "\tldrb r1, [r5, 0xA]\n" + "\tlsls r1, 28\n" + "\tmovs r0, 0xF\n" + "\tmov r9, r0\n" + "\tlsrs r1, 28\n" + "\tldrb r2, [r4, 0x19]\n" + "\tmov r0, r8\n" + "\tands r0, r2\n" + "\torrs r0, r1\n" + "\tstrb r0, [r4, 0x19]\n" + "\tldrb r1, [r5, 0xA]\n" + "\tlsrs r1, 4\n" + "\tlsls r1, 4\n" + "\tands r0, r7\n" + "\torrs r0, r1\n" + "\tstrb r0, [r4, 0x19]\n" + "\tldrh r0, [r5, 0xC]\n" + "\tstrb r0, [r4, 0x7]\n" + "\tldrh r0, [r5, 0xE]\n" + "\tstrb r0, [r4, 0x1D]\n" + "\tldr r1, =gUnknown_085055CD\n" + "\tldrb r0, [r5, 0x9]\n" + "\tadds r0, r1\n" + "\tldrb r1, [r0]\n" + "\tadds r0, r4, 0\n" + "\tadds r0, 0x20\n" + "\tstrb r1, [r0]\n" + "\tldrb r1, [r0]\n" + "\tadds r0, r4, 0\n" + "\tbl FieldObjectSetDirection\n" + "\tadds r0, r4, 0\n" + "\tbl FieldObjectHandleDynamicGraphicsId\n" + "\tldr r1, =gUnknown_0850557C\n" + "\tldrb r0, [r4, 0x6]\n" + "\tadds r0, r1\n" + "\tldrb r0, [r0]\n" + "\tcmp r0, 0\n" + "\tbeq _0808D75E\n" + "\tldrb r2, [r4, 0x19]\n" + "\tadds r0, r7, 0\n" + "\tands r0, r2\n" + "\tcmp r0, 0\n" + "\tbne _0808D746\n" + "\tlsls r0, r2, 28\n" + "\tlsrs r0, 28\n" + "\tadds r0, 0x1\n" + "\tmov r1, r9\n" + "\tands r0, r1\n" + "\tmov r1, r8\n" + "\tands r1, r2\n" + "\torrs r1, r0\n" + "\tstrb r1, [r4, 0x19]\n" + "_0808D746:\n" + "\tldrb r2, [r4, 0x19]\n" + "\tmovs r0, 0xF0\n" + "\tands r0, r2\n" + "\tcmp r0, 0\n" + "\tbne _0808D75E\n" + "\tlsrs r1, r2, 4\n" + "\tadds r1, 0x1\n" + "\tlsls r1, 4\n" + "\tadds r0, r7, 0\n" + "\tands r0, r2\n" + "\torrs r0, r1\n" + "\tstrb r0, [r4, 0x19]\n" + "_0808D75E:\n" + "\tmov r0, sp\n" + "\tldrb r0, [r0]\n" + "_0808D762:\n" + "\tadd sp, 0x4\n" + "\tpop {r3,r4}\n" + "\tmov r8, r3\n" + "\tmov r9, r4\n" + "\tpop {r4-r7}\n" + "\tpop {r1}\n" + "\tbx r1\n" + ".pool"); +} +#endif