From f57e040e1626fb2d624c354beb539c8fbaf01b4c Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Fri, 31 Dec 2021 16:46:58 +0200 Subject: [PATCH 1/5] Decompiled getEmptyEntity - still not completely matching --- src/entity.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++-- src/interrupts.c | 2 +- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/entity.c b/src/entity.c index 41d9b4f1..0c69b550 100644 --- a/src/entity.c +++ b/src/entity.c @@ -22,6 +22,7 @@ extern void sub_08017744(Entity*); extern void UnloadHitbox(); extern void sub_0804AA1C(); +void ClearDeletedEntity(Entity*); extern void _ClearAndUpdateEntities(); extern void UpdateEntities_arm(u32); @@ -232,7 +233,73 @@ void EraseAllEntities() { gUnk_03000000.unk[1].unk6 = 1; } -ASM_FUNC("./asm/getEmptyEntity.s", Entity* GetEmptyEntity()); +extern Entity gUnk_030015A0[0x4848]; +extern Entity gUnk_03003BE0; + +NONMATCH("./asm/getEmptyEntity.s", Entity* GetEmptyEntity()) { + u8 flags_ip; + Entity* ptr; + Entity* end; + Entity* rv; + Entity* currentEnt; + LinkedList* nextList; + + LinkedList* listPtr; + LinkedList* endListPtr; + + if (gEntCount <= 0x46) { + ptr = gUnk_030015A0; + end = ptr + ARRAY_COUNT(gUnk_030015A0); + + do { + if (ptr->prev == 0) { + return ptr; + } + } while (++ptr < end); + } + + ptr = &gPlayerEntity; + + do { + if ((s32)ptr->prev < 0 && (ptr->flags & 0xc) && gUpdateContext.current_entity != ptr) { + ClearDeletedEntity(ptr); + return ptr; + } + } while (++ptr < &gUnk_03003BE0); + + flags_ip = 0; + rv = NULL; + listPtr = gEntityLists; + endListPtr = listPtr + ARRAY_COUNT(gEntityLists); + + do { + currentEnt = listPtr->first; + nextList = listPtr + 1; + while ((u32)currentEnt != (u32)listPtr) { + if (currentEnt->kind != MANAGER && (currentEnt->flags & 0x1c) == flags_ip && + gUpdateContext.current_entity != currentEnt) { + flags_ip = currentEnt->flags & 0x1c; + rv = currentEnt; + } + currentEnt = currentEnt->next; + } + + listPtr = nextList; + } while (listPtr < endListPtr); + + if (rv) { + DeleteEntity(rv); + ClearDeletedEntity(rv); + } + + return rv; +} +END_NONMATCH + +//#define NDEPRECATED + +#include "entity.h" +#include "functions.h" extern Entity gUnk_030011E8[7]; @@ -308,8 +375,6 @@ void DeleteEntity(Entity* ent) { } } -void ClearDeletedEntity(Entity*); - void ClearAllDeletedEntities(void) { Entity* ent = &gPlayerEntity; do { diff --git a/src/interrupts.c b/src/interrupts.c index 90556e62..2993fffb 100644 --- a/src/interrupts.c +++ b/src/interrupts.c @@ -17,7 +17,7 @@ extern u16* gUnk_02025EB0; extern u16* gUnk_0200B650; extern u8 gUpdateVisibleTiles; extern u8 gUnk_03003DF0[]; -extern u8 gUnk_03003BE0; +extern Entity gUnk_03003BE0; extern Entity* gPlayerClones[3]; extern u16 gUnk_080B2CD8[]; From d141b5bfbf9635d22706dd7ba9505d4426ed525f Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Sat, 1 Jan 2022 02:09:40 +0200 Subject: [PATCH 2/5] Fix condition --- src/entity.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entity.c b/src/entity.c index 0c69b550..710cd609 100644 --- a/src/entity.c +++ b/src/entity.c @@ -276,7 +276,7 @@ NONMATCH("./asm/getEmptyEntity.s", Entity* GetEmptyEntity()) { currentEnt = listPtr->first; nextList = listPtr + 1; while ((u32)currentEnt != (u32)listPtr) { - if (currentEnt->kind != MANAGER && (currentEnt->flags & 0x1c) == flags_ip && + if (currentEnt->kind != MANAGER && (currentEnt->flags & 0x1c) < flags_ip && gUpdateContext.current_entity != currentEnt) { flags_ip = currentEnt->flags & 0x1c; rv = currentEnt; From 442206651de3e8ce376b71008c681547db70dd26 Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Sat, 1 Jan 2022 02:13:05 +0200 Subject: [PATCH 3/5] getEmptyEntity: More fixes --- src/entity.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/entity.c b/src/entity.c index 710cd609..4b527a73 100644 --- a/src/entity.c +++ b/src/entity.c @@ -233,7 +233,7 @@ void EraseAllEntities() { gUnk_03000000.unk[1].unk6 = 1; } -extern Entity gUnk_030015A0[0x4848]; +extern Entity gUnk_030015A0[0x48]; extern Entity gUnk_03003BE0; NONMATCH("./asm/getEmptyEntity.s", Entity* GetEmptyEntity()) { @@ -261,7 +261,7 @@ NONMATCH("./asm/getEmptyEntity.s", Entity* GetEmptyEntity()) { ptr = &gPlayerEntity; do { - if ((s32)ptr->prev < 0 && (ptr->flags & 0xc) && gUpdateContext.current_entity != ptr) { + if ((s32)ptr->prev < 0 && (ptr->flags & 0xc) && ptr != gUpdateContext.current_entity) { ClearDeletedEntity(ptr); return ptr; } @@ -276,7 +276,8 @@ NONMATCH("./asm/getEmptyEntity.s", Entity* GetEmptyEntity()) { currentEnt = listPtr->first; nextList = listPtr + 1; while ((u32)currentEnt != (u32)listPtr) { - if (currentEnt->kind != MANAGER && (currentEnt->flags & 0x1c) < flags_ip && + if (currentEnt->kind != MANAGER + && flags_ip < (currentEnt->flags & 0x1c) && gUpdateContext.current_entity != currentEnt) { flags_ip = currentEnt->flags & 0x1c; rv = currentEnt; From 1ff79f2c7e7bd57c288ed53de4aeff2d1484b313 Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Sat, 1 Jan 2022 02:25:27 +0200 Subject: [PATCH 4/5] clang format --- src/entity.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/entity.c b/src/entity.c index 4b527a73..930fb6d6 100644 --- a/src/entity.c +++ b/src/entity.c @@ -276,8 +276,7 @@ NONMATCH("./asm/getEmptyEntity.s", Entity* GetEmptyEntity()) { currentEnt = listPtr->first; nextList = listPtr + 1; while ((u32)currentEnt != (u32)listPtr) { - if (currentEnt->kind != MANAGER - && flags_ip < (currentEnt->flags & 0x1c) && + if (currentEnt->kind != MANAGER && flags_ip < (currentEnt->flags & 0x1c) && gUpdateContext.current_entity != currentEnt) { flags_ip = currentEnt->flags & 0x1c; rv = currentEnt; From 447782f21c011e421f84728a38ed7e1007aca758 Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Sun, 2 Jan 2022 03:26:38 +0200 Subject: [PATCH 5/5] Remove uintended includes --- src/entity.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/entity.c b/src/entity.c index 930fb6d6..79040e23 100644 --- a/src/entity.c +++ b/src/entity.c @@ -296,11 +296,6 @@ NONMATCH("./asm/getEmptyEntity.s", Entity* GetEmptyEntity()) { } END_NONMATCH -//#define NDEPRECATED - -#include "entity.h" -#include "functions.h" - extern Entity gUnk_030011E8[7]; Entity* sub_0805E744(void) {