Skip to content

Commit

Permalink
Added a shuffle method based on the current battle seed. Standardized…
Browse files Browse the repository at this point in the history
… other RNG.
  • Loading branch information
frutescens committed Nov 14, 2024
1 parent e40967f commit 1aa5e82
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/battle-scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/data/mystery-encounters/utils/encounter-pokemon-utils.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/field/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1aa5e82

Please sign in to comment.