Advertisement
AutoitHacks

Draw Bot - au3 Script

Mar 27th, 2012
4,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 13.76 KB | None | 0 0
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_UseUpx=n
  3. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  4. #include <WindowsConstants.au3>
  5. #include <GuiConstantsEx.au3>
  6. #include <EditConstants.au3>
  7. #include <GUIConstants.au3>
  8. #include <WinAPI.au3>
  9. #include <Color.au3>
  10. #include <Misc.au3>
  11.  
  12. If (Not HotKeySet("{F9}", "Nothing")) Then
  13.     MsgBox(16, "Error", "Could not register the F9 hotkey.")
  14.     Exit
  15. EndIf
  16. If (Not HotKeySet("{F10}", "Quit")) Then
  17.     MsgBox(16, "Error", "Could not register the F10 hotkey.")
  18.     Exit
  19. EndIf
  20.  
  21. Global $iX1, $iY1, $iX2, $iY2, $aPos, $sMsg, $sBMP_Path
  22. Global $threshold, $memDc
  23. Global $image, $GUI, $drwstate = False
  24. Global $width
  25. Global $height
  26. Global $pixels
  27. Global $pathString = "12345678"
  28. Global $scramble = False
  29. Global $rotate = 0
  30. Global $speed
  31.  
  32. #Region ### START Koda GUI section ### Form=
  33. $hMain_GUI = GUICreate("Drawball Buddy", 258, 75, 782, 291)
  34. GUICtrlCreateTab(0, 0, 257, 73)
  35. GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
  36. GUICtrlCreateTabItem("Functions")
  37. $Slider1 = GUICtrlCreateSlider(44, 25, 209, 17)
  38. GUICtrlSetLimit(-1, 45, 3)
  39. $Slider2 = GUICtrlCreateSlider(44, 49, 209, 17)
  40. GUICtrlSetLimit(-1, 45, 3)
  41. $Paint_X = GUICtrlCreateButton("Paint", 4, 25, 41, 17, $WS_GROUP)
  42. $DrawBtn = GUICtrlCreateButton("Trace", 4, 49, 41, 17, $WS_GROUP)
  43. GUICtrlCreateTabItem("About")
  44. GUICtrlCreateLabel("Author: THAT1ANONYMOUSDUDE", 16, 24, 177, 17)
  45. GUICtrlCreateLabel("Web: AutoRepo.tk", 16, 48, 93, 17)
  46. GUICtrlCreateTabItem("")
  47. GUISetState(@SW_SHOW)
  48. #EndRegion ### END Koda GUI section ###
  49.  
  50. While 1
  51.     Switch GUIGetMsg()
  52.         Case $GUI_EVENT_CLOSE
  53.             FileDelete(@ScriptDir & "\Rect.bmp")
  54.             Exit
  55.         Case $Paint_X
  56.             GUISetState(@SW_HIDE, $hMain_GUI)
  57.             _Region_Select()
  58.             TrayTip("Painting Canvas", 'Press "F10" to stop', 5, 1)
  59.             _Draw()
  60.             GUISetState(@SW_SHOW, $hMain_GUI)
  61.         Case $DrawBtn
  62.             GUISetState(@SW_HIDE, $hMain_GUI)
  63.             $imageFile = FileOpenDialog("Select Image", @WorkingDir, "Images (*.jpg;*.jpeg;*.gif;*.png;*.bmp)", 1)
  64.             _Imagedraw()
  65.             GUISetState(@SW_SHOW, $hMain_GUI)
  66.             GUICtrlSetState($DrawBtn, $GUI_DISABLE)
  67.     EndSwitch
  68. WEnd
  69.  
  70. Func _Draw()
  71.     ;Sleep(2000)
  72.     For $y = $iY1 To $iY2 Step +GUICtrlRead($Slider1)
  73.         For $x = $iX1 To $iX2 Step +GUICtrlRead($Slider2)
  74.             BlockInput(1)
  75.             MouseClick('LEFT', $x, $y, 1, 1)
  76.             BlockInput(0)
  77.             Sleep(1)
  78.             If $drwstate Then ExitLoop
  79.         Next
  80.         If $drwstate Then ExitLoop
  81.     Next
  82.     $drwstate = False
  83.     Return 1
  84. EndFunc   ;==>_Draw
  85.  
  86. ; -------------
  87.  
  88. Func _Region_Select()
  89.  
  90.     Local $aMouse_Pos, $hMask, $hMaster_Mask, $iTemp
  91.     Local $UserDLL = DllOpen("user32.dll")
  92.     Global $hRectangle_GUI = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
  93.  
  94.     _GUICreateInvRect($hRectangle_GUI, 0, 0, 1, 1)
  95.  
  96.     GUISetBkColor(0)
  97.     WinSetTrans($hRectangle_GUI, "", 50)
  98.     GUISetState(@SW_SHOW, $hRectangle_GUI)
  99.     GUISetCursor(3, 1, $hRectangle_GUI)
  100.  
  101.     ; Wait until mouse button pressed
  102.     While Not _IsPressed("01", $UserDLL)
  103.         Sleep(10)
  104.     WEnd
  105.  
  106.     ; Get first mouse position
  107.     $aMouse_Pos = MouseGetPos()
  108.     $iX1 = $aMouse_Pos[0]
  109.     $iY1 = $aMouse_Pos[1]
  110.  
  111.     ; Draw rectangle while mouse button pressed
  112.     While _IsPressed("01", $UserDLL)
  113.  
  114.         $aMouse_Pos = MouseGetPos()
  115.  
  116.         ; Set in correct order if required
  117.         If $aMouse_Pos[0] < $iX1 Then
  118.             $iX_Pos = $aMouse_Pos[0]
  119.             $iWidth = $iX1 - $aMouse_Pos[0]
  120.         Else
  121.             $iX_Pos = $iX1
  122.             $iWidth = $aMouse_Pos[0] - $iX1
  123.         EndIf
  124.         If $aMouse_Pos[1] < $iY1 Then
  125.             $iY_Pos = $aMouse_Pos[1]
  126.             $iHeight = $iY1 - $aMouse_Pos[1]
  127.         Else
  128.             $iY_Pos = $iY1
  129.             $iHeight = $aMouse_Pos[1] - $iY1
  130.         EndIf
  131.  
  132.         _GUICreateInvRect($hRectangle_GUI, $iX_Pos, $iY_Pos, $iWidth, $iHeight)
  133.  
  134.         Sleep(10)
  135.  
  136.     WEnd
  137.  
  138.     ; Get second mouse position
  139.     $iX2 = $aMouse_Pos[0]
  140.     $iY2 = $aMouse_Pos[1]
  141.  
  142.     ; Set in correct order if required
  143.     If $iX2 < $iX1 Then
  144.         $iTemp = $iX1
  145.         $iX1 = $iX2
  146.         $iX2 = $iTemp
  147.     EndIf
  148.     If $iY2 < $iY1 Then
  149.         $iTemp = $iY1
  150.         $iY1 = $iY2
  151.         $iY2 = $iTemp
  152.     EndIf
  153.  
  154.     GUIDelete($hRectangle_GUI)
  155.     DllClose($UserDLL)
  156.  
  157. EndFunc   ;==>_Region_Select
  158.  
  159. Func _GUICreateInvRect($hWnd, $iX, $iY, $iW, $iH)
  160.  
  161.     $hMask_1 = _WinAPI_CreateRectRgn(0, 0, @DesktopWidth, $iY)
  162.     $hMask_2 = _WinAPI_CreateRectRgn(0, 0, $iX, @DesktopHeight)
  163.     $hMask_3 = _WinAPI_CreateRectRgn($iX + $iW, 0, @DesktopWidth, @DesktopHeight)
  164.     $hMask_4 = _WinAPI_CreateRectRgn(0, $iY + $iH, @DesktopWidth, @DesktopHeight)
  165.  
  166.     _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_2, 2)
  167.     _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_3, 2)
  168.     _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_4, 2)
  169.  
  170.     _WinAPI_DeleteObject($hMask_2)
  171.     _WinAPI_DeleteObject($hMask_3)
  172.     _WinAPI_DeleteObject($hMask_4)
  173.  
  174.     _WinAPI_SetWindowRgn($hWnd, $hMask_1, 1)
  175.  
  176. EndFunc   ;==>_GUICreateInvRect
  177.  
  178. ;////////////////////////////////////////////////
  179.  
  180. Func _Imagedraw()
  181.  
  182.     ;; Options dialog ;;
  183.     $optGUI = GUICreate("Settings", 190, 270, -1, -1, $WS_CAPTION, BitOR($WS_EX_APPWINDOW, $WS_EX_TOOLWINDOW))
  184.     GUICtrlCreateGroup("Image processing", 10, 5, 150, 85)
  185.     GUICtrlCreateLabel("Sensitivity (0~255):", 10, 28, 110, 15)
  186.     $thresholdInput = GUICtrlCreateInput("100", 115, 25, 35, 20, $ES_NUMBER)
  187.     GUICtrlCreateLabel("Width (px):", 10, 48, 110, 15)
  188.     $widthInput = GUICtrlCreateInput("100", 115, 45, 35, 20, $ES_NUMBER)
  189.     GUICtrlCreateLabel("Height (px):", 10, 68, 110, 15)
  190.     $heightInput = GUICtrlCreateInput("100", 115, 65, 35, 20, $ES_NUMBER)
  191.     GUICtrlCreateGroup("Drawing pattern", 5, 95, 150, 140)
  192.     $horizontalRadio = GUICtrlCreateRadio("Horizontal", 10, 115, 110, 15)
  193.     $verticalRadio = GUICtrlCreateRadio("Vertical", 10, 135, 110, 15)
  194.     $diagonalRadio = GUICtrlCreateRadio("Diagonal", 10, 155, 110, 15)
  195.     $rotateRadio = GUICtrlCreateRadio("Spiral", 10, 175, 110, 15)
  196.     $scrambleRadio = GUICtrlCreateRadio("Random", 10, 195, 110, 15)
  197.     GUICtrlSetState($diagonalRadio, $GUI_CHECKED)
  198.     GUICtrlCreateLabel("Mouse speed (0~100):", 10, 213, 110, 15)
  199.     $speedInput = GUICtrlCreateInput("0", 125, 210, 25, 20, $ES_NUMBER)
  200.     $okBtn = GUICtrlCreateButton("Ok", 30, 245, 40, 20)
  201.     $cancelBtn = GUICtrlCreateButton("Cancel", 80, 245, 50, 20)
  202.     GUISetState()
  203.  
  204.     While 1
  205.         Switch (GUIGetMsg())
  206.             Case $GUI_EVENT_CLOSE
  207.                 Exit
  208.             Case $cancelBtn
  209.                 Exit
  210.             Case $okBtn
  211.                 $threshold = GUICtrlRead($thresholdInput)
  212.                 $width = GUICtrlRead($widthInput)
  213.                 $height = GUICtrlRead($heightInput)
  214.                 $speed = GUICtrlRead($speedInput)
  215.                 If (GUICtrlRead($horizontalRadio) == $GUI_CHECKED) Then
  216.                     $pathString = "45273618"
  217.                 ElseIf (GUICtrlRead($verticalRadio) == $GUI_CHECKED) Then
  218.                     $pathString = "27453618"
  219.                 ElseIf (GUICtrlRead($diagonalRadio) == $GUI_CHECKED) Then
  220.                     $pathString = "36184527"
  221.                 ElseIf (GUICtrlRead($rotateRadio) == $GUI_CHECKED) Then
  222.                     $pathString = "14678532"
  223.                     $rotate = 1
  224.                 ElseIf (GUICtrlRead($scrambleRadio) == $GUI_CHECKED) Then
  225.                     $scramble = True
  226.                 EndIf
  227.  
  228.                 GUIDelete($optGUI)
  229.                 ExitLoop
  230.         EndSwitch
  231.     WEnd
  232.  
  233.     ;; Processing dialog ;;
  234.     $GUI = GUICreate("Processing image...", $width, $height + 20, -1, -1, $WS_CAPTION, BitOR($WS_EX_APPWINDOW, $WS_EX_TOOLWINDOW))
  235.     GUISetBkColor(0xffffff)
  236.     $imageBox = GUICtrlCreatePic($imageFile, 0, 0, $width, $height)
  237.     $progress = GUICtrlCreateProgress(0, $height, $width, 20)
  238.     GUISetState()
  239.  
  240.  
  241.  
  242.     ;; Get image pixels ;;
  243.     $dc = _WinAPI_GetDC($GUI)
  244.     $memDc = _WinAPI_CreateCompatibleDC($dc)
  245.     $bitmap = _WinAPI_CreateCompatibleBitmap($dc, $width, $height)
  246.     _WinAPI_SelectObject($memDc, $bitmap)
  247.     _WinAPI_BitBlt($memDc, 0, 0, $width, $height, $dc, 0, 0, $SRCCOPY)
  248.     $bits = DllStructCreate("dword[" & ($width * $height) & "]")
  249.     DllCall("gdi32", "int", "GetBitmapBits", "ptr", $bitmap, "int", ($width * $height * 4), "ptr", DllStructGetPtr($bits))
  250.     GUICtrlDelete($imageBox)
  251.  
  252.     ;; Process the pixels ;;
  253.     Dim $pixels[$width][$height]
  254.     For $y = 0 To ($height - 1)
  255.         For $x = 0 To ($width - 1)
  256.             $index = ($y * $width) + $x
  257.             $color = DllStructGetData($bits, 1, $index)
  258.             $red = _ColorGetBlue($color)
  259.             $green = _ColorGetGreen($color)
  260.             $blue = _ColorGetRed($color)
  261.             $shade = ($red + $green + $blue) / 3
  262.             If ($shade > $threshold) Then
  263.                 $color = 0xffffff
  264.                 $pixels[$x][$y] = 0
  265.             Else
  266.                 $color = 0
  267.                 $pixels[$x][$y] = 1
  268.             EndIf
  269.             DllStructSetData($bits, 1, $color, $index)
  270.         Next
  271.  
  272.         DllCall("gdi32", "int", "SetBitmapBits", "ptr", $bitmap, "int", ($width * $height * 4), "ptr", DllStructGetPtr($bits))
  273.         _WinAPI_BitBlt($dc, 0, 0, $width, $height, $memDc, 0, 0, $SRCCOPY)
  274.         GUICtrlSetData($progress, ($y * 100) / $height)
  275.     Next
  276.  
  277.     _WinAPI_ReleaseDC($GUI, $dc)
  278.     GUIRegisterMsg($WM_PAINT, "OnPaint")
  279.  
  280.     ;; Ready to draw ;;
  281.     TrayTip("Image Ready", "Press 'F9' to print at mouse position, 'F10' to quit.", 10)
  282.     HotKeySet("{F9}", "Draw")
  283.     HotKeySet("{F10}", "Quit")
  284.     Return 1
  285. EndFunc   ;==>_Imagedraw
  286.  
  287. Func OnPaint($hwndGUI, $msgID, $wParam, $lParam)
  288.     Local $paintStruct = DllStructCreate("hwnd hdc;int fErase;dword rcPaint[4];int fRestore;int fIncUpdate;byte rgbReserved[32]")
  289.  
  290.     $dc = DllCall("user32", "hwnd", "BeginPaint", "hwnd", $hwndGUI, "ptr", DllStructGetPtr($paintStruct))
  291.     $dc = $dc[0]
  292.  
  293.     _WinAPI_BitBlt($dc, 0, 0, $width, $height, $memDc, 0, 0, $SRCCOPY)
  294.  
  295.     DllCall("user32", "hwnd", "EndPaint", "hwnd", $hwndGUI, "ptr", DllStructGetPtr($paintStruct))
  296.     Return $GUI_RUNDEFMSG
  297. EndFunc   ;==>OnPaint
  298.  
  299. Func Draw()
  300.     $mouseCenter = MouseGetPos()
  301.     $x0 = $mouseCenter[0] - ($width / 2)
  302.     $y0 = $mouseCenter[1] - ($height / 2)
  303.  
  304.     ;; Draw all the areas ;;
  305.     $stack = CreateStack(1000)
  306.     For $y = 0 To ($height - 1)
  307.         For $x = 0 To ($width - 1)
  308.             If ($pixels[$x][$y] == 1) Then
  309.                 BlockInput(1)
  310.                 MouseMove($x + $x0, $y + $y0, $speed)
  311.                 MouseDown("primary")
  312.                 DrawArea($stack, $x, $y, $x0, $y0)
  313.                 MouseUp("primary")
  314.                 BlockInput(0)
  315.             Else
  316.             EndIf
  317.             If $drwstate Then ExitLoop
  318.         Next
  319.         If $drwstate Then ExitLoop
  320.     Next
  321.     $drwstate = False
  322.     ;; Reset the pixels statuses ;;
  323.     For $y = 0 To ($height - 1) Step 1
  324.         For $x = 0 To ($width - 1) Step 1
  325.             If ($pixels[$x][$y] == 2) Then
  326.                 $pixels[$x][$y] = 1
  327.             EndIf
  328.         Next
  329.     Next
  330. EndFunc   ;==>Draw
  331.  
  332. Func DrawArea(ByRef $stack, $x, $y, $x0, $y0)
  333.     Local $path[8]
  334.     Local $continue
  335.  
  336.     $path = StringSplit($pathString, "")
  337.  
  338.     While 1
  339.         MouseMove($x + $x0, $y + $y0, $speed)
  340.         $pixels[$x][$y] = 2
  341.  
  342.         If ($scramble) Then ScramblePath($path)
  343.         If ($rotate > 0) Then RotatePath($path, $rotate)
  344.  
  345.         ;;;;;;;;;;;;;;;;;;;
  346.         ;; +---+---+---+ ;;
  347.         ;; | 1 | 2 | 3 | ;;
  348.         ;; +---+---+---+ ;;
  349.         ;; | 4 |   | 5 | ;;
  350.         ;; +---+---+---+ ;;
  351.         ;; | 6 | 7 | 8 | ;;
  352.         ;; +---+---+---+ ;;
  353.         ;;;;;;;;;;;;;;;;;;;
  354.  
  355.         $continue = False
  356.         For $i = 0 To 7
  357.             Switch ($path[$i])
  358.                 Case 1
  359.                     If (($x > 0) And ($y > 0)) Then
  360.                         If ($pixels[$x - 1][$y - 1] == 1) Then
  361.                             Push($stack, $x, $y)
  362.                             $x -= 1
  363.                             $y -= 1
  364.                             $continue = True
  365.                             ExitLoop
  366.                         EndIf
  367.                     EndIf
  368.  
  369.                 Case 2
  370.                     If ($y > 0) Then
  371.                         If ($pixels[$x][$y - 1] == 1) Then
  372.                             Push($stack, $x, $y)
  373.                             $y -= 1
  374.                             $continue = True
  375.                             ExitLoop
  376.                         EndIf
  377.                     EndIf
  378.  
  379.                 Case 3
  380.                     If (($x > 0) And ($y < 0)) Then
  381.                         If ($pixels[$x + 1][$y - 1] == 1) Then
  382.                             Push($stack, $x, $y)
  383.                             $x += 1
  384.                             $y -= 1
  385.                             $continue = True
  386.                             ExitLoop
  387.                         EndIf
  388.                     EndIf
  389.  
  390.                 Case 4
  391.                     If ($x > 0) Then
  392.                         If ($pixels[$x - 1][$y] == 1) Then
  393.                             Push($stack, $x, $y)
  394.                             $x -= 1
  395.                             $continue = True
  396.                             ExitLoop
  397.                         EndIf
  398.                     EndIf
  399.  
  400.                 Case 5
  401.                     If ($x < ($width - 1)) Then
  402.                         If ($pixels[$x + 1][$y] == 1) Then
  403.                             Push($stack, $x, $y)
  404.                             $x += 1
  405.                             $continue = True
  406.                             ExitLoop
  407.                         EndIf
  408.                     EndIf
  409.  
  410.                 Case 6
  411.                     If (($x < 0) And ($y > 0)) Then
  412.                         If ($pixels[$x - 1][$y + 1] == 1) Then
  413.                             Push($stack, $x, $y)
  414.                             $x -= 1
  415.                             $y += 1
  416.                             $continue = True
  417.                             ExitLoop
  418.                         EndIf
  419.                     EndIf
  420.  
  421.                 Case 7
  422.                     If ($y < ($height - 1)) Then
  423.                         If ($pixels[$x][$y + 1] == 1) Then
  424.                             Push($stack, $x, $y)
  425.                             $y += 1
  426.                             $continue = True
  427.                             ExitLoop
  428.                         EndIf
  429.                     EndIf
  430.  
  431.                 Case 8
  432.                     If (($x < ($width - 1)) And ($y < ($height - 1))) Then
  433.                         If ($pixels[$x + 1][$y + 1] == 1) Then
  434.                             Push($stack, $x, $y)
  435.                             $x += 1
  436.                             $y += 1
  437.                             $continue = True
  438.                             ExitLoop
  439.                         EndIf
  440.                     EndIf
  441.             EndSwitch
  442.         Next
  443.         If ($continue) Then ContinueLoop
  444.  
  445.         If (Not Pop($stack, $x, $y)) Then ExitLoop
  446.     WEnd
  447. EndFunc   ;==>DrawArea
  448.  
  449. Func ScramblePath(ByRef $path)
  450.     Local $table = "12345678"
  451.     Local $newPath[8]
  452.  
  453.     For $i = 8 To 1 Step -1
  454.         $next = StringMid($table, Random(1, $i, 1), 1)
  455.         $newPath[$i - 1] = Number($next)
  456.         $table = StringReplace($table, $next, "")
  457.     Next
  458.  
  459.     $path = $newPath
  460. EndFunc   ;==>ScramblePath
  461.  
  462. Func RotatePath(ByRef $path, $places)
  463.     If ($places == 0) Then
  464.         Return $path
  465.     Else
  466.         For $i = 1 To Abs($places)
  467.             $temp = $path[7]
  468.             $path[7] = $path[6]
  469.             $path[6] = $path[5]
  470.             $path[5] = $path[4]
  471.             $path[4] = $path[3]
  472.             $path[3] = $path[2]
  473.             $path[2] = $path[1]
  474.             $path[1] = $path[0]
  475.             $path[0] = $temp
  476.         Next
  477.     EndIf
  478. EndFunc   ;==>RotatePath
  479.  
  480. Func CreateStack($size)
  481.     Dim $stack[$size + 1][2]
  482.     $stack[0][0] = 0
  483.     $stack[0][1] = $size
  484.     Return $stack
  485. EndFunc   ;==>CreateStack
  486.  
  487. Func Push(ByRef $stack, $x, $y)
  488.     $stack[0][0] += 1
  489.     If ($stack[0][0] > $stack[0][1]) Then
  490.         $stack[0][1] += 1000
  491.         ReDim $stack[$stack[0][1] + 1][2]
  492.     EndIf
  493.  
  494.     $stack[$stack[0][0]][0] = $x
  495.     $stack[$stack[0][0]][1] = $y
  496. EndFunc   ;==>Push
  497.  
  498. Func Pop(ByRef $stack, ByRef $x, ByRef $y)
  499.     If ($stack[0][0] < 1) Then
  500.         Return False
  501.     EndIf
  502.  
  503.     $x = $stack[$stack[0][0]][0]
  504.     $y = $stack[$stack[0][0]][1]
  505.  
  506.     $stack[0][0] -= 1
  507.     Return True
  508. EndFunc   ;==>Pop
  509.  
  510. Func Nothing()
  511. EndFunc   ;==>Nothing
  512.  
  513. Func Quit()
  514.     BlockInput(0)
  515.     MouseUp("primary")
  516.     $drwstate = True
  517.     TrayTip("Operation Halt", "All Bot Operations Have Benn Halted", 3, 1)
  518. EndFunc   ;==>Quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement