-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai.py
91 lines (73 loc) · 3.05 KB
/
ai.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
from pokemon import *
from type import *
from attack import *
from battle import *
class AI():
def __init__(self, mode="safe"):
'''pesos mostram a importancia de
cada stat na hora do battlechance (somam 1)'''
self.mode = mode
self.maxpwr = 180
self.lvlweight = 0.2
self.maxweight = 0.35
self.meanweight = 0.3
self.spdweight = 0.05
self.hpweight = 0.1
def get_expected_dmg(self, atker, atk, defender):
'''calcula o dano esperado do ataque atk
no defender'''
power = atk.pwr / self.maxpwr
accu = atk.accu / 100
mult = get_multiplier(atker, atk.typ, defender) / 6
score = (power * mult * accu)
return score
def choose_atk(self, atker, defender, accu):
'''determina o ataque a ser escolhido a partir
do modo ativado'''
atk_scores = []
if self.mode == "safe":
min_accu = 100
elif self.mode == "risky":
min_accu = accu
else:
min_accu = 0
for atk in atker.atks :
dmg = 0
if atk.pp > 0 and atk.accu >= min_accu:
dmg = self.get_expected_dmg(atker, atk, defender)
atk_scores.append(dmg)
if atk_scores == [] and self.mode == "safe":
self.mode = "risky"
return self.choose_atk(atker, defender, accu)
if atk_scores == [] and self.mode == "risky":
self.mode = "allin"
return self.choose_atk(atker, defender, accu)
choice = atk_scores.index(max(atk_scores))
return choice
def calculate_battle_chance(self, atker, defender):
'''calculo da chance de batalha, o que pode fazer alterar
o modo de luta da AI'''
atks_pwr = []
for atk in atker.atks :
if atk.pp > 0:
atks_pwr.append(self.get_expected_dmg(atker, atk, defender))
hpratio = (atker.hp / defender.hp)
if hpratio >= 5 : hpratio = self.hpweight
else : hpratio = self.hpweight * hpratio / 5
leveldif = atker.level - defender.level
if leveldif >= 20 : leveldif = self.lvlweight
elif leveldif <= -20 : leveldif = -self.lvlweight
else : leveldif = self.lvlweight * leveldif / 20
speedratio = atker.spd / 255
if speedratio >= 0.8 : speedratio = self.spdweight
else : speedratio = self.spdweight * speedratio / 0.8
maxpwr = self.maxweight * max(atks_pwr)
meanpwr = self.meanweight * sum(atks_pwr) / float(len(atks_pwr))
chance_sum = 100 * (hpratio + leveldif + speedratio + maxpwr + meanpwr)
return chance_sum
def change_battle_mode(self, atker, defender):
chance_score = self.calculate_battle_chance(atker, defender)
if (chance_score >= 15): mode = "safe"
elif (chance_score >= 10): mode = "risky"
else : mode = "allin"
self.mode = mode