diff --git a/src/battle.ts b/src/battle.ts index 5105418fd9c..07e520d6bc0 100644 --- a/src/battle.ts +++ b/src/battle.ts @@ -8,7 +8,6 @@ import { shiftCharCodes, randSeedItem, randInt, - randSeedFloat, } from "#app/utils/common"; import Trainer, { TrainerVariant } from "./field/trainer"; import type { GameMode } from "./game-mode"; @@ -151,7 +150,7 @@ export default class Battle { randSeedGaussForLevel(value: number): number { let rand = 0; for (let i = value; i > 0; i--) { - rand += randSeedFloat(); + rand += Phaser.Math.RND.realInRange(0, 1); } return rand / value; } diff --git a/src/data/abilities/ability.ts b/src/data/abilities/ability.ts index ea1937579a6..f7c60316ccc 100644 --- a/src/data/abilities/ability.ts +++ b/src/data/abilities/ability.ts @@ -1,5 +1,5 @@ import { HitResult, MoveResult, PlayerPokemon } from "#app/field/pokemon"; -import { BooleanHolder, NumberHolder, toDmgValue, isNullOrUndefined, randSeedItem, randSeedInt, type Constructor, randSeedFloat } from "#app/utils/common"; +import { BooleanHolder, NumberHolder, toDmgValue, isNullOrUndefined, randSeedItem, randSeedInt, type Constructor } from "#app/utils/common"; import { getPokemonNameWithAffix } from "#app/messages"; import { BattlerTagLapseType, GroundedTag } from "#app/data/battler-tags"; import { getNonVolatileStatusEffects, getStatusEffectDescriptor, getStatusEffectHealText } from "#app/data/status-effect"; @@ -4073,7 +4073,7 @@ export class PostTurnRestoreBerryAbAttr extends PostTurnAbAttr { } // Clamp procChance to [0, 1]. Skip if didn't proc (less than pass) - const pass = randSeedFloat(); + const pass = Phaser.Math.RND.realInRange(0, 1); return Phaser.Math.Clamp(this.procChance(pokemon), 0, 1) >= pass; } diff --git a/src/data/moves/move.ts b/src/data/moves/move.ts index 1b0e7c849ac..d27197b6f97 100644 --- a/src/data/moves/move.ts +++ b/src/data/moves/move.ts @@ -29,7 +29,7 @@ import { } from "../status-effect"; import { getTypeDamageMultiplier } from "../type"; import { PokemonType } from "#enums/pokemon-type"; -import { BooleanHolder, NumberHolder, isNullOrUndefined, toDmgValue, randSeedItem, randSeedInt, getEnumValues, toReadableString, type Constructor, randSeedFloat } from "#app/utils/common"; +import { BooleanHolder, NumberHolder, isNullOrUndefined, toDmgValue, randSeedItem, randSeedInt, getEnumValues, toReadableString, type Constructor } from "#app/utils/common"; import { WeatherType } from "#enums/weather-type"; import type { ArenaTrapTag } from "../arena-tag"; import { ArenaTagSide, WeakenMoveTypeTag } from "../arena-tag"; @@ -2549,8 +2549,8 @@ export class StealHeldItemChanceAttr extends MoveEffectAttr { } apply(user: Pokemon, target: Pokemon, move: Move, args: any[]): boolean { - const rand = randSeedFloat(); - if (rand > this.chance) { + const rand = Phaser.Math.RND.realInRange(0, 1); + if (rand >= this.chance) { return false; } diff --git a/src/data/pokemon-species.ts b/src/data/pokemon-species.ts index 7000523a50d..59167ba47f6 100644 --- a/src/data/pokemon-species.ts +++ b/src/data/pokemon-species.ts @@ -8,14 +8,7 @@ import type { AnySound } from "#app/battle-scene"; import { globalScene } from "#app/global-scene"; import type { GameMode } from "#app/game-mode"; import { DexAttr, type StarterMoveset } from "#app/system/game-data"; -import { - isNullOrUndefined, - capitalizeString, - randSeedInt, - randSeedGauss, - randSeedItem, - randSeedFloat, -} from "#app/utils/common"; +import { isNullOrUndefined, capitalizeString, randSeedInt, randSeedGauss, randSeedItem } from "#app/utils/common"; import { uncatchableSpecies } from "#app/data/balance/biomes"; import { speciesEggMoves } from "#app/data/balance/egg-moves"; import { GrowthRate } from "#app/data/exp"; @@ -493,10 +486,10 @@ export abstract class PokemonSpeciesForm { break; case Species.ZACIAN: case Species.ZAMAZENTA: + // biome-ignore lint/suspicious/noFallthroughSwitchClause: Falls through if (formSpriteKey.startsWith("behemoth")) { formSpriteKey = "crowned"; } - // biome-ignore lint/suspicious/no-fallthrough: Falls through default: ret += `-${formSpriteKey}`; break; @@ -757,7 +750,7 @@ export abstract class PokemonSpeciesForm { let paletteColors: Map = new Map(); const originalRandom = Math.random; - Math.random = randSeedFloat; + Math.random = Phaser.Math.RND.frac; globalScene.executeWithSeedOffset( () => { @@ -780,7 +773,6 @@ export default class PokemonSpecies extends PokemonSpeciesForm implements Locali readonly mythical: boolean; readonly species: string; readonly growthRate: GrowthRate; - /** The chance (as a decimal) for this Species to be male, or `null` for genderless species */ readonly malePercent: number | null; readonly genderDiffs: boolean; readonly canChangeForm: boolean; @@ -897,7 +889,7 @@ export default class PokemonSpecies extends PokemonSpeciesForm implements Locali return Gender.GENDERLESS; } - if (randSeedFloat() <= this.malePercent) { + if (Phaser.Math.RND.realInRange(0, 1) <= this.malePercent) { return Gender.MALE; } return Gender.FEMALE; @@ -1146,7 +1138,7 @@ export default class PokemonSpecies extends PokemonSpeciesForm implements Locali } } - if (noEvolutionChance === 1 || randSeedFloat() <= noEvolutionChance) { + if (noEvolutionChance === 1 || Phaser.Math.RND.realInRange(0, 1) < noEvolutionChance) { return this.speciesId; } diff --git a/src/field/pokemon.ts b/src/field/pokemon.ts index de08d7dfac7..c06dae1e760 100644 --- a/src/field/pokemon.ts +++ b/src/field/pokemon.ts @@ -54,7 +54,7 @@ import { getStarterValueFriendshipCap, speciesStarterCosts, } from "#app/data/balance/starters"; -import { NumberHolder, randSeedInt, getIvsFromId, BooleanHolder, randSeedItem, isNullOrUndefined, getEnumValues, toDmgValue, fixedInt, rgbaToInt, rgbHexToRgba, rgbToHsv, deltaRgb, isBetween, type nil, type Constructor, randSeedFloat } from "#app/utils/common"; +import { NumberHolder, randSeedInt, getIvsFromId, BooleanHolder, randSeedItem, isNullOrUndefined, getEnumValues, toDmgValue, fixedInt, rgbaToInt, rgbHexToRgba, rgbToHsv, deltaRgb, isBetween, type nil, type Constructor } from "#app/utils/common"; import type { TypeDamageMultiplier } from "#app/data/type"; import { getTypeDamageMultiplier, getTypeRgb } from "#app/data/type"; import { PokemonType } from "#enums/pokemon-type"; @@ -6047,7 +6047,7 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container { let fusionPaletteColors: Map; const originalRandom = Math.random; - Math.random = () => randSeedFloat(); + Math.random = () => Phaser.Math.RND.realInRange(0, 1); globalScene.executeWithSeedOffset( () => { diff --git a/src/modifier/modifier.ts b/src/modifier/modifier.ts index 514e03a8aa8..4adb46dc47a 100644 --- a/src/modifier/modifier.ts +++ b/src/modifier/modifier.ts @@ -15,7 +15,7 @@ import { PokemonHealPhase } from "#app/phases/pokemon-heal-phase"; import type { VoucherType } from "#app/system/voucher"; import { Command } from "#app/ui/command-ui-handler"; import { addTextObject, TextStyle } from "#app/ui/text"; -import { BooleanHolder, hslToHex, isNullOrUndefined, NumberHolder, randSeedFloat, toDmgValue } from "#app/utils/common"; +import { BooleanHolder, hslToHex, isNullOrUndefined, NumberHolder, toDmgValue } from "#app/utils/common"; import { BattlerTagType } from "#enums/battler-tag-type"; import { BerryType } from "#enums/berry-type"; import type { Moves } from "#enums/moves"; @@ -3367,7 +3367,7 @@ export class ContactHeldItemTransferChanceModifier extends HeldItemTransferModif } getTransferredItemCount(): number { - return randSeedFloat() <= this.chance * this.getStackCount() ? 1 : 0; + return Phaser.Math.RND.realInRange(0, 1) < this.chance * this.getStackCount() ? 1 : 0; } getTransferMessage(pokemon: Pokemon, targetPokemon: Pokemon, item: ModifierType): string { @@ -3645,7 +3645,7 @@ export class EnemyAttackStatusEffectChanceModifier extends EnemyPersistentModifi * @returns `true` if the {@linkcode Pokemon} was affected */ override apply(enemyPokemon: Pokemon): boolean { - if (randSeedFloat() <= this.chance * this.getStackCount()) { + if (Phaser.Math.RND.realInRange(0, 1) < this.chance * this.getStackCount()) { return enemyPokemon.trySetStatus(this.effect, true); } @@ -3685,16 +3685,16 @@ export class EnemyStatusEffectHealChanceModifier extends EnemyPersistentModifier * @returns `true` if the {@linkcode Pokemon} was healed */ override apply(enemyPokemon: Pokemon): boolean { - if (!enemyPokemon.status || randSeedFloat() > this.chance * this.getStackCount()) { - return false; + if (enemyPokemon.status && Phaser.Math.RND.realInRange(0, 1) < this.chance * this.getStackCount()) { + globalScene.queueMessage( + getStatusEffectHealText(enemyPokemon.status.effect, getPokemonNameWithAffix(enemyPokemon)), + ); + enemyPokemon.resetStatus(); + enemyPokemon.updateInfo(); + return true; } - globalScene.queueMessage( - getStatusEffectHealText(enemyPokemon.status.effect, getPokemonNameWithAffix(enemyPokemon)), - ); - enemyPokemon.resetStatus(); - enemyPokemon.updateInfo(); - return true; + return false; } getMaxStackCount(): number { @@ -3773,7 +3773,7 @@ export class EnemyFusionChanceModifier extends EnemyPersistentModifier { * @returns `true` if the {@linkcode EnemyPokemon} is a fusion */ override apply(isFusion: BooleanHolder): boolean { - if (randSeedFloat() > this.chance * this.getStackCount()) { + if (Phaser.Math.RND.realInRange(0, 1) >= this.chance * this.getStackCount()) { return false; } diff --git a/src/utils/common.ts b/src/utils/common.ts index 4b493f637e3..847f8a7ecdb 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -57,8 +57,8 @@ export function randSeedGauss(stdev: number, mean = 0): number { if (!stdev) { return 0; } - const u = 1 - randSeedFloat(); - const v = randSeedFloat(); + const u = 1 - Phaser.Math.RND.realInRange(0, 1); + const v = Phaser.Math.RND.realInRange(0, 1); const z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v); return z * stdev + mean; } @@ -87,9 +87,9 @@ export function randInt(range: number, min = 0): number { } /** - * Generate a random integer using the global seed, or the current battle's seed if called via `Battle.randSeedInt` - * @param range - How large of a range of random numbers to choose from. If {@linkcode range} <= 1, returns {@linkcode min} - * @param min - The minimum integer to pick, default `0` + * Generates a random number using the global seed, or the current battle's seed if called via `Battle.randSeedInt` + * @param range How large of a range of random numbers to choose from. If {@linkcode range} <= 1, returns {@linkcode min} + * @param min The minimum integer to pick, default `0` * @returns A random integer between {@linkcode min} and ({@linkcode min} + {@linkcode range} - 1) */ export function randSeedInt(range: number, min = 0): number { @@ -108,14 +108,6 @@ export function randIntRange(min: number, max: number): number { return randInt(max - min, min); } -/** - * Generate and return a random real number between `0` and `1` using the global seed. - * @returns A random floating-point number between `0` and `1` - */ -export function randSeedFloat(): number { - return Phaser.Math.RND.frac(); -} - export function randItem(items: T[]): T { return items.length === 1 ? items[0] : items[randInt(items.length)]; } @@ -510,19 +502,12 @@ export function capitalizeString(str: string, sep: string, lowerFirstChar = true return null; } -/** - * Report whether a given value is nullish (`null`/`undefined`). - * @param val - The value whose nullishness is being checked - * @returns `true` if `val` is either `null` or `undefined` - */ -export function isNullOrUndefined(val: any): val is null | undefined { - return val === null || val === undefined; +export function isNullOrUndefined(object: any): object is null | undefined { + return object === null || object === undefined; } /** - * Capitalize the first letter of a string. - * @param str - The string whose first letter is being capitalized - * @return The original string with its first letter capitalized + * Capitalizes the first letter of a string */ export function capitalizeFirstLetter(str: string) { return str.charAt(0).toUpperCase() + str.slice(1);