Added utility types for strong typing IVs and stats

This commit is contained in:
Bertie690 2025-06-14 23:37:24 -04:00
parent 73e0a2905b
commit 887f750253
8 changed files with 97 additions and 20 deletions

View File

@ -1,3 +1,5 @@
import type { IVTuple } from "#app/@types/stat-types";
export interface DexData { export interface DexData {
[key: number]: DexEntry; [key: number]: DexEntry;
} }
@ -9,5 +11,5 @@ export interface DexEntry {
seenCount: number; seenCount: number;
caughtCount: number; caughtCount: number;
hatchedCount: number; hatchedCount: number;
ivs: number[]; ivs: IVTuple;
} }

59
src/@types/stat-types.ts Normal file
View File

@ -0,0 +1,59 @@
import type { Head, Tail } from "#app/@types/utility-types/tuple";
// biome-ignore lint/correctness/noUnusedImports: Type Imports
import type { PermanentStat, BattleStat } from "#enums/stat";
type StatTuple = [
hp: number,
atk: number,
def: number,
spAtk: number,
spDef: number,
spd: number,
acc: number,
eva: number,
];
/** Tuple containing all {@linkcode PermanentStat}s of a Pokemon. */
export type PermanentStatTuple = Head<Head<StatTuple>>;
/** Tuple containing all {@linkcode BattleStat}s of a Pokemon. */
export type BattleStatTuple = Tail<PermanentStatTuple>;
// Since typescript lacks integer range unions, we have to use THIS abomination to strongly type an IV
export type IVType =
| 0
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
| 15
| 16
| 17
| 18
| 19
| 20
| 21
| 22
| 23
| 24
| 25
| 26
| 27
| 28
| 29
| 30
| 31;
type toIvTuple<T extends [...any]> = { [k in keyof T]: IVType };
export type IVTuple = toIvTuple<PermanentStatTuple>;

View File

@ -0,0 +1,5 @@
/** Extract the elements of a tuple type, excluding the first element. */
export type Tail<T extends any[]> = Required<T> extends [any, ...infer Head] ? Head : never;
/** Extract the elements of a tuple type, excluding the last element. */
export type Head<T extends [...any]> = Required<T> extends [...infer Head, any] ? Head : never;

View File

@ -160,12 +160,16 @@ 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";
const DEBUG_RNG = false; const DEBUG_RNG = false;
const OPP_IVS_OVERRIDE_VALIDATED: number[] = ( const OPP_IVS_OVERRIDE_VALIDATED: IVTuple | null =
Array.isArray(Overrides.OPP_IVS_OVERRIDE) ? Overrides.OPP_IVS_OVERRIDE : new Array(6).fill(Overrides.OPP_IVS_OVERRIDE) Overrides.OPP_IVS_OVERRIDE === null
).map(iv => (Number.isNaN(iv) || iv === null || iv > 31 ? -1 : iv)); ? null
: Array.isArray(Overrides.OPP_IVS_OVERRIDE)
? Overrides.OPP_IVS_OVERRIDE
: new Array(6).fill(Overrides.OPP_IVS_OVERRIDE);
export interface PokeballCounts { export interface PokeballCounts {
[pb: string]: number; [pb: string]: number;
@ -970,12 +974,9 @@ export default class BattleScene extends SceneBase {
postProcess(pokemon); postProcess(pokemon);
} }
for (let i = 0; i < pokemon.ivs.length; i++) { if (OPP_IVS_OVERRIDE_VALIDATED) {
if (OPP_IVS_OVERRIDE_VALIDATED[i] > -1) { pokemon.ivs = OPP_IVS_OVERRIDE_VALIDATED;
pokemon.ivs[i] = OPP_IVS_OVERRIDE_VALIDATED[i];
} }
}
pokemon.init(); pokemon.init();
return pokemon; return pokemon;
} }

View File

@ -193,6 +193,7 @@ import { AiType } from "#enums/ai-type";
import type { MoveResult } from "#enums/move-result"; import type { MoveResult } from "#enums/move-result";
import { PokemonMove } from "#app/data/moves/pokemon-move"; import { PokemonMove } from "#app/data/moves/pokemon-move";
import type { AbAttrMap, AbAttrString } from "#app/@types/ability-types"; import type { AbAttrMap, AbAttrString } from "#app/@types/ability-types";
import type { IVTuple, PermanentStatTuple } from "#app/@types/stat-types";
/** Base typeclass for damage parameter methods, used for DRY */ /** Base typeclass for damage parameter methods, used for DRY */
type damageParams = { type damageParams = {
@ -247,8 +248,8 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
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;
@ -317,7 +318,7 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
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,
) { ) {
@ -5475,7 +5476,7 @@ export class PlayerPokemon extends Pokemon {
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,
) { ) {
@ -6088,10 +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: number[] = []; const ivs: IVTuple = Array.from(
while (ivs.length < 6) { {
ivs.push(randSeedIntRange(Math.floor(waveIndex / 10), 31)); length: 6,
} },
() => randSeedIntRange(Math.floor(waveIndex / 10), 31),
);
this.ivs = ivs; this.ivs = ivs;
} }
} }

View File

@ -22,6 +22,7 @@ import { TimeOfDay } from "#enums/time-of-day";
import { TrainerType } from "#enums/trainer-type"; import { TrainerType } from "#enums/trainer-type";
import { VariantTier } from "#enums/variant-tier"; import { VariantTier } from "#enums/variant-tier";
import { WeatherType } from "#enums/weather-type"; import { WeatherType } from "#enums/weather-type";
import type { IVTuple, IVType } from "#app/@types/stat-types";
/** /**
* This comment block exists to prevent IDEs from automatically removing unused imports * This comment block exists to prevent IDEs from automatically removing unused imports
@ -177,7 +178,11 @@ class DefaultOverrides {
readonly OPP_MOVESET_OVERRIDE: MoveId | Array<MoveId> = []; readonly OPP_MOVESET_OVERRIDE: MoveId | Array<MoveId> = [];
readonly OPP_SHINY_OVERRIDE: boolean | null = null; readonly OPP_SHINY_OVERRIDE: boolean | null = null;
readonly OPP_VARIANT_OVERRIDE: Variant | null = null; readonly OPP_VARIANT_OVERRIDE: Variant | null = null;
readonly OPP_IVS_OVERRIDE: number | number[] = []; /**
* An array of IVs to give the opposing pokemon.
* Specifying a single number will use it for all 6 IVs, while `null` will disable the override.
*/
readonly OPP_IVS_OVERRIDE: IVType | IVTuple | null = null;
readonly OPP_FORM_OVERRIDES: Partial<Record<SpeciesId, number>> = {}; readonly OPP_FORM_OVERRIDES: Partial<Record<SpeciesId, number>> = {};
/** /**
* Override to give the enemy Pokemon a given amount of health segments * Override to give the enemy Pokemon a given amount of health segments

View File

@ -14,6 +14,7 @@ import ConfirmUiHandler from "./confirm-ui-handler";
import { StatsContainer } from "./stats-container"; import { StatsContainer } from "./stats-container";
import { TextStyle, addBBCodeTextObject, addTextObject, getTextColor } from "./text"; import { TextStyle, addBBCodeTextObject, addTextObject, getTextColor } from "./text";
import { addWindow } from "./ui-theme"; import { addWindow } from "./ui-theme";
import type { PermanentStatTuple } from "#app/@types/stat-types";
interface LanguageSetting { interface LanguageSetting {
infoContainerTextSize: string; infoContainerTextSize: string;
@ -383,7 +384,7 @@ export default class PokemonInfoContainer extends Phaser.GameObjects.Container {
} }
const starterSpeciesId = pokemon.species.getRootSpeciesId(); const starterSpeciesId = pokemon.species.getRootSpeciesId();
const originalIvs: number[] | null = eggInfo const originalIvs: PermanentStatTuple | null = eggInfo
? dexEntry.caughtAttr ? dexEntry.caughtAttr
? dexEntry.ivs ? dexEntry.ivs
: null : null

View File

@ -3,6 +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";
export type nil = null | undefined; export type nil = null | undefined;
@ -182,7 +183,7 @@ export function getPlayTimeString(totalSeconds: number): string {
* @param id 32-bit number * @param id 32-bit number
* @returns An array of six numbers corresponding to 5-bit chunks from {@linkcode id} * @returns An array of six numbers corresponding to 5-bit chunks from {@linkcode id}
*/ */
export function getIvsFromId(id: number): number[] { export function getIvsFromId(id: number): IVTuple {
return [ return [
(id & 0x3e000000) >>> 25, (id & 0x3e000000) >>> 25,
(id & 0x01f00000) >>> 20, (id & 0x01f00000) >>> 20,