Advertisement
CasualGamer

T - Rex Bot 2

Feb 3rd, 2020
2,524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.14 KB | None | 0 0
  1. #include <windows.h>
  2. #include <thread>
  3. #include <chrono>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. void press_key(SHORT virtual_key_code, int& down_time, SHORT virtual_key_code2 = NULL) {
  9.     INPUT Input = { 0 };
  10.     Input.type = INPUT_KEYBOARD;
  11.     Input.ki.wVk = virtual_key_code;
  12.     SendInput(1, &Input, sizeof(Input));
  13.     ZeroMemory(&Input, sizeof(Input));
  14.     Input.type = INPUT_KEYBOARD;
  15.     Input.ki.wVk = virtual_key_code;
  16.     Input.ki.dwFlags = KEYEVENTF_KEYUP;
  17.     std::this_thread::sleep_for(std::chrono::milliseconds(down_time));
  18.     SendInput(1, &Input, sizeof(Input));
  19.     ZeroMemory(&Input, sizeof(Input));
  20.     if (virtual_key_code2 != NULL) {
  21.         Input.type = INPUT_KEYBOARD;
  22.         Input.ki.wVk = virtual_key_code2;
  23.         SendInput(1, &Input, sizeof(Input));
  24.         ZeroMemory(&Input, sizeof(Input));
  25.         Input.type = INPUT_KEYBOARD;
  26.         Input.ki.wVk = virtual_key_code2;
  27.         Input.ki.dwFlags = KEYEVENTF_KEYUP;
  28.         std::this_thread::sleep_for(std::chrono::milliseconds(140));
  29.         SendInput(1, &Input, sizeof(Input));
  30.         ZeroMemory(&Input, sizeof(Input));
  31.     }
  32. }
  33.  
  34. int get_Bitmap(int x, int y, HDC& hdcMemory, int width = 700, int height = 200) {
  35.     HDC hdcSource = GetDC(NULL);
  36.     hdcMemory = CreateCompatibleDC(hdcSource);
  37.     HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, width, height);
  38.     HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);
  39.     if (!BitBlt(hdcMemory, 0, 0, width, height, hdcSource, x, y, CAPTUREBLT | SRCCOPY)) {
  40.         cout << "BitBlt failed!" << endl;
  41.     }
  42.  
  43.     //clean up
  44.     DeleteObject(hBitmapOld);
  45.     DeleteObject(hBitmap);
  46.     ReleaseDC(NULL, hdcSource);
  47.  
  48.     return 0;
  49. }
  50.  
  51. void update_Bitmap(int x, int y, HDC& hdcMemory, bool& exit_flag, int width = 700, int height = 200) {
  52.     while (!exit_flag) {
  53.         get_Bitmap(x, y, hdcMemory, width = 700, height = 200);
  54.         std::this_thread::sleep_for(std::chrono::milliseconds(50));
  55.     }
  56.     cout << "thread: update Bitmap stopped" << endl;
  57.     return;
  58. }
  59.  
  60. void jumper(bool& exit_flag, POINT tail, POINT& cactus_min, POINT& cactus_max, COLORREF threat_color, HDC& hdcMemory, SHORT& second_key, int& down_time){
  61.     while (!exit_flag) {
  62.         for (int i = cactus_min.x + tail.x; i < cactus_max.x + tail.x; i = i + 4) {
  63.             int j = cactus_min.y + tail.y;
  64.             COLORREF color = GetPixel(hdcMemory, i, j);
  65.             if (color == threat_color) {
  66.                 press_key(VK_SPACE,down_time, second_key);
  67.                 //cout << "Cactus!" << endl;
  68.                 break;
  69.             }
  70.             std::this_thread::sleep_for(std::chrono::milliseconds(1));
  71.         }
  72.     }
  73.     cout << "thread: jumping stopped" << endl;
  74. }
  75.  
  76. void dodger(bool& exit_flag, POINT tail, POINT& bird_coords, COLORREF threat_color, HDC& hdcMemory) {
  77.     COLORREF color;
  78.     while (!exit_flag) {
  79.         for (int i = tail.x + bird_coords.x; i < tail.x + bird_coords.x + 30; i = i + 2) {
  80.             color = GetPixel(hdcMemory, i, tail.y + bird_coords.y);
  81.             if (color == threat_color) {
  82.                 //cout << "THIS IS BAT LAND!" << endl;
  83.                 int temp = 320;
  84.                 press_key(VK_DOWN, temp);
  85.             }
  86.         }
  87.         std::this_thread::sleep_for(std::chrono::milliseconds(1));
  88.     }
  89.     cout << "thread: dodging stopped" << endl;
  90. }
  91.  
  92. POINT get_tail(HDC& dc, POINT origin,int startx = 0, int starty = 0, int width = 700, int height = 200) {
  93.     POINT p = { startx, starty };
  94.     for (int i = startx; i < startx + width; i++) {//x
  95.         for (int j = starty; j < starty + height; j++) {//y
  96.             COLORREF color = GetPixel(dc, i, j);
  97.             SetCursorPos(origin.x + i, origin.y + j);
  98.             if (color == 5460819) {
  99.                 p = { i, j };
  100.                 return p;
  101.             }
  102.         }
  103.     }
  104.     return p;
  105. }
  106.  
  107. int main() {
  108.     HDC hdcMemory = NULL;
  109.     POINT origin = {}, tail = {};//tail relative to origin
  110.     int width = 350;
  111.     int height = 200;
  112.     int i=0, j=0,downtime = 320;
  113.     POINT cactus_min = { 70, 8 };
  114.     POINT cactus_max = { 130, 8 };
  115.     POINT bird_coords = { 90, -20 };
  116.     COLORREF threat_color = 5460819;
  117.     COLORREF color = NULL;
  118.     bool exit_flag = false;
  119.     SHORT second_key = NULL;
  120.  
  121.     cout << "[N1] Get Map" << endl;
  122.     cout << "[N2] Get Tail" << endl;
  123.     cout << "[N3] Start Bot" << endl;
  124.     cout << "[N4] Pixel Info" << endl;
  125.     cout << "[N0] Stop | Exit" << endl;
  126.  
  127.     while (true) {
  128.         std::this_thread::sleep_for(std::chrono::milliseconds(500));
  129.         if (GetAsyncKeyState(VK_NUMPAD0)) { // Exit
  130.             if (hdcMemory != NULL)
  131.                 DeleteDC(hdcMemory);
  132.             return 0;
  133.         }
  134.         if (GetAsyncKeyState(VK_NUMPAD1)) { // get map
  135.             GetCursorPos(&origin);
  136.             get_Bitmap(origin.x, origin.y, hdcMemory, width, height);
  137.             cout << "We got the map!" << endl;
  138.         }
  139.         if (GetAsyncKeyState(VK_NUMPAD2)) { // get tail
  140.             POINT p;
  141.             GetCursorPos(&p);
  142.             tail = get_tail(hdcMemory, origin,p.x-origin.x,p.y-origin.y,30,30);
  143.             cout << "Oh f*ck it's a t-rex!" << endl;
  144.         }
  145.         if (GetAsyncKeyState(VK_NUMPAD3)) {
  146.             cout << "Starting Bot!" << endl;
  147.             if (hdcMemory == NULL) {
  148.                 cout << "you did not find the map yet!";
  149.             }
  150.             else {
  151.                 int amount_speedups = 0;
  152.                 auto startTime = std::chrono::high_resolution_clock::now();
  153.                 auto endTime = std::chrono::high_resolution_clock::now();
  154.                 std::chrono::duration<double, std::milli> ms = (endTime - startTime);
  155.                 exit_flag = false;
  156.                 SetCursorPos(origin.x + tail.x + bird_coords.x, origin.y + bird_coords.y + tail.y);
  157.                 std::thread t_Bitmap(update_Bitmap, origin.x, origin.y,ref(hdcMemory), ref(exit_flag), width, height);
  158.                 std::thread t_Jumper(jumper, ref(exit_flag), tail, ref(cactus_min), ref(cactus_max), threat_color, ref(hdcMemory), ref(second_key), ref(downtime));
  159.                 std::thread t_Dodger(dodger, ref(exit_flag), tail, ref(bird_coords), threat_color, ref(hdcMemory));
  160.                 while (true) {
  161.                     endTime = std::chrono::high_resolution_clock::now();
  162.                     ms = (endTime - startTime);
  163.                     if (ms.count() > 10000) {
  164.                         amount_speedups++;
  165.                         cout << "Speed++" << endl;
  166.                         int addition = 7;
  167.                         cactus_min.x += addition;
  168.                         cactus_max.x += addition;
  169.                         bird_coords.x += 4;
  170.                         startTime = std::chrono::high_resolution_clock::now();
  171.                         endTime = std::chrono::high_resolution_clock::now();
  172.                     }
  173.                     if (amount_speedups == 3) {
  174.                         second_key = VK_DOWN;
  175.                     }
  176.                     if (amount_speedups == 9) {
  177.                         downtime = 240;
  178.                     }
  179.                     if (amount_speedups == 11) {
  180.                         downtime = 180;
  181.                     }
  182.                     if (GetAsyncKeyState(VK_NUMPAD0)) { // Exit
  183.                         cout << "Stopping Bot!" << endl;
  184.                         exit_flag = true;
  185.                         t_Bitmap.join();
  186.                         t_Jumper.join();
  187.                         t_Dodger.join();
  188.                         break;
  189.                     }
  190.                 }
  191.             }
  192.         }
  193.         if (GetAsyncKeyState(VK_NUMPAD4)) { // get pixel color
  194.             POINT p;
  195.             GetCursorPos(&p);
  196.             if (hdcMemory != NULL) { // variable where hdc is stored
  197.                 COLORREF color = GetPixel(hdcMemory, p.x - origin.x, p.y - origin.y);
  198.                 cout << "Absolute values" << endl;
  199.                 cout << "X: " << p.x << " Y: " << p.y << " Color: " << color << endl;
  200.                 //cout << "Relative values" << endl; dont worry about this yet :P
  201.                 //cout << "X: " << p.x - origin.x - tail.x << " Y: " << p.y - (origin.y + tail.y) << endl;
  202.             }
  203.             else
  204.                 cout << "Get map first!" << endl;
  205.         }
  206.     }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement