Jump to content


Photo

Image Search Loop


  • Please log in to reply
12 replies to this topic

#1 mojaverattle

mojaverattle
  • Members
  • 10 posts

Posted 09 December 2011 - 07:59 AM

I'm attempting to search an entire screen for a small section that meets the needed criteria. Something seems to going wrong, any help is appreciated. Here's the code I have:

Loop {
   ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, ..\EasyEnchant\level50.png
   if ErrorLevel = 0
      {
      MsgBox Image found!
	  break
	  }
   else if ErrorLevel = 1
      {   
	  MsgBox Image could not be found on the screen
      break
      }
   else if ErrorLevel = 2
      {   
	  MsgBox, Could not conduct search
      break
      }
}


#2 jpjazzy

jpjazzy
  • Members
  • 800 posts

Posted 09 December 2011 - 08:04 AM

CoordMode, Tooltip ; Use these to convert to screen coord mode rather than relative

	CoordMode, Pixel

	CoordMode, Mouse



Loop {

   ImageSearch, FoundX, FoundY, 0, 0, [color=red]%A_ScreenWidth%[/color], [color=red]%A_ScreenHeight%[/color], ..\EasyEnchant\level50.png ; These parameters must be in %% signs because they are variables passed in a value parameter.

   if ErrorLevel = 0

      {

      MsgBox Image found!

	  break

	  }

   else if ErrorLevel = 1

      {   

	  MsgBox Image could not be found on the screen

      break

      }

   else if ErrorLevel = 2

      {   

	  MsgBox, Could not conduct search

      break

      }

}


#3 mojaverattle

mojaverattle
  • Members
  • 10 posts

Posted 09 December 2011 - 08:12 AM

CoordMode, Tooltip ; Use these to convert to screen coord mode rather than relative
	CoordMode, Pixel
	CoordMode, Mouse

Loop {
   ImageSearch, FoundX, FoundY, 0, 0, [color=red]%A_ScreenWidth%[/color], [color=red]%A_ScreenHeight%[/color], ..\EasyEnchant\level50.png ; These parameters must be in %% signs because they are variables passed in a value parameter.
   if ErrorLevel = 0
      {
      MsgBox Image found!
	  break
	  }
   else if ErrorLevel = 1
      {   
	  MsgBox Image could not be found on the screen
      break
      }
   else if ErrorLevel = 2
      {   
	  MsgBox, Could not conduct search
      break
      }
}


Thanks for the quick response! That seemed to fix the issue, thanks.

#4 Leef_me

Leef_me
  • Moderators
  • 7704 posts

Posted 09 December 2011 - 08:46 AM

Hi mojaverattle, welcome to the forum.

Contrary to jpjazzy's statement you do not need the %% around these variable :arrow: A_ScreenWidth, A_ScreenHeight

The coordmode changes are probably the needed changes.

>> Something seems to going wrong, any help is appreciated.

When asking for help please be more specific, "going wrong" is a catch-all phrase meaning almost nothing.
Since you have tested for all 3 values of errorlevel, you could have said that "Image could not be found on the screen"
"gone wrong" could have been equally valid for errorlevel=0 or 2

#5 jpjazzy

jpjazzy
  • Members
  • 800 posts

Posted 09 December 2011 - 09:24 AM

@Leef_Me

Thanks for the correction... I never knew that image searches accepted expression parameters by default... I don't know how I have missed that for so long. :oops: Sorry.

#6 mojaverattle

mojaverattle
  • Members
  • 10 posts

Posted 09 December 2011 - 10:14 AM

Ok, so now I have attempted to nest another loop inside the original loop to imagesearch for another image. This way both criteria have to be meet. I believe the problem is it's only searching for one of the images, not the first, then the second. :shock:

; Start image search
   CoordMode, Tooltip
   CoordMode, Pixel
   CoordMode, Mouse
Loop {
   ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, ..\EasyEnchant\5.png
   if ErrorLevel = 0
	  {
		Loop {
		ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, ..\EasyEnchant\0.png
		if ErrorLevel = 0
					{
			Msgbox, Enchant found!
			break
			}
		else if ErrorLevel = 1
			{   
			click 615, 388, 2
			Sleep, 500
			continue
			}
		else if ErrorLevel = 2
			{   
			MsgBox, Something went wrong!
			break
			}
		}
	  }
   else if ErrorLevel = 1
      {   
      click 615, 388, 2
	  Sleep, 500
      continue
      }
   else if ErrorLevel = 2
      {   
      MsgBox, Something went wrong!!
      break
      }
}


#7 Leef_me

Leef_me
  • Moderators
  • 7704 posts

Posted 09 December 2011 - 07:28 PM

mojaverattle,
We have no context, no understanding of what you actually want to do.

A quick look over your script seems to indicate that you search for an image and if found, you search for another, and if not found you click.
However you also click in the same location at other times.

I can't give any more advise or test the script because of the lack of information.

Can you provide a screen capture ?
Can you give a non-AHk description of the sequence of steps?

#8 Guests

  • Guests

Posted 09 December 2011 - 08:13 PM

mojaverattle,
We have no context, no understanding of what you actually want to do.

A quick look over your script seems to indicate that you search for an image and if found, you search for another, and if not found you click.
However you also click in the same location at other times.

I can't give any more advise or test the script because of the lack of information.

Can you provide a screen capture ?
Can you give a non-AHk description of the sequence of steps?


Sorry about that. I'll try my best here to explain the order of sequence for the events.

1. Search screen for image "5.png"
2. If image not found, do "click 615, 388, 2" and start over
3. If image "5.png" found, search for image "0.png"
4 If image "0.png" is found send a msgbox
5. If image "0.png" not found, start over

I want to make this expression (I guess that's right), to be able to be called from a hot key, or a button. Which is also another problem, I can't seem to figure out. I'll dump the whole code so it can be reviewed. Thanks again.



; Easy Enchanting for Minecraft 1.0
; Created By MojaveRattle
; Date: 12/9/2011

; Check OS Version
	if A_OSVersion in WIN_NT4,WIN_95,WIN_98,WIN_ME  ; Check is OS is one of these versions
		{
		MsgBox This script requires Windows 2000/XP or later. ;Message
		ExitApp ;exit
		}

; Check for MC or MC launcher
	DetectHiddenWindows, On ; Enable hidden windows
	IfWinExist, Minecraft Launcher ; Check to see if the window Minecraft Launcher exists
		{
		WinActivate  ; Automatically uses the window found above.
		WinMaximize  ; same
		Winset, bottom ; Move it to the bottom
		MsgBox, We have detected you're still at the launcher! You should join your game, then try again.
		return
		exitapp ;exit
		}		
	IfWinExist, Minecraft ; Check to see if the window Minecraft exists
		{
		WinActivate  ; Automatically uses the window found above.
		WinMaximize  ; same
		Winset, bottom ; Move it to the bottom
		MsgBox, Before hitting start, be sure you're at the enchanting table with the item you want enchanted already in the enchantment slot. Default texture pack is also required.
		}		
	IfWinNotExist, Minecraft
		{
		MsgBox, Minecraft not found! You should start that first.
		ExitApp ; exit
		}
	
; Create GUI
	Gui +AlwaysOnTop ;force on top of all other windows
	Gui, Add, Picture, x-8 y0 w420 h180 , ..\Easy Enchant\images\background.png ;background image
	Gui, Add, Button, x12 y70 w180 h30 vstart gstart, Click To Start (or SHIFT+S) ;start button
	Gui, Add, Button, x212 y70 w180 h30 vexit gexit, Click To Exit (or SHIFT+E) ;exit button
	Gui, Show, x127 y87 h174 w414, Easy Enchanting for Minecraft 1.0! ;name 
	Winset, top ; move window to the top, not sure if required
	return
	
; Create image search
	imagesearch := 
	CoordMode, Tooltip ;required for imagesearch reading correctly
	CoordMode, Pixel ;required for imagesearch reading correctly
	CoordMode, Mouse ;required for imagesearch reading correctly
	Loop {
	ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, ..\Easy Enchant\images\5.png
	if ErrorLevel = 0
	{
		Loop {
		ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, ..\Easy Enchant\images\0.png
		if ErrorLevel = 0
			{
			Msgbox, enchant found!
			break
			}
		else if ErrorLevel = 1
			{   
			click 615, 388, 2
			Sleep, 500
			continue
			}
		else if ErrorLevel = 2
			{   
			MsgBox, Holy error, Batman!
			break
			}
			}
	}
	else if ErrorLevel = 1
		{   
		click 615, 388, 2
		Sleep, 500
		continue
		}
	else if ErrorLevel = 2
		{   
		MsgBox, Holy error, Batman!
		break
		}
	}

; Set Hotkeys for use		
	Shift & S:: ; create hotkey for shift and s
		imagesearch:
		return
	Shift & E:: ; create hotkey for shift and e
		ExitApp ; exit program
		return 

; Set buttons for use
	start:
		imagesearch:
		return
	exit:
		ExitApp
		return


#9 mojaverattle

mojaverattle
  • Members
  • 10 posts

Posted 09 December 2011 - 10:26 PM

Oops, I posted as guest :oops:. Ok, I updated a few things. The start button just calls the hotkey. That was the simple way to do it, from what I could tell. Although, I'm still not sure that I created this function right, or is being called correctly.

Here's the updated code:
; Easy Enchanting for Minecraft 1.0
; Created By MojaveRattle
; Date: 12/9/2011

; Check OS Version
	if A_OSVersion in WIN_NT4,WIN_95,WIN_98,WIN_ME  ; Check is OS is one of these versions
		{
		MsgBox This script requires Windows 2000/XP or later. Sorry! ;Message
		ExitApp ;exit
		}

; Check for MC or MC launcher
	DetectHiddenWindows, On ; Enable hidden windows
	IfWinExist, Minecraft Launcher ; Check to see if the window Minecraft Launcher exists
		{
		WinActivate  ; Automatically uses the window found above.
		WinMaximize  ; same
		Winset, bottom ; Move it to the bottom
		MsgBox, We have detected you're still at the launcher! You should join your game, get your enchantment read, then hit start.
		}		
	IfWinExist, Minecraft ; Check to see if the window Minecraft exists
		{
		WinActivate  ; Automatically uses the window found above.
		WinMaximize  ; same
		Winset, bottom ; Move it to the bottom
		MsgBox, Before hitting start, be sure you're at the enchanting table with the item you want enchanted already in the enchantment slot. Default texture pack is also required.
		}		
	IfWinNotExist, Minecraft
		{
		MsgBox, Minecraft not found! You should start that first.
		ExitApp ; exit
		}
	
; Create GUI
	Gui +AlwaysOnTop ;force on top of all other windows
	Gui, Add, Picture, x-8 y0 w420 h180 , ..\Easy Enchant\images\background.png ;background image
	Gui, Add, Button, x12 y70 w180 h30 vstartbutton gstartbutton, Click To Start (or SHIFT+S) ;start button
	Gui, Add, Button, x212 y70 w180 h30 vexitbutton gexitbutton, Click To Exit (or SHIFT+E) ;exit button
	Gui, Show, x127 y87 h174 w414, Easy Enchanting for Minecraft 1.0! ;name
	Winset, top ; move window to the top, not sure if required
	return
	
; Create image search
	imagesearch :=
	CoordMode, Tooltip ;required for imagesearch reading correctly
	CoordMode, Pixel ;required for imagesearch reading correctly
	CoordMode, Mouse ;required for imagesearch reading correctly
	loop
		{
		loop
			{
			ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, ..\Easy Enchant\images\5.png
			if errorlevel = 0
			{
		loop
			{
			ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, ..\Easy Enchant\images\0.png
			If errorlevel = 0
			{
			Msgbox, "Enchantment found!"
			}
			}
		}
     
		If errorlevel = 1				
			{
			click 615, 388, 2
			Sleep, 500
			gosub, imagesearch
			}
			}
		}
		return
		
; Set buttons for use
	startbutton: ; call start button
		send, shift+s ;call hotkey to start script
		return
	exitbutton: ; call exit button
		ExitApp ; exit program
		return
		
; Set Hotkeys for use      
   Shift & S:: ; create hotkey for shift and s
      imagesearch:
      return
   Shift & E:: ; create hotkey for shift and e
      ExitApp ; exit program
      return


#10 Leef_me

Leef_me
  • Moderators
  • 7704 posts

Posted 10 December 2011 - 04:55 AM

>>Although, I'm still not sure that I created this function right, or is being called correctly.

The lines that start with "CoordMode, Tooltip" will NEVER be executed.
There is no path of program execution that leads to them.

This :arrow: "imagesearch :=" is the form of a variable assignment.
It is a VERY poor idea to name variables after existing commands or functions.

Using this type of naming makes the script harder to understand,

I now see you use are meaning to use "imagesearch :" as a subroutine label. This is also a bad idea, for the same reasons as above.

#11 mojaverattle

mojaverattle
  • Members
  • 10 posts

Posted 10 December 2011 - 10:04 PM

>>Although, I'm still not sure that I created this function right, or is being called correctly.

The lines that start with "CoordMode, Tooltip" will NEVER be executed.
There is no path of program execution that leads to them.

This :arrow: "imagesearch :=" is the form of a variable assignment.
It is a VERY poor idea to name variables after existing commands or functions.

Using this type of naming makes the script harder to understand,

I now see you use are meaning to use "imagesearch :" as a subroutine label. This is also a bad idea, for the same reasons as above.


Thanks for the advice, I think it help made some advancements this time around. I made the image search a sub, and now have the buttons and hot key calling it. Which is more along the lines of what I wanted. But now it comes back down to the image search. It doesn't seem to activate until after I press the hot key or button, then click the item in-game. I'm not 100% sure what's causing this. Code provided below. Oh yeah, is there some way to keep the second image search within a range of finding the first results?



; Create image search sub
	imagesearch: ; Name the sub imagesearch
	Loop {
		ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *5 %A_ScriptDir%\images\5.png
			if ErrorLevel = 0
				Loop {
					ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *5 %A_ScriptDir%\images\0.png
					if ErrorLevel = 0
						{
						Msgbox, Found it!
						return
						}
					else if Errorlevel = 1
						{
						break
						return
						}
				}
			else if ErrorLevel = 1
				{   
				click 615, 388, 1
				sleep, 10
				click 615, 388, 1
				continue
				}
			else if ErrorLevel = 2
				{   
				MsgBox, Could not conduct search
				break
				}	
			}


#12 Leef_me

Leef_me
  • Moderators
  • 7704 posts

Posted 11 December 2011 - 12:27 AM

The ImageSearch command will match an image only if it sees the whole image.
Does your gui interfere with seeing the whle image?

? Why don't you try this:
run the script, make the the window you want to search visible, and then hit the hotkey to start the search.

If you find an image, the location if the top-left corner is returned in FoundX & FoundY.
If you change the 2nd search to use these values instead of 0,0,
then the search will start there instead of at 0,0

If the 2nd image is "near" to the 1st FoundX & FoundY you could also use
for example:
ImageSearch, FoundX2, FoundY2, FoundX-50, FoundY-50, FoundX+250, FoundY+250, *5 %A_ScriptDir%\images\0.png

The -50 and +250 values are offsets from the original "found" location
I chose them at random just to explain.

I again suggest NOT using "imagesearch" as the name of a subroutine, you can use "aimagesearch" or "bimagesearch" or any other combination, just not the exact text "imagesearch"
btw, you forgot the "return" on the end of the subroutine.

#13 punchin

punchin
  • Members
  • 62 posts

Posted 22 April 2012 - 04:26 AM

I play Minecraft also, and am working on my own script for enchanting. Just to clarify some steps.

In order to enchant, you open the enchanting menu and you have a slot to drop armor/weapons in. When you drop a valid item in the slot, 3 random level enchantments show up on the right.

If you don't get the level you're wanting, you pick the item up and drop it back in to get a new selection. That's where the "click 615, 388, 2" comes in.


If you're going for the max level (50), you can search for that. I recommend using png format and making all but the letters transparent. That way if the galactic alphabet runs a little long, it won't interfere with the image search.

Hope this helps solve some imagesearch issues for you.