Skip to content

Commit

Permalink
Move EffectState initialization handling to Battle functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pyuk-bot committed Jan 20, 2025
1 parent a460633 commit c387934
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 101 deletions.
12 changes: 5 additions & 7 deletions config/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ New sections will be added to the bottom of the specified column.
The column value will be ignored for repeat sections.
*/

import {EffectState} from '../sim/pokemon';

export const Formats: import('../sim/dex-formats').FormatList = [

// S/V Singles
Expand Down Expand Up @@ -730,7 +728,7 @@ export const Formats: import('../sim/dex-formats').FormatList = [
if (!format.getSharedPower) format = this.dex.formats.get('gen9sharedpower');
for (const ability of format.getSharedPower!(pokemon)) {
const effect = 'ability:' + ability;
pokemon.volatiles[effect] = new EffectState({id: this.toID(effect), target: pokemon}, this);
pokemon.volatiles[effect] = this.initEffectState({id: this.toID(effect), target: pokemon});
if (!pokemon.m.abils) pokemon.m.abils = [];
if (!pokemon.m.abils.includes(effect)) pokemon.m.abils.push(effect);
}
Expand Down Expand Up @@ -1464,14 +1462,14 @@ export const Formats: import('../sim/dex-formats').FormatList = [
if (!pokemon.m.innate && !BAD_ABILITIES.includes(this.toID(ally.ability))) {
pokemon.m.innate = 'ability:' + ally.ability;
if (!ngas || ally.getAbility().flags['cantsuppress'] || pokemon.hasItem('Ability Shield')) {
pokemon.volatiles[pokemon.m.innate] = new EffectState({id: pokemon.m.innate, target: pokemon}, this);
pokemon.volatiles[pokemon.m.innate] = this.initEffectState({id: pokemon.m.innate, target: pokemon});
pokemon.m.startVolatile = true;
}
}
if (!ally.m.innate && !BAD_ABILITIES.includes(this.toID(pokemon.ability))) {
ally.m.innate = 'ability:' + pokemon.ability;
if (!ngas || pokemon.getAbility().flags['cantsuppress'] || ally.hasItem('Ability Shield')) {
ally.volatiles[ally.m.innate] = new EffectState({id: ally.m.innate, target: ally}, this);
ally.volatiles[ally.m.innate] = this.initEffectState({id: ally.m.innate, target: ally});
ally.m.startVolatile = true;
}
}
Expand Down Expand Up @@ -1769,7 +1767,7 @@ export const Formats: import('../sim/dex-formats').FormatList = [
for (const item of format.getSharedItems!(pokemon)) {
if (pokemon.m.sharedItemsUsed.includes(item)) continue;
const effect = 'item:' + item;
pokemon.volatiles[effect] = new EffectState({id: this.toID(effect), target: pokemon}, this);
pokemon.volatiles[effect] = this.initEffectState({id: this.toID(effect), target: pokemon});
if (!pokemon.m.items) pokemon.m.items = [];
if (!pokemon.m.items.includes(effect)) pokemon.m.items.push(effect);
}
Expand Down Expand Up @@ -2587,7 +2585,7 @@ export const Formats: import('../sim/dex-formats').FormatList = [
if (!format.getSharedPower) format = this.dex.formats.get('gen9sharedpower');
for (const ability of format.getSharedPower!(pokemon)) {
const effect = 'ability:' + ability;
pokemon.volatiles[effect] = new EffectState({id: this.toID(effect), target: pokemon}, this);
pokemon.volatiles[effect] = this.initEffectState({id: this.toID(effect), target: pokemon});
if (!pokemon.m.abils) pokemon.m.abils = [];
if (!pokemon.m.abils.includes(effect)) pokemon.m.abils.push(effect);
}
Expand Down
4 changes: 2 additions & 2 deletions data/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ export const Items: import('../sim/dex-items').ItemDataTable = {
onDamagingHit(damage, target, source, move) {
this.add('-enditem', target, 'Air Balloon');
target.item = '';
target.itemState.clear();
this.clearEffectState(target.itemState);
this.runEvent('AfterUseItem', target, null, null, this.dex.items.get('airballoon'));
},
onAfterSubDamage(damage, target, source, effect) {
this.debug('effect: ' + effect.id);
if (effect.effectType === 'Move') {
this.add('-enditem', target, 'Air Balloon');
target.item = '';
target.itemState.clear();
this.clearEffectState(target.itemState);
this.runEvent('AfterUseItem', target, null, null, this.dex.items.get('airballoon'));
}
},
Expand Down
6 changes: 2 additions & 4 deletions data/mods/partnersincrime/moves.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {EffectState} from '../../../sim/pokemon';

export const Moves: import('../../../sim/dex-moves').ModdedMoveDataTable = {
gastroacid: {
inherit: true,
Expand Down Expand Up @@ -153,7 +151,7 @@ export const Moves: import('../../../sim/dex-moves').ModdedMoveDataTable = {
}

source.ability = targetAbility.id;
source.abilityState = new EffectState({id: this.toID(source.ability), target: source}, this);
source.abilityState = this.initEffectState({id: this.toID(source.ability), target: source});
if (source.m.innate && source.m.innate.endsWith(targetAbility.id)) {
source.removeVolatile(source.m.innate);
delete source.m.innate;
Expand All @@ -168,7 +166,7 @@ export const Moves: import('../../../sim/dex-moves').ModdedMoveDataTable = {
}

target.ability = sourceAbility.id;
target.abilityState = new EffectState({id: this.toID(target.ability), target: target}, this);
target.abilityState = this.initEffectState({id: this.toID(target.ability), target: target});
if (target.m.innate && target.m.innate.endsWith(sourceAbility.id)) {
target.removeVolatile(target.m.innate);
delete target.m.innate;
Expand Down
3 changes: 1 addition & 2 deletions data/mods/partnersincrime/scripts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Utils} from '../../../lib';
import {EffectState} from '../../../sim/pokemon';

export const Scripts: ModdedBattleScriptsData = {
gen: 9,
Expand Down Expand Up @@ -240,7 +239,7 @@ export const Scripts: ModdedBattleScriptsData = {
this.battle.dex.moves.get(this.battle.effect.id));
}
this.ability = ability.id;
this.abilityState = new EffectState({id: ability.id, target: this}, this.battle);
this.abilityState = this.battle.initEffectState({id: ability.id, target: this});
if (ability.id && this.battle.gen > 3) {
this.battle.singleEvent('Start', ability, this.abilityState, this, source);
if (ally && ally.ability !== this.ability) {
Expand Down
4 changes: 2 additions & 2 deletions data/mods/sharingiscaring/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const Items: import('../../../sim/dex-items').ModdedItemDataTable = {
this.add('-enditem', target, 'Air Balloon');
if (target.item === 'airballoon') {
target.item = '';
target.itemState.clear();
this.clearEffectState(target.itemState);
} else {
delete target.volatiles['item:airballoon'];
target.m.sharedItemsUsed.push('airballoon');
Expand All @@ -19,7 +19,7 @@ export const Items: import('../../../sim/dex-items').ModdedItemDataTable = {
this.add('-enditem', target, 'Air Balloon');
if (target.item === 'airballoon') {
target.item = '';
target.itemState.clear();
this.clearEffectState(target.itemState);
} else {
delete target.volatiles['item:airballoon'];
target.m.sharedItemsUsed.push('airballoon');
Expand Down
8 changes: 4 additions & 4 deletions data/mods/sharingiscaring/scripts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {EffectState, RESTORATIVE_BERRIES} from '../../../sim/pokemon';
import {RESTORATIVE_BERRIES} from "../../../sim/pokemon";

export const Scripts: ModdedBattleScriptsData = {
gen: 9,
Expand Down Expand Up @@ -60,7 +60,7 @@ export const Scripts: ModdedBattleScriptsData = {
} else {
this.lastItem = this.item;
this.item = '';
this.itemState.clear();
this.battle.clearEffectState(this.itemState);
}
this.usedItemThisTurn = true;
this.battle.runEvent('AfterUseItem', this, null, null, item);
Expand Down Expand Up @@ -104,7 +104,7 @@ export const Scripts: ModdedBattleScriptsData = {
} else {
this.lastItem = this.item;
this.item = '';
this.itemState.clear();
this.battle.clearEffectState(this.itemState);
}
this.usedItemThisTurn = true;
this.ateBerry = true;
Expand All @@ -129,7 +129,7 @@ export const Scripts: ModdedBattleScriptsData = {
const oldItem = this.getItem();
const oldItemState = this.itemState;
this.item = item.id;
this.itemState = new EffectState({id: item.id, target: this}, this.battle);
this.itemState = this.battle.initEffectState({id: item.id, target: this});
if (oldItem.exists) this.battle.singleEvent('End', oldItem, oldItemState, this);
if (item.id) {
this.battle.singleEvent('Start', item, this.itemState, this, source, effect);
Expand Down
6 changes: 2 additions & 4 deletions data/moves.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// List of flags and their descriptions can be found in sim/dex-moves.ts

import {EffectState} from '../sim/pokemon';

export const Moves: import('../sim/dex-moves').MoveDataTable = {
"10000000voltthunderbolt": {
num: 719,
Expand Down Expand Up @@ -17223,8 +17221,8 @@ export const Moves: import('../sim/dex-moves').MoveDataTable = {
this.singleEvent('End', targetAbility, target.abilityState, target);
source.ability = targetAbility.id;
target.ability = sourceAbility.id;
source.abilityState = new EffectState({id: this.toID(source.ability), target: source}, this);
target.abilityState = new EffectState({id: this.toID(target.ability), target: target}, this);
source.abilityState = this.initEffectState({id: this.toID(source.ability), target: source});
target.abilityState = this.initEffectState({id: this.toID(target.ability), target: target});
source.volatileStaleness = undefined;
if (!target.isAlly(source)) target.volatileStaleness = 'external';
this.singleEvent('Start', targetAbility, source.abilityState, source);
Expand Down
35 changes: 30 additions & 5 deletions sim/battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class Battle {
options.forceRandomChance : null;
this.deserialized = !!options.deserialized;
this.strictChoices = !!options.strictChoices;
this.formatData = new EffectState({id: format.id}, this);
this.formatData = this.initEffectState({id: format.id});
this.gameType = (format.gameType || 'singles');
this.field = new Field(this);
this.sides = Array(format.playerCount).fill(null) as any;
Expand Down Expand Up @@ -247,7 +247,7 @@ export class Battle {
this.ended = false;

this.effect = {id: ''} as Effect;
this.effectState = new EffectState({id: ''}, this);
this.effectState = this.initEffectState({id: ''});

this.event = {id: ''};
this.events = null;
Expand Down Expand Up @@ -592,7 +592,7 @@ export class Battle {
const parentEvent = this.event;

this.effect = effect;
this.effectState = state as EffectState || new EffectState({}, this);
this.effectState = state as EffectState || this.initEffectState({});
this.event = {id: eventid, target, source, effect: sourceEffect};
this.eventDepth++;

Expand Down Expand Up @@ -745,7 +745,7 @@ export class Battle {
if (callback !== undefined) {
if (Array.isArray(target)) throw new Error("");
handlers.unshift(this.resolvePriority({
effect: sourceEffect, callback, state: new EffectState({}, this), end: null, effectHolder: target,
effect: sourceEffect, callback, state: this.initEffectState({}), end: null, effectHolder: target,
}, `on${eventid}`));
}
}
Expand Down Expand Up @@ -858,7 +858,7 @@ export class Battle {
const parentEffect = this.effect;
const parentEffectState = this.effectState;
this.effect = handler.effect;
this.effectState = handler.state || new EffectState({}, this);
this.effectState = handler.state || this.initEffectState({});
this.effectState.target = effectHolder;

returnVal = handler.callback.apply(this, args);
Expand Down Expand Up @@ -3225,6 +3225,31 @@ export class Battle {
return this.gen >= 8 ? (this.turn - 1) % 256 : this.turn - 1;
}

initEffectState(obj: Partial<EffectState>, effectOrder?: number): EffectState {
if (!obj.id) obj.id = '';
if (effectOrder !== undefined) {
obj.effectOrder = effectOrder;
} else if (obj.id && obj.target && (!(obj.target instanceof Pokemon) || obj.target.isActive)) {
obj.effectOrder = this.effectOrder++;
} else {
obj.effectOrder = 0;
}
return obj as EffectState;
}

clearEffectState(state: EffectState) {
state.id = '';
for (const k in state) {
if (k === 'id' || k === 'target') {
continue;
} else if (k === 'effectOrder') {
state.effectOrder = 0;
} else {
delete state[k];
}
}
}

destroy() {
// deallocate ourself

Expand Down
18 changes: 9 additions & 9 deletions sim/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export class Field {
this.id = '';

this.weather = '';
this.weatherState = new EffectState({id: ''}, battle);
this.weatherState = this.battle.initEffectState({id: ''});
this.terrain = '';
this.terrainState = new EffectState({id: ''}, battle);
this.terrainState = this.battle.initEffectState({id: ''});
this.pseudoWeather = {};
}

Expand Down Expand Up @@ -67,7 +67,7 @@ export class Field {
const prevWeather = this.weather;
const prevWeatherState = this.weatherState;
this.weather = status.id;
this.weatherState = new EffectState({id: status.id}, this.battle);
this.weatherState = this.battle.initEffectState({id: status.id});
if (source) {
this.weatherState.source = source;
this.weatherState.sourceSlot = source.getSlot();
Expand All @@ -93,7 +93,7 @@ export class Field {
const prevWeather = this.getWeather();
this.battle.singleEvent('FieldEnd', prevWeather, this.weatherState, this);
this.weather = '';
this.weatherState.clear();
this.battle.clearEffectState(this.weatherState);
this.battle.eachEvent('WeatherChange');
return true;
}
Expand Down Expand Up @@ -138,12 +138,12 @@ export class Field {
const prevTerrain = this.terrain;
const prevTerrainState = this.terrainState;
this.terrain = status.id;
this.terrainState = new EffectState({
this.terrainState = this.battle.initEffectState({
id: status.id,
source,
sourceSlot: source.getSlot(),
duration: status.duration,
}, this.battle);
});
if (status.durationCallback) {
this.terrainState.duration = status.durationCallback.call(this.battle, source, source, sourceEffect);
}
Expand All @@ -161,7 +161,7 @@ export class Field {
const prevTerrain = this.getTerrain();
this.battle.singleEvent('FieldEnd', prevTerrain, this.terrainState, this);
this.terrain = '';
this.terrainState.clear();
this.battle.clearEffectState(this.terrainState);
this.battle.eachEvent('TerrainChange');
return true;
}
Expand Down Expand Up @@ -197,12 +197,12 @@ export class Field {
if (!(status as any).onFieldRestart) return false;
return this.battle.singleEvent('FieldRestart', status, state, this, source, sourceEffect);
}
state = this.pseudoWeather[status.id] = new EffectState({
state = this.pseudoWeather[status.id] = this.battle.initEffectState({
id: status.id,
source,
sourceSlot: source?.getSlot(),
duration: status.duration,
}, this.battle);
});
if (status.durationCallback) {
if (!source) throw new Error(`setting fieldcond without a source`);
state.duration = status.durationCallback.call(this.battle, source, source, sourceEffect);
Expand Down
Loading

0 comments on commit c387934

Please sign in to comment.