Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <iostream>
- #define Winver 0x500
- #include <tlhelp32.h>
- #include <stdio.h>
- #include <winable.h>
- #include <fstream>
- #include <istream>
- #include <string>
- #include <time.h>
- #include <sys/timeb.h>
- #include <vector>
- #include <algorithm>
- #include <sstream>
- #include <iterator>
- using namespace std;
- int ScreenWidth;
- int ScreenHeight;
- ofstream pixel_file;
- ifstream pixel_check;
- bool operator == (vector<unsigned int> const& v1, vector<unsigned int> const& v2){
- return (v1[0] == v2[0]) && (v1[1] == v2[1]) && (v1[2] == v2[2]);
- }
- int getMilliCount(){
- timeb tb;
- ftime(&tb);
- int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
- return nCount;
- }
- int getMilliSpan(int nTimeStart){
- int nSpan = getMilliCount() - nTimeStart;
- if(nSpan < 0)
- nSpan += 0x100000 * 1000;
- return nSpan;
- }
- void mouseMove(int x_coord, int y_coord)
- {
- INPUT mouse;
- memset(&mouse, 0, sizeof(INPUT));
- mouse.type = INPUT_MOUSE;
- // flag for the mouse hook to tell that it's a synthetic event.
- mouse.mi.dwExtraInfo = 0x200;
- mouse.mi.dx = x_coord;
- mouse.mi.dy = y_coord;
- mouse.mi.dwFlags = mouse.mi.dwFlags | MOUSEEVENTF_MOVE;
- SendInput(1, &mouse, sizeof(mouse));
- }
- vector< vector<unsigned int> > found_colors;
- vector< vector<int> > found_color_pos;
- vector< vector<unsigned int> > pixel_dat_colors;
- bool spec_find(unsigned int iter){
- for(unsigned int pix_iter = 0; pix_iter < pixel_dat_colors.size(); pix_iter++){
- if(abs(int(found_colors[iter][0] - pixel_dat_colors[pix_iter][0])) < 1
- || abs(int(found_colors[iter][1] - pixel_dat_colors[pix_iter][1])) < 1
- || abs(int(found_colors[iter][2] - pixel_dat_colors[pix_iter][2])) < 1){
- return true;
- }
- }
- return false;
- }
- void color_compare(int pos_x, int pos_y){
- vector< vector<int> > possible_pos;
- vector< vector<int> > temp_possible_pos;
- vector< vector<unsigned int> > previous_found_colors;
- int previous_pos_x = -1;
- int previous_pos_y = -1;
- int total_found = 0;
- for(unsigned int iter = 0; iter < found_colors.size(); iter++){
- unsigned int prev_col_iter;
- for(prev_col_iter = 0; prev_col_iter < previous_found_colors.size(); prev_col_iter++){
- if(previous_found_colors[prev_col_iter] == found_colors[iter]){
- break;
- }
- }
- if((prev_col_iter == previous_found_colors.size()) && spec_find(iter)){
- if(previous_pos_x == -1 && previous_pos_y == -1){
- previous_pos_x = found_color_pos[iter][0];
- previous_pos_y = found_color_pos[iter][1];
- }
- if((abs(found_color_pos[iter][0] - previous_pos_x) < 50) && (abs(found_color_pos[iter][1] - previous_pos_y) < 50)){
- previous_found_colors.push_back(found_colors[iter]);
- total_found++;
- previous_pos_x = found_color_pos[iter][0];
- previous_pos_y = found_color_pos[iter][1];
- possible_pos.push_back(found_color_pos[iter]);
- }
- else{
- if(total_found >= 4){
- break;
- }
- previous_found_colors.clear();
- possible_pos.clear();
- total_found = 0;
- }
- }
- else{
- if(total_found >= 4){
- break;
- }
- previous_found_colors.clear();
- possible_pos.clear();
- total_found = 0;
- }
- }
- //if(((total_found / pixel_dat_colors.size()) * 100) > 0){
- if(total_found > 6){
- int other_x_pos = possible_pos[possible_pos.size()/2][0];
- int other_y_pos = possible_pos[possible_pos.size()/2][1];
- //if(other_x_pos > pos_x){
- //cout << other_x_pos << endl;
- cout << pos_x - other_x_pos << endl;
- cout << pos_y - other_y_pos << endl;
- mouseMove(pos_x - other_x_pos, pos_y - other_y_pos);
- //}
- //else if(other_x_pos < pos_x){
- // mouseMove(other_x_pos - pos_x, other_y_pos - pos_y);
- //}
- }
- total_found = 0;
- }
- void get_bitmap_ava(HWND ava_hwnd, int size_x, int size_y, bool get_pixel_color = false)
- {
- if(get_pixel_color){
- pixel_file.open("pixels.dat");
- }
- found_colors.clear();
- found_color_pos.clear();
- int pos_x, pos_y;
- POINT p;
- GetCursorPos(&p);
- pos_x = size_x / 2; //crosshairs are always center screen
- pos_y = size_y / 2;
- //pos_x = p.x;
- //pos_y = p.y;
- int radius = 10;
- //int start = getMilliCount(); //track screenread speed in milliseconds
- RECT myRect;
- GetClientRect(ava_hwnd, (LPRECT)&myRect);
- ClientToScreen(ava_hwnd, (LPPOINT)&myRect.left);
- ClientToScreen(ava_hwnd, (LPPOINT)&myRect.top);
- HDC ava_dc = GetDC(ava_hwnd);
- HDC ava_dc_cap = CreateCompatibleDC(ava_dc);
- HBITMAP hCaptureBitmap = CreateCompatibleBitmap(ava_dc, size_x, size_y);
- SelectObject(ava_dc_cap, hCaptureBitmap);
- BitBlt(ava_dc_cap, 0, 0, size_x, size_y, ava_dc, 0, 0, SRCCOPY);
- //getting the size of the picture
- BITMAP bm;
- GetObject(hCaptureBitmap, sizeof(bm), &bm);
- int width(bm.bmWidth), height(bm.bmHeight);
- //creating a bitmapheader for getting the dibits
- BITMAPINFOHEADER bminfoheader;
- ::ZeroMemory(&bminfoheader, sizeof(BITMAPINFOHEADER));
- bminfoheader.biSize = sizeof(BITMAPINFOHEADER);
- bminfoheader.biWidth = width;
- bminfoheader.biHeight = -height;
- bminfoheader.biPlanes = 1;
- bminfoheader.biBitCount = 32;
- bminfoheader.biCompression = BI_RGB;
- bminfoheader.biSizeImage = width * 4 * height;
- bminfoheader.biClrUsed = 0;
- bminfoheader.biClrImportant = 0;
- DWORD dwBmpSize = ((size_x * bminfoheader.biBitCount + 31) / 32) * 4 * size_y;
- // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that
- // call HeapAlloc using a handle to the process's default heap. Therefore, GlobalAlloc and LocalAlloc
- // have greater overhead than HeapAlloc.
- HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
- char *lpbitmap = (char *)GlobalLock(hDIB);
- // Gets the "bits" from the bitmap and copies them into a buffer
- // which is pointed to by lpbitmap.
- GetDIBits(CreateCompatibleDC(0), hCaptureBitmap, 0,
- (UINT)bm.bmHeight,
- lpbitmap,
- (BITMAPINFO *)&bminfoheader, DIB_RGB_COLORS);
- // A file is created, this is where we will save the screen capture.
- HANDLE hFile = CreateFile("captureqwsx.bmp",
- GENERIC_WRITE,
- 0,
- NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL, NULL);
- // Add the size of the headers to the size of the bitmap to get the total file size
- DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- BITMAPFILEHEADER bmfh;
- //Offset to where the actual bitmap bits start.
- bmfh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
- //Size of the file
- bmfh.bfSize = dwSizeofDIB;
- //bfType must always be BM for Bitmaps
- bmfh.bfType = 0x4D42; //BM
- DWORD dwBytesWritten = 0;
- WriteFile(hFile, (LPSTR)&bmfh, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
- WriteFile(hFile, (LPSTR)&bminfoheader, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
- WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
- //Unlock and Free the DIB from the heap
- GlobalUnlock(hDIB);
- GlobalFree(hDIB);
- //Close the handle for the file that was created
- CloseHandle(hFile);
- //create a buffer and let the GetDIBits fill in the buffer
- unsigned char* pPixels = new unsigned char[(width * 4 * height)];
- if( !GetDIBits(CreateCompatibleDC(0), hCaptureBitmap, 0, height, pPixels, (BITMAPINFO*) &bminfoheader, DIB_RGB_COLORS)) // load pixel info
- {
- //return if fails but first delete the resources
- DeleteObject(hCaptureBitmap);
- delete [] pPixels; // delete the array of objects
- cout << "fail" << endl;
- return;
- }
- int x, y; // fill the x and y coordinate for grabbing pixels
- x = 0;
- y = 0;
- vector<unsigned int> RGB_colors;
- vector<int> RGB_pos;
- for(int iter = 0; iter < (size_x * size_y); iter++){
- if(x < size_x){
- x++;
- }
- else{
- x = 0;
- y++;
- }
- unsigned int r = pPixels[(width*y+x) * 4 + 2];
- unsigned int g = pPixels[(width*y+x) * 4 + 1];
- unsigned int b = pPixels[(width*y+x) * 4 + 0];
- RGB_colors.push_back(r);
- RGB_colors.push_back(g);
- RGB_colors.push_back(b);
- RGB_pos.push_back(x);
- RGB_pos.push_back(y);
- found_colors.push_back(RGB_colors);
- found_color_pos.push_back(RGB_pos);
- RGB_colors.clear();
- RGB_pos.clear();
- if(get_pixel_color){
- if(x - pos_x < radius && x - pos_x > -1*radius && y - pos_y < radius && y - pos_y > -1*radius ){
- pixel_file << r << " " << g << " " << b << "\n";
- }
- }
- }
- if(get_pixel_color){
- pixel_file.close();
- }
- if(!get_pixel_color){
- color_compare(pos_x, pos_y);
- }
- //clean up the bitmap and buffer unless you still need it
- DeleteObject(hCaptureBitmap);
- delete [] pPixels; // delete the array of objects
- ReleaseDC(ava_hwnd, ava_dc);
- DeleteDC(ava_dc_cap);
- DeleteObject(hCaptureBitmap);
- //int end = getMilliSpan(start);
- //cout << end << endl;
- }
- int main()
- {
- //use for testing
- /*RECT desktop_rect;
- HWND desktop_hwnd;
- desktop_hwnd = GetDesktopWindow();
- GetWindowRect(desktop_hwnd, &desktop_rect);
- while(true){
- if((GetKeyState(VK_RETURN) & 0x80) != 0){
- get_bitmap_ava(desktop_hwnd, desktop_rect.right, desktop_rect.bottom, true);
- Sleep(500);
- }
- if((GetKeyState(VK_LBUTTON) & 0x80) != 0){
- get_bitmap_ava(desktop_hwnd, desktop_rect.right, desktop_rect.bottom);
- }
- if((GetKeyState(VK_END) & 0x80) != 0){
- pixel_file.close();
- break;
- }
- }
- return 0;*/
- pixel_check.open("pixels.dat");
- string line;
- if(pixel_check.is_open()){
- vector<string> tokens;
- vector<unsigned int> tokints;
- while(getline(pixel_check,line)){
- istringstream iss(line);
- copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(tokens));
- for(unsigned int it = 0; it < tokens.size(); it++){
- tokints.push_back(atoi(tokens.at(it).c_str()));
- }
- pixel_dat_colors.push_back(tokints);
- tokens.clear();
- tokints.clear();
- }
- }
- pixel_check.close();
- ifstream ava_settings("C:/AeriaGames/AVA/avaGame/Config/AVAOptionSettings.ini");
- size_t found;
- int file_counter = 0;
- int ScreenWidth = 0;
- int ScreenHeight = 0;
- if(ava_settings.is_open()){
- while(getline(ava_settings,line) && file_counter < 3){
- found = line.find("=");
- if(found != string::npos){
- line.replace(0, found+1,"");
- if(file_counter == 1){
- ScreenWidth = int(atof(line.c_str()));
- }
- else if(file_counter == 2){
- ScreenHeight = int(atof(line.c_str()));
- }
- }
- file_counter++;
- }
- ava_settings.close();
- }
- TCHAR title[500];
- HWND ava_wind;
- while(true){
- if(GetWindowText(GetForegroundWindow(), title, 500) == 24){
- ava_wind = GetActiveWindow();
- break;
- }
- Sleep(100);
- }
- while(true){
- if((GetKeyState(VK_RETURN) & 0x80) != 0){ //if the user presses enter
- get_bitmap_ava(ava_wind, ScreenWidth, ScreenHeight, true); //screen read and print the color of the pixel the mouse is on
- Sleep(500);
- }
- if((GetKeyState(VK_LBUTTON) & 0x80) != 0){ //if the user left-clicks
- get_bitmap_ava(ava_wind, ScreenWidth, ScreenHeight); //screen read and search for enemies
- Sleep(100);
- }
- if((GetKeyState(VK_END) & 0x80) != 0){ //killswitch
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement