Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed switchout bug in multibattle where order of mons gets messed up #2099

Merged
merged 10 commits into from
Jan 29, 2025
71 changes: 70 additions & 1 deletion src/battle_script_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -7189,7 +7189,7 @@ static void Cmd_forcerandomswitch(void)
s32 battler2PartyId = 0;

s32 firstMonId;
s32 lastMonId = 0; // + 1
s32 lastMonId = 0;
s32 monsCount;
struct Pokemon *party = NULL;
s32 validMons = 0;
Expand All @@ -7209,12 +7209,20 @@ static void Cmd_forcerandomswitch(void)
if ((gBattlerTarget & BIT_FLANK) != B_FLANK_LEFT)
{
firstMonId = PARTY_SIZE / 2;
#ifdef BUGFIX
lastMonId = PARTY_SIZE - 1;
#else
lastMonId = PARTY_SIZE;
#endif
}
else
{
firstMonId = 0;
#ifdef BUGFIX
lastMonId = PARTY_SIZE / 2 - 1;
#else
lastMonId = PARTY_SIZE / 2;
#endif
}
monsCount = PARTY_SIZE / 2;
minNeeded = 1;
Expand All @@ -7227,12 +7235,20 @@ static void Cmd_forcerandomswitch(void)
if (GetLinkTrainerFlankId(GetBattlerMultiplayerId(gBattlerTarget)) == B_FLANK_RIGHT)
{
firstMonId = PARTY_SIZE / 2;
#ifdef BUGFIX
lastMonId = PARTY_SIZE - 1;
#else
lastMonId = PARTY_SIZE;
#endif
}
else
{
firstMonId = 0;
#ifdef BUGFIX
lastMonId = PARTY_SIZE / 2 - 1;
#else
lastMonId = PARTY_SIZE / 2;
#endif
}
monsCount = PARTY_SIZE / 2;
minNeeded = 1;
Expand All @@ -7244,7 +7260,11 @@ static void Cmd_forcerandomswitch(void)
if (GetBattlerSide(gBattlerTarget) == B_SIDE_PLAYER)
{
firstMonId = 0;
#ifdef BUGFIX
lastMonId = PARTY_SIZE - 1;
#else
lastMonId = PARTY_SIZE;
#endif
monsCount = PARTY_SIZE;
minNeeded = 2; // since there are two opponents, it has to be a double battle
}
Expand All @@ -7253,12 +7273,20 @@ static void Cmd_forcerandomswitch(void)
if ((gBattlerTarget & BIT_FLANK) != B_FLANK_LEFT)
{
firstMonId = PARTY_SIZE / 2;
#ifdef BUGFIX
lastMonId = PARTY_SIZE - 1;
#else
lastMonId = PARTY_SIZE;
#endif
}
else
{
firstMonId = 0;
#ifdef BUGFIX
lastMonId = PARTY_SIZE / 2 - 1;
#else
lastMonId = PARTY_SIZE / 2;
#endif
}
monsCount = PARTY_SIZE / 2;
minNeeded = 1;
Expand All @@ -7269,7 +7297,11 @@ static void Cmd_forcerandomswitch(void)
else if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE)
{
firstMonId = 0;
#ifdef BUGFIX
lastMonId = PARTY_SIZE - 1;
#else
lastMonId = PARTY_SIZE;
#endif
monsCount = PARTY_SIZE;
minNeeded = 2;
battler2PartyId = gBattlerPartyIndexes[gBattlerTarget];
Expand All @@ -7278,7 +7310,11 @@ static void Cmd_forcerandomswitch(void)
else
{
firstMonId = 0;
#ifdef BUGFIX
lastMonId = PARTY_SIZE - 1;
#else
lastMonId = PARTY_SIZE;
#endif
monsCount = PARTY_SIZE;
minNeeded = 1;
battler2PartyId = gBattlerPartyIndexes[gBattlerTarget]; // there is only one Pokémon out in single battles
Expand All @@ -7301,6 +7337,38 @@ static void Cmd_forcerandomswitch(void)
}
else
{
#ifdef BUGFIX
if (TryDoForceSwitchOut())
{
do
{
do
{
i = Random() % monsCount;
i += firstMonId;
}
while (i == battler2PartyId || i == battler1PartyId);
} while (GetMonData(&party[i], MON_DATA_SPECIES) == SPECIES_NONE
|| GetMonData(&party[i], MON_DATA_IS_EGG) == TRUE
|| GetMonData(&party[i], MON_DATA_HP) == 0); //should be one while loop, but that doesn't match.
shachar700 marked this conversation as resolved.
Show resolved Hide resolved
*(gBattleStruct->monToSwitchIntoId + gBattlerTarget) = i;
shachar700 marked this conversation as resolved.
Show resolved Hide resolved

if (!IsMultiBattle())
SwitchPartyOrder(gBattlerTarget);

if ((gBattleTypeFlags & BATTLE_TYPE_LINK && gBattleTypeFlags & BATTLE_TYPE_BATTLE_TOWER)
|| (gBattleTypeFlags & BATTLE_TYPE_LINK && gBattleTypeFlags & BATTLE_TYPE_MULTI)
|| (gBattleTypeFlags & BATTLE_TYPE_RECORDED_LINK && gBattleTypeFlags & BATTLE_TYPE_BATTLE_TOWER)
|| (gBattleTypeFlags & BATTLE_TYPE_RECORDED_LINK && gBattleTypeFlags & BATTLE_TYPE_MULTI))
{
SwitchPartyOrderLinkMulti(gBattlerTarget, i, 0);
SwitchPartyOrderLinkMulti(BATTLE_PARTNER(gBattlerTarget), i, 1);
}

if (gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER)
SwitchPartyOrderInGameMulti(gBattlerTarget, i);
}
#else
if (TryDoForceSwitchOut())
{
do
Expand Down Expand Up @@ -7331,6 +7399,7 @@ static void Cmd_forcerandomswitch(void)

if (gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER)
SwitchPartyOrderInGameMulti(gBattlerTarget, i);
#endif
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/battle_util2.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ void SwitchPartyOrderInGameMulti(u8 battlerId, u8 arg1)
{
s32 i;
for (i = 0; i < (int)ARRAY_COUNT(gBattlePartyCurrentOrder); i++)
gBattlePartyCurrentOrder[i] = *(0 * 3 + i + (u8 *)(gBattleStruct->battlerPartyOrders));
gBattlePartyCurrentOrder[i] = *(i + (u8 *)(gBattleStruct->battlerPartyOrders));

SwitchPartyMonSlots(GetPartyIdFromBattlePartyId(gBattlerPartyIndexes[battlerId]), GetPartyIdFromBattlePartyId(arg1));

for (i = 0; i < (int)ARRAY_COUNT(gBattlePartyCurrentOrder); i++)
*(0 * 3 + i + (u8 *)(gBattleStruct->battlerPartyOrders)) = gBattlePartyCurrentOrder[i];
*(i + (u8 *)(gBattleStruct->battlerPartyOrders)) = gBattlePartyCurrentOrder[i];
}
}

Expand Down
Loading