IV safety part 0.5

This commit is contained in:
Bertie690 2025-06-15 09:45:28 -04:00
parent 887f750253
commit cce6acec6f
12 changed files with 59 additions and 31 deletions

View File

@ -19,7 +19,7 @@ export type PermanentStatTuple = Head<Head<StatTuple>>;
/** Tuple containing all {@linkcode BattleStat}s of a Pokemon. */ /** Tuple containing all {@linkcode BattleStat}s of a Pokemon. */
export type BattleStatTuple = Tail<PermanentStatTuple>; export type BattleStatTuple = Tail<PermanentStatTuple>;
// Since typescript lacks integer range unions, we have to use THIS abomination to strongly type an IV /** Integer literal union containing all numbers from 0-31 inclusive; used to strongly type Pokemon IVs. */
export type IVType = export type IVType =
| 0 | 0
| 1 | 1
@ -56,4 +56,5 @@ export type IVType =
type toIvTuple<T extends [...any]> = { [k in keyof T]: IVType }; type toIvTuple<T extends [...any]> = { [k in keyof T]: IVType };
/** A 6-length tuple of integers in the range [0-31]; used to strongly type Pokemon IVs. */
export type IVTuple = toIvTuple<PermanentStatTuple>; export type IVTuple = toIvTuple<PermanentStatTuple>;

View File

@ -0,0 +1,9 @@
export {};
declare global {
// Array global augments to allow semi-easy working with tuples...???
interface Array<T> {
map<U>(callbackfn: (value: T, index: number, array: this) => U, thisArg?: any): { [K in keyof this]: U };
slice(start: 0): { [K in keyof this]: T };
}
}

View File

@ -160,7 +160,7 @@ import { timedEventManager } from "./global-event-manager";
import { starterColors } from "./global-vars/starter-colors"; import { starterColors } from "./global-vars/starter-colors";
import { startingWave } from "./starting-wave"; import { startingWave } from "./starting-wave";
import { PhaseManager } from "./phase-manager"; import { PhaseManager } from "./phase-manager";
import type { IVTuple } from "#app/@types/stat-types"; import type { IVTuple, IVType } from "#app/@types/stat-types";
const DEBUG_RNG = false; const DEBUG_RNG = false;
@ -169,7 +169,7 @@ const OPP_IVS_OVERRIDE_VALIDATED: IVTuple | null =
? null ? null
: Array.isArray(Overrides.OPP_IVS_OVERRIDE) : Array.isArray(Overrides.OPP_IVS_OVERRIDE)
? Overrides.OPP_IVS_OVERRIDE ? Overrides.OPP_IVS_OVERRIDE
: new Array(6).fill(Overrides.OPP_IVS_OVERRIDE); : (new Array(6).fill(Overrides.OPP_IVS_OVERRIDE) as IVTuple);
export interface PokeballCounts { export interface PokeballCounts {
[pb: string]: number; [pb: string]: number;
@ -910,7 +910,7 @@ export default class BattleScene extends SceneBase {
gender?: Gender, gender?: Gender,
shiny?: boolean, shiny?: boolean,
variant?: Variant, variant?: Variant,
ivs?: number[], ivs?: IVTuple,
nature?: Nature, nature?: Nature,
dataSource?: Pokemon | PokemonData, dataSource?: Pokemon | PokemonData,
postProcess?: (playerPokemon: PlayerPokemon) => void, postProcess?: (playerPokemon: PlayerPokemon) => void,
@ -958,23 +958,20 @@ export default class BattleScene extends SceneBase {
} }
if (boss && !dataSource) { if (boss && !dataSource) {
// Generate a 2nd higher roll and linearly interpolate between the two.
const secondaryIvs = getIvsFromId(randSeedInt(4294967296)); const secondaryIvs = getIvsFromId(randSeedInt(4294967296));
for (let s = 0; s < pokemon.ivs.length; s++) { pokemon.ivs = pokemon.ivs.map(
pokemon.ivs[s] = Math.round( (iv, i) =>
Phaser.Math.Linear( Math.round(Phaser.Math.Linear(Math.min(iv, secondaryIvs[i]), Math.max(iv, secondaryIvs[i]), 0.75)) as IVType, // ts doesn't know the number will be between 0-31
Math.min(pokemon.ivs[s], secondaryIvs[s]),
Math.max(pokemon.ivs[s], secondaryIvs[s]),
0.75,
),
); );
} }
}
if (postProcess) { if (postProcess) {
postProcess(pokemon); postProcess(pokemon);
} }
if (OPP_IVS_OVERRIDE_VALIDATED) { if (OPP_IVS_OVERRIDE_VALIDATED !== null) {
pokemon.ivs = OPP_IVS_OVERRIDE_VALIDATED; pokemon.ivs = OPP_IVS_OVERRIDE_VALIDATED;
} }
pokemon.init(); pokemon.init();
@ -2084,7 +2081,7 @@ export default class BattleScene extends SceneBase {
let scoreIncrease = let scoreIncrease =
enemy.getSpeciesForm().getBaseExp() * enemy.getSpeciesForm().getBaseExp() *
(enemy.level / this.getMaxExpLevel()) * (enemy.level / this.getMaxExpLevel()) *
((enemy.ivs.reduce((iv: number, total: number) => (total += iv), 0) / 93) * 0.2 + 0.8); ((enemy.ivs.reduce((total, iv) => total + iv, 0) / 93) * 0.2 + 0.8);
this.findModifiers(m => m instanceof PokemonHeldItemModifier && m.pokemonId === enemy.id, false).map( this.findModifiers(m => m instanceof PokemonHeldItemModifier && m.pokemonId === enemy.id, false).map(
m => (scoreIncrease *= (m as PokemonHeldItemModifier).getScoreMultiplier()), m => (scoreIncrease *= (m as PokemonHeldItemModifier).getScoreMultiplier()),
); );

View File

@ -38,6 +38,7 @@ import {
HATCH_WAVES_LEGENDARY_EGG, HATCH_WAVES_LEGENDARY_EGG,
} from "#app/data/balance/rates"; } from "#app/data/balance/rates";
import { speciesEggTiers } from "#app/data/balance/species-egg-tiers"; import { speciesEggTiers } from "#app/data/balance/species-egg-tiers";
import type { IVType } from "#app/@types/stat-types";
export const EGG_SEED = 1073741824; export const EGG_SEED = 1073741824;
@ -270,9 +271,9 @@ export class Egg {
const secondaryIvs = getIvsFromId(randSeedInt(4294967295)); const secondaryIvs = getIvsFromId(randSeedInt(4294967295));
for (let s = 0; s < ret.ivs.length; s++) { ret.ivs = ret.ivs.map(
ret.ivs[s] = Math.max(ret.ivs[s], secondaryIvs[s]); (iv, i) => Math.max(iv, secondaryIvs[i]) as IVType, // ts doesn't know the number will be between 0-31
} );
}; };
ret = ret!; // Tell TS compiler it's defined now ret = ret!; // Tell TS compiler it's defined now

View File

@ -511,7 +511,7 @@ async function postProcessTransformedPokemon(
const hiddenAbilityChance = new NumberHolder(256); const hiddenAbilityChance = new NumberHolder(256);
globalScene.applyModifiers(HiddenAbilityRateBoosterModifier, true, hiddenAbilityChance); globalScene.applyModifiers(HiddenAbilityRateBoosterModifier, true, hiddenAbilityChance);
const hasHiddenAbility = !randSeedInt(hiddenAbilityChance.value); const hasHiddenAbility = randSeedInt(hiddenAbilityChance.value) === 0;
if (hasHiddenAbility) { if (hasHiddenAbility) {
newPokemon.abilityIndex = hiddenIndex; newPokemon.abilityIndex = hiddenIndex;

View File

@ -54,6 +54,7 @@ import { PokemonType } from "#enums/pokemon-type";
import { getNatureName } from "#app/data/nature"; import { getNatureName } from "#app/data/nature";
import { getPokemonNameWithAffix } from "#app/messages"; import { getPokemonNameWithAffix } from "#app/messages";
import { timedEventManager } from "#app/global-event-manager"; import { timedEventManager } from "#app/global-event-manager";
import type { IVTuple } from "#app/@types/stat-types";
/** /**
* Animates exclamation sprite over trainer's head at start of encounter * Animates exclamation sprite over trainer's head at start of encounter
@ -96,7 +97,7 @@ export interface EnemyPokemonConfig {
passive?: boolean; passive?: boolean;
moveSet?: MoveId[]; moveSet?: MoveId[];
nature?: Nature; nature?: Nature;
ivs?: [number, number, number, number, number, number]; ivs?: IVTuple;
shiny?: boolean; shiny?: boolean;
/** Is only checked if Pokemon is shiny */ /** Is only checked if Pokemon is shiny */
variant?: Variant; variant?: Variant;

View File

@ -6089,12 +6089,12 @@ export class EnemyPokemon extends Pokemon {
if (this.hasTrainer() && globalScene.currentBattle) { if (this.hasTrainer() && globalScene.currentBattle) {
const { waveIndex } = globalScene.currentBattle; const { waveIndex } = globalScene.currentBattle;
const ivs: IVTuple = Array.from( const ivs = Array.from(
{ {
length: 6, length: 6,
}, },
() => randSeedIntRange(Math.floor(waveIndex / 10), 31), () => randSeedIntRange(Math.floor(waveIndex / 10), 31),
); ) as IVTuple;
this.ivs = ivs; this.ivs = ivs;
} }
} }

View File

@ -121,7 +121,8 @@ export class EncounterPhase extends BattlePhase {
!!globalScene.getEncounterBossSegments(battle.waveIndex, level, enemySpecies), !!globalScene.getEncounterBossSegments(battle.waveIndex, level, enemySpecies),
); );
if (globalScene.currentBattle.battleSpec === BattleSpec.FINAL_BOSS) { if (globalScene.currentBattle.battleSpec === BattleSpec.FINAL_BOSS) {
battle.enemyParty[e].ivs = new Array(6).fill(31); // max IVs for eternatus
battle.enemyParty[e].ivs = [31, 31, 31, 31, 31, 31];
} }
globalScene globalScene
.getPlayerParty() .getPlayerParty()

View File

@ -68,6 +68,7 @@ import { DexAttr } from "#enums/dex-attr";
import { AbilityAttr } from "#enums/ability-attr"; import { AbilityAttr } from "#enums/ability-attr";
import { defaultStarterSpecies, saveKey } from "#app/constants"; import { defaultStarterSpecies, saveKey } from "#app/constants";
import { encrypt, decrypt } from "#app/utils/data"; import { encrypt, decrypt } from "#app/utils/data";
import type { IVTuple } from "#app/@types/stat-types";
function getDataTypeKey(dataType: GameDataType, slotId = 0): string { function getDataTypeKey(dataType: GameDataType, slotId = 0): string {
switch (dataType) { switch (dataType) {
@ -1945,7 +1946,13 @@ export class GameData {
_unlockSpeciesNature(species.speciesId); _unlockSpeciesNature(species.speciesId);
} }
updateSpeciesDexIvs(speciesId: SpeciesId, ivs: number[]): void { /**
* When a pokemon is caught or added, maximize its lowest pre-evolution's {@linkcode DexData}
* with the IVs of the newly caught pokemon.
* @param speciesId - The {@linkcode SpeciesId} to update dex data for.
* @param ivs - The {@linkcode IVTuple | IVs} of the caught pokemon.
*/
updateSpeciesDexIvs(speciesId: SpeciesId, ivs: IVTuple): void {
let dexEntry: DexEntry; let dexEntry: DexEntry;
do { do {
dexEntry = globalScene.gameData.dexData[speciesId]; dexEntry = globalScene.gameData.dexData[speciesId];
@ -1955,7 +1962,7 @@ export class GameData {
dexIvs[i] = ivs[i]; dexIvs[i] = ivs[i];
} }
} }
if (dexIvs.filter(iv => iv === 31).length === 6) { if (dexIvs.every(iv => iv === 31)) {
globalScene.validateAchv(achvs.PERFECT_IVS); globalScene.validateAchv(achvs.PERFECT_IVS);
} }
} while (pokemonPrevolutions.hasOwnProperty(speciesId) && (speciesId = pokemonPrevolutions[speciesId])); } while (pokemonPrevolutions.hasOwnProperty(speciesId) && (speciesId = pokemonPrevolutions[speciesId]));

View File

@ -14,6 +14,7 @@ import type { MoveId } from "#enums/move-id";
import type { SpeciesId } from "#enums/species-id"; import type { SpeciesId } from "#enums/species-id";
import { CustomPokemonData } from "#app/data/custom-pokemon-data"; import { CustomPokemonData } from "#app/data/custom-pokemon-data";
import type { PokemonType } from "#enums/pokemon-type"; import type { PokemonType } from "#enums/pokemon-type";
import type { IVTuple, PermanentStatTuple } from "#app/@types/stat-types";
export default class PokemonData { export default class PokemonData {
public id: number; public id: number;
@ -31,8 +32,8 @@ export default class PokemonData {
public levelExp: number; public levelExp: number;
public gender: Gender; public gender: Gender;
public hp: number; public hp: number;
public stats: number[]; public stats: PermanentStatTuple;
public ivs: number[]; public ivs: IVTuple;
public nature: Nature; public nature: Nature;
public moveset: PokemonMove[]; public moveset: PokemonMove[];
public status: Status | null; public status: Status | null;

View File

@ -3,7 +3,7 @@ import { MoveId } from "#enums/move-id";
import i18next from "i18next"; import i18next from "i18next";
import { pokerogueApi } from "#app/plugins/api/pokerogue-api"; import { pokerogueApi } from "#app/plugins/api/pokerogue-api";
import type { Variant } from "#app/sprites/variant"; import type { Variant } from "#app/sprites/variant";
import type { IVTuple } from "#app/@types/stat-types"; import type { IVTuple, IVType } from "#app/@types/stat-types";
export type nil = null | undefined; export type nil = null | undefined;
@ -88,6 +88,8 @@ export function randInt(range: number, min = 0): number {
return Math.floor(Math.random() * range) + min; return Math.floor(Math.random() * range) + min;
} }
export function randSeedInt(range: 31, min?: IVType): IVType;
export function randSeedInt(range: number, min?: number): number;
/** /**
* Generate a random integer using the global seed, or the current battle's seed if called via `Battle.randSeedInt` * 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 range - How large of a range of random numbers to choose from. If {@linkcode range} <= 1, returns {@linkcode min}
@ -191,7 +193,7 @@ export function getIvsFromId(id: number): IVTuple {
(id & 0x00007c00) >>> 10, (id & 0x00007c00) >>> 10,
(id & 0x000003e0) >>> 5, (id & 0x000003e0) >>> 5,
id & 0x0000001f, id & 0x0000001f,
]; ] as IVTuple; // TS doesn't know the numbers are between 0-31
} }
export function formatLargeNumber(count: number, threshold: number): string { export function formatLargeNumber(count: number, threshold: number): string {

View File

@ -11,6 +11,7 @@ import { SpeciesId } from "#enums/species-id";
import GameManager from "#test/testUtils/gameManager"; import GameManager from "#test/testUtils/gameManager";
import Phaser from "phaser"; import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { PermanentStatTuple } from "#app/@types/stat-types";
describe("Moves - Fusion Flare and Fusion Bolt", () => { describe("Moves - Fusion Flare and Fusion Bolt", () => {
let phaserGame: Phaser.Game; let phaserGame: Phaser.Game;
@ -156,6 +157,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
expect(fusionFlare.calculateBattlePower).toHaveLastReturnedWith(200); expect(fusionFlare.calculateBattlePower).toHaveLastReturnedWith(200);
}, 20000); }, 20000);
// TODO: Clean these tests up
it("FUSION_FLARE and FUSION_BOLT alternating throughout turn should double power of subsequent moves", async () => { it("FUSION_FLARE and FUSION_BOLT alternating throughout turn should double power of subsequent moves", async () => {
game.override.enemyMoveset(fusionFlare.id); game.override.enemyMoveset(fusionFlare.id);
await game.classicMode.startBattle([SpeciesId.ZEKROM, SpeciesId.ZEKROM]); await game.classicMode.startBattle([SpeciesId.ZEKROM, SpeciesId.ZEKROM]);
@ -168,7 +170,10 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
game.scene.clearEnemyModifiers(); game.scene.clearEnemyModifiers();
// Mock stats by replacing entries in copy with desired values for specific stats // Mock stats by replacing entries in copy with desired values for specific stats
const stats = { const stats: {
enemy: [PermanentStatTuple, PermanentStatTuple];
player: [PermanentStatTuple, PermanentStatTuple];
} = {
enemy: [[...enemyParty[0].stats], [...enemyParty[1].stats]], enemy: [[...enemyParty[0].stats], [...enemyParty[1].stats]],
player: [[...party[0].stats], [...party[1].stats]], player: [[...party[0].stats], [...party[1].stats]],
}; };
@ -222,7 +227,10 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
game.scene.clearEnemyModifiers(); game.scene.clearEnemyModifiers();
// Mock stats by replacing entries in copy with desired values for specific stats // Mock stats by replacing entries in copy with desired values for specific stats
const stats = { const stats: {
enemy: [PermanentStatTuple, PermanentStatTuple];
player: [PermanentStatTuple, PermanentStatTuple];
} = {
enemy: [[...enemyParty[0].stats], [...enemyParty[1].stats]], enemy: [[...enemyParty[0].stats], [...enemyParty[1].stats]],
player: [[...party[0].stats], [...party[1].stats]], player: [[...party[0].stats], [...party[1].stats]],
}; };