 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
anil_robo
Joined: 09 May 2008 Posts: 4
|
Posted: Sun May 11, 2008 9:59 am Post subject: New to scripts, need help with a looping function in game |
|
|
Hi all, this is my first experiment with scripting, and I'm glad I could write a working script to automate a game! I started the script small, kept adding new functionality to my existing script, and it is becoming larger and larger. But now I'm stuck, and the script stopped working as it should, and I didn't save the previous version
Here is what I want to do:
In words:
I want to start mining from point A. If I fail, I should keep trying mining at the same spot until I succeed. If I succeed, I can't mine at the same spot, so I must move to spot B to the right. At point B, I try to mine again repeatedly at the same point until success, and come back to point A if I succeed. So you see, the process should repeat, indefinitely.
Please notice that I didn't include the concept of "stamina recovery" in the diagram to keep it a little simple. As I keep trying to mine for minerals, I lose stamina. When stamina becomes zero, I can't mine any further. So I sit down on the ground to increase stamina recovery. After stamina recovers, I stand up again and resume mining from where I left.
Here is my script:
(Please note I never wrote any scripts before, and do not have a technical background. The script is highly inefficient I think)
| Code: |
;Give some time for the game screen to come up
Sleep, 1000
;Start from point A (labeled as 'Left')
Left:
Loop
{
;If mining is successful, a yellow color is displayed on screen in the middle.
PixelSearch, Px, Py, 10, 258, 790, 258, 0x01ffff, 1, Fast
if ErrorLevel
{
;If stamina is drained, player can't mine. Check for stamina drained.
PixelSearch, Px, Py, 564, 77, 564, 77, 0x000000, 0, Fast
if ErrorLevel
{
;V key starts mining.
Send {v}
Random, mine1, 10250, 10750
Sleep %mine1%
}
else
{
;Z key makes the player sit on ground for stamina recovery.
Send {z}
Sleep 125000
;Z key toggles the player to stand on ground, ready for action again.
Send {z}
Sleep 100
}
}
else
{
;E key makes character move towards right
Send {e down}
Sleep 1200
Send {e up}
;V key starts mining again
Send {v}
Random, mine2, 10250, 10750
Sleep %mine2%
Goto, Right
}
}
;Now the player is on point B on the right side.
Right:
Loop
{
;Successful mining will be denoted by yellow color as above.
PixelSearch, Px, Py, 10, 258, 790, 258, 0x01ffff, 1, Fast
if ErrorLevel
{
;Checking for drained stamina
PixelSearch, Px, Py, 564, 77, 564, 77, 0x000000, 0, Fast
if ErrorLevel
{
Send {v}
Random, mine3, 10250, 10750
Sleep %mine3%
}
else
{
;If stamina drained, press Z to sit down
Send {z}
Sleep 125000
;Press Z to stand up after stamina recovery
Send {z}
Sleep 100
}
}
else
{
;Q key moves the player to the left
Send {q down}
Sleep 1200
Send {q up}
;V key starts mining
Send {v}
Random, mine4, 10250, 10750
Sleep %mine4%
Goto, Left
}
} |
When I run this script, the player starts mining at the place where he's standing (as expected), but when there is a success (yellow message on screen) he moves to the left side, and keep moving left on further successes. So he wanders away from mining area, and this is not good for me.
Any ideas why the script is not working? |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 702
|
Posted: Sun May 11, 2008 10:15 pm Post subject: |
|
|
i don't know why it doesn't work, but you can try replacing the relevant block with this | Code: | {
;E key makes character move towards right
;E or Q will toggle between 'e' and 'q' per event
EorQ := (EorQ == "e") ? "q" : "e"
Send {%EorQ% down}
Sleep 1200
Send {%EorQ% up}
;V key starts mining again
Send {v}
Random, mine2, 10250, 10750
Sleep %mine2%
; Goto, Right
} |
then you can get rid of everything after the "right:" label. _________________ My Home Thread
More Common Answers: 1. It's in the FAQ 2. Ternary ( ? : ) guide 3. Post code with [code][/code] tags |
|
| Back to top |
|
 |
anil_robo
Joined: 09 May 2008 Posts: 4
|
Posted: Mon May 12, 2008 1:30 am Post subject: |
|
|
Thanks for the kind assistance, VxE.
I made the change, and the script now looks like this:
| Code: |
;Give some time for the game screen to come up
Sleep, 1000
;Start from point A
Loop
{
;If mining is successful, a yellow color is displayed on screen in the middle.
PixelSearch, Px, Py, 10, 258, 790, 258, 0x01ffff, 1, Fast
if ErrorLevel
{
;If stamina is drained, player can't mine. Check for stamina drained.
PixelSearch, Px, Py, 564, 77, 564, 77, 0x000000, 0, Fast
if ErrorLevel
{
;V key starts mining.
Send {v}
Random, mine1, 10250, 10750
Sleep %mine1%
}
else
{
;Z key makes the player sit on ground for stamina recovery.
Send {z}
Sleep 125000
;Z key toggles the player to stand on ground, ready for action again.
Send {z}
Sleep 100
}
}
else
{
;E key makes character move towards right
;E or Q will toggle between 'e' and 'q' per event
EorQ := (EorQ == "e") ? "q" : "e"
Send {%EorQ% down}
Sleep 1200
Send {%EorQ% up}
;V key starts mining again
Send {v}
Random, mine2, 10250, 10750
Sleep %mine2%
}
}
;Now the player is on point B on the right side. |
The script works fine for some time now, the player is mining at point A, and moves to point B on success, and vice versa. However the player stops moving after a variable number of successes have been made. And then I have to restart the script, and then sometimes the player starts moving in reverse order (from right to left rather than left to right). It appears totally random to me. I hope someone is able to find the fault in the script. I'm trying to generate a log file for the events, but the documentation is a little too geeky for someone like me. However I won't give up on this! |
|
| Back to top |
|
 |
anil_robo
Joined: 09 May 2008 Posts: 4
|
Posted: Wed May 14, 2008 6:25 am Post subject: |
|
|
Finally got it working! Here it is:
| Code: |
;Give some time for the game screen to come up
Sleep, 1000
Loop
{
;Start from point A
;If mining is successful, a yellow color is displayed on screen in the middle.
PixelSearch, Px, Py, 211, 234, 318, 373, 0x00ffff, 0, Fast
if ErrorLevel
{
;If stamina is drained, player can't mine. Check for stamina drained.
PixelSearch, Px, Py, 564, 77, 564, 77, 0x000000, 0, Fast
if ErrorLevel
{
;V key starts mining.
Send {v}
Random, mine1, 10500, 10750
Sleep %mine1%
}
else
{
;Z key makes the player sit on ground for stamina recovery.
Send {z}
Sleep 122500
;Z key toggles the player to stand on ground, ready for action again.
Send {z}
Sleep 500
}
}
else
{
;Play a success sound
SoundPlay, success.wav
;E key makes character move towards right
;E or Q will toggle between 'e' and 'q' per event
EorQ := (EorQ == "e") ? "q" : "e"
Send {%EorQ% down}
Sleep 1200
Send {%EorQ% up}
;V key starts mining again
Send {v}
Random, mine2, 10500, 10750
Sleep %mine2%
}
}
;Now the player is on point B on the right side. |
I not only got it working, but also added new features like playing a sound on successful mining
The initial problems was most likely due to wrong pixel coordinates. I was using a pixel search area one pixel in height and almost entire screen in length, that could have triggered input from some other elements on the screen rather than mining success message.
I'm thinking that instead of setting only one area for pixel search, I should have two areas on the screen. Simultaneous reading of both areas resulting in same color (yellow) will indicate mining success. This is less likely to yield "false positives" such as a player wearing yellow clothes running around.
This leads to another noob question:
How can I cause an event based on pixelsearch results from two areas on the screen? I tried doing it but messed it up very badly  |
|
| Back to top |
|
 |
sinkfaze
Joined: 19 Mar 2008 Posts: 113
|
Posted: Wed May 14, 2008 6:34 am Post subject: |
|
|
Show us the relevant code that failed for you so we can see what may have gone wrong and if/how it can be fixed. Also, tell us what the script should have done on screen and what it actually did instead. _________________ Have trouble searching the site for information? Try Quick Search for Autohotkey. |
|
| Back to top |
|
 |
anil_robo
Joined: 09 May 2008 Posts: 4
|
Posted: Fri May 16, 2008 5:13 am Post subject: Question: Search two pixels of different colors at same time |
|
|
| sinkfaze wrote: | | Show us the relevant code that failed for you so we can see what may have gone wrong and if/how it can be fixed. Also, tell us what the script should have done on screen and what it actually did instead. |
Thanks Sinkfaze, for the reply.
The code I posted above is now working fine, I think my computer was acting up due to overheating (cpu temperature was 82 degrees celsius). Everything is working fine now after my air conditioner is fixed.
But my last question still remains unanswered:
If I want an event to take place after two different pixels on the screen are of two different colors at the same time (e.g. x1,y1 has a shade of red; x2,y2 is black - simultaneously), how will I go about this? Sorry I tried searching the forums and the documentation but my non-technical background is a hindrance
Just for the sake of someone being able to peep into my mind, here is my nonworking new script:
| Code: | if (PixelSearch, x1, y1, 704, 44, 704, 44, 0x0000b8, 0, Fast) and (PixelSearch, x2, y2, 600, 141, 600, 141, 0x000000, 0, Fast) = true
Msgbox Two colors are found in their respective locations!
else
Msgbox Two respective colors were not found in their respective locations. |
(I have checked the mentioned colors in the respective positions do exist with Autoit3 Window Spy, still I get the "Two respective colors were not found" message. The script is able to runs though, and there are no error messages given.) |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 702
|
Posted: Fri May 16, 2008 5:31 am Post subject: |
|
|
if it helps, you can turn any AHK command into a function, like: | Code: | Pixel_Search( byref OutputVarX, byref OutputVarY, X1, Y1, X2, Y2, ColorID , Variation = 0, mode = "" )
{
PixelSearch, OutputVarX, OutputVarY, % X1, % Y1, % X2, % Y2, % ColorID , % Variation, % mode
return ErrorLevel
} |
untested _________________ My Home Thread
More Common Answers: 1. It's in the FAQ 2. Ternary ( ? : ) guide 3. Post code with [code][/code] tags |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|