nopeAnon / Automated-HoloCure-Fishing

Automated Holocure Fishing Bot
GNU General Public License v3.0
66 stars 32 forks source link

blackscreen debug, no matches- Win11 32:9 5120x1440p #36

Open tmmjelde opened 10 months ago

tmmjelde commented 10 months ago

Hi! Thanks for a great project. Unfortunately it didn't work for me out of the box :)

I'm on Windows 11 and with a big monitor (49 inch, 32:9 5120x1440p) I've tried with different python versions and OpenCV versions too- as a lot of googling tells me that OpenCV 4.2 doesn't have any black screen issues. Lies. All lies..

Not sure if my system specs matter or not, but I did see this was made for Windows 10.

Anyway, I enabled debug and I see the FPS counter, but the image capture has no relevant data in it. It's just black.

When fishing there is no keybuttons pressed. I know the output should show when buttons are pressed.

With regards to the black screen this post has helped me fix it. https://github.com/BoboTiG/python-mss/issues/180

The key is something to do with this line:

If Special K is running, this number is 3. If not, 1

result = windll.user32.PrintWindow(hwnd, save_dc.GetSafeHdc(), 3)

I don't know if this has a performance impact, and to be honest I have no idea what this does. However, changing the screencapture method out allowed the debug window to show some actual images of the game client.

After that I saw that nothing was triggering on the matchmaking. I changed the screencapture to display the same coordinates that the matchmaking was using, and then it showed which area of the game the matchmaking was happening. For me it seemed like the detection was happening on top of my monitor.

I checked older versions and found this "optimization" ;) https://github.com/nopeAnon/Automated-HoloCure-Fishing/commit/6ce92608c7de3d0a25c00b1596604fbe2b57f845 Reverting to the old matchmaking fixes the issue for me.

This could be related to my big monitor. Not sure. I've tried different game resolutions as well, 480p, 720p and 1080p and they all work now.

I've got a running version now, which runs at about 30fps (rtx3080) If I run the game in 640*480 then it's even faster and easily gets me to 250 consecutive fishes. If I run it at higher resolutions I've found that at least one time I missed a button, but I still got the fish.

However, another problem is python crashes after 5-10 fish. I believe it crashes because it fails to capture a screenshot - and then the code attempts to read/change a null variable. I bet a simple test could allow such mishaps to occur without crashing, but I've never really touched Python before and it's not really something I am super interested in. if (img has data){matchmaking}else{capture another screenshot}

Anyway, not wanting to dig further into the Python way of doing that I just modified the launch batch file. :start ... python ..... goto start

The script starts up fast enough to continue fishing, and usually when it crashes it's when I'm waiting for a fish to bite - so... I guess it's good enough for me.

I'm not sure what the best way for me to share my code is, but let me know and I'll gladly do so if you want it.

Ps. Really great job here. Let me know if you want me to test something or want me to share my horrible code :)

nopeAnon commented 10 months ago

I've tried implementing this

 # If Special K is running, this number is 3. If not, 1
    result = windll.user32.PrintWindow(hwnd, save_dc.GetSafeHdc(), 3)

but the fps went from 250-500 to 30-70. But it shouldn't matter too much since holocure is fixed at 60 fps.

diff --git a/holocure_fishing.py b/holocure_fishing.py
index f3cfc63..e2b5f10 100644
--- a/holocure_fishing.py
+++ b/holocure_fishing.py
@@ -12,7 +12,7 @@ import win32con, win32gui, win32ui
 from imgproc import templates, masks
 from send_input import press

-DEBUG = False
+DEBUG = True

 def main() -> None:
@@ -54,6 +54,8 @@ def main() -> None:
         scale = round(height / 360)
         roi = np.multiply(scale, BASE_ROI)
         img_src = capture_game(hwndMain, *roi)
+        if img_src is None:
+            continue
         # used to send input - a bit wasteful to call every loop though...
         win = win32ui.CreateWindowFromHandle(hwndMain)

@@ -172,6 +174,9 @@ def capture_game(hwnd, left: int, top: int, width: int, height: int):
     dataBitMap = win32ui.CreateBitmap()
     dataBitMap.CreateCompatibleBitmap(dcObj, width, height)
     cDC.SelectObject(dataBitMap)
+
+    result = ctypes.windll.user32.PrintWindow(hwnd, cDC.GetSafeHdc(), 3)
+
     cDC.BitBlt(
         (0, 0),
         (width, height),
@@ -188,7 +193,9 @@ def capture_game(hwnd, left: int, top: int, width: int, height: int):
     cDC.DeleteDC()
     win32gui.ReleaseDC(hwnd, wDC)
     win32gui.DeleteObject(dataBitMap.GetHandle())
-    return converted
+    if result == 1:
+        return converted
+    return None

 if __name__ == "__main__":

It should also fix python crashing, but I can't replicate the bug, so I can't test it if it works. If it's still crashing, please send the error message.

And please share your code if you can. We might learn something that might be causing the issue.

tmmjelde commented 10 months ago

So this is the error I have Traceback (most recent call last): File "holocure_fishing.py", line 244, in main() File "holocure_fishing.py", line 58, in main img_src = wincap.get_screenshot() File "C:\pythontest\org\Automated-HoloCure-Fishing-master\windowcapture.py", line 57, in get_screenshot save_dc = mfc_dc.CreateCompatibleDC() win32ui.error: CreateCompatibleDC failed

"My" code is this here - although it's really pieced together from everyone elses code. I didn't write anything noteworthy. holocure_fishing.zip

I patched something together from this guy: https://www.youtube.com/watch?v=WymCpVUPWQ4&list=PL1m2M8LQlzfKtkKq2lK5xko4X-8EZzFPI&index=4

Pretty interesting things he's done (classes), and he's also got a github.

However - his code also struggles with the black screen - so I've combined that with the other stuff. He claims there's a fix for black screen in vid 5 - but alas it does not solve the issue. However, some comment in a video pointed me to the real black screen fix, which I've implemented into his code, then took that and implemented it into your code ;) I didn't make notes along as I tried things unfortunately.

So anyway, I tried adding this to the right place: if img_src is None: continue

I already had something similar to this: if result == 1: return converted return None

Problem now is I don't get an error and the Source window just freezes after a while. No more images are captured, and hence no keypresses are sent.

I also noticed that when I first run the script in debug mode it starts off at 200 fps and around there, then it gradually lowers itself down to about 60 FPS before it crashes. Not sure if there might be some sort of leak somewhere to cause that or what.

With all I've said, I should be very up front and also say I don't know either Python or OpenCV. I'm a Powershell guy, but we don't have these nice OpenCV things. Your project got me interested in actually trying :)

I should also note that I changed the batch file to use my own Python version - so I'm running on 3.9.7 with OpenCV 4.2 (because someone said that would fix black screen - which it didn't and I'm lazy) If you want me to change that back I can do that no problem.

Hexus-One commented 10 months ago

win32ui.error: CreateCompatibleDC failed

I also noticed that when I first run the script in debug mode it starts off at 200 fps and around there, then it gradually lowers itself down to about 60 FPS before it crashes. Not sure if there might be some sort of leak somewhere to cause that or what.

Yep if i remember correctly there's a limit on how many Device Contexts (DC) there can be, so the function needs to release it safely in order to run continuously without crashing - so you need to make sure the relevant objects are deleted/released before you exit the function. I haven't looked at your code but my guess is that you have an early return/early exit somewhere without ReleaseDC and DeleteObject being called, so the program is accumulating DCs and hits the limit after some time.