forked from Bandit-HaxUnit/SmartMouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove.py
More file actions
279 lines (244 loc) · 8.69 KB
/
Copy pathmove.py
File metadata and controls
279 lines (244 loc) · 8.69 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
import json
import math
import random
import time
import tkinter as tk
from pynput.mouse import Controller
MOUSE_DATA_FILE = "mousedata.json"
DISTANCE_THRESHOLDS = [12, 18, 26, 39, 58, 87, 130, 190, 260, 360, 500]
BASE_SPEEDS = {
'fast': (0.001, 0.003),
'medium': (0.003, 0.005),
'slow': (0.005, 0.007)
}
SPEED_VARIANCE = 0.3
NUM_POINTS = 10
DOT_SIZE = 6
# ------------------ EASING FUNCTIONS ------------------
ease_linear = lambda t: t
ease_out_quart = lambda t: 1 - (1 - t) ** 4
ease_in_out_quart = lambda t: 1 - 8 * (1 - t if t >= 0.5 else t) ** 4 if t >= 0.5 else 8 * t**4
ease_out_cubic = lambda t: 1 - (1 - t) ** 3
ease_in_cubic = lambda t: t ** 3
ease_in_out_cubic = lambda t: 1 + (t - 1) ** 3 if t >= 0.5 else 4 * t ** 3
def ease_out_elastic(t):
if t == 0 or t == 1:
return t
return 2 ** (-10 * t) * math.sin((t * 10 - 0.75) * (2 * math.pi / 3)) + 1
def ease_out_bounce(t):
n1, d1 = 7.5625, 2.75
if t < 1 / d1:
return n1 * t * t
elif t < 2 / d1:
return n1 * (t - 1.5 / d1) ** 2 + 0.75
elif t < 2.5 / d1:
return n1 * (t - 2.25 / d1) ** 2 + 0.9375
else:
return n1 * (t - 2.625 / d1) ** 2 + 0.984375
EASING_FUNCTIONS = {
'short': [
(ease_out_cubic, 0.4),
(ease_out_quart, 0.3),
(ease_in_out_cubic, 0.2),
(ease_linear, 0.1),
],
'medium': [
(ease_in_out_cubic, 0.3),
(ease_in_out_quart, 0.3),
(ease_out_cubic, 0.2),
(ease_out_elastic, 0.2),
],
'long': [
(ease_in_out_quart, 0.4),
(ease_out_elastic, 0.3),
(ease_out_bounce, 0.2),
(ease_in_out_cubic, 0.1),
]
}
# ------------------ ORIENTATION LOGIC (8 Directions) ------------------
def angle_to_8_direction(angle_deg):
"""
Convert angle (degrees in range -180..180 or 0..360) to one of:
N, NE, E, SE, S, SW, W, NW
"""
a = angle_deg % 360
# E = [337.5..360) + [0..22.5)
# NE = [22.5..67.5)
# N = [67.5..112.5)
# NW = [112.5..157.5)
# W = [157.5..202.5)
# SW = [202.5..247.5)
# S = [247.5..292.5)
# SE = [292.5..337.5)
if (a >= 337.5 and a < 360) or (a >= 0 and a < 22.5):
return "E"
elif a >= 22.5 and a < 67.5:
return "NE"
elif a >= 67.5 and a < 112.5:
return "N"
elif a >= 112.5 and a < 157.5:
return "NW"
elif a >= 157.5 and a < 202.5:
return "W"
elif a >= 202.5 and a < 247.5:
return "SW"
elif a >= 247.5 and a < 292.5:
return "S"
else:
return "SE"
class VisualizationOverlay:
def __init__(self):
self.root = tk.Tk()
self.root.attributes('-alpha', 0.5, '-topmost', True)
self.root.overrideredirect(True)
self.screen_width = self.root.winfo_screenwidth()
self.screen_height = self.root.winfo_screenheight()
self.root.geometry(f"{self.screen_width}x{self.screen_height}+0+0")
self.canvas = tk.Canvas(self.root, highlightthickness=0, bg='black')
self.canvas.pack(fill='both', expand=True)
self.root.bind('<Escape>', lambda e: self.root.destroy())
def draw_dot(self, x, y):
r = DOT_SIZE / 2
self.canvas.create_oval(x - r - 1, y - r - 1,
x + r + 1, y + r + 1,
fill='white', outline='')
self.canvas.create_oval(x - r, y - r,
x + r, y + r,
fill='red', outline='')
self.root.update()
def load_mousedata():
with open(MOUSE_DATA_FILE, "r") as f:
return json.load(f)
def get_base_speed(distance):
if distance <= 100:
return random.uniform(*BASE_SPEEDS['fast']) * 2
elif distance <= 250:
return random.uniform(*BASE_SPEEDS['medium']) * 2
else:
return random.uniform(*BASE_SPEEDS['slow']) * 2
def add_human_variance(base_speed):
return base_speed + random.uniform(-base_speed * SPEED_VARIANCE, base_speed * SPEED_VARIANCE)
def select_easing_function(distance):
cat = 'short' if distance <= 100 else 'medium' if distance <= 250 else 'long'
funcs, weights = zip(*EASING_FUNCTIONS[cat])
return random.choices(funcs, weights=weights)[0]
def get_sleep_duration(step, total_steps, distance, override_speed=None):
if total_steps <= 1:
return 0
t = step / (total_steps - 1)
factor = select_easing_function(distance)(t)
base_speed = add_human_variance(override_speed if override_speed else get_base_speed(distance))
return base_speed * (0.8 + 0.4 * factor)
def get_distance_category(distance):
for threshold in DISTANCE_THRESHOLDS:
if distance <= threshold:
return str(threshold)
return str(DISTANCE_THRESHOLDS[-1])
def pick_random_path(mousedata, distance, direction):
"""
mousedata structure now:
{
"12": {
"N": [ [ [dx...],[dy...] ], ... ],
"NE": [ ... ],
"E": [ ... ],
"SE": [ ... ],
"S": [ ... ],
"SW": [ ... ],
"W": [ ... ],
"NW": [ ... ]
},
"18": {...},
...
}
"""
dist_cat = get_distance_category(distance)
dist_dict = mousedata.get(dist_cat, {})
paths = dist_dict.get(direction, [])
if not paths:
return None
return random.choice(paths)
def build_exact_path(start_x, start_y, target_x, target_y, x_offsets, y_offsets):
steps = min(len(x_offsets), len(y_offsets))
if not steps:
return [(start_x, start_y), (target_x, target_y)]
dx = target_x - start_x
dy = target_y - start_y
total_offset_x = sum(x_offsets)
total_offset_y = sum(y_offsets)
adj_dx, adj_dy = dx - total_offset_x, dy - total_offset_y
path = []
for i in range(steps):
t = (i + 1) / steps
offset_x = sum(x_offsets[:i + 1])
offset_y = sum(y_offsets[:i + 1])
new_x = start_x + adj_dx * t + offset_x
new_y = start_y + adj_dy * t + offset_y
path.append((new_x, new_y))
return path
def generate_random_points(num_points, w, h):
pad = 600
return [(random.randint(pad, w - pad), random.randint(pad, h - pad)) for _ in range(num_points)]
def move_to_point(mouse, mousedata, start_x, start_y, target_x, target_y, overlay):
distance = math.hypot(target_x - start_x, target_y - start_y)
print(f"Moving to ({target_x}, {target_y}), distance = {distance:.1f}")
# Compute angle -> 8 direction
dx = target_x - start_x
dy = target_y - start_y
angle_deg = math.degrees(math.atan2(dy, dx))
direction = angle_to_8_direction(angle_deg)
print(f"Orientation determined: {direction}")
offsets_pair = pick_random_path(mousedata, distance, direction)
if not offsets_pair:
print(f"No path found for dist={distance:.1f}, direction={direction}. Jumping directly.")
mouse.position = (target_x, target_y)
overlay.draw_dot(target_x, target_y)
return
x_offsets, y_offsets = offsets_pair
path = build_exact_path(start_x, start_y, target_x, target_y, x_offsets, y_offsets)
for i, (px, py) in enumerate(path):
mouse.position = (px, py)
time.sleep(get_sleep_duration(i, len(path)*10, distance))
mouse.position = (target_x, target_y)
overlay.draw_dot(target_x, target_y)
def main():
try:
overlay = VisualizationOverlay()
mousedata = load_mousedata()
mouse = Controller()
# Example points or random generation
points = [
(765, 408),
(973, 479),
(882, 552),
(1086, 723),
(684, 391),
(636, 619),
(663, 720),
(925, 356),
(1348, 740),
(925, 356),
(1348, 740),
(925, 356),
(1348, 740),
(925, 356),
(1348, 740),
(925, 356),
(1348, 740),
(925, 356),
(1348, 740),
(925, 356),
]
# points = generate_random_points(NUM_POINTS, overlay.screen_width, overlay.screen_height)
current_x, current_y = mouse.position
for (tx, ty) in points:
move_to_point(mouse, mousedata, current_x, current_y, tx, ty, overlay)
current_x, current_y = tx, ty
time.sleep(random.uniform(0.2, 0.25))
print("Movement sequence complete. Press ESC to close visualization.")
overlay.root.mainloop()
except Exception as e:
print(f"Error: {e}")
raise
if __name__ == "__main__":
main()