forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimulation-test-1.ts
More file actions
1024 lines (946 loc) · 41.4 KB
/
Simulation-test-1.ts
File metadata and controls
1024 lines (946 loc) · 41.4 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Battle Stream Example
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Example of how to create AIs battling against each other.
* Run this using `node build && node dist/sim/examples/simulation-test-1`.
*
* @license MIT
* @author Guangcong Luo <guangcongluo@gmail.com>
*/
import {BattleStream, getPlayerStreams, Teams} from '..';
import {RandomPlayerAI} from '../tools/random-player-ai';
import * as fs from 'fs';
import {ObjectReadWriteStream} from '../../lib/streams';
import {Dex, toID} from '../dex';
import {PRNG, PRNGSeed} from '../prng';
import {AIOptions, Runner} from '../tools/runner';
import { PokemonSet } from '../teams';
import { Field } from '../field';
import { PassThrough } from 'stream';
interface trackerPokemon {
species: string | undefined
currentHp: number
currentHpPercent: number
boosts: {}
stats: {}
moves: [],
n_remaining_mons: number,
sideConditions: {}
firstTurn: number
protectCount: number
FutureSightCounter: number
teraType: string | undefined
}
// a class for tracking useful information about the pokemon currently on the field
// note this only currently works for singles battles, though could be modified to work for doubles
class activeTracker {
private _p1_active: trackerPokemon;
private _p2_active: trackerPokemon;
constructor() {
this._p1_active = {
species: undefined,
currentHp: 0,
currentHpPercent: 0,
boosts: {},
stats: {},
moves: [],
n_remaining_mons: 0,
sideConditions: {},
firstTurn: 0,
protectCount: 0,
FutureSightCounter: 0,
teraType: undefined
};
this._p2_active = {
species: undefined,
currentHp: 0,
currentHpPercent: 0,
boosts: {},
stats: {},
moves: [],
n_remaining_mons: 0,
sideConditions: {},
firstTurn: 0,
protectCount: 0,
FutureSightCounter: 0,
teraType: undefined
};
}
get p1_active(): trackerPokemon{
return this._p1_active;
}
set p1_active(pokemon: trackerPokemon) {
this._p1_active = pokemon;
}
get p2_active(): trackerPokemon {
return this._p2_active;
}
set p2_active(pokemon: trackerPokemon ) {
this._p2_active = pokemon;
}
}
/**
Structure of 'request' in random-player-ai, pokemon will have however many pokemon objects as there are on the team
{
active: [ { moves: [Array] } ],
side: { name: 'Bot 1', id: 'p1', pokemon: [ [Object], [Object] ], sideConditions: {} }
}
Structure of request.side.pokemon
i.e. can use request.side.pokemon.condition to get current hp
{
ident: 'p1: Calyrex',
details: 'Calyrex-Ice',
condition: '341/341',
active: true,
stats: { atk: 429, def: 337, spa: 185, spd: 296, spe: 218 },
moves: [ 'substitute', 'glaciallance', 'swordsdance', 'leechseed' ],
baseAbility: 'asoneglastrier',
item: 'leftovers',
pokeball: 'pokeball',
ability: 'asoneglastrier'
boosts: { atk: 0, def: 0, spa: 0, spd: 0, spe: 0, accuracy: 0, evasion: 0 },
trapped: false,
},
Structure of request.side.foe.pokemon
[
{
m: {},
baseSpecies: '[Species:darmanitangalar]',
species: '[Species:darmanitangalar]',
speciesState: { id: 'darmanitangalar' },
gender: 'M',
dynamaxLevel: 10,
gigantamax: false,
moveSlots: [ [Object], [Object], [Object], [Object] ],
position: 0,
details: 'Darmanitan-Galar, M',
status: '',
statusState: {},
volatiles: {},
hpType: 'Dark',
hpPower: 60,
baseHpType: 'Dark',
baseHpPower: 60,
baseStoredStats: { atk: 379, def: 146, spa: 86, spd: 147, spe: 317, hp: 351 },
storedStats: { atk: 379, def: 146, spa: 86, spd: 147, spe: 317 },
boosts: { atk: 0, def: 0, spa: 0, spd: 0, spe: 0, accuracy: 0, evasion: 0 },
baseAbility: 'gorillatactics',
ability: 'gorillatactics',
abilityState: { id: 'gorillatactics', choiceLock: '', target: '[Pokemon:p1a]' },
item: 'choicescarf',
itemState: { id: 'choicescarf', target: '[Pokemon:p1a]' },
lastItem: '',
usedItemThisTurn: false,
ateBerry: false,
trapped: false,
maybeTrapped: false,
maybeDisabled: false,
illusion: null,
transformed: false,
fainted: false,
faintQueued: false,
subFainted: null,
types: [ 'Ice' ],
addedType: '',
knownType: true,
apparentType: 'Ice',
switchFlag: false,
forceSwitchFlag: false,
skipBeforeSwitchOutEventFlag: false,
draggedIn: null,
newlySwitched: false,
beingCalledBack: false,
lastMove: null,
lastMoveUsed: null,
moveThisTurn: '',
statsRaisedThisTurn: false,
statsLoweredThisTurn: false,
hurtThisTurn: null,
lastDamage: 0,
attackedBy: [],
isActive: true,
activeTurns: 1,
activeMoveActions: 0,
previouslySwitchedIn: 1,
truantTurn: false,
isStarted: true,
duringMove: false,
weighthg: 1200,
speed: 475,
abilityOrder: 0,
canMegaEvo: null,
canUltraBurst: null,
canGigantamax: null,
maxhp: 351,
baseMaxhp: 351,
hp: 351,
set: {
name: 'Darmanitan',
species: 'Darmanitan-Galar',
item: 'Choice Scarf',
ability: 'Gorilla Tactics',
gender: '',
nature: 'Jolly',
evs: [Object],
ivs: [Object],
level: 100,
moves: [Array]
}
},
Structure of Dex.species.get("pokemon name")
Species {
exists: true,
tags: [ 'Restricted Legendary' ],
num: 898,
name: 'Calyrex-Ice',
baseSpecies: 'Calyrex',
forme: 'Ice',
types: [ 'Psychic', 'Ice' ],
gender: 'N',
baseStats: { hp: 100, atk: 165, def: 150, spa: 85, spd: 130, spe: 50 },
abilities: { '0': 'As One (Glastrier)' },
heightm: 2.4,
weightkg: 809.1,
color: 'White',
eggGroups: [ 'Undiscovered' ],
changesFrom: 'Calyrex',
}
Structure of request.side.sideConditions
sideConditions: {
toxicspikes: {
id: 'toxicspikes',
target: [Object],
source: [Object],
sourceSlot: 'p2a',
layers: 1
}
/*********************************************************************
* Class to represent a rule based player AI
* overrides the chooseMove, chooseSwitch, chooseTeamPreview, and shouldDynamax functions in random-player-ai
*********************************************************************/
export class HeuristicsPlayerAI extends RandomPlayerAI {
private readonly activeTracker: activeTracker;
private ENTRY_HAZARDS = ["spikes", "stealthrock", "stickyweb", "toxicspikes"]
private ANTI_HAZARDS_MOVES = ["rapidspin", "defog", "tidyup"]
private SELF_RECOVERY_MOVES = ["healorder", "milkdrink", "recover", "rest", "roost", "slackoff", "softboiled"]
private WEATHER_SETUP_MOVES = {"chillyreception":"Snow", "hail":"Hail", "raindance":"RainDance", "sandstorm":"Sandstorm",
"snowscape":"Snow", "sunnyday":"SunnyDay"}
private SOUND_BASED_MOVES = ["growl", "roar", "sing", "supersonic", "screech", "snore", "healbell", "uproar", "hypervoice",
"metalsound", "grasswhistle", "howl", "bugbuzz", "chatter", "round", "echoedvoice", "relicsong", "snarl", "nobleroar",
"disarmingvoice", "partingshot", "boomburst", "confide", "sparklingaria", "clangingscales", "clangoroussoulblaze",
"clangoroussoul", "overdrive", "eeriespell", "torchsong", "alluringvoice", "psychicnoise"]
private SPEED_TIER_COEFICIENT = 0.1
private HP_FRACTION_COEFICIENT = 0.4
private SWITCH_OUT_MATCHUP_THRESHOLD = -2
private SELF_KO_MOVE_MATCHUP_THRESHOLD = 0.3
private TRICK_ROOM_THRESHOLD = 85
private RECVOERY_MOVE_THRESHOLD = 0.4
private ACCURACY_SWITCH_THRESHOLD = -3
constructor(playerStream: ObjectReadWriteStream<string>, options: AIOptions, activeTracker: activeTracker) {
super(playerStream, options);
this.activeTracker = activeTracker;
}
// estimates a given matchup and returns a score
protected _estimateMatchup(request, nonActiveMon?): number {
this._updateActiveTracker(request)
const mon_opponent = this._getCurrentPlayer(request)
var mon = mon_opponent[0].species!
var opponent = mon_opponent[1].species!
if (nonActiveMon) mon = nonActiveMon.details!
const {Dex} = require('pokemon-showdown');
let score = 1;
score = this.bestDamageMultiplier(mon, opponent)
score -= this.bestDamageMultiplier(opponent, mon)
if (Dex.species.get(mon).baseStats.spe > Dex.species.get(opponent).baseStats.spe) {
score += this.SPEED_TIER_COEFICIENT;
} else if (Dex.species.get(opponent).baseStats.spe > Dex.species.get(mon).baseStats.spe) {
score -= this.SPEED_TIER_COEFICIENT;
}
if (request.side.id == "p1") {
if (nonActiveMon) score += this._getCurrentHp(nonActiveMon.condition) * this.HP_FRACTION_COEFICIENT;
else score += this.activeTracker.p1_active!.currentHp * this.HP_FRACTION_COEFICIENT;
score -= this.activeTracker.p2_active!.currentHp * this.HP_FRACTION_COEFICIENT;
} else {
if (nonActiveMon) score += this._getCurrentHp(nonActiveMon.condition) * this.HP_FRACTION_COEFICIENT;
else score += this.activeTracker.p2_active!.currentHp * this.HP_FRACTION_COEFICIENT;
score -= this.activeTracker.p1_active!.currentHp * this.HP_FRACTION_COEFICIENT;
}
return score;
}
// estimate matchup function for team preview
protected _estimateMatchupTeamPreview(nonActiveMon, nonActiveOpp): number {
const monName = nonActiveMon.details
const oppName = nonActiveOpp.species
const {Dex} = require('pokemon-showdown');
let score = 1;
score = this.bestDamageMultiplier(monName, oppName)
score -= this.bestDamageMultiplier(oppName, monName)
if (Dex.species.get(monName).baseStats.spe > Dex.species.get(oppName).baseStats.spe) {
score += this.SPEED_TIER_COEFICIENT;
} else if (Dex.species.get(oppName).baseStats.spe > Dex.species.get(monName).baseStats.spe) {
score -= this.SPEED_TIER_COEFICIENT;
}
// calculating max hp for opponent since there is no maxHp in dex or currenthp in request.side.foe.pokemon
const oppHp = Math.floor((((2 * Number(Dex.species.get(nonActiveOpp.species).baseStats.hp))
+ Number(nonActiveOpp.ivs.hp) + Math.floor(Number(nonActiveOpp.evs.hp) / 4) ) * Number(nonActiveOpp.level)) / 100)
+ Number(nonActiveOpp.level) + 10
score += this._getCurrentHp(nonActiveMon.condition) * this.HP_FRACTION_COEFICIENT;
score -= oppHp * this.HP_FRACTION_COEFICIENT;
return score;
}
protected shouldDynamax(request, canDynamax: boolean): boolean {
this._updateActiveTracker(request)
if (canDynamax) {
const mon_opponent = this._getCurrentPlayer(request)
const mon = mon_opponent[0]
const opponent = mon_opponent[1]
// if active mon is the last full HP mon
if (
request.side.pokemon.filter((m) => parseInt(m.condition.split('/')[0], 10) == 1).length == 1
&& mon.currentHp == 1
) {
return true
}
// Matchup advantage and full hp on full hp
if ((this._estimateMatchup(request)>0) && mon.currentHpPercent == 1 && opponent.currentHpPercent == 1) {
return true
}
// last pokemon
if (
request.side.pokemon.filter((m) => Number(this._getHpFraction(m.condition)) != 0).length == 1
&& mon.currentHpPercent == 1
) {
return true
}
}
return false
}
protected shouldTera(request, canTerastallize: boolean): boolean {
this._updateActiveTracker(request)
if (canTerastallize) {
const mon_opponent = this._getCurrentPlayer(request)
const mon = mon_opponent[0]
const opponent = mon_opponent[1]
// if active mon is the last full HP mon and tera type has advantage
if (
this.bestDamageMultiplier(mon.species!, opponent.species!, false, mon.teraType)
&& request.side.pokemon.filter((m) => parseInt(m.condition.split('/')[0], 10) == 1).length == 1
&& mon.currentHp == 1
) {
return true
}
// last pokemon and tera type has advantage
if (
this.bestDamageMultiplier(mon.species!, opponent.species!, false, mon.teraType)
&& request.side.pokemon.filter((m) => Number(this._getHpFraction(m.condition)) != 0).length == 1
&& mon.currentHpPercent == 1
) {
return true
}
// Matchup disadvantage, tera type has advantage and good hp on both player and opponent
if ((this._estimateMatchup(request)<0)
&& this.bestDamageMultiplier(mon.species!, opponent.species!, false, mon.teraType)
&& mon.currentHpPercent == 1 && opponent.currentHpPercent >= 0.75)
{
return true
}
}
return false
}
protected _should_switch_out(request) {
this._updateActiveTracker(request)
const mon_opponent = this._getCurrentPlayer(request)
const mon = mon_opponent[0]
const opponent = mon_opponent[1]
const availableSwitches = (request.side.pokemon).filter((m) => ((m.active == false) && this._getHpFraction(m.condition) != 0))
// If there is a decent switch in and not trapped...
if (availableSwitches.filter(m => this._estimateMatchup(request) > 0).length && request.side.pokemon.trapped == false) {
// ...and a 'good' reason to switch out
if (mon.boosts["accuracy"] <= this.ACCURACY_SWITCH_THRESHOLD) {
return true
}
if (mon.boosts["def"] <= -3 || mon.boosts["spd"] <= -3) {
return true
}
if (mon.boosts["atk"] <= -3 && mon.stats["atk"] >= mon.stats["spa"]) {
return true
}
if (mon.boosts["spa"] <= -3 && mon.stats["atk"] <= mon.stats["spa"]) {
return true
}
if (this._estimateMatchup(request) < this.SWITCH_OUT_MATCHUP_THRESHOLD) {
return true
}
}
return false
}
protected _stat_estimation(mon, stat) {
// Stats boosts value
if (mon.boosts[stat] > 1) {
const boost = (2 + mon.boosts[stat]) / 2
return ((2 * Dex.species.get(mon.species).baseStats[stat] + 31) + 5) * boost
} else {
const boost = 2 / (2 - mon.boosts[stat])
return ((2 * Dex.species.get(mon.species).baseStats[stat] + 31) + 5) * boost
}
}
protected chooseMove(request, active, moves: {choice: string, move}[], canDynamax, canTerastallize, possibleMoves): [string, boolean, boolean] {
this._updateActiveTracker(request)
const mon_opponent = this._getCurrentPlayer(request)
const mon = mon_opponent[0]
const opponent = mon_opponent[1]
// update protect count if it's on cooldown
if (mon.protectCount > 0) {
mon.protectCount -= 1
}
// update future sight counter if it's been used recently
if (mon.FutureSightCounter > 0) {
mon.FutureSightCounter -= 1
}
const currentWeather = request.side.pokemon[0].battle.field.weather
const allMoves = possibleMoves
// if the move is out of pp or is disabled, we ignore it when figuring out what move to use
for (var move of possibleMoves) {
if (move.pp == 0 || move.disabled == true) {
possibleMoves = possibleMoves.filter((m) => (m.id || m.move) !== (move.id || move.move));
}
}
// Rough estimation of damage ratio
const physical_ratio = this._stat_estimation(mon, "atk") / this._stat_estimation(opponent, "def")
const special_ratio = this._stat_estimation(mon, "spa") / this._stat_estimation(opponent, "spd")
// list of all side conditions on each player's side
const monSideConditionList = Object.keys(mon.sideConditions).map(move => mon.sideConditions[move].id)
const oppSideConditionList = Object.keys(opponent.sideConditions).map(move => opponent.sideConditions[move].id)
// if the pokemon has moves and shouldn't switch out, or is the last pokemon left
if (possibleMoves && !(this._should_switch_out(request))
|| (request.side.pokemon.filter((m) => Number(this._getHpFraction(m.condition)) != 0).length == 1 && mon.currentHpPercent == 1)) {
const n_remaining_mons = mon.n_remaining_mons
const n_opp_remaining_mons = opponent.n_remaining_mons
// Fake Out
// If pokemon's first turn out and opponent isn't immune use fake out
for (var move of possibleMoves) {
if ((move.id || move.move) == "fakeout" && mon.firstTurn == 1 && !Dex.species.get(opponent.species).types.includes("Ghost")) {
mon.firstTurn = 0
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
mon.firstTurn = 0
// Explosion/Self destruct
// use if health < 0.3 and opponent isn't immune
for (var move of possibleMoves) {
if (((move.id || move.move) == "explosion" || (move.id || move.move) == "selfdestruct")
&& mon.currentHpPercent<this.SELF_KO_MOVE_MATCHUP_THRESHOLD && opponent.currentHpPercent>0.5
&& !Dex.species.get(opponent.species).types.includes("Ghost")) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
// Deal with non weather related field changing effects
for (var move of possibleMoves) {
// Tailwind
if ((move.id || move.move) == "tailwind" && !monSideConditionList.includes(move.id || move.move)) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
// Trick room
// if pokemon has trick room and we have at least 3 pokemon slower than the trick room threshold
if ((move.id || move.move) == "trickroom" && !monSideConditionList.includes(move.id || move.move)
&& request.side.pokemon.map(m => m.stats.spd).filter(spd => spd <= this.TRICK_ROOM_THRESHOLD).length >= 3) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
// Aurora veil
if ((move.id || move.move) == "auroraveil" && !monSideConditionList.includes(move.id || move.move)
&& (currentWeather == "Hail" || currentWeather == "Snow")) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
// Light Screen
if ((move.id || move.move) == "lightscreen" && !monSideConditionList.includes(move.id || move.move)
&& Dex.species.get(opponent.species).baseStats.spa > Dex.species.get(opponent.species).baseStats.atk) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
// Reflect
if ((move.id || move.move) == "reflect" && !monSideConditionList.includes(move.id || move.move)
&& Dex.species.get(opponent.species).baseStats.atk > Dex.species.get(opponent.species).baseStats.spa) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
// Entry hazard...
for (var move of possibleMoves) {
// ...setup
if (
n_opp_remaining_mons >= 3
&& (this.ENTRY_HAZARDS.includes(move.id || move.move))
&& this.ENTRY_HAZARDS.filter(word => oppSideConditionList
.includes(word)).length === 0 // opponent doesn't already have an entry hazard
) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
// ...removal
else if (
n_remaining_mons >= 2
&& this.ANTI_HAZARDS_MOVES.includes(move.id || move.move)
&& this.ENTRY_HAZARDS.filter(word => monSideConditionList
.includes(word)).length > 0 // mon has an entry hazard
) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
// Court Change
// use if either the opponent has some good side condition you want or you want to give them an entry hazard
// and you have no good side conditions to give them, and they have no entry hazards
for (var move of possibleMoves) {
if (
(move.id || move.move) == "courtchange"
&& (!(this.ENTRY_HAZARDS.filter(word => monSideConditionList.includes(word)).length === 0)
|| (oppSideConditionList.includes("tailwind") || oppSideConditionList.includes("lightscreen") || oppSideConditionList.includes("reflect"))
&& !(monSideConditionList.includes("tailwind") || monSideConditionList.includes("lightscreen") || monSideConditionList.includes("reflect"))
&& this.ENTRY_HAZARDS.filter(word => oppSideConditionList.includes(word)).length === 0) ) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
// Self recovery moves
// use recovery moves if health is less than RECVOERY_MOVE_THRESHOLD
for (var move of possibleMoves) {
if (this.SELF_RECOVERY_MOVES.includes(move.id || move.move) && mon.currentHpPercent < this.RECVOERY_MOVE_THRESHOLD) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
// Strength sap
for (var move of possibleMoves) {
if ((move.id || move.move) == "strengthsap" && mon.currentHpPercent < 0.5 && Dex.species.get(opponent.species).baseStats.atk > 80) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
// Weather setup moves
for (var move of possibleMoves) {
if ((move.id || move.move) in this.WEATHER_SETUP_MOVES && currentWeather != this.WEATHER_SETUP_MOVES[(move.id || move.move)].toLowerCase()) {
// dealing with Kyogre and Groudon's upgraded weather conditions
if (!(currentWeather == "PrimordialSea" && this.WEATHER_SETUP_MOVES[(move.id || move.move)]== "RainDance")
&& !(currentWeather == "DesolateLand" && this.WEATHER_SETUP_MOVES[(move.id || move.move)] == "SunnyDay")) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
}
// Setup moves
if (mon.currentHpPercent == 1 && this._estimateMatchup(request) > 0) {
const SETUP_MOVES = JSON.parse(fs.readFileSync("../Data/UsefulDatasets/setup_moves.json", 'utf-8'));
for (var move of possibleMoves) {
// if move is a setup move and the pokemon has a stat that can be boosted by the move
if ((move.id || move.move) in SETUP_MOVES
&& Math.min(...(Object.keys(this._getNonZeroStats((move.id || move.move)))).map(key => mon.boosts[key])) < 6
) {
if ((move.id || move.move) == "curse" && (Dex.species.get(opponent.species).types.includes("Ghost"))){
continue; // curse isn't a set up move for ghost types
}
else {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
}
}
// Status Inflicting Moves
const STATUS_INFLICTING_MOVES = JSON.parse(fs.readFileSync("../Data/UsefulDatasets/status_inflicting_moves.json", 'utf-8'));
for (var move of possibleMoves) {
const activeOpp = request.side.foe.pokemon.filter(mon => mon.isActive == true)[0];
// make sure the opponent doesn't already have a status condition
if ((!(
Object.keys(activeOpp.volatiles).includes("curse") ||
Object.keys(activeOpp.volatiles).includes("sleep") ||
Object.keys(activeOpp.volatiles).includes("leechseed") ||
Object.keys(activeOpp.volatiles).includes("substitute") ||
Object.keys(activeOpp.volatiles).includes("saltcure")
) && (activeOpp.status === ''))
&& opponent.currentHpPercent > 0.6 && mon.currentHpPercent > 0.5
&& !(request.side.foe.pokemon.ability == ("leafguard") && (currentWeather === "DesolateLand" || currentWeather === "SunnyDay")) // leafguard prevents status conditions in sun
) {
const cond = STATUS_INFLICTING_MOVES[(move.id || move.move)]
switch (cond) {
case "burn":
if (!Dex.species.get(opponent.species).types.includes("Fire") && Dex.species.get(opponent.species).baseStats.atk > 80) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
break;
case "paralysis":
if (!Dex.species.get(opponent.species).types.includes("Electric") && Dex.species.get(opponent.species).baseStats.spe > Dex.species.get(mon.species).baseStats.spe) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
break;
case "sleep":
if (!(Dex.species.get(opponent.species).types.includes("Poison") && (move.id || move.move) === "spore" || "sleeppowder")
&& Dex.species.get(opponent.species).baseStats.spe > Dex.species.get(mon.species).baseStats.spe
&& request.side.foe.pokemon.ability != "insomnia"
&& request.side.foe.pokemon.ability != "sweetveil"
&& request.side.foe.pokemon.ability != "vitalspirit"
&& ((request.side.foe.pokemon.ability == "soundproof") && (move.id || move.move) == this.SOUND_BASED_MOVES.includes(move.id || move.move))
) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
break;
case "confusion":
if (!(Dex.species.get(opponent.species).types.includes("Poison") || Dex.species.get(opponent.species).types.includes("Steel"))
&& request.side.foe.pokemon.ability != "magicguard"
&& request.side.foe.pokemon.ability != "owntempo"
&& request.side.foe.pokemon.ability != "oblivious") {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
break;
case "poison":
if (!(Dex.species.get(opponent.species).types.includes("Poison") || Dex.species.get(opponent.species).types.includes("Steel"))
&& request.side.foe.pokemon.ability != "immunity"
&& request.side.foe.pokemon.ability != "magicguard") {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
break;
case "cursed":
if (Dex.species.get(mon.species).types.includes("Ghost")
&& request.side.foe.pokemon.ability != "magicguard") {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
break;
case "leech":
if (!Dex.species.get(opponent.species).types.includes("Grass")
&& request.side.foe.pokemon.ability != "magicguard") {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
break;
}
}
}
// Accuracy lowering moves
// if you have a good matchup, and the opponent isn't below -1 accuracy
for (var move of possibleMoves) {
if (mon.currentHpPercent == 1 && this._estimateMatchup(request) > 0
&& opponent.boosts["accuracy"] > this.ACCURACY_SWITCH_THRESHOLD
&& ["flash", "kinesis", "leaftornado", "mirrorshot", "mudbomb", "mudslap", "muddywater", "nightdaze", "octazooka", "sandattack", 'secretpower', 'smokescreen'].includes(move.id || move.move)) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
//Protect style moves
// Use if protect wasn't used last turn and opponent is poisoned, burned, or if your mon is leech seeded
for (var move of possibleMoves) {
const activeOpp = request.side.foe.pokemon.filter(mon => mon.isActive == true)[0];
if (["protect", "banefulbunker", "obstruct", "craftyshield", "detect", "quickguard", "spikyshield", "silktrap"].includes(move.id || move.move)) {
// stall out side conditions
if (((oppSideConditionList.includes("tailwind" || "lightscreen" || "reflect" || "trickroom")
&& !monSideConditionList.includes("tailwind" || "lightscreen" || "reflect"))
|| (Object.keys(activeOpp.volatiles).includes("curse") || activeOpp.status != '')) // opp has status conditions
&& (mon.protectCount == 0) && (request.side.foe.pokemon.ability != "unseenfist")) {
mon.protectCount = 2
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
}
// Damage dealing moves
const moveValues: { [move: string]: number } = {};
for (const move of possibleMoves) {
moveValues[(move.id || move.move)] = Dex.moves.get(move.id || move.move).basePower
* (Dex.species.get(mon.species).types.includes(Dex.moves.get((move.id || move.move)).type) ? 1.5 : 1)
* (Dex.moves.get((move.id || move.move)).category === "Physical" ? physical_ratio : special_ratio) // This line needs to be commended out if running the bot on a generation before the physical special split was introduced
* Number(Dex.moves.get((move.id || move.move)).accuracy)
* this._expectedHits((move.id || move.move))
* this.bestDamageMultiplier(move.id || move.move, opponent.species!, true);
// we handled status moves earlier, ignore them here
if (Dex.moves.get(move.id || move.move).basePower == 0) {
moveValues[(move.id || move.move)] = -100
}
// if fakeout wasn't used earlier, it will fail
if ((move.id || move.move) == "fakeout") {
moveValues[(move.id || move.move)] = 0
}
// if selfdestruct wasn't used earlier, don't use it
if ((move.id || move.move) == ("explosion" || "selfdestruct")) {
moveValues[(move.id || move.move)] = 0
}
// prioritise sleeptalk if asleep
if ((request.side.pokemon.filter(mon => mon.active == true)[0].status == "sleep") && (move.id || move.move) == ("sleeptalk")) {
moveValues[(move.id || move.move)] += 5
}
if (
(request.side.foe.pokemon.ability == "lightningrod") && Dex.moves.get((move.id || move.move)).type == "electric"
|| (request.side.foe.pokemon.ability == "flashfire") && Dex.moves.get((move.id || move.move)).type == "fire"
|| (request.side.foe.pokemon.ability == "levitate") && Dex.moves.get((move.id || move.move)).type == "ground"
|| (request.side.foe.pokemon.ability == "sapsipper") && Dex.moves.get((move.id || move.move)).type == "grass"
|| (request.side.foe.pokemon.ability == "motordrive") && Dex.moves.get((move.id || move.move)).type == "electric"
|| (request.side.foe.pokemon.ability == "stormdrain") && Dex.moves.get((move.id || move.move)).type == "water"
|| (request.side.foe.pokemon.ability == "voltabsorb") && Dex.moves.get((move.id || move.move)).type == "electric"
|| (request.side.foe.pokemon.ability == "waterabsorb") && Dex.moves.get((move.id || move.move)).type == "water"
|| (request.side.foe.pokemon.ability == "immunity") && Dex.moves.get((move.id || move.move)).type == "poison"
|| (request.side.foe.pokemon.ability == "eartheater") && Dex.moves.get((move.id || move.move)).type == "ground"
|| (request.side.foe.pokemon.ability == "suctioncup") && (move.id || move.move) == ("roar" || "whirlwind")
|| (request.side.foe.pokemon.ability == "soundproof") && (move.id || move.move) == this.SOUND_BASED_MOVES.includes(move.id || move.move)
|| (request.side.foe.pokemon.filter(mon => mon.isActive == true)[0].status != "sleep") && (move.id || move.move) == ("dreameater")
) {
moveValues[(move.id || move.move)] = 0;
}
// don't use future sight if it's still on cooldown
if ((move.id || move.move) == ("futuresight") && mon.FutureSightCounter != 0) {
moveValues[(move.id || move.move)] = 0
}
}
const bestMoveValue = Math.max(...Object.values(moveValues));
if (!('recharge' in moveValues)) {
const bestMove = Object.keys(moveValues).find(m => moveValues[m] === bestMoveValue);
if (bestMove == "futuresight" && mon.FutureSightCounter == 0) {
mon.FutureSightCounter = 3 // 3 because it's decremented before moves are used
}
var should_Dynamax = this.shouldDynamax(request, canDynamax)
var should_Tera = this.shouldTera(request, canTerastallize)
if (should_Dynamax) {
return [this._getMoveSlot(bestMove!, allMoves), true, false]
} else if (should_Tera) {
return [this._getMoveSlot(bestMove!, allMoves), false, this.shouldTera(request, canTerastallize)]
} else {
return [this._getMoveSlot(bestMove!, allMoves), false, false]
}
} else {
return ["move 1", false, false]
}
}
// healing wish (dealing with it here because you'd only use it if you should switch out anyway)
for (var move of possibleMoves) {
if ((move.id || move.move) == "healingwish" && mon.currentHpPercent<this.SELF_KO_MOVE_MATCHUP_THRESHOLD) {
return [this._getMoveSlot((move.id || move.move), allMoves), false, this.shouldTera(request, canTerastallize)]
}
}
// switch out
if (this._should_switch_out(request)) {
const availableSwitches = (request.side.pokemon).filter((m) => ((m.active == false) && this._getHpFraction(m.condition) != 0))
let bestEstimation = Math.max(...availableSwitches.map(pokemon => this._estimateMatchup(request, pokemon)))
let bestMatchup = availableSwitches.find(pokemon => this._estimateMatchup(request, pokemon) === bestEstimation)
return ["switch ".concat(this._getPokemonPos(request, bestMatchup)), false, false]
}
mon.firstTurn = 0
// otherwise can't find a good option so use a random move
return [this.prng.sample(moves).choice, false, this.shouldTera(request, canTerastallize)]
}
// gets the slot number of the pasased in move
protected _getMoveSlot(move: string, possibleMoves) {
const bestMoveSlotIndex = possibleMoves.findIndex(item => (item.id || item.move) === move) + 1
var bestMoveSlot
if (bestMoveSlotIndex == 1) bestMoveSlot = "move 1"
if (bestMoveSlotIndex == 2) bestMoveSlot = "move 2"
if (bestMoveSlotIndex == 3) bestMoveSlot = "move 3"
if (bestMoveSlotIndex == 4) bestMoveSlot = "move 4"
return bestMoveSlot
}
// gets the slot number of the bestMatchup pokemon in the team
protected _getPokemonPos(request, bestMatchup) {
return (request.side.pokemon).filter((pokemon) => (pokemon.details == bestMatchup.details && this._getHpFraction(pokemon.condition) != 0 && pokemon.active == false))[0].position+1
}
// returns an approximate number of hits for a given move for estimation purposes
protected _expectedHits(move: string): number {
const minMaxHits = Dex.moves.get(move).multihit
if (move == "triplekick" || move == "tripleaxel") {
//Triple Kick and Triple Axel have an accuracy check for each hit, and also
//rise in BP for each hit
return 1 + 2 * 0.9 + 3 * 0.81
}
if (move == "populationbomb") {
// population bomb hits until it misses, 90% accuracy
return 7
}
if (minMaxHits == undefined || minMaxHits[0] == minMaxHits[1]) {
// non multihit move
return 1
}
else {
// It hits 2-5 times
return (2 + 3) / 3 + (4 + 5) / 6
}
}
// Chooses the best pokemon to switch to
protected chooseSwitch(request, active: AnyObject | undefined, switches: {slot: number, pokemon: AnyObject}[]): number {
this._updateActiveTracker(request)
const availableSwitches = (request.side.pokemon).filter((m) => ((m.active == false) && this._getHpFraction(m.condition) != 0))
if (!availableSwitches) return 1
let bestEstimation = Math.max(...availableSwitches.map(pokemon => this._estimateMatchup(request, pokemon)))
let bestMatchup = availableSwitches.find(pokemon => this._estimateMatchup(request, pokemon) === bestEstimation)
this._getCurrentPlayer(request)[0].firstTurn = 1
return Number(this._getPokemonPos(request, bestMatchup))
}
protected chooseTeamPreview(request, team: AnyObject[]): string {
this._updateActiveTracker(request)
return "team 1"; // Uncomment to make the bot choose the best mon based on the opponent's team
const mons = request.side.pokemon
const opponentPokemon = request.side.foe.pokemon.map(m => m.set)
var bestMon
var bestAverage
var matchups
var average
for (var mon of mons) {
matchups = opponentPokemon.map(opp => this._estimateMatchupTeamPreview(mon, opp))
average = matchups.reduce((total, value) => total + value, 0) / matchups.length
if (bestAverage == undefined || average > bestAverage) {
bestMon = mon
bestAverage = average
}
}
// If you have a pokemon with some setup move that will benefit other pokemon on the team, use that first
for (var mon of mons) {
for (var move of mon.moves) {
if ((move.id || move.move) in this.WEATHER_SETUP_MOVES
|| (move.id || move.move) in this.ENTRY_HAZARDS
|| (move.id || move.move) == "tailwind"
|| (move.id || move.move) == "trickroom"
|| (move.id || move.move) == "auroraveil"
|| (move.id || move.move) == "lightscreen"
|| (move.id || move.move) == "reflect"
) {
bestMon = mon
}
}
}
this._getCurrentPlayer(request)[0].firstTurn = 1
return "team ".concat(bestMon.position+1);
}
// TODO this doesn't account for terastalised opponents, and just uses the mon's base types
// returns the type with the best damage multiplier against the opponent
protected bestDamageMultiplier(attacker: string, defender: string, isMove: boolean = false, teraType: string = ""): number {
const typeMatchups = JSON.parse(fs.readFileSync("../Data/UsefulDatasets/gen_9_type-chart.json", 'utf-8'));
var attackerTypes;
if (isMove) {
attackerTypes = [Dex.moves.get(attacker).type, "???"]
} else {
attackerTypes = Dex.species.get(attacker).types
}
if (!(teraType === "???") && !(teraType === undefined) && !(teraType === "")) {
attackerTypes = [teraType, "???"]
}
const defenderTypes = Dex.species.get(defender).types
let multiplier = 1;
let bestMultiplier = 1
let counter = 0
for (const attackerType of attackerTypes) {
multiplier = 1;
for (const defenderType of defenderTypes) {
if (!(attackerType=="???") && !(defenderType=="???")
&& !(attackerType==undefined) && !(defenderType==undefined)
&& !(attackerType=="") && !(defenderType=="") &&
typeMatchups[attackerType] !== undefined &&
typeMatchups[attackerType][defenderType] !== undefined) {
multiplier *= Number(typeMatchups[attackerType][defenderType]);
} else {
multiplier = -5 // discourage unknown move types
}
}
if (counter == 0) {
bestMultiplier = multiplier
}
counter += 1
}
return Math.max(multiplier, bestMultiplier);
}
// The move options provided by the simulator have been converted from the name
// which we're tracking, so we need to convert them back.
private fixMove(m) {
const id = toID(m.move);
if (id.startsWith('return')) return 'return';
if (id.startsWith('frustration')) return 'frustration';
if (id.startsWith('hiddenpower')) return 'hiddenpower';
return id;
}
// takes hp in the form '457/457' and returns a decimal representing the amount left (with 1 as full hp and 0 as fainted)
private _getHpFraction(condition : string): number {
if (condition == "0 fnt") return 0
const [numerator, denominator] = condition.split('/' || ' ').map(x => parseInt(x, 10));
return numerator / denominator;
}
// takes hp in the form '457/457' and returns the amount of hp left
private _getCurrentHp(condition : string): number {
if (condition == "0 fnt") return 0
return Number(condition.split('/' || ' ')[0]);
}
// takes a move name and returns the stats that are boosted by that move
private _getNonZeroStats(name: string): { [key: string]: number } {
const SETUP_MOVES = JSON.parse(fs.readFileSync("../Data/UsefulDatasets/setup_moves.json", 'utf-8'));
if (name in SETUP_MOVES) {
return Object.entries(SETUP_MOVES[name])
.filter(([, value]) => value !== 0)
.reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {});
}
else {
return {}
}
}
// fast way to update activeTracker, should be called before making decisions in choose functions
private _updateActiveTracker(request) {
// find active player
if (request.side.id == "p1") {
var mon = this.activeTracker.p1_active!
} else {
mon = this.activeTracker.p2_active!
}
// find active pokemon and update data
for (var pokemon of request.side.pokemon) {
if (pokemon.active == true) {
mon.species = pokemon.species
mon.currentHp = this._getCurrentHp(pokemon.condition)
mon.currentHpPercent = this._getHpFraction(pokemon.condition)
mon.boosts = pokemon.boosts
mon.stats = pokemon.stats
mon.moves = pokemon.moves
mon.n_remaining_mons = request.side.pokemon.filter((m) => Number(this._getHpFraction(m.condition)) != 0).length
mon.sideConditions = request.side.sideConditions
mon.teraType = pokemon.teraType
}
}
}
// returns the current pokemon for the player making the request, and the opponent
private _getCurrentPlayer(request) {
if (request.side.id == "p1") {
var mon = this.activeTracker.p1_active!
var opponent = this.activeTracker.p2_active!
return [mon, opponent]
} else {
mon = this.activeTracker.p2_active!
opponent = this.activeTracker.p1_active!
return [mon, opponent]
}
}
}
/*********************************************************************
* Run Simulation
*********************************************************************/
async function main() {
var threadNo = process.argv.slice(2)[0]
var team1No = process.argv.slice(3)[0]
var team2No = process.argv.slice(4)[0]
var testTeam1 = "../Data/WorkerFiles/" + threadNo + "1.txt"
var testTeam2 = "../Data/WorkerFiles/" + threadNo + "2.txt"
// const battleStream = new BattleStream()
// const streams = getPlayerStreams(battleStream);
var f = fs.readFileSync(testTeam1, 'utf8');
var g = fs.readFileSync(testTeam2, 'utf8');
// var team1 = Teams.pack(Teams.import(f))
// var team2 = Teams.pack(Teams.import(g))
var maybeteam1 = Teams.import(f)
var maybeteam2 = Teams.import(g)
let team1: PokemonSet[] | undefined;
let team2: PokemonSet[] | undefined;
if (maybeteam1 !== null) {
team1 = maybeteam1;
}
if (maybeteam2 !== null) {
team2 = maybeteam2;
}
console.log("[[[[[")
console.log(team1No + " vs " + team2No)