-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattle_State.py
More file actions
230 lines (146 loc) · 12 KB
/
Copy pathBattle_State.py
File metadata and controls
230 lines (146 loc) · 12 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
import ujson as json
import random
from enum import Enum
from RAIchu_Enums import MoveType, AIType, PredictionType, Player
from Battle_Resources import Battle_Resources
from RAIchu_Utils import RAIchu_Utils
class Battle_State():
def __init__(self, current_state_str, move_required, prev_action):
self.state_str = current_state_str
self.info = json.loads(current_state_str)
self.move_required = move_required
self.opp_move_required = MoveType.BATTLE_ACTION
#prev_action is the set of actions (player, opponent) taken to reach this state. Both are -1 if this is the root
#of the search tree.
self.prev_action = prev_action
def generate_player_moves(self):
if self.move_required == MoveType.NONE:
return []
moves = [0] *(RAIchu_Utils.NUM_MOVES + RAIchu_Utils.NUM_POKEMON)
if self.move_required == MoveType.BATTLE_ACTION or self.move_required == MoveType.BATTLE_SWITCH:
for i in range(len(self.info['pokemon'])):
if i != self.info['active'] and self.info['pokemon'][i]['condition'] != 0:
moves[i] = 1
if self.move_required == MoveType.BATTLE_ACTION:
for i in range(len(self.info['pokemon'][self.info['active']]['moves'])):
if self.info['pokemon'][self.info['active']]['disabled'][i] == 0:
moves[RAIchu_Utils.NUM_POKEMON+i] = 1
return moves
def generate_opponent_moves(self):
#Generates all moves the opponent could make from the current game state. Considers all possible moves
#of all pokemon the opponent is known to have.
if self.opp_move_required == MoveType.NONE:
return []
moves = [0] *(len(self.info['opp_pokemon'][self.info['opp_active']]['possible_moves']) + RAIchu_Utils.NUM_POKEMON)
if self.opp_move_required == MoveType.BATTLE_ACTION or self.opp_move_required == MoveType.BATTLE_SWITCH:
for i in range(len(self.info['opp_pokemon'])):
if i != self.info['opp_active']:
if len(self.info['opp_pokemon'][i]) == 0:
#Unknown state
moves[i] = -1
elif self.info['opp_pokemon'][i]['condition'] != 0:
moves[i] = 1
if self.opp_move_required == MoveType.BATTLE_ACTION:
for i in range(len(self.info['opp_pokemon'][self.info['opp_active']]['possible_moves'])):
if self.info['opp_pokemon'][self.info['opp_active']]['disabled'][i] == 0:
moves[RAIchu_Utils.NUM_POKEMON+i] = 1
return moves
def generate_next_game_states(self, pred_type):
#Generates all the game states reachable from a single valid action from the current game state
next_states = []
#on a forced switch, only one player will take an action
if self.move_required == MoveType.BATTLE_SWITCH:
switches = self.generate_player_moves()
next_states = [-1 for k in range(len(switches))]
for i in range(len(switches)):
if switches[i] == 1:
next_states[i] = self.simulate_action_sequence(i, -1, pred_type)
if self.opp_move_required == MoveType.BATTLE_SWITCH:
switches = self.generate_opponent_moves()
next_states = [-1 for k in range(len(switches))]
for i in range(len(switches)):
if switches[i] == 1:
next_states[i] = self.simulate_action_sequence(-1, i, pred_type)
if self.move_required == MoveType.BATTLE_ACTION and self.opp_move_required == MoveType.BATTLE_ACTION:
action_sequences = self.generate_action_sequences()
next_states = action_sequences.copy()
for i in range(len(action_sequences)):
for j in range(len(action_sequences[0])):
if action_sequences[i][j] == 1:
next_states[i][j] = self.simulate_action_sequence(i, j, pred_type)
else:
next_states[i][j] = -1
return next_states
def generate_action_sequences(self):
move_vec = self.generate_player_moves()
opp_move_vec = self.generate_opponent_moves()
actions = [[-1]*(len(opp_move_vec)) for k in range((RAIchu_Utils.NUM_MOVES + RAIchu_Utils.NUM_POKEMON))]
for i in range((RAIchu_Utils.NUM_MOVES + RAIchu_Utils.NUM_POKEMON)):
for j in range(len(opp_move_vec)):
if move_vec[i] == 1:
actions[i][j] = opp_move_vec[j]
return actions
def simulate_action_sequence(self, move, opp_move, pred_type):
#Simulate what the game state will be the next turn if the given actions are taken. Returns a new Battle_State
self.update_state_str()
next_state = Battle_State(self.state_str, MoveType.BATTLE_ACTION, (move, opp_move))
#Switches always happen first. Relative order of switches does not matter.
if move < RAIchu_Utils.NUM_POKEMON and move >= 0:
RAIchu_Utils.apply_switch(next_state, Player.SELF, move)
if opp_move >= RAIchu_Utils.NUM_POKEMON:
RAIchu_Utils.simulate_move(next_state.info['opp_pokemon'][next_state.info['opp_active']], next_state.info['pokemon'][next_state.info['active']], Player.OPPONENT, next_state.info['field'], next_state.info['opp_pokemon'][next_state.info['opp_active']]['possible_moves'][opp_move-RAIchu_Utils.NUM_POKEMON], pred_type)
if opp_move < RAIchu_Utils.NUM_POKEMON and opp_move >= 0:
RAIchu_Utils.apply_switch(next_state, Player.OPPONENT, opp_move)
if move >= RAIchu_Utils.NUM_POKEMON:
RAIchu_Utils.simulate_move(next_state.info['pokemon'][next_state.info['active']], next_state.info['opp_pokemon'][next_state.info['opp_active']], Player.SELF, next_state.info['field'], next_state.info['pokemon'][next_state.info['active']]['moves'][move-RAIchu_Utils.NUM_POKEMON], pred_type)
#if both sides attack, the pokemon with a higher speed stat acts first. If the pokemon with lower speed has no hp left after
#the pokemon with higher speed attacks, they will not get to attack and will need to switch.
if move >= RAIchu_Utils.NUM_POKEMON and opp_move >= RAIchu_Utils.NUM_POKEMON:
if RAIchu_Utils.get_stat(next_state.info['opp_pokemon'][next_state.info['opp_active']], 'spe') > RAIchu_Utils.get_stat(next_state.info['pokemon'][next_state.info['active']], 'spe'):
RAIchu_Utils.simulate_move(next_state.info['opp_pokemon'][next_state.info['opp_active']], next_state.info['pokemon'][next_state.info['active']], Player.OPPONENT, next_state.info['field'], next_state.info['opp_pokemon'][next_state.info['opp_active']]['possible_moves'][opp_move-RAIchu_Utils.NUM_POKEMON], pred_type)
if next_state.info['pokemon'][next_state.info['active']]['condition'] > 0:
RAIchu_Utils.simulate_move(next_state.info['pokemon'][next_state.info['active']], next_state.info['opp_pokemon'][next_state.info['opp_active']], Player.SELF, next_state.info['field'], next_state.info['pokemon'][next_state.info['active']]['moves'][move-RAIchu_Utils.NUM_POKEMON], pred_type)
else:
RAIchu_Utils.simulate_move(next_state.info['pokemon'][next_state.info['active']], next_state.info['opp_pokemon'][next_state.info['opp_active']], Player.SELF, next_state.info['field'], next_state.info['pokemon'][next_state.info['active']]['moves'][move-RAIchu_Utils.NUM_POKEMON], pred_type)
if next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] > 0:
RAIchu_Utils.simulate_move(next_state.info['opp_pokemon'][next_state.info['opp_active']], next_state.info['pokemon'][next_state.info['active']], Player.OPPONENT, next_state.info['field'],next_state.info['opp_pokemon'][next_state.info['opp_active']]['possible_moves'][opp_move-RAIchu_Utils.NUM_POKEMON], pred_type)
if next_state.info['pokemon'][next_state.info['active']]['condition'] <= 0:
next_state.move_required = MoveType.BATTLE_SWITCH
next_state.opp_move_required = MoveType.NONE
next_state.info['pokemon'][next_state.info['active']]['condition'] = 0
else:
if next_state.info['pokemon'][next_state.info['active']]['item'] == 'leftovers':
next_state.info['pokemon'][next_state.info['active']]['condition'] += 6
if next_state.info['pokemon'][next_state.info['active']]['condition'] > 100:
next_state.info['pokemon'][next_state.info['active']]['condition'] = 100
if 'burn' in next_state.info['pokemon'][next_state.info['active']]['status']:
next_state.info['pokemon'][next_state.info['active']]['condition'] -= 6
elif 'psn' in next_state.info['pokemon'][next_state.info['active']]['status'] or 'tox' in next_state.info['pokemon'][next_state.info['active']]['status']:
next_state.info['pokemon'][next_state.info['active']]['condition'] -= 12
if next_state.info['pokemon'][next_state.info['active']]['condition'] <= 0:
next_state.info['pokemon'][next_state.info['active']]['condition'] = 0
next_state.move_required = MoveType.BATTLE_SWITCH
next_state.opp_move_required = MoveType.NONE
if next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] <= 0:
next_state.move_required = MoveType.NONE
next_state.opp_move_required = MoveType.BATTLE_SWITCH
next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] = 0
else:
if next_state.info['opp_pokemon'][next_state.info['opp_active']]['item'] == 'leftovers':
next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] += 6
if next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] > 100:
next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] = 100
if 'burn' in next_state.info['opp_pokemon'][next_state.info['opp_active']]['status']:
next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] -= 6
elif 'psn' in next_state.info['opp_pokemon'][next_state.info['opp_active']]['status'] or 'tox' in next_state.info['opp_pokemon'][next_state.info['opp_active']]['status']:
next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] -= 12
if next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] <= 0:
next_state.info['opp_pokemon'][next_state.info['opp_active']]['condition'] = 0
next_state.move_required = MoveType.BATTLE_SWITCH
next_state.opp_move_required = MoveType.NONE
#If both pokemon faint on the same turn due to status or recoil
if next_state.info['pokemon'][next_state.info['active']]['condition'] == 0:
next_state.move_required = MoveType.BATTLE_SWITCH
return next_state
def update_state_str(self):
self.state_str = json.dumps(self.info)