├── .gitignore ├── requirements.txt ├── rods ├── __init__.py ├── golden_fishing_rod.py └── sitting_duck_fishing_pole.py ├── config.ini ├── utils.py ├── main.py ├── README.md ├── LICENSE ├── configurator.py └── bot.py /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | mss 2 | cv2 3 | numpy 4 | pyautogui 5 | pytesseract 6 | fuzzywuzzy -------------------------------------------------------------------------------- /rods/__init__.py: -------------------------------------------------------------------------------- 1 | from . import sitting_duck_fishing_pole 2 | from . import golden_fishing_rod -------------------------------------------------------------------------------- /config.ini: -------------------------------------------------------------------------------- 1 | [bot] 2 | start_after=15 3 | last_catch_interval=2 4 | box_width=10 5 | box_height=10 -------------------------------------------------------------------------------- /rods/golden_fishing_rod.py: -------------------------------------------------------------------------------- 1 | def getMask(hsv, np, cv2): 2 | # yellow mask 3 | yellow_lower = np.array([20, 100, 100]) 4 | yellow_upper = np.array([30, 255, 255]) 5 | mask = cv2.inRange(hsv, yellow_lower, yellow_upper) 6 | 7 | return mask -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | from ctypes import windll, Structure, c_long, byref 2 | 3 | class POINT(Structure): 4 | _fields_ = [("x", c_long), ("y", c_long)] 5 | 6 | def queryMousePosition(): 7 | pt = POINT() 8 | windll.user32.GetCursorPos(byref(pt)) 9 | return { "x": pt.x, "y": pt.y} -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from configurator import config, check_config_file 3 | from bot import FishingBot 4 | 5 | # Logging 6 | logging.basicConfig(level=logging.INFO) 7 | 8 | # Config 9 | if not check_config_file("config.ini"): 10 | exit("Config file parse error. Exiting.") 11 | 12 | # Initialize bot 13 | bot = FishingBot(config) 14 | 15 | if __name__ == "__main__": 16 | bot.start() -------------------------------------------------------------------------------- /rods/sitting_duck_fishing_pole.py: -------------------------------------------------------------------------------- 1 | def getMask(hsv, np, cv2): 2 | # lower mask (0-10) 3 | lower_red = np.array([0,50,50]) 4 | upper_red = np.array([10,255,255]) 5 | mask0 = cv2.inRange(hsv, lower_red, upper_red) 6 | 7 | # upper mask (170-180) 8 | lower_red = np.array([170,50,50]) 9 | upper_red = np.array([180,255,255]) 10 | mask1 = cv2.inRange(hsv, lower_red, upper_red) 11 | 12 | # join masks 13 | mask = mask0+mask1 14 | 15 | return mask -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🎣 Terraria Auto-Fishing bot 2 | With Google Tesseract OCR recognition for catch items filtering via Sonar potion. 3 | Made as an experiment. 4 | 5 | *The code has NOT been polished and is provided "as is". There are a lot of code that are redundant and there are tons of improvements that can be made.* 6 | 7 | # Screenshot 8 | ![Terraria Auto-Fishing bot](https://i.imgur.com/a0s7mtI.jpg) 9 | 10 | ## Change history 11 | - Release 12 | - Autofishing implementation with MSS & PyAutoGui 13 | 14 | - Update 1 15 | - Google Tesseract OCR was added 16 | - Minor code improvements 17 | 18 | ## Author 19 | 20 | (C) 2021 Abraham Tugalov. 21 | http://howdyho.net -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Abraham Tugalov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /configurator.py: -------------------------------------------------------------------------------- 1 | from configparser import ConfigParser, SectionProxy 2 | 3 | class Config: 4 | pass 5 | 6 | class ConfigSection: 7 | def __init__(self, section: SectionProxy, items_type: str): 8 | for key in section: 9 | if items_type == "string": 10 | self.__setattr__(key, section[key]) 11 | elif items_type == "int": 12 | try: 13 | self.__setattr__(key, section.getint(key)) 14 | except ValueError: 15 | print("Could not convert int option", key) 16 | raise 17 | 18 | # Global config 19 | config = Config() 20 | 21 | def check_config_file(filename: str) -> bool: 22 | required_structure = { 23 | "bot": ["start_after", "last_catch_interval", "box_width", "box_height"], 24 | } 25 | 26 | global config 27 | parser = ConfigParser() 28 | parser.read(filename) 29 | 30 | # Check required sections and options in them 31 | if len(parser.sections()) == 0: 32 | print("Config file missing or empty") 33 | return False 34 | for section in required_structure.keys(): 35 | if section not in parser.sections(): 36 | print(f'Missing section "{section}" in config file') 37 | return False 38 | for option in required_structure[section]: 39 | if option not in parser[section]: 40 | print(f'Missing option "{option}" in section "{section}" of config file') 41 | return False 42 | 43 | # Read again, now create Config 44 | objects_type = {"bot": "int"} 45 | for section in parser.sections(): 46 | try: 47 | config.__setattr__(section, ConfigSection(parser[section], items_type=objects_type[section])) 48 | except ValueError: 49 | return False 50 | 51 | return True -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import time 2 | import cv2 3 | import mss 4 | import numpy as np 5 | import pyautogui 6 | import pytesseract 7 | 8 | from fuzzywuzzy import fuzz 9 | 10 | from utils import queryMousePosition 11 | import rods 12 | 13 | class FishingBot(): 14 | 15 | title = "Terraria Auto-Fishing Bot" 16 | sct = None 17 | config = None 18 | rod = "Golden Fishing Rod" 19 | 20 | ocr = { 21 | "enabled": True, 22 | "exclude": False, 23 | "list": [] 24 | } 25 | 26 | active = False 27 | last_catch_time = 0 # catch interval (before new rod will be dropped) 28 | last_sonar_time = 0 # read sonar time (before it fades) 29 | 30 | def __init__(self, config): 31 | self.config = config 32 | self.sct = mss.mss() 33 | 34 | def click(self): 35 | pyautogui.mouseDown() 36 | time.sleep(0.01) 37 | pyautogui.mouseUp() 38 | 39 | def start(self): 40 | print("Starting bot after",self.config.bot.start_after,"seconds.") 41 | print("Selected rod:",self.rod) 42 | print("Please, adjust your rod!") 43 | 44 | if not self.ocr["enabled"]: 45 | time.sleep(self.config.bot.start_after) 46 | 47 | self.click() 48 | print("Rod dropped ...") 49 | self.last_catch_time = time.time() 50 | else: 51 | time.sleep(self.config.bot.start_after / 2) 52 | 53 | self.active = True 54 | self.wait() 55 | 56 | def stop(self): 57 | self.active = False 58 | 59 | def wait(self): 60 | if self.ocr["enabled"]: 61 | # OCR way 62 | while self.active: 63 | # minimum catch interval 64 | if time.time() - self.last_catch_time < self.config.bot.last_catch_interval: 65 | continue 66 | 67 | # create box shot of sonar label 68 | cur = queryMousePosition() 69 | mon = { 70 | "left": cur['x'] - 200, 71 | "top": cur['y'] - 75, 72 | "width": 400, 73 | "height": 50 74 | } 75 | img = np.asarray(self.sct.grab(mon)) 76 | 77 | # create RGB for tesseract 78 | rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 79 | 80 | # read psm 6 & 7 81 | pcm6 = pytesseract.image_to_string(rgb, lang='rus', config='--psm 6') 82 | pcm7 = pytesseract.image_to_string(rgb, lang='rus', config='--psm 7') 83 | 84 | # ТЕСТ 85 | # проверяем что это Эбонкои или нет 86 | # ЛОВИМ ТОЛЬКО КОИ! 87 | 88 | # self.show(self.title, img) 89 | 90 | if(((fuzz.ratio(pcm6.lower(), 'ящик')) > 50 or (fuzz.ratio(pcm7.lower(), 'ящик')) > 50) 91 | or ('ящик' in pcm6.lower() or 'ящик' in pcm7.lower() or 'яшик' in pcm6.lower() or 'яшик' in pcm7.lower())): 92 | print("Это Ящик!") 93 | self.catch(True) # catch ASAP 94 | else: 95 | print("Неть ...") 96 | else: 97 | # Catch all way 98 | while self.active: 99 | 100 | # minimum catch interval 101 | if time.time() - self.last_catch_time < self.config.bot.last_catch_interval: 102 | continue 103 | 104 | # create box shot around mouse 105 | cur = queryMousePosition() 106 | mon = { 107 | "left": cur['x'] - int((self.config.bot.box_height/2)), 108 | "top": cur['y'] - int((self.config.bot.box_width/2)), 109 | "width": self.config.bot.box_width, 110 | "height": self.config.bot.box_height 111 | } 112 | img = np.asarray(self.sct.grab(mon)) 113 | 114 | # create HSV 115 | hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 116 | 117 | # get mask by rod 118 | mask = rods.golden_fishing_rod.getMask(hsv, np, cv2) 119 | 120 | # check 121 | hasFish = np.sum(mask) 122 | 123 | if hasFish > 0: 124 | # no fish 125 | pass 126 | else: 127 | self.catch() 128 | 129 | def catch(self, asap = False): 130 | print("Catch! ...") 131 | 132 | if not asap: 133 | time.sleep(0.3) 134 | 135 | self.click() 136 | 137 | time.sleep(1) # wait some 138 | print("New rod dropped ...") 139 | self.click() 140 | 141 | # reset last catch time 142 | self.last_catch_time = time.time() 143 | 144 | def show(self, title, img): 145 | cv2.imshow(title, img) 146 | if cv2.waitKey(25) & 0xFF == ord("q"): 147 | cv2.destroyAllWindows() 148 | quit() --------------------------------------------------------------------------------