Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Ask gaming related questions (AHK v1.1 and older)
Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 21 Jan 2022, 17:31

Greetings,

I'm new to this.. so please forgive my not great code and issues. I'm working on a fishing script for a game called FarmRPG and need some help perfecting it and optimizing it. I'll try to provide plenty of info and details below.

Explanation of Fishing:
There is a graphic of water where an image of a fish fades in and out at random places among the water. The areas sometimes have different shades for the water, thus resulting in slight differences in the color of the fish overall (Its subtle.. but in trying to code this I've noted differences because I can't seem to get a consistent bot across them. After clicking on the fish a slider rises from the bottom of the screen with a blue dot that randomly moves back and forth. Speed of the movement varies and gets faster the more difficult the fishing becomes. Each attempt at fishing uses one bait whether you catch a fish or not.

My Code Process:
Ive setup my code where you input the bait you have, Press F1 to select the Top Left of the Fishing Area, Followed by a second F1 press in the bottom right to rectangle out the actual fishing area. Then the code scans for the Fish and left clicks when it sees it. Then scans for the blue button and clicks it when it sees it. Then adds a count to the number of fish caught. The Script terminates when you press F2, or if your fish caught matches the bait you wanted to use.

Since the slider for catching the fish rises from the bottom of the screen, I enlarge the browser window so that it comes up inside the area that I have selected for my rectangle using F1 above so that it looks for it in the same area.

Here is what I have so far:

Code: Select all

xpos1=
xpos2=
xblu1=
SetMouseDelay,-1
SetBatchLines, -1
Process, Priority,, High

InputBox, howmuchbait, Bait Amount,How much bait to use?, ; Input Amount of Bait to Use
tooltip, Put mouse in top left corner of pond and press F1
Return
f1::
If xpos1 =  
	{
	MouseGetPos, xpos1, ypos1
	tooltip, Put mouse in bottom right corner of pond and press F1
	Return
	}
If xpos2 =  
	{
	MouseGetPos, xpos2, ypos2
	tooltip, Put mouse in the Left Middle of pond and press F1
	Return
	}
If xblu1 = 
	{
	MouseGetPos, xblu1, yblu1
	}
tooltip, fishing will begin in 3 seconds. Press F2 to end early
sleep 3000
tooltip
loop	{
	sleep 500
	PixelSearch, 1Px, 1Py, %xpos1%, %ypos1%, %xpos2%, %ypos2%, 0x50452F, 80, Fast ; Find Fish
	if ErrorLevel	
		{
		}
	else	
		{
		ClickAdjustFishY:= 1Py+=30
		MouseClick, left, %1Px%, %ClickAdjustFishY% ; Click on Fish
		fishcount++	; Increase Fish Count
		sleep 1000
			loop	{
			loopcount++ ; Displays a count on screen to verify search is progressing
			PixelSearch, 2Px, 2Py, %xblu1%, %yblu1%, %xpos2%, %ypos2%, 0xFF0000, 7, Fast ; Find Blue Button
				If (ErrorLevel = 1)	{
					tooltip, I found the Circle! 
					ClickAdjustBluY:= 2Py+=45	
					MouseMove, %2Px%, %ClickAdjustBluY%	; Used to test Mouse Click Position
					MouseClick, left, %2Px%, %ClickAdjustBluY% ; Click on Blue Button
					tooltip, I tried to click the Circle!
					break
							}
			Else		{
					tooltip, No Circle Found. Attempt %loopcount%
					}
				}
		sleep 500
		if fishcount = %howmuchbait%	; End Fishing if Fishcount = Bait Wanted to Use
			{
			break
			}
		}
	}	
	
f2::
Msgbox, I got you %fishcount% fish ; Display number of fish caught
reload	;exitapp
Here are the issues that I have run into:
1. Due to what I assume are suble differences in the color of the fish because of the water graphic, I am unable to consistently get a click 100% of the time on the fish itself. I also have issues with it consistently detecting the fish every time it appears on the same area. I don't know if its how quickly the fish fades in and out or if it is that there a slight color differences across the different areas of the water.
2. Due to the speed increase of the blue dot I have been unable to get something that can reliably click it at later levels.
3. My Fishing counter also seems to be inaccurate.. as the amount of fish caught often does not equal the amount of bait used.. It usually counts up more quickly than the usage of bait.

Images of Each Level
https://imgur.com/a/Ueqa5rR
I tried to screenshot a point when the fish was visible.. Since it fades in from 0 Opacity to whatever the max is then back to 0 the opacity levels are all different of course.. but This kinda shows how the color shifts a little for each background. Especially the lava area.

Video of Level 1 being farmed with Bot:
https://imgur.com/l0fYbxE
Note How the bot just doesn't detect the fish all the time.

Video of Bot on Level 9:

https://imgur.com/QTevKA3
Note How bot does not detect fish at all. Also shows example of speed of Blue dot at the end of the video.

If anyone is willing to help with this it will be greatly appreciated. I can provide any more info if necessary. Thanks in advance!
Last edited by Drknyss on 23 Jan 2022, 11:25, edited 2 times in total.

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 22 Jan 2022, 23:04

Bumping this for any input or opinions. I'm having a lot of issue with the second part of the script (Clicking the moving blue circle).. I can't quite get that part nailed down....

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 23 Jan 2022, 10:49

So I am baffled... In my Pixelsearch.. Even though the Circle is a Solid Blue, 0xFF0000 color... My script does not seem to detect it at all.

The X and Y coords for searching are input by mouseclicks earlier in the thread. I know the scan area works because I am running a different pixel search before it.

Full Code updated and put in the first post. The lower half with the blue circle that isn't working is below.

Code: Select all

loop	{
			loopcount++ ; Displays a count on screen to verify search is progressing
			PixelSearch, 2Px, 2Py, %xblu1%, %yblu1%, %xpos2%, %ypos2%, 0xFF0000, 7, Fast ; Find Blue Button
				If (ErrorLevel = 1)	{
					tooltip, I found the Circle! 
					ClickAdjustBluY:= 2Py+=45	
					MouseMove, %2Px%, %ClickAdjustBluY%	; Used to test Mouse Click Position
					MouseClick, left, %2Px%, %ClickAdjustBluY% ; Click on Blue Button
					tooltip, I tried to click the Circle!
					break
							}
			Else		{
					tooltip, No Circle Found. Attempt %loopcount%
					}
				}
Any Thoughts?
Last edited by Drknyss on 23 Jan 2022, 11:26, edited 1 time in total.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by boiler » 23 Jan 2022, 11:02

Are you saying that you know that xblu1 and yblu are correct because you used them in another search? It seems odd that there is no “1” after yblu since the x variable is xblu1. Is that a typo?

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 23 Jan 2022, 11:29

That is a typo. My apologies. I have edited my Code in both posts above to a copy paste from the current version I am using.

But yea.. At the moment.. My Pixelsearch for the Blue seems to be doing nothing.. or finding something that isn't even remotely close to the blue color. Even if I change to

PixelSearch, 2Px, 2Py, %xblu1%, %yblu1%, %xpos2%, %ypos2%, 0xFF0000, 0, Fast ; Find Blue Button

Nothing at all seems to connect right for the click on the circle..

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by boiler » 23 Jan 2022, 11:53

I just watched the video for the first time, and it seems very likely that the blue dot is moving too fast for the script to click on the spot where it found it and it still be there. Maybe you have to do some predictive offset, although it would probably be 50-50 as to whether you should click to the left or right of it.

I suggest performing the same search on a static screen capture image when the blue dot is there and make sure it finds the color there as a first step toward troubleshooting.

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 23 Jan 2022, 15:15

I cannot believe I haven't tried this on a static image....

Anyway.. now I have.. and Something must be wrong with my search code for the blue.. even with a static image it does not find the blue circle. Trying to figure it out now.....

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by boiler » 23 Jan 2022, 15:27

One issue (and maybe the only remaining issue) is that you are saying you have found the circle if ErrorLevel is 1. It is found when ErrorLevel is 0.

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 23 Jan 2022, 15:50

Code: Select all

xpos1=
xpos2=
xblu1=
SetMouseDelay,-1
SetBatchLines, -1
Process, Priority,, High

InputBox, howmuchbait, Bait Amount,How much bait to use?, ; Input Amount of Bait to Use
tooltip, Put mouse in top left corner of pond and press F1
Return
f1::
If xpos1 =  
	{
	MouseGetPos, xpos1, ypos1
	tooltip, Put mouse in bottom right corner of pond and press F1
	Return
	}
If xpos2 =  
	{
	MouseGetPos, xpos2, ypos2
	; tooltip, Put mouse in the Left Middle of pond and press F1
	; Return
	}
tooltip, fishing will begin in 3 seconds. Press F2 to end early
sleep 3000
tooltip
loop	{
	sleep 500
	PixelSearch, 1Px, 1Py, %xpos1%, %ypos1%, %xpos2%, %ypos2%, 0x50452F, 5, Fast ; Find Fish
	if (ErrorLevel = 1)	
		{
		tooltip, No Fish Found
		}
	else if (ErrorLevel = 0)	
		{
		1Py=:= 1Py+=30
		MouseClick, left, %1Px%, %1Py% ; Click on Fish
		tooltip, Fish Found, Click
		fishcount++	; Increase Fish Count
		sleep 1000
			loop	{
			loopcount++ ; Displays a count on screen to verify search is progressing
			PixelSearch, 2Px, 2Py, %xpos1%, %ypos1%, %xpos2%, %ypos2%, 0xFF0000, 5, Fast ; Find Blue Button
				if (ErrorLevel = 1)	
					{
					tooltip, No Circle Found. Attempt %loopcount%
					}
				else if (ErrorLevel = 0)
					{
					tooltip, I found the Circle! 
					ClickAdjustBluY:= 2Py+=50	
					MouseMove, %2Px%, %ClickAdjustBluY%	; Used to test Mouse Click Position
					MouseClick, left, %2Px%, %ClickAdjustBluY% ; Click on Blue Button
					; tooltip, I tried to click the Circle!
					break
					}
			
				}
		sleep 500
		if fishcount = %howmuchbait%	; End Fishing if Fishcount = Bait Wanted to Use
			{
			break
			}
		}
	}	
	
f2::
Msgbox, I got you %fishcount% fish ; Display number of fish caught
reload	;exitapp
Well. I've made some progress and found a new issue... I have finally got the thing detecting the blue circle 100% of the time and clicking in the middle of it..

The strange issue I am running into now is that when searching for the fish.. Sometimes.. the mouse seems to end up outside of the designated search area.. which I didn't think could be possible.. so I'm not very confused.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by boiler » 23 Jan 2022, 16:04

Use MsgBox or ToolTip to see what coordinates it's finding so you can figure out why and where it's trying to click.

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 23 Jan 2022, 17:29

Trying to figure out the best way to do this for troubleshooting purposes...

Also trying to figure out the best way to consistently click the fish since the transparent nature of it gives it a different hue in every area...

Thanks for all the suggestions so far. Still working on it for sure.

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 23 Jan 2022, 20:47

Still can't quite figure out what is wrong....

i feel like sometimes I get the fish no issue..
Then all the sudden it doesnt work.

Other times I get the blue dot perfectly...

But nothing seems to be consistent...
Here is my test image:
Image

and here is my current code:

Code: Select all

xpos1=
xpos2=
SetMouseDelay,-1
SetBatchLines, -1
Process, Priority,, High

InputBox, howmuchbait, Bait Amount,How much bait to use?, ; Input Amount of Bait to Use
tooltip, Put mouse in top left corner of pond and press F1
Return
f1::
If xpos1 =  
	{
	MouseGetPos, xpos1, ypos1
	tooltip, Put mouse in bottom right corner of pond and press F1
	Return
	}
If xpos2 =  
	{
	MouseGetPos, xpos2, ypos2
	; tooltip, Put mouse in the Left Middle of pond and press F1
	; Return
	}
tooltip, fishing will begin in 3 seconds. Press F2 to end early
sleep 3000
tooltip
loop	{
	loop	{
		PixelSearch, 1Px, 1Py, %xpos1%, %ypos1%, %xpos2%, %ypos2%, 0x50452F, 20, Fast ; Find Fish
			if (ErrorLevel = 1)	
				{
				tooltip, No Fish Found
				}
			else if (ErrorLevel = 0)	
				{
				1Py=:= 1Py+=30
				MouseClick, left, %1Px%, %1Py% ; Click on Fish
				tooltip, Fish Found, Click
				fishcount++	; Increase Fish Count
				break
				}
		}
			sleep 1000
	loop	{
		loopcount++ ; Displays a count on screen to verify search is progressing
		PixelSearch, 2Px, 2Py, %xpos1%, %ypos1%, %xpos2%, %ypos2%, 0xFF0000, 5, Fast ; Find Blue Button
			if (ErrorLevel = 1)	
				{
				tooltip, No Circle Found. Attempt %loopcount%
				}
			else if (ErrorLevel = 0)
				{
				tooltip, I found the Circle! 
				2Py:= 2Py+=50	
				MouseMove, %2Px%, %2Py%	; Used to test Mouse Click Position
				MouseClick, left, %2Px%, %2Py% ; Click on Blue Button
				; tooltip, I tried to click the Circle!
				break
				}
			sleep 500
		}
	if fishcount = %howmuchbait%	; End Fishing if Fishcount = Bait Wanted to Use
	break
	}	
	
f2::
Msgbox, I got you %fishcount% fish ; Display number of fish caught
reload	;exitapp
if you test it.. when you run the code, you type the number of times you want it do the process in the box.
Press F1 in the top left corner of the lil pond area. Then press F1 in the bottom right corner of the pond area.

After 3 seconds it should begin to "fish". It should pixelscan and locate the fish. Click it. Then pixelscan and locate the circle, and click it.. and repeat until it has completed the number of times you input.

Anyone have any thoughts?

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by boiler » 23 Jan 2022, 21:19

My thoughts are that trying find and click on something that is moving around isn’t going to work 100% of the time. AutoHotkey is an interpreted language, so it’s not lightning fast. Sometimes it’s able to find the target and act in time and other times it’s not. Does it work 100% of the time now with static images? If so, then I think you can conclude that it misses sometimes when things are moving because they are moving too fast for it to always catch stuff.

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 23 Jan 2022, 22:12

Well I get that.. but it's not even working on a the static test image above at the moment. That's what I'm confused about.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by boiler » 23 Jan 2022, 22:19

If the colors you’re looking for are actually in that image, which you can check, then you have something fundamentally wrong with the script. It’s not an AHK capabilities problem. Since you provided the test image and the script, I can try to see what it is.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by boiler » 23 Jan 2022, 22:50

The only reason it wouldn't be consistent is that sometimes you are activating the window that the image is in and sometimes you're not. If you still had the File Explorer window as the active window when you press the F1 keys, then it's finding the coordinates relative to that window. Then the window that was active changes when you send a click and activate some other window, so it's not even based on the same reference point anymore. But if you activate the window the graphic is in, then it all stays relative to that window. So either add a command to activate the window that contains the graphic, or add these lines to the top of your script so it's always relative to the screen and it doesn't matter which window happens to be active at various points:

Code: Select all

CoordMode, Mouse, Screen
CoordMode, Pixel, Screen

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 23 Jan 2022, 23:03

I added those.

Strangely enough I'm still getting an issue where the Mouse is clicking at the very top of my display for the fish.. and never coming down to the Blue Circle..

The area where it is clicking is way out of the range I marked with the F1 inputs as well. And I cannot figure out why for the life of me.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by boiler » 23 Jan 2022, 23:25

This syntax is wrong and is causing 1Py to contain a string of characters instead of a numeric value, causing the problem:

Code: Select all

1Py=:= 1Py+=30
You are assigning the string := 1Py+=30 to 1Py because of the stray legacy assignment operator =.

Even if you didn't have the extra = sign to the left of the :=, you don't assign it like 1Py := 1Py+=30. That's redundant. It should be:

Code: Select all

1Py += 30
or:

Code: Select all

1Py := 1Py + 30
Both add 30 to 1Py.

The same for the other place you have it with 2Py (although that one doesn't have the extra = sign causing it to assign a string of text). Using both doesn't cause an issue, but it's just not using the operators as intended. What's causing the issue is the extra = sign you have in 1Py=:= 1Py+=30 as shown in red.
Last edited by boiler on 23 Jan 2022, 23:45, edited 4 times in total.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by boiler » 23 Jan 2022, 23:33

To demonstrate the above point, all three approaches below produce the same output, but there is no reason for the third one. It defeats the purpose of the shorthand += operator.

Code: Select all

x := 10
x += 30
MsgBox, % x

x := 10
x := x + 30
MsgBox, % x

x := 10
x := x += 30
MsgBox, % x

But you can see what was causing the problem in your script when you run the following:

Code: Select all

x := 10
x=:= x+=30
MsgBox, % x
That ends up having a null value numerically, which is why it was clicking off the window.

Drknyss
Posts: 14
Joined: 21 Jan 2022, 16:32

Re: Need Assistance Improving a Script to Automate Fishing on a Game in the browser.

Post by Drknyss » 24 Jan 2022, 15:35

That was it! Everything is working properly now! Thank you so much. I totally missed that.

I really appreciate your help and back and forth on all this. I'm still learning a lot here but It feels great to have something finally work like it should!

Thank you again!

Post Reply

Return to “Gaming Help (v1)”