Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #cs
- - 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)
- - If found check surrounding pixels within 9 pixels horizontally and vertically to locate the 1st pixel (within 3 color variation)
- - 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)
- - If found continue for 3rd pixel
- > Since screen is not static pixelchecksum is useless and hoping every single pixel takes too much time (10s+).
- > However there's posible miss if it hops 10 pixel (all 4 surrounding checks all land outside shades variation)
- > I've done some research on image matching algorithm but they're all either too complicated or not serving this particular purpose.
- > If you have any idea please try to improve it. Thanks a lot
- ### What to vary if you experience miss / fault positive ###
- - Shades variation for 1st pixel match (line under If OutofBound..)
- - Pixel variation for 2nd and 3rd pixel (in CheckColorVariPos)
- #ce
- #include <Array.au3>
- #include <GUIConstantsEx.au3>
- #include <WindowsConstants.au3>
- #include <ColorConstants.au3>
- #include <GDIPlus.au3>
- #include <StaticConstants.au3>
- #include <WindowsConstants.au3>
- #include <WinAPIProc.au3>
- #include <ScreenCapture.au3>
- #include <Misc.au3>
- #include <TrayConstants.au3>
- #include <ColorConstants.au3>
- #include <GDIPlus.au3>
- HotKeySet("^{SPACE}", "Terminate")
- Global $hBitmap
- Global $hHBitmap
- Global $BSpos[2]
- Global $user32 = DllOpen("user32.dll")
- Global $HWnD = WinGetHandle(WinGetTitle("BlueStacks App Player"))
- Global $sMatch = False
- Global $Top[2] = [430, 20]
- Global $Bottom[2] = [430, 620]
- Global $Left[2] = [15, 305]
- Global $Right[2] = [835, 305]
- 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
- Global $a = Floor(($Top[0] + $Bottom[0] + $Left[0] + $Right[0]) / 4)
- Global $b = Floor(($Top[1] + $Bottom[1] + $Left[1] + $Right[1]) / 4)
- _GDIPlus_Startup()
- getBSPos()
- WinActivate($HWnD)
- ;ZoomOut()
- _CaptureRegion()
- If _ColorCheckVariation(_PixelGetColor_GetPixel(70, 540), Hex(0xC50000, 6), 20) Then
- While 1
- $sStart = TimerInit()
- _CaptureRegion()
- If WeakBase(9) Then
- $sStop = Round(TimerDiff($sStart) / 1000, 2)
- MsgBox(0, "", "Weak base found. Time: " & $sStop & " seconds.")
- Exit
- Else
- $sStop = Round(TimerDiff($sStart) / 1000, 2)
- TrayTip("", "Not weak base. Time: " & $sStop & " seconds.","",0)
- ControlClick("BlueStacks App Player", "", "", "left", "1", 750, 500) ;click next
- Sleep(500)
- _CaptureRegion()
- While Not _ColorCheckVariation(_PixelGetColor_GetPixel(70, 540), Hex(0xC50000, 6), 20)
- Sleep(500)
- _CaptureRegion()
- WEnd
- Sleep(500)
- TrayTip("", "Start base search", "", 0)
- EndIf
- WEnd
- EndIf
- ;~ _CaptureRegion()
- ;~ If BaseCheck("TownHall 8") Then
- ;~ MsgBox(0, "", "Found")
- ;~ Else
- ;~ MsgBox(0, "", "Not Found")
- ;~ EndIf
- Func WeakBase($TH)
- Switch $TH
- Case 9
- If BaseCheck("TownHall 8") Then Return True
- ;If BaseCheck($TH-1) And BaseCheck("Wall 7") Then Return True
- EndSwitch
- Return False
- EndFunc ;==>WeakBase
- Func BaseCheck($item)
- Switch $item
- Case "TownHall 9"
- If BaseSearch(0x404450, 9, 1, 0x494C58, 11, -2, 0x414753) Then Return True
- Case "TownHall 8"
- If BaseSearch(0x9F6B40, -2, 8, 0x976641, -2, 30, 0xF8FCFF) Then Return True
- EndSwitch
- Return False
- EndFunc ;==>BaseCheck
- Func BaseSearch($pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3)
- _MultiPixelSearch($a + 5, $b + 5, 10, 15, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, -10, -10, False) ;top left
- If $sMatch Then Return True
- _MultiPixelSearch($a - 5, $b - 5, 835, 625, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, 10, 10, False) ;bottom right
- If $sMatch Then Return True
- _MultiPixelSearch($a + 5, $b - 5, 10, 625, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, -10, 10, False) ;bottom left
- If $sMatch Then Return True
- _MultiPixelSearch($a - 5, $b + 5, 835, 15, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, 10, -10, False) ;top right
- If $sMatch Then Return True
- Return False
- EndFunc ;==>BaseSearch
- Func Terminate()
- Exit
- EndFunc ;==>Terminate
- #Region ##### MULTI PIXEL SEARCH #####
- Func _MultiPixelSearch($x1, $y1, $x2, $y2, $1c, $2x, $2y, $2c, $3x, $3y, $3c, $sStepx, $sStepy, $sSingle = False)
- For $m = $x1 To $x2 + $sStepx Step $sStepx
- For $n = $y1 To $y2 + $sStepy Step $sStepy
- If OutofBound($m, $n) Then ExitLoop
- If _ColorCheckVariation(_PixelGetColor_GetPixel($m, $n), Hex($1c, 6), 10) Then
- For $o = $m - $sStepx + 1 To $m + $sStepx - 1 Step $sStepx / Abs($sStepx)
- For $p = $n - $sStepy + 1 To $n + $sStepy - 1 Step $sStepy / Abs($sStepy)
- If _ColorCheckVariation(_PixelGetColor_GetPixel($o, $p), Hex($1c, 6), 3) Then
- If CheckColorVariPos($o + $2x, $p + $2y, $2c, 3, 5) Then
- If CheckColorVariPos($o + $3x, $p + $3y, $3c, 3, 5) Then
- ;Global $sStop = Round(TimerDiff($sStart) / 1000, 2)
- $sMatch = True
- WinActivate($HWnD)
- ;MouseMove($BSpos[0] + $o, $BSpos[1] + $p)
- ExitLoop (4)
- EndIf
- EndIf
- EndIf
- Next
- Next
- EndIf
- Next
- Next
- EndFunc ;==>_MultiPixelSearch
- 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)
- While 1
- If $y < 180 Or $y > 620 Or $x < 30 Or $x > 800 Then ;out of bound
- ExitLoop
- EndIf
- If _ColorCheckVariation(_PixelGetColor_GetPixel($x, $y), Hex($1c, 6), 10) Then
- For $m = $x - $sStep + 1 To $x + $sStep - 1
- For $n = $y - $sStep + 1 To $y + $sStep - 1
- If _ColorCheckVariation(_PixelGetColor_GetPixel($m, $n), Hex($1c, 6), 5) Then
- If CheckColorVariPos($m + $3x, $n + $3y, $3c, 3, 5) Then
- If CheckColorVariPos($m + $2x, $n + $2y, $2c, 3, 5) Then
- $sMatch = True
- WinActivate($HWnD)
- MouseMove($BSpos[0] + $m, $BSpos[1] + $n)
- ExitLoop (3)
- EndIf
- EndIf
- EndIf
- Next
- Next
- EndIf
- If Not $sSingle Then ;algorithm for spriral out search (fast but still not covering all pixels on screen, need work)
- If $x = $a And $y <= $b Then $x += $sStep
- If $x >= $a And $y < $b Then
- $x += $sStep
- $y += $sStep
- ElseIf $x > $a And $y >= $b Then
- $x -= $sStep
- $y += $sStep
- ElseIf $x <= $a And $y > $b Then
- $x -= $sStep
- $y -= $sStep
- ElseIf $x < $a And $y <= $b Then
- $x += $sStep
- $y -= $sStep
- EndIf
- EndIf
- WEnd
- EndFunc ;==>_MultiPixelSearch1
- #EndRegion ##### MULTI PIXEL SEARCH #####
- #Region ##### PIXEL PROCESSING #####
- Func _CaptureRegion($iLeft = 0, $iTop = 0, $iRight = 860, $iBottom = 720, $ReturnBMP = False)
- _GDIPlus_BitmapDispose($hBitmap)
- _WinAPI_DeleteObject($hHBitmap)
- Local $iW = Number($iRight) - Number($iLeft), $iH = Number($iBottom) - Number($iTop)
- Local $hDC_Capture = _WinAPI_GetWindowDC(ControlGetHandle("BlueStacks App Player", "", "[CLASS:BlueStacksApp; INSTANCE:1]"))
- Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture)
- $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $iW, $iH)
- Local $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap)
- DllCall($user32, "int", "PrintWindow", "hwnd", $HWnD, "handle", $hMemDC, "int", 0)
- _WinAPI_SelectObject($hMemDC, $hHBitmap)
- _WinAPI_BitBlt($hMemDC, 0, 0, $iW, $iH, $hDC_Capture, $iLeft, $iTop, 0x00CC0020)
- $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
- _WinAPI_DeleteDC($hMemDC)
- _WinAPI_SelectObject($hMemDC, $hObjectOld)
- _WinAPI_ReleaseDC($HWnD, $hDC_Capture)
- EndFunc ;==>_CaptureRegion
- Func _PixelGetColor_GetPixel($iX, $iY)
- Local $aPixelColor = _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
- Return Hex($aPixelColor, 6)
- EndFunc ;==>_PixelGetColor_GetPixel
- Func _PixelSearch($iLeft, $iTop, $iRight, $iBottom, $iColor, $ColorVariation)
- _CaptureRegion($iLeft, $iTop, $iRight, $iBottom)
- For $x = $iRight - $iLeft To 0 Step -1
- For $y = $iBottom - $iTop To 0 Step -1
- Local $nColor1 = $iColor
- Local $nColor2 = _PixelGetColor_GetPixel($x, $y)
- If _ColorCheckVariation($nColor1, $nColor2, $ColorVariation) Then
- Local $Pos[2] = [$iLeft + $x, $iTop + $y]
- Return $Pos
- EndIf
- Next
- Next
- Return 0
- EndFunc ;==>_PixelSearch
- Func _ColorCheckVariation($nColor1, $nColor2, $sVari = 5)
- Local $Red1, $Red2, $Blue1, $Blue2, $Green1, $Green2
- $Red1 = Dec(StringMid(String($nColor1), 1, 2))
- $Blue1 = Dec(StringMid(String($nColor1), 3, 2))
- $Green1 = Dec(StringMid(String($nColor1), 5, 2))
- $Red2 = Dec(StringMid(String($nColor2), 1, 2))
- $Blue2 = Dec(StringMid(String($nColor2), 3, 2))
- $Green2 = Dec(StringMid(String($nColor2), 5, 2))
- If Abs($Blue1 - $Blue2) > $sVari Then Return False
- If Abs($Green1 - $Green2) > $sVari Then Return False
- If Abs($Red1 - $Red2) > $sVari Then Return False
- Return True
- EndFunc ;==>_ColorCheckVariation
- Func CheckColorVariPos($x, $y, $c, $vp = 0, $vc = 10)
- For $a = $x - $vp To $x + $vp
- For $b = $y - $vp To $y + $vp
- If _ColorCheckVariation(_PixelGetColor_GetPixel($a, $b), Hex($c, 6), $vc) Then
- Return True
- EndIf
- Next
- Next
- Return False
- EndFunc ;==>CheckColorVariPos
- #EndRegion ##### PIXEL PROCESSING #####
- #Region ##### ADDITIONAL #####
- Func getBSPos()
- $aPos = ControlGetPos("BlueStacks App Player", "", "[CLASS:BlueStacksApp; INSTANCE:1]")
- $tPoint = DllStructCreate("int X;int Y")
- DllStructSetData($tPoint, "X", $aPos[0])
- DllStructSetData($tPoint, "Y", $aPos[1])
- _WinAPI_ClientToScreen(WinGetHandle(WinGetTitle("BlueStacks App Player")), $tPoint)
- $BSpos[0] = DllStructGetData($tPoint, "X")
- $BSpos[1] = DllStructGetData($tPoint, "Y")
- $CtrlHandle = ControlGetHandle("BlueStacks App Player", "", "BlueStacksApp1")
- EndFunc ;==>getBSPos
- Func OutofBound($x, $y)
- $chkArea = TriArea($x, $y, $Top, $Left) + TriArea($x, $y, $Top, $Right) + TriArea($x, $y, $Right, $Bottom) + TriArea($x, $y, $Left, $Bottom)
- If $chkArea - $Area > 100 Then
- Return True
- Else
- Return False
- EndIf
- EndFunc ;==>OutofBound
- Func TriArea($dx, $dy, $e, $f)
- $tArea = Abs(($dx * ($e[1] - $f[1]) + $e[0] * ($f[1] - $dy) + $f[0] * ($dy - $e[1])) / 2)
- Return $tArea
- EndFunc ;==>TriArea
- Func ZoomOut() ;Zooms out
- Local $i = 0
- While 1
- Sleep(500)
- _CaptureRegion(0, 0, 860, 2)
- If _PixelGetColor_GetPixel(1, 1) = 0x000000 And _PixelGetColor_GetPixel(850, 1) = 0x000000 Then
- ExitLoop
- Else
- ControlSend("BlueStacks App Player", "", "", "{DOWN}", 0)
- $i += 1
- If $i = 40 Then
- ExitLoop
- EndIf
- EndIf
- WEnd
- EndFunc ;==>ZoomOut
- #EndRegion ##### ADDITIONAL #####
- ;~ If $sMatch Then ExitLoop
- ;~ _MultiPixelSearch2(305, 100, 555, 0, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, 3, -3, False) ;corner top
- ;~ If $sMatch Then ExitLoop
- ;~ _MultiPixelSearch2(165, 200, 630, 425, $pixel1, $x2offset, $y2offset, $pixel2, $x3offset, $y3offset, $pixel3, -3, 3, False) ;corner left
- ;~ If $sMatch Then ExitLoop
- ;~ _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