The Bytecode Club
Log inJoin the club
Video Game Reverse Engineering

Developing Bot

Started by Antidote · Dec 5, 2014

1,033 replies21,297,457 viewsLocked
Dec 21, 2014#171

@Antidote if you got time soon you maybe could have a look at the ReadValue function. i tried it for hours now and still dont understand why its stuck for me in this function.
its just not giving out any results. it opens tesseract.exe a few times but nothing happens. autoit is not receiving anything to work with.

maybe you can tell me how to test it out where exactly i can locate the problem?

the functions working:

Attack started. Begin searching..
Openig ComputeValue
Opening ReadValue

From this point on nothing more is happening. =/

@kojometa,

ReadValue stucked is likely due to missing coc.traineddata file. You have to put the traineddata file into the tessdata folder in tesseract-OCR folder
Or else you install the latest script that come with installer by @Antidote.

Dec 21, 2014#172

@GkenvinOD: If you manage to optimize it to less than a second it would be a very valuable function (100% accuracy, independent of tesseract, image files,...). If you need any assisstance I'm happy to help 🙂
@AtoZ: Yes when I first started I wasn't fully familiar with coord, pixel mode so every coordinate and color value was hand-picked using a custom script to output value (in that script coord mode was set to 2). The difference between mode 0 and mode 2 is (0,21) as the height of Windows Active Title Bar is 21 (Windows default). It doesn't matter which one though, but I will look through the code again to ensure consistency. Also you can try GkevinOD's code again with $x and $y change to +52 and +93. It's very accurate but very picky about getting first pixel correct.

Problems regarding the ReadValue function is caused by missing Tesseract or traineddata as odySSeys says. If there's no Tesseract or traineddata, $Read value will return null which will fail the check, causing it to stuck in a loop (without the check insufficient data will crash the program). Here's how to make sure it works:

  • Download Tesseract-OCR.rar from this link: http://www42.zippyshare.com/v/41826460/file.html
  • Inside the folder is tesseract.exe, traineddata, configs folder and Tesseract.au3 (this is Autoit UDF to interact with Tesseract)
  • Unrar and place the main script in the same folder, make sure it is #include "Tesseract-OCR\Tesseract.au3" in the script.
    The main script will call Tesseract.au3, Tesseract.au3 will call tesseract.exe via ShellExecuteWait (path\to\tesseract.exe)
    Simple script to test if Tesseract works:
#include "Tesseract-OCR\Tesseract.au3"
Opt ("MouseCoordMode", 0)
$Title = "BlueStacks App Player"
$Full = WinGetTitle ($Title)
$HWnD = WinGetHandle ($Full)
WinActivate ($HWnD)
Global $Left = 54
Global $Top = 81
Global $Right = 745
Global $Bottom = 555
$Read = _TesseractWinCapture($HWnD,"",0,"",1,2,$Left,$Top,$Right,$Bottom,1)
Dec 21, 2014#173

I'm still learning everything from scratch like many of you guys here (luckily Autoit has very smooth learning curve), so mistakes are here and there and I'm constantly updating the code if I find better ways to do it. Anw since many people contribute now, is there anyway we can effectively collaborate without having to copy paste every time? Something like Github or Google Code?

Dec 21, 2014#174

Good job Antidote for your work. I just tried your last bot and at the beginning it just trains the troops and then after it starts searching for a battle but without troops in the camps. Good luck again

Dec 21, 2014#175

@Antidote
Okay guys i FINALLY found out what the problem is.
Tesseract calls tesseract.exe in command - of course.
The problem was, that, no matter what, it ignores the folder stucture which is given in the tesseract.au3.
Why?
Because some time ago I installed tesseract by installer from the official tesseract website.
This istaller registered tesseract as an envoirment variable path ("My Computer > Properties > Advanced > Environment Variables > Path")
And Adds:
"C:\Users\xxxxx\AppData\Local\Tesseract-OCR"
This is causing to open the officcial tesseract.exe in the original AppData folder, no matter out of which dir you call it in cmd!
Just wanted to tell you guys because everyone who installed the official tesseract is gonna have this problem! There are 2 options to solve this problem.
Nr. 1 : Delete the envoirment variable, Edit: Theres another one called: TESSDATA_PREFIX
Nr. 2 : Put the coc.traineddata into the official Tesseract folder!

Also i had to edit the Tesseract.au3 and make sure that that one entry "tesseract.exe" is changed to "Tesseract-OCR\tesseract.exe" because i got not tesseract.exe in the botfolder itself. thus its working now!
i also changed your gui-editor to a list, i think its more comfortable with long results and because your list didnt even have a scroll possibility!

so its now:
$Results = GUICtrlCreateList("", 20, 140, 260, 225, BitOR($SS_SUNKEN,$ES_READONLY, $SS_LEFT, $WS_VSCROLL))

with that list i think the ClearBox() function is not needed anymore as well 😉 but that just a matter of personal preferences

Dec 21, 2014#176

I edited some stuff.
Most significant is the ReturnHome function.
I added a timer wich will run 150 Seconds (you can see the actual countdown in the GUI) and then return home.
Also it is always showing which function is called at the moment to recognise where potential errors are happening.

what i found out: if you pick goblins or giants in the comboboxes, it closes the script after developing because it seems to crash in AssignTroop.

http://pastebin.com/FTsnMPbT

😉

Dec 21, 2014#177

Rather detect gold and elixir change.

Dec 21, 2014#178

@kojometa: just tested out your script. The count down timer is neat, actually I did the same with TimerInit and TimerDiff, but I agree with vanadium, letting the script sleep through the whole time is quite counter intuitive, just a temporary measure til we can find better solution. DropTroop is helpful for testing as well (small mistake at  Case $VSButton, GUICtrlSetData($DTButton,"Dropping Troops..."). About goblin or giants, I've tested with giants, bar, archer and it was able to drop giants, bar, archer in consecutive order. Haven't tested goblin though. Basically what func AssignTroop does is sorting troops arcording to combobox selection. For example, selection: Giant,Bar,Bar,Arch. Troop orders in attack panel will be: Bar, Arch, Giant.
@vanadium: seems like it's how lf does, but I find it unproductive as sometimes my troops finish clearing all defense and go for loot when it detects no gold change and just end battle. i think to detect when the Return Home screen appears would be best
@AtoZ: can you share how you detect maxed zoom out? it's quite an important check to ensure the rest of the script function.
I'm still testing on GkevinOD's script. I made a script to brute force the correct first pixels but it's quite tedious to test on elixir as I'm not sure which pixels he uses for each number (trying to replicate the hex in photoshop). But this is a good approach.

Dec 21, 2014#179

It's not sufficient to detect When Home button appears: all troops and spells may not have been used for the battle. In this case, return home appears only after 3min timeout. Also, depending you get at least one star or not, the button is either "Surrender" or "End battle", with a prompt in the first case.

The best solution is to check gold + elixir change, and stop the battle after 30sec without changes. It works perfectly fine for me. It does not take that much cpu.

On my bot done in Java, i have a JSON config for:

  • the army to train,
  • the troops to deploy, in which order, how many, when to enable hero ability etc.

Sample army config:

{
"BARBARIAN": "108",
"ARCHER": "107"
}

Sample deploy config:

[
{
"@type": "ScootCorners",
"troop": "BARBARIAN"
},
{
"@type": "HoldOn",
"duration": "10",
"unit": "SECONDS"
},
{
"@type": "SendTroopCount",
"troop": "BARBARIAN",
"count": "30",
"distribution": "RANDOM"
},
{
"@type": "SendTroopCount",
"troop": "ARCHER",
"count": "30",
"distribution": "RANDOM"
},
{
"@type": "HoldOn",
"duration": "2",
"unit": "SECONDS"
},
...
]

It's very convenient to change the trained army, troop deployment because it does not require to change the code. But, hey, it's a hell easier in Java.

Dec 21, 2014#180

Make sense to me now since I tend to always use up all troops in battle I might overlook that. By detecting gold & elixir change, do you detect change in that screen region (pixelchecksum) or actually read gold & elixir to compare? How would it handle when you get 3 stars or battle end early (out of troops)?
Also I believe config file can be done in Autoit as well, requires very organized and flexible coding though. From what I see, your bot seems way ahead :D

This discussion is closed.