From 2893b2270c76d63b6e4171c787116c08a7931aae Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Sun, 18 Oct 2020 03:34:25 +0530 Subject: [PATCH] full auto engine done - semi fisher, calls last event when subscribed - corrected ping pong logic - f8 to stop player - save coods image if not able to read for debug purpose - created better controls --- fishy/engine/fullautofisher/engine.py | 100 +++++++++++++++++------ fishy/engine/fullautofisher/player.py | 33 ++++++-- fishy/engine/fullautofisher/recorder.py | 1 + fishy/engine/semifisher/fishing_event.py | 3 +- fishy/helper/hotkey.py | 4 +- 5 files changed, 104 insertions(+), 37 deletions(-) diff --git a/fishy/engine/fullautofisher/engine.py b/fishy/engine/fullautofisher/engine.py index 23aca10..5893b48 100644 --- a/fishy/engine/fullautofisher/engine.py +++ b/fishy/engine/fullautofisher/engine.py @@ -1,4 +1,5 @@ import math +import uuid import cv2 import logging @@ -15,13 +16,13 @@ from fishy.engine.semifisher import fishing_mode, fishing_event from fishy.engine.common.IEngine import IEngine from pynput import keyboard, mouse -from fishy.helper import hotkey +from fishy.helper import hotkey, helper from fishy.helper.config import config from fishy.helper.hotkey import Key mse = mouse.Controller() kb = keyboard.Controller() -offset = 10 +offset = 0 def sign(x): @@ -63,10 +64,12 @@ def get_values_from_image(img, tesseract_dir): tessdata_dir_config = f'--tessdata-dir "{tesseract_dir}" -c tessedit_char_whitelist=0123456789.' text = pytesseract.image_to_string(img, lang="eng", config=tessdata_dir_config) + text = text.replace(" ", "") vals = text.split(":") return float(vals[0]), float(vals[1]), float(vals[2]) except Exception: logging.error("Couldn't read coods") + cv2.imwrite(f"fail_{str(uuid.uuid4())[:8]}", img) return None @@ -136,14 +139,21 @@ class FullAuto(IEngine): return current = self.get_coods() - print(f"Moving from {(current[0], current[1])} to {self._target}") + print(f"Moving from {(current[0], current[1])} to {target}") move_vec = target[0] - current[0], target[1] - current[1] - target_angle = math.degrees(math.atan2(-move_vec[1], move_vec[0])) + + dist = math.sqrt(move_vec[0] ** 2 + move_vec[1] ** 2) + print(f"distance: {dist}") + if dist < 5e-05: + print("distance very small skipping") + return + + target_angle = math.degrees(math.atan2(-move_vec[1], move_vec[0])) + 90 from_angle = current[2] self.rotate_to(target_angle, from_angle) - walking_time = math.sqrt(move_vec[0] ** 2 + move_vec[1] ** 2) / self.factors[0] + walking_time = dist / self.factors[0] print(f"walking for {walking_time}") kb.press('w') time.sleep(walking_time) @@ -154,9 +164,9 @@ class FullAuto(IEngine): if from_angle is None: _, _, from_angle = self.get_coods() + if target_angle < 0: target_angle = 360 + target_angle - target_angle += 90 while target_angle > 360: target_angle -= 360 print(f"Rotating from {from_angle} to {target_angle}") @@ -187,7 +197,7 @@ class FullAuto(IEngine): fishing_mode.subscribers.append(found_hole) t = 0 - while not self._hole_found_flag and t <= self.factors[2] / 2: + while not self._hole_found_flag and t <= self.factors[2] / 3: mse.move(0, FullAuto.rotate_by) time.sleep(0.05) t += 0.05 @@ -206,29 +216,67 @@ class FullAuto(IEngine): time.sleep(0.05) self._curr_rotate_y -= 0.05 - def set_target(self): - t = self.get_coods()[:-1] - config.set("target", t) - print(f"target_coods are {t}") - def initalize_keys(self): - - hotkey.set_hotkey(Key.RIGHT, lambda: logging.info(self.get_coods())) from fishy.engine.fullautofisher.calibrate import Calibrate - hotkey.set_hotkey(Key.UP, Calibrate(self).callibrate) - - hotkey.set_hotkey(Key.F9, lambda: print(self.look_for_hole())) - - hotkey.set_hotkey(Key.F10, self.update_crop) - - # hotkey.set_hotkey(Key.DOWN, self.set_target) - # hotkey.set_hotkey(Key.RIGHT, lambda: self.move_to(self.config.get("target", None))) - from fishy.engine.fullautofisher.recorder import Recorder from fishy.engine.fullautofisher.player import Player - hotkey.set_hotkey(Key.LEFT, Recorder(self).start_recording) - hotkey.set_hotkey(Key.DOWN, Player(self).start_route) - logging.info("STARTED") + + def change_state(): + c.change_state() + + def print_coods(): + logging.info(self.get_coods()) + + def set_target(): + t = self.get_coods()[:-1] + config.set("target", t) + print(f"target_coods are {t}") + + def move_to_target(): + self.move_to(config.get("target")) + + def rotate_to(): + self.rotate_to(90) + + controls = [ + { + Key.RIGHT: Player(self).start_route, + Key.UP: Calibrate(self).callibrate, + Key.LEFT: self.update_crop, + Key.DOWN: change_state + }, + { + Key.RIGHT: print_coods, + Key.UP: Recorder(self).start_recording, + Key.LEFT: helper.empty_function, + Key.DOWN: change_state + }, + { + Key.RIGHT: set_target, + Key.UP: rotate_to, + Key.LEFT: move_to_target, + Key.DOWN: change_state + } + ] + c = Controls(controls, 0) + c.change_state() + + +class Controls: + def __init__(self, controls, first=0): + self.current_menu = first - 1 + self.controls = controls + + def change_state(self): + self.current_menu += 1 + if self.current_menu == len(self.controls): + self.current_menu = 0 + + help_str = "CONTROLS" + for key, func in self.controls[self.current_menu].items(): + hotkey.set_hotkey(key, func) + help_str += f"\n{key.value}: {func.__name__}" + logging.info(help_str) if __name__ == '__main__': diff --git a/fishy/engine/fullautofisher/player.py b/fishy/engine/fullautofisher/player.py index 3cfbf4b..b737c42 100644 --- a/fishy/engine/fullautofisher/player.py +++ b/fishy/engine/fullautofisher/player.py @@ -1,9 +1,8 @@ import logging import pickle from pprint import pprint -from tkinter.filedialog import askopenfile -from fishy.engine.semifisher import fishing_event +from fishy.engine.semifisher import fishing_event, fishing_mode from fishy.engine.fullautofisher.engine import FullAuto @@ -24,13 +23,13 @@ class Player: self.timeline.append(("check_fish", coods)) def _start_moving(self): - self.start_moving_flag = True + self.start_moving_flag = not self.start_moving_flag def _stop_recording(self): self.recording = False def _hole_complete_callback(self, e): - if e == "idle": + if e == fishing_event.State.IDLE: self.hole_complete_flag = True def start_route(self): @@ -55,9 +54,12 @@ class Player: hotkey.set_hotkey(Key.F8, self._start_moving) helper.wait_until(lambda: self.start_moving_flag) - logging.info("starting") - for action in self.timeline: - fishing_event.unsubscribe() + logging.info("starting, press f8 to stop") + forward = True + i = 0 + while self.start_moving_flag: + action = self.timeline[i] + if action[0] == "move_to": self.engine.move_to(action[1]) logging.info("moved") @@ -65,11 +67,26 @@ class Player: self.engine.move_to(action[1]) self.engine.rotate_to(action[1][2]) fishing_event.subscribe() + fishing_mode.subscribers.append(self._hole_complete_callback) # scan for fish hole logging.info("scanning") if self.engine.look_for_hole(): + logging.info("starting fishing") self.hole_complete_flag = False - helper.wait_until(lambda: self.hole_complete_flag) + helper.wait_until(lambda: self.hole_complete_flag or not self.start_moving_flag) + else: + logging.info("no hole found") # if found start fishing and wait for hole to complete # contine when hole completes + fishing_event.unsubscribe() + fishing_mode.subscribers.remove(self._hole_complete_callback) + i += 1 if forward else -1 + if i >= len(self.timeline): + forward = False + i = len(self.timeline) - 1 + elif i < 0: + forward = True + i = 0 + + logging.info("stopped") diff --git a/fishy/engine/fullautofisher/recorder.py b/fishy/engine/fullautofisher/recorder.py index bbe1904..084892b 100644 --- a/fishy/engine/fullautofisher/recorder.py +++ b/fishy/engine/fullautofisher/recorder.py @@ -21,6 +21,7 @@ class Recorder: def _mark_hole(self): coods = self.engine.get_coods() self.timeline.append(("check_fish", coods)) + logging.info("check_fish") def _stop_recording(self): self.recording = False diff --git a/fishy/engine/semifisher/fishing_event.py b/fishy/engine/semifisher/fishing_event.py index daf97ac..e2910a9 100644 --- a/fishy/engine/semifisher/fishing_event.py +++ b/fishy/engine/semifisher/fishing_event.py @@ -10,7 +10,7 @@ from fishy.engine.semifisher import fishing_mode from playsound import playsound from fishy import web -from fishy.engine.semifisher.fishing_mode import State +from fishy.engine.semifisher.fishing_mode import State, FishingMode from fishy.helper import helper import keyboard @@ -48,6 +48,7 @@ def unsubscribe(): def subscribe(): if fisher_callback not in fishing_mode.subscribers: fishing_mode.subscribers.append(fisher_callback) + fisher_callback(FishingMode.CurrentMode) def fisher_callback(event: State): diff --git a/fishy/helper/hotkey.py b/fishy/helper/hotkey.py index bd1af66..da24f49 100644 --- a/fishy/helper/hotkey.py +++ b/fishy/helper/hotkey.py @@ -8,11 +8,11 @@ from fishy.helper import helper class Key(Enum): - F9 = "f9", + F9 = "f9" F10 = "f10" F8 = "f8" F7 = "f7" - UP = "up", + UP = "up" DOWN = "down" LEFT = "left" RIGHT = "right"