import random import cv2 as cv import numpy as np import pydirectinput from window_capture import WindowCapture from vision import Vision from config_file import UserConfigs from utils import mse GREEN = 1 YELLOW = 2 BLUE = 3 RED = 4 PINK = 5 RAINBOW = 6 BIGBOMB = 7 BOMB = 8 ARROW_DOWN = 9 ARROW_RIGHT = 10 ROCK_1 = 11 ROCK_2 = 12 ROCK_3 = 13 BURGER = 14 PAB1 = 15 GOLDBAR = 16 MAGINENT = 21 CHEMTRANT = 22 TENESENT = 23 CIBUTRANT = 24 ARTISENT = 25 class Field: data_value_grid = [] data_coordinates = [] next_level = cv.imread("next_level.jpg", cv.IMREAD_COLOR) next_level_x = cv.imread("next_level_x.jpg", cv.IMREAD_COLOR) explosives = [] explosives.append(RAINBOW) explosives.append(ARROW_RIGHT) explosives.append(ARROW_DOWN) explosives.append(BIGBOMB) explosives.append(BOMB) colors = [] colors.append(GREEN) colors.append(YELLOW) colors.append(BLUE) colors.append(RED) colors.append(PINK) colors.append(MAGINENT) colors.append(CHEMTRANT) colors.append(TENESENT) colors.append(CIBUTRANT) colors.append(ARTISENT) def __init__(self): self.data_value_grid = np.zeros((8, 14), dtype=int) self.data_coordinates = np.zeros((8, 14), dtype=object) self.observation = np.zeros((8, 14), dtype=int) # 230 to 2110 = 1883 / 14 = 134.5 # 60 to 1130 = 1076 / 8 = 134.5 dim = 134.5 for e in range(0, 8, 1): for i in range(0, 14, 1): self.data_coordinates[e][i] = [i * dim, e * dim, dim, dim] # initialize the user-class self.config = UserConfigs() # initialize the StunWindowCapture class self.capture_window = WindowCapture(None, None, self.config) # initialize the StunVision class self.vision_stun = Vision() self.needles = {GREEN: cv.imread("green.jpg", cv.IMREAD_COLOR), YELLOW: cv.imread("yellow.jpg", cv.IMREAD_COLOR), BLUE: cv.imread("blue.jpg", cv.IMREAD_COLOR), RED: cv.imread("red.jpg", cv.IMREAD_COLOR), PINK: cv.imread("pink.jpg", cv.IMREAD_COLOR), RAINBOW: cv.imread("rainbow.jpg", cv.IMREAD_COLOR), BIGBOMB: cv.imread("bigbomb.jpg", cv.IMREAD_COLOR), BOMB: cv.imread("bomb.jpg", cv.IMREAD_COLOR), ARROW_DOWN: cv.imread("arrow_down.jpg", cv.IMREAD_COLOR), ARROW_RIGHT: cv.imread("arrow_right.jpg", cv.IMREAD_COLOR), ROCK_1: cv.imread("rock1.jpg", cv.IMREAD_COLOR), ROCK_2: cv.imread("rock2.jpg", cv.IMREAD_COLOR), ROCK_3: cv.imread("rock3.jpg", cv.IMREAD_COLOR), BURGER: cv.imread("burger.jpg", cv.IMREAD_COLOR), GOLDBAR: cv.imread("burger.jpg", cv.IMREAD_COLOR), PAB1: cv.imread("pab1.jpg", cv.IMREAD_COLOR), MAGINENT: cv.imread("maginent.jpg", cv.IMREAD_COLOR), CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR), TENESENT: cv.imread("tenesent.jpg", cv.IMREAD_COLOR), CIBUTRANT: cv.imread("cibutrant.jpg", cv.IMREAD_COLOR), ARTISENT: cv.imread("artisent.jpg", cv.IMREAD_COLOR) } def reset(self): self.observation = [] def assess_playfield_and_make_move(self): new_observation, new_screenshot = self.get_current_board_state() # wrong movement detection # last board state is same as actual if mse(new_observation, self.observation) == 0.0: # no movement detected -> blow explosives or reset self.detonate_explosive_when_stuck(new_observation) cv.waitKey(500) self.find_patterns_and_valid_moves(new_observation) self.observation = new_observation return new_observation def move_to(self, x, y): point_src = (1113, 598) pydirectinput.moveTo(point_src[0], point_src[1]) pydirectinput.mouseDown() w = random.randint(1, 100) cv.waitKey(150 + w) pydirectinput.moveTo(x, y) pydirectinput.mouseUp() cv.waitKey(500 + w) def change_value(self, x, y, val): self.data_value_grid[x][y] = val def pointInRect(self, point): for e in range(0, 8, 1): for i in range(0, 14, 1): x1, y1, w, h = self.data_coordinates[e][i] x2, y2 = x1 + w, y1 + h x, y = point if (x1 < x and x < x2): if (y1 < y and y < y2): return e, i return None, None def check_for_next_level(self, screen, needle): offset_left = 230 offset_down = 58 rectangles = self.vision_stun.find(screen, needle, 0.70, 1) if len(rectangles) == 0: return False point = self.vision_stun.get_click_points(rectangles)[0] self.dig_point(point[0]+ offset_left, point[1] + offset_down, 500) return True def get_current_board_state(self): try: # get an updated image of the game screenshot = self.capture_window.get_screenshot() #screenshot = cv.imread("field_farm.jpg") screenshot = screenshot[58:1134, 230:2113] # 1883,1076 # gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY) # thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] if self.check_for_next_level(screenshot, self.next_level): cv.waitKey(500) screenshot = self.capture_window.get_screenshot() screenshot = screenshot[58:1134, 230:2113] if self.check_for_next_level(screenshot, self.next_level_x): cv.waitKey(500) screenshot = self.capture_window.get_screenshot() screenshot = screenshot[58:1134, 230:2113] except: self.capture_window.release() print("Game window not available - shutting down application") return None # cv.imshow("screenshot", screenshot) # cv.waitKey(150) # continue data_coords = np.zeros((8, 14), dtype=object) # field = Field() for needle_key in self.needles.keys(): # gray_needle = cv.cvtColor(self.needles[needle_key], cv.COLOR_BGR2GRAY) # thresh_needle = cv.threshold(gray_needle, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] rectangles = self.vision_stun.find(screenshot, self.needles[needle_key], 0.70, 56) if len(rectangles) == 0: continue points = self.vision_stun.get_click_points(rectangles) for point in points: x, y = self.pointInRect(point) if x is not None and y is not None: data_coords[x][y] = int(needle_key) # self.change_value(x, y, int(needle_key)) # print(field.data_value_grid) # cv.circle(screenshot, points[0], 7, (0, 255, 0), -1) # output_image = vision_stun.draw_rectangles(screenshot, rectangles) # cv.imshow("output_image", output_image) # cv.waitKey(150) return data_coords, screenshot def find_patterns_and_valid_moves(self, state): for e in range(0, 8, 1): for i in range(0, 14, 1): if self.check_explosives(state, e, i): return for e in range(0, 8, 1): for i in range(0, 14, 1): for color in self.colors: if self.check_5_horizontal(state, e, i, color): return if self.check_5_vertical(state, e, i, color): return for e in range(0, 8, 1): for i in range(0, 14, 1): for color in self.colors: if self.check_3_horizontal(state, e, i, color): return if self.check_3_vertical(state, e, i, color): return if self.check_3_with_gap(state, e, i, color): return def detonate_explosive_when_stuck(self, state): for e in range(0, 8, 1): for i in range(0, 14, 1): for explosive in self.explosives: if self.local_pos_check(state, e, i, 0, 0, explosive): dest_pt = self.get_click_point(self.data_coordinates[e, i]) if self.local_pos_checks(state, e, i, 1, 0, self.colors): src_pt = self.get_click_point(self.data_coordinates[e + 1, i]) self.move_tile(src_pt, dest_pt) elif self.local_pos_checks(state, e, i, 0, 1, self.colors): src_pt = self.get_click_point(self.data_coordinates[e, i + 1]) self.move_tile(src_pt, dest_pt) elif self.local_pos_checks(state, e, i, -1, 0, self.colors): src_pt = self.get_click_point(self.data_coordinates[e - 1, i]) self.move_tile(src_pt, dest_pt) elif self.local_pos_checks(state, e, i, 0, -1, self.colors): src_pt = self.get_click_point(self.data_coordinates[e, i - 1]) self.move_tile(src_pt, dest_pt) else: continue return def check_explosives(self, state, e, i): for explosive in self.explosives: if self.local_pos_check(state, e, i, 0, 0, explosive): dest_pt = self.get_click_point(self.data_coordinates[e, i]) if self.local_pos_checks(state, e, i, 1, 0, self.explosives): src_pt = self.get_click_point(self.data_coordinates[e + 1, i]) self.move_tile(src_pt, dest_pt) elif self.local_pos_checks(state, e, i, 0, 1, self.explosives): src_pt = self.get_click_point(self.data_coordinates[e, i + 1]) self.move_tile(src_pt, dest_pt) elif self.local_pos_checks(state, e, i, -1, 0, self.explosives): src_pt = self.get_click_point(self.data_coordinates[e - 1, i]) self.move_tile(src_pt, dest_pt) elif self.local_pos_checks(state, e, i, 0, -1, self.explosives): src_pt = self.get_click_point(self.data_coordinates[e, i - 1]) self.move_tile(src_pt, dest_pt) else: continue return True else: continue return False def local_pos_check(self, state, e, i, e_check, i_check, needle): if e + e_check >= 0 and e + e_check <=7 and i + i_check >= 0 and i + i_check <=13: if state[e + e_check, i + i_check] == needle: return True else: return False def local_pos_checks(self, state, e, i, e_check, i_check, needles): if e + e_check >= 0 and e + e_check <=7 and i + i_check >= 0 and i + i_check <=13: for needle in needles: if state[e + e_check, i + i_check] == needle: return True else: continue return False def check_5_horizontal(self, state, e, i, color): try: # if state[e, i] == color and state[e, i + 1] == color and state[e, i + 3] == color and state[ e, i + 4] == color: if state[e, i + 2] <= 0 or state[e, i + 2] >= 6: return False # if e - 1 >= 0 and i + 2 <= 13: if state[e - 1, i + 2] == color: return True return False except: return False def check_5_vertical(self, state, e, i, color): try: # if state[e, i] == color and state[e + 1, i] == color and state[e + 3, i] == color and state[ e + 4, i] == color: if state[e + 2, i] <= 0 or state[e + 2, i] >= 6: return False # third left upper if e + 2 <= 7 and i - 1 >= 0: if state[e + 2, i - 1] == color: print("upper left", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e + 2, i - 1]) dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) self.move_tile(src_pt, dest_pt) return True if e + 2 <= 7 and i + 1 <= 13: if state[e + 2, i + 1] == color: print("upper left", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e + 2, i + 1]) dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) self.move_tile(src_pt, dest_pt) return True return False except: return False def check_3_with_gap(self, state, e, i, color): try: # second color next to starting point if i + 2 <= 13: if state[e, i] == color and state[e, i + 2] == color: # third upper if e - 1 >= 0 and i + 1 <= 13: #if state[e - 1, i - 1] == color and (state[e, i - 1] >= 1 and state[e, i - 1] <= 5): if state[e - 1, i + 1] == color and (state[e, i + 1] in self.colors): src_pt = self.get_click_point(self.data_coordinates[e - 1, i + 1]) dest_pt = self.get_click_point(self.data_coordinates[e, i + 1]) self.move_tile(src_pt, dest_pt) return True # third left lower if e + 1 <= 7 and i + 1 <= 13: if state[e + 1, i + 1] == color and (state[e, i + 1] in self.colors): src_pt = self.get_click_point(self.data_coordinates[e + 1, i + 1]) dest_pt = self.get_click_point(self.data_coordinates[e, i + 1]) self.move_tile(src_pt, dest_pt) return True if e + 2 <= 7: if state[e, i] == color and state[e + 2, i] == color: # third upper if e + 1 >= 0 and i + 1 <= 13: #if state[e - 1, i - 1] == color and (state[e, i - 1] >= 1 and state[e, i - 1] <= 5): if state[e + 1, i + 1] == color and (state[e + 1, i] in self.colors): src_pt = self.get_click_point(self.data_coordinates[e + 1, i + 1]) dest_pt = self.get_click_point(self.data_coordinates[e + 1, i]) self.move_tile(src_pt, dest_pt) return True # third left lower if e + 1 <= 7 and i - 1 >= 0: if state[e + 1, i - 1] == color and (state[e + 1, i] in self.colors): src_pt = self.get_click_point(self.data_coordinates[e + 1, i - 1]) dest_pt = self.get_click_point(self.data_coordinates[e + 1, i]) self.move_tile(src_pt, dest_pt) return True except: return False def check_3_horizontal(self, state, e, i, color): try: # second color next to starting point if state[e, i] == color and state[e, i + 1] == color: # third left upper if e - 1 >= 0 and i - 1 >= 0: if state[e - 1, i - 1] == color and (state[e, i - 1] in self.colors): print("upper left", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e - 1, i - 1]) dest_pt = self.get_click_point(self.data_coordinates[e, i - 1]) self.move_tile(src_pt, dest_pt) return True # third left lower if e + 1 <= 7 and i - 1 >= 0: if state[e + 1, i - 1] == color and (state[e, i - 1] in self.colors): print("lower left", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e + 1, i - 1]) dest_pt = self.get_click_point(self.data_coordinates[e, i - 1]) self.move_tile(src_pt, dest_pt) return True # third left with gap if i - 2 >= 0: if state[e, i - 2] == color and (state[e, i - 1] in self.colors): print("left gap ", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e, i - 2]) dest_pt = self.get_click_point(self.data_coordinates[e, i - 1]) self.move_tile(src_pt, dest_pt) return True # third right upper if e - 1 >= 0 and i + 2 <= 13: if state[e - 1, i + 2] == color and (state[e, i + 2] in self.colors): print("upper right", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e - 1, i + 2]) dest_pt = self.get_click_point(self.data_coordinates[e, i + 2]) self.move_tile(src_pt, dest_pt) return True # third right lower if e + 1 <= 7 and i + 2 <= 13: if state[e + 1, i + 2] == color and (state[e, i + 2] in self.colors): print("upper lower", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e + 1, i + 2]) dest_pt = self.get_click_point(self.data_coordinates[e, i + 2]) self.move_tile(src_pt, dest_pt) return True # third right with gap if i + 3 <= 13: if state[e, i + 3] == color and (state[e, i + 2] in self.colors): print("right gap ", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e, i + 3]) dest_pt = self.get_click_point(self.data_coordinates[e, i + 2]) self.move_tile(src_pt, dest_pt) return True except: return False def check_3_vertical(self, state, e, i, color): try: # second color next to starting point o if state[e, i] == color and state[e + 1, i] == color: # third left upper if e - 1 >= 0 and i - 1 >= 0: if state[e - 1, i - 1] == color and (state[e - 1, i] in self.colors): print("upper left", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e - 1, i - 1]) dest_pt = self.get_click_point(self.data_coordinates[e - 1, i]) self.move_tile(src_pt, dest_pt) return True # third left lower if e + 2 <= 7 and i - 1 >= 0: if state[e + 2, i - 1] == color and (state[e + 2, i] in self.colors): print("lower left", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e + 2, i - 1]) dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) self.move_tile(src_pt, dest_pt) return True # third right upper if e - 1 >= 0 and i + 1 <= 13: if state[e - 1, i + 1] == color and (state[e - 1, i] in self.colors): print("upper right", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e - 1, i + 1]) dest_pt = self.get_click_point(self.data_coordinates[e - 1, i]) self.move_tile(src_pt, dest_pt) return True # third right lower if e + 2 <= 7 and i + 1 <= 13: if state[e + 2, i + 1] == color and (state[e + 2, i] in self.colors): print("upper lower", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e + 2, i + 1]) dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) self.move_tile(src_pt, dest_pt) return True # third upper with gap if e - 2 >= 0: if state[e - 2, i] == color and (state[e - 1, i] in self.colors): print("upper gap ", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e - 2, i]) dest_pt = self.get_click_point(self.data_coordinates[e - 1, i]) self.move_tile(src_pt, dest_pt) return True # third lower with gap if e + 3 <= 7: if state[e + 3, i] == color and (state[e + 2, i] in self.colors): print("lower gap ", color, e, i) src_pt = self.get_click_point(self.data_coordinates[e + 3, i]) dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) self.move_tile(src_pt, dest_pt) return True except: return False def move_tile(self, point_source, point_dest): offset_left = 230 offset_down = 58 pydirectinput.moveTo(point_source[0] + offset_left, point_source[1] + offset_down) # pydirectinput.moveTo(0,0) pydirectinput.mouseDown() w = random.randint(25, 50) cv.waitKey(100 + w) pydirectinput.moveTo(point_dest[0] + offset_left, point_dest[1] + offset_down) pydirectinput.mouseUp() cv.waitKey(400 + w) def check_for_button_and_click_it(self, button_url): screenshot = self.capture_window.get_screenshot() # gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY) # thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] # gray_needle = cv.cvtColor(cv.imread(button_url, cv.IMREAD_UNCHANGED), cv.COLOR_BGR2GRAY) # thresh_needle = cv.threshold(gray_needle, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] needle = cv.imread(button_url, cv.IMREAD_UNCHANGED) # rectangles = self.vision_stun.find(thresh, thresh_needle, 0.4, 1) rectangles = self.vision_stun.find(screenshot, needle, 0.7, 1) if len(rectangles) == 1: pointis = self.vision_stun.get_click_points(rectangles) for pointi in pointis: self.dig_point(pointi[0], pointi[1], 150) def dig_point(self, point1, point2, dig_time): pydirectinput.moveTo(point1, point2) cv.waitKey(dig_time) pydirectinput.mouseDown() w = random.randint(50, 100) cv.waitKey(w) pydirectinput.mouseUp() def get_click_point(self, rectangle): # Loop over all the rectangles x, y, w, h = rectangle # Determine the center position center_x = x + int(w / 2) center_y = y + int(h / 2) # Save the points return int(center_x), int(center_y)