Refactor CanThrowLastUsedBall and related functions

This commit is contained in:
sphericalice 2021-12-05 23:10:05 +00:00
parent ae1d124a0c
commit 3db871c49a
3 changed files with 51 additions and 43 deletions

View File

@ -33,6 +33,14 @@ void ItemUseInBattle_EnigmaBerry(u8);
void Task_UseDigEscapeRopeOnField(u8 taskId);
u8 CanUseDigOrEscapeRopeOnCurMap(void);
u8 CheckIfItemIsTMHMOrEvolutionStone(u16 itemId);
u32 CanThrowBall(void);
enum {
BALL_FAIL_TWO_PRESENT_MONS,
BALL_FAIL_NO_ROOM,
BALL_FAIL_SEMI_INVULNERABLE,
BALL_SUCCESS,
};
bool32 CanThrowBall(void);
#endif // GUARD_ITEM_USE_H

View File

@ -3276,19 +3276,23 @@ static const struct SpriteSheet sSpriteSheet_LastUsedBallWindow =
bool32 CanThrowLastUsedBall(void)
{
#if B_LAST_USED_BALL == FALSE
#if B_LAST_USED_BALL == FALSE
return FALSE;
#else
if (!CanThrowBall())
return FALSE;
if (gBattleTypeFlags & BATTLE_TYPE_TRAINER)
return FALSE;
if (!CheckBagHasItem(gLastThrownBall, 1))
return FALSE;
#else
return (!(CanThrowBall() != 0
|| (gBattleTypeFlags & BATTLE_TYPE_TRAINER)
|| !CheckBagHasItem(gLastThrownBall, 1)));
#endif
}
return TRUE;
#endif
}
void TryAddLastUsedBallItemSprites(void)
{
#if B_LAST_USED_BALL == TRUE
#if B_LAST_USED_BALL == TRUE
if (gLastThrownBall == 0
|| (gLastThrownBall != 0 && !CheckBagHasItem(gLastThrownBall, 1)))
{
@ -3298,9 +3302,7 @@ void TryAddLastUsedBallItemSprites(void)
gLastThrownBall = gBagPockets[BALLS_POCKET].itemSlots[0].itemId;
}
if (CanThrowBall() != 0
|| (gBattleTypeFlags & BATTLE_TYPE_TRAINER)
|| !CheckBagHasItem(gLastThrownBall, 1))
if (!CanThrowLastUsedBall())
return;
// ball
@ -3325,7 +3327,7 @@ void TryAddLastUsedBallItemSprites(void)
LAST_USED_WIN_Y, 5);
gSprites[gBattleStruct->ballSpriteIds[0]].sHide = FALSE; // restore
}
#endif
#endif
}
static void DestroyLastUsedBallWinGfx(struct Sprite *sprite)
@ -3380,7 +3382,7 @@ static void SpriteCB_LastUsedBall(struct Sprite *sprite)
static void TryHideOrRestoreLastUsedBall(u8 caseId)
{
#if B_LAST_USED_BALL == TRUE
#if B_LAST_USED_BALL == TRUE
if (gBattleStruct->ballSpriteIds[0] == MAX_SPRITES)
return;
@ -3399,23 +3401,22 @@ static void TryHideOrRestoreLastUsedBall(u8 caseId)
gSprites[gBattleStruct->ballSpriteIds[1]].sHide = FALSE; // restore
break;
}
#endif
#endif
}
void TryHideLastUsedBall(void)
{
#if B_LAST_USED_BALL == TRUE
#if B_LAST_USED_BALL == TRUE
TryHideOrRestoreLastUsedBall(0);
#endif
#endif
}
void TryRestoreLastUsedBall(void)
{
#if B_LAST_USED_BALL == TRUE
#if B_LAST_USED_BALL == TRUE
if (gBattleStruct->ballSpriteIds[0] != MAX_SPRITES)
TryHideOrRestoreLastUsedBall(1);
else
TryAddLastUsedBallItemSprites();
#endif
#endif
}

View File

@ -936,34 +936,33 @@ void ItemUseOutOfBattle_EvolutionStone(u8 taskId)
SetUpItemUseCallback(taskId);
}
u32 CanThrowBall(void)
static u32 GetBallThrowFailState(void)
{
if (IsBattlerAlive(GetBattlerAtPosition(B_POSITION_OPPONENT_LEFT))
&& IsBattlerAlive(GetBattlerAtPosition(B_POSITION_OPPONENT_RIGHT)))
{
return 1; // There are two present pokemon.
}
return BALL_FAIL_TWO_PRESENT_MONS;
else if (IsPlayerPartyAndPokemonStorageFull() == TRUE)
{
return 2; // No room for mon
}
#if B_SEMI_INVULNERABLE_CATCH >= GEN_4
return BALL_FAIL_NO_ROOM;
#if B_SEMI_INVULNERABLE_CATCH >= GEN_4
else if (gStatuses3[GetCatchingBattler()] & STATUS3_SEMI_INVULNERABLE)
{
return 3; // in semi-invulnerable state
}
#endif
return BALL_FAIL_SEMI_INVULNERABLE;
#endif
return 0; // usable
return BALL_SUCCESS;
}
bool32 CanThrowBall(void)
{
return (GetBallThrowFailState() == BALL_SUCCESS);
}
static const u8 sText_CantThrowPokeBall_TwoMons[] = _("Cannot throw a ball!\nThere are two Pokémon out there!\p");
static const u8 sText_CantThrowPokeBall_SemiInvulnerable[] = _("Cannot throw a ball!\nThere's no Pokémon in sight!\p");
void ItemUseInBattle_PokeBall(u8 taskId)
{
switch (CanThrowBall())
switch (GetBallThrowFailState())
{
case 0: // usable
case BALL_SUCCESS:
default:
RemoveBagItem(gSpecialVar_ItemId, 1);
if (!InBattlePyramid())
@ -971,20 +970,20 @@ void ItemUseInBattle_PokeBall(u8 taskId)
else
CloseBattlePyramidBag(taskId);
break;
case 1: // There are two present pokemon.
case BALL_FAIL_TWO_PRESENT_MONS:
if (!InBattlePyramid())
DisplayItemMessage(taskId, 1, sText_CantThrowPokeBall_TwoMons, CloseItemMessage);
else
DisplayItemMessageInBattlePyramid(taskId, sText_CantThrowPokeBall_TwoMons, Task_CloseBattlePyramidBagMessage);
break;
case 2: // No room for mon
case BALL_FAIL_NO_ROOM:
if (!InBattlePyramid())
DisplayItemMessage(taskId, 1, gText_BoxFull, CloseItemMessage);
else
DisplayItemMessageInBattlePyramid(taskId, gText_BoxFull, Task_CloseBattlePyramidBagMessage);
break;
#if B_SEMI_INVULNERABLE_CATCH >= GEN_4
case 3: // Semi-Invulnerable
case BALL_FAIL_SEMI_INVULNERABLE:
if (!InBattlePyramid())
DisplayItemMessage(taskId, 1, sText_CantThrowPokeBall_SemiInvulnerable, CloseItemMessage);
else