Author Topic: C++ Bot, Color Problem (No .NET)  (Read 3338 times)

tvirusx1

  • Guest
C++ Bot, Color Problem (No .NET)
« on: December 03, 2009, 03:13:15 am »
I made a Bot for the Game "Dark Orbit" and it doesn't use your cursor :D, but heres the Problem:
It won't find the Color of the Bonus Boxes(they are rotating).

I tried over 150 Colors, without success... now I am thinking that I would need a sort of Color Tolerance? But how would I do that sort of thing? Or is this Possible by Picking the right Color? It's not my Code that is wrong so thats out of question, it's for definite that it's the Color.
Everything works fine but it just flies past the Bonus Boxes.(the Program has 2-4 Seconds time to find and Click the Color, depending on the ship.)

Now I need help :(.
I know most People are lazy when it comes to helping someone. I don't ask you to register at Dark Orbit just so you can help me, thats why I got a Picture of Bonus Boxes.

I hope someone can Pick a Color on the Bonus Box which is used most often. I tried too many times already, I'm not experienced with Colors...
Heres the Pic of some Bonus Boxes:

There are two ways you can help me with this:
1.)Pick the Color used most often.
or
2.)Help me make a Color Tolerance in C++.

I just hope someone can help me within a week.
Posted on: December 03, 2009, 01:17:58 AM
Sorry but I don't know how to Edit my Post.

You can use Scar's Color Picker to Pick a Color for me by the way.

Freddy1990.com

C++ Bot, Color Problem (No .NET)
« on: December 03, 2009, 03:13:15 am »

Offline Freddy

  • Owner
  • *****
  • Posts: 2614
  • Rep: 19
    • MSN Messenger - freddy1990@gmail.com
    • AOL Instant Messenger - Freddy199O
    • View Profile
    • Email
Re: C++ Bot, Color Problem (No .NET)
« Reply #1 on: December 03, 2009, 05:09:49 am »
You could convert the tolerance algorithm in this post to c++ and use that (Similarcolors): http://freddy1990.com/forums/index.php?topic=8.0

Freddy1990.com

Re: C++ Bot, Color Problem (No .NET)
« Reply #1 on: December 03, 2009, 05:09:49 am »

tvirusx1

  • Guest
Re: C++ Bot, Color Problem (No .NET)
« Reply #2 on: December 03, 2009, 05:27:33 am »
I don't use GetPixel() it's too slow, I use a Pointer to the Screen which is like 100x faster than GetPixel() it's really fast.

My technique works like this:
Code: [Select]
while(x!=Width && y!=Height)
{
    ReadPixel(x,y);
    if(c == TheColor)
        do();
    if(x==Width)
    {
        x = 0;
        y++;
    }
    x++;
}
ReadPixel(x,y);
returns the color of the screen Pixel in a Pointer.
You created Scar so I don't think I have to explain any further ;).
Posted on: December 03, 2009, 05:26:05 AM
Or your just a donator? I dunno but it seems like you understand a lot of Programming.
(finally found out how to edit)

Code: [Select]
function SimilarColors(Color1, Color2, Tolerance: Integer): Boolean;
begin
  Result := ((Abs((Color1 and $ff) - (Color2 and $ff)) <= Tolerance) and
    (Abs(((Color1 and $ff00) shr 8) - ((Color2 and $ff00) shr 8)) <= Tolerance) and
    (Abs(((Color1 and $ff0000) shr 16) - ((Color2 and $ff0000) shr 16)) <= Tolerance));
end;

I'm a noob at Pascal... or is this Delphi?

What is:
"Abs"
"shr"
"$ff   //is that Hex? 0xFF?
"and" //The code confuses me is that supposed to be "&&" in C++?


Heres my C++ attempt:
Code: [Select]
BOOL SimilarColor(COLORREF Color1, COLORREF Color2, COLORREF Tolerance)
{
    COLORREF min = (Color2 - Tolerance);
    COLORREF max = (Color2 + Tolerance);

    if(Color1 == Color2)
        return true; //same Color So return true

    if(Color1 >= min && Color1 <=max)
        return true; //similar color to Color2

    if(Color1 < min || Color1 > max)
        return false; //far off the tolerance

return false; //we shouldn't even get here, something gone wrong so return false.
}

Would that work?
« Last Edit: December 03, 2009, 11:42:11 pm by tvirusx1 »

Offline Freddy

  • Owner
  • *****
  • Posts: 2614
  • Rep: 19
    • MSN Messenger - freddy1990@gmail.com
    • AOL Instant Messenger - Freddy199O
    • View Profile
    • Email
Re: C++ Bot, Color Problem (No .NET)
« Reply #3 on: December 04, 2009, 08:49:33 am »
I'm the author of SCAR Divi.

http://www.cplusplus.com/reference/clibrary/cstdlib/abs/
shr is >>
$FF is 0xFF
and is &&

That example you posted wouldn't work at all, unless your color with tolerance is a value in between 0 and 255, but a 24-bit color value is 0 to 16777215
« Last Edit: December 04, 2009, 08:51:57 am by Freddy1990 »

tvirusx1

  • Guest
Re: C++ Bot, Color Problem (No .NET)
« Reply #4 on: December 04, 2009, 01:36:20 pm »
Wow your the Author, nice.

Thanks a lot for your help, I'll try it out tomorrow as soon as I can.

Code: [Select]
BOOL SimilarColors(int Color1, int Color2, int Tolerance)
{
return ((abs((Color1 && 0xFF) - (Color2 && 0xFF)) <= Tolerance) &&
    (abs(((Color1 && 0xFF00) >> 8) - ((Color2 && 0xFF00) >> 8)) <= Tolerance) &&
    (abs(((Color1 && 0xFF0000) >> 16) - ((Color2 && 0xFF0000) >> 16)) <= Tolerance));
}

Code: [Select]
void ClickBonusBox()
{
ic = 0;
if(SimilarColors(c,Colour[0],10)==true) //RGB(r,g,b) or //c==23456
{
ClickMouse(x,y);
Sleep(3000);
ClickMouse(Random(654,801),Random(548,642)); // random click on map

    x=816;
    y=517;
}
}

I have a feeling that SimilarColors() doesn't work properly.
I think I am doing something wrong...

It clicks everywhere....
The code is alright so it should work properly.

If you can't find any mistakes or bugs here could I Pm you my entire code?
If there isn't a mistake in the two functions above it must be somewhere in my code.

Always when my Program finds a Color it's never the Bonus Box, or it doesn't find the Color at all.

Someone else told me that using A Bitmap would return BGR instead of RGB, is that true? I made in my code a SwapColor function for that to change RGB to BGR.
I just hope you can help me :( I'm sick of this and I want to get it done as soon as possible.
« Last Edit: December 04, 2009, 11:53:52 pm by tvirusx1 »

Freddy1990.com

Re: C++ Bot, Color Problem (No .NET)
« Reply #4 on: December 04, 2009, 01:36:20 pm »

Offline Freddy

  • Owner
  • *****
  • Posts: 2614
  • Rep: 19
    • MSN Messenger - freddy1990@gmail.com
    • AOL Instant Messenger - Freddy199O
    • View Profile
    • Email
Re: C++ Bot, Color Problem (No .NET)
« Reply #5 on: December 05, 2009, 02:59:36 am »
Someone else told me that using A Bitmap would return BGR instead of RGB, is that true? I made in my code a SwapColor function for that to change RGB to BGR.
I just hope you can help me :( I'm sick of this and I want to get it done as soon as possible.
Yes, bitmaps are stored as BGRA. It's a simple matter of using the right structure to retrieve the pixel data...
« Last Edit: December 05, 2009, 04:30:05 am by Freddy1990 »

tvirusx1

  • Guest
Re: C++ Bot, Color Problem (No .NET)
« Reply #6 on: December 06, 2009, 12:17:33 am »
Any Idea how I could solve it? Heres my code:

Code: [Select]
#include <iostream>
#include <windows.h>
#include <ctime>
#include <time.h>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int x, y;
int Cm = 253;
int ClickOnMap;
int redValue, greenValue, blueValue;
int connection;
int FirstMapClick;
int FleeFromEnemy;
int EnemyGone;
int ic;
static bool RandomSeed = true;
//HDC and hWnd
COLORREF c;
HDC hdcScreen;
COLORREF ReadPixel(int x, int y);
DWORD* bits = NULL;
COLORREF crPixel;
COLORREF SetRed = 2366701;
HWND hWnd;
//Colours
COLORREF Colour[25];
//minmax
COLORREF rmax,rmin,gmax,gmin,bmax,bmin;

int Random(int RandMin, int RandMax) //my random number generator
{
if(RandomSeed)
{
RandomSeed = false;
srand((unsigned) time(NULL));
}
    int GeneratedNumber = RandMin + rand() % (RandMax - RandMin + 1);
return (GeneratedNumber);
}

int GetHandle()
{
HWND MainHandle = FindWindow(L"WindowsForms10.Window.8.app.0.33c0d9d", NULL);
if(!MainHandle)
return 10;

HWND Child1 = FindWindowEx(MainHandle, NULL, L"WindowsForms10.SysTabControl32.app.0.33c0d9d", NULL);
if(!Child1)
return 1;

HWND Child2 = FindWindowEx(Child1, NULL, L"WindowsForms10.Window.8.app.0.33c0d9d", L"DO Play");
if(!Child2)
return 2;

HWND Child3 = FindWindowEx(Child2, NULL, L"WindowsForms10.Window.8.app.0.33c0d9d", NULL);
if(!Child3)
return 3;

HWND Child4 = FindWindowEx(Child3, NULL, L"Shell Embedding", NULL);
if(!Child4)
return 4;

HWND Child5 = FindWindowEx(Child4, NULL, L"Shell DocObject View", NULL);
if(!Child5)
return 5;

HWND Child6 = FindWindowEx(Child5, NULL, L"Internet Explorer_Server", NULL);
if(!Child6)
return 6;

hWnd = FindWindowEx(Child6, NULL, L"MacromediaFlashPlayerActiveX", NULL);
if(!hWnd)
return 7;
return 0;
}

void ClickMouse(int a, int b)
{
SetActiveWindow(hWnd);
SetFocus(hWnd);
PostMessage(hWnd,WM_MOUSEMOVE, NULL, MAKELONG(a,b));
PostMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(a,b));
Sleep(100);
PostMessage(hWnd, WM_LBUTTONUP, 0x00000001, MAKELONG(a,b));
}

void AntiBan()
{
if(Random(0,10) == Random(0,10))
{
if(Random(1,3) == 1)
{
ClickMouse(476,595);//Laser Menu
Sleep(Random(1000,2000));
}
if(Random(1,3) == 2)
{
ClickMouse(524,600);//Rocket Menu
Sleep(Random(1000,2000));
}
if(Random(1,3) == 3)
{
ClickMouse(621,602);//Extras Menu
Sleep(Random(1000,2000));
}
Sleep(10);
}
}

void CheckForEnemyPlayers()
{
    hdcScreen = GetDC(hWnd);
crPixel = GetPixel(hdcScreen,811,645);
ReleaseDC(0,hdcScreen);
redValue = GetRValue(crPixel);
    greenValue = GetGValue(crPixel);
    blueValue = GetBValue(crPixel);

while(redValue != 225 && greenValue != 240 && blueValue != 255)
{
if(FleeFromEnemy == 0)
{
    ClickMouse(Random(658,662),Random(551,555));
FleeFromEnemy = 1;
}
AntiBan();
hdcScreen = GetDC(hWnd);
    crPixel = GetPixel(hdcScreen,811,645);
    ReleaseDC(0,hdcScreen);
    redValue = GetRValue(crPixel);
        greenValue = GetGValue(crPixel);
        blueValue = GetBValue(crPixel);
Sleep(Random(2000,4000));
EnemyGone = 1;

}
FleeFromEnemy = 0;
if(EnemyGone == 1)
{
EnemyGone = 0;
ClickMouse(Random(654,801),Random(548,642)); // random click on map
}
}

BOOL SimilarColors(int Color1, int Color2, int Tolerance)
{
return ((abs((Color1 && 0xFF) - (Color2 && 0xFF)) <= Tolerance) &&
    (abs(((Color1 && 0xFF00) >> 8) - ((Color2 && 0xFF00) >> 8)) <= Tolerance) &&
    (abs(((Color1 && 0xFF0000) >> 16) - ((Color2 && 0xFF0000) >> 16)) <= Tolerance));
}

void ClickBonusBox()
{
ic = 0;
if(SimilarColors(c,Colour[0],5)==true) //RGB(r,g,b) or //c==23456
{
ClickMouse(x,y);
Sleep(6000);
ClickMouse(Random(654,801),Random(548,642)); // random click on map
    x=816;
    y=517;
}
}

void SwapCol(DWORD* ptr)
{
   *ptr = (*ptr & 0x000000FF) << 16 | (*ptr & 0x0000FF00) | (*ptr & 0x00FF0000) >> 16;
}

COLORREF ReadPixel(int x, int y)
{
   DWORD* p = bits + (y * 817 + x);
   return *p;
}

void ScanPixels()
{
x = 0;
y = 73;
BITMAPINFO bi = { 0 };

    bi.bmiHeader.biSize = sizeof(BITMAPINFO);
    bi.bmiHeader.biWidth = 817;
    bi.bmiHeader.biHeight = -517; //top down
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 32;

    HDC hdc = GetDC(hWnd); //0 is the entire screen
    HDC hdcMem = CreateCompatibleDC(hdc);
    HBITMAP hDib = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
    HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hDib);

    BitBlt(hdcMem, 0, 0, 817, 517, hdc, 0, 0, SRCCOPY);
   //hdcMem is the destination, hdc is the source

   //we have all the pixel data now, to access it, just make a pointer to it.
   while(x!=817 && y!=517)
   {
       c = ReadPixel(x, y);
   SwapCol(&c);
   ClickBonusBox();
   if(x == 817)
   {
   x = 0;
   y++;
   }
   x++;
   }

DeleteObject(hDib);
DeleteObject(hbmOld);
ReleaseDC(hWnd,hdc);
ReleaseDC(hWnd,hdcMem);
DeleteDC(hdc);
DeleteDC(hdcMem);
}

void InitializeColors()
{
ic = 0;
int r = 245;
int g = 245;
int b = 199;
Colour[0] = RGB(r,g,b);
}

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
InitializeColors();
while(!hWnd)
{
GetHandle();
}

Sleep(1000);
ClickMouse(395,627); //Click "Start"
Sleep(1000);

while(!hWnd)
{
GetHandle();
}

while(connection != 1)
{
hdcScreen = GetDC(hWnd);
crPixel = GetPixel(hdcScreen,14,615);
ReleaseDC(0,hdcScreen);
redValue = GetRValue(crPixel);
        greenValue = GetGValue(crPixel);
        blueValue = GetBValue(crPixel);
if(redValue == 21 && greenValue == 63 && blueValue == 42)
{
connection = 1;
}
}

while(1)//MAIN
{
CheckForEnemyPlayers();
if(FirstMapClick == 0)
{
ClickMouse(Random(654,801),Random(548,642)); // random click on map
FirstMapClick = 1;
}
ScanPixels();
ClickOnMap++;

if(ClickOnMap == 30000)
{
ClickMouse(Random(654,801),Random(548,642)); // random click on map
ClickOnMap = 0;
}
}
return 0;
}

Freddy1990.com

Re: C++ Bot, Color Problem (No .NET)
« Reply #6 on: December 06, 2009, 12:17:33 am »

Offline Freddy

  • Owner
  • *****
  • Posts: 2614
  • Rep: 19
    • MSN Messenger - freddy1990@gmail.com
    • AOL Instant Messenger - Freddy199O
    • View Profile
    • Email
Re: C++ Bot, Color Problem (No .NET)
« Reply #7 on: December 06, 2009, 03:31:17 am »
Been a while since I've done any of this kind of stuff in c++, hmm, but can't you use this? http://msdn.microsoft.com/en-us/library/dd162938%28VS.85%29.aspx I know it works for bitmap data...

tvirusx1

  • Guest
Re: C++ Bot, Color Problem (No .NET)
« Reply #8 on: December 06, 2009, 05:16:18 am »
I'll try that out.

EDIT:
No success, there are two types of tagRGBQUAD.

1. tagRGBQUAD(); << it takes no parameters
2. tagRGBQUAD(const tagRGBQUAD &);

How would I use this in my code?
« Last Edit: December 06, 2009, 11:32:09 pm by tvirusx1 »

Offline Freddy

  • Owner
  • *****
  • Posts: 2614
  • Rep: 19
    • MSN Messenger - freddy1990@gmail.com
    • AOL Instant Messenger - Freddy199O
    • View Profile
    • Email
Re: C++ Bot, Color Problem (No .NET)
« Reply #9 on: December 07, 2009, 05:40:41 am »
Normally you can cast a pointer to the bitmap to the RGBQuad struct...

You could try to just convert the colors from the bitmap to regular color values, this is how it's done in asm:
rol eax, 8
xor al, al
bswap eax

tvirusx1

  • Guest
Re: C++ Bot, Color Problem (No .NET)
« Reply #10 on: December 09, 2009, 02:40:11 pm »
Damn I suck at this :(.
I can't get it to work, I tried finding a good Color but it didn't work, I also tried the Tolerance thingy... didn't work for me either (I set the Tolerance to 5) it clicked on every color =/.

I don't understand RGBQuad...
I am really bad at asm.
Don't bother trying to teach me how to use RGBQuad.

Is there any other way to solve this? Sorry that I'm asking so much =/.


This brings me on an Idea... you said before that Bitmap is stored in "BGRA" << I thought it's stored in "BGR" not "BGRA", I don't know what the "A" is.
I used

Code: [Select]
void SwapCol(DWORD* ptr)
{
   *ptr = (*ptr & 0x000000FF) << 16 | (*ptr & 0x0000FF00) | (*ptr & 0x00FF0000) >> 16;
}

I used to get the "BonusBoxColor" the color that I compare the single Pixels to, with GetPixel() in another Script (so the Program knows which color to search for).
Once I have the color with GetPixel I change it from RGB to BGR (with SwapCol()).

But I think I should use in the separate Program Bitmap as well because maybe SwapCol() doesn't work properly (although it should).

Maybe it's something with that "BGRA" you were talking about... I don't know.
It's late and I'll be going to sleep, got a long day tomorrow, I'll try out the Separate BitMap Program out tomorrow.

Offline Freddy

  • Owner
  • *****
  • Posts: 2614
  • Rep: 19
    • MSN Messenger - freddy1990@gmail.com
    • AOL Instant Messenger - Freddy199O
    • View Profile
    • Email
Re: C++ Bot, Color Problem (No .NET)
« Reply #11 on: December 09, 2009, 04:26:21 pm »
Your getting the bitmap as 32-bit bitmap, 32-bit = 4 bytes, 3 of them are RGB, the last one is A, Alpha. Alpha is a transparency factor. SCAR disregards transparency because it would increase the number of possible colors from 16777216 to 4294967296. And in the end it really isn't all that useful.

tvirusx1

  • Guest
Re: C++ Bot, Color Problem (No .NET)
« Reply #12 on: December 09, 2009, 11:11:05 pm »
That might be... lets say the program is looking for b=230 g=213 r=199 but it finds b=230 g=213 r=199 a=180 so thats why it might never find it because its looking for a = 0 (never declared it).

should I just simply change:
Code: [Select]
bi.bmiHeader.biBitCount = 32; to 24?
I just doesn't seem so simple.

Thanks a lot for your help, your a big help for me :D.

EDIT: If i remove the "SimilarColors()" it doesn't find anything but if i use it, it clicks everywhere...
« Last Edit: December 10, 2009, 12:06:30 am by tvirusx1 »

Offline Freddy

  • Owner
  • *****
  • Posts: 2614
  • Rep: 19
    • MSN Messenger - freddy1990@gmail.com
    • AOL Instant Messenger - Freddy199O
    • View Profile
    • Email
Re: C++ Bot, Color Problem (No .NET)
« Reply #13 on: December 10, 2009, 05:57:30 am »
it's not that simple... You can for example get the 32-bit color value, then do "color & 0xFFFFFF" to remove the alpha channel value, then you can compare the 32-bit values with eachother, but you would need to convert your 24-bit input color values to the 32-bit bitmap colors...

tvirusx1

  • Guest
Re: C++ Bot, Color Problem (No .NET)
« Reply #14 on: December 12, 2009, 01:24:59 am »
I tried:
Code: [Select]
COLORREF c;
ReadPixel(x,y); //it's BGRA now
c = (c & 0xFFFFFF); //now it should be BGR
SwapCol(&c); //now RGB
int r = GetRValue(c);
int g = GetGValue(c);
int b = GetBValue(c);

Is that what you meant? I dunno but I think something is wrong in the code... it returns weird values like r64,g64,b64 or r18,g11,b9.

I was looking up on how to convert colors and then I thought if I would use 256 Colors it would be easier for the Program to find it, no?
I thought maybe it's not my code that's wrong it might be that it just can't find it because there are way to many Colors.

Look at this:
Monochrome:

I don't think monochrome would be usable for my purposes, only black and white, no good.

16bit:

I don't think this would work either, too low amount of colors.

256 Colors:

I personally think this would be the solution.

The normal one, I'm not sure if it's 32bit or 24bit.

Too difficult for the Program to find colors =/.


I think the 256 Colors would be a solution (or Grayscale maybe?).
I hope you not sick of me and my C++ problems =/.

EDIT: It's so awful that I can't find any Color finding tutorials on the net... I'll probable make a tutorial once I'm done this.
« Last Edit: December 12, 2009, 01:29:10 am by tvirusx1 »

Freddy1990.com

Re: C++ Bot, Color Problem (No .NET)
« Reply #14 on: December 12, 2009, 01:24:59 am »

 

deductible-aliform