-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseMovement.py
82 lines (63 loc) · 2.66 KB
/
MouseMovement.py
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
# A python 3 version of SRL-6's _humanWindMouse from /lib/core/mouse.simba with modifications.
# Written by GitHub user Bahm
# https://gist.github.com/Bahm/8408ebf5d6aa83d3de8bb09655236888
import math
from pymouse import PyMouse
from random import randint
from time import sleep
class MouseMovementCalculator:
def __init__(self, gravity, wind, mouseSpeed, targetError):
self.gravity = gravity
self.wind = wind
self.mouseSpeed = mouseSpeed
self.targetError = targetError
def calcCoordsAndDelay(self, startCoords, endCoords):
veloX, veloY = (0, 0)
coordsAndDelay = []
xs, ys = startCoords
xe, ye = endCoords
totalDist = math.hypot(xs - xe, ys - ye)
self._windX = 0
self._windY = 0
while True:
veloX, veloY = self._calcVelocity((xs, ys), (xe, ye), veloX, veloY, totalDist)
xs += veloX
ys += veloY
w = round(max(randint(0, max(0, round(100/self.mouseSpeed)-1))*6, 5)*0.9)
coordsAndDelay.append(( xs, ys, w ))
if math.hypot(xs - xe, ys - ye) < 1:
break
if round(xe) != round(xs) or round(ye) != round(ys):
coordsAndDelay.append(( round(xe), round(ye), 0 ))
return coordsAndDelay
def _calcVelocity(self, curCoords, endCoords, veloX, veloY, totalDist):
xs, ys = curCoords
xe, ye = endCoords
dist = math.hypot(xs - xe, ys - ye)
self.wind = max(min(self.wind, dist), 1)
maxStep = None
D = max(min(round(round(totalDist)*0.3)/7, 25), 5)
rCnc = randint(0, 5)
if rCnc == 1:
D = 2
if D <= round(dist):
maxStep = D
else:
maxStep = round(dist)
if dist >= self.targetError:
self._windX = self._windX / math.sqrt(3) + (randint(0, round(self.wind) * 2) - self.wind) / math.sqrt(5)
self._windY = self._windY / math.sqrt(3) + (randint(0, round(self.wind) * 2) - self.wind) / math.sqrt(5)
else:
self._windX = self._windX / math.sqrt(2)
self._windY = self._windY / math.sqrt(2)
veloX = veloX + self._windX
veloY = veloY + self._windY
if(dist != 0):
veloX = veloX + self.gravity * (xe - xs) / dist
veloY = veloY + self.gravity * (ye - ys) / dist
if math.hypot(veloX, veloY) > maxStep:
randomDist = maxStep / 2.0 + randint(0, math.floor(round(maxStep) / 2))
veloMag = math.sqrt(veloX * veloX + veloY * veloY)
veloX = (veloX / veloMag) * randomDist
veloY = (veloY / veloMag) * randomDist
return (veloX, veloY)