Advertisement
asqapro

Aimbot AVA

Nov 5th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.45 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3. #define Winver 0x500
  4. #include <tlhelp32.h>
  5. #include <stdio.h>
  6. #include <winable.h>
  7. #include <fstream>
  8. #include <istream>
  9. #include <string>
  10. #include <time.h>
  11. #include <sys/timeb.h>
  12. #include <vector>
  13. #include <algorithm>
  14. #include <sstream>
  15. #include <iterator>
  16.  
  17. using namespace std;
  18.  
  19. int ScreenWidth;
  20. int ScreenHeight;
  21.  
  22. ofstream pixel_file;
  23. ifstream pixel_check;
  24.  
  25. bool operator == (vector<unsigned int> const& v1, vector<unsigned int> const& v2){
  26.     return (v1[0] == v2[0]) && (v1[1] == v2[1]) && (v1[2] == v2[2]);
  27. }
  28.  
  29. int getMilliCount(){
  30.     timeb tb;
  31.     ftime(&tb);
  32.     int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
  33.     return nCount;
  34. }
  35.  
  36. int getMilliSpan(int nTimeStart){
  37.     int nSpan = getMilliCount() - nTimeStart;
  38.     if(nSpan < 0)
  39.         nSpan += 0x100000 * 1000;
  40.     return nSpan;
  41. }
  42.  
  43. void mouseMove(int x_coord, int y_coord)
  44. {
  45.     INPUT mouse;
  46.     memset(&mouse, 0, sizeof(INPUT));
  47.     mouse.type = INPUT_MOUSE;
  48.     // flag for the mouse hook to tell that it's a synthetic event.
  49.     mouse.mi.dwExtraInfo = 0x200;
  50.     mouse.mi.dx =  x_coord;
  51.     mouse.mi.dy = y_coord;
  52.     mouse.mi.dwFlags = mouse.mi.dwFlags | MOUSEEVENTF_MOVE;
  53.     SendInput(1, &mouse, sizeof(mouse));
  54. }
  55.  
  56. vector< vector<unsigned int> > found_colors;
  57. vector< vector<int> > found_color_pos;
  58.  
  59. vector< vector<unsigned int> > pixel_dat_colors;
  60.  
  61. bool spec_find(unsigned int iter){
  62.     for(unsigned int pix_iter = 0; pix_iter < pixel_dat_colors.size(); pix_iter++){
  63.         if(abs(int(found_colors[iter][0] - pixel_dat_colors[pix_iter][0])) < 1
  64.         || abs(int(found_colors[iter][1] - pixel_dat_colors[pix_iter][1])) < 1
  65.         || abs(int(found_colors[iter][2] - pixel_dat_colors[pix_iter][2])) < 1){
  66.             return true;
  67.         }
  68.     }
  69.     return false;
  70. }
  71.  
  72. void color_compare(int pos_x, int pos_y){
  73.     vector< vector<int> > possible_pos;
  74.     vector< vector<int> > temp_possible_pos;
  75.  
  76.     vector< vector<unsigned int> > previous_found_colors;
  77.  
  78.     int previous_pos_x = -1;
  79.     int previous_pos_y = -1;
  80.     int total_found = 0;
  81.     for(unsigned int iter = 0; iter < found_colors.size(); iter++){
  82.         unsigned int prev_col_iter;
  83.         for(prev_col_iter = 0; prev_col_iter < previous_found_colors.size(); prev_col_iter++){
  84.             if(previous_found_colors[prev_col_iter] == found_colors[iter]){
  85.                 break;
  86.             }
  87.         }
  88.         if((prev_col_iter == previous_found_colors.size()) && spec_find(iter)){
  89.             if(previous_pos_x == -1 && previous_pos_y == -1){
  90.                 previous_pos_x = found_color_pos[iter][0];
  91.                 previous_pos_y = found_color_pos[iter][1];
  92.             }
  93.             if((abs(found_color_pos[iter][0] - previous_pos_x) < 50) && (abs(found_color_pos[iter][1] - previous_pos_y) < 50)){
  94.                 previous_found_colors.push_back(found_colors[iter]);
  95.                 total_found++;
  96.                 previous_pos_x = found_color_pos[iter][0];
  97.                 previous_pos_y = found_color_pos[iter][1];
  98.                 possible_pos.push_back(found_color_pos[iter]);
  99.             }
  100.             else{
  101.                 if(total_found >= 4){
  102.                     break;
  103.                 }
  104.                 previous_found_colors.clear();
  105.                 possible_pos.clear();
  106.                 total_found = 0;
  107.             }
  108.         }
  109.         else{
  110.             if(total_found >= 4){
  111.                 break;
  112.             }
  113.             previous_found_colors.clear();
  114.             possible_pos.clear();
  115.             total_found = 0;
  116.         }
  117.     }
  118.     //if(((total_found / pixel_dat_colors.size()) * 100) > 0){
  119.     if(total_found > 6){
  120.         int other_x_pos = possible_pos[possible_pos.size()/2][0];
  121.         int other_y_pos = possible_pos[possible_pos.size()/2][1];
  122.         //if(other_x_pos > pos_x){
  123.         //cout << other_x_pos << endl;
  124.         cout << pos_x - other_x_pos << endl;
  125.         cout << pos_y - other_y_pos << endl;
  126.             mouseMove(pos_x - other_x_pos, pos_y - other_y_pos);
  127.         //}
  128.         //else if(other_x_pos < pos_x){
  129.         //    mouseMove(other_x_pos - pos_x, other_y_pos - pos_y);
  130.         //}
  131.  
  132.     }
  133.     total_found = 0;
  134. }
  135.  
  136. void get_bitmap_ava(HWND ava_hwnd, int size_x, int size_y, bool get_pixel_color = false)
  137. {
  138.     if(get_pixel_color){
  139.         pixel_file.open("pixels.dat");
  140.     }
  141.     found_colors.clear();
  142.     found_color_pos.clear();
  143.     int pos_x, pos_y;
  144.     POINT p;
  145.     GetCursorPos(&p);
  146.     pos_x = size_x / 2; //crosshairs are always center screen
  147.     pos_y = size_y / 2;
  148.     //pos_x = p.x;
  149.     //pos_y = p.y;
  150.  
  151.     int radius = 10;
  152.  
  153.     //int start = getMilliCount(); //track screenread speed in milliseconds
  154.  
  155.     RECT myRect;
  156.     GetClientRect(ava_hwnd, (LPRECT)&myRect);
  157.     ClientToScreen(ava_hwnd, (LPPOINT)&myRect.left);
  158.     ClientToScreen(ava_hwnd, (LPPOINT)&myRect.top);
  159.  
  160.     HDC ava_dc = GetDC(ava_hwnd);
  161.     HDC ava_dc_cap = CreateCompatibleDC(ava_dc);
  162.     HBITMAP hCaptureBitmap = CreateCompatibleBitmap(ava_dc, size_x, size_y);
  163.     SelectObject(ava_dc_cap, hCaptureBitmap);
  164.     BitBlt(ava_dc_cap, 0, 0, size_x, size_y, ava_dc, 0, 0, SRCCOPY);
  165.  
  166.     //getting the size of the picture
  167.     BITMAP bm;
  168.     GetObject(hCaptureBitmap, sizeof(bm), &bm);
  169.     int width(bm.bmWidth), height(bm.bmHeight);
  170.  
  171.     //creating a bitmapheader for getting the dibits
  172.     BITMAPINFOHEADER bminfoheader;
  173.     ::ZeroMemory(&bminfoheader, sizeof(BITMAPINFOHEADER));
  174.     bminfoheader.biSize        = sizeof(BITMAPINFOHEADER);
  175.     bminfoheader.biWidth       = width;
  176.     bminfoheader.biHeight      = -height;
  177.     bminfoheader.biPlanes      = 1;
  178.     bminfoheader.biBitCount    = 32;
  179.     bminfoheader.biCompression = BI_RGB;
  180.  
  181.     bminfoheader.biSizeImage = width * 4 * height;
  182.     bminfoheader.biClrUsed = 0;
  183.     bminfoheader.biClrImportant = 0;
  184.  
  185.  
  186.  
  187.  
  188.     DWORD dwBmpSize = ((size_x * bminfoheader.biBitCount + 31) / 32) * 4 * size_y;
  189.  
  190.     // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that
  191.     // call HeapAlloc using a handle to the process's default heap. Therefore, GlobalAlloc and LocalAlloc
  192.     // have greater overhead than HeapAlloc.
  193.     HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
  194.     char *lpbitmap = (char *)GlobalLock(hDIB);
  195.  
  196.     // Gets the "bits" from the bitmap and copies them into a buffer
  197.     // which is pointed to by lpbitmap.
  198.     GetDIBits(CreateCompatibleDC(0), hCaptureBitmap, 0,
  199.         (UINT)bm.bmHeight,
  200.         lpbitmap,
  201.         (BITMAPINFO *)&bminfoheader, DIB_RGB_COLORS);
  202.  
  203.     // A file is created, this is where we will save the screen capture.
  204.     HANDLE hFile = CreateFile("captureqwsx.bmp",
  205.         GENERIC_WRITE,
  206.         0,
  207.         NULL,
  208.         CREATE_ALWAYS,
  209.         FILE_ATTRIBUTE_NORMAL, NULL);
  210.  
  211.  
  212.     // Add the size of the headers to the size of the bitmap to get the total file size
  213.     DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  214.  
  215.     BITMAPFILEHEADER bmfh;
  216.  
  217.  
  218.     //Offset to where the actual bitmap bits start.
  219.     bmfh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
  220.  
  221.  
  222.     //Size of the file
  223.     bmfh.bfSize = dwSizeofDIB;
  224.  
  225.  
  226.     //bfType must always be BM for Bitmaps
  227.     bmfh.bfType = 0x4D42; //BM
  228.  
  229.  
  230.     DWORD dwBytesWritten = 0;
  231.     WriteFile(hFile, (LPSTR)&bmfh, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
  232.     WriteFile(hFile, (LPSTR)&bminfoheader, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
  233.     WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
  234.  
  235.  
  236.     //Unlock and Free the DIB from the heap
  237.     GlobalUnlock(hDIB);
  238.     GlobalFree(hDIB);
  239.  
  240.     //Close the handle for the file that was created
  241.     CloseHandle(hFile);
  242.  
  243.  
  244.  
  245.     //create a buffer and let the GetDIBits fill in the buffer
  246.     unsigned char* pPixels = new unsigned char[(width * 4 * height)];
  247.     if( !GetDIBits(CreateCompatibleDC(0), hCaptureBitmap, 0, height, pPixels, (BITMAPINFO*) &bminfoheader, DIB_RGB_COLORS)) // load pixel info
  248.     {
  249.         //return if fails but first delete the resources
  250.         DeleteObject(hCaptureBitmap);
  251.         delete [] pPixels; // delete the array of objects
  252.         cout << "fail" << endl;
  253.  
  254.         return;
  255.     }
  256.  
  257.     int x, y; // fill the x and y coordinate for grabbing pixels
  258.     x = 0;
  259.     y = 0;
  260.  
  261.     vector<unsigned int> RGB_colors;
  262.     vector<int> RGB_pos;
  263.  
  264.     for(int iter = 0; iter < (size_x * size_y); iter++){
  265.         if(x < size_x){
  266.             x++;
  267.         }
  268.         else{
  269.             x = 0;
  270.             y++;
  271.         }
  272.         unsigned int r = pPixels[(width*y+x) * 4 + 2];
  273.         unsigned int g = pPixels[(width*y+x) * 4 + 1];
  274.         unsigned int b = pPixels[(width*y+x) * 4 + 0];
  275.  
  276.         RGB_colors.push_back(r);
  277.         RGB_colors.push_back(g);
  278.         RGB_colors.push_back(b);
  279.         RGB_pos.push_back(x);
  280.         RGB_pos.push_back(y);
  281.  
  282.         found_colors.push_back(RGB_colors);
  283.         found_color_pos.push_back(RGB_pos);
  284.  
  285.         RGB_colors.clear();
  286.         RGB_pos.clear();
  287.  
  288.         if(get_pixel_color){
  289.             if(x - pos_x < radius && x - pos_x > -1*radius && y - pos_y < radius && y - pos_y > -1*radius ){
  290.                 pixel_file << r << " " << g << " " << b << "\n";
  291.             }
  292.         }
  293.     }
  294.     if(get_pixel_color){
  295.         pixel_file.close();
  296.     }
  297.     if(!get_pixel_color){
  298.         color_compare(pos_x, pos_y);
  299.     }
  300.  
  301.     //clean up the bitmap and buffer unless you still need it
  302.     DeleteObject(hCaptureBitmap);
  303.     delete [] pPixels; // delete the array of objects
  304.  
  305.     ReleaseDC(ava_hwnd, ava_dc);
  306.     DeleteDC(ava_dc_cap);
  307.     DeleteObject(hCaptureBitmap);
  308.  
  309.     //int end = getMilliSpan(start);
  310.     //cout << end << endl;
  311. }
  312.  
  313. int main()
  314. {
  315.     //use for testing
  316.     /*RECT desktop_rect;
  317.     HWND desktop_hwnd;
  318.     desktop_hwnd = GetDesktopWindow();
  319.     GetWindowRect(desktop_hwnd, &desktop_rect);
  320.     while(true){
  321.         if((GetKeyState(VK_RETURN) & 0x80) != 0){
  322.             get_bitmap_ava(desktop_hwnd, desktop_rect.right, desktop_rect.bottom, true);
  323.             Sleep(500);
  324.         }
  325.         if((GetKeyState(VK_LBUTTON) & 0x80) != 0){
  326.             get_bitmap_ava(desktop_hwnd, desktop_rect.right, desktop_rect.bottom);
  327.         }
  328.         if((GetKeyState(VK_END) & 0x80) != 0){
  329.             pixel_file.close();
  330.             break;
  331.         }
  332.     }
  333.     return 0;*/
  334.     pixel_check.open("pixels.dat");
  335.     string line;
  336.     if(pixel_check.is_open()){
  337.         vector<string> tokens;
  338.         vector<unsigned int> tokints;
  339.         while(getline(pixel_check,line)){
  340.             istringstream iss(line);
  341.             copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(tokens));
  342.             for(unsigned int it = 0; it < tokens.size(); it++){
  343.                 tokints.push_back(atoi(tokens.at(it).c_str()));
  344.             }
  345.             pixel_dat_colors.push_back(tokints);
  346.             tokens.clear();
  347.             tokints.clear();
  348.         }
  349.     }
  350.     pixel_check.close();
  351.  
  352.     ifstream ava_settings("C:/AeriaGames/AVA/avaGame/Config/AVAOptionSettings.ini");
  353.     size_t found;
  354.     int file_counter = 0;
  355.     int ScreenWidth = 0;
  356.     int ScreenHeight = 0;
  357.     if(ava_settings.is_open()){
  358.         while(getline(ava_settings,line) && file_counter < 3){
  359.             found = line.find("=");
  360.             if(found != string::npos){
  361.                 line.replace(0, found+1,"");
  362.                 if(file_counter == 1){
  363.                     ScreenWidth = int(atof(line.c_str()));
  364.                 }
  365.                 else if(file_counter == 2){
  366.                     ScreenHeight = int(atof(line.c_str()));
  367.                 }
  368.             }
  369.             file_counter++;
  370.         }
  371.         ava_settings.close();
  372.     }
  373.     TCHAR title[500];
  374.     HWND ava_wind;
  375.     while(true){
  376.         if(GetWindowText(GetForegroundWindow(), title, 500) == 24){
  377.             ava_wind = GetActiveWindow();
  378.             break;
  379.         }
  380.         Sleep(100);
  381.     }
  382.     while(true){
  383.         if((GetKeyState(VK_RETURN) & 0x80) != 0){ //if the user presses enter
  384.             get_bitmap_ava(ava_wind, ScreenWidth, ScreenHeight, true); //screen read and print the color of the pixel the mouse is on
  385.             Sleep(500);
  386.         }
  387.         if((GetKeyState(VK_LBUTTON) & 0x80) != 0){ //if the user left-clicks
  388.             get_bitmap_ava(ava_wind, ScreenWidth, ScreenHeight); //screen read and search for enemies
  389.             Sleep(100);
  390.         }
  391.         if((GetKeyState(VK_END) & 0x80) != 0){ //killswitch
  392.             break;
  393.         }
  394.     }
  395.     return 0;
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement