Merge pull request #1983 from sphericalice/last_ball_refactor

Refactor CanThrowLastUsedBall and related functions
This commit is contained in:
Eduardo Quezada D'Ottone 2021-12-19 20:21:50 -03:00 committed by GitHub
commit a57043b589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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_THROW_UNABLE_TWO_MONS,
BALL_THROW_UNABLE_NO_ROOM,
BALL_THROW_UNABLE_SEMI_INVULNERABLE,
BALL_THROW_ABLE,
};
bool32 CanThrowBall(void);
#endif // GUARD_ITEM_USE_H

View File

@ -3278,19 +3278,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)))
{
@ -3299,10 +3303,8 @@ void TryAddLastUsedBallItemSprites(void)
CompactItemsInBagPocket(&gBagPockets[BALLS_POCKET]);
gLastThrownBall = gBagPockets[BALLS_POCKET].itemSlots[0].itemId;
}
if (CanThrowBall() != 0
|| (gBattleTypeFlags & BATTLE_TYPE_TRAINER)
|| !CheckBagHasItem(gLastThrownBall, 1))
if (!CanThrowLastUsedBall())
return;
// ball
@ -3323,11 +3325,11 @@ void TryAddLastUsedBallItemSprites(void)
if (gBattleStruct->ballSpriteIds[1] == MAX_SPRITES)
{
gBattleStruct->ballSpriteIds[1] = CreateSprite(&sSpriteTemplate_LastUsedBallWindow,
LAST_BALL_WIN_X_0,
LAST_USED_WIN_Y, 5);
LAST_BALL_WIN_X_0,
LAST_USED_WIN_Y, 5);
gSprites[gBattleStruct->ballSpriteIds[0]].sHide = FALSE; // restore
}
#endif
#endif
}
static void DestroyLastUsedBallWinGfx(struct Sprite *sprite)
@ -3382,7 +3384,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;
@ -3401,23 +3403,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

@ -940,34 +940,33 @@ void ItemUseOutOfBattle_EvolutionStone(u8 taskId)
SetUpItemUseCallback(taskId);
}
u32 CanThrowBall(void)
static u32 GetBallThrowableState(void)
{
if (IsBattlerAlive(GetBattlerAtPosition(B_POSITION_OPPONENT_LEFT))
&& IsBattlerAlive(GetBattlerAtPosition(B_POSITION_OPPONENT_RIGHT)))
{
return 1; // There are two present pokemon.
}
&& IsBattlerAlive(GetBattlerAtPosition(B_POSITION_OPPONENT_RIGHT)))
return BALL_THROW_UNABLE_TWO_MONS;
else if (IsPlayerPartyAndPokemonStorageFull() == TRUE)
{
return 2; // No room for mon
}
#if B_SEMI_INVULNERABLE_CATCH >= GEN_4
return BALL_THROW_UNABLE_NO_ROOM;
#if B_SEMI_INVULNERABLE_CATCH >= GEN_4
else if (gStatuses3[GetCatchingBattler()] & STATUS3_SEMI_INVULNERABLE)
{
return 3; // in semi-invulnerable state
}
#endif
return 0; // usable
return BALL_THROW_UNABLE_SEMI_INVULNERABLE;
#endif
return BALL_THROW_ABLE;
}
bool32 CanThrowBall(void)
{
return (GetBallThrowableState() == BALL_THROW_ABLE);
}
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 (GetBallThrowableState())
{
case 0: // usable
case BALL_THROW_ABLE:
default:
RemoveBagItem(gSpecialVar_ItemId, 1);
if (!InBattlePyramid())
@ -975,20 +974,20 @@ void ItemUseInBattle_PokeBall(u8 taskId)
else
CloseBattlePyramidBag(taskId);
break;
case 1: // There are two present pokemon.
case BALL_THROW_UNABLE_TWO_MONS:
if (!InBattlePyramid())
DisplayItemMessage(taskId, FONT_NORMAL, sText_CantThrowPokeBall_TwoMons, CloseItemMessage);
else
DisplayItemMessageInBattlePyramid(taskId, sText_CantThrowPokeBall_TwoMons, Task_CloseBattlePyramidBagMessage);
break;
case 2: // No room for mon
case BALL_THROW_UNABLE_NO_ROOM:
if (!InBattlePyramid())
DisplayItemMessage(taskId, FONT_NORMAL, gText_BoxFull, CloseItemMessage);
else
DisplayItemMessageInBattlePyramid(taskId, gText_BoxFull, Task_CloseBattlePyramidBagMessage);
break;
#if B_SEMI_INVULNERABLE_CATCH >= GEN_4
case 3: // Semi-Invulnerable
case BALL_THROW_UNABLE_SEMI_INVULNERABLE:
if (!InBattlePyramid())
DisplayItemMessage(taskId, FONT_NORMAL, sText_CantThrowPokeBall_SemiInvulnerable, CloseItemMessage);
else