-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_finder.py
225 lines (200 loc) · 7.97 KB
/
map_finder.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
from utils import *
from interactions import *
MAP_OFFSET_X = 1595
MAP_OFFSET_Y = 40
map_center = (-1, -1)
start = 0
res_ratio = (game_res[0] / DEFAULT_GAME_RES[0],
game_res[1] / DEFAULT_GAME_RES[1])
# Lost Ark's rescaling formula:
# Basically what the 16:9 equivalent resolution would be,
# with added black bars on top/bottom
# Implication: no y scaling compared to 16:9 equivalent!
#
# (x1, y1) is 1920x1080
# (x2, y2) is current resolution
# x: res_ratio(x2, x1) * x
# y: res_ratio(x2, x1) * y + (y2 - 9*x2/16)/2
# gui.moveTo((res_ratio[0] * (MAP_OFFSET_X+297/2)),
# res_ratio[0]*(MAP_OFFSET_Y+255/2)+(game_res[1] - 9*game_res[0]/16)/2)
def scaleMap(base_x=MAP_OFFSET_X, base_y=MAP_OFFSET_Y):
''' Calculates.
### Parameters:
`target_img` (string): filename of target image
### Returns:
`direction` (tuple): (x, y) direction vector
`target` (tuple): (x, y) position of target
'''
x = res_ratio[0] * base_x
y = res_ratio[0] * base_y + \
(game_res[1] - 9/16 * game_res[0])/2
return (x, y, 296, 255)
map_center = scaleMap(base_x=MAP_OFFSET_X+297*(1.0+(1.0-hud_scale))/2,
base_y=MAP_OFFSET_Y+255*hud_scale/2)[:2]
# gui.moveTo(map_center)
def calc_dir1(target_img):
''' Calculates the direction vector from the center of the map to a target.
### Parameters:
`target_img` (string): filename of target image
### Returns:
`direction` (tuple): (x, y) direction vector
`target` (tuple): (x, y) position of target
'''
# map_center = calc_map_center('./assets/img/map_frame.png')
print("map center: ", map_center)
target = findImage(target_img)
print("target at: ", target)
# Find angle of center TO target
angle = np.arctan2(target[1] - map_center[1],
target[0] - map_center[0])
# Retrieve direction vector to walk along
direction = (np.cos(angle), np.sin(angle))
return direction, target
def calc_dir2(position):
''' Calculates the direction vector from the center of the map to a target (pixel version).
### Parameters:
`position` (tuple): (x, y) position of target pixel
### Returns:
`direction` (tuple): (x, y) direction vector
'''
# map_center = calc_map_center('./assets/img/map_frame.png')
print("map center: ", map_center)
print("target at: ", position)
# Find angle of center TO target
angle = np.arctan2(position[1] - map_center[1],
position[0] - map_center[0])
# Retrieve direction vector to walk along
direction = (np.cos(angle), np.sin(angle))
return direction
def closest_cmp(position):
''' Comparator for pixel position from map center.
### Parameters:
`position` (tuple): (x, y) position of pixel
### Returns:
`dist` (float): distance between map center and position
'''
dist = np.linalg.norm(np.array(map_center) - np.array(position))
return dist
# -- Functions to find specific targets -- #
def find_portal():
''' Finds portal on map, then moves to and enters it. '''
# Tune parameter to adjust consistency of getting to portal
l = 300 * res_ratio[0]
portal = (-1, -1)
enter_prompt = findImage('move_portal')
# Move until portal found
while(enter_prompt == (-1, -1)):
dir, portal = calc_dir1('portal')
# Lost or didn't find the portal location
if portal == (-1, -1):
gui.click(button='right')
time.sleep(random.uniform(1,2))
break
gui.moveTo(resolution[0]/2, resolution[1]/2)
gui.move(l * dir[0], l * dir[1])
gui.click(button='right')
enter_prompt = findImage('move_portal')
gui.keyDown('g')
time.sleep(random.uniform(0.05, 0.1))
gui.keyUp('g')
def find_elites():
''' Finds the furthest elite, moves towards it, and does skill rotation.
Fails on conditions:
- if dead
- if 3 minutes have passed in Room 2
- if no more elites exist on the map
'''
# Distance traveled
l = 350 * res_ratio[0]
# Elite attack detection radius
r = 30 * res_ratio[0]
start = time.time()
while True:
dead = findImage('dead')
if dead != (-1, -1):
break
if time.time() - start > ROOM2_TIME_LIMIT:
break
elites = find_elite_color()
# Sort list of elite positions, closest to center first
elites = sorted(elites, key=lambda pos: closest_cmp(pos))
try:
dir = calc_dir2(elites[-1])
except:
print('can\'t find any more elites...')
break
gui.moveTo(resolution[0]/2, resolution[1]/2)
gui.move(l * dir[0], l * dir[1])
gui.mouseDown(button='right')
time.sleep(random.uniform(3,4))
gui.mouseUp(button='right')
if np.any(np.array([closest_cmp(position) <= r for position in elites])):
rotation(iters=1)
def find_elite_color():
''' Gets a list of all potential elite positions on the map. (by pixel color)
### Returns:
`coord_list` (list[tuple]): list of coordinates of elite pixel color
'''
# HSV2RGB Elite color range
low = (100,165,100)
high = (110,255,255)
# Broader range, less filtering
# low = (100,150,0)
# high = (110,255,255)
# For debugging filtered image
# img = cv.imread('./out_raw.png')
# img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img = np.array(gui.screenshot(region=scaleMap()))
img_HSV = cv.cvtColor(img, cv.COLOR_BGR2HSV)
mask = cv.inRange(img_HSV, low, high)
img_masked = cv.bitwise_and(img, img, mask=mask)
# For debugging filtered image
# cv.imwrite('out_raw.png', cv.cvtColor(img, cv.COLOR_BGR2RGB))
# For debugging map center
# cv.imwrite('out2.png', cv.cvtColor(img_masked, cv.COLOR_BGR2RGB))
indices = np.any(img_masked != [0, 0, 0], axis=-1)
coord_list = np.flip(np.argwhere(indices), axis=1)
coord_list = list(map(tuple, coord_list))
x_offset, y_offset = scaleMap()[:2]
coord_list = map(lambda coords: (coords[0] + x_offset, coords[1] + y_offset), coord_list)
return coord_list
def find_boss():
''' Finds the boss, moves towards it, and does skill rotation.
Fails on conditions:
- if dead
- if 3 minutes have passed in Room 2
- if boss no longer exists on the map
'''
# Tunable parameter to adjust consistency of getting to portal
l = 300 * res_ratio[0]
boss = findImage('boss')
print('boss: ', boss)
portal = findImage('portal')
while(boss != (-1, -1)):
dead = findImage('dead')
if dead != (-1, -1):
break
if time.time() - start > ROOM2_TIME_LIMIT:
break
dir, boss = calc_dir1('boss')
gui.moveTo(resolution[0]/2, resolution[1]/2)
gui.move(l * dir[0], l * dir[1])
gui.mouseDown(button='right')
time.sleep(random.uniform(2,3))
gui.mouseUp(button='right')
print('boss not found')
while(portal == (-1, -1)):
dead = findImage('dead')
if dead != (-1, -1):
break
if time.time() - start > ROOM2_TIME_LIMIT:
break
rotation(iters=1)
boss = findImage('boss')
if (boss != (-1, -1)):
find_boss()
portal = findImage('portal')
# For debugging map center
# img_test = cv.imread('./assets/img/test2.png')
# cv.circle(img_test, calc_map_center('./assets/img/map_frame_test.png'), 20, (0, 0, 255), cv.LINE_4)
# cv.imwrite('out.png', img_test)