From 85eea4869debe7dcd7e707a89ed6251a3d412651 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Sat, 3 Feb 2024 16:56:50 +0100 Subject: [PATCH] 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 --- include/global.h | 2 +- src/battle_anim_ghost.c | 4 ++-- src/battle_anim_mons.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/global.h b/include/global.h index 60abf094af..5cd5c68995 100644 --- a/include/global.h +++ b/include/global.h @@ -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 diff --git a/src/battle_anim_ghost.c b/src/battle_anim_ghost.c index 7bd6a0268e..6a12481531 100644 --- a/src/battle_anim_ghost.c +++ b/src/battle_anim_ghost.c @@ -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; } diff --git a/src/battle_anim_mons.c b/src/battle_anim_mons.c index 78e1a0a445..83d91c1ca4 100644 --- a/src/battle_anim_mons.c +++ b/src/battle_anim_mons.c @@ -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; }