-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathState.py
284 lines (265 loc) · 13.9 KB
/
State.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
from Pokemon import Pokemon
import json
import numpy as np
class State:
def __init__(self):
self.ownId = ''
self.enemyId = ''
self.ownActive = 0
self.enemyActive = 0
self.ownTeam = [Pokemon(), Pokemon(), Pokemon(), Pokemon(), Pokemon(), Pokemon()]
self.enemyTeam = [Pokemon(), Pokemon(), Pokemon(), Pokemon(), Pokemon(), Pokemon()]
self.forceSwitch = False
self.trapped = False
self.disabled = [False, False, False, False]
self.ownTransform = -1
self.enemyTransform = -1
self.ownStatBoost = [0, 0, 0, 0]
self.enemyStatBoost = [0, 0, 0, 0]
self.parsableChanges = ['switch','move','-damage','-heal','faint','-status','-curestatus','-cureteam','-transform','-boost','-unboost',]
with open('moves.json', 'r') as f:
self.moveCode = json.load(f)
with open('status.json', 'r') as f:
self.statusCode = json.load(f)
with open('pokemons.json', 'r') as f:
self.pokemonCode = json.load(f)
# parse changes to own team based on json
def updateSelf(self,json):
#print(json)
#print(json['side'])
#print(json['side']['pokemon'])
#self.ownId = json['side']['id']
#self.enemyId = 'p2' if self.ownId == 'p1' else 'p1'
i = 0
while(not json['side']['pokemon'][i]['active']):
i += 1
self.ownActive = i
self.ownTeam[0].update(json['side']['pokemon'][0])
self.ownTeam[1].update(json['side']['pokemon'][1])
self.ownTeam[2].update(json['side']['pokemon'][2])
self.ownTeam[3].update(json['side']['pokemon'][3])
self.ownTeam[4].update(json['side']['pokemon'][4])
self.ownTeam[5].update(json['side']['pokemon'][5])
self.forceSwitch = 'forceSwitch' in json
self.trapped = False
if('active' in json):
self.disabled[0] = False if 'disabled' not in json['active'][0]['moves'][0] else json['active'][0]['moves'][0]['disabled']
self.disabled[1] = True if len(json['active'][0]['moves']) <= 1 else json['active'][0]['moves'][1]['disabled']
self.disabled[2] = True if len(json['active'][0]['moves']) <= 2 else json['active'][0]['moves'][2]['disabled']
self.disabled[3] = True if len(json['active'][0]['moves']) <= 3 else json['active'][0]['moves'][3]['disabled']
self.trapped = 'trapped' in json['active'][0]
# parse chanages based on text update
def parseChange(self,change,name):
# print(change)
for line in change:
parts = line[1:].split('|')
#print(parts[0])
#print(len(parts))
#if len(parts)>=3:
# print(parts[2])
# print(name)
# print(parts[2] == name)
if parts[0] == 'player' and len(parts)>=3 and parts[2] == name:
self.ownId = parts[1]
self.enemyId = 'p2' if self.ownId == 'p1' else 'p1'
if parts[0] == 'player' and len(parts)>=3 and parts[2] != name:
self.enemyId = parts[1]
self.ownId = 'p2' if self.enemyId == 'p1' else 'p1'
if parts and parts[0] in self.parsableChanges:
#print(line)
isOwn = parts[1].startswith(self.ownId)
inPokemon = parts[1].split(' ')[1]
match parts[0]:
case 'switch':
#print(parts[1])
#print(isOwn)
#print(self.ownId)
if isOwn:
self.ownTransform = -1
self.ownStatBoost = [0, 0, 0, 0]
else:
self.enemyTransform = -1
self.enemyStatBoost = [0, 0, 0, 0]
inInfo = parts[2].split(',')
inPokemon = inInfo[0]
inLevel = 100 if len(inInfo)==1 else int(inInfo[1].translate(str.maketrans('', '', 'L ')))
self.enemyActive = 0
for pk in self.enemyTeam:
if pk.specie == inPokemon:
break
if pk.specie == '':
pk.specie = inPokemon
pk.level = inLevel
pk.getStats(inPokemon)
break
self.enemyActive += 1
case 'move':
if not isOwn:
inMove = parts[2]
#print(inPokemon)
#print(inMove)
for pk in self.enemyTeam:
if pk.specie == inPokemon:
for i in range(4):
if pk.moves[i] == inMove:
break
if pk.moves[i] == '':
pk.moves[i] = inMove
break
break
case '-damage':
if not isOwn:
inHealth = 0.0 if parts[2] == '0 fnt' else int(parts[2].split(' ')[0].split('/')[0])/int(parts[2].split(' ')[0].split('/')[1])
#print(inPokemon)
#print(inHealth)
for pk in self.enemyTeam:
if pk.specie == inPokemon:
pk.health = inHealth
break
case '-heal':
if not isOwn:
inHealth = 0.0 if parts[2] == '0 fnt' else int(parts[2].split(' ')[0].split('/')[0])/int(parts[2].split(' ')[0].split('/')[1])
#print(inPokemon)
#print(inHealth)
for pk in self.enemyTeam:
if pk.specie == inPokemon:
pk.health = inHealth
break
case 'faint':
if not isOwn:
for pk in self.enemyTeam:
if pk.specie == inPokemon:
pk.health = 0.0
pk.status = 'fnt'
break
case '-status':
if not isOwn:
for pk in self.enemyTeam:
if pk.specie == inPokemon:
pk.status = parts[2]
break
case '-curstatus':
if not isOwn:
for pk in self.enemyTeam:
if pk.specie == inPokemon:
pk.status = ''
break
case '-cureteam':
if not isOwn:
for pk in self.enemyTeam:
pk.status = ''
case '-transform':
if isOwn:
self.ownTransform = self.enemyActive
else:
self.enemyTransform = self.ownActive
case '-boost':
if not isOwn:
if parts[2] == 'atk':
self.enemyStatBoost[0] += int(parts[3])
if parts[2] == 'def':
self.enemyStatBoost[1] += int(parts[3])
if parts[2] == 'spa':
self.enemyStatBoost[2] += int(parts[3])
if parts[2] == 'spe':
self.enemyStatBoost[3] += int(parts[3])
if isOwn:
if parts[2] == 'atk':
self.ownStatBoost[0] += int(parts[3])
if parts[2] == 'def':
self.ownStatBoost[1] += int(parts[3])
if parts[2] == 'spa':
self.ownStatBoost[2] += int(parts[3])
if parts[2] == 'spe':
self.ownStatBoost[3] += int(parts[3])
case '-unboost':
if not isOwn:
if parts[2] == 'atk':
self.enemyStatBoost[0] -= int(parts[3])
if parts[2] == 'def':
self.enemyStatBoost[1] -= int(parts[3])
if parts[2] == 'spa':
self.enemyStatBoost[2] -= int(parts[3])
if parts[2] == 'spe':
self.enemyStatBoost[3] -= int(parts[3])
if isOwn:
if parts[2] == 'atk':
self.ownStatBoost[0] -= int(parts[3])
if parts[2] == 'def':
self.ownStatBoost[1] -= int(parts[3])
if parts[2] == 'spa':
self.ownStatBoost[2] -= int(parts[3])
if parts[2] == 'spe':
self.ownStatBoost[3] -= int(parts[3])
case _:
pass
# get list of valid moves (as list of 0,1)
def getAvailableMoves(self):
validMoves = [0 for _ in range(10)]
for i in range(4):
if not self.disabled[i] and not self.forceSwitch:
validMoves[i] = 1
for i in range(6):
if i != self.ownActive and self.ownTeam[i].status != 'fnt' and not self.trapped:
validMoves[i+4] = 1
if self.trapped:
validMoves
return validMoves
def __str__(self):
return 'Own Id: ' + str(self.ownId) + '\nEnemy Id: ' + str(self.enemyId) + '\n'\
+ self.ownTeam[0].__str__() + '\n' + self.ownTeam[1].__str__() + '\n' + self.ownTeam[2].__str__() + '\n' + self.ownTeam[3].__str__()\
+ '\n' + self.ownTeam[4].__str__() + '\n' + self.ownTeam[5].__str__() + '\nOwn Active: ' + str(self.ownActive) + '\nForce Switch: ' + str(self.forceSwitch)\
+ '\nTrapped: ' + str(self.trapped) + '\nOwn Transform: ' + str(self.ownTransform) + '\nDisabled: ' + str(self.disabled[0]) + ':' + str(self.disabled[1]) + ':' + str(self.disabled[2]) + ':' + str(self.disabled[3])\
+ '\nOwn Stats: ' + str(self.ownStatBoost[0]) + ':' + str(self.ownStatBoost[1]) + ':' + str(self.ownStatBoost[2]) + ':' + str(self.ownStatBoost[3])\
+ '\n' + self.enemyTeam[0].__str__() + '\n' + self.enemyTeam[1].__str__() + '\n' + self.enemyTeam[2].__str__() + '\n' + self.enemyTeam[3].__str__()\
+ '\n' + self.enemyTeam[4].__str__() + '\n' + self.enemyTeam[5].__str__() + '\nEnemy Active: ' + str(self.enemyActive) + '\nEnemy Transform: ' + str(self.enemyTransform)\
+ '\nEnemy Stats: ' + str(self.enemyStatBoost[0]) + ':' + str(self.enemyStatBoost[1]) + ':' + str(self.enemyStatBoost[2]) + ':' + str(self.enemyStatBoost[3])
def getPokemonCode(self, pokemon):
try:
return self.pokemonCode.index(pokemon)
except:
self.pokemonCode.append(pokemon)
with open('pokemons.json', 'w') as f:
json.dump(self.pokemonCode, f)
return self.pokemonCode.index(pokemon)
def getStatusCode(self,status):
try:
return self.statusCode.index(status)
except:
self.statusCode.append(status)
with open('status.json', 'w') as f:
json.dump(self.statusCode, f)
return self.statusCode.index(status)
def getMoveCode(self,move):
try:
return self.moveCode.index(move)
except:
self.moveCode.append(move)
with open('moves.json', 'w') as f:
json.dump(self.moveCode, f)
return self.moveCode.index(move)
def getPokemonState(self, pokemon):
return [self.getPokemonCode(pokemon.specie), pokemon.level, pokemon.health, \
self.getStatusCode(pokemon.status),self.getMoveCode(pokemon.moves[0]), self.getMoveCode(pokemon.moves[1]), \
self.getMoveCode(pokemon.moves[2]), self.getMoveCode(pokemon.moves[3]), pokemon.atk, pokemon.defe,
pokemon.spa, pokemon.spd, pokemon.spe]
def getState(self):
selfActiveNumber = self.ownActive if self.ownTransform == -1 else self.ownTransform
enemyActiveNumber = self.enemyActive if self.enemyTransform == -1 else self.enemyTransform
return np.concatenate([self.getPokemonState(self.ownTeam[selfActiveNumber]),
self.getPokemonState(self.enemyTeam[enemyActiveNumber]),
self.getPokemonState(self.ownTeam[0]),
self.getPokemonState(self.ownTeam[1]),
self.getPokemonState(self.ownTeam[2]),
self.getPokemonState(self.ownTeam[3]),
self.getPokemonState(self.ownTeam[4]),
self.getPokemonState(self.ownTeam[5]),
self.getPokemonState(self.enemyTeam[0]),
self.getPokemonState(self.enemyTeam[1]),
self.getPokemonState(self.enemyTeam[2]),
self.getPokemonState(self.enemyTeam[3]),
self.getPokemonState(self.enemyTeam[4]),
self.getPokemonState(self.enemyTeam[5]),
self.ownStatBoost,
self.enemyStatBoost
],axis=None)