From 5a24435047b4bbd98f5b21b0828560a80378ba3d Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sat, 21 Sep 2024 11:49:21 -0400 Subject: [PATCH] [Bug] Fix game freeze from GTS infinite loop --- .../encounters/global-trade-system-encounter.ts | 13 ++++++------- .../global-trade-system-encounter.test.ts | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/data/mystery-encounters/encounters/global-trade-system-encounter.ts b/src/data/mystery-encounters/encounters/global-trade-system-encounter.ts index 55d4953d438..95e12339913 100644 --- a/src/data/mystery-encounters/encounters/global-trade-system-encounter.ts +++ b/src/data/mystery-encounters/encounters/global-trade-system-encounter.ts @@ -431,14 +431,13 @@ function getPokemonTradeOptions(scene: BattleScene): Map function generateTradeOption(alreadyUsedSpecies: PokemonSpecies[], originalBst?: number): PokemonSpecies { let newSpecies: PokemonSpecies | undefined; + let bstCap = 9999; + let bstMin = 0; + if (originalBst) { + bstCap = originalBst + 100; + bstMin = originalBst - 100; + } while (isNullOrUndefined(newSpecies)) { - let bstCap = 9999; - let bstMin = 0; - if (originalBst) { - bstCap = originalBst + 100; - bstMin = originalBst - 100; - } - // Get all non-legendary species that fall within the Bst range requirements let validSpecies = allSpecies .filter(s => { diff --git a/src/test/mystery-encounter/encounters/global-trade-system-encounter.test.ts b/src/test/mystery-encounter/encounters/global-trade-system-encounter.test.ts index 5a99b0450ca..9ccde209827 100644 --- a/src/test/mystery-encounter/encounters/global-trade-system-encounter.test.ts +++ b/src/test/mystery-encounter/encounters/global-trade-system-encounter.test.ts @@ -69,6 +69,20 @@ describe("Global Trade System - Mystery Encounter", () => { expect(GlobalTradeSystemEncounter.options.length).toBe(4); }); + it("should not freeze when generating trade options for extreme BST non-legendaries", async () => { + const extremeBstTeam = [Species.SLAKING, Species.WISHIWASHI, Species.SUNKERN]; + await game.runToMysteryEncounter(MysteryEncounterType.GLOBAL_TRADE_SYSTEM, extremeBstTeam); + + expect(GlobalTradeSystemEncounter.encounterType).toBe(MysteryEncounterType.GLOBAL_TRADE_SYSTEM); + expect(GlobalTradeSystemEncounter.encounterTier).toBe(MysteryEncounterTier.COMMON); + expect(GlobalTradeSystemEncounter.dialogue).toBeDefined(); + expect(GlobalTradeSystemEncounter.dialogue.intro).toStrictEqual([{ text: `${namespace}.intro` }]); + expect(GlobalTradeSystemEncounter.dialogue.encounterOptionsDialogue?.title).toBe(`${namespace}.title`); + expect(GlobalTradeSystemEncounter.dialogue.encounterOptionsDialogue?.description).toBe(`${namespace}.description`); + expect(GlobalTradeSystemEncounter.dialogue.encounterOptionsDialogue?.query).toBe(`${namespace}.query`); + expect(GlobalTradeSystemEncounter.options.length).toBe(4); + }); + it("should not spawn outside of CIVILIZATION_ENCOUNTER_BIOMES", async () => { game.override.mysteryEncounterTier(MysteryEncounterTier.COMMON); game.override.startingBiome(Biome.VOLCANO);