Skip to content

Commit

Permalink
WIP 44
Browse files Browse the repository at this point in the history
  • Loading branch information
scheibo committed Feb 7, 2024
1 parent ae12617 commit 5e3b785
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 155 deletions.
4 changes: 1 addition & 3 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
The MIT License (MIT)

Copyright (c) 2020-2023 pkmn contributors
Copyright (c) 2020-2024 pkmn contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
31 changes: 16 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
},
"files": ["build", "!build/test", "src", "!src/test"],
"dependencies": {
"@pkmn/data": "^0.6.1"
"@pkmn/data": "^0.8.7"
},
"devDependencies": {
"@babel/preset-env": "^7.19.4",
"@babel/preset-typescript": "^7.18.6",
"@pkmn/eslint-config": "^2.11.0",
"@pkmn/sim": "^0.6.1",
"@smogon/calc": "^0.6.0",
"@types/jest": "^29.2.0",
"@types/node": "^18.11.7",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"eslint": "^8.26.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.1.3",
"jest": "^29.2.2",
"@babel/preset-env": "^7.23.9",
"@babel/preset-typescript": "^7.23.3",
"@pkmn/eslint-config": "^6.6.0",
"@pkmn/sim": "^0.8.7",
"@smogon/calc": "^0.9.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"eslint": "^8.56.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.6.3",
"jest": "^29.7.0",
"microbundle": "^0.15.1",
"typescript": "^4.8.4"
"typescript": "^5.3.3"
},
"scripts": {
"lint": "eslint --cache src --ext ts",
Expand Down Expand Up @@ -89,6 +89,7 @@
{
"files": ["src/**/*.ts"],
"rules": {
"import/order": "off",
"@typescript-eslint/restrict-template-expressions": 0,
"@typescript-eslint/ban-types": [
"error",
Expand Down
4 changes: 2 additions & 2 deletions src/conditions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {GenerationNum, ID, StatusName, Generation} from '@pkmn/data';
import type {Generation, GenerationNum, ID, StatusName} from '@pkmn/data';

import {toID, is} from './utils';
import {is, toID} from './utils';

export type Player = 'p1' | 'p2';

Expand Down
12 changes: 6 additions & 6 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type {
BoostsTable,
ConditionData,
Move as DMove,
GameType,
GenderName,
Generation,
GenerationNum,
HitEffect,
ID,
Move as DMove,
MoveCategory,
MoveName,
MoveTarget,
Expand All @@ -22,8 +22,8 @@ import type {
TypeName,
} from '@pkmn/data';

import {WeatherName, TerrainName} from './conditions';
import {Handlers, Handler, HANDLER_FNS, HANDLERS, HandlerKind} from './mechanics';
import {TerrainName, WeatherName} from './conditions';
import {HANDLERS, HANDLER_FNS, Handler, HandlerKind, Handlers} from './mechanics';
import {Relevancy} from './result';
import {DeepReadonly, extend, toID} from './utils';
import {State} from './state';
Expand Down Expand Up @@ -205,8 +205,8 @@ export namespace Context {
position?: number;

switching?: 'in' | 'out';
moveLastTurnResult?: false | unknown;
hurtThisTurn?: false | unknown;
moveLastTurnResult?: unknown;
hurtThisTurn?: unknown;

readonly relevant: Relevancy.Pokemon;

Expand Down Expand Up @@ -474,7 +474,7 @@ function reify<T>(
if (fn && HANDLER_FNS.has(k) && typeof fn === 'function') {
obj[k] = (c: Context) => {
const r = (fn as any)(c);
if (typeof r !== undefined && cbfn) cbfn();
if (typeof r !== 'undefined' && cbfn) cbfn();
return r;
};
}
Expand Down
36 changes: 18 additions & 18 deletions src/encode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {Generation, BoostID, StatID, NatureName, StatsTable} from '@pkmn/data';
import type {BoostID, Generation, NatureName, StatID, StatsTable} from '@pkmn/data';
import {State} from './state';
import {PseudoWeathers, SideConditions, Volatiles, Statuses} from './conditions';
import {toID, has, is} from './utils';
import {PseudoWeathers, SideConditions, Statuses, Volatiles} from './conditions';
import {has, is, toID} from './utils';
import {computeStats} from './mechanics';
import * as math from './math';

Expand Down Expand Up @@ -336,21 +336,21 @@ function getStats(
): [{p1: Exclude<StatID, 'hp'>; p2: Exclude<StatID, 'hp'>} | undefined, boolean] {
if (move.category === 'Status') return [undefined, true];
switch (move.name) {
case 'Photon Geyser':
case 'Light That Burns The Sky': {
const {atk, spa} = computeStats(gen, p1);
return [atk > spa ? {p1: 'atk', p2: 'def'} : {p1: 'spa', p2: 'spd'}, false];
}
case 'Shell Side Arm': {
const {atk, spa} = computeStats(gen, p1);
const {def, spd} = computeStats(gen, p2);
return [(atk / def) > (spa / spd) ? {p1: 'atk', p2: 'def'} : {p1: 'spa', p2: 'spd'}, false];
}
default:
return [{
p1: move.overrideOffensiveStat || (move.category === 'Special' ? 'spa' : 'atk'),
p2: move.overrideDefensiveStat || (move.category === 'Special' ? 'spd' : 'def'),
}, move.name !== 'Body Press'];
case 'Photon Geyser':
case 'Light That Burns The Sky': {
const {atk, spa} = computeStats(gen, p1);
return [atk > spa ? {p1: 'atk', p2: 'def'} : {p1: 'spa', p2: 'spd'}, false];
}
case 'Shell Side Arm': {
const {atk, spa} = computeStats(gen, p1);
const {def, spd} = computeStats(gen, p2);
return [(atk / def) > (spa / spd) ? {p1: 'atk', p2: 'def'} : {p1: 'spa', p2: 'spd'}, false];
}
default:
return [{
p1: move.overrideOffensiveStat || (move.category === 'Special' ? 'spa' : 'atk'),
p2: move.overrideDefensiveStat || (move.category === 'Special' ? 'spd' : 'def'),
}, move.name !== 'Body Press'];
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {GenerationNum, Generations, Generation, Specie, GameType} from '@pkmn/data';
import type {GameType, Generation, GenerationNum, Generations, Specie} from '@pkmn/data';

import {PokemonOptions, MoveOptions, State} from './state';
import {MoveOptions, PokemonOptions, State} from './state';
import {Result} from './result';
import {calculate} from './mechanics';

Expand Down
49 changes: 24 additions & 25 deletions src/mechanics/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {GameType, Generation, Generations, ID, MoveName, TypeName, StatsTable} from '@pkmn/data';
import type {GameType, Generation, Generations, ID, MoveName, StatsTable, TypeName} from '@pkmn/data';

import {State} from '../state';
import {Context} from '../context';
Expand All @@ -11,7 +11,7 @@ import {Conditions} from './conditions';
import {Items} from './items';
import {Moves} from './moves';

import {clamp, floor, trunc, max, min, chain, apply, abs} from '../math';
import {abs, apply, chain, clamp, floor, max, min, trunc} from '../math';

export interface Applier {
apply(side: 'p1' | 'p2', state: State, guaranteed?: boolean): void;
Expand Down Expand Up @@ -54,32 +54,32 @@ export class Appliers {
if (!id) return;

switch (kind) {
case 'Abilities':
case 'Items':
return this.handlers[kind][id]?.apply?.(side, state, guaranteed);
case 'Moves': {
case 'Abilities':
case 'Items':
return this.handlers[kind][id]?.apply?.(side, state, guaranteed);
case 'Moves': {
// If a Move handler is defined, use it, otherwise try to see if an 'apply' function can
// can be inferred based purely on information from the data files
const handler = this.handlers.Moves[id];
if (handler?.apply) return handler.apply(side, state, guaranteed);
const handler = this.handlers.Moves[id];
if (handler?.apply) return handler.apply(side, state, guaranteed);

const move = state.gen.moves.get(id);
if (!move) return;
const move = state.gen.moves.get(id);
if (!move) return;

const secondaries = move.secondaries
? move.secondaries
: move.secondary ? [move.secondary] : undefined;
if (!secondaries) return;
const secondaries = move.secondaries
? move.secondaries
: move.secondary ? [move.secondary] : undefined;
if (!secondaries) return;

for (const secondary of secondaries) {
if (guaranteed && secondary.chance && secondary.chance < 100) continue;
for (const secondary of secondaries) {
if (guaranteed && secondary.chance && secondary.chance < 100) continue;
// TODO apply secondary! need to take into account Simple etc for boosts, other affects
// for slot conditions etc
}
return;
}
return;
}
default:
throw new Error(`Invalid handler kind: '${kind}'`);
default:
throw new Error(`Invalid handler kind: '${kind}'`);
}
}
}
Expand Down Expand Up @@ -196,7 +196,7 @@ export function computeModifiedWeight(pokemon: Context.Pokemon | State.Pokemon)
return weighthg;
}

const Z_MOVES: { [type in Exclude<TypeName, '???'>]: string } = {
const Z_MOVES: { [type in Exclude<TypeName, '???' | 'Stellar'>]: string } = {
Bug: 'Savage Spin-Out',
Dark: 'Black Hole Eclipse',
Dragon: 'Devastating Drake',
Expand Down Expand Up @@ -234,10 +234,10 @@ export function getZMoveName(
item.zMoveFrom === move.name;
if (matching) return item.zMove;
}
return Z_MOVES[move.type as Exclude<TypeName, '???'>];
return Z_MOVES[move.type as Exclude<TypeName, '???'| 'Stellar'>];
}

const MAX_MOVES: { [type in Exclude<TypeName, '???'>]: string } = {
const MAX_MOVES: { [type in Exclude<TypeName, '???' | 'Stellar'>]: string } = {
Bug: 'Max Flutterby',
Dark: 'Max Darkness',
Dragon: 'Max Wyrmwind',
Expand Down Expand Up @@ -272,7 +272,7 @@ export function getMaxMovename(
const gmaxMove = gen.moves.get(pokemon.species.isGigantamax)!;
if (move.type === gmaxMove.type) return pokemon.species.isGigantamax;
}
return MAX_MOVES[move.type as Exclude<TypeName, '???'>];
return MAX_MOVES[move.type as Exclude<TypeName, '???' | 'Stellar'>];
}

// function takeItem(pokemon: State.Pokemon | Context.Pokemon, boost: BoostID, amount: number) {
Expand All @@ -283,7 +283,6 @@ export function getMaxMovename(

// }


/*
setAbility(ability: string | Ability, source?: Pokemon | null, isFromFormeChange?: boolean) {
Expand Down
10 changes: 6 additions & 4 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import type {
TypeName,
} from '@pkmn/data';

import {Conditions, ConditionKind, Player} from './conditions';
import {State, bounded, MOVE_SUGAR} from './state';
import {decodeURL, getNature, ABILITIES, STAT_ORDER, RBY_STAT_ORDER} from './encode';
import {is, toID, has} from './utils';
import {ConditionKind, Conditions, Player} from './conditions';
import {MOVE_SUGAR, State, bounded} from './state';
import {ABILITIES, RBY_STAT_ORDER, STAT_ORDER, decodeURL, getNature} from './encode';
import {has, is, toID} from './utils';

// Flags can either be specified as key:value or as 'implicits'
// eslint-disable-next-line max-len
Expand Down Expand Up @@ -86,6 +86,8 @@ const PHRASE = new RegExp([
LEVEL, // 9
EVS, // 10
HP, // 11

// eslint-disable-next-line no-misleading-character-class
new RegExp(`${POKEMON_AND_ITEM.source}$`), // 12 & 13
].map(r => r.source).join(''), 'i');

Expand Down
Loading

0 comments on commit 5e3b785

Please sign in to comment.