Skip to content

Commit

Permalink
Dex*: cache empty objects
Browse files Browse the repository at this point in the history
  • Loading branch information
larry-the-table-guy committed Oct 12, 2024
1 parent 451b17b commit 434922f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
7 changes: 5 additions & 2 deletions sim/dex-abilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {PokemonEventMethods, ConditionData} from './dex-conditions';
import {BasicEffect, toID} from './dex-data';
import {Utils} from '../lib';

interface AbilityEventMethods {
onCheckShow?: (this: Battle, pokemon: Pokemon) => void;
Expand Down Expand Up @@ -66,6 +67,8 @@ export class Ability extends BasicEffect implements Readonly<BasicEffect> {
}
}

const EMPTY_ABILITY = Utils.deepFreeze(new Ability({id: '', name: '', exists: false}));

export class DexAbilities {
readonly dex: ModdedDex;
readonly abilityCache = new Map<ID, Ability>();
Expand All @@ -77,12 +80,12 @@ export class DexAbilities {

get(name: string | Ability = ''): Ability {
if (name && typeof name !== 'string') return name;

const id = toID(name);
const id = toID(name.trim());
return this.getByID(id);
}

getByID(id: ID): Ability {
if (id === '') return EMPTY_ABILITY;
let ability = this.abilityCache.get(id);
if (ability) return ability;

Expand Down
5 changes: 3 additions & 2 deletions sim/dex-conditions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Utils} from '../lib';
import {BasicEffect, toID} from './dex-data';
import type {SecondaryEffect, MoveEventMethods} from './dex-moves';

Expand Down Expand Up @@ -633,7 +634,7 @@ export class Condition extends BasicEffect implements
}
}

const EMPTY_CONDITION: Condition = new Condition({name: '', exists: false});
const EMPTY_CONDITION: Condition = Utils.deepFreeze(new Condition({name: '', exists: false}));

export class DexConditions {
readonly dex: ModdedDex;
Expand All @@ -651,7 +652,7 @@ export class DexConditions {
}

getByID(id: ID): Condition {
if (!id) return EMPTY_CONDITION;
if (id === '') return EMPTY_CONDITION;

let condition = this.conditionCache.get(id);
if (condition) return condition;
Expand Down
7 changes: 6 additions & 1 deletion sim/dex-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export class Nature extends BasicEffect implements Readonly<BasicEffect & Nature
}
}

const EMPTY_NATURE = Utils.deepFreeze(new Nature({name: '', exists: false}));

export interface NatureData {
name: string;
plus?: StatIDExceptHP;
Expand All @@ -167,10 +169,10 @@ export class DexNatures {

get(name: string | Nature): Nature {
if (name && typeof name !== 'string') return name;

return this.getByID(toID(name));
}
getByID(id: ID): Nature {
if (id === '') return EMPTY_NATURE;
let nature = this.natureCache.get(id);
if (nature) return nature;

Expand Down Expand Up @@ -274,6 +276,8 @@ export class TypeInfo implements Readonly<TypeData> {
}
}

const EMPTY_TYPE_INFO = Utils.deepFreeze(new TypeInfo({name: '', id: '', exists: false, effectType: 'EffectType'}));

export class DexTypes {
readonly dex: ModdedDex;
readonly typeCache = new Map<ID, TypeInfo>();
Expand All @@ -290,6 +294,7 @@ export class DexTypes {
}

getByID(id: ID): TypeInfo {
if (id === '') return EMPTY_TYPE_INFO;
let type = this.typeCache.get(id);
if (type) return type;

Expand Down
8 changes: 5 additions & 3 deletions sim/dex-items.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {PokemonEventMethods, ConditionData} from './dex-conditions';
import {BasicEffect, toID} from './dex-data';
import {Utils} from '../lib';

interface FlingData {
basePower: number;
Expand Down Expand Up @@ -154,6 +155,8 @@ export class Item extends BasicEffect implements Readonly<BasicEffect> {
}
}

const EMPTY_ITEM = Utils.deepFreeze(new Item({name: '', exists: false}));

export class DexItems {
readonly dex: ModdedDex;
readonly itemCache = new Map<ID, Item>();
Expand All @@ -165,13 +168,12 @@ export class DexItems {

get(name?: string | Item): Item {
if (name && typeof name !== 'string') return name;

name = (name || '').trim();
const id = toID(name);
const id = name ? toID(name.trim()) : '' as ID;
return this.getByID(id);
}

getByID(id: ID): Item {
if (id === '') return EMPTY_ITEM;
let item = this.itemCache.get(id);
if (item) return item;
if (this.dex.data.Aliases.hasOwnProperty(id)) {
Expand Down
7 changes: 4 additions & 3 deletions sim/dex-moves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ export class DataMove extends BasicEffect implements Readonly<BasicEffect & Move
}
}

const EMPTY_MOVE = Utils.deepFreeze(new DataMove({name: '', exists: false}));

export class DexMoves {
readonly dex: ModdedDex;
readonly moveCache = new Map<ID, Move>();
Expand All @@ -625,13 +627,12 @@ export class DexMoves {

get(name?: string | Move): Move {
if (name && typeof name !== 'string') return name;

name = (name || '').trim();
const id = toID(name);
const id = name ? toID(name.trim()) : '' as ID;
return this.getByID(id);
}

getByID(id: ID): Move {
if (id === '') return EMPTY_MOVE;
let move = this.moveCache.get(id);
if (move) return move;
if (this.dex.data.Aliases.hasOwnProperty(id)) {
Expand Down
25 changes: 18 additions & 7 deletions sim/dex-species.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Utils} from '../lib';
import {toID, BasicEffect} from './dex-data';

interface SpeciesAbility {
Expand Down Expand Up @@ -355,6 +356,12 @@ export class Species extends BasicEffect implements Readonly<BasicEffect & Speci
}
}

const EMPTY_SPECIES = Utils.deepFreeze(new Species({
id: '', name: '', exists: false,
tier: 'Illegal', doublesTier: 'Illegal',
natDexTier: 'Illegal', isNonstandard: 'Custom',
}));

export class Learnset {
readonly effectType: 'Learnset';
/**
Expand Down Expand Up @@ -394,17 +401,21 @@ export class DexSpecies {
get(name?: string | Species): Species {
if (name && typeof name !== 'string') return name;

name = (name || '').trim();
let id = toID(name);
if (id === 'nidoran' && name.endsWith('♀')) {
id = 'nidoranf' as ID;
} else if (id === 'nidoran' && name.endsWith('♂')) {
id = 'nidoranm' as ID;
let id = '' as ID;
if (name) {
name = name.trim();
id = toID(name);
if (id === 'nidoran' && name.endsWith('♀')) {
id = 'nidoranf' as ID;
} else if (id === 'nidoran' && name.endsWith('♂')) {
id = 'nidoranm' as ID;
}
}

return this.getByID(id);
}

getByID(id: ID): Species {
if (id === '') return EMPTY_SPECIES;
let species: Mutable<Species> | undefined = this.speciesCache.get(id);
if (species) return species;

Expand Down

0 comments on commit 434922f

Please sign in to comment.