Radial Outward ImageSearch Topic is solved

Ask gaming related questions (AHK v1.1 and older)
JaminRoyale
Posts: 6
Joined: 08 May 2020, 12:07

Radial Outward ImageSearch

Post by JaminRoyale » 18 May 2022, 20:52

Hello,

Example: 1920x1080 pixels. Split the screen into 192x108 10x10 pixel segments. Then starting from the center 1920/2 and 1080/2, ImageSearch loop 10x10 segments radially outward until object found.

I was thinking of generating a Text file with the coords of each top left segment, but to do this by hand is not gunna work. Maybe someone can point me in a better direction.

*The point of all this is to find the object that is closest to the center of the screen.
**If i were to start making a list it would be

X1=(1920/2)-10 , Y1=(1080/2)-10
X2=(1920/2)-10, Y2=1080/2
X3=1920/2 , Y3=(1080/2)-10
X4=(1920/2) ,Y4=(1080/2)

Thanks.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Radial Outward ImageSearch  Topic is solved

Post by mikeyww » 18 May 2022, 21:22

Welcome to this AutoHotkey forum!

Code: Select all

image  = %A_ScriptDir%\test.png
incr  := 10
cx    := A_ScreenWidth  / 2
cy    := A_ScreenHeight / 2
If !FileExist(image)
 MsgBox, 48, Error, File not found.`n`n%image%
CoordMode, Pixel
CoordMode, Mouse
Loop {
 SoundBeep, 1500
 add := incr / 2 * A_Index
 x1  := Max(cx - add, 0)
 y1  := Max(cy - add, 0)
 x2  := Min(cx + add, A_ScreenWidth)
 y2  := Min(cy + add, A_ScreenHeight)
 MouseMove, x1, y1
 ImageSearch, x, y, x1, y1, x2, y2, %image%
} Until (x1 = 0 && y1 = 0 && x2 = A_ScreenWidth && y2 = A_ScreenHeight || x > "")
If (x > "")
     MsgBox, 64, Found    , Found at (%x%, %y%)
Else MsgBox, 48, Not found, Not found!
Return

JaminRoyale
Posts: 6
Joined: 08 May 2020, 12:07

Re: Radial Outward ImageSearch

Post by JaminRoyale » 18 May 2022, 23:27

Thanks for the reply,

Code: Select all

image  = %A_ScriptDir%\graphics\rock.png  ;set the image
incr  := 10                               ;set the pixel increment
cx    := A_ScreenWidth  / 2               ;Center of the screen X
cy    := A_ScreenHeight / 2               ;Center of the screen Y
If !FileExist(image)
 MsgBox, 48, Error, File not found.`n`n%image%      ;Tell the user if the image was not found
CoordMode, Pixel
CoordMode, Mouse
Loop {
 SoundBeep, 1500                      ;Everyloop, play a sound to for testing
 add := incr / 2 * A_Index            ;5 times whatever the index loop is 1, 2, 3, 4 etc
 x1  := Max(cx - add, 0)              ;Top left Search Area
 y1  := Max(cy - add, 0)              ;
 MouseMove, x1, y1                    ;Show the top left of search grid using the mouse
 sleep 100
 x2  := Min(cx + add, A_ScreenWidth)  ;Bottom Right of Search Area
 y2  := Min(cy + add, A_ScreenHeight) ;
 MouseMove, x2, y2                    ;Show the bottom right of search grid using the mouse
 ImageSearch, x, y, x1, y1, x2, y2,*rockImageSensitivity %image%                     
} Until (x1 = 0 && y1 = 0 && x2 = A_ScreenWidth && y2 = A_ScreenHeight || x > "")   
If (x > "")
     MsgBox, 64, Found    , Found at (%x%, %y%)
Else MsgBox, 48, Not found, Not found!
Return
[/quote]

I changed the mouse movement pattern a bit to help me be sure of what grid it was searching. This does indeed search radially. Now to get imagesearch to actually hit

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Radial Outward ImageSearch

Post by mikeyww » 19 May 2022, 04:22

You got it. The script that I posted did work here with my test image.

I'm not sure what you are trying to do with your variation parameter, but it might be a bug with your syntax there.

JaminRoyale
Posts: 6
Joined: 08 May 2020, 12:07

Re: Radial Outward ImageSearch

Post by JaminRoyale » 22 May 2022, 21:47

Code: Select all

 MovementTimer:
SoundBeep, 750, 20
send {%WalkActionHorizontal% up}			;Release Previous Key presses
send {%WalkActionVertical% up}
if (MovementFlag = 0)					;If Control Logic Dictates standing still, return out of timer	
		return
if (MovementFlag = 1)					;Control dictates movement. Here Each set of angles cooresponds to the movement keys WASD in 8 directions
{
	;MsgBox Moving
	if (AngleToRock > 1*vPi/8 && AngleToRock <=3*vPi/8)	;Up right
	{
		SoundBeep, 1000, 60
		WalkActionHorizontal := d
		WalkActionVertical := w
	}
	if (AngleToRock > 3*vPi/8 && AngleToRock <=5*vPi/8)	;Up
	{
		WalkActionHorizontal := ""
		WalkActionVertical := s
	}
	if (AngleToRock > 5*vPi/8 && AngleToRock <=7*vPi/8)	;Up left
	{
		WalkActionHorizontal := a
		WalkActionVertical := s
	}
	if (AngleToRock > 7*vPi/8 && AngleToRock <=9*vPi/8)	;Left
	{
		WalkActionHorizontal := a
		WalkActionVertical := ""
	}
	if (AngleToRock > 9v/8 && AngleToRock <=11*vPi/8)	;Down left
	{
		WalkActionHorizontal := a
		WalkActionVertical := s
	}
	if (AngleToRock > 11*vPi/8 && AngleToRock <=13*vPi/8)	;Down
	{
		WalkActionHorizontal := ""
		WalkActionVertical := s
	}
	if (AngleToRock > 13*vPi/8 && AngleToRock <=15*vPi/8)	;Down Right
	{
		WalkActionHorizontal := d
		WalkActionVertical := s
	}
	if (AngleToRock > 15*vPi/8 && AngleToRock <=1*vPi/8)	;Right
	{
		WalkActionHorizontal := d
		WalkActionVertical := ""
	}
	send  {%WalkActionHorizontal% down}
	send {%WalkActionVertical% down}
	SoundBeep, 800, 60
}
Return
im using this code after finding the closest image, but i cant actually get it to send the key presses. i thought i did it this way in the past but cant get it to work.

JaminRoyale
Posts: 6
Joined: 08 May 2020, 12:07

Re: Radial Outward ImageSearch

Post by JaminRoyale » 23 May 2022, 00:50

okay this is so bizarre. I created this hotkey for testing

Code: Select all

global WalkActionHorizontal := "d"
global WalkActionVertical := "w"
^g::
send {%WalkActionVertical% down}
Return
and this works fine when i press it before anything else. But when i run my other hotkey to try to set WalkAction through its control logic, pressing my old cntrl-g hotkey no longer works.

something in this code below is borked and i dont understand what

Code: Select all

ineRock() ;^ is contrl and f:: is the hotkey
{

MouseData := ClosestRock()	;MouseData will store the data from my function called ClosestRock
if (MouseData = "")
	return
xPixelsToRock := MouseData.x - cx                       ;Establish cartesian coords for x and and y from the center of the screen. This is a new origin of (0,0)
yPixelsToRock := (-1*(MouseData.y)) + cy                  ; Convert the -y into + due to being opposite intution. opposite cause imagesearhc dictates top left as 0,0 and bottom right as 1920,1080
;MsgBox, %xPixelsToRock% and %yPixelsToRock%
DistanceToRock := Sqrt((xPixelsToRock**2) + (yPixelsToRock**2))    ;Calculate the distance to the closest rock in a straight line from (0,0)
AngleToRock := ATan2(yPixelsToRock, xPixelsToRock)				;Use ArcTan and Opposite over Adjacent to calcualte the angle the rock is from the player
;MsgBox %DistanceToRock% "is the distance and" %AngleToRock% "is the angle"
if(DistanceToRock > 80)								;Tell the movement logic to move closer to the minable
{
	MovementFlag := 1
	SoundBeep, 750, 20
	send {%WalkActionHorizontal% up}			;Release Previous Key presses
	send {%WalkActionVertical% up}
	if (MovementFlag = 0)					;If Control Logic Dictates standing still, return out of timer	
		return
	if (MovementFlag = 1)					;Control dictates movement. Here Each set of angles cooresponds to the movement keys WASD in 8 directions
	{
	;MsgBox Moving
		SoundBeep, 850, 30
		if (AngleToRock > 1*vPi/8 && AngleToRock <= 3*vPi/8)	;Up right
		{
			SoundBeep, 1200, 60
			WalkActionHorizontal := "d"
			WalkActionVertical := "w"
		}
		if (AngleToRock > 3*vPi/8 && AngleToRock <= 5*vPi/8)	;Up
		{
			WalkActionHorizontal := ""
			WalkActionVertical := s
		}
		if (AngleToRock > 5*vPi/8 && AngleToRock <= 7*vPi/8)	;Up left
		{
			WalkActionHorizontal := a
			WalkActionVertical := s
		}
		if (AngleToRock > 7*vPi/8 && AngleToRock <= 9*vPi/8)	;Left
		{
			WalkActionHorizontal := a
			WalkActionVertical := ""
		}
		if (AngleToRock > 9v/8 && AngleToRock <= 11*vPi/8)	;Down left
		{
			WalkActionHorizontal := a
			WalkActionVertical := s
		}
		if (AngleToRock > 11*vPi/8 && AngleToRock <= 13*vPi/8)	;Down
		{
			WalkActionHorizontal := ""
			WalkActionVertical := s
		}
		if (AngleToRock > 13*vPi/8 && AngleToRock <= 15*vPi/8)	;Down Right
		{
			WalkActionHorizontal := d
			WalkActionVertical := s
		}
		if (AngleToRock > 15*vPi/8 && AngleToRock <= 1*vPi/8)	;Right
		{
			WalkActionHorizontal := d
			WalkActionVertical := ""
		}
		send {%WalkActionHorizontal% down}
		send {%WalkActionVertical% down}
		SoundBeep, 800, 60
	}
	MovementFlag := 1
	
}
else
{	
	MovementFlag := 0									;Tell the movement logic to stand still
	click, down right									;Mine the minable
	loop{
		sleep 250
		Selection := OCR([1665, 470, 105, 24])					;Do this until the minable is gone
			if (Selection = "")
			{
				MiningFlag := 0
				breakFlag = 1
			}
	}until breakFlag = 1
	click, up right
}
return
}

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Radial Outward ImageSearch

Post by mikeyww » 23 May 2022, 03:44

In that case, it sounds like you have no strategy to go about debugging your script. One idea is to start with the line that you think is not executing as you expect. You can then add lines before it, to display the values of variables, functions, and conditional statements that lead to that line. You then keep going, moving iteratively in a backwards or retrospective direction. The AHK documentation contains information about additional tools & commands for debugging.

You said that pressing the hotkey does not work. How do you know? Adding a simple MsgBox is one of the best ways to see whether a hotkey is being triggered, because it provides a visual display that is hard to miss. If a hotkey actually does not trigger at all, finding the cause is usually straightforward. There are actually only a few reasons, such as wrong hotkey (or hotkey not defined), wrong context, software conflict, and script not running. In more cases, something else is happening that creates an appearance of not triggering, when the hotkey is actually being triggered.

JaminRoyale
Posts: 6
Joined: 08 May 2020, 12:07

Re: Radial Outward ImageSearch

Post by JaminRoyale » 23 May 2022, 10:21

mikeyww wrote:
23 May 2022, 03:44
In that case, it sounds like you have no strategy to go about debugging your script. One idea is to start with the line that you think is not executing as you expect. You can then add lines before it, to display the values of variables, functions, and conditional statements that lead to that line. You then keep going, moving iteratively in a backwards or retrospective direction. The AHK documentation contains information about additional tools & commands for debugging.

You said that pressing the hotkey does not work. How do you know? Adding a simple MsgBox is one of the best ways to see whether a hotkey is being triggered, because it provides a visual display that is hard to miss. If a hotkey actually does not trigger at all, finding the cause is usually straightforward. There are actually only a few reasons, such as wrong hotkey (or hotkey not defined), wrong context, software conflict, and script not running. In more cases, something else is happening that creates an appearance of not triggering, when the hotkey is actually being triggered.
I spent a very long time debugging yesterday.I use soundbeeps to know when ive hit IF statements and MSG box to check values. All of variables with math are checked and double checked. The imagesearch identifies an image, then calculates the distance to the image and the angle from the center of the screen. This works. The code after uses the angle to pick a a walking direction. This is where things go wrong. As soon as a i try to set my variables for walking (W/S) or (A/D) then my send variable doesnt work.

Which makes no sense to me because i can press ^g and send %WalkActionVertical% and since i defined it in init it works. but as soon as i try to use my control code, that uses literally the exact same define, then the movement doesnt work.

User avatar
Spawnova
Posts: 554
Joined: 08 Jul 2015, 00:12
Contact:

Re: Radial Outward ImageSearch

Post by Spawnova » 23 May 2022, 10:30

I usually will use a msgbox or something to make sure the variable is what I actually expect it to be

However I do see something in your function specifically around here

Code: Select all

if (AngleToRock > 5*vPi/8 && AngleToRock <= 7*vPi/8)	;Up left
		{
			WalkActionHorizontal := a
			WalkActionVertical := s
		}
You are setting your variables to be the value of the variables a and s, did you perhaps mean to set them as actually a and s, perhaps you forgot quotes there, there is a few other cases similar inside that function

JaminRoyale
Posts: 6
Joined: 08 May 2020, 12:07

Re: Radial Outward ImageSearch

Post by JaminRoyale » 23 May 2022, 10:38

Spawnova wrote:
23 May 2022, 10:30
I usually will use a msgbox or something to make sure the variable is what I actually expect it to be

However I do see something in your function specifically around here

Code: Select all

if (AngleToRock > 5*vPi/8 && AngleToRock <= 7*vPi/8)	;Up left
		{
			WalkActionHorizontal := a
			WalkActionVertical := s
		}
You are setting your variables to be the value of the variables a and s, did you perhaps mean to set them as actually a and s, perhaps you forgot quotes there, there is a few other cases similar inside that function
TBH ive tried with quotes, without quote. The top selection W/D the first if statement has quotes and thats the one i was using for testing. with or without doesnt change the output

**oh. i see what your saying. ill change them all.

this forced me to relook at my control logic, the if statements with math. one variable was missing it multiply sign so it was hitting and overriding my walk action variable.

Post Reply

Return to “Gaming Help (v1)”