forked from Firesuiry/autoLOL
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoperater.py
219 lines (189 loc) · 5.28 KB
/
operater.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
# -*- coding: utf-8 -*-
import time, cv2
import json, os
import numpy as np
import random
from dm.MainCommucation import MainCommucation
from presetAction.Buyequip.Buyequip import Main as equip_action
from presetAction.game_set.game_set import Main as game_setting
THRESHOLD = 0.5
class operater(MainCommucation):
def __init__(self, op_id=1, img_path='', test=False):
self.id = op_id
self.test = test
self.img_path = r"dm/screen1/0.bmp"
if img_path is not '':
self.img_path = img_path
self.commandCahe = {
'time': 0,
'commandList': []
}
if not test:
super(operater, self).__init__()
self.start()
self.equip_action = equip_action(self)
self.game_setting = game_setting(self)
def get_game_img(self):
ret = self.Capture(0, 0, 2000, 2000, self.img_path)
if ret == 0:
print('capture fail')
return None
img = cv2.imread(self.img_path)
return img
def randomChooseTargetAction(self: any, action: dict):
"""
根据softmax得出的值,随机选择一个动作
:param action:动作字典,key为动作名 value为动作未指数前概率
:return:动作名称
"""
keys = np.array(list(action.keys()))
values = np.array(list(action.values()))
values = np.e ** values
randomTarget = random.random() * np.sum(values)
s = 0
targetActionIndex = 0
for i in range(len(values)):
s += values[i]
if randomTarget < s:
targetActionIndex = i
break
targetAction = keys[targetActionIndex]
return targetAction
def init_action(self):
time.sleep(1)
self.game_setting.INIT()
time.sleep(1)
self.equip_action.INIT()
time.sleep(1)
self.equip_action.Buy('多兰之刃')
time.sleep(1)
def goHome(self):
"""
返回泉水
:return:无
"""
self.MoveToPostion([1105, 707], False)
time.sleep(5)
self.keyboardCommand('B')
time.sleep(9)
def moveto_center_of_soldier(self, params):
target_pos = params['target'][0]
if target_pos[0] != -1:
self.MoveToPostion(target_pos, False)
return True
else:
print('无法移动到己方小兵中心-未发现己方小兵')
return False
def attack_nearest_enemy_soldier(self, params):
target_pos = params['target'][1]
if target_pos[0] != -1:
self.MoveToPostion(target_pos, True)
return True
else:
print('无法攻击敌方小兵-未发现敌方小兵')
return False
def actionExcute(self: any, action: dict, params: dict):
"""
根据字典随机选择动作并执行动作
:param action:动作字典
:param params:参数字典
:return:
"""
# 回家并买装备
if action == 0:
self.goHome()
# self.equip_action.auto_buy_equip(params['MONEY'])
self.equip_action.Buy('多兰之刃')
# 前进
elif action == 1:
print('执行action:前进')
targetPostion = params.get('go', None)
if targetPostion is not None:
self.MoveToPostion(targetPostion, True)
# 后退
elif action == 2:
print('执行action:后退')
targetPostion = params.get('back', None)
if targetPostion is not None:
self.MoveToPostion(targetPostion, False)
# 原地A
elif action == 3:
print('执行action:原地A')
self.attack_self_position()
# 走到己方小兵的中心位置
elif action == 4:
print('执行action:走到己方小兵的中心位置')
if not self.moveto_center_of_soldier(params):
self.actionExcute(3,params)
# 攻击最近的敌方小兵
elif action == 5:
print('执行action:攻击最近的敌方小兵')
if not self.attack_nearest_enemy_soldier(params):
self.actionExcute(1,params)
else:
print('未实现动作 待实现:[{}]'.format(action))
raise
def attack_self_position(self):
targetPostion = [590, 358]
self.MoveToPostion(targetPostion, True)
def MoveToPostion(self, postionOnMap, attack=True):
'''
攻击移动前往坐标
:param postionOnMap:
:return:
'''
if attack:
key = 'A'
self.keyboardCommand(key, Down=True)
self.mouseCommand(postionOnMap[0], postionOnMap[1], liftClick=True)
self.keyboardCommand(key, Up=True)
else:
self.mouseCommand(postionOnMap[0], postionOnMap[1], rightClick=True)
def keyboardCommand(self, keyChar, Down=False, Up=False):
"""
键盘动作
:param keyChar: 要按的键 如[W]
:param Down: 是否按下不松
:param Up: 是否仅松开
:return:
"""
time.sleep(0.1)
if Down:
dm_ret = self.KeyDownChar(keyChar)
if str(dm_ret) != '1':
print('出错',dm_ret)
elif Up:
dm_ret = self.KeyUpChar(keyChar)
if str(dm_ret) != '1':
print('出错',dm_ret)
else:
dm_ret = self.KeyPressChar(keyChar)
if str(dm_ret) != '1':
print('出错',dm_ret)
def mouseCommand(self, x=-1, y=-1, liftClick=False, rightClick=False):
'''
:param x: -1==NoMove
:param y: -1==NoMove
:param liftClick:
:param rightClick:
:return:
'''
# print('mouseCommand x:{} y:{} liftClick:{} rightClick:{} delay={}'.format(x,y,liftClick,rightClick,delay))
if x != -1 and y != -1:
dm_ret = self.MoveTo(x, y)
if str(dm_ret) != '1':
print('出错',dm_ret)
time.sleep(0.3)
if liftClick:
dm_ret = self.LeftClick()
if str(dm_ret) != '1':
print('出错',dm_ret)
if rightClick:
dm_ret = self.RightClick()
if str(dm_ret) != '1':
print('出错',dm_ret)
if __name__ == "__main__":
p = operater(1)
p.keyboardCommand('a', Down=True)
p.mouseCommand(619, 425, liftClick=True)
p.keyboardCommand('a', Up=True)