Skip to content

Commit

Permalink
Fix move animation crashing on some emulators because of division by …
Browse files Browse the repository at this point in the history
…zero (rh-hideout#4121)

* fix flip turn div by zero

* fix incinerate move anim div by zero
  • Loading branch information
DizzyEggg authored and Tclark61 committed Feb 6, 2024
1 parent 3c635a6 commit a16eda4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/battle_anim_ghost.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/battle_anim_mons.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a16eda4

Please sign in to comment.