Somniis Posted July 4, 2006 Posted July 4, 2006 If any of you more experienced AutoIT coders would not mind, I would like some coding advice on things I can do better. Whatever you see that could be shorter, made easier, or what-not, please tell me. (note that I edited some of this due to privacy issues) [autoit]#include <GUIConstants.au3> #NoTrayIcon Opt("TrayMenuMode", 1) Opt("TrayOnEventMode", 1) Opt("WinTitleMatchMode", 4) Global Const $VER = "1.3" DirCreate(@ScriptDir & "\data") Global Const $INI = @ScriptDir & "\data\skillup.ini" Global Const $DLDIR = @ScriptDir & "\data\SkillUp.zip" Global $WinHandle = '', $ExeName = 'pol.exe' Global $con = 0 IniWrite($INI, "Data", "Ver", $VER) Global $r_Ini = IniRead($INI, "Startup", "message", "") Global $r_Ini_alogin = IniRead($INI, "Data", "ALogin", "") Global $r_Ini_alogout = IniRead($INI, "Data", "ALogout", "") Global $r_Ini_restmp = IniRead($INI, "Data", "MPRest", "") Global $r_Ini_maxloops = IniRead($INI, "Data", "MaxLoops", "") Global $r_Ini_minrest = IniRead($INI, "Data", "MinRest", "") Global $r_Ini_charslot = IniRead($INI, "Data", "CharSlot", "") Global $r_Ini_saved = IniRead($INI, "Data", "Saved", "") If $r_Ini = 0 Then $gui_INI = GUICreate("First Time Startup - Information", 400, 400, -1, -1, $WS_BORDER, -1) GUICtrlCreateLabel( 'SkillMan v' & $VER & 'edited out', 10, 20, 380, 300) $Closer = GUICtrlCreateButton("Close", 150, 320, 100, 30, $BS_ICON) GUICtrlSetImage(-1, "shell32.dll", 131) GUISetState(@SW_SHOW, $gui_INI) While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Or $msg = $Closer Then GUIDelete($gui_INI) ExitLoop EndIf WEnd IniWrite($INI, "Startup", "message", "1") EndIf $gui_MAIN = GUICreate("SkillMan v" & $VER, 292, 257, (@DesktopWidth-292)/2, (@DesktopHeight-257)/2) $CharGroup = GUICtrlCreateGroup("Character Slot", 16, 24, 113, 89) $Slot1 = GUICtrlCreateRadio("Slot 1", 32, 48, 73, 17) If $r_Ini_charslot = 2 Then GUICtrlSetState($Slot1, $GUI_UNCHECKED) Else GUICtrlSetState($Slot1, $GUI_CHECKED) EndIf $Slot2 = GUICtrlCreateRadio("Slot 2", 32, 80, 73, 17) If $r_Ini_charslot = 2 Then GUICtrlSetState($Slot2, $GUI_CHECKED) EndIf GUICtrlCreateGroup("", -99, -99, 1, 1) $MPREST = GUICtrlCreateCheckbox("Rest MP", 152, 96, 81, 17) If $r_Ini_restmp = 0 And $r_Ini_saved = 1 Then GUICtrlSetState($MPREST, $GUI_UNCHECKED) ElseIf $r_Ini_saved = 0 Or $r_Ini_restmp = 1 Then GUICtrlSetState($MPREST, $GUI_CHECKED) EndIf $MPRestH = GUICtrlCreateButton("?", 240, 96, 35, 17, 0) $LOG = GUICtrlCreateCheckbox("Auto-logout", 152, 64, 81, 17) If $r_Ini_alogout = 1 Then GUICtrlSetState($LOG, $GUI_CHECKED) EndIf $ALogoutH = GUICtrlCreateButton("?", 240, 64, 35, 17, 0) $AUTOL = GUICtrlCreateCheckbox("Auto-login", 152, 32, 81, 17) If $r_Ini_alogin = 1 Then GUICtrlSetState($AUTOL, $GUI_CHECKED) EndIf $ALHelp = GUICtrlCreateButton("?", 240, 32, 35, 17, 0) GUICtrlCreateLabel("Maximum amount of macro loops:", 24, 128, 161, 17) $MacroMax = GUICtrlCreateInput("10", 192, 128, 35, 21, $ES_NUMBER) GUICtrlSetLimit(-1, 2) If $r_Ini_maxloops <> 10 And $r_Ini_saved = 1 Then GUICtrlSetData($MacroMax, $r_Ini_maxloops) EndIf $UpDown = GUICtrlCreateUpdown($MacroMax, -1) GUICtrlSetLimit(-1, 50, 10) $HLoop = GUICtrlCreateButton("?", 240, 128, 35, 17, 0) GUICtrlCreateLabel("Minutes to rest for MP:", 72, 152, 110, 17) $MinRest = GUICtrlCreateInput("2", 192, 152, 35, 21, $ES_NUMBER) GUICtrlSetLimit(-1, 2) If $r_Ini_restmp = 0 And $r_Ini_saved = 1 Then GUICtrlSetState($MinRest, $GUI_DISABLE) EndIf If $r_Ini_minrest <> 2 And $r_Ini_saved = 1 Then GUICtrlSetData($MinRest, $r_Ini_minrest) EndIf $MinRestUD = GUICtrlCreateUpdown($MinRest, -1) GUICtrlSetLimit(-1, 10, 1) $MinRestH = GUICtrlCreateButton("?", 240, 152, 35, 17, 0) $BEGIN = GUICtrlCreateButton("Start", 16, 184, 75, 57, $BS_ICON) GUICtrlSetImage(-1, "shell32.dll", 165) GUICtrlSetTip(-1, "Start the bot with above settings") $WEBSITE = GUICtrlCreateButton("Update", 112, 184, 75, 57, $BS_ICON) GUICtrlSetImage(-1, "shell32.dll", 135) GUICtrlSetTip(-1, "Check for newer version") $EXIT = GUICtrlCreateButton("Exit", 208, 184, 75, 57, $BS_ICON) GUICtrlSetImage(-1, "shell32.dll", 131) GUICtrlSetTip(-1, "Exit the bot") Global $MP_REST = 1 If $r_Ini_saved = 1 And $r_Ini_restmp <> 1 Then $MP_REST = 0 EndIf Global $LOGOUT = 0 If $r_Ini_saved = 1 And $r_Ini_alogout <> 0 Then $LOGOUT = 1 EndIf Global $AUTOLOG = 0 If $r_Ini_saved = 1 And $r_Ini_alogin <> 0 Then $AUTOLOG = 1 EndIf Global $CharSlot1 = 1 Global $CharSlot2 = 0 If $r_Ini_saved = 1 And $r_Ini_charslot = 2 Then $CharSlot1 = 0 $CharSlot2 = 1 EndIf Global $MAX_COUNT = 10 If $r_Ini_saved = 1 And $r_Ini_maxloops <> 10 Then $MAX_COUNT = $r_Ini_maxloops EndIf Global $MAX_SLEEP = 2 If $r_Ini_saved = 1 And $r_Ini_minrest <> 2 Then $MAX_SLEEP = $r_Ini_minrest EndIf Global Const $CURRENT = "edited" Global Const $CVER = "edited" GUISetState(@SW_SHOW, $gui_MAIN) While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE Or $msg = $EXIT _Tray_Exit() Case $msg = $MPRestH $msgmph = MsgBox(32, "About MP Rest", "edited") Case $msg = $ALogoutH $msgalogouth = MsgBox(32, "About Auto-logout", "edited") Case $msg = $ALHelp $msgaloginh = MsgBox(32, "About Auto-login", "edited") Case $msg = $HLoop $msghelploop = MsgBox(32, "About Maximum macro loop", "edited") Case $msg = $MinRestH $msghelpminrest = MsgBox(32, "MP Resting time", "edited") Case $msg = $MPREST $state = GUICtrlRead($MPREST) If $state = $GUI_CHECKED Then GUICtrlSetState($MinRest, $GUI_ENABLE) Else GUICtrlSetState($MinRest, $GUI_DISABLE) EndIf Case $msg = $WEBSITE Dim $sd = 0 Dim $nv = 0 ;If $fe = 0 Then Dim $d = InetGet($CVER, @ScriptDir & "\data\check.ini") If $d = 0 Then MsgBox(16, "Error", "Cannot connect to server. Please check internet settings!") FileDelete(@ScriptDir & "\data\check.ini") $sd = 1 EndIf If $sd = 0 Then Dim $c_Ini = IniRead(@ScriptDir & "\data\check.ini", "Data", "Ver", "") Dim $this_Ini = IniRead($INI, "Data", "Ver" , "") If $this_Ini < $c_Ini Then $nv = 1 ElseIf $this_Ini = $c_Ini Then $nv = 0 EndIf If $nv = 1 And $sd = 0 Then Dim $size = InetGetSize($CURRENT) If $size < 100000 Then ;retrieves webpage instead of file if small size Msgbox(16, "Error", "Download server is down. Please try again later.") FileDelete(@ScriptDir & "\data\check.ini") $sd = 1 EndIf If $sd = 0 And $nv = 1 Then Dim $kb = Round($size / 1024, 0) ;convert to kilobytes and chop off decimal places $m1 = MsgBox(4, "New version available", "New version(" & $c_Ini & ") is " & $kb & "kb. Download?") If $m1 = 6 Then ;Yes Dim $dl = InetGet($CURRENT, $DLDIR, 1, 1) If $dl = 0 Then MsgBox(16, "Error", "Cannot connect to server. Please check internet settings!") EndIf ProgressOn("Downloading...", "Retrieving new version.") While @InetGetActive Dim $p = (100 * @InetGetBytesRead) / $size Dim $kbi = Round(@InetGetBytesRead / 1024, 0) ProgressSet($p, $kbi & "/" & $kb & " kilobytes", "Download in progress.") Sleep(130) WEnd ProgressOff() FileDelete(@ScriptDir & "\data\check.ini") $m2 = MsgBox(4, "Done", "The file was downloaded to: " & $DLDIR & @LF & _ @LF & _ @LF & _ "Close this window and open the file?") If $m2 = 6 Then Run(@ComSpec & " /c Start " & FileGetShortName($DLDIR), "", @SW_MINIMIZE) ExitLoop ;exit current ver to open newer EndIf EndIf EndIf ElseIf $nv = 0 Then MsgBox(64, "Update complete", "No newer version available.") FileDelete(@ScriptDir & "\data\check.ini") EndIf EndIf ;EndIf Case $msg = $BEGIN Dim $shutd = GUICtrlRead($LOG) Dim $al = GUICtrlRead($AUTOL) Dim $mp = GUICtrlRead($MPREST) Dim $s1r = GUICtrlRead($Slot1) $MAX_COUNT = GUICtrlRead($MacroMax) $MAX_SLEEP = GUICtrlRead($MinRest) Dim $infoLOGO, $infoALOGO, $infoMP, $infoCH, $infoSL If $shutd = $GUI_CHECKED Then $LOGOUT = 1 $infoLOGO = "Yes" ElseIf $shutd = $GUI_UNCHECKED Then $LOGOUT = 0 $infoLOGO = "No" EndIf If $al = $GUI_CHECKED Then $AUTOLOG = 1 $infoALOGO = "Yes" ElseIf $al = $GUI_UNCHECKED Then $AUTOLOG = 0 $infoALOGO = "No" EndIf If $mp = $GUI_CHECKED Then $MP_REST = 1 $infoMP = "Yes" ElseIf $mp = $GUI_UNCHECKED Then $MP_REST = 0 $infoMP = "No" EndIf If $s1r = $GUI_CHECKED Then $CharSlot1 = 1 $CharSlot2 = 0 $infoCH = "Slot 1" ElseIf $s1r = $GUI_UNCHECKED Then $CharSlot1 = 0 $CharSlot2 = 1 $infoCH = "Slot 2" EndIf If $MAX_COUNT >= 51 Then $MAX_COUNT = 50 GUICtrlSetData($MacroMax, "50") MsgBox(16, "Error", "Loop amount greater than allowed; setting to 50.") EndIf If $MAX_COUNT <= 9 Then $MAX_COUNT = 10 GUICtrlSetData($MacroMax, "10") MsgBox(16, "Error", "Loop amount less than allowed; setting to 10.") EndIf If $MAX_SLEEP >= 11 Then $MAX_SLEEP = 10 GUICtrlSetData($MinRest, 10) MsgBox(16, "Error", "Sleep time over the maximum. Setting to 10 minutes.") EndIf If $MAX_SLEEP <= 0 Then $MAX_SLEEP = 1 GUICtrlSetData($MinRest, 1) MsgBox(16, "Error", "Sleep time below the minimum. Setting to 1 minute.") EndIf Dim $checkslot If $s1r = $GUI_CHECKED Then $checkslot = 1 ElseIf $s1r = $GUI_UNCHECKED Then $checkslot = 2 EndIf ; checking values to see if ini needs to be written to ; if the ini has never been saved before, ask If $r_Ini_saved = 0 Then $notsaved = MsgBox(52, "Save settings?", "Do you want to save your current settings?") If $notsaved = 6 Then IniWrite($INI, "Data", "ALogin", $AUTOLOG) IniWrite($INI, "Data", "ALogout", $LOGOUT) IniWrite($INI, "Data", "MPRest", $MP_REST) IniWrite($INI, "Data", "MaxLoops", $MAX_COUNT) IniWrite($INI, "Data", "MinRest", $MAX_SLEEP) IniWrite($INI, "Data", "CharSlot", $checkslot) IniWrite($INI, "Data", "Saved", "1") MsgBox(64, "Saved", "Settings saved!") EndIf EndIf ; debug only ;Msgbox(64, "Debug", $MP_REST & "," & $LOGOUT & "," & $AUTOLOG & "," & $MAX_COUNT & "," & $CharSlot2) ;MsgBox(64, "Debug", "MAX_SLEEP: " & $MAX_SLEEP & " " & "Converted: " & _Convert($MAX_SLEEP) & "ms") Sleep(400) If GUICtrlRead($MPREST) = $GUI_CHECKED Then $infoSL = $MAX_SLEEP & " minute(s)" Else $infoSL = "Not applicable" EndIf Opt("TrayIconHide", 0) Opt("SendKeyDelay", 1100) Opt("SendKeyDownDelay", 1100) GUISetState(@SW_HIDE, $gui_MAIN) ; Tray menu $exit_tray = TrayCreateItem("Exit SkillMan") TrayItemSetOnEvent(-1, "_Tray_Exit") TraySetState() Sleep(1000) MsgBox(64, "Information", "Press OK ONLY when the edited is running, not edited." & @LF & _ @LF & _ "Rest for MP: " & $infoMP & @LF & _ "Minutes to rest: " & $infoSL & @LF & _ "Auto-logout: " & $infoLOGO & @LF & _ "Auto-login: " & $infoALOGO & @LF & _ "Amount of loops: " & $MAX_COUNT & @LF & _ "Character Slot: " & $infoCH & @LF & _ @LF & _ "Right-click the icon in the taskbar and select Exit to stop the bot.") $WinHandle = _GetWindowHandle($ExeName) WinWaitActive($WinHandle) Sleep(1500) If $AUTOLOG = 1 Then TrayTip("Starting...", "The bot is now auto-logging you in." & @LF & _ @LF & _ "Please do not press any keys during this process.", 2, 1) _A_LOGIN() ElseIf $AUTOLOG = 0 Then TrayTip("Starting...", "You have one minute to log in.", 2, 1) Sleep(61000) EndIf TrayTip("Started!", "The bot has started!" & @LF & _ @LF & _ "Please do not press any keys while the bot is running.", 5, 2) Dim $count = 0 Do Send("{F1}"); target player Sleep(5000) Send("^1") Sleep(Random(16000, 21000, 1)) Send("^2") Sleep(Random(16000, 21000, 1)) Send("^3") Sleep(Random(16000, 21000, 1)) Send("^4") Sleep(Random(17000, 21000, 1)) Send("^5") Sleep(Random(12000, 16000, 1)) $count = $count + 1 If $MP_REST = 1 Then Select Case $count = 3 _HEAL_MP() Case $count = 6 _HEAL_MP() Case $count = 9 _HEAL_MP() Case $count = 12 _HEAL_MP() Case $count = 15 _HEAL_MP() Case $count = 18 _HEAL_MP() Case $count = 21 _HEAL_MP() Case $count = 24 _HEAL_MP() Case $count = 27 _HEAL_MP() Case $count = 30 _HEAL_MP() Case $count = 33 _HEAL_MP() Case $count = 36 _HEAL_MP() Case $count = 39 _HEAL_MP() Case $count = 42 _HEAL_MP() Case $count = 45 _HEAL_MP() Case $count = 48 _HEAL_MP() EndSelect EndIf Until $count = $MAX_COUNT If $LOGOUT = 1 Then Opt("SendKeyDelay", 400) Opt("SendKeyDownDelay", 400) Send("{ESCAPE}") Sleep(2000) Send("/shutdown") Sleep(2000) Send("{ENTER}") Sleep(1000) TrayTip("Exiting...", "Exiting in 5 seconds.", 2, 1) Sleep(5000) ExitLoop ElseIf $LOGOUT = 0 Then TrayTip("Exiting...", "Exiting in 5 seconds.", 2, 1) Sleep(5000) ExitLoop EndIf Case Else ;;;;;;; EndSelect WEnd Func _A_LOGIN() Sleep(3000) Send("{ENTER}") Sleep(10000) Send("{ENTER}") Sleep(3000) If $CharSlot2 = 1 Then Opt("SendKeyDownDelay", 5) Send("{DOWN 1}") Opt("SendKeyDownDelay", 1100) EndIf Sleep(4500) Send("{ENTER}") Sleep(1000) Send("{ENTER}") Sleep(28000) EndFunc ;==>_A_LOGIN Func _HEAL_MP() Dim $sleeptime = _Convert($MAX_SLEEP) Send("{ESCAPE}");ensure player is not targeted - cannot heal Send("{NumPadMult}") Sleep($sleeptime) Send("{NumPadMult}") EndFunc ;==>_HEAL_MP Func _Tray_Exit() GUIDelete($gui_MAIN) MsgBox(262208, "", "...der freie Denker", 1) Exit EndFunc ;==>_Tray_Exit Func _Convert($minutes) $convert = $minutes * 60000 Return($convert) EndFunc ;==>_Convert Func _GetWindowHandle($iPIDExe) $iPIDExe = ProcessExists($iPIDExe) Local $aWinlist = WinList(), $aProcList = ProcessList($iPIDExe) For $iCount = 1 To $aWinlist[0][0] If WinGetProcess($aWinlist[$iCount][0]) = $iPIDExe _ And BitAND(WinGetState($aWinlist[$iCount][1]), 2) _ And $aWinlist[$iCount][0] <> '' Then Return $aWinlist[$iCount][0] EndIf Next EndFunc ;==>_GetWindowHandle thanks to Smoke_N Exit
Somniis Posted July 4, 2006 Author Posted July 4, 2006 (edited) No, this isn't for Diablo. I'd rather not say which game it is for.. privacy issues. This bot is simply a "skillup" bot, to raise desired magic skills in a certain game. Also, if you wouldn't mind deleting the code you replied with, as it would make the thread a lot "smaller". Edited July 4, 2006 by Somniis
marfdaman Posted July 4, 2006 Posted July 4, 2006 Don't have all that much comments, but you could replace all the Select Case $msg = ... EndSelect oÝ÷ Û«¢+ÙMÝ¥Ñ ÀÌØíµÍ(% ͸¸¸)¹MÝ¥ÑoÝ÷ Ù©l¡«¢+ÙI½Õ¹ ÀÌØíÍ¥é¼ÄÀÈаÀ does not need to have ', 0' defined, as that's the default. As for the change detection, you could for example throw in an AdlibEnable. FileRead the ini file at the start of your script and stores it in a Constant oslt, and then with the adlib set to, say 1000 ms you can fileread it again and if the two strings are not the same, $r_Ini_saved = 1. Again, nothing serious here Alzo Don't take my pic to serious...~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You Looked, but you did not see!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Somniis Posted July 4, 2006 Author Posted July 4, 2006 Thank you marf. I didn't even think of using Switch! :">
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now