jezv 11 miesięcy temu
rodzic
commit
ac29a7ea5b
1 zmienionych plików z 64 dodań i 29 usunięć
  1. 64 29
      trex/main.py

+ 64 - 29
trex/main.py

@@ -12,6 +12,7 @@ cv2.namedWindow('View', cv2.WINDOW_NORMAL)
 DEFAULT_MONITOR = 3
 DEBUG = True
 scheduler = sched.scheduler(time, sleep)
+dilation_kernel = np.array([[0,0,0], [1, 1, 1], [0,0,0]]).astype('uint8')
 
 
 def take_screenshot(sct, monitor=DEFAULT_MONITOR):
@@ -49,19 +50,34 @@ def open_dino(sct):
     print('loaded')
 
 
+def shortest_jump():
+    with gui.hold('space'):
+        sleep(0.1)
+    take_down()
+
+
 def short_jump():
     with gui.hold('space'):
-        sleep(0.07)
+        sleep(0.15)
+    take_down()
 
 
 def long_jump():
     with gui.hold('space'):
         sleep(0.2)
+    take_down()
 
 
 def take_down():
     with gui.hold('down'):
-        sleep(0.05)
+        sleep(0.04)
+
+
+def jump(sec):
+    print('jump suka jump')
+    with gui.hold('space'):
+        sleep(sec)
+    take_down()
 
 
 def get_playing_bbox(sct):
@@ -73,14 +89,14 @@ def get_playing_bbox(sct):
 
     binScreen = take_binaryScreenshot(sct, sct.monitors[DEFAULT_MONITOR])
     x, y = np.where(binScreen == 1)
-    mw = int((y.max()-y.min()) / 100)
-    mh = int((x.max()-x.min()) / 100)
+    mw = (y.max()-y.min()) // 100
+    mh = (x.max()-x.min()) // 100
     playing_bbox = {'left':y.min()-2*mw, 'top':x.min()+15*mh, 'width':(y.max()-y.min())+4*mw, 'height':(x.max()-x.min())+10*mh}
     return playing_bbox, mw, mh
 
 
 def to_bbox(bin_image):
-    bin_image = cv2.dilate(bin_image, None, iterations=2)
+    bin_image = cv2.dilate(bin_image, dilation_kernel, iterations=4)
     labels, labeledScreen = cv2.connectedComponents(bin_image.astype('uint8'))
     for label in range(1, labels):
         x,y = np.where(labeledScreen==label)
@@ -97,42 +113,61 @@ if __name__ == '__main__':
     open_dino(sct)
     gui.hotkey('fn', 'f11')
 
-    playing_bbox, mw, mh = get_playing_bbox(sct)
-    dino_pos = None
-    awaitable = None
+    playing_bbox, pw, ph = get_playing_bbox(sct)
 
     binScreen = take_binaryScreenshot(sct, playing_bbox)
     binScreen = to_bbox(binScreen)
     labels, labeledScreen = cv2.connectedComponents(binScreen.astype('uint8'))
     if DEBUG: rgbScreen = binary_to_RGB(binScreen)
-    dino = min(range(1,labels), key=lambda x: np.where(labeledScreen==x)[1].min()) 
-    dino_pos = np.where(labeledScreen == dino)[1].max() + 5*mw
-    jump_zone = dino_pos + 13*mw
-    if DEBUG: rgbScreen[labeledScreen==dino] = (0, 255, 0)
+    dino_label = min(range(1,labels), key=lambda x: np.where(labeledScreen==x)[1].min()) 
+    dino_pos = np.where(labeledScreen==dino_label)
+    dino_max_x = dino_pos[1].max()
+    dino_min_y = dino_pos[0].max()
+
+    coef = 1.2
+    trigger_second = (int(dino_pos[0].mean()), int(dino_max_x + int(coef*10)*pw))
+    trigger_first = (int(dino_pos[0].mean()), int(75*pw))
+    jumping = False
+    taking = False
 
     start = time()
 
+    cv2.imshow('View', rgbScreen) 
     while cv2.waitKey(1) != ord('q'):
         binScreen = take_binaryScreenshot(sct, playing_bbox)
         binScreen = to_bbox(binScreen)
         labels, labeledScreen = cv2.connectedComponents(binScreen.astype('uint8'))
-        if DEBUG: rgbScreen = binary_to_RGB(binScreen)
-        for label in range(1, labels):
-            pos = np.where(labeledScreen == label)
-
-            if pos[1].max() < dino_pos:
-                if DEBUG: rgbScreen[pos] = (0, 255, 0)
-
-            elif pos[1].max() < jump_zone + max(1*(pos[0].max() - pos[0].min()), 1*(pos[1].max() - pos[1].min())) and (pos[1].max() - pos[1].min()) > mw :
-                # Ближайший Противник                       
-                if DEBUG: rgbScreen[pos] = (0, 0, 255)
-                if (pos[1].max() - pos[1].min()) > 8*mw:
-                    long_jump()
-                else: short_jump()
-                
-        if time() - start > 10: 
-            jump_zone += mw
+
+        if not taking and not jumping and binScreen[trigger_second] != 0:
+            print('jumping')
+            gui.keyDown('space')
+            jumping = True
+
+        elif jumping and binScreen[trigger_second[0], trigger_second[1]-pw] == 0:
+            print('landing')
+            gui.keyUp('space')
+            gui.keyDown('down')
+            jumping = False
+            taking = True
+        
+        elif taking and binScreen[dino_min_y, dino_max_x] != 0:
+            print('landed')
+            gui.keyUp('down')
+            taking = False
+
+        if time() - start > 10:
+            print(time() - start)
+            coef += 0.1
+            trigger_second = (trigger_second[0], trigger_second[1]+pw)
+            # trigger_first = (trigger_first[0], trigger_first[0]+int(pw))
             start = time()
-        cv2.imshow('View', rgbScreen)
+
+        if DEBUG: 
+            rgbScreen = binary_to_RGB(binScreen)
+            cv2.circle(rgbScreen, trigger_second[::-1], radius=2, color=(0, 255, 0), thickness=2)
+            cv2.circle(rgbScreen, trigger_first[::-1], radius=2, color=(0, 0, 255), thickness=2)
+            cv2.imshow('View', rgbScreen)
+        
+
     
     cv2.destroyAllWindows()