Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > AutoIt
You last visited: Today at 21:20

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Help needed (Metin bot autoit)

Discussion on Help needed (Metin bot autoit) within the AutoIt forum part of the Coders Den category.

Reply
 
Old   #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:


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
fullmetaltits is offline  
Old 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.
elmarcia is offline  
Thanks
1 User
Reply




All times are GMT +1. The time now is 21:21.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.