Consolidate limitedEgg code

This commit is contained in:
Guangcong Luo 2019-10-03 01:35:54 +10:00
parent 93fac102a3
commit a519717865

View File

@ -76,7 +76,7 @@ export class PokemonSources {
* which can only be bred from an event Dragonite, making it * which can only be bred from an event Dragonite, making it
* automatically incompatible with every other Dragonite egg move. * automatically incompatible with every other Dragonite egg move.
*/ */
limitedEgg?: (string | 'self')[]; limitedEgg?: (ID | 'self')[] | null;
isHidden: boolean | null; isHidden: boolean | null;
constructor(sourcesBefore = 0, sourcesAfter = 0) { constructor(sourcesBefore = 0, sourcesAfter = 0) {
@ -84,16 +84,21 @@ export class PokemonSources {
this.sourcesBefore = sourcesBefore; this.sourcesBefore = sourcesBefore;
this.sourcesAfter = sourcesAfter; this.sourcesAfter = sourcesAfter;
this.isHidden = null; this.isHidden = null;
this.limitedEgg = undefined;
} }
size() { size() {
if (this.sourcesBefore) return Infinity; if (this.sourcesBefore) return Infinity;
return this.sources.length; return this.sources.length;
} }
add(source: PokemonSource) { add(source: PokemonSource, limitedEgg?: ID | 'self') {
if (this.sources[this.sources.length - 1] !== source) this.sources.push(source); if (this.sources[this.sources.length - 1] !== source) this.sources.push(source);
if (limitedEgg && this.limitedEgg !== null) {
this.limitedEgg = [limitedEgg];
}
} }
addGen(sourceGen: number) { addGen(sourceGen: number) {
this.sourcesBefore = Math.max(this.sourcesBefore, sourceGen); this.sourcesBefore = Math.max(this.sourcesBefore, sourceGen);
this.limitedEgg = null;
} }
minSourceGen() { minSourceGen() {
if (this.sourcesBefore) return this.sourcesAfter || 1; if (this.sourcesBefore) return this.sourcesAfter || 1;
@ -153,6 +158,13 @@ export class PokemonSources {
this.restrictedMove = other.restrictedMove; this.restrictedMove = other.restrictedMove;
} }
} }
if (other.limitedEgg) {
if (!this.limitedEgg) {
this.limitedEgg = other.limitedEgg;
} else {
this.limitedEgg.push(...other.limitedEgg);
}
}
if (other.sourcesAfter > this.sourcesAfter) this.sourcesAfter = other.sourcesAfter; if (other.sourcesAfter > this.sourcesAfter) this.sourcesAfter = other.sourcesAfter;
if (other.isHidden) this.isHidden = true; if (other.isHidden) this.isHidden = true;
} }
@ -1515,11 +1527,6 @@ export class TeamValidator {
* (This is everything except in Gen 1 Tradeback) * (This is everything except in Gen 1 Tradeback)
*/ */
const noFutureGen = !ruleTable.has('allowtradeback'); const noFutureGen = !ruleTable.has('allowtradeback');
/**
* If a move can only be learned from a gen 2-5 egg, we have to check chainbreeding validity
* limitedEgg is false if there are any legal non-egg sources for the move, and true otherwise
*/
let limitedEgg = null;
let tradebackEligible = false; let tradebackEligible = false;
while (template && template.species && !alreadyChecked[template.speciesid]) { while (template && template.species && !alreadyChecked[template.speciesid]) {
@ -1606,7 +1613,7 @@ export class TeamValidator {
} else if ((!template.gender || template.gender === 'F') && learnedGen >= 2) { } else if ((!template.gender || template.gender === 'F') && learnedGen >= 2) {
// available as egg move // available as egg move
learned = learnedGen + 'Eany'; learned = learnedGen + 'Eany';
limitedEgg = false; moveSources.limitedEgg = null;
// falls through to E check below // falls through to E check below
} else { } else {
// this move is unavailable, skip it // this move is unavailable, skip it
@ -1626,7 +1633,6 @@ export class TeamValidator {
if (learned.charAt(1) === 'R') moveSources.restrictedMove = moveid; if (learned.charAt(1) === 'R') moveSources.restrictedMove = moveid;
limit1 = false; limit1 = false;
moveSources.addGen(learnedGen); moveSources.addGen(learnedGen);
limitedEgg = false;
} else if (learned.charAt(1) === 'E') { } else if (learned.charAt(1) === 'E') {
// egg moves: // egg moves:
// only if that was the source // only if that was the source
@ -1634,7 +1640,6 @@ export class TeamValidator {
// gen 6 doesn't have egg move incompatibilities except for certain cases with baby Pokemon // gen 6 doesn't have egg move incompatibilities except for certain cases with baby Pokemon
learned = learnedGen + 'E' + (template.prevo ? template.id : ''); learned = learnedGen + 'E' + (template.prevo ? template.id : '');
moveSources.add(learned); moveSources.add(learned);
limitedEgg = false;
continue; continue;
} }
// it's a past gen; egg moves can only be inherited from the father // it's a past gen; egg moves can only be inherited from the father
@ -1644,7 +1649,7 @@ export class TeamValidator {
if (eggGroups[0] === 'Undiscovered') eggGroups = dex.getTemplate(template.evos[0]).eggGroups; if (eggGroups[0] === 'Undiscovered') eggGroups = dex.getTemplate(template.evos[0]).eggGroups;
if (eggGroups[0] === 'Undiscovered' || !eggGroups.length) continue; if (eggGroups[0] === 'Undiscovered' || !eggGroups.length) continue;
let atLeastOne = false; let atLeastOne = false;
const fromSelf = (learned.substr(1) === 'Eany'); const levelUpEgg = (learned.substr(1) === 'Eany');
const eggGroupsSet = new Set(eggGroups); const eggGroupsSet = new Set(eggGroups);
learned = learned.substr(0, 2); learned = learned.substr(0, 2);
// loop through pokemon for possible fathers to inherit the egg move from // loop through pokemon for possible fathers to inherit the egg move from
@ -1660,11 +1665,11 @@ export class TeamValidator {
if (!father.learnset) continue; if (!father.learnset) continue;
// unless it's supposed to be self-breedable, can't inherit from self, prevos, evos, etc // unless it's supposed to be self-breedable, can't inherit from self, prevos, evos, etc
// only basic pokemon have egg moves, so by now all evolutions should be in alreadyChecked // only basic pokemon have egg moves, so by now all evolutions should be in alreadyChecked
if (!fromSelf && alreadyChecked[father.speciesid]) continue; if (!levelUpEgg && alreadyChecked[father.speciesid]) continue;
if (!fromSelf && father.prevo === template.id && template.evos.length === 1) continue; if (!levelUpEgg && father.prevo === template.id && template.evos.length === 1) continue;
// father must be able to learn the move // father must be able to learn the move
const fatherSources = father.learnset[moveid] || father.learnset['sketch']; const fatherSources = father.learnset[moveid] || father.learnset['sketch'];
if (!fromSelf && !fatherSources) continue; if (!levelUpEgg && !fatherSources) continue;
// must be able to breed with father // must be able to breed with father
if (!father.eggGroups.some(eggGroup => eggGroupsSet.has(eggGroup))) continue; if (!father.eggGroups.some(eggGroup => eggGroupsSet.has(eggGroup))) continue;
@ -1683,24 +1688,21 @@ export class TeamValidator {
atLeastOne = true; atLeastOne = true;
if (tradebackEligible && learnedGen === 2 && move.gen <= 1) { if (tradebackEligible && learnedGen === 2 && move.gen <= 1) {
// can tradeback // can tradeback
moveSources.add('1ET' + father.id); moveSources.add('1ET' + father.id, moveid);
} }
moveSources.add(learned + father.id); moveSources.add(learned + father.id, moveid);
if (limitedEgg !== false) limitedEgg = true;
} }
if (atLeastOne && noPastGenBreeding) { if (atLeastOne && noPastGenBreeding) {
// gen 6+ doesn't have egg move incompatibilities except for certain cases with baby Pokemon // gen 6+ doesn't have egg move incompatibilities except for certain cases with baby Pokemon
learned = learnedGen + 'E' + (template.prevo ? template.id : ''); learned = learnedGen + 'E' + (template.prevo ? template.id : '');
moveSources.add(learned); moveSources.add(learned);
limitedEgg = false;
continue; continue;
} }
// chainbreeding with itself // chainbreeding with itself
// e.g. ExtremeSpeed Dragonite // e.g. ExtremeSpeed Dragonite
if (!atLeastOne) { if (!atLeastOne) {
if (noPastGenBreeding) continue; if (noPastGenBreeding) continue;
moveSources.add(learned + template.id); moveSources.add(learned + template.id, 'self');
limitedEgg = 'self';
} }
} else if (learned.charAt(1) === 'S') { } else if (learned.charAt(1) === 'S') {
// event moves: // event moves:
@ -1788,14 +1790,6 @@ export class TeamValidator {
return {type: 'incompatible'}; return {type: 'incompatible'};
} }
if (limitedEgg) {
// pokemonSources.limitedEgg = [moveid] of egg moves with potential breeding incompatibilities
// 'self' is a possible entry (namely, ExtremeSpeed on Dragonite) meaning it's always
// incompatible with any other egg move
if (!setSources.limitedEgg) setSources.limitedEgg = [];
setSources.limitedEgg.push(limitedEgg === true ? moveid : limitedEgg);
}
if (babyOnly) setSources.babyOnly = babyOnly; if (babyOnly) setSources.babyOnly = babyOnly;
return null; return null;
} }