Refactor source files and add berries

This commit is contained in:
Flashfyre committed 2023-04-20 15:46:05 -04:00
1 parent 6135243641
commit 3546f3b5a7
103 files changed
+2598 -1171

No files matched your search

+267
View File
@@ -0,0 +1,267 @@
import { SelectModifierPhase } from "../battle-phases";
import BattleScene, { Button } from "../battle-scene";
import { ModifierTier, ModifierType, ModifierTypeOption, PokemonBaseStatBoosterModifierType, PokemonHpRestoreModifierType, PokemonReviveModifierType } from "../modifier/modifier-type";
import Pokemon, { AiType, EnemyPokemon, PlayerPokemon, PokemonMove } from "../pokemon";
import { Species } from "../data/species";
import { getTypeDamageMultiplier } from "../data/type";
import BattleMessageUiHandler from "../ui/battle-message-ui-handler";
import CommandUiHandler from "../ui/command-ui-handler";
import FightUiHandler from "../ui/fight-ui-handler";
import MessageUiHandler from "../ui/message-ui-handler";
import ModifierSelectUiHandler from "../ui/modifier-select-ui-handler";
import PartyUiHandler, { PartyUiMode } from "../ui/party-ui-handler";
import ConfirmUiHandler from "../ui/confirm-ui-handler";
import { Mode } from "../ui/ui";
export function initAutoPlay() {
const thisArg = this as BattleScene;
PlayerPokemon.prototype.getNextMove = EnemyPokemon.prototype.getNextMove;
const playerPokemon = this.getParty()[0] as PlayerPokemon;
const messageUiHandler = this.ui.handlers[Mode.MESSAGE] as BattleMessageUiHandler;
const commandUiHandler = this.ui.handlers[Mode.COMMAND] as CommandUiHandler;
const fightUiHandler = this.ui.handlers[Mode.FIGHT] as FightUiHandler;
const partyUiHandler = this.ui.handlers[Mode.PARTY] as PartyUiHandler;
const switchCheckUiHandler = this.ui.handlers[Mode.CONFIRM] as ConfirmUiHandler;
const modifierSelectUiHandler = this.ui.handlers[Mode.MODIFIER_SELECT] as ModifierSelectUiHandler;
const getBestPartyMemberIndex = () => {
const enemyPokemon = thisArg.getEnemyPokemon();
const party = thisArg.getParty();
let bestPartyMemberIndex = -1;
let bestPartyMemberEffectiveness = 0.5;
for (let p = 0; p < party.length; p++) {
const pokemon = party[p];
if (pokemon.getHpRatio() <= 0.2)
continue;
const effectiveness = enemyPokemon
? getMaxMoveEffectiveness(pokemon, enemyPokemon) / getMaxMoveEffectiveness(enemyPokemon, pokemon)
: 1;
if (effectiveness > bestPartyMemberEffectiveness) {
bestPartyMemberIndex = p;
bestPartyMemberEffectiveness = effectiveness;
}
if (enemyPokemon)
console.log(p, Species[pokemon.species.speciesId], '->', Species[enemyPokemon.species.speciesId], effectiveness);
}
if (bestPartyMemberIndex === -1) {
let highestHpValue = -1;
for (let p = 0; p < party.length; p++) {
const pokemon = party[p];
if (pokemon.hp > highestHpValue) {
highestHpValue = pokemon.hp;
bestPartyMemberIndex = p;
}
}
}
return bestPartyMemberIndex;
};
const getMaxMoveEffectiveness = (attacker: Pokemon, defender: Pokemon) => {
let maxEffectiveness = 0.5;
for (let m of attacker.moveset) {
const moveType = m.getMove().type;
const defenderTypes = defender.getTypes();
let effectiveness = getTypeDamageMultiplier(moveType, defenderTypes[0]);
if (defenderTypes.length > 1)
effectiveness *= getTypeDamageMultiplier(moveType, defenderTypes[1]);
if (effectiveness > maxEffectiveness)
maxEffectiveness = effectiveness;
}
return maxEffectiveness;
};
let nextPartyMemberIndex = -1;
const originalMessageUiHandlerShowText = MessageUiHandler.prototype.showText;
MessageUiHandler.prototype.showText = function (text: string, delay?: integer, callback?: Function, callbackDelay?: integer, prompt?: boolean, promptDelay?: integer) {
if (thisArg.auto) {
delay = 1;
callbackDelay = 0;
promptDelay = 0;
}
originalMessageUiHandlerShowText.apply(this, [ text, delay, callback, callbackDelay, prompt, promptDelay ]);
};
const originalMessageUiHandlerShowPrompt = MessageUiHandler.prototype.showPrompt;
MessageUiHandler.prototype.showPrompt = function (callback: Function, callbackDelay: integer) {
if (thisArg.auto)
callbackDelay = 0;
originalMessageUiHandlerShowPrompt.apply(this, [ callback, callbackDelay ]);
if (thisArg.auto)
thisArg.time.delayedCall(20, () => this.processInput(Button.ACTION));
};
const originalMessageUiHandlerPromptLevelUpStats = messageUiHandler.promptLevelUpStats;
messageUiHandler.promptLevelUpStats = function (partyMemberIndex: integer, prevStats: integer[], showTotals: boolean, callback?: Function) {
originalMessageUiHandlerPromptLevelUpStats.apply(this, [ partyMemberIndex, prevStats, showTotals, callback ]);
if (thisArg.auto)
thisArg.time.delayedCall(20, () => this.processInput(Button.ACTION));
};
const originalCommandUiHandlerShow = commandUiHandler.show;
commandUiHandler.show = function (args: any[]) {
originalCommandUiHandlerShow.apply(this, [ args ]);
if (thisArg.auto) {
thisArg.time.delayedCall(20, () => {
const bestPartyMemberIndex = getBestPartyMemberIndex();
if (bestPartyMemberIndex) {
console.log(bestPartyMemberIndex, thisArg.getParty())
console.log('Switching to ', Species[thisArg.getParty()[bestPartyMemberIndex].species.speciesId]);
nextPartyMemberIndex = bestPartyMemberIndex;
commandUiHandler.setCursor(2);
thisArg.time.delayedCall(20, () => this.processInput(Button.ACTION));
} else {
commandUiHandler.setCursor(0);
thisArg.time.delayedCall(20, () => this.processInput(Button.ACTION));
}
});
}
};
const originalFightUiHandlerShow = fightUiHandler.show;
fightUiHandler.show = function (args: any[]) {
originalFightUiHandlerShow.apply(this, [ args ]);
if (thisArg.auto) {
if (!playerPokemon.aiType)
playerPokemon.aiType = AiType.SMART;
thisArg.time.delayedCall(20, () => {
const nextMove = playerPokemon.getNextMove() as PokemonMove;
fightUiHandler.setCursor(playerPokemon.moveset.indexOf(nextMove));
thisArg.time.delayedCall(20, () => this.processInput(Button.ACTION));
});
}
};
const originalPartyUiHandlerShow = partyUiHandler.show;
partyUiHandler.show = function (args: any[]) {
originalPartyUiHandlerShow.apply(this, [ args ]);
if (thisArg.auto) {
thisArg.time.delayedCall(20, () => {
if (nextPartyMemberIndex === -1)
nextPartyMemberIndex = getBestPartyMemberIndex();
partyUiHandler.setCursor(nextPartyMemberIndex);
nextPartyMemberIndex = -1;
if (partyUiHandler.partyUiMode === PartyUiMode.MODIFIER || partyUiHandler.getCursor()) {
this.processInput(Button.ACTION);
thisArg.time.delayedCall(250, () => this.processInput(Button.ACTION));
} else
this.processInput(Button.CANCEL);
});
}
};
const originalSwitchCheckUiHandlerShow = switchCheckUiHandler.show;
switchCheckUiHandler.show = function (args: any[]) {
originalSwitchCheckUiHandlerShow.apply(this, [ args ]);
if (thisArg.auto) {
const bestPartyMemberIndex = getBestPartyMemberIndex();
thisArg.time.delayedCall(20, () => {
if (bestPartyMemberIndex)
nextPartyMemberIndex = bestPartyMemberIndex;
switchCheckUiHandler.setCursor(bestPartyMemberIndex ? 1 : 0);
thisArg.time.delayedCall(20, () => this.processInput(Button.ACTION));
});
}
}
const tryGetBestModifier = (modifierTypeOptions: Array<ModifierTypeOption>, predicate: Function) => {
for (let mt = 0; mt < modifierTypeOptions.length; mt++) {
const modifierTypeOption = modifierTypeOptions[mt];
if (predicate(modifierTypeOption.type)) {
return mt;
}
}
return -1;
};
const originalModifierSelectUiHandlerShow = modifierSelectUiHandler.show;
modifierSelectUiHandler.show = function (args: any[]) {
if (!thisArg.auto) {
originalModifierSelectUiHandlerShow.apply(this, [ args ]);
return;
}
if (modifierSelectUiHandler.active)
return;
thisArg.time.delayedCall(20, () => {
originalModifierSelectUiHandlerShow.apply(this, [ args ]);
const party = thisArg.getParty();
const modifierTypeOptions = modifierSelectUiHandler.options.map(o => o.modifierTypeOption);
const faintedPartyMemberIndex = party.findIndex(p => !p.hp);
const lowHpPartyMemberIndex = party.findIndex(p => p.getHpRatio() <= 0.5);
const criticalHpPartyMemberIndex = party.findIndex(p => p.getHpRatio() <= 0.25);
let optionIndex = tryGetBestModifier(modifierTypeOptions, (modifierType: ModifierType) => {
if (modifierType instanceof PokemonHpRestoreModifierType) {
if (modifierType instanceof PokemonReviveModifierType) {
if (faintedPartyMemberIndex > -1) {
nextPartyMemberIndex = faintedPartyMemberIndex;
return true;
}
} else if (criticalHpPartyMemberIndex > -1){
nextPartyMemberIndex = criticalHpPartyMemberIndex;
return true;
}
}
});
if (optionIndex === -1) {
optionIndex = tryGetBestModifier(modifierTypeOptions, (modifierType: ModifierType) => {
if (modifierType.tier >= ModifierTier.ULTRA) {
nextPartyMemberIndex = 0;
return true;
}
});
}
if (optionIndex === -1) {
optionIndex = tryGetBestModifier(modifierTypeOptions, (modifierType: ModifierType) => {
if (modifierType instanceof PokemonBaseStatBoosterModifierType) {
nextPartyMemberIndex = 0;
return true;
}
});
}
if (optionIndex === -1) {
optionIndex = tryGetBestModifier(modifierTypeOptions, (modifierType: ModifierType) => {
if (lowHpPartyMemberIndex && modifierType instanceof PokemonHpRestoreModifierType && !(ModifierType instanceof PokemonReviveModifierType)) {
nextPartyMemberIndex = lowHpPartyMemberIndex;
return true;
}
});
}
if (optionIndex === -1)
optionIndex = 0;
const trySelectModifier = () => {
modifierSelectUiHandler.setCursor(optionIndex);
thisArg.time.delayedCall(20, () => {
modifierSelectUiHandler.processInput(Button.ACTION);
thisArg.time.delayedCall(250, () => {
console.log(modifierTypeOptions[optionIndex]?.type.name);
if (thisArg.getCurrentPhase() instanceof SelectModifierPhase) {
if (optionIndex < modifierSelectUiHandler.options.length - 1) {
optionIndex++;
thisArg.time.delayedCall(250, () => trySelectModifier());
} else
modifierSelectUiHandler.processInput(Button.CANCEL);
}
});
});
};
thisArg.time.delayedCall(4000, () => trySelectModifier());
});
}
}
+293
View File
@@ -0,0 +1,293 @@
import BattleScene from "../battle-scene";
import { Gender } from "../data/gender";
import Pokemon from "../pokemon";
import { pokemonPrevolutions } from "../data/pokemon-evolutions";
import PokemonSpecies, { allSpecies } from "../data/pokemon-species";
import { Species } from "../data/species";
import * as Utils from "../utils";
interface SaveData {
trainerId: integer;
secretId: integer;
dexData: DexData;
}
export interface DexData {
[key: integer]: DexData | DexEntry
}
export interface DexEntry {
seen: boolean;
caught: boolean;
}
export interface DexEntryDetails {
shiny: boolean;
formIndex: integer;
female: boolean;
entry: DexEntry;
}
export interface StarterDexUnlockTree {
shiny: boolean | Map<boolean, StarterDexUnlockTree>
formIndex: integer | Map<integer, StarterDexUnlockTree>
female: boolean | Map<boolean, StarterDexUnlockTree>
key: string,
entry: DexEntry
}
export class GameData {
private scene: BattleScene;
public trainerId: integer;
public secretId: integer;
public dexData: DexData;
constructor(scene: BattleScene) {
this.scene = scene;
this.trainerId = Utils.randInt(65536);
this.secretId = Utils.randInt(65536);
if (!this.load())
this.initDexData();
}
private save(): boolean {
if (this.scene.quickStart)
return false;
const data: SaveData = {
trainerId: this.trainerId,
secretId: this.secretId,
dexData: this.dexData
};
localStorage.setItem('data', btoa(JSON.stringify(data)));
return true;
}
private load(): boolean {
if (!localStorage.getItem('data'))
return false;
const data = JSON.parse(atob(localStorage.getItem('data'))) as SaveData;
console.log(data);
this.trainerId = data.trainerId;
this.secretId = data.secretId;
this.dexData = data.dexData;
return true;
}
private initDexData() {
const data: DexData = {};
const initDexSubData = (dexData: DexData, count: integer): DexData[] => {
const ret: DexData[] = [];
for (let i = 0; i < count; i++) {
const newData: DexData = {};
dexData[i] = newData;
ret.push(newData);
}
return ret;
};
const initDexEntries = (dexData: DexData, count: integer): DexEntry[] => {
const ret: DexEntry[] = [];
for (let i = 0; i < count; i++) {
const entry: DexEntry = { seen: false, caught: false };
dexData[i] = entry;
ret.push(entry);
}
return ret;
};
for (let species of allSpecies) {
data[species.speciesId] = {};
if (species.forms?.length)
initDexSubData(data[species.speciesId] as DexData, 2).map(sd => species.malePercent !== null ? initDexSubData(sd, species.forms.length).map(fd => initDexEntries(fd, 2)) : initDexEntries(sd, species.forms.length));
else if (species.malePercent !== null)
initDexSubData(data[species.speciesId] as DexData, 2).map(sd => initDexEntries(sd, 2));
else
initDexEntries(data[species.speciesId] as DexData, 2)
}
const defaultStarters: Species[] = [
Species.BULBASAUR, Species.CHARMANDER, Species.SQUIRTLE,
Species.CHIKORITA, Species.CYNDAQUIL, Species.TOTODILE,
Species.TREECKO, Species.TORCHIC, Species.MUDKIP,
Species.TURTWIG, Species.CHIMCHAR, Species.PIPLUP,
Species.SNIVY, Species.TEPIG, Species.OSHAWOTT
];
for (let ds of defaultStarters) {
let entry = data[ds][0][Gender.MALE] as DexEntry;
entry.seen = true;
entry.caught = true;
}
this.dexData = data;
}
setPokemonSeen(pokemon: Pokemon): void {
const dexEntry = this.getPokemonDexEntry(pokemon);
if (!dexEntry.seen) {
dexEntry.seen = true;
this.save();
}
}
setPokemonCaught(pokemon: Pokemon): Promise<void> {
return new Promise(resolve => {
const dexEntry = this.getPokemonDexEntry(pokemon);
if (!dexEntry.caught) {
const newCatch = !this.getDefaultDexEntry(pokemon.species);
dexEntry.caught = true;
this.save();
if (newCatch && !pokemonPrevolutions.hasOwnProperty(pokemon.species.speciesId)) {
this.scene.playSoundWithoutBgm('level_up_fanfare', 1500);
this.scene.ui.showText(`${pokemon.name} has been\nadded as a starter!`, null, () => resolve(), null, true);
return;
}
}
resolve();
});
}
getPokemonDexEntry(pokemon: Pokemon) {
return this.getDexEntry(pokemon.species, pokemon.shiny, pokemon.formIndex, pokemon.gender === Gender.FEMALE);
}
getDexEntry(species: PokemonSpecies, shiny: boolean, formIndex: integer, female: boolean): DexEntry {
const shinyIndex = !shiny ? 0 : 1;
const genderIndex = !female ? 0 : 1;
const data = this.dexData[species.speciesId];
if (species.forms?.length) {
if (species.malePercent !== null)
return data[shinyIndex][formIndex][genderIndex];
return data[shinyIndex][formIndex];
} else if (species.malePercent !== null)
return data[shinyIndex][genderIndex];
return data[shinyIndex] as DexEntry;
}
getDefaultDexEntry(species: PokemonSpecies, forceShiny?: boolean, forceFormIndex?: integer, forceFemale?: boolean): DexEntryDetails {
const hasForms = !!species.forms?.length;
let shiny = false;
let formIndex = 0;
let female = false;
let entry = null;
const traverseData = (data: DexData, level: integer) => {
const keys = Object.keys(data);
keys.forEach((key: string, k: integer) => {
if (entry)
return;
switch (level) {
case 0:
shiny = !!k;
if (forceShiny !== undefined && shiny !== forceShiny)
return;
break;
case 1:
formIndex = k;
if (forceFormIndex !== undefined && formIndex !== forceFormIndex)
return;
break;
case 2:
female = !!k;
if (forceFemale !== undefined && female !== forceFemale)
return
break;
}
if ('caught' in data[key]) {
if (data[key].caught)
entry = data[key] as DexEntry;
} else
traverseData(data[key] as DexData, level + (hasForms ? 1 : 2));
});
};
traverseData(this.dexData[species.speciesId] as DexData, 0);
if (entry) {
return {
shiny: shiny,
formIndex: formIndex,
female: female,
entry: entry
};
}
return null;
}
getStarterDexUnlockTree(species: PokemonSpecies): StarterDexUnlockTree {
const hasForms = !!species.forms?.length;
const hasGender = species.malePercent !== null;
const getTreeOrValueMap = (key: string, parent?: StarterDexUnlockTree): (Map<any, any>) => {
switch (key) {
case 'shiny':
const shinyMap = new Map<boolean, StarterDexUnlockTree>();
for (let s = 0; s < 2; s++) {
const props = { shiny: !!s, formIndex: null };
shinyMap.set(!!s, {
shiny: !!s,
formIndex: hasForms ? getTreeOrValueMap('formIndex', props as StarterDexUnlockTree) : null,
female: !hasForms && hasGender ? getTreeOrValueMap('female', props as StarterDexUnlockTree) : null,
key: hasForms ? 'formIndex' : hasGender ? 'female' : 'entry',
entry: hasForms || hasGender ? null : this.dexData[species.speciesId][!s ? 0 : 1]
});
}
return shinyMap;
case 'formIndex':
const formMap = new Map<integer, StarterDexUnlockTree>();
for (let f = 0; f < species.forms.length; f++) {
const props = { shiny: parent.shiny, formIndex: f };
formMap.set(f, {
shiny: parent.shiny,
formIndex: f,
female: hasGender ? getTreeOrValueMap('female', props as StarterDexUnlockTree) : null,
key: hasGender ? 'female' : 'entry',
entry: hasGender ? null : this.dexData[species.speciesId][!parent.shiny ? 0 : 1][f]
});
}
return formMap;
case 'female':
const genderMap = new Map<boolean, StarterDexUnlockTree>();
for (let g = 0; g < 2; g++) {
genderMap.set(!!g, {
shiny: parent.shiny,
formIndex: parent.formIndex,
female: !!g,
key: 'entry',
entry: hasForms
? this.dexData[species.speciesId][!parent.shiny ? 0 : 1][parent.formIndex as integer][g]
: this.dexData[species.speciesId][!parent.shiny ? 0 : 1][g]
});
}
return genderMap;
}
};
const root = {
shiny: getTreeOrValueMap('shiny'),
formIndex: null,
female: null,
key: 'shiny',
entry: null
};
return root;
}
}
+35
View File
@@ -0,0 +1,35 @@
import BattleScene from "../battle-scene";
import * as Utils from "../utils";
export function initGameSpeed() {
const thisArg = this as BattleScene;
const transformValue = (value: number | Utils.FixedInt): number => {
if (value instanceof Utils.FixedInt)
return (value as Utils.FixedInt).value;
return thisArg.gameSpeed === 1 ? value : Math.ceil(value /= thisArg.gameSpeed);
};
const originalAddEvent = this.time.addEvent;
this.time.addEvent = function (config: Phaser.Time.TimerEvent | Phaser.Types.Time.TimerEventConfig) {
if (config.delay)
config.delay = transformValue(config.delay);
return originalAddEvent.apply(this, [ config ]);
};
const originalTweensAdd = this.tweens.add;
this.tweens.add = function (config: Phaser.Types.Tweens.TweenBuilderConfig | object) {
if (config.duration)
config.duration = transformValue(config.duration);
if (config.delay)
config.delay = transformValue(config.delay);
return originalTweensAdd.apply(this, [ config ]);
};
const originalAddCounter = this.tweens.addCounter;
this.tweens.addCounter = function (config: Phaser.Types.Tweens.NumberTweenBuilderConfig) {
if (config.duration)
config.duration = transformValue(config.duration);
if (config.delay)
config.delay = transformValue(config.delay);
return originalAddCounter.apply(this, [ config ]);
};
}