added color switch and reset trigger

This commit is contained in:
2022-10-14 18:10:54 +02:00
parent cdd0dbb780
commit d7316d7d9c
4 changed files with 43 additions and 103 deletions

View File

@@ -48,24 +48,8 @@ class Field:
colors_at_standard = True colors_at_standard = True
needles = {} needles = {}
hsh_needles = {} hsh_needles = {}
explosives = [] explosives = [RAINBOW, ARROW_RIGHT, ARROW_DOWN, BIGBOMB, BOMB]
explosives.append(RAINBOW) colors = [GREEN, YELLOW, RED, BLUE, PURPLE, MAGINENT, CHEMTRANT, TENESENT, CIBUTRANT, ARTISENT]
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)
def __init__(self): def __init__(self):
self.data_value_grid = np.zeros((8, 14), dtype=int) self.data_value_grid = np.zeros((8, 14), dtype=int)
@@ -106,12 +90,7 @@ class Field:
BOMB: cv.imread("bomb.jpg", cv.IMREAD_COLOR), BOMB: cv.imread("bomb.jpg", cv.IMREAD_COLOR),
ARROW_DOWN: cv.imread("arrow_down.jpg", cv.IMREAD_COLOR), ARROW_DOWN: cv.imread("arrow_down.jpg", cv.IMREAD_COLOR),
ARROW_RIGHT: cv.imread("arrow_right.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), MAGINENT: cv.imread("maginent.jpg", cv.IMREAD_COLOR),
CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR), CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR),
TENESENT: cv.imread("tenesent.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), 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[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[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), 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), 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), 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_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), 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), MAGINENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0),
CHEMTRANT: 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), 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): def change_value(self, x, y, val):
self.data_value_grid[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 e in range(0, 8, 1):
for i in range(0, 14, 1): for i in range(0, 14, 1):
x1, y1, w, h = self.data_coordinates[e][i] x1, y1, w, h = self.data_coordinates[e][i]
x2, y2 = x1 + w, y1 + h x2, y2 = x1 + w, y1 + h
x, y = point x, y = point
if (x1 < x and x < x2): if x1 < x and x < x2:
if (y1 < y and y < y2): if y1 < y and y < y2:
return e, i return e, i
return None, None return None, None
@@ -210,7 +191,7 @@ class Field:
try: try:
# get an updated image of the game # get an updated image of the game
screenshot = self.capture_window.get_screenshot() screenshot = self.capture_window.get_screenshot()
#screenshot = cv.imread("playfield.jpg") # screenshot = cv.imread("playfield.jpg")
screenshot = screenshot[58:1134, 230:2113] # 1883,1076 screenshot = screenshot[58:1134, 230:2113] # 1883,1076
self.screenshot = screenshot self.screenshot = screenshot
# gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY) # 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_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]) 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(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: if len(rectangles) == 0:
continue continue
points = self.vision_stun.get_click_points(rectangles) points = self.vision_stun.get_click_points(rectangles)
for point in points: 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: if x is not None and y is not None:
data_coords[x][y] = int(needle_key) data_coords[x][y] = int(needle_key)
# self.change_value(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): 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 x in range(0, 8, 1):
for y in range(0, 14, 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) 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): if self.check_explosives(state, x, y):
return return
if self.data_score_map[x, y] >= 4: if self.data_score_map[x, y] >= 4:
path_option = [] path_option = [[x, y]]
path_option.append([x, y]) recursion_reminder = [[x, y]]
recursion_reminder = [] result = self.find_next_same_color_all_directions_recursion(state, x, y, path_option,
recursion_reminder.append([x, y]) recursion_reminder, True)
result = self.find_next_same_color_all_directions_recursion(state, x, y, path_option, recursion_reminder, True)
if result == STATUS_FOUND_ENDING: if result == STATUS_FOUND_ENDING:
path_option.append([x, y]) path_option.append([x, y])
if len(path_option) >= 5: 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: if self.data_score_map[x, y] >= 1:
path_option = [] path_option = [[x, y]]
path_option.append([x, y])
recursion_reminder = []
recursion_reminder.append([x, y])
self.find_next_same_color_all_directions_recursion2(state, x, y, path_option) self.find_next_same_color_all_directions_recursion2(state, x, y, path_option)
if len(path_option) >= 3: if len(path_option) >= 3:
reserve_moves.append((state[x,y], path_option)) reserve_moves.append((state[x, y], path_option))
#print(self.data_score_map) # 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) full_dic = self.convert_moves_to_dic_by_color(full_moves)
res_dic = self.convert_moves_to_dic_by_color(reserve_moves) res_dic = self.convert_moves_to_dic_by_color(reserve_moves)
@@ -366,10 +343,9 @@ class Field:
break break
if len(reserve_moves) >= 1: if len(reserve_moves) >= 1:
for key in sorted(res_dic): for key in sorted(res_dic):
self.print_move(res_dic[key]) self.print_move(res_dic[key])
self.execute_move(res_dic[key]) self.execute_move(res_dic[key])
break break
def convert_moves_to_dic_by_color(self, reserve_moves): def convert_moves_to_dic_by_color(self, reserve_moves):
dic = {} dic = {}
@@ -381,17 +357,16 @@ class Field:
dic[color] = move dic[color] = move
return dic return dic
def print_move(self, move): def print_move(self, move):
mp = [] mp = []
for point in move: for point in move:
cp = self.get_click_point(self.data_coordinates[point[0], point[1]]) cp = self.get_click_point(self.data_coordinates[point[0], point[1]])
mp.append(cp) mp.append(cp)
cv.polylines(self.screenshot, cv.polylines(self.screenshot,
[np.array(mp)], [np.array(mp)],
isClosed=False, isClosed=False,
color=(0, 255, 0), color=(0, 255, 0),
thickness=3) thickness=3)
cv.imwrite('screenshot_pathed.jpg', self.screenshot) cv.imwrite('screenshot_pathed.jpg', self.screenshot)
@@ -402,7 +377,7 @@ class Field:
first = True first = True
for cords in move: for cords in move:
square = self.data_coordinates[cords[0], cords[1]] 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) cv.waitKey(50)
if first: if first:
pydirectinput.mouseDown() pydirectinput.mouseDown()
@@ -436,19 +411,14 @@ class Field:
upper_right_x = current_x - 1 upper_right_x = current_x - 1
upper_right_y = current_y + 1 upper_right_y = current_y + 1
directions = [] directions = [[left_x, left_y], [lower_left_x, lower_left_y], [lower_x, lower_y],
directions.append([left_x, left_y]) [lower_right_x, lower_right_y], [right_x, right_y], [upper_right_x, upper_right_y],
directions.append([lower_left_x, lower_left_y]) [upper_x, upper_y], [upper_left_x, upper_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])
return directions 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) directions = self.get_directions_array(current_x, current_y)
color = state[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.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): if (self.data_score_map[direction[0], direction[1]] >= 2) and (direction not in recursion_reminder):
recursion_reminder.append(direction) 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: if result == STATUS_FOUND_CONTINUATION:
path_store.append(direction) path_store.append(direction)
return STATUS_FOUND_CONTINUATION return STATUS_FOUND_CONTINUATION
@@ -473,7 +445,7 @@ class Field:
if look_for_ending: if look_for_ending:
for direction in directions: for direction in directions:
if direction == path_store[0]: if direction == path_store[0]:
#path_store.append(direction) # path_store.append(direction)
return STATUS_FOUND_ENDING return STATUS_FOUND_ENDING
return STATUS_FOUND_DEADEND 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.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): if (self.data_score_map[direction[0], direction[1]] >= 1) and (direction not in path_store):
path_store.append(direction) 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: if result == STATUS_FOUND_DEADEND:
return STATUS_FOUND_DEADEND return 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): def score_for_attached_same_color_all_directions(self, state, current_x, current_y):
left_x = current_x - 1 directions = self.get_directions_array(current_x, current_y)
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])
score = 0 score = 0
color = state[current_x, current_y] color = state[current_x, current_y]

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
crop/hsh_base_bigbomb.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 986 KiB

BIN
crop/reset_screen.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 761 KiB