diff --git a/src/ui/party-ui-handler.ts b/src/ui/party-ui-handler.ts index f6105c51ee3..74d4e5bcb17 100644 --- a/src/ui/party-ui-handler.ts +++ b/src/ui/party-ui-handler.ts @@ -52,6 +52,7 @@ export enum PartyUiMode { * Indicates that the party UI is open because of a start-of-encounter optional * switch. This type of switch can be cancelled. */ + // TODO: Rename to PRE_BATTLE_SWITCH POST_BATTLE_SWITCH, /** * Indicates that the party UI is open because of the move Revival Blessing. @@ -356,6 +357,522 @@ export default class PartyUiHandler extends MessageUiHandler { return true; } + private processSummaryOption(pokemon: Pokemon): boolean { + const ui = this.getUi(); + ui.playSelect(); + ui.setModeWithoutClear(UiMode.SUMMARY, pokemon).then(() => this.clearOptions()); + return true; + } + + private processPokedexOption(pokemon: Pokemon): boolean { + const ui = this.getUi(); + ui.playSelect(); + const attributes = { + shiny: pokemon.shiny, + variant: pokemon.variant, + form: pokemon.formIndex, + female: pokemon.gender === Gender.FEMALE, + }; + ui.setOverlayMode(UiMode.POKEDEX_PAGE, pokemon.species, attributes).then(() => this.clearOptions()); + return true; + } + + private processUnpauseEvolutionOption(pokemon: Pokemon): boolean { + const ui = this.getUi(); + this.clearOptions(); + ui.playSelect(); + pokemon.pauseEvolutions = !pokemon.pauseEvolutions; + this.showText( + i18next.t(pokemon.pauseEvolutions ? "partyUiHandler:pausedEvolutions" : "partyUiHandler:unpausedEvolutions", { + pokemonName: getPokemonNameWithAffix(pokemon, false), + }), + undefined, + () => this.showText("", 0), + null, + true, + ); + return true; + } + + private processUnspliceOption(pokemon: PlayerPokemon): boolean { + const ui = this.getUi(); + this.clearOptions(); + ui.playSelect(); + this.showText( + i18next.t("partyUiHandler:unspliceConfirmation", { + fusionName: pokemon.fusionSpecies?.name, + pokemonName: pokemon.getName(), + }), + null, + () => { + ui.setModeWithoutClear( + UiMode.CONFIRM, + () => { + const fusionName = pokemon.getName(); + pokemon.unfuse().then(() => { + this.clearPartySlots(); + this.populatePartySlots(); + ui.setMode(UiMode.PARTY); + this.showText( + i18next.t("partyUiHandler:wasReverted", { + fusionName: fusionName, + pokemonName: pokemon.getName(false), + }), + undefined, + () => { + ui.setMode(UiMode.PARTY); + this.showText("", 0); + }, + null, + true, + ); + }); + }, + () => { + ui.setMode(UiMode.PARTY); + this.showText("", 0); + }, + ); + }, + ); + return true; + } + + private processReleaseOption(pokemon: Pokemon): boolean { + const ui = this.getUi(); + this.clearOptions(); + ui.playSelect(); + // In release mode, we do not ask for confirmation when clicking release. + if (this.partyUiMode === PartyUiMode.RELEASE) { + this.doRelease(this.cursor); + return true; + } + if (this.cursor >= globalScene.currentBattle.getBattlerCount() || !pokemon.isAllowedInBattle()) { + this.blockInput = true; + this.showText( + i18next.t("partyUiHandler:releaseConfirmation", { + pokemonName: getPokemonNameWithAffix(pokemon, false), + }), + null, + () => { + this.blockInput = false; + ui.setModeWithoutClear( + UiMode.CONFIRM, + () => { + ui.setMode(UiMode.PARTY); + this.doRelease(this.cursor); + }, + () => { + ui.setMode(UiMode.PARTY); + this.showText("", 0); + }, + ); + }, + ); + } else { + this.showText(i18next.t("partyUiHandler:releaseInBattle"), null, () => this.showText("", 0), null, true); + } + return true; + } + + private processRenameOption(pokemon: Pokemon): boolean { + const ui = this.getUi(); + this.clearOptions(); + ui.playSelect(); + ui.setModeWithoutClear( + UiMode.RENAME_POKEMON, + { + buttonActions: [ + (nickname: string) => { + ui.playSelect(); + pokemon.nickname = nickname; + pokemon.updateInfo(); + this.clearPartySlots(); + this.populatePartySlots(); + ui.setMode(UiMode.PARTY); + }, + () => { + ui.setMode(UiMode.PARTY); + }, + ], + }, + pokemon, + ); + return true; + } + + // TODO: Does this need to check that selectCallback exists? + private processTransferOption(): boolean { + const ui = this.getUi(); + if (this.transferCursor !== this.cursor) { + if (this.transferAll) { + this.getTransferrableItemsFromPokemon(globalScene.getPlayerParty()[this.transferCursor]).forEach( + (_, i, array) => { + const invertedIndex = array.length - 1 - i; + (this.selectCallback as PartyModifierTransferSelectCallback)( + this.transferCursor, + invertedIndex, + this.transferQuantitiesMax[invertedIndex], + this.cursor, + ); + }, + ); + } else { + (this.selectCallback as PartyModifierTransferSelectCallback)( + this.transferCursor, + this.transferOptionCursor, + this.transferQuantities[this.transferOptionCursor], + this.cursor, + ); + } + } + this.clearTransfer(); + this.clearOptions(); + ui.playSelect(); + return true; + } + + // TODO: This will be largely changed with the modifier rework + private processModifierTransferModeInput(pokemon: PlayerPokemon) { + const ui = this.getUi(); + const option = this.options[this.optionsCursor]; + + if (option === PartyOption.TRANSFER) { + return this.processTransferOption(); + } + + // TODO: Revise this condition + if (!this.transferMode) { + this.startTransfer(); + + let ableToTransferText: string; + for (let p = 0; p < globalScene.getPlayerParty().length; p++) { + // this for look goes through each of the party pokemon + const newPokemon = globalScene.getPlayerParty()[p]; + // this next bit checks to see if the the selected item from the original transfer pokemon exists on the new pokemon `p` + // this returns `undefined` if the new pokemon doesn't have the item at all, otherwise it returns the `pokemonHeldItemModifier` for that item + const matchingModifier = globalScene.findModifier( + m => + m instanceof PokemonHeldItemModifier && + m.pokemonId === newPokemon.id && + m.matchType(this.getTransferrableItemsFromPokemon(pokemon)[this.transferOptionCursor]), + ) as PokemonHeldItemModifier; + const partySlot = this.partySlots.filter(m => m.getPokemon() === newPokemon)[0]; // this gets pokemon [p] for us + if (p !== this.transferCursor) { + // this skips adding the able/not able labels on the pokemon doing the transfer + if (matchingModifier) { + // if matchingModifier exists then the item exists on the new pokemon + if (matchingModifier.getMaxStackCount() === matchingModifier.stackCount) { + // checks to see if the stack of items is at max stack; if so, set the description label to "Not able" + ableToTransferText = i18next.t("partyUiHandler:notAble"); + } else { + // if the pokemon isn't at max stack, make the label "Able" + ableToTransferText = i18next.t("partyUiHandler:able"); + } + } else { + // if matchingModifier doesn't exist, that means the pokemon doesn't have any of the item, and we need to show "Able" + ableToTransferText = i18next.t("partyUiHandler:able"); + } + } else { + // this else relates to the transfer pokemon. We set the text to be blank so there's no "Able"/"Not able" text + ableToTransferText = ""; + } + partySlot.slotHpBar.setVisible(false); + partySlot.slotHpOverlay.setVisible(false); + partySlot.slotHpText.setVisible(false); + partySlot.slotDescriptionLabel.setText(ableToTransferText); + partySlot.slotDescriptionLabel.setVisible(true); + } + this.clearOptions(); + ui.playSelect(); + return true; + } + return false; + } + + // TODO: Might need to check here for when this.transferMode is active. + private processModifierTransferModeLeftRightInput(button: Button) { + let success = false; + const option = this.options[this.optionsCursor]; + if (button === Button.LEFT) { + /** Decrease quantity for the current item and update UI */ + if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { + this.transferQuantities[option] = + this.transferQuantities[option] === 1 + ? this.transferQuantitiesMax[option] + : this.transferQuantities[option] - 1; + this.updateOptions(); + success = this.setCursor( + this.optionsCursor, + ); /** Place again the cursor at the same position. Necessary, otherwise the cursor disappears */ + } + } + + if (button === Button.RIGHT) { + /** Increase quantity for the current item and update UI */ + if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { + this.transferQuantities[option] = + this.transferQuantities[option] === this.transferQuantitiesMax[option] + ? 1 + : this.transferQuantities[option] + 1; + this.updateOptions(); + success = this.setCursor( + this.optionsCursor, + ); /** Place again the cursor at the same position. Necessary, otherwise the cursor disappears */ + } + } + return success; + } + + // TODO: Might need to check here for when this.transferMode is active. + private processModifierTransferModeUpDownInput(button: Button.UP | Button.DOWN) { + let success = false; + const option = this.options[this.optionsCursor]; + + if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { + if (option !== PartyOption.ALL) { + this.transferQuantities[option] = this.transferQuantitiesMax[option]; + } + this.updateOptions(); + } + success = this.moveOptionCursor(button); + + return success; + } + + private moveOptionCursor(button: Button.UP | Button.DOWN): boolean { + if (button === Button.UP) { + return this.setCursor(this.optionsCursor ? this.optionsCursor - 1 : this.options.length - 1); + } + return this.setCursor(this.optionsCursor < this.options.length - 1 ? this.optionsCursor + 1 : 0); + } + + private processRememberMoveModeInput(pokemon: PlayerPokemon) { + const ui = this.getUi(); + const option = this.options[this.optionsCursor]; + + // clear overlay on cancel + this.moveInfoOverlay.clear(); + const filterResult = (this.selectFilter as PokemonSelectFilter)(pokemon); + if (filterResult === null) { + this.selectCallback?.(this.cursor, option); + this.clearOptions(); + } else { + this.clearOptions(); + this.showText(filterResult as string, undefined, () => this.showText("", 0), undefined, true); + } + ui.playSelect(); + return true; + } + + private processRememberMoveModeUpDownInput(button: Button.UP | Button.DOWN) { + let success = false; + + success = this.moveOptionCursor(button); + + // show move description + const option = this.options[this.optionsCursor]; + const pokemon = globalScene.getPlayerParty()[this.cursor]; + const move = allMoves[pokemon.getLearnableLevelMoves()[option]]; + if (move) { + this.moveInfoOverlay.show(move); + } else { + // or hide the overlay, in case it's the cancel button + this.moveInfoOverlay.clear(); + } + + return success; + } + + private getTransferrableItemsFromPokemon(pokemon: PlayerPokemon) { + return globalScene.findModifiers( + m => m instanceof PokemonHeldItemModifier && m.isTransferable && m.pokemonId === pokemon.id, + ) as PokemonHeldItemModifier[]; + } + + private getFilterResult(option: number, pokemon: PlayerPokemon): string | null { + let filterResult: string | null; + if (option !== PartyOption.TRANSFER && option !== PartyOption.SPLICE) { + filterResult = (this.selectFilter as PokemonSelectFilter)(pokemon); + if (filterResult === null && (option === PartyOption.SEND_OUT || option === PartyOption.PASS_BATON)) { + filterResult = this.FilterChallengeLegal(pokemon); + } + if (filterResult === null && this.partyUiMode === PartyUiMode.MOVE_MODIFIER) { + filterResult = this.moveSelectFilter(pokemon.moveset[this.optionsCursor]); + } + } else { + filterResult = (this.selectFilter as PokemonModifierTransferSelectFilter)( + pokemon, + this.getTransferrableItemsFromPokemon(globalScene.getPlayerParty()[this.transferCursor])[ + this.transferOptionCursor + ], + ); + } + return filterResult; + } + + private processActionButtonForOptions(option: PartyOption) { + const ui = this.getUi(); + if (option === PartyOption.CANCEL) { + return this.processOptionMenuInput(Button.CANCEL); + } + + // If the input has been already processed we are done, otherwise move on until the correct option is found + const pokemon = globalScene.getPlayerParty()[this.cursor]; + + // TODO: Careful about using success for the return values here. Find a better way + // PartyOption.ALL, and options specific to the mode (held items) + if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { + return this.processModifierTransferModeInput(pokemon); + } + + // options specific to the mode (moves) + if (this.partyUiMode === PartyUiMode.REMEMBER_MOVE_MODIFIER) { + return this.processRememberMoveModeInput(pokemon); + } + + // These are the options that do not involve a callback + if (option === PartyOption.SUMMARY) { + return this.processSummaryOption(pokemon); + } + if (option === PartyOption.POKEDEX) { + return this.processPokedexOption(pokemon); + } + if (option === PartyOption.UNPAUSE_EVOLUTION) { + return this.processUnpauseEvolutionOption(pokemon); + } + if (option === PartyOption.UNSPLICE) { + return this.processUnspliceOption(pokemon); + } + if (option === PartyOption.RENAME) { + return this.processRenameOption(pokemon); + } + // This is only relevant for PartyUiMode.CHECK + // TODO: This risks hitting the other options (.MOVE_i and ALL) so does it? Do we need an extra check? + if ( + option >= PartyOption.FORM_CHANGE_ITEM && + globalScene.getCurrentPhase() instanceof SelectModifierPhase && + this.partyUiMode === PartyUiMode.CHECK + ) { + const formChangeItemModifiers = this.getFormChangeItemsModifiers(pokemon); + const modifier = formChangeItemModifiers[option - PartyOption.FORM_CHANGE_ITEM]; + modifier.active = !modifier.active; + globalScene.triggerPokemonFormChange(pokemon, SpeciesFormChangeItemTrigger, false, true); + } + + // If the pokemon is filtered out for this option, we cannot continue + const filterResult = this.getFilterResult(option, pokemon); + if (filterResult) { + this.clearOptions(); + this.showText(filterResult as string, undefined, () => this.showText("", 0), undefined, true); + return true; + } + + // For what modes is a selectCallback needed? + // PartyUiMode.SELECT (SELECT) + // PartyUiMode.RELEASE (RELEASE) + // PartyUiMode.FAINT_SWITCH (SEND_OUT or PASS_BATON (?)) + // PartyUiMode.REVIVAL_BLESSING (REVIVE) + // PartyUiMode.MODIFIER_TRANSFER (held items, and ALL) + // PartyUiMode.CHECK --- no specific option, only relevant on cancel? + // PartyUiMode.SPLICE (SPLICE) + // PartyUiMode.MOVE_MODIFIER (MOVE_1, MOVE_2, MOVE_3, MOVE_4) + // PartyUiMode.TM_MODIFIER (TEACH) + // PartyUiMode.REMEMBER_MOVE_MODIFIER (no specific option, callback is invoked when selecting a move) + // PartyUiMode.MODIFIER (APPLY option) + // PartyUiMode.POST_BATTLE_SWITCH (SEND_OUT) + + // These are the options that need a callback + if (option === PartyOption.RELEASE) { + return this.processReleaseOption(pokemon); + } + + if (this.partyUiMode === PartyUiMode.SPLICE) { + if (option === PartyOption.SPLICE) { + (this.selectCallback as PartyModifierSpliceSelectCallback)(this.transferCursor, this.cursor); + this.clearTransfer(); + } else if (option === PartyOption.APPLY) { + this.startTransfer(); + } + this.clearOptions(); + ui.playSelect(); + return true; + } + + // This is used when switching out using the Pokemon command (possibly holding a Baton held item). In this case there is no callback. + if ( + (option === PartyOption.PASS_BATON || option === PartyOption.SEND_OUT) && + this.partyUiMode === PartyUiMode.SWITCH + ) { + this.clearOptions(); + (globalScene.getCurrentPhase() as CommandPhase).handleCommand( + Command.POKEMON, + this.cursor, + option === PartyOption.PASS_BATON, + ); + } + + if ( + [ + PartyOption.SEND_OUT, // When sending out at the start of battle, or due to an effect + PartyOption.PASS_BATON, // When passing the baton due to the Baton Pass move + PartyOption.REVIVE, + PartyOption.APPLY, + PartyOption.TEACH, + PartyOption.MOVE_1, + PartyOption.MOVE_2, + PartyOption.MOVE_3, + PartyOption.MOVE_4, + PartyOption.SELECT, + ].includes(option) && + this.selectCallback + ) { + this.clearOptions(); + const selectCallback = this.selectCallback; + this.selectCallback = null; + selectCallback(this.cursor, option); + return true; + } + + return false; + } + + private processOptionMenuInput(button: Button) { + const ui = this.getUi(); + const option = this.options[this.optionsCursor]; + + // Button.CANCEL has no special behavior for any option + if (button === Button.CANCEL) { + this.clearOptions(); + ui.playSelect(); + return true; + } + + if (button === Button.ACTION) { + return this.processActionButtonForOptions(option); + } + + if (button === Button.UP || button === Button.DOWN) { + if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { + return this.processModifierTransferModeUpDownInput(button); + } + + if (this.partyUiMode === PartyUiMode.REMEMBER_MOVE_MODIFIER) { + return this.processRememberMoveModeUpDownInput(button); + } + + return this.moveOptionCursor(button); + } + + if (button === Button.LEFT || button === Button.RIGHT) { + if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { + return this.processModifierTransferModeLeftRightInput(button); + } + } + + return false; + } + processInput(button: Button): boolean { const ui = this.getUi(); @@ -375,463 +892,120 @@ export default class PartyUiHandler extends MessageUiHandler { return false; } - let success = false; - if (this.optionsMode) { - const option = this.options[this.optionsCursor]; - if (button === Button.ACTION) { - const pokemon = globalScene.getPlayerParty()[this.cursor]; - if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER && !this.transferMode && option !== PartyOption.CANCEL) { - this.startTransfer(); - - let ableToTransfer: string; - for (let p = 0; p < globalScene.getPlayerParty().length; p++) { - // this for look goes through each of the party pokemon - const newPokemon = globalScene.getPlayerParty()[p]; - // this next line gets all of the transferable items from pokemon [p]; it does this by getting all the held modifiers that are transferable and checking to see if they belong to pokemon [p] - const getTransferrableItemsFromPokemon = (newPokemon: PlayerPokemon) => - globalScene.findModifiers( - m => - m instanceof PokemonHeldItemModifier && - (m as PokemonHeldItemModifier).isTransferable && - (m as PokemonHeldItemModifier).pokemonId === newPokemon.id, - ) as PokemonHeldItemModifier[]; - // this next bit checks to see if the the selected item from the original transfer pokemon exists on the new pokemon [p]; this returns undefined if the new pokemon doesn't have the item at all, otherwise it returns the pokemonHeldItemModifier for that item - const matchingModifier = globalScene.findModifier( - m => - m instanceof PokemonHeldItemModifier && - m.pokemonId === newPokemon.id && - m.matchType(getTransferrableItemsFromPokemon(pokemon)[this.transferOptionCursor]), - ) as PokemonHeldItemModifier; - const partySlot = this.partySlots.filter(m => m.getPokemon() === newPokemon)[0]; // this gets pokemon [p] for us - if (p !== this.transferCursor) { - // this skips adding the able/not able labels on the pokemon doing the transfer - if (matchingModifier) { - // if matchingModifier exists then the item exists on the new pokemon - if (matchingModifier.getMaxStackCount() === matchingModifier.stackCount) { - // checks to see if the stack of items is at max stack; if so, set the description label to "Not able" - ableToTransfer = i18next.t("partyUiHandler:notAble"); - } else { - // if the pokemon isn't at max stack, make the label "Able" - ableToTransfer = i18next.t("partyUiHandler:able"); - } - } else { - // if matchingModifier doesn't exist, that means the pokemon doesn't have any of the item, and we need to show "Able" - ableToTransfer = i18next.t("partyUiHandler:able"); - } - } else { - // this else relates to the transfer pokemon. We set the text to be blank so there's no "Able"/"Not able" text - ableToTransfer = ""; - } - partySlot.slotHpBar.setVisible(false); - partySlot.slotHpOverlay.setVisible(false); - partySlot.slotHpText.setVisible(false); - partySlot.slotDescriptionLabel.setText(ableToTransfer); - partySlot.slotDescriptionLabel.setVisible(true); - } - - this.clearOptions(); - ui.playSelect(); - return true; - } - if (this.partyUiMode === PartyUiMode.REMEMBER_MOVE_MODIFIER && option !== PartyOption.CANCEL) { - // clear overlay on cancel - this.moveInfoOverlay.clear(); - const filterResult = (this.selectFilter as PokemonSelectFilter)(pokemon); - if (filterResult === null) { - this.selectCallback?.(this.cursor, option); - this.clearOptions(); - } else { - this.clearOptions(); - this.showText(filterResult as string, undefined, () => this.showText("", 0), undefined, true); - } - ui.playSelect(); - return true; - } - if ( - ![ - PartyOption.SUMMARY, - PartyOption.POKEDEX, - PartyOption.UNPAUSE_EVOLUTION, - PartyOption.UNSPLICE, - PartyOption.RELEASE, - PartyOption.CANCEL, - PartyOption.RENAME, - ].includes(option) || - (option === PartyOption.RELEASE && this.partyUiMode === PartyUiMode.RELEASE) - ) { - let filterResult: string | null; - const getTransferrableItemsFromPokemon = (pokemon: PlayerPokemon) => - globalScene.findModifiers( - m => m instanceof PokemonHeldItemModifier && m.isTransferable && m.pokemonId === pokemon.id, - ) as PokemonHeldItemModifier[]; - if (option !== PartyOption.TRANSFER && option !== PartyOption.SPLICE) { - filterResult = (this.selectFilter as PokemonSelectFilter)(pokemon); - if (filterResult === null && (option === PartyOption.SEND_OUT || option === PartyOption.PASS_BATON)) { - filterResult = this.FilterChallengeLegal(pokemon); - } - if (filterResult === null && this.partyUiMode === PartyUiMode.MOVE_MODIFIER) { - filterResult = this.moveSelectFilter(pokemon.moveset[this.optionsCursor]); - } - } else { - filterResult = (this.selectFilter as PokemonModifierTransferSelectFilter)( - pokemon, - getTransferrableItemsFromPokemon(globalScene.getPlayerParty()[this.transferCursor])[ - this.transferOptionCursor - ], - ); - } - if (filterResult === null) { - if (this.partyUiMode !== PartyUiMode.SPLICE) { - this.clearOptions(); - } - if (this.selectCallback && this.partyUiMode !== PartyUiMode.CHECK) { - if (option === PartyOption.TRANSFER) { - if (this.transferCursor !== this.cursor) { - if (this.transferAll) { - getTransferrableItemsFromPokemon(globalScene.getPlayerParty()[this.transferCursor]).forEach( - (_, i, array) => { - const invertedIndex = array.length - 1 - i; - (this.selectCallback as PartyModifierTransferSelectCallback)( - this.transferCursor, - invertedIndex, - this.transferQuantitiesMax[invertedIndex], - this.cursor, - ); - }, - ); - } else { - (this.selectCallback as PartyModifierTransferSelectCallback)( - this.transferCursor, - this.transferOptionCursor, - this.transferQuantities[this.transferOptionCursor], - this.cursor, - ); - } - } - this.clearTransfer(); - } else if (this.partyUiMode === PartyUiMode.SPLICE) { - if (option === PartyOption.SPLICE) { - (this.selectCallback as PartyModifierSpliceSelectCallback)(this.transferCursor, this.cursor); - this.clearTransfer(); - } else { - this.startTransfer(); - } - this.clearOptions(); - } else if (option === PartyOption.RELEASE) { - this.doRelease(this.cursor); - } else { - const selectCallback = this.selectCallback; - this.selectCallback = null; - selectCallback(this.cursor, option); - } - } else { - if ( - option >= PartyOption.FORM_CHANGE_ITEM && - globalScene.getCurrentPhase() instanceof SelectModifierPhase - ) { - if (this.partyUiMode === PartyUiMode.CHECK) { - const formChangeItemModifiers = this.getFormChangeItemsModifiers(pokemon); - const modifier = formChangeItemModifiers[option - PartyOption.FORM_CHANGE_ITEM]; - modifier.active = !modifier.active; - globalScene.triggerPokemonFormChange(pokemon, SpeciesFormChangeItemTrigger, false, true); - } - } else if (this.cursor) { - (globalScene.getCurrentPhase() as CommandPhase).handleCommand( - Command.POKEMON, - this.cursor, - option === PartyOption.PASS_BATON, - ); - } - } - if ( - this.partyUiMode !== PartyUiMode.MODIFIER && - this.partyUiMode !== PartyUiMode.TM_MODIFIER && - this.partyUiMode !== PartyUiMode.MOVE_MODIFIER - ) { - ui.playSelect(); - } - return true; - } - this.clearOptions(); - this.showText(filterResult as string, undefined, () => this.showText("", 0), undefined, true); - } else if (option === PartyOption.SUMMARY) { - ui.playSelect(); - ui.setModeWithoutClear(UiMode.SUMMARY, pokemon).then(() => this.clearOptions()); - return true; - } else if (option === PartyOption.POKEDEX) { - ui.playSelect(); - const attributes = { - shiny: pokemon.shiny, - variant: pokemon.variant, - form: pokemon.formIndex, - female: pokemon.gender === Gender.FEMALE, - }; - ui.setOverlayMode(UiMode.POKEDEX_PAGE, pokemon.species, attributes).then(() => this.clearOptions()); - return true; - } else if (option === PartyOption.UNPAUSE_EVOLUTION) { - this.clearOptions(); - ui.playSelect(); - pokemon.pauseEvolutions = !pokemon.pauseEvolutions; - this.showText( - i18next.t( - pokemon.pauseEvolutions ? "partyUiHandler:pausedEvolutions" : "partyUiHandler:unpausedEvolutions", - { pokemonName: getPokemonNameWithAffix(pokemon, false) }, - ), - undefined, - () => this.showText("", 0), - null, - true, - ); - } else if (option === PartyOption.UNSPLICE) { - this.clearOptions(); - ui.playSelect(); - this.showText( - i18next.t("partyUiHandler:unspliceConfirmation", { - fusionName: pokemon.fusionSpecies?.name, - pokemonName: pokemon.getName(), - }), - null, - () => { - ui.setModeWithoutClear( - UiMode.CONFIRM, - () => { - const fusionName = pokemon.getName(); - pokemon.unfuse().then(() => { - this.clearPartySlots(); - this.populatePartySlots(); - ui.setMode(UiMode.PARTY); - this.showText( - i18next.t("partyUiHandler:wasReverted", { - fusionName: fusionName, - pokemonName: pokemon.getName(false), - }), - undefined, - () => { - ui.setMode(UiMode.PARTY); - this.showText("", 0); - }, - null, - true, - ); - }); - }, - () => { - ui.setMode(UiMode.PARTY); - this.showText("", 0); - }, - ); - }, - ); - } else if (option === PartyOption.RELEASE) { - this.clearOptions(); - ui.playSelect(); - if (this.cursor >= globalScene.currentBattle.getBattlerCount() || !pokemon.isAllowedInBattle()) { - this.blockInput = true; - this.showText( - i18next.t("partyUiHandler:releaseConfirmation", { - pokemonName: getPokemonNameWithAffix(pokemon, false), - }), - null, - () => { - this.blockInput = false; - ui.setModeWithoutClear( - UiMode.CONFIRM, - () => { - ui.setMode(UiMode.PARTY); - this.doRelease(this.cursor); - }, - () => { - ui.setMode(UiMode.PARTY); - this.showText("", 0); - }, - ); - }, - ); - } else { - this.showText(i18next.t("partyUiHandler:releaseInBattle"), null, () => this.showText("", 0), null, true); - } - return true; - } else if (option === PartyOption.RENAME) { - this.clearOptions(); - ui.playSelect(); - ui.setModeWithoutClear( - UiMode.RENAME_POKEMON, - { - buttonActions: [ - (nickname: string) => { - ui.playSelect(); - pokemon.nickname = nickname; - pokemon.updateInfo(); - this.clearPartySlots(); - this.populatePartySlots(); - ui.setMode(UiMode.PARTY); - }, - () => { - ui.setMode(UiMode.PARTY); - }, - ], - }, - pokemon, - ); - return true; - } else if (option === PartyOption.CANCEL) { - return this.processInput(Button.CANCEL); - } else if (option === PartyOption.SELECT) { - ui.playSelect(); - return true; - } - } else if (button === Button.CANCEL) { - this.clearOptions(); + let success = false; + success = this.processOptionMenuInput(button); + if (success) { ui.playSelect(); - return true; + } + return success; + } + + if (button === Button.ACTION) { + return this.processPartyActionInput(); + } + + if (button === Button.CANCEL) { + return this.processPartyCancelInput(); + } + + if (button === Button.UP || button === Button.DOWN || button === Button.RIGHT || button === Button.LEFT) { + return this.processPartyDirectionalInput(button); + } + + return false; + } + + private allowCancel(): boolean { + return !(this.partyUiMode === PartyUiMode.FAINT_SWITCH || this.partyUiMode === PartyUiMode.REVIVAL_BLESSING); + } + + private processPartyActionInput(): boolean { + const ui = this.getUi(); + if (this.cursor < 6) { + if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER && !this.transferMode) { + /** Initialize item quantities for the selected Pokemon */ + const itemModifiers = globalScene.findModifiers( + m => + m instanceof PokemonHeldItemModifier && + m.isTransferable && + m.pokemonId === globalScene.getPlayerParty()[this.cursor].id, + ) as PokemonHeldItemModifier[]; + this.transferQuantities = itemModifiers.map(item => item.getStackCount()); + this.transferQuantitiesMax = itemModifiers.map(item => item.getStackCount()); + } + this.showOptions(); + ui.playSelect(); + } + // Pressing return button + if (this.cursor === 6) { + if (!this.allowCancel()) { + ui.playError(); } else { - switch (button) { - case Button.LEFT: - /** Decrease quantity for the current item and update UI */ - if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { - this.transferQuantities[option] = - this.transferQuantities[option] === 1 - ? this.transferQuantitiesMax[option] - : this.transferQuantities[option] - 1; - this.updateOptions(); - success = this.setCursor( - this.optionsCursor, - ); /** Place again the cursor at the same position. Necessary, otherwise the cursor disappears */ - } - break; - case Button.RIGHT: - /** Increase quantity for the current item and update UI */ - if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { - this.transferQuantities[option] = - this.transferQuantities[option] === this.transferQuantitiesMax[option] - ? 1 - : this.transferQuantities[option] + 1; - this.updateOptions(); - success = this.setCursor( - this.optionsCursor, - ); /** Place again the cursor at the same position. Necessary, otherwise the cursor disappears */ - } - break; - case Button.UP: - /** If currently selecting items to transfer, reset quantity selection */ - if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { - if (option !== PartyOption.ALL) { - this.transferQuantities[option] = this.transferQuantitiesMax[option]; - } - this.updateOptions(); - } - success = this.setCursor( - this.optionsCursor ? this.optionsCursor - 1 : this.options.length - 1, - ); /** Move cursor */ - break; - case Button.DOWN: - /** If currently selecting items to transfer, reset quantity selection */ - if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) { - if (option !== PartyOption.ALL) { - this.transferQuantities[option] = this.transferQuantitiesMax[option]; - } - this.updateOptions(); - } - success = this.setCursor( - this.optionsCursor < this.options.length - 1 ? this.optionsCursor + 1 : 0, - ); /** Move cursor */ - break; - } - - // show move description - if (this.partyUiMode === PartyUiMode.REMEMBER_MOVE_MODIFIER) { - const option = this.options[this.optionsCursor]; - const pokemon = globalScene.getPlayerParty()[this.cursor]; - const move = allMoves[pokemon.getLearnableLevelMoves()[option]]; - if (move) { - this.moveInfoOverlay.show(move); - } else { - // or hide the overlay, in case it's the cancel button - this.moveInfoOverlay.clear(); - } - } + return this.processInput(Button.CANCEL); } - } else { - if (button === Button.ACTION) { - if (this.cursor < 6) { - if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER && !this.transferMode) { - /** Initialize item quantities for the selected Pokemon */ - const itemModifiers = globalScene.findModifiers( - m => - m instanceof PokemonHeldItemModifier && - m.isTransferable && - m.pokemonId === globalScene.getPlayerParty()[this.cursor].id, - ) as PokemonHeldItemModifier[]; - this.transferQuantities = itemModifiers.map(item => item.getStackCount()); - this.transferQuantitiesMax = itemModifiers.map(item => item.getStackCount()); - } - this.showOptions(); - ui.playSelect(); - } else if (this.partyUiMode === PartyUiMode.FAINT_SWITCH || this.partyUiMode === PartyUiMode.REVIVAL_BLESSING) { - ui.playError(); - } else { - return this.processInput(Button.CANCEL); - } - return true; + } + return true; + } + + private processPartyCancelInput(): boolean { + const ui = this.getUi(); + if ( + (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER || this.partyUiMode === PartyUiMode.SPLICE) && + this.transferMode + ) { + this.clearTransfer(); + ui.playSelect(); + } else if (this.allowCancel()) { + if (this.selectCallback) { + const selectCallback = this.selectCallback; + this.selectCallback = null; + selectCallback(6, PartyOption.CANCEL); + ui.playSelect(); + } else { + ui.setMode(UiMode.COMMAND, this.fieldIndex); + ui.playSelect(); } - if (button === Button.CANCEL) { - if ( - (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER || this.partyUiMode === PartyUiMode.SPLICE) && - this.transferMode - ) { - this.clearTransfer(); - ui.playSelect(); - } else if (this.partyUiMode !== PartyUiMode.FAINT_SWITCH && this.partyUiMode !== PartyUiMode.REVIVAL_BLESSING) { - if (this.selectCallback) { - const selectCallback = this.selectCallback; - this.selectCallback = null; - selectCallback(6, PartyOption.CANCEL); - ui.playSelect(); - } else { - ui.setMode(UiMode.COMMAND, this.fieldIndex); - ui.playSelect(); - } + } + return true; + } + + private processPartyDirectionalInput(button: Button.UP | Button.DOWN | Button.LEFT | Button.RIGHT): boolean { + const ui = this.getUi(); + const slotCount = this.partySlots.length; + const battlerCount = globalScene.currentBattle.getBattlerCount(); + + let success = false; + switch (button) { + case Button.UP: + success = this.setCursor(this.cursor ? (this.cursor < 6 ? this.cursor - 1 : slotCount - 1) : 6); + break; + case Button.DOWN: + success = this.setCursor(this.cursor < 6 ? (this.cursor < slotCount - 1 ? this.cursor + 1 : 6) : 0); + break; + case Button.LEFT: + if (this.cursor >= battlerCount && this.cursor <= 6) { + success = this.setCursor(0); } - - return true; - } - - const slotCount = this.partySlots.length; - const battlerCount = globalScene.currentBattle.getBattlerCount(); - - switch (button) { - case Button.UP: - success = this.setCursor(this.cursor ? (this.cursor < 6 ? this.cursor - 1 : slotCount - 1) : 6); + break; + case Button.RIGHT: + if (slotCount === battlerCount) { + success = this.setCursor(6); break; - case Button.DOWN: - success = this.setCursor(this.cursor < 6 ? (this.cursor < slotCount - 1 ? this.cursor + 1 : 6) : 0); + } + if (battlerCount >= 2 && slotCount > battlerCount && this.getCursor() === 0 && this.lastCursor === 1) { + success = this.setCursor(2); break; - case Button.LEFT: - if (this.cursor >= battlerCount && this.cursor <= 6) { - success = this.setCursor(0); - } + } + if (slotCount > battlerCount && this.cursor < battlerCount) { + success = this.setCursor(this.lastCursor < 6 ? this.lastCursor || battlerCount : battlerCount); break; - case Button.RIGHT: - if (slotCount === battlerCount) { - success = this.setCursor(6); - break; - } - if (battlerCount >= 2 && slotCount > battlerCount && this.getCursor() === 0 && this.lastCursor === 1) { - success = this.setCursor(2); - break; - } - if (slotCount > battlerCount && this.cursor < battlerCount) { - success = this.setCursor(this.lastCursor < 6 ? this.lastCursor || battlerCount : battlerCount); - break; - } - } + } } if (success) { ui.playSelect(); } - return success; } @@ -860,64 +1034,66 @@ export default class PartyUiHandler extends MessageUiHandler { } setCursor(cursor: number): boolean { - let changed: boolean; - if (this.optionsMode) { - changed = this.optionsCursor !== cursor; - let isScroll = false; - if (changed && this.optionsScroll) { - if (Math.abs(cursor - this.optionsCursor) === this.options.length - 1) { - this.optionsScrollCursor = cursor ? this.optionsScrollTotal - 8 : 0; - this.updateOptions(); - } else { - const isDown = cursor && cursor > this.optionsCursor; - if (isDown) { - if (this.options[cursor] === PartyOption.SCROLL_DOWN) { - isScroll = true; - this.optionsScrollCursor++; - } - } else { - if (!cursor && this.optionsScrollCursor) { - isScroll = true; - this.optionsScrollCursor--; - } - } - if (isScroll && this.optionsScrollCursor === 1) { - this.optionsScrollCursor += isDown ? 1 : -1; - } - } + return this.setOptionsCursor(cursor); + } + const changed = this.cursor !== cursor; + if (changed) { + this.lastCursor = this.cursor; + this.cursor = cursor; + if (this.lastCursor < 6) { + this.partySlots[this.lastCursor].deselect(); + } else if (this.lastCursor === 6) { + this.partyCancelButton.deselect(); } - if (isScroll) { + if (cursor < 6) { + this.partySlots[cursor].select(); + } else if (cursor === 6) { + this.partyCancelButton.select(); + } + } + return changed; + } + + private setOptionsCursor(cursor: number): boolean { + const changed = this.optionsCursor !== cursor; + let isScroll = false; + if (changed && this.optionsScroll) { + if (Math.abs(cursor - this.optionsCursor) === this.options.length - 1) { + this.optionsScrollCursor = cursor ? this.optionsScrollTotal - 8 : 0; this.updateOptions(); } else { - this.optionsCursor = cursor; - } - if (!this.optionsCursorObj) { - this.optionsCursorObj = globalScene.add.image(0, 0, "cursor"); - this.optionsCursorObj.setOrigin(0, 0); - this.optionsContainer.add(this.optionsCursorObj); - } - this.optionsCursorObj.setPosition( - 8 - this.optionsBg.displayWidth, - -19 - 16 * (this.options.length - 1 - this.optionsCursor), - ); - } else { - changed = this.cursor !== cursor; - if (changed) { - this.lastCursor = this.cursor; - this.cursor = cursor; - if (this.lastCursor < 6) { - this.partySlots[this.lastCursor].deselect(); - } else if (this.lastCursor === 6) { - this.partyCancelButton.deselect(); + const isDown = cursor && cursor > this.optionsCursor; + if (isDown) { + if (this.options[cursor] === PartyOption.SCROLL_DOWN) { + isScroll = true; + this.optionsScrollCursor++; + } + } else { + if (!cursor && this.optionsScrollCursor) { + isScroll = true; + this.optionsScrollCursor--; + } } - if (cursor < 6) { - this.partySlots[cursor].select(); - } else if (cursor === 6) { - this.partyCancelButton.select(); + if (isScroll && this.optionsScrollCursor === 1) { + this.optionsScrollCursor += isDown ? 1 : -1; } } } + if (isScroll) { + this.updateOptions(); + } else { + this.optionsCursor = cursor; + } + if (!this.optionsCursorObj) { + this.optionsCursorObj = globalScene.add.image(0, 0, "cursor"); + this.optionsCursorObj.setOrigin(0, 0); + this.optionsContainer.add(this.optionsCursorObj); + } + this.optionsCursorObj.setPosition( + 8 - this.optionsBg.displayWidth, + -19 - 16 * (this.options.length - 1 - this.optionsCursor), + ); return changed; } @@ -984,149 +1160,81 @@ export default class PartyUiHandler extends MessageUiHandler { this.setCursor(0); } - updateOptions(): void { - const pokemon = globalScene.getPlayerParty()[this.cursor]; + private allowBatonModifierSwitch(): boolean { + return !!( + this.partyUiMode !== PartyUiMode.FAINT_SWITCH && + globalScene.findModifier( + m => + m instanceof SwitchEffectTransferModifier && + m.pokemonId === globalScene.getPlayerField()[this.fieldIndex].id, + ) + ); + } - const learnableLevelMoves = - this.partyUiMode === PartyUiMode.REMEMBER_MOVE_MODIFIER ? pokemon.getLearnableLevelMoves() : []; + // TODO: add FORCED_SWITCH (and perhaps also BATON_PASS_SWITCH) to the modes + private isBatonPassMove(): boolean { + const moveHistory = globalScene.getPlayerField()[this.fieldIndex].getMoveHistory(); + return !!( + this.partyUiMode === PartyUiMode.FAINT_SWITCH && + moveHistory.length && + allMoves[moveHistory[moveHistory.length - 1].move].getAttrs(ForceSwitchOutAttr)[0]?.isBatonPass() && + moveHistory[moveHistory.length - 1].result === MoveResult.SUCCESS + ); + } - if (this.partyUiMode === PartyUiMode.REMEMBER_MOVE_MODIFIER && learnableLevelMoves?.length) { + private getItemModifiers(pokemon: Pokemon): PokemonHeldItemModifier[] { + return ( + (globalScene.findModifiers( + m => m instanceof PokemonHeldItemModifier && m.isTransferable && m.pokemonId === pokemon.id, + ) as PokemonHeldItemModifier[]) ?? [] + ); + } + + private updateOptionsWithRememberMoveModifierMode(pokemon): void { + const learnableMoves = pokemon.getLearnableLevelMoves(); + for (let m = 0; m < learnableMoves.length; m++) { + this.options.push(m); + } + if (learnableMoves?.length) { // show the move overlay with info for the first move - this.moveInfoOverlay.show(allMoves[learnableLevelMoves[0]]); + this.moveInfoOverlay.show(allMoves[learnableMoves[0]]); } + } - const itemModifiers = - this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER - ? (globalScene.findModifiers( - m => m instanceof PokemonHeldItemModifier && m.isTransferable && m.pokemonId === pokemon.id, - ) as PokemonHeldItemModifier[]) - : []; - - if (this.options.length) { - this.options.splice(0, this.options.length); - this.optionsContainer.removeAll(true); - this.eraseOptionsCursor(); + private updateOptionsWithMoveModifierMode(pokemon): void { + // MOVE_1, MOVE_2, MOVE_3, MOVE_4 + for (let m = 0; m < pokemon.moveset.length; m++) { + this.options.push(PartyOption.MOVE_1 + m); } + } - let formChangeItemModifiers: PokemonFormChangeItemModifier[] | undefined; + private updateOptionsWithModifierTransferMode(pokemon): void { + const itemModifiers = this.getItemModifiers(pokemon); + for (let im = 0; im < itemModifiers.length; im++) { + this.options.push(im); + } + if (itemModifiers.length > 1) { + this.options.push(PartyOption.ALL); + } + } + + private addCommonOptions(pokemon): void { + this.options.push(PartyOption.SUMMARY); + this.options.push(PartyOption.POKEDEX); + this.options.push(PartyOption.RENAME); if ( - this.partyUiMode !== PartyUiMode.MOVE_MODIFIER && - this.partyUiMode !== PartyUiMode.REMEMBER_MOVE_MODIFIER && - (this.transferMode || this.partyUiMode !== PartyUiMode.MODIFIER_TRANSFER) + pokemonEvolutions.hasOwnProperty(pokemon.species.speciesId) || + (pokemon.isFusion() && pokemon.fusionSpecies && pokemonEvolutions.hasOwnProperty(pokemon.fusionSpecies.speciesId)) ) { - switch (this.partyUiMode) { - case PartyUiMode.SWITCH: - case PartyUiMode.FAINT_SWITCH: - case PartyUiMode.POST_BATTLE_SWITCH: - if (this.cursor >= globalScene.currentBattle.getBattlerCount()) { - const allowBatonModifierSwitch = - this.partyUiMode !== PartyUiMode.FAINT_SWITCH && - globalScene.findModifier( - m => - m instanceof SwitchEffectTransferModifier && - (m as SwitchEffectTransferModifier).pokemonId === globalScene.getPlayerField()[this.fieldIndex].id, - ); - - const moveHistory = globalScene.getPlayerField()[this.fieldIndex].getMoveHistory(); - const isBatonPassMove = - this.partyUiMode === PartyUiMode.FAINT_SWITCH && - moveHistory.length && - allMoves[moveHistory[moveHistory.length - 1].move].getAttrs(ForceSwitchOutAttr)[0]?.isBatonPass() && - moveHistory[moveHistory.length - 1].result === MoveResult.SUCCESS; - - // isBatonPassMove and allowBatonModifierSwitch shouldn't ever be true - // at the same time, because they both explicitly check for a mutually - // exclusive partyUiMode. But better safe than sorry. - this.options.push( - isBatonPassMove && !allowBatonModifierSwitch ? PartyOption.PASS_BATON : PartyOption.SEND_OUT, - ); - if (allowBatonModifierSwitch && !isBatonPassMove) { - // the BATON modifier gives an extra switch option for - // pokemon-command switches, allowing buffs to be optionally passed - this.options.push(PartyOption.PASS_BATON); - } - } - break; - case PartyUiMode.REVIVAL_BLESSING: - this.options.push(PartyOption.REVIVE); - break; - case PartyUiMode.MODIFIER: - this.options.push(PartyOption.APPLY); - break; - case PartyUiMode.TM_MODIFIER: - this.options.push(PartyOption.TEACH); - break; - case PartyUiMode.MODIFIER_TRANSFER: - this.options.push(PartyOption.TRANSFER); - break; - case PartyUiMode.SPLICE: - if (this.transferMode) { - if (this.cursor !== this.transferCursor) { - this.options.push(PartyOption.SPLICE); - } - } else { - this.options.push(PartyOption.APPLY); - } - break; - case PartyUiMode.RELEASE: - this.options.push(PartyOption.RELEASE); - break; - case PartyUiMode.CHECK: - if (globalScene.getCurrentPhase() instanceof SelectModifierPhase) { - formChangeItemModifiers = this.getFormChangeItemsModifiers(pokemon); - for (let i = 0; i < formChangeItemModifiers.length; i++) { - this.options.push(PartyOption.FORM_CHANGE_ITEM + i); - } - } - break; - case PartyUiMode.SELECT: - this.options.push(PartyOption.SELECT); - break; - } - - this.options.push(PartyOption.SUMMARY); - this.options.push(PartyOption.POKEDEX); - this.options.push(PartyOption.RENAME); - - if ( - pokemonEvolutions.hasOwnProperty(pokemon.species.speciesId) || - (pokemon.isFusion() && - pokemon.fusionSpecies && - pokemonEvolutions.hasOwnProperty(pokemon.fusionSpecies.speciesId)) - ) { - this.options.push(PartyOption.UNPAUSE_EVOLUTION); - } - - if (this.partyUiMode === PartyUiMode.SWITCH) { - if (pokemon.isFusion()) { - this.options.push(PartyOption.UNSPLICE); - } - this.options.push(PartyOption.RELEASE); - } else if (this.partyUiMode === PartyUiMode.SPLICE && pokemon.isFusion()) { - this.options.push(PartyOption.UNSPLICE); - } - } else if (this.partyUiMode === PartyUiMode.MOVE_MODIFIER) { - for (let m = 0; m < pokemon.moveset.length; m++) { - this.options.push(PartyOption.MOVE_1 + m); - } - } else if (this.partyUiMode === PartyUiMode.REMEMBER_MOVE_MODIFIER) { - const learnableMoves = pokemon.getLearnableLevelMoves(); - for (let m = 0; m < learnableMoves.length; m++) { - this.options.push(m); - } - } else { - for (let im = 0; im < itemModifiers.length; im++) { - this.options.push(im); - } - if (itemModifiers.length > 1) { - this.options.push(PartyOption.ALL); - } + this.options.push(PartyOption.UNPAUSE_EVOLUTION); } + } + private addCancelAndScrollOptions(): void { this.optionsScrollTotal = this.options.length; - let optionStartIndex = this.optionsScrollCursor; - let optionEndIndex = Math.min( + const optionStartIndex = this.optionsScrollCursor; + const optionEndIndex = Math.min( this.optionsScrollTotal, optionStartIndex + (!optionStartIndex || this.optionsScrollCursor + 8 >= this.optionsScrollTotal ? 8 : 7), ); @@ -1145,14 +1253,122 @@ export default class PartyUiHandler extends MessageUiHandler { } this.options.push(PartyOption.CANCEL); + } + + updateOptions(): void { + const pokemon = globalScene.getPlayerParty()[this.cursor]; + + if (this.options.length) { + this.options.splice(0, this.options.length); + this.optionsContainer.removeAll(true); + this.eraseOptionsCursor(); + } + + switch (this.partyUiMode) { + case PartyUiMode.MOVE_MODIFIER: + this.updateOptionsWithMoveModifierMode(pokemon); + break; + case PartyUiMode.REMEMBER_MOVE_MODIFIER: + this.updateOptionsWithRememberMoveModifierMode(pokemon); + break; + case PartyUiMode.MODIFIER_TRANSFER: + if (!this.transferMode) { + this.updateOptionsWithModifierTransferMode(pokemon); + } else { + this.options.push(PartyOption.TRANSFER); + this.addCommonOptions(pokemon); + } + break; + // TODO: This still needs to be broken up. + // It could use a rework differentiating different kind of switches + // to treat baton passing separately from switching on faint. + case PartyUiMode.SWITCH: + case PartyUiMode.FAINT_SWITCH: + case PartyUiMode.POST_BATTLE_SWITCH: + if (this.cursor >= globalScene.currentBattle.getBattlerCount()) { + const allowBatonModifierSwitch = this.allowBatonModifierSwitch(); + const isBatonPassMove = this.isBatonPassMove(); + + // isBatonPassMove and allowBatonModifierSwitch shouldn't ever be true + // at the same time, because they both explicitly check for a mutually + // exclusive partyUiMode. But better safe than sorry. + this.options.push( + isBatonPassMove && !allowBatonModifierSwitch ? PartyOption.PASS_BATON : PartyOption.SEND_OUT, + ); + if (allowBatonModifierSwitch && !isBatonPassMove) { + // the BATON modifier gives an extra switch option for + // pokemon-command switches, allowing buffs to be optionally passed + this.options.push(PartyOption.PASS_BATON); + } + } + this.addCommonOptions(pokemon); + if (this.partyUiMode === PartyUiMode.SWITCH) { + if (pokemon.isFusion()) { + this.options.push(PartyOption.UNSPLICE); + } + this.options.push(PartyOption.RELEASE); + } + break; + case PartyUiMode.REVIVAL_BLESSING: + this.options.push(PartyOption.REVIVE); + this.addCommonOptions(pokemon); + break; + case PartyUiMode.MODIFIER: + this.options.push(PartyOption.APPLY); + this.addCommonOptions(pokemon); + break; + case PartyUiMode.TM_MODIFIER: + this.options.push(PartyOption.TEACH); + this.addCommonOptions(pokemon); + break; + case PartyUiMode.SPLICE: + if (this.transferMode) { + if (this.cursor !== this.transferCursor) { + this.options.push(PartyOption.SPLICE); + } + } else { + this.options.push(PartyOption.APPLY); + } + this.addCommonOptions(pokemon); + if (pokemon.isFusion()) { + this.options.push(PartyOption.UNSPLICE); + } + break; + case PartyUiMode.RELEASE: + this.options.push(PartyOption.RELEASE); + this.addCommonOptions(pokemon); + break; + case PartyUiMode.CHECK: + if (globalScene.getCurrentPhase() instanceof SelectModifierPhase) { + const formChangeItemModifiers = this.getFormChangeItemsModifiers(pokemon); + for (let i = 0; i < formChangeItemModifiers.length; i++) { + this.options.push(PartyOption.FORM_CHANGE_ITEM + i); + } + } + this.addCommonOptions(pokemon); + break; + case PartyUiMode.SELECT: + this.options.push(PartyOption.SELECT); + this.addCommonOptions(pokemon); + break; + } + + // Generic, these are applied to all Modes + this.addCancelAndScrollOptions(); + + this.updateOptionsWindow(); + } + + private updateOptionsWindow(): void { + const pokemon = globalScene.getPlayerParty()[this.cursor]; this.optionsBg = addWindow(0, 0, 0, 16 * this.options.length + 13); this.optionsBg.setOrigin(1, 1); this.optionsContainer.add(this.optionsBg); - optionStartIndex = 0; - optionEndIndex = this.options.length; + const optionStartIndex = 0; + const optionEndIndex = this.options.length; let widestOptionWidth = 0; const optionTexts: BBCodeText[] = []; @@ -1185,6 +1401,7 @@ export default class PartyUiHandler extends MessageUiHandler { } break; default: + const formChangeItemModifiers = this.getFormChangeItemsModifiers(pokemon); if (formChangeItemModifiers && option >= PartyOption.FORM_CHANGE_ITEM) { const modifier = formChangeItemModifiers[option - PartyOption.FORM_CHANGE_ITEM]; optionName = `${modifier.active ? i18next.t("partyUiHandler:DEACTIVATE") : i18next.t("partyUiHandler:ACTIVATE")} ${modifier.type.name}`; @@ -1200,6 +1417,7 @@ export default class PartyUiHandler extends MessageUiHandler { break; } } else if (this.partyUiMode === PartyUiMode.REMEMBER_MOVE_MODIFIER) { + const learnableLevelMoves = pokemon.getLearnableLevelMoves(); const move = learnableLevelMoves[option]; optionName = allMoves[move].name; altText = !pokemon @@ -1209,6 +1427,7 @@ export default class PartyUiHandler extends MessageUiHandler { } else if (option === PartyOption.ALL) { optionName = i18next.t("partyUiHandler:ALL"); } else { + const itemModifiers = this.getItemModifiers(pokemon); const itemModifier = itemModifiers[option]; optionName = itemModifier.type.name; } @@ -1222,6 +1441,7 @@ export default class PartyUiHandler extends MessageUiHandler { optionText.setOrigin(0, 0); /** For every item that has stack bigger than 1, display the current quantity selection */ + const itemModifiers = this.getItemModifiers(pokemon); const itemModifier = itemModifiers[option]; if ( this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER &&