-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghost_core.py
More file actions
295 lines (236 loc) · 10.7 KB
/
Copy pathghost_core.py
File metadata and controls
295 lines (236 loc) · 10.7 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
import time
import random
import sys
import math
from evdev import UInput, ecodes as e
class HighResSleeper:
def __init__(self, spin_cap_sec=0.00025, drift_sec=0.00002):
self.spin_cap = float(spin_cap_sec)
self.drift = float(drift_sec)
def sleep_until(self, target_time):
clock_drift = random.uniform(-self.drift, self.drift)
target = target_time + clock_drift
while True:
now = time.perf_counter()
rem = target - now
if rem <= 0:
break
if rem > self.spin_cap:
time.sleep(rem - self.spin_cap)
class ClickerChannel:
def __init__(self, default_btn):
self.active = False
self.target_btn = default_btn
self.next_tick = 0.0
self.state = "cruising"
self.state_end_time = 0.0
self.current_variance = 0.0
self.cps = 12.0
self.jitter_enabled = False
self.jitter_strength = 2.0
self.human_lvl = 1
def reset(self):
self.next_tick = time.perf_counter()
self.state = "cruising"
self.state_end_time = time.perf_counter()
self.current_variance = 0.0
def get_next_delay(self):
now = time.perf_counter()
if now >= self.state_end_time:
rand = random.random()
if rand < 0.70:
self.state = "cruising"
self.state_end_time = now + random.uniform(0.4, 1.2)
self.current_variance = random.uniform(-1.5, 1.5)
elif rand < 0.85:
self.state = "burst"
self.state_end_time = now + random.uniform(0.2, 0.4)
self.current_variance = random.uniform(4.0, 7.0)
else:
self.state = "tired"
self.state_end_time = now + random.uniform(0.3, 0.6)
self.current_variance = random.uniform(-6.0, -3.0)
target_cps = self.cps + self.current_variance
roughness = random.gauss(0, 1.5)
final_cps = target_cps + roughness
final_cps = max(6.0, min(22.0, final_cps))
return 1.0 / final_cps
class GhostEngine:
def __init__(self):
keyboard_keys = list(e.keys.keys())
mouse_buttons = [e.BTN_LEFT, e.BTN_RIGHT, e.BTN_MIDDLE, e.BTN_SIDE, e.BTN_EXTRA]
try:
self.ui = UInput(
{
e.EV_KEY: keyboard_keys + mouse_buttons,
e.EV_REL: [e.REL_X, e.REL_Y, e.REL_WHEEL],
},
name="Moonlight HID",
vendor=0x1234,
product=0x5678,
version=0x1
)
except PermissionError:
print("ROOT REQUIRED: run with sudo")
sys.exit(1)
self.left = ClickerChannel(e.BTN_LEFT)
self.left.jitter_enabled = True
self.right = ClickerChannel(e.BTN_RIGHT)
self.mode = "mouse"
self.paused = False
self.drift_x = 0.0
self.drift_y = 0.0
self.wtap_enabled = False
self.wtap_chance = 0.0
self.blockhit_enabled = False
self.blockhit_chance = 0.0
self.holding_s = False
self.holding_rmb = False
def cleanup(self):
try:
self.ui.write(e.EV_KEY, e.BTN_LEFT, 0)
self.ui.write(e.EV_KEY, e.BTN_RIGHT, 0)
self.ui.write(e.EV_KEY, e.KEY_W, 0)
self.ui.write(e.EV_KEY, e.KEY_S, 0)
if self.left.target_btn:
self.ui.write(e.EV_KEY, self.left.target_btn, 0)
if self.right.target_btn:
self.ui.write(e.EV_KEY, self.right.target_btn, 0)
self.ui.syn()
self.ui.close()
except:
pass
def apply_jitter(self, strength, human_lvl):
if strength <= 0: return
if random.random() < 0.50:
multiplier = 1.0 if human_lvl == 1 else 2.2
intensity = strength * multiplier
dx = random.gauss(0, intensity)
dy = random.gauss(0, intensity)
self.drift_x += random.uniform(-0.2, 0.2)
self.drift_y += random.uniform(-0.2, 0.2)
self.drift_x = max(-2.5, min(2.5, self.drift_x))
self.drift_y = max(-2.5, min(2.5, self.drift_y))
final_x = int(dx + self.drift_x)
final_y = int(dy + self.drift_y)
if final_x != 0 or final_y != 0:
self.ui.write(e.EV_REL, e.REL_X, final_x)
self.ui.write(e.EV_REL, e.REL_Y, final_y)
def run(self, state_q, config_q):
sleeper = HighResSleeper()
try:
while True:
while not config_q.empty():
cfg = config_q.get()
if 'mode' in cfg:
self.mode = cfg['mode']
if self.mode == 'mouse':
self.left.target_btn = e.BTN_LEFT
self.right.target_btn = e.BTN_RIGHT
if 'cps_left' in cfg: self.left.cps = float(cfg['cps_left'])
if 'cps_right' in cfg: self.right.cps = float(cfg['cps_right'])
if 'jitter' in cfg: self.left.jitter_strength = float(cfg['jitter'])
if 'rand' in cfg:
self.left.human_lvl = int(cfg['rand'])
self.right.human_lvl = int(cfg['rand'])
if 'target_btn' in cfg:
if self.mode == 'keyboard':
val = int(cfg['target_btn'])
self.left.target_btn = None if val == -1 else val
if 'assist_wtap' in cfg: self.wtap_enabled = bool(cfg['assist_wtap'])
if 'assist_wtap_chance' in cfg: self.wtap_chance = float(cfg['assist_wtap_chance']) / 100.0
if 'assist_blockhit' in cfg: self.blockhit_enabled = bool(cfg['assist_blockhit'])
if 'assist_blockhit_chance' in cfg: self.blockhit_chance = float(cfg['assist_blockhit_chance']) / 100.0
while not state_q.empty():
msg = state_q.get()
if msg == "STOP":
return
if msg == "PAUSE": self.paused = True
if msg == "RESUME": self.paused = False
if msg == "ENABLE_LEFT":
if not self.left.active:
self.left.active = True
self.left.reset()
self.drift_x = 0.0
self.drift_y = 0.0
elif msg == "DISABLE_LEFT":
self.left.active = False
if self.holding_s:
self.ui.write(e.EV_KEY, e.KEY_S, 0)
self.ui.syn()
self.holding_s = False
if self.holding_rmb:
self.ui.write(e.EV_KEY, e.BTN_RIGHT, 0)
self.ui.syn()
self.holding_rmb = False
if msg == "ENABLE_RIGHT":
if not self.right.active:
self.right.active = True
self.right.reset()
elif msg == "DISABLE_RIGHT":
self.right.active = False
if self.paused:
time.sleep(0.05)
continue
now = time.perf_counter()
if not self.left.active and not self.right.active:
time.sleep(0.01)
continue
processed_something = False
if self.left.active and now >= self.left.next_tick:
if self.mode == 'mouse' and self.left.jitter_enabled:
self.apply_jitter(self.left.jitter_strength, self.left.human_lvl)
do_blockhit = (self.mode == 'mouse' and self.blockhit_enabled and random.random() < self.blockhit_chance)
do_wtap = (self.mode == 'mouse' and self.wtap_enabled and random.random() < self.wtap_chance)
if self.left.target_btn is not None:
self.ui.write(e.EV_KEY, self.left.target_btn, 1)
self.ui.syn()
if do_blockhit:
self.holding_rmb = True
self.ui.write(e.EV_KEY, e.BTN_RIGHT, 1)
self.ui.syn()
if do_wtap:
self.holding_s = True
self.ui.write(e.EV_KEY, e.KEY_S, 1)
self.ui.syn()
hold = max(0.022, min(0.15, random.lognormvariate(-3.2, 0.25)))
full_delay = self.left.get_next_delay()
self.left.next_tick = now + full_delay
sleeper.sleep_until(now + hold)
if self.left.target_btn is not None:
self.ui.write(e.EV_KEY, self.left.target_btn, 0)
self.ui.syn()
if do_blockhit:
time.sleep(random.uniform(0.01, 0.03))
self.ui.write(e.EV_KEY, e.BTN_RIGHT, 0)
self.ui.syn()
self.holding_rmb = False
if do_wtap:
time.sleep(random.uniform(0.01, 0.02))
self.ui.write(e.EV_KEY, e.KEY_S, 0)
self.ui.syn()
self.holding_s = False
processed_something = True
now = time.perf_counter()
if self.mode == 'mouse' and self.right.active and now >= self.right.next_tick:
self.ui.write(e.EV_KEY, self.right.target_btn, 1)
self.ui.syn()
hold = max(0.022, min(0.15, random.lognormvariate(-3.2, 0.25)))
full_delay = self.right.get_next_delay()
self.right.next_tick = now + full_delay
sleeper.sleep_until(now + hold)
self.ui.write(e.EV_KEY, self.right.target_btn, 0)
self.ui.syn()
processed_something = True
if not processed_something:
next_event = 9999999999.0
if self.left.active: next_event = min(next_event, self.left.next_tick)
if self.mode == 'mouse' and self.right.active: next_event = min(next_event, self.right.next_tick)
rem = next_event - time.perf_counter()
if rem > 0.001: time.sleep(min(rem, 0.05))
except KeyboardInterrupt:
pass
except Exception as err:
print(f"Engine Error: {err}")
finally:
self.cleanup()