Specifically, the windMouse function.
Specifically, the windMouse function.
It's easy to port from pascal. But perhaps even easier to port from Java: https://github.com/BenLand100/SMART/...java#L201-L253
!No priv. messages please
Haven't tested.. just ported..
C++ Code://
// main.cpp
// Mouse
//
// Created by Brandon on 2018-05-25.
// Copyright © 2018 XIO. All rights reserved.
//
#include <iostream>
#include <random>
#include <cmath>
#include <thread>
#include <chrono>
#if defined(_WIN32) || defined(_WIN64)
#include <Window.h>
#elif defined(__APPLE__)
#import <Foundation/Foundation.h>
#else
#include <X11/Xlib.h>
#endif
struct POINT
{
std::int32_t x;
std::int32_t y;
};
void MoveMouse(POINT p)
{
#if defined(_WIN32) || defined(_WIN64)
SetCursorPos(p.x, p.y);
#elif defined(__APPLE__)
CGWarpMouseCursorPosition(CGPointMake(p.x, p.y));
#else
Display *display = XOpenDisplay(0);
XWarpPointer(display, None, None, 0, 0, 0, 0, p.x, p.y);
XCloseDisplay(display);
#endif
}
POINT WindMouse(double x1, double y1, double x2, double y2, double gravity, double wind, double minWait, double maxWait, double maxStep, double targetArea)
{
double sqrt3 = std::sqrt(3.0);
double sqrt5 = std::sqrt(5.0);
double dist = 0.0, veloX = 0.0, veloY = 0.0, windX = 0.0, windY = 0.0;
POINT lastPosition = {0, 0};
std::random_device rdevice;
std::default_random_engine rengine(rdevice());
while ((dist = std::hypot(x2 - x1, y2 - y1)) >= 1)
{
wind = std::min(wind, dist);
if (dist >= targetArea)
{
windX = windX / sqrt3 + (rengine() * (wind * 2.0 + 1.0) - wind) / sqrt5;
windY = windY / sqrt3 + (rengine() * (wind * 2.0 + 1.0) - wind) / sqrt5;
}
else
{
windX /= sqrt3;
windY /= sqrt3;
if (maxStep < 3)
{
maxStep = rengine() * 3 + 3.0;
}
else
{
maxStep /= sqrt5;
}
}
veloX += windX + gravity * (x2 - x1) / dist;
veloY += windY + gravity * (y2 - y1) / dist;
double veloMag = std::hypot(veloX, veloY);
if (veloMag > maxStep)
{
double randomDist = maxStep / 2.0 + rengine() * maxStep / 2.0;
veloX = (veloX / veloMag) * randomDist;
veloY = (veloY / veloMag) * randomDist;
}
x1 += veloX;
y1 += veloY;
std::int32_t mx = static_cast<std::int32_t>(std::round(x1));
std::int32_t my = static_cast<std::int32_t>(std::round(y1));
if (mx < 0 || my < 0)
{
break;
}
if (lastPosition.x != mx || lastPosition.y != my)
{
MoveMouse(POINT{mx, my});
lastPosition = POINT{mx, my};
}
double step = std::hypot(x1 - lastPosition.x, y1 - lastPosition.y);
std::int64_t milliseconds = static_cast<std::int64_t>(std::round((maxWait - minWait) * (step / maxStep) + minWait));
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
}
return lastPosition;
}
POINT WindMouse(POINT p)
{
std::random_device rdevice;
std::default_random_engine rengine(rdevice());
POINT lastPosition = {-1, -1};
double speed = (rengine() * 15.0 + 15.0) / 10.0;
return WindMouse(lastPosition.x, lastPosition.y, p.x, p.y, 9.0, 3.0, 5.0 / speed, 10.0 / speed, 10.0 * speed, 8.0 * speed);
}
int main(int argc, const char * argv[])
{
WindMouse(POINT{100, 100});
return 0;
}
Last edited by Brandon; 05-26-2018 at 01:52 AM.
I am Ggzz..
Hackintosher
Thanks for the port! Spent a bit tinkering with it and got it to compile. I used Visual Studios and created a empty CLR project. This is edited file.
C++ Code://
// main.cpp
// Mouse
//
// Created by Brandon on 2018-05-25.
// Copyright © 2018 XIO. All rights reserved.
//
#include <iostream>
#include <random>
#include <cmath>
#include <thread>
#include <chrono>
using namespace std;
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#elif defined(__APPLE__)
#import <Foundation/Foundation.h>
#else
#include <X11/Xlib.h>
#endif
struct MOUSEPOINT
{
int32_t x;
int32_t y;
};
void MoveMouse(MOUSEPOINT p)
{
#if defined(_WIN32) || defined(_WIN64)
SetCursorPos(p.x, p.y);
#elif defined(__APPLE__)
CGWarpMouseCursorPosition(CGPointMake(p.x, p.y));
#else
Display *display = XOpenDisplay(0);
XWarpPointer(display, None, None, 0, 0, 0, 0, p.x, p.y);
XCloseDisplay(display);
#endif
}
MOUSEPOINT WindMouse(double x1, double y1, double x2, double y2, double gravity, double wind, double minWait, double maxWait, double maxStep, double targetArea)
{
double sqrt3 = std::sqrt(3.0);
double sqrt5 = std::sqrt(5.0);
double dist = 0.0, veloX = 0.0, veloY = 0.0, windX = 0.0, windY = 0.0;
MOUSEPOINT lastPosition = { 0, 0 };
std::random_device rdevice;
std::default_random_engine rengine(rdevice());
while ((dist = hypot(x2 - x1, y2 - y1)) >= 1)
{
wind = min(wind, dist);
if (dist >= targetArea)
{
windX = windX / sqrt3 + (rengine() * (wind * 2.0 + 1.0) - wind) / sqrt5;
windY = windY / sqrt3 + (rengine() * (wind * 2.0 + 1.0) - wind) / sqrt5;
}
else
{
windX /= sqrt3;
windY /= sqrt3;
if (maxStep < 3)
{
maxStep = rengine() * 3 + 3.0;
}
else
{
maxStep /= sqrt5;
}
}
veloX += windX + gravity * (x2 - x1) / dist;
veloY += windY + gravity * (y2 - y1) / dist;
double veloMag = hypot(veloX, veloY);
if (veloMag > maxStep)
{
double randomDist = maxStep / 2.0 + rengine() * maxStep / 2.0;
veloX = (veloX / veloMag) * randomDist;
veloY = (veloY / veloMag) * randomDist;
}
x1 += veloX;
y1 += veloY;
int32_t mx = static_cast<int32_t>(round(x1));
int32_t my = static_cast<int32_t>(round(y1));
if (mx < 0 || my < 0)
{
break;
}
if (lastPosition.x != mx || lastPosition.y != my)
{
MoveMouse(MOUSEPOINT{ mx, my });
lastPosition = MOUSEPOINT{ mx, my };
}
double step = hypot(x1 - lastPosition.x, y1 - lastPosition.y);
//cout << "===" << endl;
//cout << "maxWait: " << maxWait << endl;
//cout << "minWait: " << minWait << endl;
//cout << round(maxWait - minWait) << endl;
//cout << "step: " << step << endl;
//cout << "maxStep: " << maxStep << endl;
//cout << (step / maxStep) << endl;
int64_t milliseconds = static_cast<int64_t>(((maxWait - minWait) * (step / maxStep) + minWait));
//cout << milliseconds << endl;
this_thread::sleep_for(chrono::milliseconds(milliseconds));
}
return lastPosition;
}
MOUSEPOINT WindMouse(MOUSEPOINT p)
{
random_device rdevice;
default_random_engine rengine(rdevice());
MOUSEPOINT lastPosition = { -1, -1 };
double speed = (rengine() * 15.0 + 15.0) / 10.0;
return WindMouse(lastPosition.x, lastPosition.y, p.x, p.y, 9.0, 3.0, 5.0 / speed, 10.0 / speed, 10.0 * speed, 8.0 * speed);
}
int main(int argc, const char * argv[])
{
while (true) {
if (GetAsyncKeyState(VK_MENU)) {
WindMouse(MOUSEPOINT{ rand() % 1360, rand() % 760 });
}
}
return 0;
}
I renamed the "Point" struct because I got a redefinition error. The problem now is that milliseconds is always 0 resulting in nearly instantaneous mouse movements. I think the problem lies in "rengine()". I did some googling and it seems that this function is some abstract base class for the implementation of other "engines". I googled "rengine() c++" and couldn't find anything concrete leading me to think this isn't C++ related. I am a freshman in college majoring in Computer Science so my knowledge of C++ is still very much of a novice. Would you happen to know a C++ "version" of rengine()? Or is there something I am missing?
@Brandon; @bloos;
std::random_device follows a uniform distribution, which in itself is incorrect for this scenario if you ask me. Secondly, it generates non-deterministic numbers which is nice but requires a non-deterministic source (hardware device as per the docs suggest) to generate values. If this is not present then it can generate the exact same values each time (since the seed would be the exact same each time you initialize it) - a simple (not ideal) fix would be to make std::random_device into a static variable so that the same first number generated with that particular seed isn't used repeatedly. I also remember reading somewhere that if a non-deterministic hardware source is not available, rand() is used and rand() is bad. I would suggest to replace std::random_device with Mersenne Twister 19937 RNG and to generate numbers following a normal distribution. So something like this:
C++ Code:// Define generator somewhere, seeded with system time.
static std::mt19937 RNG(std::chrono::high_resolution_clock::now().time_since_epoch().count());
// Define Distribution
std::normal_distribution Distribution(/*MEAN*/, /*DEVIATION*/);
// Generate number.
std::cout << Distribution(RNG) << std::endl;
Pretty sick talk on random number generators for anyone interested:
https://channel9.msdn.com/Events/Goi...idered-Harmful
Last edited by Kasi; 05-26-2018 at 11:57 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)