I'm trying to translate a Pascal mouse movement algorithm to C++ but I'm not too good at it. The mouse moves to a position but when it moves again it only goes a small increment and crashes after a few more moves or if I move the mouse a few times and interfere with it. It's supposed to mimic a human using the mouse.
Here is the page where the algorithm is and my code so far:
Pascal code:
http://villavu.com/repositories/srl/SRL/core/Mouse.scar
My code(functions I wanted):
#include "stdafx.h"
double random(double x)
{
srand((unsigned)time(0));
return (double)((rand()%(int)x));
}
void WindMouse(double xs,double ys,double xe,double ye,double gravity,double wind,double minSleep,double maxSleep,double maxStep,double targetArea)
{
double veloX=0, veloY=0, windX=0, windY=0, veloMag, dist, randomDist, lastDist, step;
int lastX, lastY;
double sqrt2, sqrt3, sqrt5;
sqrt2= sqrt(2.0);
sqrt3= sqrt(3.0);
sqrt5= sqrt(5.0);
while( _hypot(xs - xe,ys - ye) > 1)
{
dist= _hypot(xs - xe,ys - ye);
wind= min(wind, dist);
if(dist >= targetArea )
{
windX = windX / sqrt3 + (random(floor(wind) * 2.0 + 1.0) - wind) / sqrt5;
windY = windY / sqrt3 + (random(floor(wind) * 2.0 + 1.0) - wind) / sqrt5;
} else
{
windX= windX / sqrt2;
windY= windY / sqrt2;
if((maxStep < 3) )
{
maxStep= random(3) + 3.0;
} else
{
maxStep= maxStep / sqrt5;
}
}
veloX= veloX + windX;
veloY= veloY + windY;
veloX= veloX + gravity * (xe - xs) / dist;
veloY= veloY + gravity * (ye - ys) / dist;
if(_hypot(veloX, veloY) > maxStep )
{
randomDist = maxStep / 2.0 + random(floor(maxStep) / 2);
veloMag= sqrt(veloX * veloX + veloY * veloY);
veloX= (veloX / veloMag) * randomDist;
veloY= (veloY / veloMag) * randomDist;
}
lastX= (int)floor(xs);
lastY= (int)floor(ys);
xs= xs + veloX;
ys= ys + veloY;
if((lastX != floor(xs)) || (lastY != floor(ys)) )
SetCursorPos((int)floor(xs), (int)floor(ys));
step= _hypot(xs - lastX, ys - lastY);
Sleep((int)floor((maxSleep - minSleep) * (step / maxStep) + minSleep));
lastDist= dist;
}
if((floor(xe) != floor(xs)) || (floor(ye) != floor(ys)) )
SetCursorPos((int)floor(xe), (int)floor(ye));
}
void MMouse(int x,int y,int rx,int ry)
{
int cx, cy;
double randSpeed;
POINT *p = new POINT;
GetCursorPos(p);
cx=p->x;
cy=p->y;
randSpeed= (random(10.0) / 2.0 + 10.0) / 10.0;
if(randSpeed == 0.0 )
randSpeed = 0.1;
x = x + (int)random(rx);
x = y + (int)random(ry);
WindMouse((double)cx,(double)cy,(double)x,(double)y,9.0,3.0,10.0/randSpeed,15.0/randSpeed,10.0*randSpeed,10.0*randSpeed);
delete p;
}
void Mouse(int mousex,int mousey,int ranx,int rany)
{
int a=0, b, c;
POINT *p1 = new POINT;
MMouse(mousex, mousey, ranx, rany);
Sleep((int)(60 + random(30)));
GetCursorPos(p1);
b=p1->x;
c=p1->y;
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
do{
Sleep((int)(20 + random(30)));
a = a + 1;
}while( (a > 4));
GetCursorPos(p1);
b=p1->x;
c=p1->y;
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep((int)(100 + random(100)));
delete p1;
}
void SleepAndSetCursorPos(int Time)
{
bool Moving;
double x, y, xv=0, yv=0;
double gx, gy;
int T;
POINT *p2= new POINT;
GetCursorPos(p2);
x=(double)p2->x;
y=(double)p2->y;
if((random(2) == 0) )
Moving = 0;
else
Moving = 1;
gx = 130 + random(500);
gy = 130 + random(300);
T = GetTickCount();
do{
Sleep(10);
if((Moving) )
{
if((gx > x) )
xv = xv + 0.1;
else
xv = xv - 0.1;
if((gy > y) )
yv = yv + 0.1;
else
yv = yv - 0.1;
x = x + xv;
y = y + yv;
SetCursorPos((int)floor(x), (int)floor(y));
}
if((random(100) == 0) )
Moving = ! Moving;
if((random(30) == 0) )
{
gx = 130 + random(500);
gy = 130 + random(300);
}
}while( (abs((int)(GetTickCount() - T)) <= Time));
delete p2;
}