Jump to content

pixelsearch between two area's


smcombs
 Share

Recommended Posts

Here is the new code it seems to work pretty well. Again, this script is to search a box from the middle to the object. Not sure what all purposes this can be used for but its pretty cool. I am taking a screenshot and then placing the screenshot into a gui that I have created. I then search the picture for objects, once it finds one it starts from the middle of my square and searches to the object itself to see if their are any walls blocking me from getting the object. I still have to write code to auto-calc the center of the square but would like any revisions or ideas people have.

Func searchforObject()
    $coords = PixelSearch($x1,$y1,$x2,$y2,$object,0,0,$handle2) ;This performs a search on the new gui and map that I create in my script.
    If Not @error Then
        $foundObject = True
        If $coords[0] >= 72 And $coords[1] >= 64 Then
            $pathClear = checkforWall($coords,1,0,0,0)
            If $pathClear = True Then
                $found = True
                Return $coords[0] & $coords[1]
            EndIf
        EndIf
        If $coords[0] <= 72 And $coords[1] <= 64 Then
            $pathClear = checkforWall($coords,0,1,0,0)
            If $pathClear = True Then
                $found = True
                Return $coords[0] & $coords[1]
            EndIf
        EndIf
        If $coords[0] >= 72 And $coords[1] <= 64 Then
            $pathClear = checkforWall($coords,0,0,1,0)
            If $pathClear = True Then
                $found = True
                Return $coords[0] & $coords[1]
            EndIf
        EndIf
        If $coords[0] <= 72 And $coords[1] >= 64 Then
            $pathClear = checkforWall($coords,0,0,0,1)
            If $pathClear = True Then
                $found = True
                Return $coords[0] & $coords[1]
            EndIf
        EndIf
    Else
        $foundObject = False
        $found = False
    EndIf
EndFunc

Func checkforWall($coords,$lowerRight,$upperLeft,$upperRight,$lowerLeft)
    Local $wall[5]
    $wall[0] = 4
    $wall[1] = 0x435A2A
    $wall[2] = 0x495728
    $wall[3] = 0x313B10
    $wall[4] = 0x293018
    $shade = 10
    $centerx = 72
    $centery = 64
    $oncex = 72
    $oncey = 64
    $once = True
    $coords2 = 0
    $coords3 = 0
    Do
        Sleep(10)
        If $once = True Then
            If $lowerRight = 1 Then
                $centerx = $centerx + 1
                $centery = $centery + 1
                $coords2 = coords2($oncex,$oncey,$centerx,$centery,$wall,$shade,$handle)
            ElseIf $upperLeft = 1 Then
                $oncex = $oncex - 1
                $oncey = $oncey - 1
                $coords2 = coords2($oncex,$oncey,$centerx,$centery,$wall,$shade,$handle)
            ElseIf $upperRight = 1 Then
                $centerx = $centerx + 1
                $oncey = $oncey - 1
                $coords2 = coords2($oncex,$oncey,$centerx,$centery,$wall,$shade,$handle)
            ElseIf $lowerLeft = 1 Then
                $oncex = $oncex - 1
                $centery = $centery + 1
                $coords2 = coords2($oncex,$oncey,$centerx,$centery,$wall,$shade,$handle)
            EndIf
            $once = False
        Else
            If $lowerRight = 1 Then
                $oncex = $oncex + 1
                $oncey = $oncey + 1
                $centerx = $centerx + 1
                $centery = $centery + 1
                $coords2 = coords2($oncex,$oncey,$centerx,$centery,$wall,$shade,$handle)
                _WinAPI_DrawLine($handle,$oncex,$oncey,$centerx,$centery) ; horizontal left
                If $centery = $coords[1] Then
                    Do
                        $centerx = $centerx + 1
                        $coords3 = coords3($centerx,$centery,$wall,$shade,$handle)
                        _WinAPI_DrawLine($handle,$centerx,$centery,$centerx,$centery) ; horizontal left
                    Until IsArray($coords3) Or $centerx = $coords[0]
                    ExitLoop
                EndIf
                If $centerx = $coords[0] Then
                    Do
                        $centery = $centery + 1
                        $coords3 = coords3($centerx,$centery,$wall,$shade,$handle)
                        _WinAPI_DrawLine($handle,$centerx,$centery,$centerx,$centery) ; horizontal left
                    Until IsArray($coords3) Or $centery = $coords[1]
                    ExitLoop
                EndIf
            ElseIf $upperLeft = 1 Then
                $oncex = $oncex - 1
                $oncey = $oncey - 1
                $centerx = $centerx - 1
                $centery = $centery - 1
                $coords2 = coords2($oncex,$oncey,$centerx,$centery,$wall,$shade,$handle)
                _WinAPI_DrawLine($handle,$oncex,$oncey,$centerx,$centery) ; horizontal left
                If $oncey = $coords[1] Then
                    Do
                        $centerx = $centerx - 1
                        $coords3 = coords3($centerx,$centery,$wall,$shade,$handle)
                        _WinAPI_DrawLine($handle,$centerx,$centery,$centerx,$centery) ; horizontal left
                    Until IsArray($coords3) Or $centerx = $coords[0]
                    ExitLoop
                EndIf
                If $oncex = $coords[0] Then
                    Do
                        $centery = $centery - 1
                        $coords3 = coords3($centerx,$centery,$wall,$shade,$handle)
                        _WinAPI_DrawLine($handle,$centerx,$centery,$centerx,$centery) ; horizontal left
                    Until IsArray($coords3) Or $centery = $coords[1]
                    ExitLoop
                EndIf
            ElseIf $upperRight = 1 Then
                $oncex = $oncex + 1
                $oncey = $oncey - 1
                $centerx = $centerx + 1
                $centery = $centery - 1
                $coords2 = coords2($oncex,$oncey,$centerx,$centery,$wall,$shade,$handle)
                _WinAPI_DrawLine($handle,$oncex,$oncey,$centerx,$centery) ; horizontal left
                If $centery - 1 = $coords[1] Then
                    Do
                        $centerx = $centerx + 1
                        $coords3 = coords3($centerx,$centery,$wall,$shade,$handle)
                        _WinAPI_DrawLine($handle,$centerx,$centery,$centerx,$centery) ; horizontal left
                    Until IsArray($coords3) Or $centerx = $coords[0]
                    ExitLoop
                EndIf
                If $oncex + 1 = $coords[0] Then
                    Do
                        $centery = $centery - 1
                        $coords3 = coords3($centerx,$centery,$wall,$shade,$handle)
                        _WinAPI_DrawLine($handle,$centerx,$centery,$centerx,$centery) ; horizontal left
                    Until IsArray($coords3) Or $centery = $coords[1]
                    ExitLoop
                EndIf
            ElseIf $lowerLeft = 1 Then
                $oncex = $oncex - 1
                $oncey = $oncey + 1
                $centerx = $centerx - 1
                $centery = $centery + 1
                $coords2 = coords2($oncex,$oncey,$centerx,$centery,$wall,$shade,$handle)
                _WinAPI_DrawLine($handle,$oncex,$oncey,$centerx,$centery) ; horizontal left
                If $oncey + 1 = $coords[1] Then
                    Do
                        $centerx = $centerx - 1
                        $coords3 = coords3($centerx,$centery,$wall,$shade,$handle)
                        _WinAPI_DrawLine($handle,$centerx,$centery,$centerx,$centery) ; horizontal left
                    Until IsArray($coords3) Or $centerx = $coords[0]
                    ExitLoop
                EndIf
                If $centerx - 1 = $coords[0] Then
                    Do
                        $centery = $centery + 1
                        $coords3 = coords3($centerx,$centery,$wall,$shade,$handle)
                        _WinAPI_DrawLine($handle,$centerx,$centery,$centerx,$centery) ; horizontal left
                    Until IsArray($coords3) Or $centery = $coords[1]
                    ExitLoop
                EndIf
            EndIf
        EndIf
    Until IsArray($coords2)
    If IsArray($coords2) Then
        _WinAPI_DrawLine($handle,$coords[0],$coords[1],$coords[0],$coords[1]) ; horizontal left
    EndIf
    If IsArray($coords3) Then
        _WinAPI_DrawLine($handle,$coords[0],$coords[1],$coords[0],$coords[1]) ; horizontal left
    Else
        Return True
    EndIf
EndFunc

Func coords2($oncex,$oncey,$centerx,$centery,$wall,$shade,$handle)
    For $i = 0 To $wall[0] Step 1
        $coords2 = PixelSearch($oncex,$oncey,$centerx,$centery,$wall[$i],$shade,0,$handle)
        If Not @error Then
            ExitLoop
        EndIf
    Next
    Return $coords2
EndFunc

Func coords3($centerx,$centery,$wall,$shade,$handle)
    For $i = 0 To $wall[0] Step 1
        $coords3 = PixelSearch($centerx,$centery,$centerx,$centery,$wall[$i],$shade,0,$handle)
        If Not @error Then
            ExitLoop
        EndIf
    Next
    Return $coords3
EndFunc
Edited by smcombs
Link to comment
Share on other sites

one way to do it would be to grab a screen shot of that area then run through it backwards; another option is to use nested for loops that step backwards (y on the outside, x on the inside for a row by row check, reverse for a column by column check) and compare to the value returned by PixelGetColor

Link to comment
Share on other sites

Hope it's correct:

$coords = PixelSearch(0,0,300,300,0xFFFFFF,0,0,$handle)

If Not @error Then
    Local $iLeft = 150, $iTop = 150, $iRight = 150, $iBottom = 150
    If $coords[0] < 150 Then
        $iLeft = $coords[0]
    Else
        $iRight = $coords[0]
    EndIf
    
    If $coords[1] < 150 Then 
        $iTop =  $coords[1]
    Else
        $iBottom = $coords[1]
    EndIf
    
    $somethingGreen = Pixelsearch($iLeft,$iTop,$iRight,$iBottom,$green,0,0,$handle)
    If Not @error Then
        MsgBox(0,"","Something is in the way between the middle and the object")
    Endif
EndIf
Link to comment
Share on other sites

Ok, I really appreciate the guidance it definitely pointed me in the right direction.

And here is what I came up with:

$x1 = 877
$y1 = 23
$x2 = 1021
$y2 = 151
$coords = PixelSearch($x1,$y1,$x2,$y2,$object,0,0,$handle)
If Not @error Then
    If $coords[0] >= 949 And $coords[1] >= 87 Then
        $coords2 = PixelSearch(949,87,$coords[0],$coords[1],$objectInTheWay,15,0,$handle)
        If Not @error Then
            $x2 = $coords2[0]
            $y2 = $coords2[1]
            searchforObject()
        Else
            $x1 = 877
            $y1 = 23
            $x2 = 1021
            $y2 = 151
            $found = True
            Return $coords[0] & $coords[1]
        EndIf
    ElseIf $coords[0] <= 949 And $coords[1] <= 87 Then
        $coords2 = PixelSearch($coords[0],$coords[1],949,87,$objectInTheWay,15,0,$handle)
        If Not @error Then
            $x1 = $coords2[0]
            $y1 = $coords2[1]
            searchforObject()
        Else
            $x1 = 877
            $y1 = 23
            $x2 = 1021
            $y2 = 151
            $found = True
            Return $coords[0] & $coords[1]
        EndIf
    ElseIf $coords[0] <= 949 And $coords[1] >= 87 Then
        $coords2 = PixelSearch($coords[0],87,949,$coords[1],$objectInTheWay,15,0,$handle)
        If Not @error Then
            $x1 = $coords2[0]
            $y2 = $coords2[1]
            searchforObject()
        Else
            $x1 = 877
            $y1 = 23
            $x2 = 1021
            $y2 = 151
            $found = True
            Return $coords[0] & $coords[1]
        EndIf
    ElseIf $coords[0] >= 949 And $coords[1] <= 87 Then
        $coords2 = PixelSearch(949,$coords[1],$coords[0],87,$objectInTheWay,15,0,$handle)
        If Not @error Then
            $x2 = $coords2[0]
            $y1 = $coords2[1]
            searchforObject()
        Else
            $x1 = 877
            $y1 = 23
            $x2 = 1021
            $y2 = 151
            $found = True
            Return $coords[0] & $coords[1]
        EndIf
    Else 
        $found = True
        Return $coords[0] & $coords[1]
    EndIf
Else
    Return False
EndIf

Although its not what I want completely but its close. Problem with this is that it is still pixel searching a square. I need to come up with a pixel to pixel almost like a line type of search. I thought about doing something like a pixelsearch from the center of the square then + 1 pixel on the $x or $y depending on which 1/4 of the square, the object is in. Then just keep going until it reaches the destination or until it finds $objectInTheWay. I'll keep playing with it.

Edited by smcombs
Link to comment
Share on other sites

I have revised the code for the function. It looks alot better this time. I would love others opinions and testing to see if it works for them. Should be able to just plugin a few variables and be set. Here it is:

Func searchforObject()
    $coords = PixelSearch($x1,$y1,$x2,$y2,$collectItem,0,0,$handle2) ;This performs a search on the new gui and map.
    If Not @error Then
        If $coords[0] >= 72 And $coords[1] >= 64 Then
            $pathClear = checkforWall($coords,1,0,0,0)
            If $pathClear = True Then
                $found = True
                Return $coords[0] & $coords[1]
            EndIf
        EndIf
        If $coords[0] <= 72 And $coords[1] <= 64 Then
            $pathClear = checkforWall($coords,0,1,0,0)
            If $pathClear = True Then
                $found = True
                Return $coords[0] & $coords[1]
            EndIf
        EndIf
        If $coords[0] >= 72 And $coords[1] <= 64 Then
            $pathClear = checkforWall($coords,0,0,1,0)
            If $pathClear = True Then
                $found = True
                Return $coords[0] & $coords[1]
            EndIf
        EndIf
        If $coords[0] <= 72 And $coords[1] >= 64 Then
            $pathClear = checkforWall($coords,0,0,0,1)
            If $pathClear = True Then
                $found = True
                Return $coords[0] & $coords[1]
            EndIf
        EndIf
    Else
        $found = False
    EndIf
EndFunc

Func checkforWall($coords,$lowerRight,$upperLeft,$upperRight,$lowerLeft)
    $wall = 0x314118
    $shade = 15
    $centerx = 72
    $centery = 64
    $oncex = 72
    $oncey = 64
    $once = True
    Do
        Sleep(10)
        If $once = True Then
            If $lowerRight = 1 Then
                $centerx = $centerx + 1
                $centery = $centery + 1
                $coords2 = PixelSearch($oncex,$oncey,$centerx,$centery,$wall,$shade,0,$handle)
            ElseIf $upperLeft = 1 Then
                $oncex = $oncex - 1
                $oncey = $oncey - 1
                $coords2 = PixelSearch($oncex,$oncey,$centerx,$centery,$wall,$shade,0,$handle)
            ElseIf $upperRight = 1 Then
                $centerx = $centerx + 1
                $centery = $centery - 1     
                $coords2 = PixelSearch($oncex,$oncey,$centerx,$centery,$wall,$shade,0,$handle)
            ElseIf $lowerLeft = 1 Then
                $oncex = $oncex - 1
                $oncey = $oncey + 1
                $coords2 = PixelSearch($oncex,$oncey,$centerx,$centery,$wall,$shade,0,$handle)
            EndIf
            $once = False
        Else
            If $lowerRight = 1 Then
                $oncex = $oncex + 1
                $oncey = $oncey + 1
                $centerx = $centerx + 1
                $centery = $centery + 1
                $coords2 = PixelSearch($oncex,$oncey,$centerx,$centery,$wall,$shade,0,$handle)
;~              _WinAPI_DrawLine($handle,$oncex,$oncey,$centerx,$centery) ; horizontal left
            ElseIf $upperLeft = 1 Then
                $oncex = $oncex - 1
                $oncey = $oncey - 1
                $centerx = $centerx - 1
                $centery = $centery - 1
                $coords2 = PixelSearch($oncex,$oncey,$centerx,$centery,$wall,$shade,0,$handle)
;~              _WinAPI_DrawLine($handle,$oncex,$oncey,$centerx,$centery) ; horizontal left
            ElseIf $upperRight = 1 Then
                $oncex = $oncex + 1
                $oncey = $oncey - 1
                $centerx = $centerx + 1
                $centery = $centery - 1
                $coords2 = PixelSearch($oncex,$oncey,$centerx,$centery,$wall,$shade,0,$handle)
;~              _WinAPI_DrawLine($handle,$oncex,$oncey,$centerx,$centery) ; horizontal left
            ElseIf $lowerLeft = 1 Then
                $oncex = $oncex - 1
                $oncey = $oncey + 1
                $centerx = $centerx - 1
                $centery = $centery + 1
                $coords2 = PixelSearch($oncex,$oncey,$centerx,$centery,$wall,$shade,0,$handle)
;~              _WinAPI_DrawLine($handle,$oncex,$oncey,$centerx,$centery) ; horizontal left
            EndIf
        EndIf
    Until $centerx = $coords[0] Or $centery = $coords[1] Or IsArray($coords2)
    If IsArray($coords2) Then
        _WinAPI_DrawLine($handle,$coords[0],$coords[1],$coords[0],$coords[1]) ; horizontal left
        $objectisBlocked = True
    Else
        $objectisBlocked = False
    EndIf
    While 1
        Sleep(100)
        If $objectisBlocked = True Then
            ExitLoop
        EndIf
        If $centerx = $coords[0] Then
            Do
                Sleep(10)
                If $lowerRight = 1 Then
                    $centery = $centery + 1
                    $coords3 = PixelSearch($centerx,$centery + 1,$centerx,$centery + 1,$wall,$shade,0,$handle)
;~                  _WinAPI_DrawLine($handle,$centerx,$centery + 1,$centerx,$centery + 1) ; horizontal left
                ElseIf $upperLeft = 1 Then
                    $centery = $centery - 1
                    $coords3 = PixelSearch($centerx,$centery - 1,$centerx,$centery - 1,$wall,$shade,0,$handle)
;~                  _WinAPI_DrawLine($handle,$centerx,$centery + 1,$centerx,$centery + 1) ; horizontal left
                ElseIf $upperRight = 1 Then
                    $centery = $centery - 1
                    $coords3 = PixelSearch($centerx,$centery - 1,$centerx,$centery - 1,$wall,$shade,0,$handle)
;~                  _WinAPI_DrawLine($handle,$centerx,$centery - 1,$centerx,$centery - 1) ; horizontal left
                ElseIf $lowerLeft = 1 Then
                    $centery = $centery + 1
                    $coords3 = PixelSearch($centerx,$centery + 1,$centerx,$centery + 1,$wall,$shade,0,$handle)
;~                  _WinAPI_DrawLine($handle,$centerx,$centery + 1,$centerx,$centery + 1) ; horizontal left
                EndIf
            Until $centery = $coords[1] Or IsArray($coords3)
            If IsArray($coords3) Then
                _WinAPI_DrawLine($handle,$coords[0],$coords[1],$coords[0],$coords[1]) ; horizontal left
                $objectisBlocked = True
            Else
                Return True
            EndIf
        EndIf
        If $centery = $coords[1] Then
            Do
                Sleep(10)
                If $lowerRight = 1 Then
                    $centerx = $centerx + 1
                    $coords3 = PixelSearch($centerx + 1,$centery,$centerx + 1,$centery,$wall,$shade,0,$handle)
;~                  _WinAPI_DrawLine($handle,$centerx + 1,$centery,$centerx + 1,$centery) ; horizontal left
                ElseIf $upperLeft = 1 Then
                    $centerx = $centerx - 1
                    $coords3 = PixelSearch($centerx - 1,$centery,$centerx - 1,$centery,$wall,$shade,0,$handle)
;~                  _WinAPI_DrawLine($handle,$centerx + 1,$centery,$centerx + 1,$centery) ; horizontal left
                ElseIf $upperRight = 1 Then
                    $centerx = $centerx + 1
                    $coords3 = PixelSearch($centerx + 1,$centery,$centerx + 1,$centery,$wall,$shade,0,$handle)
;~                  _WinAPI_DrawLine($handle,$centerx + 1,$centery,$centerx + 1,$centery) ; horizontal left
                ElseIf $lowerLeft = 1 Then
                    $centerx = $centerx - 1
                    $coords3 = PixelSearch($centerx - 1,$centery,$centerx - 1,$centery,$wall,$shade,0,$handle)
;~                  _WinAPI_DrawLine($handle,$centerx - 1,$centery,$centerx - 1,$centery) ; horizontal left
                EndIf
            Until $centerx = $coords[0] Or IsArray($coords3)
            If IsArray($coords3) Then
                _WinAPI_DrawLine($handle,$coords[0],$coords[1],$coords[0],$coords[1]) ; horizontal left
                $objectisBlocked = True
            Else
                Return True
            EndIf
        EndIf
    WEnd
EndFunc

I am open to corrections suggestions and definitely efficiency.

Thanks again,

Steven

Edited by smcombs
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...