-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfishingbot.py
More file actions
206 lines (172 loc) · 5.97 KB
/
Copy pathfishingbot.py
File metadata and controls
206 lines (172 loc) · 5.97 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import pyautogui
import time
import random
import sys
import threading
if sys.platform == "win32":
import ctypes
import ctypes.util
_user32 = ctypes.windll.user32
class _CURSORINFO(ctypes.Structure):
_fields_ = [
("cbSize", ctypes.c_uint),
("flags", ctypes.c_uint),
("hCursor", ctypes.c_void_p),
("ptScreenPos", ctypes.c_long * 2),
]
def get_cursor_info():
ci = _CURSORINFO()
ci.cbSize = ctypes.sizeof(_CURSORINFO)
_user32.GetCursorInfo(ctypes.byref(ci))
return ci.hCursor
else:
import ctypes
import ctypes.util
import hashlib
class _XFixesCursorImage(ctypes.Structure):
_fields_ = [
("x", ctypes.c_short),
("y", ctypes.c_short),
("width", ctypes.c_ushort),
("height", ctypes.c_ushort),
("xhot", ctypes.c_ushort),
("yhot", ctypes.c_ushort),
("cursor_serial", ctypes.c_ulong),
("pixels", ctypes.POINTER(ctypes.c_ulong)),
("atom", ctypes.c_ulong),
("name", ctypes.c_char_p),
]
_libX11 = ctypes.CDLL(ctypes.util.find_library("X11"))
_libX11.XOpenDisplay.restype = ctypes.c_void_p
_libX11.XFree.argtypes = [ctypes.c_void_p]
_libXfixes = ctypes.CDLL(ctypes.util.find_library("Xfixes"))
_libXfixes.XFixesGetCursorImage.restype = ctypes.POINTER(_XFixesCursorImage)
_dpy = _libX11.XOpenDisplay(None)
def get_cursor_info():
cursor_ptr = _libXfixes.XFixesGetCursorImage(_dpy)
c = cursor_ptr.contents
n = c.width * c.height
pixel_array = (ctypes.c_ulong * n).from_address(
ctypes.addressof(c.pixels.contents)
)
digest = hashlib.md5(bytes(pixel_array)).digest()
_libX11.XFree(cursor_ptr)
return digest
class FishingBot:
def __init__(
self,
bait_image="./bait.png",
fishing_button="b",
mouse_offset_px=35,
edge_reset=(10, 10),
start_delay=2,
confidence=0.7,
on_status=None,
on_fish_caught=None,
):
self.bait_image = bait_image
self.fishing_button = fishing_button
self.mouse_offset_px = mouse_offset_px
self.edge_reset = edge_reset
self.start_delay = start_delay
self.confidence = confidence
self.on_status = on_status or (lambda msg: None)
self.on_fish_caught = on_fish_caught or (lambda count: None)
self._stop_event = threading.Event()
self._thread = None
self._fish_count = 0
@property
def running(self):
return self._thread is not None and self._thread.is_alive()
def start(self):
if self.running:
return
self._stop_event.clear()
self._fish_count = 0
self._thread = threading.Thread(target=self._run, daemon=True)
self._thread.start()
def stop(self):
self._stop_event.set()
def _stopped(self):
return self._stop_event.is_set()
def _sleep(self, seconds):
"""Interruptible sleep. Returns True if interrupted."""
return self._stop_event.wait(timeout=seconds)
def _find_on_screen(self):
while not self._stopped():
try:
loc = pyautogui.locateOnScreen(
self.bait_image, confidence=self.confidence
)
cp_loc = pyautogui.center(loc)
return cp_loc
except Exception:
if self._sleep(0.25):
return None
return None
def _run(self):
self.on_status("Starting in {} seconds...".format(self.start_delay))
if self._sleep(self.start_delay):
self.on_status("Stopped.")
return
self.on_status("Bot running.")
while not self._stopped():
delay = 0.25 + random.uniform(0, 1.5)
if self._sleep(delay):
break
# 20% chance to press "space"
chance = random.randint(0, 100)
if chance <= 20:
pyautogui.press("space")
if self._sleep(2):
break
pyautogui.write(self.fishing_button)
self.on_status("Cast line, searching for bait...")
# Reset mouse position
pyautogui.moveTo(*self.edge_reset)
bait = self._find_on_screen()
if bait is None:
break
pyautogui.moveTo(
bait.x,
bait.y + self.mouse_offset_px + random.randint(1, 4),
)
self.on_status("Watching bobber...")
cursor_info = get_cursor_info()
catch_start = time.time()
caught = False
while not self._stopped():
curr_cursor_info = get_cursor_info()
if curr_cursor_info != cursor_info:
if self._sleep(0.25 + random.uniform(0, 0.15)):
break
pyautogui.click(bait.x, bait.y)
self._fish_count += 1
self.on_status("Fish caught! (#{})".format(self._fish_count))
self.on_fish_caught(self._fish_count)
caught = True
break
if time.time() - catch_start > 22:
self.on_status("Bobber timed out, recasting...")
break
# Avoid tight CPU-burning loop
if self._sleep(0.05):
break
self.on_status("Stopped.")
if __name__ == "__main__":
bot = FishingBot(
bait_image="./bait.png",
fishing_button="b",
mouse_offset_px=35,
edge_reset=(10, 10),
start_delay=2,
confidence=0.7,
on_status=lambda msg: print("[Bot]", msg),
on_fish_caught=lambda count: print("[Fish]", count),
)
bot.start()
try:
while bot.running:
time.sleep(0.5)
except KeyboardInterrupt:
bot.stop()