Skip to content

Commit

Permalink
Merge pull request #269 from DizzyEggg/sub_80869E4
Browse files Browse the repository at this point in the history
Match sub_80869E4 and sub_8002A70 with correct params
  • Loading branch information
SethBarberee authored Nov 27, 2024
2 parents 55459b9 + d0cc867 commit 33a7303
Show file tree
Hide file tree
Showing 8 changed files with 587 additions and 683 deletions.
619 changes: 0 additions & 619 deletions asm/code_8002774.s

Large diffs are not rendered by default.

497 changes: 497 additions & 0 deletions asm/code_8002774_2.s

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions include/code_8002774.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef GUARD_CODE_8002774_H
#define GUARD_CODE_8002774_H

s32 sub_8002A70(s32 a0, s32 a1, s32 a2);
s32 sub_8002B04(s32, s32);

#endif
2 changes: 1 addition & 1 deletion include/code_80869E4.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ void sub_8086854(void);
void sub_80868F4(Entity *entity);
void sub_8086910(Entity *entity);
void sub_808692C(void);
void sub_80869E4(Entity *, s32, u8, s8 direction);
void sub_80869E4(struct Entity *entity, s32 a1, u8 a2, s32 _someDirection);

#endif // GUARD_CODE_80869E4_H
2 changes: 2 additions & 0 deletions ld_script.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ SECTIONS {
asm/event_flag.o(.text);
src/event_flag.o(.text);
asm/code_8002774.o(.text);
src/code_8002774.o(.text);
asm/code_8002774_2.o(.text);
src/memory.o(.text);
asm/memory.o(.text);
src/memory_mid.o(.text);
Expand Down
68 changes: 68 additions & 0 deletions src/code_8002774.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "global.h"
#include "code_8002774.h"
#include "constants/direction.h"

s32 sub_8002A70(s32 _direction1, s32 _direction2, s32 _caseId)
{
s32 direction1 = (s8)(_direction1);
s32 direction2 = (s8)(_direction2);
s32 caseId = (u8)(_caseId);

if (direction1 == direction2)
return direction1;

switch (caseId) {
case 1:
if (direction1 >= 0) {
direction1 += 7;
direction1 &= DIRECTION_MASK;
}
break;
case 2:
if (direction1 >= 0) {
direction1 += 1;
direction1 &= DIRECTION_MASK;
}
break;
case 3:
if (direction1 >= 0) {
direction1 += 6;
direction1 &= DIRECTION_MASK;
}
break;
case 4:
if (direction1 >= 0) {
direction1 += 2;
direction1 &= DIRECTION_MASK;
}
break;
case 11:
direction1 = sub_8002B04(direction1, direction2);
// Fallthrough
case 10:
direction1 = sub_8002B04(direction1, direction2);
break;
}
return direction1;
}

s32 sub_8002B04(s32 _direction1, s32 _direction2)
{
s32 direction1 = (s8)(_direction1);
s32 direction2 = (s8)(_direction2);

if (direction2 != -1)
{
s32 newDirection = ((direction2 + NUM_DIRECTIONS) - direction1) % NUM_DIRECTIONS;

if (newDirection != 0) {
if (newDirection < 4)
newDirection = (direction1 + 1) % NUM_DIRECTIONS;
else
newDirection = (direction1 + 7) % NUM_DIRECTIONS;

direction1 = (s8)newDirection;
}
}
return direction1;
}
74 changes: 11 additions & 63 deletions src/code_80869E4.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "global.h"
#include "code_800E9E4.h"
#include "code_803E46C.h"
#include "code_803E668.h"
#include "code_806CD90.h"
Expand Down Expand Up @@ -474,71 +475,18 @@ void SpriteLookAroundEffect(Entity *entity)
sub_803E708(15, 70);
}

#ifdef NONMATCHING // 100% match, but params for sub_8002A70 are disputed... https://decomp.me/scratch/E3iF1
void sub_80869E4(Entity *a0, s32 a1, u8 a2, s8 a3)
void sub_80869E4(struct Entity *entity, s32 a1, u8 a2, s32 _someDirection)
{
u32 r4;
s32 tmp;
struct EntityInfo* info;
s32 someDirection = (s8) _someDirection;
struct EntityInfo *info = GetEntInfo(entity);
s32 direction = (s8) info->action.direction;

tmp = (s8)a3;

info = GetEntInfo(a0);
r4 = (s8)info->action.direction;
while (r4 != tmp) {
r4 = sub_8002A70(r4, tmp, a2);
info->action.direction = r4 % 8;
sub_806CE68(a0, info->action.direction);
while (direction != someDirection) {
direction = (s8) sub_8002A70(direction, someDirection, a2);
info->action.direction = direction;
info->action.direction &= DIRECTION_MASK;
sub_806CE68(entity, info->action.direction);
sub_803E708(a1, 70);
}
}
#else
NAKED
void sub_80869E4(Entity *a0, s32 a1, u8 a2, s8 a3)
{
asm_unified(
"push {r4-r7,lr}\n"
"\tmov r7, r9\n"
"\tmov r6, r8\n"
"\tpush {r6,r7}\n"
"\tadds r7, r0, 0\n"
"\tmov r9, r1\n"
"\tlsls r2, 24\n"
"\tlsrs r2, 24\n"
"\tmov r8, r2\n"
"\tlsls r3, 24\n"
"\tasrs r6, r3, 24\n"
"\tldr r0, [r7, 0x70]\n"
"\tadds r0, 0x46\n"
"\tmovs r4, 0\n"
"\tldrsb r4, [r0, r4]\n"
"\tcmp r4, r6\n"
"\tbeq _08086A30\n"
"\tadds r5, r0, 0\n"
"_08086A08:\n"
"\tadds r0, r4, 0\n"
"\tadds r1, r6, 0\n"
"\tmov r2, r8\n"
"\tbl sub_8002A70\n"
"\tlsls r0, 24\n"
"\tasrs r4, r0, 24\n"
"\tmovs r0, 0x7\n"
"\tands r0, r4\n"
"\tstrb r0, [r5]\n"
"\tldrb r1, [r5]\n"
"\tadds r0, r7, 0\n"
"\tbl sub_806CE68\n"
"\tmov r0, r9\n"
"\tmovs r1, 0x46\n"
"\tbl sub_803E708\n"
"\tcmp r4, r6\n"
"\tbne _08086A08\n"
"_08086A30:\n"
"\tpop {r3,r4}\n"
"\tmov r8, r3\n"
"\tmov r9, r4\n"
"\tpop {r4-r7}\n"
"\tpop {r0}\n"
"\tbx r0");
}
#endif // NONMATCHING

1 change: 1 addition & 0 deletions src/dungeon_move.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "code_803E724.h"
#include "code_803E46C.h"
#include "code_8041AD0.h"
#include "code_800E9E4.h"

extern void sub_80429C8(Entity *r0);
extern bool8 sub_8045888(Entity *r0);
Expand Down

0 comments on commit 33a7303

Please sign in to comment.