Jump to content

Loops within a loop


Recommended Posts

Hello, new to this forum and AutoIt, this is my first attempt at creating a script in order to perform the role of a rather monotonous task.

After much trial and error I have nearly succeeded in completing this 'bot', I am at the point where the script will search for a pixel colour and click on that pixel repetedly in order to defeat the target. My intention was to perform a PixelSearch in multiple ranges, the idea being that I could prioritise targets closer to me and attack only them, as opposed to the cursor randomly clicking every pixel it found of a paticular colour as it did when I first started making this script today. So far this appears to work, however I encountered another problem, being that the script was only attacking things when they came close to me, so I divided all the PixelSearch into their own loops, and if the desired pixel wasn't found to move onto the next loop (or rather increase the scan range). Now my problem is as far as I know that once this script has run once it does not run again, the result being that I attack a target only once, and it seems since I introduced the loops that it often will not attack at all, although I suspect this might be because PixelSearch isn't scanning as often as is therefore missing my defined colour.

As I am typing this I have thought of another question which could invalidate all my work so far, the way I have set these loops up, am I correct in saying that if PixelSearch found pixels at a greater distance from me, is it just as likely to click on those pixels or will it click on the one's closest to me first (or rather the one's within the smallest given range)

The question I originally started this topic for was ... is it possible to create an infinite loop that will run the 5 other loops. With the purpose of this script being able to run, doing what I intended it to do for an indefinate amount of time?

My apologies if I have rambled but I have not slept in 40 hours.

Many thanks in advance.

; Declare Variables
$atk = 0

; ToolTip displaying function keys
Tooltip("HOME = On/Off   END = Exit", 0, 0)

; List of command keys
If Not WinActive("Soma Mythological Biography", "") Then WinActivate("Soma Mythological Biography", "")
HotKeySet("{HOME}", "atkBrics")
HotKeySet("{END}", "exitNow")
WinWaitActive("Soma Mythological Biography", "")

; Scan range loops
While True
   If $atk = 1 Then
      $coord1 = PixelSearch(320,264,482,368,0x6B5142,5)
        If NOT @error Then
            MouseClick("left", $coord1[0], $coord1[1], 1, 0)
            Sleep(50)
                If $coord1 = @error Then ExitLoop
                EndIf
        EndIf
WEnd
    
While True
    If $atk = 1 Then
       $coord2 = PixelSearch(239,193,556,448,0x6B5142,5)
        If NOT @error Then
            MouseClick("left", $coord2[0], $coord2[1], 1, 0)
            Sleep(50)
                If $coord2 = @error Then ExitLoop
                EndIf
        EndIf
WEnd

While True
    If $atk=1 Then
       $coord3 = PixelSearch(144,128,645,456,0x6B5142,5)
        If NOT @error Then
            MouseClick("left", $coord3[0], $coord3[1], 1, 0)
            Sleep(50)
                If $coord3 = @error Then ExitLoop
                EndIf
        EndIf
WEnd

While True
    If $atk = 1 Then 
       $coord4 = PixelSearch(56,63,757,525,0x6B5142,5)
        If NOT @error Then
            MouseClick("left", $coord4[0], $coord4[1], 1, 0)
            Sleep(50)
                If $coord4 = @error Then ExitLoop
                EndIf
        EndIf
WEnd
    
While True
    If $atk = 1 Then
       $coord5 = PixelSearch(3,23,779,548,0x6B5142,5)
        If NOT @error Then
            MouseClick("left", $coord5[0], $coord5[1], 1, 0)
            Sleep(50)
                If $coord5 = @error Then ExitLoop
                EndIf
        EndIf
    WEnd


; Functions
; On/Off atkBrics():
Func atkBrics()
   If $atk = 0 Then
      $atk = 1
      Tooltip("Auto attacking Brics (HOME to disable)", 0, 0)
   Else
      $atk = 0
      Tooltip("HOME = On/Off    END = Exit", 0, 0)
   EndIf
EndFunc

; Exit
Func exitNow()
   Exit
EndFunc
Link to comment
Share on other sites

Umm

; Declare Variables
$atk = 0

; ToolTip displaying function keys
Tooltip("HOME = On/Off   END = Exit", 0, 0)

; List of command keys
If Not WinActive("Soma Mythological Biography", "") Then WinActivate("Soma Mythological Biography", "")
HotKeySet("{HOME}", "atkBrics")
HotKeySet("{END}", "exitNow")
WinWaitActive("Soma Mythological Biography", "")

; Scan range loops
While True
    If $atk = 1 Then _SearchClick(320,264,482,368,0x6B5142,5)
    If $atk = 1 Then _SearchClick(239,193,556,448,0x6B5142,5)
    If $atk = 1 Then _SearchClick(144,128,645,456,0x6B5142,5)
    If $atk = 1 Then _SearchClick(56,63,757,525,0x6B5142,5)
    If $atk = 1 Then _SearchClick(3,23,779,548,0x6B5142,5)
WEnd

Func _SearchClick($a, $b, $c, $d, $e, $f)
    $coord = PixelSearch($a,$b,$c,$d,$e,$f)
    If NOT @error Then
        MouseClick("left", $coord[0], $coord[1], 1, 0)
        Sleep(50)
    EndIf
EndFunc

; Functions
; On/Off atkBrics():
Func atkBrics()
   If $atk = 0 Then
      $atk = 1
      Tooltip("Auto attacking Brics (HOME to disable)", 0, 0)
   Else
      $atk = 0
      Tooltip("HOME = On/Off    END = Exit", 0, 0)
   EndIf
EndFunc

; Exit
Func exitNow()
   Exit
EndFunc
Edited by omikron48
Link to comment
Share on other sites

It would seem that I am in the same situation as before, where the mouse is jumping around all possible pixels within the specified range constantly, what I need to do is get it to scan in an increasing area, then failing to find anything begin the scan from the smallest area and work it's way back up. Secondly I need it to prioritise clicking the pixel in the smallest scan area before then scanning again from the smallest area back up to the largest area. Perhaps I should note that this game is 2D/Isometric

Link to comment
Share on other sites

Thats what i'm trying to do, got a feeling I have to put something between these

If $atk = 1 Then _SearchClick(320,264,482,368,0x6B5142)

If $atk = 1 Then _SearchClick(239,193,556,448,0x6B5142)

If $atk = 1 Then _SearchClick(144,128,645,456,0x6B5142)

If $atk = 1 Then _SearchClick(56,63,757,525,0x6B5142,5)

If $atk = 1 Then _SearchClick(3,23,779,548,0x6B5142,5)

It's just a matter of finding the appropriate commands at the moment

Link to comment
Share on other sites

You can try writing a function of your own which uses PixelGetColor and searches in an outward spiral from a center pixel, and then returns the coordinates of a matched pixel. What you need to consider for this is setting a maximum box size to limit the amount of looping you want in a single search. After that, it's just a matter of looping through the pixels surrounding your center, which are square outlines in increasing sizes. You just map out your search pixels relative to the center.

Edited by omikron48
Link to comment
Share on other sites

I have found the following code for finding the closest coords, I'm just not sure where to implement a MouseClick or even if this would work, but then I don't see why it wouldn't but I could just be being a n00b :)

After some time it ends with this error

(32) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Dim $output[$Array[0][0]+1]

Dim $output[^ ERROR

>Exit code: 1 Time: 83.314

;Declare Variables
$atk = 0

Global $coord5

;ToolTop Displaying function keys  0x8C8A8C
Tooltip("HOME = On/Off    END = Exit", 0, 0)

;List of command keys
If Not WinActive("Soma Mythological Biography", "") Then WinActivate("Soma Mythological Biography", "")
HotKeySet("{HOME}", "atkBrics")
HotKeySet("{END}", "exitNow")
WinWaitActive("Soma Mythological Biography", "")

;Scan range loops
While True
    If $atk = 1 Then _SearchClick(320,264,482,368,0x6B5142,5)
    If $atk = 1 Then _SearchClick(239,193,556,448,0x6B5142,5)
    If $atk = 1 Then _SearchClick(144,128,645,456,0x6B5142,5)
    If $atk = 1 Then _SearchClick(56,63,757,525,0x6B5142,5)
    If $atk = 1 Then _SearchClick(3,23,779,548,0x6B5142,5)
WEnd

Func _SearchClick($a, $b, $c, $d, $e, $f)
    $coord5 = PixelSearch($a,$b,$c,$d,$e,$f)
    If NOT @error Then
        Call("_Closest");MouseClick("left",$coord5[0],$coord5[1],1,0) I have ommited this line for the time being as I want the MouseClick to occur after finding the closest coords
    EndIf
EndFunc

Func _Closest($Array = $coord5,$XCenter = 329,$YCenter = 402)
    Dim $output[$Array[0][0]+1]
    $output[0] = $Array[0][0]
    For $i = 1 to $Array[0][0]
        $output[$i] = Round(Sqrt(($Array[$i][0]-$XCenter)^2+($Array[$i][1]-$YCenter)^2),2)
    Next
    $t = 0
    $s = $output[1]
    For $i = 1 to $output[0]
        If $output[$i] < $s Then 
            $s = $output[$i]
            $t = $i
        EndIf
    Next
    Dim $output[2]
    $output[0] = $Array[$t][0]
    $output[1] = $Array[$t][1]
    Return $output
EndFunc
    

;Functions
;On/Off atkBrics():
Func atkBrics()
    If $atk = 0 Then
        $atk = 1
        Tooltip("Auto attacking Brics (HOME to disable)", 0, 0)
    Else
        $atk = 0
        Tooltip("Home = On/Off     END = Exit", 0, 0)
    EndIf
EndFunc

;Exit
Func exitNow()
    Exit
EndFunc

Is it possible to have the script perform a pixel search on the active window, then put the co-ords of the foun pixels through pythags theorem and have that output these co-ords with their relative distances into a table or array, that could store perhaps 8-12 lines of co-ords and relative distance (that could be updated constantly) then have the script use this information to perform a MouseClick at the co-ords with the lowest relative distance?

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