Compare commits

...

3 Commits

Author SHA1 Message Date
86b6d060aa big fix equip ui 2022-10-11 17:24:17 +02:00
6cd9cc84d2 added craft bot 2022-10-11 17:23:19 +02:00
8376dba417 added ui support to farm bot 2022-10-11 17:22:46 +02:00
11 changed files with 557 additions and 1 deletions

View File

@@ -0,0 +1,93 @@
# Run tkinter code in another thread
import threading
import tkinter as tk
from tkinter import ttk
class CraftOverlay(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.run_mode = 'init'
self.root = tk.Tk
self.crafts_use = tk.StringVar
self.mode_use = tk.StringVar
self.CraftsLabel = tk.Label
self.CraftsEntry = tk.Entry
self.ModeLabel = tk.Label
self.Mode_Box = ttk.Combobox
self.ButtonFrame = tk.Frame
self.StartButton = tk.Button
self.StopButton = tk.Button
self.TkPosition = '133x92+60+600'
self.StatusLabel = tk.Label
self.start()
def run(self):
self.root = tk.Tk()
self.crafts_use = tk.StringVar(self.root, value='100')
self.mode_use = tk.StringVar(self.root, value='m25x1')
self.CraftsLabel = tk.Label(self.root, text="Crafts:", font=("Helvetica", 10, "bold"),
background="grey")
self.CraftsLabel.grid(row=0, column=0, sticky='w')
self.CraftsEntry = tk.Entry(self.root, textvariable=self.crafts_use, font=("Helvetica", 10, "bold"), width='12')
self.CraftsEntry.grid(row=0, column=1, columnspan=2)
self.ModeLabel = tk.Label(self.root, text="Mode:", font=("Helvetica", 10, "bold"),
background="grey")
self.ModeLabel.grid(row=2, column=0, sticky='w')
self.Mode_Box = ttk.Combobox(self.root, textvariable=self.mode_use, font=("Helvetica", 8, "bold"),
width='11')
self.Mode_Box['values'] = ('m25x1', 'm10x2', 'm5x3', 'simple', 'm1x5_T', 'm1x5_X', 'house')
self.Mode_Box.grid(row=2, column=1, columnspan=2, sticky='w')
self.ButtonFrame = tk.Frame(self.root)
self.StartButton = tk.Button(self.ButtonFrame, text="Start", command=self.start_button_callback, width='8')
self.StartButton.grid(row=0, column=0)
self.StopButton = tk.Button(self.ButtonFrame, text="Stop", command=self.stop_button_callback, width='8',
state=tk.DISABLED)
self.StopButton.grid(row=0, column=1)
self.ButtonFrame.grid(row=3, column=0, columnspan=2)
self.StatusLabel = tk.Label(self.root, text="", font=("Helvetica", 10, "bold"),
background="grey")
self.StatusLabel.grid(row=4, column=0, columnspan=2)
# self.ClearButton.pack(side="top")
self.root.geometry(self.TkPosition)
self.root.overrideredirect(1) # fenster ohne aussen rum :-)
# self.root.attributes('-alpha', 0.7) # fenster transparent
self.root.attributes('-topmost', 1) # fenster immer im vordergrund
# self.root.wm_attributes("-disabled", True)
self.root.configure(background='black')
self.root.mainloop()
def start_button_callback(self):
self.StartButton.configure(state=tk.DISABLED)
self.StopButton.configure(state=tk.NORMAL)
self.StatusLabel.configure(text='')
self.run_mode = 'started'
def stop_button_callback(self):
self.StartButton.configure(state=tk.NORMAL)
self.StopButton.configure(state=tk.DISABLED)
self.run_mode = 'stopped'
def get_run_mode(self):
return self.run_mode
def update_status_label(self, mode, energy_to_go):
text = mode + ": " + str(energy_to_go)
self.StatusLabel.configure(text=text)

335
craft_table/main_craft.py Normal file
View File

@@ -0,0 +1,335 @@
from time import time
import sys
import cv2 as cv
import keyboard
from utils import get_click_point, dig_point, check_for_ok_button, check_for_craft_button
import pydirectinput
from craft_overlay import CraftOverlay
from config_file import UserConfigs
from window_capture import WindowCapture
from vision import Vision
MODE25X1 = "m25x1"
MODE10X2 = "m10x2"
MODE5X3 = "m5x3"
MODE_SIMPLE_STACK = "simple"
MODE1X5_T = "m1x5_T"
MODE1X5_X = "m1x5_X"
MODE_HOUSE = "house"
def run():
overlay = CraftOverlay()
while True:
if overlay.run_mode == 'stopped':
overlay.update_status_label("stopped", "0")
overlay.run_mode = 'init'
continue
elif overlay.run_mode == 'started':
cv.waitKey(500)
elif overlay.run_mode == 'init':
continue
elif overlay.run_mode == 'finished':
overlay.update_status_label("finished", "0")
overlay.run_mode = 'init'
continue
else:
#boelk
pass
mode = str(overlay.mode_use.get())
run_target = int(overlay.crafts_use.get())
run_counter = 0
while True:
if overlay.run_mode == 'stopped':
break
if run_counter >= run_target:
run_counter = 0
overlay.run_mode = 'finished'
break
ab1 = [420, 1180, 142, 142]
ab2 = [562, 1180, 142, 142]
ab3 = [704, 1180, 142, 142]
ab4 = [846, 1180, 142, 142]
ab5 = [986, 1180, 142, 142]
ab6 = [1128, 1180, 142, 142]
ab10 = [1698, 1180, 142, 142]
if mode == MODE25X1:
click_square_center(ab1)
# grid 770 1630 160 1020 172 o
dim = 172
for i in range(0, 5, 1):
for e in range(0, 5, 1):
click_square_center([770 + (i * dim), 160 + (e * dim), dim, dim])
if overlay.run_mode == 'stopped':
break
click_craft_button()
for i in range(0, 5, 1):
for e in range(0, 5, 1):
collect_grid_reverse_click([770 + (i * dim), 160 + (e * dim), dim, dim])
run_counter = run_counter + 1
overlay.update_status_label(mode, run_target - run_counter)
if overlay.run_mode == 'stopped':
break
click_square_center(ab10)
elif mode == MODE10X2:
for r in range(0, 2, 1):
# grid 770 1630 160 1020 172
dim = 172
if r == 0:
click_square_center(ab1)
start = 0
target = 3
else:
click_square_center(ab2)
start = 1
target = 4
for i in range(start, target, 2):
for e in range(0, 5, 1):
click_square_center([770 + (i * dim), 160 + (e * dim), dim, dim])
if overlay.run_mode == 'stopped':
break
click_craft_button()
for i in range(0, 4, 2):
for e in range(0, 5, 1):
collect_grid_reverse_click([770 + (i * dim), 160 + (e * dim), dim, dim])
run_counter = run_counter + 1
overlay.update_status_label(mode, run_target - run_counter)
if overlay.run_mode == 'stopped':
break
click_square_center(ab10)
elif mode == MODE5X3:
if overlay.run_mode == 'stopped':
break
for r in range(0, 3, 1):
# grid 770 1630 160 1020 172oo
dim = 172
if r == 0:
click_square_center(ab1)
start = 0
target = 1
elif r == 1:
click_square_center(ab2)
start = 1
target = 2
else:
click_square_center(ab3)
start = 2
target = 3
for i in range(start, target, 1):
for e in range(0, 5, 1):
click_square_center([770 + (i * dim), 160 + (e * dim), dim, dim])
if overlay.run_mode == 'stopped':
break
click_craft_button()
for i in range(0, 1, 1):
if overlay.run_mode == 'stopped':
break
for e in range(0, 5, 1):
collect_grid_reverse_click([770 + (i * dim), 160 + (e * dim), dim, dim])
run_counter = run_counter + 1
overlay.update_status_label(mode, run_target - run_counter)
if overlay.run_mode == 'stopped':
break
click_square_center(ab10)
elif mode == MODE1X5_T:
dim = 172
tps = []
tps.append(ab1)
tps.append([770 + (0 * dim), 160 + (2 * dim), dim, dim])
tps.append([770 + (1 * dim), 160 + (2 * dim), dim, dim])
tps.append(ab2)
tps.append([770 + (2 * dim), 160 + (1 * dim), dim, dim])
tps.append([770 + (2 * dim), 160 + (2 * dim), dim, dim])
tps.append([770 + (2 * dim), 160 + (3 * dim), dim, dim])
result_tp = [770 + (0 * dim), 160 + (3 * dim), dim, dim]
for tp in tps:
click_square_center(tp)
if overlay.run_mode == 'stopped':
break
click_craft_button()
if overlay.run_mode == 'stopped':
break
click_square_center(result_tp)
if overlay.run_mode == 'stopped':
break
click_square_center(ab10)
run_counter = run_counter + 1
overlay.update_status_label(mode, run_target - run_counter)
if overlay.run_mode == 'stopped':
break
elif mode == MODE1X5_X:
dim = 172
tps = []
tps.append(ab1)
tps.append([770 + (0 * dim), 160 + (3 * dim), dim, dim])
tps.append([770 + (1 * dim), 160 + (3 * dim), dim, dim])
tps.append([770 + (2 * dim), 160 + (3 * dim), dim, dim])
tps.append([770 + (1 * dim), 160 + (4 * dim), dim, dim])
tps.append([770 + (1 * dim), 160 + (2 * dim), dim, dim])
result_tp = [770 + (0 * dim), 160 + (4 * dim), dim, dim]
for tp in tps:
click_square_center(tp)
if overlay.run_mode == 'stopped':
break
click_craft_button()
if overlay.run_mode == 'stopped':
break
click_square_center(result_tp)
if overlay.run_mode == 'stopped':
break
click_square_center(ab10)
run_counter = run_counter + 1
overlay.update_status_label(mode, run_target - run_counter)
if overlay.run_mode == 'stopped':
break
elif mode == MODE_HOUSE:
dim = 172
cements = []
cements.append([770 + (0 * dim), 160 + (4 * dim), dim, dim])
cements.append([770 + (1 * dim), 160 + (4 * dim), dim, dim])
cements.append([770 + (2 * dim), 160 + (4 * dim), dim, dim])
cements.append([770 + (3 * dim), 160 + (4 * dim), dim, dim])
cements.append([770 + (4 * dim), 160 + (4 * dim), dim, dim])
bricks = []
bricks.append([770 + (0 * dim), 160 + (3 * dim), dim, dim])
bricks.append([770 + (1 * dim), 160 + (3 * dim), dim, dim])
bricks.append([770 + (3 * dim), 160 + (3 * dim), dim, dim])
bricks.append([770 + (4 * dim), 160 + (3 * dim), dim, dim])
bricks.append([770 + (0 * dim), 160 + (2 * dim), dim, dim])
bricks.append([770 + (2 * dim), 160 + (2 * dim), dim, dim])
bricks.append([770 + (4 * dim), 160 + (2 * dim), dim, dim])
boards = []
boards.append([770 + (2 * dim), 160 + (3 * dim), dim, dim])
boards.append([770 + (0 * dim), 160 + (1 * dim), dim, dim])
boards.append([770 + (1 * dim), 160 + (1 * dim), dim, dim])
boards.append([770 + (2 * dim), 160 + (1 * dim), dim, dim])
boards.append([770 + (3 * dim), 160 + (1 * dim), dim, dim])
boards.append([770 + (4 * dim), 160 + (1 * dim), dim, dim])
boards.append([770 + (2 * dim), 160 + (0 * dim), dim, dim])
glasses = []
glasses.append([770 + (1 * dim), 160 + (2 * dim), dim, dim])
glasses.append([770 + (3 * dim), 160 + (2 * dim), dim, dim])
tp_hammer = [770 + (1 * dim), 160 + (0 * dim), dim, dim]
tp_nails = [770 + (3 * dim), 160 + (0 * dim), dim, dim]
tp_result = [770 + (0 * dim), 160 + (4 * dim), dim, dim]
click_square_center(ab1)
for cement in cements:
click_square_center(cement)
if overlay.run_mode == 'stopped':
break
click_square_center(ab2)
for brick in bricks:
click_square_center(brick)
if overlay.run_mode == 'stopped':
break
click_square_center(ab3)
for board in boards:
click_square_center(board)
if overlay.run_mode == 'stopped':
break
click_square_center(ab4)
for glass in glasses:
click_square_center(glass)
if overlay.run_mode == 'stopped':
break
click_square_center(ab5)
click_square_center(tp_hammer)
if overlay.run_mode == 'stopped':
break
click_square_center(ab6)
click_square_center(tp_nails)
if overlay.run_mode == 'stopped':
break
click_craft_button()
if overlay.run_mode == 'stopped':
break
click_square_center(tp_result)
click_square_center(ab10)
if overlay.run_mode == 'stopped':
break
run_counter = run_counter + 1
overlay.update_status_label(mode, run_target - run_counter)
if overlay.run_mode == 'stopped':
break
elif mode == MODE_SIMPLE_STACK:
# initialize the StunWindowCapture class
capture_config = UserConfigs()
#capture_window = WindowCapture(None, "screen_conf", capture_config)
capture_window2 = WindowCapture(None, "bla", capture_config)
# initialize the StunVision class
vision_stun = Vision()
# odig_overlay = DiggingOverlay(config)
check_for_craft_button(capture_window2, vision_stun, capture_config)
run_counter = run_counter + 1
overlay.update_status_label(mode, run_target - run_counter)
#check_for_ok_button(capture_window, vision_stun, capture_config)
def collect_grid_reverse_click(square):
pydirectinput.moveTo(get_click_point(square)[0], get_click_point(square)[1])
cv.waitKey(50)
pydirectinput.mouseUp()
cv.waitKey(50)
pydirectinput.mouseDown()
cv.waitKey(100)
def click_craft_button():
dig_point(1910, 330, 100)
cv.waitKey(1500)
def click_square_center(square):
cp = get_click_point(square)
dig_point(cp[0], cp[1], 100)
cv.waitKey(100)
if __name__ == "__main__":
run()
sys.exit()

BIN
craft_table/ok_button.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
craft_table/play.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 659 KiB

View File

@@ -40,6 +40,10 @@ def run():
continue
elif overlay.run_mode == 'paused':
continue
elif overlay.run_mode == 'finished':
overlay.update_status_label("finished", "0")
overlay.run_mode = 'init'
continue
else:
break
@@ -250,6 +254,8 @@ def run():
print('FPS {}'.format(1 / (time() - outer_loop_time)))
outer_loop_time = time()
cv.waitKey(150)
overlay.run_mode = 'finished'
def check_and_move_tile(capture_win, visio, conf, rect, needl, hsv, tresh, mask, point_source, point_dest):

View File

@@ -51,6 +51,7 @@ class Field:
colors.append(CHEMTRANT)
colors.append(TENESENT)
colors.append(CIBUTRANT)
colors.append(ARTISENT)
def __init__(self):
@@ -92,7 +93,8 @@ class Field:
MAGINENT: cv.imread("maginent.jpg", cv.IMREAD_COLOR),
CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR),
TENESENT: cv.imread("tenesent.jpg", cv.IMREAD_COLOR),
CIBUTRANT: cv.imread("cibutrant.jpg", cv.IMREAD_COLOR)
CIBUTRANT: cv.imread("cibutrant.jpg", cv.IMREAD_COLOR),
ARTISENT: cv.imread("artisent.jpg", cv.IMREAD_COLOR)
}
def reset(self):

BIN
farm/artisent.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

66
farm/farm_overlay.py Normal file
View File

@@ -0,0 +1,66 @@
# Run tkinter code in another thread
import threading
import tkinter as tk
from tkinter import ttk
class FarmOverlay(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.run_mode = 'init'
self.root = tk.Tk
self.ButtonFrame = tk.Frame
self.StartButton = tk.Button
self.StopButton = tk.Button
self.TkPosition = '133x52+60+600'
self.StatusLabel = tk.Label
self.start()
def run(self):
self.root = tk.Tk()
self.ButtonFrame = tk.Frame(self.root)
self.StartButton = tk.Button(self.ButtonFrame, text="Start", command=self.start_button_callback, width='8')
self.StartButton.grid(row=0, column=0)
self.StopButton = tk.Button(self.ButtonFrame, text="Stop", command=self.stop_button_callback, width='8',
state=tk.DISABLED)
self.StopButton.grid(row=0, column=1)
self.ButtonFrame.grid(row=3, column=0, columnspan=2)
self.StatusLabel = tk.Label(self.root, text="", font=("Helvetica", 10, "bold"),
background="grey")
self.StatusLabel.grid(row=4, column=0, columnspan=2)
# self.ClearButton.pack(side="top")
self.root.geometry(self.TkPosition)
self.root.overrideredirect(1) # fenster ohne aussen rum :-)
# self.root.attributes('-alpha', 0.7) # fenster transparent
self.root.attributes('-topmost', 1) # fenster immer im vordergrund
# self.root.wm_attributes("-disabled", True)
self.root.configure(background='black')
self.root.mainloop()
def start_button_callback(self):
self.StartButton.configure(state=tk.DISABLED)
self.StopButton.configure(state=tk.NORMAL)
self.StatusLabel.configure(text='')
self.run_mode = 'started'
def stop_button_callback(self):
self.StartButton.configure(state=tk.NORMAL)
self.StopButton.configure(state=tk.DISABLED)
self.run_mode = 'stopped'
def get_run_mode(self):
return self.run_mode
def update_status_label(self, mode, energy_to_go):
text = mode + ": " + str(energy_to_go)
self.StatusLabel.configure(text=text)

35
farm/main_farm_ui.py Normal file
View File

@@ -0,0 +1,35 @@
from Field_Representation import Field
from time import time
import cv2 as cv
import keyboard
from farm_overlay import FarmOverlay
def run():
field = Field()
overlay = FarmOverlay()
while True:
if overlay.run_mode == 'stopped':
overlay.update_status_label("stopped", "0")
overlay.run_mode = 'init'
continue
elif overlay.run_mode == 'started':
cv.waitKey(500)
elif overlay.run_mode == 'init':
continue
else:
# boelk
pass
while True:
if overlay.run_mode == 'stopped':
break
cords = field.assess_playfield_and_make_move()
print(cords)
cv.waitKey(1000)
if __name__ == "__main__":
run()

View File

@@ -12,6 +12,7 @@ import cv2 as cv
import pydirectinput
import keyboard
def mse(imageA, imageB):
# the 'Mean Squared Error' between the two images is the
# sum of the squared difference between the two images;
@@ -173,3 +174,21 @@ def check_for_ok_button(cap_win, vis, conf):
for pointi in pointis:
dig_point(pointi[0] + offset_left, pointi[1] + offset_down, 150)
def check_for_craft_button(cap_win, vis, conf):
screenshot = cap_win.get_screenshot()
needle = cv.imread("play.jpg", cv.IMREAD_UNCHANGED)
rectangles = vis.find(screenshot, needle, 0.7, 1)
if len(rectangles) == 1:
pointis = vis.get_click_points(rectangles)
for pointi in pointis:
dig_point(pointi[0], pointi[1], 150)
def get_click_point(rectangle):
# Loop over all the rectangles
x, y, w, h = rectangle
# Determine the center position
center_x = x + int(w / 2)
center_y = y + int(h / 2)
# Save the points
return int(center_x), int(center_y)