Fix move animation crashing on some emulators because of division by zero (#4121)

* fix flip turn div by zero

* fix incinerate move anim div by zero
This commit is contained in:
DizzyEggg 2024-02-03 16:56:50 +01:00 committed by GitHub
parent e828ae58a1
commit 85eea4869d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 5 additions and 5 deletions

View file

@ -61,7 +61,7 @@
// Used in cases where division by 0 can occur in the retail version.
// Avoids invalid opcodes on some emulators, and the otherwise UB.
#ifdef UBFIX
#define SAFE_DIV(a, b) ((b) ? (a) / (b) : 0)
#define SAFE_DIV(a, b) (((b) != 0) ? (a) / (b) : 0)
#else
#define SAFE_DIV(a, b) ((a) / (b))
#endif

View file

@ -474,8 +474,8 @@ void AnimShadowBall(struct Sprite *sprite)
sprite->data[3] = gBattleAnimArgs[2];
sprite->data[4] = sprite->x << 4;
sprite->data[5] = sprite->y << 4;
sprite->data[6] = ((oldPosX - sprite->x) << 4) / (gBattleAnimArgs[0] << 1);
sprite->data[7] = ((oldPosY - sprite->y) << 4) / (gBattleAnimArgs[0] << 1);
sprite->data[6] = SAFE_DIV(((oldPosX - sprite->x) << 4), (gBattleAnimArgs[0] << 1));
sprite->data[7] = SAFE_DIV(((oldPosY - sprite->y) << 4), (gBattleAnimArgs[0] << 1));
sprite->callback = AnimShadowBall_Step;
}

View file

@ -1025,8 +1025,8 @@ void InitSpriteDataForLinearTranslation(struct Sprite *sprite)
{
s16 x = (sprite->data[2] - sprite->data[1]) << 8;
s16 y = (sprite->data[4] - sprite->data[3]) << 8;
sprite->data[1] = x / sprite->data[0];
sprite->data[2] = y / sprite->data[0];
sprite->data[1] = SAFE_DIV(x, sprite->data[0]);
sprite->data[2] = SAFE_DIV(y, sprite->data[0]);
sprite->data[4] = 0;
sprite->data[3] = 0;
}