|
You last visited: Today at 21:20
Advertisement
Help needed (Metin bot autoit)
Discussion on Help needed (Metin bot autoit) within the AutoIt forum part of the Coders Den category.
05/15/2020, 18:22
|
#1
|
elite*gold: 0
Join Date: Apr 2015
Posts: 1
Received Thanks: 0
|
Help needed (Metin bot autoit)
Hi guys!
Can anyone help me with this code? I wrote this but after a few mins or hours maybe its get crashed. Stack overflow. An expert in autoit maybe laught at it but its my first try.
I understood the effect of overflow but cant fix it sadly.
Here is the Code:
WinWaitActive("Mt2Mester - Verzió: 7.9.3")
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("!{PAUSE}","Quit")
Dim $Paused
TogglePause()
While 1
Start()
WEnd
func Start()
ToolTip('Pixeling.',0,0)
Sleep(600)
ToolTip("")
While 1
$cords = PixelSearch(12,47,951,702,0xEB1609,5)
if not(@error) Then
MouseClick("left",$cords[0],$cords[1],1,3)
Sleep(500)
Send("{f1}")
Sleep(Random(200, 400, 1))
Send("{2}")
Sleep(Random(100, 300, 1))
Close()
Else
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")
Return Start()
EndIf
WEnd
EndFunc
func Close()
ToolTip('Pixeling.',0,0)
Sleep(600)
ToolTip("")
While 1
$cords = PixelSearch(406,337,689,487,0xEB1609,5)
if not(@error) Then
Send("{f1}")
MouseClick("left",$cords[0],$cords[1],1,3)
Sleep(1000)
Send("{space}")
Send("{2}")
Sleep(Random(100, 300, 1))
combatheal()
Else
Return Start()
EndIf
WEnd
EndFunc
Func TogglePause()
$Paused = NOT $Paused
While $Paused
WEnd
Start()
EndFunc
Func combatheal()
While 1
$cords = PixelSearch(147,717,147,717,0x222222,1)
if not(@error) Then
Sleep(Random(100, 200, 1))
Send("{4 2}")
Return combatheal()
Else
Sleep(Random(100, 200, 1))
Return Close()
EndIf
WEnd
EndFunc
func Quit()
Exit
EndFunc
Some info:
This is a basic metin2 bot or script.
Pause key = activate/deactivate
Start() = begin searching red colour and if its find it do some basic attack and jump to
Close() = another pixelsearch again for red colour but in closerange
Combatheal() = pixelsearch, monitoring hp bar
IF the Start() did not find any red(monster hpbar)
doing some basic movement for seeking new targets.
I dont understand where is the endless loop or what is excatly the endless loop in this script.
If anybody can help me with few code snippets or some instruction to fix it myself I will be very happy.
Im thankful for the answers and any help 
Sry for the pathetic language use.. its not my first.
Thx, Kipper
|
|
|
05/16/2020, 19:46
|
#2
|
elite*gold: 0
Join Date: Apr 2011
Posts: 363
Received Thanks: 167
|
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:
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.
|
|
|
All times are GMT +1. The time now is 21:21.
|
|