forked from ScottehMax/pyMon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
battleutils.py
262 lines (192 loc) · 7.55 KB
/
battleutils.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
import json
import random
from requests_futures.sessions import FuturesSession
import utils
POKEDEX = json.load(open('./BattlePokedex.json'))
TYPES = json.load(open('./BattleTypeChart.json'))
MOVES = json.load(open('./BattleMovedex.json'))
def natureboost(nature):
table = {"Adamant": {"plus": "atk", "minus": "spa"},
"Bashful": {},
"Bold": {"plus": "def", "minus": "atk"},
"Brave": {"plus": "atk", "minus": "spe"},
"Calm": {"plus": "spd", "minus": "atk"},
"Careful": {"plus": "spd", "minus": "spa"},
"Docile": {},
"Gentle": {"plus": "spd", "minus": "def"},
"Hardy": {},
"Hasty": {"plus": "spe", "minus": "def"},
"Impish": {"plus": "def", "minus": "spa"},
"Jolly": {"plus": "spe", "minus": "spa"},
"Lax": {"plus": "def", "minus": "spd"},
"Lonely": {"plus": "atk", "minus": "def"},
"Mild": {"plus": "spa", "minus": "def"},
"Modest": {"plus": "spa", "minus": "atk"},
"Naive": {"plus": "spe", "minus": "spd"},
"Naughty": {"plus": "atk", "minus": "spd"},
"Quiet": {"plus": "spa", "minus": "spe"},
"Quirky": {},
"Rash": {"plus": "spa", "minus": "spd"},
"Relaxed": {"plus": "def", "minus": "spe"},
"Sassy": {"plus": "spd", "minus": "spe"},
"Serious": {},
"Timid": {"plus": "spe", "minus": "atk"}}
return table[nature.capitalize()]
def statcalc_hp(base, iv, ev, level):
return int((((iv + 2*base + (ev/4.0) + 100) * level)/100.0) + 10)
def statcalc(stat, base, iv, ev, level, nature):
boosts = natureboost(nature)
if boosts.get('plus') == stat:
if boosts['minus'] == stat: # If both
naturemod = 1.0
else: # If only plus
naturemod = 1.1
elif boosts.get('minus') == stat: # If only minus
naturemod = 0.9
else: # If this stat isn't affected by the nature
naturemod = 1.0
# Double int() is because the result is rounded down before naturemod is
# applied, then rounded down again... thanks, gamefreak!
return int(int(((((iv + 2*base + (ev/4.0)) * level)/100.0) + 5)) * naturemod)
def single_effect(source, target):
multiplier_map = {0: 1,
1: 2,
2: 0.5,
3: 0}
return multiplier_map[TYPES[target]['damageTaken'][source]]
def effectiveness(move, target):
result = 1
for user_type in target.types:
result *= single_effect(MOVES[move]['type'], user_type)
return result
def modifier_calc(source, target, move):
result = 1.0
for user_type in source['types']:
if MOVES[move]['type'] == user_type:
result *= 1.5
break
result *= effectiveness(move, target)
if random.randint(1, 16) == 1:
result *= 2.0
other = 1.0 # Not implemented
result *= other
randoffset = random.randint(85, 100)/100.0
result *= randoffset
return result
def dmg_calc(source, target, move):
a = (2*source['level'] + 10)/250.0
if MOVES[move]['category'] == 'Physical':
b = float(source['attack']) / target['defense']
elif MOVES[move]['category'] == 'Special':
b = float(source['spattack']) / target['spdefense']
else:
b = 0
c = MOVES[move]['basePower']
mod = modifier_calc(source, target, move)
return int((a * b * c + 2) * mod)
def calculate_move_score(active, types, target, move):
move = utils.condense(move)
# Score is zero for all of these
blacklist = ['suckerpunch',
'focuspunch',
'fakeout',
'return',
'frustration',
'snore',
'dreameater',
'lastresort',
'explosion',
'selfdestruct',
'synchronoise',
'belch',
'trumpcard',
'wringout'
]
badlist = ['gigaimpact',
'hyperbeam',
'rockwrecker',
'frenzyplant',
'hydrocannon',
'blastburn',
'roaroftime',
'skyattack',
'solarbeam',
'freezeshock',
'iceburn',
'doomdesire',
'futuresight ',
'leafstorm',
'overheat',
'dracometeor',
'psychoboost',
'superpower',
'hammerarm'
]
if not MOVES.get(move):
return 0
if MOVES.get(move).get('category') == 'Status' or move in blacklist:
return 0
res = 1.0
for user_type in types:
if MOVES[move]['type'] == user_type:
res *= 1.5
if move in badlist:
res *= 0.5
res *= MOVES[move]['basePower']
if type(MOVES[move]['accuracy']) == int:
res *= (MOVES[move]['accuracy'] / 100.0)
res *= effectiveness(move, target)
return res
def load_formats():
session = FuturesSession()
def bg_cb(sess, resp):
resp.data = utils.load_js_obj_literal(resp.text)
future = session.get('https://raw.githubusercontent.com/Zarel/Pokemon-Showdown/master/data/formats-data.js',
background_callback=bg_cb)
r = future.result()
return r.data
class Battle:
def __init__(self, **kwargs):
# Initialises the battle state
# Pass in a dict with battle id, and the names of each player
self.id = kwargs.get('id')
def add_to_team(self, player, mon):
mon_info = mon.split(', ')
print mon_info
if len(mon_info) == 2:
mon_info.append('Genderless')
print 'new moninfo', mon_info
level = int(mon_info[1][1:]) if len(mon_info) > 1 and mon_info[1][1:] else 100
mon = Pokemon(species=mon_info[0], level=level, gender=mon_info[2])
player.team.append(mon)
class Player:
def __init__(self, **kwargs):
self.username = kwargs.get('username')
self.id = kwargs.get('id')
self.team = []
class Pokemon:
def __init__(self, **kwargs):
# You'd pass in a dict containing information on the 'mon here.
# {'species': 'vaporeon', 'level': 100, 'moves': ['Surf', 'Ice Beam']} ... etc
# Initialising. They'll be overwritten if they exist
self.level = 100
self.ivs = [0] * 6
self.evs = [0] * 6
self.nature = 'Hardy'
for arg in kwargs:
setattr(self, arg, kwargs.get(arg))
dex = POKEDEX.get(utils.condense(self.species))
base_stats = dex.get('baseStats')
self.types = dex.get('types')
self.maxhp = statcalc_hp(base_stats['hp'], self.ivs[0], self.evs[0], self.level)
self.hp = self.maxhp
self.attack = statcalc('atk', base_stats['atk'], self.ivs[1], self.evs[1], self.level, self.nature)
self.defense = statcalc('def', base_stats['def'], self.ivs[2], self.evs[2], self.level, self.nature)
self.spattack = statcalc('spa', base_stats['spa'], self.ivs[3], self.evs[3], self.level, self.nature)
self.spdefense = statcalc('spd', base_stats['spd'], self.ivs[4], self.evs[4], self.level, self.nature)
self.speed = statcalc('spe', base_stats['spe'], self.ivs[5], self.evs[5], self.level, self.nature)
# Volatiles
self.volatiles = {'boosts': {}, 'volatile_status': None}
self.status = None
def update_info(self, attribute, new_value):
setattr(self, attribute, new_value)