Skip to content

Commit

Permalink
Added pathfinding, Matrix, Binary Matrix, starting soon to clean the…
Browse files Browse the repository at this point in the history
… code.
  • Loading branch information
Flowtter committed Mar 22, 2020
1 parent f878c66 commit 2fceaac
Show file tree
Hide file tree
Showing 12 changed files with 565 additions and 197 deletions.
Binary file modified assets/Dot.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
219 changes: 120 additions & 99 deletions src/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pyautogui
import win32gui
import imutils
from pathfinding import astar

from detection import axe, trees, player, rock, blackrock, river, terrain, green

Expand All @@ -17,22 +18,45 @@ def debug_main():
#im = single_screenshot()

im = cv2.imread("../test_data/img_debug.png", cv2.COLOR_RGB2BGR)
im, im2 = do_map(im)
im = cut(im)
#im, im2 = do_map(im)

show2([im, im2])
game = game_map(20,36,22,16,10) # (self, height, width, cell_size, refresh_rate):
game.init_matrix()


get_array_from_bin(game, im)


game.print_matrix()
#print()
#game.print_binary()

game.draw_matrix()
im4 = game.draw_image()

game2 = game_map(20,36,22,16,10) # (self, height, width, cell_size, refresh_rate):
game2.init_matrix()
get_array_from_bin(game2, im)
im5 = astar_map(game2)


grid(im)

show2([im, im4, im5])


def do_map(im):
im = rotate(im, -8.5)
x, y = 0, 115
h, w = 330, 800
def cut(im):
im = rotate(im, -8)
x, y = 0, 125
h, w = 320, 800
im = im[y:y+h, x:x+w]

rows,cols = im.shape[:-1]


a , b , c = [382,52],[500,50],[400,200]
offsetx = 24
offsetx = 20
d, e, f = [382 + offsetx ,52],[500 + offsetx,50],[400 ,200]

pts1 = np.float32([a, b, c])
Expand All @@ -43,138 +67,131 @@ def do_map(im):
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(im,M,(cols,rows))

im = dst
return dst

axe.draw_contours(im, cv2.cvtColor(im, cv2.COLOR_BGR2GRAY))

bin_green = green.draw_contours_return_bin(im, cv2.cvtColor(im,cv2.COLOR_BGR2HSV ))
def astar_map(g):
start, end = (15,19), (19,6)
print(g.get_matrix()[start[1]][start[0]])
print(g.get_matrix()[end[1]][end[0]])

mean_x, mean_y = player.draw_and_return_contours(im, cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
try:
original = g.get_binary_matrix()
astar.run(original, start, end, g)
except:
print(" ")

mean_x//=23
mean_y//=18

g.matrix_add(start[1], start[0], 'S')
g.matrix_add(end[1], end[0], 'U')
g.draw_matrix()


bin_trees = trees.draw_contours_return_bin(im, cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
bin_rocks = rock.draw_contours_return_bin(im, cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
bin_black = blackrock.draw_contours_return_bin(im, cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
bin_river = river.draw_contours_return_bin(im, cv2.cvtColor(im,cv2.COLOR_BGR2HSV ))
bin_terrain = terrain.draw_contours_return_bin(im, cv2.cvtColor(im,cv2.COLOR_BGR2HSV ))
return g.draw_image()

mega_bin = bin_green + bin_trees + bin_rocks + bin_black + bin_river + bin_terrain

x, y = mega_bin.shape[:-1]
def unpack_array(arr, vall, game):
for e in arr:
game.matrix_add(e[0], e[1], vall)

game = game_map(20,35,23,10) # (self, height, width, cell_size, refresh_rate):
game.init_matrix()
game.draw_cell()

def get_array_from_bin(game, im):
x, y = player.get_pos(im, cv2.cvtColor(im,cv2.COLOR_BGR2HSV))

arrtree = element(game, bin_trees, bin_trees, 6)
arrrock = element(game, bin_rocks, bin_rocks, 3)
arrblack = element(game, bin_black, bin_black, 5)
bin_green = green.get_bin(im, cv2.cvtColor(im,cv2.COLOR_BGR2HSV))
bin_trees = trees.get_bin(im, cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
bin_rocks = rock.get_bin(im, cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
bin_black = blackrock.get_bin(im, cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
bin_river = river.get_bin(im, cv2.cvtColor(im,cv2.COLOR_BGR2HSV ))
bin_terrain = terrain.get_bin(im, cv2.cvtColor(im,cv2.COLOR_BGR2HSV ))
bin_axe = axe.get_bin(im, cv2.cvtColor(im,cv2.COLOR_BGR2HSV ))

#arrplayer = element(game, bin_player, bin_player, 6)
arrtree = element(game, bin_trees, bin_trees, 3)
arrrock = element(game, bin_rocks, bin_rocks, 4)
arrblack = element(game, bin_black, bin_black, 3)
arrriver = element(game, bin_river, bin_river, 3)
arrmain = element(game, bin_green, bin_green, 3)

arrout = element(game, bin_terrain, bin_terrain, 4)

for e in arrmain:
game.draw_main(e[0], e[1])

for e in arrtree:
game.draw_tree(e[0], e[1])

for e in arrrock:
game.draw_rock(e[0], e[1])

for e in arrblack:
game.draw_black(e[0], e[1])
arrmain = element(game, bin_green, bin_green, 5)
areaxe = element(game, bin_axe, bin_axe, 3)

for e in arrriver:
game.draw_river(e[0], e[1])
arrout = element(game, bin_terrain, bin_terrain, 6)

unpack_array(arrmain, 'M', game)
unpack_array(arrriver,'R', game)
unpack_array(arrblack, 'B', game)
unpack_array(arrtree,'T', game)
unpack_array(arrrock, 'K', game)
unpack_array(arrout, '0', game)


game.matrix_add(areaxe[0][0], areaxe[0][1] + 1, 'A') # offset

game.matrix_add(x, y, 'P')

for e in arrout:
game.draw_out(e[0], e[1])
for i in range (2):
for j in range (20):
game.draw_out(i, j)
game.matrix_add(i, j, '0')
for j in range (20):
game.draw_out(34, j)



im2 = game.draw_image()


game.draw_player(mean_x, mean_y)

return im, im2

game.matrix_add(35, j, '0')


game.matrix_add(x//22, y//16, 'P')


def element(game, bin, im, nb):
tiny_offset = 0
result = []
for x in range (0, 790, 22):
for y in range (0, 322, 18):
l = int(tiny_offset)
arr = get_pixel_color(bin, x-12 - l , y)
arr1 = get_pixel_color(bin, x-5 - l , y)
arr2 = get_pixel_color(bin, x+2 - l , y)
for x in range (22, 810, 22):
for y in range (0, 320, 16):
arr0 = get_pixel_color(bin, x-10, y)
arr1 = get_pixel_color(bin, x-5 , y)
arr2 = get_pixel_color(bin, x+0 , y)

arr3 = get_pixel_color(bin, x-12 - l , y+12)
arr4 = get_pixel_color(bin, x-5 - l , y+12)
arr5 = get_pixel_color(bin, x+2 - l , y+12)
arr3 = get_pixel_color(bin, x-10, y+12)
arr4 = get_pixel_color(bin, x-5 , y+12)
arr5 = get_pixel_color(bin, x+0 , y+12)

arr6 = get_pixel_color(bin, x-12 - l , y+7)
arr7 = get_pixel_color(bin, x-5 - l , y+7)
arr8 = get_pixel_color(bin, x+2 - l , y+7)
arr6 = get_pixel_color(bin, x-10, y+7)
arr7 = get_pixel_color(bin, x-5 , y+7)
arr8 = get_pixel_color(bin, x+0 , y+7)

arrE = [arr, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8]
arrE = [arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8]
somme = 0
for i in range (len(arrE)):
somme += (arrE[i][0] != 0 and arrE[i][1] != 0 and arrE[i][2] != 0)
if somme >= nb: # !=
result.append([x//23, y//18])
grid(im)
if somme >= nb:
result.append([x//22 - 1, y//16]) # minus one because magic
return result


def grid(im):
tiny_offset = 0

for x in range (0, 820, 23):
tiny_offset += 0.8
for y in range (0, 330):
l = int(tiny_offset)
im = set_pixel_color(im, x - l + 3, y, (100,0,100))
tiny_offset = 0
for x in range (5, 900, 22):
tiny_offset += 0.1
for y in range (0, 400):
im = set_pixel_color(im, x + int(tiny_offset), y, (100,0,100))

for y in range (0, 400, 16):
for x in range (0, 900):
im = set_pixel_color(im, x, y, (100,0,100))

for y in range (0, 350, 19):
tiny_offset += 1.2
for x in range (0, 750):
l = int(tiny_offset)
im = set_pixel_color(im, x , y - l + 0, (100,0,100))

def dot(im):
tiny_offset = 0
for x in range (0, 790, 22):
for y in range (0, 322, 18):
l = int(tiny_offset)
for x in range (22, 810, 22):
for y in range (0, 320, 16):

im = set_area_color(im, x - 12 - l, y , (0,0,255), 2)
im = set_area_color(im, x - 5 - l, y , (0,255,255), 2)
im = set_area_color(im, x + 2 - l, y , (255,0,255), 2)
im = set_area_color(im, x - 10, y , (0,0,255), 2)
im = set_area_color(im, x - 5, y , (0,255,0), 2)
im = set_area_color(im, x + 0, y , (0,255,255), 2)

im = set_area_color(im, x - 12 - l, y + 12, (0,0,255), 2)
im = set_area_color(im, x - 5 - l, y + 12, (0,255,255), 2)
im = set_area_color(im, x + 2 - l, y + 12, (255,0,255), 2)
im = set_area_color(im, x - 10, y + 12, (255,0,0), 2)
im = set_area_color(im, x - 5, y + 12, (255,0,255), 2)
im = set_area_color(im, x + 0, y + 12, (255,255,0), 2)

im = set_area_color(im, x - 12 - l, y + 6, (0,0,255), 2)
im = set_area_color(im, x - 5 - l, y + 6, (0,255,255), 2)
im = set_area_color(im, x + 2 - l, y + 6, (255,0,255), 2)
im = set_area_color(im, x - 10, y + 6, (255,255,255), 2)
im = set_area_color(im, x - 5, y + 6, (50,50,50), 2)
im = set_area_color(im, x + 0, y + 6, (150,150,150), 2)


def rotate(image, angle):
Expand All @@ -183,8 +200,8 @@ def rotate(image, angle):
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result

# functions that allow you to work on a single screenshot instead of on the game

# functions that allow you to work on a single screenshot instead of on the game
def debug_show(im):
"""function that shows the image"""
hsv_frame = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
Expand All @@ -202,6 +219,7 @@ def debug_show(im):
if k == 27:
break


def show(im):
"""function that shows the image without edit"""
cv2.imshow("im1", im)
Expand All @@ -210,6 +228,7 @@ def show(im):
if k == 27:
break


def show2(ims):
"""function that shows the images without edit"""
for i in range (len(ims)):
Expand All @@ -219,6 +238,7 @@ def show2(ims):
if k == 27:
break


def debug_save(im):
"""function that saves the image"""
hsv_frame = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
Expand All @@ -232,6 +252,7 @@ def debug_save(im):
player.draw_contours(im, hsv_frame)
cv2.imwrite("../test_data/img_masked2.png", im)


def single_screenshot():
"""function that take a single screnshot of the game to work on it"""
hwnd = win32gui.FindWindow(None, "Unrailed!")
Expand All @@ -252,13 +273,13 @@ def single_screenshot():
cv2.COLOR_RGB2BGR
)


def save_screenshot():
"""function that take a single screnshot of the game and save it"""
cv2.imwrite("../test_data/img_debug.png", single_screenshot())


# functions to work on the image in BGR

def get_pixel_color(im, x, y):
"""function to get the pixel color"""
rows, cols = im.shape[:-1]
Expand Down
Loading

0 comments on commit 2fceaac

Please sign in to comment.