From 3507ac397415b445f40fd70c48550fe00deb1484 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Tue, 5 Mar 2019 09:02:05 +0100 Subject: [PATCH 1/2] Start fixing flying.c --- src/flying.c | 421 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 321 insertions(+), 100 deletions(-) diff --git a/src/flying.c b/src/flying.c index 7f48e57cb..951906448 100644 --- a/src/flying.c +++ b/src/flying.c @@ -108,7 +108,7 @@ const union AffineAnimCmd gUnknown_085962EC[] = const union AffineAnimCmd *const gUnknown_08596314[] = { - gUnknown_085962EC, + gUnknown_085962EC, }; const union AffineAnimCmd gUnknown_08596318[] = @@ -357,7 +357,8 @@ void sub_810DE70(struct Sprite *sprite) sprite->callback(sprite); } -static void sub_810DE98(struct Sprite *sprite) { +static void sub_810DE98(struct Sprite *sprite) +{ sprite->pos2.x = Sin(sprite->data[1], 32); sprite->pos2.y = Cos(sprite->data[1], 8); sprite->data[1] += 5; @@ -392,8 +393,7 @@ static void sub_810DF18(u8 taskId) { gPlttBufferFaded[base + 0x101 + i] = gPlttBufferFaded[base + 0x100 + i]; i--; - } - while (i > 0); + } while (i > 0); gPlttBufferFaded[base + 0x101] = temp; } @@ -524,7 +524,9 @@ static void sub_810E24C(struct Sprite *sprite) sprite->data[3] &= 0xFF; } - if ((u32)(sprite->pos1.x + sprite->pos2.x + 32) > 304 || sprite->pos1.y + sprite->pos2.y > 160) + if (sprite->pos1.x + sprite->pos2.x < -32 + || sprite->pos1.x + sprite->pos2.x > 272 + || sprite->pos1.y + sprite->pos2.y > 160) { gSprites[GetAnimBattlerSpriteId(ANIM_ATTACKER)].invisible = 0; DestroyAnimSprite(sprite); @@ -547,55 +549,62 @@ void sub_810E2C8(struct Sprite *sprite) } // FAKEMATCHING +struct FeatherDanceData +{ + u8 unk0_0a:1; + u8 unk0_0b:1; + u8 unk0_0c:1; + u8 unk0_0d:1; + u8 unk0_1:4; + u8 unk1; + u16 unk2; + s16 unk4; + u16 unk6; + u16 unk8; + u16 unkA; + u8 unkC[2]; + u16 unkE_0:1; + u16 unkE_1:15; +}; + void sub_810E314(struct Sprite *sprite) { - s16 *data; - u8 slot; - s16 spriteCoord; - int t1, t2; - u32 arg2; - u32 matrixNum; - u8 sinIndex; - register s16 sinVal asm ("r4"); - register int sinVal2 asm ("r0"); + u8 battler, matrixNum, sinIndex; + s16 spriteCoord, sinVal; - data = sprite->data; + struct FeatherDanceData *data = (struct FeatherDanceData *)sprite->data; if (gBattleAnimArgs[7] & 0x100) - slot = gBattleAnimAttacker; + battler = gBattleAnimAttacker; else - slot = gBattleAnimTarget; + battler = gBattleAnimTarget; - if (GetBattlerSide(slot) == B_SIDE_PLAYER) + if (GetBattlerSide(battler) == B_SIDE_PLAYER) gBattleAnimArgs[0] = -gBattleAnimArgs[0]; - sprite->pos1.x = GetBattlerSpriteCoord(slot, 0) + gBattleAnimArgs[0]; - spriteCoord = GetBattlerSpriteCoord(slot, 1); + sprite->pos1.x = GetBattlerSpriteCoord(battler, BATTLER_COORD_ATTR_HEIGHT) + gBattleAnimArgs[0]; + spriteCoord = GetBattlerSpriteCoord(battler, BATTLER_COORD_ATTR_WIDTH); sprite->pos1.y = spriteCoord + gBattleAnimArgs[1]; - data[4] = sprite->pos1.y << 8; - t1 = (spriteCoord + (u16) gBattleAnimArgs[6]) << 1; - data[7] = (data[7] & 1) | t1; - ((u8 *) data)[0] |= 4; + data->unk8 = sprite->pos1.y << 8; + data->unkE_1 = spriteCoord + gBattleAnimArgs[6]; + data->unk0_0c = 1; + data->unk2 = gBattleAnimArgs[2] & 0xFF; + data->unkA = (gBattleAnimArgs[2] >> 8) & 0xFF; + data->unk4 = gBattleAnimArgs[3]; + data->unk6 = gBattleAnimArgs[4]; + *(u16*)(data->unkC) = gBattleAnimArgs[5]; - arg2 = (u16) gBattleAnimArgs[2]; - data[1] = (u8) gBattleAnimArgs[2]; - arg2 <<= 16; - data[5] = arg2 >> 24; - data[2] = gBattleAnimArgs[3]; - data[3] = gBattleAnimArgs[4]; - data[6] = gBattleAnimArgs[5]; - - if ((u16) (data[1] - 64) <= 0x7f) + if (data->unk2 >= 64 && data->unk2 <= 191) { if (!IsContest()) - sprite->oam.priority = GetBattlerSpriteBGPriority(slot) + 1; + sprite->oam.priority = GetBattlerSpriteBGPriority(battler) + 1; else - sprite->oam.priority = GetBattlerSpriteBGPriority(slot); + sprite->oam.priority = GetBattlerSpriteBGPriority(battler); - ((u8 *) data)[14] = data[7] & -2; + data->unkE_0 = 0; - if (!(data[2] & 0x8000)) + if (!(data->unk4 & 0x8000)) { sprite->hFlip ^= 1; sprite->animNum = sprite->hFlip; @@ -606,10 +615,10 @@ void sub_810E314(struct Sprite *sprite) } else { - sprite->oam.priority = GetBattlerSpriteBGPriority(slot); - ((u8 *) data)[14] |= 1; + sprite->oam.priority = GetBattlerSpriteBGPriority(battler); + data->unkE_0 = 1; - if (data[2] & 0x8000) + if (data->unk4 & 0x8000) { sprite->hFlip ^= 1; sprite->animNum = sprite->hFlip; @@ -619,24 +628,236 @@ void sub_810E314(struct Sprite *sprite) } } - t2 = (u16) data[1] >> 6 << 4; - ((u8 *) data)[0] = (15 & data[0]) | t2; - - sprite->pos2.x = (gSineTable[(u16) data[1]] * (u8) data[6]) >> 8; - + data->unk0_1 = data->unk2 >> 6; + sprite->pos2.x = (gSineTable[data->unk2] * data->unkC[0]) >> 8; matrixNum = sprite->oam.matrixNum; - sinIndex = (-sprite->pos2.x >> 1) + data[5]; + sinIndex = (-sprite->pos2.x >> 1) + data->unkA; sinVal = gSineTable[sinIndex]; gOamMatrices[matrixNum].a = gOamMatrices[matrixNum].d = gSineTable[sinIndex + 64]; - gOamMatrices[matrixNum].b = sinVal; - sinVal2 = -sinVal; - gOamMatrices[matrixNum].c = sinVal2; + // The comparison below is completely pointless. 'sprite' is sure to be a valid pointer and + // both the 'if' and 'else' parts are exactly the same. + // The only reason for this is making sure the compiler generates the exact ASM. + if (sprite) + { + gOamMatrices[matrixNum].b = sinVal; + gOamMatrices[matrixNum].c = -sinVal; + } + else + { + gOamMatrices[matrixNum].b = sinVal; + gOamMatrices[matrixNum].c = -sinVal; + } sprite->callback = sub_810E520; } +#ifndef NONMATCHING +void sub_810E520(struct Sprite *sprite) +{ + u8 matrixNum, sinIndex, var; + s16 sinVal = 0; + struct FeatherDanceData *data = (struct FeatherDanceData *)sprite->data; + + if (data->unk0_0a) + { + if (data->unk1-- % 256 == 0) + { + data->unk0_0a = 0; + data->unk1 = 0; + } + } + else + { + switch (data->unk2 >> 6) + { + case 0: + if (data->unk0_1 == 1) + { + data->unk0_0d = 1; + data->unk0_0a = 1; + data->unk1 = 0; + } + else if (data->unk0_1 == 3) + { + data->unk0_0b ^= 1; + data->unk1 = 0; + } + else if (data->unk0_0d) + { + sprite->hFlip ^= 1; + sprite->animNum = sprite->hFlip; + sprite->animBeginning = 1; + sprite->animEnded = 0; + if (data->unk0_0c) + { + if (!IsContest()) + { + if (!data->unkE_0) + sprite->oam.priority--; + else + sprite->oam.priority++; + } + else + { + if (!data->unkE_0) + sprite->subpriority -= 12; + else + sprite->subpriority += 12; + } + data->unkE_0 ^= 1; + } + data->unk0_0d = 0; + } + data->unk0_1 = 0; + break; + case 1: + if (data->unk0_1 == 0) + { + data->unk0_0d = 1; + data->unk0_0a = 1; + data->unk1 = 0; + } + else if (data->unk0_1 == 2) + { + data->unk0_0a = 1; + data->unk1 = 0; + } + else if (data->unk0_0d) + { + sprite->hFlip ^= 1; + sprite->animNum = sprite->hFlip; + sprite->animBeginning = 1; + sprite->animEnded = 0; + if (data->unk0_0c) + { + if (!IsContest()) + { + if (!data->unkE_0) + sprite->oam.priority--; + else + sprite->oam.priority++; + } + else + { + if (!data->unkE_0) + sprite->subpriority -= 12; + else + sprite->subpriority += 12; + } + data->unkE_0 ^= 1; + } + data->unk0_0d = 0; + } + data->unk0_1 = 1; + break; + case 2: + if (data->unk0_1 == 0) + { + data->unk0_0d = 1; + data->unk0_0a = 1; + data->unk1 = 0; + } + else if (data->unk0_1 == 2) + { + data->unk0_0a = 1; + data->unk1 = 0; + } + else if (data->unk0_0d) + { + sprite->hFlip ^= 1; + sprite->animNum = sprite->hFlip; + sprite->animBeginning = 1; + sprite->animEnded = 0; + if (data->unk0_0c) + { + if (!IsContest()) + { + if (!data->unkE_0) + sprite->oam.priority--; + else + sprite->oam.priority++; + } + else + { + if (!data->unkE_0) + sprite->subpriority -= 12; + else + sprite->subpriority += 12; + } + data->unkE_0 ^= 1; + } + data->unk0_0d = 0; + } + data->unk0_1 = 2; + break; + case 3: + if (data->unk0_1 == 2) + { + data->unk0_0d = 1; + } + else if (data->unk0_1 == 0) + { + data->unk0_0b ^= 1; + data->unk1 = 0; + } + else if (data->unk0_0d) + { + sprite->hFlip ^= 1; + sprite->animNum = sprite->hFlip; + sprite->animBeginning = 1; + sprite->animEnded = 0; + if (data->unk0_0c) + { + if (!IsContest()) + { + if (!data->unkE_0) + sprite->oam.priority--; + else + sprite->oam.priority++; + } + else + { + if (!data->unkE_0) + sprite->subpriority -= 12; + else + sprite->subpriority += 12; + } + data->unkE_0 ^= 1; + } + data->unk0_0d = 0; + } + data->unk0_1 = 3; + break; + } + + var = data->unkC[data->unk0_0b]; + sprite->pos2.x = (gSineTable[data->unk2] * var) >> 8; + matrixNum = sprite->oam.matrixNum; + + sinIndex = (-sprite->pos2.x >> 1) + data->unkA; + sinVal = gSineTable[sinIndex]; + + gOamMatrices[matrixNum].a = gOamMatrices[matrixNum].d = gSineTable[sinIndex + 64]; + gOamMatrices[matrixNum].b = sinVal; + gOamMatrices[matrixNum].c = -sinVal; + + data->unk8 += data->unk6; + sprite->pos1.y = data->unk8 >> 8; + if (data->unk4 & 0x8000) + data->unk2 = (data->unk2 - (data->unk4 & 0x7FFF)) & 0xFF; + else + data->unk2 = (data->unk2 + (data->unk4 & 0x7FFF)) & 0xFF; + + if (sprite->pos1.y + sprite->pos2.y > data->unkE_1) + { + sprite->data[0] = 0; + sprite->callback = sub_810E2C8; + } + } +} +#else NAKED void sub_810E520(struct Sprite *sprite) { @@ -1345,6 +1566,7 @@ _0810EA36:\n\ .pool\n\ "); } +#endif void sub_810EA4C(struct Sprite *sprite) { @@ -1363,7 +1585,7 @@ void sub_810EAA0(struct Sprite * sprite) InitSpritePosToAnimAttacker(sprite, 0); else InitSpritePosToAnimTarget(sprite, FALSE); - + if ((!gBattleAnimArgs[2] && !GetBattlerSide(gBattleAnimAttacker)) || (gBattleAnimArgs[2] == 1 && !GetBattlerSide(gBattleAnimTarget))) { @@ -1424,14 +1646,15 @@ void sub_810EC34(struct Sprite *sprite) { switch (sprite->data[0]) { - case 0: - InitSpritePosToAnimAttacker(sprite, 1); - gSprites[GetAnimBattlerSpriteId(0)].invisible = 1; - ++sprite->data[0]; - break; - case 1: - if (sprite->affineAnimEnded) - DestroyAnimSprite(sprite); + case 0: + InitSpritePosToAnimAttacker(sprite, 1); + gSprites[GetAnimBattlerSpriteId(0)].invisible = 1; + ++sprite->data[0]; + break; + case 1: + if (sprite->affineAnimEnded) + DestroyAnimSprite(sprite); + break; } } @@ -1439,23 +1662,24 @@ void sub_810EC94(struct Sprite *sprite) { switch (sprite->data[0]) { - case 0: - sprite->pos1.y = GetBattlerSpriteCoord(gBattleAnimTarget, 1); - sprite->pos2.y = -sprite->pos1.y - 32; - sprite->data[0]++; - break; - case 1: - sprite->pos2.y += 10; - if (sprite->pos2.y >= 0) - ++sprite->data[0]; - break; - case 2: - sprite->pos2.y -= 10; - if (sprite->pos1.y + sprite->pos2.y < -32) - { - gSprites[GetAnimBattlerSpriteId(0)].invisible = 0; - DestroyAnimSprite(sprite); - } + case 0: + sprite->pos1.y = GetBattlerSpriteCoord(gBattleAnimTarget, 1); + sprite->pos2.y = -sprite->pos1.y - 32; + sprite->data[0]++; + break; + case 1: + sprite->pos2.y += 10; + if (sprite->pos2.y >= 0) + ++sprite->data[0]; + break; + case 2: + sprite->pos2.y -= 10; + if (sprite->pos1.y + sprite->pos2.y < -32) + { + gSprites[GetAnimBattlerSpriteId(0)].invisible = 0; + DestroyAnimSprite(sprite); + } + break; } } @@ -1471,7 +1695,9 @@ void sub_810ED28(struct Sprite *sprite) void sub_810ED70(struct Sprite *sprite) { if (sprite->data[0] > 0) + { sprite->data[0]--; + } else if (sprite->pos1.y + sprite->pos2.y > -32) { sprite->data[2] += sprite->data[1]; @@ -1503,7 +1729,7 @@ void sub_810EE14(struct Sprite *sprite) u32 matrixNum; int t1, t2; - + switch (sprite->data[0]) { case 0: @@ -1528,7 +1754,7 @@ void sub_810EE14(struct Sprite *sprite) sprite->data[1] -= 40; else sprite->data[1] += 40; - + sprite->data[2]++; TrySetSpriteRotScale(sprite, 0, 256, sprite->data[1], 0); @@ -1540,7 +1766,7 @@ void sub_810EE14(struct Sprite *sprite) if (t2 > 128) t2 = 128; - + /* NONMATCHING * compiles to: @@ -1684,22 +1910,18 @@ void sub_810EEF8(struct Sprite *sprite) if (v1 % 2) sprite->data[0] = 736 + v1; - else sprite->data[0] = 736 - v1; - if (v2 % 2) sprite->data[1] = 896 + v2; - else sprite->data[1] = 896 - v2; - + sprite->data[2] = gBattleAnimArgs[0]; if (sprite->data[2]) sprite->oam.matrixNum = 8; - if (gBattleAnimArgs[1] == 0) { @@ -1733,7 +1955,6 @@ void sub_810EFA8(struct Sprite *sprite) if (sprite->data[0] < 0) sprite->data[0] = 0; - if (++sprite->data[3] == 31) DestroyAnimSprite(sprite); @@ -1750,27 +1971,26 @@ void sub_810F018(struct Sprite *sprite) { switch (sprite->data[0]) { - case 0: - if (++sprite->data[1] > 8) - { - sprite->data[1] = 0; - sprite->invisible ^= 1; - if (++sprite->data[2] > 5 && sprite->invisible) - sprite->data[0]++; - } - break; - case 1: - DestroyAnimSprite(sprite); + case 0: + if (++sprite->data[1] > 8) + { + sprite->data[1] = 0; + sprite->invisible ^= 1; + if (++sprite->data[2] > 5 && sprite->invisible) + sprite->data[0]++; + } + break; + case 1: + DestroyAnimSprite(sprite); + break; } } void sub_810F084(struct Sprite *sprite) { - s16 posx, posy; u16 rotation; - - posx = sprite->pos1.x; - posy = sprite->pos1.y; + s16 posx = sprite->pos1.x; + s16 posy = sprite->pos1.y; sprite->pos1.x = GetBattlerSpriteCoord(gBattleAnimAttacker, 2); sprite->pos1.y = GetBattlerSpriteCoord(gBattleAnimAttacker, 3); @@ -1797,7 +2017,8 @@ void sub_810F140(struct Sprite *sprite) sprite->pos1.x = sprite->data[4] >> 4; sprite->pos1.y = sprite->data[5] >> 4; - if ((u16) (sprite->pos1.x + 0x2d) > 0x14a || sprite->pos1.y > 0x9d || sprite->pos1.y < -0x2d) + if (sprite->pos1.x > 285 || sprite->pos1.x < -45 + || sprite->pos1.y > 157 || sprite->pos1.y < -45) DestroySpriteAndMatrix(sprite); } From 596d85bcbd13c11ad71307e79e641035811941db Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Tue, 5 Mar 2019 17:07:37 +0100 Subject: [PATCH 2/2] What is love, baby dont hurt me --- src/flying.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flying.c b/src/flying.c index b48538706..a157582e9 100644 --- a/src/flying.c +++ b/src/flying.c @@ -652,7 +652,7 @@ void sub_810E314(struct Sprite *sprite) sprite->callback = sub_810E520; } -#ifndef NONMATCHING +#ifdef NONMATCHING /* Things I can't get to match: 1. data->unk0_1 bitfield behaviour. The way bits are read is different.