Advertisement
Guest User

Untitled

a guest
Mar 4th, 2024
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.96 KB | None | 0 0
  1. // TODO: Eventually make some information globals so we can display randomization debug information inside the menu
  2. namespace autoclicker {
  3.     float target;
  4.     float cps;
  5.  
  6.     void run();
  7.  
  8.     void deviate();
  9.     void spike();
  10.     void fluctuate();
  11.  
  12.     void set_after_for(bool& condition, util::timer& timer, float after, float time);
  13.  
  14.     std::vector<float> delays;
  15.     void test_randomization();
  16. }
  17.  
  18. enum class e_spike : uint8_t {
  19.     none = 0,
  20.     up,
  21.     down
  22. };
  23.  
  24. // This code is pretty messy
  25. // I think it would be pretty nice if I generate the delays around 100 ahead so I can test it all the time
  26. void autoclicker::run() {
  27.     target = mouse_down ? settings::cps : 5.0f;
  28.  
  29.     deviate();
  30.     spike();
  31.     fluctuate();
  32.  
  33.     if (!mouse_down) {
  34.         delays.clear();
  35.         return;
  36.     }
  37.  
  38.     if (settings::only_mc && hwnd != GetForegroundWindow())
  39.         return;
  40.  
  41.     // No point in clicking at this low of a cps
  42.     if (cps < 1.5f)
  43.         return;
  44.  
  45.     static util::timer timer;
  46.     static auto delay = 0.0f;
  47.  
  48.     // Subtract that 1ms just to try to make it a bit more accurate
  49.     if (!timer.has_time_passed(std::chrono::duration<float>(delay)))
  50.         return;
  51.  
  52.     timer.reset();
  53.  
  54.     // Ignore test timer for debugging
  55.     static util::timer test;
  56.  
  57.     // Send mouse down input
  58.     INPUT input = { 0 };
  59.  
  60.     static bool holding = false;
  61.  
  62.     if (!holding) {
  63.         input.type = INPUT_MOUSE;
  64.         input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
  65.         ::SendInput(1, &input, sizeof(INPUT));
  66.  
  67.         test.reset();
  68.     }
  69.  
  70.     // TODO: Incorporate blockhit
  71.  
  72.     // Random hold if break blocks is disabled
  73.     static util::timer hold_timer;
  74.     static auto hold_delay = 0.0f;
  75.  
  76.     if (!settings::break_blocks) {
  77.         if (hold_timer.has_time_passed(std::chrono::duration<float>(hold_delay))) {
  78.             hold_timer.reset();
  79.  
  80.             hold_delay = util::random(0.02f, 0.05f);
  81.  
  82.             holding = false;
  83.         }
  84.         else {
  85.             holding = true;
  86.             return;
  87.         }
  88.     }
  89.     else {
  90.         hold_delay = 0.0f;
  91.     }
  92.  
  93.     // Send mouse up input
  94.     input = { 0 };
  95.     input.type = INPUT_MOUSE;
  96.     input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
  97.     ::SendInput(1, &input, sizeof(INPUT));
  98.  
  99.     delay = (1.0f / cps) - hold_delay;
  100.     delays.push_back(cps);
  101.  
  102.     test_randomization();
  103.  
  104.     //printf("[autoclicker] target %f\tcps %f\n", target, cps);
  105. }
  106.  
  107. // There are some somewhat magic seeming numbers in how this autoclickers randomization is setup but at least its
  108. //  not like those ones using ms delays instead of seconds so its readable
  109.  
  110. void autoclicker::deviate() {
  111.     static util::timer timer;
  112.     static auto update = util::random(3.0f, 15.0f);
  113.     static auto deviation = 0.0f;
  114.  
  115.     if (timer.has_time_passed(std::chrono::duration<float>(update))) {
  116.         timer.reset();
  117.  
  118.         update = util::random<float>(3.0f, 15.0f);
  119.  
  120.         deviation = util::random<float>(-2.0f, 0.5f);
  121.  
  122.         printf("[deviate] %fcps for %fs\n", deviation, update);
  123.     }
  124.  
  125.     // TODO: Slowly go downwards overtime and even stop and restart since no one can click the same cps forever
  126.  
  127.     target += deviation;
  128. }
  129.  
  130. // This includes dropping even though I do think dropping should be less rare
  131. void autoclicker::spike() {
  132.     static util::timer timer;
  133.     static auto after = util::random<float>(5.0f, 20.0f);
  134.     static auto time = util::random<float>(0.2f, 0.8f);
  135.  
  136.     static e_spike spike_stage = e_spike::none;
  137.     static bool spiking;
  138.  
  139.     // Check for when it stops spiking
  140.     static auto previous = spiking;
  141.     if (!previous && spiking) {
  142.         spike_stage = (e_spike)util::random<uint16_t>(1, 2);
  143.  
  144.         printf("[spike] %d for %fs\n", (int)spike_stage, time);
  145.     }
  146.     if (previous && !spiking) {
  147.         spike_stage = e_spike::none;
  148.  
  149.         after = util::random<float>(5.0f, 20.0f);
  150.         time = util::random<float>(0.2f, 0.8f);
  151.  
  152.         printf("[spike] none for %fs\n", after);
  153.     }
  154.     previous = spiking;
  155.  
  156.     set_after_for(spiking, timer, after, time);
  157.  
  158.     switch (spike_stage) {
  159.         case e_spike::none:
  160.             break;
  161.         case e_spike::up:
  162.             target += settings::up;
  163.             break;
  164.         case e_spike::down:
  165.             target -= settings::down;
  166.             break;
  167.         default:
  168.             break;
  169.     }
  170. }
  171.  
  172. // TODO: Make all fields customizable
  173. void autoclicker::fluctuate() {
  174.     constexpr auto delay = 0.1f;
  175.     constexpr auto radius = 2.0f;
  176.  
  177.     static util::timer timer;
  178.  
  179.     if (!timer.has_time_passed(std::chrono::duration<float>(delay)))
  180.         return;
  181.  
  182.     timer.reset();
  183.  
  184.     // Normal fluctuation
  185.     const auto previous = cps;
  186.     cps += util::random(-0.5f, 0.4f);
  187.  
  188.     const auto step = 1.0f / delay;
  189.  
  190.     // TODO: I'm fairly sure this math is so so incorrect, I just cba to fix it right after waking up
  191.     const auto exp = (target - cps) / radius;
  192.  
  193.     //printf("[fluctuate] target %fcps\tcurrent %fcps\tcalculation %f\n", target, cps, exp);
  194.  
  195.     // Will make it take 2 seconds to reach desired target
  196.     cps += util::random(exp / (step * 1.5f), exp / (step * 2.0f));
  197.  
  198.     //printf("[fluctuate] change %f\n", cps - previous);
  199. }
  200.  
  201. void autoclicker::set_after_for(bool& condition, util::timer &timer, float after, float time) {
  202.     if (!condition && timer.has_time_passed(std::chrono::duration<float>(after))) {
  203.         timer.reset();
  204.         condition = true;
  205.     }
  206.  
  207.     if (condition && timer.has_time_passed(std::chrono::duration<float>(time))) {
  208.         timer.reset();
  209.         condition = false;
  210.     }
  211. }
  212.  
  213. // Mimic some of these checks
  214. // https://github.com/sim0n/Baldr/tree/master/src/main/java/net/rifttech/baldr/check/impl/autoclicker
  215. void autoclicker::test_randomization() {
  216.     // This could cause overflows
  217.     float sum = std::accumulate(delays.begin(), delays.end(), 0.0f);
  218.     float average = sum / static_cast<float>(delays.size());
  219.  
  220.     std::vector<float> differences(delays.size());
  221.  
  222.     std::transform(delays.begin(), delays.end(), differences.begin(), [average](float x) {
  223.         return x - average;
  224.     });
  225.  
  226.     float squared_differences = std::inner_product(differences.begin(), differences.end(), differences.begin(), 0.0f);
  227.     float standard_deviation = std::sqrt(squared_differences / (float)delays.size());
  228.  
  229.     if (standard_deviation < 0.45f) {
  230.         //printf("[alert] standard deviation is too low %f\n", standard_deviation);
  231.     }
  232.     else {
  233.         //printf("[randomization] standard deviation %f\n", standard_deviation);
  234.     }
  235.  
  236.     // There is 100% a better way to calculate this
  237.     float r = 0.0f;
  238.  
  239.     for (auto difference : differences) {
  240.         r += std::pow(difference, 4.0f);
  241.     }
  242.  
  243.     const auto kurtosis = (float)differences.size() * r / std::pow(squared_differences, 2.0f);
  244.  
  245.     if (kurtosis <= 0.0f) {
  246.         //printf("[alert] kurtosis is too low %f\n", kurtosis);
  247.     }
  248.     else {
  249.         //printf("[randomization] kurtosis %f\n", kurtosis);
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement