def check_stability(): match_percent = 0 curr_state = np.array(region_grabber(Game_Region)) while match_percent < 0.97: sleep(0.4) new_state = np.array(region_grabber(Game_Region)) match_percent = cv2.matchTemplate(curr_state, new_state, cv2.TM_CCOEFF_NORMED) curr_state = new_state return cv2.cvtColor(curr_state, cv2.COLOR_RGB2BGR)
def search_for_crafting_window(max_res: Tuple[int, int, int, int], timeout: int = 2, sleep_time: int = .1, crafting: bool = False): print('making sure crafting window is up') c = 0 t1 = datetime.now() t2 = datetime.now() while (t2 - t1).seconds < timeout: im = region_grabber(max_res) pos = imagesearcharea("images/crafting/crafting_window.png", max_res[0], max_res[1], max_res[2], max_res[3], .8, im) if pos[0] != -1: if crafting: # print('Since crafting sleeping and checking again') sleep(sleep_time) collectible_window = search_for_collectible_window(max_res=max_res) if collectible_window[0]: pos2 = collectible_window[1] click_pos = (pos2[0] + 40, pos2[1] + 20) print(click_pos) pyautogui.moveTo(click_pos[0], click_pos[1], duration=.05) pyautogui.mouseDown() sleep(.01) pyautogui.mouseUp() t2 = datetime.now() else: return True else: if crafting: return False sleep(sleep_time) c += 1 t2 = datetime.now() print(f'Crafting window not found after {t2 - t1} seconds') return False
def search_for_collectible_window(max_res: Tuple[int, int, int, int]): im = region_grabber(max_res) pos = imagesearcharea("images/crafting/yes_no_collectible.png", max_res[0], max_res[1], max_res[2], max_res[3], .95, im) if pos[0] != -1: return True, pos else: return False, None
def img_to_base64(x1, y1, x2, y2): """ Input: input the co-ordinates output: capture of x & y co-ordinates in base64 form (string) """ save_dir = "temp.png" screenshot = region_grabber(region=(x1, y1, x2, y2)) screenshot.save(save_dir, "PNG") with open(save_dir, "rb") as image_file: encoded_base64 = base64.b64encode(image_file.read()) return encoded_base64 #gcloud_vision_test()
def search_crafting_log(max_res: Tuple[int, int, int, int], timeout: int = 5): print('looking if crafting log is up') c = 0 t1 = datetime.now() t2 = datetime.now() while (t2 - t1).seconds < timeout: im = region_grabber(max_res) pos = imagesearcharea("images/crafting/crafting_log.png", max_res[0], max_res[1], max_res[2], max_res[3], .8, im) if pos[0] != -1: print('crafting log window found') return True sleep(.1) c += 1 t2 = datetime.now() return False
def region_grabber_example(): image_region = region_grabber(region=(0, 0, 800, 600)) for file in os.listdir(os.path.curdir): if file.lower().endswith('.png'): pos = imagesearcharea(file, x1=0, y1=0, x2=800, y2=600, precision=0.8, im=image_region) if type(pos) is tuple: print("position : ", pos[0], pos[1]) else: print("image not found")
def escape(max_res: Tuple[int, int, int, int]): print('escaping') c = 0 escape_window = False while c < 20: im = region_grabber(max_res) pos = imagesearcharea("images/escape_window.png", max_res[0], max_res[1], max_res[2], max_res[3], .8, im) if pos[0] != -1: escape_window = True pyautogui.press('esc') elif escape_window: print('escape window found and closed') return True else: print(f'escape window not found {c} times - pressing esc') pyautogui.press('esc') sleep(.5) c += 1 return False
def click_synthesize(max_res: Tuple[int, int, int, int], timeout: int = 2): c = 0 t1 = datetime.now() t2 = datetime.now() while (t2 - t1).seconds < timeout: im = region_grabber(max_res) pos = imagesearcharea("images/crafting/synthesize.png", max_res[0], max_res[1], max_res[2], max_res[3], .8, im) if pos[0] != -1: print('clicking synthesis') click_pos = (pos[0] + 40, pos[1] + 20) pyautogui.moveTo(click_pos[0], click_pos[1], duration=.05) sleep(.1) #pyautogui.leftClick(duration=.05) pyautogui.mouseDown() sleep(.01) pyautogui.mouseUp() return True sleep(.1) c += 1 return False
def do_enter_id_code(self): pyautogui.moveTo(800, 910) pyautogui.mouseDown() pyautogui.mouseUp() search_region = [[788, 710], [824, 770]] region_shift = 28 number_list = [] sleep(.7) im = region_grabber((0, 0, 1920, 1080)) for i in range(5): for number in range(10): if len(number_list) == i: temp_search = [ search_region[0][0], search_region[0][1], search_region[1][0], search_region[1][1] ] pos = imagesearcharea( self.main_path + "\\task_images\\enter_id_code\\numbers\\" + str(number) + ".jpg", temp_search[0], temp_search[1], temp_search[2], temp_search[3]) if pos[0] != -1: number_list.append(number) search_region[0][ 0] = search_region[0][0] + region_shift search_region[1][ 0] = search_region[1][0] + region_shift if number_list[-1] == 1: search_region[0][0] = search_region[0][0] - 6 search_region[1][0] = search_region[1][0] - 6 if self.debug: print(f"found {number}!") if self.debug: print(f"ID code is {number_list}") x_locs = (836, 960, 1075) y_locs = (130, 240, 350, 460) for number in number_list: if number == 0: temp_x = x_locs[1] temp_y = y_locs[3] else: if number < 4: temp_y = y_locs[0] elif number < 7: temp_y = y_locs[1] else: temp_y = y_locs[2] if (number - 1) % 3 == 0: temp_x = x_locs[0] elif (number - 2) % 3 == 0: temp_x = x_locs[1] else: temp_x = x_locs[2] pyautogui.moveTo((temp_x, temp_y)) pyautogui.mouseDown() pyautogui.mouseUp() pyautogui.moveTo((x_locs[2], y_locs[3])) pyautogui.mouseDown() pyautogui.mouseUp()
def main(): global is_in_menu args = parse_args() start_x, start_y = 0, 0 end_x, end_y = GetSystemMetrics(0), GetSystemMetrics(1) if args.scrcpy_window_name: start_x, start_y, end_x, end_y = get_phone_window_location( args.scrcpy_window_name) menu_func = None if args.mode == 'battle': menu_func = start_battle elif args.mode == 'replay': menu_func = start_replay print(start_x, start_y, end_x, end_y) while True: menu_func(start_x, start_y, end_x, end_y) is_in_menu = False search_thread = threading.Thread(target=search_for_replay, kwargs={ 'start_x': start_x, 'start_y': start_y, 'end_x': end_x, 'end_y': end_y }) search_thread.start() time.sleep(.1) is_in_menu = False # Search for Tounge Chest and click Alice while not is_in_menu: im = region_grabber((start_x, start_y, end_x, end_y)) # Search for chest and witches and click them if we find them click_image_relative_to_window('./references/tongue_chest.png', start_x, start_y, end_x, end_y, im=im) click_image_relative_to_window('./references/Alice.png', start_x, start_y, end_x, end_y, offset=2, precision=0.92, im=im) click_image_relative_to_window('./references/Lisa.png', start_x, start_y, end_x, end_y, offset=2, precision=0.92, im=im) time.sleep(.1) search_thread.join() print('finished one level, replaying') is_in_menu = True
if pos[0] != -1: x1 = min(x1, pos[0]) x2 = max(x2, pos[0]) y1 = min(y1, pos[1]) y2 = max(y2, pos[1]) else: print(f"Failed to find {i}") exit(0) print("playing") grabs = [25, 29, 50] clicks = [] im = region_grabber((x1, y1, x2 + 100, y2 + 100)) for i in range(1, 51): pos = imagesearcharea(f"./imgs/{i}.png", x1, y1, x2 + 200, y2 + 200, im=im) if pos[0] != -1: #pyautogui.click(pos[0] + 10 + x1, pos[1] + 10 + y1) clicks.append([pos[0] + 10 + x1, pos[1] + 10 + y1]) else: print(f"Failed to find {i}") exit if i in grabs: for c in clicks: pyautogui.click(c[0], c[1]) clicks = [] im = region_grabber((x1, y1, x2 + 100, y2 + 100))
im_region=(627, 997, 1027, 1063) precision=0.999 im_width = im_region[2] - im_region[0] im_height = im_region[3] - im_region[1] aux=-1 heat=-1 build=0 read_conf(my_conf) print (my_conf) while 1: if GetWindowText(GetForegroundWindow()) == "Diablo III" and aux == 1: if heat<8: keyboard.press_and_release('v') heat=heat+1 im = region_grabber(im_region) #im.save("actionbar.png") for ifile in my_conf[build]['keymap']: pos = imagesearcharea(my_conf[build]['name']+"/"+ifile,0,0,im_width,im_height,precision,im) if pos[0] != -1: if len(my_conf[build]['keymap'][ifile]) == 1: keyboard.press_and_release(my_conf[build]['keymap'][ifile]) else: keyboard.press('shift') mouse.click(my_conf[build]['keymap'][ifile]) keyboard.release('shift') for item in interval_map: now=datetime.datetime.now() if int(((now - interval_map[item][1])*1000).seconds) > interval_map[item][0]: keyboard.press_and_release(item) interval_map[item][1] = now