Jump to content

Constant Pixelcheck In Background Of Script


Recommended Posts

Is there a way to have a PixelChecksum running in the background while your script is doing other things, including clicking on the screen and doing other PixelChecksums? Like if you are in an online game with someone, like an online Battleship or something, and there is a built in chat window that pops up when they talk to you, would there be a way to check for that window, but also continue clicking and doing other checks in different parts of the screen? I think it is possible, but I'm not sure if it would conflict with other pixelchecks or even with running speed.

Any ideas would be appreciated. General layout follows:

Func StartProgram()

$somevariable = PixelChecksum(x1,y1,x2,y2)

;While $somevariable doesn't occur...

;BLOCK CODE

$someothervariable = PixelCheck(x3,y3,x4,y4)

MouseClick("left",somex,somey)

~extra things~

;End block code if $somevariable occurs

Func SomeVariableHappened()

~some other code~

StartProgram()

[Above obviously not actual code, as it has not been written yet.]

Link to comment
Share on other sites

You could give my Multithreading code a try. The include file is linked in my signature. It would look something like this:

#include "Coroutine.au3"

$childThread = _CoCreate('Func _CheckSum($x1, $y1, $x2, $y2)|$badCheckSum = "123456789"|$continue = "true"|$somevariable = PixelCheckSum($x1, $y1, $x2, $y2)|While $somevariable <> $badCheckSum|$somevariable = PixelCheckSum($x1, $y1, $x2, $y2)|Sleep(100)|WEnd|$continue = _CoYield("error");Pauses coroutine and yields the value "error", waits for call to _CoResume()|If $continue <> "true" Then|ExitLoop|EndIf|WEnd|EndFunc');Creates coroutine template
$PID1 = _CoStart($childThread);Starts instance of coroutine

; Game-playing code block
While $gameNotOver == "true"
  ; Play Game
    If _CoStatus($PID1) == "yielded" Then
        $yieldedValue = _CoResume($PID1);Retrieves Yielded value and resumes coroutine script
        If $yieldedValue == "error" Then
        ;Handle Error
        EndIf
    EndIf
WEnd

_CoCleanup()

It might seem a little foreign, but I think this is exactly what you're looking for. Sure, there are other ways to do it, but this way is a bit more clean, and easy to update or tweak. If you have any further problems, feel free to PM me some of your code, and i'll help you out. Multithreading is confusing stuff.

Edit: Forgot to add _CoCleanup() to the end of the script.

Edited by neogia

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Link to comment
Share on other sites

Well, I got the code for the Coroutine.au3...can't say I understand what it does in and of itself. But as to the other code that you posted, neogia, I'm not really sure how to use that.

I don't have the coding for the program typed out, but I don't exactly know what your code does. Could you be a little clearer?

Edited by moviemadnessman
Link to comment
Share on other sites

Well, I got the code for the Coroutine.au3...can't say I understand what it does in and of itself. But as to the other code that you posted, neogia, I'm not really sure how to use that.

I don't have the coding for the program typed out, but I don't exactly know what your code does. Could you be a little clearer?

Actually, I did find a bit of code I could use for this example. I'll attach it to this post. If you could show me the code to check multiple areas and run the code, that would be great. Or help me to learn so I can do it myself. Although in the case of neogia's multithreading code, I think it would be easier to just show me, then explain what you did.

Anyone else who has ideas, feel free to show me the light.

Main_Program.au3

Link to comment
Share on other sites

  • Moderators

The answer to your question is right in the file you posted:

$checksum = PixelChecksum($talx, $taly, $tarx, $tary); Get the original sum
  While $checksum = PixelChecksum($talx, $taly, $tarx, $tary)
    Sleep(100); sleep (do nothing) until the checksum area is different
    Wend

Edit:

This is probably how I would explain it:

Local $CheckSum = PixelChecksum($xtopleft, $ytopleft, $xbottomright, $ybottomright); Original Value
Do
    $CompareSums = PixelChecksum($xtopleft, $ytopleft, $xbottomright, $ybottomright); Keep checking area
    Sleep(100); Give the CPU a rest and only check the area every 100 milliseconds
Until $CompareSums <> $CheckSum; If the new value is less than or greater than the original value, leave the loop and do next task.
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

The answer to your question is right in the file you posted:

$checksum = PixelChecksum($talx, $taly, $tarx, $tary); Get the original sum
  While $checksum = PixelChecksum($talx, $taly, $tarx, $tary)
    Sleep(100); sleep (do nothing) until the checksum area is different
Wend
That is indeed the code to start the loop. But while the loop (TradeReady) is running, I need it to constantly be running this code at the same time:

$var = PixelGetColor($wpcx, $wpcy)

If $var = $wpcc Then

MouseClick("left", $wtx, $wty, 2, 1)

TradeReady()

EndIf

So while the TradeReady function is going about its business, it also needs to know if the above code happens, because if it does, then the TradeReady code can never complete itself. It also needs to run:

$pm = PixelChecksum($pmclx, $pmcly, $pmcrx, $pmcry)

While $pm =PixelChecksum($pmclx, $pmcly, $pmcrx, $pmcry)

Sleep(100)

WEnd

MouseClick("left", $pmtx, $pmty, 2, 1)

Send($pmmessage)

Send("{ENTER}")

MouseClick("left", $pmcx, $pmcy, 2, 1)

TradeReady()

in the background at the same time, for the same reason.

That's what I need to know how to do, if it is even possible.

Link to comment
Share on other sites

  • Moderators

Is this conditional? I mean, does the trade stuff only happen if the Checksums are the same? If so, just stick that stuff inside the loop.

Try this... List every step of action that will be taking:

1. Do something

2. Do something else

3. If this condition is true then do this, if not then do this

etc....

You may not need to multithread is my point.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Is this conditional? I mean, does the trade stuff only happen if the Checksums are the same? If so, just stick that stuff inside the loop.

What happens is this: The program looks for someone wanting to look at my items for trade. It accepts, and the coding will trade with them and then confirm and close all the trade place windows to restart itself. However, if they leave at any point after requesting a trade, the program needs to be able to recognize this and be able to stop itself. However, I can't have it checking after every line of code, because the program already needs to look at what the person has, how many I need, then worry about making sure the amount the person takes from me is the same as I get from them.

So Basically, I'm trying to get it so it will run the tradeready code, but at the same time it has to make sure they don't leave, or if they do leave it must know to reset itself.

Then on top of all that, the whole time the program starts up until the program is exited out, it needs to check to see if a chat window pops up, so that it can close it as soon as possible.

That's pretty much the problem. I can't get the program to run all its cod AND check the two conditionals at the same time.

Link to comment
Share on other sites

  • Moderators

Is this for a game? Sounds like one my friend plays (I have no idea what the name of it is though). But from your description, it just sounds like you need a loop to check, if check is true, then another loop to trade while checking to make sure the original check is still true, and making sure that they are still there.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Is this for a game? Sounds like one my friend plays (I have no idea what the name of it is though). But from your description, it just sounds like you need a loop to check, if check is true, then another loop to trade while checking to make sure the original check is still true, and making sure that they are still there.

It's a combination of creating samples for future use, and in the process making a trading program for Magic Online. As to the double loop, how would the code look? Would it be something along the lines of:

Do TradeReady()

Until $var = PixelGetColor($wpcx, $wpcy)

If $var = $wpcc Then

MouseClick("left", $wtx, $wty, 2, 1)

TradeReady()

EndIf

I thought [and past code I've both written and used on other platforms support this] that loop 1, loop 2 would make loop2 complete before finishing loop1. If this is the case, then I can't see how the double loop would work. I feel kind of stupid for having to ask for something like a loop, but even those who know alot need help from time to time {my sig says it all}.

I just don't see how I'd go about setting up the outside loop so that the inside loop works, but at the same time keep the outside loop in control, almost.

Link to comment
Share on other sites

  • Moderators

Making it conditional, if you know what you want to do with what checksum values, maybe something like this:

While 1
    $Checksum = PixelChecksum($xtopleft, $ytopleft, $xbottomright, $ybottomright)
    If $Checksum  = $SomeValue Then
        TradeFunc($Checksum)
    EndIf
WEnd

Func TradeFunc($i_Checksum)
    If $i_Checksum = $DoThisValue Then
        While 1
            $NewSum = PixelChecksum($xtopleft, $ytopleft, $xbottomright, $ybottomright)
    ;Do whatever
            If $NewSum <> $i_Checksum Then ExitLoop; return back to original loop
            Sleep(10)
        WEnd
    ElseIf $i_Checksum = $DoThisOtherValue Then
        While 1
            $NewSum = PixelChecksum($xtopleft, $ytopleft, $xbottomright, $ybottomright)
    ;Do whatever
            If $NewSum <> $i_Checksum Then ExitLoop; return back to original loop
            Sleep(10)
        WEnd
    EndIf
EndFunc

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Making it conditional, if you know what you want to do with what checksum values, maybe something like this:

While 1
    $Checksum = PixelChecksum($xtopleft, $ytopleft, $xbottomright, $ybottomright)
    If $Checksum  = $SomeValue Then
        TradeFunc($Checksum)
    EndIf
WEnd

TradeFunc($i_Checksum)
    If $i_Checksum = $DoThisValue Then
        While 1
            $NewSum = PixelChecksum($xtopleft, $ytopleft, $xbottomright, $ybottomright)
    ;Do whatever
            If $NewSum <> $i_Checksum Then ExitLoop; return back to original loop
            Sleep(10)
        WEnd
    ElseIf $i_Checksum = $DoThisOtherValue Then
        While 1
            $NewSum = PixelChecksum($xtopleft, $ytopleft, $xbottomright, $ybottomright)
    ;Do whatever
            If $NewSum <> $i_Checksum Then ExitLoop; return back to original loop
            Sleep(10)
        WEnd
    EndIf
EndFunc
What is $i_checksum, or actually, where is it defined? This code may do what I need it to; I'll test it out, but it could be a little while before I can post if it works or not (have to finish the script). Also, is TradeFunc the same as saying Func Trade? Just curious.
Link to comment
Share on other sites

  • Moderators

What is $i_checksum, or actually, where is it defined? This code may do what I need it to; I'll test it out, but it could be a little while before I can post if it works or not (have to finish the script). Also, is TradeFunc the same as saying Func Trade? Just curious.

No, I had a typo... should have been Func TradeFunc($i_Checksum) and it is defined in the first While1 loop up top, it sends the value it found to that function. I'll fix my typo.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Ok, I'm going to explain my code in detail:

$TradeReady = _CoCreate('Func TradeReady()|;Put your working code for TradeReady() here, with each line separated by the pipe symbol, like this |EndFunc', "|")

So this piece of code isn't hopefully too confusing. It basically equates to this:

Func TradeReady()
   ;Put your working code for TradeReady() here, with each line separated by the pipe symbol, like this
EndFunc

Your working code will definitely add some length to this function, but it's pretty simple, just remove the line breaks from your "TradeReady()" function and replace them with "|", then throw it in the _CoCreate() function, which creates a "Coroutine", and the ID for that coroutine is stored in $TradeReady, the variable. Now you can create more of these coroutines.. create one for each simultaneously running loop or function that you want. Here's some more you'll probably need:

$WatchForTradeCancel = _CoCreate('Func WatchForTradeCancel()|$badCheckSum = "123456789";Put in the checksum that means the trade has been cancelled|$checkSum = PixelCheckSum(x1, y1, x2, y2)|While $checkSum <> $badCheckSum;While the trade window is still open|$checkSum = PixelCheckSum(x1, y1, x2, y2)|Sleep(100)|WEnd|EndFunc')

$ChatWindowHandler = _CoCreate('Func ChatWindowHandler()|$badCheckSum = "123456789';Put in the checksum that means you have received a chat|While 1;Continue looping until loop is exited|$checkSum = PixelCheckSum(x1, y1, x2, y2)|While $checkSum <> $badCheckSum|$checkSum = PixelCheckSum(x1, y1, x2, y2)|Sleep(100)|WEnd|;This is the code that handles each time you receive a chat|MouseClick("left", x, y)|Send("PM Message")|Send("{ENTER}")|MouseClick("left", x, y)|WEnd|EndFunc')

Here's those two in easy-to-read format:

Func WatchForTradeCancel()
    $badCheckSum = "123456789";Put in the checksum that means the trade has been cancelled
    $checkSum = PixelCheckSum(x1, y1, x2, y2)
    While $checkSum <> $badCheckSum;While the trade window is still open
        $checkSum = PixelCheckSum(x1, y1, x2, y2)
        Sleep(100)
    WEnd
EndFunc

Func ChatWindowHandler()
    $badCheckSum = "123456789';Put in the checksum that means you have received a chat
    While 1;Continue looping until loop is exited
        $checkSum = PixelCheckSum(x1, y1, x2, y2)
        While $checkSum <> $badCheckSum
            $checkSum = PixelCheckSum(x1, y1, x2, y2)
            Sleep(100)
        WEnd
       ;This is the code that handles each time you receive a chat
        MouseClick("left", x, y) 
        Send("PM Message") 
        Send("{ENTER}") 
        MouseClick("left", x, y) 
    WEnd
EndFunc

Ok, now you've got three... I guess you can call them templates, for functions that you want running at the same time as the main script: $TradeReady, $WatchForTradeCancel, and $ChatWindowHandler. You can add more as you see fit, but these three should get you started. Now you need to construct your main loop.

;Coroutine construction code above

HotKeySet("{END}", "_StopTrading");Finish Current Trade and exit
HotKeySet("{DEL}", "_Exit");Stop immediately and cancel any current trades

_CoStart($ChatWindowHandler);This will run by itself and handle any chats that come in

$tradeCheckSum = "123456789";This checksum means a trade is open
$tradeSuccessful = "true";Just a tracking variable to determine if trade was... well successful
$keepTrading = "true";This is used by _StopTrading() to tell script to finish trade and quit
While 1
    If $keepTrading == "true" Then
        $checkSum = PixelCheckSum(x1, y1, x2, y2);Check if a trade window is open.
        While $checkSum <> $tradeCheckSum
            $checkSum = PixelCheckSum(x1, y1, x2, y2)
            Sleep(100)
            If $keepTrading == "false" Then
                ExitLoop
            EndIf
        WEnd
        $PID_TradeReady = _CoStart($TradeReady);This starts an instance of the coroutine you created earlier, and stores a reference to this "instance" in $PID_TradeReady
        $PID_WatchForTradeCancel = _CoStart($WatchForTradeCancel)
       ;Now you have three coroutines all running separate of eachother
       ;ChatWindowHandler will take care of itself, but you need to check the status of the other two coroutines to determine what to do
        While _CoStatus($PID_TradeReady) <> "dead";While the trade hasn't finished
            If _CoStatus($PID_WatchForTradeCancel) == "dead" Then
                _CoKill($PID_TradeReady);If the trade was cancelled, then you want to cancel your $TradeReady instance with _CoKill()
                $tradeSuccessful = "false"
            EndIf
        WEnd
        _CoKill($PID_WatchForTradeCancel);Just in case it doesn't close if the trade was successful, maybe a different checksum?
       ;This $tradeSuccessful code is optional, just to determine if the trade was cancelled or not.
        If $tradeSuccessful == "true" Then
           ;Do some stuff
        ElseIf $tradeSuccessful == "false" Then
           ;Do some other stuff
        EndIf
        $tradeSuccessful = "true";Reset success variable
    Else;$keepTrading is false
        ExitLoop
    EndIf       
WEnd

Func _StopTrading()
    $keepTrading = "false"
EndFunc

Func _Exit()
    _CoCleanup();This stops all current coroutines, and deletes the coroutine temp files
    Exit
EndFunc

So this part is your main script. It sets a couple hotkeys so you can exit the script, then it starts the chat handler which will run until it is stopped by _CoCleanup(). Then starts the main loop which checks to see if a trade window is open, and if it is, starts an instance of $TradeReady, and $WatchForTradeCancel. Then the loop checks to see if their status is "dead" (in other words they've finished), and depending on which one is finished, tells you if the trade was successful or not.

Hopefully, this is a bit more clear.

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Link to comment
Share on other sites

Thanks to both neogia and smoke_n for their help with this. I will test the two codes and see if everything works alright, and when I get the code working (when I finish writing all the code), I'll post again about if it the program does what I want.

Thanks again for the help, guys, even if it doesn't work, I think I can say I've learned more.

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...