using PixelSearch to automate.

Ask gaming related questions (AHK v1.1 and older)
Who885
Posts: 1
Joined: 25 May 2016, 21:37

using PixelSearch to automate.

25 May 2016, 21:43

Hello, i play this game and i decided to give scripting a try. i use 2 programs one to actually script and another to get the RGB colors(Macro creator) Image In this game you can shadow spar for power. so i tried to make it so when the shadow spar appears it moves up, down, left and right so it can hit the shadow spar. for whatever reason i cannot get it to do this. almost like its not detecting it at all. iv used imageSearch too but not only did it not work iv found it to consistently move in the wrong direction. any help?
John
Posts: 78
Joined: 10 Apr 2014, 08:59

Re: using PixelSearch to automate.

26 May 2016, 05:09

Here's something I quickly cooked up that should yield satisfactory results.
There is some 'debugging' things to help you with understanding how it works and what you should be looking for when figuring out what went wrong and where. It's usually a good idea to use paint, notepad, etc. simple programs to make sure your script works in the first place and it's not your target program causing the issues. Also use messageboxes and tooltips heavily to pinpoint the issue.

The main difference being that this one is using a single pixelsearch instead of many smaller ones and determining the 'direction' with logics/math/stuff. Since the search area is so small (in your searches roughly 44*44, 4 times), bulk of the time goes in 'initializing' the PixelSearch while searching the entire 133*136 area once will have to 'initialize' it only once resulting in almost 4 times faster searches while covering more ground.

Code: Select all

Debug := true ;for testing/visualizing purposes
if (Debug) { ;when using something like paint to test the script, in paint the color "0xAB9E7E" is red: 28, green: 51, blue:  140
	coordmode,tooltip,screen
	coordmode,pixel,screen
}

space::
x1 := 660, x2 := 793 ;the left and right x position of the search area
y1 := 482, y2 := 621 ;the top and bottom y position of the search area
center_x := x1+(x2-x1)/2 ;center point of the search area, along x_axis
center_y := y1+(y2-y1)/2 ;center point of the search area, along y_axis
deadzone := (x2-x1)/3 ;the size of the area to ignore, i.e. width of the search area divided by 3, giving us ~44 pixel area that doesn't trigger actions
if (Debug) { ;displays tooltips on each side of the search area, offset slightly so they don't obstruct the search area
	tooltip,up,%center_x%,% y1 - 20,5
	tooltip,down,%center_x%,% y2,6
	tooltip,left,% x1 - 30,%center_y%,7
	tooltip,right,% x2,%center_y%,8
}
loop % (Debug) ? "1" : a_tickcount { ;if 'debugging' then repeat the loop only once, otherwise repeat 'forever' ("a_tickcount" is a really high number so almost 'comparable' to infinite loop)
	PixelSearch, found_x, found_y, %x1%, %y1%, %x2%, %y2%,0xAB9E7E, 0, Fast RGB
	if (!ErrorLevel) { ;ErrorLevel is set to 0 if the color is found, so "!ErrorLevel" -> ErrorLevel is false -> false = 0, true = 1
		dir_x := found_x-center_x ;getting the direction on x-axis
		dir_y := found_y-center_y ;getting the direction on y-axis
		if (abs(dir_x) > deadzone || abs(dir_y) > deadzone) { ;check that at least one is greater than the deadzone area
			if (abs(dir_x) > abs(dir_y)) { ;if the movement on x_axis is greater than on y, then...
				send_str := (dir_x < 0) ? "{left}" : "{right}" ;if dir_x is less than 0 then the point was on the left side so put "left" into "send_str", otherwise "right"
			}
			else { ;else y was greater than x
				send_str := (dir_y < 0) ? "{up}" : "{down}" ;same
			}
			if (Debug) ;depending on debug, either show a tooltip or send the key
				tooltip % send_str "`n" dir_x "|" dir_y
			else
				send % send_str ;send the string we determined before
			}
		}
	}
	else if (Debug) ;clear the tooltip if nothing was found
		tooltip
}
return

rshift::exitapp ;to close the script if things go haywire
..and here is the script without the debugging stuff and comments, how it'd look like in use,

Code: Select all

=::
x1 := 660, x2 := 793
y1 := 482, y2 := 621
center_x := x1+(x2-x1)/2
center_y := y1+(y2-y1)/2
deadzone := (x2-x1)/3
loop {
	PixelSearch, found_x, found_y, %x1%, %y1%, %x2%, %y2%,0xAB9E7E, 0, Fast RGB
	if (!ErrorLevel) {
		dir_x := found_x-center_x
		dir_y := found_y-center_y
		if (abs(dir_x) > deadzone || abs(dir_y) > deadzone) {
			if (abs(dir_x) > abs(dir_y)) {
				send_str := (dir_x < 0) ? "{left}" : "{right}"
			}
			else {
				send_str := (dir_y < 0) ? "{up}" : "{down}"
			}
			send % send_str
		}
	}
}
return

rshift::exitapp ;to close the script if things go haywire
Edit: added the deadzone variable to emulate similar behavior as in your's which i'm guessing is/was detecting change from default position.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 62 guests