-
Notifications
You must be signed in to change notification settings - Fork 0
/
poke_environ.py
1166 lines (1006 loc) · 47.3 KB
/
poke_environ.py
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
import gym
import gym.spaces
from gym.utils import seeding
import enum
import numpy as np
from pokemon_json import *
from poke_common import *
import random
import math
from sklearn.preprocessing import LabelBinarizer
import math
class Actions(enum.Enum):
Attack_Slot_1 = 0
Attack_Slot_2 = 1
Attack_Slot_3 = 2
Attack_Slot_4 = 3
Attack_Z_Slot_1 = 4
Attack_Z_Slot_2 = 5
Attack_Z_Slot_3 = 6
Attack_Z_Slot_4 = 7
Attack_UltraBurst_Slot_1 = 8
Attack_UltraBurst_Slot_2 = 9
Attack_UltraBurst_Slot_3 = 10
Attack_UltraBurst_Slot_4 = 11
Change_Pokemon_Slot_1 = 12
Change_Pokemon_Slot_2 = 13
Change_Pokemon_Slot_3 = 14
Change_Pokemon_Slot_4 = 15
Change_Pokemon_Slot_5 = 16
Change_Pokemon_Slot_6 = 17
Attack_Struggle = 18
class Player():
def __init__(self, pokemon):
self.pokemon = pokemon
self.curr_pokemon = pokemon[0]
self.is_player = True
def encode_pokemon(self):
# if we have empty spots for missing pokemon, just set 0s
encodes = []
# add current pokemon first to denote that it is selected.
for pkmn in self.pokemon:
encodes.extend(self.encode_pokemon_state(pkmn))
return encodes
def switch_curr_pokemon(self, position):
new_pokemon = self.pokemon[position]
self.curr_pokemon = new_pokemon
return new_pokemon
#Type
#Attack
#SP Attack
# Defense
#SP Defense
# health
# speed
#Ability
# Bot has some info hidden like atk damage, speed etc. keep hidden to simulate opponent
def _add_pokemon_encode(self, pokemon):
# empty pokemon slot
if pokemon is None and self.is_player:
return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
elif pokemon is None:
return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
element_2nd_type = ''
if pokemon.element_2nd_type != None:
element_2nd_type = pokemon.element_2nd_type
item = 'Item Unknown'
if pokemon.item != None:
item = pokemon.item
encode = []
encode.append(pokemon.name)
encode.append(pokemon.level)
encode.append(pokemon.curr_health/float(pokemon.max_health))
encode.append(pokemon.element_1st_type.value)
encode.append(element_2nd_type.value)
encode.append(pokemon.level)
encode.append(pokemon.weight)
if self.is_player:
encode.append(pokemon.item)
encode.append(pokemon.atk)
encode.append(pokemon.spatk)
encode.append(pokemon.defense)
encode.append(pokemon.spdef)
encode.append(pokemon.ability)
return encode
def encode_pokemon_state(self, pokemon):
# empty pokemon slot
if pokemon is None and self.is_player:
return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
elif pokemon is None:
return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
element_2nd_type = ''
if pokemon.element_2nd_type != None:
element_2nd_type = pokemon.element_2nd_type.value
raw_encode = [
all_pokemon_names_labels.transform([pokemon.num]), # category
pokemon.level,
pokemon.curr_health/float(pokemon.max_health),
all_element_types_labels.transform([pokemon.element_1st_type.value]), # category
all_element_types_labels.transform([element_2nd_type]), # category
pokemon.weight,
pokemon.accuracy_modifier,
pokemon.attack_modifier,
pokemon.spatk_modifier,
pokemon.defense_modifier,
pokemon.spdef_modifier,
pokemon.speed_modifier,
pokemon.evasion_modifier,
all_status_labels.transform([pokemon.status.value]), # category
]
if self.is_player:
raw_encode.append(all_items_labels.transform([pokemon.item]))
raw_encode.append(pokemon.atk)
raw_encode.append(pokemon.spatk)
raw_encode.append(pokemon.defense)
raw_encode.append(pokemon.spdef)
raw_encode.append(all_abilities_labels.transform([pokemon.ability]))
return flatten(raw_encode)
class RandomAgent(Player):
def __init__(self, pokemon):
super().__init__(pokemon)
self.is_player = False
class State:
def __init__(self, player, computer_agent):
self.player = player
self.computer_agent = computer_agent
self.weather_condition = ''
self.weather_turns = 0
self.terrain_condition = ''
self.terrain_turns = 0
self.player_choiced = False
self.player_choiced_move = ''
self.agent_choiced = False
self.agent_choiced_move = ''
self.player_selected_pokemon = '' # category
self.agent_selected_pokemon = '' # category
self.user_trapped = False
self.agent_trapped = False
self.player_used_rocks = False
self.agent_used_rocks = False
self.block_next_attack = False
self.protect_used_last_turn = False
self.player_has_substitute = False
self.agent_has_substitute = False
self.state_transcript = ''
self.state_reverse_transcript = ''
#used to switch fainted pokemon without them taking a hit on entry
self.player_attack_effectiveness = ELEMENT_MODIFIER.NUETRAL #Category
self.agent_attack_effectiveness = ELEMENT_MODIFIER.NUETRAL # Category
self.player_must_switch = False
self.agent_must_switch = False
self.player_flinched = False
self.agent_flinched = False
self.player_confused = False
self.agent_confused = False
self.player_moved_first = False
self.player_move_succeeded = False
self.agent_move_succeeded = False
self.should_self_print = True
self.reward = 0
self.turns = 0
def reset(self, player, computer_agent, should_print=False):
self.player = player
self.computer_agent = computer_agent
self.turns = 0
self.should_self_print = should_print
self.weather_condition = ''
self.weather_turns = 0
self.terrain_condition = ''
self.terrain_turns = 0
self.player_choiced = False
self.player_choiced_move = ''
self.agent_choiced = False
self.agent_choiced_move = ''
self.player_selected_pokemon = '' # category
self.agent_selected_pokemon = '' # category
self.user_trapped = False
self.agent_trapped = False
self.player_used_rocks = False
self.agent_used_rocks = False
self.block_next_attack = False
self.protect_used_last_turn = False
self.player_has_substitute = False
self.agent_has_substitute = False
self.state_transcript = ''
self.state_reverse_transcript = ''
#used to switch fainted pokemon without them taking a hit on entry
self.player_attack_effectiveness = ELEMENT_MODIFIER.NUETRAL #Category
self.agent_attack_effectiveness = ELEMENT_MODIFIER.NUETRAL # Category
self.player_must_switch = False
self.agent_must_switch = False
self.player_flinched = False
self.agent_flinched = False
self.player_confused = False
self.agent_confused = False
self.player_moved_first = False
self.player_move_succeeded = False
self.agent_move_succeeded = False
self.reward = 0
self.turns = 0
def simulate_battle(self):
done = False
winner = None
steps = 0
rewards = 0
while done == False:
steps += 1
self.reset_state_transcript()
print()
turns_message = 'Turn %d Begin!\n' %(steps,)
self.printo_magnet(turns_message)
self.append_to_transcript(turns_message)
player_action = self.sample_actions(True)
agent_action = self.sample_actions(False)
self.printo_magnet(str(player_action))
self.printo_magnet(str(agent_action))
self.apply_battle_sequence(player_action, agent_action)
self.end_of_turn_calc()
done, winner = self.is_battle_over()
rewards += self.reward
self.printo_magnet('winner is: %s' % (winner,))
return winner, steps, self.reward
def printo_magnet(self, message):
message = message.strip()
if message == '' or self.should_self_print == False:
return
player_regex = '_p_' # for player replace with nothing, for agent replace with opposing
agent_regex = '_a_' # for player replace with opposing, for agent replace with nothing
message = message.replace('_p_', '')
message = message.replace('_a_', 'Opposing ')
print(message)
def end_of_turn_calc(self):
message = ''
message = '%s\n%s' % (message, Game.apply_status_damage(self.player.curr_pokemon))
message = message.strip()
message = '%s\n%s' % (message, Game.apply_heal_modifiers(self.terrain_condition, self.player.curr_pokemon))
message = message.strip()
message = '%s\n%s' % (message, Game.apply_status_damage(self.computer_agent.curr_pokemon))
message = message.strip()
message = '%s\n%s' % (message, Game.apply_heal_modifiers(self.terrain_condition, self.computer_agent.curr_pokemon))
message = message.strip()
#Lazy reset
self.player_must_switch = False
self.agent_must_switch = False
if self.player.curr_pokemon.curr_health <= 0:
self.reward -= 20
self.player_must_switch = True
message = '_p_%s\n%s has fainted' % (message, self.player.curr_pokemon.name)
message = message.strip()
if self.computer_agent.curr_pokemon.curr_health <= 0:
self.reward += 20
self.agent_must_switch = True
message = '_a_%s\n%s has fainted' % (message, self.computer_agent.curr_pokemon.name)
message = message.strip()
self.printo_magnet(message)
self.append_to_transcript(message)
def encode_field_state(self):
player_choiced_move = '' if self.player_choiced_move == '' else self.player_choiced_move.value
agent_choiced_move = '' if self.agent_choiced_move == '' else self.agent_choiced_move.value
player_selected_pokemon = '' if self.player_selected_pokemon == '' else self.player_selected_pokemon.value
agent_selected_pokemon = '' if self.agent_selected_pokemon == '' else self.agent_selected_pokemon.value
raw_encode = [
all_weather_labels.transform([self.weather_condition]), # category
self.weather_turns,
all_terrains_labels.transform([self.terrain_condition]), # category
self.terrain_turns,
self.bool_to_int(self.player_choiced),
all_attack_slot_labels.transform([player_choiced_move]), # category
self.bool_to_int(self.agent_choiced),
all_attack_slot_labels.transform([agent_choiced_move]), # category
all_pokemon_slot_labels.transform([player_selected_pokemon]), # category
all_pokemon_slot_labels.transform([agent_selected_pokemon]),# category
all_effectiveness_labels.transform([self.player_attack_effectiveness.value]), # category
all_effectiveness_labels.transform([self.agent_attack_effectiveness.value]),# category
self.bool_to_int(self.user_trapped),
self.bool_to_int(self.agent_trapped),
self.bool_to_int(self.player_used_rocks),
self.bool_to_int(self.agent_used_rocks),
self.bool_to_int(self.player_has_substitute),
self.bool_to_int(self.agent_has_substitute),
self.bool_to_int(self.player_must_switch),
self.bool_to_int(self.player_flinched),
self.bool_to_int(self.agent_flinched),
self.bool_to_int(self.player_confused),
self.bool_to_int(self.agent_confused),
self.bool_to_int(self.player_moved_first),
self.bool_to_int(self.player_move_succeeded),
self.bool_to_int(self.agent_move_succeeded),
]
return flatten(raw_encode)
def get_observation(self, includeTranscript=False):
if not includeTranscript:
return self.encode_field_state() + self.player.encode_pokemon() + self.computer_agent.encode_pokemon()
results = {
'transcript': self.state_transcript,
'field': self.encode_field_state(),
'player': self.player.encode_pokemon(),
'agent':self.computer_agent.encode_pokemon(),
'combined': self.encode_field_state() + self.player.encode_pokemon() + self.computer_agent.encode_pokemon(),
'valid_moves_player': self.get_valid_moves_for_player(),
'valid_moves_agent': self.get_valid_moves_for_agent(),
'valid_onehot_player': self.valid_onehot_moves(self.get_valid_moves_for_player()),
'valid_onehot_agent': self.valid_onehot_moves(self.get_valid_moves_for_agent()),
}
return results
def valid_onehot_moves(self, avail_moves):
moves = np.zeros(19)
for move in avail_moves:
moves[move.value] = 1
# moves[np.nonzero(moves==0)] = -500
moves[np.nonzero(moves==0)] = -40000
moves[np.nonzero(moves==1)] = 0
return moves
def bool_to_int(self, value):
return 1 if value else 0
def reset_state_transcript(self):
self.state_transcript = ''
self.state_reverse_transcript = ''
def append_to_transcript(self, message):
message = message.strip()
if message == '':
return
player_regex = '_p_' # for player replace with nothing, for agent replace with opposing
agent_regex = '_a_' # for player replace with opposing, for agent replace with nothing
self.state_transcript = '%s\n%s' % (self.state_transcript, message)
self.state_transcript = self.state_transcript.replace('_p_', '')
self.state_transcript = self.state_transcript.replace('_a_', 'Opposing ')
# apply reverse logic.
self.state_reverse_transcript = '%s\n%s' % (self.state_reverse_transcript, message)
self.state_reverse_transcript = self.state_reverse_transcript.replace('_a_', '')
self.state_reverse_transcript = self.state_reverse_transcript.replace('_p_', 'Opposing ')
def force_switch(self, is_player):
if is_player:
available_switches = [pkmn for pkmn in self.player.pokemon if pkmn.curr_health > 0 and pkmn != self.player.curr_pokemon]
if len(available_switches) > 0:
self.player.curr_pokemon = available_switches[random.randint(0, len(available_switches)-1)]
message = '_p_%s was dragged out' % (self.player.curr_pokemon,)
self.printo_magnet(message)
self.append_to_transcript(message)
else:
available_switches = [pkmn for pkmn in self.computer_agent.pokemon if pkmn.curr_health > 0 and pkmn != self.computer_agent.curr_pokemon]
if len(available_switches) > 0:
self.computer_agent.curr_pokemon = available_switches[random.randint(0, len(available_switches)-1)]
message = '_a_%s was dragged out' % (self.computer_agent.curr_pokemon,)
self.printo_magnet(message)
self.append_to_transcript(message)
def perform_switch(self, switch_action, is_player):
switch_position = switch_action.value - 12
old_pokemon = self.player.curr_pokemon if is_player else self.computer_agent.curr_pokemon
new_pokemon = self.player.switch_curr_pokemon(switch_position) if is_player else self.computer_agent.switch_curr_pokemon(switch_position)
# convert change to switch position
if is_player:
self.player_selected_pokemon = CurrentPokemon(switch_action.value)
else:
self.agent_selected_pokemon = CurrentPokemon(switch_action.value)
self.reset_stats_on_entry(new_pokemon, is_player)
message = 'Come back %s!, Let\'s bring it %s!' % (old_pokemon.name, new_pokemon.name)
self.printo_magnet(message)
self.append_to_transcript(message)
self.apply_entry_hazard(new_pokemon, is_player)
def apply_painsplit(self):
total_hp = self.player.curr_pokemon.curr_health + self.computer_agent.curr_pokemon.curr_health
self.player.curr_pokemon.curr_health = min(self.player.curr_pokemon.max_health, math.ceil(total_hp/2.0))
self.computer_agent.curr_pokemon.curr_health = min(self.computer_agent.curr_pokemon.max_health, math.ceil(total_hp/2.0))
def perform_attack_sequence(self, act, pkmn, enemy, is_player=False):
attack = pkmn.attacks[act.value]
accuracy = attack.accuracy
message = "%s used %s" % (pkmn.name, attack.attack_name)
self.printo_magnet(message)
self.append_to_transcript(message)
attack.pp -= 1
self.printo_magnet('accuracy: %.2f'% (accuracy,))
if not (self.block_next_attack and attack.target != TARGET.SELF) and (accuracy is not True or abs(random.random()) < accuracy):
if is_player:
self.player_move_succeeded = True
else:
self.agent_move_succeeded = True
# Handle status differently. or do NOTHING
if attack.category == CATEGORY.STATUS:
status = attack.status
if status != None:
tar = enemy if attack.target == TARGET.NORMAL else pkmn
self.apply_boosts(status, True, tar)
else:
#duplicate code to determine if super effective or resisted
modifier = 1
modifier *= Game.effective_modifier(enemy.element_1st_type, attack)
modifier *= Game.effective_modifier(enemy.element_2nd_type, attack)
message = ''
effective_reward = 0
if modifier > 1:
message = 'It was super effective'
if is_player:
effective_reward = 5
else:
effective_reward = -5
elif modifier == 0:
message = 'It doesn\'t effect %s' % (enemy.name)
if is_player:
effective_reward = -5
else:
effective_reward = 5
elif modifier < 1:
message = 'It\'s not very effective'
effective_reward = -5
if is_player:
effective_reward = -2
else:
effective_reward = 5
if message != '':
self.printo_magnet(message)
self.append_to_transcript(message)
self.reward += effective_reward
# calculate recoil damage based on health change
attack_damage = self.calculate(attack, pkmn, enemy)
damage_dealt = enemy.curr_health-attack_damage
if is_player and self.player_has_substitute:
recoil_damage_pokemon = math.ceil(min(self.player_substitue_health, damage_dealt)*0.33)
self.player_substitue_health -= attack_damage
if self.player_substitue_health <= 0:
self.player_has_substitute = False
message = '_p_Substitute broke!'
self.printo_magnet(message)
self.append_to_transcript(message)
elif not is_player and self.agent_has_substitute:
recoil_damage_pokemon = math.ceil(min(self.agent_substitue_health, damage_dealt)*0.33)
self.agent_substitue_health -= attack_damage
if self.agent_substitue_health <= 0:
self.agent_has_substitute = False
message = '_a_Substitute broke!'
self.printo_magnet(message)
self.append_to_transcript(message)
else:
health_lost = abs(enemy.curr_health-damage_dealt)
if health_lost > enemy.curr_health:
health_lost = enemy.curr_health
recoil_damage_pokemon = math.ceil(min(enemy.curr_health, abs(damage_dealt))*0.33)
message = "%s lost %.2f health" % (enemy.name, health_lost)
enemy.curr_health = max(0, damage_dealt)
self.printo_magnet(message)
self.append_to_transcript(message)
if attack.has_recoil:
pkmn.curr_health -= recoil_damage_pokemon
message = "%s lost %.2f health from recoil" % (pkmn.name, recoil_damage_pokemon)
self.printo_magnet(message)
self.append_to_transcript(message)
# stealth rocks special conditions
if attack.id == 'stealthrock':
message = 'Jagged rocks thrown on field'
self.printo_magnet(message)
self.append_to_transcript(message)
if is_player:
self.player_used_rocks = True
else:
self.agent_used_rocks = True
if attack.id == 'defog' or attack.attack_name == 'rapidspin':
# reverse for removing rocks
if is_player:
self.agent_used_rocks = False
message = 'Rocks removed from field'
self.printo_magnet(message)
self.append_to_transcript(message)
else:
self.player_used_rocks = False
message = 'Oppsing team removed rocks'
self.printo_magnet(message)
self.append_to_transcript(message)
if attack.id in ['softboiled', 'recover', 'roost', 'morningsun']:
pkmn.curr_health = min(pkmn.curr_health + pkmn.max_health/2, pkmn.max_health)
message = "%s restored some health" % (pkmn.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
if attack.id == 'healbell':
self.heal_all_partners(is_player)
if attack.id == 'roar':
self.force_switch(is_player)
if attack.id == 'painsplit':
self.apply_painsplit()
message1 = '%s shared the pain' % pkmn.name
message2 = '%s shared the pain' % enemy.name
self.printo_magnet(message1)
self.append_to_transcript(message1)
self.printo_magnet(message2)
self.append_to_transcript(message2)
if attack.id == 'protect' and not self.protect_used_last_turn:
self.block_next_attack = True
# apply boosts and effects/
if attack.boosts is not None and len(attack.boosts) > 0:
for boost in attack.boosts:
if isinstance(attack.boosts, dict):
boost = (boost, attack.boosts[boost])
tar = enemy if attack.target == TARGET.NORMAL else pkmn
self.apply_boosts(boost, True, tar)
twenty_five_percent = pkmn.max_health / 4.0
if attack.id == 'substitute' and pkmn.curr_health > twenty_five_percent:
pkmn.curr_health -= twenty_five_percent
if is_player:
self.player_has_substitute = True
self.player_substitue_health = twenty_five_percent
else:
self.agent_has_substitute = True
self.agent_substitue_health = twenty_five_percent
# apply secondary
secondary = attack.secondary
if secondary is not None:
# apply boosts and effects/
if secondary.boosts is not None and len(secondary.boosts) > 0:
for boost in secondary.boosts:
tar = enemy if attack.target == TARGET.NORMAL else pkmn
self.apply_boosts(boost, True, tar)
if secondary.status is not None:
tar = pkmn if secondary.is_target_self else enemy
self.apply_boosts(secondary.status, secondary.chance, tar)
else:
message = 'But it failed/Missed'
if is_player:
self.player_move_succeeded = False
else:
self.agent_move_succeeded = False
self.printo_magnet(message)
self.append_to_transcript(message)
self.block_next_attack = False
def heal_all_partners(self, is_player):
if is_player:
for pkmn in self.player.pokemon:
if pkmn.status != Status.NOTHING:
pkmn.status = Status.NOTHING
message = '%s is cured' % (pkmn.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
else:
for pkmn in self.computer_agent.pokemon:
if pkmn.status != Status.NOTHING:
pkmn.status = Status.NOTHING
message = '%s is cured' % (pkmn.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
def apply_boosts(self, boost, chance, target):
if chance is not True and random.random() > chance:
return
if boost == 'par':
target.status = Status.PARALYSIS
message = '%s is paralyzed' % (target.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
return
if boost == 'brn':
target.status = Status.BURN
message = '%s is burned' % (target.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
return
if boost == 'psn':
target.status = Status.POISON
message = '%s is poisoned' % (target.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
return
if boost == 'tox':
target.status = Status.TOXIC
message = '%s is badly poisoned' % (target.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
return
# perhaps an unimplemented status early exit
if not isinstance(boost, tuple):
return
stat, val = boost[0], boost[1]
modifier_value = 1.5 if val > 0 else 0.5
incre_decre = 'increased' if val > 0 else 'decreased'
if stat == 'atk':
target.attack_modifier *= modifier_value
if stat == 'def':
target.defense_modifier *= modifier_value
if stat == 'spa':
target.spatk_modifier *= modifier_value
if stat == 'spd':
target.spdef_modifier *= modifier_value
if stat == 'spe':
target.speed_modifier *= modifier_value
if stat == 'evasion':
target.evasion_modifier *= modifier_value
if stat == 'accuracy':
target.accuracy_modifier *= modifier_value
message = '%s\'s %s has %s'% (target.name, stat, incre_decre)
self.printo_magnet(message)
self.append_to_transcript(message)
def reset_stats_on_entry(self, pokemon, is_player=True):
pokemon.accuracy_modifier = 1
pokemon.attack_modifier = 1
pokemon.spatk_modifier = 1
pokemon.defense_modifier = 1
pokemon.spdef_modifier = 1
pokemon.speed_modifier = 1
pokemon.accuracy_modifier = 1
message = Game.apply_natural_cure(pokemon)
self.printo_magnet(message)
self.append_to_transcript(message)
# reset choice
if is_player:
self.player_choiced = False
self.player_has_substitute = False
self.player_substitue_health = 0
self.player_choiced_move = ''
else:
self.agent_choiced = False
self.agent_has_substitute = False
self.agent_substitue_health = 0
self.agent_choiced_move = ''
def apply_entry_hazard(self, pokemon, is_player=True):
if is_player and self.agent_used_rocks:
# remove 25 % health
pokemon.curr_health = max(0, pokemon.curr_health-(pokemon.max_health/4))
message = 'Rocks dugged into _p_ %s' % (pokemon.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
if is_player == False and self.player_used_rocks:
# remove 25 % health
pokemon.curr_health = max(0, pokemon.curr_health-(pokemon.max_health/4))
message = 'Rocks dugged into _a_ %s' % (pokemon.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
def sample_actions(self, is_player=False):
if is_player:
actions = self.get_valid_moves_for_player()
else:
actions = self.get_valid_moves_for_agent()
return np.random.choice(actions, 1)[0]
def take_action_if_valid_else_random_actions(self, action):
actions = self.get_valid_moves_for_player()
if action in actions:
return action
return np.random.choice(actions, 1)[0]
def is_battle_over(self):
player_living_pokemon_count = len([pkmn for pkmn in self.player.pokemon if pkmn.curr_health > 0])
agent_living_pokemon_count = len([pkmn for pkmn in self.computer_agent.pokemon if pkmn.curr_health > 0])
if player_living_pokemon_count == 0:
self.reward -= 100
return True, 'agent'
if agent_living_pokemon_count == 0:
self.reward += 100
return True, 'player'
return False, None
def get_valid_moves_for_player(self):
curr_pokemon = self.player.curr_pokemon
pokemon = self.player.pokemon
valid_moves = []
if curr_pokemon.curr_health > 0:
if self.player_choiced:
if curr_pokemon.attacks[self.player_choiced_move].pp > 0:
valid_moves.append(Actions(self.player_choiced_move))
else:
for idx, atk in enumerate(curr_pokemon.attacks):
if atk is not None and atk.pp > 0:
valid_moves.append(Actions(idx))
if len(valid_moves) == 0: # can only struggle
valid_moves.append(Actions.Attack_Struggle)
for idx, pkmn in enumerate(self.player.pokemon):
if pkmn.curr_health > 0 and pkmn is not curr_pokemon:
valid_moves.append(Actions(12+idx))
return valid_moves
def get_valid_moves_for_agent(self):
curr_pokemon = self.computer_agent.curr_pokemon
pokemon = self.computer_agent.pokemon
valid_moves = []
if curr_pokemon.curr_health > 0:
if self.agent_choiced:
if curr_pokemon.attacks[self.agent_choiced_move].pp > 0:
valid_moves.append(Actions(self.player_choiced_move))
else:
for idx, atk in enumerate(curr_pokemon.attacks):
if atk is not None and atk.pp > 0:
valid_moves.append(Actions(idx))
if len(valid_moves) == 0: # can only struggle
valid_moves.append(Actions.Attack_Struggle)
for idx, pkmn in enumerate(self.computer_agent.pokemon):
if pkmn.curr_health > 0 and pkmn is not curr_pokemon:
valid_moves.append(Actions(12+idx))
return valid_moves
# dont handle u turn but it is good to think about.
def apply_battle_sequence(self, action, computer_action):
assert isinstance(action, Actions)
assert isinstance(computer_action, Actions)
# reset
self.player_attack_effectiveness = ELEMENT_MODIFIER.NUETRAL #Category
self.agent_attack_effectiveness = ELEMENT_MODIFIER.NUETRAL # Category
player_is_first, actions_order = Game.get_action_order(self.player, self.computer_agent, action, computer_action)
# When pokemon faint, only valid switches are allowed.
if self.player_must_switch or self.agent_must_switch:
if self.player_must_switch and action in [Actions.Change_Pokemon_Slot_1, Actions.Change_Pokemon_Slot_2, Actions.Change_Pokemon_Slot_3, Actions.Change_Pokemon_Slot_4, Actions.Change_Pokemon_Slot_5, Actions.Change_Pokemon_Slot_6]:
self.perform_switch(action, True)
if self.agent_must_switch and computer_action in [Actions.Change_Pokemon_Slot_1, Actions.Change_Pokemon_Slot_2, Actions.Change_Pokemon_Slot_3, Actions.Change_Pokemon_Slot_4, Actions.Change_Pokemon_Slot_5, Actions.Change_Pokemon_Slot_6]:
self.perform_switch(computer_action, False)
#dont allow any fighting
return
self.player_moved_first = player_is_first
for idx, act in enumerate(actions_order):
# perform here in case switch was engaged
player_pokemon = self.player.curr_pokemon
agent_pokemon = self.computer_agent.curr_pokemon
if idx == 0:
is_player = player_is_first
else:
is_player = not player_is_first
if is_player:
pkmn = player_pokemon
enemy = agent_pokemon
else:
pkmn = agent_pokemon
enemy = player_pokemon
# Handle switch
if act in [Actions.Change_Pokemon_Slot_1, Actions.Change_Pokemon_Slot_2, Actions.Change_Pokemon_Slot_3, Actions.Change_Pokemon_Slot_4, Actions.Change_Pokemon_Slot_5, Actions.Change_Pokemon_Slot_6]:
self.perform_switch(act, is_player)
self.reward -= 2
# if pokemon fainted, cant attack
if pkmn.curr_health <= 0:
continue
if act in [Actions.Attack_Slot_1, Actions.Attack_Slot_2, Actions.Attack_Slot_3, Actions.Attack_Slot_4]:
self.perform_attack_sequence(act, pkmn, enemy, is_player)
# if contains a choice, lock attack
if pkmn.item == ITEMS.CHOICE_SCARF:
if is_player:
self.player_choiced = True
self.player_choiced_move = act
else:
self.agent_choiced = True
self.agent_choiced_move = act
# struggle deals 10% damage to target, 25% to user. Keep it simple
if act == Actions.Attack_Struggle:
enemy.curr_health = max(0, enemy.curr_health-(enemy.max_health/10))
pkmn.curr_health = min(0, pkmn.curr_health-(pkmn.max_health/4))
message = '%s used struggle ' % (pkmn.name,)
self.printo_magnet(message)
self.append_to_transcript(message)
#reset protect
if self.protect_used_last_turn:
self.protect_used_last_turn = False
if self.block_next_attack:
self.block_next_attack = False
self.protect_used_last_turn = True
def calculate(self, attack, pkmn, target):
base_atk = pkmn.atk if attack.category == CATEGORY.PHYSICAL else pkmn.spatk
base_def = target.defense if attack.category == CATEGORY.PHYSICAL else target.spdef
return (((2*pkmn.level/5+2) * attack.power * (base_atk/base_def))/50 + 2) * self.get_modifier(pkmn, target, attack)
def get_modifier(self, pokemon, enemy, attack):
modifier = 1.5 if self.is_stab(pokemon, attack) else 1
modifier *= Game.apply_attack_modifiers(attack, pokemon, enemy, self.weather_condition, self.terrain_condition)
message = 'modifier is: %.2f' % (modifier,)
self.printo_magnet(message)
self.append_to_transcript(message)
return modifier
def is_stab(self, pokemon, attack):
return pokemon.element_1st_type == attack.element_type or pokemon.element_2nd_type == attack.element_type
@property
def shape(self):
# player has 6 pokemon with 16 slots
# computer agent has 6 pokemon with 11 slots since some info hidden.
# return (len(self.encode()['combined']), )
return (len(self.encode()), )
def encode(self, includeTranscript=False):
"""
Convert current state into numpy array.
"""
return self.get_observation(includeTranscript)
def step(self, player_action, agent_action):
"""
Perform one step in our price, adjust offset, check for the end of prices
and handle position change
:param action:
:return: reward, done
"""
assert isinstance(player_action, Actions)
self.reward = 0
self.reset_state_transcript()
self.turns += 1
turns_message = 'Turn %d Begin!\n' %(self.turns,)
self.printo_magnet(turns_message)
self.append_to_transcript(turns_message)
self.printo_magnet(str(player_action))
self.printo_magnet(str(agent_action))
self.apply_battle_sequence(player_action, agent_action)
self.end_of_turn_calc()
done, winner = self.is_battle_over()
# Penalty for taking too long
if self.turns >= 150:
done = True
self.reward = -500
winner = 'agent'
return self.reward, done, winner
class PokeEnv(gym.Env):
#class PokeEnv():
metadata = {'render.modes': ['human']}
def __init__(self, player, computer_agent, computer_network=None):
# if network exists, use that decide moves. Otherwise use random move.
self._state = State(player, computer_agent)
self.player = player
self.computer_agent = computer_agent
self.computer_network = computer_network
self.action_space = gym.spaces.Discrete(n=len(Actions))
# self.observation_space = gym.spaces.Discrete(n=self._state.shape[0])
self.observation_space = gym.spaces.Box(low=0, high=255, shape=(1, self._state.shape[0]), dtype=np.float32)
self.seed()
def set_network(computer_network):
self.computer_network = computer_network
def sample_actions(self):
return self._state.sample_actions(True)
def reset(self):
self.player.pokemon = get_random_pokemon_team()
self.computer_agent.pokemon = get_random_pokemon_team()
self._state.reset(self.player, self.computer_agent)
return self._state.encode()
def get_current_transcript(self):
return self._state.state_transcript
def step(self, action_idx):
action = Actions(action_idx)
if self.computer_network == None:
agent_action = self._state.sample_actions(False)
newaction = self._state.take_action_if_valid_else_random_actions(action)
if action != newaction and False:
print('Bot chose %s but got %s' % (action, newaction))
obs = self._state.encode(includeTranscript=True)
print('prev: valid_onehot', obs['valid_onehot_player'])
reward, done, winner = self._state.step(newaction, agent_action)
obs = self._state.encode(includeTranscript=True)
info = {"transcript":obs['transcript'], "valid_onehot_player": obs['valid_onehot_player'], 'winner': winner}
# print('valid_onehot', obs['valid_onehot_player'])
return obs['combined'], reward, done, info
def render(self, mode='human', close=False):
pass
def close(self):
pass
def seed(self, seed=None):
self.np_random, seed1 = seeding.np_random(seed)
seed2 = seeding.hash_seed(seed1 + 1) % 2 ** 31
return [seed1, seed2]
class RewardScaler(gym.RewardWrapper):
""" may not be needed for fake pokemon?
Bring rewards to a reasonable scale for PPO.
This is incrediably important and effects performance drastically
"""
def reward(self, reward):
return reward * 0.01
class Game(object):
@staticmethod
def get_action_order(player, computer_agent, action, computer_action):
actions_order = []
# switches happen first
if computer_action in [Actions.Change_Pokemon_Slot_1, Actions.Change_Pokemon_Slot_2, Actions.Change_Pokemon_Slot_3, Actions.Change_Pokemon_Slot_4, Actions.Change_Pokemon_Slot_5, Actions.Change_Pokemon_Slot_6]:
actions_order.append(computer_action)
actions_order.append(action)
return False, actions_order
if action in [Actions.Change_Pokemon_Slot_1, Actions.Change_Pokemon_Slot_2, Actions.Change_Pokemon_Slot_3, Actions.Change_Pokemon_Slot_4, Actions.Change_Pokemon_Slot_5, Actions.Change_Pokemon_Slot_6]:
actions_order.append(action)
actions_order.append(computer_action)
return True, actions_order
# Handle struggle case
if action == Actions.Attack_Struggle:
actions_order.append(computer_action)
actions_order.append(action)
return False, actions_order
if computer_action == Actions.Attack_Struggle:
actions_order.append(action)
actions_order.append(computer_action)
return True, actions_order
player_pokemon = player.curr_pokemon