From 1aa5e8293f7cae3985ca1dbd209924e5b0e310d1 Mon Sep 17 00:00:00 2001 From: frutescens Date: Thu, 14 Nov 2024 14:25:23 -0800 Subject: [PATCH] Added a shuffle method based on the current battle seed. Standardized other RNG. --- src/battle-scene.ts | 17 +++++++++++++++++ .../encounters/absolute-avarice-encounter.ts | 2 +- .../utils/encounter-pokemon-utils.ts | 4 ++-- src/field/pokemon.ts | 9 +++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/battle-scene.ts b/src/battle-scene.ts index e659b588208..d4bce3fa9c6 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -1049,6 +1049,23 @@ export default class BattleScene extends SceneBase { return this.currentBattle?.randSeedInt(this, range, min); } + /** + * Shuffles an array based on the current battle's seed. + * @param {array} items a list of items + * @returns a new randomly shuffled array + */ + randBattleSeedShuffle(items: any[]): any[] { + if (items.length <= 1) { + return items; + } + const newArray = items.slice(0); + for (let i = items.length - 1; i > 0; i--) { + const j = this.currentBattle?.randSeedInt(this, i); + [ newArray[i], newArray[j] ] = [ newArray[j], newArray[i] ]; + } + return newArray; + } + reset(clearScene: boolean = false, clearData: boolean = false, reloadI18n: boolean = false): void { if (clearData) { this.gameData = new GameData(this); diff --git a/src/data/mystery-encounters/encounters/absolute-avarice-encounter.ts b/src/data/mystery-encounters/encounters/absolute-avarice-encounter.ts index 9c00148fbac..6efb3b99419 100644 --- a/src/data/mystery-encounters/encounters/absolute-avarice-encounter.ts +++ b/src/data/mystery-encounters/encounters/absolute-avarice-encounter.ts @@ -318,7 +318,7 @@ export const AbsoluteAvariceEncounter: MysteryEncounter = if (returnedBerryCount > 0) { for (let i = 0; i < returnedBerryCount; i++) { // Shuffle remaining berry types and pop - Phaser.Math.RND.shuffle(berryTypesAsArray); + pokemon.randSeedShuffle(berryTypesAsArray); const randBerryType = berryTypesAsArray.pop(); const berryModType = generateModifierType(scene, modifierTypes.BERRY, [ randBerryType ]) as BerryModifierType; diff --git a/src/data/mystery-encounters/utils/encounter-pokemon-utils.ts b/src/data/mystery-encounters/utils/encounter-pokemon-utils.ts index fc85754bdde..148627efebb 100644 --- a/src/data/mystery-encounters/utils/encounter-pokemon-utils.ts +++ b/src/data/mystery-encounters/utils/encounter-pokemon-utils.ts @@ -1,6 +1,6 @@ import BattleScene from "#app/battle-scene"; import i18next from "i18next"; -import { isNullOrUndefined, randSeedInt } from "#app/utils"; +import { isNullOrUndefined, randSeedInt, randSeedShuffle } from "#app/utils"; import { PokemonHeldItemModifier } from "#app/modifier/modifier"; import Pokemon, { EnemyPokemon, PlayerPokemon } from "#app/field/pokemon"; import { doPokeballBounceAnim, getPokeballAtlasKey, getPokeballCatchMultiplier, getPokeballTintColor } from "#app/data/pokeball"; @@ -241,7 +241,7 @@ export function getRandomSpeciesByStarterTier(starterTiers: number | [number, nu if (tryFilterStarterTiers.length > 0) { const index = randSeedInt(tryFilterStarterTiers.length); - return Phaser.Math.RND.shuffle(tryFilterStarterTiers)[index][0].speciesId; + return randSeedShuffle(tryFilterStarterTiers)[index][0].speciesId; } return Species.BULBASAUR; diff --git a/src/field/pokemon.ts b/src/field/pokemon.ts index 5d912f7d6e6..b0580fafc22 100644 --- a/src/field/pokemon.ts +++ b/src/field/pokemon.ts @@ -4044,6 +4044,15 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container { return this.randSeedInt((max - min) + 1, min); } + /** + * Shuffles an array using the current battle's seed, or the global seed if `this.scene.currentBattle` is falsy. + * @param {array} items an array of items + * @returns {array} a new shuffled array of items + */ + randSeedShuffle(items: any[]): any[] { + return this.scene.currentBattle ? this.scene.randBattleSeedShuffle(items) : Utils.randSeedShuffle(items); + } + /** * Causes a Pokemon to leave the field (such as in preparation for a switch out/escape). * @param clearEffects Indicates if effects should be cleared (true) or passed