Skip to content

Commit

Permalink
Merge pull request #291 from Kermalis/repairs
Browse files Browse the repository at this point in the history
Repairs
  • Loading branch information
AnonymousRandomPerson authored Dec 8, 2024
2 parents 2630c22 + 1d726bf commit 5c1d47a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 134 deletions.
1 change: 1 addition & 0 deletions asm/pokemon_2.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

.text

@ https://decomp.me/scratch/aIvCa - SpriteOAM memes
thumb_func_start InitShadowSprites
InitShadowSprites:
push {r4-r7,lr}
Expand Down
2 changes: 1 addition & 1 deletion include/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void SetSavingIconCoords(DungeonPos *);
void sub_8004E8C(unkStruct_2039DB0 *);
void sub_8005180(void);
void sub_8005304(void);
void sub_800533C(ax_pose **, UnkSpriteMem **, axdata1 *, u16 *, bool8);
void sub_800533C(ax_pose **, UnkSpriteMem **, axdata1 *, unkStruct_2039DB0 *, bool8);
void AxResInitFile(axdata *, OpenedFile *, u32, u32, u32, u32, bool8);
void AxResInit(axdata *, axmain *, u32, u32, u32, u32, bool8);
void AxResInitUnorientedFile(axdata *, OpenedFile *, u32, u32, u32, bool8);
Expand Down
9 changes: 8 additions & 1 deletion include/structs/axdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ typedef struct axdata1
u8 paletteNum;
} axdata1;

// Size: 0x2
typedef struct __attribute__((packed, aligned(2))) ax_pose_unk2
{
u8 unk0;
s8 unk1;
} ax_pose_unk2;

// size: 0xA
typedef struct __attribute__((packed, aligned(2))) ax_pose
{
/* 0x0 */ s16 sprite;
u16 unk2; // Always 0 in red (except for end markers which are 0xFFFF)
/* 0x2 */ ax_pose_unk2 unk2; // Always {0, 0} in red (except for end markers which are {0xFF, 0xFF})
/* 0x4 */ u16 flags1;
/* 0x6 */ u16 flags2;
/* 0x8 */ u16 flags3;
Expand Down
2 changes: 1 addition & 1 deletion include/structs/sprite_oam.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
typedef struct unkStruct_2039DB0
{
u16 unk0; // SpriteOAM attrib1 mask
s16 unk2; // SpriteOAM attrib2 mask
u16 unk2; // SpriteOAM attrib2 mask
u16 unk4; // SpriteOAM attrib3 mask
u16 unk6; // SpriteOAM attrib1 value
u16 unk8; // SpriteOAM attrib2 value
Expand Down
6 changes: 3 additions & 3 deletions include/structs/sprite_oam_temp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "sprite_oam.h"

// This file is here for temporary decomp notes about SpriteOAM. It can be deleted later when we figure it out.
// SpriteOAM initialization is handled by inlines. Potentially has inlines for other scenarios [for example: AddSprite()]
// SpriteOAM initialization is handled by inlines. Potentially has inlines for other scenarios [for example: AddSprite() and AddAxSprite()]

// There is lots of testing done and evidence that it most likely is not handled by macros.
// Some funcs (such as sub_8039174) can be fakematched with "{} while(0);" contexts and a temp var. Note that it's not a "do {} while(0)" but just a "{} while(0)".
Expand All @@ -14,7 +14,7 @@
// Most of the special work seems to be done on the first two u16 of SpriteOAM.
// They are usually loaded/stored separately from the second half of the struct.
// There doesn't seem to be a logical reason for SpriteOAM to be split into two substructs, but the evidence points to it.
// AddSprite() still hasn't been matched (I'm writing this Dec 4 2024), because it suggests SpriteOAM is actually two separate substructs.
// AddSprite() and AddAxSprite() still haven't been matched (I'm writing this Dec 4 2024), because they suggest SpriteOAM is actually two separate substructs.
// "volatile u32/u16" was the first thing we tried, but it produces horrible asm and doesn't make sense for Chunsoft to have used it in this context.
// The main breakthrough came from producing 99% similar asm through attrib1/attrib2 being a substruct.

Expand Down Expand Up @@ -211,7 +211,7 @@ static inline void SetSize(SpriteOAM2 *s, u32 size)

// attrib3 inline:
// For some reason this one is all combined. Also matches pretty much every time.
// It's possible the attrib1 ones are also combined like this, but not the attrib2 ones since they're called out of order or not at all.
// It's possible the attrib1 ones are also combined like this, but not the attrib2 ones since they're called out of order or omitted.
// And just like the attrib2 inlines, attempting to macro or inline further results in removed preloading.

static inline void SetAttrib3(SpriteOAM2 *s, u32 tileNum, u32 prio, u32 palNum)
Expand Down
2 changes: 1 addition & 1 deletion src/code_800558C.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern const s16 gUnknown_80B83EA[16 * 10];
static void sub_800561C(struct axMapSprite *, s32, s32, const RGB *);
const RGB *sub_8005674(struct axMapSprite *, s32);

void DoAxFrame_800558C(struct axObject *a0, s32 spriteX, s32 spriteY, u32 a3, u32 paletteNum, u16 *spriteMasks)
void DoAxFrame_800558C(struct axObject *a0, s32 spriteX, s32 spriteY, u32 a3, u32 paletteNum, unkStruct_2039DB0 *spriteMasks)
{
if (!(a0->axdata.flags >> 15))
return;
Expand Down
24 changes: 13 additions & 11 deletions src/code_8069E0C.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extern void sub_8005700(DungeonPos *a0, struct axObject *a1);
u32 EntityGetStatusSprites(Entity *entity);
void UpdateDungeonPokemonSprite(int id, short species, int status, char visible);
extern void sub_8042EC8(Entity *a0, s32 a1);
void DoAxFrame_800558C(struct axObject *a0, s32 spriteX, s32 spriteY, u32 a3, u32 paletteNum, u16 *spriteMasks);
void DoAxFrame_800558C(struct axObject *a0, s32 spriteX, s32 spriteY, u32 a3, u32 paletteNum, unkStruct_2039DB0 *spriteMasks);
extern Entity *sub_804550C(s16 a);
extern Entity *sub_80453AC(s16 id);
extern void sub_803F580(s32);
Expand Down Expand Up @@ -2059,7 +2059,7 @@ void sub_806C51C(Entity *entity)
u32 statusSprites;
bool8 r4;
u8 r7;
u16 spriteMasks[6];
unkStruct_2039DB0 spriteMasks;
s32 xSprite, ySprite;

EntityInfo *entInfo = GetEntInfo(entity);
Expand Down Expand Up @@ -2246,20 +2246,22 @@ void sub_806C51C(Entity *entity)

r4 = FALSE;
r7 = sub_806CF54(entity);
spriteMasks[0] = 0xF3FF;
spriteMasks[1] = 0xFFFF;
spriteMasks[2] = 0xF3FF;
spriteMasks[3] = 0;
spriteMasks[4] = 0;
spriteMasks[5] = gDungeon->unk181e8.unk18208 << 10;

spriteMasks.unk0 = 0xF3FF;
spriteMasks.unk2 = 0xFFFF;
spriteMasks.unk4 = 0xF3FF;
spriteMasks.unk6 = 0;
spriteMasks.unk8 = 0;
spriteMasks.unkA = gDungeon->unk181e8.unk18208 << 10;

if (entInfo->isNotTeamMember && (entInfo->apparentID == MONSTER_DEOXYS_ATTACK || entInfo->apparentID == MONSTER_DEOXYS_DEFENSE || entInfo->apparentID == MONSTER_DEOXYS_SPEED)) {
r4 = TRUE;
}
if (entInfo->invisibleClassStatus.status == STATUS_INVISIBLE && !gDungeon->unk181e8.unk1820F) {
r4 = TRUE;
}
if (r4) {
spriteMasks[3] |= 0x400;
spriteMasks.unk6 |= 0x400;
}

var_34 = 0;
Expand All @@ -2285,10 +2287,10 @@ void sub_806C51C(Entity *entity)
}

if (entity->unk22 == 0) {
DoAxFrame_800558C(&entity->axObj, x, y, y2, overworldPal, spriteMasks);
DoAxFrame_800558C(&entity->axObj, x, y, y2, overworldPal, &spriteMasks);
}
else if (entity->unk22 == 1 && (gUnknown_202EDCC & 1)) {
DoAxFrame_800558C(&entity->axObj, x, y, y2, overworldPal, spriteMasks);
DoAxFrame_800558C(&entity->axObj, x, y, y2, overworldPal, &spriteMasks);
}
}

Expand Down
126 changes: 10 additions & 116 deletions src/sprite.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,112 +156,8 @@ void sub_8004E8C(unkStruct_2039DB0 *a0)
a0->unkA = 0;
}

// https://decomp.me/scratch/VYqKb
// spriteMasks is a u16[6]
#ifdef NONMATCHING
static void AddAxSprite(ax_pose *a0, axdata1 *a1, UnkSpriteMem *a2, u16 *spriteMasks)
{
// size: 0xC
struct UnkStackFor8004EA8
{
u16 unk0;
u8 unk2;
s8 unk3;
u16 flags1;
u16 flags2;
u16 flags3;
u16 unkA;
} sp;
SpriteOAM *sprite;
u32 uVar9;
s32 r7;

s32 x;
s32 y;
s32 tileNum;
s32 earlyMask;

if (a2 != NULL)
RegisterSpriteParts_80052BC(a2);

if (sSpriteCount >= 128)
return;

sp.unk0 = a0->sprite;
*((u16 *)&sp.unk2) = a0->unk2; // ?????????
sp.flags1 = a0->flags1 & ~(0x100 | 0x200);
sp.flags2 = a0->flags2 & ~(0x200 | 0x400 | 0x800);
sp.flags3 = a0->flags3;
sp.unkA = ((a0->flags2 & (0x200 | 0x400 | 0x800)) >> 9) | ((a0->flags1 & (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80 | 0x100 | 0x200)) << 4);
sprite = sUnknown_20262A8 + sSpriteCount;
r7 = a1->unk16 + sp.unk3;

if (r7 < 0)
r7 = 0;
if (r7 > 255)
r7 = 255;

if (spriteMasks == NULL) {
sprite->attrib1 = sp.flags1;
sprite->attrib2 = sp.flags2;
sprite->attrib3 = sp.flags3;
sprite->unk6 = sp.unkA;
}
else {
sprite->attrib1 = (spriteMasks[0] & sp.flags1) | spriteMasks[3];
sprite->attrib2 = (spriteMasks[1] & sp.flags2) | spriteMasks[4];
sprite->attrib3 = (spriteMasks[2] & sp.flags3) | spriteMasks[5];
sprite->unk6 = sp.unkA;
}

if (sp.unk2 != 0 && sUnknown_2025672[sp.unk2] != 0) {
tileNum = sUnknown_2025672[sp.unk2] & 0x3FF;
}
else {
tileNum = (sprite->attrib3 & 0x3FF) + a1->vramTileOrMaybeAnimTimer;
tileNum &= 0x3FF;
}

// Set tileNum, maintain priority/paletteNum
sprite->attrib3 = tileNum | (sprite->attrib3 & 0xFC00);

x = (sprite->attrib2 & 0x1FF) - 256;
x += a1->pos.x;
if (x < -64)
return;
if (x >= DISPLAY_WIDTH)
return;

// Set x, maintain matrixNum/size
sprite->attrib2 = (x & 0x1FF) | (sprite->attrib2 & 0xFE00);

uVar9 = sprite->unk6 << 16;
earlyMask = 0xFFF;

y = (uVar9 >> 20) - 512;
y += a1->pos.y;
if (y < -64)
return;
if (y >= DISPLAY_HEIGHT)
return;

// Set y, maintain affineMode/objMode/mosaic/bpp/shape
sprite->attrib1 = (y & 0xFF) | (sprite->attrib1 & 0xFF00);

// Set paletteNum, maintain tileNum/priority
if (((uVar9 >> 17) & 1) == 0)
sprite->attrib3 = ((a1->paletteNum & 0xF) << 12) | (sprite->attrib3 & earlyMask);

if (sp.unk2 != 0)
sprite->attrib3 = ((sUnknown_2025682[sp.unk2] & 0xF) << 12) | (sprite->attrib3 & earlyMask);

sUnknown_2025EA8[sSpriteCount].unk0 = sUnknown_20256A0.sprites[r7].unk0;
sUnknown_20256A0.sprites[r7].unk0 = sUnknown_2025EA8 + sSpriteCount;
sSpriteCount++;
}
#else
NAKED
static void AddAxSprite(ax_pose *a0, axdata1 *a1, UnkSpriteMem *a2, u16 *spriteMasks)
// https://decomp.me/scratch/VYqKb - Same issue as AddSprite()
NAKED static void AddAxSprite(ax_pose *a0, axdata1 *a1, UnkSpriteMem *a2, unkStruct_2039DB0 *spriteMasks)
{
asm_unified(
"push {r4-r7,lr}\n"
Expand Down Expand Up @@ -523,11 +419,10 @@ static void AddAxSprite(ax_pose *a0, axdata1 *a1, UnkSpriteMem *a2, u16 *spriteM
"_080050A8: .4byte sUnknown_2025EA8\n"
"_080050AC: .4byte sUnknown_20256A0");
}
#endif // NONMATCHING

// a2 and a3 are always called with NULL lol
#ifdef NONMATCHING // https://decomp.me/scratch/YCfKG
void AddSprite(SpriteOAM *a0, s32 a1, UnkSpriteMem *a2, unkStruct_2039DB0 *a3)
void AddSprite(SpriteOAM *a0, s32 a1, UnkSpriteMem *a2, unkStruct_2039DB0 *spriteMasks)
{
s32 yPos;
SpriteOAM *spr;
Expand All @@ -544,16 +439,16 @@ void AddSprite(SpriteOAM *a0, s32 a1, UnkSpriteMem *a2, unkStruct_2039DB0 *a3)
if (a1 > 255)
a1 = 255;

if (a3 == NULL) {
if (spriteMasks == NULL) {
spr->attrib1 = a0->attrib1;
spr->attrib2 = a0->attrib2;
spr->attrib3 = a0->attrib3;
spr->unk6 = a0->unk6;
}
else {
spr->attrib1 = (a0->attrib1 & a3->unk0) | a3->unk6;
spr->attrib2 = (a0->attrib2 & a3->unk2) | a3->unk8;
spr->attrib3 = (a0->attrib3 & a3->unk4) | a3->unkA;
spr->attrib1 = (a0->attrib1 & spriteMasks->unk0) | spriteMasks->unk6;
spr->attrib2 = (a0->attrib2 & spriteMasks->unk2) | spriteMasks->unk8;
spr->attrib3 = (a0->attrib3 & spriteMasks->unk4) | spriteMasks->unkA;
spr->unk6 = a0->unk6;
}

Expand All @@ -577,7 +472,7 @@ void AddSprite(SpriteOAM *a0, s32 a1, UnkSpriteMem *a2, unkStruct_2039DB0 *a3)
}
#else
NAKED
void AddSprite(SpriteOAM *a0, s32 a1, UnkSpriteMem *a2, unkStruct_2039DB0 *a3)
void AddSprite(SpriteOAM *a0, s32 a1, UnkSpriteMem *a2, unkStruct_2039DB0 *spriteMasks)
{
asm_unified(
"push {r4-r7,lr}\n"
Expand Down Expand Up @@ -836,15 +731,14 @@ void sub_8005304(void)
}
}

// spriteMasks is a u16[6]
void sub_800533C(ax_pose **a0, UnkSpriteMem **a1, axdata1 *a2, u16 *spriteMasks, bool8 a4)
void sub_800533C(ax_pose **a0, UnkSpriteMem **a1, axdata1 *a2, unkStruct_2039DB0 *spriteMasks, bool8 a4)
{
UnkSpriteMem *mem;
ax_pose *r4;

r4 = a0[a2->poseId];
sCharMemCursor = OBJ_VRAM0 + (a2->vramTileOrMaybeAnimTimer * 0x20);
for (mem = NULL; (u16)r4->sprite != 0xFFFF || r4->unk2 != 0xFFFF; r4++, mem = NULL) {
for (mem = NULL; (u16)r4->sprite != 0xFFFF || *((u16*)&r4->unk2) != 0xFFFF; r4++, mem = NULL) {
if (a4 != 0 && r4->sprite > -1)
mem = a1[r4->sprite];

Expand Down

0 comments on commit 5c1d47a

Please sign in to comment.