[Guide] Scripted Health Check.

12/31/2009 02:37 Theri#1
Didn't realize there was now a sub-forum, suppose this is in the wrong spot then, oh well. :p

This is written for AutoIt. Something similar would also work in AutoHotKey.

It's also not much of a tutorial, the code is short and straight forward. If you can get something out of it, great. This is more or less an example of something you can do that's pretty easy but useful.

Below I break it up into three sections with comments for pretty much everything, at the bottom is the code without any comments at all.

This only works on character with less than 100 health. If over 100 health you would have to increase $lifebar[0] and change the "36" at the bottom if you want to test this on a character with over 100 health.

Lets declare variables.

Code:
;Declare our variables, we'll put them into global scope.

Global $colorhex,$variation,$percent,$lifebar[3]

;The hex code of the pixel color we're search for.

$colorhex = 0xCD398D

;x coord start of life bar.  I'm pretty sure it really starts at 162 on my screen.
;Hower we dont start searching till after the health text, in this example we're
;starting at x coord 198, this will vary if your health is higher than the character
;I used in this example.

$lifebar[0] = 198

;Y coord of life bar.

$lifebar[1] = 736 

;X coord end of life bar. Not the actual end which is more like 262. 258 is the last pixel that can max our $colorhex.

$lifebar[2] = 258

;How much variation to use in PixelSearch, we want no variation so we set to 0.

$variation = 0
Now lets switch to our game, pause a second and then we'll call our function to check health. After we check our health bar it pops up a message box stating your health percent. Its not totally accurate but its close enough in this given example.

Code:
;Switch to your game screen.

WinActivate("Mabinogi : Dragon")

;x.X

sleep(2000)

;Call our function.

_lifecheck()

;Just a little msg box displaying your health percent, - 1% in my limited testing.

msgbox(0,"Health Percent",$percent)
And now the function itself.

Code:
;Start of our function code.
Func _lifecheck()
	;Loop our search
	Do
		PixelSearch($lifebar[0],$lifebar[1],$lifebar[0],$lifebar[1],$colorhex,$variation)
		;PixelSearch sets @error so that if @error is defined our pixel wasn't found.
		If Not @error Then
		;Found our $colorhex so we'll increase our X Coord by 1 to search in a line.
		$lifebar[0] +=1
		ElseIf @error Then
		;Our $colorhex was not found which means our health bar is not full.
		EndIf
		;We loop until we don't match our pixel color
		;or we reach the end of the bar.
	Until @error or $lifebar[0] = $lifebar[2]
	If $lifebar[0] = $lifebar[2] Then
		;Health bar was full, give or take. :p
		$percent = 100
	Else
		;Oh noes.
		$percent = 36 + ($lifebar[0]-198)			
		;If our health bar starts at 162 and ends at 262 thats 100 pixels.
		;We start at 198 or 36 pixels in. So that 36 plus the difference between
		;where our search ended minus the starting point equals our percentage.
	EndIf
EndFunc
All together without comments.

Code:
Global $colorhex,$variation,$percent,$lifebar[3]
$colorhex = 0xCD398D
$lifebar[0] = 198
$lifebar[1] = 736 
$lifebar[2] = 258
$variation = 0

WinActivate("Mabinogi : Dragon")
sleep(2000)

_lifecheck()
msgbox(0,"Health Percent",$percent)


Func _lifecheck()
	Do
		PixelSearch($lifebar[0],$lifebar[1],$lifebar[0],$lifebar[1],$colorhex,$variation)
		If Not @error Then
		$lifebar[0] +=1
		ElseIf @error Then
		EndIf
	Until @error or $lifebar[0] = $lifebar[2]
	If $lifebar[0] = $lifebar[2] Then
		$percent = 100
	Else
		$percent = 36 + ($lifebar[0]-198)			
	EndIf
EndFunc
01/05/2010 20:18 pawntobishop#2
Interesting... useful if you want to preserve the life of your bots i suppose.
01/05/2010 20:24 Theri#3
It has its uses, can be extended to check mana or stamina. There's a faster way to do this than what I posted but if someone was really interested in it they could probably figure it out. ;)
02/16/2010 05:30 pawntobishop#4
This is a really cool snippit of code. Bumping.
02/17/2010 00:23 Theri#5
What are you, a spam bot now? :p
02/17/2010 03:54 pawntobishop#6
I'm not a spam bot, I just feel some valid posts should be on the front page rather then crap thats outdated and full of flames.
02/17/2010 19:30 Theri#7
I see. ;) Well a better version of this is in my bot functions thread.
02/17/2010 21:27 pawntobishop#8
I do enjoy your competence with bots.
02/18/2010 22:54 BamIPwnedYou#9
I agree with pawntobishop, your bot scripts are pretty beastly.