-
Notifications
You must be signed in to change notification settings - Fork 0
/
clicker.cpp
164 lines (159 loc) · 6.88 KB
/
clicker.cpp
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
#include <thread>
#include <iostream>
#include "clicker.h"
#include "imgui.h"
#include <windows.h>
#include <random>
#include <math.h>
[[noreturn]] void clicker::clicker(unsigned int &leftKeyHexValue, unsigned int &rightKeyHexValue, bool &leftClickerStatus, bool &rightClickerStatus, int &leftCurrent, int &rightCurrent) {
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
static bool leftToggleState = false, rightToggleState = false;
static bool leftWasDown = false, rightWasDown = false;
while (true) {
bool isLeftKeyDown = (GetAsyncKeyState(leftKeyHexValue) & 0x8000) != 0;
if (leftClickerStatus) {
if (leftCurrent == 1) { // Toggle
if (isLeftKeyDown && !leftWasDown) {
leftToggleState = !leftToggleState;
leftWasDown = true;
} else if (!isLeftKeyDown) {
leftWasDown = false;
}
}
if ((leftCurrent == 1 && leftToggleState) || (leftCurrent == 0 && isLeftKeyDown)) {
clicker::sendclick(FC2_TEAM_MOUSE_LEFT, clicker::LeftcurrentCPS);
}
}
bool isRightKeyDown = (GetAsyncKeyState(rightKeyHexValue) & 0x8000) != 0;
if (rightClickerStatus) {
if (rightCurrent == 1) { // Toggle
if (isRightKeyDown && !rightWasDown) {
rightToggleState = !rightToggleState;
rightWasDown = true;
} else if (!isRightKeyDown) {
rightWasDown = false;
}
}
if ((rightCurrent == 1 && rightToggleState) || (rightCurrent == 0 && isRightKeyDown)) {
clicker::sendclick(FC2_TEAM_MOUSE_RIGHT, clicker::RightcurrentCPS);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(3));
}
}
void clicker::KeySelectionCombo(const char *combo_label, unsigned int &selectedKey) {
std::string currentKeyString = "Select a key";
for (const auto &key: clicker::keyMap) {
if (key.second == selectedKey) {
currentKeyString = key.first;
break;
}
}
ImGui::PushItemWidth(150);
if (ImGui::BeginCombo(combo_label, currentKeyString.c_str())) {
for (const auto &key: clicker::keyMap) {
bool is_selected = (selectedKey == key.second);
if (ImGui::Selectable(key.first.c_str(), is_selected)) {
selectedKey = key.second;
}
if (is_selected) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
}
//https://blat-blatnik.github.io/computerBear/making-accurate-sleep-function/
void timerSleep(double seconds) {
using namespace std::chrono;
static HANDLE timer = CreateWaitableTimer(NULL, FALSE, NULL);
static double estimate = 5e-3;
static double mean = 5e-3;
static double m2 = 0;
static int64_t count = 1;
while (seconds - estimate > 1e-7) {
double toWait = seconds - estimate;
LARGE_INTEGER due;
due.QuadPart = -int64_t(toWait * 1e7);
auto start = high_resolution_clock::now();
SetWaitableTimerEx(timer, &due, 0, NULL, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
auto end = high_resolution_clock::now();
double observed = (end - start).count() / 1e9;
seconds -= observed;
++count;
double error = observed - toWait;
double delta = error - mean;
mean += delta / count;
m2 += delta * (error - mean);
double stddev = sqrt(m2 / (count - 1));
estimate = mean + stddev;
}
// spin lock
auto start = high_resolution_clock::now();
while ((high_resolution_clock::now() - start).count() / 1e9 < seconds);
}
void clicker::sendclick(FC2_TEAM_MOUSE_CODE code, int cps) {
fc2::input::down(code);
timerSleep(0.5/cps);
fc2::input::up(code);
}
void sleep(int seconds){
std::this_thread::sleep_for(std::chrono::seconds(seconds));
}
int rndCPS(int currentCPS, int clickerRange) {
static std::random_device rd;
static std::mt19937 gen(rd());
int minCPS = std::max(1, currentCPS - clickerRange);
int maxCPS = currentCPS + clickerRange;
std::uniform_int_distribution<> dis(minCPS, maxCPS);
return dis(gen);
}
[[noreturn]] void clicker::update(int &eventDuration, int &Sleep){
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 100);
while (true) {
clicker::LeftcurrentCPS = clicker::LeftTargetedCPS;
clicker::RightcurrentCPS = clicker::RightTargetedCPS;
if (clicker::LeftSpike && dis(gen) <= clicker::LeftSpikeChance) {
clicker::LeftcurrentCPS = clicker::LeftTargetedCPS + clicker::LeftSpikeAmount;
printf("Left spike!, new cps is: %d, sleeping for %d\n", clicker::LeftcurrentCPS, eventDuration);
sleep(eventDuration);
}
if (clicker::RightSpike && dis(gen) <= clicker::RightSpikeChance) {
clicker::RightcurrentCPS = clicker::RightTargetedCPS + clicker::RightSpikeAmount;
printf("Right spike!, new cps is: %d, sleeping for %d\n", clicker::RightcurrentCPS, eventDuration);
sleep(eventDuration);
}
if (clicker::LeftDrop && dis(gen) <= clicker::LeftDropChance) {
clicker::LeftcurrentCPS = clicker::LeftTargetedCPS - clicker::LeftDropAmount;
if(clicker::LeftcurrentCPS < 0){
clicker::LeftcurrentCPS = 1;
}
printf("Left drop!, new cps is: %d, sleeping for %d\n", clicker::LeftcurrentCPS, eventDuration);
sleep(eventDuration);
}
if (clicker::RightDrop && dis(gen) <= clicker::RightDropChance) {
clicker::RightcurrentCPS = clicker::RightTargetedCPS - clicker::RightDropAmount;
if(clicker::RightcurrentCPS < 0){
clicker::RightcurrentCPS = 1;
}
printf("Right drop!, new cps is: %d, sleeping for %d\n", clicker::RightcurrentCPS, eventDuration);
sleep(eventDuration);
}
//even if any of the events are disabled the cps must be updated with its range
clicker::LeftcurrentCPS = rndCPS(clicker::LeftcurrentCPS, clicker::LeftClickerRange);
clicker::RightcurrentCPS = rndCPS(clicker::RightcurrentCPS, clicker::RightClickerRange);
printf("Left cps: %d, Right cps: %d\n", clicker::LeftcurrentCPS, clicker::RightcurrentCPS);
std::this_thread::sleep_for(std::chrono::milliseconds(Sleep));
}
}
void clicker::updateCPSTask(){
std::jthread bgThread([]() {update(clicker::EventDuration, clicker::GlobalSleep);});
bgThread.detach();
}
void clicker::BackgroundTask() {
std::jthread bgThread([]() {clicker(clicker::Leftclickerkey,clicker::Rightclickerkey, clicker::LeftClickerStatus,clicker::RightClickerStatus,clicker::current, clicker::rightCurrent);});
bgThread.detach();
}