Skip to content

Commit

Permalink
Speed up Random Battles species selection (#10138)
Browse files Browse the repository at this point in the history
* Speed up Random Battles species selection

* Apply suggestions from code review

Thanks, you're right. This simplifies the code. I will make the other necessary adjustments

Co-authored-by: urkerab <[email protected]>

* Improve gen 9 weight code

* Apply the same changes to old gens

* Go back to baseSpeciesPool

* unintended newlines

* missed one

* Removing the final newline

---------

Co-authored-by: urkerab <[email protected]>
  • Loading branch information
livid-washed and urkerab authored Jan 29, 2024
1 parent f2cea16 commit dab8f0a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 77 deletions.
2 changes: 1 addition & 1 deletion data/mods/gen1/random-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class RandomGen1Teams extends RandomGen2Teams {
const weaknessCount: {[k: string]: number} = {Electric: 0, Psychic: 0, Water: 0, Ice: 0, Ground: 0, Fire: 0};
let numMaxLevelPokemon = 0;

const pokemonPool = this.getPokemonPool(type, pokemon, isMonotype, Object.keys(this.randomData))[0];
const pokemonPool = Object.keys(this.getPokemonPool(type, pokemon, isMonotype, Object.keys(this.randomData))[0]);
while (pokemonPool.length && pokemon.length < this.maxTeamSize) {
const species = this.dex.species.get(this.sampleNoReplace(pokemonPool));
if (!species.exists) continue;
Expand Down
7 changes: 1 addition & 6 deletions data/mods/gen3/random-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,12 +687,7 @@ export class RandomGen3Teams extends RandomGen4Teams {
const [pokemonPool, baseSpeciesPool] = this.getPokemonPool(type, pokemon, isMonotype, pokemonList);
while (baseSpeciesPool.length && pokemon.length < this.maxTeamSize) {
const baseSpecies = this.sampleNoReplace(baseSpeciesPool);
const currentSpeciesPool: Species[] = [];
for (const poke of pokemonPool) {
const species = this.dex.species.get(poke);
if (species.baseSpecies === baseSpecies) currentSpeciesPool.push(species);
}
const species = this.sample(currentSpeciesPool);
const species = this.dex.species.get(this.sample(pokemonPool[baseSpecies]));
if (!species.exists) continue;

// Limit to one of each species (Species Clause)
Expand Down
7 changes: 1 addition & 6 deletions data/mods/gen5/random-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,12 +955,7 @@ export class RandomGen5Teams extends RandomGen6Teams {
const [pokemonPool, baseSpeciesPool] = this.getPokemonPool(type, pokemon, isMonotype, pokemonList);
while (baseSpeciesPool.length && pokemon.length < this.maxTeamSize) {
const baseSpecies = this.sampleNoReplace(baseSpeciesPool);
const currentSpeciesPool: Species[] = [];
for (const poke of pokemonPool) {
const species = this.dex.species.get(poke);
if (species.baseSpecies === baseSpecies) currentSpeciesPool.push(species);
}
const species = this.sample(currentSpeciesPool);
const species = this.dex.species.get(this.sample(pokemonPool[baseSpecies]));
if (!species.exists) continue;

// Limit to one of each species (Species Clause)
Expand Down
18 changes: 8 additions & 10 deletions data/mods/gen7/random-doubles-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1373,19 +1373,17 @@ export class RandomGen7DoublesTeams extends RandomGen8Teams {
const currentSpeciesPool: Species[] = [];
// Check if the base species has a mega forme available
let canMega = false;
for (const poke of pokemonPool) {
for (const poke of pokemonPool[baseSpecies]) {
const species = this.dex.species.get(poke);
if (!hasMega && species.baseSpecies === baseSpecies && species.isMega) canMega = true;
if (!hasMega && species.isMega) canMega = true;
}
for (const poke of pokemonPool) {
for (const poke of pokemonPool[baseSpecies]) {
const species = this.dex.species.get(poke);
if (species.baseSpecies === baseSpecies) {
// Prevent multiple megas
if (hasMega && species.isMega) continue;
// Prevent base forme, if a mega is available
if (canMega && !species.isMega) continue;
currentSpeciesPool.push(species);
}
// Prevent multiple megas
if (hasMega && species.isMega) continue;
// Prevent base forme, if a mega is available
if (canMega && !species.isMega) continue;
currentSpeciesPool.push(species);
}
const species = this.sample(currentSpeciesPool);
if (!species.exists) continue;
Expand Down
18 changes: 8 additions & 10 deletions data/mods/gen7/random-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1329,19 +1329,17 @@ export class RandomGen7Teams extends RandomGen8Teams {
const currentSpeciesPool: Species[] = [];
// Check if the base species has a mega forme available
let canMega = false;
for (const poke of pokemonPool) {
for (const poke of pokemonPool[baseSpecies]) {
const species = this.dex.species.get(poke);
if (!hasMega && species.baseSpecies === baseSpecies && species.isMega) canMega = true;
if (!hasMega && species.isMega) canMega = true;
}
for (const poke of pokemonPool) {
for (const poke of pokemonPool[baseSpecies]) {
const species = this.dex.species.get(poke);
if (species.baseSpecies === baseSpecies) {
// Prevent multiple megas
if (hasMega && species.isMega) continue;
// Prevent base forme, if a mega is available
if (canMega && !species.isMega) continue;
currentSpeciesPool.push(species);
}
// Prevent multiple megas
if (hasMega && species.isMega) continue;
// Prevent base forme, if a mega is available
if (canMega && !species.isMega) continue;
currentSpeciesPool.push(species);
}
const species = this.sample(currentSpeciesPool);

Expand Down
28 changes: 13 additions & 15 deletions data/mods/gen8/random-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2413,11 +2413,10 @@ export class RandomGen8Teams {
pokemonToExclude: RandomTeamsTypes.RandomSet[] = [],
isMonotype = false,
pokemonList: string[]
) {
): [{[k: string]: ID[]}, string[]] {
const exclude = pokemonToExclude.map(p => toID(p.species));
const pokemonPool = [];
const pokemonPool: {[k: string]: ID[]} = {};
const baseSpeciesPool = [];
const baseSpeciesCount: {[k: string]: number} = {};
for (const pokemon of pokemonList) {
let species = this.dex.species.get(pokemon);
if (exclude.includes(species.id)) continue;
Expand All @@ -2428,14 +2427,18 @@ export class RandomGen8Teams {
if (!species.types.includes(type)) continue;
}
}
pokemonPool.push(pokemon);
baseSpeciesCount[species.baseSpecies] = (baseSpeciesCount[species.baseSpecies] || 0) + 1;

if (species.baseSpecies in pokemonPool) {
pokemonPool[species.baseSpecies].push(species.id);
} else {
pokemonPool[species.baseSpecies] = [species.id];
}
}
// Include base species 1x if 1-3 formes, 2x if 4-6 formes, 3x if 7+ formes
for (const baseSpecies of Object.keys(baseSpeciesCount)) {
for (let i = 0; i < Math.min(Math.ceil(baseSpeciesCount[baseSpecies] / 3), 3); i++) {
baseSpeciesPool.push(baseSpecies);
}
for (const baseSpecies of Object.keys(pokemonPool)) {
// Squawkabilly has 4 formes, but only 2 functionally different formes, so only include it 1x
const weight = (baseSpecies === 'Squawkabilly') ? 1 : Math.min(Math.ceil(pokemonPool[baseSpecies].length / 3), 3);
for (let i = 0; i < weight; i++) baseSpeciesPool.push(baseSpecies);
}
return [pokemonPool, baseSpeciesPool];
}
Expand Down Expand Up @@ -2474,12 +2477,7 @@ export class RandomGen8Teams {
const [pokemonPool, baseSpeciesPool] = this.getPokemonPool(type, pokemon, isMonotype, pokemonList);
while (baseSpeciesPool.length && pokemon.length < this.maxTeamSize) {
const baseSpecies = this.sampleNoReplace(baseSpeciesPool);
const currentSpeciesPool: Species[] = [];
for (const poke of pokemonPool) {
const species = this.dex.species.get(poke);
if (species.baseSpecies === baseSpecies) currentSpeciesPool.push(species);
}
let species = this.sample(currentSpeciesPool);
let species = this.dex.species.get(this.sample(pokemonPool[baseSpecies]));
if (!species.exists) continue;

// Limit to one of each species (Species Clause)
Expand Down
16 changes: 4 additions & 12 deletions data/mods/potd/random-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ export class RandomPOTDTeams extends RandomTeams {
const teamDetails: RandomTeamsTypes.TeamDetails = {};

const pokemonList = isDoubles ? Object.keys(this.randomDoublesSets) : Object.keys(this.randomSets);
const [pokemonPool, baseSpeciesPool] = this.getPokemonPool(type, pokemon, isMonotype, pokemonList);

// Remove PotD from baseSpeciesPool
if (baseSpeciesPool.includes(potd.baseSpecies)) {
this.fastPop(baseSpeciesPool, baseSpeciesPool.indexOf(potd.baseSpecies));
}
const [pokemonPool, baseSpeciesPool] = this.getPokemonPool(
type, pokemon, isMonotype, pokemonList.filter(m => this.dex.species.get(m).baseSpecies !== potd.baseSpecies)
);

// Add PotD to type counts
for (const typeName of potd.types) {
Expand All @@ -56,12 +53,7 @@ export class RandomPOTDTeams extends RandomTeams {

while (baseSpeciesPool.length && pokemon.length < this.maxTeamSize) {
const baseSpecies = this.sampleNoReplace(baseSpeciesPool);
const currentSpeciesPool: Species[] = [];
for (const poke of pokemonPool) {
const species = this.dex.species.get(poke);
if (species.baseSpecies === baseSpecies) currentSpeciesPool.push(species);
}
let species = this.sample(currentSpeciesPool);
let species = this.dex.species.get(this.sample(pokemonPool[baseSpecies]));
if (!species.exists) continue;

// Limit to one of each species (Species Clause)
Expand Down
30 changes: 13 additions & 17 deletions data/random-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1716,11 +1716,10 @@ export class RandomTeams {
pokemonToExclude: RandomTeamsTypes.RandomSet[] = [],
isMonotype = false,
pokemonList: string[]
) {
): [{[k: string]: ID[]}, string[]] {
const exclude = pokemonToExclude.map(p => toID(p.species));
const pokemonPool = [];
const pokemonPool: {[k: string]: ID[]} = {};
const baseSpeciesPool = [];
const baseSpeciesCount: {[k: string]: number} = {};
for (const pokemon of pokemonList) {
let species = this.dex.species.get(pokemon);
if (exclude.includes(species.id)) continue;
Expand All @@ -1731,16 +1730,18 @@ export class RandomTeams {
if (!species.types.includes(type)) continue;
}
}
pokemonPool.push(pokemon);
baseSpeciesCount[species.baseSpecies] = (baseSpeciesCount[species.baseSpecies] || 0) + 1;

if (species.baseSpecies in pokemonPool) {
pokemonPool[species.baseSpecies].push(species.id);
} else {
pokemonPool[species.baseSpecies] = [species.id];
}
}
// Include base species 1x if 1-3 formes, 2x if 4-6 formes, 3x if 7+ formes
for (const baseSpecies of Object.keys(baseSpeciesCount)) {
for (let i = 0; i < Math.min(Math.ceil(baseSpeciesCount[baseSpecies] / 3), 3); i++) {
baseSpeciesPool.push(baseSpecies);
// Squawkabilly has 4 formes, but only 2 functionally different formes, so only include it 1x
if (baseSpecies === 'Squawkabilly') break;
}
for (const baseSpecies of Object.keys(pokemonPool)) {
// Squawkabilly has 4 formes, but only 2 functionally different formes, so only include it 1x
const weight = (baseSpecies === 'Squawkabilly') ? 1 : Math.min(Math.ceil(pokemonPool[baseSpecies].length / 3), 3);
for (let i = 0; i < weight; i++) baseSpeciesPool.push(baseSpecies);
}
return [pokemonPool, baseSpeciesPool];
}
Expand Down Expand Up @@ -1779,12 +1780,7 @@ export class RandomTeams {
let leadsRemaining = this.format.gameType === 'doubles' ? 2 : 1;
while (baseSpeciesPool.length && pokemon.length < this.maxTeamSize) {
const baseSpecies = this.sampleNoReplace(baseSpeciesPool);
const currentSpeciesPool: Species[] = [];
for (const poke of pokemonPool) {
const species = this.dex.species.get(poke);
if (species.baseSpecies === baseSpecies) currentSpeciesPool.push(species);
}
let species = this.sample(currentSpeciesPool);
let species = this.dex.species.get(this.sample(pokemonPool[baseSpecies]));
if (!species.exists) continue;

// Limit to one of each species (Species Clause)
Expand Down

0 comments on commit dab8f0a

Please sign in to comment.