Jump to content

PixelSearch is giving me trouble!


Recommended Posts

Ok, so from my understanding, PixelSearch searches a specified area for a pixel fo a certain color. If it finds the pixel it returns coordinates, otherwise it returns @error 1.

What I'm trying to do is run a certain app and do a sequence of commands in it. The first thing I need to do is to click a name, however it is in a list of other names (graphical not text :idiot: ) and it can shift positions. So I run a series of pixel searches to determine where the name is and if it's on the page. If the name is not found, it turns the page.

However, every time I run a test, it just turns to the next page, even though the name is there, which makes me think it's not running the search or not using the results. Here is the pertinent part, followed by the whole messy thing (in case you want to look at everything and tell me if anything else is screwed up):

$A = PixelSearch( 248, 219, 255, 608, 14602076, 2 )
        If Not @error Then
            $R = PixelSearch( 284, $A[1], 295, 632, 16777215, 2 )
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf

The whole schebang:

HotKeySet("{NUMPADSUB}", "Sing")
Global $Singing

While 1
    If $Singing then
        Sleep(2000)
        MouseClick( "left", 955, 657, 2, 1 )
        $A = PixelSearch( 248, 219, 255, 608, 14602076, 2 )
        If Not @error Then
            $R = PixelSearch( 284, $A[1], 295, 632, 16777215, 2 )
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
        If Not @error Then
            $C = PixelSearch( 359, 194, 362, 632, 16777215, 1 )
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
        If Not @error Then
            $O = PixelSearch( 376, $C[1], 380, 632, 16777215, 1 )
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
        If Not @error Then
            $R = PixelSearch( 394, $O[1], 396, 632, 16777215, 1 )
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
        If Not @error Then
            $V = PixelSearch( 406, $R[1], 410, 632, 16777215, 1 )
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
        If Not @error Then
            $I = PixelSearch( 426, $V[1], 430, 632, 16777215, 1 )
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
        If Not @error Then
            $N = PixelSearch( 439, $I[1], 443, 632, 16777215, 1 )
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
        If Not @error Then
            $1 = PixelSearch( 455, 200, 500, 206, 16777215, 1 )
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
        If Not @error Then
            $2 = PixelSearch( 455, 274, 500, 280, 16777215, 1 )
        Else
            MouseMove( 300, 230 )
            PixelSearch( 358, 235, 383, 230, 16777215, 1 )
            If @error Then
                Call("Play")
                ContinueLoop
            EndIf
        EndIf
        If Not @error Then
            $3 = PixelSearch( 455, 351, 500, 357, 16777215, 1 )
        Else
            MouseMove( 300, 300 )
            PixelSearch( 358, 308, 383, 313, 16777215, 1 )
            If @error Then
                Call("Play")
                ContinueLoop
            EndIf   
        EndIf
        If Not @error Then
            $4 = PixelSearch( 455, 428, 500, 433, 16777215, 1 )
        Else
            MouseMove( 300, 380 )
            PixelSearch( 358, 384, 383, 391, 16777215, 1 )
            If @error Then
                Call("Play")
                ContinueLoop
            EndIf   
        EndIf
        If Not @error Then
            $5 = PixelSearch( 455, 502, 500, 507, 16777215, 1 )
        Else
            MouseMove( 300, 450 )
            PixelSearch( 358, 459, 383, 464, 16777215, 1 )
            If @error Then
                Call("Play")
                ContinueLoop
            EndIf
        EndIf
        If Not @error Then
            $6 = PixelSearch( 455, 580, 500, 586, 16777215, 1 )
        Else
            MouseMove( 300, 530 )
            PixelSearch( 358, 535, 383, 540, 16777215, 1 )
            If @error Then
                Call("Play")
                ContinueLoop
            EndIf   
        EndIf
        If Not @error Then
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        Else
            MouseMove( 300, 605 )
            PixelSearch( 358, 610, 383, 615, 16777215, 1 )
            If @error Then
                Call("Play")
                ContinueLoop
            EndIf   
        EndIf
        If Not @error Then
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
    EndIf

WEnd

Func Sing()
    $Singing = NOT $Singing
EndFunc

Func Play()
    Sleep(1000)
    MouseClick( "left" )
    Sleep(3000)
    Send("ggggg")
    Sleep(500)
    MouseClick( "left" )
    Sleep(5000)
    MouseClick( "left", 206, 446, 2, 1 )
    Sleep(1000)
    MouseClick( "left", 295, 546, 1, 1 )
    Sleep(300000)
    MouseClick( "left", 708, 630, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 383, 275, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 508, 649, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 931, 635, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 586, 330, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 730, 651, 2, 1 )
    Sleep(13000)
EndFunc

Thanks!

Link to comment
Share on other sites

Alright so I managed to trim my code quite a bit, however I get the following error upon activation:

If $_APIXRECT[$_NY-1][$_NX-1]<>PixelGetColor($_ACOORD[0]+$_NX-1,$_ACOORD[1]+$_NY-1) Then
If ^ ERROR

Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

My new code with your ingenius func is:

HotKeySet("{NUMPADSUB}", "Sing")
Global $Singing

While 1
    If $Singing then
        Sleep(2000)
        MouseClick( "left", 955, 657, 2, 1 )
        $a = _LoadPixelRectFromFile( "corvin.txt" )
        $b = _FindPixelRect(354,188,460,629,$a)
        If Not @error Then
            Call("Play")
            ContinueLoop
        Else
            Sleep(1000)
            MouseMove( 615, 687, 1 )
            Sleep(1000)
            MouseClick( "left", 615, 687, 1, 1 )
            ContinueLoop
        EndIf
    EndIf

WEnd

Func Sing()
    $Singing = NOT $Singing
EndFunc

Func Play()
    MouseMove( $b[0], $b[1])
    Sleep(1000)
    MouseClick( "left" )
    Sleep(3000)
    Send("ggggg")
    Sleep(500)
    MouseClick( "left" )
    Sleep(5000)
    MouseClick( "left", 206, 446, 2, 1 )
    Sleep(1000)
    MouseClick( "left", 295, 546, 1, 1 )
    Sleep(300000)
    MouseClick( "left", 708, 630, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 383, 275, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 508, 649, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 931, 635, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 586, 330, 2, 1 )
    Sleep(2000)
    MouseClick( "left", 730, 651, 2, 1 )
    Sleep(13000)
EndFunc

Func _FindPixelRect($_NL, $_NT, $_NR, $_NB, ByRef $_APIXRECT)
  If Not IsArray($_APIXRECT) Or Not UBound($_APIXRECT, 2) Then
     SetError(1)
     Return 0
  EndIf
 
  If ($_NL + UBound($_APIXRECT, 2) > $_NR) Or($_NT + UBound($_APIXRECT) > $_NB) Then
     SetError(2)
     Return 0
  EndIf
 
  $_NR = $_NR - UBound($_APIXRECT, 2)+1
  $_NB = $_NB - UBound($_APIXRECT)+1
  $_NLIMITX = UBound($_APIXRECT)
  $_NLIMITY = UBound($_APIXRECT, 2)
 
  $_ACOORD = PixelSearch($_NL, $_NT, $_NR, $_NB, $_APIXRECT[0][0])
  While Not @error
     For $_NY = 1 To $_NLIMITY
        For $_NX = 1 To $_NLIMITX
           If $_APIXRECT[$_NY-1][$_NX-1]<>PixelGetColor($_ACOORD[0]+$_NX-1,$_ACOORD[1]+$_NY-1) Then
              SetError(1)
              ExitLoop 2
           EndIf
        Next
     Next
     If Not @error Then
        Return $_ACOORD
     Else
        $_ATEMP=PixelSearch($_ACOORD[0]+1, $_ACOORD[1], $_NR, $_ACOORD[1], $_APIXRECT[0][0])
        If @error Then
           $_NT = $_ACOORD[1] + 1
           If $_NT > $_NB Then
              SetError(1)
              Return 0
           EndIf
           $_ACOORD = PixelSearch($_NL, $_NT, $_NR, $_NB, $_APIXRECT[0][0])
        Else
           $_ACOORD = $_ATEMP
        EndIf
     EndIf
  Wend
  SetError(1)
  Return 0
EndFunc


Func _LoadPixelRect($_NL, $_NT, $_NR, $_NB)
  If ($_NL > $_NR Or $_NT > $_NB) Then
     SetError(1)
     Return 0
  EndIf
 
  Dim $_APIXRECT[$_NB-$_NT+1][$_NR-$_NL+1]
 
  For $_NY = $_NT To $_NB
     For $_NX = $_NL To $_NR
        $_APIXRECT[$_NY-$_NT][$_NX-$_NL] = PixelGetColor($_NX,$_NY)
     Next
  Next
 
  Return $_APIXRECT
EndFunc


Func _LoadPixelRectFromFile($_SZBUFFER)
  If Not FileExists($_SZBUFFER) Then
     SetError(1)
     Return 0
  EndIf
 
  $_SZBUFFER = StringStripCR(FileRead($_SZBUFFER,FileGetSize($_SZBUFFER)))
  If StringRight($_SZBUFFER,1)== @LF Then $_SZBUFFER = StringTrimRight($_SZBUFFER,1)
  $_SZBUFFER = StringSplit($_SZBUFFER,@LF)
  $_SZLINE = StringSplit($_SZBUFFER[1],",")
 
  $_NLIMITX = $_SZLINE[0]
  $_NLIMITY = $_SZBUFFER[0]
 
  Dim $_APIXRECT[$_NLIMITY][$_NLIMITX]
 
  For $_NY = 1 To $_NLIMITY
     $_SZLINE = StringSplit($_SZBUFFER[$_NY],",")
     If $_SZLINE[0] <> $_NLIMITX Then
        SetError(1)
        Return 0
     EndIf
     For $_NX = 1 To $_NLIMITX
        $_APIXRECT[$_NY-1][$_NX-1] = Number($_SZLINE[$_NX])
     Next
  Next
 
  Return $_APIXRECT
EndFunc


Func _SavePixelRect(ByRef $_ARECT, $_SZFILE)
  Dim $_SZBUFFER=""
 
  If Not IsArray($_ARECT) Or Not UBound($_ARECT, 2) Then
     SetError(1)
     Return 0
  EndIf
 
  $_LIMITY = UBound($_ARECT)
  $_LIMITX = UBound($_ARECT,2)
 
  For $_NY = 0 To $_LIMITY-1
     For $_NX = 0 To $_LIMITX-1
        $_SZBUFFER = $_SZBUFFER & $_ARECT[$_NY][$_NX] & ","
     Next
     $_SZBUFFER = StringTrimRight($_SZBUFFER,1) & @CRLF
  Next
 
  $_SZBUFFER = StringTrimRight($_SZBUFFER,2)
 
  FileDelete($_SZFILE)
  FileWrite($_SZFILE,$_SZBUFFER)
  Return 1
EndFunc

Edit: when I make the search width match the array width the error goes away, but it doesnt find the image. Im going to try doubling the search area.

Edit2: double the search area no error but it goes on as if the thing wasnt there, even though it is.

Edited by Othello
Link to comment
Share on other sites

Guest spacebong

First time poster, first time scripting, first time using autoit. I'm pretty much the biggest newb here. I tried your code without error too but there seems to be something wrong with the _LoadPixelRecFromFile, well it might just be me. When I added the following code to test to see what is actually loading, I get a msgbox with nothing in it:

$a = _LoadPixelRectFromFile("pix.txt")

MsgBox (4096, " ", $a)

Is it becuase its an array?

Link to comment
Share on other sites

First time poster, first time scripting, first time using autoit.  I'm pretty much the biggest newb here.  I tried your code without error too but there seems to be something wrong with the _LoadPixelRecFromFile, well it might just be me.  When I added the following code to test to see what is actually loading, I get a msgbox with nothing in it:

$a = _LoadPixelRectFromFile("pix.txt")

MsgBox (4096, " ", $a)

Is it becuase its an array?

<{POST_SNAPBACK}>

Did you save an array to pix.txt first? If you dont have an array in pix.txt it wont load anything.
Link to comment
Share on other sites

Guest spacebong

Yeah I did the _LoadPixelRect and _SavePixelRect. The text file is there and when I open the file with notepad I see the array. Maybe when I try to output the array I need to indicate what subscript to pull.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...