File size: 10,030 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var battle_queue_exports = {};
__export(battle_queue_exports, {
  BattleQueue: () => BattleQueue,
  default: () => battle_queue_default
});
module.exports = __toCommonJS(battle_queue_exports);
/**
 * Simulator Battle Action Queue
 * Pokemon Showdown - http://pokemonshowdown.com/
 *
 * The action queue is the core of the battle simulation. A rough overview of
 * the core battle loop:
 *
 * - chosen moves/switches are added to the action queue
 * - the action queue is sorted in speed/priority order
 * - we go through the action queue
 * - repeat
 *
 * @license MIT
 */
class BattleQueue {
  constructor(battle) {
    this.battle = battle;
    this.list = [];
    const queueScripts = battle.format.queue || battle.dex.data.Scripts.queue;
    if (queueScripts)
      Object.assign(this, queueScripts);
  }
  shift() {
    return this.list.shift();
  }
  peek(end) {
    return this.list[end ? this.list.length - 1 : 0];
  }
  push(action) {
    return this.list.push(action);
  }
  unshift(action) {
    return this.list.unshift(action);
  }
  [Symbol.iterator]() {
    return this.list[Symbol.iterator]();
  }
  entries() {
    return this.list.entries();
  }
  /**
   * Takes an ActionChoice, and fills it out into a full Action object.
   *
   * Returns an array of Actions because some ActionChoices (like mega moves)
   * resolve to two Actions (mega evolution + use move)
   */
  resolveAction(action, midTurn = false) {
    if (!action)
      throw new Error(`Action not passed to resolveAction`);
    if (action.choice === "pass")
      return [];
    const actions = [action];
    if (!action.side && action.pokemon)
      action.side = action.pokemon.side;
    if (!action.move && action.moveid)
      action.move = this.battle.dex.getActiveMove(action.moveid);
    if (!action.order) {
      const orders = {
        team: 1,
        start: 2,
        instaswitch: 3,
        beforeTurn: 4,
        beforeTurnMove: 5,
        revivalblessing: 6,
        runSwitch: 101,
        switch: 103,
        megaEvo: 104,
        megaEvoX: 104,
        megaEvoY: 104,
        runDynamax: 105,
        terastallize: 106,
        priorityChargeMove: 107,
        shift: 200,
        // default is 200 (for moves)
        residual: 300
      };
      if (action.choice in orders) {
        action.order = orders[action.choice];
      } else {
        action.order = 200;
        if (!["move", "event"].includes(action.choice)) {
          throw new Error(`Unexpected orderless action ${action.choice}`);
        }
      }
    }
    if (!midTurn) {
      if (action.choice === "move") {
        if (!action.maxMove && !action.zmove && action.move.beforeTurnCallback) {
          actions.unshift(...this.resolveAction({
            choice: "beforeTurnMove",
            pokemon: action.pokemon,
            move: action.move,
            targetLoc: action.targetLoc
          }));
        }
        if (action.mega && !action.pokemon.isSkyDropped()) {
          actions.unshift(...this.resolveAction({
            choice: "megaEvo",
            pokemon: action.pokemon
          }));
        }
        if (action.megax && !action.pokemon.isSkyDropped()) {
          actions.unshift(...this.resolveAction({
            choice: "megaEvoX",
            pokemon: action.pokemon
          }));
        }
        if (action.megay && !action.pokemon.isSkyDropped()) {
          actions.unshift(...this.resolveAction({
            choice: "megaEvoY",
            pokemon: action.pokemon
          }));
        }
        if (action.terastallize && !action.pokemon.terastallized) {
          actions.unshift(...this.resolveAction({
            choice: "terastallize",
            pokemon: action.pokemon
          }));
        }
        if (action.maxMove && !action.pokemon.volatiles["dynamax"]) {
          actions.unshift(...this.resolveAction({
            choice: "runDynamax",
            pokemon: action.pokemon
          }));
        }
        if (!action.maxMove && !action.zmove && action.move.priorityChargeCallback) {
          actions.unshift(...this.resolveAction({
            choice: "priorityChargeMove",
            pokemon: action.pokemon,
            move: action.move
          }));
        }
        action.fractionalPriority = this.battle.runEvent("FractionalPriority", action.pokemon, null, action.move, 0);
      } else if (["switch", "instaswitch"].includes(action.choice)) {
        if (typeof action.pokemon.switchFlag === "string") {
          action.sourceEffect = this.battle.dex.moves.get(action.pokemon.switchFlag);
        }
        action.pokemon.switchFlag = false;
      }
    }
    const deferPriority = this.battle.gen === 7 && action.mega && action.mega !== "done";
    if (action.move) {
      let target = null;
      action.move = this.battle.dex.getActiveMove(action.move);
      if (!action.targetLoc) {
        target = this.battle.getRandomTarget(action.pokemon, action.move);
        if (target)
          action.targetLoc = action.pokemon.getLocOf(target);
      }
      action.originalTarget = action.pokemon.getAtLoc(action.targetLoc);
    }
    if (!deferPriority)
      this.battle.getActionSpeed(action);
    return actions;
  }
  /**
   * Makes the passed action happen next (skipping speed order).
   */
  prioritizeAction(action, sourceEffect) {
    for (const [i, curAction] of this.list.entries()) {
      if (curAction === action) {
        this.list.splice(i, 1);
        break;
      }
    }
    action.sourceEffect = sourceEffect;
    action.order = 3;
    this.list.unshift(action);
  }
  /**
   * Changes a pokemon's action, and inserts its new action
   * in priority order.
   *
   * You'd normally want the OverrideAction event (which doesn't
   * change priority order).
   */
  changeAction(pokemon, action) {
    this.cancelAction(pokemon);
    if (!action.pokemon)
      action.pokemon = pokemon;
    this.insertChoice(action);
  }
  addChoice(choices) {
    if (!Array.isArray(choices))
      choices = [choices];
    for (const choice of choices) {
      const resolvedChoices = this.resolveAction(choice);
      this.list.push(...resolvedChoices);
      for (const resolvedChoice of resolvedChoices) {
        if (resolvedChoice && resolvedChoice.choice === "move" && resolvedChoice.move.id !== "recharge") {
          resolvedChoice.pokemon.side.lastSelectedMove = resolvedChoice.move.id;
        }
      }
    }
  }
  willAct() {
    for (const action of this.list) {
      if (["move", "switch", "instaswitch", "shift"].includes(action.choice)) {
        return action;
      }
    }
    return null;
  }
  willMove(pokemon) {
    if (pokemon.fainted)
      return null;
    for (const action of this.list) {
      if (action.choice === "move" && action.pokemon === pokemon) {
        return action;
      }
    }
    return null;
  }
  cancelAction(pokemon) {
    const oldLength = this.list.length;
    for (let i = 0; i < this.list.length; i++) {
      if (this.list[i].pokemon === pokemon) {
        this.list.splice(i, 1);
        i--;
      }
    }
    return this.list.length !== oldLength;
  }
  cancelMove(pokemon) {
    for (const [i, action] of this.list.entries()) {
      if (action.choice === "move" && action.pokemon === pokemon) {
        this.list.splice(i, 1);
        return true;
      }
    }
    return false;
  }
  willSwitch(pokemon) {
    for (const action of this.list) {
      if (["switch", "instaswitch"].includes(action.choice) && action.pokemon === pokemon) {
        return action;
      }
    }
    return null;
  }
  /**
   * Inserts the passed action into the action queue when it normally
   * would have happened (sorting by priority/speed), without
   * re-sorting the existing actions.
   */
  insertChoice(choices, midTurn = false) {
    if (Array.isArray(choices)) {
      for (const choice2 of choices) {
        this.insertChoice(choice2);
      }
      return;
    }
    const choice = choices;
    if (choice.pokemon) {
      choice.pokemon.updateSpeed();
    }
    const actions = this.resolveAction(choice, midTurn);
    let firstIndex = null;
    let lastIndex = null;
    for (const [i, curAction] of this.list.entries()) {
      const compared = this.battle.comparePriority(actions[0], curAction);
      if (compared <= 0 && firstIndex === null) {
        firstIndex = i;
      }
      if (compared < 0) {
        lastIndex = i;
        break;
      }
    }
    if (firstIndex === null) {
      this.list.push(...actions);
    } else {
      if (lastIndex === null)
        lastIndex = this.list.length;
      const index = firstIndex === lastIndex ? firstIndex : this.battle.random(firstIndex, lastIndex + 1);
      this.list.splice(index, 0, ...actions);
    }
  }
  clear() {
    this.list = [];
  }
  debug(action) {
    if (action) {
      return `${action.order || ""}:${action.priority || ""}:${action.speed || ""}:${action.subOrder || ""} - ${action.choice}${action.pokemon ? " " + action.pokemon : ""}${action.move ? " " + action.move : ""}`;
    }
    return this.list.map(
      (queueAction) => this.debug(queueAction)
    ).join("\n") + "\n";
  }
  sort() {
    this.battle.speedSort(this.list);
    return this;
  }
}
var battle_queue_default = BattleQueue;
//# sourceMappingURL=battle-queue.js.map