Created
February 26, 2024 23:45
-
-
Save scheibo/44e77c46cec2aae560be0763d69836aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Generations} from '@pkmn/data'; | |
import {TeamGenerators} from '@pkmn/randoms'; | |
import {Battle, Dex, ID, PRNG, PRNGSeed, Teams, extractChannelMessages} from '@pkmn/sim'; | |
import {RandomPlayer} from './random-player'; | |
Teams.setGeneratorFactory(TeamGenerators as any); | |
const gens = new Generations(Dex as any); | |
const gen = gens.get(process.argv[2] ?? 1); | |
const prng = new PRNG([1, 2, 3, 4]); | |
const newSeed = (r: PRNG) => | |
[r.next(0x10000), r.next(0x10000), r.next(0x10000), r.next(0x10000)] as PRNGSeed; | |
const random = (min?: number, max?: number) => prng.next(min, max); | |
const wins = {p1: {swapped: 0, raw: 0}, p2: {swapped: 0, raw: 0}}; | |
for (let i = 0; i < 10000; i++) { | |
const a = Teams.generate(`gen${gen.num}randombattle`, {seed: newSeed(prng)}); | |
const b = Teams.generate(`gen${gen.num}randombattle`, {seed: newSeed(prng)}); | |
for (const [t1, t2, swapped] of [[a, b, false], [b, a, true]] as const) { | |
const p1 = new RandomPlayer(gens, random, {switch: {consider: 0 / 100}}); | |
const p2 = new RandomPlayer(gens, random, {switch: {consider: 0 / 100}}); | |
const p1spec = {name: 'p1', team: Teams.pack(t1)}; | |
const p2spec = {name: 'p2', team: Teams.pack(t2)}; | |
const battle = new Battle({ | |
formatid: `gen${gen.num}customgame` as ID, | |
seed: prng.initialSeed, | |
strictChoices: false, | |
send: (type: string, data: any) => { | |
if (Array.isArray(data)) data = data.join('\n'); | |
if (type === 'update') { | |
const msgs = extractChannelMessages(data, [-1, 0, 1, 2]); | |
// console.debug(msgs[-1].join('\n')); | |
p1.accept(msgs[1].join('\n')); | |
p2.accept(msgs[2].join('\n')); | |
} else if (type === 'sideupdate') { | |
const index = data.indexOf('\n'); | |
const side = data.slice(0, index); | |
(side === 'p1' ? p1 : p2).accept(data.slice(index + 1)); | |
} | |
}, | |
}); | |
let started = false; | |
do { | |
if (!started) { | |
battle.setPlayer('p1', p1spec); | |
battle.setPlayer('p2', p2spec); | |
started = true; | |
} else { | |
battle.makeChoices(p1.choice, p2.choice); | |
battle.sendUpdates(); | |
} | |
} while (!battle.ended); | |
if (battle.winner) { | |
const w = wins[battle.winner as 'p1' | 'p2']; | |
w.swapped++; | |
if (!swapped) w.raw++; | |
} | |
} | |
} | |
console.debug(wins); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment