Advertisement
CocBotTBC

Untitled

Jan 19th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 11.37 KB | None | 0 0
  1. #cs
  2. - Hop every 10 pixels horizontally and vertically to find the color of the 1st pixel with 10 color variation (due to 10 pixels hop it'll likely to land on the surrounding pixels)
  3. - If found check surrounding pixels within 9 pixels horizontally and vertically to locate the 1st pixel (within 3 color variation)
  4. - If found continue to check for 2nd pixel (using x-offset and y-offset), allow for 3 pixels variation (to compensate for possible off-position of 1st pixel)
  5. - If found continue for 3rd pixel
  6.  
  7. > Since screen is not static pixelchecksum is useless and hoping every single pixel takes too much time (10s+).
  8. > However there's posible miss if it hops 10 pixel (all 4 surrounding checks all land outside shades variation)
  9. > I've done some research on image matching algorithm but they're all either too complicated or not serving this particular purpose.
  10. > If you have any idea please try to improve it. Thanks a lot
  11.  
  12. ### What to vary if you experience miss / fault positive ###
  13. - Shades variation for 1st pixel match (line under If OutofBound..)
  14. - Pixel variation for 2nd and 3rd pixel (in CheckColorVariPos)
  15. #ce
  16.  
  17. #include <Array.au3>
  18. #include <GUIConstantsEx.au3>
  19. #include <WindowsConstants.au3>
  20. #include <ColorConstants.au3>
  21. #include <GDIPlus.au3>
  22. #include <StaticConstants.au3>
  23. #include <WindowsConstants.au3>
  24. #include <WinAPIProc.au3>
  25. #include <ScreenCapture.au3>
  26. #include <Misc.au3>
  27. #include <TrayConstants.au3>
  28. #include <ColorConstants.au3>
  29. #include <GDIPlus.au3>
  30.  
  31. HotKeySet("^{SPACE}", "Terminate")
  32.  
  33. Global $hBitmap
  34. Global $hHBitmap
  35. Global $BSpos[2]
  36. Global $user32 = DllOpen("user32.dll")
  37. Global $HWnD = WinGetHandle(WinGetTitle("BlueStacks App Player"))
  38.  
  39. Global $sMatch = False
  40.  
  41. Global $Top[2] = [430, 20]
  42. Global $Bottom[2] = [430, 620]
  43. Global $Left[2] = [15, 305]
  44. Global $Right[2] = [835, 305]
  45. Global $Area = (($Top[0] * $Right[1] - $Right[0] * $Top[1]) + ($Right[0] * $Bottom[1] - $Bottom[0] * $Right[1]) + ($Bottom[0] * $Left[1] - $Left[0] * $Bottom[1]) + ($Left[0] * $Top[1] - $Top[0] * $Left[1])) / 2
  46. Global $a = Floor(($Top[0] + $Bottom[0] + $Left[0] + $Right[0]) / 4)
  47. Global $b = Floor(($Top[1] + $Bottom[1] + $Left[1] + $Right[1]) / 4)
  48.  
  49. _GDIPlus_Startup()
  50. getBSPos()
  51. WinActivate($HWnD)
  52. ;ZoomOut()
  53. _CaptureRegion()
  54. If _ColorCheckVariation(_PixelGetColor_GetPixel(70, 540), Hex(0xC50000, 6), 20) Then
  55.     While 1
  56.         $sStart = TimerInit()
  57.         _CaptureRegion()
  58.         If WeakBase(9) Then
  59.             $sStop = Round(TimerDiff($sStart) / 1000, 2)
  60.             MsgBox(0, "", "Weak base found. Time: " & $sStop & " seconds.")
  61.             Exit
  62.         Else
  63.             $sStop = Round(TimerDiff($sStart) / 1000, 2)
  64.             TrayTip("", "Not weak base. Time: " & $sStop & " seconds.","",0)
  65.             ControlClick("BlueStacks App Player", "", "", "left", "1", 750, 500) ;click next
  66.             Sleep(500)
  67.             _CaptureRegion()
  68.             While Not _ColorCheckVariation(_PixelGetColor_GetPixel(70, 540), Hex(0xC50000, 6), 20)
  69.                 Sleep(500)
  70.                 _CaptureRegion()
  71.             WEnd
  72.             Sleep(500)
  73.             TrayTip("", "Start base search", "", 0)
  74.         EndIf
  75.     WEnd
  76. EndIf
  77. ;~ _CaptureRegion()
  78. ;~ If BaseCheck("TownHall 8") Then
  79. ;~  MsgBox(0, "", "Found")
  80. ;~ Else
  81. ;~  MsgBox(0, "", "Not Found")
  82. ;~ EndIf
  83.  
  84. Func WeakBase($TH)
  85.     Switch $TH
  86.         Case 9
  87.             If BaseCheck("TownHall 8") Then Return True
  88.             ;If BaseCheck($TH-1) And BaseCheck("Wall 7") Then Return True
  89.     EndSwitch
  90.     Return False
  91. EndFunc   ;==>WeakBase
  92.  
  93. Func BaseCheck($item)
  94.     Switch $item
  95.         Case "TownHall 9"
  96.             If BaseSearch(0x404450, 9, 1, 0x494C58, 11, -2, 0x414753) Then Return True
  97.         Case "TownHall 8"
  98.             If BaseSearch(0x9F6B40, -2, 8, 0x976641, -2, 30, 0xF8FCFF) Then Return True
  99.     EndSwitch
  100.     Return False
  101. EndFunc   ;==>BaseCheck
  102.  
  103. Func BaseSearch($pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3)
  104.     _MultiPixelSearch($a + 5, $b + 5, 10, 15, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, -10, -10, False) ;top left
  105.     If $sMatch Then Return True
  106.     _MultiPixelSearch($a - 5, $b - 5, 835, 625, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, 10, 10, False) ;bottom right
  107.     If $sMatch Then Return True
  108.     _MultiPixelSearch($a + 5, $b - 5, 10, 625, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, -10, 10, False) ;bottom left
  109.     If $sMatch Then Return True
  110.     _MultiPixelSearch($a - 5, $b + 5, 835, 15, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, 10, -10, False) ;top right
  111.     If $sMatch Then Return True
  112.     Return False
  113. EndFunc   ;==>BaseSearch
  114.  
  115. Func Terminate()
  116.     Exit
  117. EndFunc   ;==>Terminate
  118.  
  119. #Region ##### MULTI PIXEL SEARCH #####
  120. Func _MultiPixelSearch($x1, $y1, $x2, $y2, $1c, $2x, $2y, $2c, $3x, $3y, $3c, $sStepx, $sStepy, $sSingle = False)
  121.     For $m = $x1 To $x2 + $sStepx Step $sStepx
  122.         For $n = $y1 To $y2 + $sStepy Step $sStepy
  123.             If OutofBound($m, $n) Then ExitLoop
  124.             If _ColorCheckVariation(_PixelGetColor_GetPixel($m, $n), Hex($1c, 6), 10) Then
  125.                 For $o = $m - $sStepx + 1 To $m + $sStepx - 1 Step $sStepx / Abs($sStepx)
  126.                     For $p = $n - $sStepy + 1 To $n + $sStepy - 1 Step $sStepy / Abs($sStepy)
  127.                         If _ColorCheckVariation(_PixelGetColor_GetPixel($o, $p), Hex($1c, 6), 3) Then
  128.                             If CheckColorVariPos($o + $2x, $p + $2y, $2c, 3, 5) Then
  129.                                 If CheckColorVariPos($o + $3x, $p + $3y, $3c, 3, 5) Then
  130.                                     ;Global $sStop = Round(TimerDiff($sStart) / 1000, 2)
  131.                                     $sMatch = True
  132.                                     WinActivate($HWnD)
  133.                                     ;MouseMove($BSpos[0] + $o, $BSpos[1] + $p)
  134.                                     ExitLoop (4)
  135.                                 EndIf
  136.                             EndIf
  137.                         EndIf
  138.                     Next
  139.                 Next
  140.             EndIf
  141.         Next
  142.     Next
  143. EndFunc   ;==>_MultiPixelSearch
  144. Func _MultiPixelSearch1($x, $y, $1c, $2x, $2y, $2c, $3x, $3y, $3c, $sStep = 10, $sSingle = False) ; first pixel (x,y), second pixel (x offset, y offset, color), third pixel (x offset, y offset, color)
  145.     While 1
  146.         If $y < 180 Or $y > 620 Or $x < 30 Or $x > 800 Then ;out of bound
  147.             ExitLoop
  148.         EndIf
  149.         If _ColorCheckVariation(_PixelGetColor_GetPixel($x, $y), Hex($1c, 6), 10) Then
  150.             For $m = $x - $sStep + 1 To $x + $sStep - 1
  151.                 For $n = $y - $sStep + 1 To $y + $sStep - 1
  152.                     If _ColorCheckVariation(_PixelGetColor_GetPixel($m, $n), Hex($1c, 6), 5) Then
  153.                         If CheckColorVariPos($m + $3x, $n + $3y, $3c, 3, 5) Then
  154.                             If CheckColorVariPos($m + $2x, $n + $2y, $2c, 3, 5) Then
  155.                                 $sMatch = True
  156.                                 WinActivate($HWnD)
  157.                                 MouseMove($BSpos[0] + $m, $BSpos[1] + $n)
  158.                                 ExitLoop (3)
  159.                             EndIf
  160.                         EndIf
  161.                     EndIf
  162.                 Next
  163.             Next
  164.         EndIf
  165.         If Not $sSingle Then ;algorithm for spriral out search (fast but still not covering all pixels on screen, need work)
  166.             If $x = $a And $y <= $b Then $x += $sStep
  167.             If $x >= $a And $y < $b Then
  168.                 $x += $sStep
  169.                 $y += $sStep
  170.             ElseIf $x > $a And $y >= $b Then
  171.                 $x -= $sStep
  172.                 $y += $sStep
  173.             ElseIf $x <= $a And $y > $b Then
  174.                 $x -= $sStep
  175.                 $y -= $sStep
  176.             ElseIf $x < $a And $y <= $b Then
  177.                 $x += $sStep
  178.                 $y -= $sStep
  179.             EndIf
  180.         EndIf
  181.     WEnd
  182. EndFunc   ;==>_MultiPixelSearch1
  183. #EndRegion ##### MULTI PIXEL SEARCH #####
  184.  
  185. #Region ##### PIXEL PROCESSING #####
  186. Func _CaptureRegion($iLeft = 0, $iTop = 0, $iRight = 860, $iBottom = 720, $ReturnBMP = False)
  187.     _GDIPlus_BitmapDispose($hBitmap)
  188.     _WinAPI_DeleteObject($hHBitmap)
  189.     Local $iW = Number($iRight) - Number($iLeft), $iH = Number($iBottom) - Number($iTop)
  190.     Local $hDC_Capture = _WinAPI_GetWindowDC(ControlGetHandle("BlueStacks App Player", "", "[CLASS:BlueStacksApp; INSTANCE:1]"))
  191.     Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture)
  192.     $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $iW, $iH)
  193.     Local $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap)
  194.     DllCall($user32, "int", "PrintWindow", "hwnd", $HWnD, "handle", $hMemDC, "int", 0)
  195.     _WinAPI_SelectObject($hMemDC, $hHBitmap)
  196.     _WinAPI_BitBlt($hMemDC, 0, 0, $iW, $iH, $hDC_Capture, $iLeft, $iTop, 0x00CC0020)
  197.     $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
  198.     _WinAPI_DeleteDC($hMemDC)
  199.     _WinAPI_SelectObject($hMemDC, $hObjectOld)
  200.     _WinAPI_ReleaseDC($HWnD, $hDC_Capture)
  201. EndFunc   ;==>_CaptureRegion
  202. Func _PixelGetColor_GetPixel($iX, $iY)
  203.     Local $aPixelColor = _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
  204.     Return Hex($aPixelColor, 6)
  205. EndFunc   ;==>_PixelGetColor_GetPixel
  206. Func _PixelSearch($iLeft, $iTop, $iRight, $iBottom, $iColor, $ColorVariation)
  207.     _CaptureRegion($iLeft, $iTop, $iRight, $iBottom)
  208.     For $x = $iRight - $iLeft To 0 Step -1
  209.         For $y = $iBottom - $iTop To 0 Step -1
  210.             Local $nColor1 = $iColor
  211.             Local $nColor2 = _PixelGetColor_GetPixel($x, $y)
  212.             If _ColorCheckVariation($nColor1, $nColor2, $ColorVariation) Then
  213.                 Local $Pos[2] = [$iLeft + $x, $iTop + $y]
  214.                 Return $Pos
  215.             EndIf
  216.         Next
  217.     Next
  218.     Return 0
  219. EndFunc   ;==>_PixelSearch
  220. Func _ColorCheckVariation($nColor1, $nColor2, $sVari = 5)
  221.     Local $Red1, $Red2, $Blue1, $Blue2, $Green1, $Green2
  222.     $Red1 = Dec(StringMid(String($nColor1), 1, 2))
  223.     $Blue1 = Dec(StringMid(String($nColor1), 3, 2))
  224.     $Green1 = Dec(StringMid(String($nColor1), 5, 2))
  225.     $Red2 = Dec(StringMid(String($nColor2), 1, 2))
  226.     $Blue2 = Dec(StringMid(String($nColor2), 3, 2))
  227.     $Green2 = Dec(StringMid(String($nColor2), 5, 2))
  228.     If Abs($Blue1 - $Blue2) > $sVari Then Return False
  229.     If Abs($Green1 - $Green2) > $sVari Then Return False
  230.     If Abs($Red1 - $Red2) > $sVari Then Return False
  231.     Return True
  232. EndFunc   ;==>_ColorCheckVariation
  233. Func CheckColorVariPos($x, $y, $c, $vp = 0, $vc = 10)
  234.     For $a = $x - $vp To $x + $vp
  235.         For $b = $y - $vp To $y + $vp
  236.             If _ColorCheckVariation(_PixelGetColor_GetPixel($a, $b), Hex($c, 6), $vc) Then
  237.                 Return True
  238.             EndIf
  239.         Next
  240.     Next
  241.     Return False
  242. EndFunc   ;==>CheckColorVariPos
  243. #EndRegion ##### PIXEL PROCESSING #####
  244.  
  245. #Region ##### ADDITIONAL #####
  246. Func getBSPos()
  247.     $aPos = ControlGetPos("BlueStacks App Player", "", "[CLASS:BlueStacksApp; INSTANCE:1]")
  248.     $tPoint = DllStructCreate("int X;int Y")
  249.     DllStructSetData($tPoint, "X", $aPos[0])
  250.     DllStructSetData($tPoint, "Y", $aPos[1])
  251.     _WinAPI_ClientToScreen(WinGetHandle(WinGetTitle("BlueStacks App Player")), $tPoint)
  252.     $BSpos[0] = DllStructGetData($tPoint, "X")
  253.     $BSpos[1] = DllStructGetData($tPoint, "Y")
  254.     $CtrlHandle = ControlGetHandle("BlueStacks App Player", "", "BlueStacksApp1")
  255. EndFunc   ;==>getBSPos
  256. Func OutofBound($x, $y)
  257.     $chkArea = TriArea($x, $y, $Top, $Left) + TriArea($x, $y, $Top, $Right) + TriArea($x, $y, $Right, $Bottom) + TriArea($x, $y, $Left, $Bottom)
  258.     If $chkArea - $Area > 100 Then
  259.         Return True
  260.     Else
  261.         Return False
  262.     EndIf
  263. EndFunc   ;==>OutofBound
  264. Func TriArea($dx, $dy, $e, $f)
  265.     $tArea = Abs(($dx * ($e[1] - $f[1]) + $e[0] * ($f[1] - $dy) + $f[0] * ($dy - $e[1])) / 2)
  266.     Return $tArea
  267. EndFunc   ;==>TriArea
  268. Func ZoomOut() ;Zooms out
  269.     Local $i = 0
  270.     While 1
  271.         Sleep(500)
  272.         _CaptureRegion(0, 0, 860, 2)
  273.         If _PixelGetColor_GetPixel(1, 1) = 0x000000 And _PixelGetColor_GetPixel(850, 1) = 0x000000 Then
  274.             ExitLoop
  275.         Else
  276.             ControlSend("BlueStacks App Player", "", "", "{DOWN}", 0)
  277.             $i += 1
  278.             If $i = 40 Then
  279.                 ExitLoop
  280.             EndIf
  281.         EndIf
  282.     WEnd
  283. EndFunc   ;==>ZoomOut
  284. #EndRegion ##### ADDITIONAL #####
  285.  
  286.  
  287.  
  288. ;~  If $sMatch Then ExitLoop
  289. ;~  _MultiPixelSearch2(305, 100, 555, 0,  $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, 3, -3, False)  ;corner top
  290. ;~  If $sMatch Then ExitLoop
  291. ;~  _MultiPixelSearch2(165, 200, 630, 425,  $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, -3, 3, False)    ;corner left
  292. ;~  If $sMatch Then ExitLoop
  293. ;~  _MultiPixelSearch2(720, 220, 835, 410,  $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, -3, 3, False)    ;corner right
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement