Make fishing agent capable of detecting bobber
This commit is contained in:
parent
369fe8d316
commit
d51914053c
BIN
assets/fishing/Bobber.jpg
Normal file
BIN
assets/fishing/Bobber.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 7.7 KiB |
BIN
assets/fishing/FishIcon.jpg
Normal file
BIN
assets/fishing/FishIcon.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.7 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.3 MiB After Width: | Height: | Size: 348 KiB |
|
|
@ -1,73 +1,67 @@
|
||||||
"""Module providing a fishing agent."""
|
"""Module providing a fishing agent."""
|
||||||
|
|
||||||
import os
|
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
import time
|
import time
|
||||||
import cv2 as cv
|
import cv2 as cv
|
||||||
import numpy as np
|
|
||||||
import pyautogui
|
import pyautogui
|
||||||
from core.Logger import Logger
|
from core.Logger import Logger
|
||||||
|
|
||||||
class FishingAgent:
|
class FishingAgent:
|
||||||
def __init__(self, monitor) -> None:
|
def __init__(self, monitor) -> None:
|
||||||
self.logger = Logger("Fishing Agent")
|
self.logger = Logger("Fishing Agent")
|
||||||
|
self.retry = 0
|
||||||
self.monitor = monitor
|
self.monitor = monitor
|
||||||
assetPath = os.path.join(
|
self.lureLoc = None
|
||||||
os.path.dirname(os.path.realpath(__file__)),
|
self.fishingBobberImg = cv.imread("assets/fishing/Bobber.jpg", cv.IMREAD_GRAYSCALE)
|
||||||
"..\\..\\assets"
|
self.fishingIconImg = cv.imread("assets/fishing/FishIcon.jpg", cv.IMREAD_GRAYSCALE)
|
||||||
)
|
|
||||||
self.fishingBobberImg = cv.imread(os.path.join(assetPath,"fishing","Bobber.png"), cv.IMREAD_GRAYSCALE)
|
|
||||||
self.fishingThread = None
|
self.fishingThread = None
|
||||||
|
|
||||||
def castLure(self):
|
def castLure(self):
|
||||||
self.agentLog("Casting Lure.")
|
self.retry = 0
|
||||||
# pyautogui.press('1')
|
pyautogui.moveTo(2,3)
|
||||||
time.sleep(1)
|
self.logger.log("Casting Lure.")
|
||||||
|
res = self.monitor.findMatchAndMoveToPosition(self.fishingIconImg)
|
||||||
|
if (res == -1):
|
||||||
|
self.logger.log("Trying again in 1 second...")
|
||||||
|
time.sleep(1)
|
||||||
|
self.castLure()
|
||||||
|
pyautogui.click()
|
||||||
self.findLure()
|
self.findLure()
|
||||||
|
|
||||||
def findLure(self):
|
def findLure(self):
|
||||||
if self.mainAgent.currImg is not None:
|
self.logger.log("Moving cursor to Lure.")
|
||||||
self.agentLog("Searching for Bobber.")
|
time.sleep(2)
|
||||||
cv.imshow("Bobber", self.fishingBobber)
|
res = self.monitor.findMatch(self.fishingBobberImg)
|
||||||
|
if (res == -1):
|
||||||
lureLoc = cv.matchTemplate(self.mainAgent.currImg, self.fishingBobber, cv.TM_CCORR_NORMED)
|
if (self.retry > 4):
|
||||||
lureLocArray = np.array(lureLoc)
|
self.logger.log("Took too to find lure long, retrying fishing from the beginning")
|
||||||
|
self.castLure()
|
||||||
minVal, maxVal, minLoc, maxLoc = cv.minMaxLoc(lureLocArray)
|
else:
|
||||||
self.moveToLure(maxLoc)
|
self.logger.log("Trying again in 1 second...")
|
||||||
else:
|
self.retry += 1
|
||||||
print("No curr img")
|
time.sleep(1)
|
||||||
|
self.findLure()
|
||||||
def moveToLure(self, maxLoc):
|
self.lureLoc = res
|
||||||
if maxLoc:
|
self.watchLure()
|
||||||
self.agentLog("Moving to Bobber.")
|
|
||||||
pyautogui.moveTo(maxLoc)
|
|
||||||
|
|
||||||
def watchLure(self):
|
def watchLure(self):
|
||||||
self.agentLog("Waiting for fish...")
|
self.logger.log("Waiting for fish...")
|
||||||
|
while True:
|
||||||
|
time.sleep(0.1)
|
||||||
|
res = self.monitor.findMatch(self.fishingBobberImg)
|
||||||
|
print(res)
|
||||||
|
|
||||||
|
|
||||||
def pullLine(self):
|
def pullLine(self):
|
||||||
self.agentLog("Pulling line!")
|
self.logger.log("Pulling line!")
|
||||||
|
|
||||||
def test(self):
|
|
||||||
while True:
|
|
||||||
time.sleep(1)
|
|
||||||
self.logger.log("###")
|
|
||||||
point = self.monitor.findMatch(self.fishingBobberImg)
|
|
||||||
pyautogui.moveTo(point[0], point[1])
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.monitor.screenshot is None:
|
|
||||||
self.logger.log("Screenshot capture not found...")
|
|
||||||
return
|
|
||||||
self.logger.log("Starting fishing thread in 3 seconds...")
|
self.logger.log("Starting fishing thread in 3 seconds...")
|
||||||
time.sleep(3)
|
time.sleep(1)
|
||||||
|
self.logger.log("Starting fishing thread in 2 seconds...")
|
||||||
# print("Switching to fishing hotbar (hotbar 4)")
|
time.sleep(1)
|
||||||
# pyautogui.keyDown('shift')
|
self.logger.log("Starting fishing thread in 1 seconds...")
|
||||||
# pyautogui.press('4')
|
time.sleep(1)
|
||||||
# pyautogui.keyUp('shift')
|
|
||||||
# time.sleep(1)
|
|
||||||
|
|
||||||
self.fishingThread = Thread(
|
self.fishingThread = Thread(
|
||||||
target=self.castLure,
|
target=self.castLure,
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,9 @@ class Monitor:
|
||||||
fpsPrintTime = time.time()
|
fpsPrintTime = time.time()
|
||||||
while True:
|
while True:
|
||||||
newScreenshot = pyautogui.screenshot()
|
newScreenshot = pyautogui.screenshot()
|
||||||
newScreenshot = cv.cvtColor(np.array(newScreenshot), cv.IMREAD_GRAYSCALE)
|
newScreenshot = cv.cvtColor(np.array(newScreenshot), cv.COLOR_RGB2BGR)
|
||||||
self.screenshot = newScreenshot
|
grayScreenshot = cv.cvtColor(newScreenshot, cv.COLOR_BGR2GRAY)
|
||||||
|
self.screenshot = grayScreenshot
|
||||||
cv.imwrite("assets/screenshot.jpg", self.screenshot)
|
cv.imwrite("assets/screenshot.jpg", self.screenshot)
|
||||||
currTime = time.time()
|
currTime = time.time()
|
||||||
if currTime - fpsPrintTime >= FPS_REPORT_DELAY:
|
if currTime - fpsPrintTime >= FPS_REPORT_DELAY:
|
||||||
|
|
@ -46,12 +47,16 @@ class Monitor:
|
||||||
|
|
||||||
def findMatch(self, template, method = "TM_CCOEFF_NORMED"):
|
def findMatch(self, template, method = "TM_CCOEFF_NORMED"):
|
||||||
methodInt = getattr(cv, method)
|
methodInt = getattr(cv, method)
|
||||||
screen = cv.imread("assets/screenshot.jpg", cv.IMREAD_GRAYSCALE)
|
res = cv.matchTemplate(self.screenshot, template, methodInt)
|
||||||
assert template is not None, "file could not be read, check with os.path.exists()"
|
|
||||||
res = cv.matchTemplate(screen, template, methodInt)
|
|
||||||
w, h = template.shape[::-1]
|
w, h = template.shape[::-1]
|
||||||
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
|
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
|
||||||
|
|
||||||
|
matchRatio = max_val * 100
|
||||||
|
self.logger.log(matchRatio)
|
||||||
|
if (matchRatio < 40):
|
||||||
|
self.logger.log("Cannot find matching result...")
|
||||||
|
return -1
|
||||||
|
|
||||||
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
|
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
|
||||||
if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
|
if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
|
||||||
top_left = min_loc
|
top_left = min_loc
|
||||||
|
|
@ -61,6 +66,14 @@ class Monitor:
|
||||||
|
|
||||||
return (top_left[0] + bottom_right[0]) / 2, (top_left[1] + bottom_right[1]) / 2
|
return (top_left[0] + bottom_right[0]) / 2, (top_left[1] + bottom_right[1]) / 2
|
||||||
|
|
||||||
|
def findMatchAndMoveToPosition(self, template, method = "TM_CCOEFF_NORMED"):
|
||||||
|
point = self.findMatch(template, method)
|
||||||
|
if (point == -1):
|
||||||
|
return -1
|
||||||
|
time.sleep(0.2)
|
||||||
|
pyautogui.moveTo(point[0], point[1])
|
||||||
|
time.sleep(0.15)
|
||||||
|
|
||||||
def stopScreenCaptureThread(self):
|
def stopScreenCaptureThread(self):
|
||||||
if (self.screenThread):
|
if (self.screenThread):
|
||||||
self.screenThread.terminate()
|
self.screenThread.terminate()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user