Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Fixing up other misc. RNG calls #4870

Draft
wants to merge 4 commits into
base: beta
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions src/data/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2363,7 +2363,7 @@ export class StealHeldItemChanceAttr extends MoveEffectAttr {
if (move.hitsSubstitute(user, target)) {
return resolve(false);
}
const rand = Phaser.Math.RND.realInRange(0, 1);
const rand = user.randSeedInt(100);
if (rand >= this.chance) {
return resolve(false);
}
Expand Down Expand Up @@ -8208,7 +8208,7 @@ export function initMoves() {
.attr(MultiHitPowerIncrementAttr, 3)
.checkAllHits(),
new AttackMove(Moves.THIEF, Type.DARK, MoveCategory.PHYSICAL, 60, 100, 25, -1, 0, 2)
.attr(StealHeldItemChanceAttr, 0.3),
.attr(StealHeldItemChanceAttr, 30),
new StatusMove(Moves.SPIDER_WEB, Type.BUG, -1, 10, -1, 0, 2)
.attr(AddBattlerTagAttr, BattlerTagType.TRAPPED, false, true, 1),
new StatusMove(Moves.MIND_READER, Type.NORMAL, -1, 5, -1, 0, 2)
Expand Down Expand Up @@ -8747,7 +8747,7 @@ export function initMoves() {
.attr(HighCritAttr)
.attr(StatusEffectAttr, StatusEffect.POISON),
new AttackMove(Moves.COVET, Type.NORMAL, MoveCategory.PHYSICAL, 60, 100, 25, -1, 0, 3)
.attr(StealHeldItemChanceAttr, 0.3),
.attr(StealHeldItemChanceAttr, 30),
new AttackMove(Moves.VOLT_TACKLE, Type.ELECTRIC, MoveCategory.PHYSICAL, 120, 100, 15, 10, 0, 3)
.attr(RecoilAttr, false, 0.33)
.attr(StatusEffectAttr, StatusEffect.PARALYSIS)
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
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
29 changes: 20 additions & 9 deletions src/modifier/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3538,7 +3538,11 @@ export class EnemyTurnHealModifier extends EnemyPersistentModifier {
return 10;
}
}

/**
* Modifer Class for Burn, Poison, and Paralysis Tokens
* Burn/Poison Tokens -> 2.5% chance of status on hit
* Paralysis Token -> 5% chance of status on hit
*/
export class EnemyAttackStatusEffectChanceModifier extends EnemyPersistentModifier {
public effect: StatusEffect;
public chance: number;
Expand All @@ -3548,19 +3552,23 @@ export class EnemyAttackStatusEffectChanceModifier extends EnemyPersistentModifi

this.effect = effect;
//Hardcode temporarily
this.chance = .025 * ((this.effect === StatusEffect.BURN || this.effect === StatusEffect.POISON) ? 2 : 1);
if (this.effect === StatusEffect.BURN || this.effect === StatusEffect.POISON) {
this.chance = 2.5;
} else {
this.chance = 5;
}
}

match(modifier: Modifier): boolean {
return modifier instanceof EnemyAttackStatusEffectChanceModifier && modifier.effect === this.effect;
}

clone(): EnemyAttackStatusEffectChanceModifier {
return new EnemyAttackStatusEffectChanceModifier(this.type, this.effect, this.chance * 100, this.stackCount);
return new EnemyAttackStatusEffectChanceModifier(this.type, this.effect, this.chance, this.stackCount);
}

getArgs(): any[] {
return [ this.effect, this.chance * 100 ];
return [ this.effect, this.chance ];
}

/**
Expand All @@ -3569,7 +3577,7 @@ export class EnemyAttackStatusEffectChanceModifier extends EnemyPersistentModifi
* @returns `true` if the {@linkcode Pokemon} was affected
*/
override apply(enemyPokemon: Pokemon): boolean {
if (Phaser.Math.RND.realInRange(0, 1) < (this.chance * this.getStackCount())) {
if (enemyPokemon.randSeedInt(100) < (this.chance * this.getStackCount())) {
return enemyPokemon.trySetStatus(this.effect, true);
}

Expand All @@ -3581,26 +3589,29 @@ export class EnemyAttackStatusEffectChanceModifier extends EnemyPersistentModifi
}
}

/**
* Modifier Class for Full Heal Token
*/
export class EnemyStatusEffectHealChanceModifier extends EnemyPersistentModifier {
public chance: number;

constructor(type: ModifierType, chancePercent: number, stackCount?: number) {
super(type, stackCount);

//Hardcode temporarily
this.chance = .025;
this.chance = 2.5;
}

match(modifier: Modifier): boolean {
return modifier instanceof EnemyStatusEffectHealChanceModifier;
}

clone(): EnemyStatusEffectHealChanceModifier {
return new EnemyStatusEffectHealChanceModifier(this.type, this.chance * 100, this.stackCount);
return new EnemyStatusEffectHealChanceModifier(this.type, this.chance, this.stackCount);
}

getArgs(): any[] {
return [ this.chance * 100 ];
return [ this.chance ];
}

/**
Expand All @@ -3609,7 +3620,7 @@ export class EnemyStatusEffectHealChanceModifier extends EnemyPersistentModifier
* @returns `true` if the {@linkcode Pokemon} was healed
*/
override apply(enemyPokemon: Pokemon): boolean {
if (enemyPokemon.status && Phaser.Math.RND.realInRange(0, 1) < (this.chance * this.getStackCount())) {
if (enemyPokemon.status && enemyPokemon.randSeedInt(100) < (this.chance * this.getStackCount())) {
enemyPokemon.scene.queueMessage(getStatusEffectHealText(enemyPokemon.status.effect, getPokemonNameWithAffix(enemyPokemon)));
enemyPokemon.resetStatus();
enemyPokemon.updateInfo();
Expand Down
2 changes: 1 addition & 1 deletion src/test/abilities/unburden.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe("Abilities - Unburden", () => {
{ name: "BERRY", type: BerryType.LUM, count: 1 },
]);
// For the various tests that use Thief, give it a 100% steal rate
vi.spyOn(allMoves[Moves.THIEF], "attrs", "get").mockReturnValue([ new StealHeldItemChanceAttr(1.0) ]);
vi.spyOn(allMoves[Moves.THIEF], "attrs", "get").mockReturnValue([ new StealHeldItemChanceAttr(100) ]);
});

it("should activate when a berry is eaten", async () => {
Expand Down
Loading