Skip to content

Commit

Permalink
Fix Teleport ending trainer battles (#3166)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexOn1ine authored Aug 7, 2023
1 parent 1fb42e4 commit 42992ca
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 72 deletions.
17 changes: 8 additions & 9 deletions asm/macros/battle_script.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@
.2byte \holdEffect
.4byte \jumpInstr
.endm

.macro dostockpilestatchangeswearoff, battler:req, statChangeInstr:req
callnative BS_DoStockpileStatChangesWearOff
.byte \battler
Expand Down Expand Up @@ -1354,7 +1354,7 @@
.macro setsnow
callnative BS_SetSnow
.endm

.macro setzeffect
callnative BS_SetZEffect
.endm
Expand All @@ -1364,12 +1364,6 @@
callnative BS_TrySymbiosis
.endm

@ returns TRUE or FALSE to gBattleCommunication[0]
.macro canteleport battler:req
callnative BS_CanTeleport
.byte \battler
.endm

@ returns B_SIDE_x to gBattleCommunication[0]
.macro getbattlerside battler:req
callnative BS_GetBattlerSide
Expand Down Expand Up @@ -2077,7 +2071,7 @@
.macro swapsidestatuses
various BS_ATTACKER, VARIOUS_SWAP_SIDE_STATUSES
.endm

.macro swapstats stat:req
various BS_ATTACKER, VARIOUS_SWAP_STATS
.byte \stat
Expand Down Expand Up @@ -2178,6 +2172,11 @@
jumpifbyte CMP_COMMON_BITS, gMoveResultFlags, MOVE_RESULT_NO_EFFECT, \jumpInstr
.endm

.macro jumpifside battler:req, side:req, equalJumpInstr:req
getbattlerside \battler
jumpifbyte CMP_EQUAL, gBattleCommunication, \side, \equalJumpInstr
.endm

.macro jumpifbattletype flags:req, jumpInstr:req
jumpifword CMP_COMMON_BITS, gBattleTypeFlags, \flags, \jumpInstr
.endm
Expand Down
34 changes: 5 additions & 29 deletions data/battle_scripts_1.s
Original file line number Diff line number Diff line change
Expand Up @@ -5401,15 +5401,14 @@ BattleScript_EffectHurricane:
BattleScript_EffectTeleport:
attackcanceler
attackstring
ppreduce
.if B_TELEPORT_BEHAVIOR >= GEN_7
canteleport BS_ATTACKER
jumpifbyte CMP_EQUAL, gBattleCommunication, TRUE, BattleScript_EffectTeleportNew
goto BattleScript_ButItFailed
jumpifbattletype BATTLE_TYPE_TRAINER, BattleScript_EffectBatonPass
jumpifside BS_ATTACKER, B_SIDE_PLAYER, BattleScript_EffectBatonPass
.else
jumpifbattletype BATTLE_TYPE_TRAINER, BattleScript_ButItFailed
.endif
BattleScript_EffectTeleportTryToRunAway:
ppreduce
getifcantrunfrombattle BS_ATTACKER
jumpifbyte CMP_EQUAL, gBattleCommunication, BATTLE_RUN_FORBIDDEN, BattleScript_ButItFailed
jumpifbyte CMP_EQUAL, gBattleCommunication, BATTLE_RUN_FAILURE, BattleScript_PrintAbilityMadeIneffective
Expand All @@ -5420,29 +5419,6 @@ BattleScript_EffectTeleportTryToRunAway:
setoutcomeonteleport BS_ATTACKER
goto BattleScript_MoveEnd

BattleScript_EffectTeleportNew:
getbattlerside BS_ATTACKER
jumpifbyte CMP_EQUAL, gBattleCommunication, B_SIDE_OPPONENT, BattleScript_EffectTeleportTryToRunAway
attackanimation
waitanimation
openpartyscreen BS_ATTACKER, BattleScript_EffectTeleportNewEnd
switchoutabilities BS_ATTACKER
waitstate
switchhandleorder BS_ATTACKER, 2
returntoball BS_ATTACKER
getswitchedmondata BS_ATTACKER
switchindataupdate BS_ATTACKER
hpthresholds BS_ATTACKER
trytoclearprimalweather
printstring STRINGID_EMPTYSTRING3
waitmessage 1
printstring STRINGID_SWITCHINMON
switchinanim BS_ATTACKER, TRUE
waitstate
switchineffects BS_ATTACKER
BattleScript_EffectTeleportNewEnd:
goto BattleScript_MoveEnd

BattleScript_EffectBeatUp::
attackcanceler
accuracycheck BattleScript_PrintMoveMissed, ACC_CURR_MOVE
Expand Down Expand Up @@ -7897,7 +7873,7 @@ BattleScript_WishMegaEvolution::
BattleScript_PrimalReversion::
call BattleScript_PrimalReversionRet
end2

BattleScript_PrimalReversionRestoreAttacker::
call BattleScript_PrimalReversionRet
copybyte gBattlerAttacker, sSAVED_BATTLER
Expand Down Expand Up @@ -8944,7 +8920,7 @@ BattleScript_BadDreams_ShowPopUp:
goto BattleScript_BadDreams_DmgAfterPopUp
BattleScript_BadDreams_HidePopUp:
destroyabilitypopup
tryfaintmon BS_TARGET
tryfaintmon BS_TARGET
goto BattleScript_BadDreamsIncrement

BattleScript_TookAttack::
Expand Down
34 changes: 0 additions & 34 deletions src/battle_script_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -8837,33 +8837,6 @@ static void HandleScriptMegaPrimal(u32 caseId, u32 battlerId, bool32 isMega)
}
}

static bool32 CanTeleport(u8 battlerId)
{
struct Pokemon *party = GetBattlerParty(battlerId);
u32 species, count, i;

for (i = 0; i < PARTY_SIZE; i++)
{
species = GetMonData(&party[i], MON_DATA_SPECIES_OR_EGG);
if (species != SPECIES_NONE && species != SPECIES_EGG && GetMonData(&party[i], MON_DATA_HP) != 0)
count++;
}

switch (GetBattlerSide(battlerId))
{
case B_SIDE_OPPONENT:
if (count == 1 || gBattleTypeFlags & BATTLE_TYPE_DOUBLE)
return FALSE;
break;
case B_SIDE_PLAYER:
if (count == 1 || (gBattleTypeFlags & BATTLE_TYPE_DOUBLE && count <= 2))
return FALSE;
break;
}

return TRUE;
}

// Return True if the order was changed, and false if the order was not changed(for example because the target would move after the attacker anyway).
static bool32 ChangeOrderTargetAfterAttacker(void)
{
Expand Down Expand Up @@ -16327,13 +16300,6 @@ void BS_GetBattlerSide(void)
gBattlescriptCurrInstr = cmd->nextInstr;
}

void BS_CanTeleport(void)
{
NATIVE_ARGS(u8 battler);
gBattleCommunication[0] = CanTeleport(cmd->battler);
gBattlescriptCurrInstr = cmd->nextInstr;
}

void BS_TrySymbiosis(void)
{
NATIVE_ARGS();
Expand Down
61 changes: 61 additions & 0 deletions test/move_effect_teleport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "global.h"
#include "test_battle.h"

ASSUMPTIONS
{
ASSUME(gBattleMoves[MOVE_TELEPORT].effect == EFFECT_TELEPORT);
}

SINGLE_BATTLE_TEST("Teleport fails when there is no pokemon to switch in")
{
GIVEN {
PLAYER(SPECIES_WOBBUFFET);
OPPONENT(SPECIES_WOBBUFFET);
} WHEN {
TURN { MOVE(opponent, MOVE_TELEPORT); }
} SCENE {
MESSAGE("But it failed!");
}
}

SINGLE_BATTLE_TEST("Teleport fails when there no alive pokemon left")
{
GIVEN {
PLAYER(SPECIES_WOBBUFFET);
OPPONENT(SPECIES_WOBBUFFET);
OPPONENT(SPECIES_WYNAUT) { HP(0); }
} WHEN {
TURN { MOVE(opponent, MOVE_TELEPORT); }
} SCENE {
MESSAGE("But it failed!");
}
}

SINGLE_BATTLE_TEST("Teleport forces the pokemon to switch out")
{
GIVEN {
PLAYER(SPECIES_WOBBUFFET);
OPPONENT(SPECIES_WOBBUFFET);
OPPONENT(SPECIES_WYNAUT);
} WHEN {
TURN { MOVE(opponent, MOVE_TELEPORT); SEND_OUT(opponent, 1); }
} SCENE {
ANIMATION(ANIM_TYPE_MOVE, MOVE_TELEPORT, opponent);
MESSAGE("2 sent out Wynaut!");
}
}

SINGLE_BATTLE_TEST("Teleport does not fail if the user is trapped")
{
GIVEN {
PLAYER(SPECIES_WOBBUFFET);
OPPONENT(SPECIES_WOBBUFFET);
OPPONENT(SPECIES_WYNAUT);
} WHEN {
TURN { MOVE(player, MOVE_FIRE_SPIN); MOVE(opponent, MOVE_TELEPORT); SEND_OUT(opponent, 1); }
} SCENE {
ANIMATION(ANIM_TYPE_MOVE, MOVE_FIRE_SPIN, player);
ANIMATION(ANIM_TYPE_MOVE, MOVE_TELEPORT, opponent);
MESSAGE("2 sent out Wynaut!");
}
}

0 comments on commit 42992ca

Please sign in to comment.