diff --git a/crop/Field_Representation_crop.py b/crop/Field_Representation_crop.py index dcb5f53..f43307d 100644 --- a/crop/Field_Representation_crop.py +++ b/crop/Field_Representation_crop.py @@ -48,24 +48,8 @@ class Field: colors_at_standard = True needles = {} hsh_needles = {} - 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(RED) - colors.append(BLUE) - colors.append(PURPLE) - - colors.append(MAGINENT) - colors.append(CHEMTRANT) - colors.append(TENESENT) - colors.append(CIBUTRANT) - colors.append(ARTISENT) + explosives = [RAINBOW, ARROW_RIGHT, ARROW_DOWN, BIGBOMB, BOMB] + colors = [GREEN, YELLOW, RED, BLUE, PURPLE, MAGINENT, CHEMTRANT, TENESENT, CIBUTRANT, ARTISENT] def __init__(self): self.data_value_grid = np.zeros((8, 14), dtype=int) @@ -106,12 +90,7 @@ class Field: 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), @@ -122,13 +101,15 @@ class Field: self.hsh_needles = {order[0]: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), order[1]: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), order[2]: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - order[3]: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + order[3]: HsvFilter(60, 40, 0, 115, 255, 255, 0, 0, 0, 0), order[4]: HsvFilter(120, 45, 0, 170, 255, 255, 0, 0, 0, 0), + RAINBOW: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - BIGBOMB: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + BIGBOMB: HsvFilter(60, 40, 0, 179, 129, 129, 0, 0, 0, 0), BOMB: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), ARROW_DOWN: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), ARROW_RIGHT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + MAGINENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), CHEMTRANT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), TENESENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), @@ -187,14 +168,14 @@ class Field: def change_value(self, x, y, val): self.data_value_grid[x][y] = val - def pointInRect(self, point): + def point_in_rect(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): + if x1 < x and x < x2: + if y1 < y and y < y2: return e, i return None, None @@ -210,7 +191,7 @@ class Field: try: # get an updated image of the game screenshot = self.capture_window.get_screenshot() - #screenshot = cv.imread("playfield.jpg") + # screenshot = cv.imread("playfield.jpg") screenshot = screenshot[58:1134, 230:2113] # 1883,1076 self.screenshot = screenshot # gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY) @@ -243,13 +224,13 @@ class Field: processed_screenshot = self.vision_stun.apply_hsv_filter(screenshot, self.hsh_needles[needle_key]) processed_needle = self.vision_stun.apply_hsv_filter(self.needles[needle_key], self.hsh_needles[needle_key]) rectangles = self.vision_stun.find(processed_screenshot, processed_needle, 0.70, 56) - #rectangles = self.vision_stun.find(screenshot, self.needles[needle_key], 0.70, 56) + # 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) + x, y = self.point_in_rect(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)) @@ -317,7 +298,7 @@ class Field: def find_patterns_and_valid_moves(self, state): - #score_map = np.zeros((8, 14), dtype=object) + # score_map = np.zeros((8, 14), dtype=object) for x in range(0, 8, 1): for y in range(0, 14, 1): self.data_score_map[x, y] = self.score_for_attached_same_color_all_directions(state, x, y) @@ -329,26 +310,22 @@ class Field: if self.check_explosives(state, x, y): return if self.data_score_map[x, y] >= 4: - path_option = [] - path_option.append([x, y]) - recursion_reminder = [] - recursion_reminder.append([x, y]) - result = self.find_next_same_color_all_directions_recursion(state, x, y, path_option, recursion_reminder, True) + path_option = [[x, y]] + recursion_reminder = [[x, y]] + result = self.find_next_same_color_all_directions_recursion(state, x, y, path_option, + recursion_reminder, True) if result == STATUS_FOUND_ENDING: path_option.append([x, y]) if len(path_option) >= 5: - full_moves.append((state[x,y], path_option)) + full_moves.append((state[x, y], path_option)) if self.data_score_map[x, y] >= 1: - path_option = [] - path_option.append([x, y]) - recursion_reminder = [] - recursion_reminder.append([x, y]) + path_option = [[x, y]] self.find_next_same_color_all_directions_recursion2(state, x, y, path_option) if len(path_option) >= 3: - reserve_moves.append((state[x,y], path_option)) - #print(self.data_score_map) + reserve_moves.append((state[x, y], path_option)) + # print(self.data_score_map) - #screenshot = cv.imread("screenshot_scored.jpg") + # screenshot = cv.imread("screenshot_scored.jpg") full_dic = self.convert_moves_to_dic_by_color(full_moves) res_dic = self.convert_moves_to_dic_by_color(reserve_moves) @@ -366,10 +343,9 @@ class Field: break if len(reserve_moves) >= 1: for key in sorted(res_dic): - self.print_move(res_dic[key]) - self.execute_move(res_dic[key]) - break - + self.print_move(res_dic[key]) + self.execute_move(res_dic[key]) + break def convert_moves_to_dic_by_color(self, reserve_moves): dic = {} @@ -381,17 +357,16 @@ class Field: dic[color] = move return dic - def print_move(self, move): mp = [] for point in move: cp = self.get_click_point(self.data_coordinates[point[0], point[1]]) mp.append(cp) cv.polylines(self.screenshot, - [np.array(mp)], - isClosed=False, - color=(0, 255, 0), - thickness=3) + [np.array(mp)], + isClosed=False, + color=(0, 255, 0), + thickness=3) cv.imwrite('screenshot_pathed.jpg', self.screenshot) @@ -402,7 +377,7 @@ class Field: first = True for cords in move: square = self.data_coordinates[cords[0], cords[1]] - pydirectinput.moveTo(get_click_point(square)[0] + offset_left, get_click_point(square)[1]+offset_down) + pydirectinput.moveTo(get_click_point(square)[0] + offset_left, get_click_point(square)[1] + offset_down) cv.waitKey(50) if first: pydirectinput.mouseDown() @@ -436,19 +411,14 @@ class Field: upper_right_x = current_x - 1 upper_right_y = current_y + 1 - directions = [] - directions.append([left_x, left_y]) - directions.append([lower_left_x, lower_left_y]) - directions.append([lower_x, lower_y]) - directions.append([lower_right_x, lower_right_y]) - directions.append([right_x, right_y]) - directions.append([upper_right_x, upper_right_y]) - directions.append([upper_x, upper_y]) - directions.append([upper_left_x, upper_left_y]) + directions = [[left_x, left_y], [lower_left_x, lower_left_y], [lower_x, lower_y], + [lower_right_x, lower_right_y], [right_x, right_y], [upper_right_x, upper_right_y], + [upper_x, upper_y], [upper_left_x, upper_left_y]] return directions - def find_next_same_color_all_directions_recursion(self, state, current_x, current_y, path_store, recursion_reminder, look_for_ending): + def find_next_same_color_all_directions_recursion(self, state, current_x, current_y, path_store, recursion_reminder, + look_for_ending): directions = self.get_directions_array(current_x, current_y) color = state[current_x, current_y] @@ -456,7 +426,9 @@ class Field: if self.is_direction_in_bounce_and_same_color(state, direction, color) == 1: if (self.data_score_map[direction[0], direction[1]] >= 2) and (direction not in recursion_reminder): recursion_reminder.append(direction) - result = self.find_next_same_color_all_directions_recursion(state, direction[0], direction[1], path_store , recursion_reminder, look_for_ending) + result = self.find_next_same_color_all_directions_recursion(state, direction[0], direction[1], + path_store, recursion_reminder, + look_for_ending) if result == STATUS_FOUND_CONTINUATION: path_store.append(direction) return STATUS_FOUND_CONTINUATION @@ -473,7 +445,7 @@ class Field: if look_for_ending: for direction in directions: if direction == path_store[0]: - #path_store.append(direction) + # path_store.append(direction) return STATUS_FOUND_ENDING return STATUS_FOUND_DEADEND @@ -486,48 +458,16 @@ class Field: if self.is_direction_in_bounce_and_same_color(state, direction, color) == 1: if (self.data_score_map[direction[0], direction[1]] >= 1) and (direction not in path_store): path_store.append(direction) - result = self.find_next_same_color_all_directions_recursion2(state, direction[0], direction[1], path_store) + result = self.find_next_same_color_all_directions_recursion2(state, direction[0], direction[1], + path_store) if result == STATUS_FOUND_DEADEND: return STATUS_FOUND_DEADEND return STATUS_FOUND_DEADEND - def score_for_attached_same_color_all_directions(self, state, current_x, current_y): - left_x = current_x - 1 - left_y = current_y - - right_x = current_x + 1 - right_y = current_y - - upper_x = current_x - upper_y = current_y - 1 - - lower_x = current_x - lower_y = current_y + 1 - - lower_left_x = current_x + 1 - lower_left_y = current_y - 1 - - lower_right_x = current_x + 1 - lower_right_y = current_y + 1 - - upper_left_x = current_x - 1 - upper_left_y = current_y - 1 - - upper_right_x = current_x - 1 - upper_right_y = current_y + 1 - - directions = [] - directions.append([left_x, left_y]) - directions.append([right_x, right_y]) - directions.append([lower_x, lower_y]) - directions.append([upper_x, upper_y]) - directions.append([lower_left_x, lower_left_y]) - directions.append([lower_right_x, lower_right_y]) - directions.append([upper_left_x, upper_left_y]) - directions.append([upper_right_x, upper_right_y]) + directions = self.get_directions_array(current_x, current_y) score = 0 color = state[current_x, current_y] diff --git a/crop/bigbomb.jpg b/crop/bigbomb.jpg index 492214f..42324b8 100644 Binary files a/crop/bigbomb.jpg and b/crop/bigbomb.jpg differ diff --git a/crop/hsh_base_bigbomb.jpg b/crop/hsh_base_bigbomb.jpg new file mode 100644 index 0000000..d3e4e0f Binary files /dev/null and b/crop/hsh_base_bigbomb.jpg differ diff --git a/crop/reset_screen.jpg b/crop/reset_screen.jpg new file mode 100644 index 0000000..8560bf6 Binary files /dev/null and b/crop/reset_screen.jpg differ