📜 ⬆️ ⬇️

Bots for browser games on AutoIT

Instead of the preface

Today I received a link to an article on the technology of creating a “macro bot for a browser game . There it was written with regret that AutoIT is poorly represented in Habré. With the pieces described in the article, I indulged a year or two ago. Recently I have been using the IE.au3 library, which allows me to work with browser toys just wonders. I would like to share this information myself. I just warn you right away that it’s only for work under MS-Internet Explorer. I will tell fans of other browsers right away - you can probably do the same thing with any browser, you just need to look for the appropriate library and I can’t say how they work. I can only say about the IE.au3 library - it is included in the standard AutoIT installation kit , has been fairly well tested, provided with comments and examples, the functions described in it are conveniently highlighted and suggested to be completed when typing with appropriate hints as the standard functions of the package (or there to call this very AutoIT).

Victim selection

As an example of the work of AutoIT with browser games, I propose the game “ My Village ” (the original name of the game is “ My Free Farm ”) - http://www.mojaderewnja.ru/. The game really begs for automation because it forces you to perform too much routine action. For example, if you don’t pay about 180 rubles a month to a project, then you will need to plant and water the plants manually. And this is usually 120 clicks on the field, only to plant the plants, then 120 clicks to water. Fields can be not one and not two, but up to ten. And some plants grow for about 10 minutes, so such clicks either discourage the desire to play or make you pay for a casual toy a monthly amount for which you can have a month of modest Internet or good cable television. So automating “ My Village ” is just what we need. Looks at the sticking field like this:
image

Option number 1 - a primitive version

We are looking for on the screen where to poke, and poke the "mice" where necessary. I’ll say right away that this is the wrong option, but for general erudition I will give the text of the script with explanations. Part 1: we are looking for a “point of reference” on the screen for which it will be possible to cling and from it consider the location of the squares where we will run around the field.
Func GetTopLeftCorner ( $ window )
Global $ x = 0 , $ y = 0
WinActivate ( $ window , "" )
If WinActive ( $ window ) Then
$ size = WinGetPos ( "[active]" )
For $ i = 1 To $ size [ 3 ]
If CheckRGB ( PixelGetColor ( $ x + $ i , $ y + $ i ) , Dec ( "402215" ) , Dec ( "5A352A" ) ) Then
If CheckRGB ( PixelGetColor ( $ x + $ i - 259 , $ y + $ i ) , Dec ( "331A0D" ) , Dec ( "624232" ) )
For $ j = $ i To 1 Step - 1
If not CheckRGB ( PixelGetColor ( $ i + 1 , $ j ) , Dec ( "000000" ) , Dec ( "3A301D" ) ) Then
$ x = $ i
$ y = $ j
ExitLoop ( 2 )
Endif
Next
Exitloop
Endif
Endif
Next
Endif
Endfunc
The function checks the colors of points, running diagonally, searches for the left edge of the game window in the browser. Then it scans the colors of the dots, running up, looking for the upper border of the game window. I will consider the found point as a “ point of reference ”. The distance from it to each cell on the game field is fixed and easily calculated by the index. The result of the function is returned in global variables $ x and $ y , which correspond to the coordinates of the point horizontally and vertically, respectively. When you call the function, you must specify the title of the program window as a parameter . It can be set manually by defining it in advance using the AutoIt Window Info utility, which is installed along with AutoIt.The script above uses the self-made CheckRGB function:
Func CheckRGB ( $ color , $ min , $ max )
Local $ rgb [ 3 ] [ 3 ]
$ res = True
$ rgb [ 2 ] [ 0 ] = BitAND ( $ min , Dec ( "FF0000" ) ) / Dec ( "10000" )
$ rgb [ 1 ] [ 0 ] = BitAND ( $ min , Dec ( "00FF00" ) ) / Dec ( "100" )
$ rgb [ 0 ] [ 0 ] = BitAND ( $ min , Dec ( "0000FF" ) )
$ rgb [ 2 ] [ 1 ] = BitAND ( $ color , Dec ( "FF0000" ) ) / Dec ( "10000" )
$ rgb [ 1 ] [ 1 ] = BitAND ( $ color , Dec ( "00FF00" ) ) / Dec ( "100" )
$ rgb [ 0 ] [ 1 ] = BitAND ( $ color , Dec ( "0000FF" ) )
$ rgb [ 2 ] [ 2 ] = BitAND ( $ max , Dec ( "FF0000" ) ) / Dec ( "10000" )
$ rgb [ 1 ] [ 2 ] = BitAND ( $ max , Dec ( "00FF00" ) ) / Dec ( "100" )
$ rgb [ 0 ] [ 2 ] = BitAND ( $ max , Dec ( "0000FF" ) )
For $ i = 0 To 2
If $ rgb [ $ i ] [ 0 ] > $ rgb [ $ i ] [ 1 ] Or $ rgb [ $ i ] [ 1 ] > $ rgb [ $ i ] [ 2 ] Then
$ res = False
Exitloop
Endif
Next
Return $ res
Endfunc
- The function checks the color of the point transmitted as the first parameter $ color to ensure that it is in the color range from $ min to $ max , taking into account the three component colors of the point ( RGB ). Returns, respectively, True or False .
')
Directly procedure sticking all the cells on the field looks like this:
For $ i = 1 To 12
For $ j = 1 To 10
MouseClick ( "left" , $ x + ( $ i * 40 ) + 103 , $ y + ( $ j * 40 ) + 172 , 1 , 1 )
Sleep ( 25 )
Next
Next
It's all clear. 10 and 12 in the loop headers is the size of the field in the cells. Constants in MouseClick parameters are calculated using the same AutoIt Window Info utility, relative to the previously defined “reference point”. The value of the delay in the Sleep can be changed at will - this will affect the speed of sticking, but too fast can lead to errors in the execution of the game script.

The advantage of this method is that it works in any browser. Everywhere where you can open the game window so that all the arable land is visible. The disadvantage is that the game window should always be open, you need to select the speed of sticking and you should not touch the mouse while the script is running (otherwise you shouldn't click where it is necessary).

Option number 2 - the correct version

We do not use the mouse pointer at all. Instead, we poke on the desired object loaded in the IE browser by its ID . (The IDs of these cells can be viewed in the HTML code of the loaded page, they have the names f1 - f120, respectively).
Local $ i , $ j , $ n
Local $ oIE = _IEAttach ( $ gamewindowname )
For $ i = 1 To 12
For $ j = 0 To 9
$ obj = _IEGetObjById ( $ oIE , "f" & ( 12 * $ j + $ i ) )
If $ obj <> 0 Then $ obj . click ( )
Next
Next


Option number 3 - the most correct option

The disadvantage of the previous version is that when we need to stick around the field, we need to switch from the game and run the script. It is not comfortable. A truly user-friendly script should not force you to perform additional actions in order to save on other routine operations. Therefore, I simply replace the standard action in the game “ plant one seed ” with “ plant the entire field with selected seeds ”. This looks like this:
$ obj = _IEGetObjById ( $ oIE , "anpflanzen" )
If $ obj <> 0 Then _
$ obj . outerHTML = "... there is a piece of HTML code, which, for convenience, I cite separately"
Here is this piece of HTML code to insert into the previous snippet:
< SPAN id = anpflanzen class = link onclick = "
selectMode (0, true, selected);
var i, j, s, n = 0;
for (i = 1; i <13; i = i + rackElement [selected] .x)
for (j = 0; j <10; j = j + rackElement [selected] .y) {
s = 12 * j + i; cache_me (" & $ zone & ", s, garten_prod [s], garten_kategorie [s], n ++);} " >
< IMG onmouseover = "this.src = 'http: //mff.wavecdn.de/mff/garden_menue_seed.gif'; showDiv ('tooltipseed')" onmouseout = "this.src = 'http: //mff.wavecdn. de / mff / leer.gif '; hideDiv (' tooltipseed ') " src = " http://mff.wavecdn.de/mff/leer.gif " width = 53 height = 68 >
< DIV style = "DISPLAY: none" id = tooltipseed class = blackbox onmouseover = "showDiv ('tooltipseed')" onmouseout = "hideDiv ('tooltipseed')" src = "http://mff.wavecdn.de/mff/ leer.gif " > plant everything < / div > < / span >
The code itself and the JavaScript scripts were borrowed from the original HTML code of the game and the script file loaded with it, and were slightly altered from watering to planting.

Source: https://habr.com/ru/post/126957/


All Articles