Skip to content

Instantly share code, notes, and snippets.

@scheibo
Created March 11, 2024 02:05
Show Gist options
  • Save scheibo/f299051612bf9350cf36b522cc9e0cf6 to your computer and use it in GitHub Desktop.
Save scheibo/f299051612bf9350cf36b522cc9e0cf6 to your computer and use it in GitHub Desktop.
import type {Battle, Side, Pokemon} from ".";
function encodeBattle(battle: Battle) {
const buf: any[] = [];
for (const side of battle.sides) {
encodeSide(side, buf);
}
buf.push(battle.turn);
buf.push(battle.lastDamage);
return buf;
}
function encodeSide(side: Side, buf: any[]) {
// important! put mons in the correct order!!!
let slot = 0;
for (const pokemon of side.pokemon) {
slot++;
encodePokemon(pokemon, slot === 1, buf);
}
buf.push(side.lastUsedMove);
buf.push(side.lastSelectedMove);
buf.push(side.lastMoveIndex);
buf.push(side.lastMoveCounterable);
}
function encodePokemon(pokemon: Pokemon, active: boolean, buf: any[]) {
buf.push(pokemon.species);
// probably doesn't matter to split out into type1/type2
buf.push(pokemon.types);
buf.push(pokemon.level);
buf.push(pokemon.hp);
// might want to one hot encode status?
buf.push(pokemon.status);
buf.push(pokemon.statusData.sleep);
buf.push(pokemon.statusData.self);
buf.push(pokemon.statusData.toxic);
buf.push(pokemon.statusData.toxic);
for (const stat of ['hp', 'atk', 'def', 'spc', 'spe'] as const) {
buf.push(pokemon.stats[stat]);
}
for (const move of pokemon.moves) {
// possibly want to augment with additional info about the move like BP/type
buf.push(move.id);
buf.push(move.pp);
if (active) buf.push(move.disabled);
}
if (active) {
// can save a byte dropping evasion in Smogon RBY because only Metronome can modify it
for (const boost of ['atk', 'def', 'spc', 'spe', 'accuracy', 'evasion'] ) {
buf.push(pokemon.boosts[boost]);
}
buf.push(pokemon.volatiles.bide?.duration);
buf.push(pokemon.volatiles.bide?.damage);
buf.push(pokemon.volatiles.thrashing?.duration);
buf.push(pokemon.volatiles.thrashing?.accuracy);
buf.push(pokemon.volatiles.flinch);
buf.push(pokemon.volatiles.binding?.duration);
buf.push(pokemon.volatiles.invulnerable);
buf.push(pokemon.volatiles.confusion?.duration);
buf.push(pokemon.volatiles.mist);
buf.push(pokemon.volatiles.focusenergy);
buf.push(pokemon.volatiles.substitute?.hp);
buf.push(pokemon.volatiles.recharging);
buf.push(pokemon.volatiles.rage?.accuracy);
buf.push(pokemon.volatiles.leechseed);
buf.push(pokemon.volatiles.lightscreen);
buf.push(pokemon.volatiles.reflect);
buf.push(pokemon.volatiles.transform?.player);
buf.push(pokemon.volatiles.transform?.slot);
buf.push(pokemon.stored.species);
buf.push(pokemon.stored.types);
for (const stat of ['hp', 'atk', 'def', 'spc', 'spe'] as const) {
buf.push(pokemon.stored.stats[stat]);
}
for (const move of pokemon.stored.moves) {
buf.push(move.id);
buf.push(move.pp);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment