Advertisement
CocBotTBC

Parallel Search

Jan 17th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 9.65 KB | None | 0 0
  1. #include <Array.au3>
  2. #include <GUIConstantsEx.au3>
  3. #include <WindowsConstants.au3>
  4. #include <ColorConstants.au3>
  5. #include <GDIPlus.au3>
  6. #include <StaticConstants.au3>
  7. #include <WindowsConstants.au3>
  8. #include <WinAPIProc.au3>
  9. #include <ScreenCapture.au3>
  10. #include <Misc.au3>
  11. #include <TrayConstants.au3>
  12. #include <ColorConstants.au3>
  13. #include <GDIPlus.au3>
  14.  
  15. HotKeySet("^{SPACE}", "Terminate")
  16.  
  17. Global $gi_ChildProcParent = 0
  18. Global $gs_ChildProcReciverFunction = ""
  19. Global $gv_ChildProcReviverParameter = 0
  20. Global $_ChildProc_pid[1], $_ChildProc_num_children = 0
  21. Global $hBitmap
  22. Global $hHBitmap
  23. Global $BSpos[2]
  24. Global $a = 860 / 2, $b = 720 / 2
  25. Global $iMatch = False
  26. Global $user32 = DllOpen("user32.dll")
  27. Global $HWnD = WinGetHandle(WinGetTitle("BlueStacks App Player"))
  28.  
  29. Global $xSearch[6] = [430, 430, 430, 150, 500, 750]
  30. Global $ySearch[6] = [450, 250, 50, 310, 310, 310]
  31.  
  32. _GDIPlus_Startup()
  33. getBSPos()
  34. Global $sStart = TimerInit()
  35. _MultiPixelSearch($a, $b, 0x9F6B40, -2, 8, 0x976641, -2, 30, 0xF8FCFF)
  36. If Not $iMatch Then
  37.     ParallelSearch(6)
  38. EndIf
  39.  
  40. #Region ##### PARALLEL SEARCH #####
  41. Func ParallelSearch($nNum)
  42.     __ChildProcStartup()
  43.     _ChildProc_WriteToEnvVar("n", $nNum)
  44.     For $i = 0 To $nNum - 1
  45.         _ChildProc_WriteToEnvVar("x" & $i, $xSearch[$i])
  46.         _ChildProc_WriteToEnvVar("y" & $i, $ySearch[$i])
  47.         _ChildProc_Start("ProcessSearch", $i, 0)
  48.     Next
  49.     While _ChildProc_GetChildCount() > 0
  50.         Sleep(250)
  51.     WEnd
  52. EndFunc   ;==>ParallelSearch
  53. Func ProcessSearch($order)
  54.     $count = _ChildProc_ReadFromEnvVar("n")
  55.     $xX = _ChildProc_ReadFromEnvVar("x" & $order)
  56.     $yY = _ChildProc_ReadFromEnvVar("y" & $order)
  57.     _CaptureRegion()
  58.     _MultiPixelSearch($xX, $yY, 0x9F6B40, -2, 8, 0x976641, -2, 30, 0xF8FCFF)
  59.     If $iMatch Then
  60.         $sStop = Round(TimerDiff($sStart) / 1000, 2)
  61.         MsgBox(0, "", "Found in process [" & $order + 1 & "] of [" & $count & "] . Time: " & $sStop & " seconds.")
  62.     EndIf
  63. EndFunc   ;==>ProcessSearch
  64. #EndRegion ##### PARALLEL SEARCH #####
  65. #Region ##### PIXEL PROCESSING #####
  66. Func _MultiPixelSearch($x, $y, $1c, $2x, $2y, $2c, $3x, $3y, $3c, $sStep = 10, $sSingle = False)
  67.     While 1
  68.         If $y < 180 Or $y > 620 Or $x < 30 Or $x > 800 Then
  69.             ;SetLog("Out of bound (" & $x & ", " & $y & "). Time: " & Round(TimerDiff($cStart) / 1000, 2) & " seconds.")
  70.             $iMatch = False
  71.             ExitLoop
  72.         EndIf
  73.         If _ColorCheckVariation(_PixelGetColor_GetPixel($x, $y), Hex($1c, 6), 18) Then
  74.             For $m = $x - $sStep + 1 To $x + $sStep - 1
  75.                 For $n = $y - $sStep + 1 To $y + $sStep - 1
  76.                     If _ColorCheckVariation(_PixelGetColor_GetPixel($m, $n), Hex($1c, 6), 5) Then
  77.                         If _ColorCheckVariation(_PixelGetColor_GetPixel($m + $2x, $n + $2y), Hex($2c, 6), 5) Then
  78.                             If CheckColorVariPos($m + $3x, $n + $3y, $3c, 3, 5) Then
  79.                                 WinActivate($HWnD)
  80.                                 MouseMove($BSpos[0] + $m, $BSpos[1] + $n)
  81.                                 $iMatch = True
  82.                                 ExitLoop (3)
  83.                             EndIf
  84.                         EndIf
  85.                     EndIf
  86.                 Next
  87.             Next
  88.         EndIf
  89.         If Not $sSingle Then
  90.             If $x = $a And $y <= $b Then $x += $sStep
  91.             If $x >= $a And $y < $b Then
  92.                 _MultiPixelSearch($x + $sStep, $y + $sStep, $1c, $2x, $2y, $2c, $3x, $3y, $3c)
  93.             ElseIf $x > $a And $y >= $b Then
  94.                 _MultiPixelSearch($x - $sStep, $y + $sStep, $1c, $2x, $2y, $2c, $3x, $3y, $3c)
  95.             ElseIf $x <= $a And $y > $b Then
  96.                 _MultiPixelSearch($x - $sStep, $y - $sStep, $1c, $2x, $2y, $2c, $3x, $3y, $3c)
  97.             ElseIf $x < $a And $y <= $b Then
  98.                 _MultiPixelSearch($x + $sStep, $y - $sStep, $1c, $2x, $2y, $2c, $3x, $3y, $3c)
  99.             EndIf
  100.         EndIf
  101.         ExitLoop
  102.     WEnd
  103. EndFunc   ;==>_MultiPixelSearch
  104. Func _CaptureRegion($iLeft = 0, $iTop = 0, $iRight = 860, $iBottom = 720, $ReturnBMP = False)
  105.     _GDIPlus_BitmapDispose($hBitmap)
  106.     _WinAPI_DeleteObject($hHBitmap)
  107.     Local $iW = Number($iRight) - Number($iLeft), $iH = Number($iBottom) - Number($iTop)
  108.     Local $hDC_Capture = _WinAPI_GetWindowDC(ControlGetHandle("BlueStacks App Player", "", "[CLASS:BlueStacksApp; INSTANCE:1]"))
  109.     Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture)
  110.     $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $iW, $iH)
  111.     Local $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap)
  112.     DllCall($user32, "int", "PrintWindow", "hwnd", $HWnD, "handle", $hMemDC, "int", 0)
  113.     _WinAPI_SelectObject($hMemDC, $hHBitmap)
  114.     _WinAPI_BitBlt($hMemDC, 0, 0, $iW, $iH, $hDC_Capture, $iLeft, $iTop, 0x00CC0020)
  115.     $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
  116.     _WinAPI_DeleteDC($hMemDC)
  117.     _WinAPI_SelectObject($hMemDC, $hObjectOld)
  118.     _WinAPI_ReleaseDC($HWnD, $hDC_Capture)
  119. EndFunc   ;==>_CaptureRegion
  120. Func _PixelGetColor_GetPixel($iX, $iY)
  121.     Local $aPixelColor = _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
  122.     Return Hex($aPixelColor, 6)
  123. EndFunc   ;==>_PixelGetColor_GetPixel
  124. Func _PixelSearch($iLeft, $iTop, $iRight, $iBottom, $iColor, $ColorVariation)
  125.     _CaptureRegion($iLeft, $iTop, $iRight, $iBottom)
  126.     For $x = $iRight - $iLeft To 0 Step -1
  127.         For $y = $iBottom - $iTop To 0 Step -1
  128.             Local $nColor1 = $iColor
  129.             Local $nColor2 = _PixelGetColor_GetPixel($x, $y)
  130.             If _ColorCheckVariation($nColor1, $nColor2, $ColorVariation) Then
  131.                 Local $Pos[2] = [$iLeft + $x, $iTop + $y]
  132.                 Return $Pos
  133.             EndIf
  134.         Next
  135.     Next
  136.     Return 0
  137. EndFunc   ;==>_PixelSearch
  138. Func _ColorCheckVariation($nColor1, $nColor2, $sVari = 5)
  139.     Local $Red1, $Red2, $Blue1, $Blue2, $Green1, $Green2
  140.     $Red1 = Dec(StringMid(String($nColor1), 1, 2))
  141.     $Blue1 = Dec(StringMid(String($nColor1), 3, 2))
  142.     $Green1 = Dec(StringMid(String($nColor1), 5, 2))
  143.     $Red2 = Dec(StringMid(String($nColor2), 1, 2))
  144.     $Blue2 = Dec(StringMid(String($nColor2), 3, 2))
  145.     $Green2 = Dec(StringMid(String($nColor2), 5, 2))
  146.     If Abs($Blue1 - $Blue2) > $sVari Then Return False
  147.     If Abs($Green1 - $Green2) > $sVari Then Return False
  148.     If Abs($Red1 - $Red2) > $sVari Then Return False
  149.     Return True
  150. EndFunc   ;==>_ColorCheckVariation
  151. Func CheckColorVariPos($x, $y, $c, $vp = 0, $vc = 10)
  152.     For $a = $x - $vp To $x + $vp
  153.         For $b = $y - $vp To $y + $vp
  154.             If _ColorCheckVariation(_PixelGetColor_GetPixel($a, $b), Hex($c, 6), $vc) Then
  155.                 Return True
  156.             EndIf
  157.         Next
  158.     Next
  159.     Return False
  160. EndFunc   ;==>CheckColorVariPos
  161. #EndRegion ##### PIXEL PROCESSING #####
  162. #Region ##### CHILD PROCESS #####
  163. Func _ChildProc_Start($sFunction = Default, $vParameter = Default, $max_children = 0)
  164.     Local $iPid
  165.     If IsKeyword($sFunction) Or $sFunction = "" Then $sFunction = "__ChildProcDummy"
  166.     EnvSet("ChildProc", "0x" & Hex(StringToBinary($sFunction)))
  167.     EnvSet("ChildProcParent", @AutoItPID)
  168.     If Not IsKeyword($vParameter) Then
  169.         EnvSet("ChildProcParameterPresent", "True")
  170.         EnvSet("ChildProcParameter", StringToBinary($vParameter))
  171.     Else
  172.         EnvSet("ChildProcParameterPresent", "False")
  173.     EndIf
  174.     If $max_children > 0 And $_ChildProc_num_children >= $max_children Then
  175.         While _ChildProc_GetChildCount() >= $max_children
  176.             Sleep(250)
  177.         WEnd
  178.     EndIf
  179.     If @Compiled Then
  180.         $iPid = Run(FileGetShortName(@AutoItExe), @WorkingDir, @SW_HIDE, 1 + 2 + 4)
  181.     Else
  182.         $iPid = Run(FileGetShortName(@AutoItExe) & ' "' & @ScriptFullPath & '"', @WorkingDir, @SW_HIDE, 1 + 2 + 4)
  183.     EndIf
  184.     If @error Then SetError(1)
  185.     If $_ChildProc_pid[0] = "" Then
  186.         $_ChildProc_pid[0] = $iPid
  187.     Else
  188.         _ArrayAdd($_ChildProc_pid, $iPid)
  189.     EndIf
  190.     $_ChildProc_num_children = $_ChildProc_num_children + 1
  191.     Return $iPid
  192. EndFunc   ;==>_ChildProc_Start
  193. Func _ChildProc_WriteToEnvVar($sName, $vValue = Default)
  194.     Local $vTmp
  195.     $sName = "_CP_" & $sName
  196.     If $vValue = "" Or $vValue = Default Then
  197.         EnvSet($sName)
  198.         Return True
  199.     EndIf
  200.     EnvSet($sName, $vValue)
  201.     Return True
  202. EndFunc   ;==>_ChildProc_WriteToEnvVar
  203. Func _ChildProc_ReadFromEnvVar($sName, $fOption = 1)
  204.     $sName = "_CP_" & $sName
  205.     If $fOption = "" Or $fOption = Default Then $fOption = False
  206.     $value = EnvGet($sName)
  207.     If $fOption Then
  208.         EnvSet($sName)
  209.     EndIf
  210.     Return $value
  211. EndFunc   ;==>_ChildProc_ReadFromEnvVar
  212. Func _ChildProc_GetChildCount()
  213.     $num_children = 0
  214.     For $pid_num = 0 To (UBound($_ChildProc_pid) - 1)
  215.         If ProcessExists($_ChildProc_pid[$pid_num]) = True Then $num_children = $num_children + 1
  216.     Next
  217.     Return $num_children
  218. EndFunc   ;==>_ChildProc_GetChildCount
  219. Func __ChildProcDummy($vPar = Default)
  220.     If Not IsKeyword($vPar) Then _ChildProcReciver($vPar)
  221.     While ProcessExists($gi_ChildProcParent)
  222.         Sleep(500)
  223.     WEnd
  224. EndFunc   ;==>__ChildProcDummy
  225. Func __ChildProcStartup()
  226.     Local $sCmd = EnvGet("ChildProc")
  227.     If StringLeft($sCmd, 2) = "0x" Then
  228.         $sCmd = BinaryToString($sCmd)
  229.         $gi_ChildProcParent = Number(EnvGet("ChildProcParent"))
  230.         If StringInStr($sCmd, "(") And StringInStr($sCmd, ")") Then
  231.             Execute($sCmd)
  232.             If @error And Not @Compiled Then MsgBox(16, "ChildProc Error", "Unable to Execute: " & $sCmd)
  233.             Exit
  234.         EndIf
  235.         If EnvGet("ChildProcParameterPresent") = "True" Then
  236.             Call($sCmd, BinaryToString(EnvGet("ChildProcParameter")))
  237.             If @error And Not @Compiled Then MsgBox(16, "ChildProc Error", "Unable to Call: " & $sCmd & @LF & "Parameter: " & BinaryToString(EnvGet("ChildProcParameter")))
  238.         Else
  239.             Call($sCmd)
  240.             If @error And Not @Compiled Then MsgBox(16, "ChildProc Error", "Unable to Call: " & $sCmd)
  241.         EndIf
  242.         Exit
  243.     EndIf
  244. EndFunc   ;==>__ChildProcStartup
  245. #EndRegion ##### CHILD PROCESS #####
  246. #Region ##### ADDITIONAL #####
  247. Func getBSPos()
  248.     $aPos = ControlGetPos("BlueStacks App Player", "", "[CLASS:BlueStacksApp; INSTANCE:1]")
  249.     $tPoint = DllStructCreate("int X;int Y")
  250.     DllStructSetData($tPoint, "X", $aPos[0])
  251.     DllStructSetData($tPoint, "Y", $aPos[1])
  252.     _WinAPI_ClientToScreen(WinGetHandle(WinGetTitle("BlueStacks App Player")), $tPoint)
  253.     $BSpos[0] = DllStructGetData($tPoint, "X")
  254.     $BSpos[1] = DllStructGetData($tPoint, "Y")
  255.     $CtrlHandle = ControlGetHandle("BlueStacks App Player", "", "BlueStacksApp1")
  256. EndFunc   ;==>getBSPos
  257. Func Terminate()
  258.     Exit
  259. EndFunc   ;==>Terminate
  260. #EndRegion ##### ADDITIONAL #####
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement