Skip to content

Commit

Permalink
Random Battles: Fix STAB in 4-player battles
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnikaCodes committed Apr 9, 2021
1 parent 9ee76d7 commit 2baca93
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion data/mods/gen7/random-teams.ts
Expand Up @@ -955,7 +955,7 @@ export class RandomGen7Teams extends RandomTeams {
if (this.format.gameType === 'multi') {
// Random Multi Battle uses doubles move pools, but Ally Switch fails in multi battles
const allySwitch = movePool.indexOf('allyswitch');
if (allySwitch !== undefined) {
if (allySwitch > -1) {

This comment has been minimized.

Copy link
@Zarel

Zarel Apr 10, 2021

Member

Convention is to always compare to 0 (>= 0, < 0) rather than comparing to -1. Not a big deal, but just for future reference.

This comment has been minimized.

Copy link
@AnnikaCodes

AnnikaCodes Apr 10, 2021

Author Collaborator

OK, good to know!

if (movePool.length > 4) {
this.fastPop(movePool, allySwitch);
} else {
Expand Down
7 changes: 6 additions & 1 deletion data/random-teams.ts
Expand Up @@ -224,6 +224,11 @@ export class RandomTeams {
// element at the given index with the removed element
// is much, much faster than using list.splice(index, 1).
const length = list.length;
if (index < 0 || index >= list.length) {
// sanity check
throw new Error(`Index ${index} out of bounds for given array`);
}

const element = list[index];
list[index] = list[length - 1];
list.pop();
Expand Down Expand Up @@ -1630,7 +1635,7 @@ export class RandomTeams {
// Random Multi Battle uses doubles move pools, but Ally Switch fails in multi battles
// Random Free-For-All also uses doubles move pools, for now
const allySwitch = movePool.indexOf('allyswitch');
if (allySwitch !== undefined) {
if (allySwitch > -1) {
if (movePool.length > 4) {
this.fastPop(movePool, allySwitch);
} else {
Expand Down
18 changes: 17 additions & 1 deletion test/random-battles/gen8.js
Expand Up @@ -3,7 +3,7 @@
*/
'use strict';

const {testSet, testNotBothMoves} = require('./tools');
const {testSet, testNotBothMoves, testHasSTAB} = require('./tools');
const assert = require('../assert');

describe('[Gen 8] Random Battle', () => {
Expand Down Expand Up @@ -39,6 +39,12 @@ describe('[Gen 8] Random Doubles Battle', () => {
assert(set.moves.includes('bodypress'), `Melmetal should get Body Press (got ${set.moves})`);
});
});

it('should enforce STAB on Pinsir, Pikachu, and Zygarde', () => {
for (const pkmn of ['pinsir', 'pikachu', 'zygarde']) {
testHasSTAB(pkmn, options);
}
});
});

describe('[Gen 8] Random Battle (No Dmax)', () => {
Expand All @@ -47,3 +53,13 @@ describe('[Gen 8] Random Battle (No Dmax)', () => {

// const options = {format: 'gen8randombattlenodmax', isDynamax: true};
});

describe('[Gen 8] Free-for-All Random Battle', () => {
const options = {format: 'gen8freeforallrandombattle', isDoubles: true};

it('should enforce STAB on Pinsir, Pikachu, and Zygarde', () => {
for (const pkmn of ['pinsir', 'pikachu', 'zygarde']) {
testHasSTAB(pkmn, options);
}
});
});
18 changes: 18 additions & 0 deletions test/random-battles/tools.js
Expand Up @@ -27,6 +27,23 @@ function testSet(pokemon, options, test) {
}
}

/**
* Tests that a Pokémon always gets STAB moves.
*
* @param {ID} pokemon
* @param {{format?: string, rounds?: number, isDoubles?: boolean, isLead?: boolean, isDynamax?: boolean}} options
*/
function testHasSTAB(pokemon, options) {
const dex = Dex.forFormat(options.format || 'gen8randombattle');
const types = dex.species.get(pokemon).types;
testSet(pokemon, options, set => {
assert(
set.moves.some(move => types.includes(dex.moves.get(move).type)),
`${pokemon} should have at least one STAB move (generated moveset: ${set.moves})`
);
});
}

/**
* Tests that a Pokémon does not get two moves together.
*
Expand Down Expand Up @@ -63,3 +80,4 @@ function testTeam(options, test) {
exports.testSet = testSet;
exports.testNotBothMoves = testNotBothMoves;
exports.testTeam = testTeam;
exports.testHasSTAB = testHasSTAB;

0 comments on commit 2baca93

Please sign in to comment.