Pokemon_server / dist /sim /tools /random-player-ai.js.map
Jofthomas's picture
Jofthomas HF staff
Upload 4781 files
5c2ed06 verified
{
"version": 3,
"sources": ["../../../sim/tools/random-player-ai.ts"],
"sourcesContent": ["/**\n * Example random player AI.\n *\n * Pokemon Showdown - http://pokemonshowdown.com/\n *\n * @license MIT\n */\n\nimport type { ObjectReadWriteStream } from '../../lib/streams';\nimport { BattlePlayer } from '../battle-stream';\nimport { PRNG, type PRNGSeed } from '../prng';\nimport type { ChoiceRequest } from '../side';\n\nexport class RandomPlayerAI extends BattlePlayer {\n\tprotected readonly move: number;\n\tprotected readonly mega: number;\n\tprotected readonly prng: PRNG;\n\n\tconstructor(\n\t\tplayerStream: ObjectReadWriteStream<string>,\n\t\toptions: { move?: number, mega?: number, seed?: PRNG | PRNGSeed | null } = {},\n\t\tdebug = false\n\t) {\n\t\tsuper(playerStream, debug);\n\t\tthis.move = options.move || 1.0;\n\t\tthis.mega = options.mega || 0;\n\t\tthis.prng = PRNG.get(options.seed);\n\t}\n\n\treceiveError(error: Error) {\n\t\t// If we made an unavailable choice we will receive a followup request to\n\t\t// allow us the opportunity to correct our decision.\n\t\tif (error.message.startsWith('[Unavailable choice]')) return;\n\t\tthrow error;\n\t}\n\n\toverride receiveRequest(request: ChoiceRequest) {\n\t\tif (request.wait) {\n\t\t\t// wait request\n\t\t\t// do nothing\n\t\t} else if (request.forceSwitch) {\n\t\t\t// switch request\n\t\t\tconst pokemon = request.side.pokemon;\n\t\t\tconst chosen: number[] = [];\n\t\t\tconst choices = request.forceSwitch.map((mustSwitch, i) => {\n\t\t\t\tif (!mustSwitch) return `pass`;\n\n\t\t\t\tconst canSwitch = range(1, 6).filter(j => (\n\t\t\t\t\tpokemon[j - 1] &&\n\t\t\t\t\t// not active\n\t\t\t\t\tj > request.forceSwitch.length &&\n\t\t\t\t\t// not chosen for a simultaneous switch\n\t\t\t\t\t!chosen.includes(j) &&\n\t\t\t\t\t// not fainted or fainted and using Revival Blessing\n\t\t\t\t\t!pokemon[j - 1].condition.endsWith(` fnt`) === !pokemon[i].reviving\n\t\t\t\t));\n\n\t\t\t\tif (!canSwitch.length) return `pass`;\n\t\t\t\tconst target = this.chooseSwitch(\n\t\t\t\t\tundefined,\n\t\t\t\t\tcanSwitch.map(slot => ({ slot, pokemon: pokemon[slot - 1] }))\n\t\t\t\t);\n\t\t\t\tchosen.push(target);\n\t\t\t\treturn `switch ${target}`;\n\t\t\t});\n\n\t\t\tthis.choose(choices.join(`, `));\n\t\t} else if (request.teamPreview) {\n\t\t\tthis.choose(this.chooseTeamPreview(request.side.pokemon));\n\t\t} else if (request.active) {\n\t\t\t// move request\n\t\t\tlet [canMegaEvo, canUltraBurst, canZMove, canDynamax, canTerastallize] = [true, true, true, true, true];\n\t\t\tconst pokemon = request.side.pokemon;\n\t\t\tconst chosen: number[] = [];\n\t\t\tconst choices = request.active.map((active: AnyObject, i: number) => {\n\t\t\t\tif (pokemon[i].condition.endsWith(` fnt`) || pokemon[i].commanding) return `pass`;\n\n\t\t\t\tcanMegaEvo = canMegaEvo && active.canMegaEvo;\n\t\t\t\tcanUltraBurst = canUltraBurst && active.canUltraBurst;\n\t\t\t\tcanZMove = canZMove && !!active.canZMove;\n\t\t\t\tcanDynamax = canDynamax && !!active.canDynamax;\n\t\t\t\tcanTerastallize = canTerastallize && !!active.canTerastallize;\n\n\t\t\t\t// Determine whether we should change form if we do end up switching\n\t\t\t\tconst change = (canMegaEvo || canUltraBurst || canDynamax) && this.prng.random() < this.mega;\n\t\t\t\t// If we've already dynamaxed or if we're planning on potentially dynamaxing\n\t\t\t\t// we need to use the maxMoves instead of our regular moves\n\n\t\t\t\tconst useMaxMoves = (!active.canDynamax && active.maxMoves) || (change && canDynamax);\n\t\t\t\tconst possibleMoves = useMaxMoves ? active.maxMoves.maxMoves : active.moves;\n\n\t\t\t\tlet canMove = range(1, possibleMoves.length).filter(j => (\n\t\t\t\t\t// not disabled\n\t\t\t\t\t!possibleMoves[j - 1].disabled\n\t\t\t\t\t// NOTE: we don't actually check for whether we have PP or not because the\n\t\t\t\t\t// simulator will mark the move as disabled if there is zero PP and there are\n\t\t\t\t\t// situations where we actually need to use a move with 0 PP (Gen 1 Wrap).\n\t\t\t\t)).map(j => ({\n\t\t\t\t\tslot: j,\n\t\t\t\t\tmove: possibleMoves[j - 1].move,\n\t\t\t\t\ttarget: possibleMoves[j - 1].target,\n\t\t\t\t\tzMove: false,\n\t\t\t\t}));\n\t\t\t\tif (canZMove) {\n\t\t\t\t\tcanMove.push(...range(1, active.canZMove.length)\n\t\t\t\t\t\t.filter(j => active.canZMove[j - 1])\n\t\t\t\t\t\t.map(j => ({\n\t\t\t\t\t\t\tslot: j,\n\t\t\t\t\t\t\tmove: active.canZMove[j - 1].move,\n\t\t\t\t\t\t\ttarget: active.canZMove[j - 1].target,\n\t\t\t\t\t\t\tzMove: true,\n\t\t\t\t\t\t})));\n\t\t\t\t}\n\n\t\t\t\t// Filter out adjacentAlly moves if we have no allies left, unless they're our\n\t\t\t\t// only possible move options.\n\t\t\t\tconst hasAlly = pokemon.length > 1 && !pokemon[i ^ 1].condition.endsWith(` fnt`);\n\t\t\t\tconst filtered = canMove.filter(m => m.target !== `adjacentAlly` || hasAlly);\n\t\t\t\tcanMove = filtered.length ? filtered : canMove;\n\n\t\t\t\tconst moves = canMove.map(m => {\n\t\t\t\t\tlet move = `move ${m.slot}`;\n\t\t\t\t\t// NOTE: We don't generate all possible targeting combinations.\n\t\t\t\t\tif (request.active.length > 1) {\n\t\t\t\t\t\tif ([`normal`, `any`, `adjacentFoe`].includes(m.target)) {\n\t\t\t\t\t\t\tmove += ` ${1 + this.prng.random(2)}`;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (m.target === `adjacentAlly`) {\n\t\t\t\t\t\t\tmove += ` -${(i ^ 1) + 1}`;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (m.target === `adjacentAllyOrSelf`) {\n\t\t\t\t\t\t\tif (hasAlly) {\n\t\t\t\t\t\t\t\tmove += ` -${1 + this.prng.random(2)}`;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tmove += ` -${i + 1}`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (m.zMove) move += ` zmove`;\n\t\t\t\t\treturn { choice: move, move: m };\n\t\t\t\t});\n\n\t\t\t\tconst canSwitch = range(1, 6).filter(j => (\n\t\t\t\t\tpokemon[j - 1] &&\n\t\t\t\t\t// not active\n\t\t\t\t\t!pokemon[j - 1].active &&\n\t\t\t\t\t// not chosen for a simultaneous switch\n\t\t\t\t\t!chosen.includes(j) &&\n\t\t\t\t\t// not fainted\n\t\t\t\t\t!pokemon[j - 1].condition.endsWith(` fnt`)\n\t\t\t\t));\n\t\t\t\tconst switches = active.trapped ? [] : canSwitch;\n\n\t\t\t\tif (switches.length && (!moves.length || this.prng.random() > this.move)) {\n\t\t\t\t\tconst target = this.chooseSwitch(\n\t\t\t\t\t\tactive,\n\t\t\t\t\t\tcanSwitch.map(slot => ({ slot, pokemon: pokemon[slot - 1] }))\n\t\t\t\t\t);\n\t\t\t\t\tchosen.push(target);\n\t\t\t\t\treturn `switch ${target}`;\n\t\t\t\t} else if (moves.length) {\n\t\t\t\t\tconst move = this.chooseMove(active, moves);\n\t\t\t\t\tif (move.endsWith(` zmove`)) {\n\t\t\t\t\t\tcanZMove = false;\n\t\t\t\t\t\treturn move;\n\t\t\t\t\t} else if (change) {\n\t\t\t\t\t\tif (canTerastallize) {\n\t\t\t\t\t\t\tcanTerastallize = false;\n\t\t\t\t\t\t\treturn `${move} terastallize`;\n\t\t\t\t\t\t} else if (canDynamax) {\n\t\t\t\t\t\t\tcanDynamax = false;\n\t\t\t\t\t\t\treturn `${move} dynamax`;\n\t\t\t\t\t\t} else if (canMegaEvo) {\n\t\t\t\t\t\t\tcanMegaEvo = false;\n\t\t\t\t\t\t\treturn `${move} mega`;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tcanUltraBurst = false;\n\t\t\t\t\t\t\treturn `${move} ultra`;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn move;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tthrow new Error(`${this.constructor.name} unable to make choice ${i}. request='${typeof request}',` +\n\t\t\t\t\t\t` chosen='${chosen}', (mega=${canMegaEvo}, ultra=${canUltraBurst}, zmove=${canZMove},` +\n\t\t\t\t\t\t` dynamax='${canDynamax}', terastallize=${canTerastallize})`);\n\t\t\t\t}\n\t\t\t});\n\t\t\tthis.choose(choices.join(`, `));\n\t\t}\n\t}\n\n\tprotected chooseTeamPreview(team: AnyObject[]): string {\n\t\treturn `default`;\n\t}\n\n\tprotected chooseMove(active: AnyObject, moves: { choice: string, move: AnyObject }[]): string {\n\t\treturn this.prng.sample(moves).choice;\n\t}\n\n\tprotected chooseSwitch(active: AnyObject | undefined, switches: { slot: number, pokemon: AnyObject }[]): number {\n\t\treturn this.prng.sample(switches).slot;\n\t}\n}\n\n// Creates an array of numbers progressing from start up to and including end\nfunction range(start: number, end?: number, step = 1) {\n\tif (end === undefined) {\n\t\tend = start;\n\t\tstart = 0;\n\t}\n\tconst result = [];\n\tfor (; start <= end; start += step) {\n\t\tresult.push(start);\n\t}\n\treturn result;\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,2BAA6B;AAC7B,kBAAoC;AAVpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaO,MAAM,uBAAuB,kCAAa;AAAA,EAKhD,YACC,cACA,UAA2E,CAAC,GAC5E,QAAQ,OACP;AACD,UAAM,cAAc,KAAK;AACzB,SAAK,OAAO,QAAQ,QAAQ;AAC5B,SAAK,OAAO,QAAQ,QAAQ;AAC5B,SAAK,OAAO,iBAAK,IAAI,QAAQ,IAAI;AAAA,EAClC;AAAA,EAEA,aAAa,OAAc;AAG1B,QAAI,MAAM,QAAQ,WA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