pokerogue/src/phases/switch-phase.ts
Bertie690 0f8b1f63b5
[Bug/Ability] Fixed bugs with Intimidate triggers after reload/initial switch (#6212)
* Added TODO test case + documentation for failing intim test

* Fixed comment

* Fixed intimidate bugs fr fr

* Update src/data/phase-priority-queue.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Update src/data/phase-priority-queue.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Cleanup remove phase logic

* Do not add unnecessary activateAbilityPhases for pokemon that do not have their passive enabled

* Remove leftover log messages

* Add TODO comment

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
Co-authored-by: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com>
2025-08-24 14:09:16 -05:00

94 lines
3.9 KiB
TypeScript

import { globalScene } from "#app/global-scene";
import { DynamicPhaseType } from "#enums/dynamic-phase-type";
import { SwitchType } from "#enums/switch-type";
import { UiMode } from "#enums/ui-mode";
import { BattlePhase } from "#phases/battle-phase";
import { PartyOption, PartyUiHandler, PartyUiMode } from "#ui/party-ui-handler";
/**
* Opens the party selector UI and transitions into a {@linkcode SwitchSummonPhase}
* for the player (if a switch would be valid for the current battle state).
*/
export class SwitchPhase extends BattlePhase {
public readonly phaseName = "SwitchPhase";
protected readonly fieldIndex: number;
private readonly switchType: SwitchType;
private readonly isModal: boolean;
private readonly doReturn: boolean;
/**
* Creates a new SwitchPhase
* @param switchType {@linkcode SwitchType} The type of switch logic this phase implements
* @param fieldIndex Field index to switch out
* @param isModal Indicates if the switch should be forced (true) or is
* optional (false).
* @param doReturn Indicates if the party member on the field should be
* recalled to ball or has already left the field. Passed to {@linkcode SwitchSummonPhase}.
*/
constructor(switchType: SwitchType, fieldIndex: number, isModal: boolean, doReturn: boolean) {
super();
this.switchType = switchType;
this.fieldIndex = fieldIndex;
this.isModal = isModal;
this.doReturn = doReturn;
}
start() {
super.start();
// Skip modal switch if impossible (no remaining party members that aren't in battle)
if (this.isModal && !globalScene.getPlayerParty().filter(p => p.isAllowedInBattle() && !p.isActive(true)).length) {
return super.end();
}
/**
* Skip if the fainted party member has been revived already. doReturn is
* only passed as `false` from FaintPhase (as opposed to other usages such
* as ForceSwitchOutAttr or CheckSwitchPhase), so we only want to check this
* if the mon should have already been returned but is still alive and well
* on the field. see also; battle.test.ts
*/
if (this.isModal && !this.doReturn && !globalScene.getPlayerParty()[this.fieldIndex].isFainted()) {
return super.end();
}
// Check if there is any space still in field
if (
this.isModal &&
globalScene.getPlayerField().filter(p => p.isAllowedInBattle() && p.isActive(true)).length >=
globalScene.currentBattle.getBattlerCount()
) {
return super.end();
}
// Override field index to 0 in case of double battle where 2/3 remaining legal party members fainted at once
const fieldIndex =
globalScene.currentBattle.getBattlerCount() === 1 || globalScene.getPokemonAllowedInBattle().length > 1
? this.fieldIndex
: 0;
globalScene.ui.setMode(
UiMode.PARTY,
this.isModal ? PartyUiMode.FAINT_SWITCH : PartyUiMode.POST_BATTLE_SWITCH,
fieldIndex,
(slotIndex: number, option: PartyOption) => {
if (slotIndex >= globalScene.currentBattle.getBattlerCount() && slotIndex < 6) {
// Remove any pre-existing PostSummonPhase under the same field index.
// Pre-existing PostSummonPhases may occur when this phase is invoked during a prompt to switch at the start of a wave.
// TODO: Separate the animations from `SwitchSummonPhase` and co. into another phase and use that on initial switch - this is a band-aid fix
globalScene.phaseManager.tryRemoveDynamicPhase(
DynamicPhaseType.POST_SUMMON,
p => p.is("PostSummonPhase") && p.player && p.fieldIndex === this.fieldIndex,
"all",
);
const switchType = option === PartyOption.PASS_BATON ? SwitchType.BATON_PASS : this.switchType;
globalScene.phaseManager.unshiftNew("SwitchSummonPhase", switchType, fieldIndex, slotIndex, this.doReturn);
}
globalScene.ui.setMode(UiMode.MESSAGE).then(() => super.end());
},
PartyUiHandler.FilterNonFainted,
);
}
}