Advertisement
asqapro

AimbotAVA Update

Nov 13th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.21 KB | None | 0 0
  1. //LINK AGAINST gdi32, user32, kernel32 TO RUN CORRECTLY
  2.  
  3. #include <windows.h>
  4. #include <iostream>
  5. #define Winver 0x500
  6. #include <tlhelp32.h>
  7. #include <stdio.h>
  8. #include <winable.h>
  9. #include <fstream>
  10. #include <istream>
  11. #include <string>
  12. #include <time.h>
  13. #include <sys/timeb.h>
  14. #include <vector>
  15. #include <algorithm>
  16. #include <sstream>
  17. #include <iterator>
  18.  
  19. using namespace std;
  20.  
  21. int ScreenWidth; //the width of the screen
  22. int ScreenHeight; //the height of the screen
  23.  
  24. ofstream pixel_file; //to-be-written to file that will hold the pixel colors
  25. ifstream pixel_check; //to-be-read from file that holds the pixel colors
  26.  
  27. bool operator == (vector<unsigned int> const& v1, vector<unsigned int> const& v2){ //for comparing 2 vectors
  28.     return (v1[0] == v2[0]) && (v1[1] == v2[1]) && (v1[2] == v2[2]);
  29. }
  30.  
  31. int getMilliCount(){ //timing function, used to test speed
  32.     timeb tb;
  33.     ftime(&tb);
  34.     int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
  35.     return nCount;
  36. }
  37.  
  38. int getMilliSpan(int nTimeStart){ //timing function, used to test speed
  39.     int nSpan = getMilliCount() - nTimeStart;
  40.     if(nSpan < 0)
  41.         nSpan += 0x100000 * 1000;
  42.     return nSpan;
  43. }
  44.  
  45. void mouseMove(int x_coord, int y_coord) //moves the mouse x and y amounts
  46. {
  47.     INPUT mouse;
  48.     memset(&mouse, 0, sizeof(INPUT));
  49.     mouse.type = INPUT_MOUSE;
  50.     // flag for the mouse hook to tell that it's a synthetic event.
  51.     mouse.mi.dwExtraInfo = 0x200;
  52.     mouse.mi.dx = x_coord;
  53.     mouse.mi.dy = y_coord;
  54.     mouse.mi.dwFlags = mouse.mi.dwFlags | MOUSEEVENTF_MOVE;
  55.     SendInput(1, &mouse, sizeof(mouse));
  56. }
  57.  
  58. int curr_color[2]; //the current pixel color
  59. unsigned int pix_data[1000][2]; //reference radius for specific size
  60. int pix_pos_piece[1]; //a pixel x, y coordinate
  61. int pix_pos_counter = 0; //the counter for all of the positions
  62.  
  63. void get_bitmap_ava(HWND ava_hwnd, int size_x, int size_y, bool get_pixel_color = false) //grabs the bitmap
  64. {
  65.     int pix_pos_full[size_x * size_y][1]; //use max possible positions just in case
  66.     if(get_pixel_color){ //if the user pressed enter to grab pixels
  67.         pixel_file.open("pixels.dat"); //open the write-to file
  68.     }
  69.  
  70.     int pos_x, pos_y; //the starting x and y position
  71.     pos_x = size_x / 2; //crosshairs are always center screen
  72.     pos_y = size_y / 2;
  73.     //POINT p; //used for special testing
  74.     //GetCursorPos(&p);
  75.     //pos_x = p.x;
  76.     //pos_y = p.y;
  77.  
  78.     int radius = 10; //the amount of pixels to grab if saving pixel colors
  79.  
  80.     //int start = getMilliCount(); //track screenread speed in milliseconds
  81.  
  82.     //researching, unsure if needed
  83.     //RECT myRect;
  84.     //GetClientRect(ava_hwnd, (LPRECT)&myRect);
  85.     //ClientToScreen(ava_hwnd, (LPPOINT)&myRect.left);
  86.     //ClientToScreen(ava_hwnd, (LPPOINT)&myRect.top);
  87.  
  88.     HDC ava_dc = GetDC(ava_hwnd); //get the device context of the window
  89.     HDC ava_dc_cap = CreateCompatibleDC(ava_dc); //create a compatible dc out of that
  90.     HBITMAP hCaptureBitmap = CreateCompatibleBitmap(ava_dc, size_x, size_y); //make a bitmap
  91.     SelectObject(ava_dc_cap, hCaptureBitmap); //select it in memory
  92.     BitBlt(ava_dc_cap, 0, 0, size_x, size_y, ava_dc, 0, 0, SRCCOPY); //and bitblt it
  93.  
  94.     //getting the size of the picture
  95.     BITMAP bm;
  96.     GetObject(hCaptureBitmap, sizeof(bm), &bm);
  97.     int width(bm.bmWidth), height(bm.bmHeight);
  98.  
  99.     //creating a bitmapheader for getting the dibits
  100.     BITMAPINFOHEADER bminfoheader;
  101.     ::ZeroMemory(&bminfoheader, sizeof(BITMAPINFOHEADER));
  102.     bminfoheader.biSize        = sizeof(BITMAPINFOHEADER);
  103.     bminfoheader.biWidth       = width;
  104.     bminfoheader.biHeight      = -height;
  105.     bminfoheader.biPlanes      = 1;
  106.     bminfoheader.biBitCount    = 32;
  107.     bminfoheader.biCompression = BI_RGB;
  108.  
  109.     bminfoheader.biSizeImage = width * 4 * height;
  110.     bminfoheader.biClrUsed = 0;
  111.     bminfoheader.biClrImportant = 0;
  112.  
  113.  
  114.     //ALL THAT IS COMMENTED OUT BELOW IS TO SAVE THE PICTURE TO A FILE
  115.  
  116.     /*DWORD dwBmpSize = ((size_x * bminfoheader.biBitCount + 31) / 32) * 4 * size_y;
  117.  
  118.     HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
  119.     char *lpbitmap = (char *)GlobalLock(hDIB);
  120.  
  121.     // Gets the "bits" from the bitmap and copies them into a buffer
  122.     // which is pointed to by lpbitmap.
  123.     GetDIBits(CreateCompatibleDC(0), hCaptureBitmap, 0,
  124.         (UINT)bm.bmHeight,
  125.         lpbitmap,
  126.         (BITMAPINFO *)&bminfoheader, DIB_RGB_COLORS);
  127.  
  128.     // A file isz3 created, this is where we will save the screen capture.
  129.     HANDLE hFile = CreateFile("captureqwsx.bmp",
  130.         GENERIC_WRITE,
  131.         0,
  132.         NULL,
  133.         CREATE_ALWAYS,
  134.         FILE_ATTRIBUTE_NORMAL, NULL);
  135.  
  136.  
  137.     // Add the size of the headers to the size of the bitmap to get the total file size
  138.     DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  139.  
  140.     BITMAPFILEHEADER bmfh;
  141.  
  142.  
  143.     //Offset to where the actual bitmap bits start.
  144.     bmfh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
  145.  
  146.  
  147.     //Size of the file
  148.     bmfh.bfSize = dwSizeofDIB;
  149.  
  150.  
  151.     //bfType must always be BM for Bitmaps
  152.     bmfh.bfType = 0x4D42; //BM
  153.  
  154.  
  155.     DWORD dwBytesWritten = 0;
  156.     WriteFile(hFile, (LPSTR)&bmfh, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
  157.     WriteFile(hFile, (LPSTR)&bminfoheader, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
  158.     WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
  159.  
  160.  
  161.     //Unlock and Free the DIB from the heap
  162.     GlobalUnlock(hDIB);
  163.     GlobalFree(hDIB);
  164.  
  165.     //Close the handle for the file that was created
  166.     CloseHandle(hFile);*/
  167.  
  168.  
  169.     //FILE SAVING PROCESS ENDS HERE
  170.  
  171.  
  172.     //create a buffer and let the GetDIBits fill in the buffer
  173.     unsigned char* pPixels = new unsigned char[(width * 4 * height)];
  174.     if( !GetDIBits(CreateCompatibleDC(0), hCaptureBitmap, 0, height, pPixels, (BITMAPINFO*) &bminfoheader, DIB_RGB_COLORS)) // load pixel info
  175.     {
  176.         //return if fails but first delete the resources
  177.         DeleteObject(hCaptureBitmap);
  178.         delete [] pPixels; // delete the array of objects
  179.         cout << "fail" << endl;
  180.  
  181.         return;
  182.     }
  183.  
  184.     int x, y; // fill the x and y coordinate for grabbing pixels
  185.     x = 0;
  186.     y = 0;
  187.  
  188.     int total_found = 0; //the total amount of pixels matching pixels being searched for
  189.     int previous_x = -1; //set the initial prev_x and y to -1 to avoid issues
  190.     int previous_y = -1;
  191.     for(int iter = 0; iter < (size_x * size_y); iter++){ //loop through all of the pixels in the screen
  192.         if(x < size_x){ //if the pixels have not reached the right of the screen
  193.             x++; //keep going
  194.         }
  195.         else{
  196.             x = 0; //if they do reach the right, reset to the left
  197.             y++; //and go down one row
  198.         }
  199.         unsigned int r = pPixels[(width*y+x) * 4 + 2]; //the pixel color in r g b color format
  200.         unsigned int g = pPixels[(width*y+x) * 4 + 1];
  201.         unsigned int b = pPixels[(width*y+x) * 4 + 0];
  202.         curr_color[0] = r; //set the current color array values
  203.         curr_color[1] = g; //old values wrote over with each new pixel
  204.         curr_color[2] = b;
  205.         pix_pos_piece[0] = x; //set the current position
  206.         pix_pos_piece[1] = y; //wrote over with each new pixel
  207.         if(!get_pixel_color){ //if we are looking for pixels (not saving them)
  208.             bool done; //a bool to check if we have gone through the entire set of pixels to look for
  209.             for(unsigned int iter2 = 0; iter2 < sizeof(pix_data)/sizeof(unsigned int); iter2++){
  210.                 if(abs(int(curr_color[0] - pix_data[iter2][0])) < 1 && abs(int(curr_color[1] - pix_data[iter2][1])) < 1 && abs(int(curr_color[2] - pix_data[iter2][2])) < 1){
  211.                     //if all of the current pixel colors are within 1 color difference of the pixels being searched for
  212.                     break; //exit the loop
  213.                 }
  214.                 if(sizeof(pix_data)/sizeof(unsigned int) - 1 == iter2){ //if it reaches the end of the loop
  215.                     done = true;
  216.                 }
  217.             }
  218.             if((abs(int(previous_x - x)) < 50 || abs(int(previous_y - y))) < 50 && (!done)){
  219.                 //if the found pixels are within 50 pixels of the last found ones
  220.                 total_found++; //increment the total found
  221.                 pix_pos_full[pix_pos_counter][0] = pix_pos_piece[0]; //fill in the pix_pos array
  222.                 pix_pos_full[pix_pos_counter][1] = pix_pos_piece[1]; //with the x, y values of the current pixel
  223.             }
  224.             else{ //if they were not found within 50 pixels or not the right color
  225.                 total_found = 0; //reset the total found (needs to be a grouping to match a pattern)
  226.                 pix_pos_counter = 0; //reset the pix_pos_counter
  227.             }
  228.         }
  229.  
  230.         if(get_pixel_color){ //if the user wants to save pixels
  231.             if(abs(x - pos_x) < radius && abs(y - pos_y) < radius){ //if the x and y positions of the pixels are within radius amount of the center of the screen
  232.                 pixel_file << r << " " << g << " " << b << "\n"; //write the r g b to the file
  233.             }
  234.         }
  235.     }
  236.  
  237.     cout << total_found << endl; //for testing purposes, to see how many were found. Currently, either 0 or all of them are "found"
  238.     if(!get_pixel_color){ //make sure the user did not want to save pixels
  239.         if(total_found > 6){ //if more than 6 were found (arbitrary number, will be worked into percentage later)
  240.             int other_x_pos = pix_pos_full[(sizeof(pix_pos_full)/sizeof(int))/2][0]; //grab the center position
  241.             int other_y_pos = pix_pos_full[(sizeof(pix_pos_full)/sizeof(int))/2][1]; //of the pixel positions
  242.             //if(other_x_pos > pos_x){
  243.             //cout << other_x_pos << endl;
  244.  
  245.             //I am having trouble determining how much to move
  246.             //the mouse moves by x amount (the distance between pos_x and other_pos_x)
  247.             //but I can't determine it correctly
  248.             cout << pos_x - other_x_pos << endl;
  249.             cout << pos_y - other_y_pos << endl;
  250.             mouseMove(pos_x - other_x_pos, pos_y - other_y_pos);
  251.             //}
  252.             //else if(other_x_pos < pos_x){
  253.             //    mouseMove(other_x_pos - pos_x, other_y_pos - pos_y);
  254.             //}
  255.         }
  256.     }
  257.     total_found = 0; //reset the variables
  258.     pix_pos_counter = 0; //for the next screen grab
  259.     if(get_pixel_color){ //if the save-to file was opened
  260.         pixel_file.close(); //close it
  261.     }
  262.  
  263.     //clean up the bitmap and buffer unless you still need it
  264.     DeleteObject(hCaptureBitmap);
  265.     delete [] pPixels; // delete the array of objects
  266.  
  267.     ReleaseDC(ava_hwnd, ava_dc);
  268.     DeleteDC(ava_dc_cap);
  269.     DeleteObject(hCaptureBitmap);
  270.  
  271.     //int end = getMilliSpan(start);
  272.     //cout << end << endl;
  273. }
  274.  
  275. int main()
  276. {
  277.     //use for testing
  278.     /*RECT desktop_rect;
  279.     HWND desktop_hwnd;
  280.     desktop_hwnd = GetDesktopWindow();
  281.     GetWindowRect(desktop_hwnd, &desktop_rect);
  282.     while(true){
  283.         if((GetKeyState(VK_RETURN) & 0x80) != 0){
  284.             get_bitmap_ava(desktop_hwnd, desktop_rect.right, desktop_rect.bottom, true);
  285.             Sleep(500);
  286.         }
  287.         if((GetKeyState(VK_LBUTTON) & 0x80) != 0){
  288.             get_bitmap_ava(desktop_hwnd, desktop_rect.right, desktop_rect.bottom);
  289.         }
  290.         if((GetKeyState(VK_END) & 0x80) != 0){
  291.             pixel_file.close();
  292.             break;
  293.         }
  294.     }
  295.     return 0;*/
  296.     pixel_check.open("pixels.dat"); //the file that contains pixels to look for
  297.     string line; //a line to hold each set up numbers
  298.     if(pixel_check.is_open()){ //if the file open ok
  299.         vector<string> tokens; //to hold the 3 numbers as strings
  300.         unsigned int tokints[2]; //to hold the 3 numbers as ints
  301.         int counter = 0; //indexing for the pix_data array
  302.         while(getline(pixel_check,line)){ //get each line in the file
  303.             istringstream iss(line); //create a stringstream
  304.             copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(tokens)); //copy the sstream elements into tokens
  305.             for(unsigned int it = 0; it < tokens.size(); it++){
  306.                 tokints[it] = atoi(tokens.at(it).c_str()); //and convert/ copy tokens into tokints as ints
  307.             }
  308.             pix_data[counter][0] = tokints[0]; //fill pix_data
  309.             pix_data[counter][1] = tokints[1];
  310.             pix_data[counter][2] = tokints[2];
  311.             counter++;
  312.         }
  313.     }
  314.     pixel_check.close(); //close the file
  315.  
  316.     //normally the resolution is grabbed here, but I am hiding the game's name
  317.  
  318.     //there is also a block of code to check when the game is the active window
  319.     //I'm hiding that too
  320.     while(true){
  321.         if((GetKeyState(VK_RETURN) & 0x80) != 0){ //if the user presses enter
  322.             get_bitmap_ava(ava_wind, ScreenWidth, ScreenHeight, true); //screen read and save the pixels the center of the screen is on
  323.             Sleep(500);
  324.         }
  325.         if((GetKeyState(VK_LBUTTON) & 0x80) != 0){ //if the user left-clicks
  326.             get_bitmap_ava(ava_wind, ScreenWidth, ScreenHeight); //screen read and search for pixels
  327.             Sleep(100);
  328.         }
  329.         if((GetKeyState(VK_END) & 0x80) != 0){ //killswitch
  330.             break;
  331.         }
  332.     }
  333.     return 0;
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement