Convert uses of StatusEffect[] to Uint8Array

This commit is contained in:
Sirz Benjie 2025-09-22 10:39:51 -05:00
parent fd4c037959
commit ccaef59660
No known key found for this signature in database
GPG Key ID: 4A524B4D196C759E
6 changed files with 42 additions and 31 deletions

View File

@ -64,6 +64,7 @@ import type {
} from "#types/ability-types";
import type { Localizable } from "#types/locales";
import type { Closed, Exact } from "#types/type-helpers";
import type { GenericUint8Array, ReadonlyGenericInt8Array, ReadonlyGenericUint8Array } from "#types/typed-arrays";
import { coerceArray } from "#utils/array";
import type { Constructor } from "#utils/common";
import { BooleanHolder, NumberHolder, randSeedFloat, randSeedInt, randSeedItem, toDmgValue } from "#utils/common";
@ -1210,13 +1211,13 @@ export class PostDefendTerrainChangeAbAttr extends PostDefendAbAttr {
export class PostDefendContactApplyStatusEffectAbAttr extends PostDefendAbAttr {
private chance: number;
private effects: StatusEffect[];
private readonly effects: ReadonlyGenericUint8Array<StatusEffect>;
constructor(chance: number, ...effects: StatusEffect[]) {
super(true);
this.chance = chance;
this.effects = effects;
this.effects = new Uint8Array(effects);
}
override canApply({ pokemon, move, opponent: attacker }: PostMoveInteractionAbAttrParams): boolean {
@ -2180,14 +2181,14 @@ export class PostAttackStealHeldItemAbAttr extends PostAttackAbAttr {
export class PostAttackApplyStatusEffectAbAttr extends PostAttackAbAttr {
private contactRequired: boolean;
private chance: number;
private effects: StatusEffect[];
private readonly effects: ReadonlyGenericUint8Array<StatusEffect>;
constructor(contactRequired: boolean, chance: number, ...effects: StatusEffect[]) {
super();
this.contactRequired = contactRequired;
this.chance = chance;
this.effects = effects;
this.effects = new Uint8Array(effects);
}
override canApply(params: PostMoveInteractionAbAttrParams): boolean {
@ -2940,7 +2941,7 @@ export class PostSummonTerrainChangeAbAttr extends PostSummonAbAttr {
* Heals a status effect if the Pokemon is afflicted with it upon switch in (or gain)
*/
export class PostSummonHealStatusAbAttr extends PostSummonRemoveEffectAbAttr {
private immuneEffects: StatusEffect[];
private readonly immuneEffects: ReadonlyGenericUint8Array<StatusEffect>;
private statusHealed: StatusEffect;
/**
@ -2948,12 +2949,12 @@ export class PostSummonHealStatusAbAttr extends PostSummonRemoveEffectAbAttr {
*/
constructor(...immuneEffects: StatusEffect[]) {
super();
this.immuneEffects = immuneEffects;
this.immuneEffects = new Uint8Array(immuneEffects);
}
public override canApply({ pokemon }: AbAttrBaseParams): boolean {
const status = pokemon.status?.effect;
return status != null && (this.immuneEffects.length === 0 || this.immuneEffects.includes(status));
return status != null && this.immuneEffects.includes(status);
}
public override apply({ pokemon }: AbAttrBaseParams): void {
@ -3049,7 +3050,7 @@ export class PostSummonCopyAbilityAbAttr extends PostSummonAbAttr {
* Removes supplied status effects from the user's field.
*/
export class PostSummonUserFieldRemoveStatusEffectAbAttr extends PostSummonAbAttr {
private statusEffect: StatusEffect[];
private readonly statusEffect: ReadonlyGenericUint8Array<StatusEffect>;
/**
* @param statusEffect - The status effects to be removed from the user's field.
@ -3057,7 +3058,7 @@ export class PostSummonUserFieldRemoveStatusEffectAbAttr extends PostSummonAbAtt
constructor(...statusEffect: StatusEffect[]) {
super(false);
this.statusEffect = statusEffect;
this.statusEffect = new Uint8Array(statusEffect);
}
override canApply({ pokemon }: AbAttrBaseParams): boolean {
@ -3637,7 +3638,7 @@ export class PreSetStatusAbAttr extends AbAttr {
* Provides immunity to status effects to specified targets.
*/
export class PreSetStatusEffectImmunityAbAttr extends PreSetStatusAbAttr {
protected immuneEffects: StatusEffect[];
protected readonly immuneEffects: ReadonlyGenericUint8Array<StatusEffect>;
/**
* @param immuneEffects - An array of {@linkcode StatusEffect}s to prevent application.
@ -3646,7 +3647,7 @@ export class PreSetStatusEffectImmunityAbAttr extends PreSetStatusAbAttr {
constructor(...immuneEffects: StatusEffect[]) {
super();
this.immuneEffects = immuneEffects;
this.immuneEffects = new Uint8Array(immuneEffects);
}
override canApply({ effect, cancelled }: PreSetStatusAbAttrParams): boolean {
@ -3705,7 +3706,7 @@ export interface UserFieldStatusEffectImmunityAbAttrParams extends AbAttrBasePar
*/
export class UserFieldStatusEffectImmunityAbAttr extends CancelInteractionAbAttr {
private declare readonly _: never;
protected immuneEffects: StatusEffect[];
protected readonly immuneEffects: ReadonlyGenericUint8Array<StatusEffect>;
/**
* @param immuneEffects - An array of {@linkcode StatusEffect}s to prevent application.
@ -3714,7 +3715,7 @@ export class UserFieldStatusEffectImmunityAbAttr extends CancelInteractionAbAttr
constructor(...immuneEffects: StatusEffect[]) {
super();
this.immuneEffects = immuneEffects;
this.immuneEffects = new Uint8Array(immuneEffects);
}
override canApply({ effect, cancelled }: UserFieldStatusEffectImmunityAbAttrParams): boolean {
@ -3998,7 +3999,7 @@ export class BlockNonDirectDamageAbAttr extends CancelInteractionAbAttr {
* This attribute will block any status damage that you put in the parameter.
*/
export class BlockStatusDamageAbAttr extends CancelInteractionAbAttr {
private effects: StatusEffect[];
private readonly effects: ReadonlyGenericUint8Array<StatusEffect>;
/**
* @param effects - The status effect(s) that will be blocked from damaging the ability pokemon
@ -4006,7 +4007,7 @@ export class BlockStatusDamageAbAttr extends CancelInteractionAbAttr {
constructor(...effects: StatusEffect[]) {
super(false);
this.effects = effects;
this.effects = new Uint8Array(effects);
}
override canApply({ pokemon, cancelled }: AbAttrParamsWithCancel): boolean {
@ -4539,7 +4540,7 @@ export class PostTurnAbAttr extends AbAttr {
* @sealed
*/
export class PostTurnStatusHealAbAttr extends PostTurnAbAttr {
private effects: StatusEffect[];
private readonly effects: GenericUint8Array<StatusEffect>;
/**
* @param effects - The status effect(s) that will qualify healing the ability pokemon
@ -4547,7 +4548,7 @@ export class PostTurnStatusHealAbAttr extends PostTurnAbAttr {
constructor(...effects: StatusEffect[]) {
super(false);
this.effects = effects;
this.effects = new Uint8Array(effects);
}
override canApply({ pokemon }: AbAttrBaseParams): boolean {
@ -5808,14 +5809,14 @@ export interface IgnoreTypeStatusEffectImmunityAbAttrParams extends AbAttrParams
* @sealed
*/
export class IgnoreTypeStatusEffectImmunityAbAttr extends AbAttr {
private statusEffect: StatusEffect[];
private defenderType: PokemonType[];
private readonly statusEffect: ReadonlyGenericUint8Array<StatusEffect>;
private readonly defenderType: ReadonlyGenericInt8Array<PokemonType>;
constructor(statusEffect: StatusEffect[], defenderType: PokemonType[]) {
super(false);
this.statusEffect = statusEffect;
this.defenderType = defenderType;
this.statusEffect = new Uint8Array(statusEffect);
this.defenderType = new Int8Array(defenderType);
}
override canApply({ statusEffect, defenderType, cancelled }: IgnoreTypeStatusEffectImmunityAbAttrParams): boolean {

View File

@ -95,6 +95,7 @@ import i18next from "i18next";
import { applyChallenges } from "#utils/challenge-utils";
import { MovePhaseTimingModifier } from "#enums/move-phase-timing-modifier";
import type { AbstractConstructor } from "#types/type-helpers";
import type { ReadonlyGenericUint8Array } from "#types/typed-arrays";
/**
* A function used to conditionally determine execution of a given {@linkcode MoveAttr}.
@ -2595,11 +2596,11 @@ export class StatusEffectAttr extends MoveEffectAttr {
* Used for {@linkcode Moves.TRI_ATTACK} and {@linkcode Moves.DIRE_CLAW}.
*/
export class MultiStatusEffectAttr extends StatusEffectAttr {
public effects: StatusEffect[];
public readonly effects: ReadonlyGenericUint8Array<StatusEffect>;
constructor(effects: StatusEffect[], selfTarget?: boolean) {
super(effects[0], selfTarget);
this.effects = effects;
this.effects = new Uint8Array(effects);
}
apply(user: Pokemon, target: Pokemon, move: Move, args: any[]): boolean {
@ -2925,7 +2926,7 @@ export class StealEatBerryAttr extends EatBerryAttr {
*/
export class HealStatusEffectAttr extends MoveEffectAttr {
/** List of Status Effects to cure */
private effects: StatusEffect[];
private readonly effects: ReadonlyGenericUint8Array<StatusEffect>;
/**
* @param selfTarget - Whether this move targets the user
@ -2933,7 +2934,7 @@ export class HealStatusEffectAttr extends MoveEffectAttr {
*/
constructor(selfTarget: boolean, effects: StatusEffect | StatusEffect[]) {
super(selfTarget, { lastHitOnly: true });
this.effects = coerceArray(effects)
this.effects = new Uint8Array(coerceArray(effects));
}
/**

View File

@ -15,6 +15,7 @@ import { WeatherType } from "#enums/weather-type";
import type { PlayerPokemon } from "#field/pokemon";
import { AttackTypeBoosterModifier } from "#modifiers/modifier";
import type { AttackTypeBoosterModifierType } from "#modifiers/modifier-type";
import type { ReadonlyGenericUint8Array } from "#types/typed-arrays";
import { coerceArray } from "#utils/array";
export interface EncounterRequirement {
@ -696,7 +697,7 @@ export class AbilityRequirement extends EncounterPokemonRequirement {
}
export class StatusEffectRequirement extends EncounterPokemonRequirement {
requiredStatusEffect: StatusEffect[];
requiredStatusEffect: ReadonlyGenericUint8Array<StatusEffect>;
minNumberOfPokemon: number;
invertQuery: boolean;
@ -704,7 +705,7 @@ export class StatusEffectRequirement extends EncounterPokemonRequirement {
super();
this.minNumberOfPokemon = minNumberOfPokemon;
this.invertQuery = invertQuery;
this.requiredStatusEffect = coerceArray(statusEffect);
this.requiredStatusEffect = new Uint8Array(coerceArray(statusEffect));
}
override meetsRequirement(): boolean {

View File

@ -11,6 +11,7 @@ import type { TimeOfDay } from "#enums/time-of-day";
import { WeatherType } from "#enums/weather-type";
import type { Pokemon } from "#field/pokemon";
import type { PokemonFormChangeItemModifier } from "#modifiers/modifier";
import type { ReadonlyGenericUint8Array } from "#types/typed-arrays";
import { coerceArray } from "#utils/array";
import type { Constructor } from "#utils/common";
import { toCamelCase } from "#utils/strings";
@ -122,12 +123,12 @@ export class SpeciesFormChangeActiveTrigger extends SpeciesFormChangeTrigger {
}
export class SpeciesFormChangeStatusEffectTrigger extends SpeciesFormChangeTrigger {
public statusEffects: StatusEffect[];
public readonly statusEffects: ReadonlyGenericUint8Array<StatusEffect>;
public invert: boolean;
constructor(statusEffects: StatusEffect | StatusEffect[], invert = false) {
super();
this.statusEffects = coerceArray(statusEffects);
this.statusEffects = new Uint8Array(coerceArray(statusEffects));
this.invert = invert;
// this.description = i18next.t("pokemonEvolutions:forms.statusEffect");
}

View File

@ -54,6 +54,13 @@ declare module "phaser" {
}
}
namespace Math {
interface RandomDataGenerator {
pick<T>(array: ArrayLike<T>): T;
weightedPick<T>(array: ArrayLike<T>): T;
}
}
namespace Input {
namespace Gamepad {
interface GamepadPlugin {

View File

@ -125,11 +125,11 @@ export function randSeedFloat(): number {
return Phaser.Math.RND.frac();
}
export function randItem<T>(items: T[]): T {
export function randItem<T>(items: ArrayLike<T>): T {
return items.length === 1 ? items[0] : items[randInt(items.length)];
}
export function randSeedItem<T>(items: T[]): T {
export function randSeedItem<T>(items: ArrayLike<T>): T {
return items.length === 1 ? items[0] : Phaser.Math.RND.pick(items);
}