@@ -48,19 +48,21 @@ class Autofish(CustomAction):
4848 else :
4949 image_dir = abs_path / "resource/base/image/auto_fish"
5050 continue_img = image_dir / "continue.png"
51- valid_region_img = image_dir / "valid_region.png"
51+ valid_region_left_img = image_dir / "valid_region_left.png"
52+ valid_region_right_img = image_dir / "valid_region_right.png"
5253 slider_img = image_dir / "slider.png"
5354 success_catch_img = image_dir / "success_catch.png"
5455
5556 slider_template = cv2 .imread (str (slider_img ), cv2 .IMREAD_COLOR )
56- valid_region_template = cv2 .imread (str (valid_region_img ), cv2 .IMREAD_COLOR )
57+ valid_region_left_template = cv2 .imread (str (valid_region_left_img ), cv2 .IMREAD_COLOR )
58+ valid_region_right_template = cv2 .imread (str (valid_region_right_img ), cv2 .IMREAD_COLOR )
5759 continue_template = cv2 .imread (str (continue_img ), cv2 .IMREAD_COLOR )
5860 success_catch_template = cv2 .imread (str (success_catch_img ), cv2 .IMREAD_COLOR )
5961
6062 def run (self , context : Context , argv : CustomAction .RunArg ) -> CustomAction .RunResult :
6163 print ("=== Autofish Action Started ===" )
6264 controller = context .tasker .controller
63-
65+
6466 fishing_count = 10
6567 check_freq = 0.001
6668 if argv .custom_action_param :
@@ -70,42 +72,41 @@ def run(self, context: Context, argv: CustomAction.RunArg) -> CustomAction.RunRe
7072 check_freq = params .get ("freq" , 0.1 )
7173 except :
7274 pass
73-
75+
7476 # Key codes for A, D, F, ESC
7577 KEY_A = 65
7678 KEY_D = 68
7779 KEY_F = 70
7880 KEY_ESC = 27
79-
81+
8082 # Original coordinates from autofish.py
8183 success_region = (520 , 160 , 785 , 190 )
8284 settlement_region = (564 , 642 , 1206 , 664 )
8385 game_region = (400 , 33 , 882 , 63 )
84-
85- for count in range (fishing_count ):
86- if context .tasker .stopping :
86+
87+ for i in range (fishing_count ):
88+ if context .tasker .stopping :
8789 return CustomAction .RunResult (success = False )
88- print (f"=== Fishing { count + 1 } /{ fishing_count } ===" )
89-
90+ print (f"=== Fishing { i + 1 } /{ fishing_count } ===" )
91+
9092 # 1. Clear settlement screen
9193 img = get_image (controller )
9294 match_settle , _ , _ , _ = match_template_in_region (img , settlement_region , self .continue_template , 0.8 )
9395 if match_settle :
94- print ("Found settlement screen, closing ..." )
96+ print (" Closing settlement screen..." )
9597 for _ in range (5 ):
9698 controller .post_key_down (KEY_ESC )
9799 time .sleep (0.1 )
98100 controller .post_key_up (KEY_ESC )
99101 time .sleep (1 )
100-
102+
101103 img = get_image (controller )
102104 m , _ , _ , _ = match_template_in_region (img , settlement_region , self .continue_template , 0.8 )
103105 if not m :
104- print ("Settlement closed." )
106+ print (" Settlement closed." )
105107 break
106-
107- # 2. Minigame loop
108- print ("Waiting for fish to bite and entering minigame..." )
108+
109+ # 2. Wait for fish to bite
109110 while True :
110111 controller .post_key_down (KEY_F )
111112 time .sleep (0.1 )
@@ -114,45 +115,65 @@ def run(self, context: Context, argv: CustomAction.RunArg) -> CustomAction.RunRe
114115 img = get_image (controller )
115116 m_catch , _ , _ , _ = match_template_in_region (img , success_region , self .success_catch_template , 0.8 )
116117 if m_catch :
117- print ("Fish caught! (Settlement screen appeared) " )
118+ print (" Fish hooked! " )
118119 break
119120
121+ # 3. Minigame: reel in and balance slider
120122 start_time = time .time ()
121-
122- count = 0
123+ frame = 0
124+ deadzone = 15
125+
123126 while time .time () - start_time < 100 :
124127 time .sleep (check_freq )
125128 img = get_image (controller )
126- count += 1
129+ frame += 1
127130
128- if count % 10 == 0 :
131+ if frame % 10 == 0 :
129132 m_settle , _ , _ , _ = match_template_in_region (img , settlement_region , self .continue_template , 0.8 )
130133 if m_settle :
131- print ("Fish caught! (Settlement screen appeared) " )
134+ print (" Fish caught!" )
132135 break
133-
134- m_region , _ , x_region , _ = match_template_in_region (img , game_region , self .valid_region_template , 0.7 )
136+
137+ m_left , _ , x_left , _ = match_template_in_region (img , game_region , self .valid_region_left_template , 0.7 )
138+ m_right , _ , x_right , _ = match_template_in_region (img , game_region , self .valid_region_right_template , 0.7 )
135139 m_slider , _ , x_slider , _ = match_template_in_region (img , game_region , self .slider_template , 0.7 )
136140
137- if m_region and m_slider :
138- # Reel in
139- controller .post_key_down (KEY_F )
140- time .sleep (0.05 )
141- controller .post_key_up (KEY_F )
142-
141+ if m_slider :
142+ # Reel in (throttled)
143+ if frame % 10 == 0 :
144+ controller .post_key_down (KEY_F )
145+ time .sleep (0.05 )
146+ controller .post_key_up (KEY_F )
147+
143148 # Balance slider
144- if x_slider > x_region + 20 :
145- controller .post_key_up (KEY_D )
146- controller .post_key_down (KEY_A )
149+ if m_left and m_right :
150+ target = (x_left + x_right ) / 2
151+ offset = x_slider - target
152+ elif not m_left and m_right :
153+ target = x_right
154+ offset = x_slider - target
155+ elif m_left and not m_right :
156+ target = x_left
157+ offset = x_slider - target
147158 else :
148- controller .post_key_up (KEY_A )
149- controller .post_key_down (KEY_D )
150-
159+ offset = 0
160+
161+ if m_left or m_right :
162+ if offset > deadzone :
163+ controller .post_key_up (KEY_D )
164+ controller .post_key_down (KEY_A )
165+ elif offset < - deadzone :
166+ controller .post_key_up (KEY_A )
167+ controller .post_key_down (KEY_D )
168+ else :
169+ controller .post_key_up (KEY_A )
170+ controller .post_key_up (KEY_D )
171+
151172 # Release all keys
152173 controller .post_key_up (KEY_D )
153174 controller .post_key_up (KEY_A )
154175 controller .post_key_up (KEY_F )
155- print ("Current iteration finished ." )
156-
176+ print (" Finished ." )
177+
157178 print ("All fishing tasks complete." )
158179 return CustomAction .RunResult (success = True )
0 commit comments