-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwizAPI.py
489 lines (407 loc) · 16.6 KB
/
wizAPI.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import win32gui
import pyautogui
import cv2
import time
class wizAPI:
def __init__(self, handle=None):
self._handle = handle
self._spell_memory = {}
self._friends_area = (625, 65, 20, 240)
self._spell_area = (245, 290, 370, 70)
self._enemy_area = (68, 26, 650, 35)
def wait(self, s):
""" Alias for time.sleep() that return self for function chaining """
time.sleep(s)
return self
def register_window(self, name="Wizard101", nth=0):
""" Assigns the instance to a wizard101 window (Required before using any other API functions) """
def win_enum_callback(handle, param):
if name == str(win32gui.GetWindowText(handle)):
param.append(handle)
handles = []
# Get all windows with the name "Wizard101"
win32gui.EnumWindows(win_enum_callback, handles)
handles.sort()
# Assigns the one at index nth
self._handle = handles[nth]
return self
def is_active(self):
""" Returns true if the window is focused """
return self._handle == win32gui.GetForegroundWindow()
def set_active(self):
""" Sets the window to active if it isn't already """
if not self.is_active():
""" Press alt before and after to prevent a nasty bug """
pyautogui.press('alt')
win32gui.SetForegroundWindow(self._handle)
pyautogui.press('alt')
return self
def get_window_rect(self):
"""Get the bounding rectangle of the window """
rect = win32gui.GetWindowRect(self._handle)
return [rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1]]
def match_image(self, largeImg, smallImg, threshold=0.1, debug=False):
""" Finds smallImg in largeImg using template matching """
""" Adjust threshold for the precision of the match (between 0 and 1, the lowest being more precise """
""" Returns false if no match was found with the given threshold """
method = cv2.TM_SQDIFF_NORMED
# Read the images from the file
small_image = cv2.imread(smallImg)
large_image = cv2.imread(largeImg)
w, h = small_image.shape[:-1]
result = cv2.matchTemplate(small_image, large_image, method)
# We want the minimum squared difference
mn, _, mnLoc, _ = cv2.minMaxLoc(result)
if (mn >= threshold):
return False
# Extract the coordinates of our best match
x, y = mnLoc
if debug:
# Draw the rectangle:
# Get the size of the template. This is the same size as the match.
trows, tcols = small_image.shape[:2]
# Draw the rectangle on large_image
cv2.rectangle(large_image, (x, y),
(x+tcols, y+trows), (0, 0, 255), 2)
# Display the original image with the rectangle around the match.
cv2.imshow('output', large_image)
# The image is only displayed if we call this
cv2.waitKey(0)
# Return coordinates to center of match
return (x + (w * 0.5), y + (h * 0.5))
def pixel_matches_color(self, coords, rgb, threshold=0):
""" Matches the color of a pixel relative to the window's position """
wx, wy = self.get_window_rect()[:2]
x, y = coords
# self.move_mouse(x, y)
return pyautogui.pixelMatchesColor(x + wx, y + wy, rgb, tolerance=threshold)
def move_mouse(self, x, y, speed=.5):
""" Moves to mouse to the position (x, y) relative to the window's position """
wx, wy = self.get_window_rect()[:2]
pyautogui.moveTo(wx + x, wy + y, speed)
return self
def click(self, x, y, delay=.1, speed=.5, button='left'):
""" Moves the mouse to (x, y) relative to the window and presses the mouse button """
(self.set_active()
.move_mouse(x, y, speed=speed)
.wait(delay))
pyautogui.click(button=button)
return self
def screenshot(self, name, region=False):
"""
- Captures a screenshot of the window and saves it to 'name'
- Can also be used the capture specific parts of the window by passing in the region arg. (x, y, width, height) (Relative to the window position)
"""
self.set_active()
# region should be a tuple
# Example: (x, y, width, height)
window = self.get_window_rect()
if not region:
# Set the default region to the area of the window
region = window
else:
# Adjust the region so that it is relative to the window
wx, wy = window[:2]
region = list(region)
region[0] += wx
region[1] += wy
pyautogui.screenshot(name, region=region)
def teleport_to_friend(self, match_img):
"""
Completes a set of actions to teleport to a friend.
The friend must have the proper symbol next to it
symbol must match the image passed as 'match_img'
"""
self.set_active()
# Check if friends already opened (and close it)
while self.pixel_matches_color((780, 364), (230, 0, 0), 40):
self.click(780, 364).wait(0.2)
# Open friend menu
self.click(780, 50)
# Find friend that matches friend match_img
self.screenshot('friend_area.png', region=self._friends_area)
found = self.match_image(
'friend_area.png', match_img)
if found is not False:
x, y = found
offset_x, offset_y = self._friends_area[:2]
(self.click(offset_x + x + 50, offset_y + y) # Select friend
.click(450, 115) # Select port
.click(415, 395) # Select yes
)
return self
else:
print('Friend cound not be found')
return False
def enter_dungeon_dialog(self):
""" Detects if the 'Enter Dungeon' dialog is present """
self.set_active()
return (self.pixel_matches_color((253, 550), (4, 195, 4), 5) and
self.pixel_matches_color((284, 550), (20, 218, 11), 5))
def is_DS_loading(self):
""" Matches an orange pixel in the Dragonspyre loading screen """
self.set_active()
return self.pixel_matches_color((108, 551), (252, 127, 5), 20)
def hold_key(self, key, holdtime):
"""
Holds a key for a specific amount of time, usefull for moving with the W A S D keys
"""
self.set_active()
pyautogui.keyDown(key)
time.sleep(holdtime)
pyautogui.keyUp(key)
return self
def press_key(self, key):
"""
Presses a key, useful for pressing 'x' to enter a dungeon
"""
self.set_active()
pyautogui.press(key)
return self
def is_health_low(self):
self.set_active()
# Matches a pixel in the lower third of the health globe
POSITION = (23, 563)
COLOR = (126, 41, 3)
THRESHOLD = 10
return not self.pixel_matches_color(POSITION, COLOR, threshold=THRESHOLD)
def is_mana_low(self):
self.set_active()
# Matches a pixel in the lower third of the mana globe
POSITION = (79, 591)
COLOR = (66, 13, 83)
THRESHOLD = 10
return not self.pixel_matches_color(POSITION, COLOR, threshold=THRESHOLD)
def use_potion_if_needed(self):
mana_low = self.is_mana_low()
health_low = self.is_health_low()
if mana_low:
print('Mana is low, using potion')
if health_low:
print('Health is low, using potion')
if mana_low or health_low:
self.click(160, 590, delay=.2)
def pass_turn(self):
self.click(254, 398, delay=.5).move_mouse(200, 400)
return self
def is_turn_to_play(self):
""" matches a yellow pixel in the 'pass' button """
return self.pixel_matches_color((238, 398), (255, 255, 0), 20)
def wait_for_next_turn(self):
""" Wait for spell round to begin """
while self.is_turn_to_play():
self.wait(1)
print('Spell round begins')
""" Start detecting if it's our turn to play again """
while not self.is_turn_to_play():
self.wait(1)
print('Our turn to play')
return self
def wait_for_turn_to_play(self):
while not self.is_turn_to_play():
self.wait(.5)
def wait_for_end_of_round(self):
""" Similar to wait_for_next_turn, but also detects if its the end of the battle """
""" Wait for spell round to begin """
while self.is_turn_to_play():
self.wait(1)
""" Start detecting if it's our turn to play again """
""" Or if it's the end of the battle """
while not (self.is_turn_to_play() or self.is_idle()):
self.wait(1)
return self
def is_idle(self):
""" Matches a pink pixel in the pet icon (only visible when not in battle) """
return self.pixel_matches_color((140, 554), (252, 146, 206), 2)
def find_spell(self, spell_name, threshold=0.15, max_tries=2, recapture=True):
"""
Attempts the find the spell passed is 'spell_name'
returns False if not found with the given threshold
Use recapture=False to not re-take the screenshot of the spell_area
Adds spell position to memory for later use
"""
self.set_active()
tries = 0
res = False
while not res and tries < max_tries:
tries += 1
if tries > 1:
# Wait 1 second before re-trying
self.wait(1)
recapture = True
if recapture:
self.mouse_out_of_area(self._spell_area)
self.screenshot('spell_area.png', region=self._spell_area)
res = self.match_image(
'spell_area.png', ('spells/' + spell_name + '.png'), threshold)
if res is not False:
x, y = res
offset_x, offset_y = self._spell_area[:2]
spell_pos = (offset_x + x, offset_y + y)
# Remember location
self._spell_memory[spell_name] = spell_pos
return spell_pos
else:
return False
def find_unusable_spells(self, limit=-1):
""" Returns an array of the positions of unusable spells (grayed out) """
""" Useful for farming Loremaster, it prevents getting a crowded deck if you learn a new spell """
self.set_active()
self.mouse_out_of_area(self._spell_area)
self.screenshot('spell_area.png', region=self._spell_area)
w, h = (28, 38) # The size of the gray area we're looking for
img = cv2.imread('spell_area.png')
rows, cols = img.shape[:2]
pts = []
# Determine if a pixel is gray enough
def isGray(pixel, threshold):
return abs(int(min(*pixel)) - int(max(*pixel))) <= threshold
i = 2
j = 0
while j < (cols - w):
""" find a rectangle with no color """
grayScale = True
for y in range(h):
for x in range(w):
pixel = img[i + y, j + x]
if not isGray(pixel, threshold=30):
grayScale = False
if not grayScale:
break
if not grayScale:
break
if grayScale:
offset_x, offset_y = self._spell_area[:2]
spell_pos = (offset_x + j + w/2, offset_y + i+h/2)
pts.append(spell_pos)
j += w
# Break if we've reached the limit in requested areas
if limit > 0 and len(pts) >= limit:
break
j += 1
self._spell_memory["unusable"] = pts
return pts
def discard_unusable_spells(self, limit=-1):
count = 0
while True:
count += 1
print(count)
try:
# Try accessing from memory
card_pos = self._spell_memory["unusable"][0]
except (KeyError, IndexError):
result = self.find_unusable_spells(limit=1)
if len(result) is not 0:
card_pos = result[0]
else:
break
print(card_pos)
# Right click the card position
self.click(*card_pos, button='right', delay=.2)
# Flush card memory
self.flush_spell_memory()
def flush_spell_memory(self):
"""
This action gets called everytime there is a destructive action to the spells (The spells change position)
For example: Casting, Enchanting, Discarding
"""
self._spell_memory = {}
return
def select_spell(self, spell):
"""
Clicks on a spell
Attemps to look in memory to see if we already have found this spell
Returns false if the spell can't be found
"""
try:
spell_pos = self._spell_memory[spell]
except KeyError:
spell_pos = self.find_spell(spell)
if spell_pos is not False:
self.click(*spell_pos, delay=.3)
return self
else:
return False
def cast_spell(self, spell):
"""
Clicks on the spell and clears memory cache
if the spell requires a target, chain it with .at_target([enemy_pos])
"""
if self.find_spell(spell):
print('Casting', spell)
self.flush_spell_memory()
return self.select_spell(spell)
else:
return False
def enchant(self, spell_name, enchant_name, threshold=0.1, silent_fail=False):
""" Attemps the enchant 'spell_name' with 'enchant_name' """
if self.find_spell(spell_name, threshold=threshold) and self.find_spell(enchant_name, recapture=False, threshold=threshold):
print('Enchanting', spell_name, 'with', enchant_name)
self.select_spell(enchant_name)
self.select_spell(spell_name)
self.flush_spell_memory()
return self
else:
if not silent_fail:
print("One or more spells couldn't be found:",
spell_name, enchant_name)
return False
def get_enemy_pos(self, enemy_img):
"""
Attemps to find the position of an enemy the matches the image provided
returns 1, 2, 3, or 4 if found
otherwise returns False
(In my example, the image to match is the balance symbol, as only the Loremaster has it in this fight. It could also be a screenshot of the name of the enemy in question)
"""
self.screenshot('enemy_area.png', region=self._enemy_area)
found = self.match_image('enemy_area.png', enemy_img, threshold=.2)
if found is not False:
found_x, _ = found
enemy_pos = round((found_x - 60) / 170) + 1
return enemy_pos
else:
return False
def at_target(self, target_pos):
""" Clicks the target, based on position 1, 2, 3, or 4 """
x = (174 * (target_pos - 1)) + 130
y = 50
self.click(x, y, delay=.2)
return self
def mouse_out_of_area(self, area):
""" Move the mouse outside of an area, to make sure the mouse doesn't interfere with image matching """
# Adjust the region so that it is relative to the window
wx, wy = self.get_window_rect()[:2]
region = list(area)
region[0] += wx
region[1] += wy
def in_area(area):
px, py = pyautogui.position()
x, y, w, h = area
return (px > x and px < (x + w) and py > y and py < (y + h))
while in_area(region):
pyautogui.moveRel(0, -100, duration=0.5)
return self
def face_arrow(self):
""" Faces the questing arrow, useful for finding your way out of a dungeon """
self.set_active()
pyautogui.keyDown('a')
count = 0
while not self.pixel_matches_color((385, 531), (133, 120, 14), 2):
count += 1
pass
pyautogui.keyUp('a')
self.hold_key('d', min(count / 100, 0.2))
return self
def count_enemies(self):
Y = 75
COLOR = (207, 186, 135)
num_enemies = 0
for i in range(4):
X = (174 * (i)) + 203
if self.pixel_matches_color((X, Y), COLOR, threshold=30):
num_enemies += 1
if num_enemies == 1:
print(num_enemies, 'enemy in battle')
else:
print(num_enemies, 'enemies in battle')
return num_enemies