Hi there im glad to help. I will explain first what is wrong with your code and propose better approach for this problem.
1) You have lots of while loop that aren't even necessary, just with one is more than enough.
2) You have a recursive problem that will fill up the stack.
What is recursion?, is when you call a function over an over again without a return condition.
Func Start()
while 1
;some code ...
return Start()
endFunc
Start is calling itself in a while loop, even though you have some if that wont call the function, eventually it will and fill up stack after some execution time.
Solution:
Don't use recursive calls if you don't have a finish condition.
My approach:
Coding bots is easy, first step is to think which steps are important and simply write a function for each step.
In your bot you have 4 main states, and one independant state (paused), take a look at this state machine:
[Only registered and activated users can see links. Click Here To Register...]
If you don't know what a state machine is, is as simple as saying which conditions have to be met to change state, arrows with text written means condition have to be true to go to pointed state, arrows with no text means change state to pointing state.
Following that state machine logic we got this script:
Code:
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("!{PAUSE}","Close")
#Region State Machine Functions
Func EnemySearch()
;Search enemy bar
$cords = PixelSearch(12,47,951,702,0xEB1609,5)
Return IsArray($cords)
EndFunc
Func AttackEnemy()
;inner state of Attacking state
$state = "attacking"
;Heal check important to be in top
If(haveLowHealth()) Then
$state = "heal"
Return $state
EndIf
;Switch attacking enemy state to killed if close target was killed
$cords = isTargetAlive()
If(@error) Then
$state = "killed"
Return $state
EndIf
;Continue attacking otherwise
MouseClick("left",$cords[0],$cords[1],1,3)
Sleep(500)
Send("{f1}")
Sleep(Random(200, 400, 1))
Send("{2}")
Sleep(Random(100, 300, 1))
Return $state
EndFunc
Func Wander()
Sleep(Random(100, 300, 1))
Send("{w down}") ; Holds the A key down
Sleep(Random(1000, 2000, 1))
Send("{w up}") ; Releases the A key
Sleep(Random(700, 1100, 1))
MouseClick("left",82,133,1,3)
Sleep(Random(1000, 2100, 1))
MouseClick("left",876,128,1,3)
Sleep(Random(1000, 2100, 1))
MouseClick("left",940,617,1,3)
Sleep(Random(1000, 2100, 1))
MouseMove(595,374)
MouseDown("right")
MouseMove(612,374)
MouseUp("right")
EndFunc
Func Heal()
Sleep(Random(100, 200, 1))
Send("{4 2}")
EndFunc
#EndRegion
#Region AttackState sub states
Func isTargetAlive()
;Search close target alive
$cords = PixelSearch(406,337,689,487,0xEB1609,5)
If @error Then Return SetError(1)
Return $cords
EndFunc
Func haveLowHealth()
;Check low health threshold
$cords = PixelSearch(147,717,147,717,0x222222,1)
Return IsArray($cords)
EndFunc
#EndRegion
Func TogglePause()
$isPaused = Not $isPaused ; toggle pause state
if($isPaused) Then
$LAST_STATE = $CURRENT_STATE
$CURRENT_STATE = $STATE_PAUSED
Else
$CURRENT_STATE = $LAST_STATE
EndIf
EndFunc
Func Close()
Exit 0
EndFunc
Global Const $STATE_ENEMY_SEARCH = "searching"
Global Const $STATE_ATTACKING = "attacking"
Global Const $STATE_HEAL = "healing"
Global Const $STATE_WANDER = "wandering"
Global Const $STATE_PAUSED = "paused"
Global $isPaused = True
Global $CURRENT_STATE = $STATE_PAUSED
Global $LAST_STATE = $STATE_ENEMY_SEARCH ; for pause purposes
;Only change state if not paused
Func changeState($newState)
if(Not $isPaused) Then $CURRENT_STATE = $newState
EndFunc
Func mainLoop()
While Sleep(5) ; prevent high cpu ussage
ToolTip($CURRENT_STATE,0,0)
Switch $CURRENT_STATE
Case $STATE_ENEMY_SEARCH
$found = EnemySearch()
If($found) Then
changeState($STATE_ATTACKING)
Else
changeState($STATE_WANDER)
EndIf
Case $STATE_ATTACKING
$state = AttackEnemy()
if($state == "killed") Then
changeState($STATE_ENEMY_SEARCH)
ElseIf ($state == "heal") Then
changeState($STATE_HEAL)
EndIf
Case $STATE_HEAL
Heal()
changeState($STATE_ATTACKING)
Case $STATE_WANDER
Wander()
changeState($STATE_ENEMY_SEARCH)
Case $STATE_PAUSED
Sleep(10)
EndSwitch
WEnd
EndFunc
;call main loop function
mainLoop()
mainLoop switch is based on State machine diagram, i hope is easy to follow.