-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoebot.py
More file actions
380 lines (324 loc) · 12.8 KB
/
poebot.py
File metadata and controls
380 lines (324 loc) · 12.8 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import cv2 as cv
import pyautogui
from time import time, sleep
from utils import Utils
from threading import Thread, Lock
from math import sqrt
class PoeBotState:
INITIALIZING = 1
SEARCHING = 2
PORTING_BACK = 3
FINDING_WAYPOINT = 4
NEXT_DESTINATION = 5
RESET_NODE_AND_GO = 6
class PoeBot:
#constants
INITIALIZING_SECONDS = 3
ATTEMPT_UNSTUCK_COUNT = 1
PORT_BACK_COUNT = 1
INVALID_LOOT_COUNT = 1
INVALID_MAP_LOOT_COUNT = 1
#threading properties
stopped = True
lock = None
#properties
state = None
map_target_history = []
map_history_counter = 0
map_invalid_target_history = []
map_unstuck_attempts_count = 0
loot_history = []
invalid_loot_history = []
map_loot_history = []
map_invalid_loot_history = []
invalid_loot_count = 0
map_invalid_loot_count = 0
invalid_loot_unstuck_count = 0
invalid_map_loot_unstuck_count = 0
center = (0, 0)
port_back = False
screenshot = None
timestamp = None
movement_screenshot = None
window_offset = (0, 0)
window_w = 0
window_h = 0
#threading updates
loot_targets = []
map_targets = []
map_loot_targets = []
waypoint_target = []
waypoint_tooltip_target = []
act_target = []
node_target = []
reset_tooltip_target = []
def __init__(self, window_offset, window_size):
#create thread lock
self.lock = Lock()
self.window_offset = window_offset
self.window_w = window_size[0]
self.window_h = window_size[1]
int_w = int(self.window_w / 2)
int_h = int(self.window_h / 2)
self.center = (int_w, int_h)
self.state = PoeBotState.INITIALIZING
self.timestamp = time()
def map_move_to_target(self):
target = self.map_find_next_target()
pyautogui.moveTo(x = target[0], y = target[1])
pyautogui.mouseDown()
sleep(2)
pyautogui.mouseUp()
return True
def map_find_next_target(self):
next_target = Utils.getClosestPixelToCenter(self.map_targets, self.center)
if self.map_move_is_stuck(next_target):
self.attempt_unstuck(next_target)
next_target = Utils.getClosestPixelToCenter(self.map_targets, self.center)
if self.should_port_back():
self.port_back = True
self.map_unstuck_attempts_count = 0
coord = self.get_screen_position((next_target[0], next_target[1]))
self.map_target_history.append([next_target[0], next_target[1]])
return coord
def should_port_back(self):
return self.map_unstuck_attempts_count > PoeBot.PORT_BACK_COUNT
def map_move_is_stuck(self, next_target):
if any(self.map_target_history):
attempted_same_target = not any(x != next_target for x in self.map_target_history[-1:])
if attempted_same_target:
self.map_history_counter += 1
if self.map_history_counter > PoeBot.ATTEMPT_UNSTUCK_COUNT:
self.map_target_history = []
self.map_history_counter = 0
self.map_unstuck_attempts_count += 1
return True
return False
def attempt_unstuck(self, next_target):
self.map_invalid_target_history.append(next_target)
for x in self.map_invalid_target_history:
try:
self.map_targets.remove(x)
except ValueError:
pass
def pickup_loot(self):
next_target = Utils.getClosestPixelToCenter(self.loot_targets, self.center)
if self.pickup_loot_is_stuck(next_target):
self.pickup_loot_attempt_unstuck(next_target)
next_target = Utils.getClosestPixelToCenter(self.loot_targets, self.center)
if self.pickup_loot_should_port_back():
self.port_back = True
self.invalid_loot_unstuck_count = 0
coord = self.get_screen_position(next_target)
pyautogui.click(x = coord[0], y = coord[1])
sleep(2)
self.loot_history.append([next_target[0], next_target[1]])
def pickup_loot_should_port_back(self):
return self.invalid_loot_unstuck_count > PoeBot.PORT_BACK_COUNT
def pickup_loot_is_stuck(self, next_target):
if any(self.loot_history):
attempted_same_target = not any(x != next_target for x in self.loot_history[-3:])
if attempted_same_target:
self.invalid_loot_count += 1
if self.invalid_loot_count > PoeBot.INVALID_LOOT_COUNT:
self.loot_history = []
self.invalid_loot_count = 0
self.invalid_loot_unstuck_count += 1
return True
return False
def pickup_loot_attempt_unstuck(self, next_target):
self.invalid_loot_history.append(next_target)
for x in self.invalid_loot_history:
try:
self.loot_targets.remove(x)
except ValueError:
pass
def go_to_map_loot(self):
next_target = Utils.getClosestPixelToCenter(self.map_loot_targets, self.center)
if self.pickup_map_loot_is_stuck(next_target):
self.pickup_map_loot_attempt_unstuck(next_target)
next_target = Utils.getClosestPixelToCenter(self.map_loot_targets, self.center)
if self.pickup_map_loot_should_port_back():
self.port_back = True
self.invalid_map_loot_unstuck_count = 0
coord = self.get_screen_position(next_target)
pyautogui.moveTo(x = coord[0], y = coord[1])
pyautogui.mouseDown()
sleep(2)
pyautogui.mouseUp()
self.map_loot_history.append([next_target[0], next_target[1]])
def pickup_map_loot_should_port_back(self):
return self.invalid_map_loot_unstuck_count > PoeBot.PORT_BACK_COUNT
def pickup_map_loot_is_stuck(self, next_target):
if not any(self.map_loot_history):
attempted_same_target = not any(x != next_target for x in self.map_loot_history[-3:])
if attempted_same_target:
self.map_invalid_loot_count += 1
if self.map_invalid_loot_count > PoeBot.INVALID_LOOT_COUNT:
self.map_loot_history = []
self.map_invalid_loot_count = 0
self.invalid_map_loot_unstuck_count += 1
return True
return False
def pickup_map_loot_attempt_unstuck(self, next_target):
self.map_invalid_loot_history.append(next_target)
for x in self.map_invalid_loot_history:
try:
self.map_loot_targets.remove(x)
except ValueError:
pass
def get_screen_position(self, pos):
return (pos[0] + self.window_offset[0], pos[1] + self.window_offset[1])
def do_port_back(self):
window_center = self.get_screen_position((self.center[0], self.center[1]))
#adjust height for port location
window_center = (window_center[0], window_center[1] - 100)
pyautogui.click(x = window_center[0], y = window_center[1])
pyautogui.press('T')
sleep(4)
pyautogui.click(x = window_center[0], y = window_center[1], clicks = 2)
# sleep until the town loads
sleep(15)
#bring up the overlay map
pyautogui.press('tab')
def move_to_waypoint(self):
waypoint = self.get_screen_position(self.waypoint_target)
pyautogui.click(x = waypoint[0], y = waypoint[1])
def click_waypoint(self):
waypoint = self.get_screen_position(self.waypoint_tooltip_target)
adjusted_x = waypoint[0] + 15
adjusted_y = waypoint[1] + 5
pyautogui.moveTo(x = adjusted_x, y = adjusted_y)
sleep(2)
pyautogui.click(x = adjusted_x, y = adjusted_y)
def click_act(self):
act = self.get_screen_position(self.act_target)
adjusted_x = act[0] + 20
adjusted_y = act[1] + 10
pyautogui.moveTo(x = adjusted_x, y = adjusted_y)
sleep(2)
pyautogui.click(x = adjusted_x, y = adjusted_y)
def click_node(self):
node = self.get_screen_position(self.node_target)
adjusted_x = node[0] + 10
adjusted_y = node[1] + 10
pyautogui.moveTo(x = adjusted_x, y = adjusted_y)
sleep(2)
pyautogui.keyDown('ctrl')
pyautogui.click(x = adjusted_x, y = adjusted_y)
pyautogui.keyUp('ctrl')
def click_reset_tooltip_target(self):
reset_tooltip = self.get_screen_position(self.reset_tooltip_target)
adjusted_x = reset_tooltip[0] + 10
adjusted_y = reset_tooltip[1] + 10
pyautogui.moveTo(x = adjusted_x, y = adjusted_y)
sleep(2)
pyautogui.click(x = adjusted_x, y = adjusted_y)
sleep(10)
pyautogui.press('tab')
def update_map_targets(self, targets):
self.lock.acquire()
self.map_targets = targets
self.lock.release()
def update_loot_targets(self, targets):
self.lock.acquire()
self.loot_targets = targets
self.lock.release()
def update_map_loot_targets(self, targets):
self.lock.acquire()
self.map_loot_targets = targets
self.lock.release()
def update_waypoint_target(self, target):
self.lock.acquire()
self.waypoint_target = target
self.lock.release()
def update_waypoint_tooltip_target(self, target):
self.lock.acquire()
self.waypoint_tooltip_target = target
self.lock.release()
def update_act_target(self, target):
self.lock.acquire()
self.act_target = target
self.lock.release()
def update_node_target(self, target):
self.lock.acquire()
self.node_target = target
self.lock.release()
def update_reset_tooltip_target(self, target):
self.lock.acquire()
self.reset_tooltip_target = target
self.lock.release()
def start(self):
self.stopped = False
t = Thread(target=self.run)
t.start()
def stop(self):
self.lock.acquire()
self.stopped = True
self.lock.release()
def run(self):
while not self.stopped:
if self.state == PoeBotState.INITIALIZING:
print('INITIALIZING')
if self.start_time():
self.lock.acquire()
self.state = PoeBotState.SEARCHING
self.lock.release()
elif self.state == PoeBotState.SEARCHING:
if self.port_back:
self.lock.acquire()
self.state = PoeBotState.PORTING_BACK
self.lock.release()
elif any(self.loot_targets):
print('LOOTING')
self.pickup_loot()
elif any(self.map_loot_targets):
print('MOVING TO MAP LOOT')
self.go_to_map_loot()
elif any(self.map_targets):
print('MOVING')
self.map_move_to_target()
else:
pass
elif self.state == PoeBotState.PORTING_BACK:
print('PORT BACK')
self.do_port_back()
self.lock.acquire()
self.state = PoeBotState.FINDING_WAYPOINT
self.port_back = False
self.lock.release()
elif self.state == PoeBotState.FINDING_WAYPOINT:
print('FINDING WAYPOINT')
if any(self.waypoint_tooltip_target):
self.click_waypoint()
self.lock.acquire()
self.state = PoeBotState.NEXT_DESTINATION
self.lock.release()
elif any(self.waypoint_target):
self.move_to_waypoint()
else:
pass
elif self.state == PoeBotState.NEXT_DESTINATION:
print('SELECTING ACT & MAP')
if any(self.node_target):
self.click_node()
self.lock.acquire()
self.state = PoeBotState.RESET_NODE_AND_GO
self.lock.release()
elif any(self.act_target):
self.click_act()
else:
pass
elif self.state == PoeBotState.RESET_NODE_AND_GO:
print('RESETTING')
if any(self.reset_tooltip_target):
self.click_reset_tooltip_target()
self.lock.acquire()
self.state = PoeBotState.SEARCHING
self.lock.release()
else:
pass
else:
pass
def start_time(self):
return time() > self.timestamp + self.INITIALIZING_SECONDS