-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
129 lines (101 loc) · 3.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import math
import random
import time
import numpy as np
import pyautogui
from fiber_detection import get_detection_fiber
from gather_state import get_gather_state
from value_sort import sort_resource_by_value
pyautogui.FAILSAFE = False
def random_move():
pyautogui.moveTo(
random.choice(range(1, 1920)),
random.choice(range(1, 1080)),
2,
pyautogui.easeInOutQuad,
)
pyautogui.click()
time.sleep(random.choice(range(1, 3)))
def stream(image):
"""
based on stream of image, create the best decision
"""
return {"fiber_inference": get_detection_fiber(image)}
def get_nearest_resource(result):
k0, j0 = (1920 / 2, 1080 / 2)
x0, y0 = (1920, 1080)
nearest = 0
for index, item in enumerate(result):
if item["confidence"] < 0.7:
continue
x1, y1, x2, y2 = (
item["box"]["x1"],
item["box"]["y1"],
item["box"]["x2"],
item["box"]["y2"],
)
k1, j1 = math.ceil((x1 + x2) / 2), math.ceil((y1 + y2) / 2)
if (k1 - k0) ** 2 + (j1 - j0) ** 2 < (x0 - k0) ** 2 + (y0 - k0) ** 2:
x0, y0 = k1, j1
nearest = index
bbox = result[nearest]["box"]
x1, y1, x2, y2 = bbox["x1"], bbox["y1"], bbox["x2"], bbox["y2"]
return x1, y1, x2, y2
def mount_up():
pyautogui.press("a")
def act(env_state):
"""
based on env state, agent will decide what to do
"""
fiber_inference = env_state["fiber_inference"][0]
if len(fiber_inference) != 0:
# array = sort_resource_by_value(fiber_inference)
# box = array[0]["box"]
# pyautogui.moveTo(
# math.ceil((box["x1"] + box["x2"]) / 2),
# math.ceil((box["y1"] + box["y2"]) / 2),
# 0.5,
# pyautogui.easeInOutQuad,
# )
# nearest = get_nearest_resource(fiber_inference)
x1, y1, x2, y2 = (
fiber_inference[0]["box"]["x1"],
fiber_inference[0]["box"]["y1"],
fiber_inference[0]["box"]["x2"],
fiber_inference[0]["box"]["y2"],
)
pyautogui.moveTo(
math.ceil((x1 + x2) / 2),
math.ceil((y1 + y2) / 2),
0.5,
pyautogui.easeInOutQuad,
)
pyautogui.click()
start_time = time.time()
while True:
result = call_back()
if result:
break
if time.time() - start_time > 5:
break
def call_back():
region = (800, 200, 300, 600)
array = []
while True:
screenshot = pyautogui.screenshot(region=region)
img_np = np.array(screenshot)
array.append(img_np)
if len(array) == 3:
break
for img in array:
gather_state = get_gather_state(img)
print(gather_state)
if "0/9" in gather_state or "0/6" in gather_state:
return True
def main_loop():
while True:
image = pyautogui.screenshot()
env = stream(image)
act(env)
# Start the main loop
main_loop()